gaudieffects: port solarize to 0.11
[platform/upstream/gstreamer.git] / gst / gaudieffects / gstsolarize.c
1 /*
2  * GStreamer
3  * Copyright (C) <2010-2012> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Solarize - curve adjustment video effect.
6  * Based on Pete Warden's FreeFrame plugin with the same name.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Alternatively, the contents of this file may be used under the
27  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28  * which case the following provisions apply instead of the ones
29  * mentioned above:
30  *
31  * This library is free software; you can redistribute it and/or
32  * modify it under the terms of the GNU Library General Public
33  * License as published by the Free Software Foundation; either
34  * version 2 of the License, or (at your option) any later version.
35  *
36  * This library is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39  * Library General Public License for more details.
40  *
41  * You should have received a copy of the GNU Library General Public
42  * License along with this library; if not, write to the
43  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
44  * Boston, MA 02111-1307, USA.
45  */
46
47 /**
48  * SECTION:element-solarize
49  *
50  * Solarize does a smart inverse in a video stream in realtime.
51  *
52  * <refsect2>
53  * <title>Example launch line</title>
54  * |[
55  * gst-launch -v videotestsrc ! solarize ! ffmpegcolorspace ! autovideosink
56  * ]| This pipeline shows the effect of solarize on a test stream
57  * </refsect2>
58  */
59
60 #ifdef HAVE_CONFIG_H
61 #  include <config.h>
62 #endif
63
64 #include <gst/gst.h>
65 #include <math.h>
66
67 #include "gstplugin.h"
68 #include "gstsolarize.h"
69
70 GST_DEBUG_CATEGORY_STATIC (gst_solarize_debug);
71 #define GST_CAT_DEFAULT gst_solarize_debug
72
73 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
74 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
75 #else
76 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
77 #endif
78
79 /* Filter signals and args. */
80 enum
81 {
82   LAST_SIGNAL
83 };
84
85 enum
86 {
87   PROP_0 = 0,
88   PROP_THRESHOLD,
89   PROP_START,
90   PROP_END,
91   PROP_SILENT
92 };
93
94 /* Initializations */
95
96 #define DEFAULT_THRESHOLD 127
97 #define DEFAULT_START 50
98 #define DEFAULT_END 185
99
100 static gint gate_int (gint value, gint min, gint max);
101 static void transform (guint32 * src, guint32 * dest, gint video_area,
102     gint threshold, gint start, gint end);
103
104 /* The capabilities of the inputs and outputs. */
105
106 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS (CAPS_STR)
110     );
111
112 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
113     GST_PAD_SRC,
114     GST_PAD_ALWAYS,
115     GST_STATIC_CAPS (CAPS_STR)
116     );
117
118 G_DEFINE_TYPE (GstSolarize, gst_solarize, GST_TYPE_VIDEO_FILTER);
119
120 static void gst_solarize_set_property (GObject * object, guint prop_id,
121     const GValue * value, GParamSpec * pspec);
122 static void gst_solarize_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec);
124
125 static gboolean gst_solarize_set_caps (GstBaseTransform * btrans,
126     GstCaps * incaps, GstCaps * outcaps);
127 static GstFlowReturn gst_solarize_transform (GstBaseTransform * btrans,
128     GstBuffer * in_buf, GstBuffer * out_buf);
129
130 /* GObject vmethod implementations */
131
132 /* Initialize the solarize's class. */
133 static void
134 gst_solarize_class_init (GstSolarizeClass * klass)
135 {
136   GObjectClass *gobject_class = (GObjectClass *) klass;
137   GstElementClass *gstelement_class = (GstElementClass *) klass;
138   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
139
140   gst_element_class_set_details_simple (gstelement_class,
141       "Solarize",
142       "Filter/Effect/Video",
143       "Solarize tunable inverse in the video signal.",
144       "Luis de Bethencourt <luis@debethencourt.com>");
145
146   gst_element_class_add_pad_template (gstelement_class,
147       gst_static_pad_template_get (&src_factory));
148   gst_element_class_add_pad_template (gstelement_class,
149       gst_static_pad_template_get (&sink_factory));
150
151   gobject_class->set_property = gst_solarize_set_property;
152   gobject_class->get_property = gst_solarize_get_property;
153
154   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
155       g_param_spec_uint ("threshold", "Threshold",
156           "Threshold parameter", 0, 256, DEFAULT_THRESHOLD,
157           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
158
159   g_object_class_install_property (gobject_class, PROP_START,
160       g_param_spec_uint ("start", "Start",
161           "Start parameter", 0, 256, DEFAULT_START,
162           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
163
164   g_object_class_install_property (gobject_class, PROP_END,
165       g_param_spec_uint ("end", "End",
166           "End parameter", 0, 256, DEFAULT_END,
167           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
168
169   g_object_class_install_property (gobject_class, PROP_SILENT,
170       g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
171           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172
173   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_solarize_set_caps);
174   trans_class->transform = GST_DEBUG_FUNCPTR (gst_solarize_transform);
175 }
176
177 /* Initialize the element,
178  * instantiate pads and add them to element,
179  * set pad calback functions, and
180  * initialize instance structure.
181  */
182 static void
183 gst_solarize_init (GstSolarize * filter)
184 {
185   filter->threshold = DEFAULT_THRESHOLD;
186   filter->start = DEFAULT_START;
187   filter->end = DEFAULT_END;
188   filter->silent = FALSE;
189 }
190
191 static void
192 gst_solarize_set_property (GObject * object, guint prop_id,
193     const GValue * value, GParamSpec * pspec)
194 {
195   GstSolarize *filter = GST_SOLARIZE (object);
196
197   switch (prop_id) {
198     case PROP_SILENT:
199       filter->silent = g_value_get_boolean (value);
200       break;
201     case PROP_THRESHOLD:
202       filter->threshold = g_value_get_uint (value);
203       break;
204     case PROP_START:
205       filter->start = g_value_get_uint (value);
206       break;
207     case PROP_END:
208       filter->end = g_value_get_uint (value);
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213   }
214 }
215
216 static void
217 gst_solarize_get_property (GObject * object, guint prop_id,
218     GValue * value, GParamSpec * pspec)
219 {
220   GstSolarize *filter = GST_SOLARIZE (object);
221
222   GST_OBJECT_LOCK (filter);
223   switch (prop_id) {
224     case PROP_SILENT:
225       g_value_set_boolean (value, filter->silent);
226       break;
227     case PROP_THRESHOLD:
228       g_value_set_uint (value, filter->threshold);
229       break;
230     case PROP_START:
231       g_value_set_uint (value, filter->start);
232       break;
233     case PROP_END:
234       g_value_set_uint (value, filter->end);
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238       break;
239   }
240   GST_OBJECT_UNLOCK (filter);
241 }
242
243 /* GstElement vmethod implementations */
244
245 /* Handle the link with other elements. */
246 static gboolean
247 gst_solarize_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
248     GstCaps * outcaps)
249 {
250   GstSolarize *solarize = GST_SOLARIZE (btrans);
251   GstVideoInfo info;
252
253   if (!gst_video_info_from_caps (&info, incaps))
254     goto invalid_caps;
255
256   solarize->info = info;
257
258   solarize->width = GST_VIDEO_INFO_WIDTH (&info);
259   solarize->height = GST_VIDEO_INFO_HEIGHT (&info);
260
261   return TRUE;
262
263   /* ERRORS */
264 invalid_caps:
265   {
266     GST_DEBUG_OBJECT (btrans, "could not parse caps");
267     return FALSE;
268   }
269 }
270
271 /* Actual processing. */
272 static GstFlowReturn
273 gst_solarize_transform (GstBaseTransform * btrans,
274     GstBuffer * in_buf, GstBuffer * out_buf)
275 {
276   GstSolarize *filter = GST_SOLARIZE (btrans);
277   gint video_size, threshold, start, end;
278   guint32 *src, *dest;
279   GstClockTime timestamp;
280   gint64 stream_time;
281   GstVideoFrame in_frame, out_frame;
282
283   gst_video_frame_map (&in_frame, &filter->info, in_buf, GST_MAP_READ);
284   gst_video_frame_map (&out_frame, &filter->info, out_buf, GST_MAP_WRITE);
285
286   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
287   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
288
289   /* GstController: update the properties */
290   timestamp = GST_BUFFER_TIMESTAMP (in_buf);
291   stream_time =
292       gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
293
294   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
295       GST_TIME_ARGS (timestamp));
296
297   if (GST_CLOCK_TIME_IS_VALID (stream_time))
298     gst_object_sync_values (GST_OBJECT (filter), stream_time);
299
300   GST_OBJECT_LOCK (filter);
301   threshold = filter->threshold;
302   start = filter->start;
303   end = filter->end;
304   GST_OBJECT_UNLOCK (filter);
305
306   video_size = filter->width * filter->height;
307   transform (src, dest, video_size, threshold, start, end);
308
309   return GST_FLOW_OK;
310 }
311
312 /* Entry point to initialize the plug-in.
313  * Register the element factories and other features. */
314 gboolean
315 gst_solarize_plugin_init (GstPlugin * solarize)
316 {
317   /* debug category for fltering log messages */
318   GST_DEBUG_CATEGORY_INIT (gst_solarize_debug, "solarize",
319       0, "Template solarize");
320
321   return gst_element_register (solarize, "solarize", GST_RANK_NONE,
322       GST_TYPE_SOLARIZE);
323 }
324
325 /*** Now the image processing work.... ***/
326 /* Keep the values inbounds. */
327 static gint
328 gate_int (gint value, gint min, gint max)
329 {
330   if (value < min) {
331     return min;
332   } else if (value > max) {
333     return max;
334   } else {
335     return value;
336   }
337 }
338
339 /* Transform processes each frame. */
340 static void
341 transform (guint32 * src, guint32 * dest, gint video_area,
342     gint threshold, gint start, gint end)
343 {
344   guint32 in;
345   guint32 color[3];
346   gint x, c;
347   gint floor = 0;
348   gint ceiling = 255;
349
350   gint period, up_length, down_length, height_scale, param;
351
352   period = end - start;
353   if (period == 0) {
354     period = 1;
355   }
356
357   up_length = threshold - start;
358   if (up_length == 0) {
359     up_length = 1;
360   }
361
362   down_length = end - threshold;
363   if (down_length == 0) {
364     down_length = 1;
365   }
366
367   height_scale = ceiling - floor;
368
369   /* Loop through pixels. */
370   for (x = 0; x < video_area; x++) {
371     in = *src++;
372
373     color[0] = (in >> 16) & 0xff;
374     color[1] = (in >> 8) & 0xff;
375     color[2] = (in) & 0xff;
376
377
378     /* Loop through colors. */
379     for (c = 0; c < 3; c++) {
380       param = color[c];
381       param += 256;
382       param -= start;
383       param %= period;
384
385       if (param < up_length) {
386         color[c] = param * height_scale;
387         color[c] /= up_length;
388         color[c] += floor;
389       } else {
390         color[c] = down_length - (param - up_length);
391         color[c] *= height_scale;
392         color[c] /= down_length;
393         color[c] += floor;
394       }
395     }
396
397     color[0] = gate_int (color[0], 0, 255);
398     color[1] = gate_int (color[1], 0, 255);
399     color[2] = gate_int (color[2], 0, 255);
400
401     *dest++ = (color[0] << 16) | (color[1] << 8) | color[2];
402   }
403 }