1 /* GStreamer GdkPixbuf overlay
2 * Copyright (C) 2012-2014 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
21 * SECTION:element-gdkpixbufoverlay
23 * The gdkpixbufoverlay element overlays an image loaded from file onto
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.
31 * Changing the image at runtime is currently not supported.
33 * Negative offsets are also not yet supported.
36 * <title>Example launch line</title>
38 * gst-launch-1.0 -v videotestsrc ! gdkpixbufoverlay location=image.png ! autovideosink
40 * Overlays the image in image.png onto the test video picture produced by
50 #include "gstgdkpixbufoverlay.h"
52 #include <gst/video/gstvideometa.h>
54 GST_DEBUG_CATEGORY_STATIC (gdkpixbufoverlay_debug);
55 #define GST_CAT_DEFAULT gdkpixbufoverlay_debug
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);
63 static gboolean gst_gdk_pixbuf_overlay_start (GstBaseTransform * trans);
64 static gboolean gst_gdk_pixbuf_overlay_stop (GstBaseTransform * trans);
66 gst_gdk_pixbuf_overlay_transform_frame_ip (GstVideoFilter * filter,
67 GstVideoFrame * frame);
68 static void gst_gdk_pixbuf_overlay_before_transform (GstBaseTransform * trans,
70 static gboolean gst_gdk_pixbuf_overlay_set_info (GstVideoFilter * filter,
71 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
72 GstVideoInfo * out_info);
74 gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay,
76 static void gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay,
84 PROP_POSITIONING_MODE,
94 #define VIDEO_FORMATS "{ RGBx, RGB, BGR, BGRx, xRGB, xBGR, " \
95 "RGBA, BGRA, ARGB, ABGR, I420, YV12, AYUV, YUY2, UYVY, " \
96 "v308, v210, v216, Y41B, Y42B, Y444, YVYU, NV12, NV21, UYVP, " \
97 "RGB16, BGR16, RGB15, BGR15, UYVP, A420, YUV9, YVU9, " \
98 "IYU1, ARGB64, AYUV64, r210, I420_10LE, I420_10BE, " \
99 "GRAY8, GRAY16_BE, GRAY16_LE }"
101 /* FIXME 2.0: change to absolute positioning */
102 #define DEFAULT_POSITIONING_MODE \
103 GST_GDK_PIXBUF_POSITIONING_PIXELS_RELATIVE_TO_EDGES
105 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS))
111 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
114 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS))
117 G_DEFINE_TYPE (GstGdkPixbufOverlay, gst_gdk_pixbuf_overlay,
118 GST_TYPE_VIDEO_FILTER);
120 #define GST_TYPE_GDK_PIXBUF_POSITIONING_MODE \
121 (gst_gdk_pixbuf_positioning_mode_get_type())
124 gst_gdk_pixbuf_positioning_mode_get_type (void)
126 static const GEnumValue pos_modes[] = {
127 {GST_GDK_PIXBUF_POSITIONING_PIXELS_RELATIVE_TO_EDGES,
128 "pixels-relative-to-edges", "pixels-relative-to-edges"},
129 {GST_GDK_PIXBUF_POSITIONING_PIXELS_ABSOLUTE, "pixels-absolute",
134 static GType type; /* 0 */
137 type = g_enum_register_static ("GstGdkPixbufPositioningMode", pos_modes);
144 gst_gdk_pixbuf_overlay_class_init (GstGdkPixbufOverlayClass * klass)
146 GstVideoFilterClass *videofilter_class = GST_VIDEO_FILTER_CLASS (klass);
147 GstBaseTransformClass *basetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
148 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
149 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151 gobject_class->set_property = gst_gdk_pixbuf_overlay_set_property;
152 gobject_class->get_property = gst_gdk_pixbuf_overlay_get_property;
153 gobject_class->finalize = gst_gdk_pixbuf_overlay_finalize;
155 basetrans_class->start = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_start);
156 basetrans_class->stop = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_stop);
158 basetrans_class->before_transform =
159 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_before_transform);
161 videofilter_class->set_info =
162 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_set_info);
163 videofilter_class->transform_frame_ip =
164 GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_transform_frame_ip);
166 g_object_class_install_property (gobject_class, PROP_LOCATION,
167 g_param_spec_string ("location", "location",
168 "Location of image file to overlay", NULL, GST_PARAM_CONTROLLABLE
169 | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
170 | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (gobject_class, PROP_OFFSET_X,
172 g_param_spec_int ("offset-x", "X Offset",
173 "For positive value, horizontal offset of overlay image in pixels from"
174 " left of video image. For negative value, horizontal offset of overlay"
175 " image in pixels from right of video image", G_MININT, G_MAXINT, 0,
176 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
177 | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class, PROP_OFFSET_Y,
179 g_param_spec_int ("offset-y", "Y Offset",
180 "For positive value, vertical offset of overlay image in pixels from"
181 " top of video image. For negative value, vertical offset of overlay"
182 " image in pixels from bottom of video image", G_MININT, G_MAXINT, 0,
183 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
184 | G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_RELATIVE_X,
186 g_param_spec_double ("relative-x", "Relative X Offset",
187 "Horizontal offset of overlay image in fractions of video image "
188 "width, from top-left corner of video image", 0.0, 1.0, 0.0,
189 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
190 | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_RELATIVE_Y,
192 g_param_spec_double ("relative-y", "Relative Y Offset",
193 "Vertical offset of overlay image in fractions of video image "
194 "height, from top-left corner of video image", 0.0, 1.0, 0.0,
195 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
196 | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_OVERLAY_WIDTH,
198 g_param_spec_int ("overlay-width", "Overlay Width",
199 "Width of overlay image in pixels (0 = same as overlay image)", 0,
201 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
202 | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_OVERLAY_HEIGHT,
204 g_param_spec_int ("overlay-height", "Overlay Height",
205 "Height of overlay image in pixels (0 = same as overlay image)", 0,
207 GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE
208 | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class, PROP_ALPHA,
210 g_param_spec_double ("alpha", "Alpha", "Global alpha of overlay image",
211 0.0, 1.0, 1.0, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
212 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 * GstGdkPixbufOverlay:pixbuf:
216 * GdkPixbuf object to render.
220 g_object_class_install_property (gobject_class, PROP_PIXBUF,
221 g_param_spec_object ("pixbuf", "Pixbuf", "GdkPixbuf object to render",
222 GDK_TYPE_PIXBUF, GST_PARAM_CONTROLLABLE | GST_PARAM_MUTABLE_PLAYING
223 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 * GstGdkPixbufOverlay:positioning-mode:
227 * Positioning mode of offset-x and offset-y properties. Determines how
228 * negative x/y offsets will be interpreted. By default negative values
229 * are for positioning relative to the right/bottom edge of the video
230 * image, but you can use this property to select absolute positioning
231 * relative to a (0, 0) origin in the top-left corner. That way negative
232 * offsets will be to the left/above the video image, which allows you to
233 * smoothly slide logos into and out of the frame if desired.
237 g_object_class_install_property (gobject_class, PROP_POSITIONING_MODE,
238 g_param_spec_enum ("positioning-mode", "Positioning mode",
239 "Positioning mode of offset-x and offset-y properties",
240 GST_TYPE_GDK_PIXBUF_POSITIONING_MODE, DEFAULT_POSITIONING_MODE,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 gst_element_class_add_static_pad_template (element_class, &sink_template);
244 gst_element_class_add_static_pad_template (element_class, &src_template);
246 gst_element_class_set_static_metadata (element_class,
247 "GdkPixbuf Overlay", "Filter/Effect/Video",
248 "Overlay an image onto a video stream",
249 "Tim-Philipp Müller <tim centricular net>");
250 GST_DEBUG_CATEGORY_INIT (gdkpixbufoverlay_debug, "gdkpixbufoverlay", 0,
251 "debug category for gdkpixbufoverlay element");
255 gst_gdk_pixbuf_overlay_init (GstGdkPixbufOverlay * overlay)
257 overlay->offset_x = 0;
258 overlay->offset_y = 0;
260 overlay->relative_x = 0.0;
261 overlay->relative_y = 0.0;
263 overlay->positioning_mode = DEFAULT_POSITIONING_MODE;
265 overlay->overlay_width = 0;
266 overlay->overlay_height = 0;
268 overlay->alpha = 1.0;
270 overlay->pixbuf = NULL;
274 gst_gdk_pixbuf_overlay_set_property (GObject * object, guint property_id,
275 const GValue * value, GParamSpec * pspec)
277 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
279 GST_OBJECT_LOCK (overlay);
281 switch (property_id) {
284 g_free (overlay->location);
285 overlay->location = g_value_dup_string (value);
286 if (!gst_gdk_pixbuf_overlay_load_image (overlay, &err)) {
287 GST_ERROR_OBJECT (overlay, "Could not load overlay image: %s",
294 GdkPixbuf *pixbuf = g_value_get_object (value);
296 if (overlay->pixbuf != NULL)
297 g_object_unref (overlay->pixbuf);
298 overlay->pixbuf = g_object_ref (pixbuf);
299 gst_gdk_pixbuf_overlay_set_pixbuf (overlay, g_object_ref (pixbuf));
303 overlay->offset_x = g_value_get_int (value);
304 overlay->update_composition = TRUE;
307 overlay->offset_y = g_value_get_int (value);
308 overlay->update_composition = TRUE;
310 case PROP_RELATIVE_X:
311 overlay->relative_x = g_value_get_double (value);
312 overlay->update_composition = TRUE;
314 case PROP_RELATIVE_Y:
315 overlay->relative_y = g_value_get_double (value);
316 overlay->update_composition = TRUE;
318 case PROP_OVERLAY_WIDTH:
319 overlay->overlay_width = g_value_get_int (value);
320 overlay->update_composition = TRUE;
322 case PROP_OVERLAY_HEIGHT:
323 overlay->overlay_height = g_value_get_int (value);
324 overlay->update_composition = TRUE;
327 overlay->alpha = g_value_get_double (value);
328 overlay->update_composition = TRUE;
330 case PROP_POSITIONING_MODE:
331 overlay->positioning_mode = g_value_get_enum (value);
332 overlay->update_composition = TRUE;
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
339 GST_OBJECT_UNLOCK (overlay);
343 gst_gdk_pixbuf_overlay_get_property (GObject * object, guint property_id,
344 GValue * value, GParamSpec * pspec)
346 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
348 GST_OBJECT_LOCK (overlay);
350 switch (property_id) {
352 g_value_set_string (value, overlay->location);
355 g_value_set_object (value, overlay->pixbuf);
358 g_value_set_int (value, overlay->offset_x);
361 g_value_set_int (value, overlay->offset_y);
363 case PROP_RELATIVE_X:
364 g_value_set_double (value, overlay->relative_x);
366 case PROP_RELATIVE_Y:
367 g_value_set_double (value, overlay->relative_y);
369 case PROP_OVERLAY_WIDTH:
370 g_value_set_int (value, overlay->overlay_width);
372 case PROP_OVERLAY_HEIGHT:
373 g_value_set_int (value, overlay->overlay_height);
376 g_value_set_double (value, overlay->alpha);
378 case PROP_POSITIONING_MODE:
379 g_value_set_enum (value, overlay->positioning_mode);
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
386 GST_OBJECT_UNLOCK (overlay);
390 gst_gdk_pixbuf_overlay_finalize (GObject * object)
392 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
394 g_free (overlay->location);
395 overlay->location = NULL;
397 G_OBJECT_CLASS (gst_gdk_pixbuf_overlay_parent_class)->finalize (object);
401 gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay, GError ** err)
405 pixbuf = gdk_pixbuf_new_from_file (overlay->location, err);
410 gst_gdk_pixbuf_overlay_set_pixbuf (overlay, pixbuf);
414 /* Takes ownership of pixbuf; call with OBJECT_LOCK */
416 gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay,
419 GstVideoMeta *video_meta;
421 gint width, height, stride, w, h, plane;
423 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
424 GdkPixbuf *alpha_pixbuf;
426 /* FIXME: we could do this much more efficiently ourselves below, but
427 * we're lazy for now */
428 /* FIXME: perhaps expose substitute_color via properties */
429 alpha_pixbuf = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0);
430 g_object_unref (pixbuf);
431 pixbuf = alpha_pixbuf;
434 width = gdk_pixbuf_get_width (pixbuf);
435 height = gdk_pixbuf_get_height (pixbuf);
436 stride = gdk_pixbuf_get_rowstride (pixbuf);
437 pixels = gdk_pixbuf_get_pixels (pixbuf);
439 /* the memory layout in GdkPixbuf is R-G-B-A, we want:
440 * - B-G-R-A on little-endian platforms
441 * - A-R-G-B on big-endian platforms
443 for (h = 0; h < height; ++h) {
444 p = pixels + (h * stride);
445 for (w = 0; w < width; ++w) {
448 /* R-G-B-A ==> B-G-R-A */
453 if (G_BYTE_ORDER == G_BIG_ENDIAN) {
454 /* B-G-R-A ==> A-R-G-B */
455 /* we can probably assume sane alignment */
456 *((guint32 *) p) = GUINT32_SWAP_LE_BE (*((guint32 *) p));
464 gst_buffer_unref (overlay->pixels);
466 /* assume we have row padding even for the last row */
467 /* transfer ownership of pixbuf to the buffer */
468 overlay->pixels = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
469 pixels, height * stride, 0, height * stride, pixbuf,
470 (GDestroyNotify) g_object_unref);
472 video_meta = gst_buffer_add_video_meta (overlay->pixels,
473 GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
476 for (plane = 0; plane < video_meta->n_planes; ++plane)
477 video_meta->stride[plane] = stride;
479 overlay->update_composition = TRUE;
481 GST_INFO_OBJECT (overlay, "Updated pixbuf, %d x %d", width, height);
485 gst_gdk_pixbuf_overlay_start (GstBaseTransform * trans)
487 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
490 if (overlay->location != NULL) {
491 if (!gst_gdk_pixbuf_overlay_load_image (overlay, &err))
492 goto error_loading_image;
494 gst_base_transform_set_passthrough (trans, FALSE);
496 GST_WARNING_OBJECT (overlay, "no image location set, doing nothing");
497 gst_base_transform_set_passthrough (trans, TRUE);
505 GST_ELEMENT_ERROR (overlay, RESOURCE, OPEN_READ,
506 ("Could not load overlay image."), ("%s", err->message));
513 gst_gdk_pixbuf_overlay_stop (GstBaseTransform * trans)
515 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
518 gst_video_overlay_composition_unref (overlay->comp);
519 overlay->comp = NULL;
522 gst_buffer_replace (&overlay->pixels, NULL);
528 gst_gdk_pixbuf_overlay_set_info (GstVideoFilter * filter, GstCaps * incaps,
529 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
531 GST_INFO_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
536 gst_gdk_pixbuf_overlay_update_composition (GstGdkPixbufOverlay * overlay)
538 GstGdkPixbufPositioningMode positioning_mode;
539 GstVideoOverlayComposition *comp;
540 GstVideoOverlayRectangle *rect;
541 GstVideoMeta *overlay_meta;
542 gint x, y, width, height;
544 GST_VIDEO_INFO_WIDTH (&GST_VIDEO_FILTER (overlay)->in_info);
546 GST_VIDEO_INFO_HEIGHT (&GST_VIDEO_FILTER (overlay)->in_info);
549 gst_video_overlay_composition_unref (overlay->comp);
550 overlay->comp = NULL;
553 if (overlay->alpha == 0.0 || overlay->pixels == NULL)
556 overlay_meta = gst_buffer_get_video_meta (overlay->pixels);
558 positioning_mode = overlay->positioning_mode;
560 if (positioning_mode == GST_GDK_PIXBUF_POSITIONING_PIXELS_ABSOLUTE) {
561 x = overlay->offset_x + (overlay->relative_x * overlay_meta->width);
562 y = overlay->offset_y + (overlay->relative_y * overlay_meta->height);
564 x = overlay->offset_x < 0 ?
565 video_width + overlay->offset_x - overlay_meta->width +
566 (overlay->relative_x * overlay_meta->width) :
567 overlay->offset_x + (overlay->relative_x * overlay_meta->width);
568 y = overlay->offset_y < 0 ?
569 video_height + overlay->offset_y - overlay_meta->height +
570 (overlay->relative_y * overlay_meta->height) :
571 overlay->offset_y + (overlay->relative_y * overlay_meta->height);
574 width = overlay->overlay_width;
576 width = overlay_meta->width;
578 height = overlay->overlay_height;
580 height = overlay_meta->height;
582 GST_DEBUG_OBJECT (overlay, "overlay image dimensions: %d x %d, alpha=%.2f",
583 overlay_meta->width, overlay_meta->height, overlay->alpha);
584 GST_DEBUG_OBJECT (overlay, "properties: x,y: %d,%d (%g%%,%g%%) - WxH: %dx%d",
585 overlay->offset_x, overlay->offset_y,
586 overlay->relative_x * 100.0, overlay->relative_y * 100.0,
587 overlay->overlay_height, overlay->overlay_width);
588 GST_DEBUG_OBJECT (overlay, "overlay rendered: %d x %d @ %d,%d (onto %d x %d)",
589 width, height, x, y, video_width, video_height);
591 rect = gst_video_overlay_rectangle_new_raw (overlay->pixels,
592 x, y, width, height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
594 if (overlay->alpha != 1.0)
595 gst_video_overlay_rectangle_set_global_alpha (rect, overlay->alpha);
597 comp = gst_video_overlay_composition_new (rect);
598 gst_video_overlay_rectangle_unref (rect);
600 overlay->comp = comp;
604 gst_gdk_pixbuf_overlay_before_transform (GstBaseTransform * trans,
607 GstClockTime stream_time;
609 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
610 GST_BUFFER_TIMESTAMP (outbuf));
612 if (GST_CLOCK_TIME_IS_VALID (stream_time))
613 gst_object_sync_values (GST_OBJECT (trans), stream_time);
617 gst_gdk_pixbuf_overlay_transform_frame_ip (GstVideoFilter * filter,
618 GstVideoFrame * frame)
620 GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (filter);
622 GST_OBJECT_LOCK (overlay);
624 if (G_UNLIKELY (overlay->update_composition)) {
625 gst_gdk_pixbuf_overlay_update_composition (overlay);
626 overlay->update_composition = FALSE;
629 GST_OBJECT_UNLOCK (overlay);
631 if (overlay->comp != NULL)
632 gst_video_overlay_composition_blend (overlay->comp, frame);