• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstsolarize.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_solarize_debug);
70 #define GST_CAT_DEFAULT gst_solarize_debug
71 
72 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
73 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
74 #else
75 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
76 #endif
77 
78 /* Filter signals and args. */
79 enum
80 {
81   LAST_SIGNAL
82 };
83 
84 enum
85 {
86   PROP_0 = 0,
87   PROP_THRESHOLD,
88   PROP_START,
89   PROP_END,
90 };
91 
92 /* Initializations */
93 
94 #define DEFAULT_THRESHOLD 127
95 #define DEFAULT_START 50
96 #define DEFAULT_END 185
97 
98 static void transform (guint32 * src, guint32 * dest, gint video_area,
99     gint threshold, gint start, gint end);
100 
101 /* The capabilities of the inputs and outputs. */
102 
103 static GstStaticPadTemplate gst_solarize_sink_template =
104 GST_STATIC_PAD_TEMPLATE ("sink",
105     GST_PAD_SINK,
106     GST_PAD_ALWAYS,
107     GST_STATIC_CAPS (CAPS_STR)
108     );
109 
110 static GstStaticPadTemplate gst_solarize_src_template =
111 GST_STATIC_PAD_TEMPLATE ("src",
112     GST_PAD_SRC,
113     GST_PAD_ALWAYS,
114     GST_STATIC_CAPS (CAPS_STR)
115     );
116 
117 #define gst_solarize_parent_class parent_class
118 G_DEFINE_TYPE (GstSolarize, gst_solarize, GST_TYPE_VIDEO_FILTER);
119 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (solarize, "solarize", GST_RANK_NONE,
120     GST_TYPE_SOLARIZE, GST_DEBUG_CATEGORY_INIT (gst_solarize_debug, "solarize",
121         0, "Template solarize"));
122 
123 static void gst_solarize_set_property (GObject * object, guint prop_id,
124     const GValue * value, GParamSpec * pspec);
125 static void gst_solarize_get_property (GObject * object, guint prop_id,
126     GValue * value, GParamSpec * pspec);
127 static void gst_solarize_finalize (GObject * object);
128 
129 static GstFlowReturn gst_solarize_transform_frame (GstVideoFilter * vfilter,
130     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
131 
132 /* GObject vmethod implementations */
133 
134 /* Initialize the solarize's class. */
135 static void
gst_solarize_class_init(GstSolarizeClass * klass)136 gst_solarize_class_init (GstSolarizeClass * klass)
137 {
138   GObjectClass *gobject_class = (GObjectClass *) klass;
139   GstElementClass *gstelement_class = (GstElementClass *) klass;
140   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
141 
142   gst_element_class_set_static_metadata (gstelement_class,
143       "Solarize",
144       "Filter/Effect/Video",
145       "Solarize tunable inverse in the video signal.",
146       "Luis de Bethencourt <luis@debethencourt.com>");
147 
148   gst_element_class_add_static_pad_template (gstelement_class,
149       &gst_solarize_sink_template);
150   gst_element_class_add_static_pad_template (gstelement_class,
151       &gst_solarize_src_template);
152 
153   gobject_class->set_property = gst_solarize_set_property;
154   gobject_class->get_property = gst_solarize_get_property;
155   gobject_class->finalize = gst_solarize_finalize;
156 
157   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
158       g_param_spec_uint ("threshold", "Threshold",
159           "Threshold parameter", 0, 256, DEFAULT_THRESHOLD,
160           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
161 
162   g_object_class_install_property (gobject_class, PROP_START,
163       g_param_spec_uint ("start", "Start",
164           "Start parameter", 0, 256, DEFAULT_START,
165           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
166 
167   g_object_class_install_property (gobject_class, PROP_END,
168       g_param_spec_uint ("end", "End",
169           "End parameter", 0, 256, DEFAULT_END,
170           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
171 
172   vfilter_class->transform_frame =
173       GST_DEBUG_FUNCPTR (gst_solarize_transform_frame);
174 }
175 
176 /* Initialize the element,
177  * instantiate pads and add them to element,
178  * set pad callback functions, and
179  * initialize instance structure.
180  */
181 static void
gst_solarize_init(GstSolarize * filter)182 gst_solarize_init (GstSolarize * filter)
183 {
184   filter->threshold = DEFAULT_THRESHOLD;
185   filter->start = DEFAULT_START;
186   filter->end = DEFAULT_END;
187 }
188 
189 static void
gst_solarize_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)190 gst_solarize_set_property (GObject * object, guint prop_id,
191     const GValue * value, GParamSpec * pspec)
192 {
193   GstSolarize *filter = GST_SOLARIZE (object);
194 
195   switch (prop_id) {
196     case PROP_THRESHOLD:
197       filter->threshold = g_value_get_uint (value);
198       break;
199     case PROP_START:
200       filter->start = g_value_get_uint (value);
201       break;
202     case PROP_END:
203       filter->end = g_value_get_uint (value);
204       break;
205     default:
206       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207       break;
208   }
209 }
210 
211 static void
gst_solarize_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)212 gst_solarize_get_property (GObject * object, guint prop_id,
213     GValue * value, GParamSpec * pspec)
214 {
215   GstSolarize *filter = GST_SOLARIZE (object);
216 
217   GST_OBJECT_LOCK (filter);
218   switch (prop_id) {
219     case PROP_THRESHOLD:
220       g_value_set_uint (value, filter->threshold);
221       break;
222     case PROP_START:
223       g_value_set_uint (value, filter->start);
224       break;
225     case PROP_END:
226       g_value_set_uint (value, filter->end);
227       break;
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231   }
232   GST_OBJECT_UNLOCK (filter);
233 }
234 
235 static void
gst_solarize_finalize(GObject * object)236 gst_solarize_finalize (GObject * object)
237 {
238   G_OBJECT_CLASS (parent_class)->finalize (object);
239 }
240 
241 /* GstElement vmethod implementations */
242 
243 /* Actual processing. */
244 static GstFlowReturn
gst_solarize_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)245 gst_solarize_transform_frame (GstVideoFilter * vfilter,
246     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
247 {
248   GstSolarize *filter = GST_SOLARIZE (vfilter);
249   gint video_size, threshold, start, end;
250   guint32 *src, *dest;
251   GstClockTime timestamp;
252   gint64 stream_time;
253 
254   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
255   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
256 
257   /* GstController: update the properties */
258   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
259   stream_time =
260       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
261       GST_FORMAT_TIME, timestamp);
262 
263   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
264       GST_TIME_ARGS (timestamp));
265 
266   if (GST_CLOCK_TIME_IS_VALID (stream_time))
267     gst_object_sync_values (GST_OBJECT (filter), stream_time);
268 
269   GST_OBJECT_LOCK (filter);
270   threshold = filter->threshold;
271   start = filter->start;
272   end = filter->end;
273   GST_OBJECT_UNLOCK (filter);
274 
275   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
276       GST_VIDEO_FRAME_HEIGHT (in_frame);
277 
278   transform (src, dest, video_size, threshold, start, end);
279 
280   return GST_FLOW_OK;
281 }
282 
283 /*** Now the image processing work.... ***/
284 
285 /* Transform processes each frame. */
286 static void
transform(guint32 * src,guint32 * dest,gint video_area,gint threshold,gint start,gint end)287 transform (guint32 * src, guint32 * dest, gint video_area,
288     gint threshold, gint start, gint end)
289 {
290   guint32 in;
291   guint32 color[3];
292   gint period = 1, up_length = 1, down_length = 1;
293   gint x, c;
294   gint param;
295   static const guint ceiling = 255;
296 
297   if (end != start)
298     period = end - start;
299 
300   if (threshold != start)
301     up_length = threshold - start;
302 
303   if (threshold != end)
304     down_length = end - threshold;
305 
306   /* Loop through pixels. */
307   for (x = 0; x < video_area; x++) {
308     in = *src++;
309 
310     color[0] = (in >> 16) & 0xff;
311     color[1] = (in >> 8) & 0xff;
312     color[2] = (in) & 0xff;
313 
314     /* Loop through colors. */
315     for (c = 0; c < 3; c++) {
316       param = color[c];
317       param += 256;
318       param -= start;
319       param %= period;
320 
321       if (param < up_length) {
322         color[c] = param * ceiling;
323         color[c] /= up_length;
324       } else {
325         color[c] = down_length - (param - up_length);
326         color[c] *= ceiling;
327         color[c] /= down_length;
328       }
329     }
330 
331     /* Clamp colors */
332     for (c = 0; c < 3; c++) {
333       if (G_UNLIKELY (color[c] > 255))
334         color[c] = 255;
335     }
336 
337     *dest++ = (color[0] << 16) | (color[1] << 8) | color[2];
338   }
339 }
340