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