2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
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.
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.
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.
22 * This file was (probably) generated from gstvideobalance.c,
23 * gstvideobalance.c,v 1.7 2003/11/08 02:48:59 dschleef Exp
27 * SECTION:element-videobalance
29 * Adjusts brightness, contrast, hue, saturation on a video stream.
32 * <title>Example launch line</title>
34 * gst-launch videotestsrc ! videobalance saturation=0.0 ! ffmpegcolorspace ! ximagesink
35 * ]| This pipeline converts the image to black and white by setting the
39 * Last reviewed on 2006-03-03 (0.10.3)
46 #include "gstvideobalance.h"
48 #include <liboil/liboil.h>
53 #include <gst/video/video.h>
54 #include <gst/interfaces/colorbalance.h>
57 #define M_PI 3.14159265358979323846
61 #define rint(x) (floor((x)+0.5))
64 static const GstElementDetails video_balance_details =
65 GST_ELEMENT_DETAILS ("Video balance",
66 "Filter/Effect/Video",
67 "Adjusts brightness, contrast, hue, saturation on a video stream",
68 "David Schleef <ds@schleef.org>");
70 /* GstVideoBalance signals and args */
71 #define DEFAULT_PROP_CONTRAST 1.0
72 #define DEFAULT_PROP_BRIGHTNESS 0.0
73 #define DEFAULT_PROP_HUE 0.0
74 #define DEFAULT_PROP_SATURATION 1.0
85 static GstStaticPadTemplate gst_video_balance_src_template =
86 GST_STATIC_PAD_TEMPLATE ("src",
89 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420, YV12 }"))
92 static GstStaticPadTemplate gst_video_balance_sink_template =
93 GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420, YV12 }"))
100 * look-up tables (LUT).
103 gst_video_balance_update_tables_planar411 (GstVideoBalance * vb)
106 gdouble y, u, v, hue_cos, hue_sin;
109 for (i = 0; i < 256; i++) {
110 y = 16 + ((i - 16) * vb->contrast + vb->brightness * 255);
115 vb->tabley[i] = rint (y);
118 /* FIXME this is a bogus transformation for hue, but you get
120 hue_cos = cos (M_PI * vb->hue);
121 hue_sin = sin (M_PI * vb->hue);
123 /* U/V lookup tables are 2D, since we need both U/V for each table
125 for (i = -128; i < 128; i++) {
126 for (j = -128; j < 128; j++) {
127 u = 128 + ((i * hue_cos + j * hue_sin) * vb->saturation);
128 v = 128 + ((-i * hue_sin + j * hue_cos) * vb->saturation);
137 vb->tableu[i + 128][j + 128] = rint (u);
138 vb->tablev[i + 128][j + 128] = rint (v);
144 gst_video_balance_is_passthrough (GstVideoBalance * videobalance)
146 return videobalance->contrast == 1.0 &&
147 videobalance->brightness == 0.0 &&
148 videobalance->hue == 0.0 && videobalance->saturation == 1.0;
152 gst_video_balance_update_properties (GstVideoBalance * videobalance)
154 videobalance->passthru = gst_video_balance_is_passthrough (videobalance);
156 if (!videobalance->passthru) {
157 gst_video_balance_update_tables_planar411 (videobalance);
163 oil_tablelookup_u8 (guint8 * dest, int dstr, guint8 * src, int sstr,
164 guint8 * table, int tstr, int n)
168 for (i = 0; i < n; i++) {
169 *dest = table[*src * tstr];
177 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
178 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
179 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
181 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
182 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
183 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
184 #define GST_VIDEO_I420_SIZE(w,h) (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
187 gst_video_balance_planar411_ip (GstVideoBalance * videobalance, guint8 * data,
188 gint width, gint height)
192 guint8 *udata, *vdata;
193 gint ystride, ustride, vstride;
194 gint width2, height2;
196 ydata = data + GST_VIDEO_I420_Y_OFFSET (width, height);
197 ystride = GST_VIDEO_I420_Y_ROWSTRIDE (width);
199 for (y = 0; y < height; y++) {
200 oil_tablelookup_u8 (ydata, 1, ydata, 1, videobalance->tabley, 1, width);
205 height2 = height >> 1;
207 udata = data + GST_VIDEO_I420_U_OFFSET (width, height);
208 vdata = data + GST_VIDEO_I420_V_OFFSET (width, height);
209 ustride = GST_VIDEO_I420_U_ROWSTRIDE (width);
210 vstride = GST_VIDEO_I420_V_ROWSTRIDE (width);
212 for (y = 0; y < height2; y++) {
216 uptr = udata + y * ustride;
217 vptr = vdata + y * vstride;
219 for (x = 0; x < width2; x++) {
223 *uptr++ = videobalance->tableu[u1][v1];
224 *vptr++ = videobalance->tablev[u1][v1];
229 /* get notified of caps and plug in the correct process function */
231 gst_video_balance_set_caps (GstBaseTransform * base, GstCaps * incaps,
234 GstVideoBalance *this;
235 GstStructure *structure;
238 this = GST_VIDEO_BALANCE (base);
240 GST_DEBUG_OBJECT (this,
241 "set_caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps, outcaps);
243 structure = gst_caps_get_structure (incaps, 0);
245 res = gst_structure_get_int (structure, "width", &this->width);
246 res &= gst_structure_get_int (structure, "height", &this->height);
250 this->size = GST_VIDEO_I420_SIZE (this->width, this->height);
257 gst_video_balance_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
259 GstVideoBalance *videobalance;
264 videobalance = GST_VIDEO_BALANCE (base);
266 /* if no change is needed, we are done */
267 if (videobalance->passthru)
270 data = GST_BUFFER_DATA (outbuf);
271 size = GST_BUFFER_SIZE (outbuf);
273 width = videobalance->width;
274 height = videobalance->height;
276 if (size < videobalance->size)
279 gst_video_balance_planar411_ip (videobalance, data,
280 videobalance->width, videobalance->height);
288 GST_ELEMENT_ERROR (videobalance, STREAM, FORMAT,
289 (NULL), ("Invalid buffer size %d, expected %d", size,
290 videobalance->size));
291 return GST_FLOW_ERROR;
299 static GstVideoFilterClass *parent_class = NULL;
301 static void gst_video_balance_set_property (GObject * object, guint prop_id,
302 const GValue * value, GParamSpec * pspec);
303 static void gst_video_balance_get_property (GObject * object, guint prop_id,
304 GValue * value, GParamSpec * pspec);
307 gst_video_balance_base_init (gpointer g_class)
309 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
311 gst_element_class_set_details (element_class, &video_balance_details);
313 gst_element_class_add_pad_template (element_class,
314 gst_static_pad_template_get (&gst_video_balance_sink_template));
315 gst_element_class_add_pad_template (element_class,
316 gst_static_pad_template_get (&gst_video_balance_src_template));
320 gst_video_balance_finalize (GObject * object)
322 GList *channels = NULL;
323 GstVideoBalance *balance;
326 balance = GST_VIDEO_BALANCE (object);
328 if (balance->tableu) {
329 for (i = 0; i < 256; i++)
330 g_free (balance->tableu[i]);
331 g_free (balance->tableu);
332 balance->tableu = NULL;
335 if (balance->tablev) {
336 for (i = 0; i < 256; i++)
337 g_free (balance->tablev[i]);
338 g_free (balance->tablev);
339 balance->tablev = NULL;
342 if (balance->tabley) {
343 g_free (balance->tabley);
344 balance->tabley = NULL;
347 channels = balance->channels;
349 GstColorBalanceChannel *channel = channels->data;
351 g_object_unref (channel);
352 channels->data = NULL;
353 channels = g_list_next (channels);
356 if (balance->channels)
357 g_list_free (balance->channels);
359 G_OBJECT_CLASS (parent_class)->finalize (object);
363 gst_video_balance_class_init (gpointer g_class, gpointer class_data)
365 GObjectClass *gobject_class;
366 GstBaseTransformClass *trans_class;
368 gobject_class = G_OBJECT_CLASS (g_class);
369 trans_class = GST_BASE_TRANSFORM_CLASS (g_class);
371 parent_class = g_type_class_peek_parent (g_class);
373 gobject_class->set_property = gst_video_balance_set_property;
374 gobject_class->get_property = gst_video_balance_get_property;
376 g_object_class_install_property (gobject_class, PROP_CONTRAST,
377 g_param_spec_double ("contrast", "Contrast", "contrast",
378 0.0, 2.0, DEFAULT_PROP_CONTRAST, G_PARAM_READWRITE));
379 g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
380 g_param_spec_double ("brightness", "Brightness", "brightness",
381 -1.0, 1.0, DEFAULT_PROP_BRIGHTNESS, G_PARAM_READWRITE));
382 g_object_class_install_property (gobject_class, PROP_HUE,
383 g_param_spec_double ("hue", "Hue", "hue",
384 -1.0, 1.0, DEFAULT_PROP_HUE, G_PARAM_READWRITE));
385 g_object_class_install_property (gobject_class, PROP_SATURATION,
386 g_param_spec_double ("saturation", "Saturation", "saturation",
387 0.0, 2.0, DEFAULT_PROP_SATURATION, G_PARAM_READWRITE));
389 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_video_balance_finalize);
391 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_balance_set_caps);
392 trans_class->transform_ip =
393 GST_DEBUG_FUNCPTR (gst_video_balance_transform_ip);
401 gst_video_balance_init (GTypeInstance * instance, gpointer g_class)
403 GstVideoBalance *videobalance = GST_VIDEO_BALANCE (instance);
404 GstVideoFilter *videofilter;
405 char *channels[4] = { "HUE", "SATURATION",
406 "BRIGHTNESS", "CONTRAST"
410 GST_DEBUG ("gst_video_balance_init");
412 videofilter = GST_VIDEO_FILTER (videobalance);
415 videobalance->contrast = DEFAULT_PROP_CONTRAST;
416 videobalance->brightness = DEFAULT_PROP_BRIGHTNESS;
417 videobalance->hue = DEFAULT_PROP_HUE;
418 videobalance->saturation = DEFAULT_PROP_SATURATION;
420 gst_video_balance_update_properties (videobalance);
422 videobalance->tabley = g_new (guint8, 256);
423 videobalance->tableu = g_new (guint8 *, 256);
424 videobalance->tablev = g_new (guint8 *, 256);
425 for (i = 0; i < 256; i++) {
426 videobalance->tableu[i] = g_new (guint8, 256);
427 videobalance->tablev[i] = g_new (guint8, 256);
430 /* Generate the channels list */
431 for (i = 0; i < (sizeof (channels) / sizeof (char *)); i++) {
432 GstColorBalanceChannel *channel;
434 channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
435 channel->label = g_strdup (channels[i]);
436 channel->min_value = -1000;
437 channel->max_value = 1000;
439 videobalance->channels = g_list_append (videobalance->channels, channel);
445 gst_video_balance_interface_supported (GstImplementsInterface * iface,
448 g_assert (type == GST_TYPE_COLOR_BALANCE);
453 gst_video_balance_interface_init (GstImplementsInterfaceClass * klass)
455 klass->supported = gst_video_balance_interface_supported;
459 gst_video_balance_colorbalance_list_channels (GstColorBalance * balance)
461 GstVideoBalance *videobalance = GST_VIDEO_BALANCE (balance);
463 g_return_val_if_fail (videobalance != NULL, NULL);
464 g_return_val_if_fail (GST_IS_VIDEO_BALANCE (videobalance), NULL);
466 return videobalance->channels;
470 gst_video_balance_colorbalance_set_value (GstColorBalance * balance,
471 GstColorBalanceChannel * channel, gint value)
473 GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
475 g_return_if_fail (vb != NULL);
476 g_return_if_fail (GST_IS_VIDEO_BALANCE (vb));
477 g_return_if_fail (GST_IS_VIDEO_FILTER (vb));
478 g_return_if_fail (channel->label != NULL);
480 if (!g_ascii_strcasecmp (channel->label, "HUE")) {
481 vb->hue = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
482 } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
483 vb->saturation = (value + 1000.0) * 2.0 / 2000.0;
484 } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
485 vb->brightness = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
486 } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
487 vb->contrast = (value + 1000.0) * 2.0 / 2000.0;
490 gst_video_balance_update_properties (vb);
494 gst_video_balance_colorbalance_get_value (GstColorBalance * balance,
495 GstColorBalanceChannel * channel)
497 GstVideoBalance *vb = GST_VIDEO_BALANCE (balance);
500 g_return_val_if_fail (vb != NULL, 0);
501 g_return_val_if_fail (GST_IS_VIDEO_BALANCE (vb), 0);
502 g_return_val_if_fail (channel->label != NULL, 0);
504 if (!g_ascii_strcasecmp (channel->label, "HUE")) {
505 value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
506 } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
507 value = vb->saturation * 2000.0 / 2.0 - 1000.0;
508 } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
509 value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
510 } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
511 value = vb->contrast * 2000.0 / 2.0 - 1000.0;
518 gst_video_balance_colorbalance_init (GstColorBalanceClass * iface)
520 GST_COLOR_BALANCE_TYPE (iface) = GST_COLOR_BALANCE_SOFTWARE;
521 iface->list_channels = gst_video_balance_colorbalance_list_channels;
522 iface->set_value = gst_video_balance_colorbalance_set_value;
523 iface->get_value = gst_video_balance_colorbalance_get_value;
527 gst_video_balance_set_property (GObject * object, guint prop_id,
528 const GValue * value, GParamSpec * pspec)
530 GstVideoBalance *src;
532 src = GST_VIDEO_BALANCE (object);
534 GST_DEBUG ("gst_video_balance_set_property");
537 src->contrast = g_value_get_double (value);
539 case PROP_BRIGHTNESS:
540 src->brightness = g_value_get_double (value);
543 src->hue = g_value_get_double (value);
545 case PROP_SATURATION:
546 src->saturation = g_value_get_double (value);
552 gst_video_balance_update_properties (src);
556 gst_video_balance_get_property (GObject * object, guint prop_id, GValue * value,
559 GstVideoBalance *src;
561 src = GST_VIDEO_BALANCE (object);
565 g_value_set_double (value, src->contrast);
567 case PROP_BRIGHTNESS:
568 g_value_set_double (value, src->brightness);
571 g_value_set_double (value, src->hue);
573 case PROP_SATURATION:
574 g_value_set_double (value, src->saturation);
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583 gst_video_balance_get_type (void)
585 static GType video_balance_type = 0;
587 if (!video_balance_type) {
588 static const GTypeInfo video_balance_info = {
589 sizeof (GstVideoBalanceClass),
590 gst_video_balance_base_init,
592 gst_video_balance_class_init,
595 sizeof (GstVideoBalance),
597 gst_video_balance_init,
600 static const GInterfaceInfo iface_info = {
601 (GInterfaceInitFunc) gst_video_balance_interface_init,
606 static const GInterfaceInfo colorbalance_info = {
607 (GInterfaceInitFunc) gst_video_balance_colorbalance_init,
612 video_balance_type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
613 "GstVideoBalance", &video_balance_info, 0);
615 g_type_add_interface_static (video_balance_type,
616 GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
617 g_type_add_interface_static (video_balance_type, GST_TYPE_COLOR_BALANCE,
620 return video_balance_type;
625 plugin_init (GstPlugin * plugin)
627 return gst_element_register (plugin, "videobalance", GST_RANK_NONE,
628 GST_TYPE_VIDEO_BALANCE);
631 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
634 "Changes hue, saturation, brightness etc. on video images",
635 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);