2 * Copyright (C) 2022 Intel Corporation
3 * Author: U. Artie Eoff <ullysses.a.eoff@intel.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-vacompositor
23 * @title: vacompositor
24 * @short_description: A VA-API based video compositing element
26 * A video compositing element that uses VA-API VPP to accelerate the compose,
27 * blending, and scaling of multiple inputs into one output.
29 * ## Example launch line
31 * gst-launch-1.0 videotestsrc \
32 * ! "video/x-raw,format=(string)NV12,width=640,height=480" \
33 * ! tee name=testsrc ! queue ! vacompositor name=comp \
34 * sink_1::width=160 sink_1::height=120 sink_1::xpos=480 \
35 * sink_1::ypos=360 sink_1::alpha=0.75 \
36 * ! autovideosink testsrc. ! queue ! comp.
47 #include "gstvacompositor.h"
49 #include <gst/va/gstva.h>
50 #include <gst/video/video.h>
51 #include <va/va_drmcommon.h>
53 #include "gstvacaps.h"
54 #include "gstvadisplay_priv.h"
55 #include "gstvafilter.h"
57 GST_DEBUG_CATEGORY_STATIC (gst_va_compositor_debug);
58 #define GST_CAT_DEFAULT gst_va_compositor_debug
60 struct _GstVaCompositorPad
62 GstVideoAggregatorPad parent;
83 #define DEFAULT_PAD_XPOS 0
84 #define DEFAULT_PAD_YPOS 0
85 #define DEFAULT_PAD_WIDTH 0
86 #define DEFAULT_PAD_HEIGHT 0
87 #define DEFAULT_PAD_ALPHA 1.0
89 G_DEFINE_TYPE (GstVaCompositorPad, gst_va_compositor_pad,
90 GST_TYPE_VIDEO_AGGREGATOR_PAD);
93 gst_va_compositor_pad_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec)
96 GstVaCompositorPad *const self = GST_VA_COMPOSITOR_PAD (object);
100 g_value_set_int (value, self->xpos);
103 g_value_set_int (value, self->ypos);
106 g_value_set_int (value, self->width);
108 case PROP_PAD_HEIGHT:
109 g_value_set_int (value, self->height);
112 g_value_set_double (value, self->alpha);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 gst_va_compositor_pad_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec)
124 GstVaCompositorPad *const self = GST_VA_COMPOSITOR_PAD (object);
126 GST_OBJECT_LOCK (object);
129 self->xpos = g_value_get_int (value);
132 self->ypos = g_value_get_int (value);
135 self->width = g_value_get_int (value);
137 case PROP_PAD_HEIGHT:
138 self->height = g_value_get_int (value);
141 self->alpha = g_value_get_double (value);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 GST_OBJECT_UNLOCK (object);
151 gst_va_compositor_pad_finalize (GObject * object)
153 GstVaCompositorPad *const self = GST_VA_COMPOSITOR_PAD (object);
156 gst_buffer_pool_set_active (self->pool, FALSE);
157 gst_clear_object (&self->pool);
160 G_OBJECT_CLASS (gst_va_compositor_pad_parent_class)->finalize (object);
164 gst_va_compositor_pad_init (GstVaCompositorPad * self)
167 self->xpos = DEFAULT_PAD_XPOS;
168 self->ypos = DEFAULT_PAD_YPOS;
169 self->width = DEFAULT_PAD_WIDTH;
170 self->height = DEFAULT_PAD_HEIGHT;
171 self->alpha = DEFAULT_PAD_ALPHA;
175 gst_va_compositor_pad_class_init (GstVaCompositorPadClass * klass)
177 GObjectClass *const gobject_class = G_OBJECT_CLASS (klass);
178 GstVideoAggregatorPadClass *const vaggpad_class =
179 GST_VIDEO_AGGREGATOR_PAD_CLASS (klass);
181 gobject_class->finalize = gst_va_compositor_pad_finalize;
182 gobject_class->get_property = gst_va_compositor_pad_get_property;
183 gobject_class->set_property = gst_va_compositor_pad_set_property;
185 g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
186 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
187 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
188 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
190 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
191 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
192 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_PAD_WIDTH,
194 g_param_spec_int ("width", "Width",
195 "Width of the picture (0, to use the width of the input frame)",
196 0, G_MAXINT, DEFAULT_PAD_WIDTH,
197 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_PAD_HEIGHT,
199 g_param_spec_int ("height", "Height",
200 "Height of the picture (0, to use the height of the input frame)",
201 0, G_MAXINT, DEFAULT_PAD_HEIGHT,
202 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
204 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
206 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
208 /* Don't use mapped video frames. Handle video buffers directly */
209 vaggpad_class->prepare_frame = NULL;
210 vaggpad_class->clean_frame = NULL;
213 #define GST_VA_COMPOSITOR(obj) ((GstVaCompositor *) obj)
214 #define GST_VA_COMPOSITOR_CLASS(klass) ((GstVaCompositorClass *) klass)
215 #define GST_VA_COMPOSITOR_GET_CLASS(obj) \
216 (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaCompositorClass))
218 typedef struct _GstVaCompositor GstVaCompositor;
219 typedef struct _GstVaCompositorClass GstVaCompositorClass;
221 struct _GstVaCompositorClass
223 GstVideoAggregatorClass parent_class;
226 gchar *render_device_path;
229 struct _GstVaCompositor
231 GstVideoAggregator parent;
233 GstVaDisplay *display;
236 GstVideoInfo other_info; /* downstream info */
237 GstBufferPool *other_pool; /* downstream pool */
239 guint32 scale_method;
244 gchar *render_device_path;
250 PROP_DEVICE_PATH = 1,
255 static GParamSpec *properties[N_PROPERTIES];
256 static GstElementClass *parent_class = NULL;
259 gst_va_compositor_set_property (GObject * object, guint prop_id,
260 const GValue * value, GParamSpec * pspec)
262 GstVaCompositor *const self = GST_VA_COMPOSITOR (object);
265 case PROP_SCALE_METHOD:
267 GST_OBJECT_LOCK (object);
268 self->scale_method = g_value_get_enum (value);
269 GST_OBJECT_UNLOCK (object);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_va_compositor_get_property (GObject * object, guint prop_id,
279 GValue * value, GParamSpec * pspec)
281 GstVaCompositor *const self = GST_VA_COMPOSITOR (object);
284 case PROP_DEVICE_PATH:
286 if (!(self->display && GST_IS_VA_DISPLAY_DRM (self->display))) {
287 g_value_set_string (value, NULL);
290 g_object_get_property (G_OBJECT (self->display), "path", value);
293 case PROP_SCALE_METHOD:
295 GST_OBJECT_LOCK (object);
296 g_value_set_enum (value, self->scale_method);
297 GST_OBJECT_UNLOCK (object);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gst_va_compositor_start (GstAggregator * agg)
308 GstElement *const element = GST_ELEMENT (agg);
309 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
310 GstVaCompositorClass *const klass = GST_VA_COMPOSITOR_GET_CLASS (agg);
312 if (!gst_va_ensure_element_data (element, klass->render_device_path,
315 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DEVICE_PATH]);
317 self->filter = gst_va_filter_new (self->display);
318 if (!gst_va_filter_open (self->filter))
321 return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
325 gst_va_compositor_stop (GstAggregator * agg)
327 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
329 gst_va_filter_close (self->filter);
330 gst_clear_object (&self->filter);
331 gst_clear_object (&self->display);
332 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DEVICE_PATH]);
334 return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);
338 gst_va_compositor_dispose (GObject * object)
340 GstVaCompositor *const self = GST_VA_COMPOSITOR (object);
342 if (self->other_pool) {
343 gst_buffer_pool_set_active (self->other_pool, FALSE);
344 gst_clear_object (&self->other_pool);
347 gst_clear_object (&self->display);
349 G_OBJECT_CLASS (parent_class)->dispose (object);
353 gst_va_compositor_request_new_pad (GstElement * element, GstPadTemplate * templ,
354 const gchar * req_name, const GstCaps * caps)
356 GstPad *newpad = GST_PAD (GST_ELEMENT_CLASS
357 (parent_class)->request_new_pad (element, templ, req_name, caps));
360 GST_DEBUG_OBJECT (element, "could not create/add pad");
362 gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (newpad),
363 GST_OBJECT_NAME (newpad));
369 gst_va_compositor_release_pad (GstElement * element, GstPad * pad)
371 GstVaCompositor *const self = GST_VA_COMPOSITOR (element);
373 gst_child_proxy_child_removed (GST_CHILD_PROXY (self), G_OBJECT (pad),
374 GST_OBJECT_NAME (pad));
376 GST_ELEMENT_CLASS (parent_class)->release_pad (element, pad);
380 gst_va_compositor_set_context (GstElement * element, GstContext * context)
382 GstVaDisplay *old_display, *new_display;
383 GstVaCompositor *const self = GST_VA_COMPOSITOR (element);
384 GstVaCompositorClass *const klass = GST_VA_COMPOSITOR_GET_CLASS (self);
387 old_display = self->display ? gst_object_ref (self->display) : NULL;
388 ret = gst_va_handle_set_context (element, context, klass->render_device_path,
390 new_display = self->display ? gst_object_ref (self->display) : NULL;
393 || (old_display && new_display && old_display != new_display
395 GST_ELEMENT_WARNING (element, RESOURCE, BUSY,
396 ("Can't replace VA display while operating"), (NULL));
399 gst_clear_object (&old_display);
400 gst_clear_object (&new_display);
402 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
406 _handle_context_query (GstVaCompositor * const self, GstQuery * query)
408 GstVaDisplay *display = NULL;
409 gboolean ret = FALSE;
411 gst_object_replace ((GstObject **) & display, (GstObject *) self->display);
412 ret = gst_va_handle_context_query (GST_ELEMENT_CAST (self), query, display);
413 gst_clear_object (&display);
419 gst_va_compositor_sink_getcaps (GstPad * pad, GstCaps * filter)
422 GstCaps *template_caps;
423 GstCaps *filtered_caps;
424 GstCaps *returned_caps;
426 template_caps = gst_pad_get_pad_template_caps (pad);
428 sinkcaps = gst_pad_get_current_caps (pad);
430 sinkcaps = gst_caps_ref (template_caps);
432 sinkcaps = gst_caps_merge (sinkcaps, gst_caps_ref (template_caps));
436 filtered_caps = gst_caps_intersect (sinkcaps, filter);
437 gst_caps_unref (sinkcaps);
439 filtered_caps = sinkcaps;
442 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
444 gst_caps_unref (template_caps);
445 gst_caps_unref (filtered_caps);
447 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
449 return returned_caps;
453 gst_va_compositor_sink_acceptcaps (GstPad * pad, GstCaps * caps)
456 GstCaps *template_caps;
458 template_caps = gst_pad_get_pad_template_caps (pad);
459 template_caps = gst_caps_make_writable (template_caps);
461 ret = gst_caps_can_intersect (caps, template_caps);
462 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
463 (ret ? "" : "not "), caps);
464 gst_caps_unref (template_caps);
470 gst_va_compositor_sink_query (GstAggregator * agg, GstAggregatorPad * pad,
473 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
475 switch (GST_QUERY_TYPE (query)) {
476 case GST_QUERY_CONTEXT:
478 if (_handle_context_query (self, query))
484 GstCaps *filter, *caps;
486 gst_query_parse_caps (query, &filter);
487 caps = gst_va_compositor_sink_getcaps (GST_PAD (pad), filter);
488 gst_query_set_caps_result (query, caps);
489 gst_caps_unref (caps);
492 case GST_QUERY_ACCEPT_CAPS:
497 gst_query_parse_accept_caps (query, &caps);
498 ret = gst_va_compositor_sink_acceptcaps (GST_PAD (pad), caps);
499 gst_query_set_accept_caps_result (query, ret);
506 return GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, pad, query);
510 gst_va_compositor_src_query (GstAggregator * agg, GstQuery * query)
512 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
514 switch (GST_QUERY_TYPE (query)) {
515 case GST_QUERY_CONTEXT:
516 if (_handle_context_query (self, query))
523 return GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
526 static GstAllocator *
527 gst_va_compositor_allocator_from_caps (GstVaCompositor * const self,
530 GstAllocator *allocator = NULL;
532 if (gst_caps_is_dmabuf (caps)) {
533 allocator = gst_va_dmabuf_allocator_new (self->display);
535 GArray *surface_formats = gst_va_filter_get_surface_formats (self->filter);
536 allocator = gst_va_allocator_new (self->display, surface_formats);
542 /* Answer upstream allocation query. */
544 gst_va_compositor_propose_allocation (GstAggregator * agg,
545 GstAggregatorPad * aggpad, GstQuery * decide_query, GstQuery * query)
547 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
548 GstAllocator *allocator = NULL;
549 GstAllocationParams params = { 0, };
553 gboolean update_allocator = FALSE;
554 guint size, usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC;
556 gst_query_parse_allocation (query, &caps, NULL);
561 if (!gst_video_info_from_caps (&info, caps))
564 if (gst_query_get_n_allocation_pools (query) > 0)
567 size = GST_VIDEO_INFO_SIZE (&info);
569 if (gst_query_get_n_allocation_params (query) > 0) {
570 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
571 if (!GST_IS_VA_DMABUF_ALLOCATOR (allocator)
572 && !GST_IS_VA_ALLOCATOR (allocator))
573 gst_clear_object (&allocator);
574 update_allocator = TRUE;
576 gst_allocation_params_init (¶ms);
580 if (!(allocator = gst_va_compositor_allocator_from_caps (self, caps)))
584 /* Now we have a VA-based allocator */
586 pool = gst_va_pool_new_with_config (caps, size, 1, 0, usage_hint,
587 GST_VA_FEATURE_AUTO, allocator, ¶ms);
589 gst_object_unref (allocator);
593 if (update_allocator)
594 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
596 gst_query_add_allocation_param (query, allocator, ¶ms);
598 gst_query_add_allocation_pool (query, pool, size, 1, 0);
600 GST_DEBUG_OBJECT (self,
601 "proposing %" GST_PTR_FORMAT " with allocator %" GST_PTR_FORMAT,
604 gst_object_unref (allocator);
605 gst_object_unref (pool);
607 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
613 GST_ERROR_OBJECT (self, "failed to set config");
618 static GstBufferPool *
619 _create_other_pool (GstAllocator * allocator, GstAllocationParams * params,
620 GstCaps * caps, guint size)
622 GstBufferPool *pool = NULL;
623 GstStructure *config;
625 pool = gst_video_buffer_pool_new ();
626 config = gst_buffer_pool_get_config (pool);
628 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
629 gst_buffer_pool_config_set_allocator (config, allocator, params);
630 if (!gst_buffer_pool_set_config (pool, config)) {
631 gst_clear_object (&pool);
637 /* configure the allocation query that was answered downstream */
639 gst_va_compositor_decide_allocation (GstAggregator * agg, GstQuery * query)
641 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
642 GstVideoAggregator *const vagg = GST_VIDEO_AGGREGATOR (agg);
644 GstAllocator *allocator = NULL, *other_allocator = NULL;
645 GstAllocationParams params, other_params;
646 GstBufferPool *pool = NULL, *other_pool = NULL;
647 GstCaps *caps = NULL;
648 GstStructure *config;
650 guint min, max, size = 0, usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE;
651 gboolean update_pool, update_allocator, has_videometa, copy_frames;
653 gst_query_parse_allocation (query, &caps, NULL);
655 gst_allocation_params_init (&other_params);
656 gst_allocation_params_init (¶ms);
658 if (!gst_video_info_from_caps (&info, caps)) {
659 GST_ERROR_OBJECT (self, "Cannot parse caps %" GST_PTR_FORMAT, caps);
663 if (gst_query_get_n_allocation_params (query) > 0) {
664 gst_query_parse_nth_allocation_param (query, 0, &allocator, &other_params);
665 if (allocator && !(GST_IS_VA_DMABUF_ALLOCATOR (allocator)
666 || GST_IS_VA_ALLOCATOR (allocator))) {
667 /* save the allocator for the other pool */
668 other_allocator = allocator;
671 update_allocator = TRUE;
673 update_allocator = FALSE;
676 if (gst_query_get_n_allocation_pools (query) > 0) {
677 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
680 if (!GST_IS_VA_POOL (pool)) {
681 GST_DEBUG_OBJECT (self,
682 "may need other pool for copy frames %" GST_PTR_FORMAT, pool);
690 size = GST_VIDEO_INFO_SIZE (&info);
697 if (gst_caps_is_dmabuf (caps) && GST_VIDEO_INFO_IS_RGB (&info))
698 usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC;
699 if (!(allocator = gst_va_compositor_allocator_from_caps (self, caps)))
704 pool = gst_va_pool_new ();
706 config = gst_buffer_pool_get_config (pool);
707 gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
708 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
709 gst_buffer_pool_config_set_params (config, caps, size, min, max);
710 gst_buffer_pool_config_set_va_allocation_params (config, usage_hint,
711 GST_VA_FEATURE_AUTO);
712 if (!gst_buffer_pool_set_config (pool, config)) {
713 gst_object_unref (allocator);
714 gst_object_unref (pool);
718 if (GST_IS_VA_DMABUF_ALLOCATOR (allocator)) {
719 gst_va_dmabuf_allocator_get_format (allocator, &vagg->info, NULL);
720 } else if (GST_IS_VA_ALLOCATOR (allocator)) {
721 gst_va_allocator_get_format (allocator, &vagg->info, NULL, NULL);
724 if (update_allocator)
725 gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms);
727 gst_query_add_allocation_param (query, allocator, ¶ms);
730 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
732 gst_query_add_allocation_pool (query, pool, size, min, max);
734 has_videometa = gst_query_find_allocation_meta (query,
735 GST_VIDEO_META_API_TYPE, NULL);
737 copy_frames = (!has_videometa && gst_va_pool_requires_video_meta (pool)
738 && gst_caps_is_raw (caps));
741 gst_object_replace ((GstObject **) & self->other_pool,
742 (GstObject *) other_pool);
745 _create_other_pool (other_allocator, &other_params, caps, size);
747 GST_DEBUG_OBJECT (self, "Use the other pool for copy %" GST_PTR_FORMAT,
750 gst_clear_object (&self->other_pool);
753 GST_DEBUG_OBJECT (self,
754 "decided pool %" GST_PTR_FORMAT " with allocator %" GST_PTR_FORMAT,
757 gst_object_unref (allocator);
758 gst_object_unref (pool);
759 gst_clear_object (&other_allocator);
760 gst_clear_object (&other_pool);
765 static GstBufferPool *
766 _get_sinkpad_pool (GstVaCompositor * const self, GstVaCompositorPad * const pad)
768 GstAllocator *allocator;
769 GstAllocationParams params = { 0, };
772 guint size, usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_VPP_READ;
777 gst_allocation_params_init (¶ms);
779 caps = gst_pad_get_current_caps (GST_PAD (pad));
780 gst_video_info_from_caps (&info, caps);
782 size = GST_VIDEO_INFO_SIZE (&info);
784 allocator = gst_va_compositor_allocator_from_caps (self, caps);
785 pad->pool = gst_va_pool_new_with_config (caps, size, 1, 0, usage_hint,
786 GST_VA_FEATURE_AUTO, allocator, ¶ms);
787 gst_caps_unref (caps);
790 gst_object_unref (allocator);
794 if (GST_IS_VA_DMABUF_ALLOCATOR (allocator)) {
795 gst_va_dmabuf_allocator_get_format (allocator, &info, NULL);
796 } else if (GST_IS_VA_ALLOCATOR (allocator)) {
797 gst_va_allocator_get_format (allocator, &info, NULL, NULL);
800 gst_object_unref (allocator);
802 if (!gst_buffer_pool_set_active (pad->pool, TRUE)) {
803 GST_WARNING_OBJECT (self, "failed to active the sinkpad pool %"
804 GST_PTR_FORMAT, pad->pool);
812 _get_plane_data_size (GstVideoInfo * info, guint plane)
814 gint comp[GST_VIDEO_MAX_COMPONENTS];
815 gint height, padded_height;
817 gst_video_format_info_component (info->finfo, plane, comp);
819 height = GST_VIDEO_INFO_HEIGHT (info);
821 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (info->finfo, comp[0], height);
823 return GST_VIDEO_INFO_PLANE_STRIDE (info, plane) * padded_height;
827 _try_import_dmabuf_unlocked (GstVaCompositor * const self, GstVideoInfo * info,
831 GstMemory *mems[GST_VIDEO_MAX_PLANES];
832 guint i, n_mem, n_planes;
833 gsize offset[GST_VIDEO_MAX_PLANES];
834 uintptr_t fd[GST_VIDEO_MAX_PLANES];
836 n_planes = GST_VIDEO_INFO_N_PLANES (info);
837 n_mem = gst_buffer_n_memory (inbuf);
838 meta = gst_buffer_get_video_meta (inbuf);
840 /* This will eliminate most non-dmabuf out there */
841 if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
844 /* We cannot have multiple dmabuf per plane */
845 if (n_mem > n_planes)
848 /* Update video info based on video meta */
850 GST_VIDEO_INFO_WIDTH (info) = meta->width;
851 GST_VIDEO_INFO_HEIGHT (info) = meta->height;
853 for (i = 0; i < meta->n_planes; i++) {
854 GST_VIDEO_INFO_PLANE_OFFSET (info, i) = meta->offset[i];
855 GST_VIDEO_INFO_PLANE_STRIDE (info, i) = meta->stride[i];
859 /* Find and validate all memories */
860 for (i = 0; i < n_planes; i++) {
866 plane_size = _get_plane_data_size (info, i);
868 if (!gst_buffer_find_memory (inbuf, info->offset[i], plane_size,
869 &mem_idx, &length, &mem_skip))
872 /* We can't have more then one dmabuf per plane */
876 mems[i] = gst_buffer_peek_memory (inbuf, mem_idx);
878 /* And all memory found must be dmabuf */
879 if (!gst_is_dmabuf_memory (mems[i]))
882 offset[i] = mems[i]->offset + mem_skip;
883 fd[i] = gst_dmabuf_memory_get_fd (mems[i]);
886 /* Now create a VASurfaceID for the buffer */
887 return gst_va_dmabuf_memories_setup (self->display, info, n_planes,
888 mems, fd, offset, VA_SURFACE_ATTRIB_USAGE_HINT_VPP_READ);
891 extern GRecMutex GST_VA_SHARED_LOCK;
894 _try_import_buffer (GstVaCompositor * const self,
895 GstVaCompositorPad * const pad, GstBuffer * inbuf)
902 surface = gst_va_buffer_get_surface (inbuf);
903 if (surface != VA_INVALID_ID)
906 caps = gst_pad_get_current_caps (GST_PAD (pad));
907 gst_video_info_from_caps (&info, caps);
908 gst_caps_unref (caps);
910 g_rec_mutex_lock (&GST_VA_SHARED_LOCK);
911 ret = _try_import_dmabuf_unlocked (self, &info, inbuf);
912 g_rec_mutex_unlock (&GST_VA_SHARED_LOCK);
918 gst_va_compositor_import_buffer (GstVaCompositor * const self,
919 GstVaCompositorPad * const pad, GstBuffer * inbuf, GstBuffer ** buf)
921 GstBuffer *buffer = NULL;
926 GstVideoFrame in_frame, out_frame;
927 gboolean imported, copied;
929 imported = _try_import_buffer (self, pad, inbuf);
931 *buf = gst_buffer_ref (inbuf);
935 /* input buffer doesn't come from a vapool, thus it is required to
936 * have a pool, grab from it a new buffer and copy the input
937 * buffer to the new one */
938 if (!(pool = _get_sinkpad_pool (self, pad)))
939 return GST_FLOW_ERROR;
941 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
942 if (ret != GST_FLOW_OK)
945 GST_LOG_OBJECT (self, "copying input frame");
947 caps = gst_pad_get_current_caps (GST_PAD (pad));
948 gst_video_info_from_caps (&info, caps);
949 gst_caps_unref (caps);
951 if (!gst_video_frame_map (&in_frame, &info, inbuf, GST_MAP_READ))
954 if (!gst_video_frame_map (&out_frame, &info, buffer, GST_MAP_WRITE)) {
955 gst_video_frame_unmap (&in_frame);
959 copied = gst_video_frame_copy (&out_frame, &in_frame);
961 gst_video_frame_unmap (&out_frame);
962 gst_video_frame_unmap (&in_frame);
973 GST_ELEMENT_WARNING (self, CORE, NOT_IMPLEMENTED, (NULL),
974 ("invalid video buffer received"));
976 gst_buffer_unref (buffer);
981 typedef struct _GstVaCompositorSampleGenerator GstVaCompositorSampleGenerator;
982 struct _GstVaCompositorSampleGenerator
984 GstVaCompositor *comp;
986 GstVaComposeSample sample;
989 static GstVaComposeSample *
990 gst_va_compositor_sample_next (gpointer data)
992 GstVaCompositorSampleGenerator *generator;
993 GstVideoAggregatorPad *vaggpad;
994 GstVaCompositorPad *pad;
998 GstVideoCropMeta *crop = NULL;
1000 generator = (GstVaCompositorSampleGenerator *) data;
1002 /* at the end of the generator? */
1003 while (generator->current) {
1004 /* get the current sinkpad for processing */
1005 vaggpad = GST_VIDEO_AGGREGATOR_PAD (generator->current->data);
1007 /* increment to next sinkpad */
1008 generator->current = generator->current->next;
1012 generator->sample = (GstVaComposeSample) { 0, };
1015 /* current sinkpad may not be queueing buffers yet (e.g. timestamp-offset)
1016 * or it may have reached EOS */
1017 if (!gst_video_aggregator_pad_has_current_buffer (vaggpad))
1020 inbuf = gst_video_aggregator_pad_get_current_buffer (vaggpad);
1021 pad = GST_VA_COMPOSITOR_PAD (vaggpad);
1023 res = gst_va_compositor_import_buffer (generator->comp, pad, inbuf, &buf);
1024 if (res != GST_FLOW_OK)
1025 return &generator->sample;
1027 crop = gst_buffer_get_video_crop_meta (buf);
1029 GST_OBJECT_LOCK (vaggpad);
1031 generator->sample = (GstVaComposeSample) {
1033 .input_region = (VARectangle) {
1034 .x = crop ? crop->x : 0,
1035 .y = crop ? crop->y : 0,
1036 .width = crop ? crop->width : GST_VIDEO_INFO_WIDTH (&vaggpad->info),
1037 .height = crop ? crop->height : GST_VIDEO_INFO_HEIGHT (&vaggpad->info),
1039 .output_region = (VARectangle) {
1042 .width = (pad->width == DEFAULT_PAD_WIDTH)
1043 ? GST_VIDEO_INFO_WIDTH (&vaggpad->info) : pad->width,
1044 .height = (pad->height == DEFAULT_PAD_HEIGHT)
1045 ? GST_VIDEO_INFO_HEIGHT (&vaggpad->info) : pad->height,
1047 .flags = generator->comp->scale_method,
1048 .alpha = pad->alpha,
1051 GST_OBJECT_UNLOCK (vaggpad);
1053 return &generator->sample;
1060 gst_va_compositor_copy_output_buffer (GstVaCompositor * const self,
1061 GstBuffer * src_buf, GstBuffer * dst_buf)
1063 GstVideoAggregator *const vagg = GST_VIDEO_AGGREGATOR (self);
1064 GstVideoFrame src_frame, dst_frame;
1066 GST_LOG_OBJECT (self, "copying output buffer");
1068 if (!gst_video_frame_map (&src_frame, &vagg->info, src_buf, GST_MAP_READ)) {
1069 GST_ERROR_OBJECT (self, "couldn't map source buffer");
1073 if (!gst_video_frame_map (&dst_frame, &self->other_info, dst_buf,
1075 GST_ERROR_OBJECT (self, "couldn't map output buffer");
1076 gst_video_frame_unmap (&src_frame);
1080 if (!gst_video_frame_copy (&dst_frame, &src_frame)) {
1081 GST_ERROR_OBJECT (self, "couldn't copy output buffer");
1082 gst_video_frame_unmap (&src_frame);
1083 gst_video_frame_unmap (&dst_frame);
1087 gst_video_frame_unmap (&src_frame);
1088 gst_video_frame_unmap (&dst_frame);
1093 static GstFlowReturn
1094 gst_va_compositor_aggregate_frames (GstVideoAggregator * vagg,
1097 GstVaCompositor *const self = GST_VA_COMPOSITOR (vagg);
1098 GstVaCompositorSampleGenerator generator;
1099 GstVaComposeTransaction tx;
1100 GstBuffer *vabuffer;
1101 gboolean need_copy = FALSE;
1102 GstFlowReturn ret = GST_FLOW_OK;
1104 if (self->other_pool) {
1105 /* create a va buffer for filter */
1106 ret = GST_VIDEO_AGGREGATOR_CLASS (parent_class)->create_output_buffer
1108 if (ret != GST_FLOW_OK)
1113 /* already a va buffer */
1114 vabuffer = gst_buffer_ref (outbuf);
1118 generator = (GstVaCompositorSampleGenerator) {
1120 .current = GST_ELEMENT (self)->sinkpads,
1122 tx = (GstVaComposeTransaction) {
1123 .next = gst_va_compositor_sample_next,
1125 .user_data = (gpointer) &generator,
1129 GST_OBJECT_LOCK (self);
1131 if (!gst_va_filter_compose (self->filter, &tx)) {
1132 GST_ERROR_OBJECT (self, "couldn't apply filter");
1133 ret = GST_FLOW_ERROR;
1136 GST_OBJECT_UNLOCK (self);
1138 if (ret != GST_FLOW_OK)
1141 if (need_copy && !gst_va_compositor_copy_output_buffer (self, vabuffer,
1143 GST_ERROR_OBJECT (self, "couldn't copy va buffer to output buffer");
1144 ret = GST_FLOW_ERROR;
1148 gst_buffer_unref (vabuffer);
1152 static GstFlowReturn
1153 gst_va_compositor_create_output_buffer (GstVideoAggregator * vagg,
1154 GstBuffer ** outbuf)
1156 GstVaCompositor *const self = GST_VA_COMPOSITOR (vagg);
1161 if (!self->other_pool)
1162 /* no copy necessary, so use a va buffer directly */
1163 return GST_VIDEO_AGGREGATOR_CLASS (parent_class)->create_output_buffer
1166 /* use output buffers from downstream pool for copy */
1167 if (!gst_buffer_pool_is_active (self->other_pool) &&
1168 !gst_buffer_pool_set_active (self->other_pool, TRUE)) {
1169 GST_ERROR_OBJECT (self, "failed to activate other pool %"
1170 GST_PTR_FORMAT, self->other_pool);
1171 return GST_FLOW_ERROR;
1174 /* acquire a buffer from downstream pool for copy */
1175 ret = gst_buffer_pool_acquire_buffer (self->other_pool, outbuf, NULL);
1176 if (ret != GST_FLOW_OK || !*outbuf) {
1177 GST_ERROR_OBJECT (self, "failed to acquire output buffer");
1178 return GST_FLOW_ERROR;
1185 gst_va_compositor_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
1187 GstVaCompositor *const self = GST_VA_COMPOSITOR (agg);
1189 if (!gst_video_info_from_caps (&self->other_info, caps)) {
1190 GST_ERROR_OBJECT (self, "invalid caps");
1194 if (self->other_pool) {
1195 gst_buffer_pool_set_active (self->other_pool, FALSE);
1196 gst_clear_object (&self->other_pool);
1199 return GST_AGGREGATOR_CLASS (parent_class)->negotiated_src_caps (agg, caps);
1203 gst_va_compositor_pad_get_output_size (GstVaCompositorPad * const pad,
1204 gint * width, gint * height)
1206 GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (pad);
1207 *width = (pad->width == DEFAULT_PAD_WIDTH)
1208 ? GST_VIDEO_INFO_WIDTH (&vaggpad->info) : pad->width;
1209 *height = (pad->height == DEFAULT_PAD_HEIGHT)
1210 ? GST_VIDEO_INFO_HEIGHT (&vaggpad->info) : pad->height;
1212 *width += MAX (pad->xpos, 0);
1213 *height += MAX (pad->ypos, 0);
1217 gst_va_compositor_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
1219 GstVideoAggregator *const vagg = GST_VIDEO_AGGREGATOR (agg);
1221 gint best_width = -1, best_height = -1;
1222 gint best_fps_n = -1, best_fps_d = -1;
1223 gdouble best_fps = 0.;
1224 GstCaps *ret = NULL;
1227 ret = gst_caps_make_writable (caps);
1229 GST_OBJECT_LOCK (vagg);
1230 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
1231 GstVideoAggregatorPad *const vaggpad = l->data;
1232 GstVaCompositorPad *const pad = GST_VA_COMPOSITOR_PAD (vaggpad);
1233 gint this_width, this_height;
1237 fps_n = GST_VIDEO_INFO_FPS_N (&vaggpad->info);
1238 fps_d = GST_VIDEO_INFO_FPS_D (&vaggpad->info);
1240 gst_va_compositor_pad_get_output_size (pad, &this_width, &this_height);
1242 if (best_width < this_width)
1243 best_width = this_width;
1244 if (best_height < this_height)
1245 best_height = this_height;
1250 gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);
1252 if (best_fps < cur_fps) {
1258 GST_OBJECT_UNLOCK (vagg);
1260 if (best_fps_n <= 0 || best_fps_d <= 0 || best_fps == 0.0) {
1266 s = gst_caps_get_structure (ret, 0);
1267 gst_structure_fixate_field_nearest_int (s, "width", best_width);
1268 gst_structure_fixate_field_nearest_int (s, "height", best_height);
1269 if (gst_structure_has_field (s, "framerate")) {
1270 gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
1273 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, best_fps_n,
1277 return gst_caps_fixate (ret);
1281 static const gchar *caps_str =
1282 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
1283 "{ NV12, I420, YV12, YUY2, RGBA, BGRA, P010_10LE, ARGB, ABGR }") " ;"
1284 GST_VIDEO_CAPS_MAKE ("{ VUYA, GRAY8, NV12, NV21, YUY2, UYVY, YV12, "
1285 "I420, P010_10LE, RGBA, BGRA, ARGB, ABGR }");
1289 gst_va_compositor_class_init (gpointer g_class, gpointer class_data)
1291 GstCaps *doc_caps, *caps = NULL;
1292 GstPadTemplate *sink_pad_templ, *src_pad_templ;
1293 GObjectClass *const object_class = G_OBJECT_CLASS (g_class);
1294 GstElementClass *const element_class = GST_ELEMENT_CLASS (g_class);
1295 GstAggregatorClass *const agg_class = GST_AGGREGATOR_CLASS (g_class);
1296 GstVideoAggregatorClass *const vagg_class =
1297 GST_VIDEO_AGGREGATOR_CLASS (g_class);
1298 GstVaCompositorClass *const klass = GST_VA_COMPOSITOR_CLASS (g_class);
1299 GstVaDisplay *display;
1300 GstVaFilter *filter;
1301 struct CData *cdata = class_data;
1304 parent_class = g_type_class_peek_parent (g_class);
1306 klass->render_device_path = g_strdup (cdata->render_device_path);
1308 if (cdata->description) {
1309 long_name = g_strdup_printf ("VA-API Video Compositor in %s",
1310 cdata->description);
1312 long_name = g_strdup ("VA-API Video Compositor");
1315 display = gst_va_display_drm_new_from_path (klass->render_device_path);
1316 filter = gst_va_filter_new (display);
1318 if (gst_va_filter_open (filter)) {
1319 caps = gst_va_filter_get_caps (filter);
1321 caps = gst_caps_from_string (caps_str);
1324 object_class->dispose = GST_DEBUG_FUNCPTR (gst_va_compositor_dispose);
1325 object_class->get_property =
1326 GST_DEBUG_FUNCPTR (gst_va_compositor_get_property);
1327 object_class->set_property =
1328 GST_DEBUG_FUNCPTR (gst_va_compositor_set_property);
1330 gst_element_class_set_static_metadata (element_class, long_name,
1331 "Filter/Editor/Video/Compositor/Hardware",
1332 "VA-API based video compositor",
1333 "U. Artie Eoff <ullysses.a.eoff@intel.com>");
1335 element_class->request_new_pad =
1336 GST_DEBUG_FUNCPTR (gst_va_compositor_request_new_pad);
1337 element_class->release_pad =
1338 GST_DEBUG_FUNCPTR (gst_va_compositor_release_pad);
1339 element_class->set_context =
1340 GST_DEBUG_FUNCPTR (gst_va_compositor_set_context);
1342 doc_caps = gst_caps_from_string (caps_str);
1344 sink_pad_templ = gst_pad_template_new_with_gtype ("sink_%u", GST_PAD_SINK,
1345 GST_PAD_REQUEST, caps, GST_TYPE_VA_COMPOSITOR_PAD);
1346 gst_element_class_add_pad_template (element_class, sink_pad_templ);
1347 gst_pad_template_set_documentation_caps (sink_pad_templ,
1348 gst_caps_ref (doc_caps));
1350 src_pad_templ = gst_pad_template_new_with_gtype ("src", GST_PAD_SRC,
1351 GST_PAD_ALWAYS, caps, GST_TYPE_AGGREGATOR_PAD);
1352 gst_element_class_add_pad_template (element_class, src_pad_templ);
1353 gst_pad_template_set_documentation_caps (src_pad_templ,
1354 gst_caps_ref (doc_caps));
1356 gst_caps_unref (doc_caps);
1357 gst_caps_unref (caps);
1359 agg_class->sink_query = GST_DEBUG_FUNCPTR (gst_va_compositor_sink_query);
1360 agg_class->src_query = GST_DEBUG_FUNCPTR (gst_va_compositor_src_query);
1361 agg_class->start = GST_DEBUG_FUNCPTR (gst_va_compositor_start);
1362 agg_class->stop = GST_DEBUG_FUNCPTR (gst_va_compositor_stop);
1363 agg_class->propose_allocation =
1364 GST_DEBUG_FUNCPTR (gst_va_compositor_propose_allocation);
1365 agg_class->fixate_src_caps =
1366 GST_DEBUG_FUNCPTR (gst_va_compositor_fixate_src_caps);
1367 agg_class->negotiated_src_caps =
1368 GST_DEBUG_FUNCPTR (gst_va_compositor_negotiated_src_caps);
1369 agg_class->decide_allocation =
1370 GST_DEBUG_FUNCPTR (gst_va_compositor_decide_allocation);
1372 vagg_class->aggregate_frames =
1373 GST_DEBUG_FUNCPTR (gst_va_compositor_aggregate_frames);
1374 vagg_class->create_output_buffer =
1375 GST_DEBUG_FUNCPTR (gst_va_compositor_create_output_buffer);
1377 properties[PROP_DEVICE_PATH] = g_param_spec_string ("device-path",
1378 "Device Path", "DRM device path", NULL,
1379 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1381 properties[PROP_SCALE_METHOD] = g_param_spec_enum ("scale-method",
1382 "Scale Method", "Scale method to use", GST_TYPE_VA_SCALE_METHOD,
1383 VA_FILTER_SCALING_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1385 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
1388 g_free (cdata->description);
1389 g_free (cdata->render_device_path);
1391 gst_object_unref (filter);
1392 gst_object_unref (display);
1396 gst_va_compositor_child_proxy_get_child_by_index (GstChildProxy * proxy,
1399 GstVaCompositor *self = GST_VA_COMPOSITOR (proxy);
1400 GObject *obj = NULL;
1402 GST_OBJECT_LOCK (self);
1403 obj = g_list_nth_data (GST_ELEMENT_CAST (self)->sinkpads, index);
1405 gst_object_ref (obj);
1406 GST_OBJECT_UNLOCK (self);
1412 gst_va_compositor_child_proxy_get_children_count (GstChildProxy * proxy)
1414 GstVaCompositor *self = GST_VA_COMPOSITOR (proxy);
1417 GST_OBJECT_LOCK (self);
1418 count = GST_ELEMENT_CAST (self)->numsinkpads;
1419 GST_OBJECT_UNLOCK (self);
1420 GST_INFO_OBJECT (self, "Children Count: %d", count);
1426 gst_va_compositor_child_proxy_init (gpointer g_iface, gpointer iface_data)
1428 GstChildProxyInterface *iface = (GstChildProxyInterface *) g_iface;
1430 iface->get_child_by_index = gst_va_compositor_child_proxy_get_child_by_index;
1431 iface->get_children_count = gst_va_compositor_child_proxy_get_children_count;
1435 gst_va_compositor_init (GTypeInstance * instance, gpointer g_class)
1437 GstVaCompositor *const self = GST_VA_COMPOSITOR (instance);
1439 self->other_pool = NULL;
1443 _register_debug_category (gpointer data)
1445 GST_DEBUG_CATEGORY_INIT (gst_va_compositor_debug, "vacompositor", 0,
1446 "VA Video Compositor");
1452 gst_va_compositor_register (GstPlugin * plugin, GstVaDevice * device,
1455 static GOnce debug_once = G_ONCE_INIT;
1457 GTypeInfo type_info = {
1458 .class_size = sizeof (GstVaCompositorClass),
1459 .class_init = gst_va_compositor_class_init,
1460 .instance_size = sizeof (GstVaCompositor),
1461 .instance_init = gst_va_compositor_init,
1463 GInterfaceInfo interface_info = {
1464 (GInterfaceInitFunc) gst_va_compositor_child_proxy_init,
1466 struct CData *cdata;
1468 gchar *type_name, *feature_name;
1470 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
1471 g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
1473 cdata = g_new (struct CData, 1);
1474 cdata->description = NULL;
1475 cdata->render_device_path = g_strdup (device->render_device_path);
1477 type_info.class_data = cdata;
1479 type_name = g_strdup ("GstVaCompositor");
1480 feature_name = g_strdup ("vacompositor");
1482 /* The first compositor to be registered should use a constant
1483 * name, like vacompositor, for any additional compositors, we
1484 * create unique names, using the render device name. */
1485 if (g_type_from_name (type_name)) {
1486 gchar *basename = g_path_get_basename (device->render_device_path);
1488 g_free (feature_name);
1489 type_name = g_strdup_printf ("GstVa%sCompositor", basename);
1490 feature_name = g_strdup_printf ("va%scompositor", basename);
1491 cdata->description = basename;
1493 /* lower rank for non-first device */
1498 g_once (&debug_once, _register_debug_category, NULL);
1500 type = g_type_register_static (GST_TYPE_VIDEO_AGGREGATOR, type_name,
1502 g_type_add_interface_static (type, GST_TYPE_CHILD_PROXY, &interface_info);
1504 ret = gst_element_register (plugin, feature_name, rank, type);
1507 g_free (feature_name);