2 * Functions for auto gain.
4 * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 /* auto gain and exposure algorithm based on the knee algorithm described here:
19 http://ytse.tricolour.net/docs/LowLightOptimization.html
21 Returns 0 if no changes were made, 1 if the gain and or exposure settings
23 int gspca_expo_autogain(
24 struct gspca_dev
*gspca_dev
,
31 s32 gain
, orig_gain
, exposure
, orig_exposure
;
32 int i
, steps
, retval
= 0;
34 if (v4l2_ctrl_g_ctrl(gspca_dev
->autogain
) == 0)
37 orig_gain
= gain
= v4l2_ctrl_g_ctrl(gspca_dev
->gain
);
38 orig_exposure
= exposure
= v4l2_ctrl_g_ctrl(gspca_dev
->exposure
);
40 /* If we are of a multiple of deadzone, do multiple steps to reach the
41 desired lumination fast (with the risc of a slight overshoot) */
42 steps
= abs(desired_avg_lum
- avg_lum
) / deadzone
;
44 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
45 avg_lum
, desired_avg_lum
, steps
);
47 for (i
= 0; i
< steps
; i
++) {
48 if (avg_lum
> desired_avg_lum
) {
51 else if (exposure
> exposure_knee
)
53 else if (gain
> gspca_dev
->gain
->default_value
)
55 else if (exposure
> gspca_dev
->exposure
->minimum
)
57 else if (gain
> gspca_dev
->gain
->minimum
)
62 if (gain
< gspca_dev
->gain
->default_value
)
64 else if (exposure
< exposure_knee
)
66 else if (gain
< gain_knee
)
68 else if (exposure
< gspca_dev
->exposure
->maximum
)
70 else if (gain
< gspca_dev
->gain
->maximum
)
77 if (gain
!= orig_gain
) {
78 v4l2_ctrl_s_ctrl(gspca_dev
->gain
, gain
);
81 if (exposure
!= orig_exposure
) {
82 v4l2_ctrl_s_ctrl(gspca_dev
->exposure
, exposure
);
87 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: changed gain: %d, expo: %d\n",
91 EXPORT_SYMBOL(gspca_expo_autogain
);
93 /* Autogain + exposure algorithm for cameras with a coarse exposure control
94 (usually this means we can only control the clockdiv to change exposure)
95 As changing the clockdiv so that the fps drops from 30 to 15 fps for
96 example, will lead to a huge exposure change (it effectively doubles),
97 this algorithm normally tries to only adjust the gain (between 40 and
98 80 %) and if that does not help, only then changes exposure. This leads
99 to a much more stable image then using the knee algorithm which at
100 certain points of the knee graph will only try to adjust exposure,
101 which leads to oscilating as one exposure step is huge.
103 Returns 0 if no changes were made, 1 if the gain and or exposure settings
105 int gspca_coarse_grained_expo_autogain(
106 struct gspca_dev
*gspca_dev
,
111 s32 gain_low
, gain_high
, gain
, orig_gain
, exposure
, orig_exposure
;
112 int steps
, retval
= 0;
114 if (v4l2_ctrl_g_ctrl(gspca_dev
->autogain
) == 0)
117 orig_gain
= gain
= v4l2_ctrl_g_ctrl(gspca_dev
->gain
);
118 orig_exposure
= exposure
= v4l2_ctrl_g_ctrl(gspca_dev
->exposure
);
120 gain_low
= (s32
)(gspca_dev
->gain
->maximum
- gspca_dev
->gain
->minimum
) /
121 5 * 2 + gspca_dev
->gain
->minimum
;
122 gain_high
= (s32
)(gspca_dev
->gain
->maximum
- gspca_dev
->gain
->minimum
) /
123 5 * 4 + gspca_dev
->gain
->minimum
;
125 /* If we are of a multiple of deadzone, do multiple steps to reach the
126 desired lumination fast (with the risc of a slight overshoot) */
127 steps
= (desired_avg_lum
- avg_lum
) / deadzone
;
129 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: lum: %d, desired: %d, steps: %d\n",
130 avg_lum
, desired_avg_lum
, steps
);
132 if ((gain
+ steps
) > gain_high
&&
133 exposure
< gspca_dev
->exposure
->maximum
) {
135 gspca_dev
->exp_too_low_cnt
++;
136 gspca_dev
->exp_too_high_cnt
= 0;
137 } else if ((gain
+ steps
) < gain_low
&&
138 exposure
> gspca_dev
->exposure
->minimum
) {
140 gspca_dev
->exp_too_high_cnt
++;
141 gspca_dev
->exp_too_low_cnt
= 0;
144 if (gain
> gspca_dev
->gain
->maximum
)
145 gain
= gspca_dev
->gain
->maximum
;
146 else if (gain
< gspca_dev
->gain
->minimum
)
147 gain
= gspca_dev
->gain
->minimum
;
148 gspca_dev
->exp_too_high_cnt
= 0;
149 gspca_dev
->exp_too_low_cnt
= 0;
152 if (gspca_dev
->exp_too_high_cnt
> 3) {
154 gspca_dev
->exp_too_high_cnt
= 0;
155 } else if (gspca_dev
->exp_too_low_cnt
> 3) {
157 gspca_dev
->exp_too_low_cnt
= 0;
160 if (gain
!= orig_gain
) {
161 v4l2_ctrl_s_ctrl(gspca_dev
->gain
, gain
);
164 if (exposure
!= orig_exposure
) {
165 v4l2_ctrl_s_ctrl(gspca_dev
->exposure
, exposure
);
170 gspca_dbg(gspca_dev
, D_FRAM
, "autogain: changed gain: %d, expo: %d\n",
174 EXPORT_SYMBOL(gspca_coarse_grained_expo_autogain
);