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