• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) <2010-2015> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Dilate - dilated eye 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-dilate
49  * @title: dilate
50  *
51  * Dilate adjusts the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! dilate ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of dilate 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 "gstdilate.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_dilate_debug);
70 #define GST_CAT_DEFAULT gst_dilate_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 #define gst_dilate_parent_class parent_class
79 G_DEFINE_TYPE (GstDilate, gst_dilate, GST_TYPE_VIDEO_FILTER);
80 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (dilate, "dilate", GST_RANK_NONE,
81     GST_TYPE_DILATE, GST_DEBUG_CATEGORY_INIT (gst_dilate_debug, "dilate", 0,
82         "Template dilate"));
83 
84 /* Filter signals and args. */
85 enum
86 {
87   LAST_SIGNAL
88 };
89 
90 enum
91 {
92   PROP_0,
93   PROP_ERODE,
94 };
95 
96 /* Initializations */
97 
98 #define DEFAULT_ERODE FALSE
99 
100 static void transform (guint32 * src, guint32 * dest, gint video_area,
101     gint width, gint height, gboolean erode);
102 static inline guint32 get_luminance (guint32 in);
103 
104 /* The capabilities of the inputs and outputs. */
105 
106 static GstStaticPadTemplate gst_dilate_sink_template =
107 GST_STATIC_PAD_TEMPLATE ("sink",
108     GST_PAD_SINK,
109     GST_PAD_ALWAYS,
110     GST_STATIC_CAPS (CAPS_STR)
111     );
112 
113 static GstStaticPadTemplate gst_dilate_src_template =
114 GST_STATIC_PAD_TEMPLATE ("src",
115     GST_PAD_SRC,
116     GST_PAD_ALWAYS,
117     GST_STATIC_CAPS (CAPS_STR)
118     );
119 
120 static void gst_dilate_set_property (GObject * object, guint prop_id,
121     const GValue * value, GParamSpec * pspec);
122 static void gst_dilate_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec);
124 static void gst_dilate_finalize (GObject * object);
125 
126 static GstFlowReturn gst_dilate_transform_frame (GstVideoFilter * vfilter,
127     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
128 
129 /* GObject vmethod implementations */
130 
131 /* Initialize the dilate's class. */
132 static void
gst_dilate_class_init(GstDilateClass * klass)133 gst_dilate_class_init (GstDilateClass * klass)
134 {
135   GObjectClass *gobject_class = (GObjectClass *) klass;
136   GstElementClass *gstelement_class = (GstElementClass *) klass;
137   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
138 
139   gst_element_class_set_static_metadata (gstelement_class,
140       "Dilate",
141       "Filter/Effect/Video",
142       "Dilate copies the brightest pixel around.",
143       "Luis de Bethencourt <luis@debethencourt.com>");
144 
145   gst_element_class_add_static_pad_template (gstelement_class,
146       &gst_dilate_sink_template);
147   gst_element_class_add_static_pad_template (gstelement_class,
148       &gst_dilate_src_template);
149 
150   gobject_class->set_property = gst_dilate_set_property;
151   gobject_class->get_property = gst_dilate_get_property;
152   gobject_class->finalize = gst_dilate_finalize;
153 
154   g_object_class_install_property (gobject_class, PROP_ERODE,
155       g_param_spec_boolean ("erode", "Erode", "Erode parameter", FALSE,
156           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
157 
158   vfilter_class->transform_frame =
159       GST_DEBUG_FUNCPTR (gst_dilate_transform_frame);
160 }
161 
162 /* Initialize the element,
163  * instantiate pads and add them to element,
164  * set pad callback functions, and
165  * initialize instance structure.
166  */
167 static void
gst_dilate_init(GstDilate * filter)168 gst_dilate_init (GstDilate * filter)
169 {
170   filter->erode = DEFAULT_ERODE;
171 }
172 
173 static void
gst_dilate_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)174 gst_dilate_set_property (GObject * object, guint prop_id,
175     const GValue * value, GParamSpec * pspec)
176 {
177   GstDilate *filter = GST_DILATE (object);
178 
179   switch (prop_id) {
180     case PROP_ERODE:
181       filter->erode = g_value_get_boolean (value);
182       break;
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188 
189 static void
gst_dilate_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)190 gst_dilate_get_property (GObject * object, guint prop_id,
191     GValue * value, GParamSpec * pspec)
192 {
193   GstDilate *filter = GST_DILATE (object);
194 
195   GST_OBJECT_LOCK (filter);
196   switch (prop_id) {
197     case PROP_ERODE:
198       g_value_set_boolean (value, filter->erode);
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204   GST_OBJECT_UNLOCK (filter);
205 }
206 
207 static void
gst_dilate_finalize(GObject * object)208 gst_dilate_finalize (GObject * object)
209 {
210   G_OBJECT_CLASS (parent_class)->finalize (object);
211 }
212 
213 /* GstElement vmethod implementations */
214 
215 /* Actual processing. */
216 static GstFlowReturn
gst_dilate_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)217 gst_dilate_transform_frame (GstVideoFilter * vfilter,
218     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
219 {
220   GstDilate *filter = GST_DILATE (vfilter);
221   gint video_size, width, height;
222   gboolean erode;
223   guint32 *src, *dest;
224   GstClockTime timestamp;
225   gint64 stream_time;
226 
227   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
228   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
229 
230   width = GST_VIDEO_FRAME_WIDTH (in_frame);
231   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
232 
233   /* GstController: update the properties */
234   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
235   stream_time =
236       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
237       GST_FORMAT_TIME, timestamp);
238 
239   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
240       GST_TIME_ARGS (timestamp));
241 
242   if (GST_CLOCK_TIME_IS_VALID (stream_time))
243     gst_object_sync_values (GST_OBJECT (filter), stream_time);
244 
245   GST_OBJECT_LOCK (filter);
246   erode = filter->erode;
247   GST_OBJECT_UNLOCK (filter);
248 
249   video_size = width * height;
250   transform (src, dest, video_size, width, height, erode);
251 
252   return GST_FLOW_OK;
253 }
254 
255 /*** Now the image processing work.... ***/
256 
257 /* Return luminance of the color */
258 static inline guint32
get_luminance(guint32 in)259 get_luminance (guint32 in)
260 {
261   guint32 red, green, blue, luminance;
262 
263   red = (in >> 16) & 0xff;
264   green = (in >> 8) & 0xff;
265   blue = (in) & 0xff;
266 
267   luminance = ((90 * red) + (115 * green) + (51 * blue));
268 
269   return luminance;
270 }
271 
272 /* Transform processes each frame. */
273 static void
transform(guint32 * src,guint32 * dest,gint video_area,gint width,gint height,gboolean erode)274 transform (guint32 * src, guint32 * dest, gint video_area, gint width,
275     gint height, gboolean erode)
276 {
277   guint32 out_luminance, down_luminance, right_luminance;
278   guint32 up_luminance, left_luminance;
279 
280   guint32 *src_end = src + video_area;
281   guint32 *up;
282   guint32 *left;
283   guint32 *down;
284   guint32 *right;
285 
286   while (src != src_end) {
287     guint32 *src_line_start = src;
288     guint32 *src_line_end = src + width;
289 
290     while (src != src_line_end) {
291       up = src - width;
292       if (up < src) {
293         up = src;
294       }
295 
296       left = src - 1;
297       if (left < src_line_start) {
298         left = src;
299       }
300 
301       down = src + width;
302       if (down >= src_end) {
303         down = src;
304       }
305 
306       right = src + 1;
307       if (right >= src_line_end) {
308         right = src;
309       }
310 
311       *dest = *src;
312       out_luminance = get_luminance (*src);
313 
314       down_luminance = get_luminance (*down);
315       if ((erode && down_luminance < out_luminance) ||
316           (!erode && down_luminance > out_luminance)) {
317         *dest = *down;
318         out_luminance = down_luminance;
319       }
320 
321       right_luminance = get_luminance (*right);
322       if ((erode && right_luminance < out_luminance) ||
323           (!erode && right_luminance > out_luminance)) {
324         *dest = *right;
325         out_luminance = right_luminance;
326       }
327 
328       up_luminance = get_luminance (*up);
329       if ((erode && up_luminance < out_luminance) ||
330           (!erode && up_luminance > out_luminance)) {
331         *dest = *up;
332         out_luminance = up_luminance;
333       }
334 
335       left_luminance = get_luminance (*left);
336       if ((erode && left_luminance < out_luminance) ||
337           (!erode && left_luminance > out_luminance)) {
338         *dest = *left;
339       }
340 
341       src += 1;
342       dest += 1;
343     }
344   }
345 }
346