2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4 * Copyright (C) 2003 Arwed v. Merkatz <v.merkatz@gmx.net>
5 * Copyright (C) 2006 Mark Nauwelaerts <manauw@skynet.be>
6 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * This file was (probably) generated from
26 * gstvideotemplate.c,v 1.12 2004/01/07 21:07:12 ds Exp
28 * make_filter,v 1.6 2004/01/07 21:33:01 ds Exp
32 * SECTION:element-gamma
34 * Performs gamma correction on a video stream.
37 * <title>Example launch line</title>
39 * gst-launch videotestsrc ! gamma gamma=2.0 ! ffmpegcolorspace ! ximagesink
40 * ]| This pipeline will make the image "brighter".
42 * gst-launch videotestsrc ! gamma gamma=0.5 ! ffmpegcolorspace ! ximagesink
43 * ]| This pipeline will make the image "darker".
46 * Last reviewed on 2010-04-18 (0.10.22)
57 #include <gst/video/video.h>
58 #include <gst/controller/gstcontroller.h>
60 GST_DEBUG_CATEGORY_STATIC (gamma_debug);
61 #define GST_CAT_DEFAULT gamma_debug
63 /* GstGamma properties */
71 #define DEFAULT_PROP_GAMMA 1
73 static GstStaticPadTemplate gst_gamma_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
78 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
79 GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
80 GST_VIDEO_CAPS_YUV ("Y444") ";"
81 GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_RGBx ";"
82 GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_BGRx ";"
83 GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
84 GST_VIDEO_CAPS_YUV ("Y42B") ";"
85 GST_VIDEO_CAPS_YUV ("NV12") ";"
86 GST_VIDEO_CAPS_YUV ("NV21") ";"
87 GST_VIDEO_CAPS_YUV ("YUY2") ";"
88 GST_VIDEO_CAPS_YUV ("UYVY") ";"
89 GST_VIDEO_CAPS_YUV ("YVYU") ";"
90 GST_VIDEO_CAPS_YUV ("I420") ";"
91 GST_VIDEO_CAPS_YUV ("YV12") ";"
92 GST_VIDEO_CAPS_YUV ("IYUV") ";" GST_VIDEO_CAPS_YUV ("Y41B")
96 static GstStaticPadTemplate gst_gamma_sink_template =
97 GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
101 GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
102 GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
103 GST_VIDEO_CAPS_YUV ("Y444") ";"
104 GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_RGBx ";"
105 GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_BGRx ";"
106 GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
107 GST_VIDEO_CAPS_YUV ("Y42B") ";"
108 GST_VIDEO_CAPS_YUV ("NV12") ";"
109 GST_VIDEO_CAPS_YUV ("NV21") ";"
110 GST_VIDEO_CAPS_YUV ("YUY2") ";"
111 GST_VIDEO_CAPS_YUV ("UYVY") ";"
112 GST_VIDEO_CAPS_YUV ("YVYU") ";"
113 GST_VIDEO_CAPS_YUV ("I420") ";"
114 GST_VIDEO_CAPS_YUV ("YV12") ";"
115 GST_VIDEO_CAPS_YUV ("IYUV") ";" GST_VIDEO_CAPS_YUV ("Y41B")
119 static void gst_gamma_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_gamma_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static gboolean gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
126 static GstFlowReturn gst_gamma_transform_ip (GstBaseTransform * transform,
128 static void gst_gamma_before_transform (GstBaseTransform * transform,
131 static void gst_gamma_calculate_tables (GstGamma * gamma);
133 GST_BOILERPLATE (GstGamma, gst_gamma, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
136 gst_gamma_base_init (gpointer g_class)
138 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
140 gst_element_class_set_details_simple (element_class, "Video gamma correction",
141 "Filter/Effect/Video",
142 "Adjusts gamma on a video stream",
143 "Arwed v. Merkatz <v.merkatz@gmx.net>");
145 gst_element_class_add_pad_template (element_class,
146 gst_static_pad_template_get (&gst_gamma_sink_template));
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&gst_gamma_src_template));
152 gst_gamma_class_init (GstGammaClass * g_class)
154 GObjectClass *gobject_class = (GObjectClass *) g_class;
155 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
157 GST_DEBUG_CATEGORY_INIT (gamma_debug, "gamma", 0, "gamma");
159 gobject_class->set_property = gst_gamma_set_property;
160 gobject_class->get_property = gst_gamma_get_property;
162 g_object_class_install_property (gobject_class, PROP_GAMMA,
163 g_param_spec_double ("gamma", "Gamma", "gamma",
164 0.01, 10, DEFAULT_PROP_GAMMA,
165 GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
167 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_gamma_set_caps);
168 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_gamma_transform_ip);
169 trans_class->before_transform =
170 GST_DEBUG_FUNCPTR (gst_gamma_before_transform);
174 gst_gamma_init (GstGamma * gamma, GstGammaClass * g_class)
177 gamma->gamma = DEFAULT_PROP_GAMMA;
178 gst_gamma_calculate_tables (gamma);
182 gst_gamma_set_property (GObject * object, guint prop_id, const GValue * value,
185 GstGamma *gamma = GST_GAMMA (object);
189 gdouble val = g_value_get_double (value);
191 GST_DEBUG_OBJECT (gamma, "Changing gamma from %lf to %lf", gamma->gamma,
193 GST_OBJECT_LOCK (gamma);
195 gst_gamma_calculate_tables (gamma);
196 GST_OBJECT_UNLOCK (gamma);
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 gst_gamma_get_property (GObject * object, guint prop_id, GValue * value,
209 GstGamma *gamma = GST_GAMMA (object);
213 g_value_set_double (value, gamma->gamma);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_gamma_calculate_tables (GstGamma * gamma)
228 if (gamma->gamma == 1.0) {
229 GST_BASE_TRANSFORM (gamma)->passthrough = TRUE;
232 GST_BASE_TRANSFORM (gamma)->passthrough = FALSE;
234 exp = 1.0 / gamma->gamma;
235 for (n = 0; n < 256; n++) {
237 val = pow (val, exp);
239 gamma->gamma_table[n] = (guint8) floor (val + 0.5);
244 gst_gamma_planar_yuv_ip (GstGamma * gamma, guint8 * data)
247 gint width, row_stride, row_wrap;
248 const guint8 *table = gamma->gamma_table;
251 data + gst_video_format_get_component_offset (gamma->format, 0,
252 gamma->width, gamma->height);
254 width = gst_video_format_get_component_width (gamma->format, 0, gamma->width);
255 height = gst_video_format_get_component_height (gamma->format, 0,
257 row_stride = gst_video_format_get_row_stride (gamma->format, 0, gamma->width);
258 row_wrap = row_stride - width;
260 for (i = 0; i < height; i++) {
261 for (j = 0; j < width; j++) {
262 *data = table[*data];
270 gst_gamma_packed_yuv_ip (GstGamma * gamma, guint8 * data)
273 gint width, row_stride, row_wrap;
275 const guint8 *table = gamma->gamma_table;
277 data = data + gst_video_format_get_component_offset (gamma->format, 0,
278 gamma->width, gamma->height);
280 width = gst_video_format_get_component_width (gamma->format, 0, gamma->width);
281 height = gst_video_format_get_component_height (gamma->format, 0,
283 row_stride = gst_video_format_get_row_stride (gamma->format, 0, gamma->width);
284 pixel_stride = gst_video_format_get_pixel_stride (gamma->format, 0);
285 row_wrap = row_stride - pixel_stride * width;
287 for (i = 0; i < height; i++) {
288 for (j = 0; j < width; j++) {
289 *data = table[*data];
290 data += pixel_stride;
296 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
298 298, -100, -208, 34707,
302 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
304 -38, -74, 112, 32768,
305 112, -94, -18, 32768,
308 #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)
311 gst_gamma_packed_rgb_ip (GstGamma * gamma, guint8 * data)
314 gint width, row_stride, row_wrap;
316 const guint8 *table = gamma->gamma_table;
321 offsets[0] = gst_video_format_get_component_offset (gamma->format, 0,
322 gamma->width, gamma->height);
323 offsets[1] = gst_video_format_get_component_offset (gamma->format, 1,
324 gamma->width, gamma->height);
325 offsets[2] = gst_video_format_get_component_offset (gamma->format, 2,
326 gamma->width, gamma->height);
328 width = gst_video_format_get_component_width (gamma->format, 0, gamma->width);
329 height = gst_video_format_get_component_height (gamma->format, 0,
331 row_stride = gst_video_format_get_row_stride (gamma->format, 0, gamma->width);
332 pixel_stride = gst_video_format_get_pixel_stride (gamma->format, 0);
333 row_wrap = row_stride - pixel_stride * width;
335 for (i = 0; i < height; i++) {
336 for (j = 0; j < width; j++) {
337 r = data[offsets[0]];
338 g = data[offsets[1]];
339 b = data[offsets[2]];
341 y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
342 u = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
343 v = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
345 y = table[CLAMP (y, 0, 255)];
346 r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
347 g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
348 b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
350 data[offsets[0]] = CLAMP (r, 0, 255);
351 data[offsets[1]] = CLAMP (g, 0, 255);
352 data[offsets[2]] = CLAMP (b, 0, 255);
353 data += pixel_stride;
360 gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
363 GstGamma *gamma = GST_GAMMA (base);
365 GST_DEBUG_OBJECT (gamma,
366 "setting caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps,
369 if (!gst_video_format_parse_caps (incaps, &gamma->format, &gamma->width,
374 gst_video_format_get_size (gamma->format, gamma->width, gamma->height);
376 switch (gamma->format) {
377 case GST_VIDEO_FORMAT_I420:
378 case GST_VIDEO_FORMAT_YV12:
379 case GST_VIDEO_FORMAT_Y41B:
380 case GST_VIDEO_FORMAT_Y42B:
381 case GST_VIDEO_FORMAT_Y444:
382 case GST_VIDEO_FORMAT_NV12:
383 case GST_VIDEO_FORMAT_NV21:
384 gamma->process = gst_gamma_planar_yuv_ip;
386 case GST_VIDEO_FORMAT_YUY2:
387 case GST_VIDEO_FORMAT_UYVY:
388 case GST_VIDEO_FORMAT_AYUV:
389 case GST_VIDEO_FORMAT_YVYU:
390 gamma->process = gst_gamma_packed_yuv_ip;
392 case GST_VIDEO_FORMAT_ARGB:
393 case GST_VIDEO_FORMAT_ABGR:
394 case GST_VIDEO_FORMAT_RGBA:
395 case GST_VIDEO_FORMAT_BGRA:
396 case GST_VIDEO_FORMAT_xRGB:
397 case GST_VIDEO_FORMAT_xBGR:
398 case GST_VIDEO_FORMAT_RGBx:
399 case GST_VIDEO_FORMAT_BGRx:
400 case GST_VIDEO_FORMAT_RGB:
401 case GST_VIDEO_FORMAT_BGR:
402 gamma->process = gst_gamma_packed_rgb_ip;
412 GST_ERROR_OBJECT (gamma, "Invalid caps: %" GST_PTR_FORMAT, incaps);
417 gst_gamma_before_transform (GstBaseTransform * base, GstBuffer * outbuf)
419 GstGamma *gamma = GST_GAMMA (base);
420 GstClockTime timestamp, stream_time;
422 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
424 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
426 GST_DEBUG_OBJECT (gamma, "sync to %" GST_TIME_FORMAT,
427 GST_TIME_ARGS (timestamp));
429 if (GST_CLOCK_TIME_IS_VALID (stream_time))
430 gst_object_sync_values (G_OBJECT (gamma), stream_time);
434 gst_gamma_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
436 GstGamma *gamma = GST_GAMMA (base);
443 if (base->passthrough)
446 data = GST_BUFFER_DATA (outbuf);
447 size = GST_BUFFER_SIZE (outbuf);
449 if (size != gamma->size)
452 GST_OBJECT_LOCK (gamma);
453 gamma->process (gamma, data);
454 GST_OBJECT_UNLOCK (gamma);
462 GST_ELEMENT_ERROR (gamma, STREAM, FORMAT,
463 (NULL), ("Invalid buffer size %d, expected %d", size, gamma->size));
464 return GST_FLOW_ERROR;
468 GST_ERROR_OBJECT (gamma, "Not negotiated yet");
469 return GST_FLOW_NOT_NEGOTIATED;