Branch data Line data Source code
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., 59 Temple Place - Suite 330,
44 : : * Boston, MA 02111-1307, USA.
45 : : */
46 : :
47 : : /**
48 : : * SECTION:element-chromium
49 : : *
50 : : * Chromium breaks the colors of a video stream in realtime.
51 : : *
52 : : * <refsect2>
53 : : * <title>Example launch line</title>
54 : : * |[
55 : : * gst-launch -v videotestsrc ! chromium ! ffmpegcolorspace ! autovideosink
56 : : * ]| This pipeline shows the effect of chromium on a test stream
57 : : * </refsect2>
58 : : */
59 : :
60 : : #ifdef HAVE_CONFIG_H
61 : : # include <config.h>
62 : : #endif
63 : :
64 : : #include <math.h>
65 : : #include <gst/gst.h>
66 : : #include <gst/controller/gstcontroller.h>
67 : :
68 : : #include "gstplugin.h"
69 : : #include "gstchromium.h"
70 : :
71 : : #include <gst/video/video.h>
72 : :
73 : : GST_DEBUG_CATEGORY_STATIC (gst_chromium_debug);
74 : : #define GST_CAT_DEFAULT gst_chromium_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_EDGE_A,
92 : : PROP_EDGE_B,
93 : : PROP_SILENT
94 : : };
95 : :
96 : : /* Initializations */
97 : :
98 : : #define DEFAULT_EDGE_A 200
99 : : #define DEFAULT_EDGE_B 1
100 : :
101 : : const float pi = 3.141582f;
102 : :
103 : : gint cosTablePi = 512;
104 : : gint cosTableTwoPi = (2 * 512);
105 : : gint cosTableOne = 512;
106 : : gint cosTableMask = 1023;
107 : :
108 : : gint cosTable[2 * 512];
109 : :
110 : : static gint gate_int (gint value, gint min, gint max);
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 sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
120 : : GST_PAD_SINK,
121 : : GST_PAD_ALWAYS,
122 : : GST_STATIC_CAPS (CAPS_STR)
123 : : );
124 : :
125 : : static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
126 : : GST_PAD_SRC,
127 : : GST_PAD_ALWAYS,
128 : : GST_STATIC_CAPS (CAPS_STR)
129 : : );
130 : :
131 [ + + ]: 21 : GST_BOILERPLATE (GstChromium, gst_chromium, GstVideoFilter,
132 : 21 : GST_TYPE_VIDEO_FILTER);
133 : :
134 : : static void gst_chromium_set_property (GObject * object, guint prop_id,
135 : : const GValue * value, GParamSpec * pspec);
136 : : static void gst_chromium_get_property (GObject * object, guint prop_id,
137 : : GValue * value, GParamSpec * pspec);
138 : :
139 : : static gboolean gst_chromium_set_caps (GstBaseTransform * btrans,
140 : : GstCaps * incaps, GstCaps * outcaps);
141 : : static GstFlowReturn gst_chromium_transform (GstBaseTransform * btrans,
142 : : GstBuffer * in_buf, GstBuffer * out_buf);
143 : :
144 : : /* GObject vmethod implementations */
145 : :
146 : : static void
147 : 6 : gst_chromium_base_init (gpointer gclass)
148 : : {
149 : 6 : GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
150 : :
151 : 6 : gst_element_class_set_details_simple (element_class,
152 : : "Chromium",
153 : : "Filter/Effect/Video",
154 : : "Chromium breaks the colors of the video signal.",
155 : : "Luis de Bethencourt <luis@debethencourt.com>");
156 : :
157 : 6 : gst_element_class_add_pad_template (element_class,
158 : : gst_static_pad_template_get (&src_factory));
159 : 6 : gst_element_class_add_pad_template (element_class,
160 : : gst_static_pad_template_get (&sink_factory));
161 : 6 : }
162 : :
163 : : /* Initialize the chromium's class. */
164 : : static void
165 : 6 : gst_chromium_class_init (GstChromiumClass * klass)
166 : : {
167 : 6 : GObjectClass *gobject_class = (GObjectClass *) klass;
168 : 6 : GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
169 : :
170 : 6 : gobject_class->set_property = gst_chromium_set_property;
171 : 6 : gobject_class->get_property = gst_chromium_get_property;
172 : :
173 : 6 : g_object_class_install_property (gobject_class, PROP_EDGE_A,
174 : : g_param_spec_uint ("edge-a", "Edge A",
175 : : "First edge parameter", 0, 256, DEFAULT_EDGE_A,
176 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
177 : :
178 : 6 : g_object_class_install_property (gobject_class, PROP_EDGE_B,
179 : : g_param_spec_uint ("edge-b", "Edge B",
180 : : "Second edge parameter", 0, 256, DEFAULT_EDGE_B,
181 : : G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
182 : :
183 : 6 : g_object_class_install_property (gobject_class, PROP_SILENT,
184 : : g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
185 : : FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 : :
187 : 6 : trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_chromium_set_caps);
188 : 6 : trans_class->transform = GST_DEBUG_FUNCPTR (gst_chromium_transform);
189 : 6 : }
190 : :
191 : : /* Initialize the element,
192 : : * instantiate pads and add them to element,
193 : : * set pad calback functions, and
194 : : * initialize instance structure.
195 : : */
196 : : static void
197 : 4 : gst_chromium_init (GstChromium * filter, GstChromiumClass * gclass)
198 : : {
199 : 4 : filter->edge_a = DEFAULT_EDGE_A;
200 : 4 : filter->edge_b = DEFAULT_EDGE_B;
201 : 4 : filter->silent = FALSE;
202 : :
203 : 4 : setup_cos_table ();
204 : 4 : }
205 : :
206 : : static void
207 : 0 : gst_chromium_set_property (GObject * object, guint prop_id,
208 : : const GValue * value, GParamSpec * pspec)
209 : : {
210 : 0 : GstChromium *filter = GST_CHROMIUM (object);
211 : :
212 [ # # # # ]: 0 : switch (prop_id) {
213 : : case PROP_SILENT:
214 : 0 : filter->silent = g_value_get_boolean (value);
215 : 0 : break;
216 : : case PROP_EDGE_A:
217 : 0 : filter->edge_a = g_value_get_uint (value);
218 : 0 : break;
219 : : case PROP_EDGE_B:
220 : 0 : filter->edge_b = g_value_get_uint (value);
221 : 0 : break;
222 : : default:
223 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 : 0 : break;
225 : : }
226 : 0 : }
227 : :
228 : :
229 : : static void
230 : 3 : gst_chromium_get_property (GObject * object, guint prop_id,
231 : : GValue * value, GParamSpec * pspec)
232 : : {
233 : 3 : GstChromium *filter = GST_CHROMIUM (object);
234 : :
235 : 3 : GST_OBJECT_LOCK (filter);
236 [ + + + - ]: 3 : switch (prop_id) {
237 : : case PROP_SILENT:
238 : 1 : g_value_set_boolean (value, filter->silent);
239 : 1 : break;
240 : : case PROP_EDGE_A:
241 : 1 : g_value_set_uint (value, filter->edge_a);
242 : 1 : break;
243 : : case PROP_EDGE_B:
244 : 1 : g_value_set_uint (value, filter->edge_b);
245 : 1 : break;
246 : : default:
247 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 : 0 : break;
249 : : }
250 : 3 : GST_OBJECT_UNLOCK (filter);
251 : 3 : }
252 : :
253 : : /* GstElement vmethod implementations */
254 : :
255 : : /* Handle the link with other elements. */
256 : : static gboolean
257 : 0 : gst_chromium_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
258 : : GstCaps * outcaps)
259 : : {
260 : 0 : GstChromium *filter = GST_CHROMIUM (btrans);
261 : : GstStructure *structure;
262 : 0 : gboolean ret = FALSE;
263 : :
264 : 0 : structure = gst_caps_get_structure (incaps, 0);
265 : :
266 : 0 : GST_OBJECT_LOCK (filter);
267 [ # # # # ]: 0 : if (gst_structure_get_int (structure, "width", &filter->width) &&
268 : 0 : gst_structure_get_int (structure, "height", &filter->height)) {
269 : 0 : ret = TRUE;
270 : : }
271 : 0 : GST_OBJECT_UNLOCK (filter);
272 : :
273 : 0 : return ret;
274 : : }
275 : :
276 : : /* Actual processing. */
277 : : static GstFlowReturn
278 : 0 : gst_chromium_transform (GstBaseTransform * btrans,
279 : : GstBuffer * in_buf, GstBuffer * out_buf)
280 : : {
281 : 0 : GstChromium *filter = GST_CHROMIUM (btrans);
282 : : gint video_size, edge_a, edge_b;
283 : 0 : guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
284 : 0 : guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
285 : : GstClockTime timestamp;
286 : : gint64 stream_time;
287 : :
288 : : /* GstController: update the properties */
289 : 0 : timestamp = GST_BUFFER_TIMESTAMP (in_buf);
290 : 0 : stream_time =
291 : 0 : gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
292 : :
293 [ # # ][ # # ]: 0 : GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
[ # # ][ # # ]
[ # # ]
294 : : GST_TIME_ARGS (timestamp));
295 : :
296 [ # # ]: 0 : if (GST_CLOCK_TIME_IS_VALID (stream_time))
297 : 0 : gst_object_sync_values (G_OBJECT (filter), stream_time);
298 : :
299 : 0 : GST_OBJECT_LOCK (filter);
300 : 0 : edge_a = filter->edge_a;
301 : 0 : edge_b = filter->edge_b;
302 : 0 : GST_OBJECT_UNLOCK (filter);
303 : :
304 : 0 : video_size = filter->width * filter->height;
305 : 0 : transform (src, dest, video_size, edge_a, edge_b);
306 : :
307 : 0 : return GST_FLOW_OK;
308 : : }
309 : :
310 : : /* Entry point to initialize the plug-in.
311 : : * Register the element factories and other features. */
312 : : gboolean
313 : 12 : gst_chromium_plugin_init (GstPlugin * chromium)
314 : : {
315 : : /* debug category for fltering log messages */
316 [ + - ]: 12 : GST_DEBUG_CATEGORY_INIT (gst_chromium_debug, "chromium", 0,
317 : : "Template chromium");
318 : :
319 : 12 : return gst_element_register (chromium, "chromium", GST_RANK_NONE,
320 : : GST_TYPE_CHROMIUM);
321 : : }
322 : :
323 : : /*** Now the image processing work.... ***/
324 : : /* Set up the cosine table. */
325 : : void
326 : 4 : setup_cos_table (void)
327 : : {
328 : : int angle;
329 : :
330 [ + + ]: 4100 : for (angle = 0; angle < cosTableTwoPi; ++angle) {
331 : 4096 : float angleInRadians = ((float) (angle) / cosTablePi) * pi;
332 : 4096 : cosTable[angle] = (int) (cos (angleInRadians) * cosTableOne);
333 : : }
334 : 4 : }
335 : :
336 : : /* Keep the values absolute. */
337 : : static inline int
338 : 0 : abs_int (int val)
339 : : {
340 [ # # ]: 0 : if (val > 0) {
341 : 0 : return val;
342 : : } else {
343 : 0 : return -val;
344 : : }
345 : : }
346 : :
347 : : /* Keep the values inbounds. */
348 : : static gint
349 : 0 : gate_int (gint value, gint min, gint max)
350 : : {
351 [ # # ]: 0 : if (value < min) {
352 : 0 : return min;
353 [ # # ]: 0 : } else if (value > max) {
354 : 0 : return max;
355 : : } else {
356 : 0 : return value;
357 : : }
358 : : }
359 : :
360 : : /* Cosine from Table. */
361 : : static gint
362 : 0 : cos_from_table (int angle)
363 : : {
364 : 0 : angle &= cosTableMask;
365 : 0 : return cosTable[angle];
366 : : }
367 : :
368 : : /* Transform processes each frame. */
369 : : static void
370 : 0 : transform (guint32 * src, guint32 * dest, gint video_area,
371 : : gint edge_a, gint edge_b)
372 : : {
373 : : guint32 in, red, green, blue;
374 : : gint x;
375 : :
376 [ # # ]: 0 : for (x = 0; x < video_area; x++) {
377 : 0 : in = *src++;
378 : :
379 : 0 : red = (in >> 16) & 0xff;
380 : 0 : green = (in >> 8) & 0xff;
381 : 0 : blue = (in) & 0xff;
382 : :
383 : 0 : red = abs_int (cos_from_table ((red + edge_a) + ((red * edge_b) / 2)));
384 : 0 : green = abs_int (cos_from_table (
385 : 0 : (green + edge_a) + ((green * edge_b) / 2)));
386 : 0 : blue = abs_int (cos_from_table ((blue + edge_a) + ((blue * edge_b) / 2)));
387 : :
388 : 0 : red = gate_int (red, 0, 255);
389 : 0 : green = gate_int (green, 0, 255);
390 : 0 : blue = gate_int (blue, 0, 255);
391 : :
392 : 0 : *dest++ = (red << 16) | (green << 8) | blue;
393 : : }
394 : 0 : }
|