• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) <2010> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Chromium - burning chrome 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-chromium
49  * @title: chromium
50  *
51  * Chromium breaks the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! chromium ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of chromium on a test stream
57  *
58  */
59 
60 #ifdef HAVE_CONFIG_H
61 #  include <config.h>
62 #endif
63 
64 #include <math.h>
65 #include <gst/gst.h>
66 
67 #include "gstchromium.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_chromium_debug);
70 #define GST_CAT_DEFAULT gst_chromium_debug
71 
72 #define gst_chromium_parent_class parent_class
73 G_DEFINE_TYPE (GstChromium, gst_chromium, GST_TYPE_VIDEO_FILTER);
74 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (chromium, "chromium", GST_RANK_NONE,
75     GST_TYPE_CHROMIUM, GST_DEBUG_CATEGORY_INIT (gst_chromium_debug, "chromium",
76         0, "Template chromium"));
77 
78 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
79 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
80 #else
81 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
82 #endif
83 
84 /* Filter signals and args. */
85 enum
86 {
87   LAST_SIGNAL
88 };
89 
90 enum
91 {
92   PROP_0 = 0,
93   PROP_EDGE_A,
94   PROP_EDGE_B,
95 };
96 
97 /* Initializations */
98 
99 #define DEFAULT_EDGE_A 200
100 #define DEFAULT_EDGE_B 1
101 
102 const float pi = 3.141582f;
103 
104 gint cosTablePi = 512;
105 gint cosTableTwoPi = (2 * 512);
106 gint cosTableOne = 512;
107 gint cosTableMask = 1023;
108 
109 gint cosTable[2 * 512];
110 
111 void setup_cos_table (void);
112 static gint cos_from_table (int angle);
113 static inline int abs_int (int val);
114 static void transform (guint32 * src, guint32 * dest, gint video_area,
115     gint edge_a, gint edge_b);
116 
117 /* The capabilities of the inputs and outputs. */
118 
119 static GstStaticPadTemplate gst_chromium_sink_template =
120 GST_STATIC_PAD_TEMPLATE ("sink",
121     GST_PAD_SINK,
122     GST_PAD_ALWAYS,
123     GST_STATIC_CAPS (CAPS_STR)
124     );
125 
126 static GstStaticPadTemplate gst_chromium_src_template =
127 GST_STATIC_PAD_TEMPLATE ("src",
128     GST_PAD_SRC,
129     GST_PAD_ALWAYS,
130     GST_STATIC_CAPS (CAPS_STR)
131     );
132 
133 static void gst_chromium_set_property (GObject * object, guint prop_id,
134     const GValue * value, GParamSpec * pspec);
135 static void gst_chromium_get_property (GObject * object, guint prop_id,
136     GValue * value, GParamSpec * pspec);
137 static void gst_chromium_finalize (GObject * object);
138 
139 static GstFlowReturn gst_chromium_transform_frame (GstVideoFilter * vfilter,
140     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
141 
142 /* GObject vmethod implementations */
143 
144 /* Initialize the chromium's class. */
145 static void
gst_chromium_class_init(GstChromiumClass * klass)146 gst_chromium_class_init (GstChromiumClass * klass)
147 {
148   GObjectClass *gobject_class = (GObjectClass *) klass;
149   GstElementClass *gstelement_class = (GstElementClass *) klass;
150   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
151 
152   gst_element_class_set_static_metadata (gstelement_class, "Chromium",
153       "Filter/Effect/Video",
154       "Chromium breaks the colors of the video signal.",
155       "Luis de Bethencourt <luis@debethencourt.com>");
156 
157   gst_element_class_add_static_pad_template (gstelement_class,
158       &gst_chromium_sink_template);
159   gst_element_class_add_static_pad_template (gstelement_class,
160       &gst_chromium_src_template);
161 
162   gobject_class->set_property = gst_chromium_set_property;
163   gobject_class->get_property = gst_chromium_get_property;
164   gobject_class->finalize = gst_chromium_finalize;
165 
166   g_object_class_install_property (gobject_class, PROP_EDGE_A,
167       g_param_spec_uint ("edge-a", "Edge A",
168           "First edge parameter", 0, 256, DEFAULT_EDGE_A,
169           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
170 
171   g_object_class_install_property (gobject_class, PROP_EDGE_B,
172       g_param_spec_uint ("edge-b", "Edge B",
173           "Second edge parameter", 0, 256, DEFAULT_EDGE_B,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
175 
176   vfilter_class->transform_frame =
177       GST_DEBUG_FUNCPTR (gst_chromium_transform_frame);
178 }
179 
180 /* Initialize the element,
181  * instantiate pads and add them to element,
182  * set pad callback functions, and
183  * initialize instance structure.
184  */
185 static void
gst_chromium_init(GstChromium * filter)186 gst_chromium_init (GstChromium * filter)
187 {
188   filter->edge_a = DEFAULT_EDGE_A;
189   filter->edge_b = DEFAULT_EDGE_B;
190 
191   setup_cos_table ();
192 }
193 
194 static void
gst_chromium_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)195 gst_chromium_set_property (GObject * object, guint prop_id,
196     const GValue * value, GParamSpec * pspec)
197 {
198   GstChromium *filter = GST_CHROMIUM (object);
199 
200   switch (prop_id) {
201     case PROP_EDGE_A:
202       filter->edge_a = g_value_get_uint (value);
203       break;
204     case PROP_EDGE_B:
205       filter->edge_b = g_value_get_uint (value);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212 
213 static void
gst_chromium_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)214 gst_chromium_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec)
216 {
217   GstChromium *filter = GST_CHROMIUM (object);
218 
219   GST_OBJECT_LOCK (filter);
220   switch (prop_id) {
221     case PROP_EDGE_A:
222       g_value_set_uint (value, filter->edge_a);
223       break;
224     case PROP_EDGE_B:
225       g_value_set_uint (value, filter->edge_b);
226       break;
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230   }
231   GST_OBJECT_UNLOCK (filter);
232 }
233 
234 static void
gst_chromium_finalize(GObject * object)235 gst_chromium_finalize (GObject * object)
236 {
237   G_OBJECT_CLASS (parent_class)->finalize (object);
238 }
239 
240 /* GstElement vmethod implementations */
241 
242 /* Actual processing. */
243 static GstFlowReturn
gst_chromium_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)244 gst_chromium_transform_frame (GstVideoFilter * vfilter,
245     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
246 {
247   GstChromium *filter = GST_CHROMIUM (vfilter);
248   gint video_size, edge_a, edge_b;
249   guint32 *src, *dest;
250   GstClockTime timestamp;
251   gint64 stream_time;
252 
253   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
254   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
255 
256   /* GstController: update the properties */
257   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
258   stream_time =
259       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
260       GST_FORMAT_TIME, timestamp);
261 
262   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
263       GST_TIME_ARGS (timestamp));
264 
265   if (GST_CLOCK_TIME_IS_VALID (stream_time))
266     gst_object_sync_values (GST_OBJECT (filter), stream_time);
267 
268   GST_OBJECT_LOCK (filter);
269   edge_a = filter->edge_a;
270   edge_b = filter->edge_b;
271   GST_OBJECT_UNLOCK (filter);
272 
273   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
274       GST_VIDEO_FRAME_HEIGHT (in_frame);
275   transform (src, dest, video_size, edge_a, edge_b);
276 
277   return GST_FLOW_OK;
278 }
279 
280 /*** Now the image processing work.... ***/
281 /* Set up the cosine table. */
282 void
setup_cos_table(void)283 setup_cos_table (void)
284 {
285   int angle;
286 
287   for (angle = 0; angle < cosTableTwoPi; ++angle) {
288     float angleInRadians = ((float) (angle) / cosTablePi) * pi;
289     cosTable[angle] = (int) (cos (angleInRadians) * cosTableOne);
290   }
291 }
292 
293 /* Keep the values absolute. */
294 static inline int
abs_int(int val)295 abs_int (int val)
296 {
297   if (val > 0) {
298     return val;
299   } else {
300     return -val;
301   }
302 }
303 
304 /* Cosine from Table. */
305 static gint
cos_from_table(int angle)306 cos_from_table (int angle)
307 {
308   angle &= cosTableMask;
309   return cosTable[angle];
310 }
311 
312 /* Transform processes each frame. */
313 static void
transform(guint32 * src,guint32 * dest,gint video_area,gint edge_a,gint edge_b)314 transform (guint32 * src, guint32 * dest, gint video_area,
315     gint edge_a, gint edge_b)
316 {
317   guint32 in;
318   gint x, red, green, blue;
319 
320   for (x = 0; x < video_area; x++) {
321     in = *src++;
322 
323     red = (in >> 16) & 0xff;
324     green = (in >> 8) & 0xff;
325     blue = (in) & 0xff;
326 
327     red = abs_int (cos_from_table ((red + edge_a) + ((red * edge_b) / 2)));
328     green = abs_int (cos_from_table (
329             (green + edge_a) + ((green * edge_b) / 2)));
330     blue = abs_int (cos_from_table ((blue + edge_a) + ((blue * edge_b) / 2)));
331 
332     red = CLAMP (red, 0, 255);
333     green = CLAMP (green, 0, 255);
334     blue = CLAMP (blue, 0, 255);
335 
336     *dest++ = (red << 16) | (green << 8) | blue;
337   }
338 }
339