0825aa46ffe0055eaa71ab66e8f20bd996bd3a5b
[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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * This file was (probably) generated from gstvideobalance.c,
23  * gstvideobalance.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /*#define DEBUG_ENABLED */
31 #include "gstvideobalance.h"
32 #ifdef HAVE_LIBOIL
33 #include <liboil/liboil.h>
34 #endif
35 #include <string.h>
36 #include <math.h>
37
38 #include <gst/colorbalance/colorbalance.h>
39
40 /* GstVideobalance signals and args */
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum
48 {
49   ARG_0,
50   ARG_CONTRAST,
51   ARG_BRIGHTNESS,
52   ARG_HUE,
53   ARG_SATURATION
54       /* FILL ME */
55 };
56
57 static GstVideofilterClass *parent_class = NULL;
58
59 static void gst_videobalance_base_init (gpointer g_class);
60 static void gst_videobalance_class_init (gpointer g_class, gpointer class_data);
61 static void gst_videobalance_init (GTypeInstance * instance, gpointer g_class);
62
63 static void gst_videobalance_set_property (GObject * object, guint prop_id,
64     const GValue * value, GParamSpec * pspec);
65 static void gst_videobalance_get_property (GObject * object, guint prop_id,
66     GValue * value, GParamSpec * pspec);
67
68 static void gst_videobalance_planar411 (GstVideofilter * videofilter,
69     void *dest, void *src);
70 static void gst_videobalance_setup (GstVideofilter * videofilter);
71
72 static void gst_videobalance_interface_init (GstImplementsInterfaceClass *
73     klass);
74 static void gst_videobalance_colorbalance_init (GstColorBalanceClass * iface);
75
76 static void gst_videobalance_dispose (GObject * object);
77 static void gst_videobalance_update_properties (GstVideobalance * videobalance);
78
79 GType
80 gst_videobalance_get_type (void)
81 {
82   static GType videobalance_type = 0;
83
84   if (!videobalance_type) {
85     static const GTypeInfo videobalance_info = {
86       sizeof (GstVideobalanceClass),
87       gst_videobalance_base_init,
88       NULL,
89       gst_videobalance_class_init,
90       NULL,
91       NULL,
92       sizeof (GstVideobalance),
93       0,
94       gst_videobalance_init,
95     };
96
97     static const GInterfaceInfo iface_info = {
98       (GInterfaceInitFunc) gst_videobalance_interface_init,
99       NULL,
100       NULL,
101     };
102
103     static const GInterfaceInfo colorbalance_info = {
104       (GInterfaceInitFunc) gst_videobalance_colorbalance_init,
105       NULL,
106       NULL,
107     };
108
109     videobalance_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
110         "GstVideobalance", &videobalance_info, 0);
111
112     g_type_add_interface_static (videobalance_type,
113         GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
114     g_type_add_interface_static (videobalance_type, GST_TYPE_COLOR_BALANCE,
115         &colorbalance_info);
116   }
117   return videobalance_type;
118 }
119
120 static GstVideofilterFormat gst_videobalance_formats[] = {
121   {"I420", 12, gst_videobalance_planar411,},
122 };
123
124
125 static void
126 gst_videobalance_base_init (gpointer g_class)
127 {
128   static GstElementDetails videobalance_details =
129       GST_ELEMENT_DETAILS ("Video Balance Control",
130       "Filter/Effect/Video",
131       "Adjusts brightness, contrast, hue, saturation on a video stream",
132       "David Schleef <ds@schleef.org>");
133   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
134   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
135   int i;
136
137   gst_element_class_set_details (element_class, &videobalance_details);
138
139   for (i = 0; i < G_N_ELEMENTS (gst_videobalance_formats); i++) {
140     gst_videofilter_class_add_format (videofilter_class,
141         gst_videobalance_formats + i);
142   }
143
144   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
145 }
146
147 static void
148 gst_videobalance_dispose (GObject * object)
149 {
150   GList *channels = NULL;
151   GstVideobalance *balance;
152   gint i;
153
154   balance = GST_VIDEOBALANCE (object);
155
156   if (balance->tableu) {
157     for (i = 0; i < 256; i++)
158       g_free (balance->tableu[i]);
159     g_free (balance->tableu);
160     balance->tableu = NULL;
161   }
162
163   if (balance->tablev) {
164     for (i = 0; i < 256; i++)
165       g_free (balance->tablev[i]);
166     g_free (balance->tablev);
167     balance->tablev = NULL;
168   }
169
170   if (balance->tabley) {
171     g_free (balance->tabley);
172     balance->tabley = NULL;
173   }
174
175   channels = balance->channels;
176   while (channels) {
177     GstColorBalanceChannel *channel = channels->data;
178
179     g_object_unref (channel);
180     channels->data = NULL;
181     channels = g_list_next (channels);
182   }
183
184   if (balance->channels)
185     g_list_free (balance->channels);
186
187   G_OBJECT_CLASS (parent_class)->dispose (object);
188 }
189
190 static void
191 gst_videobalance_class_init (gpointer g_class, gpointer class_data)
192 {
193   GObjectClass *gobject_class;
194   GstVideofilterClass *videofilter_class;
195
196   gobject_class = G_OBJECT_CLASS (g_class);
197   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
198
199   parent_class = g_type_class_ref (GST_TYPE_VIDEOFILTER);
200
201   gobject_class->set_property = gst_videobalance_set_property;
202   gobject_class->get_property = gst_videobalance_get_property;
203
204   g_object_class_install_property (gobject_class, ARG_CONTRAST,
205       g_param_spec_double ("contrast", "Contrast", "contrast",
206           0, 2, 1, G_PARAM_READWRITE));
207   g_object_class_install_property (gobject_class, ARG_BRIGHTNESS,
208       g_param_spec_double ("brightness", "Brightness", "brightness",
209           -1, 1, 0, G_PARAM_READWRITE));
210   g_object_class_install_property (gobject_class, ARG_HUE,
211       g_param_spec_double ("hue", "Hue", "hue", -1, 1, 0, G_PARAM_READWRITE));
212   g_object_class_install_property (gobject_class, ARG_SATURATION,
213       g_param_spec_double ("saturation", "Saturation", "saturation",
214           0, 2, 1, G_PARAM_READWRITE));
215
216   gobject_class->dispose = gst_videobalance_dispose;
217
218   videofilter_class->setup = gst_videobalance_setup;
219
220 #ifdef HAVE_LIBOIL
221   oil_init ();
222 #endif
223 }
224
225 static void
226 gst_videobalance_init (GTypeInstance * instance, gpointer g_class)
227 {
228   GstVideobalance *videobalance = GST_VIDEOBALANCE (instance);
229   GstVideofilter *videofilter;
230   char *channels[4] = { "HUE", "SATURATION",
231     "BRIGHTNESS", "CONTRAST"
232   };
233   gint i;
234
235   GST_DEBUG ("gst_videobalance_init");
236
237   videofilter = GST_VIDEOFILTER (videobalance);
238
239   /* do stuff */
240   videobalance->contrast = 1.0;
241   videobalance->brightness = 0.0;
242   videobalance->saturation = 1.0;
243   videobalance->hue = 0.0;
244
245   videobalance->needupdate = FALSE;
246   videofilter->passthru = TRUE;
247
248   videobalance->tabley = g_new (guint8, 256);
249   videobalance->tableu = g_new (guint8 *, 256);
250   videobalance->tablev = g_new (guint8 *, 256);
251   for (i = 0; i < 256; i++) {
252     videobalance->tableu[i] = g_new (guint8, 256);
253     videobalance->tablev[i] = g_new (guint8, 256);
254   }
255
256   /* Generate the channels list */
257   for (i = 0; i < (sizeof (channels) / sizeof (char *)); i++) {
258     GstColorBalanceChannel *channel;
259
260     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
261     channel->label = g_strdup (channels[i]);
262     channel->min_value = -1000;
263     channel->max_value = 1000;
264
265     videobalance->channels = g_list_append (videobalance->channels, channel);
266   }
267
268 }
269
270 static gboolean
271 gst_videobalance_interface_supported (GstImplementsInterface * iface,
272     GType type)
273 {
274   g_assert (type == GST_TYPE_COLOR_BALANCE);
275   return TRUE;
276 }
277
278 static void
279 gst_videobalance_interface_init (GstImplementsInterfaceClass * klass)
280 {
281   klass->supported = gst_videobalance_interface_supported;
282 }
283
284 static const GList *
285 gst_videobalance_colorbalance_list_channels (GstColorBalance * balance)
286 {
287   GstVideobalance *videobalance = GST_VIDEOBALANCE (balance);
288
289   g_return_val_if_fail (videobalance != NULL, NULL);
290   g_return_val_if_fail (GST_IS_VIDEOBALANCE (videobalance), NULL);
291
292   return videobalance->channels;
293 }
294
295 static void
296 gst_videobalance_colorbalance_set_value (GstColorBalance * balance,
297     GstColorBalanceChannel * channel, gint value)
298 {
299   GstVideobalance *vb = GST_VIDEOBALANCE (balance);
300
301   g_return_if_fail (vb != NULL);
302   g_return_if_fail (GST_IS_VIDEOBALANCE (vb));
303   g_return_if_fail (GST_IS_VIDEOFILTER (vb));
304   g_return_if_fail (channel->label != NULL);
305
306   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
307     vb->hue = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
308   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
309     vb->saturation = (value + 1000.0) * 2.0 / 2000.0;
310   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
311     vb->brightness = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
312   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
313     vb->contrast = (value + 1000.0) * 2.0 / 2000.0;
314   }
315
316   gst_videobalance_update_properties (vb);
317 }
318
319 static gint
320 gst_videobalance_colorbalance_get_value (GstColorBalance * balance,
321     GstColorBalanceChannel * channel)
322 {
323   GstVideobalance *vb = GST_VIDEOBALANCE (balance);
324   gint value = 0;
325
326   g_return_val_if_fail (vb != NULL, 0);
327   g_return_val_if_fail (GST_IS_VIDEOBALANCE (vb), 0);
328   g_return_val_if_fail (channel->label != NULL, 0);
329
330   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
331     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
332   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
333     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
334   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
335     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
336   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
337     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
338   }
339
340   return value;
341 }
342
343 static void
344 gst_videobalance_colorbalance_init (GstColorBalanceClass * iface)
345 {
346   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
347   iface->list_channels = gst_videobalance_colorbalance_list_channels;
348   iface->set_value = gst_videobalance_colorbalance_set_value;
349   iface->get_value = gst_videobalance_colorbalance_get_value;
350 }
351
352 static void
353 gst_videobalance_update_properties (GstVideobalance * videobalance)
354 {
355   GstVideofilter *vf = GST_VIDEOFILTER (videobalance);
356
357   videobalance->needupdate = TRUE;
358
359   if (videobalance->contrast == 1.0 &&
360       videobalance->brightness == 0.0 &&
361       videobalance->hue == 0.0 && videobalance->saturation == 1.0) {
362     vf->passthru = TRUE;
363   } else {
364     vf->passthru = FALSE;
365   }
366 }
367
368 static void
369 gst_videobalance_set_property (GObject * object, guint prop_id,
370     const GValue * value, GParamSpec * pspec)
371 {
372   GstVideobalance *src;
373
374   g_return_if_fail (GST_IS_VIDEOBALANCE (object));
375   src = GST_VIDEOBALANCE (object);
376
377   GST_DEBUG ("gst_videobalance_set_property");
378   switch (prop_id) {
379     case ARG_CONTRAST:
380       src->contrast = g_value_get_double (value);
381       break;
382     case ARG_BRIGHTNESS:
383       src->brightness = g_value_get_double (value);
384       break;
385     case ARG_HUE:
386       src->hue = g_value_get_double (value);
387       break;
388     case ARG_SATURATION:
389       src->saturation = g_value_get_double (value);
390       break;
391     default:
392       break;
393   }
394
395   gst_videobalance_update_properties (src);
396 }
397
398 static void
399 gst_videobalance_get_property (GObject * object, guint prop_id, GValue * value,
400     GParamSpec * pspec)
401 {
402   GstVideobalance *src;
403
404   g_return_if_fail (GST_IS_VIDEOBALANCE (object));
405   src = GST_VIDEOBALANCE (object);
406
407   switch (prop_id) {
408     case ARG_CONTRAST:
409       g_value_set_double (value, src->contrast);
410       break;
411     case ARG_BRIGHTNESS:
412       g_value_set_double (value, src->brightness);
413       break;
414     case ARG_HUE:
415       g_value_set_double (value, src->hue);
416       break;
417     case ARG_SATURATION:
418       g_value_set_double (value, src->saturation);
419       break;
420     default:
421       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
422       break;
423   }
424 }
425
426 static gboolean
427 plugin_init (GstPlugin * plugin)
428 {
429   if (!gst_library_load ("gstvideofilter"))
430     return FALSE;
431
432   return gst_element_register (plugin, "videobalance", GST_RANK_NONE,
433       GST_TYPE_VIDEOBALANCE);
434 }
435
436 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
437     GST_VERSION_MINOR,
438     "videobalance",
439     "Changes hue, saturation, brightness etc. on video images",
440     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
441
442 static void
443 gst_videobalance_setup (GstVideofilter * videofilter)
444 {
445   GstVideobalance *videobalance;
446
447   g_return_if_fail (GST_IS_VIDEOBALANCE (videofilter));
448   videobalance = GST_VIDEOBALANCE (videofilter);
449
450   /* if any setup needs to be done, do it here */
451
452 }
453
454 /*
455  * look-up tables (LUT).
456  */
457
458 static void
459 gst_videobalance_update_tables_planar411 (GstVideobalance * vb)
460 {
461   gint i, j;
462   gdouble y, u, v, hue_cos, hue_sin;
463
464   /* Y */
465   for (i = 0; i < 256; i++) {
466     y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
467     if (y < 0)
468       y = 0;
469     else if (y > 255)
470       y = 255;
471     vb->tabley[i] = rint (y);
472   }
473
474   /* FIXME this is a bogus transformation for hue, but you get
475    * the idea */
476   hue_cos = cos (M_PI * vb->hue);
477   hue_sin = sin (M_PI * vb->hue);
478
479   /* U/V lookup tables are 2D, since we need both U/V for each table
480    * separately. */
481   for (i = -128; i < 128; i++) {
482     for (j = -128; j < 128; j++) {
483       u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
484       v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
485       if (u < 0)
486         u = 0;
487       else if (u > 255)
488         u = 255;
489       if (v < 0)
490         v = 0;
491       else if (v > 255)
492         v = 255;
493       vb->tableu[i + 128][j + 128] = rint (u);
494       vb->tablev[i + 128][j + 128] = rint (v);
495     }
496   }
497 }
498
499 #ifndef HAVE_LIBOIL
500 void
501 oil_tablelookup_u8 (guint8 * dest, int dstr, guint8 * src, int sstr,
502     guint8 * table, int tstr, int n)
503 {
504   int i;
505
506   for (i = 0; i < n; i++) {
507     *dest = table[*src * tstr];
508     dest += dstr;
509     src += sstr;
510   }
511 }
512 #endif
513
514 static void
515 gst_videobalance_planar411 (GstVideofilter * videofilter, void *dest, void *src)
516 {
517   GstVideobalance *videobalance;
518   int width;
519   int height;
520   int x, y;
521
522   g_return_if_fail (GST_IS_VIDEOBALANCE (videofilter));
523   videobalance = GST_VIDEOBALANCE (videofilter);
524
525   if (videobalance->needupdate) {
526     gst_videobalance_update_tables_planar411 (videobalance);
527     videobalance->needupdate = FALSE;
528   }
529
530   width = videofilter->from_width;
531   height = videofilter->from_height;
532
533   {
534     guint8 *cdest = dest;
535     guint8 *csrc = src;
536
537     for (y = 0; y < height; y++) {
538       oil_tablelookup_u8 (cdest + y * width, 1, csrc + y * width, 1,
539           videobalance->tabley, 1, width);
540     }
541   }
542
543   {
544     gint u1, v1;
545     guint8 *usrc, *vsrc;
546     guint8 *udest, *vdest;
547
548     usrc = src + width * height;
549     udest = dest + width * height;
550     vsrc = src + width * height + (width / 2) * (height / 2);
551     vdest = dest + width * height + (width / 2) * (height / 2);
552
553     for (y = 0; y < height / 2; y++) {
554       for (x = 0; x < width / 2; x++) {
555         u1 = usrc[y * (width / 2) + x];
556         v1 = vsrc[y * (width / 2) + x];
557         udest[y * (width / 2) + x] = videobalance->tableu[u1][v1];
558         vdest[y * (width / 2) + x] = videobalance->tablev[u1][v1];
559       }
560     }
561   }
562
563 }