Merge branch 'dtmf-moved-from-bad'
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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-1.0 videotestsrc ! videobalance saturation=0.0 ! videoconvert ! 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, NV12, NV21 }"))
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, NV12, NV21 }"))
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_semiplanar_yuv (GstVideoBalance * videobalance,
228     GstVideoFrame * frame)
229 {
230   gint x, y;
231   guint8 *ydata;
232   guint8 *uvdata;
233   gint ystride, uvstride;
234   gint width, height;
235   gint width2, height2;
236   guint8 *tabley = videobalance->tabley;
237   guint8 **tableu = videobalance->tableu;
238   guint8 **tablev = videobalance->tablev;
239   gint upos, vpos;
240
241   width = GST_VIDEO_FRAME_WIDTH (frame);
242   height = GST_VIDEO_FRAME_HEIGHT (frame);
243
244   ydata = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
245   ystride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
246
247   for (y = 0; y < height; y++) {
248     guint8 *yptr;
249
250     yptr = ydata + y * ystride;
251     for (x = 0; x < width; x++) {
252       *yptr = tabley[*yptr];
253       yptr++;
254     }
255   }
256
257   width2 = GST_VIDEO_FRAME_COMP_WIDTH (frame, 1);
258   height2 = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 1);
259
260   uvdata = GST_VIDEO_FRAME_PLANE_DATA (frame, 1);
261   uvstride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 1);
262
263   upos = GST_VIDEO_INFO_FORMAT (&frame->info) == GST_VIDEO_FORMAT_NV12 ? 0 : 1;
264   vpos = GST_VIDEO_INFO_FORMAT (&frame->info) == GST_VIDEO_FORMAT_NV12 ? 1 : 0;
265
266   for (y = 0; y < height2; y++) {
267     guint8 *uvptr;
268     guint8 u1, v1;
269
270     uvptr = uvdata + y * uvstride;
271
272     for (x = 0; x < width2; x++) {
273       u1 = uvptr[upos];
274       v1 = uvptr[vpos];
275
276       uvptr[upos] = tableu[u1][v1];
277       uvptr[vpos] = tablev[u1][v1];
278       uvptr += 2;
279     }
280   }
281 }
282
283 static void
284 gst_video_balance_packed_yuv (GstVideoBalance * videobalance,
285     GstVideoFrame * frame)
286 {
287   gint x, y, stride;
288   guint8 *ydata, *udata, *vdata;
289   gint yoff, uoff, voff;
290   gint width, height;
291   gint width2, height2;
292   guint8 *tabley = videobalance->tabley;
293   guint8 **tableu = videobalance->tableu;
294   guint8 **tablev = videobalance->tablev;
295
296   width = GST_VIDEO_FRAME_WIDTH (frame);
297   height = GST_VIDEO_FRAME_HEIGHT (frame);
298
299   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
300   ydata = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
301   yoff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
302
303   for (y = 0; y < height; y++) {
304     guint8 *yptr;
305
306     yptr = ydata + y * stride;
307     for (x = 0; x < width; x++) {
308       *yptr = tabley[*yptr];
309       yptr += yoff;
310     }
311   }
312
313   width2 = GST_VIDEO_FRAME_COMP_WIDTH (frame, 1);
314   height2 = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 1);
315
316   udata = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
317   vdata = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
318   uoff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 1);
319   voff = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 2);
320
321   for (y = 0; y < height2; y++) {
322     guint8 *uptr, *vptr;
323     guint8 u1, v1;
324
325     uptr = udata + y * stride;
326     vptr = vdata + y * stride;
327
328     for (x = 0; x < width2; x++) {
329       u1 = *uptr;
330       v1 = *vptr;
331
332       *uptr = tableu[u1][v1];
333       *vptr = tablev[u1][v1];
334
335       uptr += uoff;
336       vptr += voff;
337     }
338   }
339 }
340
341 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
342   298, 0, 409, -57068,
343   298, -100, -208, 34707,
344   298, 516, 0, -70870,
345 };
346
347 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
348   66, 129, 25, 4096,
349   -38, -74, 112, 32768,
350   112, -94, -18, 32768,
351 };
352
353 #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)
354
355 static void
356 gst_video_balance_packed_rgb (GstVideoBalance * videobalance,
357     GstVideoFrame * frame)
358 {
359   gint i, j, height;
360   gint width, stride, row_wrap;
361   gint pixel_stride;
362   guint8 *data;
363   gint offsets[3];
364   gint r, g, b;
365   gint y, u, v;
366   gint u_tmp, v_tmp;
367   guint8 *tabley = videobalance->tabley;
368   guint8 **tableu = videobalance->tableu;
369   guint8 **tablev = videobalance->tablev;
370
371   width = GST_VIDEO_FRAME_WIDTH (frame);
372   height = GST_VIDEO_FRAME_HEIGHT (frame);
373
374   offsets[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
375   offsets[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
376   offsets[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
377
378   data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
379   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
380
381   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
382   row_wrap = stride - pixel_stride * width;
383
384   for (i = 0; i < height; i++) {
385     for (j = 0; j < width; j++) {
386       r = data[offsets[0]];
387       g = data[offsets[1]];
388       b = data[offsets[2]];
389
390       y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
391       u_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
392       v_tmp = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
393
394       y = CLAMP (y, 0, 255);
395       u_tmp = CLAMP (u_tmp, 0, 255);
396       v_tmp = CLAMP (v_tmp, 0, 255);
397
398       y = tabley[y];
399       u = tableu[u_tmp][v_tmp];
400       v = tablev[u_tmp][v_tmp];
401
402       r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
403       g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
404       b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
405
406       data[offsets[0]] = CLAMP (r, 0, 255);
407       data[offsets[1]] = CLAMP (g, 0, 255);
408       data[offsets[2]] = CLAMP (b, 0, 255);
409       data += pixel_stride;
410     }
411     data += row_wrap;
412   }
413 }
414
415 /* get notified of caps and plug in the correct process function */
416 static gboolean
417 gst_video_balance_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
418     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
419 {
420   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (vfilter);
421
422   GST_DEBUG_OBJECT (videobalance,
423       "in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps);
424
425   videobalance->process = NULL;
426
427   switch (GST_VIDEO_INFO_FORMAT (in_info)) {
428     case GST_VIDEO_FORMAT_I420:
429     case GST_VIDEO_FORMAT_YV12:
430     case GST_VIDEO_FORMAT_Y41B:
431     case GST_VIDEO_FORMAT_Y42B:
432     case GST_VIDEO_FORMAT_Y444:
433       videobalance->process = gst_video_balance_planar_yuv;
434       break;
435     case GST_VIDEO_FORMAT_YUY2:
436     case GST_VIDEO_FORMAT_UYVY:
437     case GST_VIDEO_FORMAT_AYUV:
438     case GST_VIDEO_FORMAT_YVYU:
439       videobalance->process = gst_video_balance_packed_yuv;
440       break;
441     case GST_VIDEO_FORMAT_NV12:
442     case GST_VIDEO_FORMAT_NV21:
443       videobalance->process = gst_video_balance_semiplanar_yuv;
444       break;
445     case GST_VIDEO_FORMAT_ARGB:
446     case GST_VIDEO_FORMAT_ABGR:
447     case GST_VIDEO_FORMAT_RGBA:
448     case GST_VIDEO_FORMAT_BGRA:
449     case GST_VIDEO_FORMAT_xRGB:
450     case GST_VIDEO_FORMAT_xBGR:
451     case GST_VIDEO_FORMAT_RGBx:
452     case GST_VIDEO_FORMAT_BGRx:
453     case GST_VIDEO_FORMAT_RGB:
454     case GST_VIDEO_FORMAT_BGR:
455       videobalance->process = gst_video_balance_packed_rgb;
456       break;
457     default:
458       goto unknown_format;
459       break;
460   }
461
462   return TRUE;
463
464   /* ERRORS */
465 unknown_format:
466   {
467     GST_ERROR_OBJECT (videobalance, "unknown format %" GST_PTR_FORMAT, incaps);
468     return FALSE;
469   }
470 }
471
472 static void
473 gst_video_balance_before_transform (GstBaseTransform * base, GstBuffer * buf)
474 {
475   GstVideoBalance *balance = GST_VIDEO_BALANCE (base);
476   GstClockTime timestamp, stream_time;
477
478   timestamp = GST_BUFFER_TIMESTAMP (buf);
479   stream_time =
480       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
481
482   GST_DEBUG_OBJECT (balance, "sync to %" GST_TIME_FORMAT,
483       GST_TIME_ARGS (timestamp));
484
485   if (GST_CLOCK_TIME_IS_VALID (stream_time))
486     gst_object_sync_values (GST_OBJECT (balance), stream_time);
487 }
488
489 static GstFlowReturn
490 gst_video_balance_transform_frame_ip (GstVideoFilter * vfilter,
491     GstVideoFrame * frame)
492 {
493   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (vfilter);
494
495   if (!videobalance->process)
496     goto not_negotiated;
497
498   GST_OBJECT_LOCK (videobalance);
499   videobalance->process (videobalance, frame);
500   GST_OBJECT_UNLOCK (videobalance);
501
502   return GST_FLOW_OK;
503
504   /* ERRORS */
505 not_negotiated:
506   {
507     GST_ERROR_OBJECT (videobalance, "Not negotiated yet");
508     return GST_FLOW_NOT_NEGOTIATED;
509   }
510 }
511
512 static void
513 gst_video_balance_finalize (GObject * object)
514 {
515   GList *channels = NULL;
516   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
517
518   g_free (balance->tableu[0]);
519
520   channels = balance->channels;
521   while (channels) {
522     GstColorBalanceChannel *channel = channels->data;
523
524     g_object_unref (channel);
525     channels->data = NULL;
526     channels = g_list_next (channels);
527   }
528
529   if (balance->channels)
530     g_list_free (balance->channels);
531
532   G_OBJECT_CLASS (parent_class)->finalize (object);
533 }
534
535 static void
536 gst_video_balance_class_init (GstVideoBalanceClass * klass)
537 {
538   GObjectClass *gobject_class = (GObjectClass *) klass;
539   GstElementClass *gstelement_class = (GstElementClass *) klass;
540   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
541   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) 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_static_metadata (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->before_transform =
577       GST_DEBUG_FUNCPTR (gst_video_balance_before_transform);
578   trans_class->transform_ip_on_passthrough = FALSE;
579
580   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_balance_set_info);
581   vfilter_class->transform_frame_ip =
582       GST_DEBUG_FUNCPTR (gst_video_balance_transform_frame_ip);
583 }
584
585 static void
586 gst_video_balance_init (GstVideoBalance * videobalance)
587 {
588   const gchar *channels[4] = { "HUE", "SATURATION",
589     "BRIGHTNESS", "CONTRAST"
590   };
591   gint i;
592
593   /* Initialize propertiews */
594   videobalance->contrast = DEFAULT_PROP_CONTRAST;
595   videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
596   videobalance->hue = DEFAULT_PROP_HUE;
597   videobalance->saturation = DEFAULT_PROP_SATURATION;
598
599   videobalance->tableu[0] = g_new (guint8, 256 * 256 * 2);
600   for (i = 0; i < 256; i++) {
601     videobalance->tableu[i] =
602         videobalance->tableu[0] + i * 256 * sizeof (guint8);
603     videobalance->tablev[i] =
604         videobalance->tableu[0] + 256 * 256 * sizeof (guint8) +
605         i * 256 * sizeof (guint8);
606   }
607
608   gst_video_balance_update_properties (videobalance);
609
610   /* Generate the channels list */
611   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
612     GstColorBalanceChannel *channel;
613
614     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
615     channel->label = g_strdup (channels[i]);
616     channel->min_value = -1000;
617     channel->max_value = 1000;
618
619     videobalance->channels = g_list_append (videobalance->channels, channel);
620   }
621 }
622
623 static const GList *
624 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
625 {
626   GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
627
628   g_return_val_if_fail (videobalance != NULL, NULL);
629   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
630
631   return videobalance->channels;
632 }
633
634 static void
635 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
636     GstColorBalanceChannel * channel, gint value)
637 {
638   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
639   gdouble new_val;
640   gboolean changed = FALSE;
641
642   g_return_if_fail (vb != NULL);
643   g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
644   g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
645   g_return_if_fail (channel->label != NULL);
646
647   GST_OBJECT_LOCK (vb);
648   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
649     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
650     changed = new_val != vb->hue;
651     vb->hue = new_val;
652   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
653     new_val = (value + 1000.0) * 2.0 / 2000.0;
654     changed = new_val != vb->saturation;
655     vb->saturation = new_val;
656   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
657     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
658     changed = new_val != vb->brightness;
659     vb->brightness = new_val;
660   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
661     new_val = (value + 1000.0) * 2.0 / 2000.0;
662     changed = new_val != vb->contrast;
663     vb->contrast = new_val;
664   }
665   GST_OBJECT_UNLOCK (vb);
666
667   if (changed)
668     gst_video_balance_update_properties (vb);
669
670   if (changed) {
671     gst_color_balance_value_changed (balance, channel,
672         gst_color_balance_get_value (balance, channel));
673   }
674 }
675
676 static gint
677 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
678     GstColorBalanceChannel * channel)
679 {
680   GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
681   gint value = 0;
682
683   g_return_val_if_fail (vb != NULL, 0);
684   g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
685   g_return_val_if_fail (channel->label != NULL, 0);
686
687   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
688     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
689   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
690     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
691   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
692     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
693   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
694     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
695   }
696
697   return value;
698 }
699
700 static GstColorBalanceType
701 gst_video_balance_colorbalance_get_balance_type (GstColorBalance * balance)
702 {
703   return GST_COLOR_BALANCE_SOFTWARE;
704 }
705
706 static void
707 gst_video_balance_colorbalance_init (GstColorBalanceInterface * iface)
708 {
709   iface->list_channels = gst_video_balance_colorbalance_list_channels;
710   iface->set_value = gst_video_balance_colorbalance_set_value;
711   iface->get_value = gst_video_balance_colorbalance_get_value;
712   iface->get_balance_type = gst_video_balance_colorbalance_get_balance_type;
713 }
714
715 static GstColorBalanceChannel *
716 gst_video_balance_find_channel (GstVideoBalance * balance, const gchar * label)
717 {
718   GList *l;
719
720   for (l = balance->channels; l; l = l->next) {
721     GstColorBalanceChannel *channel = l->data;
722
723     if (g_ascii_strcasecmp (channel->label, label) == 0)
724       return channel;
725   }
726   return NULL;
727 }
728
729 static void
730 gst_video_balance_set_property (GObject * object, guint prop_id,
731     const GValue * value, GParamSpec * pspec)
732 {
733   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
734   gdouble d;
735   const gchar *label = NULL;
736
737   GST_OBJECT_LOCK (balance);
738   switch (prop_id) {
739     case PROP_CONTRAST:
740       d = g_value_get_double (value);
741       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
742           balance->contrast, d);
743       if (d != balance->contrast)
744         label = "CONTRAST";
745       balance->contrast = d;
746       break;
747     case PROP_BRIGHTNESS:
748       d = g_value_get_double (value);
749       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
750           balance->brightness, d);
751       if (d != balance->brightness)
752         label = "BRIGHTNESS";
753       balance->brightness = d;
754       break;
755     case PROP_HUE:
756       d = g_value_get_double (value);
757       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
758           d);
759       if (d != balance->hue)
760         label = "HUE";
761       balance->hue = d;
762       break;
763     case PROP_SATURATION:
764       d = g_value_get_double (value);
765       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
766           balance->saturation, d);
767       if (d != balance->saturation)
768         label = "SATURATION";
769       balance->saturation = d;
770       break;
771     default:
772       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
773       break;
774   }
775
776   GST_OBJECT_UNLOCK (balance);
777   gst_video_balance_update_properties (balance);
778
779   if (label) {
780     GstColorBalanceChannel *channel =
781         gst_video_balance_find_channel (balance, label);
782     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
783         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
784   }
785 }
786
787 static void
788 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
789     GParamSpec * pspec)
790 {
791   GstVideoBalance *balance = GST_VIDEO_BALANCE (object);
792
793   switch (prop_id) {
794     case PROP_CONTRAST:
795       g_value_set_double (value, balance->contrast);
796       break;
797     case PROP_BRIGHTNESS:
798       g_value_set_double (value, balance->brightness);
799       break;
800     case PROP_HUE:
801       g_value_set_double (value, balance->hue);
802       break;
803     case PROP_SATURATION:
804       g_value_set_double (value, balance->saturation);
805       break;
806     default:
807       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
808       break;
809   }
810 }