Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / videofilter / gstvideobalance.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  * Copyright (C) <2010> Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /*
23  * This file was (probably) generated from gstvideobalance.c,
24  * gstvideobalance.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
25  */
26
27 /**
28  * SECTION:element-videobalance
29  *
30  * Adjusts brightness, contrast, hue, saturation on a video stream.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch videotestsrc ! videobalance saturation=0.0 ! ffmpegcolorspace ! ximagesink
36  * ]| This pipeline converts the image to black and white by setting the
37  * saturation to 0.0.
38  * </refsect2>
39  *
40  * Last reviewed on 2010-04-18 (0.10.22)
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47 #include <gst/math-compat.h>
48
49 #include "gstvideobalance.h"
50 #include <string.h>
51
52 #include <gst/controller/gstcontroller.h>
53 #include <gst/interfaces/colorbalance.h>
54
55 GST_DEBUG_CATEGORY_STATIC (videobalance_debug);
56 #define GST_CAT_DEFAULT videobalance_debug
57
58 /* GstVideoBalance properties */
59 #define DEFAULT_PROP_CONTRAST           1.0
60 #define DEFAULT_PROP_BRIGHTNESS         0.0
61 #define DEFAULT_PROP_HUE                0.0
62 #define DEFAULT_PROP_SATURATION         1.0
63
64 enum
65 {
66   PROP_0,
67   PROP_CONTRAST,
68   PROP_BRIGHTNESS,
69   PROP_HUE,
70   PROP_SATURATION
71 };
72
73 static GstStaticPadTemplate gst_video_balance_src_template =
74     GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
78         GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
79         GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
80         GST_VIDEO_CAPS_YUV ("Y444") ";"
81         GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_RGBx ";"
82         GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_BGRx ";"
83         GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
84         GST_VIDEO_CAPS_YUV ("Y42B") ";"
85         GST_VIDEO_CAPS_YUV ("YUY2") ";"
86         GST_VIDEO_CAPS_YUV ("UYVY") ";"
87         GST_VIDEO_CAPS_YUV ("YVYU") ";"
88         GST_VIDEO_CAPS_YUV ("I420") ";"
89         GST_VIDEO_CAPS_YUV ("YV12") ";"
90         GST_VIDEO_CAPS_YUV ("IYUV") ";" GST_VIDEO_CAPS_YUV ("Y41B")
91     )
92     );
93
94 static GstStaticPadTemplate gst_video_balance_sink_template =
95     GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
99         GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
100         GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
101         GST_VIDEO_CAPS_YUV ("Y444") ";"
102         GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_RGBx ";"
103         GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_BGRx ";"
104         GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
105         GST_VIDEO_CAPS_YUV ("Y42B") ";"
106         GST_VIDEO_CAPS_YUV ("YUY2") ";"
107         GST_VIDEO_CAPS_YUV ("UYVY") ";"
108         GST_VIDEO_CAPS_YUV ("YVYU") ";"
109         GST_VIDEO_CAPS_YUV ("I420") ";"
110         GST_VIDEO_CAPS_YUV ("YV12") ";"
111         GST_VIDEO_CAPS_YUV ("IYUV") ";" GST_VIDEO_CAPS_YUV ("Y41B")
112     )
113     );
114
115 static void gst_video_balance_colorbalance_init (GstColorBalanceClass * iface);
116 static void gst_video_balance_interface_init (GstImplementsInterfaceClass *
117     klass);
118
119 static void gst_video_balance_set_property (GObject * object, guint prop_id,
120     const GValue * value, GParamSpec * pspec);
121 static void gst_video_balance_get_property (GObject * object, guint prop_id,
122     GValue * value, GParamSpec * pspec);
123
124 static void
125 _do_init (GType video_balance_type)
126 {
127   static const GInterfaceInfo iface_info = {
128     (GInterfaceInitFunc) gst_video_balance_interface_init,
129     NULL,
130     NULL,
131   };
132   static const GInterfaceInfo colorbalance_info = {
133     (GInterfaceInitFunc) gst_video_balance_colorbalance_init,
134     NULL,
135     NULL,
136   };
137
138   g_type_add_interface_static (video_balance_type,
139       GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
140   g_type_add_interface_static (video_balance_type, GST_TYPE_COLOR_BALANCE,
141       &colorbalance_info);
142 }
143
144 GST_BOILERPLATE_FULL (GstVideoBalance, gst_video_balance, GstVideoFilter,
145     GST_TYPE_VIDEO_FILTER, _do_init);
146
147 /*
148  * look-up tables (LUT).
149  */
150 static void
151 gst_video_balance_update_tables (GstVideoBalance * vb)
152 {
153   gint i, j;
154   gdouble y, u, v, hue_cos, hue_sin;
155
156   /* Y */
157   for (i = 0; i < 256; i++) {
158     y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
159     if (y < 0)
160       y = 0;
161     else if (y > 255)
162       y = 255;
163     vb->tabley[i] = rint (y);
164   }
165
166   hue_cos = cos (G_PI * vb->hue);
167   hue_sin = sin (G_PI * vb->hue);
168
169   /* U/V lookup tables are 2D, since we need both U/V for each table
170    * separately. */
171   for (i = -128; i < 128; i++) {
172     for (j = -128; j < 128; j++) {
173       u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
174       v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
175       if (u < 0)
176         u = 0;
177       else if (u > 255)
178         u = 255;
179       if (v < 0)
180         v = 0;
181       else if (v > 255)
182         v = 255;
183       vb->tableu[i + 128][j + 128] = rint (u);
184       vb->tablev[i + 128][j + 128] = rint (v);
185     }
186   }
187 }
188
189 static gboolean
190 gst_video_balance_is_passthrough (GstVideoBalance * videobalance)
191 {
192   return videobalance->contrast == 1.0 &&
193       videobalance->brightness == 0.0 &&
194       videobalance->hue == 0.0 && videobalance->saturation == 1.0;
195 }
196
197 static void
198 gst_video_balance_update_properties (GstVideoBalance * videobalance)
199 {
200   gboolean passthrough = gst_video_balance_is_passthrough (videobalance);
201   GstBaseTransform *base = GST_BASE_TRANSFORM (videobalance);
202
203   base->passthrough = passthrough;
204
205   if (!passthrough)
206     gst_video_balance_update_tables (videobalance);
207 }
208
209 static void
210 gst_video_balance_planar_yuv (GstVideoBalance * videobalance, guint8 * data)
211 {
212   gint x, y;
213   guint8 *ydata;
214   guint8 *udata, *vdata;
215   gint ystride, ustride, vstride;
216   GstVideoFormat format;
217   gint width, height;
218   gint width2, height2;
219   guint8 *tabley = videobalance->tabley;
220   guint8 **tableu = videobalance->tableu;
221   guint8 **tablev = videobalance->tablev;
222
223   format = videobalance->format;
224   width = videobalance->width;
225   height = videobalance->height;
226
227   ydata =
228       data + gst_video_format_get_component_offset (format, 0, width, height);
229   ystride = gst_video_format_get_row_stride (format, 0, width);
230
231   for (y = 0; y < height; y++) {
232     guint8 *yptr;
233
234     yptr = ydata + y * ystride;
235     for (x = 0; x < width; x++) {
236       *ydata = tabley[*ydata];
237       ydata++;
238     }
239   }
240
241   width2 = gst_video_format_get_component_width (format, 1, width);
242   height2 = gst_video_format_get_component_height (format, 1, height);
243
244   udata =
245       data + gst_video_format_get_component_offset (format, 1, width, height);
246   vdata =
247       data + gst_video_format_get_component_offset (format, 2, width, height);
248   ustride = gst_video_format_get_row_stride (format, 1, width);
249   vstride = gst_video_format_get_row_stride (format, 1, width);
250
251   for (y = 0; y < height2; y++) {
252     guint8 *uptr, *vptr;
253     guint8 u1, v1;
254
255     uptr = udata + y * ustride;
256     vptr = vdata + y * vstride;
257
258     for (x = 0; x < width2; x++) {
259       u1 = *uptr;
260       v1 = *vptr;
261
262       *uptr++ = tableu[u1][v1];
263       *vptr++ = tablev[u1][v1];
264     }
265   }
266 }
267
268 static void
269 gst_video_balance_packed_yuv (GstVideoBalance * videobalance, guint8 * data)
270 {
271   gint x, y;
272   guint8 *ydata;
273   guint8 *udata, *vdata;
274   gint ystride, ustride, vstride;
275   gint yoff, uoff, voff;
276   GstVideoFormat format;
277   gint width, height;
278   gint width2, height2;
279   guint8 *tabley = videobalance->tabley;
280   guint8 **tableu = videobalance->tableu;
281   guint8 **tablev = videobalance->tablev;
282
283   format = videobalance->format;
284   width = videobalance->width;
285   height = videobalance->height;
286
287   ydata =
288       data + gst_video_format_get_component_offset (format, 0, width, height);
289   ystride = gst_video_format_get_row_stride (format, 0, width);
290   yoff = gst_video_format_get_pixel_stride (format, 0);
291
292   for (y = 0; y < height; y++) {
293     guint8 *yptr;
294
295     yptr = ydata + y * ystride;
296     for (x = 0; x < width; x++) {
297       *ydata = tabley[*ydata];
298       ydata += yoff;
299     }
300   }
301
302   width2 = gst_video_format_get_component_width (format, 1, width);
303   height2 = gst_video_format_get_component_height (format, 1, height);
304
305   udata =
306       data + gst_video_format_get_component_offset (format, 1, width, height);
307   vdata =
308       data + gst_video_format_get_component_offset (format, 2, width, height);
309   ustride = gst_video_format_get_row_stride (format, 1, width);
310   vstride = gst_video_format_get_row_stride (format, 1, width);
311   uoff = gst_video_format_get_pixel_stride (format, 1);
312   voff = gst_video_format_get_pixel_stride (format, 2);
313
314   for (y = 0; y < height2; y++) {
315     guint8 *uptr, *vptr;
316     guint8 u1, v1;
317
318     uptr = udata + y * ustride;
319     vptr = vdata + y * vstride;
320
321     for (x = 0; x < width2; x++) {
322       u1 = *uptr;
323       v1 = *vptr;
324
325       *uptr = tableu[u1][v1];
326       *vptr = tablev[u1][v1];
327
328       uptr += uoff;
329       vptr += voff;
330     }
331   }
332 }
333
334 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
335   298, 0, 409, -57068,
336   298, -100, -208, 34707,
337   298, 516, 0, -70870,
338 };
339
340 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
341   66, 129, 25, 4096,
342   -38, -74, 112, 32768,
343   112, -94, -18, 32768,
344 };
345
346 #define APPLY_MATRIX(m,o,v1,v2,v3) ((m[o*4] * v1 + m[o*4+1] * v2 + m[o*4+2] * v3 + m[o*4+3]) >> 8)
347
348 static void
349 gst_video_balance_packed_rgb (GstVideoBalance * videobalance, guint8 * data)
350 {
351   gint i, j, height;
352   gint width, row_stride, row_wrap;
353   gint pixel_stride;
354   gint offsets[3];
355   gint r, g, b;
356   gint y, u, v;
357   gint u_tmp, v_tmp;
358   guint8 *tabley = videobalance->tabley;
359   guint8 **tableu = videobalance->tableu;
360   guint8 **tablev = videobalance->tablev;
361
362   offsets[0] = gst_video_format_get_component_offset (videobalance->format, 0,
363       videobalance->width, videobalance->height);
364   offsets[1] = gst_video_format_get_component_offset (videobalance->format, 1,
365       videobalance->width, videobalance->height);
366   offsets[2] = gst_video_format_get_component_offset (videobalance->format, 2,
367       videobalance->width, videobalance->height);
368
369   width =
370       gst_video_format_get_component_width (videobalance->format, 0,
371       videobalance->width);
372   height =
373       gst_video_format_get_component_height (videobalance->format, 0,
374       videobalance->height);
375   row_stride =
376       gst_video_format_get_row_stride (videobalance->format, 0,
377       videobalance->width);
378   pixel_stride = gst_video_format_get_pixel_stride (videobalance->format, 0);
379   row_wrap = row_stride - pixel_stride * width;
380
381   for (i = 0; i < height; i++) {
382     for (j = 0; j < width; j++) {
383       r = data[offsets[0]];
384       g = data[offsets[1]];
385       b = data[offsets[2]];
386
387       y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
388       u_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
389       v_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
390
391       y = CLAMP (y, 0, 255);
392       u_tmp = CLAMP (u_tmp, 0, 255);
393       v_tmp = CLAMP (v_tmp, 0, 255);
394
395       y = tabley[y];
396       u = tableu[u_tmp][v_tmp];
397       v = tablev[u_tmp][v_tmp];
398
399       r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
400       g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
401       b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
402
403       data[offsets[0]] = CLAMP (r, 0, 255);
404       data[offsets[1]] = CLAMP (g, 0, 255);
405       data[offsets[2]] = CLAMP (b, 0, 255);
406       data += pixel_stride;
407     }
408     data += row_wrap;
409   }
410 }
411
412 /* get notified of caps and plug in the correct process function */
413 static gboolean
414 gst_video_balance_set_caps (GstBaseTransform * base, GstCaps * incaps,
415     GstCaps * outcaps)
416 {
417   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (base);
418
419   GST_DEBUG_OBJECT (videobalance,
420       "in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps);
421
422   videobalance->process = NULL;
423
424   if (!gst_video_format_parse_caps (incaps, &videobalance->format,
425           &videobalance->width, &videobalance->height))
426     goto invalid_caps;
427
428   videobalance->size =
429       gst_video_format_get_size (videobalance->format, videobalance->width,
430       videobalance->height);
431
432   switch (videobalance->format) {
433     case GST_VIDEO_FORMAT_I420:
434     case GST_VIDEO_FORMAT_YV12:
435     case GST_VIDEO_FORMAT_Y41B:
436     case GST_VIDEO_FORMAT_Y42B:
437     case GST_VIDEO_FORMAT_Y444:
438       videobalance->process = gst_video_balance_planar_yuv;
439       break;
440     case GST_VIDEO_FORMAT_YUY2:
441     case GST_VIDEO_FORMAT_UYVY:
442     case GST_VIDEO_FORMAT_AYUV:
443     case GST_VIDEO_FORMAT_YVYU:
444       videobalance->process = gst_video_balance_packed_yuv;
445       break;
446     case GST_VIDEO_FORMAT_ARGB:
447     case GST_VIDEO_FORMAT_ABGR:
448     case GST_VIDEO_FORMAT_RGBA:
449     case GST_VIDEO_FORMAT_BGRA:
450     case GST_VIDEO_FORMAT_xRGB:
451     case GST_VIDEO_FORMAT_xBGR:
452     case GST_VIDEO_FORMAT_RGBx:
453     case GST_VIDEO_FORMAT_BGRx:
454     case GST_VIDEO_FORMAT_RGB:
455     case GST_VIDEO_FORMAT_BGR:
456       videobalance->process = gst_video_balance_packed_rgb;
457       break;
458     default:
459       break;
460   }
461
462   return videobalance->process != NULL;
463
464 invalid_caps:
465   GST_ERROR_OBJECT (videobalance, "Invalid caps: %" GST_PTR_FORMAT, incaps);
466   return FALSE;
467 }
468
469 static void
470 gst_video_balance_before_transform (GstBaseTransform * base, GstBuffer * buf)
471 {
472   GstVideoBalance *balance = GST_VIDEO_BALANCE (base);
473   GstClockTime timestamp, stream_time;
474
475   timestamp = GST_BUFFER_TIMESTAMP (buf);
476   stream_time =
477       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
478
479   GST_DEBUG_OBJECT (balance, "sync to %" GST_TIME_FORMAT,
480       GST_TIME_ARGS (timestamp));
481
482   if (GST_CLOCK_TIME_IS_VALID (stream_time))
483     gst_object_sync_values (G_OBJECT (balance), stream_time);
484 }
485
486 static GstFlowReturn
487 gst_video_balance_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
488 {
489   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (base);
490   guint8 *data;
491   gsize size;
492
493   if (!videobalance->process)
494     goto not_negotiated;
495
496   /* if no change is needed, we are done */
497   if (base->passthrough)
498     goto done;
499
500   data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
501
502   if (size != videobalance->size)
503     goto wrong_size;
504
505   GST_OBJECT_LOCK (videobalance);
506   videobalance->process (videobalance, data);
507   GST_OBJECT_UNLOCK (videobalance);
508
509   gst_buffer_unmap (outbuf, data, size);
510
511 done:
512   return GST_FLOW_OK;
513
514   /* ERRORS */
515 wrong_size:
516   {
517     GST_ELEMENT_ERROR (videobalance, STREAM, FORMAT,
518         (NULL), ("Invalid buffer size %d, expected %d", size,
519             videobalance->size));
520     gst_buffer_unmap (outbuf, data, size);
521     return GST_FLOW_ERROR;
522   }
523 not_negotiated:
524   GST_ERROR_OBJECT (videobalance, "Not negotiated yet");
525   return GST_FLOW_NOT_NEGOTIATED;
526 }
527
528 static void
529 gst_video_balance_base_init (gpointer g_class)
530 {
531   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
532
533   gst_element_class_set_details_simple (element_class, "Video balance",
534       "Filter/Effect/Video",
535       "Adjusts brightness, contrast, hue, saturation on a video stream",
536       "David Schleef <ds@schleef.org>");
537
538   gst_element_class_add_pad_template (element_class,
539       gst_static_pad_template_get (&gst_video_balance_sink_template));
540   gst_element_class_add_pad_template (element_class,
541       gst_static_pad_template_get (&gst_video_balance_src_template));
542 }
543
544 static void
545 gst_video_balance_finalize (GObject * object)
546 {
547   GList *channels = NULL;
548   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
549
550   g_free (balance->tableu[0]);
551
552   channels = balance->channels;
553   while (channels) {
554     GstColorBalanceChannel *channel = channels->data;
555
556     g_object_unref (channel);
557     channels->data = NULL;
558     channels = g_list_next (channels);
559   }
560
561   if (balance->channels)
562     g_list_free (balance->channels);
563
564   G_OBJECT_CLASS (parent_class)->finalize (object);
565 }
566
567 static void
568 gst_video_balance_class_init (GstVideoBalanceClass * klass)
569 {
570   GObjectClass *gobject_class = (GObjectClass *) klass;
571   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
572
573   GST_DEBUG_CATEGORY_INIT (videobalance_debug, "videobalance", 0,
574       "videobalance");
575
576   gobject_class->finalize = gst_video_balance_finalize;
577   gobject_class->set_property = gst_video_balance_set_property;
578   gobject_class->get_property = gst_video_balance_get_property;
579
580   g_object_class_install_property (gobject_class, PROP_CONTRAST,
581       g_param_spec_double ("contrast", "Contrast", "contrast",
582           0.0, 2.0, DEFAULT_PROP_CONTRAST,
583           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
585       g_param_spec_double ("brightness", "Brightness", "brightness", -1.0, 1.0,
586           DEFAULT_PROP_BRIGHTNESS,
587           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588   g_object_class_install_property (gobject_class, PROP_HUE,
589       g_param_spec_double ("hue", "Hue", "hue", -1.0, 1.0, DEFAULT_PROP_HUE,
590           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
591   g_object_class_install_property (gobject_class, PROP_SATURATION,
592       g_param_spec_double ("saturation", "Saturation", "saturation", 0.0, 2.0,
593           DEFAULT_PROP_SATURATION,
594           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595
596   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_balance_set_caps);
597   trans_class->transform_ip =
598       GST_DEBUG_FUNCPTR (gst_video_balance_transform_ip);
599   trans_class->before_transform =
600       GST_DEBUG_FUNCPTR (gst_video_balance_before_transform);
601 }
602
603 static void
604 gst_video_balance_init (GstVideoBalance * videobalance,
605     GstVideoBalanceClass * klass)
606 {
607   const gchar *channels[4] = { "HUE", "SATURATION",
608     "BRIGHTNESS", "CONTRAST"
609   };
610   gint i;
611
612   /* Initialize propertiews */
613   videobalance->contrast = DEFAULT_PROP_CONTRAST;
614   videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
615   videobalance->hue = DEFAULT_PROP_HUE;
616   videobalance->saturation = DEFAULT_PROP_SATURATION;
617
618   videobalance->tableu[0] = g_new (guint8, 256 * 256 * 2);
619   for (i = 0; i < 256; i++) {
620     videobalance->tableu[i] =
621         videobalance->tableu[0] + i * 256 * sizeof (guint8);
622     videobalance->tablev[i] =
623         videobalance->tableu[0] + 256 * 256 * sizeof (guint8) +
624         i * 256 * sizeof (guint8);
625   }
626
627   gst_video_balance_update_properties (videobalance);
628
629   /* Generate the channels list */
630   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
631     GstColorBalanceChannel *channel;
632
633     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
634     channel->label = g_strdup (channels[i]);
635     channel->min_value = -1000;
636     channel->max_value = 1000;
637
638     videobalance->channels = g_list_append (videobalance->channels, channel);
639   }
640 }
641
642 static gboolean
643 gst_video_balance_interface_supported (GstImplementsInterface * iface,
644     GType type)
645 {
646   g_assert (type == GST_TYPE_COLOR_BALANCE);
647   return TRUE;
648 }
649
650 static void
651 gst_video_balance_interface_init (GstImplementsInterfaceClass * klass)
652 {
653   klass->supported = gst_video_balance_interface_supported;
654 }
655
656 static const GList *
657 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
658 {
659   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
660
661   g_return_val_if_fail (videobalance != NULL, NULL);
662   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
663
664   return videobalance->channels;
665 }
666
667 static void
668 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
669     GstColorBalanceChannel * channel, gint value)
670 {
671   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
672   gdouble new_val;
673   gboolean changed;
674
675   g_return_if_fail (vb != NULL);
676   g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
677   g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
678   g_return_if_fail (channel->label != NULL);
679
680   GST_BASE_TRANSFORM_LOCK (vb);
681   GST_OBJECT_LOCK (vb);
682   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
683     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
684     changed = new_val != vb->hue;
685     vb->hue = new_val;
686   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
687     new_val = (value + 1000.0) * 2.0 / 2000.0;
688     changed = new_val != vb->saturation;
689     vb->saturation = new_val;
690   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
691     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
692     changed = new_val != vb->brightness;
693     vb->brightness = new_val;
694   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
695     new_val = (value + 1000.0) * 2.0 / 2000.0;
696     changed = new_val != vb->contrast;
697     vb->contrast = new_val;
698   }
699
700   gst_video_balance_update_properties (vb);
701   GST_OBJECT_UNLOCK (vb);
702   GST_BASE_TRANSFORM_UNLOCK (vb);
703
704   gst_color_balance_value_changed (balance, channel,
705       gst_color_balance_get_value (balance, channel));
706 }
707
708 static gint
709 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
710     GstColorBalanceChannel * channel)
711 {
712   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
713   gint value = 0;
714
715   g_return_val_if_fail (vb != NULL, 0);
716   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
717   g_return_val_if_fail (channel->label != NULL, 0);
718
719   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
720     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
721   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
722     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
723   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
724     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
725   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
726     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
727   }
728
729   return value;
730 }
731
732 static void
733 gst_video_balance_colorbalance_init (GstColorBalanceClass * iface)
734 {
735   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
736   iface->list_channels = gst_video_balance_colorbalance_list_channels;
737   iface->set_value = gst_video_balance_colorbalance_set_value;
738   iface->get_value = gst_video_balance_colorbalance_get_value;
739 }
740
741 static GstColorBalanceChannel *
742 gst_video_balance_find_channel (GstVideoBalance * balance, const gchar * label)
743 {
744   GList *l;
745
746   for (l = balance->channels; l; l = l->next) {
747     GstColorBalanceChannel *channel = l->data;
748
749     if (g_ascii_strcasecmp (channel->label, label) == 0)
750       return channel;
751   }
752   return NULL;
753 }
754
755 static void
756 gst_video_balance_set_property (GObject * object, guint prop_id,
757     const GValue * value, GParamSpec * pspec)
758 {
759   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
760   gdouble d;
761   const gchar *label = NULL;
762
763   GST_BASE_TRANSFORM_LOCK (balance);
764   GST_OBJECT_LOCK (balance);
765   switch (prop_id) {
766     case PROP_CONTRAST:
767       d = g_value_get_double (value);
768       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
769           balance->contrast, d);
770       if (d != balance->contrast)
771         label = "CONTRAST";
772       balance->contrast = d;
773       break;
774     case PROP_BRIGHTNESS:
775       d = g_value_get_double (value);
776       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
777           balance->brightness, d);
778       if (d != balance->brightness)
779         label = "BRIGHTNESS";
780       balance->brightness = d;
781       break;
782     case PROP_HUE:
783       d = g_value_get_double (value);
784       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
785           d);
786       if (d != balance->hue)
787         label = "HUE";
788       balance->hue = d;
789       break;
790     case PROP_SATURATION:
791       d = g_value_get_double (value);
792       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
793           balance->saturation, d);
794       if (d != balance->saturation)
795         label = "SATURATION";
796       balance->saturation = d;
797       break;
798     default:
799       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
800       break;
801   }
802
803   gst_video_balance_update_properties (balance);
804   GST_OBJECT_UNLOCK (balance);
805   GST_BASE_TRANSFORM_UNLOCK (balance);
806
807   if (label) {
808     GstColorBalanceChannel *channel =
809         gst_video_balance_find_channel (balance, label);
810     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
811         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
812   }
813 }
814
815 static void
816 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
817     GParamSpec * pspec)
818 {
819   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
820
821   switch (prop_id) {
822     case PROP_CONTRAST:
823       g_value_set_double (value, balance->contrast);
824       break;
825     case PROP_BRIGHTNESS:
826       g_value_set_double (value, balance->brightness);
827       break;
828     case PROP_HUE:
829       g_value_set_double (value, balance->hue);
830       break;
831     case PROP_SATURATION:
832       g_value_set_double (value, balance->saturation);
833       break;
834     default:
835       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
836       break;
837   }
838 }