1 /*
2 * GStreamer
3 * Copyright (C) <2010> Jan Schmidt <thaytan@noraisin.net>
4 * Copyright (C) <2012> Luis de Bethencourt <luis@debethencourt.com>
5 *
6 * Chromium - burning chrome video effect.
7 * Based on Pete Warden's FreeFrame plugin with the same name.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Alternatively, the contents of this file may be used under the
28 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
29 * which case the following provisions apply instead of the ones
30 * mentioned above:
31 *
32 * This library is free software; you can redistribute it and/or
33 * modify it under the terms of the GNU Library General Public
34 * License as published by the Free Software Foundation; either
35 * version 2 of the License, or (at your option) any later version.
36 *
37 * This library is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 * Library General Public License for more details.
41 *
42 * You should have received a copy of the GNU Library General Public
43 * License along with this library; if not, write to the
44 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
45 * Boston, MA 02110-1301, USA.
46 */
47
48 /**
49 * SECTION:element-gaussianblur
50 * @title: gaussianblur
51 *
52 * Gaussianblur blurs the video stream in realtime.
53 *
54 * ## Example launch line
55 * |[
56 * gst-launch-1.0 -v videotestsrc ! gaussianblur ! videoconvert ! autovideosink
57 * ]| This pipeline shows the effect of gaussianblur on a test stream
58 *
59 */
60
61 #ifdef HAVE_CONFIG_H
62 #include <config.h>
63 #include <string.h>
64 #endif
65
66 #include <math.h>
67 #include <gst/gst.h>
68
69 #include "gstplugin.h"
70 #include "gstgaussblur.h"
71
72 static void gst_gaussianblur_finalize (GObject * object);
73
74 static gboolean gst_gaussianblur_set_info (GstVideoFilter * filter,
75 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
76 GstVideoInfo * out_info);
77 static GstFlowReturn gst_gaussianblur_transform_frame (GstVideoFilter * vfilter,
78 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
79
80 static void gst_gaussianblur_set_property (GObject * object,
81 guint prop_id, const GValue * value, GParamSpec * pspec);
82 static void gst_gaussianblur_get_property (GObject * object,
83 guint prop_id, GValue * value, GParamSpec * pspec);
84
85 GST_DEBUG_CATEGORY_STATIC (gst_gauss_blur_debug);
86 #define GST_CAT_DEFAULT gst_gauss_blur_debug
87
88 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
89 #define CAPS_STR_RGB GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
90 #else
91 #define CAPS_STR_RGB GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
92 #endif
93
94 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("AYUV")
95
96 /* The capabilities of the inputs and outputs. */
97 static GstStaticPadTemplate gst_gaussianblur_sink_template =
98 GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_PAD_SINK,
100 GST_PAD_ALWAYS,
101 GST_STATIC_CAPS (CAPS_STR)
102 );
103
104 static GstStaticPadTemplate gst_gaussianblur_src_template =
105 GST_STATIC_PAD_TEMPLATE ("src",
106 GST_PAD_SRC,
107 GST_PAD_ALWAYS,
108 GST_STATIC_CAPS (CAPS_STR)
109 );
110
111 enum
112 {
113 PROP_0,
114 PROP_SIGMA
115 };
116
117 static gboolean make_gaussian_kernel (GstGaussianBlur * gb, float sigma);
118 static void gaussian_smooth (GstGaussianBlur * gb, guint8 * image,
119 guint8 * out_image);
120
121 #define gst_gaussianblur_parent_class parent_class
122 G_DEFINE_TYPE (GstGaussianBlur, gst_gaussianblur, GST_TYPE_VIDEO_FILTER);
123
124 #define DEFAULT_SIGMA 1.2
125
126 /* Initalize the gaussianblur's class. */
127 static void
gst_gaussianblur_class_init(GstGaussianBlurClass * klass)128 gst_gaussianblur_class_init (GstGaussianBlurClass * klass)
129 {
130 GObjectClass *gobject_class = (GObjectClass *) klass;
131 GstElementClass *gstelement_class = (GstElementClass *) klass;
132 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
133
134 gst_element_class_set_static_metadata (gstelement_class,
135 "GstGaussianBlur",
136 "Filter/Effect/Video",
137 "Perform Gaussian blur/sharpen on a video",
138 "Jan Schmidt <thaytan@noraisin.net>");
139
140 gst_element_class_add_static_pad_template (gstelement_class,
141 &gst_gaussianblur_sink_template);
142 gst_element_class_add_static_pad_template (gstelement_class,
143 &gst_gaussianblur_src_template);
144
145 gobject_class->set_property = gst_gaussianblur_set_property;
146 gobject_class->get_property = gst_gaussianblur_get_property;
147 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gaussianblur_finalize);
148
149 g_object_class_install_property (gobject_class, PROP_SIGMA,
150 g_param_spec_double ("sigma", "Sigma",
151 "Sigma value for gaussian blur (negative for sharpen)",
152 -20.0, 20.0, DEFAULT_SIGMA,
153 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
154
155 vfilter_class->transform_frame =
156 GST_DEBUG_FUNCPTR (gst_gaussianblur_transform_frame);
157 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_gaussianblur_set_info);
158 }
159
160 static gboolean
gst_gaussianblur_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)161 gst_gaussianblur_set_info (GstVideoFilter * filter, GstCaps * incaps,
162 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
163 {
164 GstGaussianBlur *gb = GST_GAUSSIANBLUR (filter);
165 guint32 n_elems;
166
167 gb->width = GST_VIDEO_INFO_WIDTH (in_info);
168 gb->height = GST_VIDEO_INFO_HEIGHT (in_info);
169
170 /* get stride */
171 gb->stride = GST_VIDEO_INFO_COMP_STRIDE (in_info, 0);
172 n_elems = gb->stride * gb->height;
173 gb->tempim = g_malloc (sizeof (gfloat) * n_elems);
174
175 return TRUE;
176 }
177
178 static void
gst_gaussianblur_init(GstGaussianBlur * gb)179 gst_gaussianblur_init (GstGaussianBlur * gb)
180 {
181 gb->sigma = (gfloat) DEFAULT_SIGMA;
182 gb->cur_sigma = -1.0;
183 }
184
185 static void
gst_gaussianblur_finalize(GObject * object)186 gst_gaussianblur_finalize (GObject * object)
187 {
188 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
189
190 g_free (gb->tempim);
191 gb->tempim = NULL;
192
193 g_free (gb->smoothedim);
194 gb->smoothedim = NULL;
195
196 g_free (gb->kernel);
197 gb->kernel = NULL;
198 g_free (gb->kernel_sum);
199 gb->kernel_sum = NULL;
200
201 G_OBJECT_CLASS (parent_class)->finalize (object);
202 }
203
204 static GstFlowReturn
gst_gaussianblur_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)205 gst_gaussianblur_transform_frame (GstVideoFilter * vfilter,
206 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
207 {
208 GstGaussianBlur *filter = GST_GAUSSIANBLUR (vfilter);
209 GstClockTime timestamp;
210 gint64 stream_time;
211 gfloat sigma;
212 guint8 *src, *dest;
213
214 /* GstController: update the properties */
215 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
216 stream_time =
217 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
218 GST_FORMAT_TIME, timestamp);
219
220 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
221 GST_TIME_ARGS (timestamp));
222
223 if (GST_CLOCK_TIME_IS_VALID (stream_time))
224 gst_object_sync_values (GST_OBJECT (filter), stream_time);
225
226 GST_OBJECT_LOCK (filter);
227 sigma = filter->sigma;
228 GST_OBJECT_UNLOCK (filter);
229
230 if (filter->cur_sigma != sigma) {
231 g_free (filter->kernel);
232 filter->kernel = NULL;
233 g_free (filter->kernel_sum);
234 filter->kernel_sum = NULL;
235 filter->cur_sigma = sigma;
236 }
237 if (filter->kernel == NULL &&
238 !make_gaussian_kernel (filter, filter->cur_sigma)) {
239 GST_ELEMENT_ERROR (filter, RESOURCE, NO_SPACE_LEFT, ("Out of memory"),
240 ("Failed to allocation gaussian kernel"));
241 return GST_FLOW_ERROR;
242 }
243
244 /*
245 * Perform gaussian smoothing on the image using the input standard
246 * deviation.
247 */
248 src = GST_VIDEO_FRAME_COMP_DATA (in_frame, 0);
249 dest = GST_VIDEO_FRAME_COMP_DATA (out_frame, 0);
250 gst_video_frame_copy (out_frame, in_frame);
251 if (filter->sigma != 0.0)
252 gaussian_smooth (filter, src, dest);
253
254 return GST_FLOW_OK;
255 }
256
257 static void
blur_row_x(GstGaussianBlur * gb,guint8 * in_row,gfloat * out_row)258 blur_row_x (GstGaussianBlur * gb, guint8 * in_row, gfloat * out_row)
259 {
260 int c, cc, center;
261 float dot[4], sum;
262 int k, kmin, kmax;
263
264 center = gb->windowsize / 2;
265
266 for (c = 0; c < gb->width; c++) {
267 /* Calculate min */
268 cc = center - c;
269 kmin = MAX (0, cc);
270 cc = kmin - cc;
271 /* Calc max */
272 kmax = MIN (gb->windowsize, gb->width - cc);
273 cc *= 4;
274
275 dot[0] = dot[1] = dot[2] = dot[3] = 0.0;
276 /* Calculate sum for range */
277 sum = gb->kernel_sum[kmax - 1];
278 sum -= kmin ? gb->kernel_sum[kmin - 1] : 0.0;
279
280 for (k = kmin; k < kmax; k++) {
281 float coeff = gb->kernel[k];
282 dot[0] += (float) in_row[cc++] * coeff;
283 dot[1] += (float) in_row[cc++] * coeff;
284 dot[2] += (float) in_row[cc++] * coeff;
285 dot[3] += (float) in_row[cc++] * coeff;
286 }
287
288 out_row[c * 4] = dot[0] / sum;
289 out_row[c * 4 + 1] = dot[1] / sum;
290 out_row[c * 4 + 2] = dot[2] / sum;
291 out_row[c * 4 + 3] = dot[3] / sum;
292 }
293 }
294
295 static void
gaussian_smooth(GstGaussianBlur * gb,guint8 * image,guint8 * out_image)296 gaussian_smooth (GstGaussianBlur * gb, guint8 * image, guint8 * out_image)
297 {
298 int r, c, rr, center;
299 float dot[4], sum;
300 int k, kmin, kmax;
301 guint8 *in_row = image;
302 float *tmp_out_row = gb->tempim;
303 float *tmp_in_pos;
304 gint y_avail = 0;
305 guint8 *out_row;
306
307 /* Apply the gaussian kernel */
308 center = gb->windowsize / 2;
309
310 /* Blur in the y - direction. */
311 for (r = 0; r < gb->height; r++) {
312 /* Calculate input row range */
313 rr = center - r;
314 kmin = MAX (0, rr);
315 rr = kmin - rr;
316 /* Calc max */
317 kmax = MIN (gb->windowsize, gb->height - rr);
318
319 /* Precalculate sum for range */
320 sum = gb->kernel_sum[kmax - 1];
321 sum -= kmin ? gb->kernel_sum[kmin - 1] : 0.0;
322
323 /* Blur more input rows (x direction blur) */
324 while (y_avail <= (r + center) && y_avail < gb->height) {
325 blur_row_x (gb, in_row, tmp_out_row);
326 in_row += gb->stride;
327 tmp_out_row += gb->stride;
328 y_avail++;
329 }
330
331 tmp_in_pos = gb->tempim + (rr * gb->stride);
332 out_row = out_image + r * gb->stride;
333
334 for (c = 0; c < gb->width; c++) {
335 float *tmp = tmp_in_pos;
336
337 dot[0] = dot[1] = dot[2] = dot[3] = 0.0;
338 for (k = kmin; k < kmax; k++, tmp += gb->stride) {
339 float kern = gb->kernel[k];
340 dot[0] += tmp[0] * kern;
341 dot[1] += tmp[1] * kern;
342 dot[2] += tmp[2] * kern;
343 dot[3] += tmp[3] * kern;
344 }
345
346 *out_row++ = (guint8) CLAMP ((dot[0] / sum + 0.5), 0, 255);
347 *out_row++ = (guint8) CLAMP ((dot[1] / sum + 0.5), 0, 255);
348 *out_row++ = (guint8) CLAMP ((dot[2] / sum + 0.5), 0, 255);
349 *out_row++ = (guint8) CLAMP ((dot[3] / sum + 0.5), 0, 255);
350
351 tmp_in_pos += 4;
352 }
353 }
354 }
355
356 /*
357 * Create a one dimensional gaussian kernel.
358 */
359 static gboolean
make_gaussian_kernel(GstGaussianBlur * gb,float sigma)360 make_gaussian_kernel (GstGaussianBlur * gb, float sigma)
361 {
362 int i, center, left, right;
363 float sum, sum2;
364 const float fe = -0.5 / (sigma * sigma);
365 const float dx = 1.0 / (sigma * sqrt (2 * G_PI));
366
367 center = ceil (2.5 * fabs (sigma));
368 gb->windowsize = (int) (1 + 2 * center);
369
370 gb->kernel = g_new (float, gb->windowsize);
371 gb->kernel_sum = g_new (float, gb->windowsize);
372 if (gb->kernel == NULL || gb->kernel_sum == NULL)
373 return FALSE;
374
375 if (gb->windowsize == 1) {
376 gb->kernel[0] = 1.0;
377 gb->kernel_sum[0] = 1.0;
378 return TRUE;
379 }
380
381 /* Center co-efficient */
382 sum = gb->kernel[center] = dx;
383
384 /* Other coefficients */
385 left = center - 1;
386 right = center + 1;
387 for (i = 1; i <= center; i++, left--, right++) {
388 float fx = dx * pow (G_E, fe * i * i);
389 gb->kernel[right] = gb->kernel[left] = fx;
390 sum += 2 * fx;
391 }
392
393 if (sigma < 0) {
394 sum = -sum;
395 gb->kernel[center] += 2.0 * sum;
396 }
397
398 for (i = 0; i < gb->windowsize; i++)
399 gb->kernel[i] /= sum;
400
401 sum2 = 0.0;
402 for (i = 0; i < gb->windowsize; i++) {
403 sum2 += gb->kernel[i];
404 gb->kernel_sum[i] = sum2;
405 }
406
407 #if 0
408 g_print ("Sigma %f: ", sigma);
409 for (i = 0; i < gb->windowsize; i++)
410 g_print ("%f ", gb->kernel[i]);
411 g_print ("\n");
412 g_print ("sums: ");
413 for (i = 0; i < gb->windowsize; i++)
414 g_print ("%f ", gb->kernel_sum[i]);
415 g_print ("\n");
416 g_print ("sum %f sum2 %f\n", sum, sum2);
417 #endif
418
419 return TRUE;
420 }
421
422 static void
gst_gaussianblur_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)423 gst_gaussianblur_set_property (GObject * object,
424 guint prop_id, const GValue * value, GParamSpec * pspec)
425 {
426 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
427 switch (prop_id) {
428 case PROP_SIGMA:
429 GST_OBJECT_LOCK (object);
430 gb->sigma = g_value_get_double (value);
431 GST_OBJECT_UNLOCK (object);
432 break;
433 default:
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 break;
436 }
437 }
438
439 static void
gst_gaussianblur_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)440 gst_gaussianblur_get_property (GObject * object,
441 guint prop_id, GValue * value, GParamSpec * pspec)
442 {
443 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
444 switch (prop_id) {
445 case PROP_SIGMA:
446 GST_OBJECT_LOCK (gb);
447 g_value_set_double (value, gb->sigma);
448 GST_OBJECT_UNLOCK (gb);
449 break;
450 default:
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452 break;
453 }
454 }
455
456 /* Register the element factories and other features. */
457 gboolean
gst_gauss_blur_plugin_init(GstPlugin * plugin)458 gst_gauss_blur_plugin_init (GstPlugin * plugin)
459 {
460 /* debug category for fltering log messages */
461 GST_DEBUG_CATEGORY_INIT (gst_gauss_blur_debug, "gaussianblur",
462 0, "Gaussian Blur video effect");
463
464 return gst_element_register (plugin, "gaussianblur", GST_RANK_NONE,
465 GST_TYPE_GAUSSIANBLUR);
466 }
467