gst-indent
[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  *
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   for (i = 0; i < 256; i++) {
157     g_free (balance->tableu[i]);
158     g_free (balance->tablev[i]);
159   }
160   g_free (balance->tabley);
161   g_free (balance->tableu);
162   g_free (balance->tablev);
163
164   channels = balance->channels;
165
166   while (channels) {
167     GstColorBalanceChannel *channel = channels->data;
168
169     g_object_unref (channel);
170     channels = g_list_next (channels);
171   }
172
173   if (balance->channels)
174     g_list_free (balance->channels);
175
176   G_OBJECT_CLASS (parent_class)->dispose (object);
177 }
178
179 static void
180 gst_videobalance_class_init (gpointer g_class, gpointer class_data)
181 {
182   GObjectClass *gobject_class;
183   GstVideofilterClass *videofilter_class;
184
185   gobject_class = G_OBJECT_CLASS (g_class);
186   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
187
188   parent_class = g_type_class_ref (GST_TYPE_VIDEOFILTER);
189
190   g_object_class_install_property (gobject_class, ARG_CONTRAST,
191       g_param_spec_double ("contrast", "Contrast", "contrast",
192           0, 2, 1, G_PARAM_READWRITE));
193   g_object_class_install_property (gobject_class, ARG_BRIGHTNESS,
194       g_param_spec_double ("brightness", "Brightness", "brightness",
195           -1, 1, 0, G_PARAM_READWRITE));
196   g_object_class_install_property (gobject_class, ARG_HUE,
197       g_param_spec_double ("hue", "Hue", "hue", -1, 1, 0, G_PARAM_READWRITE));
198   g_object_class_install_property (gobject_class, ARG_SATURATION,
199       g_param_spec_double ("saturation", "Saturation", "saturation",
200           0, 2, 1, G_PARAM_READWRITE));
201
202   gobject_class->set_property = gst_videobalance_set_property;
203   gobject_class->get_property = gst_videobalance_get_property;
204   gobject_class->dispose = gst_videobalance_dispose;
205
206   videofilter_class->setup = gst_videobalance_setup;
207
208 #ifdef HAVE_LIBOIL
209   oil_init ();
210 #endif
211 }
212
213 static void
214 gst_videobalance_init (GTypeInstance * instance, gpointer g_class)
215 {
216   GstVideobalance *videobalance = GST_VIDEOBALANCE (instance);
217   GstVideofilter *videofilter;
218   char *channels[4] = { "HUE", "SATURATION",
219     "BRIGHTNESS", "CONTRAST"
220   };
221   gint i;
222
223   GST_DEBUG ("gst_videobalance_init");
224
225   videofilter = GST_VIDEOFILTER (videobalance);
226
227   /* do stuff */
228   videobalance->contrast = 1.0;
229   videobalance->brightness = 0.0;
230   videobalance->saturation = 1.0;
231   videobalance->hue = 0.0;
232
233   videobalance->needupdate = FALSE;
234   videofilter->passthru = TRUE;
235
236   videobalance->tabley = g_new (guint8, 256);
237   videobalance->tableu = g_new (guint8 *, 256);
238   videobalance->tablev = g_new (guint8 *, 256);
239   for (i = 0; i < 256; i++) {
240     videobalance->tableu[i] = g_new (guint8, 256);
241     videobalance->tablev[i] = g_new (guint8, 256);
242   }
243
244   /* Generate the channels list */
245   for (i = 0; i < (sizeof (channels) / sizeof (char *)); i++) {
246     GstColorBalanceChannel *channel;
247
248     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
249     channel->label = g_strdup (channels[i]);
250     channel->min_value = -1000;
251     channel->max_value = 1000;
252
253     videobalance->channels = g_list_append (videobalance->channels, channel);
254   }
255
256 }
257
258 static gboolean
259 gst_videobalance_interface_supported (GstImplementsInterface * iface,
260     GType type)
261 {
262   g_assert (type == GST_TYPE_COLOR_BALANCE);
263   return TRUE;
264 }
265
266 static void
267 gst_videobalance_interface_init (GstImplementsInterfaceClass * klass)
268 {
269   klass->supported = gst_videobalance_interface_supported;
270 }
271
272 static const GList *
273 gst_videobalance_colorbalance_list_channels (GstColorBalance * balance)
274 {
275   GstVideobalance *videobalance = GST_VIDEOBALANCE (balance);
276
277   g_return_val_if_fail (videobalance != NULL, NULL);
278   g_return_val_if_fail (GST_IS_VIDEOBALANCE (videobalance), NULL);
279
280   return videobalance->channels;
281 }
282
283 static void
284 gst_videobalance_colorbalance_set_value (GstColorBalance * balance,
285     GstColorBalanceChannel * channel, gint value)
286 {
287   GstVideobalance *vb = GST_VIDEOBALANCE (balance);
288   GstVideofilter *vf = GST_VIDEOFILTER (vb);
289
290   g_return_if_fail (vb != NULL);
291   g_return_if_fail (GST_IS_VIDEOBALANCE (vb));
292   g_return_if_fail (GST_IS_VIDEOFILTER (vf));
293   g_return_if_fail (channel->label != NULL);
294
295   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
296     vb->hue = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
297   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
298     vb->saturation = (value + 1000.0) * 2.0 / 2000.0;
299   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
300     vb->brightness = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
301   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
302     vb->contrast = (value + 1000.0) * 2.0 / 2000.0;
303   }
304
305   gst_videobalance_update_properties (vb);
306 }
307
308 static gint
309 gst_videobalance_colorbalance_get_value (GstColorBalance * balance,
310     GstColorBalanceChannel * channel)
311 {
312   GstVideobalance *vb = GST_VIDEOBALANCE (balance);
313   gint value = 0;
314
315   g_return_val_if_fail (vb != NULL, 0);
316   g_return_val_if_fail (GST_IS_VIDEOBALANCE (vb), 0);
317   g_return_val_if_fail (channel->label != NULL, 0);
318
319   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
320     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
321   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
322     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
323   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
324     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
325   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
326     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
327   }
328
329   return value;
330 }
331
332 static void
333 gst_videobalance_colorbalance_init (GstColorBalanceClass * iface)
334 {
335   GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
336   iface->list_channels = gst_videobalance_colorbalance_list_channels;
337   iface->set_value = gst_videobalance_colorbalance_set_value;
338   iface->get_value = gst_videobalance_colorbalance_get_value;
339 }
340
341 static void
342 gst_videobalance_update_properties (GstVideobalance * videobalance)
343 {
344   GstVideofilter *vf = GST_VIDEOFILTER (videobalance);
345
346   videobalance->needupdate = TRUE;
347
348   if (videobalance->contrast == 1.0 &&
349       videobalance->brightness == 0.0 &&
350       videobalance->hue == 0.0 && videobalance->saturation == 1.0) {
351     vf->passthru = TRUE;
352   } else {
353     vf->passthru = FALSE;
354   }
355 }
356
357 static void
358 gst_videobalance_set_property (GObject * object, guint prop_id,
359     const GValue * value, GParamSpec * pspec)
360 {
361   GstVideobalance *src;
362
363   /* it's not null if we got it, but it might not be ours */
364   g_return_if_fail (GST_IS_VIDEOBALANCE (object));
365   src = GST_VIDEOBALANCE (object);
366
367   GST_DEBUG ("gst_videobalance_set_property");
368   switch (prop_id) {
369     case ARG_CONTRAST:
370       src->contrast = g_value_get_double (value);
371       break;
372     case ARG_BRIGHTNESS:
373       src->brightness = g_value_get_double (value);
374       break;
375     case ARG_HUE:
376       src->hue = g_value_get_double (value);
377       break;
378     case ARG_SATURATION:
379       src->saturation = g_value_get_double (value);
380       break;
381     default:
382       break;
383   }
384
385   gst_videobalance_update_properties (src);
386 }
387
388 static void
389 gst_videobalance_get_property (GObject * object, guint prop_id, GValue * value,
390     GParamSpec * pspec)
391 {
392   GstVideobalance *src;
393
394   /* it's not null if we got it, but it might not be ours */
395   g_return_if_fail (GST_IS_VIDEOBALANCE (object));
396   src = GST_VIDEOBALANCE (object);
397
398   switch (prop_id) {
399     case ARG_CONTRAST:
400       g_value_set_double (value, src->contrast);
401       break;
402     case ARG_BRIGHTNESS:
403       g_value_set_double (value, src->brightness);
404       break;
405     case ARG_HUE:
406       g_value_set_double (value, src->hue);
407       break;
408     case ARG_SATURATION:
409       g_value_set_double (value, src->saturation);
410       break;
411     default:
412       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
413       break;
414   }
415 }
416
417 static gboolean
418 plugin_init (GstPlugin * plugin)
419 {
420   if (!gst_library_load ("gstvideofilter"))
421     return FALSE;
422
423   return gst_element_register (plugin, "videobalance", GST_RANK_NONE,
424       GST_TYPE_VIDEOBALANCE);
425 }
426
427 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
428     GST_VERSION_MINOR,
429     "videobalance",
430     "Changes hue, saturation, brightness etc. on video images",
431     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)
432
433      static void gst_videobalance_setup (GstVideofilter * videofilter)
434 {
435   GstVideobalance *videobalance;
436
437   g_return_if_fail (GST_IS_VIDEOBALANCE (videofilter));
438   videobalance = GST_VIDEOBALANCE (videofilter);
439
440   /* if any setup needs to be done, do it here */
441
442 }
443
444 /*
445  * look-up tables (LUT).
446  */
447
448 static void
449 gst_videobalance_update_tables_planar411 (GstVideobalance * vb)
450 {
451   gint i, j;
452   gdouble y, u, v, hue_cos, hue_sin;
453
454   /* Y */
455   for (i = 0; i < 256; i++) {
456     y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
457     if (y < 0)
458       y = 0;
459     else if (y > 255)
460       y = 255;
461     vb->tabley[i] = rint (y);
462   }
463
464   /* FIXME this is a bogus transformation for hue, but you get
465    * the idea */
466   hue_cos = cos (M_PI * vb->hue);
467   hue_sin = sin (M_PI * vb->hue);
468
469   /* U/V lookup tables are 2D, since we need both U/V for each table
470    * separately. */
471   for (i = -128; i < 128; i++) {
472     for (j = -128; j < 128; j++) {
473       u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
474       v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
475       if (u < 0)
476         u = 0;
477       else if (u > 255)
478         u = 255;
479       if (v < 0)
480         v = 0;
481       else if (v > 255)
482         v = 255;
483       vb->tableu[i + 128][j + 128] = rint (u);
484       vb->tablev[i + 128][j + 128] = rint (v);
485     }
486   }
487 }
488
489 #ifndef HAVE_LIBOIL
490 void
491 tablelookup_u8 (guint8 * dest, int dstr, guint8 * src, int sstr,
492     guint8 * table, int tstr, int n)
493 {
494   int i;
495
496   for (i = 0; i < n; i++) {
497     *dest = table[*src * tstr];
498     dest += dstr;
499     src += sstr;
500   }
501 }
502 #endif
503
504 static void
505 gst_videobalance_planar411 (GstVideofilter * videofilter, void *dest, void *src)
506 {
507   GstVideobalance *videobalance;
508   int width;
509   int height;
510   int x, y;
511
512   g_return_if_fail (GST_IS_VIDEOBALANCE (videofilter));
513   videobalance = GST_VIDEOBALANCE (videofilter);
514
515   if (videobalance->needupdate) {
516     gst_videobalance_update_tables_planar411 (videobalance);
517     videobalance->needupdate = FALSE;
518   }
519
520   width = videofilter->from_width;
521   height = videofilter->from_height;
522
523   {
524     guint8 *cdest = dest;
525     guint8 *csrc = src;
526
527     for (y = 0; y < height; y++) {
528       tablelookup_u8 (cdest + y * width, 1, csrc + y * width, 1,
529           videobalance->tabley, 1, width);
530     }
531   }
532
533   {
534     gint u1, v1;
535     guint8 *usrc, *vsrc;
536     guint8 *udest, *vdest;
537
538     usrc = src + width * height;
539     udest = dest + width * height;
540     vsrc = src + width * height + (width / 2) * (height / 2);
541     vdest = dest + width * height + (width / 2) * (height / 2);
542
543     for (y = 0; y < height / 2; y++) {
544       for (x = 0; x < width / 2; x++) {
545         u1 = usrc[y * (width / 2) + x];
546         v1 = vsrc[y * (width / 2) + x];
547         udest[y * (width / 2) + x] = videobalance->tableu[u1][v1];
548         vdest[y * (width / 2) + x] = videobalance->tablev[u1][v1];
549       }
550     }
551   }
552
553 }