Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / videofilter / gstgamma.c
1 /* GStreamer
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>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /*
25  * This file was (probably) generated from
26  * gstvideotemplate.c,v 1.12 2004/01/07 21:07:12 ds Exp 
27  * and
28  * make_filter,v 1.6 2004/01/07 21:33:01 ds Exp 
29  */
30
31 /**
32  * SECTION:element-gamma
33  *
34  * Performs gamma correction on a video stream.
35  *
36  * <refsect2>
37  * <title>Example launch line</title>
38  * |[
39  * gst-launch videotestsrc ! gamma gamma=2.0 ! ffmpegcolorspace ! ximagesink
40  * ]| This pipeline will make the image "brighter".
41  * |[
42  * gst-launch videotestsrc ! gamma gamma=0.5 ! ffmpegcolorspace ! ximagesink
43  * ]| This pipeline will make the image "darker".
44  * </refsect2>
45  *
46  * Last reviewed on 2010-04-18 (0.10.22)
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gstgamma.h"
54 #include <string.h>
55 #include <math.h>
56
57 #include <gst/video/video.h>
58 #include <gst/controller/gstcontroller.h>
59
60 GST_DEBUG_CATEGORY_STATIC (gamma_debug);
61 #define GST_CAT_DEFAULT gamma_debug
62
63 /* GstGamma properties */
64 enum
65 {
66   PROP_0,
67   PROP_GAMMA
68       /* FILL ME */
69 };
70
71 #define DEFAULT_PROP_GAMMA  1
72
73 static GstStaticPadTemplate gst_gamma_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
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 }"))
81     );
82
83 static GstStaticPadTemplate gst_gamma_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
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 }"))
91     );
92
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);
97
98 static gboolean gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
99     GstCaps * outcaps);
100 static GstFlowReturn gst_gamma_transform_ip (GstBaseTransform * transform,
101     GstBuffer * buf);
102 static void gst_gamma_before_transform (GstBaseTransform * transform,
103     GstBuffer * buf);
104
105 static void gst_gamma_calculate_tables (GstGamma * gamma);
106
107 G_DEFINE_TYPE (GstGamma, gst_gamma, GST_TYPE_VIDEO_FILTER);
108
109 static void
110 gst_gamma_class_init (GstGammaClass * g_class)
111 {
112   GObjectClass *gobject_class = (GObjectClass *) g_class;
113   GstElementClass *gstelement_class = (GstElementClass *) g_class;
114   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
115
116   GST_DEBUG_CATEGORY_INIT (gamma_debug, "gamma", 0, "gamma");
117
118   gobject_class->set_property = gst_gamma_set_property;
119   gobject_class->get_property = gst_gamma_get_property;
120
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));
125
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");
129
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));
134
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);
139 }
140
141 static void
142 gst_gamma_init (GstGamma * gamma)
143 {
144   /* properties */
145   gamma->gamma = DEFAULT_PROP_GAMMA;
146   gst_gamma_calculate_tables (gamma);
147 }
148
149 static void
150 gst_gamma_set_property (GObject * object, guint prop_id, const GValue * value,
151     GParamSpec * pspec)
152 {
153   GstGamma *gamma = GST_GAMMA (object);
154
155   switch (prop_id) {
156     case PROP_GAMMA:{
157       gdouble val = g_value_get_double (value);
158
159       GST_DEBUG_OBJECT (gamma, "Changing gamma from %lf to %lf", gamma->gamma,
160           val);
161       GST_OBJECT_LOCK (gamma);
162       gamma->gamma = val;
163       gst_gamma_calculate_tables (gamma);
164       GST_OBJECT_UNLOCK (gamma);
165       break;
166     }
167     default:
168       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169       break;
170   }
171 }
172
173 static void
174 gst_gamma_get_property (GObject * object, guint prop_id, GValue * value,
175     GParamSpec * pspec)
176 {
177   GstGamma *gamma = GST_GAMMA (object);
178
179   switch (prop_id) {
180     case PROP_GAMMA:
181       g_value_set_double (value, gamma->gamma);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188
189 static void
190 gst_gamma_calculate_tables (GstGamma * gamma)
191 {
192   gint n;
193   gdouble val;
194   gdouble exp;
195
196   if (gamma->gamma == 1.0) {
197     GST_BASE_TRANSFORM (gamma)->passthrough = TRUE;
198     return;
199   }
200   GST_BASE_TRANSFORM (gamma)->passthrough = FALSE;
201
202   exp = 1.0 / gamma->gamma;
203   for (n = 0; n < 256; n++) {
204     val = n / 255.0;
205     val = pow (val, exp);
206     val = 255.0 * val;
207     gamma->gamma_table[n] = (guint8) floor (val + 0.5);
208   }
209 }
210
211 static void
212 gst_gamma_planar_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
213 {
214   gint i, j, height;
215   gint width, stride, row_wrap;
216   const guint8 *table = gamma->gamma_table;
217   guint8 *data;
218
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;
224
225   for (i = 0; i < height; i++) {
226     for (j = 0; j < width; j++) {
227       *data = table[*data];
228       data++;
229     }
230     data += row_wrap;
231   }
232 }
233
234 static void
235 gst_gamma_packed_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
236 {
237   gint i, j, height;
238   gint width, stride, row_wrap;
239   gint pixel_stride;
240   const guint8 *table = gamma->gamma_table;
241   guint8 *data;
242
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;
249
250   for (i = 0; i < height; i++) {
251     for (j = 0; j < width; j++) {
252       *data = table[*data];
253       data += pixel_stride;
254     }
255     data += row_wrap;
256   }
257 }
258
259 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
260   298, 0, 409, -57068,
261   298, -100, -208, 34707,
262   298, 516, 0, -70870,
263 };
264
265 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
266   66, 129, 25, 4096,
267   -38, -74, 112, 32768,
268   112, -94, -18, 32768,
269 };
270
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)
272
273 static void
274 gst_gamma_packed_rgb_ip (GstGamma * gamma, GstVideoFrame * frame)
275 {
276   gint i, j, height;
277   gint width, stride, row_wrap;
278   gint pixel_stride;
279   const guint8 *table = gamma->gamma_table;
280   gint offsets[3];
281   gint r, g, b;
282   gint y, u, v;
283   guint8 *data;
284
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);
289
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);
293
294   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
295   row_wrap = stride - pixel_stride * width;
296
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]];
302
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);
306
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);
311
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;
316     }
317     data += row_wrap;
318   }
319 }
320
321 static gboolean
322 gst_gamma_set_caps (GstBaseTransform * base, GstCaps * incaps,
323     GstCaps * outcaps)
324 {
325   GstGamma *gamma = GST_GAMMA (base);
326   GstVideoInfo info;
327
328   GST_DEBUG_OBJECT (gamma,
329       "setting caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps,
330       outcaps);
331
332   if (!gst_video_info_from_caps (&info, incaps))
333     goto invalid_caps;
334
335   gamma->info = info;
336
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;
346       break;
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;
352       break;
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;
364       break;
365     default:
366       goto invalid_caps;
367       break;
368   }
369
370   return TRUE;
371
372 invalid_caps:
373   {
374     GST_ERROR_OBJECT (gamma, "Invalid caps: %" GST_PTR_FORMAT, incaps);
375     return FALSE;
376   }
377 }
378
379 static void
380 gst_gamma_before_transform (GstBaseTransform * base, GstBuffer * outbuf)
381 {
382   GstGamma *gamma = GST_GAMMA (base);
383   GstClockTime timestamp, stream_time;
384
385   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
386   stream_time =
387       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
388
389   GST_DEBUG_OBJECT (gamma, "sync to %" GST_TIME_FORMAT,
390       GST_TIME_ARGS (timestamp));
391
392   if (GST_CLOCK_TIME_IS_VALID (stream_time))
393     gst_object_sync_values (G_OBJECT (gamma), stream_time);
394 }
395
396 static GstFlowReturn
397 gst_gamma_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
398 {
399   GstGamma *gamma = GST_GAMMA (base);
400   GstVideoFrame frame;
401
402   if (!gamma->process)
403     goto not_negotiated;
404
405   if (base->passthrough)
406     goto done;
407
408   if (!gst_video_frame_map (&frame, &gamma->info, outbuf, GST_MAP_READWRITE))
409     goto wrong_buffer;
410
411   GST_OBJECT_LOCK (gamma);
412   gamma->process (gamma, &frame);
413   GST_OBJECT_UNLOCK (gamma);
414
415   gst_video_frame_unmap (&frame);
416
417 done:
418   return GST_FLOW_OK;
419
420   /* ERRORS */
421 wrong_buffer:
422   {
423     GST_ELEMENT_ERROR (gamma, STREAM, FORMAT,
424         (NULL), ("Invalid buffer received"));
425     return GST_FLOW_ERROR;
426   }
427 not_negotiated:
428   {
429     GST_ERROR_OBJECT (gamma, "Not negotiated yet");
430     return GST_FLOW_NOT_NEGOTIATED;
431   }
432 }