1 /* Video compositor plugin
2 * Copyright (C) 2004, 2008 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) 2014 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
5 * Copyright (C) 2014 Thibault Saunier <tsaunier@gnome.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-compositor
27 * Compositor can accept AYUV, VUYA, ARGB and BGRA video streams. For each of the requested
28 * sink pads it will compare the incoming geometry and framerate to define the
29 * output parameters. Indeed output video frames will have the geometry of the
30 * biggest incoming video stream and the framerate of the fastest incoming one.
32 * Compositor will do colorspace conversion.
34 * Individual parameters for each input stream can be configured on the
37 * * "xpos": The x-coordinate position of the top-left corner of the picture (#gint)
38 * * "ypos": The y-coordinate position of the top-left corner of the picture (#gint)
39 * * "width": The width of the picture; the input will be scaled if necessary (#gint)
40 * * "height": The height of the picture; the input will be scaled if necessary (#gint)
41 * * "alpha": The transparency of the picture; between 0.0 and 1.0. The blending
42 * is a simple copy when fully-transparent (0.0) and fully-opaque (1.0). (#gdouble)
43 * * "zorder": The z-order position of the picture in the composition (#guint)
48 * videotestsrc pattern=1 ! \
49 * video/x-raw,format=AYUV,framerate=\(fraction\)10/1,width=100,height=100 ! \
50 * videobox border-alpha=0 top=-70 bottom=-70 right=-220 ! \
51 * compositor name=comp sink_0::alpha=0.7 sink_1::alpha=0.5 ! \
52 * videoconvert ! xvimagesink \
54 * video/x-raw,format=AYUV,framerate=\(fraction\)5/1,width=320,height=240 ! comp.
55 * ]| A pipeline to demonstrate compositor used together with videobox.
56 * This should show a 320x240 pixels video test source with some transparency
57 * showing the background checker pattern. Another video test source with just
58 * the snow pattern of 100x100 pixels is overlaid on top of the first one on
59 * the left vertically centered with a small transparency showing the first
60 * video test source behind and the checker pattern under it. Note that the
61 * framerate of the output video is 10 frames per second.
63 * gst-launch-1.0 videotestsrc pattern=1 ! \
64 * video/x-raw, framerate=\(fraction\)10/1, width=100, height=100 ! \
65 * compositor name=comp ! videoconvert ! ximagesink \
67 * video/x-raw, framerate=\(fraction\)5/1, width=320, height=240 ! comp.
68 * ]| A pipeline to demonstrate bgra comping. (This does not demonstrate alpha blending).
70 * gst-launch-1.0 videotestsrc pattern=1 ! \
71 * video/x-raw,format =I420, framerate=\(fraction\)10/1, width=100, height=100 ! \
72 * compositor name=comp ! videoconvert ! ximagesink \
74 * video/x-raw,format=I420, framerate=\(fraction\)5/1, width=320, height=240 ! comp.
75 * ]| A pipeline to test I420
77 * gst-launch-1.0 compositor name=comp sink_1::alpha=0.5 sink_1::xpos=50 sink_1::ypos=50 ! \
78 * videoconvert ! ximagesink \
79 * videotestsrc pattern=snow timestamp-offset=3000000000 ! \
80 * "video/x-raw,format=AYUV,width=640,height=480,framerate=(fraction)30/1" ! \
81 * timeoverlay ! queue2 ! comp. \
82 * videotestsrc pattern=smpte ! \
83 * "video/x-raw,format=AYUV,width=800,height=600,framerate=(fraction)10/1" ! \
84 * timeoverlay ! queue2 ! comp.
85 * ]| A pipeline to demonstrate synchronized compositing (the second stream starts after 3 seconds)
95 #include "compositor.h"
98 #define orc_memset memset
100 #include <orc/orcfunctions.h>
103 GST_DEBUG_CATEGORY_STATIC (gst_compositor_debug);
104 #define GST_CAT_DEFAULT gst_compositor_debug
106 #define FORMATS " { AYUV, VUYA, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
107 " YVYU, I420, YV12, NV12, NV21, Y41B, RGB, BGR, xRGB, xBGR, "\
110 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
113 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
116 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
122 static void gst_compositor_child_proxy_init (gpointer g_iface,
123 gpointer iface_data);
125 #define GST_TYPE_COMPOSITOR_OPERATOR (gst_compositor_operator_get_type())
127 gst_compositor_operator_get_type (void)
129 static GType compositor_operator_type = 0;
131 static const GEnumValue compositor_operator[] = {
132 {COMPOSITOR_OPERATOR_SOURCE, "Source", "source"},
133 {COMPOSITOR_OPERATOR_OVER, "Over", "over"},
134 {COMPOSITOR_OPERATOR_ADD, "Add", "add"},
138 if (!compositor_operator_type) {
139 compositor_operator_type =
140 g_enum_register_static ("GstCompositorOperator", compositor_operator);
142 return compositor_operator_type;
145 #define GST_TYPE_COMPOSITOR_BACKGROUND (gst_compositor_background_get_type())
147 gst_compositor_background_get_type (void)
149 static GType compositor_background_type = 0;
151 static const GEnumValue compositor_background[] = {
152 {COMPOSITOR_BACKGROUND_CHECKER, "Checker pattern", "checker"},
153 {COMPOSITOR_BACKGROUND_BLACK, "Black", "black"},
154 {COMPOSITOR_BACKGROUND_WHITE, "White", "white"},
155 {COMPOSITOR_BACKGROUND_TRANSPARENT,
156 "Transparent Background to enable further compositing", "transparent"},
160 if (!compositor_background_type) {
161 compositor_background_type =
162 g_enum_register_static ("GstCompositorBackground",
163 compositor_background);
165 return compositor_background_type;
168 #define GST_TYPE_COMPOSITOR_SIZING_POLICY (gst_compositor_sizing_policy_get_type())
170 gst_compositor_sizing_policy_get_type (void)
172 static GType sizing_policy_type = 0;
174 static const GEnumValue sizing_polices[] = {
175 {COMPOSITOR_SIZING_POLICY_NONE,
176 "None: Image is scaled to fill configured destination rectangle without "
177 "padding or keeping the aspect ratio", "none"},
178 {COMPOSITOR_SIZING_POLICY_KEEP_ASPECT_RATIO,
179 "Keep Aspect Ratio: Image is scaled to fit destination rectangle "
180 "specified by GstCompositorPad:{xpos, ypos, width, height} "
181 "with preserved aspect ratio. Resulting image will be centered in "
182 "the destination rectangle with padding if necessary",
183 "keep-aspect-ratio"},
187 if (!sizing_policy_type) {
189 g_enum_register_static ("GstCompositorSizingPolicy", sizing_polices);
191 return sizing_policy_type;
194 #define DEFAULT_PAD_XPOS 0
195 #define DEFAULT_PAD_YPOS 0
196 #define DEFAULT_PAD_WIDTH -1
197 #define DEFAULT_PAD_HEIGHT -1
198 #define DEFAULT_PAD_ALPHA 1.0
199 #define DEFAULT_PAD_OPERATOR COMPOSITOR_OPERATOR_OVER
200 #define DEFAULT_PAD_SIZING_POLICY COMPOSITOR_SIZING_POLICY_NONE
211 PROP_PAD_SIZING_POLICY,
214 G_DEFINE_TYPE (GstCompositorPad, gst_compositor_pad,
215 GST_TYPE_VIDEO_AGGREGATOR_PARALLEL_CONVERT_PAD);
218 gst_compositor_pad_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec)
221 GstCompositorPad *pad = GST_COMPOSITOR_PAD (object);
225 g_value_set_int (value, pad->xpos);
228 g_value_set_int (value, pad->ypos);
231 g_value_set_int (value, pad->width);
233 case PROP_PAD_HEIGHT:
234 g_value_set_int (value, pad->height);
237 g_value_set_double (value, pad->alpha);
239 case PROP_PAD_OPERATOR:
240 g_value_set_enum (value, pad->op);
242 case PROP_PAD_SIZING_POLICY:
243 g_value_set_enum (value, pad->sizing_policy);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gst_compositor_pad_set_property (GObject * object, guint prop_id,
253 const GValue * value, GParamSpec * pspec)
255 GstCompositorPad *pad = GST_COMPOSITOR_PAD (object);
259 pad->xpos = g_value_get_int (value);
262 pad->ypos = g_value_get_int (value);
265 pad->width = g_value_get_int (value);
266 gst_video_aggregator_convert_pad_update_conversion_info
267 (GST_VIDEO_AGGREGATOR_CONVERT_PAD (pad));
269 case PROP_PAD_HEIGHT:
270 pad->height = g_value_get_int (value);
271 gst_video_aggregator_convert_pad_update_conversion_info
272 (GST_VIDEO_AGGREGATOR_CONVERT_PAD (pad));
275 pad->alpha = g_value_get_double (value);
277 case PROP_PAD_OPERATOR:
278 pad->op = g_value_get_enum (value);
279 gst_video_aggregator_pad_set_needs_alpha (GST_VIDEO_AGGREGATOR_PAD (pad),
280 pad->op == COMPOSITOR_OPERATOR_ADD);
282 case PROP_PAD_SIZING_POLICY:
283 pad->sizing_policy = g_value_get_enum (value);
284 gst_video_aggregator_convert_pad_update_conversion_info
285 (GST_VIDEO_AGGREGATOR_CONVERT_PAD (pad));
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 _mixer_pad_get_output_size (GstCompositor * comp, GstCompositorPad * comp_pad,
295 gint out_par_n, gint out_par_d, gint * width, gint * height,
296 gint * x_offset, gint * y_offset)
298 GstVideoAggregatorPad *vagg_pad = GST_VIDEO_AGGREGATOR_PAD (comp_pad);
299 gint pad_width, pad_height;
307 /* FIXME: Anything better we can do here? */
308 if (!vagg_pad->info.finfo
309 || vagg_pad->info.finfo->format == GST_VIDEO_FORMAT_UNKNOWN) {
310 GST_DEBUG_OBJECT (comp_pad, "Have no caps yet");
314 if (comp->zero_size_is_unscaled) {
317 0 ? GST_VIDEO_INFO_WIDTH (&vagg_pad->info) : comp_pad->width;
320 0 ? GST_VIDEO_INFO_HEIGHT (&vagg_pad->info) : comp_pad->height;
324 0 ? GST_VIDEO_INFO_WIDTH (&vagg_pad->info) : comp_pad->width;
327 0 ? GST_VIDEO_INFO_HEIGHT (&vagg_pad->info) : comp_pad->height;
330 if (pad_width == 0 || pad_height == 0)
333 if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, pad_width, pad_height,
334 GST_VIDEO_INFO_PAR_N (&vagg_pad->info),
335 GST_VIDEO_INFO_PAR_D (&vagg_pad->info), out_par_n, out_par_d)) {
336 GST_WARNING_OBJECT (comp_pad, "Cannot calculate display aspect ratio");
340 GST_LOG_OBJECT (comp_pad, "scaling %ux%u by %u/%u (%u/%u / %u/%u)", pad_width,
341 pad_height, dar_n, dar_d, GST_VIDEO_INFO_PAR_N (&vagg_pad->info),
342 GST_VIDEO_INFO_PAR_D (&vagg_pad->info), out_par_n, out_par_d);
344 switch (comp_pad->sizing_policy) {
345 case COMPOSITOR_SIZING_POLICY_NONE:
346 /* Pick either height or width, whichever is an integer multiple of the
347 * display aspect ratio. However, prefer preserving the height to account
348 * for interlaced video. */
349 if (pad_height % dar_n == 0) {
350 pad_width = gst_util_uint64_scale_int (pad_height, dar_n, dar_d);
351 } else if (pad_width % dar_d == 0) {
352 pad_height = gst_util_uint64_scale_int (pad_width, dar_d, dar_n);
354 pad_width = gst_util_uint64_scale_int (pad_height, dar_n, dar_d);
357 case COMPOSITOR_SIZING_POLICY_KEEP_ASPECT_RATIO:
359 gint from_dar_n, from_dar_d, to_dar_n, to_dar_d, num, den;
361 /* Calculate DAR again with actual video size */
362 if (!gst_util_fraction_multiply (GST_VIDEO_INFO_WIDTH (&vagg_pad->info),
363 GST_VIDEO_INFO_HEIGHT (&vagg_pad->info),
364 GST_VIDEO_INFO_PAR_N (&vagg_pad->info),
365 GST_VIDEO_INFO_PAR_D (&vagg_pad->info), &from_dar_n,
367 from_dar_n = from_dar_d = -1;
370 if (!gst_util_fraction_multiply (pad_width, pad_height,
371 out_par_n, out_par_d, &to_dar_n, &to_dar_d)) {
372 to_dar_n = to_dar_d = -1;
375 if (from_dar_n != to_dar_n || from_dar_d != to_dar_d) {
376 /* Calculate new output resolution */
377 if (from_dar_n != -1 && from_dar_d != -1
378 && gst_util_fraction_multiply (from_dar_n, from_dar_d,
379 out_par_d, out_par_n, &num, &den)) {
380 GstVideoRectangle src_rect, dst_rect, rst_rect;
382 src_rect.h = gst_util_uint64_scale_int (pad_width, den, num);
383 if (src_rect.h == 0) {
389 src_rect.x = src_rect.y = 0;
390 src_rect.w = pad_width;
392 dst_rect.x = dst_rect.y = 0;
393 dst_rect.w = pad_width;
394 dst_rect.h = pad_height;
396 /* Scale rect to be centered in destination rect */
397 gst_video_center_rect (&src_rect, &dst_rect, &rst_rect, TRUE);
399 GST_LOG_OBJECT (comp_pad,
400 "Re-calculated size %dx%d -> %dx%d (x-offset %d, y-offset %d)",
401 pad_width, pad_height, rst_rect.w, rst_rect.h, rst_rect.x,
404 *x_offset = rst_rect.x;
405 *y_offset = rst_rect.y;
406 pad_width = rst_rect.w;
407 pad_height = rst_rect.h;
409 GST_WARNING_OBJECT (comp_pad, "Failed to calculate output size");
422 *height = pad_height;
426 is_point_contained (const GstVideoRectangle rect, const gint px, const gint py)
428 if ((px >= rect.x) && (px <= rect.x + rect.w) &&
429 (py >= rect.y) && (py <= rect.y + rect.h))
434 /* Test whether rectangle2 contains rectangle 1 (geometrically) */
436 is_rectangle_contained (const GstVideoRectangle rect1,
437 const GstVideoRectangle rect2)
439 if ((rect2.x <= rect1.x) && (rect2.y <= rect1.y) &&
440 ((rect2.x + rect2.w) >= (rect1.x + rect1.w)) &&
441 ((rect2.y + rect2.h) >= (rect1.y + rect1.h)))
446 static GstVideoRectangle
447 clamp_rectangle (gint x, gint y, gint w, gint h, gint outer_width,
452 GstVideoRectangle clamped;
454 /* Clamp the x/y coordinates of this frame to the output boundaries to cover
455 * the case where (say, with negative xpos/ypos or w/h greater than the output
456 * size) the non-obscured portion of the frame could be outside the bounds of
457 * the video itself and hence not visible at all */
458 clamped.x = CLAMP (x, 0, outer_width);
459 clamped.y = CLAMP (y, 0, outer_height);
460 clamped.w = CLAMP (x2, 0, outer_width) - clamped.x;
461 clamped.h = CLAMP (y2, 0, outer_height) - clamped.y;
466 /* Call this with the lock taken */
468 _pad_obscures_rectangle (GstVideoAggregator * vagg, GstVideoAggregatorPad * pad,
469 const GstVideoRectangle rect)
471 GstVideoRectangle pad_rect;
472 GstCompositorPad *cpad = GST_COMPOSITOR_PAD (pad);
473 GstStructure *converter_config = NULL;
474 gboolean fill_border = TRUE;
475 guint32 border_argb = 0xff000000;
476 gint x_offset, y_offset;
478 /* No buffer to obscure the rectangle with */
479 if (!gst_video_aggregator_pad_has_current_buffer (pad))
482 /* Can't obscure if we introduce alpha or if the format has an alpha
483 * component as we'd have to inspect every pixel to know if the frame is
484 * opaque, so assume it doesn't obscure
486 if (cpad->alpha != 1.0 || GST_VIDEO_INFO_HAS_ALPHA (&pad->info))
489 /* If a converter-config is set and it is either configured to not fill any
490 * borders, or configured to use a non-opaque color, then we have to handle
491 * the pad as potentially containing transparency */
492 g_object_get (pad, "converter-config", &converter_config, NULL);
493 if (converter_config) {
494 gst_structure_get (converter_config, GST_VIDEO_CONVERTER_OPT_BORDER_ARGB,
495 G_TYPE_UINT, &border_argb, NULL);
496 gst_structure_get (converter_config, GST_VIDEO_CONVERTER_OPT_FILL_BORDER,
497 G_TYPE_BOOLEAN, &fill_border, NULL);
499 gst_clear_structure (&converter_config);
500 if (!fill_border || (border_argb & 0xff000000) != 0xff000000)
503 pad_rect.x = cpad->xpos;
504 pad_rect.y = cpad->ypos;
505 /* Handle pixel and display aspect ratios to find the actual size */
506 _mixer_pad_get_output_size (GST_COMPOSITOR (vagg), cpad,
507 GST_VIDEO_INFO_PAR_N (&vagg->info), GST_VIDEO_INFO_PAR_D (&vagg->info),
508 &(pad_rect.w), &(pad_rect.h), &x_offset, &y_offset);
509 pad_rect.x += x_offset;
510 pad_rect.y += y_offset;
512 if (!is_rectangle_contained (rect, pad_rect))
515 GST_DEBUG_OBJECT (pad, "Pad %s %ix%i@(%i,%i) obscures rect %ix%i@(%i,%i)",
516 GST_PAD_NAME (pad), pad_rect.w, pad_rect.h, pad_rect.x, pad_rect.y,
517 rect.w, rect.h, rect.x, rect.y);
523 gst_compositor_pad_prepare_frame_start (GstVideoAggregatorPad * pad,
524 GstVideoAggregator * vagg, GstBuffer * buffer,
525 GstVideoFrame * prepared_frame)
527 GstCompositorPad *cpad = GST_COMPOSITOR_PAD (pad);
529 gboolean frame_obscured = FALSE;
531 /* The rectangle representing this frame, clamped to the video's boundaries.
532 * Due to the clamping, this is different from the frame width/height above. */
533 GstVideoRectangle frame_rect;
535 /* There's three types of width/height here:
536 * 1. GST_VIDEO_FRAME_WIDTH/HEIGHT:
537 * The frame width/height (same as pad->info.height/width;
538 * see gst_video_frame_map())
539 * 2. cpad->width/height:
540 * The optional pad property for scaling the frame (if zero, the video is
542 * 3. conversion_info.width/height:
543 * Equal to cpad->width/height if it's set, otherwise it's the pad
544 * width/height. See ->set_info()
547 _mixer_pad_get_output_size (GST_COMPOSITOR (vagg), cpad,
548 GST_VIDEO_INFO_PAR_N (&vagg->info), GST_VIDEO_INFO_PAR_D (&vagg->info),
549 &width, &height, &cpad->x_offset, &cpad->y_offset);
551 if (cpad->alpha == 0.0) {
552 GST_DEBUG_OBJECT (pad, "Pad has alpha 0.0, not converting frame");
556 if (gst_aggregator_pad_is_inactive (GST_AGGREGATOR_PAD (pad)))
559 frame_rect = clamp_rectangle (cpad->xpos + cpad->x_offset,
560 cpad->ypos + cpad->y_offset, width, height,
561 GST_VIDEO_INFO_WIDTH (&vagg->info), GST_VIDEO_INFO_HEIGHT (&vagg->info));
563 if (frame_rect.w == 0 || frame_rect.h == 0) {
564 GST_DEBUG_OBJECT (pad, "Resulting frame is zero-width or zero-height "
565 "(w: %i, h: %i), skipping", frame_rect.w, frame_rect.h);
569 GST_OBJECT_LOCK (vagg);
570 /* Check if this frame is obscured by a higher-zorder frame
571 * TODO: Also skip a frame if it's obscured by a combination of
572 * higher-zorder frames */
573 l = g_list_find (GST_ELEMENT (vagg)->sinkpads, pad);
574 /* The pad might've just been removed */
577 for (; l; l = l->next) {
578 if (_pad_obscures_rectangle (vagg, l->data, frame_rect)) {
579 frame_obscured = TRUE;
583 GST_OBJECT_UNLOCK (vagg);
588 GST_VIDEO_AGGREGATOR_PAD_CLASS
589 (gst_compositor_pad_parent_class)->prepare_frame_start (pad, vagg, buffer,
594 gst_compositor_pad_create_conversion_info (GstVideoAggregatorConvertPad * pad,
595 GstVideoAggregator * vagg, GstVideoInfo * conversion_info)
597 GstCompositorPad *cpad = GST_COMPOSITOR_PAD (pad);
599 gint x_offset, y_offset;
601 GST_VIDEO_AGGREGATOR_CONVERT_PAD_CLASS
602 (gst_compositor_pad_parent_class)->create_conversion_info (pad, vagg,
604 if (!conversion_info->finfo)
607 _mixer_pad_get_output_size (GST_COMPOSITOR (vagg), cpad,
608 GST_VIDEO_INFO_PAR_N (&vagg->info), GST_VIDEO_INFO_PAR_D (&vagg->info),
609 &width, &height, &x_offset, &y_offset);
611 /* The only thing that can change here is the width
612 * and height, otherwise set_info would've been called */
613 if (GST_VIDEO_INFO_WIDTH (conversion_info) != width ||
614 GST_VIDEO_INFO_HEIGHT (conversion_info) != height) {
615 GstVideoInfo tmp_info;
617 /* Initialize with the wanted video format and our original width and
618 * height as we don't want to rescale. Then copy over the wanted
619 * colorimetry, and chroma-site and our current pixel-aspect-ratio
620 * and other relevant fields.
622 gst_video_info_set_format (&tmp_info,
623 GST_VIDEO_INFO_FORMAT (conversion_info), width, height);
624 tmp_info.chroma_site = conversion_info->chroma_site;
625 tmp_info.colorimetry = conversion_info->colorimetry;
626 tmp_info.par_n = conversion_info->par_n;
627 tmp_info.par_d = conversion_info->par_d;
628 tmp_info.fps_n = conversion_info->fps_n;
629 tmp_info.fps_d = conversion_info->fps_d;
630 tmp_info.flags = conversion_info->flags;
631 tmp_info.interlace_mode = conversion_info->interlace_mode;
633 *conversion_info = tmp_info;
638 gst_compositor_pad_class_init (GstCompositorPadClass * klass)
640 GObjectClass *gobject_class = (GObjectClass *) klass;
641 GstVideoAggregatorPadClass *vaggpadclass =
642 (GstVideoAggregatorPadClass *) klass;
643 GstVideoAggregatorConvertPadClass *vaggcpadclass =
644 (GstVideoAggregatorConvertPadClass *) klass;
646 gobject_class->set_property = gst_compositor_pad_set_property;
647 gobject_class->get_property = gst_compositor_pad_get_property;
649 g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
650 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
651 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
652 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
653 g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
654 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
655 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
656 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
657 g_object_class_install_property (gobject_class, PROP_PAD_WIDTH,
658 g_param_spec_int ("width", "Width", "Width of the picture",
659 G_MININT, G_MAXINT, DEFAULT_PAD_WIDTH,
660 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
661 g_object_class_install_property (gobject_class, PROP_PAD_HEIGHT,
662 g_param_spec_int ("height", "Height", "Height of the picture",
663 G_MININT, G_MAXINT, DEFAULT_PAD_HEIGHT,
664 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
665 g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
666 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
668 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
669 g_object_class_install_property (gobject_class, PROP_PAD_OPERATOR,
670 g_param_spec_enum ("operator", "Operator",
671 "Blending operator to use for blending this pad over the previous ones",
672 GST_TYPE_COMPOSITOR_OPERATOR, DEFAULT_PAD_OPERATOR,
673 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
676 * GstCompositorPad:sizing-policy:
678 * Specifies sizing policy to use. Depending on selected sizing policy,
679 * scaled image might not fully cover the configured target rectangle area
680 * (e.g., "keep-aspect-ratio"). In that case, any uncovered area will be
681 * filled with background unless the uncovered area is drawn by other image.
685 g_object_class_install_property (gobject_class, PROP_PAD_SIZING_POLICY,
686 g_param_spec_enum ("sizing-policy", "Sizing policy",
687 "Sizing policy to use for image scaling",
688 GST_TYPE_COMPOSITOR_SIZING_POLICY, DEFAULT_PAD_SIZING_POLICY,
689 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
691 vaggpadclass->prepare_frame_start =
692 GST_DEBUG_FUNCPTR (gst_compositor_pad_prepare_frame_start);
694 vaggcpadclass->create_conversion_info =
695 GST_DEBUG_FUNCPTR (gst_compositor_pad_create_conversion_info);
697 gst_type_mark_as_plugin_api (GST_TYPE_COMPOSITOR_SIZING_POLICY, 0);
701 gst_compositor_pad_init (GstCompositorPad * compo_pad)
703 compo_pad->xpos = DEFAULT_PAD_XPOS;
704 compo_pad->ypos = DEFAULT_PAD_YPOS;
705 compo_pad->alpha = DEFAULT_PAD_ALPHA;
706 compo_pad->op = DEFAULT_PAD_OPERATOR;
707 compo_pad->width = DEFAULT_PAD_WIDTH;
708 compo_pad->height = DEFAULT_PAD_HEIGHT;
709 compo_pad->sizing_policy = DEFAULT_PAD_SIZING_POLICY;
714 #define DEFAULT_BACKGROUND COMPOSITOR_BACKGROUND_CHECKER
715 #define DEFAULT_ZERO_SIZE_IS_UNSCALED TRUE
716 #define DEFAULT_MAX_THREADS 0
722 PROP_ZERO_SIZE_IS_UNSCALED,
724 PROP_IGNORE_INACTIVE_PADS,
728 gst_compositor_get_property (GObject * object,
729 guint prop_id, GValue * value, GParamSpec * pspec)
731 GstCompositor *self = GST_COMPOSITOR (object);
734 case PROP_BACKGROUND:
735 g_value_set_enum (value, self->background);
737 case PROP_ZERO_SIZE_IS_UNSCALED:
738 g_value_set_boolean (value, self->zero_size_is_unscaled);
740 case PROP_MAX_THREADS:
741 g_value_set_uint (value, self->max_threads);
743 case PROP_IGNORE_INACTIVE_PADS:
744 g_value_set_boolean (value,
745 gst_aggregator_get_ignore_inactive_pads (GST_AGGREGATOR (object)));
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754 gst_compositor_set_property (GObject * object,
755 guint prop_id, const GValue * value, GParamSpec * pspec)
757 GstCompositor *self = GST_COMPOSITOR (object);
760 case PROP_BACKGROUND:
761 self->background = g_value_get_enum (value);
763 case PROP_ZERO_SIZE_IS_UNSCALED:
764 self->zero_size_is_unscaled = g_value_get_boolean (value);
766 case PROP_MAX_THREADS:
767 self->max_threads = g_value_get_uint (value);
769 case PROP_IGNORE_INACTIVE_PADS:
770 gst_aggregator_set_ignore_inactive_pads (GST_AGGREGATOR (object),
771 g_value_get_boolean (value));
774 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
779 #define gst_compositor_parent_class parent_class
780 G_DEFINE_TYPE_WITH_CODE (GstCompositor, gst_compositor,
781 GST_TYPE_VIDEO_AGGREGATOR, G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
782 gst_compositor_child_proxy_init));
783 GST_ELEMENT_REGISTER_DEFINE (compositor, "compositor", GST_RANK_PRIMARY + 1,
784 GST_TYPE_COMPOSITOR);
787 set_functions (GstCompositor * self, const GstVideoInfo * info)
789 gboolean ret = FALSE;
792 self->overlay = NULL;
793 self->fill_checker = NULL;
794 self->fill_color = NULL;
796 switch (GST_VIDEO_INFO_FORMAT (info)) {
797 case GST_VIDEO_FORMAT_AYUV:
798 self->blend = gst_compositor_blend_ayuv;
799 self->overlay = gst_compositor_overlay_ayuv;
800 self->fill_checker = gst_compositor_fill_checker_ayuv;
801 self->fill_color = gst_compositor_fill_color_ayuv;
804 case GST_VIDEO_FORMAT_VUYA:
805 self->blend = gst_compositor_blend_vuya;
806 self->overlay = gst_compositor_overlay_vuya;
807 self->fill_checker = gst_compositor_fill_checker_vuya;
808 self->fill_color = gst_compositor_fill_color_vuya;
811 case GST_VIDEO_FORMAT_ARGB:
812 self->blend = gst_compositor_blend_argb;
813 self->overlay = gst_compositor_overlay_argb;
814 self->fill_checker = gst_compositor_fill_checker_argb;
815 self->fill_color = gst_compositor_fill_color_argb;
818 case GST_VIDEO_FORMAT_BGRA:
819 self->blend = gst_compositor_blend_bgra;
820 self->overlay = gst_compositor_overlay_bgra;
821 self->fill_checker = gst_compositor_fill_checker_bgra;
822 self->fill_color = gst_compositor_fill_color_bgra;
825 case GST_VIDEO_FORMAT_ABGR:
826 self->blend = gst_compositor_blend_abgr;
827 self->overlay = gst_compositor_overlay_abgr;
828 self->fill_checker = gst_compositor_fill_checker_abgr;
829 self->fill_color = gst_compositor_fill_color_abgr;
832 case GST_VIDEO_FORMAT_RGBA:
833 self->blend = gst_compositor_blend_rgba;
834 self->overlay = gst_compositor_overlay_rgba;
835 self->fill_checker = gst_compositor_fill_checker_rgba;
836 self->fill_color = gst_compositor_fill_color_rgba;
839 case GST_VIDEO_FORMAT_Y444:
840 self->blend = gst_compositor_blend_y444;
841 self->overlay = self->blend;
842 self->fill_checker = gst_compositor_fill_checker_y444;
843 self->fill_color = gst_compositor_fill_color_y444;
846 case GST_VIDEO_FORMAT_Y42B:
847 self->blend = gst_compositor_blend_y42b;
848 self->overlay = self->blend;
849 self->fill_checker = gst_compositor_fill_checker_y42b;
850 self->fill_color = gst_compositor_fill_color_y42b;
853 case GST_VIDEO_FORMAT_YUY2:
854 self->blend = gst_compositor_blend_yuy2;
855 self->overlay = self->blend;
856 self->fill_checker = gst_compositor_fill_checker_yuy2;
857 self->fill_color = gst_compositor_fill_color_yuy2;
860 case GST_VIDEO_FORMAT_UYVY:
861 self->blend = gst_compositor_blend_uyvy;
862 self->overlay = self->blend;
863 self->fill_checker = gst_compositor_fill_checker_uyvy;
864 self->fill_color = gst_compositor_fill_color_uyvy;
867 case GST_VIDEO_FORMAT_YVYU:
868 self->blend = gst_compositor_blend_yvyu;
869 self->overlay = self->blend;
870 self->fill_checker = gst_compositor_fill_checker_yvyu;
871 self->fill_color = gst_compositor_fill_color_yvyu;
874 case GST_VIDEO_FORMAT_I420:
875 self->blend = gst_compositor_blend_i420;
876 self->overlay = self->blend;
877 self->fill_checker = gst_compositor_fill_checker_i420;
878 self->fill_color = gst_compositor_fill_color_i420;
881 case GST_VIDEO_FORMAT_YV12:
882 self->blend = gst_compositor_blend_yv12;
883 self->overlay = self->blend;
884 self->fill_checker = gst_compositor_fill_checker_yv12;
885 self->fill_color = gst_compositor_fill_color_yv12;
888 case GST_VIDEO_FORMAT_NV12:
889 self->blend = gst_compositor_blend_nv12;
890 self->overlay = self->blend;
891 self->fill_checker = gst_compositor_fill_checker_nv12;
892 self->fill_color = gst_compositor_fill_color_nv12;
895 case GST_VIDEO_FORMAT_NV21:
896 self->blend = gst_compositor_blend_nv21;
897 self->overlay = self->blend;
898 self->fill_checker = gst_compositor_fill_checker_nv21;
899 self->fill_color = gst_compositor_fill_color_nv21;
902 case GST_VIDEO_FORMAT_Y41B:
903 self->blend = gst_compositor_blend_y41b;
904 self->overlay = self->blend;
905 self->fill_checker = gst_compositor_fill_checker_y41b;
906 self->fill_color = gst_compositor_fill_color_y41b;
909 case GST_VIDEO_FORMAT_RGB:
910 self->blend = gst_compositor_blend_rgb;
911 self->overlay = self->blend;
912 self->fill_checker = gst_compositor_fill_checker_rgb;
913 self->fill_color = gst_compositor_fill_color_rgb;
916 case GST_VIDEO_FORMAT_BGR:
917 self->blend = gst_compositor_blend_bgr;
918 self->overlay = self->blend;
919 self->fill_checker = gst_compositor_fill_checker_bgr;
920 self->fill_color = gst_compositor_fill_color_bgr;
923 case GST_VIDEO_FORMAT_xRGB:
924 self->blend = gst_compositor_blend_xrgb;
925 self->overlay = self->blend;
926 self->fill_checker = gst_compositor_fill_checker_xrgb;
927 self->fill_color = gst_compositor_fill_color_xrgb;
930 case GST_VIDEO_FORMAT_xBGR:
931 self->blend = gst_compositor_blend_xbgr;
932 self->overlay = self->blend;
933 self->fill_checker = gst_compositor_fill_checker_xbgr;
934 self->fill_color = gst_compositor_fill_color_xbgr;
937 case GST_VIDEO_FORMAT_RGBx:
938 self->blend = gst_compositor_blend_rgbx;
939 self->overlay = self->blend;
940 self->fill_checker = gst_compositor_fill_checker_rgbx;
941 self->fill_color = gst_compositor_fill_color_rgbx;
944 case GST_VIDEO_FORMAT_BGRx:
945 self->blend = gst_compositor_blend_bgrx;
946 self->overlay = self->blend;
947 self->fill_checker = gst_compositor_fill_checker_bgrx;
948 self->fill_color = gst_compositor_fill_color_bgrx;
959 _fixate_caps (GstAggregator * agg, GstCaps * caps)
961 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
963 gint best_width = -1, best_height = -1;
964 gint best_fps_n = -1, best_fps_d = -1;
966 gdouble best_fps = 0.;
970 ret = gst_caps_make_writable (caps);
972 /* we need this to calculate how large to make the output frame */
973 s = gst_caps_get_structure (ret, 0);
974 if (gst_structure_has_field (s, "pixel-aspect-ratio")) {
975 gst_structure_fixate_field_nearest_fraction (s, "pixel-aspect-ratio", 1, 1);
976 gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_n, &par_d);
981 GST_OBJECT_LOCK (vagg);
982 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
983 GstVideoAggregatorPad *vaggpad = l->data;
984 GstCompositorPad *compositor_pad = GST_COMPOSITOR_PAD (vaggpad);
985 gint this_width, this_height;
992 if (gst_aggregator_pad_is_inactive (GST_AGGREGATOR_PAD (vaggpad)))
995 fps_n = GST_VIDEO_INFO_FPS_N (&vaggpad->info);
996 fps_d = GST_VIDEO_INFO_FPS_D (&vaggpad->info);
997 _mixer_pad_get_output_size (GST_COMPOSITOR (vagg), compositor_pad, par_n,
998 par_d, &width, &height, &x_offset, &y_offset);
1000 if (width == 0 || height == 0)
1003 /* {x,y}_offset represent padding size of each top and left area.
1004 * To calculate total resolution, count bottom and right padding area
1006 this_width = width + MAX (compositor_pad->xpos + 2 * x_offset, 0);
1007 this_height = height + MAX (compositor_pad->ypos + 2 * y_offset, 0);
1009 if (best_width < this_width)
1010 best_width = this_width;
1011 if (best_height < this_height)
1012 best_height = this_height;
1017 gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);
1019 if (best_fps < cur_fps) {
1025 GST_OBJECT_UNLOCK (vagg);
1027 if (best_fps_n <= 0 || best_fps_d <= 0 || best_fps == 0.0) {
1033 gst_structure_fixate_field_nearest_int (s, "width", best_width);
1034 gst_structure_fixate_field_nearest_int (s, "height", best_height);
1035 gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
1037 ret = gst_caps_fixate (ret);
1043 gst_parallelized_task_thread_func (gpointer data)
1045 GstParallelizedTaskRunner *runner = data;
1048 g_mutex_lock (&runner->lock);
1049 idx = runner->n_todo--;
1050 g_assert (runner->n_todo >= -1);
1051 g_mutex_unlock (&runner->lock);
1053 g_assert (runner->func != NULL);
1055 runner->func (runner->task_data[idx]);
1059 gst_parallelized_task_runner_join (GstParallelizedTaskRunner * self)
1061 gboolean joined = FALSE;
1064 g_mutex_lock (&self->lock);
1065 if (!(joined = gst_queue_array_is_empty (self->tasks))) {
1066 gpointer task = gst_queue_array_pop_head (self->tasks);
1067 g_mutex_unlock (&self->lock);
1068 gst_task_pool_join (self->pool, task);
1070 g_mutex_unlock (&self->lock);
1076 gst_parallelized_task_runner_free (GstParallelizedTaskRunner * self)
1078 gst_parallelized_task_runner_join (self);
1080 gst_queue_array_free (self->tasks);
1082 gst_task_pool_cleanup (self->pool);
1083 gst_object_unref (self->pool);
1084 g_mutex_clear (&self->lock);
1088 static GstParallelizedTaskRunner *
1089 gst_parallelized_task_runner_new (guint n_threads, GstTaskPool * pool,
1090 gboolean async_tasks)
1092 GstParallelizedTaskRunner *self;
1095 n_threads = g_get_num_processors ();
1097 self = g_new0 (GstParallelizedTaskRunner, 1);
1100 self->pool = g_object_ref (pool);
1101 self->own_pool = FALSE;
1103 /* No reason to split up the work between more threads than the
1105 if (GST_IS_SHARED_TASK_POOL (pool))
1108 gst_shared_task_pool_get_max_threads (GST_SHARED_TASK_POOL (pool)));
1110 self->pool = gst_shared_task_pool_new ();
1111 self->own_pool = TRUE;
1112 gst_shared_task_pool_set_max_threads (GST_SHARED_TASK_POOL (self->pool),
1114 gst_task_pool_prepare (self->pool, NULL);
1117 self->tasks = gst_queue_array_new (n_threads);
1119 self->n_threads = n_threads;
1122 g_mutex_init (&self->lock);
1124 /* Set when scheduling a job */
1126 self->task_data = NULL;
1127 self->async_tasks = async_tasks;
1133 gst_parallelized_task_runner_finish (GstParallelizedTaskRunner * self)
1135 g_return_if_fail (self->func != NULL);
1137 gst_parallelized_task_runner_join (self);
1140 self->task_data = NULL;
1144 gst_parallelized_task_runner_run (GstParallelizedTaskRunner * self,
1145 GstParallelizedTaskFunc func, gpointer * task_data)
1147 guint n_threads = self->n_threads;
1150 self->task_data = task_data;
1152 if (n_threads > 1 || self->async_tasks) {
1154 g_mutex_lock (&self->lock);
1155 self->n_todo = self->n_threads - 1;
1156 if (!self->async_tasks) {
1157 /* if not async, perform one of the functions in the current thread */
1161 for (; i < n_threads; i++) {
1163 gst_task_pool_push (self->pool, gst_parallelized_task_thread_func,
1166 /* The return value of push() is nullable but NULL is only returned
1167 * with the shared task pool when gst_task_pool_prepare() has not been
1168 * called and would thus be a programming error that we should hard-fail
1171 g_assert (task != NULL);
1172 gst_queue_array_push_tail (self->tasks, task);
1174 g_mutex_unlock (&self->lock);
1177 if (!self->async_tasks) {
1178 self->func (self->task_data[self->n_threads - 1]);
1180 gst_parallelized_task_runner_finish (self);
1185 _negotiated_caps (GstAggregator * agg, GstCaps * caps)
1187 GstCompositor *compositor = GST_COMPOSITOR (agg);
1188 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
1189 GstVideoInfo v_info;
1192 GST_DEBUG_OBJECT (agg, "Negotiated caps %" GST_PTR_FORMAT, caps);
1194 if (!gst_video_info_from_caps (&v_info, caps))
1197 if (!set_functions (compositor, &v_info)) {
1198 GST_ERROR_OBJECT (agg, "Failed to setup vfuncs");
1202 if (compositor->max_threads == 0)
1203 n_threads = g_get_num_processors ();
1205 n_threads = compositor->max_threads;
1207 /* Magic number of 200 lines */
1208 if (GST_VIDEO_INFO_HEIGHT (&v_info) / n_threads < 200)
1209 n_threads = (GST_VIDEO_INFO_HEIGHT (&v_info) + 199) / 200;
1213 /* XXX: implement better thread count change */
1214 if (compositor->blend_runner
1215 && compositor->blend_runner->n_threads != n_threads) {
1216 gst_parallelized_task_runner_free (compositor->blend_runner);
1217 compositor->blend_runner = NULL;
1219 if (!compositor->blend_runner) {
1220 GstTaskPool *pool = gst_video_aggregator_get_execution_task_pool (vagg);
1221 compositor->blend_runner =
1222 gst_parallelized_task_runner_new (n_threads, pool, FALSE);
1223 gst_clear_object (&pool);
1226 return GST_AGGREGATOR_CLASS (parent_class)->negotiated_src_caps (agg, caps);
1230 _should_draw_background (GstVideoAggregator * vagg)
1232 GstVideoRectangle bg_rect;
1233 gboolean draw = TRUE;
1236 bg_rect.x = bg_rect.y = 0;
1238 GST_OBJECT_LOCK (vagg);
1239 bg_rect.w = GST_VIDEO_INFO_WIDTH (&vagg->info);
1240 bg_rect.h = GST_VIDEO_INFO_HEIGHT (&vagg->info);
1241 /* Check if the background is completely obscured by a pad
1242 * TODO: Also skip if it's obscured by a combination of pads */
1243 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
1244 if (gst_aggregator_pad_is_inactive (GST_AGGREGATOR_PAD (l->data))
1246 gst_video_aggregator_pad_get_prepared_frame (GST_VIDEO_AGGREGATOR_PAD
1250 if (_pad_obscures_rectangle (vagg, l->data, bg_rect)) {
1255 GST_OBJECT_UNLOCK (vagg);
1260 frames_can_copy (const GstVideoFrame * frame1, const GstVideoFrame * frame2)
1262 if (GST_VIDEO_FRAME_FORMAT (frame1) != GST_VIDEO_FRAME_FORMAT (frame2))
1264 if (GST_VIDEO_FRAME_HEIGHT (frame1) != GST_VIDEO_FRAME_HEIGHT (frame2))
1266 if (GST_VIDEO_FRAME_WIDTH (frame1) != GST_VIDEO_FRAME_WIDTH (frame2))
1271 struct CompositePadInfo
1273 GstVideoFrame *prepared_frame;
1274 GstCompositorPad *pad;
1275 GstCompositorBlendMode blend_mode;
1278 struct CompositeTask
1280 GstCompositor *compositor;
1281 GstVideoFrame *out_frame;
1282 guint dst_line_start;
1284 gboolean draw_background;
1286 struct CompositePadInfo *pads_info;
1290 _draw_background (GstCompositor * comp, GstVideoFrame * outframe,
1291 guint y_start, guint y_end, BlendFunction * composite)
1293 *composite = comp->blend;
1295 switch (comp->background) {
1296 case COMPOSITOR_BACKGROUND_CHECKER:
1297 comp->fill_checker (outframe, y_start, y_end);
1299 case COMPOSITOR_BACKGROUND_BLACK:
1300 comp->fill_color (outframe, y_start, y_end, 16, 128, 128);
1302 case COMPOSITOR_BACKGROUND_WHITE:
1303 comp->fill_color (outframe, y_start, y_end, 240, 128, 128);
1305 case COMPOSITOR_BACKGROUND_TRANSPARENT:
1307 guint i, plane, num_planes, height;
1309 num_planes = GST_VIDEO_FRAME_N_PLANES (outframe);
1310 for (plane = 0; plane < num_planes; ++plane) {
1311 const GstVideoFormatInfo *info;
1312 gint comp[GST_VIDEO_MAX_COMPONENTS];
1314 gsize rowsize, plane_stride;
1317 info = outframe->info.finfo;
1318 pdata = GST_VIDEO_FRAME_PLANE_DATA (outframe, plane);
1319 plane_stride = GST_VIDEO_FRAME_PLANE_STRIDE (outframe, plane);
1321 gst_video_format_info_component (info, plane, comp);
1322 rowsize = GST_VIDEO_FRAME_COMP_WIDTH (outframe, comp[0])
1323 * GST_VIDEO_FRAME_COMP_PSTRIDE (outframe, comp[0]);
1324 height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (info, comp[0],
1327 yoffset = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (info, comp[0], y_start);
1329 pdata += yoffset * plane_stride;
1330 for (i = 0; i < height; ++i) {
1331 memset (pdata, 0, rowsize);
1332 pdata += plane_stride;
1335 /* use overlay to keep background transparent */
1336 *composite = comp->overlay;
1343 blend_pads (struct CompositeTask *comp)
1345 BlendFunction composite;
1348 composite = comp->compositor->blend;
1350 if (comp->draw_background) {
1351 _draw_background (comp->compositor, comp->out_frame, comp->dst_line_start,
1352 comp->dst_line_end, &composite);
1355 for (i = 0; i < comp->n_pads; i++) {
1356 composite (comp->pads_info[i].prepared_frame,
1357 comp->pads_info[i].pad->xpos + comp->pads_info[i].pad->x_offset,
1358 comp->pads_info[i].pad->ypos + comp->pads_info[i].pad->y_offset,
1359 comp->pads_info[i].pad->alpha, comp->out_frame, comp->dst_line_start,
1360 comp->dst_line_end, comp->pads_info[i].blend_mode);
1364 static GstFlowReturn
1365 gst_compositor_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1367 GstCompositor *compositor = GST_COMPOSITOR (vagg);
1369 GstVideoFrame out_frame, *outframe;
1370 gboolean draw_background;
1371 guint drawn_a_pad = FALSE;
1372 struct CompositePadInfo *pads_info;
1373 guint i, n_pads = 0;
1375 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, GST_MAP_WRITE)) {
1376 GST_WARNING_OBJECT (vagg, "Could not map output buffer");
1377 return GST_FLOW_ERROR;
1380 outframe = &out_frame;
1382 /* If one of the frames to be composited completely obscures the background,
1383 * don't bother drawing the background at all. We can also always use the
1384 * 'blend' BlendFunction in that case because it only changes if we have to
1385 * overlay on top of a transparent background. */
1386 draw_background = _should_draw_background (vagg);
1388 GST_OBJECT_LOCK (vagg);
1389 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
1390 GstVideoAggregatorPad *pad = l->data;
1391 GstVideoFrame *prepared_frame =
1392 gst_video_aggregator_pad_get_prepared_frame (pad);
1398 /* If no prepared frame, we should draw background unconditionally in order
1399 * to clear output buffer */
1401 draw_background = TRUE;
1403 pads_info = g_newa (struct CompositePadInfo, n_pads);
1406 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
1407 GstVideoAggregatorPad *pad = l->data;
1408 GstCompositorPad *compo_pad = GST_COMPOSITOR_PAD (pad);
1409 GstVideoFrame *prepared_frame =
1410 gst_video_aggregator_pad_get_prepared_frame (pad);
1411 GstCompositorBlendMode blend_mode = COMPOSITOR_BLEND_MODE_OVER;
1413 switch (compo_pad->op) {
1414 case COMPOSITOR_OPERATOR_SOURCE:
1415 blend_mode = COMPOSITOR_BLEND_MODE_SOURCE;
1417 case COMPOSITOR_OPERATOR_OVER:
1418 blend_mode = COMPOSITOR_BLEND_MODE_OVER;
1420 case COMPOSITOR_OPERATOR_ADD:
1421 blend_mode = COMPOSITOR_BLEND_MODE_ADD;
1424 g_assert_not_reached ();
1428 if (prepared_frame != NULL) {
1429 /* If this is the first pad we're drawing, and we didn't draw the
1430 * background, and @prepared_frame has the same format, height, and width
1431 * as @outframe, then we can just copy it as-is. Subsequent pads (if any)
1432 * will be composited on top of it. */
1433 if (!drawn_a_pad && !draw_background &&
1434 frames_can_copy (prepared_frame, outframe)) {
1435 gst_video_frame_copy (outframe, prepared_frame);
1437 pads_info[n_pads].pad = compo_pad;
1438 pads_info[n_pads].prepared_frame = prepared_frame;
1439 pads_info[n_pads].blend_mode = blend_mode;
1447 guint n_threads, lines_per_thread;
1449 struct CompositeTask *tasks;
1450 struct CompositeTask **tasks_p;
1452 n_threads = compositor->blend_runner->n_threads;
1454 tasks = g_newa (struct CompositeTask, n_threads);
1455 tasks_p = g_newa (struct CompositeTask *, n_threads);
1457 out_height = GST_VIDEO_FRAME_HEIGHT (outframe);
1458 lines_per_thread = (out_height + n_threads - 1) / n_threads;
1460 for (i = 0; i < n_threads; i++) {
1461 tasks[i].compositor = compositor;
1462 tasks[i].n_pads = n_pads;
1463 tasks[i].pads_info = pads_info;
1464 tasks[i].out_frame = outframe;
1465 tasks[i].draw_background = draw_background;
1466 /* This is a dumb split of the work by number of output lines.
1467 * If there is a section of the output that reads from a lot of source
1468 * pads, then that thread will consume more time. Maybe tracking and
1469 * splitting on the source fill rate would produce better results. */
1470 tasks[i].dst_line_start = i * lines_per_thread;
1471 tasks[i].dst_line_end = MIN ((i + 1) * lines_per_thread, out_height);
1473 tasks_p[i] = &tasks[i];
1476 gst_parallelized_task_runner_run (compositor->blend_runner,
1477 (GstParallelizedTaskFunc) blend_pads, (gpointer *) tasks_p);
1480 GST_OBJECT_UNLOCK (vagg);
1482 gst_video_frame_unmap (outframe);
1488 gst_compositor_request_new_pad (GstElement * element, GstPadTemplate * templ,
1489 const gchar * req_name, const GstCaps * caps)
1494 GST_ELEMENT_CLASS (parent_class)->request_new_pad (element,
1495 templ, req_name, caps);
1498 goto could_not_create;
1500 gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (newpad),
1501 GST_OBJECT_NAME (newpad));
1507 GST_DEBUG_OBJECT (element, "could not create/add pad");
1513 gst_compositor_release_pad (GstElement * element, GstPad * pad)
1515 GstCompositor *compositor;
1517 compositor = GST_COMPOSITOR (element);
1519 GST_DEBUG_OBJECT (compositor, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1521 gst_child_proxy_child_removed (GST_CHILD_PROXY (compositor), G_OBJECT (pad),
1522 GST_OBJECT_NAME (pad));
1524 GST_ELEMENT_CLASS (parent_class)->release_pad (element, pad);
1528 src_pad_mouse_event (GstElement * element, GstPad * pad, gpointer user_data)
1530 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR_CAST (element);
1531 GstCompositor *comp = GST_COMPOSITOR (element);
1532 GstCompositorPad *cpad = GST_COMPOSITOR_PAD (pad);
1534 gst_structure_copy (gst_event_get_structure (GST_EVENT_CAST (user_data)));
1535 gdouble event_x, event_y;
1536 gint offset_x, offset_y;
1537 GstVideoRectangle rect;
1539 gst_structure_get (st, "pointer_x", G_TYPE_DOUBLE, &event_x,
1540 "pointer_y", G_TYPE_DOUBLE, &event_y, NULL);
1542 /* Find output rectangle of this pad */
1543 _mixer_pad_get_output_size (comp, cpad,
1544 GST_VIDEO_INFO_PAR_N (&vagg->info),
1545 GST_VIDEO_INFO_PAR_D (&vagg->info),
1546 &(rect.w), &(rect.h), &offset_x, &offset_y);
1547 rect.x = cpad->xpos + offset_x;
1548 rect.y = cpad->ypos + offset_y;
1550 /* Translate coordinates and send event if it lies in this rectangle */
1551 if (is_point_contained (rect, event_x, event_y)) {
1552 GstVideoAggregatorPad *vpad = GST_VIDEO_AGGREGATOR_PAD_CAST (cpad);
1555 w = (gdouble) GST_VIDEO_INFO_WIDTH (&vpad->info);
1556 h = (gdouble) GST_VIDEO_INFO_HEIGHT (&vpad->info);
1557 x = (event_x - (gdouble) rect.x) * (w / (gdouble) rect.w);
1558 y = (event_y - (gdouble) rect.y) * (h / (gdouble) rect.h);
1560 gst_structure_set (st, "pointer_x", G_TYPE_DOUBLE, x,
1561 "pointer_y", G_TYPE_DOUBLE, y, NULL);
1562 gst_pad_push_event (pad, gst_event_new_navigation (st));
1564 gst_structure_free (st);
1571 _src_event (GstAggregator * agg, GstEvent * event)
1573 GstNavigationEventType event_type;
1575 switch (GST_EVENT_TYPE (event)) {
1576 case GST_EVENT_NAVIGATION:
1578 event_type = gst_navigation_event_get_type (event);
1579 switch (event_type) {
1580 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
1581 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:
1582 case GST_NAVIGATION_EVENT_MOUSE_MOVE:
1583 case GST_NAVIGATION_EVENT_MOUSE_SCROLL:
1584 gst_element_foreach_sink_pad (GST_ELEMENT_CAST (agg),
1585 src_pad_mouse_event, event);
1586 gst_event_unref (event);
1597 return GST_AGGREGATOR_CLASS (parent_class)->src_event (agg, event);
1601 _sink_query (GstAggregator * agg, GstAggregatorPad * bpad, GstQuery * query)
1603 switch (GST_QUERY_TYPE (query)) {
1604 case GST_QUERY_ALLOCATION:{
1607 GstBufferPool *pool;
1609 GstStructure *structure;
1611 gst_query_parse_allocation (query, &caps, NULL);
1616 if (!gst_video_info_from_caps (&info, caps))
1619 size = GST_VIDEO_INFO_SIZE (&info);
1621 pool = gst_video_buffer_pool_new ();
1623 structure = gst_buffer_pool_get_config (pool);
1624 gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
1626 if (!gst_buffer_pool_set_config (pool, structure)) {
1627 gst_object_unref (pool);
1631 gst_query_add_allocation_pool (query, pool, size, 0, 0);
1632 gst_object_unref (pool);
1633 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1638 return GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
1643 gst_compositor_finalize (GObject * object)
1645 GstCompositor *compositor = GST_COMPOSITOR (object);
1647 if (compositor->blend_runner)
1648 gst_parallelized_task_runner_free (compositor->blend_runner);
1649 compositor->blend_runner = NULL;
1651 G_OBJECT_CLASS (parent_class)->finalize (object);
1654 /* GObject boilerplate */
1656 gst_compositor_class_init (GstCompositorClass * klass)
1658 GObjectClass *gobject_class = (GObjectClass *) klass;
1659 GstElementClass *gstelement_class = (GstElementClass *) klass;
1660 GstVideoAggregatorClass *videoaggregator_class =
1661 (GstVideoAggregatorClass *) klass;
1662 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
1664 gobject_class->get_property = gst_compositor_get_property;
1665 gobject_class->set_property = gst_compositor_set_property;
1666 gobject_class->finalize = gst_compositor_finalize;
1668 gstelement_class->request_new_pad =
1669 GST_DEBUG_FUNCPTR (gst_compositor_request_new_pad);
1670 gstelement_class->release_pad =
1671 GST_DEBUG_FUNCPTR (gst_compositor_release_pad);
1672 agg_class->sink_query = _sink_query;
1673 agg_class->src_event = _src_event;
1674 agg_class->fixate_src_caps = _fixate_caps;
1675 agg_class->negotiated_src_caps = _negotiated_caps;
1676 videoaggregator_class->aggregate_frames = gst_compositor_aggregate_frames;
1678 g_object_class_install_property (gobject_class, PROP_BACKGROUND,
1679 g_param_spec_enum ("background", "Background", "Background type",
1680 GST_TYPE_COMPOSITOR_BACKGROUND,
1681 DEFAULT_BACKGROUND, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1684 * compositor:zero-size-is-unscaled:
1686 * Whether a pad with height or width 0 should be left unscaled
1687 * in that dimension, or simply not composited in. Setting it to
1688 * %FALSE might be useful when animating those properties.
1692 g_object_class_install_property (gobject_class, PROP_ZERO_SIZE_IS_UNSCALED,
1693 g_param_spec_boolean ("zero-size-is-unscaled", "Zero size is unscaled",
1694 "If TRUE, then input video is unscaled in that dimension "
1695 "if width or height is 0 (for backwards compatibility)",
1696 DEFAULT_ZERO_SIZE_IS_UNSCALED,
1697 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1700 * compositor:max-threads:
1702 * Maximum number of blending/rendering worker threads to spawn (0 = auto)
1706 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
1707 g_param_spec_uint ("max-threads", "Max Threads",
1708 "Maximum number of blending/rendering worker threads to spawn "
1709 "(0 = auto)", 0, G_MAXINT, DEFAULT_MAX_THREADS,
1710 GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
1711 G_PARAM_STATIC_STRINGS));
1713 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
1714 &src_factory, GST_TYPE_AGGREGATOR_PAD);
1715 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
1716 &sink_factory, GST_TYPE_COMPOSITOR_PAD);
1718 gst_element_class_set_static_metadata (gstelement_class, "Compositor",
1719 "Filter/Editor/Video/Compositor",
1720 "Composite multiple video streams", "Wim Taymans <wim@fluendo.com>, "
1721 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1724 * compositor:ignore-inactive-pads:
1726 * Don't wait for inactive pads when live. An inactive pad
1727 * is a pad that hasn't yet received a buffer, but that has
1728 * been waited on at least once.
1730 * The purpose of this property is to avoid aggregating on
1731 * timeout when new pads are requested in advance of receiving
1732 * data flow, for example the user may decide to connect it later,
1733 * but wants to configure it already.
1737 g_object_class_install_property (gobject_class,
1738 PROP_IGNORE_INACTIVE_PADS, g_param_spec_boolean ("ignore-inactive-pads",
1739 "Ignore inactive pads",
1740 "Avoid timing out waiting for inactive pads", FALSE,
1741 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1743 gst_type_mark_as_plugin_api (GST_TYPE_COMPOSITOR_PAD, 0);
1744 gst_type_mark_as_plugin_api (GST_TYPE_COMPOSITOR_OPERATOR, 0);
1745 gst_type_mark_as_plugin_api (GST_TYPE_COMPOSITOR_BACKGROUND, 0);
1749 gst_compositor_init (GstCompositor * self)
1751 /* initialize variables */
1752 self->background = DEFAULT_BACKGROUND;
1753 self->zero_size_is_unscaled = DEFAULT_ZERO_SIZE_IS_UNSCALED;
1754 self->max_threads = DEFAULT_MAX_THREADS;
1757 /* GstChildProxy implementation */
1759 gst_compositor_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
1762 GstCompositor *compositor = GST_COMPOSITOR (child_proxy);
1763 GObject *obj = NULL;
1765 GST_OBJECT_LOCK (compositor);
1766 obj = g_list_nth_data (GST_ELEMENT_CAST (compositor)->sinkpads, index);
1768 gst_object_ref (obj);
1769 GST_OBJECT_UNLOCK (compositor);
1775 gst_compositor_child_proxy_get_children_count (GstChildProxy * child_proxy)
1778 GstCompositor *compositor = GST_COMPOSITOR (child_proxy);
1780 GST_OBJECT_LOCK (compositor);
1781 count = GST_ELEMENT_CAST (compositor)->numsinkpads;
1782 GST_OBJECT_UNLOCK (compositor);
1783 GST_INFO_OBJECT (compositor, "Children Count: %d", count);
1789 gst_compositor_child_proxy_init (gpointer g_iface, gpointer iface_data)
1791 GstChildProxyInterface *iface = g_iface;
1793 iface->get_child_by_index = gst_compositor_child_proxy_get_child_by_index;
1794 iface->get_children_count = gst_compositor_child_proxy_get_children_count;
1797 /* Element registration */
1799 plugin_init (GstPlugin * plugin)
1801 GST_DEBUG_CATEGORY_INIT (gst_compositor_debug, "compositor", 0, "compositor");
1803 gst_compositor_init_blend ();
1805 return GST_ELEMENT_REGISTER (compositor, plugin);
1808 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1811 "Compositor", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,