autogain_functions.c 4.9 KB

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