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