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_MAKE ("{ AYUV, "
78 "ARGB, BGRA, ABGR, RGBA, ABGR, RGBA, Y444, "
79 "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
80 "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
83 static GstStaticPadTemplate gst_gamma_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
87 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
88 "ARGB, BGRA, ABGR, RGBA, ABGR, RGBA, Y444, "
89 "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
90 "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
93 static void gst_gamma_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_gamma_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 static gboolean gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
100 static GstFlowReturn gst_gamma_transform_ip (GstBaseTransform * transform,
102 static void gst_gamma_before_transform (GstBaseTransform * transform,
105 static void gst_gamma_calculate_tables (GstGamma * gamma);
107 G_DEFINE_TYPE (GstGamma, gst_gamma, GST_TYPE_VIDEO_FILTER);
110 gst_gamma_class_init (GstGammaClass * g_class)
112 GObjectClass *gobject_class = (GObjectClass *) g_class;
113 GstElementClass *gstelement_class = (GstElementClass *) g_class;
114 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
116 GST_DEBUG_CATEGORY_INIT (gamma_debug, "gamma", 0, "gamma");
118 gobject_class->set_property = gst_gamma_set_property;
119 gobject_class->get_property = gst_gamma_get_property;
121 g_object_class_install_property (gobject_class, PROP_GAMMA,
122 g_param_spec_double ("gamma", "Gamma", "gamma",
123 0.01, 10, DEFAULT_PROP_GAMMA,
124 GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
126 gst_element_class_set_details_simple (gstelement_class,
127 "Video gamma correction", "Filter/Effect/Video",
128 "Adjusts gamma on a video stream", "Arwed v. Merkatz <v.merkatz@gmx.net");
130 gst_element_class_add_pad_template (gstelement_class,
131 gst_static_pad_template_get (&gst_gamma_sink_template));
132 gst_element_class_add_pad_template (gstelement_class,
133 gst_static_pad_template_get (&gst_gamma_src_template));
135 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_gamma_set_caps);
136 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_gamma_transform_ip);
137 trans_class->before_transform =
138 GST_DEBUG_FUNCPTR (gst_gamma_before_transform);
142 gst_gamma_init (GstGamma * gamma)
145 gamma->gamma = DEFAULT_PROP_GAMMA;
146 gst_gamma_calculate_tables (gamma);
150 gst_gamma_set_property (GObject * object, guint prop_id, const GValue * value,
153 GstGamma *gamma = GST_GAMMA (object);
157 gdouble val = g_value_get_double (value);
159 GST_DEBUG_OBJECT (gamma, "Changing gamma from %lf to %lf", gamma->gamma,
161 GST_OBJECT_LOCK (gamma);
163 gst_gamma_calculate_tables (gamma);
164 GST_OBJECT_UNLOCK (gamma);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 gst_gamma_get_property (GObject * object, guint prop_id, GValue * value,
177 GstGamma *gamma = GST_GAMMA (object);
181 g_value_set_double (value, gamma->gamma);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 gst_gamma_calculate_tables (GstGamma * gamma)
196 if (gamma->gamma == 1.0) {
197 GST_BASE_TRANSFORM (gamma)->passthrough = TRUE;
200 GST_BASE_TRANSFORM (gamma)->passthrough = FALSE;
202 exp = 1.0 / gamma->gamma;
203 for (n = 0; n < 256; n++) {
205 val = pow (val, exp);
207 gamma->gamma_table[n] = (guint8) floor (val + 0.5);
212 gst_gamma_planar_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
215 gint width, stride, row_wrap;
216 const guint8 *table = gamma->gamma_table;
219 data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
220 stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
221 width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
222 height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
223 row_wrap = stride - width;
225 for (i = 0; i < height; i++) {
226 for (j = 0; j < width; j++) {
227 *data = table[*data];
235 gst_gamma_packed_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
238 gint width, stride, row_wrap;
240 const guint8 *table = gamma->gamma_table;
243 data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
244 stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
245 width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
246 height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
247 pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
248 row_wrap = stride - pixel_stride * width;
250 for (i = 0; i < height; i++) {
251 for (j = 0; j < width; j++) {
252 *data = table[*data];
253 data += pixel_stride;
259 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
261 298, -100, -208, 34707,
265 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
267 -38, -74, 112, 32768,
268 112, -94, -18, 32768,
271 #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)
274 gst_gamma_packed_rgb_ip (GstGamma * gamma, GstVideoFrame * frame)
277 gint width, stride, row_wrap;
279 const guint8 *table = gamma->gamma_table;
285 data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
286 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
287 width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
288 height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
290 offsets[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
291 offsets[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
292 offsets[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
294 pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
295 row_wrap = stride - pixel_stride * width;
297 for (i = 0; i < height; i++) {
298 for (j = 0; j < width; j++) {
299 r = data[offsets[0]];
300 g = data[offsets[1]];
301 b = data[offsets[2]];
303 y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
304 u = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
305 v = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
307 y = table[CLAMP (y, 0, 255)];
308 r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
309 g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
310 b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
312 data[offsets[0]] = CLAMP (r, 0, 255);
313 data[offsets[1]] = CLAMP (g, 0, 255);
314 data[offsets[2]] = CLAMP (b, 0, 255);
315 data += pixel_stride;
322 gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
325 GstGamma *gamma = GST_GAMMA (base);
328 GST_DEBUG_OBJECT (gamma,
329 "setting caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps,
332 if (!gst_video_info_from_caps (&info, incaps))
337 switch (GST_VIDEO_INFO_FORMAT (&info)) {
338 case GST_VIDEO_FORMAT_I420:
339 case GST_VIDEO_FORMAT_YV12:
340 case GST_VIDEO_FORMAT_Y41B:
341 case GST_VIDEO_FORMAT_Y42B:
342 case GST_VIDEO_FORMAT_Y444:
343 case GST_VIDEO_FORMAT_NV12:
344 case GST_VIDEO_FORMAT_NV21:
345 gamma->process = gst_gamma_planar_yuv_ip;
347 case GST_VIDEO_FORMAT_YUY2:
348 case GST_VIDEO_FORMAT_UYVY:
349 case GST_VIDEO_FORMAT_AYUV:
350 case GST_VIDEO_FORMAT_YVYU:
351 gamma->process = gst_gamma_packed_yuv_ip;
353 case GST_VIDEO_FORMAT_ARGB:
354 case GST_VIDEO_FORMAT_ABGR:
355 case GST_VIDEO_FORMAT_RGBA:
356 case GST_VIDEO_FORMAT_BGRA:
357 case GST_VIDEO_FORMAT_xRGB:
358 case GST_VIDEO_FORMAT_xBGR:
359 case GST_VIDEO_FORMAT_RGBx:
360 case GST_VIDEO_FORMAT_BGRx:
361 case GST_VIDEO_FORMAT_RGB:
362 case GST_VIDEO_FORMAT_BGR:
363 gamma->process = gst_gamma_packed_rgb_ip;
374 GST_ERROR_OBJECT (gamma, "Invalid caps: %" GST_PTR_FORMAT, incaps);
380 gst_gamma_before_transform (GstBaseTransform * base, GstBuffer * outbuf)
382 GstGamma *gamma = GST_GAMMA (base);
383 GstClockTime timestamp, stream_time;
385 timestamp = GST_BUFFER_TIMESTAMP (outbuf);
387 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
389 GST_DEBUG_OBJECT (gamma, "sync to %" GST_TIME_FORMAT,
390 GST_TIME_ARGS (timestamp));
392 if (GST_CLOCK_TIME_IS_VALID (stream_time))
393 gst_object_sync_values (G_OBJECT (gamma), stream_time);
397 gst_gamma_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
399 GstGamma *gamma = GST_GAMMA (base);
405 if (base->passthrough)
408 if (!gst_video_frame_map (&frame, &gamma->info, outbuf, GST_MAP_READWRITE))
411 GST_OBJECT_LOCK (gamma);
412 gamma->process (gamma, &frame);
413 GST_OBJECT_UNLOCK (gamma);
415 gst_video_frame_unmap (&frame);
423 GST_ELEMENT_ERROR (gamma, STREAM, FORMAT,
424 (NULL), ("Invalid buffer received"));
425 return GST_FLOW_ERROR;
429 GST_ERROR_OBJECT (gamma, "Not negotiated yet");
430 return GST_FLOW_NOT_NEGOTIATED;