autogain_functions.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Functions for auto gain.
  3. *
  4. * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>
  5. *
  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.
  10. *
  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.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "gspca.h"
  21. /* auto gain and exposure algorithm based on the knee algorithm described here:
  22. http://ytse.tricolour.net/docs/LowLightOptimization.html
  23. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  24. where changed. */
  25. int gspca_expo_autogain(
  26. struct gspca_dev *gspca_dev,
  27. int avg_lum,
  28. int desired_avg_lum,
  29. int deadzone,
  30. int gain_knee,
  31. int exposure_knee)
  32. {
  33. s32 gain, orig_gain, exposure, orig_exposure;
  34. int i, steps, retval = 0;
  35. if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
  36. return 0;
  37. orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
  38. orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
  39. /* If we are of a multiple of deadzone, do multiple steps to reach the
  40. desired lumination fast (with the risc of a slight overshoot) */
  41. steps = abs(desired_avg_lum - avg_lum) / deadzone;
  42. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
  43. avg_lum, desired_avg_lum, steps);
  44. for (i = 0; i < steps; i++) {
  45. if (avg_lum > desired_avg_lum) {
  46. if (gain > gain_knee)
  47. gain--;
  48. else if (exposure > exposure_knee)
  49. exposure--;
  50. else if (gain > gspca_dev->gain->default_value)
  51. gain--;
  52. else if (exposure > gspca_dev->exposure->minimum)
  53. exposure--;
  54. else if (gain > gspca_dev->gain->minimum)
  55. gain--;
  56. else
  57. break;
  58. } else {
  59. if (gain < gspca_dev->gain->default_value)
  60. gain++;
  61. else if (exposure < exposure_knee)
  62. exposure++;
  63. else if (gain < gain_knee)
  64. gain++;
  65. else if (exposure < gspca_dev->exposure->maximum)
  66. exposure++;
  67. else if (gain < gspca_dev->gain->maximum)
  68. gain++;
  69. else
  70. break;
  71. }
  72. }
  73. if (gain != orig_gain) {
  74. v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
  75. retval = 1;
  76. }
  77. if (exposure != orig_exposure) {
  78. v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
  79. retval = 1;
  80. }
  81. if (retval)
  82. PDEBUG(D_FRAM, "autogain: changed gain: %d, expo: %d",
  83. gain, exposure);
  84. return retval;
  85. }
  86. EXPORT_SYMBOL(gspca_expo_autogain);
  87. /* Autogain + exposure algorithm for cameras with a coarse exposure control
  88. (usually this means we can only control the clockdiv to change exposure)
  89. As changing the clockdiv so that the fps drops from 30 to 15 fps for
  90. example, will lead to a huge exposure change (it effectively doubles),
  91. this algorithm normally tries to only adjust the gain (between 40 and
  92. 80 %) and if that does not help, only then changes exposure. This leads
  93. to a much more stable image then using the knee algorithm which at
  94. certain points of the knee graph will only try to adjust exposure,
  95. which leads to oscilating as one exposure step is huge.
  96. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  97. where changed. */
  98. int gspca_coarse_grained_expo_autogain(
  99. struct gspca_dev *gspca_dev,
  100. int avg_lum,
  101. int desired_avg_lum,
  102. int deadzone)
  103. {
  104. s32 gain_low, gain_high, gain, orig_gain, exposure, orig_exposure;
  105. int steps, retval = 0;
  106. if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
  107. return 0;
  108. orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
  109. orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
  110. gain_low = (s32)(gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
  111. 5 * 2 + gspca_dev->gain->minimum;
  112. gain_high = (s32)(gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
  113. 5 * 4 + gspca_dev->gain->minimum;
  114. /* If we are of a multiple of deadzone, do multiple steps to reach the
  115. desired lumination fast (with the risc of a slight overshoot) */
  116. steps = (desired_avg_lum - avg_lum) / deadzone;
  117. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
  118. avg_lum, desired_avg_lum, steps);
  119. if ((gain + steps) > gain_high &&
  120. exposure < gspca_dev->exposure->maximum) {
  121. gain = gain_high;
  122. gspca_dev->exp_too_low_cnt++;
  123. gspca_dev->exp_too_high_cnt = 0;
  124. } else if ((gain + steps) < gain_low &&
  125. exposure > gspca_dev->exposure->minimum) {
  126. gain = gain_low;
  127. gspca_dev->exp_too_high_cnt++;
  128. gspca_dev->exp_too_low_cnt = 0;
  129. } else {
  130. gain += steps;
  131. if (gain > gspca_dev->gain->maximum)
  132. gain = gspca_dev->gain->maximum;
  133. else if (gain < gspca_dev->gain->minimum)
  134. gain = gspca_dev->gain->minimum;
  135. gspca_dev->exp_too_high_cnt = 0;
  136. gspca_dev->exp_too_low_cnt = 0;
  137. }
  138. if (gspca_dev->exp_too_high_cnt > 3) {
  139. exposure--;
  140. gspca_dev->exp_too_high_cnt = 0;
  141. } else if (gspca_dev->exp_too_low_cnt > 3) {
  142. exposure++;
  143. gspca_dev->exp_too_low_cnt = 0;
  144. }
  145. if (gain != orig_gain) {
  146. v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
  147. retval = 1;
  148. }
  149. if (exposure != orig_exposure) {
  150. v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
  151. retval = 1;
  152. }
  153. if (retval)
  154. PDEBUG(D_FRAM, "autogain: changed gain: %d, expo: %d",
  155. gain, exposure);
  156. return retval;
  157. }
  158. EXPORT_SYMBOL(gspca_coarse_grained_expo_autogain);