2020-03-22 18:00:50 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2020 Igalia, S.L.
|
|
|
|
* Author: Víctor Jáquez <vjaquez@igalia.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstvaallocator
|
|
|
|
* @title: VA allocators
|
|
|
|
* @short_description: VA allocators
|
|
|
|
* @sources:
|
|
|
|
* - gstvaallocator.h
|
|
|
|
*
|
|
|
|
* There are two types of VA allocators:
|
|
|
|
*
|
|
|
|
* * #GstVaAllocator
|
|
|
|
* * #GstVaDmabufAllocator
|
|
|
|
*/
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstvaallocator.h"
|
|
|
|
|
2023-03-03 19:12:13 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2020-03-22 18:00:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2023-03-03 19:12:13 +00:00
|
|
|
#endif
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
#include "gstvasurfacecopy.h"
|
2022-02-23 07:40:19 +00:00
|
|
|
#include "gstvavideoformat.h"
|
2021-10-07 19:51:55 +00:00
|
|
|
#include "vasurfaceimage.h"
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2023-08-30 19:49:22 +00:00
|
|
|
#ifndef DRM_FORMAT_INVALID
|
|
|
|
#define DRM_FORMAT_INVALID 0
|
|
|
|
#endif
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_va_memory_debug
|
2021-10-05 11:36:56 +00:00
|
|
|
GST_DEBUG_CATEGORY (gst_va_memory_debug);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
_init_debug_category (void)
|
|
|
|
{
|
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2021-03-22 03:34:36 +00:00
|
|
|
static gsize _init = 0;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
if (g_once_init_enter (&_init)) {
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_va_memory_debug, "vamemory", 0, "VA memory");
|
|
|
|
g_once_init_leave (&_init, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:30:14 +00:00
|
|
|
/*=========================== Quarks for GstMemory ===========================*/
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
static GQuark
|
|
|
|
gst_va_buffer_surface_quark (void)
|
|
|
|
{
|
|
|
|
static gsize surface_quark = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&surface_quark)) {
|
|
|
|
GQuark quark = g_quark_from_string ("GstVaBufferSurface");
|
|
|
|
g_once_init_leave (&surface_quark, quark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface_quark;
|
|
|
|
}
|
|
|
|
|
2021-01-19 07:17:58 +00:00
|
|
|
static GQuark
|
|
|
|
gst_va_buffer_aux_surface_quark (void)
|
|
|
|
{
|
|
|
|
static gsize surface_quark = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&surface_quark)) {
|
|
|
|
GQuark quark = g_quark_from_string ("GstVaBufferAuxSurface");
|
|
|
|
g_once_init_leave (&surface_quark, quark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface_quark;
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:30:14 +00:00
|
|
|
/*========================= GstVaBufferSurface ===============================*/
|
|
|
|
|
|
|
|
typedef struct _GstVaBufferSurface GstVaBufferSurface;
|
|
|
|
struct _GstVaBufferSurface
|
|
|
|
{
|
|
|
|
GstVaDisplay *display;
|
|
|
|
VASurfaceID surface;
|
2021-02-12 17:43:00 +00:00
|
|
|
guint n_mems;
|
|
|
|
GstMemory *mems[GST_VIDEO_MAX_PLANES];
|
2021-03-22 03:34:36 +00:00
|
|
|
gint ref_count;
|
|
|
|
gint ref_mems_count;
|
2020-10-03 14:30:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_buffer_surface_unref (gpointer data)
|
|
|
|
{
|
|
|
|
GstVaBufferSurface *buf = data;
|
|
|
|
|
|
|
|
g_return_if_fail (buf && GST_IS_VA_DISPLAY (buf->display));
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&buf->ref_count)) {
|
|
|
|
GST_LOG_OBJECT (buf->display, "Destroying surface %#x", buf->surface);
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_surfaces (buf->display, &buf->surface, 1);
|
2020-10-03 14:30:14 +00:00
|
|
|
gst_clear_object (&buf->display);
|
2023-01-08 18:03:49 +00:00
|
|
|
g_free (buf);
|
2020-10-03 14:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstVaBufferSurface *
|
2023-06-04 16:51:56 +00:00
|
|
|
gst_va_buffer_surface_new (VASurfaceID surface)
|
2020-10-03 14:30:14 +00:00
|
|
|
{
|
2023-01-08 18:03:49 +00:00
|
|
|
GstVaBufferSurface *buf = g_new (GstVaBufferSurface, 1);
|
2020-10-03 14:30:14 +00:00
|
|
|
|
|
|
|
g_atomic_int_set (&buf->ref_count, 0);
|
2021-02-12 17:43:00 +00:00
|
|
|
g_atomic_int_set (&buf->ref_mems_count, 0);
|
2020-10-03 14:30:14 +00:00
|
|
|
buf->surface = surface;
|
|
|
|
buf->display = NULL;
|
2021-02-12 17:43:00 +00:00
|
|
|
buf->n_mems = 0;
|
2020-10-03 14:30:14 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
/*=========================== GstVaMemoryPool ================================*/
|
|
|
|
|
|
|
|
/* queue for disposed surfaces */
|
|
|
|
typedef struct _GstVaMemoryPool GstVaMemoryPool;
|
|
|
|
struct _GstVaMemoryPool
|
|
|
|
{
|
|
|
|
GstAtomicQueue *queue;
|
|
|
|
gint surface_count;
|
|
|
|
|
|
|
|
GMutex lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GST_VA_MEMORY_POOL_CAST(obj) ((GstVaMemoryPool *)obj)
|
|
|
|
#define GST_VA_MEMORY_POOL_LOCK(obj) g_mutex_lock (&GST_VA_MEMORY_POOL_CAST(obj)->lock)
|
|
|
|
#define GST_VA_MEMORY_POOL_UNLOCK(obj) g_mutex_unlock (&GST_VA_MEMORY_POOL_CAST(obj)->lock)
|
|
|
|
|
2020-10-04 08:01:31 +00:00
|
|
|
static void
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_init (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
self->queue = gst_atomic_queue_new (2);
|
|
|
|
|
|
|
|
g_mutex_init (&self->lock);
|
|
|
|
|
|
|
|
self->surface_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_memory_pool_finalize (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
g_mutex_clear (&self->lock);
|
|
|
|
|
|
|
|
gst_atomic_queue_unref (self->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_memory_pool_flush_unlocked (GstVaMemoryPool * self,
|
|
|
|
GstVaDisplay * display)
|
2020-10-04 08:01:31 +00:00
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVaBufferSurface *buf;
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
while ((mem = gst_atomic_queue_pop (self->queue))) {
|
2020-10-04 08:01:31 +00:00
|
|
|
/* destroy the surface */
|
|
|
|
buf = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_surface_quark ());
|
2020-10-07 08:16:27 +00:00
|
|
|
if (buf) {
|
|
|
|
if (g_atomic_int_dec_and_test (&buf->ref_count)) {
|
|
|
|
GST_LOG ("Destroying surface %#x", buf->surface);
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_surfaces (display, &buf->surface, 1);
|
2020-11-23 19:44:27 +00:00
|
|
|
self->surface_count -= 1; /* GstVaDmabufAllocator */
|
2023-01-08 18:03:49 +00:00
|
|
|
g_free (buf);
|
2020-10-07 08:16:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-23 19:44:27 +00:00
|
|
|
self->surface_count -= 1; /* GstVaAllocator */
|
2020-10-04 08:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
|
2020-11-17 12:17:03 +00:00
|
|
|
/* when mem are pushed available queue its allocator is unref,
|
|
|
|
* then now it is required to ref the allocator here because
|
|
|
|
* memory's finalize will unref it again */
|
|
|
|
gst_object_ref (mem->allocator);
|
2020-10-04 08:01:31 +00:00
|
|
|
gst_memory_unref (mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
static void
|
|
|
|
gst_va_memory_pool_flush (GstVaMemoryPool * self, GstVaDisplay * display)
|
|
|
|
{
|
|
|
|
GST_VA_MEMORY_POOL_LOCK (self);
|
|
|
|
gst_va_memory_pool_flush_unlocked (self, display);
|
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gst_va_memory_pool_push (GstVaMemoryPool * self, GstMemory * mem)
|
|
|
|
{
|
|
|
|
gst_atomic_queue_push (self->queue, gst_memory_ref (mem));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GstMemory *
|
|
|
|
gst_va_memory_pool_pop (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
return gst_atomic_queue_pop (self->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline GstMemory *
|
|
|
|
gst_va_memory_pool_peek (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
return gst_atomic_queue_peek (self->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline guint
|
|
|
|
gst_va_memory_pool_surface_count (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
return g_atomic_int_get (&self->surface_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gst_va_memory_pool_surface_inc (GstVaMemoryPool * self)
|
|
|
|
{
|
|
|
|
g_atomic_int_inc (&self->surface_count);
|
|
|
|
}
|
|
|
|
|
2020-10-03 14:30:14 +00:00
|
|
|
/*=========================== GstVaDmabufAllocator ===========================*/
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* GstVaDmabufAllocator:
|
|
|
|
*
|
|
|
|
* A pooled memory allocator backed by the DMABufs exported from a
|
|
|
|
* VASurfaceID. Also it is possible to import DMAbufs into a
|
|
|
|
* VASurfaceID.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2022-03-30 05:03:59 +00:00
|
|
|
typedef struct _GstVaDmabufAllocator GstVaDmabufAllocator;
|
|
|
|
typedef struct _GstVaDmabufAllocatorClass GstVaDmabufAllocatorClass;
|
|
|
|
|
2020-10-03 14:37:54 +00:00
|
|
|
struct _GstVaDmabufAllocator
|
|
|
|
{
|
|
|
|
GstDmaBufAllocator parent;
|
|
|
|
|
|
|
|
GstVaDisplay *display;
|
|
|
|
|
|
|
|
GstMemoryMapFunction parent_map;
|
2021-10-06 13:20:50 +00:00
|
|
|
GstMemoryCopyFunction parent_copy;
|
2020-10-03 14:37:54 +00:00
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
GstVideoInfoDmaDrm info;
|
2020-10-07 16:03:20 +00:00
|
|
|
guint usage_hint;
|
2020-11-17 12:18:37 +00:00
|
|
|
|
2021-10-06 13:20:50 +00:00
|
|
|
GstVaSurfaceCopy *copy;
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
GstVaMemoryPool pool;
|
2020-10-03 14:37:54 +00:00
|
|
|
};
|
|
|
|
|
2022-02-23 07:40:19 +00:00
|
|
|
struct _GstVaDmabufAllocatorClass
|
|
|
|
{
|
|
|
|
GstDmaBufAllocatorClass parent_class;
|
|
|
|
};
|
|
|
|
|
2020-10-03 14:37:54 +00:00
|
|
|
#define gst_va_dmabuf_allocator_parent_class dmabuf_parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstVaDmabufAllocator, gst_va_dmabuf_allocator,
|
|
|
|
GST_TYPE_DMABUF_ALLOCATOR, _init_debug_category ());
|
|
|
|
|
2021-11-23 13:10:55 +00:00
|
|
|
static GstVaSurfaceCopy *
|
|
|
|
_ensure_surface_copy (GstVaSurfaceCopy ** old, GstVaDisplay * display,
|
|
|
|
GstVideoInfo * info)
|
|
|
|
{
|
|
|
|
GstVaSurfaceCopy *surface_copy;
|
|
|
|
|
|
|
|
surface_copy = g_atomic_pointer_get (old);
|
|
|
|
if (!surface_copy) {
|
|
|
|
surface_copy = gst_va_surface_copy_new (display, info);
|
|
|
|
|
|
|
|
/* others create a new one and set it before us */
|
|
|
|
if (surface_copy &&
|
|
|
|
!g_atomic_pointer_compare_and_exchange (old, NULL, surface_copy)) {
|
|
|
|
gst_va_surface_copy_free (surface_copy);
|
|
|
|
surface_copy = g_atomic_pointer_get (old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface_copy;
|
|
|
|
}
|
|
|
|
|
2021-10-06 13:20:50 +00:00
|
|
|
/* If a buffer contains multiple memories (dmabuf objects) its very
|
|
|
|
* difficult to provide a realiable way to fast-copy single memories:
|
|
|
|
* While VA API sees surfaces with dependant dmabufs, GStreamer only
|
|
|
|
* copies dmabufs in isolation; trying to solve it while keeping a
|
|
|
|
* reference of the copied buffer and dmabuf index is very fragile. */
|
|
|
|
static GstMemory *
|
|
|
|
gst_va_dmabuf_mem_copy (GstMemory * gmem, gssize offset, gssize size)
|
|
|
|
{
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (gmem->allocator);
|
|
|
|
GstVaBufferSurface *buf;
|
|
|
|
gsize mem_size;
|
|
|
|
|
|
|
|
buf = gst_mini_object_get_qdata (GST_MINI_OBJECT (gmem),
|
|
|
|
gst_va_buffer_surface_quark ());
|
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
if (buf->n_mems > 1 && self->info.drm_modifier != DRM_FORMAT_MOD_LINEAR) {
|
2021-10-06 13:20:50 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to copy multi-dmabuf because non-linear "
|
2023-06-13 09:48:31 +00:00
|
|
|
"modifier: %#" G_GINT64_MODIFIER "x.", self->info.drm_modifier);
|
2021-10-06 13:20:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if it's full memory copy */
|
|
|
|
mem_size = gst_memory_get_sizes (gmem, NULL, NULL);
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem_size > offset ? mem_size - offset : 0;
|
|
|
|
|
|
|
|
/* @XXX: if one-memory buffer it's possible to copy */
|
|
|
|
if (offset == 0 && size == mem_size && buf->n_mems == 1) {
|
2021-11-22 08:07:27 +00:00
|
|
|
GstVaBufferSurface *buf_copy = NULL;
|
2021-10-06 13:20:50 +00:00
|
|
|
GstMemory *copy;
|
2021-11-23 13:10:55 +00:00
|
|
|
GstVaSurfaceCopy *copy_func;
|
2021-10-06 13:20:50 +00:00
|
|
|
|
|
|
|
GST_VA_MEMORY_POOL_LOCK (&self->pool);
|
|
|
|
copy = gst_va_memory_pool_pop (&self->pool);
|
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (&self->pool);
|
|
|
|
|
2021-11-22 08:07:27 +00:00
|
|
|
if (copy) {
|
|
|
|
gst_object_ref (copy->allocator);
|
|
|
|
|
|
|
|
buf_copy = gst_mini_object_get_qdata (GST_MINI_OBJECT (copy),
|
|
|
|
gst_va_buffer_surface_quark ());
|
|
|
|
|
|
|
|
g_assert (g_atomic_int_get (&buf_copy->ref_mems_count) == 0);
|
|
|
|
|
|
|
|
g_atomic_int_add (&buf_copy->ref_mems_count, 1);
|
|
|
|
} else {
|
2021-10-06 13:20:50 +00:00
|
|
|
GstBuffer *buffer = gst_buffer_new ();
|
2021-11-22 08:07:27 +00:00
|
|
|
|
2021-10-06 13:20:50 +00:00
|
|
|
if (!gst_va_dmabuf_allocator_setup_buffer (gmem->allocator, buffer)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to create a new dmabuf memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy = gst_buffer_get_memory (buffer, 0);
|
|
|
|
gst_buffer_unref (buffer);
|
2021-11-22 08:07:27 +00:00
|
|
|
|
|
|
|
buf_copy = gst_mini_object_get_qdata (GST_MINI_OBJECT (copy),
|
|
|
|
gst_va_buffer_surface_quark ());
|
2021-10-06 13:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (buf_copy->n_mems == 1);
|
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
copy_func =
|
|
|
|
_ensure_surface_copy (&self->copy, self->display, &self->info.vinfo);
|
|
|
|
if (copy_func
|
|
|
|
&& gst_va_surface_copy (copy_func, buf_copy->surface, buf->surface))
|
2021-10-06 13:20:50 +00:00
|
|
|
return copy;
|
|
|
|
|
|
|
|
gst_memory_unref (copy);
|
|
|
|
|
|
|
|
/* try system memory */
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
if (self->info.drm_modifier != DRM_FORMAT_MOD_LINEAR) {
|
2021-10-06 13:20:50 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to copy dmabuf because non-linear "
|
2023-06-13 09:48:31 +00:00
|
|
|
"modifier: %#" G_GINT64_MODIFIER "x.", self->info.drm_modifier);
|
2021-10-06 13:20:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallback to system memory */
|
|
|
|
return self->parent_copy (gmem, offset, size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
static gpointer
|
|
|
|
gst_va_dmabuf_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (gmem->allocator);
|
2020-10-06 17:54:26 +00:00
|
|
|
VASurfaceID surface = gst_va_memory_get_surface (gmem);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2024-01-23 14:34:15 +00:00
|
|
|
if (flags & GST_MAP_READWRITE) {
|
|
|
|
if (self->info.drm_modifier != DRM_FORMAT_MOD_LINEAR) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to map the dmabuf because the modifier "
|
|
|
|
"is: %#" G_GINT64_MODIFIER "x, which is not linear.",
|
|
|
|
self->info.drm_modifier);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-29 15:21:24 +00:00
|
|
|
|
2024-01-23 14:34:15 +00:00
|
|
|
if (!va_sync_surface (self->display, surface))
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
return self->parent_map (gmem, maxsize, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-09-30 17:35:14 +00:00
|
|
|
gst_va_dmabuf_allocator_finalize (GObject * object)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (object);
|
|
|
|
|
2021-10-06 13:20:50 +00:00
|
|
|
g_clear_pointer (&self->copy, gst_va_surface_copy_free);
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_finalize (&self->pool);
|
2020-11-17 12:17:03 +00:00
|
|
|
gst_clear_object (&self->display);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-09-30 17:35:14 +00:00
|
|
|
G_OBJECT_CLASS (dmabuf_parent_class)->finalize (object);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-09-30 17:35:14 +00:00
|
|
|
gst_va_dmabuf_allocator_dispose (GObject * object)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
2020-09-30 17:35:14 +00:00
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (object);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_flush_unlocked (&self->pool, self->display);
|
|
|
|
if (gst_va_memory_pool_surface_count (&self->pool) != 0) {
|
|
|
|
GST_WARNING_OBJECT (self, "Surfaces leaked: %d",
|
|
|
|
gst_va_memory_pool_surface_count (&self->pool));
|
|
|
|
}
|
2020-10-07 08:16:27 +00:00
|
|
|
|
2020-09-30 17:35:14 +00:00
|
|
|
G_OBJECT_CLASS (dmabuf_parent_class)->dispose (object);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_dmabuf_allocator_class_init (GstVaDmabufAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->dispose = gst_va_dmabuf_allocator_dispose;
|
2020-09-30 17:35:14 +00:00
|
|
|
object_class->finalize = gst_va_dmabuf_allocator_finalize;
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_dmabuf_allocator_init (GstVaDmabufAllocator * self)
|
|
|
|
{
|
2021-10-06 13:20:50 +00:00
|
|
|
GstAllocator *allocator = GST_ALLOCATOR (self);
|
|
|
|
|
|
|
|
self->parent_map = allocator->mem_map;
|
|
|
|
allocator->mem_map = gst_va_dmabuf_mem_map;
|
|
|
|
self->parent_copy = allocator->mem_copy;
|
|
|
|
allocator->mem_copy = gst_va_dmabuf_mem_copy;
|
|
|
|
|
2023-06-14 08:32:52 +00:00
|
|
|
gst_video_info_dma_drm_init (&self->info);
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_init (&self->pool);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_new:
|
|
|
|
* @display: a #GstVaDisplay
|
|
|
|
*
|
|
|
|
* Instanciate a new pooled allocator backed with both DMABuf and
|
|
|
|
* VASurfaceID.
|
|
|
|
*
|
|
|
|
* Returns: a new allocated #GstAllocator
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-03-22 18:00:50 +00:00
|
|
|
GstAllocator *
|
|
|
|
gst_va_dmabuf_allocator_new (GstVaDisplay * display)
|
|
|
|
{
|
|
|
|
GstVaDmabufAllocator *self;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_DISPLAY (display), NULL);
|
|
|
|
|
|
|
|
self = g_object_new (GST_TYPE_VA_DMABUF_ALLOCATOR, NULL);
|
|
|
|
self->display = gst_object_ref (display);
|
|
|
|
gst_object_ref_sink (self);
|
|
|
|
|
|
|
|
return GST_ALLOCATOR (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline goffset
|
|
|
|
_get_fd_size (gint fd)
|
|
|
|
{
|
2023-03-03 19:12:13 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2020-03-22 18:00:50 +00:00
|
|
|
return lseek (fd, 0, SEEK_END);
|
2023-03-03 19:12:13 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2020-09-30 13:53:39 +00:00
|
|
|
gst_va_dmabuf_memory_release (GstMiniObject * mini_object)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
GstMemory *mem = GST_MEMORY_CAST (mini_object);
|
2021-02-12 17:43:00 +00:00
|
|
|
GstVaBufferSurface *buf;
|
2020-03-22 18:00:50 +00:00
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (mem->allocator);
|
2021-02-12 17:43:00 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
buf = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_surface_quark ());
|
|
|
|
if (!buf)
|
|
|
|
return TRUE; /* free this unknown buffer */
|
|
|
|
|
|
|
|
/* if this is the last reference to the GstVaBufferSurface, iterates
|
|
|
|
* its array of memories to push them into the queue with thread
|
|
|
|
* safetly. */
|
|
|
|
GST_VA_MEMORY_POOL_LOCK (&self->pool);
|
|
|
|
if (g_atomic_int_dec_and_test (&buf->ref_mems_count)) {
|
|
|
|
for (i = 0; i < buf->n_mems; i++) {
|
|
|
|
GST_LOG_OBJECT (self, "releasing %p: dmabuf %d, va surface %#x",
|
|
|
|
buf->mems[i], gst_dmabuf_memory_get_fd (buf->mems[i]), buf->surface);
|
|
|
|
gst_va_memory_pool_push (&self->pool, buf->mems[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (&self->pool);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
/* note: if ref_mem_count doesn't reach zero, that memory will
|
|
|
|
* "float" until it's pushed back into the pool by the last va
|
|
|
|
* buffer surface ref */
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-11-17 12:17:03 +00:00
|
|
|
/* Keep last in case we are holding on the last allocator ref */
|
|
|
|
gst_object_unref (mem->allocator);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
|
|
|
/* don't call mini_object's free */
|
|
|
|
return FALSE;
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
static gboolean
|
2023-02-22 08:25:32 +00:00
|
|
|
_modifier_found (guint64 modifier, guint64 * modifiers, guint num_modifiers)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
/* user doesn't care the returned modifier */
|
|
|
|
if (num_modifiers == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
for (i = 0; i < num_modifiers; i++)
|
|
|
|
if (modifier == modifiers[i])
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_va_create_surface_and_export_to_dmabuf (GstVaDisplay * display,
|
|
|
|
guint usage_hint, guint64 * modifiers, guint num_modifiers,
|
|
|
|
GstVideoInfo * info, VASurfaceID * ret_surface,
|
|
|
|
VADRMPRIMESurfaceDescriptor * ret_desc)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
VADRMPRIMESurfaceDescriptor desc = { 0, };
|
2023-02-22 08:25:32 +00:00
|
|
|
guint32 i, fourcc, rt_format, export_flags;
|
|
|
|
GstVideoFormat format;
|
2020-03-22 18:00:50 +00:00
|
|
|
VASurfaceID surface;
|
2023-06-14 08:32:52 +00:00
|
|
|
guint64 prev_modifier = DRM_FORMAT_MOD_INVALID;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2023-02-22 08:25:32 +00:00
|
|
|
_init_debug_category ();
|
|
|
|
|
|
|
|
format = GST_VIDEO_INFO_FORMAT (info);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
fourcc = gst_va_fourcc_from_video_format (format);
|
|
|
|
rt_format = gst_va_chroma_from_video_format (format);
|
2023-02-22 08:25:32 +00:00
|
|
|
if (fourcc == 0 || rt_format == 0)
|
2020-03-22 18:00:50 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-02-22 08:25:32 +00:00
|
|
|
if (!va_create_surfaces (display, rt_format, fourcc,
|
|
|
|
GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info),
|
2023-08-30 19:34:58 +00:00
|
|
|
usage_hint, modifiers, num_modifiers, NULL, &surface, 1))
|
2020-03-22 18:00:50 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2021-02-15 14:54:11 +00:00
|
|
|
/* workaround for missing layered dmabuf formats in i965 */
|
2023-02-22 08:25:32 +00:00
|
|
|
if (GST_VA_DISPLAY_IS_IMPLEMENTATION (display, INTEL_I965)
|
2021-02-15 14:54:11 +00:00
|
|
|
&& (fourcc == VA_FOURCC_YUY2 || fourcc == VA_FOURCC_UYVY)) {
|
2020-08-20 11:49:33 +00:00
|
|
|
/* These are not representable as separate planes */
|
|
|
|
export_flags = VA_EXPORT_SURFACE_COMPOSED_LAYERS;
|
|
|
|
} else {
|
|
|
|
/* Each layer will contain exactly one plane. For example, an NV12
|
|
|
|
* surface will be exported as two layers */
|
|
|
|
export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
export_flags |= VA_EXPORT_SURFACE_READ_WRITE;
|
|
|
|
|
2023-02-22 08:25:32 +00:00
|
|
|
if (!va_export_surface_to_dmabuf (display, surface, export_flags, &desc))
|
2020-03-22 18:00:50 +00:00
|
|
|
goto failed;
|
|
|
|
|
2023-02-22 08:25:32 +00:00
|
|
|
if (GST_VIDEO_INFO_N_PLANES (info) != desc.num_layers)
|
|
|
|
goto failed;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2023-11-30 11:00:27 +00:00
|
|
|
/* YUY2 and YUYV are the same. radeonsi returns always YUYV.
|
|
|
|
* There's no reason to fail if the different fourcc if there're dups.
|
|
|
|
* https://fourcc.org/pixel-format/yuv-yuy2/ */
|
2020-03-22 18:00:50 +00:00
|
|
|
if (fourcc != desc.fourcc) {
|
2023-11-30 11:00:27 +00:00
|
|
|
GST_INFO ("Different fourcc: requested %" GST_FOURCC_FORMAT " - returned %"
|
|
|
|
GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc),
|
2020-03-22 18:00:50 +00:00
|
|
|
GST_FOURCC_ARGS (desc.fourcc));
|
|
|
|
}
|
|
|
|
|
2021-10-12 09:32:30 +00:00
|
|
|
if (desc.num_objects == 0) {
|
|
|
|
GST_ERROR ("Failed to export surface to dmabuf");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2023-02-22 08:25:32 +00:00
|
|
|
for (i = 0; i < desc.num_objects; i++) {
|
|
|
|
guint64 modifier = desc.objects[i].drm_format_modifier;
|
|
|
|
|
|
|
|
if (!_modifier_found (modifier, modifiers, num_modifiers)) {
|
|
|
|
GST_ERROR ("driver set a modifier different from allowed list: "
|
|
|
|
"0x%016" G_GINT64_MODIFIER "x", modifier);
|
|
|
|
goto failed;
|
|
|
|
}
|
2023-06-13 09:48:31 +00:00
|
|
|
/* XXX: all dmabufs in buffer have to have the same modifier, otherwise the
|
|
|
|
* drm-format field in caps is ill-designed */
|
2023-02-22 08:25:32 +00:00
|
|
|
if (i > 0 && modifier != prev_modifier) {
|
|
|
|
GST_ERROR ("Different objects have different modifier");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_modifier = modifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_surface = surface;
|
|
|
|
if (ret_desc)
|
|
|
|
*ret_desc = desc;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
{
|
|
|
|
va_destroy_surfaces (display, &surface, 1);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_get_modifier_for_format:
|
|
|
|
* @display: a #GstVaDisplay
|
|
|
|
* @format: a #GstVideoFormat
|
|
|
|
* @usage_hint: VA usage hint
|
|
|
|
*
|
|
|
|
* Get the underlying modifier for specified @format and @usage_hint.
|
|
|
|
*
|
|
|
|
* Returns: the underlying modifier.
|
|
|
|
*
|
|
|
|
* Since: 1.24
|
|
|
|
*/
|
|
|
|
guint64
|
|
|
|
gst_va_dmabuf_get_modifier_for_format (GstVaDisplay * display,
|
|
|
|
GstVideoFormat format, guint usage_hint)
|
|
|
|
{
|
|
|
|
VADRMPRIMESurfaceDescriptor desc = { 0, };
|
|
|
|
VASurfaceID surface;
|
|
|
|
GstVideoInfo info;
|
|
|
|
|
|
|
|
gst_video_info_init (&info);
|
|
|
|
gst_video_info_set_format (&info, format, 64, 64);
|
|
|
|
|
|
|
|
if (!_va_create_surface_and_export_to_dmabuf (display, usage_hint,
|
|
|
|
NULL, 0, &info, &surface, &desc))
|
|
|
|
return DRM_FORMAT_MOD_INVALID;
|
|
|
|
|
|
|
|
va_destroy_surfaces (display, &surface, 1);
|
|
|
|
|
|
|
|
return desc.objects[0].drm_format_modifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates an exported VASurfaceID and adds it as @buffer's memories
|
|
|
|
* qdata
|
|
|
|
*
|
|
|
|
* If @info is not NULL, a dummy (non-pooled) buffer is created to
|
|
|
|
* update offsets and strides, and it has to be unrefed immediately.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_va_dmabuf_allocator_setup_buffer_full (GstAllocator * allocator,
|
2023-06-13 09:48:31 +00:00
|
|
|
GstBuffer * buffer, GstVideoInfoDmaDrm * info)
|
2023-02-22 08:25:32 +00:00
|
|
|
{
|
|
|
|
GstVaBufferSurface *buf;
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
|
|
|
VADRMPRIMESurfaceDescriptor desc = { 0, };
|
|
|
|
VASurfaceID surface;
|
|
|
|
guint32 i;
|
|
|
|
GDestroyNotify buffer_destroy = NULL;
|
|
|
|
gsize object_offset[4];
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_DMABUF_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
if (!_va_create_surface_and_export_to_dmabuf (self->display, self->usage_hint,
|
2023-06-14 08:32:52 +00:00
|
|
|
&self->info.drm_modifier, 1, &self->info.vinfo, &surface, &desc))
|
2023-02-22 08:25:32 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-06-04 16:51:56 +00:00
|
|
|
buf = gst_va_buffer_surface_new (surface);
|
2023-06-13 09:48:31 +00:00
|
|
|
if (G_UNLIKELY (info))
|
2020-10-07 16:03:20 +00:00
|
|
|
*info = self->info;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
buf->n_mems = desc.num_objects;
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
for (i = 0; i < desc.num_objects; i++) {
|
|
|
|
gint fd = desc.objects[i].fd;
|
2023-08-30 11:01:04 +00:00
|
|
|
/* prime descriptor reports the total size of the object, including regions
|
|
|
|
* which aren't part surface's space. Let's just grab the surface's size: */
|
2022-06-23 12:31:10 +00:00
|
|
|
gsize size = _get_fd_size (fd);
|
2020-03-22 18:00:50 +00:00
|
|
|
GstMemory *mem = gst_dmabuf_allocator_alloc (allocator, fd, size);
|
|
|
|
|
2023-08-30 11:01:04 +00:00
|
|
|
if (desc.objects[i].size < size) {
|
2022-06-23 12:31:10 +00:00
|
|
|
GST_WARNING_OBJECT (self, "driver bug: fd size (%" G_GSIZE_FORMAT
|
2023-08-30 11:01:04 +00:00
|
|
|
") is bigger than object descriptor size (%" G_GUINT32_FORMAT ")",
|
2022-06-23 12:31:10 +00:00
|
|
|
size, desc.objects[i].size);
|
|
|
|
}
|
|
|
|
|
2022-06-08 13:02:52 +00:00
|
|
|
object_offset[i] = gst_buffer_get_size (buffer);
|
2020-03-22 18:00:50 +00:00
|
|
|
gst_buffer_append_memory (buffer, mem);
|
2021-02-12 17:43:00 +00:00
|
|
|
buf->mems[i] = mem;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-11-13 19:20:47 +00:00
|
|
|
if (G_LIKELY (!info)) {
|
2020-10-07 16:03:20 +00:00
|
|
|
GST_MINI_OBJECT (mem)->dispose = gst_va_dmabuf_memory_release;
|
2021-02-12 17:43:00 +00:00
|
|
|
g_atomic_int_add (&buf->ref_mems_count, 1);
|
2020-11-13 19:20:47 +00:00
|
|
|
} else {
|
|
|
|
/* if no @info, surface will be destroyed as soon as buffer is
|
|
|
|
* destroyed (e.g. gst_va_dmabuf_allocator_try()) */
|
|
|
|
buf->display = gst_object_ref (self->display);
|
|
|
|
buffer_destroy = gst_va_buffer_surface_unref;
|
|
|
|
}
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
g_atomic_int_add (&buf->ref_count, 1);
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
|
2020-11-13 19:20:47 +00:00
|
|
|
gst_va_buffer_surface_quark (), buf, buffer_destroy);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
if (G_UNLIKELY (info)) {
|
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (&info->vinfo, i) =
|
|
|
|
GST_VIDEO_INFO_SIZE (&info->vinfo);
|
|
|
|
}
|
2021-02-08 11:25:07 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "buffer %p: new dmabuf %d / surface %#x [%dx%d] "
|
2022-04-01 19:59:58 +00:00
|
|
|
"size %" G_GSIZE_FORMAT " drm mod %#" G_GINT64_MODIFIER "x",
|
2023-06-13 09:48:31 +00:00
|
|
|
buffer, fd, surface, GST_VIDEO_INFO_WIDTH (&self->info.vinfo),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->info.vinfo), size,
|
|
|
|
self->info.drm_modifier);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
if (G_UNLIKELY (info)) {
|
2023-06-13 09:48:31 +00:00
|
|
|
if (desc.num_objects > 0) {
|
|
|
|
/* update drm modifier and format */
|
|
|
|
info->drm_modifier = desc.objects[0].drm_format_modifier;
|
|
|
|
info->drm_fourcc = gst_va_drm_fourcc_from_video_format
|
|
|
|
(GST_VIDEO_INFO_FORMAT (&self->info.vinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_VIDEO_INFO_SIZE (&info->vinfo) = gst_buffer_get_size (buffer);
|
2022-06-08 13:02:52 +00:00
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
for (i = 0; i < desc.num_layers; i++) {
|
|
|
|
g_assert (desc.layers[i].num_planes == 1);
|
2023-06-13 09:48:31 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (&info->vinfo, i) =
|
2022-06-08 13:02:52 +00:00
|
|
|
object_offset[desc.layers[i].object_index[0]] +
|
|
|
|
desc.layers[i].offset[0];
|
2023-06-13 09:48:31 +00:00
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (&info->vinfo, i) = desc.layers[i].pitch[0];
|
2020-10-07 16:03:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_surface_inc (&self->pool);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_setup_buffer:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @buffer: an empty #GstBuffer
|
|
|
|
*
|
2023-06-13 09:48:31 +00:00
|
|
|
* This function creates a new VASurfaceID and exposes its DMABufs,
|
2022-02-07 16:34:57 +00:00
|
|
|
* later it populates the @buffer with those DMABufs.
|
|
|
|
*
|
|
|
|
* Return: %TRUE if @buffer is populated correctly; %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_dmabuf_allocator_setup_buffer (GstAllocator * allocator,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
return gst_va_dmabuf_allocator_setup_buffer_full (allocator, buffer, NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:53:05 +00:00
|
|
|
static VASurfaceID
|
|
|
|
gst_va_dmabuf_allocator_prepare_buffer_unlocked (GstVaDmabufAllocator * self,
|
2020-09-30 17:35:14 +00:00
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
2021-02-12 17:43:00 +00:00
|
|
|
GstMemory *mems[GST_VIDEO_MAX_PLANES] = { 0, };
|
|
|
|
GstVaBufferSurface *buf;
|
|
|
|
gint i, j, idx;
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
mems[0] = gst_va_memory_pool_pop (&self->pool);
|
|
|
|
if (!mems[0])
|
2020-11-17 13:53:05 +00:00
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
buf = gst_mini_object_get_qdata (GST_MINI_OBJECT (mems[0]),
|
|
|
|
gst_va_buffer_surface_quark ());
|
|
|
|
if (!buf)
|
|
|
|
return VA_INVALID_ID;
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
if (buf->surface == VA_INVALID_ID)
|
|
|
|
return VA_INVALID_ID;
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
for (idx = 1; idx < buf->n_mems; idx++) {
|
|
|
|
/* grab next memory from queue */
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVaBufferSurface *pbuf;
|
|
|
|
|
|
|
|
mem = gst_va_memory_pool_peek (&self->pool);
|
|
|
|
if (!mem)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
|
|
|
pbuf = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_surface_quark ());
|
|
|
|
if (!pbuf)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
|
|
|
if (pbuf->surface != buf->surface) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"expecting memory with surface %#x but got %#x: "
|
|
|
|
"possible memory interweaving", buf->surface, pbuf->surface);
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
mems[idx] = gst_va_memory_pool_pop (&self->pool);
|
2020-11-17 13:53:05 +00:00
|
|
|
};
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
/* append memories */
|
|
|
|
for (i = 0; i < buf->n_mems; i++) {
|
|
|
|
gboolean found = FALSE;
|
|
|
|
|
|
|
|
/* find next memory to append */
|
|
|
|
for (j = 0; j < idx; j++) {
|
|
|
|
if (buf->mems[i] == mems[j]) {
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if not found, free all the popped memories and bail */
|
|
|
|
if (!found) {
|
|
|
|
if (!buf->display)
|
|
|
|
buf->display = gst_object_ref (self->display);
|
|
|
|
for (j = 0; j < idx; j++) {
|
|
|
|
gst_object_ref (buf->mems[j]->allocator);
|
|
|
|
GST_MINI_OBJECT (mems[j])->dispose = NULL;
|
|
|
|
gst_memory_unref (mems[j]);
|
|
|
|
}
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_atomic_int_add (&buf->ref_mems_count, 1);
|
|
|
|
gst_object_ref (buf->mems[i]->allocator);
|
|
|
|
gst_buffer_append_memory (buffer, buf->mems[i]);
|
2021-02-05 13:05:53 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
GST_LOG ("bufer %p: memory %p - dmabuf %d / surface %#x", buffer,
|
|
|
|
buf->mems[i], gst_dmabuf_memory_get_fd (buf->mems[i]),
|
|
|
|
gst_va_memory_get_surface (buf->mems[i]));
|
2020-11-17 12:17:03 +00:00
|
|
|
}
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2021-02-12 17:43:00 +00:00
|
|
|
return buf->surface;
|
2020-11-17 13:53:05 +00:00
|
|
|
}
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_prepare_buffer:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @buffer: an empty #GstBuffer
|
|
|
|
*
|
|
|
|
* This method will populate @buffer with pooled VASurfaceID/DMABuf
|
|
|
|
* memories. It doesn't allocate new VASurfacesID.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @buffer was populated correctly; %FALSE
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-11-17 13:53:05 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_dmabuf_allocator_prepare_buffer (GstAllocator * allocator,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaDmabufAllocator *self;
|
2020-11-17 13:53:05 +00:00
|
|
|
VASurfaceID surface;
|
|
|
|
|
2022-02-23 07:40:19 +00:00
|
|
|
g_return_val_if_fail (GST_IS_VA_DMABUF_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
GST_VA_MEMORY_POOL_LOCK (&self->pool);
|
2020-11-17 13:53:05 +00:00
|
|
|
surface = gst_va_dmabuf_allocator_prepare_buffer_unlocked (self, buffer);
|
2020-11-23 19:44:27 +00:00
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (&self->pool);
|
2020-11-17 13:53:05 +00:00
|
|
|
|
|
|
|
return (surface != VA_INVALID_ID);
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_flush:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
|
|
|
* Removes all the memories in @allocator's pool.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-09-30 17:35:14 +00:00
|
|
|
void
|
|
|
|
gst_va_dmabuf_allocator_flush (GstAllocator * allocator)
|
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaDmabufAllocator *self;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_VA_DMABUF_ALLOCATOR (allocator));
|
|
|
|
|
|
|
|
self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_flush (&self->pool, self->display);
|
2020-09-30 17:35:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_try:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
|
|
|
* Try to allocate a test buffer in order to verify that the
|
|
|
|
* allocator's configuration is valid.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the configuration is valid; %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
static gboolean
|
|
|
|
gst_va_dmabuf_allocator_try (GstAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaDmabufAllocator *self;
|
2023-06-13 09:48:31 +00:00
|
|
|
GstVideoInfoDmaDrm info;
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2022-02-23 07:40:19 +00:00
|
|
|
g_return_val_if_fail (GST_IS_VA_DMABUF_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
|
|
|
info = self->info;
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
buffer = gst_buffer_new ();
|
|
|
|
ret = gst_va_dmabuf_allocator_setup_buffer_full (allocator, buffer, &info);
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
self->info = info;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_set_format:
|
|
|
|
* @allocator: a #GstAllocator
|
2023-06-14 08:32:52 +00:00
|
|
|
* @info: (in) (out caller-allocates) (not nullable): a #GstVideoInfoDmaDrm
|
2022-02-07 16:34:57 +00:00
|
|
|
* @usage_hint: VA usage hint
|
|
|
|
*
|
|
|
|
* Sets the configuration defined by @info and @usage_hint for
|
|
|
|
* @allocator, and it tries the configuration, if @allocator has not
|
|
|
|
* allocated memories yet.
|
|
|
|
*
|
2023-06-14 08:32:52 +00:00
|
|
|
* If @allocator has memory allocated already, and frame size, format
|
|
|
|
* and modifier in @info are the same as currently configured in
|
|
|
|
* @allocator, the rest of @info parameters are updated internally.
|
2022-02-07 16:34:57 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if the configuration is valid or updated; %FALSE if
|
|
|
|
* configuration is not valid or not updated.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-03-22 18:00:50 +00:00
|
|
|
gboolean
|
2020-10-07 16:03:20 +00:00
|
|
|
gst_va_dmabuf_allocator_set_format (GstAllocator * allocator,
|
2023-06-14 08:32:52 +00:00
|
|
|
GstVideoInfoDmaDrm * info, guint usage_hint)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
2020-10-07 16:03:20 +00:00
|
|
|
GstVaDmabufAllocator *self;
|
2020-03-22 18:00:50 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
g_return_val_if_fail (GST_IS_VA_DMABUF_ALLOCATOR (allocator), FALSE);
|
|
|
|
g_return_val_if_fail (info, FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
if (gst_va_memory_pool_surface_count (&self->pool) != 0) {
|
2023-06-14 08:32:52 +00:00
|
|
|
if (info->drm_modifier == self->info.drm_modifier
|
|
|
|
&& GST_VIDEO_INFO_FORMAT (&info->vinfo)
|
2023-06-13 09:48:31 +00:00
|
|
|
== GST_VIDEO_INFO_FORMAT (&self->info.vinfo)
|
2023-06-14 08:32:52 +00:00
|
|
|
&& GST_VIDEO_INFO_WIDTH (&info->vinfo)
|
2023-06-13 09:48:31 +00:00
|
|
|
== GST_VIDEO_INFO_WIDTH (&self->info.vinfo)
|
2023-06-14 08:32:52 +00:00
|
|
|
&& GST_VIDEO_INFO_HEIGHT (&info->vinfo)
|
2023-06-13 09:48:31 +00:00
|
|
|
== GST_VIDEO_INFO_HEIGHT (&self->info.vinfo)
|
2020-10-07 16:03:20 +00:00
|
|
|
&& usage_hint == self->usage_hint) {
|
2023-06-14 08:32:52 +00:00
|
|
|
*info = self->info; /* update callee info (offset & stride) */
|
2020-10-07 16:03:20 +00:00
|
|
|
return TRUE;
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
2020-10-07 16:03:20 +00:00
|
|
|
return FALSE;
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
2020-10-07 16:03:20 +00:00
|
|
|
|
|
|
|
self->usage_hint = usage_hint;
|
2023-06-14 08:32:52 +00:00
|
|
|
self->info = *info;
|
2020-10-07 16:03:20 +00:00
|
|
|
|
2021-10-06 13:20:50 +00:00
|
|
|
g_clear_pointer (&self->copy, gst_va_surface_copy_free);
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
ret = gst_va_dmabuf_allocator_try (allocator);
|
|
|
|
|
|
|
|
if (ret)
|
2023-06-14 08:32:52 +00:00
|
|
|
*info = self->info;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_allocator_get_format:
|
|
|
|
* @allocator: a #GstAllocator
|
2023-06-14 08:32:52 +00:00
|
|
|
* @info: (out) (optional): a #GstVideoInfoDmaDrm
|
2022-02-07 16:34:57 +00:00
|
|
|
* @usage_hint: (out) (optional): VA usage hint
|
|
|
|
*
|
|
|
|
* Gets current internal configuration of @allocator.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @allocator is already configured; %FALSE
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_dmabuf_allocator_get_format (GstAllocator * allocator,
|
2023-06-14 08:32:52 +00:00
|
|
|
GstVideoInfoDmaDrm * info, guint * usage_hint)
|
2020-10-07 16:03:20 +00:00
|
|
|
{
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (allocator);
|
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&self->info.vinfo) == GST_VIDEO_FORMAT_UNKNOWN)
|
2020-10-07 16:03:20 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (info)
|
2023-06-14 08:32:52 +00:00
|
|
|
*info = self->info;
|
2020-10-07 16:03:20 +00:00
|
|
|
if (usage_hint)
|
|
|
|
*usage_hint = self->usage_hint;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-08-30 19:34:58 +00:00
|
|
|
static gboolean
|
|
|
|
_is_fd_repeated (uintptr_t fds[GST_VIDEO_MAX_PLANES], guint cur, guint * prev)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
g_assert (cur <= GST_VIDEO_MAX_PLANES);
|
|
|
|
|
|
|
|
if (cur == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < cur; i++) {
|
|
|
|
if (fds[i] == fds[cur]) {
|
|
|
|
if (prev)
|
|
|
|
*prev = i;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_dmabuf_memories_setup:
|
|
|
|
* @display: a #GstVaDisplay
|
2023-08-30 19:49:22 +00:00
|
|
|
* @drm_info: a #GstVideoInfoDmaDrm
|
2022-02-07 16:34:57 +00:00
|
|
|
* @mem: (array fixed-size=4) (element-type GstMemory): Memories. One
|
|
|
|
* per plane.
|
2023-08-30 19:25:41 +00:00
|
|
|
* @fds: (array fixed-size=4) (element-type uintptr_t): array of
|
2022-02-07 16:34:57 +00:00
|
|
|
* DMABuf file descriptors.
|
|
|
|
* @offset: (array fixed-size=4) (element-type gsize): array of memory
|
|
|
|
* offsets.
|
|
|
|
* @usage_hint: VA usage hint.
|
|
|
|
*
|
|
|
|
* It imports the array of @mem, representing a single frame, into a
|
|
|
|
* VASurfaceID and it's attached into every @mem.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if frame is imported correctly into a VASurfaceID;
|
|
|
|
* %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 09:08:49 +00:00
|
|
|
/* XXX: use a surface pool to control the created surfaces */
|
2020-08-19 14:52:10 +00:00
|
|
|
gboolean
|
2023-08-30 19:49:22 +00:00
|
|
|
gst_va_dmabuf_memories_setup (GstVaDisplay * display,
|
|
|
|
GstVideoInfoDmaDrm * drm_info, GstMemory * mem[GST_VIDEO_MAX_PLANES],
|
|
|
|
uintptr_t fds[GST_VIDEO_MAX_PLANES], gsize offset[GST_VIDEO_MAX_PLANES],
|
|
|
|
guint usage_hint)
|
2020-08-19 14:52:10 +00:00
|
|
|
{
|
|
|
|
GstVideoFormat format;
|
|
|
|
GstVaBufferSurface *buf;
|
2023-08-30 19:49:22 +00:00
|
|
|
GstVideoInfo *info = &drm_info->vinfo;
|
2020-08-19 14:52:10 +00:00
|
|
|
/* *INDENT-OFF* */
|
2023-08-30 19:34:58 +00:00
|
|
|
VADRMPRIMESurfaceDescriptor desc = {
|
2020-08-19 14:52:10 +00:00
|
|
|
.width = GST_VIDEO_INFO_WIDTH (info),
|
|
|
|
.height = GST_VIDEO_INFO_HEIGHT (info),
|
2023-08-30 19:34:58 +00:00
|
|
|
/* GStreamer can only describe one PRIME layer */
|
|
|
|
.num_layers = 1,
|
2020-08-19 14:52:10 +00:00
|
|
|
};
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
VASurfaceID surface;
|
2023-08-30 19:49:22 +00:00
|
|
|
guint32 fourcc, rt_format, drm_fourcc;
|
|
|
|
guint64 drm_modifier;
|
2023-08-30 19:34:58 +00:00
|
|
|
guint i, j, prev, n_planes;
|
2020-08-19 14:52:10 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_DISPLAY (display), FALSE);
|
2023-08-30 19:25:41 +00:00
|
|
|
|
|
|
|
n_planes = GST_VIDEO_INFO_N_PLANES (info);
|
2020-08-19 14:52:10 +00:00
|
|
|
|
2023-08-30 19:49:22 +00:00
|
|
|
format = (drm_info->drm_fourcc == DRM_FORMAT_INVALID) ?
|
|
|
|
GST_VIDEO_INFO_FORMAT (info) :
|
|
|
|
gst_va_video_format_from_drm_fourcc (drm_info->drm_fourcc);
|
2020-08-19 14:52:10 +00:00
|
|
|
if (format == GST_VIDEO_FORMAT_UNKNOWN)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-08-30 19:49:22 +00:00
|
|
|
drm_fourcc = (drm_info->drm_fourcc == DRM_FORMAT_INVALID) ?
|
|
|
|
gst_va_drm_fourcc_from_video_format (format) : drm_info->drm_fourcc;
|
|
|
|
if (drm_fourcc == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2020-08-19 14:52:10 +00:00
|
|
|
rt_format = gst_va_chroma_from_video_format (format);
|
|
|
|
if (rt_format == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
fourcc = gst_va_fourcc_from_video_format (format);
|
|
|
|
if (fourcc == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-08-30 19:49:22 +00:00
|
|
|
drm_modifier = (drm_info->drm_modifier == DRM_FORMAT_MOD_INVALID) ?
|
|
|
|
DRM_FORMAT_MOD_LINEAR : drm_info->drm_modifier;
|
|
|
|
|
2023-08-30 19:34:58 +00:00
|
|
|
desc.fourcc = fourcc;
|
|
|
|
desc.layers[0].num_planes = n_planes;
|
2023-08-30 19:49:22 +00:00
|
|
|
desc.layers[0].drm_format = drm_fourcc;
|
2020-08-19 14:52:10 +00:00
|
|
|
|
2023-08-30 19:34:58 +00:00
|
|
|
for (i = j = 0; i < n_planes; i++) {
|
|
|
|
desc.layers[0].pitch[i] = GST_VIDEO_INFO_PLANE_STRIDE (info, i);
|
|
|
|
desc.layers[0].offset[i] = offset[i];
|
|
|
|
|
|
|
|
if (_is_fd_repeated (fds, i, &prev)) {
|
|
|
|
desc.layers[0].object_index[i] = prev;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.objects[j].fd = fds[i];
|
|
|
|
desc.objects[j].size = _get_fd_size (fds[i]);
|
2023-08-30 19:49:22 +00:00
|
|
|
desc.objects[j].drm_format_modifier = drm_modifier;
|
2023-08-30 19:34:58 +00:00
|
|
|
|
|
|
|
desc.layers[0].object_index[i] = j;
|
|
|
|
j++;
|
2020-08-19 14:52:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 19:34:58 +00:00
|
|
|
desc.num_objects = j;
|
|
|
|
|
|
|
|
ret = va_create_surfaces (display, rt_format, fourcc, desc.width, desc.height,
|
|
|
|
usage_hint, NULL, 0, &desc, &surface, 1);
|
2020-08-19 14:52:10 +00:00
|
|
|
if (!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-08-30 19:34:58 +00:00
|
|
|
GST_LOG_OBJECT (display, "Created surface %#x [%dx%d]", surface, desc.width,
|
|
|
|
desc.height);
|
2020-08-19 14:52:10 +00:00
|
|
|
|
2023-06-04 16:51:56 +00:00
|
|
|
buf = gst_va_buffer_surface_new (surface);
|
2020-08-19 14:52:10 +00:00
|
|
|
buf->display = gst_object_ref (display);
|
2021-02-12 17:43:00 +00:00
|
|
|
buf->n_mems = n_planes;
|
|
|
|
memcpy (buf->mems, mem, sizeof (buf->mems));
|
2020-08-19 14:52:10 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n_planes; i++) {
|
|
|
|
g_atomic_int_add (&buf->ref_count, 1);
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem[i]),
|
2020-10-03 14:30:14 +00:00
|
|
|
gst_va_buffer_surface_quark (), buf, gst_va_buffer_surface_unref);
|
2020-08-19 14:52:10 +00:00
|
|
|
GST_INFO_OBJECT (display, "setting surface %#x to dmabuf fd %d",
|
|
|
|
buf->surface, gst_dmabuf_memory_get_fd (mem[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
/*===================== GstVaAllocator / GstVaMemory =========================*/
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* GstVaAllocator:
|
|
|
|
*
|
|
|
|
* A pooled memory allocator backed by VASurfaceID.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2022-03-30 05:03:59 +00:00
|
|
|
typedef struct _GstVaAllocator GstVaAllocator;
|
|
|
|
typedef struct _GstVaAllocatorClass GstVaAllocatorClass;
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
struct _GstVaAllocator
|
|
|
|
{
|
|
|
|
GstAllocator parent;
|
|
|
|
|
|
|
|
GstVaDisplay *display;
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
gboolean use_derived;
|
2020-08-01 13:47:19 +00:00
|
|
|
GArray *surface_formats;
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-10-19 15:32:44 +00:00
|
|
|
GstVideoFormat surface_format;
|
|
|
|
GstVideoFormat img_format;
|
|
|
|
guint32 fourcc;
|
|
|
|
guint32 rt_format;
|
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
GstVideoInfo info;
|
|
|
|
guint usage_hint;
|
2020-11-17 12:18:37 +00:00
|
|
|
|
2022-06-24 17:42:36 +00:00
|
|
|
guint32 hacks;
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
GstVaSurfaceCopy *copy;
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
GstVaMemoryPool pool;
|
2020-03-22 18:00:50 +00:00
|
|
|
};
|
|
|
|
|
2022-02-23 07:40:19 +00:00
|
|
|
struct _GstVaAllocatorClass
|
|
|
|
{
|
|
|
|
GstAllocatorClass parent_class;
|
|
|
|
};
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
typedef struct _GstVaMemory GstVaMemory;
|
|
|
|
struct _GstVaMemory
|
|
|
|
{
|
2020-09-28 14:59:44 +00:00
|
|
|
GstMemory mem;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
VASurfaceID surface;
|
|
|
|
GstVideoFormat surface_format;
|
|
|
|
VAImage image;
|
|
|
|
gpointer mapped_data;
|
|
|
|
|
|
|
|
GstMapFlags prev_mapflags;
|
2021-03-22 03:34:36 +00:00
|
|
|
gint map_count;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
gboolean is_derived;
|
|
|
|
gboolean is_dirty;
|
|
|
|
GMutex lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstVaAllocator, gst_va_allocator, GST_TYPE_ALLOCATOR,
|
|
|
|
_init_debug_category ());
|
|
|
|
|
2020-09-28 14:59:44 +00:00
|
|
|
static gboolean _va_unmap (GstVaMemory * mem);
|
|
|
|
|
2020-09-30 17:35:14 +00:00
|
|
|
static void
|
|
|
|
gst_va_allocator_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstVaAllocator *self = GST_VA_ALLOCATOR (object);
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
g_clear_pointer (&self->copy, gst_va_surface_copy_free);
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_finalize (&self->pool);
|
2020-11-17 12:17:03 +00:00
|
|
|
g_clear_pointer (&self->surface_formats, g_array_unref);
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_clear_object (&self->display);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_va_allocator_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
static void
|
|
|
|
gst_va_allocator_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstVaAllocator *self = GST_VA_ALLOCATOR (object);
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_flush_unlocked (&self->pool, self->display);
|
|
|
|
if (gst_va_memory_pool_surface_count (&self->pool) != 0) {
|
|
|
|
GST_WARNING_OBJECT (self, "Surfaces leaked: %d",
|
|
|
|
gst_va_memory_pool_surface_count (&self->pool));
|
|
|
|
}
|
2020-10-07 08:16:27 +00:00
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
G_OBJECT_CLASS (gst_va_allocator_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_va_free (GstAllocator * allocator, GstMemory * mem)
|
|
|
|
{
|
|
|
|
GstVaAllocator *self = GST_VA_ALLOCATOR (allocator);
|
|
|
|
GstVaMemory *va_mem = (GstVaMemory *) mem;
|
|
|
|
|
2020-09-28 14:59:44 +00:00
|
|
|
if (va_mem->mapped_data) {
|
|
|
|
g_warning (G_STRLOC ":%s: Freeing memory %p still mapped", G_STRFUNC,
|
|
|
|
va_mem);
|
|
|
|
_va_unmap (va_mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (va_mem->surface != VA_INVALID_ID && mem->parent == NULL) {
|
|
|
|
GST_LOG_OBJECT (self, "Destroying surface %#x", va_mem->surface);
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_surfaces (self->display, &va_mem->surface, 1);
|
2020-09-28 14:59:44 +00:00
|
|
|
}
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
g_mutex_clear (&va_mem->lock);
|
|
|
|
|
2023-01-08 18:03:49 +00:00
|
|
|
g_free (va_mem);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_va_allocator_class_init (GstVaAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->dispose = gst_va_allocator_dispose;
|
2020-09-30 17:35:14 +00:00
|
|
|
object_class->finalize = gst_va_allocator_finalize;
|
2020-03-22 18:00:50 +00:00
|
|
|
allocator_class->free = _va_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
_clean_mem (GstVaMemory * mem)
|
|
|
|
{
|
|
|
|
memset (&mem->image, 0, sizeof (mem->image));
|
|
|
|
mem->image.image_id = VA_INVALID_ID;
|
|
|
|
mem->image.buf = VA_INVALID_ID;
|
|
|
|
|
2023-11-30 13:47:06 +00:00
|
|
|
mem->is_derived = FALSE;
|
2020-03-22 18:00:50 +00:00
|
|
|
mem->is_dirty = FALSE;
|
|
|
|
mem->prev_mapflags = 0;
|
|
|
|
mem->mapped_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_reset_mem (GstVaMemory * mem, GstAllocator * allocator, gsize size)
|
|
|
|
{
|
|
|
|
_clean_mem (mem);
|
|
|
|
g_atomic_int_set (&mem->map_count, 0);
|
|
|
|
g_mutex_init (&mem->lock);
|
|
|
|
|
2020-09-28 14:59:44 +00:00
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem), 0, allocator, NULL, size,
|
|
|
|
0 /* align */ , 0 /* offset */ , size);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 19:35:25 +00:00
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
static inline gboolean
|
2023-12-04 19:05:48 +00:00
|
|
|
_is_old_mesa (GstVaAllocator * va_allocator)
|
|
|
|
{
|
2023-12-11 19:35:25 +00:00
|
|
|
return GST_VA_DISPLAY_IS_IMPLEMENTATION (va_allocator->display, MESA_GALLIUM)
|
|
|
|
&& !gst_va_display_check_version (va_allocator->display, 23, 2);
|
2023-12-04 19:05:48 +00:00
|
|
|
}
|
2023-12-11 19:35:25 +00:00
|
|
|
#endif /* G_OS_WIN32 */
|
2023-12-04 19:05:48 +00:00
|
|
|
|
2021-03-31 09:52:07 +00:00
|
|
|
static inline void
|
|
|
|
_update_info (GstVideoInfo * info, const VAImage * image)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < image->num_planes; i++) {
|
|
|
|
GST_VIDEO_INFO_PLANE_OFFSET (info, i) = image->offsets[i];
|
|
|
|
GST_VIDEO_INFO_PLANE_STRIDE (info, i) = image->pitches[i];
|
|
|
|
}
|
|
|
|
|
2023-09-09 13:12:39 +00:00
|
|
|
/* Don't update image size for one planed images since drivers might add extra
|
|
|
|
* bits which will drop wrong raw images with filesink, for example. Multiple
|
|
|
|
* plane images require video meta */
|
|
|
|
if (image->num_planes > 1)
|
|
|
|
GST_VIDEO_INFO_SIZE (info) = image->data_size;
|
2021-03-31 09:52:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 06:09:35 +00:00
|
|
|
static inline gboolean
|
2023-12-11 16:28:06 +00:00
|
|
|
_update_image_info (GstVaAllocator * va_allocator,
|
|
|
|
GstVaFeature feat_use_derived)
|
2020-10-20 06:09:35 +00:00
|
|
|
{
|
|
|
|
VASurfaceID surface;
|
|
|
|
VAImage image = {.image_id = VA_INVALID_ID, };
|
|
|
|
|
|
|
|
/* Create a test surface first */
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_create_surfaces (va_allocator->display, va_allocator->rt_format,
|
2020-10-20 06:09:35 +00:00
|
|
|
va_allocator->fourcc, GST_VIDEO_INFO_WIDTH (&va_allocator->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&va_allocator->info), va_allocator->usage_hint,
|
2023-02-22 08:25:32 +00:00
|
|
|
NULL, 0, NULL, &surface, 1)) {
|
2020-10-20 06:09:35 +00:00
|
|
|
GST_ERROR_OBJECT (va_allocator, "Failed to create a test surface");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (va_allocator, "Created surface %#x [%dx%d]", surface,
|
|
|
|
GST_VIDEO_INFO_WIDTH (&va_allocator->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&va_allocator->info));
|
|
|
|
|
2023-10-16 07:28:21 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* XXX: Derived image is problematic for D3D backend */
|
2023-12-11 16:28:06 +00:00
|
|
|
if (feat_use_derived != GST_VA_FEATURE_DISABLED) {
|
2023-10-16 07:28:21 +00:00
|
|
|
GST_INFO_OBJECT (va_allocator, "Disable image derive on Windows.");
|
2023-12-11 16:28:06 +00:00
|
|
|
feat_use_derived = GST_VA_FEATURE_DISABLED;
|
2023-10-16 07:28:21 +00:00
|
|
|
}
|
|
|
|
va_allocator->use_derived = FALSE;
|
2023-12-04 19:05:48 +00:00
|
|
|
#else
|
|
|
|
/* XXX: Derived in Mesa <23.3 can't use derived images for P010 format
|
|
|
|
* https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24381
|
|
|
|
*/
|
|
|
|
if (va_allocator->img_format == GST_VIDEO_FORMAT_P010_10LE
|
|
|
|
&& _is_old_mesa (va_allocator)) {
|
2023-12-11 16:28:06 +00:00
|
|
|
if (feat_use_derived != GST_VA_FEATURE_DISABLED) {
|
2023-12-04 19:05:48 +00:00
|
|
|
GST_INFO_OBJECT (va_allocator, "Disable image derive on old Mesa.");
|
2023-12-11 16:28:06 +00:00
|
|
|
feat_use_derived = GST_VA_FEATURE_DISABLED;
|
2023-12-04 19:05:48 +00:00
|
|
|
}
|
|
|
|
va_allocator->use_derived = FALSE;
|
|
|
|
}
|
2023-10-16 07:28:21 +00:00
|
|
|
#endif
|
2020-10-20 06:09:35 +00:00
|
|
|
/* Try derived first, but different formats can never derive */
|
2023-12-11 16:28:06 +00:00
|
|
|
if (feat_use_derived != GST_VA_FEATURE_DISABLED
|
2022-03-29 12:52:33 +00:00
|
|
|
&& va_allocator->surface_format == va_allocator->img_format) {
|
2023-12-11 16:28:06 +00:00
|
|
|
va_allocator->use_derived =
|
|
|
|
va_get_derive_image (va_allocator->display, surface, &image);
|
|
|
|
if (va_allocator->use_derived)
|
2023-07-13 08:56:56 +00:00
|
|
|
goto done;
|
2021-03-31 09:52:07 +00:00
|
|
|
image.image_id = VA_INVALID_ID; /* reset it */
|
2020-10-20 06:09:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 16:28:06 +00:00
|
|
|
if (feat_use_derived == GST_VA_FEATURE_ENABLED && !va_allocator->use_derived)
|
2022-03-29 12:52:33 +00:00
|
|
|
GST_WARNING_OBJECT (va_allocator, "Derived images are disabled.");
|
|
|
|
|
2020-10-20 06:09:35 +00:00
|
|
|
/* Then we try to create a image. */
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_create_image (va_allocator->display, va_allocator->img_format,
|
2020-10-20 06:09:35 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&va_allocator->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&va_allocator->info), &image)) {
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_surfaces (va_allocator->display, &surface, 1);
|
2020-10-20 06:09:35 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-07-13 08:56:56 +00:00
|
|
|
done:
|
2021-03-31 09:52:07 +00:00
|
|
|
_update_info (&va_allocator->info, &image);
|
2023-09-09 13:12:39 +00:00
|
|
|
if (GST_VIDEO_INFO_SIZE (&va_allocator->info) > image.data_size) {
|
|
|
|
GST_WARNING_OBJECT (va_allocator,
|
|
|
|
"image size is lesser than the minimum required");
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_image (va_allocator->display, image.image_id);
|
|
|
|
va_destroy_surfaces (va_allocator->display, &surface, 1);
|
2020-10-20 06:09:35 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
static gpointer
|
|
|
|
_va_map_unlocked (GstVaMemory * mem, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = GST_MEMORY_CAST (mem)->allocator;
|
|
|
|
GstVaAllocator *va_allocator;
|
|
|
|
GstVaDisplay *display;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mem->surface != VA_INVALID_ID, NULL);
|
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), NULL);
|
|
|
|
|
|
|
|
if (g_atomic_int_get (&mem->map_count) > 0) {
|
2021-04-03 12:16:22 +00:00
|
|
|
if (!(mem->prev_mapflags & flags) || !mem->mapped_data)
|
2020-03-22 18:00:50 +00:00
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_allocator = GST_VA_ALLOCATOR (allocator);
|
|
|
|
display = va_allocator->display;
|
|
|
|
|
|
|
|
if (flags & GST_MAP_WRITE) {
|
|
|
|
mem->is_dirty = TRUE;
|
|
|
|
} else { /* GST_MAP_READ only */
|
|
|
|
mem->is_dirty = FALSE;
|
|
|
|
}
|
|
|
|
|
2020-08-02 15:52:50 +00:00
|
|
|
if (flags & GST_MAP_VA) {
|
|
|
|
mem->mapped_data = &mem->surface;
|
|
|
|
goto success;
|
|
|
|
}
|
2023-10-16 07:28:21 +00:00
|
|
|
|
2023-11-30 13:47:06 +00:00
|
|
|
mem->is_derived = va_allocator->use_derived;
|
2023-10-16 07:28:21 +00:00
|
|
|
|
2023-11-30 13:47:06 +00:00
|
|
|
if (!va_ensure_image (display, mem->surface, &va_allocator->info, &mem->image,
|
|
|
|
va_allocator->use_derived))
|
2020-03-22 18:00:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!mem->is_derived) {
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_get_image (display, mem->surface, &mem->image))
|
2020-03-22 18:00:50 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-10-12 08:39:39 +00:00
|
|
|
if (!va_map_buffer (display, mem->image.buf, flags, &mem->mapped_data))
|
2020-03-22 18:00:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
success:
|
|
|
|
{
|
|
|
|
mem->prev_mapflags = flags;
|
|
|
|
g_atomic_int_add (&mem->map_count, 1);
|
|
|
|
return mem->mapped_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
{
|
2021-10-07 19:51:55 +00:00
|
|
|
va_destroy_image (display, mem->image.image_id);
|
2020-03-22 18:00:50 +00:00
|
|
|
_clean_mem (mem);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
_va_map (GstVaMemory * mem, gsize maxsize, GstMapFlags flags)
|
|
|
|
{
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
g_mutex_lock (&mem->lock);
|
|
|
|
data = _va_map_unlocked (mem, flags);
|
|
|
|
g_mutex_unlock (&mem->lock);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_va_unmap_unlocked (GstVaMemory * mem)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = GST_MEMORY_CAST (mem)->allocator;
|
|
|
|
GstVaDisplay *display;
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
|
|
|
if (!g_atomic_int_dec_and_test (&mem->map_count))
|
|
|
|
return TRUE;
|
|
|
|
|
2020-08-02 15:52:50 +00:00
|
|
|
if (mem->prev_mapflags & GST_MAP_VA)
|
|
|
|
goto bail;
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
display = GST_VA_ALLOCATOR (allocator)->display;
|
|
|
|
|
|
|
|
if (mem->image.image_id != VA_INVALID_ID) {
|
|
|
|
if (mem->is_dirty && !mem->is_derived) {
|
2021-10-07 19:51:55 +00:00
|
|
|
ret = va_put_image (display, mem->surface, &mem->image);
|
2020-03-22 18:00:50 +00:00
|
|
|
mem->is_dirty = FALSE;
|
|
|
|
}
|
|
|
|
/* XXX(victor): if is derived and is dirty, create another surface
|
|
|
|
* an replace it in mem */
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:51:55 +00:00
|
|
|
ret &= va_unmap_buffer (display, mem->image.buf);
|
|
|
|
ret &= va_destroy_image (display, mem->image.image_id);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-08-02 15:52:50 +00:00
|
|
|
bail:
|
2020-03-22 18:00:50 +00:00
|
|
|
_clean_mem (mem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_va_unmap (GstVaMemory * mem)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_mutex_lock (&mem->lock);
|
|
|
|
ret = _va_unmap_unlocked (mem);
|
|
|
|
g_mutex_unlock (&mem->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstMemory *
|
|
|
|
_va_share (GstMemory * mem, gssize offset, gssize size)
|
|
|
|
{
|
2020-09-28 14:59:44 +00:00
|
|
|
GstVaMemory *vamem = (GstVaMemory *) mem;
|
|
|
|
GstVaMemory *sub;
|
|
|
|
GstMemory *parent;
|
2021-04-03 12:16:22 +00:00
|
|
|
|
2020-09-28 14:59:44 +00:00
|
|
|
GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT ", %" G_GSIZE_FORMAT, mem, offset,
|
|
|
|
size);
|
|
|
|
|
|
|
|
/* find real parent */
|
|
|
|
if ((parent = vamem->mem.parent) == NULL)
|
|
|
|
parent = (GstMemory *) vamem;
|
|
|
|
|
|
|
|
if (size == -1)
|
|
|
|
size = mem->maxsize - offset;
|
|
|
|
|
2023-01-08 18:03:49 +00:00
|
|
|
sub = g_new (GstVaMemory, 1);
|
2021-04-03 12:16:22 +00:00
|
|
|
|
2020-09-28 14:59:44 +00:00
|
|
|
/* the shared memory is alwyas readonly */
|
|
|
|
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
|
|
|
|
GST_MINI_OBJECT_FLAG_LOCK_READONLY, vamem->mem.allocator, parent,
|
|
|
|
vamem->mem.maxsize, vamem->mem.align, vamem->mem.offset + offset, size);
|
|
|
|
|
|
|
|
sub->surface = vamem->surface;
|
|
|
|
sub->surface_format = vamem->surface_format;
|
|
|
|
|
|
|
|
_clean_mem (sub);
|
|
|
|
|
|
|
|
g_atomic_int_set (&sub->map_count, 0);
|
|
|
|
g_mutex_init (&sub->lock);
|
|
|
|
|
|
|
|
return GST_MEMORY_CAST (sub);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
/* XXX(victor): deep copy implementation. */
|
2021-04-03 12:16:22 +00:00
|
|
|
static GstMemory *
|
|
|
|
_va_copy (GstMemory * mem, gssize offset, gssize size)
|
|
|
|
{
|
|
|
|
GstMemory *copy;
|
|
|
|
GstMapInfo sinfo, dinfo;
|
|
|
|
GstVaAllocator *va_allocator = GST_VA_ALLOCATOR (mem->allocator);
|
2021-10-05 13:21:01 +00:00
|
|
|
GstVaMemory *va_copy, *va_mem = (GstVaMemory *) mem;
|
|
|
|
gsize mem_size;
|
2021-04-03 12:16:22 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("%p: copy %" G_GSSIZE_FORMAT ", %" G_GSIZE_FORMAT, mem, offset,
|
|
|
|
size);
|
|
|
|
|
|
|
|
{
|
|
|
|
GST_VA_MEMORY_POOL_LOCK (&va_allocator->pool);
|
|
|
|
copy = gst_va_memory_pool_pop (&va_allocator->pool);
|
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (&va_allocator->pool);
|
|
|
|
|
|
|
|
if (!copy) {
|
|
|
|
copy = gst_va_allocator_alloc (mem->allocator);
|
|
|
|
if (!copy) {
|
|
|
|
GST_WARNING ("failed to allocate new memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gst_object_ref (mem->allocator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
va_copy = (GstVaMemory *) copy;
|
|
|
|
mem_size = gst_memory_get_sizes (mem, NULL, NULL);
|
2021-04-03 12:16:22 +00:00
|
|
|
|
|
|
|
if (size == -1)
|
2021-10-05 13:21:01 +00:00
|
|
|
size = mem_size > offset ? mem_size - offset : 0;
|
|
|
|
|
|
|
|
if (offset == 0 && size == mem_size) {
|
2021-11-23 13:10:55 +00:00
|
|
|
GstVaSurfaceCopy *copy_func;
|
|
|
|
|
|
|
|
copy_func = _ensure_surface_copy (&va_allocator->copy,
|
|
|
|
va_allocator->display, &va_allocator->info);
|
|
|
|
if (copy_func
|
|
|
|
&& gst_va_surface_copy (copy_func, va_copy->surface, va_mem->surface))
|
2021-10-05 13:21:01 +00:00
|
|
|
return copy;
|
2021-04-03 12:16:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
if (!gst_memory_map (mem, &sinfo, GST_MAP_READ)) {
|
|
|
|
GST_WARNING ("failed to map memory to copy");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-03 12:16:22 +00:00
|
|
|
if (!gst_memory_map (copy, &dinfo, GST_MAP_WRITE)) {
|
|
|
|
GST_WARNING ("could not write map memory %p", copy);
|
|
|
|
gst_allocator_free (mem->allocator, copy);
|
|
|
|
gst_memory_unmap (mem, &sinfo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (dinfo.data, sinfo.data + offset, size);
|
|
|
|
gst_memory_unmap (copy, &dinfo);
|
|
|
|
gst_memory_unmap (mem, &sinfo);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
static void
|
|
|
|
gst_va_allocator_init (GstVaAllocator * self)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = GST_ALLOCATOR (self);
|
|
|
|
|
|
|
|
allocator->mem_type = GST_ALLOCATOR_VASURFACE;
|
|
|
|
allocator->mem_map = (GstMemoryMapFunction) _va_map;
|
|
|
|
allocator->mem_unmap = (GstMemoryUnmapFunction) _va_unmap;
|
|
|
|
allocator->mem_share = _va_share;
|
2021-04-03 12:16:22 +00:00
|
|
|
allocator->mem_copy = _va_copy;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_init (&self->pool);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
GST_OBJECT_FLAG_SET (self, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:35:14 +00:00
|
|
|
static gboolean
|
|
|
|
gst_va_memory_release (GstMiniObject * mini_object)
|
|
|
|
{
|
|
|
|
GstMemory *mem = GST_MEMORY_CAST (mini_object);
|
|
|
|
GstVaAllocator *self = GST_VA_ALLOCATOR (mem->allocator);
|
|
|
|
|
2021-02-05 13:05:53 +00:00
|
|
|
GST_LOG ("releasing %p: surface %#x", mem, gst_va_memory_get_surface (mem));
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_push (&self->pool, mem);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-11-17 12:17:03 +00:00
|
|
|
/* Keep last in case we are holding on the last allocator ref */
|
|
|
|
gst_object_unref (mem->allocator);
|
|
|
|
|
2020-09-30 17:35:14 +00:00
|
|
|
/* don't call mini_object's free */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_alloc:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
|
|
|
* Allocate a new VASurfaceID backed #GstMemory.
|
|
|
|
*
|
|
|
|
* Returns: a #GstMemory backed with a VASurfaceID; %NULL, otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-20 06:31:22 +00:00
|
|
|
GstMemory *
|
|
|
|
gst_va_allocator_alloc (GstAllocator * allocator)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
GstVaAllocator *self;
|
|
|
|
GstVaMemory *mem;
|
|
|
|
VASurfaceID surface;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), NULL);
|
|
|
|
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
|
|
|
|
2021-02-24 18:14:42 +00:00
|
|
|
if (self->rt_format == 0) {
|
2021-02-23 05:47:29 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Unknown fourcc or chroma format");
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-20 06:31:22 +00:00
|
|
|
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_create_surfaces (self->display, self->rt_format, self->fourcc,
|
2020-10-07 16:03:20 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&self->info),
|
2023-02-22 08:25:32 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->info), self->usage_hint, NULL, 0, NULL,
|
2020-08-19 14:45:49 +00:00
|
|
|
&surface, 1))
|
2020-03-22 18:00:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
2023-01-08 18:03:49 +00:00
|
|
|
mem = g_new (GstVaMemory, 1);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
mem->surface = surface;
|
2020-10-19 15:32:44 +00:00
|
|
|
mem->surface_format = self->surface_format;
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-10-07 16:03:20 +00:00
|
|
|
_reset_mem (mem, allocator, GST_VIDEO_INFO_SIZE (&self->info));
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-10-20 06:31:22 +00:00
|
|
|
GST_MINI_OBJECT (mem)->dispose = gst_va_memory_release;
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_surface_inc (&self->pool);
|
2020-10-07 08:16:27 +00:00
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
GST_LOG_OBJECT (self, "Created surface %#x [%dx%d]", mem->surface,
|
2020-10-07 16:03:20 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&self->info), GST_VIDEO_INFO_HEIGHT (&self->info));
|
2020-03-22 18:00:50 +00:00
|
|
|
|
|
|
|
return GST_MEMORY_CAST (mem);
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_new:
|
|
|
|
* @display: a #GstVaDisplay
|
|
|
|
* @surface_formats: (element-type guint) (transfer full): a #GArray
|
|
|
|
* of valid #GstVideoFormat for surfaces in current VA context.
|
|
|
|
*
|
|
|
|
* Instanciate a new pooled #GstAllocator backed by VASurfaceID.
|
|
|
|
*
|
|
|
|
* Returns: a #GstVaDisplay
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-03-22 18:00:50 +00:00
|
|
|
GstAllocator *
|
2020-08-01 13:47:19 +00:00
|
|
|
gst_va_allocator_new (GstVaDisplay * display, GArray * surface_formats)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
GstVaAllocator *self;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_DISPLAY (display), NULL);
|
|
|
|
|
|
|
|
self = g_object_new (GST_TYPE_VA_ALLOCATOR, NULL);
|
|
|
|
self->display = gst_object_ref (display);
|
2020-08-01 13:47:19 +00:00
|
|
|
self->surface_formats = surface_formats;
|
2020-03-22 18:00:50 +00:00
|
|
|
gst_object_ref_sink (self);
|
|
|
|
|
|
|
|
return GST_ALLOCATOR (self);
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_setup_buffer:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
|
|
|
* Populates an empty @buffer with a VASuface backed #GstMemory.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @buffer is populated; %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-02-07 15:12:56 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_allocator_setup_buffer (GstAllocator * allocator, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMemory *mem = gst_va_allocator_alloc (allocator);
|
|
|
|
if (!mem)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gst_buffer_append_memory (buffer, mem);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:53:05 +00:00
|
|
|
static VASurfaceID
|
|
|
|
gst_va_allocator_prepare_buffer_unlocked (GstVaAllocator * self,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
VASurfaceID surface;
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
mem = gst_va_memory_pool_pop (&self->pool);
|
2020-11-17 13:53:05 +00:00
|
|
|
if (!mem)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
|
|
|
gst_object_ref (mem->allocator);
|
|
|
|
surface = gst_va_memory_get_surface (mem);
|
|
|
|
gst_buffer_append_memory (buffer, mem);
|
|
|
|
|
2021-02-05 13:05:53 +00:00
|
|
|
GST_LOG ("buffer %p: memory %p - surface %#x", buffer, mem, surface);
|
|
|
|
|
2020-11-17 13:53:05 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_prepare_buffer:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @buffer: an empty #GstBuffer
|
|
|
|
*
|
|
|
|
* This method will populate @buffer with pooled VASurfaceID
|
|
|
|
* memories. It doesn't allocate new VASurfacesID.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @buffer was populated correctly; %FALSE
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-09-30 17:35:14 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_allocator_prepare_buffer (GstAllocator * allocator, GstBuffer * buffer)
|
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaAllocator *self;
|
2020-09-30 17:35:14 +00:00
|
|
|
VASurfaceID surface;
|
|
|
|
|
2022-02-23 07:40:19 +00:00
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
GST_VA_MEMORY_POOL_LOCK (&self->pool);
|
2020-11-17 13:53:05 +00:00
|
|
|
surface = gst_va_allocator_prepare_buffer_unlocked (self, buffer);
|
2020-11-23 19:44:27 +00:00
|
|
|
GST_VA_MEMORY_POOL_UNLOCK (&self->pool);
|
2020-11-17 12:18:37 +00:00
|
|
|
|
2020-11-17 13:53:05 +00:00
|
|
|
return (surface != VA_INVALID_ID);
|
|
|
|
}
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_flush:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
|
|
|
* Removes all the memories in @allocator's pool.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-09-30 17:35:14 +00:00
|
|
|
void
|
|
|
|
gst_va_allocator_flush (GstAllocator * allocator)
|
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaAllocator *self;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_VA_ALLOCATOR (allocator));
|
|
|
|
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
2020-09-30 17:35:14 +00:00
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
gst_va_memory_pool_flush (&self->pool, self->display);
|
2020-09-30 17:35:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_try:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
|
|
|
* Try to allocate a test buffer in order to verify that the
|
|
|
|
* allocator's configuration is valid.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the configuration is valid; %FALSE otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
static gboolean
|
2023-12-11 16:28:06 +00:00
|
|
|
gst_va_allocator_try (GstAllocator * allocator, GstVaFeature feat_use_derived)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaAllocator *self;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
2020-03-22 18:00:50 +00:00
|
|
|
|
2020-10-19 15:32:44 +00:00
|
|
|
self->fourcc = 0;
|
|
|
|
self->rt_format = 0;
|
|
|
|
self->use_derived = FALSE;
|
|
|
|
self->img_format = GST_VIDEO_INFO_FORMAT (&self->info);
|
|
|
|
|
|
|
|
self->surface_format =
|
|
|
|
gst_va_video_surface_format_from_image_format (self->img_format,
|
|
|
|
self->surface_formats);
|
|
|
|
if (self->surface_format == GST_VIDEO_FORMAT_UNKNOWN) {
|
|
|
|
/* try a surface without fourcc but rt_format only */
|
|
|
|
self->fourcc = 0;
|
|
|
|
self->rt_format = gst_va_chroma_from_video_format (self->img_format);
|
|
|
|
} else {
|
2022-06-24 17:42:36 +00:00
|
|
|
if (G_LIKELY (!(self->hacks & GST_VA_HACK_SURFACE_NO_FOURCC)))
|
|
|
|
self->fourcc = gst_va_fourcc_from_video_format (self->surface_format);
|
2020-10-19 15:32:44 +00:00
|
|
|
self->rt_format = gst_va_chroma_from_video_format (self->surface_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->rt_format == 0) {
|
|
|
|
GST_ERROR_OBJECT (allocator, "Unsupported image format: %s",
|
|
|
|
gst_video_format_to_string (self->img_format));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-12-11 16:28:06 +00:00
|
|
|
if (!_update_image_info (self, feat_use_derived)) {
|
2020-10-20 06:31:22 +00:00
|
|
|
GST_ERROR_OBJECT (allocator, "Failed to update allocator info");
|
2020-03-22 18:00:50 +00:00
|
|
|
return FALSE;
|
2020-10-20 06:31:22 +00:00
|
|
|
}
|
2020-10-19 15:32:44 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (self,
|
|
|
|
"va allocator info, surface format: %s, image format: %s, "
|
|
|
|
"use derived: %s, rt format: 0x%x, fourcc: %" GST_FOURCC_FORMAT,
|
2020-11-13 19:25:36 +00:00
|
|
|
(self->surface_format == GST_VIDEO_FORMAT_UNKNOWN) ? "unknown"
|
|
|
|
: gst_video_format_to_string (self->surface_format),
|
2020-10-19 15:32:44 +00:00
|
|
|
gst_video_format_to_string (self->img_format),
|
|
|
|
self->use_derived ? "true" : "false", self->rt_format,
|
|
|
|
GST_FOURCC_ARGS (self->fourcc));
|
2020-10-07 16:03:20 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_set_format:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @info: (inout): a #GstVideoInfo
|
|
|
|
* @usage_hint: VA usage hint
|
2023-12-11 16:28:06 +00:00
|
|
|
* @feat_use_derived: a #GstVaFeature
|
2022-02-07 16:34:57 +00:00
|
|
|
*
|
|
|
|
* Sets the configuration defined by @info, @usage_hint and
|
|
|
|
* @use_derived for @allocator, and it tries the configuration, if
|
|
|
|
* @allocator has not allocated memories yet.
|
|
|
|
*
|
|
|
|
* If @allocator has memory allocated already, and frame size and
|
|
|
|
* format in @info are the same as currently configured in @allocator,
|
|
|
|
* the rest of @info parameters are updated internally.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the configuration is valid or updated; %FALSE if
|
|
|
|
* configuration is not valid or not updated.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_allocator_set_format (GstAllocator * allocator, GstVideoInfo * info,
|
2023-12-11 16:28:06 +00:00
|
|
|
guint usage_hint, GstVaFeature feat_use_derived)
|
2020-10-07 16:03:20 +00:00
|
|
|
{
|
|
|
|
GstVaAllocator *self;
|
2023-12-11 16:28:06 +00:00
|
|
|
gboolean use_derived;
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), FALSE);
|
|
|
|
g_return_val_if_fail (info, FALSE);
|
|
|
|
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
|
|
|
|
2023-12-11 16:28:06 +00:00
|
|
|
use_derived = feat_use_derived == GST_VA_FEATURE_AUTO ? self->use_derived
|
|
|
|
: feat_use_derived == GST_VA_FEATURE_DISABLED ? FALSE : TRUE;
|
|
|
|
|
2020-11-23 19:44:27 +00:00
|
|
|
if (gst_va_memory_pool_surface_count (&self->pool) != 0) {
|
2020-10-07 16:03:20 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (info) == GST_VIDEO_INFO_FORMAT (&self->info)
|
|
|
|
&& GST_VIDEO_INFO_WIDTH (info) == GST_VIDEO_INFO_WIDTH (&self->info)
|
|
|
|
&& GST_VIDEO_INFO_HEIGHT (info) == GST_VIDEO_INFO_HEIGHT (&self->info)
|
2023-12-11 16:28:06 +00:00
|
|
|
&& usage_hint == self->usage_hint && use_derived == self->use_derived) {
|
2020-10-07 16:03:20 +00:00
|
|
|
*info = self->info; /* update callee info (offset & stride) */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->usage_hint = usage_hint;
|
|
|
|
self->info = *info;
|
|
|
|
|
2021-10-05 13:21:01 +00:00
|
|
|
g_clear_pointer (&self->copy, gst_va_surface_copy_free);
|
|
|
|
|
2023-12-11 16:28:06 +00:00
|
|
|
ret = gst_va_allocator_try (allocator, feat_use_derived);
|
2020-10-07 16:03:20 +00:00
|
|
|
if (ret)
|
|
|
|
*info = self->info;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_get_format:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @info: (out) (optional): a #GstVideoInfo
|
|
|
|
* @usage_hint: (out) (optional): VA usage hint
|
2023-12-11 16:28:06 +00:00
|
|
|
* @use_derived: (out) (optional): whether derived images are used for buffer
|
|
|
|
* mapping.
|
2022-02-07 16:34:57 +00:00
|
|
|
*
|
|
|
|
* Gets current internal configuration of @allocator.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if @allocator is already configured; %FALSE
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-10-07 16:03:20 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_allocator_get_format (GstAllocator * allocator, GstVideoInfo * info,
|
2023-12-11 16:28:06 +00:00
|
|
|
guint * usage_hint, gboolean * use_derived)
|
2020-10-07 16:03:20 +00:00
|
|
|
{
|
2022-02-23 07:40:19 +00:00
|
|
|
GstVaAllocator *self;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_VA_ALLOCATOR (allocator), FALSE);
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
2020-10-07 16:03:20 +00:00
|
|
|
|
|
|
|
if (GST_VIDEO_INFO_FORMAT (&self->info) == GST_VIDEO_FORMAT_UNKNOWN)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
*info = self->info;
|
|
|
|
if (usage_hint)
|
|
|
|
*usage_hint = self->usage_hint;
|
2022-03-29 12:52:33 +00:00
|
|
|
if (use_derived)
|
2023-12-11 16:28:06 +00:00
|
|
|
*use_derived = self->use_derived;
|
2020-10-07 16:03:20 +00:00
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_set_hacks: (skip)
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
* @hacks: hacks id to set
|
|
|
|
*
|
|
|
|
* Internal method to set allocator specific logic changes.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2022-06-24 17:42:36 +00:00
|
|
|
void
|
|
|
|
gst_va_allocator_set_hacks (GstAllocator * allocator, guint32 hacks)
|
|
|
|
{
|
|
|
|
GstVaAllocator *self;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_VA_ALLOCATOR (allocator));
|
|
|
|
self = GST_VA_ALLOCATOR (allocator);
|
|
|
|
|
|
|
|
self->hacks = hacks;
|
|
|
|
}
|
|
|
|
|
2022-11-04 09:06:37 +00:00
|
|
|
/**
|
|
|
|
* gst_va_allocator_peek_display:
|
|
|
|
* @allocator: a #GstAllocator
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (transfer none): the display which this
|
2022-11-04 09:06:37 +00:00
|
|
|
* @allocator belongs to. The reference of the display is unchanged.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
|
|
|
GstVaDisplay *
|
|
|
|
gst_va_allocator_peek_display (GstAllocator * allocator)
|
|
|
|
{
|
|
|
|
if (!allocator)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (GST_IS_VA_DMABUF_ALLOCATOR (allocator)) {
|
|
|
|
return GST_VA_DMABUF_ALLOCATOR (allocator)->display;
|
|
|
|
} else if (GST_IS_VA_ALLOCATOR (allocator)) {
|
|
|
|
return GST_VA_ALLOCATOR (allocator)->display;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:00:50 +00:00
|
|
|
/*============ Utilities =====================================================*/
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
2022-12-11 09:12:25 +00:00
|
|
|
* gst_va_memory_get_surface: (skip)
|
2022-02-07 16:34:57 +00:00
|
|
|
* @mem: a #GstMemory
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (type guint): the VASurfaceID in @mem.
|
2022-02-07 16:34:57 +00:00
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-03-22 18:00:50 +00:00
|
|
|
VASurfaceID
|
2020-10-06 17:54:26 +00:00
|
|
|
gst_va_memory_get_surface (GstMemory * mem)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
VASurfaceID surface = VA_INVALID_ID;
|
|
|
|
|
|
|
|
if (!mem->allocator)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
2020-08-19 14:48:39 +00:00
|
|
|
if (GST_IS_DMABUF_ALLOCATOR (mem->allocator)) {
|
2020-03-22 18:00:50 +00:00
|
|
|
GstVaBufferSurface *buf;
|
|
|
|
|
|
|
|
buf = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_surface_quark ());
|
2020-10-06 17:54:26 +00:00
|
|
|
if (buf)
|
2020-03-22 18:00:50 +00:00
|
|
|
surface = buf->surface;
|
|
|
|
} else if (GST_IS_VA_ALLOCATOR (mem->allocator)) {
|
|
|
|
GstVaMemory *va_mem = (GstVaMemory *) mem;
|
|
|
|
surface = va_mem->surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:28:34 +00:00
|
|
|
/**
|
|
|
|
* gst_va_memory_peek_display:
|
|
|
|
* @mem: a #GstMemory
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (transfer none): the display which
|
2022-11-04 12:28:34 +00:00
|
|
|
* this @mem belongs to. The reference of the display is unchanged.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
|
|
|
GstVaDisplay *
|
|
|
|
gst_va_memory_peek_display (GstMemory * mem)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
|
|
|
if (!mem)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
allocator = GST_MEMORY_CAST (mem)->allocator;
|
|
|
|
/* no allocator, not VA kind memory. */
|
|
|
|
if (!allocator)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_va_allocator_peek_display (allocator);
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
2022-12-11 09:12:25 +00:00
|
|
|
* gst_va_buffer_get_surface: (skip)
|
2022-02-07 16:34:57 +00:00
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (type guint): the VASurfaceID in @buffer.
|
2022-02-07 16:34:57 +00:00
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2020-03-22 18:00:50 +00:00
|
|
|
VASurfaceID
|
2020-10-06 17:54:26 +00:00
|
|
|
gst_va_buffer_get_surface (GstBuffer * buffer)
|
2020-03-22 18:00:50 +00:00
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
if (!mem)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
2020-10-06 17:54:26 +00:00
|
|
|
return gst_va_memory_get_surface (mem);
|
2020-03-22 18:00:50 +00:00
|
|
|
}
|
2021-01-19 07:17:58 +00:00
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
|
|
|
* gst_va_buffer_create_aux_surface:
|
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
|
|
|
* Creates a new VASurfaceID with @buffer's allocator and attached it
|
|
|
|
* to it.
|
|
|
|
*
|
|
|
|
* *This method is used only by plugin's internal VA decoder.*
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the new VASurfaceID is attached to @buffer
|
|
|
|
* correctly; %FALSE, otherwise.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-01-19 07:17:58 +00:00
|
|
|
gboolean
|
|
|
|
gst_va_buffer_create_aux_surface (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
VASurfaceID surface = VA_INVALID_ID;
|
|
|
|
GstVaDisplay *display = NULL;
|
|
|
|
GstVideoFormat format;
|
|
|
|
GstVaBufferSurface *surface_buffer;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
if (!mem)
|
|
|
|
return FALSE;
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/* Already created. */
|
2021-01-19 07:17:58 +00:00
|
|
|
surface_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_aux_surface_quark ());
|
|
|
|
if (surface_buffer)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!mem->allocator)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (GST_IS_VA_DMABUF_ALLOCATOR (mem->allocator)) {
|
|
|
|
GstVaDmabufAllocator *self = GST_VA_DMABUF_ALLOCATOR (mem->allocator);
|
|
|
|
guint32 fourcc, rt_format;
|
|
|
|
|
2023-06-13 09:48:31 +00:00
|
|
|
format = GST_VIDEO_INFO_FORMAT (&self->info.vinfo);
|
2021-01-19 07:17:58 +00:00
|
|
|
fourcc = gst_va_fourcc_from_video_format (format);
|
|
|
|
rt_format = gst_va_chroma_from_video_format (format);
|
|
|
|
if (fourcc == 0 || rt_format == 0) {
|
|
|
|
GST_ERROR_OBJECT (self, "Unsupported format: %s",
|
2023-06-13 09:48:31 +00:00
|
|
|
gst_video_format_to_string (format));
|
2021-01-19 07:17:58 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
display = self->display;
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_create_surfaces (self->display, rt_format, fourcc,
|
2023-06-13 09:48:31 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&self->info.vinfo),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->info.vinfo), self->usage_hint, NULL,
|
|
|
|
0, NULL, &surface, 1))
|
2021-01-19 07:17:58 +00:00
|
|
|
return FALSE;
|
|
|
|
} else if (GST_IS_VA_ALLOCATOR (mem->allocator)) {
|
|
|
|
GstVaAllocator *self = GST_VA_ALLOCATOR (mem->allocator);
|
|
|
|
|
2021-02-24 18:14:42 +00:00
|
|
|
if (self->rt_format == 0) {
|
2021-01-19 07:17:58 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Unknown fourcc or chroma format");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
display = self->display;
|
|
|
|
format = GST_VIDEO_INFO_FORMAT (&self->info);
|
2021-10-07 19:51:55 +00:00
|
|
|
if (!va_create_surfaces (self->display, self->rt_format, self->fourcc,
|
2021-01-19 07:17:58 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&self->info),
|
2023-02-22 08:25:32 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->info), self->usage_hint, NULL, 0,
|
|
|
|
NULL, &surface, 1))
|
2021-01-19 07:17:58 +00:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!display || surface == VA_INVALID_ID)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-06-04 16:51:56 +00:00
|
|
|
surface_buffer = gst_va_buffer_surface_new (surface);
|
2021-01-19 07:17:58 +00:00
|
|
|
surface_buffer->display = gst_object_ref (display);
|
|
|
|
g_atomic_int_add (&surface_buffer->ref_count, 1);
|
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_aux_surface_quark (), surface_buffer,
|
|
|
|
gst_va_buffer_surface_unref);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:34:57 +00:00
|
|
|
/**
|
2022-12-11 09:12:25 +00:00
|
|
|
* gst_va_buffer_get_aux_surface: (skip)
|
2022-02-07 16:34:57 +00:00
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (type guint): the VASurfaceID attached to
|
2022-02-07 16:34:57 +00:00
|
|
|
* @buffer.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-01-19 07:17:58 +00:00
|
|
|
VASurfaceID
|
|
|
|
gst_va_buffer_get_aux_surface (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstVaBufferSurface *surface_buffer;
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
if (!mem)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
|
|
|
surface_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
|
|
|
|
gst_va_buffer_aux_surface_quark ());
|
|
|
|
if (!surface_buffer)
|
|
|
|
return VA_INVALID_ID;
|
|
|
|
|
|
|
|
/* No one increments it, and its lifetime is the same with the
|
|
|
|
gstmemory itself */
|
|
|
|
g_assert (g_atomic_int_get (&surface_buffer->ref_count) == 1);
|
|
|
|
|
|
|
|
return surface_buffer->surface;
|
|
|
|
}
|
2022-11-04 12:28:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_va_buffer_peek_display:
|
|
|
|
* @buffer: a #GstBuffer
|
|
|
|
*
|
2022-12-11 09:12:25 +00:00
|
|
|
* Returns: (transfer none): the display which this
|
2022-11-04 12:28:34 +00:00
|
|
|
* @buffer belongs to. The reference of the display is unchanged.
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
|
|
|
GstVaDisplay *
|
|
|
|
gst_va_buffer_peek_display (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
/* Buffer without mem, not VA kind memory. */
|
|
|
|
if (!mem)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_va_memory_peek_display (mem);
|
|
|
|
}
|