various: fix pad template ref leaks
[platform/upstream/gstreamer.git] / gst / gaudieffects / gstsolarize.c
1 /*
2  * GStreamer
3  * Copyright (C) 2010 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 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_solarize_debug);
74 #define GST_CAT_DEFAULT gst_solarize_debug
75
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
78 #else
79 #define CAPS_STR GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR
80 #endif
81
82 /* Filter signals and args. */
83 enum
84 {
85   LAST_SIGNAL
86 };
87
88 enum
89 {
90   PROP_0 = 0,
91   PROP_THRESHOLD,
92   PROP_START,
93   PROP_END,
94   PROP_SILENT
95 };
96
97 /* Initializations */
98
99 #define DEFAULT_THRESHOLD 127
100 #define DEFAULT_START 50
101 #define DEFAULT_END 185
102
103 static gint gate_int (gint value, gint min, gint max);
104 static void transform (guint32 * src, guint32 * dest, gint video_area,
105     gint threshold, gint start, gint end);
106
107 /* The capabilities of the inputs and outputs. */
108
109 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
110     GST_PAD_SINK,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS (CAPS_STR)
113     );
114
115 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
116     GST_PAD_SRC,
117     GST_PAD_ALWAYS,
118     GST_STATIC_CAPS (CAPS_STR)
119     );
120
121 GST_BOILERPLATE (GstSolarize, gst_solarize, GstVideoFilter,
122     GST_TYPE_VIDEO_FILTER);
123
124 static void gst_solarize_set_property (GObject * object, guint prop_id,
125     const GValue * value, GParamSpec * pspec);
126 static void gst_solarize_get_property (GObject * object, guint prop_id,
127     GValue * value, GParamSpec * pspec);
128
129 static gboolean gst_solarize_set_caps (GstBaseTransform * btrans,
130     GstCaps * incaps, GstCaps * outcaps);
131 static GstFlowReturn gst_solarize_transform (GstBaseTransform * btrans,
132     GstBuffer * in_buf, GstBuffer * out_buf);
133
134 /* GObject vmethod implementations */
135
136 static void
137 gst_solarize_base_init (gpointer gclass)
138 {
139   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
140
141   gst_element_class_set_details_simple (element_class,
142       "Solarize",
143       "Filter/Effect/Video",
144       "Solarize tunable inverse in the video signal.",
145       "Luis de Bethencourt <luis@debethencourt.com>");
146
147   gst_element_class_add_static_pad_template (element_class, &src_factory);
148   gst_element_class_add_static_pad_template (element_class, &sink_factory);
149 }
150
151 /* Initialize the solarize's class. */
152 static void
153 gst_solarize_class_init (GstSolarizeClass * klass)
154 {
155   GObjectClass *gobject_class = (GObjectClass *) klass;
156   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
157
158   gobject_class->set_property = gst_solarize_set_property;
159   gobject_class->get_property = gst_solarize_get_property;
160
161   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
162       g_param_spec_uint ("threshold", "Threshold",
163           "Threshold parameter", 0, 256, DEFAULT_THRESHOLD,
164           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
165
166   g_object_class_install_property (gobject_class, PROP_START,
167       g_param_spec_uint ("start", "Start",
168           "Start parameter", 0, 256, DEFAULT_START,
169           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
170
171   g_object_class_install_property (gobject_class, PROP_END,
172       g_param_spec_uint ("end", "End",
173           "End parameter", 0, 256, DEFAULT_END,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
175
176   g_object_class_install_property (gobject_class, PROP_SILENT,
177       g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
178           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179
180   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_solarize_set_caps);
181   trans_class->transform = GST_DEBUG_FUNCPTR (gst_solarize_transform);
182 }
183
184 /* Initialize the element,
185  * instantiate pads and add them to element,
186  * set pad calback functions, and
187  * initialize instance structure.
188  */
189 static void
190 gst_solarize_init (GstSolarize * filter, GstSolarizeClass * gclass)
191 {
192   filter->threshold = DEFAULT_THRESHOLD;
193   filter->start = DEFAULT_START;
194   filter->end = DEFAULT_END;
195   filter->silent = FALSE;
196 }
197
198 static void
199 gst_solarize_set_property (GObject * object, guint prop_id,
200     const GValue * value, GParamSpec * pspec)
201 {
202   GstSolarize *filter = GST_SOLARIZE (object);
203
204   switch (prop_id) {
205     case PROP_SILENT:
206       filter->silent = g_value_get_boolean (value);
207       break;
208     case PROP_THRESHOLD:
209       filter->threshold = g_value_get_uint (value);
210       break;
211     case PROP_START:
212       filter->start = g_value_get_uint (value);
213       break;
214     case PROP_END:
215       filter->end = g_value_get_uint (value);
216       break;
217     default:
218       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219       break;
220   }
221 }
222
223 static void
224 gst_solarize_get_property (GObject * object, guint prop_id,
225     GValue * value, GParamSpec * pspec)
226 {
227   GstSolarize *filter = GST_SOLARIZE (object);
228
229   GST_OBJECT_LOCK (filter);
230   switch (prop_id) {
231     case PROP_SILENT:
232       g_value_set_boolean (value, filter->silent);
233       break;
234     case PROP_THRESHOLD:
235       g_value_set_uint (value, filter->threshold);
236       break;
237     case PROP_START:
238       g_value_set_uint (value, filter->start);
239       break;
240     case PROP_END:
241       g_value_set_uint (value, filter->end);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247   GST_OBJECT_UNLOCK (filter);
248 }
249
250 /* GstElement vmethod implementations */
251
252 /* Handle the link with other elements. */
253 static gboolean
254 gst_solarize_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
255     GstCaps * outcaps)
256 {
257   GstSolarize *filter = GST_SOLARIZE (btrans);
258   GstStructure *structure;
259   gboolean ret = FALSE;
260
261   GST_OBJECT_LOCK (filter);
262   structure = gst_caps_get_structure (incaps, 0);
263   if (gst_structure_get_int (structure, "width", &filter->width) &&
264       gst_structure_get_int (structure, "height", &filter->height)) {
265     ret = TRUE;
266   }
267   GST_OBJECT_UNLOCK (filter);
268
269   return ret;
270 }
271
272 /* Actual processing. */
273 static GstFlowReturn
274 gst_solarize_transform (GstBaseTransform * btrans,
275     GstBuffer * in_buf, GstBuffer * out_buf)
276 {
277   GstSolarize *filter = GST_SOLARIZE (btrans);
278   gint video_size, threshold, start, end;
279   guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
280   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
281   GstClockTime timestamp;
282   gint64 stream_time;
283
284   /* GstController: update the properties */
285   timestamp = GST_BUFFER_TIMESTAMP (in_buf);
286   stream_time =
287       gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
288
289   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
290       GST_TIME_ARGS (timestamp));
291
292   if (GST_CLOCK_TIME_IS_VALID (stream_time))
293     gst_object_sync_values (G_OBJECT (filter), stream_time);
294
295   GST_OBJECT_LOCK (filter);
296   threshold = filter->threshold;
297   start = filter->start;
298   end = filter->end;
299   GST_OBJECT_UNLOCK (filter);
300
301   video_size = filter->width * filter->height;
302   transform (src, dest, video_size, threshold, start, end);
303
304   return GST_FLOW_OK;
305 }
306
307 /* Entry point to initialize the plug-in.
308  * Register the element factories and other features. */
309 gboolean
310 gst_solarize_plugin_init (GstPlugin * solarize)
311 {
312   /* debug category for fltering log messages */
313   GST_DEBUG_CATEGORY_INIT (gst_solarize_debug, "solarize",
314       0, "Template solarize");
315
316   return gst_element_register (solarize, "solarize", GST_RANK_NONE,
317       GST_TYPE_SOLARIZE);
318 }
319
320 /*** Now the image processing work.... ***/
321 /* Keep the values inbounds. */
322 static gint
323 gate_int (gint value, gint min, gint max)
324 {
325   if (value < min) {
326     return min;
327   } else if (value > max) {
328     return max;
329   } else {
330     return value;
331   }
332 }
333
334 /* Transform processes each frame. */
335 static void
336 transform (guint32 * src, guint32 * dest, gint video_area,
337     gint threshold, gint start, gint end)
338 {
339   guint32 in;
340   guint32 color[3];
341   gint x, c;
342   gint floor = 0;
343   gint ceiling = 255;
344
345   gint period, up_length, down_length, height_scale, param;
346
347   period = end - start;
348   if (period == 0) {
349     period = 1;
350   }
351
352   up_length = threshold - start;
353   if (up_length == 0) {
354     up_length = 1;
355   }
356
357   down_length = end - threshold;
358   if (down_length == 0) {
359     down_length = 1;
360   }
361
362   height_scale = ceiling - floor;
363
364   /* Loop through pixels. */
365   for (x = 0; x < video_area; x++) {
366     in = *src++;
367
368     color[0] = (in >> 16) & 0xff;
369     color[1] = (in >> 8) & 0xff;
370     color[2] = (in) & 0xff;
371
372
373     /* Loop through colors. */
374     for (c = 0; c < 3; c++) {
375       param = color[c];
376       param += 256;
377       param -= start;
378       param %= period;
379
380       if (param < up_length) {
381         color[c] = param * height_scale;
382         color[c] /= up_length;
383         color[c] += floor;
384       } else {
385         color[c] = down_length - (param - up_length);
386         color[c] *= height_scale;
387         color[c] /= down_length;
388         color[c] += floor;
389       }
390     }
391
392     color[0] = gate_int (color[0], 0, 255);
393     color[1] = gate_int (color[1], 0, 255);
394     color[2] = gate_int (color[2], 0, 255);
395
396     *dest++ = (color[0] << 16) | (color[1] << 8) | color[2];
397   }
398 }