[media] gspca: Add autogain functions for use with control framework drivers
authorHans de Goede <hdegoede@redhat.com>
Wed, 9 May 2012 12:58:33 +0000 (09:58 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Mon, 14 May 2012 12:33:15 +0000 (09:33 -0300)
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
drivers/media/video/gspca/Makefile
drivers/media/video/gspca/autogain_functions.c [new file with mode: 0644]
drivers/media/video/gspca/gspca.h

index 79ebe46e1ad792b80d3a013aa9efba786ea82d9f..c901da0bd657a0bda2883974ce29ae31aa84a789 100644 (file)
@@ -43,7 +43,7 @@ obj-$(CONFIG_USB_GSPCA_VICAM)    += gspca_vicam.o
 obj-$(CONFIG_USB_GSPCA_XIRLINK_CIT) += gspca_xirlink_cit.o
 obj-$(CONFIG_USB_GSPCA_ZC3XX)    += gspca_zc3xx.o
 
-gspca_main-objs     := gspca.o
+gspca_main-objs     := gspca.o autogain_functions.o
 gspca_benq-objs     := benq.o
 gspca_conex-objs    := conex.o
 gspca_cpia1-objs    := cpia1.o
diff --git a/drivers/media/video/gspca/autogain_functions.c b/drivers/media/video/gspca/autogain_functions.c
new file mode 100644 (file)
index 0000000..67db674
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+ * Functions for auto gain.
+ *
+ * Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include "gspca.h"
+
+/* auto gain and exposure algorithm based on the knee algorithm described here:
+   http://ytse.tricolour.net/docs/LowLightOptimization.html
+
+   Returns 0 if no changes were made, 1 if the gain and or exposure settings
+   where changed. */
+int gspca_expo_autogain(
+                       struct gspca_dev *gspca_dev,
+                       int avg_lum,
+                       int desired_avg_lum,
+                       int deadzone,
+                       int gain_knee,
+                       int exposure_knee)
+{
+       s32 gain, orig_gain, exposure, orig_exposure;
+       int i, steps, retval = 0;
+
+       if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
+               return 0;
+
+       orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
+       orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
+
+       /* If we are of a multiple of deadzone, do multiple steps to reach the
+          desired lumination fast (with the risc of a slight overshoot) */
+       steps = abs(desired_avg_lum - avg_lum) / deadzone;
+
+       PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
+               avg_lum, desired_avg_lum, steps);
+
+       for (i = 0; i < steps; i++) {
+               if (avg_lum > desired_avg_lum) {
+                       if (gain > gain_knee)
+                               gain--;
+                       else if (exposure > exposure_knee)
+                               exposure--;
+                       else if (gain > gspca_dev->gain->default_value)
+                               gain--;
+                       else if (exposure > gspca_dev->exposure->minimum)
+                               exposure--;
+                       else if (gain > gspca_dev->gain->minimum)
+                               gain--;
+                       else
+                               break;
+               } else {
+                       if (gain < gspca_dev->gain->default_value)
+                               gain++;
+                       else if (exposure < exposure_knee)
+                               exposure++;
+                       else if (gain < gain_knee)
+                               gain++;
+                       else if (exposure < gspca_dev->exposure->maximum)
+                               exposure++;
+                       else if (gain < gspca_dev->gain->maximum)
+                               gain++;
+                       else
+                               break;
+               }
+       }
+
+       if (gain != orig_gain) {
+               v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
+               retval = 1;
+       }
+       if (exposure != orig_exposure) {
+               v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
+               retval = 1;
+       }
+
+       if (retval)
+               PDEBUG(D_FRAM, "autogain: changed gain: %d, expo: %d",
+                       gain, exposure);
+       return retval;
+}
+EXPORT_SYMBOL(gspca_expo_autogain);
+
+/* Autogain + exposure algorithm for cameras with a coarse exposure control
+   (usually this means we can only control the clockdiv to change exposure)
+   As changing the clockdiv so that the fps drops from 30 to 15 fps for
+   example, will lead to a huge exposure change (it effectively doubles),
+   this algorithm normally tries to only adjust the gain (between 40 and
+   80 %) and if that does not help, only then changes exposure. This leads
+   to a much more stable image then using the knee algorithm which at
+   certain points of the knee graph will only try to adjust exposure,
+   which leads to oscilating as one exposure step is huge.
+
+   Returns 0 if no changes were made, 1 if the gain and or exposure settings
+   where changed. */
+int gspca_coarse_grained_expo_autogain(
+                       struct gspca_dev *gspca_dev,
+                       int avg_lum,
+                       int desired_avg_lum,
+                       int deadzone)
+{
+       s32 gain_low, gain_high, gain, orig_gain, exposure, orig_exposure;
+       int steps, retval = 0;
+
+       if (v4l2_ctrl_g_ctrl(gspca_dev->autogain) == 0)
+               return 0;
+
+       orig_gain = gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
+       orig_exposure = exposure = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
+
+       gain_low  = (gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
+                   5 * 2 + gspca_dev->gain->minimum;
+       gain_high = (gspca_dev->gain->maximum - gspca_dev->gain->minimum) /
+                   5 * 4 + gspca_dev->gain->minimum;
+
+       /* If we are of a multiple of deadzone, do multiple steps to reach the
+          desired lumination fast (with the risc of a slight overshoot) */
+       steps = (desired_avg_lum - avg_lum) / deadzone;
+
+       PDEBUG(D_FRAM, "autogain: lum: %d, desired: %d, steps: %d",
+               avg_lum, desired_avg_lum, steps);
+
+       if ((gain + steps) > gain_high &&
+           exposure < gspca_dev->exposure->maximum) {
+               gain = gain_high;
+               gspca_dev->exp_too_low_cnt++;
+               gspca_dev->exp_too_high_cnt = 0;
+       } else if ((gain + steps) < gain_low &&
+                  exposure > gspca_dev->exposure->minimum) {
+               gain = gain_low;
+               gspca_dev->exp_too_high_cnt++;
+               gspca_dev->exp_too_low_cnt = 0;
+       } else {
+               gain += steps;
+               if (gain > gspca_dev->gain->maximum)
+                       gain = gspca_dev->gain->maximum;
+               else if (gain < gspca_dev->gain->minimum)
+                       gain = gspca_dev->gain->minimum;
+               gspca_dev->exp_too_high_cnt = 0;
+               gspca_dev->exp_too_low_cnt = 0;
+       }
+
+       if (gspca_dev->exp_too_high_cnt > 3) {
+               exposure--;
+               gspca_dev->exp_too_high_cnt = 0;
+       } else if (gspca_dev->exp_too_low_cnt > 3) {
+               exposure++;
+               gspca_dev->exp_too_low_cnt = 0;
+       }
+
+       if (gain != orig_gain) {
+               v4l2_ctrl_s_ctrl(gspca_dev->gain, gain);
+               retval = 1;
+       }
+       if (exposure != orig_exposure) {
+               v4l2_ctrl_s_ctrl(gspca_dev->exposure, exposure);
+               retval = 1;
+       }
+
+       if (retval)
+               PDEBUG(D_FRAM, "autogain: changed gain: %d, expo: %d",
+                       gain, exposure);
+       return retval;
+}
+EXPORT_SYMBOL(gspca_coarse_grained_expo_autogain);
index 436d881ce4433c599714d9314e17a437b27b0f88..449ff8e37fe77e8b389e8f043c0b67f797458b05 100644 (file)
@@ -6,6 +6,7 @@
 #include <linux/usb.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <linux/mutex.h>
 
@@ -174,6 +175,15 @@ struct gspca_dev {
        unsigned ctrl_dis;              /* disabled controls (bit map) */
        unsigned ctrl_inac;             /* inactive controls (bit map) */
 
+       /* autogain and exposure or gain control cluster, these are global as
+          the autogain/exposure functions in autogain_functions.c use them */
+       struct {
+               struct v4l2_ctrl *autogain;
+               struct v4l2_ctrl *exposure;
+               struct v4l2_ctrl *gain;
+               int exp_too_low_cnt, exp_too_high_cnt;
+       };
+
 #define USB_BUF_SZ 64
        __u8 *usb_buf;                          /* buffer for USB exchanges */
        struct urb *urb[MAX_NURBS];
@@ -242,4 +252,9 @@ int gspca_resume(struct usb_interface *intf);
 #endif
 int gspca_auto_gain_n_exposure(struct gspca_dev *gspca_dev, int avg_lum,
        int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee);
+int gspca_expo_autogain(struct gspca_dev *gspca_dev, int avg_lum,
+       int desired_avg_lum, int deadzone, int gain_knee, int exposure_knee);
+int gspca_coarse_grained_expo_autogain(struct gspca_dev *gspca_dev,
+        int avg_lum, int desired_avg_lum, int deadzone);
+
 #endif /* GSPCAV2_H */