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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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-1.0 videotestsrc ! gamma gamma=2.0 ! videoconvert ! ximagesink
40  * ]| This pipeline will make the image "brighter".
41  * |[
42  * gst-launch-1.0 videotestsrc ! gamma gamma=0.5 ! videoconvert ! ximagesink
43  * ]| This pipeline will make the image "darker".
44  * </refsect2>
45  */
46 
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50 
51 #include "gstgamma.h"
52 #include <string.h>
53 #include <math.h>
54 
55 #include <gst/video/video.h>
56 
57 GST_DEBUG_CATEGORY_STATIC (gamma_debug);
58 #define GST_CAT_DEFAULT gamma_debug
59 
60 /* GstGamma properties */
61 enum
62 {
63   PROP_0,
64   PROP_GAMMA
65       /* FILL ME */
66 };
67 
68 #define DEFAULT_PROP_GAMMA  1
69 
70 static GstStaticPadTemplate gst_gamma_src_template =
71 GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
75             "ARGB, BGRA, ABGR, RGBA, Y444, "
76             "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
77             "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
78     );
79 
80 static GstStaticPadTemplate gst_gamma_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
82     GST_PAD_SINK,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
85             "ARGB, BGRA, ABGR, RGBA, Y444, "
86             "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
87             "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
88     );
89 
90 static void gst_gamma_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_gamma_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94 
95 static gboolean gst_gamma_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
96     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
97 static GstFlowReturn gst_gamma_transform_frame_ip (GstVideoFilter * vfilter,
98     GstVideoFrame * frame);
99 static void gst_gamma_before_transform (GstBaseTransform * transform,
100     GstBuffer * buf);
101 
102 static void gst_gamma_calculate_tables (GstGamma * gamma);
103 
104 G_DEFINE_TYPE (GstGamma, gst_gamma, GST_TYPE_VIDEO_FILTER);
105 
106 static void
gst_gamma_class_init(GstGammaClass * g_class)107 gst_gamma_class_init (GstGammaClass * g_class)
108 {
109   GObjectClass *gobject_class = (GObjectClass *) g_class;
110   GstElementClass *gstelement_class = (GstElementClass *) g_class;
111   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
112   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) g_class;
113 
114   GST_DEBUG_CATEGORY_INIT (gamma_debug, "gamma", 0, "gamma");
115 
116   gobject_class->set_property = gst_gamma_set_property;
117   gobject_class->get_property = gst_gamma_get_property;
118 
119   g_object_class_install_property (gobject_class, PROP_GAMMA,
120       g_param_spec_double ("gamma", "Gamma", "gamma",
121           0.01, 10, DEFAULT_PROP_GAMMA,
122           GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
123 
124   gst_element_class_set_static_metadata (gstelement_class,
125       "Video gamma correction", "Filter/Effect/Video",
126       "Adjusts gamma on a video stream", "Arwed v. Merkatz <v.merkatz@gmx.net");
127 
128   gst_element_class_add_static_pad_template (gstelement_class,
129       &gst_gamma_sink_template);
130   gst_element_class_add_static_pad_template (gstelement_class,
131       &gst_gamma_src_template);
132 
133   trans_class->before_transform =
134       GST_DEBUG_FUNCPTR (gst_gamma_before_transform);
135   trans_class->transform_ip_on_passthrough = FALSE;
136 
137   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_gamma_set_info);
138   vfilter_class->transform_frame_ip =
139       GST_DEBUG_FUNCPTR (gst_gamma_transform_frame_ip);
140 }
141 
142 static void
gst_gamma_init(GstGamma * gamma)143 gst_gamma_init (GstGamma * gamma)
144 {
145   /* properties */
146   gamma->gamma = DEFAULT_PROP_GAMMA;
147   gst_gamma_calculate_tables (gamma);
148 }
149 
150 static void
gst_gamma_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)151 gst_gamma_set_property (GObject * object, guint prop_id, const GValue * value,
152     GParamSpec * pspec)
153 {
154   GstGamma *gamma = GST_GAMMA (object);
155 
156   switch (prop_id) {
157     case PROP_GAMMA:{
158       gdouble val = g_value_get_double (value);
159 
160       GST_DEBUG_OBJECT (gamma, "Changing gamma from %lf to %lf", gamma->gamma,
161           val);
162       GST_OBJECT_LOCK (gamma);
163       gamma->gamma = val;
164       GST_OBJECT_UNLOCK (gamma);
165       gst_gamma_calculate_tables (gamma);
166       break;
167     }
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173 
174 static void
gst_gamma_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)175 gst_gamma_get_property (GObject * object, guint prop_id, GValue * value,
176     GParamSpec * pspec)
177 {
178   GstGamma *gamma = GST_GAMMA (object);
179 
180   switch (prop_id) {
181     case PROP_GAMMA:
182       g_value_set_double (value, gamma->gamma);
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187   }
188 }
189 
190 static void
gst_gamma_calculate_tables(GstGamma * gamma)191 gst_gamma_calculate_tables (GstGamma * gamma)
192 {
193   gint n;
194   gdouble val;
195   gdouble exp;
196   gboolean passthrough = FALSE;
197 
198   GST_OBJECT_LOCK (gamma);
199   if (gamma->gamma == 1.0) {
200     passthrough = TRUE;
201   } else {
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   GST_OBJECT_UNLOCK (gamma);
211 
212   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (gamma), passthrough);
213 }
214 
215 static void
gst_gamma_planar_yuv_ip(GstGamma * gamma,GstVideoFrame * frame)216 gst_gamma_planar_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
217 {
218   gint i, j, height;
219   gint width, stride, row_wrap;
220   const guint8 *table = gamma->gamma_table;
221   guint8 *data;
222 
223   data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
224   stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
225   width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
226   height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
227   row_wrap = stride - width;
228 
229   for (i = 0; i < height; i++) {
230     for (j = 0; j < width; j++) {
231       *data = table[*data];
232       data++;
233     }
234     data += row_wrap;
235   }
236 }
237 
238 static void
gst_gamma_packed_yuv_ip(GstGamma * gamma,GstVideoFrame * frame)239 gst_gamma_packed_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
240 {
241   gint i, j, height;
242   gint width, stride, row_wrap;
243   gint pixel_stride;
244   const guint8 *table = gamma->gamma_table;
245   guint8 *data;
246 
247   data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
248   stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
249   width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
250   height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
251   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
252   row_wrap = stride - pixel_stride * width;
253 
254   for (i = 0; i < height; i++) {
255     for (j = 0; j < width; j++) {
256       *data = table[*data];
257       data += pixel_stride;
258     }
259     data += row_wrap;
260   }
261 }
262 
263 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
264   298, 0, 409, -57068,
265   298, -100, -208, 34707,
266   298, 516, 0, -70870,
267 };
268 
269 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
270   66, 129, 25, 4096,
271   -38, -74, 112, 32768,
272   112, -94, -18, 32768,
273 };
274 
275 #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)
276 
277 static void
gst_gamma_packed_rgb_ip(GstGamma * gamma,GstVideoFrame * frame)278 gst_gamma_packed_rgb_ip (GstGamma * gamma, GstVideoFrame * frame)
279 {
280   gint i, j, height;
281   gint width, stride, row_wrap;
282   gint pixel_stride;
283   const guint8 *table = gamma->gamma_table;
284   gint offsets[3];
285   gint r, g, b;
286   gint y, u, v;
287   guint8 *data;
288 
289   data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
290   stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
291   width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
292   height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
293 
294   offsets[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
295   offsets[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
296   offsets[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
297 
298   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
299   row_wrap = stride - pixel_stride * width;
300 
301   for (i = 0; i < height; i++) {
302     for (j = 0; j < width; j++) {
303       r = data[offsets[0]];
304       g = data[offsets[1]];
305       b = data[offsets[2]];
306 
307       y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
308       u = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
309       v = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);
310 
311       y = table[CLAMP (y, 0, 255)];
312       r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
313       g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
314       b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
315 
316       data[offsets[0]] = CLAMP (r, 0, 255);
317       data[offsets[1]] = CLAMP (g, 0, 255);
318       data[offsets[2]] = CLAMP (b, 0, 255);
319       data += pixel_stride;
320     }
321     data += row_wrap;
322   }
323 }
324 
325 static gboolean
gst_gamma_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)326 gst_gamma_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
327     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
328 {
329   GstGamma *gamma = GST_GAMMA (vfilter);
330 
331   GST_DEBUG_OBJECT (gamma,
332       "setting caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps,
333       outcaps);
334 
335   switch (GST_VIDEO_INFO_FORMAT (in_info)) {
336     case GST_VIDEO_FORMAT_I420:
337     case GST_VIDEO_FORMAT_YV12:
338     case GST_VIDEO_FORMAT_Y41B:
339     case GST_VIDEO_FORMAT_Y42B:
340     case GST_VIDEO_FORMAT_Y444:
341     case GST_VIDEO_FORMAT_NV12:
342     case GST_VIDEO_FORMAT_NV21:
343       gamma->process = gst_gamma_planar_yuv_ip;
344       break;
345     case GST_VIDEO_FORMAT_YUY2:
346     case GST_VIDEO_FORMAT_UYVY:
347     case GST_VIDEO_FORMAT_AYUV:
348     case GST_VIDEO_FORMAT_YVYU:
349       gamma->process = gst_gamma_packed_yuv_ip;
350       break;
351     case GST_VIDEO_FORMAT_ARGB:
352     case GST_VIDEO_FORMAT_ABGR:
353     case GST_VIDEO_FORMAT_RGBA:
354     case GST_VIDEO_FORMAT_BGRA:
355     case GST_VIDEO_FORMAT_xRGB:
356     case GST_VIDEO_FORMAT_xBGR:
357     case GST_VIDEO_FORMAT_RGBx:
358     case GST_VIDEO_FORMAT_BGRx:
359     case GST_VIDEO_FORMAT_RGB:
360     case GST_VIDEO_FORMAT_BGR:
361       gamma->process = gst_gamma_packed_rgb_ip;
362       break;
363     default:
364       goto invalid_caps;
365       break;
366   }
367   return TRUE;
368 
369   /* ERRORS */
370 invalid_caps:
371   {
372     GST_ERROR_OBJECT (gamma, "Invalid caps: %" GST_PTR_FORMAT, incaps);
373     return FALSE;
374   }
375 }
376 
377 static void
gst_gamma_before_transform(GstBaseTransform * base,GstBuffer * outbuf)378 gst_gamma_before_transform (GstBaseTransform * base, GstBuffer * outbuf)
379 {
380   GstGamma *gamma = GST_GAMMA (base);
381   GstClockTime timestamp, stream_time;
382 
383   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
384   stream_time =
385       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
386 
387   GST_DEBUG_OBJECT (gamma, "sync to %" GST_TIME_FORMAT,
388       GST_TIME_ARGS (timestamp));
389 
390   if (GST_CLOCK_TIME_IS_VALID (stream_time))
391     gst_object_sync_values (GST_OBJECT (gamma), stream_time);
392 }
393 
394 static GstFlowReturn
gst_gamma_transform_frame_ip(GstVideoFilter * vfilter,GstVideoFrame * frame)395 gst_gamma_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
396 {
397   GstGamma *gamma = GST_GAMMA (vfilter);
398 
399   if (!gamma->process)
400     goto not_negotiated;
401 
402   GST_OBJECT_LOCK (gamma);
403   gamma->process (gamma, frame);
404   GST_OBJECT_UNLOCK (gamma);
405 
406   return GST_FLOW_OK;
407 
408   /* ERRORS */
409 not_negotiated:
410   {
411     GST_ERROR_OBJECT (gamma, "Not negotiated yet");
412     return GST_FLOW_NOT_NEGOTIATED;
413   }
414 }
415