2012-11-08 14:41:22 +00:00
|
|
|
/*
|
|
|
|
* gstvaapivideobufferpool.c - Gstreamer/VA video buffer pool
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
2013-11-22 04:57:18 +00:00
|
|
|
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
|
2012-11-08 14:41:22 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2015-04-03 14:08:30 +00:00
|
|
|
#include "gstcompat.h"
|
2012-11-08 14:41:22 +00:00
|
|
|
#include "gstvaapivideobufferpool.h"
|
|
|
|
#include "gstvaapivideobuffer.h"
|
|
|
|
#include "gstvaapivideomemory.h"
|
2016-05-20 12:15:53 +00:00
|
|
|
#include "gstvaapipluginutil.h"
|
2015-04-03 14:09:08 +00:00
|
|
|
#if (USE_GLX || USE_EGL)
|
2013-11-20 17:20:07 +00:00
|
|
|
#include "gstvaapivideometa_texture.h"
|
|
|
|
#endif
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapivideopool);
|
2012-11-08 14:41:22 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_debug_vaapivideopool
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
PROP_DISPLAY,
|
2012-11-08 14:41:22 +00:00
|
|
|
};
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
struct _GstVaapiVideoBufferPoolPrivate
|
|
|
|
{
|
|
|
|
GstAllocator *allocator;
|
2017-06-08 13:49:05 +00:00
|
|
|
GstVideoInfo vmeta_vinfo;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiDisplay *display;
|
2016-05-25 08:58:01 +00:00
|
|
|
guint options;
|
2015-01-26 17:30:47 +00:00
|
|
|
guint use_dmabuf_memory:1;
|
2018-02-20 01:25:13 +00:00
|
|
|
guint forced_video_meta:1;
|
2020-03-15 14:07:31 +00:00
|
|
|
/* Map between surface and GstMemory, only DMA */
|
|
|
|
GHashTable *dma_mem_map;
|
2012-11-08 14:41:22 +00:00
|
|
|
};
|
|
|
|
|
2018-06-24 11:07:20 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GstVaapiVideoBufferPool,
|
|
|
|
gst_vaapi_video_buffer_pool, GST_TYPE_BUFFER_POOL);
|
2012-11-08 14:41:22 +00:00
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_finalize (GObject * object)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv =
|
|
|
|
GST_VAAPI_VIDEO_BUFFER_POOL (object)->priv;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_display_replace (&priv->display, NULL);
|
2020-12-12 09:30:41 +00:00
|
|
|
gst_clear_object (&priv->allocator);
|
2020-03-15 14:07:31 +00:00
|
|
|
if (priv->dma_mem_map)
|
|
|
|
g_hash_table_destroy (priv->dma_mem_map);
|
2015-04-28 00:50:19 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_vaapi_video_buffer_pool_parent_class)->finalize (object);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv =
|
|
|
|
GST_VAAPI_VIDEO_BUFFER_POOL (object)->priv;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
switch (prop_id) {
|
2012-11-08 14:41:22 +00:00
|
|
|
case PROP_DISPLAY:
|
2018-06-13 16:10:28 +00:00
|
|
|
priv->display = g_value_dup_object (value);
|
2014-08-21 08:45:31 +00:00
|
|
|
break;
|
2012-11-08 14:41:22 +00:00
|
|
|
default:
|
2014-08-21 08:45:31 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv =
|
|
|
|
GST_VAAPI_VIDEO_BUFFER_POOL (object)->priv;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
switch (prop_id) {
|
2012-11-08 14:41:22 +00:00
|
|
|
case PROP_DISPLAY:
|
2014-08-21 08:45:31 +00:00
|
|
|
g_value_set_pointer (value, priv->display);
|
|
|
|
break;
|
2012-11-08 14:41:22 +00:00
|
|
|
default:
|
2014-08-21 08:45:31 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 13:52:06 +00:00
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
fill_video_alignment (GstVaapiVideoBufferPool * pool, GstVideoAlignment * align)
|
2014-07-25 13:52:06 +00:00
|
|
|
{
|
2017-06-08 13:49:05 +00:00
|
|
|
GstVideoInfo *const vip = &pool->priv->vmeta_vinfo;
|
2014-08-21 08:45:31 +00:00
|
|
|
guint i;
|
2017-08-01 15:39:04 +00:00
|
|
|
gint nth_bit;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
|
|
|
gst_video_alignment_reset (align);
|
2017-08-01 15:39:04 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vip); i++) {
|
|
|
|
nth_bit = g_bit_nth_lsf (GST_VIDEO_INFO_PLANE_STRIDE (vip, i), 0);
|
|
|
|
if (nth_bit >= 0)
|
|
|
|
align->stride_align[i] = (1U << nth_bit) - 1;
|
|
|
|
}
|
2014-07-25 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static const gchar **
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_get_options (GstBufferPool * pool)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
static const gchar *g_options[] = {
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META,
|
|
|
|
GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
return g_options;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_set_config (GstBufferPool * pool,
|
|
|
|
GstStructure * config)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GstVaapiVideoBufferPool *const base_pool = GST_VAAPI_VIDEO_BUFFER_POOL (pool);
|
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv = base_pool->priv;
|
2016-05-20 16:46:14 +00:00
|
|
|
GstCaps *caps;
|
2017-06-08 14:05:49 +00:00
|
|
|
GstVideoInfo new_allocation_vinfo;
|
|
|
|
const GstVideoInfo *allocator_vinfo;
|
2017-06-09 11:05:36 +00:00
|
|
|
const GstVideoInfo *negotiated_vinfo;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVideoAlignment align;
|
|
|
|
GstAllocator *allocator;
|
2018-02-15 18:22:08 +00:00
|
|
|
gboolean ret;
|
2016-10-19 14:15:01 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
2017-06-09 13:02:08 +00:00
|
|
|
guint surface_alloc_flags;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_DEBUG_OBJECT (base_pool, "config %" GST_PTR_FORMAT, config);
|
2016-05-20 16:46:14 +00:00
|
|
|
|
|
|
|
caps = NULL;
|
2016-10-19 14:15:01 +00:00
|
|
|
if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers,
|
|
|
|
&max_buffers))
|
2014-08-21 08:45:31 +00:00
|
|
|
goto error_invalid_config;
|
2016-05-23 20:30:04 +00:00
|
|
|
if (!caps)
|
2014-08-21 08:45:31 +00:00
|
|
|
goto error_no_caps;
|
2017-06-08 14:05:49 +00:00
|
|
|
if (!gst_video_info_from_caps (&new_allocation_vinfo, caps))
|
2016-05-23 20:30:04 +00:00
|
|
|
goto error_invalid_caps;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
2020-01-29 10:54:38 +00:00
|
|
|
if (!gst_buffer_pool_config_has_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META))
|
|
|
|
goto error_no_vaapi_video_meta_option;
|
|
|
|
|
2016-05-20 16:46:14 +00:00
|
|
|
allocator = NULL;
|
|
|
|
if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
|
|
|
|
goto error_invalid_allocator;
|
2015-01-26 17:30:47 +00:00
|
|
|
|
2017-06-09 13:02:08 +00:00
|
|
|
/* it is a valid allocator? */
|
|
|
|
if (allocator
|
|
|
|
&& (g_strcmp0 (allocator->mem_type, GST_VAAPI_VIDEO_MEMORY_NAME) != 0
|
|
|
|
&& g_strcmp0 (allocator->mem_type,
|
2020-01-29 10:55:39 +00:00
|
|
|
GST_VAAPI_DMABUF_ALLOCATOR_NAME) != 0))
|
|
|
|
goto error_invalid_allocator;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
2017-06-09 13:02:08 +00:00
|
|
|
/* get the allocator properties */
|
|
|
|
if (allocator) {
|
|
|
|
priv->use_dmabuf_memory = gst_vaapi_is_dmabuf_allocator (allocator);
|
|
|
|
negotiated_vinfo =
|
|
|
|
gst_allocator_get_vaapi_negotiated_video_info (allocator);
|
|
|
|
allocator_vinfo =
|
|
|
|
gst_allocator_get_vaapi_video_info (allocator, &surface_alloc_flags);
|
|
|
|
} else {
|
|
|
|
priv->use_dmabuf_memory = FALSE;
|
|
|
|
negotiated_vinfo = NULL;
|
|
|
|
allocator_vinfo = NULL;
|
|
|
|
surface_alloc_flags = 0;
|
|
|
|
}
|
2017-04-21 17:07:18 +00:00
|
|
|
|
2017-06-09 13:02:08 +00:00
|
|
|
/* reset or update the allocator if video resolution changed */
|
2017-06-09 15:00:40 +00:00
|
|
|
if (allocator_vinfo
|
|
|
|
&& gst_video_info_changed (allocator_vinfo, &new_allocation_vinfo)) {
|
2017-06-09 13:02:08 +00:00
|
|
|
gst_object_replace ((GstObject **) & priv->allocator, NULL);
|
2017-04-21 17:07:18 +00:00
|
|
|
|
2020-01-29 10:55:28 +00:00
|
|
|
/* dmabuf allocator can change its parameters: no need to create a
|
|
|
|
* new one */
|
2020-02-02 17:04:35 +00:00
|
|
|
if (priv->use_dmabuf_memory) {
|
2017-06-08 14:05:49 +00:00
|
|
|
gst_allocator_set_vaapi_video_info (allocator, &new_allocation_vinfo,
|
2017-04-21 17:07:18 +00:00
|
|
|
surface_alloc_flags);
|
2017-06-09 13:02:08 +00:00
|
|
|
} else {
|
|
|
|
allocator = NULL;
|
2017-04-21 17:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 13:02:08 +00:00
|
|
|
/* create a new allocator if needed */
|
|
|
|
if (!allocator) {
|
2020-02-02 17:04:35 +00:00
|
|
|
/* if no allocator set, let's create a VAAPI one */
|
|
|
|
allocator = gst_vaapi_video_allocator_new (priv->display,
|
|
|
|
&new_allocation_vinfo, surface_alloc_flags, 0);
|
2017-06-09 13:02:08 +00:00
|
|
|
if (!allocator)
|
|
|
|
goto error_no_allocator;
|
|
|
|
|
|
|
|
if (negotiated_vinfo) {
|
|
|
|
gst_allocator_set_vaapi_negotiated_video_info (allocator,
|
|
|
|
negotiated_vinfo);
|
2016-05-20 16:46:14 +00:00
|
|
|
}
|
2017-06-09 13:02:08 +00:00
|
|
|
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_INFO_OBJECT (base_pool, "created new allocator %" GST_PTR_FORMAT,
|
|
|
|
allocator);
|
2017-06-09 13:02:08 +00:00
|
|
|
gst_buffer_pool_config_set_allocator (config, allocator, NULL);
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use the allocator and set the video info for the vmeta */
|
|
|
|
if (allocator) {
|
|
|
|
if (priv->allocator)
|
|
|
|
gst_object_unref (priv->allocator);
|
|
|
|
if ((priv->allocator = allocator))
|
|
|
|
gst_object_ref (allocator);
|
|
|
|
|
|
|
|
negotiated_vinfo =
|
|
|
|
gst_allocator_get_vaapi_negotiated_video_info (priv->allocator);
|
2017-06-23 15:33:03 +00:00
|
|
|
allocator_vinfo = gst_allocator_get_vaapi_video_info (allocator, NULL);
|
2017-06-09 13:02:08 +00:00
|
|
|
priv->vmeta_vinfo = (negotiated_vinfo) ?
|
2017-06-23 15:33:03 +00:00
|
|
|
*negotiated_vinfo : *allocator_vinfo;
|
2017-06-09 13:02:08 +00:00
|
|
|
|
2017-06-23 12:38:10 +00:00
|
|
|
/* last resource to set the correct buffer size */
|
|
|
|
if (GST_VIDEO_INFO_SIZE (allocator_vinfo) != size) {
|
2016-10-19 14:15:01 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps,
|
2017-06-23 12:38:10 +00:00
|
|
|
GST_VIDEO_INFO_SIZE (allocator_vinfo), min_buffers, max_buffers);
|
2016-10-19 14:15:01 +00:00
|
|
|
}
|
2016-05-20 16:46:14 +00:00
|
|
|
}
|
|
|
|
if (!priv->allocator)
|
|
|
|
goto error_no_allocator;
|
|
|
|
|
2016-05-25 08:58:01 +00:00
|
|
|
priv->options = 0;
|
|
|
|
if (gst_buffer_pool_config_has_option (config,
|
2017-06-09 13:02:08 +00:00
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META)) {
|
2016-05-25 08:58:01 +00:00
|
|
|
priv->options |= GST_VAAPI_VIDEO_BUFFER_POOL_OPTION_VIDEO_META;
|
2018-02-15 18:22:08 +00:00
|
|
|
} else if (gst_caps_is_video_raw (caps) && !priv->use_dmabuf_memory) {
|
2016-06-14 16:45:22 +00:00
|
|
|
gint i;
|
2018-02-15 18:22:08 +00:00
|
|
|
|
2017-06-09 15:00:40 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&new_allocation_vinfo); i++) {
|
|
|
|
if (GST_VIDEO_INFO_PLANE_OFFSET (&new_allocation_vinfo, i) !=
|
2017-06-08 13:49:05 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (&priv->vmeta_vinfo, i) ||
|
2017-06-09 15:00:40 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (&new_allocation_vinfo, i) !=
|
2020-03-05 21:22:23 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (&priv->vmeta_vinfo, i) ||
|
|
|
|
GST_VIDEO_INFO_SIZE (&new_allocation_vinfo) !=
|
|
|
|
GST_VIDEO_INFO_SIZE (&priv->vmeta_vinfo)) {
|
2016-06-14 16:45:22 +00:00
|
|
|
priv->options |= GST_VAAPI_VIDEO_BUFFER_POOL_OPTION_VIDEO_META;
|
2018-02-20 01:25:13 +00:00
|
|
|
priv->forced_video_meta = TRUE;
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_INFO_OBJECT (base_pool, "adding unrequested video meta");
|
2016-06-14 16:45:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
|
2016-05-25 08:58:01 +00:00
|
|
|
if (gst_buffer_pool_config_has_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
|
2014-08-21 08:45:31 +00:00
|
|
|
fill_video_alignment (GST_VAAPI_VIDEO_BUFFER_POOL (pool), &align);
|
|
|
|
gst_buffer_pool_config_set_video_alignment (config, &align);
|
|
|
|
}
|
|
|
|
|
2016-05-25 08:58:01 +00:00
|
|
|
if (!priv->use_dmabuf_memory && gst_buffer_pool_config_has_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META))
|
|
|
|
priv->options |= GST_VAAPI_VIDEO_BUFFER_POOL_OPTION_GL_TEXTURE_UPLOAD;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
2016-06-14 16:45:22 +00:00
|
|
|
ret =
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_BUFFER_POOL_CLASS
|
|
|
|
(gst_vaapi_video_buffer_pool_parent_class)->set_config (pool, config);
|
2018-02-15 18:22:08 +00:00
|
|
|
return ret;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
2012-11-08 14:41:22 +00:00
|
|
|
error_invalid_config:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_WARNING_OBJECT (base_pool, "invalid config");
|
2014-08-21 08:45:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
error_no_caps:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_WARNING_OBJECT (base_pool, "no caps in config");
|
2014-08-21 08:45:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-23 20:30:04 +00:00
|
|
|
error_invalid_caps:
|
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_WARNING_OBJECT (base_pool, "invalid caps %" GST_PTR_FORMAT, caps);
|
2016-05-23 20:30:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-20 16:46:14 +00:00
|
|
|
error_invalid_allocator:
|
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_INFO_OBJECT (base_pool, "no allocator in config");
|
2016-05-20 16:46:14 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
error_no_vaapi_video_meta_option:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_WARNING_OBJECT (base_pool, "no GstVaapiVideoMeta option in config");
|
2014-08-21 08:45:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-05-20 16:46:14 +00:00
|
|
|
error_no_allocator:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-29 10:52:38 +00:00
|
|
|
GST_WARNING_OBJECT (base_pool, "no allocator defined");
|
2014-08-21 08:45:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:07:31 +00:00
|
|
|
static void
|
|
|
|
vaapi_buffer_pool_cache_dma_mem (GstVaapiVideoBufferPool * pool,
|
|
|
|
GstVaapiSurfaceProxy * proxy, GstMemory * mem)
|
|
|
|
{
|
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv = pool->priv;
|
|
|
|
GstVaapiSurface *surface;
|
|
|
|
|
|
|
|
surface = GST_VAAPI_SURFACE_PROXY_SURFACE (proxy);
|
|
|
|
g_assert (surface);
|
|
|
|
g_assert (gst_vaapi_surface_peek_buffer_proxy (surface));
|
|
|
|
|
|
|
|
if (!priv->dma_mem_map)
|
|
|
|
priv->dma_mem_map = g_hash_table_new_full (g_direct_hash,
|
|
|
|
g_direct_equal, NULL, (GDestroyNotify) gst_memory_unref);
|
|
|
|
|
|
|
|
if (!g_hash_table_contains (priv->dma_mem_map, surface)) {
|
|
|
|
g_hash_table_insert (priv->dma_mem_map, surface, gst_memory_ref (mem));
|
|
|
|
} else {
|
|
|
|
g_assert (g_hash_table_lookup (priv->dma_mem_map, surface) == mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
vaapi_buffer_pool_lookup_dma_mem (GstVaapiVideoBufferPool * pool,
|
|
|
|
GstVaapiSurfaceProxy * proxy)
|
|
|
|
{
|
|
|
|
GstVaapiSurface *surface;
|
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv = pool->priv;
|
|
|
|
GstVaapiBufferProxy *buf_proxy;
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
g_assert (priv->use_dmabuf_memory);
|
|
|
|
|
|
|
|
if (!priv->dma_mem_map)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
surface = GST_VAAPI_SURFACE_PROXY_SURFACE (proxy);
|
|
|
|
g_assert (surface);
|
|
|
|
|
|
|
|
buf_proxy = gst_vaapi_surface_peek_buffer_proxy (surface);
|
|
|
|
/* Have not exported yet */
|
|
|
|
if (!buf_proxy) {
|
|
|
|
g_assert (!g_hash_table_contains (priv->dma_mem_map, surface));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem = g_hash_table_lookup (priv->dma_mem_map, surface);
|
|
|
|
g_assert (mem);
|
|
|
|
|
|
|
|
return gst_memory_ref (mem);
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static GstFlowReturn
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_alloc_buffer (GstBufferPool * pool,
|
|
|
|
GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GstVaapiVideoBufferPool *const base_pool = GST_VAAPI_VIDEO_BUFFER_POOL (pool);
|
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv = base_pool->priv;
|
2016-10-19 15:05:04 +00:00
|
|
|
GstVaapiVideoBufferPoolAcquireParams *const priv_params =
|
|
|
|
(GstVaapiVideoBufferPoolAcquireParams *) params;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoMeta *meta;
|
|
|
|
GstMemory *mem;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
|
|
|
if (!priv->allocator)
|
|
|
|
goto error_no_allocator;
|
|
|
|
|
2020-03-15 13:50:24 +00:00
|
|
|
meta = gst_vaapi_video_meta_new (priv->display);
|
|
|
|
if (!meta)
|
|
|
|
goto error_create_meta;
|
|
|
|
|
|
|
|
buffer = gst_vaapi_video_buffer_new (meta);
|
2014-08-21 08:45:31 +00:00
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
goto error_create_buffer;
|
|
|
|
|
2016-10-19 15:05:04 +00:00
|
|
|
if (priv_params && priv_params->proxy)
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (meta, priv_params->proxy);
|
|
|
|
|
2020-03-15 14:07:31 +00:00
|
|
|
if (priv->use_dmabuf_memory) {
|
|
|
|
mem = NULL;
|
|
|
|
if (priv_params && priv_params->proxy) {
|
|
|
|
mem = vaapi_buffer_pool_lookup_dma_mem (base_pool, priv_params->proxy);
|
|
|
|
if (!mem) {
|
|
|
|
mem = gst_vaapi_dmabuf_memory_new (priv->allocator, meta);
|
|
|
|
if (!mem)
|
|
|
|
goto error_create_memory;
|
|
|
|
|
|
|
|
vaapi_buffer_pool_cache_dma_mem (base_pool, priv_params->proxy, mem);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mem = gst_vaapi_dmabuf_memory_new (priv->allocator, meta);
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-26 17:30:47 +00:00
|
|
|
mem = gst_vaapi_video_memory_new (priv->allocator, meta);
|
2020-03-15 14:07:31 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!mem)
|
|
|
|
goto error_create_memory;
|
2014-08-21 09:12:39 +00:00
|
|
|
gst_vaapi_video_meta_replace (&meta, NULL);
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_buffer_append_memory (buffer, mem);
|
|
|
|
|
2016-05-25 08:58:01 +00:00
|
|
|
if (priv->options & GST_VAAPI_VIDEO_BUFFER_POOL_OPTION_VIDEO_META) {
|
2017-06-08 13:49:05 +00:00
|
|
|
GstVideoInfo *const vip = &priv->vmeta_vinfo;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVideoMeta *vmeta;
|
|
|
|
|
|
|
|
vmeta = gst_buffer_add_video_meta_full (buffer, 0,
|
|
|
|
GST_VIDEO_INFO_FORMAT (vip), GST_VIDEO_INFO_WIDTH (vip),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (vip), GST_VIDEO_INFO_N_PLANES (vip),
|
|
|
|
&GST_VIDEO_INFO_PLANE_OFFSET (vip, 0),
|
|
|
|
&GST_VIDEO_INFO_PLANE_STRIDE (vip, 0));
|
2015-01-26 17:30:47 +00:00
|
|
|
|
|
|
|
if (GST_VAAPI_IS_VIDEO_MEMORY (mem)) {
|
|
|
|
vmeta->map = gst_video_meta_map_vaapi_memory;
|
|
|
|
vmeta->unmap = gst_video_meta_unmap_vaapi_memory;
|
|
|
|
}
|
2020-05-17 07:55:42 +00:00
|
|
|
|
|
|
|
GST_META_FLAG_SET (vmeta, GST_META_FLAG_POOLED);
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
2015-04-03 14:09:08 +00:00
|
|
|
#if (USE_GLX || USE_EGL)
|
2020-05-17 07:55:42 +00:00
|
|
|
if (priv->options & GST_VAAPI_VIDEO_BUFFER_POOL_OPTION_GL_TEXTURE_UPLOAD) {
|
|
|
|
GstMeta *tex_meta = gst_buffer_add_texture_upload_meta (buffer);
|
|
|
|
if (tex_meta)
|
|
|
|
GST_META_FLAG_SET (tex_meta, GST_META_FLAG_POOLED);
|
|
|
|
}
|
2013-11-20 17:20:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
*out_buffer_ptr = buffer;
|
|
|
|
return GST_FLOW_OK;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
/* ERRORS */
|
2012-11-08 14:41:22 +00:00
|
|
|
error_no_allocator:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_ERROR_OBJECT (base_pool, "no GstAllocator in buffer pool");
|
2014-08-21 08:45:31 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
error_create_meta:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_ERROR_OBJECT (base_pool, "failed to allocate vaapi video meta");
|
2014-08-21 08:45:31 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
error_create_buffer:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_ERROR_OBJECT (base_pool, "failed to create video buffer");
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_meta_unref (meta);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
error_create_memory:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GST_ERROR_OBJECT (base_pool, "failed to create video memory");
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
gst_vaapi_video_meta_unref (meta);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 15:07:07 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_vaapi_video_buffer_pool_acquire_buffer (GstBufferPool * pool,
|
|
|
|
GstBuffer ** out_buffer_ptr, GstBufferPoolAcquireParams * params)
|
|
|
|
{
|
2020-01-27 17:05:14 +00:00
|
|
|
GstVaapiVideoBufferPool *const base_pool = GST_VAAPI_VIDEO_BUFFER_POOL (pool);
|
|
|
|
GstVaapiVideoBufferPoolPrivate *const priv = base_pool->priv;
|
2016-10-19 15:07:07 +00:00
|
|
|
GstVaapiVideoBufferPoolAcquireParams *const priv_params =
|
|
|
|
(GstVaapiVideoBufferPoolAcquireParams *) params;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVaapiVideoMeta *meta;
|
|
|
|
GstVaapiSurface *surface;
|
|
|
|
|
|
|
|
ret =
|
|
|
|
GST_BUFFER_POOL_CLASS
|
|
|
|
(gst_vaapi_video_buffer_pool_parent_class)->acquire_buffer (pool, &buffer,
|
|
|
|
params);
|
|
|
|
|
|
|
|
if (!priv->use_dmabuf_memory || !params || !priv_params->proxy
|
|
|
|
|| ret != GST_FLOW_OK) {
|
|
|
|
*out_buffer_ptr = buffer;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:07:31 +00:00
|
|
|
/* Some pool users, such as decode, needs to acquire a buffer for a
|
|
|
|
* specified surface (via surface proxy). If not it is a DMABuf, we
|
|
|
|
* just replace the underlying surface proxy of buffer's
|
|
|
|
* GstVaapiVideoMeta. But in DMABuf case, the thing is a little bit
|
|
|
|
* more complicated:
|
|
|
|
*
|
|
|
|
* For DMABuf, GstMemory is-a GstFdMemory, which doesn't provide a
|
|
|
|
* way to change its FD, thus once created it's bound to a
|
|
|
|
* surface. On the other side, for performace reason, when the
|
|
|
|
* buffer is released, the buffer and its memory are cached in the
|
|
|
|
* buffer pool, and at next acquire_buffer() may still reuse a
|
|
|
|
* buffer and its memory. But the pushed surface by the decoder may
|
|
|
|
* be different from the one popped by the pool, so we need to
|
|
|
|
* replace the buffer's memory with the correct one. */
|
2016-10-19 15:07:07 +00:00
|
|
|
g_assert (gst_buffer_n_memory (buffer) == 1);
|
|
|
|
|
2020-01-22 17:50:36 +00:00
|
|
|
/* Update the underlying surface proxy */
|
|
|
|
meta = gst_buffer_get_vaapi_video_meta (buffer);
|
2020-01-24 10:55:22 +00:00
|
|
|
if (!meta) {
|
|
|
|
*out_buffer_ptr = buffer;
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (meta, priv_params->proxy);
|
2020-01-22 17:50:36 +00:00
|
|
|
|
2020-03-15 14:07:31 +00:00
|
|
|
mem = vaapi_buffer_pool_lookup_dma_mem (base_pool, priv_params->proxy);
|
|
|
|
if (mem) {
|
|
|
|
if (mem == gst_buffer_peek_memory (buffer, 0)) {
|
|
|
|
gst_memory_unref (mem);
|
|
|
|
*out_buffer_ptr = buffer;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2016-10-19 15:07:07 +00:00
|
|
|
} else {
|
2020-03-15 14:07:31 +00:00
|
|
|
/* Should be an unexported surface */
|
|
|
|
surface = GST_VAAPI_SURFACE_PROXY_SURFACE (priv_params->proxy);
|
|
|
|
g_assert (surface);
|
|
|
|
g_assert (gst_vaapi_surface_peek_buffer_proxy (surface) == NULL);
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (meta, priv_params->proxy);
|
2020-01-22 17:50:36 +00:00
|
|
|
mem = gst_vaapi_dmabuf_memory_new (priv->allocator, meta);
|
2020-03-15 14:07:31 +00:00
|
|
|
if (mem)
|
|
|
|
vaapi_buffer_pool_cache_dma_mem (base_pool, priv_params->proxy, mem);
|
2016-10-19 15:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mem) {
|
|
|
|
gst_buffer_replace_memory (buffer, 0, mem);
|
|
|
|
gst_buffer_unset_flags (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
|
2020-03-15 14:07:31 +00:00
|
|
|
*out_buffer_ptr = buffer;
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
} else {
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
*out_buffer_ptr = NULL;
|
|
|
|
return GST_FLOW_ERROR;
|
2016-10-19 15:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_reset_buffer (GstBufferPool * pool,
|
|
|
|
GstBuffer * buffer)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstMemory *const mem = gst_buffer_peek_memory (buffer, 0);
|
2020-01-22 17:50:36 +00:00
|
|
|
GstVaapiVideoMeta *meta;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
/* Release the underlying surface proxy */
|
2020-01-22 17:50:36 +00:00
|
|
|
if (GST_VAAPI_IS_VIDEO_MEMORY (mem)) {
|
2015-01-26 17:30:47 +00:00
|
|
|
gst_vaapi_video_memory_reset_surface (GST_VAAPI_VIDEO_MEMORY_CAST (mem));
|
2020-02-15 17:25:37 +00:00
|
|
|
} else if (!gst_vaapi_dmabuf_memory_holds_surface (mem)) {
|
|
|
|
/* If mem holds an internally created surface, don't reset it!
|
|
|
|
* While surface is passed, we should clear it to avoid wrong
|
|
|
|
* reference. */
|
2020-01-22 17:50:36 +00:00
|
|
|
meta = gst_buffer_get_vaapi_video_meta (buffer);
|
2020-03-15 13:50:24 +00:00
|
|
|
g_assert (meta);
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (meta, NULL);
|
2020-01-22 17:50:36 +00:00
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_BUFFER_POOL_CLASS (gst_vaapi_video_buffer_pool_parent_class)->reset_buffer
|
|
|
|
(pool, buffer);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_class_init (GstVaapiVideoBufferPoolClass * klass)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GObjectClass *const object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstBufferPoolClass *const pool_class = GST_BUFFER_POOL_CLASS (klass);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_debug_vaapivideopool,
|
|
|
|
"vaapivideopool", 0, "VA-API video pool");
|
|
|
|
|
|
|
|
object_class->finalize = gst_vaapi_video_buffer_pool_finalize;
|
|
|
|
object_class->set_property = gst_vaapi_video_buffer_pool_set_property;
|
|
|
|
object_class->get_property = gst_vaapi_video_buffer_pool_get_property;
|
|
|
|
pool_class->get_options = gst_vaapi_video_buffer_pool_get_options;
|
|
|
|
pool_class->set_config = gst_vaapi_video_buffer_pool_set_config;
|
|
|
|
pool_class->alloc_buffer = gst_vaapi_video_buffer_pool_alloc_buffer;
|
2016-10-19 15:07:07 +00:00
|
|
|
pool_class->acquire_buffer = gst_vaapi_video_buffer_pool_acquire_buffer;
|
2014-08-21 08:45:31 +00:00
|
|
|
pool_class->reset_buffer = gst_vaapi_video_buffer_pool_reset_buffer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GstVaapiVideoBufferPool:display:
|
|
|
|
*
|
|
|
|
* The #GstVaapiDisplay this object is bound to.
|
|
|
|
*/
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_DISPLAY,
|
2018-06-13 16:10:28 +00:00
|
|
|
g_param_spec_object ("display", "Display",
|
2014-08-21 08:45:31 +00:00
|
|
|
"The GstVaapiDisplay to use for this video pool",
|
2018-06-13 16:10:28 +00:00
|
|
|
GST_TYPE_VAAPI_DISPLAY, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_init (GstVaapiVideoBufferPool * pool)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2018-06-24 11:07:20 +00:00
|
|
|
pool->priv = gst_vaapi_video_buffer_pool_get_instance_private (pool);
|
2020-03-15 14:07:31 +00:00
|
|
|
pool->priv->dma_mem_map = NULL;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstBufferPool *
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_buffer_pool_new (GstVaapiDisplay * display)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
return g_object_new (GST_VAAPI_TYPE_VIDEO_BUFFER_POOL,
|
|
|
|
"display", display, NULL);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
2018-02-20 01:25:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vaapi_video_buffer_pool_copy_buffer:
|
|
|
|
* @pool: a #GstVaapiVideoBufferPool
|
|
|
|
*
|
|
|
|
* Returns if the @pool force set of #GstVideoMeta. If so, the element
|
|
|
|
* should copy the generated buffer by the pool to a system allocated
|
|
|
|
* buffer. Otherwise, downstream could not display correctly the
|
|
|
|
* frame.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if #GstVideoMeta is forced.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
gst_vaapi_video_buffer_pool_copy_buffer (GstBufferPool * pool)
|
|
|
|
{
|
2018-06-24 11:07:20 +00:00
|
|
|
GstVaapiVideoBufferPool *va_pool = (GstVaapiVideoBufferPool *) pool;
|
2018-02-20 01:25:13 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (GST_VAAPI_IS_VIDEO_BUFFER_POOL (pool), FALSE);
|
|
|
|
|
2018-06-24 11:07:20 +00:00
|
|
|
return va_pool->priv->forced_video_meta;
|
2018-02-20 01:25:13 +00:00
|
|
|
}
|