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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gst/vaapi/sysdeps.h"
|
|
|
|
#include "gstvaapivideomemory.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
|
|
|
|
#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) \
|
|
|
|
gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(vip))
|
|
|
|
#endif
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiVideoMemory --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static GstVaapiImage *
|
|
|
|
new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
|
|
|
|
{
|
2013-07-09 13:44:35 +00:00
|
|
|
return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
|
2013-04-09 14:02:06 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
ensure_image(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
2013-04-10 12:37:42 +00:00
|
|
|
if (!mem->image && mem->use_direct_rendering) {
|
|
|
|
mem->image = gst_vaapi_surface_derive_image(mem->surface);
|
|
|
|
if (!mem->image) {
|
|
|
|
GST_WARNING("failed to derive image, fallbacking to copy");
|
|
|
|
mem->use_direct_rendering = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
if (!mem->image) {
|
|
|
|
GstVaapiDisplay * const display =
|
|
|
|
gst_vaapi_video_meta_get_display(mem->meta);
|
|
|
|
|
|
|
|
mem->image = new_image(display, mem->image_info);
|
|
|
|
if (!mem->image)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gst_vaapi_video_meta_set_image(mem->meta, mem->image);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaapiSurface *
|
|
|
|
new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
|
|
|
|
{
|
2013-07-10 08:34:24 +00:00
|
|
|
GstVaapiSurface *surface;
|
2013-10-09 08:06:40 +00:00
|
|
|
GstVaapiChromaType chroma_type;
|
2013-07-10 08:34:24 +00:00
|
|
|
|
|
|
|
/* Try with explicit format first */
|
2013-10-09 08:06:40 +00:00
|
|
|
if (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)
|
2013-04-09 14:02:06 +00:00
|
|
|
return NULL;
|
2013-10-09 08:06:40 +00:00
|
|
|
return gst_vaapi_surface_new(display, chroma_type,
|
2013-04-09 14:02:06 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
|
|
|
|
}
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
static GstVaapiSurfaceProxy *
|
|
|
|
new_surface_proxy(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
|
|
|
GstVaapiVideoAllocator * const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
|
|
|
|
|
|
|
|
return gst_vaapi_surface_proxy_new_from_pool(
|
|
|
|
GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static gboolean
|
|
|
|
ensure_surface(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
2013-10-09 07:33:56 +00:00
|
|
|
if (!mem->proxy) {
|
|
|
|
gst_vaapi_surface_proxy_replace(&mem->proxy,
|
|
|
|
gst_vaapi_video_meta_get_surface_proxy(mem->meta));
|
2013-04-09 14:02:06 +00:00
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
if (!mem->proxy) {
|
|
|
|
mem->proxy = new_surface_proxy(mem);
|
|
|
|
if (!mem->proxy)
|
|
|
|
return FALSE;
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
|
|
|
|
}
|
|
|
|
mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
2013-10-09 07:33:56 +00:00
|
|
|
g_return_val_if_fail(mem->surface != NULL, FALSE);
|
2013-04-09 14:02:06 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
|
|
|
|
GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
GstVaapiVideoMemory * const mem =
|
2013-11-28 18:08:28 +00:00
|
|
|
GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
|
2013-04-09 14:02:06 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(mem, FALSE);
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
|
|
|
|
allocator), FALSE);
|
|
|
|
g_return_val_if_fail(mem->meta, FALSE);
|
|
|
|
|
2013-06-05 09:01:51 +00:00
|
|
|
if (mem->map_type &&
|
|
|
|
mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
|
|
|
|
goto error_incompatible_map;
|
2013-04-09 14:02:06 +00:00
|
|
|
|
2013-06-03 08:06:29 +00:00
|
|
|
/* Map for writing */
|
2013-04-09 14:02:06 +00:00
|
|
|
if (++mem->map_count == 1) {
|
|
|
|
if (!ensure_surface(mem))
|
|
|
|
goto error_ensure_surface;
|
|
|
|
if (!ensure_image(mem))
|
|
|
|
goto error_ensure_image;
|
2013-10-09 08:33:55 +00:00
|
|
|
|
|
|
|
// Check that we can actually map the surface, or image
|
|
|
|
if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE &&
|
|
|
|
!mem->use_direct_rendering)
|
|
|
|
goto error_unsupported_map;
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
if (!gst_vaapi_image_map(mem->image))
|
|
|
|
goto error_map_image;
|
2013-06-05 09:01:51 +00:00
|
|
|
mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
|
2013-04-09 14:02:06 +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:
|
|
|
|
{
|
|
|
|
GST_ERROR("incompatible map type (%d)", mem->map_type);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-04-09 14:02:06 +00:00
|
|
|
error_unsupported_map:
|
|
|
|
{
|
|
|
|
GST_ERROR("unsupported map flags (0x%x)", flags);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
error_ensure_surface:
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
error_ensure_image:
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
error_map_image:
|
|
|
|
{
|
|
|
|
GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
|
|
|
|
GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
|
|
|
|
GstMapInfo *info)
|
|
|
|
{
|
|
|
|
GstVaapiVideoMemory * const mem =
|
2013-11-28 18:08:28 +00:00
|
|
|
GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
|
2013-04-09 14:02:06 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(mem, FALSE);
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
|
|
|
|
allocator), FALSE);
|
|
|
|
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) {
|
2013-06-05 09:01:51 +00:00
|
|
|
mem->map_type = 0;
|
|
|
|
|
2013-06-03 08:06:29 +00:00
|
|
|
/* Unmap VA image used for read/writes */
|
|
|
|
if (info->flags & GST_MAP_READWRITE)
|
|
|
|
gst_vaapi_image_unmap(mem->image);
|
2013-04-09 14:02:06 +00:00
|
|
|
|
|
|
|
/* Commit VA image to surface */
|
2013-04-10 12:37:42 +00:00
|
|
|
if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
|
2013-04-09 14:02:06 +00:00
|
|
|
if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
|
|
|
|
goto error_upload_image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_upload_image:
|
|
|
|
{
|
|
|
|
GST_ERROR("failed to upload image");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
GstMemory *
|
|
|
|
gst_vaapi_video_memory_new(GstAllocator *base_allocator,
|
|
|
|
GstVaapiVideoMeta *meta)
|
|
|
|
{
|
|
|
|
GstVaapiVideoAllocator * const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
|
|
|
|
const GstVideoInfo *vip;
|
|
|
|
GstVaapiVideoMemory *mem;
|
|
|
|
|
|
|
|
mem = g_slice_new(GstVaapiVideoMemory);
|
|
|
|
if (!mem)
|
|
|
|
return NULL;
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
vip = &allocator->image_info;
|
2013-07-12 15:14:49 +00:00
|
|
|
gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
|
2012-11-08 14:41:22 +00:00
|
|
|
GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
|
|
|
|
|
2013-06-05 09:01:51 +00:00
|
|
|
mem->proxy = NULL;
|
2013-04-09 14:02:06 +00:00
|
|
|
mem->surface_info = &allocator->surface_info;
|
|
|
|
mem->surface = NULL;
|
|
|
|
mem->image_info = &allocator->image_info;
|
|
|
|
mem->image = NULL;
|
2012-11-08 14:41:22 +00:00
|
|
|
mem->meta = gst_vaapi_video_meta_ref(meta);
|
2013-06-05 09:01:51 +00:00
|
|
|
mem->map_type = 0;
|
2013-04-09 14:02:06 +00:00
|
|
|
mem->map_count = 0;
|
2013-04-10 12:37:42 +00:00
|
|
|
mem->use_direct_rendering = allocator->has_direct_rendering;
|
2012-11-08 14:41:22 +00:00
|
|
|
return GST_MEMORY_CAST(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
2013-10-09 07:33:56 +00:00
|
|
|
mem->surface = NULL;
|
|
|
|
gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
|
2013-05-06 12:43:38 +00:00
|
|
|
gst_vaapi_object_replace(&mem->image, NULL);
|
2012-11-08 14:41:22 +00:00
|
|
|
gst_vaapi_video_meta_unref(mem->meta);
|
2013-07-12 15:14:49 +00:00
|
|
|
gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
|
2012-11-08 14:41:22 +00:00
|
|
|
g_slice_free(GstVaapiVideoMemory, mem);
|
|
|
|
}
|
|
|
|
|
2013-10-09 07:47:18 +00:00
|
|
|
void
|
|
|
|
gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
|
|
|
mem->surface = NULL;
|
|
|
|
gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
|
|
|
|
if (mem->use_direct_rendering)
|
|
|
|
gst_vaapi_object_replace(&mem->image, NULL);
|
|
|
|
gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static gpointer
|
|
|
|
gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
|
|
|
|
{
|
2013-06-05 09:01:51 +00:00
|
|
|
if (mem->map_type &&
|
|
|
|
mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
|
|
|
|
goto error_incompatible_map;
|
|
|
|
|
|
|
|
if (mem->map_count == 0) {
|
|
|
|
gst_vaapi_surface_proxy_replace(&mem->proxy,
|
|
|
|
gst_vaapi_video_meta_get_surface_proxy(mem->meta));
|
|
|
|
if (!mem->proxy)
|
|
|
|
goto error_no_surface_proxy;
|
|
|
|
mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
|
|
|
|
}
|
|
|
|
mem->map_count++;
|
|
|
|
return mem->proxy;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_incompatible_map:
|
|
|
|
GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
|
|
|
|
return NULL;
|
|
|
|
error_no_surface_proxy:
|
|
|
|
GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
|
2012-11-08 14:41:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
|
|
|
|
{
|
2013-06-05 09:01:51 +00:00
|
|
|
if (mem->map_type &&
|
|
|
|
mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
|
|
|
|
goto error_incompatible_map;
|
|
|
|
|
|
|
|
if (--mem->map_count == 0) {
|
|
|
|
gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
|
|
|
|
mem->map_type = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_incompatible_map:
|
|
|
|
GST_ERROR("incompatible map type (%d)", mem->map_type);
|
|
|
|
return;
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaapiVideoMemory *
|
|
|
|
gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
|
|
|
|
gssize offset, gssize size)
|
|
|
|
{
|
2013-06-05 09:01:51 +00:00
|
|
|
GstMemory *out_mem;
|
|
|
|
|
|
|
|
if (offset != 0 || size != -1)
|
|
|
|
goto error_unsupported;
|
|
|
|
|
|
|
|
out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
|
|
|
|
mem->meta);
|
|
|
|
if (!out_mem)
|
|
|
|
goto error_allocate_memory;
|
|
|
|
return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_unsupported:
|
|
|
|
GST_ERROR("failed to copy partial memory (unsupported operation)");
|
|
|
|
return NULL;
|
|
|
|
error_allocate_memory:
|
|
|
|
GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
|
2012-11-08 14:41:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaapiVideoMemory *
|
|
|
|
gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
|
|
|
|
gssize offset, gssize size)
|
|
|
|
{
|
|
|
|
GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
|
|
|
|
GstVaapiVideoMemory *mem2, gsize *offset_ptr)
|
|
|
|
{
|
|
|
|
GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
/* --- GstVaapiVideoAllocator --- */
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST((klass), \
|
|
|
|
GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
|
|
|
|
GstVaapiVideoAllocatorClass))
|
|
|
|
|
|
|
|
#define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(GstVaapiVideoAllocator,
|
|
|
|
gst_vaapi_video_allocator,
|
|
|
|
GST_TYPE_ALLOCATOR)
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
|
|
|
|
GstAllocationParams *params)
|
|
|
|
{
|
|
|
|
g_warning("use gst_vaapi_video_memory_new() to allocate from "
|
|
|
|
"GstVaapiVideoMemory allocator");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
|
|
|
|
{
|
|
|
|
gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
|
|
|
|
}
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_video_allocator_finalize(GObject *object)
|
|
|
|
{
|
|
|
|
GstVaapiVideoAllocator * const allocator =
|
|
|
|
GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
|
|
|
|
|
|
|
|
gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
|
|
|
|
{
|
2013-10-09 07:33:56 +00:00
|
|
|
GObjectClass * const object_class = G_OBJECT_CLASS(klass);
|
2012-11-08 14:41:22 +00:00
|
|
|
GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
|
|
|
|
"vaapivideomemory", 0, "VA-API video memory allocator");
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
object_class->finalize = gst_vaapi_video_allocator_finalize;
|
2012-11-08 14:41:22 +00:00
|
|
|
allocator_class->alloc = gst_vaapi_video_allocator_alloc;
|
|
|
|
allocator_class->free = gst_vaapi_video_allocator_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
|
|
|
|
{
|
2013-07-10 08:34:24 +00:00
|
|
|
GstVideoFormat format;
|
2013-04-09 14:02:06 +00:00
|
|
|
const guchar *data;
|
2013-07-10 08:34:24 +00:00
|
|
|
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);
|
2013-04-09 14:02:06 +00:00
|
|
|
|
|
|
|
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 = gst_vaapi_image_get_plane(image, 0);
|
|
|
|
for (i = 1; i < num_planes; i++) {
|
|
|
|
const guchar * const plane = gst_vaapi_image_get_plane(image, i);
|
|
|
|
if (data > plane)
|
|
|
|
data = plane;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-08 14:41:22 +00:00
|
|
|
GstAllocator *
|
2013-10-09 15:25:10 +00:00
|
|
|
gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
|
2012-11-08 14:41:22 +00:00
|
|
|
{
|
|
|
|
GstVaapiVideoAllocator *allocator;
|
2013-04-10 12:37:42 +00:00
|
|
|
GstVaapiSurface *surface;
|
2013-04-09 14:02:06 +00:00
|
|
|
GstVaapiImage *image;
|
2012-11-08 14:41:22 +00:00
|
|
|
|
2013-05-23 16:22:50 +00:00
|
|
|
g_return_val_if_fail(display != NULL, NULL);
|
2013-10-09 15:25:10 +00:00
|
|
|
g_return_val_if_fail(vip != NULL, NULL);
|
2012-11-08 14:41:22 +00:00
|
|
|
|
|
|
|
allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
|
|
|
|
if (!allocator)
|
|
|
|
return NULL;
|
|
|
|
|
2013-10-09 15:25:10 +00:00
|
|
|
allocator->video_info = *vip;
|
2013-04-09 14:02:06 +00:00
|
|
|
gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
|
|
|
|
GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
|
|
|
|
|
2013-04-10 12:37:42 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
|
|
|
|
image = NULL;
|
|
|
|
do {
|
|
|
|
surface = new_surface(display, vip);
|
|
|
|
if (!surface)
|
|
|
|
break;
|
|
|
|
image = gst_vaapi_surface_derive_image(surface);
|
|
|
|
if (!image)
|
|
|
|
break;
|
2013-10-09 08:06:40 +00:00
|
|
|
if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
|
|
|
|
break;
|
2013-04-10 12:37:42 +00:00
|
|
|
if (!gst_vaapi_image_map(image))
|
|
|
|
break;
|
|
|
|
allocator->has_direct_rendering = gst_video_info_update_from_image(
|
|
|
|
&allocator->surface_info, image);
|
|
|
|
gst_vaapi_image_unmap(image);
|
|
|
|
GST_INFO("has direct-rendering for %s surfaces: %s",
|
|
|
|
GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
|
|
|
|
allocator->has_direct_rendering ? "yes" : "no");
|
|
|
|
} while (0);
|
2013-06-03 08:22:44 +00:00
|
|
|
if (surface)
|
|
|
|
gst_vaapi_object_unref(surface);
|
|
|
|
if (image)
|
|
|
|
gst_vaapi_object_unref(image);
|
2013-04-10 12:37:42 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 07:33:56 +00:00
|
|
|
allocator->surface_pool = gst_vaapi_surface_pool_new(display,
|
|
|
|
&allocator->surface_info);
|
|
|
|
if (!allocator->surface_pool)
|
|
|
|
goto error_create_pool;
|
|
|
|
|
2013-04-09 14:02:06 +00:00
|
|
|
allocator->image_info = *vip;
|
|
|
|
if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
|
|
|
|
gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
|
|
|
|
GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
|
|
|
|
|
2013-04-10 12:37:42 +00:00
|
|
|
if (allocator->has_direct_rendering)
|
|
|
|
allocator->image_info = allocator->surface_info;
|
|
|
|
else {
|
2013-04-09 14:02:06 +00:00
|
|
|
do {
|
|
|
|
image = new_image(display, &allocator->image_info);
|
|
|
|
if (!image)
|
|
|
|
break;
|
|
|
|
if (!gst_vaapi_image_map(image))
|
|
|
|
break;
|
|
|
|
gst_video_info_update_from_image(&allocator->image_info, image);
|
|
|
|
gst_vaapi_image_unmap(image);
|
|
|
|
} while (0);
|
2013-05-06 12:43:38 +00:00
|
|
|
gst_vaapi_object_unref(image);
|
2013-04-09 14:02:06 +00:00
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
return GST_ALLOCATOR_CAST(allocator);
|
2013-10-09 07:33:56 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_create_pool:
|
|
|
|
{
|
|
|
|
GST_ERROR("failed to allocate VA surface pool");
|
|
|
|
gst_object_unref(allocator);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-08 14:41:22 +00:00
|
|
|
}
|