Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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       *yptr = tabley[*yptr];
237       yptr++;
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       *yptr = tabley[*yptr];
298       yptr += 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   guint 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_DATA (outbuf);
501   size = GST_BUFFER_SIZE (outbuf);
502
503   if (size != videobalance->size)
504     goto wrong_size;
505
506   GST_OBJECT_LOCK (videobalance);
507   videobalance->process (videobalance, data);
508   GST_OBJECT_UNLOCK (videobalance);
509
510 done:
511   return GST_FLOW_OK;
512
513   /* ERRORS */
514 wrong_size:
515   {
516     GST_ELEMENT_ERROR (videobalance, STREAM, FORMAT,
517         (NULL), ("Invalid buffer size %d, expected %d", size,
518             videobalance->size));
519     return GST_FLOW_ERROR;
520   }
521 not_negotiated:
522   GST_ERROR_OBJECT (videobalance, "Not negotiated yet");
523   return GST_FLOW_NOT_NEGOTIATED;
524 }
525
526 static void
527 gst_video_balance_base_init (gpointer g_class)
528 {
529   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
530
531   gst_element_class_set_details_simple (element_class, "Video balance",
532       "Filter/Effect/Video",
533       "Adjusts brightness, contrast, hue, saturation on a video stream",
534       "David Schleef <ds@schleef.org>");
535
536   gst_element_class_add_static_pad_template (element_class,
537       &gst_video_balance_sink_template);
538   gst_element_class_add_static_pad_template (element_class,
539       &gst_video_balance_src_template);
540 }
541
542 static void
543 gst_video_balance_finalize (GObject * object)
544 {
545   GList *channels = NULL;
546   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
547
548   g_free (balance->tableu[0]);
549
550   channels = balance->channels;
551   while (channels) {
552     GstColorBalanceChannel *channel = channels->data;
553
554     g_object_unref (channel);
555     channels->data = NULL;
556     channels = g_list_next (channels);
557   }
558
559   if (balance->channels)
560     g_list_free (balance->channels);
561
562   G_OBJECT_CLASS (parent_class)->finalize (object);
563 }
564
565 static void
566 gst_video_balance_class_init (GstVideoBalanceClass * klass)
567 {
568   GObjectClass *gobject_class = (GObjectClass *) klass;
569   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
570
571   GST_DEBUG_CATEGORY_INIT (videobalance_debug, "videobalance", 0,
572       "videobalance");
573
574   gobject_class->finalize = gst_video_balance_finalize;
575   gobject_class->set_property = gst_video_balance_set_property;
576   gobject_class->get_property = gst_video_balance_get_property;
577
578   g_object_class_install_property (gobject_class, PROP_CONTRAST,
579       g_param_spec_double ("contrast", "Contrast", "contrast",
580           0.0, 2.0, DEFAULT_PROP_CONTRAST,
581           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
583       g_param_spec_double ("brightness", "Brightness", "brightness", -1.0, 1.0,
584           DEFAULT_PROP_BRIGHTNESS,
585           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
586   g_object_class_install_property (gobject_class, PROP_HUE,
587       g_param_spec_double ("hue", "Hue", "hue", -1.0, 1.0, DEFAULT_PROP_HUE,
588           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
589   g_object_class_install_property (gobject_class, PROP_SATURATION,
590       g_param_spec_double ("saturation", "Saturation", "saturation", 0.0, 2.0,
591           DEFAULT_PROP_SATURATION,
592           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
593
594   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_balance_set_caps);
595   trans_class->transform_ip =
596       GST_DEBUG_FUNCPTR (gst_video_balance_transform_ip);
597   trans_class->before_transform =
598       GST_DEBUG_FUNCPTR (gst_video_balance_before_transform);
599 }
600
601 static void
602 gst_video_balance_init (GstVideoBalance * videobalance,
603     GstVideoBalanceClass * klass)
604 {
605   const gchar *channels[4] = { "HUE", "SATURATION",
606     "BRIGHTNESS", "CONTRAST"
607   };
608   gint i;
609
610   /* Initialize propertiews */
611   videobalance->contrast = DEFAULT_PROP_CONTRAST;
612   videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
613   videobalance->hue = DEFAULT_PROP_HUE;
614   videobalance->saturation = DEFAULT_PROP_SATURATION;
615
616   videobalance->tableu[0] = g_new (guint8, 256 * 256 * 2);
617   for (i = 0; i < 256; i++) {
618     videobalance->tableu[i] =
619         videobalance->tableu[0] + i * 256 * sizeof (guint8);
620     videobalance->tablev[i] =
621         videobalance->tableu[0] + 256 * 256 * sizeof (guint8) +
622         i * 256 * sizeof (guint8);
623   }
624
625   gst_video_balance_update_properties (videobalance);
626
627   /* Generate the channels list */
628   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
629     GstColorBalanceChannel *channel;
630
631     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
632     channel->label = g_strdup (channels[i]);
633     channel->min_value = -1000;
634     channel->max_value = 1000;
635
636     videobalance->channels = g_list_append (videobalance->channels, channel);
637   }
638 }
639
640 static gboolean
641 gst_video_balance_interface_supported (GstImplementsInterface * iface,
642     GType type)
643 {
644   g_assert (type == GST_TYPE_COLOR_BALANCE);
645   return TRUE;
646 }
647
648 static void
649 gst_video_balance_interface_init (GstImplementsInterfaceClass * klass)
650 {
651   klass->supported = gst_video_balance_interface_supported;
652 }
653
654 static const GList *
655 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
656 {
657   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
658
659   g_return_val_if_fail (videobalance != NULL, NULL);
660   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
661
662   return videobalance->channels;
663 }
664
665 static void
666 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
667     GstColorBalanceChannel * channel, gint value)
668 {
669   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
670   gdouble new_val;
671   gboolean changed = FALSE;
672
673   g_return_if_fail (vb != NULL);
674   g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
675   g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
676   g_return_if_fail (channel->label != NULL);
677
678   GST_BASE_TRANSFORM_LOCK (vb);
679   GST_OBJECT_LOCK (vb);
680   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
681     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
682     changed = new_val != vb->hue;
683     vb->hue = new_val;
684   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
685     new_val = (value + 1000.0) * 2.0 / 2000.0;
686     changed = new_val != vb->saturation;
687     vb->saturation = new_val;
688   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
689     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
690     changed = new_val != vb->brightness;
691     vb->brightness = new_val;
692   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
693     new_val = (value + 1000.0) * 2.0 / 2000.0;
694     changed = new_val != vb->contrast;
695     vb->contrast = new_val;
696   }
697
698   if (changed)
699     gst_video_balance_update_properties (vb);
700   GST_OBJECT_UNLOCK (vb);
701   GST_BASE_TRANSFORM_UNLOCK (vb);
702
703   if (changed) {
704     gst_color_balance_value_changed (balance, channel,
705         gst_color_balance_get_value (balance, channel));
706   }
707 }
708
709 static gint
710 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
711     GstColorBalanceChannel * channel)
712 {
713   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
714   gint value = 0;
715
716   g_return_val_if_fail (vb != NULL, 0);
717   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
718   g_return_val_if_fail (channel->label != NULL, 0);
719
720   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
721     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
722   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
723     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
724   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
725     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
726   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
727     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
728   }
729
730   return value;
731 }
732
733 static void
734 gst_video_balance_colorbalance_init (GstColorBalanceClass * iface)
735 {
736   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
737   iface->list_channels = gst_video_balance_colorbalance_list_channels;
738   iface->set_value = gst_video_balance_colorbalance_set_value;
739   iface->get_value = gst_video_balance_colorbalance_get_value;
740 }
741
742 static GstColorBalanceChannel *
743 gst_video_balance_find_channel (GstVideoBalance * balance, const gchar * label)
744 {
745   GList *l;
746
747   for (l = balance->channels; l; l = l->next) {
748     GstColorBalanceChannel *channel = l->data;
749
750     if (g_ascii_strcasecmp (channel->label, label) == 0)
751       return channel;
752   }
753   return NULL;
754 }
755
756 static void
757 gst_video_balance_set_property (GObject * object, guint prop_id,
758     const GValue * value, GParamSpec * pspec)
759 {
760   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
761   gdouble d;
762   const gchar *label = NULL;
763
764   GST_BASE_TRANSFORM_LOCK (balance);
765   GST_OBJECT_LOCK (balance);
766   switch (prop_id) {
767     case PROP_CONTRAST:
768       d = g_value_get_double (value);
769       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
770           balance->contrast, d);
771       if (d != balance->contrast)
772         label = "CONTRAST";
773       balance->contrast = d;
774       break;
775     case PROP_BRIGHTNESS:
776       d = g_value_get_double (value);
777       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
778           balance->brightness, d);
779       if (d != balance->brightness)
780         label = "BRIGHTNESS";
781       balance->brightness = d;
782       break;
783     case PROP_HUE:
784       d = g_value_get_double (value);
785       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
786           d);
787       if (d != balance->hue)
788         label = "HUE";
789       balance->hue = d;
790       break;
791     case PROP_SATURATION:
792       d = g_value_get_double (value);
793       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
794           balance->saturation, d);
795       if (d != balance->saturation)
796         label = "SATURATION";
797       balance->saturation = d;
798       break;
799     default:
800       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
801       break;
802   }
803
804   gst_video_balance_update_properties (balance);
805   GST_OBJECT_UNLOCK (balance);
806   GST_BASE_TRANSFORM_UNLOCK (balance);
807
808   if (label) {
809     GstColorBalanceChannel *channel =
810         gst_video_balance_find_channel (balance, label);
811     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
812         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
813   }
814 }
815
816 static void
817 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
818     GParamSpec * pspec)
819 {
820   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
821
822   switch (prop_id) {
823     case PROP_CONTRAST:
824       g_value_set_double (value, balance->contrast);
825       break;
826     case PROP_BRIGHTNESS:
827       g_value_set_double (value, balance->brightness);
828       break;
829     case PROP_HUE:
830       g_value_set_double (value, balance->hue);
831       break;
832     case PROP_SATURATION:
833       g_value_set_double (value, balance->saturation);
834       break;
835     default:
836       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
837       break;
838   }
839 }