1 /* GStreamer GdkPixbuf overlay
2 * Copyright (C) 2012-2014 Tim-Philipp Müller <tim centricular net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19
20 /**
21 * SECTION:element-gdkpixbufoverlay
22 * @title: gdkpixbufoverlay
23 *
24 * The gdkpixbufoverlay element overlays an image loaded from file onto
25 * a video stream.
26 *
27 * Changing the positioning or overlay width and height properties at runtime
28 * is supported, but it might be prudent to to protect the property setting
29 * code with GST_BASE_TRANSFORM_LOCK and GST_BASE_TRANSFORM_UNLOCK, as
30 * g_object_set() is not atomic for multiple properties passed in one go.
31 *
32 * Changing the image at runtime is currently not supported.
33 *
34 * Negative offsets are also not yet supported.
35 *
36 * ## Example launch line
37 * |[
38 * gst-launch-1.0 -v videotestsrc ! gdkpixbufoverlay location=image.png ! autovideosink
39 * ]|
40 * Overlays the image in image.png onto the test video picture produced by
41 * videotestsrc.
42 *
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <gst/gst.h>
50 #include "gstgdkpixbufoverlay.h"
51
52 #include "gstgdkpixbufelements.h"
53 #include <gst/video/gstvideometa.h>
54
55 GST_DEBUG_CATEGORY_STATIC (gdkpixbufoverlay_debug);
56 #define GST_CAT_DEFAULT gdkpixbufoverlay_debug
57
58 static void gst_gdk_pixbuf_overlay_set_property (GObject * object,
59 guint property_id, const GValue * value, GParamSpec * pspec);
60 static void gst_gdk_pixbuf_overlay_get_property (GObject * object,
61 guint property_id, GValue * value, GParamSpec * pspec);
62 static void gst_gdk_pixbuf_overlay_finalize (GObject * object);
63
64 static gboolean gst_gdk_pixbuf_overlay_start (GstBaseTransform * trans);
65 static gboolean gst_gdk_pixbuf_overlay_stop (GstBaseTransform * trans);
66 static GstFlowReturn
67 gst_gdk_pixbuf_overlay_transform_frame_ip (GstVideoFilter * filter,
68 GstVideoFrame * frame);
69 static void gst_gdk_pixbuf_overlay_before_transform (GstBaseTransform * trans,
70 GstBuffer * outbuf);
71 static gboolean gst_gdk_pixbuf_overlay_set_info (GstVideoFilter * filter,
72 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
73 GstVideoInfo * out_info);
74 static gboolean
75 gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay,
76 GError ** err);
77 static void gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay,
78 GdkPixbuf * pixbuf);
79
80 enum
81 {
82 PROP_0,
83 PROP_LOCATION,
84 PROP_PIXBUF,
85 PROP_POSITIONING_MODE,
86 PROP_OFFSET_X,
87 PROP_OFFSET_Y,
88 PROP_RELATIVE_X,
89 PROP_RELATIVE_Y,
90 PROP_COEF_X,
91 PROP_COEF_Y,
92 PROP_OVERLAY_WIDTH,
93 PROP_OVERLAY_HEIGHT,
94 PROP_ALPHA
95 };
96
97 /* FIXME 2.0: change to absolute positioning */
98 #define DEFAULT_POSITIONING_MODE \
99 GST_GDK_PIXBUF_POSITIONING_PIXELS_RELATIVE_TO_EDGES
100
101 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
102 GST_PAD_SINK,
103 GST_PAD_ALWAYS,
104 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
105 (GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS))
106 );
107
108 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_PAD_SRC,
110 GST_PAD_ALWAYS,
111 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
112 (GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS))
113 );
114
115 G_DEFINE_TYPE (GstGdkPixbufOverlay, gst_gdk_pixbuf_overlay,
116 GST_TYPE_VIDEO_FILTER);
117 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (gdkpixbufoverlay, "gdkpixbufoverlay",
118 GST_RANK_NONE, GST_TYPE_GDK_PIXBUF_OVERLAY,
119 gdk_pixbuf_element_init (plugin));
120
121 #define GST_TYPE_GDK_PIXBUF_POSITIONING_MODE \
122 (gst_gdk_pixbuf_positioning_mode_get_type())
123
124 static GType
gst_gdk_pixbuf_positioning_mode_get_type(void)125 gst_gdk_pixbuf_positioning_mode_get_type (void)
126 {
127 static const GEnumValue pos_modes[] = {
128 {GST_GDK_PIXBUF_POSITIONING_PIXELS_RELATIVE_TO_EDGES,
129 "pixels-relative-to-edges", "pixels-relative-to-edges"},
130 {GST_GDK_PIXBUF_POSITIONING_PIXELS_ABSOLUTE, "pixels-absolute",
131 "pixels-absolute"},
132 {0, NULL, NULL},
133 };
134
135 static GType type; /* 0 */
136
137 if (!type) {
138 type = g_enum_register_static ("GstGdkPixbufPositioningMode", pos_modes);
139 }
140
141 return type;
142 }
143
144 static void
gst_gdk_pixbuf_overlay_class_init(GstGdkPixbufOverlayClass * klass)145 gst_gdk_pixbuf_overlay_class_init (GstGdkPixbufOverlayClass * klass)
146 {
147 GstVideoFilterClass *videofilter_class = GST_VIDEO_FILTER_CLASS (klass);
148 GstBaseTransformClass *basetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
149 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
150 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151
152 gobject_class->set_property = gst_gdk_pixbuf_overlay_set_property;
153 gobject_class->get_property = gst_gdk_pixbuf_overlay_get_property;
154 gobject_class->finalize = gst_gdk_pixbuf_overlay_finalize;
155
156 basetrans_class->start = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_start);
157 basetrans_class->stop = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_stop);
158
159 basetrans_class->before_transform =
160 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_before_transform);
161
162 videofilter_class->set_info =
163 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_set_info);
164 videofilter_class->transform_frame_ip =
165 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_transform_frame_ip);
166
167 g_object_class_install_property (gobject_class, PROP_LOCATION,
168 g_param_spec_string ("location", "location",
169 "Location of image file to overlay", NULL, GST_PARAM_CONTROLLABLE
170 | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
171 | G_PARAM_STATIC_STRINGS));
172 g_object_class_install_property (gobject_class, PROP_OFFSET_X,
173 g_param_spec_int ("offset-x", "X Offset",
174 "For positive value, horizontal offset of overlay image in pixels from"
175 " left of video image. For negative value, horizontal offset of overlay"
176 " image in pixels from right of video image", G_MININT, G_MAXINT, 0,
177 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
178 | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_OFFSET_Y,
180 g_param_spec_int ("offset-y", "Y Offset",
181 "For positive value, vertical offset of overlay image in pixels from"
182 " top of video image. For negative value, vertical offset of overlay"
183 " image in pixels from bottom of video image", G_MININT, G_MAXINT, 0,
184 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
185 | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class, PROP_RELATIVE_X,
187 g_param_spec_double ("relative-x", "Relative X Offset",
188 "Horizontal offset of overlay image in fractions of video image "
189 "width, from top-left corner of video image"
190 " (in relative positioning)", -1.0, 1.0, 0.0,
191 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
192 | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_RELATIVE_Y,
194 g_param_spec_double ("relative-y", "Relative Y Offset",
195 "Vertical offset of overlay image in fractions of video image "
196 "height, from top-left corner of video image"
197 " (in relative positioning)", -1.0, 1.0, 0.0,
198 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
199 | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
201 g_param_spec_int ("overlay-width", "Overlay Width",
202 "Width of overlay image in pixels (0 = same as overlay image)", 0,
203 G_MAXINT, 0,
204 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
205 | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
207 g_param_spec_int ("overlay-height", "Overlay Height",
208 "Height of overlay image in pixels (0 = same as overlay image)", 0,
209 G_MAXINT, 0,
210 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
211 | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_ALPHA,
213 g_param_spec_double ("alpha", "Alpha", "Global alpha of overlay image",
214 0.0, 1.0, 1.0, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
215 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 /**
217 * GstGdkPixbufOverlay:pixbuf:
218 *
219 * GdkPixbuf object to render.
220 *
221 * Since: 1.6
222 */
223 g_object_class_install_property (gobject_class, PROP_PIXBUF,
224 g_param_spec_object ("pixbuf", "Pixbuf", "GdkPixbuf object to render",
225 GDK_TYPE_PIXBUF, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
226 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 /**
228 * GstGdkPixbufOverlay:positioning-mode:
229 *
230 * Positioning mode of offset-x and offset-y properties. Determines how
231 * negative x/y offsets will be interpreted. By default negative values
232 * are for positioning relative to the right/bottom edge of the video
233 * image, but you can use this property to select absolute positioning
234 * relative to a (0, 0) origin in the top-left corner. That way negative
235 * offsets will be to the left/above the video image, which allows you to
236 * smoothly slide logos into and out of the frame if desired.
237 *
238 * Since: 1.6
239 */
240 g_object_class_install_property (gobject_class, PROP_POSITIONING_MODE,
241 g_param_spec_enum ("positioning-mode", "Positioning mode",
242 "Positioning mode of offset-x and offset-y properties",
243 GST_TYPE_GDK_PIXBUF_POSITIONING_MODE, DEFAULT_POSITIONING_MODE,
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245
246 /* FIXME the following actually act as a RELATIVE_X/RELATIVE_Y,
247 * but those were already slightly mutated/abused with ABSOLUTE positioning,
248 * so let's keep that and follow suit
249 * Suffice it to say all that could do with cleanup (2.0 ??) */
250 /**
251 * GstGdkPixbufOverlay:coef-x:
252 *
253 * In absolute positioning mode, the x coordinate of overlay image's
254 * top-left corner is now given by
255 * offset-x + (relative-x * overlay_width) + (coef-x * video_width).
256 * This allows to align the image absolutely and relatively
257 * to any edge or center position.
258 *
259 * Since: 1.12
260 */
261 g_object_class_install_property (gobject_class, PROP_COEF_X,
262 g_param_spec_double ("coef-x", "Relative X Offset",
263 "Horizontal offset of overlay image in fractions of video image "
264 "width, from top-left corner of video image (absolute positioning)",
265 -1.0, 1.0, 0.0, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
266 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 /**
268 * GstGdkPixbufOverlay:coef-y:
269 *
270 * In absolute positioning mode, the y coordinate of overlay image's
271 * top-left corner is now given by
272 * offset-y + (relative-y * overlay_height) + (coef-y * video_height).
273 * This allows to align the image absolutely and relatively
274 * to any edge or center position.
275 *
276 * Since: 1.12
277 */
278 g_object_class_install_property (gobject_class, PROP_COEF_Y,
279 g_param_spec_double ("coef-y", "Relative Y Offset",
280 "Vertical offset of overlay image in fractions of video image "
281 "height, from top-left corner of video image (absolute positioning)",
282 -1.0, 1.0, 0.0, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
283 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284
285 gst_element_class_add_static_pad_template (element_class, &sink_template);
286 gst_element_class_add_static_pad_template (element_class, &src_template);
287
288 gst_element_class_set_static_metadata (element_class,
289 "GdkPixbuf Overlay", "Filter/Effect/Video",
290 "Overlay an image onto a video stream",
291 "Tim-Philipp Müller <tim centricular net>");
292 GST_DEBUG_CATEGORY_INIT (gdkpixbufoverlay_debug, "gdkpixbufoverlay", 0,
293 "debug category for gdkpixbufoverlay element");
294
295 gst_type_mark_as_plugin_api (GST_TYPE_GDK_PIXBUF_POSITIONING_MODE, 0);
296 }
297
298 static void
gst_gdk_pixbuf_overlay_init(GstGdkPixbufOverlay * overlay)299 gst_gdk_pixbuf_overlay_init (GstGdkPixbufOverlay * overlay)
300 {
301 overlay->offset_x = 0;
302 overlay->offset_y = 0;
303
304 overlay->relative_x = 0.0;
305 overlay->relative_y = 0.0;
306
307 overlay->coef_x = 0.0;
308 overlay->coef_y = 0.0;
309
310 overlay->positioning_mode = DEFAULT_POSITIONING_MODE;
311
312 overlay->overlay_width = 0;
313 overlay->overlay_height = 0;
314
315 overlay->alpha = 1.0;
316
317 overlay->pixbuf = NULL;
318 }
319
320 void
gst_gdk_pixbuf_overlay_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)321 gst_gdk_pixbuf_overlay_set_property (GObject * object, guint property_id,
322 const GValue * value, GParamSpec * pspec)
323 {
324 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
325
326 GST_OBJECT_LOCK (overlay);
327
328 switch (property_id) {
329 case PROP_LOCATION:{
330 GError *err = NULL;
331 g_free (overlay->location);
332 overlay->location = g_value_dup_string (value);
333 if (!gst_gdk_pixbuf_overlay_load_image (overlay, &err)) {
334 GST_ERROR_OBJECT (overlay, "Could not load overlay image: %s",
335 err->message);
336 g_error_free (err);
337 }
338 break;
339 }
340 case PROP_PIXBUF:{
341 GdkPixbuf *pixbuf = g_value_get_object (value);
342
343 if (overlay->pixbuf != NULL)
344 g_object_unref (overlay->pixbuf);
345 if (pixbuf) {
346 overlay->pixbuf = g_object_ref (pixbuf);
347 gst_gdk_pixbuf_overlay_set_pixbuf (overlay, g_object_ref (pixbuf));
348 } else {
349 overlay->pixbuf = NULL;
350 gst_buffer_replace (&overlay->pixels, NULL);
351 }
352 break;
353 }
354 case PROP_OFFSET_X:
355 overlay->offset_x = g_value_get_int (value);
356 overlay->update_composition = TRUE;
357 break;
358 case PROP_OFFSET_Y:
359 overlay->offset_y = g_value_get_int (value);
360 overlay->update_composition = TRUE;
361 break;
362 case PROP_RELATIVE_X:
363 overlay->relative_x = g_value_get_double (value);
364 overlay->update_composition = TRUE;
365 break;
366 case PROP_RELATIVE_Y:
367 overlay->relative_y = g_value_get_double (value);
368 overlay->update_composition = TRUE;
369 break;
370 case PROP_COEF_X:
371 overlay->coef_x = g_value_get_double (value);
372 overlay->update_composition = TRUE;
373 break;
374 case PROP_COEF_Y:
375 overlay->coef_y = g_value_get_double (value);
376 overlay->update_composition = TRUE;
377 break;
378 case PROP_OVERLAY_WIDTH:
379 overlay->overlay_width = g_value_get_int (value);
380 overlay->update_composition = TRUE;
381 break;
382 case PROP_OVERLAY_HEIGHT:
383 overlay->overlay_height = g_value_get_int (value);
384 overlay->update_composition = TRUE;
385 break;
386 case PROP_ALPHA:
387 overlay->alpha = g_value_get_double (value);
388 overlay->update_composition = TRUE;
389 break;
390 case PROP_POSITIONING_MODE:
391 overlay->positioning_mode = g_value_get_enum (value);
392 overlay->update_composition = TRUE;
393 break;
394 default:
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
396 break;
397 }
398
399 GST_OBJECT_UNLOCK (overlay);
400 }
401
402 void
gst_gdk_pixbuf_overlay_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)403 gst_gdk_pixbuf_overlay_get_property (GObject * object, guint property_id,
404 GValue * value, GParamSpec * pspec)
405 {
406 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
407
408 GST_OBJECT_LOCK (overlay);
409
410 switch (property_id) {
411 case PROP_LOCATION:
412 g_value_set_string (value, overlay->location);
413 break;
414 case PROP_PIXBUF:
415 g_value_set_object (value, overlay->pixbuf);
416 break;
417 case PROP_OFFSET_X:
418 g_value_set_int (value, overlay->offset_x);
419 break;
420 case PROP_OFFSET_Y:
421 g_value_set_int (value, overlay->offset_y);
422 break;
423 case PROP_RELATIVE_X:
424 g_value_set_double (value, overlay->relative_x);
425 break;
426 case PROP_RELATIVE_Y:
427 g_value_set_double (value, overlay->relative_y);
428 break;
429 case PROP_COEF_X:
430 g_value_set_double (value, overlay->coef_x);
431 break;
432 case PROP_COEF_Y:
433 g_value_set_double (value, overlay->coef_y);
434 break;
435 case PROP_OVERLAY_WIDTH:
436 g_value_set_int (value, overlay->overlay_width);
437 break;
438 case PROP_OVERLAY_HEIGHT:
439 g_value_set_int (value, overlay->overlay_height);
440 break;
441 case PROP_ALPHA:
442 g_value_set_double (value, overlay->alpha);
443 break;
444 case PROP_POSITIONING_MODE:
445 g_value_set_enum (value, overlay->positioning_mode);
446 break;
447 default:
448 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
449 break;
450 }
451
452 GST_OBJECT_UNLOCK (overlay);
453 }
454
455 void
gst_gdk_pixbuf_overlay_finalize(GObject * object)456 gst_gdk_pixbuf_overlay_finalize (GObject * object)
457 {
458 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
459
460 g_free (overlay->location);
461 overlay->location = NULL;
462
463 G_OBJECT_CLASS (gst_gdk_pixbuf_overlay_parent_class)->finalize (object);
464 }
465
466 static gboolean
gst_gdk_pixbuf_overlay_load_image(GstGdkPixbufOverlay * overlay,GError ** err)467 gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay, GError ** err)
468 {
469 GdkPixbuf *pixbuf;
470
471 pixbuf = gdk_pixbuf_new_from_file (overlay->location, err);
472
473 if (pixbuf == NULL)
474 return FALSE;
475
476 gst_gdk_pixbuf_overlay_set_pixbuf (overlay, pixbuf);
477 return TRUE;
478 }
479
480 /* Takes ownership of pixbuf; call with OBJECT_LOCK */
481 static void
gst_gdk_pixbuf_overlay_set_pixbuf(GstGdkPixbufOverlay * overlay,GdkPixbuf * pixbuf)482 gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay,
483 GdkPixbuf * pixbuf)
484 {
485 GstVideoMeta *video_meta;
486 guint8 *pixels, *p;
487 gint width, height, stride, w, h, plane;
488
489 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
490 GdkPixbuf *alpha_pixbuf;
491
492 /* FIXME: we could do this much more efficiently ourselves below, but
493 * we're lazy for now */
494 /* FIXME: perhaps expose substitute_color via properties */
495 alpha_pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0);
496 g_object_unref (pixbuf);
497 pixbuf = alpha_pixbuf;
498 }
499
500 width = gdk_pixbuf_get_width (pixbuf);
501 height = gdk_pixbuf_get_height (pixbuf);
502 stride = gdk_pixbuf_get_rowstride (pixbuf);
503 pixels = gdk_pixbuf_get_pixels (pixbuf);
504
505 /* the memory layout in GdkPixbuf is R-G-B-A, we want:
506 * - B-G-R-A on little-endian platforms
507 * - A-R-G-B on big-endian platforms
508 */
509 for (h = 0; h < height; ++h) {
510 p = pixels + (h * stride);
511 for (w = 0; w < width; ++w) {
512 guint8 tmp;
513
514 /* R-G-B-A ==> B-G-R-A */
515 tmp = p[0];
516 p[0] = p[2];
517 p[2] = tmp;
518
519 if (G_BYTE_ORDER == G_BIG_ENDIAN) {
520 /* B-G-R-A ==> A-R-G-B */
521 /* we can probably assume sane alignment */
522 *((guint32 *) p) = GUINT32_SWAP_LE_BE (*((guint32 *) p));
523 }
524
525 p += 4;
526 }
527 }
528
529 if (overlay->pixels)
530 gst_buffer_unref (overlay->pixels);
531
532 /* assume we have row padding even for the last row */
533 /* transfer ownership of pixbuf to the buffer */
534 overlay->pixels = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
535 pixels, height * stride, 0, height * stride, pixbuf,
536 (GDestroyNotify) g_object_unref);
537
538 video_meta = gst_buffer_add_video_meta (overlay->pixels,
539 GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
540 width, height);
541
542 for (plane = 0; plane < video_meta->n_planes; ++plane)
543 video_meta->stride[plane] = stride;
544
545 overlay->update_composition = TRUE;
546
547 GST_INFO_OBJECT (overlay, "Updated pixbuf, %d x %d", width, height);
548 }
549
550 static gboolean
gst_gdk_pixbuf_overlay_start(GstBaseTransform * trans)551 gst_gdk_pixbuf_overlay_start (GstBaseTransform * trans)
552 {
553 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
554 GError *err = NULL;
555
556 if (overlay->location != NULL) {
557 if (!gst_gdk_pixbuf_overlay_load_image (overlay, &err))
558 goto error_loading_image;
559
560 gst_base_transform_set_passthrough (trans, FALSE);
561 } else {
562 GST_WARNING_OBJECT (overlay, "no image location set, doing nothing");
563 gst_base_transform_set_passthrough (trans, TRUE);
564 }
565
566 return TRUE;
567
568 /* ERRORS */
569 error_loading_image:
570 {
571 GST_ELEMENT_ERROR (overlay, RESOURCE, OPEN_READ,
572 ("Could not load overlay image."), ("%s", err->message));
573 g_error_free (err);
574 return FALSE;
575 }
576 }
577
578 static gboolean
gst_gdk_pixbuf_overlay_stop(GstBaseTransform * trans)579 gst_gdk_pixbuf_overlay_stop (GstBaseTransform * trans)
580 {
581 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
582
583 if (overlay->comp) {
584 gst_video_overlay_composition_unref (overlay->comp);
585 overlay->comp = NULL;
586 }
587
588 gst_buffer_replace (&overlay->pixels, NULL);
589
590 return TRUE;
591 }
592
593 static gboolean
gst_gdk_pixbuf_overlay_set_info(GstVideoFilter * filter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)594 gst_gdk_pixbuf_overlay_set_info (GstVideoFilter * filter, GstCaps * incaps,
595 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
596 {
597 GST_INFO_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
598 return TRUE;
599 }
600
601 static void
gst_gdk_pixbuf_overlay_update_composition(GstGdkPixbufOverlay * overlay)602 gst_gdk_pixbuf_overlay_update_composition (GstGdkPixbufOverlay * overlay)
603 {
604 GstGdkPixbufPositioningMode positioning_mode;
605 GstVideoOverlayComposition *comp;
606 GstVideoOverlayRectangle *rect;
607 GstVideoMeta *overlay_meta;
608 gint x, y, width, height;
609 gint video_width =
610 GST_VIDEO_INFO_WIDTH (&GST_VIDEO_FILTER (overlay)->in_info);
611 gint video_height =
612 GST_VIDEO_INFO_HEIGHT (&GST_VIDEO_FILTER (overlay)->in_info);
613
614 if (overlay->comp) {
615 gst_video_overlay_composition_unref (overlay->comp);
616 overlay->comp = NULL;
617 }
618
619 if (overlay->alpha == 0.0 || overlay->pixels == NULL)
620 return;
621
622 overlay_meta = gst_buffer_get_video_meta (overlay->pixels);
623
624 positioning_mode = overlay->positioning_mode;
625 GST_DEBUG_OBJECT (overlay, "overlay positioning mode %d", positioning_mode);
626
627 width = overlay->overlay_width;
628 if (width == 0)
629 width = overlay_meta->width;
630
631 height = overlay->overlay_height;
632 if (height == 0)
633 height = overlay_meta->height;
634
635 if (positioning_mode == GST_GDK_PIXBUF_POSITIONING_PIXELS_ABSOLUTE) {
636 x = overlay->offset_x + (overlay->relative_x * width) +
637 (overlay->coef_x * video_width);
638 y = overlay->offset_y + (overlay->relative_y * height) +
639 (overlay->coef_y * video_height);
640 } else {
641 x = overlay->offset_x < 0 ?
642 video_width + overlay->offset_x - width +
643 (overlay->relative_x * video_width) :
644 overlay->offset_x + (overlay->relative_x * video_width);
645 y = overlay->offset_y < 0 ?
646 video_height + overlay->offset_y - height +
647 (overlay->relative_y * video_height) :
648 overlay->offset_y + (overlay->relative_y * video_height);
649 }
650
651 GST_DEBUG_OBJECT (overlay, "overlay image dimensions: %d x %d, alpha=%.2f",
652 overlay_meta->width, overlay_meta->height, overlay->alpha);
653 GST_DEBUG_OBJECT (overlay, "properties: x,y: %d,%d "
654 "(%g%%,%g%%) coef (%g%%,%g%%) - WxH: %dx%d",
655 overlay->offset_x, overlay->offset_y,
656 overlay->relative_x * 100.0, overlay->relative_y * 100.0,
657 overlay->coef_x * 100.0, overlay->coef_y * 100.0,
658 overlay->overlay_height, overlay->overlay_width);
659 GST_DEBUG_OBJECT (overlay, "overlay rendered: %d x %d @ %d,%d (onto %d x %d)",
660 width, height, x, y, video_width, video_height);
661
662 rect = gst_video_overlay_rectangle_new_raw (overlay->pixels,
663 x, y, width, height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
664
665 if (overlay->alpha != 1.0)
666 gst_video_overlay_rectangle_set_global_alpha (rect, overlay->alpha);
667
668 comp = gst_video_overlay_composition_new (rect);
669 gst_video_overlay_rectangle_unref (rect);
670
671 overlay->comp = comp;
672 }
673
674 static void
gst_gdk_pixbuf_overlay_before_transform(GstBaseTransform * trans,GstBuffer * outbuf)675 gst_gdk_pixbuf_overlay_before_transform (GstBaseTransform * trans,
676 GstBuffer * outbuf)
677 {
678 GstClockTime stream_time;
679 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
680 gboolean set_passthrough = FALSE;
681
682 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
683 GST_BUFFER_TIMESTAMP (outbuf));
684
685 if (GST_CLOCK_TIME_IS_VALID (stream_time))
686 gst_object_sync_values (GST_OBJECT (trans), stream_time);
687
688 /* now properties have been sync'ed; maybe need to update composition */
689 GST_OBJECT_LOCK (overlay);
690 if (G_UNLIKELY (overlay->update_composition)) {
691 gst_gdk_pixbuf_overlay_update_composition (overlay);
692 overlay->update_composition = FALSE;
693 set_passthrough = TRUE;
694 }
695 GST_OBJECT_UNLOCK (overlay);
696
697 /* determine passthrough mode so the buffer is writable if needed
698 * when passed into _transform_ip */
699 if (G_UNLIKELY (set_passthrough))
700 gst_base_transform_set_passthrough (trans, overlay->comp == NULL);
701 }
702
703 static GstFlowReturn
gst_gdk_pixbuf_overlay_transform_frame_ip(GstVideoFilter * filter,GstVideoFrame * frame)704 gst_gdk_pixbuf_overlay_transform_frame_ip (GstVideoFilter * filter,
705 GstVideoFrame * frame)
706 {
707 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (filter);
708
709 if (overlay->comp != NULL)
710 gst_video_overlay_composition_blend (overlay->comp, frame);
711
712 return GST_FLOW_OK;
713 }
714