8fc9ff69e8feae0974089fedfff6cbba6e742025
[platform/upstream/gstreamer.git] / gst / gaudieffects / gstsolarize.c
1 /*
2  * GStreamer
3  * Copyright (C) <2010-2015> 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., 51 Franklin St, Fifth Floor,
44  * Boston, MA 02110-1301, USA.
45  */
46
47 /**
48  * SECTION:element-solarize
49  * @title: solarize
50  *
51  * Solarize does a smart inverse in a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! solarize ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of solarize on a test stream
57  *
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 };
92
93 /* Initializations */
94
95 #define DEFAULT_THRESHOLD 127
96 #define DEFAULT_START 50
97 #define DEFAULT_END 185
98
99 static void transform (guint32 * src, guint32 * dest, gint video_area,
100     gint threshold, gint start, gint end);
101
102 /* The capabilities of the inputs and outputs. */
103
104 static GstStaticPadTemplate gst_solarize_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS (CAPS_STR)
109     );
110
111 static GstStaticPadTemplate gst_solarize_src_template =
112 GST_STATIC_PAD_TEMPLATE ("src",
113     GST_PAD_SRC,
114     GST_PAD_ALWAYS,
115     GST_STATIC_CAPS (CAPS_STR)
116     );
117
118 #define gst_solarize_parent_class parent_class
119 G_DEFINE_TYPE (GstSolarize, gst_solarize, GST_TYPE_VIDEO_FILTER);
120
121 static void gst_solarize_set_property (GObject * object, guint prop_id,
122     const GValue * value, GParamSpec * pspec);
123 static void gst_solarize_get_property (GObject * object, guint prop_id,
124     GValue * value, GParamSpec * pspec);
125 static void gst_solarize_finalize (GObject * object);
126
127 static GstFlowReturn gst_solarize_transform_frame (GstVideoFilter * vfilter,
128     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
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   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
139
140   gst_element_class_set_static_metadata (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_static_pad_template (gstelement_class,
147       &gst_solarize_sink_template);
148   gst_element_class_add_static_pad_template (gstelement_class,
149       &gst_solarize_src_template);
150
151   gobject_class->set_property = gst_solarize_set_property;
152   gobject_class->get_property = gst_solarize_get_property;
153   gobject_class->finalize = gst_solarize_finalize;
154
155   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
156       g_param_spec_uint ("threshold", "Threshold",
157           "Threshold parameter", 0, 256, DEFAULT_THRESHOLD,
158           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
159
160   g_object_class_install_property (gobject_class, PROP_START,
161       g_param_spec_uint ("start", "Start",
162           "Start parameter", 0, 256, DEFAULT_START,
163           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
164
165   g_object_class_install_property (gobject_class, PROP_END,
166       g_param_spec_uint ("end", "End",
167           "End parameter", 0, 256, DEFAULT_END,
168           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
169
170   vfilter_class->transform_frame =
171       GST_DEBUG_FUNCPTR (gst_solarize_transform_frame);
172 }
173
174 /* Initialize the element,
175  * instantiate pads and add them to element,
176  * set pad calback functions, and
177  * initialize instance structure.
178  */
179 static void
180 gst_solarize_init (GstSolarize * filter)
181 {
182   filter->threshold = DEFAULT_THRESHOLD;
183   filter->start = DEFAULT_START;
184   filter->end = DEFAULT_END;
185 }
186
187 static void
188 gst_solarize_set_property (GObject * object, guint prop_id,
189     const GValue * value, GParamSpec * pspec)
190 {
191   GstSolarize *filter = GST_SOLARIZE (object);
192
193   switch (prop_id) {
194     case PROP_THRESHOLD:
195       filter->threshold = g_value_get_uint (value);
196       break;
197     case PROP_START:
198       filter->start = g_value_get_uint (value);
199       break;
200     case PROP_END:
201       filter->end = g_value_get_uint (value);
202       break;
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206   }
207 }
208
209 static void
210 gst_solarize_get_property (GObject * object, guint prop_id,
211     GValue * value, GParamSpec * pspec)
212 {
213   GstSolarize *filter = GST_SOLARIZE (object);
214
215   GST_OBJECT_LOCK (filter);
216   switch (prop_id) {
217     case PROP_THRESHOLD:
218       g_value_set_uint (value, filter->threshold);
219       break;
220     case PROP_START:
221       g_value_set_uint (value, filter->start);
222       break;
223     case PROP_END:
224       g_value_set_uint (value, filter->end);
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229   }
230   GST_OBJECT_UNLOCK (filter);
231 }
232
233 static void
234 gst_solarize_finalize (GObject * object)
235 {
236   G_OBJECT_CLASS (parent_class)->finalize (object);
237 }
238
239 /* GstElement vmethod implementations */
240
241 /* Actual processing. */
242 static GstFlowReturn
243 gst_solarize_transform_frame (GstVideoFilter * vfilter,
244     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
245 {
246   GstSolarize *filter = GST_SOLARIZE (vfilter);
247   gint video_size, threshold, start, end;
248   guint32 *src, *dest;
249   GstClockTime timestamp;
250   gint64 stream_time;
251
252   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
253   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
254
255   /* GstController: update the properties */
256   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
257   stream_time =
258       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
259       GST_FORMAT_TIME, timestamp);
260
261   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
262       GST_TIME_ARGS (timestamp));
263
264   if (GST_CLOCK_TIME_IS_VALID (stream_time))
265     gst_object_sync_values (GST_OBJECT (filter), stream_time);
266
267   GST_OBJECT_LOCK (filter);
268   threshold = filter->threshold;
269   start = filter->start;
270   end = filter->end;
271   GST_OBJECT_UNLOCK (filter);
272
273   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
274       GST_VIDEO_FRAME_HEIGHT (in_frame);
275
276   transform (src, dest, video_size, threshold, start, end);
277
278   return GST_FLOW_OK;
279 }
280
281 /* Entry point to initialize the plug-in.
282  * Register the element factories and other features. */
283 gboolean
284 gst_solarize_plugin_init (GstPlugin * solarize)
285 {
286   /* debug category for fltering log messages */
287   GST_DEBUG_CATEGORY_INIT (gst_solarize_debug, "solarize",
288       0, "Template solarize");
289
290   return gst_element_register (solarize, "solarize", GST_RANK_NONE,
291       GST_TYPE_SOLARIZE);
292 }
293
294 /*** Now the image processing work.... ***/
295
296 /* Transform processes each frame. */
297 static void
298 transform (guint32 * src, guint32 * dest, gint video_area,
299     gint threshold, gint start, gint end)
300 {
301   guint32 in;
302   guint32 color[3];
303   gint period = 1, up_length = 1, down_length = 1;
304   gint x, c;
305   gint param;
306   static const guint ceiling = 255;
307
308   if (end != start)
309     period = end - start;
310
311   if (threshold != start)
312     up_length = threshold - start;
313
314   if (threshold != end)
315     down_length = end - threshold;
316
317   /* Loop through pixels. */
318   for (x = 0; x < video_area; x++) {
319     in = *src++;
320
321     color[0] = (in >> 16) & 0xff;
322     color[1] = (in >> 8) & 0xff;
323     color[2] = (in) & 0xff;
324
325     /* Loop through colors. */
326     for (c = 0; c < 3; c++) {
327       param = color[c];
328       param += 256;
329       param -= start;
330       param %= period;
331
332       if (param < up_length) {
333         color[c] = param * ceiling;
334         color[c] /= up_length;
335       } else {
336         color[c] = down_length - (param - up_length);
337         color[c] *= ceiling;
338         color[c] /= down_length;
339       }
340     }
341
342     /* Clamp colors */
343     for (c = 0; c < 3; c++) {
344       if (G_UNLIKELY (color[c] > 255))
345         color[c] = 255;
346     }
347
348     *dest++ = (color[0] << 16) | (color[1] << 8) | color[2];
349   }
350 }