videofilter: avoid holding object lock when calling basetransform function
[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/video/colorbalance.h>
53
54 GST_DEBUG_CATEGORY_STATIC (videobalance_debug);
55 #define GST_CAT_DEFAULT videobalance_debug
56
57 /* GstVideoBalance properties */
58 #define DEFAULT_PROP_CONTRAST           1.0
59 #define DEFAULT_PROP_BRIGHTNESS         0.0
60 #define DEFAULT_PROP_HUE                0.0
61 #define DEFAULT_PROP_SATURATION         1.0
62
63 enum
64 {
65   PROP_0,
66   PROP_CONTRAST,
67   PROP_BRIGHTNESS,
68   PROP_HUE,
69   PROP_SATURATION
70 };
71
72 static GstStaticPadTemplate gst_video_balance_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
77             "ARGB, BGRA, ABGR, RGBA, Y444, xRGB, RGBx, "
78             "xBGR, BGRx, RGB, BGR, Y42B, YUY2, UYVY, YVYU, "
79             "I420, YV12, IYUV, Y41B }"))
80     );
81
82 static GstStaticPadTemplate gst_video_balance_sink_template =
83 GST_STATIC_PAD_TEMPLATE ("sink",
84     GST_PAD_SINK,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
87             "ARGB, BGRA, ABGR, RGBA, Y444, xRGB, RGBx, "
88             "xBGR, BGRx, RGB, BGR, Y42B, YUY2, UYVY, YVYU, "
89             "I420, YV12, IYUV, Y41B }"))
90     );
91
92 static void gst_video_balance_colorbalance_init (GstColorBalanceInterface *
93     iface);
94
95 static void gst_video_balance_set_property (GObject * object, guint prop_id,
96     const GValue * value, GParamSpec * pspec);
97 static void gst_video_balance_get_property (GObject * object, guint prop_id,
98     GValue * value, GParamSpec * pspec);
99
100 #define gst_video_balance_parent_class parent_class
101 G_DEFINE_TYPE_WITH_CODE (GstVideoBalance, gst_video_balance,
102     GST_TYPE_VIDEO_FILTER,
103     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
104         gst_video_balance_colorbalance_init));
105
106 /*
107  * look-up tables (LUT).
108  */
109 static void
110 gst_video_balance_update_tables (GstVideoBalance * vb)
111 {
112   gint i, j;
113   gdouble y, u, v, hue_cos, hue_sin;
114
115   /* Y */
116   for (i = 0; i < 256; i++) {
117     y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
118     if (y < 0)
119       y = 0;
120     else if (y > 255)
121       y = 255;
122     vb->tabley[i] = rint (y);
123   }
124
125   hue_cos = cos (G_PI * vb->hue);
126   hue_sin = sin (G_PI * vb->hue);
127
128   /* U/V lookup tables are 2D, since we need both U/V for each table
129    * separately. */
130   for (i = -128; i < 128; i++) {
131     for (j = -128; j < 128; j++) {
132       u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
133       v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
134       if (u < 0)
135         u = 0;
136       else if (u > 255)
137         u = 255;
138       if (v < 0)
139         v = 0;
140       else if (v > 255)
141         v = 255;
142       vb->tableu[i + 128][j + 128] = rint (u);
143       vb->tablev[i + 128][j + 128] = rint (v);
144     }
145   }
146 }
147
148 static gboolean
149 gst_video_balance_is_passthrough (GstVideoBalance * videobalance)
150 {
151   return videobalance->contrast == 1.0 &&
152       videobalance->brightness == 0.0 &&
153       videobalance->hue == 0.0 && videobalance->saturation == 1.0;
154 }
155
156 static void
157 gst_video_balance_update_properties (GstVideoBalance * videobalance)
158 {
159   gboolean passthrough;
160   GstBaseTransform *base = GST_BASE_TRANSFORM (videobalance);
161
162   GST_OBJECT_LOCK (videobalance);
163   passthrough = gst_video_balance_is_passthrough (videobalance);
164   if (!passthrough)
165     gst_video_balance_update_tables (videobalance);
166   GST_OBJECT_UNLOCK (videobalance);
167
168   gst_base_transform_set_passthrough (base, passthrough);
169 }
170
171 static void
172 gst_video_balance_planar_yuv (GstVideoBalance * videobalance,
173     GstVideoFrame * frame)
174 {
175   gint x, y;
176   guint8 *ydata;
177   guint8 *udata, *vdata;
178   gint ystride, ustride, vstride;
179   gint width, height;
180   gint width2, height2;
181   guint8 *tabley = videobalance->tabley;
182   guint8 **tableu = videobalance->tableu;
183   guint8 **tablev = videobalance->tablev;
184
185   width = GST_VIDEO_FRAME_WIDTH (frame);
186   height = GST_VIDEO_FRAME_HEIGHT (frame);
187
188   ydata = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
189   ystride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
190
191   for (y = 0; y < height; y++) {
192     guint8 *yptr;
193
194     yptr = ydata + y * ystride;
195     for (x = 0; x < width; x++) {
196       *yptr = tabley[*yptr];
197       yptr++;
198     }
199   }
200
201   width2 = GST_VIDEO_FRAME_COMP_WIDTH (frame, 1);
202   height2 = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 1);
203
204   udata = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
205   vdata = GST_VIDEO_FRAME_PLANE_DATA (frame, 2);
206   ustride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
207   vstride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 2);
208
209   for (y = 0; y < height2; y++) {
210     guint8 *uptr, *vptr;
211     guint8 u1, v1;
212
213     uptr = udata + y * ustride;
214     vptr = vdata + y * vstride;
215
216     for (x = 0; x < width2; x++) {
217       u1 = *uptr;
218       v1 = *vptr;
219
220       *uptr++ = tableu[u1][v1];
221       *vptr++ = tablev[u1][v1];
222     }
223   }
224 }
225
226 static void
227 gst_video_balance_packed_yuv (GstVideoBalance * videobalance,
228     GstVideoFrame * frame)
229 {
230   gint x, y, stride;
231   guint8 *ydata, *udata, *vdata;
232   gint yoff, uoff, voff;
233   gint width, height;
234   gint width2, height2;
235   guint8 *tabley = videobalance->tabley;
236   guint8 **tableu = videobalance->tableu;
237   guint8 **tablev = videobalance->tablev;
238
239   width = GST_VIDEO_FRAME_WIDTH (frame);
240   height = GST_VIDEO_FRAME_HEIGHT (frame);
241
242   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
243   ydata = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
244   yoff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
245
246   for (y = 0; y < height; y++) {
247     guint8 *yptr;
248
249     yptr = ydata + y * stride;
250     for (x = 0; x < width; x++) {
251       *yptr = tabley[*yptr];
252       yptr += yoff;
253     }
254   }
255
256   width2 = GST_VIDEO_FRAME_COMP_WIDTH (frame, 1);
257   height2 = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 1);
258
259   udata = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
260   vdata = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
261   uoff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 1);
262   voff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 2);
263
264   for (y = 0; y < height2; y++) {
265     guint8 *uptr, *vptr;
266     guint8 u1, v1;
267
268     uptr = udata + y * stride;
269     vptr = vdata + y * stride;
270
271     for (x = 0; x < width2; x++) {
272       u1 = *uptr;
273       v1 = *vptr;
274
275       *uptr = tableu[u1][v1];
276       *vptr = tablev[u1][v1];
277
278       uptr += uoff;
279       vptr += voff;
280     }
281   }
282 }
283
284 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
285   298, 0, 409, -57068,
286   298, -100, -208, 34707,
287   298, 516, 0, -70870,
288 };
289
290 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
291   66, 129, 25, 4096,
292   -38, -74, 112, 32768,
293   112, -94, -18, 32768,
294 };
295
296 #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)
297
298 static void
299 gst_video_balance_packed_rgb (GstVideoBalance * videobalance,
300     GstVideoFrame * frame)
301 {
302   gint i, j, height;
303   gint width, stride, row_wrap;
304   gint pixel_stride;
305   guint8 *data;
306   gint offsets[3];
307   gint r, g, b;
308   gint y, u, v;
309   gint u_tmp, v_tmp;
310   guint8 *tabley = videobalance->tabley;
311   guint8 **tableu = videobalance->tableu;
312   guint8 **tablev = videobalance->tablev;
313
314   width = GST_VIDEO_FRAME_WIDTH (frame);
315   height = GST_VIDEO_FRAME_HEIGHT (frame);
316
317   offsets[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
318   offsets[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
319   offsets[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
320
321   data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
322   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
323
324   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
325   row_wrap = stride - pixel_stride * width;
326
327   for (i = 0; i < height; i++) {
328     for (j = 0; j < width; j++) {
329       r = data[offsets[0]];
330       g = data[offsets[1]];
331       b = data[offsets[2]];
332
333       y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
334       u_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
335       v_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
336
337       y = CLAMP (y, 0, 255);
338       u_tmp = CLAMP (u_tmp, 0, 255);
339       v_tmp = CLAMP (v_tmp, 0, 255);
340
341       y = tabley[y];
342       u = tableu[u_tmp][v_tmp];
343       v = tablev[u_tmp][v_tmp];
344
345       r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
346       g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
347       b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
348
349       data[offsets[0]] = CLAMP (r, 0, 255);
350       data[offsets[1]] = CLAMP (g, 0, 255);
351       data[offsets[2]] = CLAMP (b, 0, 255);
352       data += pixel_stride;
353     }
354     data += row_wrap;
355   }
356 }
357
358 /* get notified of caps and plug in the correct process function */
359 static gboolean
360 gst_video_balance_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
361     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
362 {
363   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (vfilter);
364
365   GST_DEBUG_OBJECT (videobalance,
366       "in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps);
367
368   videobalance->process = NULL;
369
370   switch (GST_VIDEO_INFO_FORMAT (in_info)) {
371     case GST_VIDEO_FORMAT_I420:
372     case GST_VIDEO_FORMAT_YV12:
373     case GST_VIDEO_FORMAT_Y41B:
374     case GST_VIDEO_FORMAT_Y42B:
375     case GST_VIDEO_FORMAT_Y444:
376       videobalance->process = gst_video_balance_planar_yuv;
377       break;
378     case GST_VIDEO_FORMAT_YUY2:
379     case GST_VIDEO_FORMAT_UYVY:
380     case GST_VIDEO_FORMAT_AYUV:
381     case GST_VIDEO_FORMAT_YVYU:
382       videobalance->process = gst_video_balance_packed_yuv;
383       break;
384     case GST_VIDEO_FORMAT_ARGB:
385     case GST_VIDEO_FORMAT_ABGR:
386     case GST_VIDEO_FORMAT_RGBA:
387     case GST_VIDEO_FORMAT_BGRA:
388     case GST_VIDEO_FORMAT_xRGB:
389     case GST_VIDEO_FORMAT_xBGR:
390     case GST_VIDEO_FORMAT_RGBx:
391     case GST_VIDEO_FORMAT_BGRx:
392     case GST_VIDEO_FORMAT_RGB:
393     case GST_VIDEO_FORMAT_BGR:
394       videobalance->process = gst_video_balance_packed_rgb;
395       break;
396     default:
397       goto unknown_format;
398       break;
399   }
400
401   return TRUE;
402
403   /* ERRORS */
404 unknown_format:
405   {
406     GST_ERROR_OBJECT (videobalance, "unknown format %" GST_PTR_FORMAT, incaps);
407     return FALSE;
408   }
409 }
410
411 static void
412 gst_video_balance_before_transform (GstBaseTransform * base, GstBuffer * buf)
413 {
414   GstVideoBalance *balance = GST_VIDEO_BALANCE (base);
415   GstClockTime timestamp, stream_time;
416
417   timestamp = GST_BUFFER_TIMESTAMP (buf);
418   stream_time =
419       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
420
421   GST_DEBUG_OBJECT (balance, "sync to %" GST_TIME_FORMAT,
422       GST_TIME_ARGS (timestamp));
423
424   if (GST_CLOCK_TIME_IS_VALID (stream_time))
425     gst_object_sync_values (GST_OBJECT (balance), stream_time);
426 }
427
428 static GstFlowReturn
429 gst_video_balance_transform_frame_ip (GstVideoFilter * vfilter,
430     GstVideoFrame * frame)
431 {
432   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (vfilter);
433
434   if (!videobalance->process)
435     goto not_negotiated;
436
437   /* if no change is needed, we are done */
438   if (gst_base_transform_is_passthrough (GST_BASE_TRANSFORM (vfilter)))
439     goto done;
440
441   GST_OBJECT_LOCK (videobalance);
442   videobalance->process (videobalance, frame);
443   GST_OBJECT_UNLOCK (videobalance);
444
445 done:
446   return GST_FLOW_OK;
447
448   /* ERRORS */
449 not_negotiated:
450   {
451     GST_ERROR_OBJECT (videobalance, "Not negotiated yet");
452     return GST_FLOW_NOT_NEGOTIATED;
453   }
454 }
455
456 static void
457 gst_video_balance_finalize (GObject * object)
458 {
459   GList *channels = NULL;
460   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
461
462   g_free (balance->tableu[0]);
463
464   channels = balance->channels;
465   while (channels) {
466     GstColorBalanceChannel *channel = channels->data;
467
468     g_object_unref (channel);
469     channels->data = NULL;
470     channels = g_list_next (channels);
471   }
472
473   if (balance->channels)
474     g_list_free (balance->channels);
475
476   G_OBJECT_CLASS (parent_class)->finalize (object);
477 }
478
479 static void
480 gst_video_balance_class_init (GstVideoBalanceClass * klass)
481 {
482   GObjectClass *gobject_class = (GObjectClass *) klass;
483   GstElementClass *gstelement_class = (GstElementClass *) klass;
484   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
485   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
486
487   GST_DEBUG_CATEGORY_INIT (videobalance_debug, "videobalance", 0,
488       "videobalance");
489
490   gobject_class->finalize = gst_video_balance_finalize;
491   gobject_class->set_property = gst_video_balance_set_property;
492   gobject_class->get_property = gst_video_balance_get_property;
493
494   g_object_class_install_property (gobject_class, PROP_CONTRAST,
495       g_param_spec_double ("contrast", "Contrast", "contrast",
496           0.0, 2.0, DEFAULT_PROP_CONTRAST,
497           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
499       g_param_spec_double ("brightness", "Brightness", "brightness", -1.0, 1.0,
500           DEFAULT_PROP_BRIGHTNESS,
501           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502   g_object_class_install_property (gobject_class, PROP_HUE,
503       g_param_spec_double ("hue", "Hue", "hue", -1.0, 1.0, DEFAULT_PROP_HUE,
504           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
505   g_object_class_install_property (gobject_class, PROP_SATURATION,
506       g_param_spec_double ("saturation", "Saturation", "saturation", 0.0, 2.0,
507           DEFAULT_PROP_SATURATION,
508           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509
510   gst_element_class_set_details_simple (gstelement_class, "Video balance",
511       "Filter/Effect/Video",
512       "Adjusts brightness, contrast, hue, saturation on a video stream",
513       "David Schleef <ds@schleef.org>");
514
515   gst_element_class_add_pad_template (gstelement_class,
516       gst_static_pad_template_get (&gst_video_balance_sink_template));
517   gst_element_class_add_pad_template (gstelement_class,
518       gst_static_pad_template_get (&gst_video_balance_src_template));
519
520   trans_class->before_transform =
521       GST_DEBUG_FUNCPTR (gst_video_balance_before_transform);
522
523   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_balance_set_info);
524   vfilter_class->transform_frame_ip =
525       GST_DEBUG_FUNCPTR (gst_video_balance_transform_frame_ip);
526 }
527
528 static void
529 gst_video_balance_init (GstVideoBalance * videobalance)
530 {
531   const gchar *channels[4] = { "HUE", "SATURATION",
532     "BRIGHTNESS", "CONTRAST"
533   };
534   gint i;
535
536   /* Initialize propertiews */
537   videobalance->contrast = DEFAULT_PROP_CONTRAST;
538   videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
539   videobalance->hue = DEFAULT_PROP_HUE;
540   videobalance->saturation = DEFAULT_PROP_SATURATION;
541
542   videobalance->tableu[0] = g_new (guint8, 256 * 256 * 2);
543   for (i = 0; i < 256; i++) {
544     videobalance->tableu[i] =
545         videobalance->tableu[0] + i * 256 * sizeof (guint8);
546     videobalance->tablev[i] =
547         videobalance->tableu[0] + 256 * 256 * sizeof (guint8) +
548         i * 256 * sizeof (guint8);
549   }
550
551   gst_video_balance_update_properties (videobalance);
552
553   /* Generate the channels list */
554   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
555     GstColorBalanceChannel *channel;
556
557     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
558     channel->label = g_strdup (channels[i]);
559     channel->min_value = -1000;
560     channel->max_value = 1000;
561
562     videobalance->channels = g_list_append (videobalance->channels, channel);
563   }
564 }
565
566 static const GList *
567 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
568 {
569   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
570
571   g_return_val_if_fail (videobalance != NULL, NULL);
572   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
573
574   return videobalance->channels;
575 }
576
577 static void
578 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
579     GstColorBalanceChannel * channel, gint value)
580 {
581   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
582   gdouble new_val;
583   gboolean changed = FALSE;
584
585   g_return_if_fail (vb != NULL);
586   g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
587   g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
588   g_return_if_fail (channel->label != NULL);
589
590   GST_OBJECT_LOCK (vb);
591   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
592     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
593     changed = new_val != vb->hue;
594     vb->hue = new_val;
595   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
596     new_val = (value + 1000.0) * 2.0 / 2000.0;
597     changed = new_val != vb->saturation;
598     vb->saturation = new_val;
599   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
600     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
601     changed = new_val != vb->brightness;
602     vb->brightness = new_val;
603   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
604     new_val = (value + 1000.0) * 2.0 / 2000.0;
605     changed = new_val != vb->contrast;
606     vb->contrast = new_val;
607   }
608
609   if (changed)
610     gst_video_balance_update_properties (vb);
611   GST_OBJECT_UNLOCK (vb);
612
613   if (changed) {
614     gst_color_balance_value_changed (balance, channel,
615         gst_color_balance_get_value (balance, channel));
616   }
617 }
618
619 static gint
620 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
621     GstColorBalanceChannel * channel)
622 {
623   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
624   gint value = 0;
625
626   g_return_val_if_fail (vb != NULL, 0);
627   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
628   g_return_val_if_fail (channel->label != NULL, 0);
629
630   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
631     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
632   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
633     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
634   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
635     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
636   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
637     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
638   }
639
640   return value;
641 }
642
643 static GstColorBalanceType
644 gst_video_balance_colorbalance_get_balance_type (GstColorBalance * balance)
645 {
646   return GST_COLOR_BALANCE_SOFTWARE;
647 }
648
649 static void
650 gst_video_balance_colorbalance_init (GstColorBalanceInterface * iface)
651 {
652   iface->list_channels = gst_video_balance_colorbalance_list_channels;
653   iface->set_value = gst_video_balance_colorbalance_set_value;
654   iface->get_value = gst_video_balance_colorbalance_get_value;
655   iface->get_balance_type = gst_video_balance_colorbalance_get_balance_type;
656 }
657
658 static GstColorBalanceChannel *
659 gst_video_balance_find_channel (GstVideoBalance * balance, const gchar * label)
660 {
661   GList *l;
662
663   for (l = balance->channels; l; l = l->next) {
664     GstColorBalanceChannel *channel = l->data;
665
666     if (g_ascii_strcasecmp (channel->label, label) == 0)
667       return channel;
668   }
669   return NULL;
670 }
671
672 static void
673 gst_video_balance_set_property (GObject * object, guint prop_id,
674     const GValue * value, GParamSpec * pspec)
675 {
676   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
677   gdouble d;
678   const gchar *label = NULL;
679
680   GST_OBJECT_LOCK (balance);
681   switch (prop_id) {
682     case PROP_CONTRAST:
683       d = g_value_get_double (value);
684       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
685           balance->contrast, d);
686       if (d != balance->contrast)
687         label = "CONTRAST";
688       balance->contrast = d;
689       break;
690     case PROP_BRIGHTNESS:
691       d = g_value_get_double (value);
692       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
693           balance->brightness, d);
694       if (d != balance->brightness)
695         label = "BRIGHTNESS";
696       balance->brightness = d;
697       break;
698     case PROP_HUE:
699       d = g_value_get_double (value);
700       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
701           d);
702       if (d != balance->hue)
703         label = "HUE";
704       balance->hue = d;
705       break;
706     case PROP_SATURATION:
707       d = g_value_get_double (value);
708       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
709           balance->saturation, d);
710       if (d != balance->saturation)
711         label = "SATURATION";
712       balance->saturation = d;
713       break;
714     default:
715       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
716       break;
717   }
718
719   GST_OBJECT_UNLOCK (balance);
720   gst_video_balance_update_properties (balance);
721
722   if (label) {
723     GstColorBalanceChannel *channel =
724         gst_video_balance_find_channel (balance, label);
725     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
726         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
727   }
728 }
729
730 static void
731 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
732     GParamSpec * pspec)
733 {
734   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
735
736   switch (prop_id) {
737     case PROP_CONTRAST:
738       g_value_set_double (value, balance->contrast);
739       break;
740     case PROP_BRIGHTNESS:
741       g_value_set_double (value, balance->brightness);
742       break;
743     case PROP_HUE:
744       g_value_set_double (value, balance->hue);
745       break;
746     case PROP_SATURATION:
747       g_value_set_double (value, balance->saturation);
748       break;
749     default:
750       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
751       break;
752   }
753 }