autogain_functions.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Functions for auto gain.
  3. *
  4. * Copyright (C) 2010-2011 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. /* auto gain and exposure algorithm based on the knee algorithm described here:
  21. http://ytse.tricolour.net/docs/LowLightOptimization.html
  22. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  23. where changed. */
  24. static inline int auto_gain_n_exposure(
  25. struct gspca_dev *gspca_dev,
  26. int avg_lum,
  27. int desired_avg_lum,
  28. int deadzone,
  29. int gain_knee,
  30. int exposure_knee)
  31. {
  32. struct sd *sd = (struct sd *) gspca_dev;
  33. int i, steps, gain, orig_gain, exposure, orig_exposure;
  34. int retval = 0;
  35. orig_gain = gain = sd->ctrls[GAIN].val;
  36. orig_exposure = exposure = sd->ctrls[EXPOSURE].val;
  37. /* If we are of a multiple of deadzone, do multiple steps to reach the
  38. desired lumination fast (with the risc of a slight overshoot) */
  39. steps = abs(desired_avg_lum - avg_lum) / deadzone;
  40. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
  41. avg_lum, desired_avg_lum, steps);
  42. for (i = 0; i < steps; i++) {
  43. if (avg_lum > desired_avg_lum) {
  44. if (gain > gain_knee)
  45. gain--;
  46. else if (exposure > exposure_knee)
  47. exposure--;
  48. else if (gain > sd->ctrls[GAIN].def)
  49. gain--;
  50. else if (exposure > sd->ctrls[EXPOSURE].min)
  51. exposure--;
  52. else if (gain > sd->ctrls[GAIN].min)
  53. gain--;
  54. else
  55. break;
  56. } else {
  57. if (gain < sd->ctrls[GAIN].def)
  58. gain++;
  59. else if (exposure < exposure_knee)
  60. exposure++;
  61. else if (gain < gain_knee)
  62. gain++;
  63. else if (exposure < sd->ctrls[EXPOSURE].max)
  64. exposure++;
  65. else if (gain < sd->ctrls[GAIN].max)
  66. gain++;
  67. else
  68. break;
  69. }
  70. }
  71. if (gain != orig_gain) {
  72. sd->ctrls[GAIN].val = gain;
  73. setgain(gspca_dev);
  74. retval = 1;
  75. }
  76. if (exposure != orig_exposure) {
  77. sd->ctrls[EXPOSURE].val = exposure;
  78. setexposure(gspca_dev);
  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. /* Autogain + exposure algorithm for cameras with a coarse exposure control
  87. (usually this means we can only control the clockdiv to change exposure)
  88. As changing the clockdiv so that the fps drops from 30 to 15 fps for
  89. example, will lead to a huge exposure change (it effectively doubles),
  90. this algorithm normally tries to only adjust the gain (between 40 and
  91. 80 %) and if that does not help, only then changes exposure. This leads
  92. to a much more stable image then using the knee algorithm which at
  93. certain points of the knee graph will only try to adjust exposure,
  94. which leads to oscilating as one exposure step is huge.
  95. Note this assumes that the sd struct for the cam in question has
  96. exp_too_high_cnt and exp_too_high_cnt int members for use by this function.
  97. Returns 0 if no changes were made, 1 if the gain and or exposure settings
  98. where changed. */
  99. static inline int coarse_grained_expo_autogain(
  100. struct gspca_dev *gspca_dev,
  101. int avg_lum,
  102. int desired_avg_lum,
  103. int deadzone)
  104. {
  105. struct sd *sd = (struct sd *) gspca_dev;
  106. int steps, gain, orig_gain, exposure, orig_exposure;
  107. int gain_low, gain_high;
  108. int retval = 0;
  109. orig_gain = gain = sd->ctrls[GAIN].val;
  110. orig_exposure = exposure = sd->ctrls[EXPOSURE].val;
  111. gain_low = (sd->ctrls[GAIN].max - sd->ctrls[GAIN].min) / 5 * 2;
  112. gain_low += sd->ctrls[GAIN].min;
  113. gain_high = (sd->ctrls[GAIN].max - sd->ctrls[GAIN].min) / 5 * 4;
  114. gain_high += sd->ctrls[GAIN].min;
  115. /* If we are of a multiple of deadzone, do multiple steps to reach the
  116. desired lumination fast (with the risc of a slight overshoot) */
  117. steps = (desired_avg_lum - avg_lum) / deadzone;
  118. PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
  119. avg_lum, desired_avg_lum, steps);
  120. if ((gain + steps) > gain_high &&
  121. exposure < sd->ctrls[EXPOSURE].max) {
  122. gain = gain_high;
  123. sd->exp_too_low_cnt++;
  124. sd->exp_too_high_cnt = 0;
  125. } else if ((gain + steps) < gain_low &&
  126. exposure > sd->ctrls[EXPOSURE].min) {
  127. gain = gain_low;
  128. sd->exp_too_high_cnt++;
  129. sd->exp_too_low_cnt = 0;
  130. } else {
  131. gain += steps;
  132. if (gain > sd->ctrls[GAIN].max)
  133. gain = sd->ctrls[GAIN].max;
  134. else if (gain < sd->ctrls[GAIN].min)
  135. gain = sd->ctrls[GAIN].min;
  136. sd->exp_too_high_cnt = 0;
  137. sd->exp_too_low_cnt = 0;
  138. }
  139. if (sd->exp_too_high_cnt > 3) {
  140. exposure--;
  141. sd->exp_too_high_cnt = 0;
  142. } else if (sd->exp_too_low_cnt > 3) {
  143. exposure++;
  144. sd->exp_too_low_cnt = 0;
  145. }
  146. if (gain != orig_gain) {
  147. sd->ctrls[GAIN].val = gain;
  148. setgain(gspca_dev);
  149. retval = 1;
  150. }
  151. if (exposure != orig_exposure) {
  152. sd->ctrls[EXPOSURE].val = exposure;
  153. setexposure(gspca_dev);
  154. retval = 1;
  155. }
  156. if (retval)
  157. PDEBUG(D_FRAM, "autogain: changed gain: %d, expo: %d",
  158. gain, exposure);
  159. return retval;
  160. }