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 "gstgaussblur.h"
70
71 static void gst_gaussianblur_finalize (GObject * object);
72
73 static gboolean gst_gaussianblur_set_info (GstVideoFilter * filter,
74 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
75 GstVideoInfo * out_info);
76 static GstFlowReturn gst_gaussianblur_transform_frame (GstVideoFilter * vfilter,
77 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
78
79 static void gst_gaussianblur_set_property (GObject * object,
80 guint prop_id, const GValue * value, GParamSpec * pspec);
81 static void gst_gaussianblur_get_property (GObject * object,
82 guint prop_id, GValue * value, GParamSpec * pspec);
83
84 GST_DEBUG_CATEGORY_STATIC (gst_gauss_blur_debug);
85 #define GST_CAT_DEFAULT gst_gauss_blur_debug
86
87 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
88 #define CAPS_STR_RGB GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
89 #else
90 #define CAPS_STR_RGB GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
91 #endif
92
93 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("AYUV")
94
95 /* The capabilities of the inputs and outputs. */
96 static GstStaticPadTemplate gst_gaussianblur_sink_template =
97 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_PAD_SINK,
99 GST_PAD_ALWAYS,
100 GST_STATIC_CAPS (CAPS_STR)
101 );
102
103 static GstStaticPadTemplate gst_gaussianblur_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
105 GST_PAD_SRC,
106 GST_PAD_ALWAYS,
107 GST_STATIC_CAPS (CAPS_STR)
108 );
109
110 enum
111 {
112 PROP_0,
113 PROP_SIGMA
114 };
115
116 static gboolean make_gaussian_kernel (GstGaussianBlur * gb, float sigma);
117 static void gaussian_smooth (GstGaussianBlur * gb, guint8 * image,
118 guint8 * out_image);
119
120 #define gst_gaussianblur_parent_class parent_class
121 G_DEFINE_TYPE (GstGaussianBlur, gst_gaussianblur, GST_TYPE_VIDEO_FILTER);
122 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (gaussianblur, "gaussianblur",
123 GST_RANK_NONE, GST_TYPE_GAUSSIANBLUR,
124 GST_DEBUG_CATEGORY_INIT (gst_gauss_blur_debug, "gaussianblur", 0,
125 "Gaussian Blur video effect"));
126 #define DEFAULT_SIGMA 1.2
127
128 /* Initialize the gaussianblur's class. */
129 static void
gst_gaussianblur_class_init(GstGaussianBlurClass * klass)130 gst_gaussianblur_class_init (GstGaussianBlurClass * klass)
131 {
132 GObjectClass *gobject_class = (GObjectClass *) klass;
133 GstElementClass *gstelement_class = (GstElementClass *) klass;
134 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
135
136 gst_element_class_set_static_metadata (gstelement_class,
137 "GstGaussianBlur",
138 "Filter/Effect/Video",
139 "Perform Gaussian blur/sharpen on a video",
140 "Jan Schmidt <thaytan@noraisin.net>");
141
142 gst_element_class_add_static_pad_template (gstelement_class,
143 &gst_gaussianblur_sink_template);
144 gst_element_class_add_static_pad_template (gstelement_class,
145 &gst_gaussianblur_src_template);
146
147 gobject_class->set_property = gst_gaussianblur_set_property;
148 gobject_class->get_property = gst_gaussianblur_get_property;
149 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gaussianblur_finalize);
150
151 g_object_class_install_property (gobject_class, PROP_SIGMA,
152 g_param_spec_double ("sigma", "Sigma",
153 "Sigma value for gaussian blur (negative for sharpen)",
154 -20.0, 20.0, DEFAULT_SIGMA,
155 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
156
157 vfilter_class->transform_frame =
158 GST_DEBUG_FUNCPTR (gst_gaussianblur_transform_frame);
159 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_gaussianblur_set_info);
160 }
161
162 static gboolean
gst_gaussianblur_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)163 gst_gaussianblur_set_info (GstVideoFilter * filter, GstCaps * incaps,
164 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
165 {
166 GstGaussianBlur *gb = GST_GAUSSIANBLUR (filter);
167 guint32 n_elems;
168
169 gb->width = GST_VIDEO_INFO_WIDTH (in_info);
170 gb->height = GST_VIDEO_INFO_HEIGHT (in_info);
171
172 /* get stride */
173 gb->stride = GST_VIDEO_INFO_COMP_STRIDE (in_info, 0);
174 n_elems = gb->stride * gb->height;
175 gb->tempim = g_malloc (sizeof (gfloat) * n_elems);
176
177 return TRUE;
178 }
179
180 static void
gst_gaussianblur_init(GstGaussianBlur * gb)181 gst_gaussianblur_init (GstGaussianBlur * gb)
182 {
183 gb->sigma = (gfloat) DEFAULT_SIGMA;
184 gb->cur_sigma = -1.0;
185 }
186
187 static void
gst_gaussianblur_finalize(GObject * object)188 gst_gaussianblur_finalize (GObject * object)
189 {
190 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
191
192 g_free (gb->tempim);
193 gb->tempim = NULL;
194
195 g_free (gb->smoothedim);
196 gb->smoothedim = NULL;
197
198 g_free (gb->kernel);
199 gb->kernel = NULL;
200 g_free (gb->kernel_sum);
201 gb->kernel_sum = NULL;
202
203 G_OBJECT_CLASS (parent_class)->finalize (object);
204 }
205
206 static GstFlowReturn
gst_gaussianblur_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)207 gst_gaussianblur_transform_frame (GstVideoFilter * vfilter,
208 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
209 {
210 GstGaussianBlur *filter = GST_GAUSSIANBLUR (vfilter);
211 GstClockTime timestamp;
212 gint64 stream_time;
213 gfloat sigma;
214 guint8 *src, *dest;
215
216 /* GstController: update the properties */
217 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
218 stream_time =
219 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
220 GST_FORMAT_TIME, timestamp);
221
222 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
223 GST_TIME_ARGS (timestamp));
224
225 if (GST_CLOCK_TIME_IS_VALID (stream_time))
226 gst_object_sync_values (GST_OBJECT (filter), stream_time);
227
228 GST_OBJECT_LOCK (filter);
229 sigma = filter->sigma;
230 GST_OBJECT_UNLOCK (filter);
231
232 if (filter->cur_sigma != sigma) {
233 g_free (filter->kernel);
234 filter->kernel = NULL;
235 g_free (filter->kernel_sum);
236 filter->kernel_sum = NULL;
237 filter->cur_sigma = sigma;
238 }
239 if (filter->kernel == NULL &&
240 !make_gaussian_kernel (filter, filter->cur_sigma)) {
241 GST_ELEMENT_ERROR (filter, RESOURCE, NO_SPACE_LEFT, ("Out of memory"),
242 ("Failed to allocation gaussian kernel"));
243 return GST_FLOW_ERROR;
244 }
245
246 /*
247 * Perform gaussian smoothing on the image using the input standard
248 * deviation.
249 */
250 src = GST_VIDEO_FRAME_COMP_DATA (in_frame, 0);
251 dest = GST_VIDEO_FRAME_COMP_DATA (out_frame, 0);
252 gst_video_frame_copy (out_frame, in_frame);
253 if (filter->sigma != 0.0)
254 gaussian_smooth (filter, src, dest);
255
256 return GST_FLOW_OK;
257 }
258
259 static void
blur_row_x(GstGaussianBlur * gb,guint8 * in_row,gfloat * out_row)260 blur_row_x (GstGaussianBlur * gb, guint8 * in_row, gfloat * out_row)
261 {
262 int c, cc, center;
263 float dot[4], sum;
264 int k, kmin, kmax;
265
266 center = gb->windowsize / 2;
267
268 for (c = 0; c < gb->width; c++) {
269 /* Calculate min */
270 cc = center - c;
271 kmin = MAX (0, cc);
272 cc = kmin - cc;
273 /* Calc max */
274 kmax = MIN (gb->windowsize, gb->width - cc);
275 cc *= 4;
276
277 dot[0] = dot[1] = dot[2] = dot[3] = 0.0;
278 /* Calculate sum for range */
279 sum = gb->kernel_sum[kmax - 1];
280 sum -= kmin ? gb->kernel_sum[kmin - 1] : 0.0;
281
282 for (k = kmin; k < kmax; k++) {
283 float coeff = gb->kernel[k];
284 dot[0] += (float) in_row[cc++] * coeff;
285 dot[1] += (float) in_row[cc++] * coeff;
286 dot[2] += (float) in_row[cc++] * coeff;
287 dot[3] += (float) in_row[cc++] * coeff;
288 }
289
290 out_row[c * 4] = dot[0] / sum;
291 out_row[c * 4 + 1] = dot[1] / sum;
292 out_row[c * 4 + 2] = dot[2] / sum;
293 out_row[c * 4 + 3] = dot[3] / sum;
294 }
295 }
296
297 static void
gaussian_smooth(GstGaussianBlur * gb,guint8 * image,guint8 * out_image)298 gaussian_smooth (GstGaussianBlur * gb, guint8 * image, guint8 * out_image)
299 {
300 int r, c, rr, center;
301 float dot[4], sum;
302 int k, kmin, kmax;
303 guint8 *in_row = image;
304 float *tmp_out_row = gb->tempim;
305 float *tmp_in_pos;
306 gint y_avail = 0;
307 guint8 *out_row;
308
309 /* Apply the gaussian kernel */
310 center = gb->windowsize / 2;
311
312 /* Blur in the y - direction. */
313 for (r = 0; r < gb->height; r++) {
314 /* Calculate input row range */
315 rr = center - r;
316 kmin = MAX (0, rr);
317 rr = kmin - rr;
318 /* Calc max */
319 kmax = MIN (gb->windowsize, gb->height - rr);
320
321 /* Precalculate sum for range */
322 sum = gb->kernel_sum[kmax - 1];
323 sum -= kmin ? gb->kernel_sum[kmin - 1] : 0.0;
324
325 /* Blur more input rows (x direction blur) */
326 while (y_avail <= (r + center) && y_avail < gb->height) {
327 blur_row_x (gb, in_row, tmp_out_row);
328 in_row += gb->stride;
329 tmp_out_row += gb->stride;
330 y_avail++;
331 }
332
333 tmp_in_pos = gb->tempim + (rr * gb->stride);
334 out_row = out_image + r * gb->stride;
335
336 for (c = 0; c < gb->width; c++) {
337 float *tmp = tmp_in_pos;
338
339 dot[0] = dot[1] = dot[2] = dot[3] = 0.0;
340 for (k = kmin; k < kmax; k++, tmp += gb->stride) {
341 float kern = gb->kernel[k];
342 dot[0] += tmp[0] * kern;
343 dot[1] += tmp[1] * kern;
344 dot[2] += tmp[2] * kern;
345 dot[3] += tmp[3] * kern;
346 }
347
348 *out_row++ = (guint8) CLAMP ((dot[0] / sum + 0.5), 0, 255);
349 *out_row++ = (guint8) CLAMP ((dot[1] / sum + 0.5), 0, 255);
350 *out_row++ = (guint8) CLAMP ((dot[2] / sum + 0.5), 0, 255);
351 *out_row++ = (guint8) CLAMP ((dot[3] / sum + 0.5), 0, 255);
352
353 tmp_in_pos += 4;
354 }
355 }
356 }
357
358 /*
359 * Create a one dimensional gaussian kernel.
360 */
361 static gboolean
make_gaussian_kernel(GstGaussianBlur * gb,float sigma)362 make_gaussian_kernel (GstGaussianBlur * gb, float sigma)
363 {
364 int i, center, left, right;
365 float sum, sum2;
366 const float fe = -0.5 / (sigma * sigma);
367 const float dx = 1.0 / (sigma * sqrt (2 * G_PI));
368
369 center = ceil (2.5 * fabs (sigma));
370 gb->windowsize = (int) (1 + 2 * center);
371
372 gb->kernel = g_new (float, gb->windowsize);
373 gb->kernel_sum = g_new (float, gb->windowsize);
374 if (gb->kernel == NULL || gb->kernel_sum == NULL)
375 return FALSE;
376
377 if (gb->windowsize == 1) {
378 gb->kernel[0] = 1.0;
379 gb->kernel_sum[0] = 1.0;
380 return TRUE;
381 }
382
383 /* Center co-efficient */
384 sum = gb->kernel[center] = dx;
385
386 /* Other coefficients */
387 left = center - 1;
388 right = center + 1;
389 for (i = 1; i <= center; i++, left--, right++) {
390 float fx = dx * pow (G_E, fe * i * i);
391 gb->kernel[right] = gb->kernel[left] = fx;
392 sum += 2 * fx;
393 }
394
395 if (sigma < 0) {
396 sum = -sum;
397 gb->kernel[center] += 2.0 * sum;
398 }
399
400 for (i = 0; i < gb->windowsize; i++)
401 gb->kernel[i] /= sum;
402
403 sum2 = 0.0;
404 for (i = 0; i < gb->windowsize; i++) {
405 sum2 += gb->kernel[i];
406 gb->kernel_sum[i] = sum2;
407 }
408
409 #if 0
410 g_print ("Sigma %f: ", sigma);
411 for (i = 0; i < gb->windowsize; i++)
412 g_print ("%f ", gb->kernel[i]);
413 g_print ("\n");
414 g_print ("sums: ");
415 for (i = 0; i < gb->windowsize; i++)
416 g_print ("%f ", gb->kernel_sum[i]);
417 g_print ("\n");
418 g_print ("sum %f sum2 %f\n", sum, sum2);
419 #endif
420
421 return TRUE;
422 }
423
424 static void
gst_gaussianblur_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)425 gst_gaussianblur_set_property (GObject * object,
426 guint prop_id, const GValue * value, GParamSpec * pspec)
427 {
428 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
429 switch (prop_id) {
430 case PROP_SIGMA:
431 GST_OBJECT_LOCK (object);
432 gb->sigma = g_value_get_double (value);
433 GST_OBJECT_UNLOCK (object);
434 break;
435 default:
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
437 break;
438 }
439 }
440
441 static void
gst_gaussianblur_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)442 gst_gaussianblur_get_property (GObject * object,
443 guint prop_id, GValue * value, GParamSpec * pspec)
444 {
445 GstGaussianBlur *gb = GST_GAUSSIANBLUR (object);
446 switch (prop_id) {
447 case PROP_SIGMA:
448 GST_OBJECT_LOCK (gb);
449 g_value_set_double (value, gb->sigma);
450 GST_OBJECT_UNLOCK (gb);
451 break;
452 default:
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454 break;
455 }
456 }
457