souphttpsrc: Fix keep-alive handling
[platform/upstream/gst-plugins-good.git] / 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  *
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_OVERLAY_WIDTH,
90   PROP_OVERLAY_HEIGHT,
91   PROP_ALPHA
92 };
93
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 }"
100
101 /* FIXME 2.0: change to absolute positioning */
102 #define DEFAULT_POSITIONING_MODE \
103     GST_GDK_PIXBUF_POSITIONING_PIXELS_RELATIVE_TO_EDGES
104
105 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS))
109     );
110
111 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
112     GST_PAD_SRC,
113     GST_PAD_ALWAYS,
114     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS))
115     );
116
117 G_DEFINE_TYPE (GstGdkPixbufOverlay, gst_gdk_pixbuf_overlay,
118     GST_TYPE_VIDEO_FILTER);
119
120 #define GST_TYPE_GDK_PIXBUF_POSITIONING_MODE \
121     (gst_gdk_pixbuf_positioning_mode_get_type())
122
123 static GType
124 gst_gdk_pixbuf_positioning_mode_get_type (void)
125 {
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",
130         "pixels-absolute"},
131     {0, NULL, NULL},
132   };
133
134   static GType type;            /* 0 */
135
136   if (!type) {
137     type = g_enum_register_static ("GstGdkPixbufPositioningMode", pos_modes);
138   }
139
140   return type;
141 }
142
143 static void
144 gst_gdk_pixbuf_overlay_class_init (GstGdkPixbufOverlayClass * klass)
145 {
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);
150
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;
154
155   basetrans_class->start = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_start);
156   basetrans_class->stop = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_stop);
157
158   basetrans_class->before_transform =
159       GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_overlay_before_transform);
160
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);
165
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,
200           G_MAXINT, 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,
206           G_MAXINT, 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));
213   /**
214    * GstGdkPixbufOverlay:pixbuf:
215    *
216    * GdkPixbuf object to render.
217    *
218    * Since: 1.6
219    */
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));
224   /**
225    * GstGdkPixbufOverlay:positioning-mode:
226    *
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.
234    *
235    * Since: 1.6
236    */
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));
242
243   gst_element_class_add_static_pad_template (element_class, &sink_template);
244   gst_element_class_add_static_pad_template (element_class, &src_template);
245
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");
252 }
253
254 static void
255 gst_gdk_pixbuf_overlay_init (GstGdkPixbufOverlay * overlay)
256 {
257   overlay->offset_x = 0;
258   overlay->offset_y = 0;
259
260   overlay->relative_x = 0.0;
261   overlay->relative_y = 0.0;
262
263   overlay->positioning_mode = DEFAULT_POSITIONING_MODE;
264
265   overlay->overlay_width = 0;
266   overlay->overlay_height = 0;
267
268   overlay->alpha = 1.0;
269
270   overlay->pixbuf = NULL;
271 }
272
273 void
274 gst_gdk_pixbuf_overlay_set_property (GObject * object, guint property_id,
275     const GValue * value, GParamSpec * pspec)
276 {
277   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
278
279   GST_OBJECT_LOCK (overlay);
280
281   switch (property_id) {
282     case PROP_LOCATION:{
283       GError *err = NULL;
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",
288             err->message);
289         g_error_free (err);
290       }
291       break;
292     }
293     case PROP_PIXBUF:{
294       GdkPixbuf *pixbuf = g_value_get_object (value);
295
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));
300       break;
301     }
302     case PROP_OFFSET_X:
303       overlay->offset_x = g_value_get_int (value);
304       overlay->update_composition = TRUE;
305       break;
306     case PROP_OFFSET_Y:
307       overlay->offset_y = g_value_get_int (value);
308       overlay->update_composition = TRUE;
309       break;
310     case PROP_RELATIVE_X:
311       overlay->relative_x = g_value_get_double (value);
312       overlay->update_composition = TRUE;
313       break;
314     case PROP_RELATIVE_Y:
315       overlay->relative_y = g_value_get_double (value);
316       overlay->update_composition = TRUE;
317       break;
318     case PROP_OVERLAY_WIDTH:
319       overlay->overlay_width = g_value_get_int (value);
320       overlay->update_composition = TRUE;
321       break;
322     case PROP_OVERLAY_HEIGHT:
323       overlay->overlay_height = g_value_get_int (value);
324       overlay->update_composition = TRUE;
325       break;
326     case PROP_ALPHA:
327       overlay->alpha = g_value_get_double (value);
328       overlay->update_composition = TRUE;
329       break;
330     case PROP_POSITIONING_MODE:
331       overlay->positioning_mode = g_value_get_enum (value);
332       overlay->update_composition = TRUE;
333       break;
334     default:
335       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
336       break;
337   }
338
339   GST_OBJECT_UNLOCK (overlay);
340 }
341
342 void
343 gst_gdk_pixbuf_overlay_get_property (GObject * object, guint property_id,
344     GValue * value, GParamSpec * pspec)
345 {
346   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
347
348   GST_OBJECT_LOCK (overlay);
349
350   switch (property_id) {
351     case PROP_LOCATION:
352       g_value_set_string (value, overlay->location);
353       break;
354     case PROP_PIXBUF:
355       g_value_set_object (value, overlay->pixbuf);
356       break;
357     case PROP_OFFSET_X:
358       g_value_set_int (value, overlay->offset_x);
359       break;
360     case PROP_OFFSET_Y:
361       g_value_set_int (value, overlay->offset_y);
362       break;
363     case PROP_RELATIVE_X:
364       g_value_set_double (value, overlay->relative_x);
365       break;
366     case PROP_RELATIVE_Y:
367       g_value_set_double (value, overlay->relative_y);
368       break;
369     case PROP_OVERLAY_WIDTH:
370       g_value_set_int (value, overlay->overlay_width);
371       break;
372     case PROP_OVERLAY_HEIGHT:
373       g_value_set_int (value, overlay->overlay_height);
374       break;
375     case PROP_ALPHA:
376       g_value_set_double (value, overlay->alpha);
377       break;
378     case PROP_POSITIONING_MODE:
379       g_value_set_enum (value, overlay->positioning_mode);
380       break;
381     default:
382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
383       break;
384   }
385
386   GST_OBJECT_UNLOCK (overlay);
387 }
388
389 void
390 gst_gdk_pixbuf_overlay_finalize (GObject * object)
391 {
392   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (object);
393
394   g_free (overlay->location);
395   overlay->location = NULL;
396
397   G_OBJECT_CLASS (gst_gdk_pixbuf_overlay_parent_class)->finalize (object);
398 }
399
400 static gboolean
401 gst_gdk_pixbuf_overlay_load_image (GstGdkPixbufOverlay * overlay, GError ** err)
402 {
403   GdkPixbuf *pixbuf;
404
405   pixbuf = gdk_pixbuf_new_from_file (overlay->location, err);
406
407   if (pixbuf == NULL)
408     return FALSE;
409
410   gst_gdk_pixbuf_overlay_set_pixbuf (overlay, pixbuf);
411   return TRUE;
412 }
413
414 /* Takes ownership of pixbuf; call with OBJECT_LOCK */
415 static void
416 gst_gdk_pixbuf_overlay_set_pixbuf (GstGdkPixbufOverlay * overlay,
417     GdkPixbuf * pixbuf)
418 {
419   GstVideoMeta *video_meta;
420   guint8 *pixels, *p;
421   gint width, height, stride, w, h, plane;
422
423   if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
424     GdkPixbuf *alpha_pixbuf;
425
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;
432   }
433
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);
438
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
442    */
443   for (h = 0; h < height; ++h) {
444     p = pixels + (h * stride);
445     for (w = 0; w < width; ++w) {
446       guint8 tmp;
447
448       /* R-G-B-A ==> B-G-R-A */
449       tmp = p[0];
450       p[0] = p[2];
451       p[2] = tmp;
452
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));
457       }
458
459       p += 4;
460     }
461   }
462
463   if (overlay->pixels)
464     gst_buffer_unref (overlay->pixels);
465
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);
471
472   video_meta = gst_buffer_add_video_meta (overlay->pixels,
473       GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
474       width, height);
475
476   for (plane = 0; plane < video_meta->n_planes; ++plane)
477     video_meta->stride[plane] = stride;
478
479   overlay->update_composition = TRUE;
480
481   GST_INFO_OBJECT (overlay, "Updated pixbuf, %d x %d", width, height);
482 }
483
484 static gboolean
485 gst_gdk_pixbuf_overlay_start (GstBaseTransform * trans)
486 {
487   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
488   GError *err = NULL;
489
490   if (overlay->location != NULL) {
491     if (!gst_gdk_pixbuf_overlay_load_image (overlay, &err))
492       goto error_loading_image;
493
494     gst_base_transform_set_passthrough (trans, FALSE);
495   } else {
496     GST_WARNING_OBJECT (overlay, "no image location set, doing nothing");
497     gst_base_transform_set_passthrough (trans, TRUE);
498   }
499
500   return TRUE;
501
502 /* ERRORS */
503 error_loading_image:
504   {
505     GST_ELEMENT_ERROR (overlay, RESOURCE, OPEN_READ,
506         ("Could not load overlay image."), ("%s", err->message));
507     g_error_free (err);
508     return FALSE;
509   }
510 }
511
512 static gboolean
513 gst_gdk_pixbuf_overlay_stop (GstBaseTransform * trans)
514 {
515   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (trans);
516
517   if (overlay->comp) {
518     gst_video_overlay_composition_unref (overlay->comp);
519     overlay->comp = NULL;
520   }
521
522   gst_buffer_replace (&overlay->pixels, NULL);
523
524   return TRUE;
525 }
526
527 static gboolean
528 gst_gdk_pixbuf_overlay_set_info (GstVideoFilter * filter, GstCaps * incaps,
529     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
530 {
531   GST_INFO_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
532   return TRUE;
533 }
534
535 static void
536 gst_gdk_pixbuf_overlay_update_composition (GstGdkPixbufOverlay * overlay)
537 {
538   GstGdkPixbufPositioningMode positioning_mode;
539   GstVideoOverlayComposition *comp;
540   GstVideoOverlayRectangle *rect;
541   GstVideoMeta *overlay_meta;
542   gint x, y, width, height;
543   gint video_width =
544       GST_VIDEO_INFO_WIDTH (&GST_VIDEO_FILTER (overlay)->in_info);
545   gint video_height =
546       GST_VIDEO_INFO_HEIGHT (&GST_VIDEO_FILTER (overlay)->in_info);
547
548   if (overlay->comp) {
549     gst_video_overlay_composition_unref (overlay->comp);
550     overlay->comp = NULL;
551   }
552
553   if (overlay->alpha == 0.0 || overlay->pixels == NULL)
554     return;
555
556   overlay_meta = gst_buffer_get_video_meta (overlay->pixels);
557
558   positioning_mode = overlay->positioning_mode;
559
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);
563   } else {
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);
572   }
573
574   width = overlay->overlay_width;
575   if (width == 0)
576     width = overlay_meta->width;
577
578   height = overlay->overlay_height;
579   if (height == 0)
580     height = overlay_meta->height;
581
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);
590
591   rect = gst_video_overlay_rectangle_new_raw (overlay->pixels,
592       x, y, width, height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
593
594   if (overlay->alpha != 1.0)
595     gst_video_overlay_rectangle_set_global_alpha (rect, overlay->alpha);
596
597   comp = gst_video_overlay_composition_new (rect);
598   gst_video_overlay_rectangle_unref (rect);
599
600   overlay->comp = comp;
601 }
602
603 static void
604 gst_gdk_pixbuf_overlay_before_transform (GstBaseTransform * trans,
605     GstBuffer * outbuf)
606 {
607   GstClockTime stream_time;
608
609   stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
610       GST_BUFFER_TIMESTAMP (outbuf));
611
612   if (GST_CLOCK_TIME_IS_VALID (stream_time))
613     gst_object_sync_values (GST_OBJECT (trans), stream_time);
614 }
615
616 static GstFlowReturn
617 gst_gdk_pixbuf_overlay_transform_frame_ip (GstVideoFilter * filter,
618     GstVideoFrame * frame)
619 {
620   GstGdkPixbufOverlay *overlay = GST_GDK_PIXBUF_OVERLAY (filter);
621
622   GST_OBJECT_LOCK (overlay);
623
624   if (G_UNLIKELY (overlay->update_composition)) {
625     gst_gdk_pixbuf_overlay_update_composition (overlay);
626     overlay->update_composition = FALSE;
627   }
628
629   GST_OBJECT_UNLOCK (overlay);
630
631   if (overlay->comp != NULL)
632     gst_video_overlay_composition_blend (overlay->comp, frame);
633
634   return GST_FLOW_OK;
635 }