Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.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 #define gst_video_balance_parent_class parent_class
125 G_DEFINE_TYPE_WITH_CODE (GstVideoBalance, gst_video_balance,
126     GST_TYPE_VIDEO_FILTER,
127     G_IMPLEMENT_INTERFACE (GST_TYPE_IMPLEMENTS_INTERFACE,
128         gst_video_balance_interface_init);
129     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
130         gst_video_balance_colorbalance_init));
131
132 /*
133  * look-up tables (LUT).
134  */
135 static void
136 gst_video_balance_update_tables (GstVideoBalance * vb)
137 {
138   gint i, j;
139   gdouble y, u, v, hue_cos, hue_sin;
140
141   /* Y */
142   for (i = 0; i < 256; i++) {
143     y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
144     if (y < 0)
145       y = 0;
146     else if (y > 255)
147       y = 255;
148     vb->tabley[i] = rint (y);
149   }
150
151   hue_cos = cos (G_PI * vb->hue);
152   hue_sin = sin (G_PI * vb->hue);
153
154   /* U/V lookup tables are 2D, since we need both U/V for each table
155    * separately. */
156   for (i = -128; i < 128; i++) {
157     for (j = -128; j < 128; j++) {
158       u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
159       v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
160       if (u < 0)
161         u = 0;
162       else if (u > 255)
163         u = 255;
164       if (v < 0)
165         v = 0;
166       else if (v > 255)
167         v = 255;
168       vb->tableu[i + 128][j + 128] = rint (u);
169       vb->tablev[i + 128][j + 128] = rint (v);
170     }
171   }
172 }
173
174 static gboolean
175 gst_video_balance_is_passthrough (GstVideoBalance * videobalance)
176 {
177   return videobalance->contrast == 1.0 &&
178       videobalance->brightness == 0.0 &&
179       videobalance->hue == 0.0 && videobalance->saturation == 1.0;
180 }
181
182 static void
183 gst_video_balance_update_properties (GstVideoBalance * videobalance)
184 {
185   gboolean passthrough = gst_video_balance_is_passthrough (videobalance);
186   GstBaseTransform *base = GST_BASE_TRANSFORM (videobalance);
187
188   base->passthrough = passthrough;
189
190   if (!passthrough)
191     gst_video_balance_update_tables (videobalance);
192 }
193
194 static void
195 gst_video_balance_planar_yuv (GstVideoBalance * videobalance, guint8 * data)
196 {
197   gint x, y;
198   guint8 *ydata;
199   guint8 *udata, *vdata;
200   gint ystride, ustride, vstride;
201   GstVideoFormat format;
202   gint width, height;
203   gint width2, height2;
204   guint8 *tabley = videobalance->tabley;
205   guint8 **tableu = videobalance->tableu;
206   guint8 **tablev = videobalance->tablev;
207
208   format = videobalance->format;
209   width = videobalance->width;
210   height = videobalance->height;
211
212   ydata =
213       data + gst_video_format_get_component_offset (format, 0, width, height);
214   ystride = gst_video_format_get_row_stride (format, 0, width);
215
216   for (y = 0; y < height; y++) {
217     guint8 *yptr;
218
219     yptr = ydata + y * ystride;
220     for (x = 0; x < width; x++) {
221       *yptr = tabley[*yptr];
222       yptr++;
223     }
224   }
225
226   width2 = gst_video_format_get_component_width (format, 1, width);
227   height2 = gst_video_format_get_component_height (format, 1, height);
228
229   udata =
230       data + gst_video_format_get_component_offset (format, 1, width, height);
231   vdata =
232       data + gst_video_format_get_component_offset (format, 2, width, height);
233   ustride = gst_video_format_get_row_stride (format, 1, width);
234   vstride = gst_video_format_get_row_stride (format, 1, width);
235
236   for (y = 0; y < height2; y++) {
237     guint8 *uptr, *vptr;
238     guint8 u1, v1;
239
240     uptr = udata + y * ustride;
241     vptr = vdata + y * vstride;
242
243     for (x = 0; x < width2; x++) {
244       u1 = *uptr;
245       v1 = *vptr;
246
247       *uptr++ = tableu[u1][v1];
248       *vptr++ = tablev[u1][v1];
249     }
250   }
251 }
252
253 static void
254 gst_video_balance_packed_yuv (GstVideoBalance * videobalance, guint8 * data)
255 {
256   gint x, y;
257   guint8 *ydata;
258   guint8 *udata, *vdata;
259   gint ystride, ustride, vstride;
260   gint yoff, uoff, voff;
261   GstVideoFormat format;
262   gint width, height;
263   gint width2, height2;
264   guint8 *tabley = videobalance->tabley;
265   guint8 **tableu = videobalance->tableu;
266   guint8 **tablev = videobalance->tablev;
267
268   format = videobalance->format;
269   width = videobalance->width;
270   height = videobalance->height;
271
272   ydata =
273       data + gst_video_format_get_component_offset (format, 0, width, height);
274   ystride = gst_video_format_get_row_stride (format, 0, width);
275   yoff = gst_video_format_get_pixel_stride (format, 0);
276
277   for (y = 0; y < height; y++) {
278     guint8 *yptr;
279
280     yptr = ydata + y * ystride;
281     for (x = 0; x < width; x++) {
282       *yptr = tabley[*yptr];
283       yptr += yoff;
284     }
285   }
286
287   width2 = gst_video_format_get_component_width (format, 1, width);
288   height2 = gst_video_format_get_component_height (format, 1, height);
289
290   udata =
291       data + gst_video_format_get_component_offset (format, 1, width, height);
292   vdata =
293       data + gst_video_format_get_component_offset (format, 2, width, height);
294   ustride = gst_video_format_get_row_stride (format, 1, width);
295   vstride = gst_video_format_get_row_stride (format, 1, width);
296   uoff = gst_video_format_get_pixel_stride (format, 1);
297   voff = gst_video_format_get_pixel_stride (format, 2);
298
299   for (y = 0; y < height2; y++) {
300     guint8 *uptr, *vptr;
301     guint8 u1, v1;
302
303     uptr = udata + y * ustride;
304     vptr = vdata + y * vstride;
305
306     for (x = 0; x < width2; x++) {
307       u1 = *uptr;
308       v1 = *vptr;
309
310       *uptr = tableu[u1][v1];
311       *vptr = tablev[u1][v1];
312
313       uptr += uoff;
314       vptr += voff;
315     }
316   }
317 }
318
319 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
320   298, 0, 409, -57068,
321   298, -100, -208, 34707,
322   298, 516, 0, -70870,
323 };
324
325 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
326   66, 129, 25, 4096,
327   -38, -74, 112, 32768,
328   112, -94, -18, 32768,
329 };
330
331 #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)
332
333 static void
334 gst_video_balance_packed_rgb (GstVideoBalance * videobalance, guint8 * data)
335 {
336   gint i, j, height;
337   gint width, row_stride, row_wrap;
338   gint pixel_stride;
339   gint offsets[3];
340   gint r, g, b;
341   gint y, u, v;
342   gint u_tmp, v_tmp;
343   guint8 *tabley = videobalance->tabley;
344   guint8 **tableu = videobalance->tableu;
345   guint8 **tablev = videobalance->tablev;
346
347   offsets[0] = gst_video_format_get_component_offset (videobalance->format, 0,
348       videobalance->width, videobalance->height);
349   offsets[1] = gst_video_format_get_component_offset (videobalance->format, 1,
350       videobalance->width, videobalance->height);
351   offsets[2] = gst_video_format_get_component_offset (videobalance->format, 2,
352       videobalance->width, videobalance->height);
353
354   width =
355       gst_video_format_get_component_width (videobalance->format, 0,
356       videobalance->width);
357   height =
358       gst_video_format_get_component_height (videobalance->format, 0,
359       videobalance->height);
360   row_stride =
361       gst_video_format_get_row_stride (videobalance->format, 0,
362       videobalance->width);
363   pixel_stride = gst_video_format_get_pixel_stride (videobalance->format, 0);
364   row_wrap = row_stride - pixel_stride * width;
365
366   for (i = 0; i < height; i++) {
367     for (j = 0; j < width; j++) {
368       r = data[offsets[0]];
369       g = data[offsets[1]];
370       b = data[offsets[2]];
371
372       y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
373       u_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
374       v_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
375
376       y = CLAMP (y, 0, 255);
377       u_tmp = CLAMP (u_tmp, 0, 255);
378       v_tmp = CLAMP (v_tmp, 0, 255);
379
380       y = tabley[y];
381       u = tableu[u_tmp][v_tmp];
382       v = tablev[u_tmp][v_tmp];
383
384       r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
385       g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
386       b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
387
388       data[offsets[0]] = CLAMP (r, 0, 255);
389       data[offsets[1]] = CLAMP (g, 0, 255);
390       data[offsets[2]] = CLAMP (b, 0, 255);
391       data += pixel_stride;
392     }
393     data += row_wrap;
394   }
395 }
396
397 /* get notified of caps and plug in the correct process function */
398 static gboolean
399 gst_video_balance_set_caps (GstBaseTransform * base, GstCaps * incaps,
400     GstCaps * outcaps)
401 {
402   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (base);
403
404   GST_DEBUG_OBJECT (videobalance,
405       "in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps);
406
407   videobalance->process = NULL;
408
409   if (!gst_video_format_parse_caps (incaps, &videobalance->format,
410           &videobalance->width, &videobalance->height))
411     goto invalid_caps;
412
413   videobalance->size =
414       gst_video_format_get_size (videobalance->format, videobalance->width,
415       videobalance->height);
416
417   switch (videobalance->format) {
418     case GST_VIDEO_FORMAT_I420:
419     case GST_VIDEO_FORMAT_YV12:
420     case GST_VIDEO_FORMAT_Y41B:
421     case GST_VIDEO_FORMAT_Y42B:
422     case GST_VIDEO_FORMAT_Y444:
423       videobalance->process = gst_video_balance_planar_yuv;
424       break;
425     case GST_VIDEO_FORMAT_YUY2:
426     case GST_VIDEO_FORMAT_UYVY:
427     case GST_VIDEO_FORMAT_AYUV:
428     case GST_VIDEO_FORMAT_YVYU:
429       videobalance->process = gst_video_balance_packed_yuv;
430       break;
431     case GST_VIDEO_FORMAT_ARGB:
432     case GST_VIDEO_FORMAT_ABGR:
433     case GST_VIDEO_FORMAT_RGBA:
434     case GST_VIDEO_FORMAT_BGRA:
435     case GST_VIDEO_FORMAT_xRGB:
436     case GST_VIDEO_FORMAT_xBGR:
437     case GST_VIDEO_FORMAT_RGBx:
438     case GST_VIDEO_FORMAT_BGRx:
439     case GST_VIDEO_FORMAT_RGB:
440     case GST_VIDEO_FORMAT_BGR:
441       videobalance->process = gst_video_balance_packed_rgb;
442       break;
443     default:
444       break;
445   }
446
447   return videobalance->process != NULL;
448
449 invalid_caps:
450   GST_ERROR_OBJECT (videobalance, "Invalid caps: %" GST_PTR_FORMAT, incaps);
451   return FALSE;
452 }
453
454 static void
455 gst_video_balance_before_transform (GstBaseTransform * base, GstBuffer * buf)
456 {
457   GstVideoBalance *balance = GST_VIDEO_BALANCE (base);
458   GstClockTime timestamp, stream_time;
459
460   timestamp = GST_BUFFER_TIMESTAMP (buf);
461   stream_time =
462       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
463
464   GST_DEBUG_OBJECT (balance, "sync to %" GST_TIME_FORMAT,
465       GST_TIME_ARGS (timestamp));
466
467   if (GST_CLOCK_TIME_IS_VALID (stream_time))
468     gst_object_sync_values (G_OBJECT (balance), stream_time);
469 }
470
471 static GstFlowReturn
472 gst_video_balance_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
473 {
474   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (base);
475   guint8 *data;
476   gsize size;
477
478   if (!videobalance->process)
479     goto not_negotiated;
480
481   /* if no change is needed, we are done */
482   if (base->passthrough)
483     goto done;
484
485   data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
486
487   if (size != videobalance->size)
488     goto wrong_size;
489
490   GST_OBJECT_LOCK (videobalance);
491   videobalance->process (videobalance, data);
492   GST_OBJECT_UNLOCK (videobalance);
493
494   gst_buffer_unmap (outbuf, data, size);
495
496 done:
497   return GST_FLOW_OK;
498
499   /* ERRORS */
500 wrong_size:
501   {
502     GST_ELEMENT_ERROR (videobalance, STREAM, FORMAT,
503         (NULL), ("Invalid buffer size %d, expected %d", size,
504             videobalance->size));
505     gst_buffer_unmap (outbuf, data, size);
506     return GST_FLOW_ERROR;
507   }
508 not_negotiated:
509   GST_ERROR_OBJECT (videobalance, "Not negotiated yet");
510   return GST_FLOW_NOT_NEGOTIATED;
511 }
512
513 static void
514 gst_video_balance_finalize (GObject * object)
515 {
516   GList *channels = NULL;
517   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
518
519   g_free (balance->tableu[0]);
520
521   channels = balance->channels;
522   while (channels) {
523     GstColorBalanceChannel *channel = channels->data;
524
525     g_object_unref (channel);
526     channels->data = NULL;
527     channels = g_list_next (channels);
528   }
529
530   if (balance->channels)
531     g_list_free (balance->channels);
532
533   G_OBJECT_CLASS (parent_class)->finalize (object);
534 }
535
536 static void
537 gst_video_balance_class_init (GstVideoBalanceClass * klass)
538 {
539   GObjectClass *gobject_class = (GObjectClass *) klass;
540   GstElementClass *gstelement_class = (GstElementClass *) klass;
541   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
542
543   GST_DEBUG_CATEGORY_INIT (videobalance_debug, "videobalance", 0,
544       "videobalance");
545
546   gobject_class->finalize = gst_video_balance_finalize;
547   gobject_class->set_property = gst_video_balance_set_property;
548   gobject_class->get_property = gst_video_balance_get_property;
549
550   g_object_class_install_property (gobject_class, PROP_CONTRAST,
551       g_param_spec_double ("contrast", "Contrast", "contrast",
552           0.0, 2.0, DEFAULT_PROP_CONTRAST,
553           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
554   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
555       g_param_spec_double ("brightness", "Brightness", "brightness", -1.0, 1.0,
556           DEFAULT_PROP_BRIGHTNESS,
557           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558   g_object_class_install_property (gobject_class, PROP_HUE,
559       g_param_spec_double ("hue", "Hue", "hue", -1.0, 1.0, DEFAULT_PROP_HUE,
560           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561   g_object_class_install_property (gobject_class, PROP_SATURATION,
562       g_param_spec_double ("saturation", "Saturation", "saturation", 0.0, 2.0,
563           DEFAULT_PROP_SATURATION,
564           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
565
566   gst_element_class_set_details_simple (gstelement_class, "Video balance",
567       "Filter/Effect/Video",
568       "Adjusts brightness, contrast, hue, saturation on a video stream",
569       "David Schleef <ds@schleef.org>");
570
571   gst_element_class_add_pad_template (gstelement_class,
572       gst_static_pad_template_get (&gst_video_balance_sink_template));
573   gst_element_class_add_pad_template (gstelement_class,
574       gst_static_pad_template_get (&gst_video_balance_src_template));
575
576   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_balance_set_caps);
577   trans_class->transform_ip =
578       GST_DEBUG_FUNCPTR (gst_video_balance_transform_ip);
579   trans_class->before_transform =
580       GST_DEBUG_FUNCPTR (gst_video_balance_before_transform);
581 }
582
583 static void
584 gst_video_balance_init (GstVideoBalance * videobalance)
585 {
586   const gchar *channels[4] = { "HUE", "SATURATION",
587     "BRIGHTNESS", "CONTRAST"
588   };
589   gint i;
590
591   /* Initialize propertiews */
592   videobalance->contrast = DEFAULT_PROP_CONTRAST;
593   videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
594   videobalance->hue = DEFAULT_PROP_HUE;
595   videobalance->saturation = DEFAULT_PROP_SATURATION;
596
597   videobalance->tableu[0] = g_new (guint8, 256 * 256 * 2);
598   for (i = 0; i < 256; i++) {
599     videobalance->tableu[i] =
600         videobalance->tableu[0] + i * 256 * sizeof (guint8);
601     videobalance->tablev[i] =
602         videobalance->tableu[0] + 256 * 256 * sizeof (guint8) +
603         i * 256 * sizeof (guint8);
604   }
605
606   gst_video_balance_update_properties (videobalance);
607
608   /* Generate the channels list */
609   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
610     GstColorBalanceChannel *channel;
611
612     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
613     channel->label = g_strdup (channels[i]);
614     channel->min_value = -1000;
615     channel->max_value = 1000;
616
617     videobalance->channels = g_list_append (videobalance->channels, channel);
618   }
619 }
620
621 static gboolean
622 gst_video_balance_interface_supported (GstImplementsInterface * iface,
623     GType type)
624 {
625   g_assert (type == GST_TYPE_COLOR_BALANCE);
626   return TRUE;
627 }
628
629 static void
630 gst_video_balance_interface_init (GstImplementsInterfaceClass * klass)
631 {
632   klass->supported = gst_video_balance_interface_supported;
633 }
634
635 static const GList *
636 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
637 {
638   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
639
640   g_return_val_if_fail (videobalance != NULL, NULL);
641   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
642
643   return videobalance->channels;
644 }
645
646 static void
647 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
648     GstColorBalanceChannel * channel, gint value)
649 {
650   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
651   gdouble new_val;
652   gboolean changed = FALSE;
653
654   g_return_if_fail (vb != NULL);
655   g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
656   g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
657   g_return_if_fail (channel->label != NULL);
658
659   GST_BASE_TRANSFORM_LOCK (vb);
660   GST_OBJECT_LOCK (vb);
661   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
662     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
663     changed = new_val != vb->hue;
664     vb->hue = new_val;
665   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
666     new_val = (value + 1000.0) * 2.0 / 2000.0;
667     changed = new_val != vb->saturation;
668     vb->saturation = new_val;
669   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
670     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
671     changed = new_val != vb->brightness;
672     vb->brightness = new_val;
673   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
674     new_val = (value + 1000.0) * 2.0 / 2000.0;
675     changed = new_val != vb->contrast;
676     vb->contrast = new_val;
677   }
678
679   if (changed)
680     gst_video_balance_update_properties (vb);
681   GST_OBJECT_UNLOCK (vb);
682   GST_BASE_TRANSFORM_UNLOCK (vb);
683
684   if (changed) {
685     gst_color_balance_value_changed (balance, channel,
686         gst_color_balance_get_value (balance, channel));
687   }
688 }
689
690 static gint
691 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
692     GstColorBalanceChannel * channel)
693 {
694   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
695   gint value = 0;
696
697   g_return_val_if_fail (vb != NULL, 0);
698   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
699   g_return_val_if_fail (channel->label != NULL, 0);
700
701   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
702     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
703   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
704     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
705   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
706     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
707   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
708     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
709   }
710
711   return value;
712 }
713
714 static void
715 gst_video_balance_colorbalance_init (GstColorBalanceClass * iface)
716 {
717   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
718   iface->list_channels = gst_video_balance_colorbalance_list_channels;
719   iface->set_value = gst_video_balance_colorbalance_set_value;
720   iface->get_value = gst_video_balance_colorbalance_get_value;
721 }
722
723 static GstColorBalanceChannel *
724 gst_video_balance_find_channel (GstVideoBalance * balance, const gchar * label)
725 {
726   GList *l;
727
728   for (l = balance->channels; l; l = l->next) {
729     GstColorBalanceChannel *channel = l->data;
730
731     if (g_ascii_strcasecmp (channel->label, label) == 0)
732       return channel;
733   }
734   return NULL;
735 }
736
737 static void
738 gst_video_balance_set_property (GObject * object, guint prop_id,
739     const GValue * value, GParamSpec * pspec)
740 {
741   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
742   gdouble d;
743   const gchar *label = NULL;
744
745   GST_BASE_TRANSFORM_LOCK (balance);
746   GST_OBJECT_LOCK (balance);
747   switch (prop_id) {
748     case PROP_CONTRAST:
749       d = g_value_get_double (value);
750       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
751           balance->contrast, d);
752       if (d != balance->contrast)
753         label = "CONTRAST";
754       balance->contrast = d;
755       break;
756     case PROP_BRIGHTNESS:
757       d = g_value_get_double (value);
758       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
759           balance->brightness, d);
760       if (d != balance->brightness)
761         label = "BRIGHTNESS";
762       balance->brightness = d;
763       break;
764     case PROP_HUE:
765       d = g_value_get_double (value);
766       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
767           d);
768       if (d != balance->hue)
769         label = "HUE";
770       balance->hue = d;
771       break;
772     case PROP_SATURATION:
773       d = g_value_get_double (value);
774       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
775           balance->saturation, d);
776       if (d != balance->saturation)
777         label = "SATURATION";
778       balance->saturation = d;
779       break;
780     default:
781       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
782       break;
783   }
784
785   gst_video_balance_update_properties (balance);
786   GST_OBJECT_UNLOCK (balance);
787   GST_BASE_TRANSFORM_UNLOCK (balance);
788
789   if (label) {
790     GstColorBalanceChannel *channel =
791         gst_video_balance_find_channel (balance, label);
792     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
793         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
794   }
795 }
796
797 static void
798 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
799     GParamSpec * pspec)
800 {
801   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
802
803   switch (prop_id) {
804     case PROP_CONTRAST:
805       g_value_set_double (value, balance->contrast);
806       break;
807     case PROP_BRIGHTNESS:
808       g_value_set_double (value, balance->brightness);
809       break;
810     case PROP_HUE:
811       g_value_set_double (value, balance->hue);
812       break;
813     case PROP_SATURATION:
814       g_value_set_double (value, balance->saturation);
815       break;
816     default:
817       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
818       break;
819   }
820 }