blob: 7ae7c43951cd89f573f15a12ae3d7e2db9145b44 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans de Goede8cd05842012-05-09 09:58:33 -03002/*
3 * Functions for auto gain.
4 *
5 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>
Hans de Goede8cd05842012-05-09 09:58:33 -03006 */
7#include "gspca.h"
8
9/* auto gain and exposure algorithm based on the knee algorithm described here:
10 http://ytse.tricolour.net/docs/LowLightOptimization.html
11
12 Returns 0 if no changes were made, 1 if the gain and or exposure settings
13 where changed. */
14int gspca_expo_autogain(
15 struct gspca_dev *gspca_dev,
16 int avg_lum,
17 int desired_avg_lum,
18 int deadzone,
19 int gain_knee,
20 int exposure_knee)
21{
22 s32 gain, orig_gain, exposure, orig_exposure;
23 int i, steps, retval = 0;
24
25 if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050026 return 0;
Hans de Goede8cd05842012-05-09 09:58:33 -030027
28 orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
29 orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
30
31 /* If we are of a multiple of deadzone, do multiple steps to reach the
32 desired lumination fast (with the risc of a slight overshoot) */
33 steps = abs(desired_avg_lum - avg_lum) / deadzone;
34
Joe Perches37d5efb2017-09-22 15:20:33 -040035 gspca_dbg(gspca_dev, D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
36 avg_lum, desired_avg_lum, steps);
Hans de Goede8cd05842012-05-09 09:58:33 -030037
38 for (i = 0; i < steps; i++) {
39 if (avg_lum > desired_avg_lum) {
40 if (gain > gain_knee)
41 gain--;
42 else if (exposure > exposure_knee)
43 exposure--;
44 else if (gain > gspca_dev->gain->default_value)
45 gain--;
46 else if (exposure > gspca_dev->exposure->minimum)
47 exposure--;
48 else if (gain > gspca_dev->gain->minimum)
49 gain--;
50 else
51 break;
52 } else {
53 if (gain < gspca_dev->gain->default_value)
54 gain++;
55 else if (exposure < exposure_knee)
56 exposure++;
57 else if (gain < gain_knee)
58 gain++;
59 else if (exposure < gspca_dev->exposure->maximum)
60 exposure++;
61 else if (gain < gspca_dev->gain->maximum)
62 gain++;
63 else
64 break;
65 }
66 }
67
68 if (gain != orig_gain) {
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050069 v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
Hans de Goede8cd05842012-05-09 09:58:33 -030070 retval = 1;
71 }
72 if (exposure != orig_exposure) {
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -050073 v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
Hans de Goede8cd05842012-05-09 09:58:33 -030074 retval = 1;
75 }
76
77 if (retval)
Joe Perches37d5efb2017-09-22 15:20:33 -040078 gspca_dbg(gspca_dev, D_FRAM, "autogain: changed gain: %d, expo: %d\n",
79 gain, exposure);
Hans de Goede8cd05842012-05-09 09:58:33 -030080 return retval;
81}
82EXPORT_SYMBOL(gspca_expo_autogain);
83
84/* Autogain + exposure algorithm for cameras with a coarse exposure control
85 (usually this means we can only control the clockdiv to change exposure)
86 As changing the clockdiv so that the fps drops from 30 to 15 fps for
87 example, will lead to a huge exposure change (it effectively doubles),
88 this algorithm normally tries to only adjust the gain (between 40 and
89 80 %) and if that does not help, only then changes exposure. This leads
90 to a much more stable image then using the knee algorithm which at
91 certain points of the knee graph will only try to adjust exposure,
Mauro Carvalho Chehab3e4d8f42019-02-18 14:29:03 -050092 which leads to oscillating as one exposure step is huge.
Hans de Goede8cd05842012-05-09 09:58:33 -030093
94 Returns 0 if no changes were made, 1 if the gain and or exposure settings
95 where changed. */
96int gspca_coarse_grained_expo_autogain(
97 struct gspca_dev *gspca_dev,
98 int avg_lum,
99 int desired_avg_lum,
100 int deadzone)
101{
102 s32 gain_low, gain_high, gain, orig_gain, exposure, orig_exposure;
103 int steps, retval = 0;
104
105 if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500106 return 0;
Hans de Goede8cd05842012-05-09 09:58:33 -0300107
108 orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
109 orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
110
Hans Verkuil0d5e8c42014-07-17 12:31:23 -0300111 gain_low = (s32)(gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
Hans de Goede8cd05842012-05-09 09:58:33 -0300112 5 * 2 + gspca_dev->gain->minimum;
Hans Verkuil0d5e8c42014-07-17 12:31:23 -0300113 gain_high = (s32)(gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
Hans de Goede8cd05842012-05-09 09:58:33 -0300114 5 * 4 + gspca_dev->gain->minimum;
115
116 /* If we are of a multiple of deadzone, do multiple steps to reach the
117 desired lumination fast (with the risc of a slight overshoot) */
118 steps = (desired_avg_lum - avg_lum) / deadzone;
119
Joe Perches37d5efb2017-09-22 15:20:33 -0400120 gspca_dbg(gspca_dev, D_FRAM, "autogain: lum: %d, desired: %d, steps: %d\n",
121 avg_lum, desired_avg_lum, steps);
Hans de Goede8cd05842012-05-09 09:58:33 -0300122
123 if ((gain + steps) > gain_high &&
124 exposure < gspca_dev->exposure->maximum) {
125 gain = gain_high;
126 gspca_dev->exp_too_low_cnt++;
127 gspca_dev->exp_too_high_cnt = 0;
128 } else if ((gain + steps) < gain_low &&
129 exposure > gspca_dev->exposure->minimum) {
130 gain = gain_low;
131 gspca_dev->exp_too_high_cnt++;
132 gspca_dev->exp_too_low_cnt = 0;
133 } else {
134 gain += steps;
135 if (gain > gspca_dev->gain->maximum)
136 gain = gspca_dev->gain->maximum;
137 else if (gain < gspca_dev->gain->minimum)
138 gain = gspca_dev->gain->minimum;
139 gspca_dev->exp_too_high_cnt = 0;
140 gspca_dev->exp_too_low_cnt = 0;
141 }
142
143 if (gspca_dev->exp_too_high_cnt > 3) {
144 exposure--;
145 gspca_dev->exp_too_high_cnt = 0;
146 } else if (gspca_dev->exp_too_low_cnt > 3) {
147 exposure++;
148 gspca_dev->exp_too_low_cnt = 0;
149 }
150
151 if (gain != orig_gain) {
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500152 v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
Hans de Goede8cd05842012-05-09 09:58:33 -0300153 retval = 1;
154 }
155 if (exposure != orig_exposure) {
Mauro Carvalho Chehab4a3fad72018-01-04 06:47:28 -0500156 v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
Hans de Goede8cd05842012-05-09 09:58:33 -0300157 retval = 1;
158 }
159
160 if (retval)
Joe Perches37d5efb2017-09-22 15:20:33 -0400161 gspca_dbg(gspca_dev, D_FRAM, "autogain: changed gain: %d, expo: %d\n",
162 gain, exposure);
Hans de Goede8cd05842012-05-09 09:58:33 -0300163 return retval;
164}
165EXPORT_SYMBOL(gspca_coarse_grained_expo_autogain);