2012-11-08 14:41:22 +00:00
|
|
|
/*
|
|
|
|
* gstvaapivideomemory.c - Gstreamer/VA video memory
|
|
|
|
*
|
|
|
|
* 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"
|
2015-03-02 10:12:53 +00:00
|
|
|
#include <unistd.h>
|
2015-01-26 17:30:47 +00:00
|
|
|
#include <gst/vaapi/gstvaapisurface_drm.h>
|
2014-07-24 04:46:22 +00:00
|
|
|
#include <gst/vaapi/gstvaapisurfacepool.h>
|
|
|
|
#include <gst/vaapi/gstvaapiimagepool.h>
|
2012-11-08 14:41:22 +00:00
|
|
|
#include "gstvaapivideomemory.h"
|
2016-05-23 13:38:07 +00:00
|
|
|
#include "gstvaapipluginutil.h"
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapivideomemory);
|
2012-11-08 14:41:22 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_debug_vaapivideomemory
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
#ifndef GST_VIDEO_INFO_FORMAT_STRING
|
|
|
|
#define GST_VIDEO_INFO_FORMAT_STRING(vip) \
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (vip))
|
2013-04-09 14:02:06 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-25 13:44:58 +00:00
|
|
|
/* Defined if native VA surface formats are preferred over direct rendering */
|
|
|
|
#define USE_NATIVE_FORMATS 1
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiVideoMemory --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2015-06-30 06:44:18 +00:00
|
|
|
static void gst_vaapi_video_memory_reset_image (GstVaapiVideoMemory * mem);
|
2014-07-24 04:46:22 +00:00
|
|
|
|
2014-07-23 16:54:13 +00:00
|
|
|
static guchar *
|
2014-08-21 08:45:31 +00:00
|
|
|
get_image_data (GstVaapiImage * image)
|
2014-07-23 16:54:13 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
guchar *data;
|
|
|
|
VAImage va_image;
|
2014-07-23 16:54:13 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
data = gst_vaapi_image_get_plane (image, 0);
|
|
|
|
if (!data || !gst_vaapi_image_get_image (image, &va_image))
|
|
|
|
return NULL;
|
2014-07-23 16:54:13 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
data -= va_image.offsets[0];
|
|
|
|
return data;
|
2014-07-23 16:54:13 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static GstVaapiImage *
|
2014-08-21 08:45:31 +00:00
|
|
|
new_image (GstVaapiDisplay * display, const GstVideoInfo * vip)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2015-01-15 15:14:13 +00:00
|
|
|
if (!GST_VIDEO_INFO_WIDTH (vip) || !GST_VIDEO_INFO_HEIGHT (vip))
|
|
|
|
return NULL;
|
2014-08-21 08:45:31 +00:00
|
|
|
return gst_vaapi_image_new (display, GST_VIDEO_INFO_FORMAT (vip),
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
ensure_image (GstVaapiVideoMemory * mem)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!mem->image && mem->use_direct_rendering) {
|
|
|
|
mem->image = gst_vaapi_surface_derive_image (mem->surface);
|
2013-04-09 14:02:06 +00:00
|
|
|
if (!mem->image) {
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_WARNING ("failed to derive image, fallbacking to copy");
|
|
|
|
mem->use_direct_rendering = FALSE;
|
|
|
|
} else if (gst_vaapi_surface_get_format (mem->surface) !=
|
|
|
|
GST_VIDEO_INFO_FORMAT (mem->image_info)) {
|
|
|
|
gst_vaapi_object_replace (&mem->image, NULL);
|
|
|
|
mem->use_direct_rendering = FALSE;
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mem->image) {
|
|
|
|
GstVaapiVideoAllocator *const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST (GST_MEMORY_CAST (mem)->allocator);
|
|
|
|
|
|
|
|
mem->image = gst_vaapi_video_pool_get_object (allocator->image_pool);
|
|
|
|
if (!mem->image)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gst_vaapi_video_meta_set_image (mem->meta, mem->image);
|
|
|
|
return TRUE;
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:23:13 +00:00
|
|
|
static gboolean
|
|
|
|
ensure_image_is_current (GstVaapiVideoMemory * mem)
|
|
|
|
{
|
|
|
|
if (mem->use_direct_rendering)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!GST_VAAPI_VIDEO_MEMORY_FLAG_IS_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_IMAGE_IS_CURRENT)) {
|
|
|
|
if (!gst_vaapi_surface_get_image (mem->surface, mem->image))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_IMAGE_IS_CURRENT);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static GstVaapiSurface *
|
2014-08-21 08:45:31 +00:00
|
|
|
new_surface (GstVaapiDisplay * display, const GstVideoInfo * vip)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiSurface *surface;
|
|
|
|
GstVaapiChromaType chroma_type;
|
|
|
|
|
|
|
|
/* Try with explicit format first */
|
|
|
|
if (!USE_NATIVE_FORMATS &&
|
|
|
|
GST_VIDEO_INFO_FORMAT (vip) != GST_VIDEO_FORMAT_ENCODED) {
|
|
|
|
surface = gst_vaapi_surface_new_with_format (display,
|
|
|
|
GST_VIDEO_INFO_FORMAT (vip), GST_VIDEO_INFO_WIDTH (vip),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (vip));
|
|
|
|
if (surface)
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to pick something compatible, i.e. with same chroma type */
|
|
|
|
chroma_type =
|
|
|
|
gst_vaapi_video_format_get_chroma_type (GST_VIDEO_INFO_FORMAT (vip));
|
|
|
|
if (!chroma_type)
|
|
|
|
return NULL;
|
|
|
|
return gst_vaapi_surface_new (display, chroma_type,
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
static GstVaapiSurfaceProxy *
|
2014-08-21 08:45:31 +00:00
|
|
|
new_surface_proxy (GstVaapiVideoMemory * mem)
|
2013-10-09 07:33:56 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoAllocator *const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST (GST_MEMORY_CAST (mem)->allocator);
|
2013-10-09 07:33:56 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
return
|
|
|
|
gst_vaapi_surface_proxy_new_from_pool (GST_VAAPI_SURFACE_POOL
|
|
|
|
(allocator->surface_pool));
|
2013-10-09 07:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
ensure_surface (GstVaapiVideoMemory * mem)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!mem->proxy) {
|
|
|
|
gst_vaapi_surface_proxy_replace (&mem->proxy,
|
|
|
|
gst_vaapi_video_meta_get_surface_proxy (mem->meta));
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
if (!mem->proxy) {
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->proxy = new_surface_proxy (mem);
|
|
|
|
if (!mem->proxy)
|
|
|
|
return FALSE;
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (mem->meta, mem->proxy);
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
|
|
|
mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE (mem->proxy);
|
|
|
|
return mem->surface != NULL;
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:23:13 +00:00
|
|
|
static gboolean
|
|
|
|
ensure_surface_is_current (GstVaapiVideoMemory * mem)
|
|
|
|
{
|
|
|
|
if (mem->use_direct_rendering)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!GST_VAAPI_VIDEO_MEMORY_FLAG_IS_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SURFACE_IS_CURRENT)) {
|
2014-11-24 12:20:33 +00:00
|
|
|
if (GST_VAAPI_VIDEO_MEMORY_FLAG_IS_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_IMAGE_IS_CURRENT)
|
|
|
|
&& !gst_vaapi_surface_put_image (mem->surface, mem->image))
|
2014-11-21 14:23:13 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SURFACE_IS_CURRENT);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_video_meta_map_vaapi_memory (GstVideoMeta * meta, guint plane,
|
|
|
|
GstMapInfo * info, gpointer * data, gint * stride, GstMapFlags flags)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2016-02-03 10:04:15 +00:00
|
|
|
GstAllocator *allocator;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoMemory *const mem =
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_CAST (gst_buffer_peek_memory (meta->buffer, 0));
|
|
|
|
g_return_val_if_fail (mem, FALSE);
|
2016-02-03 10:04:15 +00:00
|
|
|
|
|
|
|
allocator = GST_MEMORY_CAST (mem)->allocator;
|
|
|
|
g_return_val_if_fail (GST_VAAPI_IS_VIDEO_ALLOCATOR (allocator), FALSE);
|
2014-08-21 08:45:31 +00:00
|
|
|
g_return_val_if_fail (mem->meta, FALSE);
|
|
|
|
|
|
|
|
if (mem->map_type && mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
|
|
|
|
goto error_incompatible_map;
|
|
|
|
|
|
|
|
/* Map for writing */
|
|
|
|
if (++mem->map_count == 1) {
|
|
|
|
if (!ensure_surface (mem))
|
|
|
|
goto error_ensure_surface;
|
|
|
|
if (!ensure_image (mem))
|
|
|
|
goto error_ensure_image;
|
|
|
|
|
2016-09-09 10:03:37 +00:00
|
|
|
// Load VA image from surface
|
|
|
|
if ((flags & GST_MAP_READ) && !ensure_image_is_current (mem))
|
2014-11-21 14:23:13 +00:00
|
|
|
goto error_no_current_image;
|
2014-08-21 08:45:31 +00:00
|
|
|
|
|
|
|
if (!gst_vaapi_image_map (mem->image))
|
|
|
|
goto error_map_image;
|
|
|
|
mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
|
2014-11-21 14:23:13 +00:00
|
|
|
|
|
|
|
// Mark surface as dirty and expect updates from image
|
|
|
|
if (flags & GST_MAP_WRITE)
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_UNSET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SURFACE_IS_CURRENT);
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*data = gst_vaapi_image_get_plane (mem->image, plane);
|
|
|
|
*stride = gst_vaapi_image_get_pitch (mem->image, plane);
|
|
|
|
info->flags = flags;
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2013-06-05 09:01:51 +00:00
|
|
|
error_incompatible_map:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("incompatible map type (%d)", mem->map_type);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
error_ensure_surface:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
const GstVideoInfo *const vip = mem->surface_info;
|
|
|
|
GST_ERROR ("failed to create %s surface of size %ux%u",
|
|
|
|
GST_VIDEO_INFO_FORMAT_STRING (vip),
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
error_ensure_image:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
const GstVideoInfo *const vip = mem->image_info;
|
|
|
|
GST_ERROR ("failed to create %s image of size %ux%u",
|
|
|
|
GST_VIDEO_INFO_FORMAT_STRING (vip),
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
error_map_image:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to map image %" GST_VAAPI_ID_FORMAT,
|
|
|
|
GST_VAAPI_ID_ARGS (gst_vaapi_image_get_id (mem->image)));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-11-21 14:23:13 +00:00
|
|
|
error_no_current_image:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to make image current");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_video_meta_unmap_vaapi_memory (GstVideoMeta * meta, guint plane,
|
|
|
|
GstMapInfo * info)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2016-02-03 10:04:15 +00:00
|
|
|
GstAllocator *allocator;
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoMemory *const mem =
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_CAST (gst_buffer_peek_memory (meta->buffer, 0));
|
|
|
|
g_return_val_if_fail (mem, FALSE);
|
2016-02-03 10:04:15 +00:00
|
|
|
|
|
|
|
allocator = GST_MEMORY_CAST (mem)->allocator;
|
|
|
|
g_return_val_if_fail (GST_VAAPI_IS_VIDEO_ALLOCATOR (allocator), FALSE);
|
2014-08-21 08:45:31 +00:00
|
|
|
g_return_val_if_fail (mem->meta, FALSE);
|
|
|
|
g_return_val_if_fail (mem->surface, FALSE);
|
|
|
|
g_return_val_if_fail (mem->image, FALSE);
|
|
|
|
|
|
|
|
if (--mem->map_count == 0) {
|
|
|
|
mem->map_type = 0;
|
|
|
|
|
|
|
|
/* Unmap VA image used for read/writes */
|
2014-11-21 14:23:13 +00:00
|
|
|
if (info->flags & GST_MAP_READWRITE) {
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_image_unmap (mem->image);
|
|
|
|
|
2014-11-21 14:23:13 +00:00
|
|
|
if (info->flags & GST_MAP_WRITE) {
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_IMAGE_IS_CURRENT);
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
GstMemory *
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_new (GstAllocator * base_allocator,
|
|
|
|
GstVaapiVideoMeta * meta)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoAllocator *const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST (base_allocator);
|
|
|
|
const GstVideoInfo *vip;
|
|
|
|
GstVaapiVideoMemory *mem;
|
|
|
|
|
2015-01-26 17:30:47 +00:00
|
|
|
g_return_val_if_fail (GST_VAAPI_IS_VIDEO_ALLOCATOR (allocator), NULL);
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
mem = g_slice_new (GstVaapiVideoMemory);
|
|
|
|
if (!mem)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vip = &allocator->image_info;
|
|
|
|
gst_memory_init (&mem->parent_instance, GST_MEMORY_FLAG_NO_SHARE,
|
|
|
|
gst_object_ref (allocator), NULL, GST_VIDEO_INFO_SIZE (vip), 0,
|
|
|
|
0, GST_VIDEO_INFO_SIZE (vip));
|
|
|
|
|
|
|
|
mem->proxy = NULL;
|
|
|
|
mem->surface_info = &allocator->surface_info;
|
|
|
|
mem->surface = NULL;
|
|
|
|
mem->image_info = &allocator->image_info;
|
|
|
|
mem->image = NULL;
|
2014-08-21 09:12:39 +00:00
|
|
|
mem->meta = meta ? gst_vaapi_video_meta_ref (meta) : NULL;
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->map_type = 0;
|
|
|
|
mem->map_count = 0;
|
|
|
|
mem->use_direct_rendering = allocator->has_direct_rendering;
|
2014-11-21 14:23:13 +00:00
|
|
|
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SURFACE_IS_CURRENT);
|
2014-08-21 08:45:31 +00:00
|
|
|
return GST_MEMORY_CAST (mem);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_free (GstVaapiVideoMemory * mem)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->surface = NULL;
|
|
|
|
gst_vaapi_video_memory_reset_image (mem);
|
|
|
|
gst_vaapi_surface_proxy_replace (&mem->proxy, NULL);
|
2014-08-21 09:12:39 +00:00
|
|
|
gst_vaapi_video_meta_replace (&mem->meta, NULL);
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_object_unref (GST_MEMORY_CAST (mem)->allocator);
|
|
|
|
g_slice_free (GstVaapiVideoMemory, mem);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 04:46:22 +00:00
|
|
|
void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_reset_image (GstVaapiVideoMemory * mem)
|
2014-07-24 04:46:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoAllocator *const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST (GST_MEMORY_CAST (mem)->allocator);
|
|
|
|
|
|
|
|
if (mem->use_direct_rendering)
|
|
|
|
gst_vaapi_object_replace (&mem->image, NULL);
|
|
|
|
else if (mem->image) {
|
|
|
|
gst_vaapi_video_pool_put_object (allocator->image_pool, mem->image);
|
|
|
|
mem->image = NULL;
|
|
|
|
}
|
2014-11-21 14:43:35 +00:00
|
|
|
|
2016-10-20 15:01:57 +00:00
|
|
|
/* Don't synchronize to surface, this shall have happened during
|
|
|
|
* unmaps */
|
2014-11-21 14:43:35 +00:00
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_UNSET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_IMAGE_IS_CURRENT);
|
2014-07-24 04:46:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 07:47:18 +00:00
|
|
|
void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_reset_surface (GstVaapiVideoMemory * mem)
|
2013-10-09 07:47:18 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->surface = NULL;
|
|
|
|
gst_vaapi_video_memory_reset_image (mem);
|
|
|
|
gst_vaapi_surface_proxy_replace (&mem->proxy, NULL);
|
2014-08-21 09:12:39 +00:00
|
|
|
if (mem->meta)
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (mem->meta, NULL);
|
2014-11-21 14:43:35 +00:00
|
|
|
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_UNSET (mem,
|
|
|
|
GST_VAAPI_VIDEO_MEMORY_FLAG_SURFACE_IS_CURRENT);
|
2013-10-09 07:47:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:23:13 +00:00
|
|
|
gboolean
|
|
|
|
gst_vaapi_video_memory_sync (GstVaapiVideoMemory * mem)
|
|
|
|
{
|
2015-02-18 09:46:11 +00:00
|
|
|
g_return_val_if_fail (mem, FALSE);
|
2014-11-21 14:23:13 +00:00
|
|
|
|
|
|
|
return ensure_surface_is_current (mem);
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static gpointer
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_map (GstVaapiVideoMemory * mem, gsize maxsize,
|
|
|
|
guint flags)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
gpointer data;
|
|
|
|
|
2014-08-21 09:12:39 +00:00
|
|
|
g_return_val_if_fail (mem, NULL);
|
|
|
|
g_return_val_if_fail (mem->meta, NULL);
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
if (mem->map_count == 0) {
|
|
|
|
switch (flags & GST_MAP_READWRITE) {
|
|
|
|
case 0:
|
|
|
|
// No flags set: return a GstVaapiSurfaceProxy
|
|
|
|
gst_vaapi_surface_proxy_replace (&mem->proxy,
|
|
|
|
gst_vaapi_video_meta_get_surface_proxy (mem->meta));
|
|
|
|
if (!mem->proxy)
|
|
|
|
goto error_no_surface_proxy;
|
2014-11-21 14:23:13 +00:00
|
|
|
if (!ensure_surface_is_current (mem))
|
|
|
|
goto error_no_current_surface;
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
|
|
|
|
break;
|
|
|
|
case GST_MAP_READ:
|
|
|
|
// Only read flag set: return raw pixels
|
|
|
|
if (!ensure_surface (mem))
|
|
|
|
goto error_no_surface;
|
|
|
|
if (!ensure_image (mem))
|
|
|
|
goto error_no_image;
|
2014-11-21 14:23:13 +00:00
|
|
|
if (!ensure_image_is_current (mem))
|
|
|
|
goto error_no_current_image;
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!gst_vaapi_image_map (mem->image))
|
|
|
|
goto error_map_image;
|
|
|
|
mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error_unsupported_map;
|
2014-07-23 16:54:13 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
}
|
2014-07-23 16:54:13 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
switch (mem->map_type) {
|
2014-07-23 16:54:13 +00:00
|
|
|
case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!mem->proxy)
|
|
|
|
goto error_no_surface_proxy;
|
|
|
|
data = mem->proxy;
|
|
|
|
break;
|
2014-07-23 16:54:13 +00:00
|
|
|
case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!mem->image)
|
|
|
|
goto error_no_image;
|
|
|
|
data = get_image_data (mem->image);
|
|
|
|
break;
|
2014-07-23 16:54:13 +00:00
|
|
|
default:
|
2014-08-21 08:45:31 +00:00
|
|
|
goto error_unsupported_map_type;
|
|
|
|
}
|
|
|
|
mem->map_count++;
|
|
|
|
return data;
|
2013-06-05 09:01:51 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
/* ERRORS */
|
2014-07-23 16:54:13 +00:00
|
|
|
error_unsupported_map:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("unsupported map flags (0x%x)", flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-23 16:54:13 +00:00
|
|
|
error_unsupported_map_type:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("unsupported map type (%d)", mem->map_type);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-05 09:01:51 +00:00
|
|
|
error_no_surface_proxy:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to extract GstVaapiSurfaceProxy from video meta");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-23 21:49:53 +00:00
|
|
|
error_no_surface:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to extract VA surface from video buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-21 14:23:13 +00:00
|
|
|
error_no_current_surface:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to make surface current");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-23 16:54:13 +00:00
|
|
|
error_no_image:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to extract VA image from video buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-21 14:23:13 +00:00
|
|
|
error_no_current_image:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to make image current");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-23 16:54:13 +00:00
|
|
|
error_map_image:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to map VA image");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_unmap (GstVaapiVideoMemory * mem)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
if (mem->map_count == 1) {
|
|
|
|
switch (mem->map_type) {
|
|
|
|
case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
|
|
|
|
gst_vaapi_surface_proxy_replace (&mem->proxy, NULL);
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
|
|
|
|
gst_vaapi_image_unmap (mem->image);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error_incompatible_map;
|
2013-06-05 09:01:51 +00:00
|
|
|
}
|
2014-08-21 08:45:31 +00:00
|
|
|
mem->map_type = 0;
|
|
|
|
}
|
|
|
|
mem->map_count--;
|
|
|
|
return;
|
2013-06-05 09:01:51 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
/* ERRORS */
|
2013-06-05 09:01:51 +00:00
|
|
|
error_incompatible_map:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("incompatible map type (%d)", mem->map_type);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaapiVideoMemory *
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_copy (GstVaapiVideoMemory * mem,
|
2012-11-08 14:41:22 +00:00
|
|
|
gssize offset, gssize size)
|
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoMeta *meta;
|
|
|
|
GstMemory *out_mem;
|
|
|
|
gsize maxsize;
|
|
|
|
|
2014-08-21 09:12:39 +00:00
|
|
|
g_return_val_if_fail (mem, NULL);
|
|
|
|
g_return_val_if_fail (mem->meta, NULL);
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
/* XXX: this implements a soft-copy, i.e. underlying VA surfaces
|
|
|
|
are not copied */
|
|
|
|
(void) gst_memory_get_sizes (GST_MEMORY_CAST (mem), NULL, &maxsize);
|
|
|
|
if (offset != 0 || (size != -1 && (gsize) size != maxsize))
|
|
|
|
goto error_unsupported;
|
|
|
|
|
2014-11-21 14:23:13 +00:00
|
|
|
if (!ensure_surface_is_current (mem))
|
|
|
|
goto error_no_current_surface;
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
meta = gst_vaapi_video_meta_copy (mem->meta);
|
|
|
|
if (!meta)
|
|
|
|
goto error_allocate_memory;
|
|
|
|
|
|
|
|
out_mem = gst_vaapi_video_memory_new (GST_MEMORY_CAST (mem)->allocator, meta);
|
|
|
|
gst_vaapi_video_meta_unref (meta);
|
|
|
|
if (!out_mem)
|
|
|
|
goto error_allocate_memory;
|
|
|
|
return GST_VAAPI_VIDEO_MEMORY_CAST (out_mem);
|
|
|
|
|
|
|
|
/* ERRORS */
|
2014-11-21 14:23:13 +00:00
|
|
|
error_no_current_surface:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to make surface current");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-05 09:01:51 +00:00
|
|
|
error_unsupported:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to copy partial memory (unsupported operation)");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-05 09:01:51 +00:00
|
|
|
error_allocate_memory:
|
2016-10-20 16:12:04 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to allocate GstVaapiVideoMemory copy");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaapiVideoMemory *
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_share (GstVaapiVideoMemory * mem,
|
2012-11-08 14:41:22 +00:00
|
|
|
gssize offset, gssize size)
|
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_FIXME ("unimplemented GstVaapiVideoAllocator::mem_share() hook");
|
|
|
|
return NULL;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_is_span (GstVaapiVideoMemory * mem1,
|
|
|
|
GstVaapiVideoMemory * mem2, gsize * offset_ptr)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_FIXME ("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
|
|
|
|
return FALSE;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiVideoAllocator --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
#define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST ((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
|
|
|
|
GstVaapiVideoAllocatorClass))
|
2012-11-08 14:41:22 +00:00
|
|
|
|
|
|
|
#define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
|
2014-08-21 08:45:31 +00:00
|
|
|
(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
G_DEFINE_TYPE (GstVaapiVideoAllocator,
|
|
|
|
gst_vaapi_video_allocator, GST_TYPE_ALLOCATOR);
|
2012-11-08 14:41:22 +00:00
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_allocator_free (GstAllocator * allocator, GstMemory * mem)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_memory_free (GST_VAAPI_VIDEO_MEMORY_CAST (mem));
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_allocator_finalize (GObject * object)
|
2013-10-09 07:33:56 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoAllocator *const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST (object);
|
2013-10-09 07:33:56 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_pool_replace (&allocator->surface_pool, NULL);
|
|
|
|
gst_vaapi_video_pool_replace (&allocator->image_pool, NULL);
|
2013-10-09 07:33:56 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
G_OBJECT_CLASS (gst_vaapi_video_allocator_parent_class)->finalize (object);
|
2013-10-09 07:33:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_allocator_class_init (GstVaapiVideoAllocatorClass * 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);
|
|
|
|
GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_debug_vaapivideomemory,
|
|
|
|
"vaapivideomemory", 0, "VA-API video memory allocator");
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
object_class->finalize = gst_vaapi_video_allocator_finalize;
|
|
|
|
allocator_class->free = gst_vaapi_video_allocator_free;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_allocator_init (GstVaapiVideoAllocator * allocator)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
|
|
|
|
base_allocator->mem_map = (GstMemoryMapFunction)
|
|
|
|
gst_vaapi_video_memory_map;
|
|
|
|
base_allocator->mem_unmap = (GstMemoryUnmapFunction)
|
|
|
|
gst_vaapi_video_memory_unmap;
|
|
|
|
base_allocator->mem_copy = (GstMemoryCopyFunction)
|
|
|
|
gst_vaapi_video_memory_copy;
|
|
|
|
base_allocator->mem_share = (GstMemoryShareFunction)
|
|
|
|
gst_vaapi_video_memory_share;
|
|
|
|
base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
|
|
|
|
gst_vaapi_video_memory_is_span;
|
2016-05-19 14:34:50 +00:00
|
|
|
|
|
|
|
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static gboolean
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_video_info_update_from_image (GstVideoInfo * vip, GstVaapiImage * image)
|
2013-04-09 14:02:06 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVideoFormat format;
|
|
|
|
const guchar *data;
|
|
|
|
guint i, num_planes, data_size, width, height;
|
|
|
|
|
|
|
|
/* Reset format from image */
|
|
|
|
format = gst_vaapi_image_get_format (image);
|
|
|
|
gst_vaapi_image_get_size (image, &width, &height);
|
|
|
|
gst_video_info_set_format (vip, format, width, height);
|
|
|
|
|
|
|
|
num_planes = gst_vaapi_image_get_plane_count (image);
|
|
|
|
g_return_val_if_fail (num_planes == GST_VIDEO_INFO_N_PLANES (vip), FALSE);
|
|
|
|
|
|
|
|
/* Determine the base data pointer */
|
|
|
|
data = get_image_data (image);
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
data_size = gst_vaapi_image_get_data_size (image);
|
|
|
|
|
|
|
|
/* Check that we don't have disjoint planes */
|
|
|
|
for (i = 0; i < num_planes; i++) {
|
|
|
|
const guchar *const plane = gst_vaapi_image_get_plane (image, i);
|
|
|
|
if (plane - data > data_size)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update GstVideoInfo structure */
|
|
|
|
for (i = 0; i < num_planes; i++) {
|
|
|
|
const guchar *const plane = gst_vaapi_image_get_plane (image, i);
|
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (vip, i) = plane - data;
|
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (vip, i) = gst_vaapi_image_get_pitch (image, i);
|
|
|
|
}
|
|
|
|
GST_VIDEO_INFO_SIZE (vip) = data_size;
|
|
|
|
return TRUE;
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 14:03:43 +00:00
|
|
|
static inline void
|
|
|
|
allocator_configure_surface_info (GstVaapiDisplay * display,
|
|
|
|
GstVaapiVideoAllocator * allocator)
|
|
|
|
{
|
|
|
|
const GstVideoInfo *vinfo;
|
|
|
|
GstVaapiSurface *surface = NULL;
|
|
|
|
GstVaapiImage *image = NULL;
|
|
|
|
gboolean updated;
|
2015-07-23 18:07:59 +00:00
|
|
|
GstVideoFormat fmt;
|
2015-07-23 14:03:43 +00:00
|
|
|
|
|
|
|
vinfo = &allocator->video_info;
|
|
|
|
|
2015-07-23 18:07:59 +00:00
|
|
|
fmt = gst_vaapi_video_format_get_best_native (GST_VIDEO_INFO_FORMAT (vinfo));
|
|
|
|
gst_video_info_set_format (&allocator->surface_info, fmt,
|
2015-07-23 14:03:43 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (vinfo), GST_VIDEO_INFO_HEIGHT (vinfo));
|
|
|
|
|
|
|
|
/* nothing to configure */
|
2015-08-03 14:33:02 +00:00
|
|
|
if (USE_NATIVE_FORMATS ||
|
|
|
|
GST_VIDEO_INFO_FORMAT (vinfo) == GST_VIDEO_FORMAT_ENCODED)
|
2015-07-23 14:03:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
surface = new_surface (display, vinfo);
|
|
|
|
if (!surface)
|
|
|
|
goto bail;
|
|
|
|
image = gst_vaapi_surface_derive_image (surface);
|
|
|
|
if (!image)
|
|
|
|
goto bail;
|
|
|
|
if (!gst_vaapi_image_map (image))
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
updated = gst_video_info_update_from_image (&allocator->surface_info, image);
|
|
|
|
|
|
|
|
allocator->has_direct_rendering = !USE_NATIVE_FORMATS && updated &&
|
|
|
|
(GST_VAAPI_IMAGE_FORMAT (image) == GST_VIDEO_INFO_FORMAT (vinfo));
|
|
|
|
|
|
|
|
GST_INFO ("has direct-rendering for %s surfaces: %s",
|
|
|
|
GST_VIDEO_INFO_FORMAT_STRING (&allocator->surface_info),
|
|
|
|
allocator->has_direct_rendering ? "yes" : "no");
|
|
|
|
|
|
|
|
gst_vaapi_image_unmap (image);
|
|
|
|
|
|
|
|
bail:
|
|
|
|
if (surface)
|
|
|
|
gst_vaapi_object_unref (surface);
|
|
|
|
if (image)
|
|
|
|
gst_vaapi_object_unref (image);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
allocator_configure_image_info (GstVaapiDisplay * display,
|
|
|
|
GstVaapiVideoAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstVaapiImage *image = NULL;
|
|
|
|
const GstVideoInfo *vinfo;
|
|
|
|
|
|
|
|
if (allocator->has_direct_rendering) {
|
|
|
|
allocator->image_info = allocator->surface_info;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vinfo = &allocator->video_info;
|
2016-05-23 13:38:07 +00:00
|
|
|
allocator->image_info = *vinfo;
|
|
|
|
gst_video_info_force_nv12_if_encoded (&allocator->image_info);
|
2015-07-23 14:03:43 +00:00
|
|
|
|
|
|
|
image = new_image (display, &allocator->image_info);
|
|
|
|
if (!image)
|
|
|
|
goto bail;
|
|
|
|
if (!gst_vaapi_image_map (image))
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
gst_video_info_update_from_image (&allocator->image_info, image);
|
|
|
|
gst_vaapi_image_unmap (image);
|
|
|
|
|
|
|
|
bail:
|
|
|
|
if (image)
|
|
|
|
gst_vaapi_object_unref (image);
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
GstAllocator *
|
2014-08-21 08:45:31 +00:00
|
|
|
gst_vaapi_video_allocator_new (GstVaapiDisplay * display,
|
2016-10-20 09:19:50 +00:00
|
|
|
const GstVideoInfo * vip, guint surface_alloc_flags)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
2014-08-21 08:45:31 +00:00
|
|
|
GstVaapiVideoAllocator *allocator;
|
2013-04-10 12:37:42 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
g_return_val_if_fail (display != NULL, NULL);
|
|
|
|
g_return_val_if_fail (vip != NULL, NULL);
|
2014-07-24 04:46:22 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
allocator = g_object_new (GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
|
|
|
|
if (!allocator)
|
|
|
|
return NULL;
|
2013-10-09 07:33:56 +00:00
|
|
|
|
2014-08-21 08:45:31 +00:00
|
|
|
allocator->video_info = *vip;
|
|
|
|
|
2015-07-23 14:03:43 +00:00
|
|
|
allocator_configure_surface_info (display, allocator);
|
2015-01-27 10:44:12 +00:00
|
|
|
allocator->surface_pool = gst_vaapi_surface_pool_new_full (display,
|
2016-10-20 09:19:50 +00:00
|
|
|
&allocator->surface_info, surface_alloc_flags);
|
2014-08-21 08:45:31 +00:00
|
|
|
if (!allocator->surface_pool)
|
|
|
|
goto error_create_surface_pool;
|
|
|
|
|
2015-07-23 14:03:43 +00:00
|
|
|
allocator_configure_image_info (display, allocator);
|
2014-08-21 08:45:31 +00:00
|
|
|
allocator->image_pool = gst_vaapi_image_pool_new (display,
|
|
|
|
&allocator->image_info);
|
|
|
|
if (!allocator->image_pool)
|
|
|
|
goto error_create_image_pool;
|
2015-01-26 17:30:47 +00:00
|
|
|
|
|
|
|
gst_allocator_set_vaapi_video_info (GST_ALLOCATOR_CAST (allocator),
|
|
|
|
&allocator->image_info, 0);
|
2014-08-21 08:45:31 +00:00
|
|
|
return GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
/* ERRORS */
|
2014-07-24 04:46:22 +00:00
|
|
|
error_create_surface_pool:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to allocate VA surface pool");
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-24 04:46:22 +00:00
|
|
|
error_create_image_pool:
|
2014-08-21 08:45:31 +00:00
|
|
|
{
|
|
|
|
GST_ERROR ("failed to allocate VA image pool");
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
2015-01-26 17:30:47 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiDmaBufMemory --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#define GST_VAAPI_BUFFER_PROXY_QUARK gst_vaapi_buffer_proxy_quark_get ()
|
|
|
|
static GQuark
|
|
|
|
gst_vaapi_buffer_proxy_quark_get (void)
|
|
|
|
{
|
|
|
|
static gsize g_quark;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&g_quark)) {
|
|
|
|
gsize quark = (gsize) g_quark_from_static_string ("GstVaapiBufferProxy");
|
|
|
|
g_once_init_leave (&g_quark, quark);
|
|
|
|
}
|
|
|
|
return g_quark;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstMemory *
|
|
|
|
gst_vaapi_dmabuf_memory_new (GstAllocator * allocator, GstVaapiVideoMeta * meta)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVaapiDisplay *display;
|
|
|
|
GstVaapiSurface *surface;
|
|
|
|
GstVaapiSurfaceProxy *proxy;
|
|
|
|
GstVaapiBufferProxy *dmabuf_proxy;
|
2015-03-02 10:12:53 +00:00
|
|
|
gint dmabuf_fd;
|
2015-01-26 17:30:47 +00:00
|
|
|
const GstVideoInfo *vip;
|
|
|
|
guint flags;
|
|
|
|
|
|
|
|
g_return_val_if_fail (allocator != NULL, NULL);
|
|
|
|
g_return_val_if_fail (meta != NULL, NULL);
|
|
|
|
|
|
|
|
vip = gst_allocator_get_vaapi_video_info (allocator, &flags);
|
|
|
|
if (!vip)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
display = gst_vaapi_video_meta_get_display (meta);
|
|
|
|
if (!meta)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
surface = gst_vaapi_surface_new_full (display, vip, flags);
|
|
|
|
if (!surface)
|
|
|
|
goto error_create_surface;
|
|
|
|
|
|
|
|
proxy = gst_vaapi_surface_proxy_new (surface);
|
|
|
|
if (!proxy)
|
|
|
|
goto error_create_surface_proxy;
|
|
|
|
|
|
|
|
dmabuf_proxy = gst_vaapi_surface_get_dma_buf_handle (surface);
|
|
|
|
gst_vaapi_object_unref (surface);
|
|
|
|
if (!dmabuf_proxy)
|
|
|
|
goto error_create_dmabuf_proxy;
|
|
|
|
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy (meta, proxy);
|
|
|
|
gst_vaapi_surface_proxy_unref (proxy);
|
|
|
|
|
2016-10-19 14:39:54 +00:00
|
|
|
/* Need dup because GstDmabufMemory creates the GstFdMemory with flag
|
|
|
|
* GST_FD_MEMORY_FLAG_NONE. So when being freed it calls close on the fd
|
|
|
|
* because GST_FD_MEMORY_FLAG_DONT_CLOSE is not set. */
|
2015-03-02 10:12:53 +00:00
|
|
|
dmabuf_fd = gst_vaapi_buffer_proxy_get_handle (dmabuf_proxy);
|
|
|
|
if (dmabuf_fd < 0 || (dmabuf_fd = dup (dmabuf_fd)) < 0)
|
|
|
|
goto error_create_dmabuf_handle;
|
|
|
|
|
|
|
|
mem = gst_dmabuf_allocator_alloc (allocator, dmabuf_fd,
|
2015-01-26 17:30:47 +00:00
|
|
|
gst_vaapi_buffer_proxy_get_size (dmabuf_proxy));
|
|
|
|
if (!mem)
|
|
|
|
goto error_create_dmabuf_memory;
|
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (mem),
|
|
|
|
GST_VAAPI_BUFFER_PROXY_QUARK, dmabuf_proxy,
|
|
|
|
(GDestroyNotify) gst_vaapi_buffer_proxy_unref);
|
|
|
|
return mem;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_create_surface:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to create VA surface (format:%s size:%ux%u)",
|
|
|
|
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (vip)),
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error_create_surface_proxy:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to create VA surface proxy");
|
|
|
|
gst_vaapi_object_unref (surface);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error_create_dmabuf_proxy:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to export VA surface to DMABUF");
|
|
|
|
gst_vaapi_surface_proxy_unref (proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-02 10:12:53 +00:00
|
|
|
error_create_dmabuf_handle:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to duplicate DMABUF handle");
|
|
|
|
gst_vaapi_buffer_proxy_unref (dmabuf_proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-26 17:30:47 +00:00
|
|
|
error_create_dmabuf_memory:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to create DMABUF memory");
|
|
|
|
gst_vaapi_buffer_proxy_unref (dmabuf_proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiDmaBufAllocator --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
GstAllocator *
|
|
|
|
gst_vaapi_dmabuf_allocator_new (GstVaapiDisplay * display,
|
|
|
|
const GstVideoInfo * vip, guint flags)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = NULL;
|
|
|
|
GstVaapiSurface *surface = NULL;
|
|
|
|
GstVaapiImage *image = NULL;
|
|
|
|
GstVideoInfo alloc_info;
|
|
|
|
|
|
|
|
g_return_val_if_fail (display != NULL, NULL);
|
|
|
|
g_return_val_if_fail (vip != NULL, NULL);
|
|
|
|
|
|
|
|
do {
|
|
|
|
surface = gst_vaapi_surface_new_full (display, vip, flags);
|
|
|
|
if (!surface)
|
|
|
|
break;
|
|
|
|
|
|
|
|
image = gst_vaapi_surface_derive_image (surface);
|
|
|
|
if (!image || !gst_vaapi_image_map (image))
|
|
|
|
break;
|
|
|
|
|
|
|
|
gst_video_info_set_format (&alloc_info, GST_VIDEO_INFO_FORMAT (vip),
|
|
|
|
GST_VIDEO_INFO_WIDTH (vip), GST_VIDEO_INFO_HEIGHT (vip));
|
|
|
|
gst_video_info_update_from_image (&alloc_info, image);
|
|
|
|
gst_vaapi_image_unmap (image);
|
|
|
|
|
|
|
|
allocator = gst_dmabuf_allocator_new ();
|
|
|
|
if (!allocator)
|
|
|
|
break;
|
|
|
|
gst_allocator_set_vaapi_video_info (allocator, &alloc_info, flags);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
gst_vaapi_object_replace (&image, NULL);
|
|
|
|
gst_vaapi_object_replace (&surface, NULL);
|
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiVideoInfo = { GstVideoInfo, flags } --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
static GstVideoInfo *
|
|
|
|
gst_vaapi_video_info_copy (const GstVideoInfo * vip)
|
|
|
|
{
|
|
|
|
GstVideoInfo *out_vip;
|
|
|
|
|
|
|
|
out_vip = g_slice_new (GstVideoInfo);
|
|
|
|
if (!out_vip)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
gst_video_info_init (out_vip);
|
|
|
|
*out_vip = *vip;
|
|
|
|
return out_vip;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_video_info_free (GstVideoInfo * vip)
|
|
|
|
{
|
|
|
|
g_slice_free (GstVideoInfo, vip);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GST_VAAPI_TYPE_VIDEO_INFO gst_vaapi_video_info_get_type ()
|
|
|
|
static GType
|
|
|
|
gst_vaapi_video_info_get_type (void)
|
|
|
|
{
|
|
|
|
static gsize g_type;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&g_type)) {
|
|
|
|
GType type;
|
|
|
|
type = g_boxed_type_register_static ("GstVaapiVideoInfo",
|
|
|
|
(GBoxedCopyFunc) gst_vaapi_video_info_copy,
|
|
|
|
(GBoxedFreeFunc) gst_vaapi_video_info_free);
|
|
|
|
g_once_init_leave (&g_type, type);
|
|
|
|
}
|
|
|
|
return (GType) g_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GST_VAAPI_VIDEO_INFO_QUARK gst_vaapi_video_info_quark_get ()
|
|
|
|
static GQuark
|
|
|
|
gst_vaapi_video_info_quark_get (void)
|
|
|
|
{
|
|
|
|
static gsize g_quark;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&g_quark)) {
|
|
|
|
gsize quark = (gsize) g_quark_from_static_string ("GstVaapiVideoInfo");
|
|
|
|
g_once_init_leave (&g_quark, quark);
|
|
|
|
}
|
|
|
|
return g_quark;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INFO_QUARK info_quark_get ()
|
|
|
|
static GQuark
|
|
|
|
info_quark_get (void)
|
|
|
|
{
|
|
|
|
static gsize g_quark;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&g_quark)) {
|
|
|
|
gsize quark = (gsize) g_quark_from_static_string ("info");
|
|
|
|
g_once_init_leave (&g_quark, quark);
|
|
|
|
}
|
|
|
|
return g_quark;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FLAGS_QUARK flags_quark_get ()
|
|
|
|
static GQuark
|
|
|
|
flags_quark_get (void)
|
|
|
|
{
|
|
|
|
static gsize g_quark;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&g_quark)) {
|
|
|
|
gsize quark = (gsize) g_quark_from_static_string ("flags");
|
|
|
|
g_once_init_leave (&g_quark, quark);
|
|
|
|
}
|
|
|
|
return g_quark;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GstVideoInfo *
|
|
|
|
gst_allocator_get_vaapi_video_info (GstAllocator * allocator,
|
|
|
|
guint * out_flags_ptr)
|
|
|
|
{
|
|
|
|
const GstStructure *structure;
|
|
|
|
const GValue *value;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ALLOCATOR (allocator), NULL);
|
|
|
|
|
|
|
|
structure =
|
|
|
|
g_object_get_qdata (G_OBJECT (allocator), GST_VAAPI_VIDEO_INFO_QUARK);
|
|
|
|
if (!structure)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (out_flags_ptr) {
|
|
|
|
value = gst_structure_id_get_value (structure, FLAGS_QUARK);
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
*out_flags_ptr = g_value_get_uint (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
value = gst_structure_id_get_value (structure, INFO_QUARK);
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
return g_value_get_boxed (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_allocator_set_vaapi_video_info (GstAllocator * allocator,
|
|
|
|
const GstVideoInfo * vip, guint flags)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_ALLOCATOR (allocator), FALSE);
|
|
|
|
g_return_val_if_fail (vip != NULL, FALSE);
|
|
|
|
|
|
|
|
g_object_set_qdata_full (G_OBJECT (allocator), GST_VAAPI_VIDEO_INFO_QUARK,
|
|
|
|
gst_structure_new_id (GST_VAAPI_VIDEO_INFO_QUARK,
|
|
|
|
INFO_QUARK, GST_VAAPI_TYPE_VIDEO_INFO, vip,
|
|
|
|
FLAGS_QUARK, G_TYPE_UINT, flags, NULL),
|
|
|
|
(GDestroyNotify) gst_structure_free);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2016-05-20 16:46:14 +00:00
|
|
|
|
2016-07-29 13:13:29 +00:00
|
|
|
/**
|
|
|
|
* gst_allocator_get_vaapi_image_size:
|
|
|
|
* @allocator: a #GstAllocator instance.
|
|
|
|
* @size: (out) (optional): the VA image size created by @allocator.
|
|
|
|
*
|
|
|
|
* This function gets the size of the VA images instantiated by the
|
|
|
|
* @allocator.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @allocator is VA valid
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
gst_allocator_get_vaapi_image_size (GstAllocator * allocator, guint * size)
|
|
|
|
{
|
|
|
|
GstVaapiVideoAllocator *alloc;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
if (g_strcmp0 (allocator->mem_type, GST_VAAPI_VIDEO_MEMORY_NAME))
|
|
|
|
return FALSE;
|
|
|
|
alloc = GST_VAAPI_VIDEO_ALLOCATOR_CAST (allocator);
|
|
|
|
if (alloc && size)
|
|
|
|
*size = GST_VIDEO_INFO_SIZE (&alloc->image_info);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:46:14 +00:00
|
|
|
gboolean
|
|
|
|
gst_vaapi_is_dmabuf_allocator (GstAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstStructure *st;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
if (g_strcmp0 (allocator->mem_type, GST_ALLOCATOR_DMABUF) != 0)
|
|
|
|
return FALSE;
|
|
|
|
st = g_object_get_qdata (G_OBJECT (allocator), GST_VAAPI_VIDEO_INFO_QUARK);
|
|
|
|
return (st != NULL);
|
|
|
|
}
|