2015-11-20 18:47:29 +00:00
|
|
|
/*
|
2016-02-17 16:10:53 +00:00
|
|
|
* This file is part of FFmpeg.
|
2015-11-20 18:47:29 +00:00
|
|
|
*
|
2016-02-17 16:10:53 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2015-11-20 18:47:29 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2016-02-17 16:10:53 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2015-11-20 18:47:29 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-02-17 16:10:53 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2015-11-20 18:47:29 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2022-01-19 13:37:51 +00:00
|
|
|
#include "avassert.h"
|
2015-11-20 18:47:29 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "hwcontext.h"
|
|
|
|
#include "hwcontext_internal.h"
|
|
|
|
#include "imgutils.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "pixdesc.h"
|
|
|
|
#include "pixfmt.h"
|
|
|
|
|
2016-07-03 08:09:36 +00:00
|
|
|
static const HWContextType * const hw_table[] = {
|
2016-01-13 13:25:58 +00:00
|
|
|
#if CONFIG_CUDA
|
|
|
|
&ff_hwcontext_type_cuda,
|
|
|
|
#endif
|
2017-06-06 16:51:07 +00:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
&ff_hwcontext_type_d3d11va,
|
|
|
|
#endif
|
2023-12-05 06:46:43 +00:00
|
|
|
#if CONFIG_D3D12VA
|
|
|
|
&ff_hwcontext_type_d3d12va,
|
|
|
|
#endif
|
2017-06-18 17:16:27 +00:00
|
|
|
#if CONFIG_LIBDRM
|
|
|
|
&ff_hwcontext_type_drm,
|
|
|
|
#endif
|
2016-03-31 11:12:41 +00:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
&ff_hwcontext_type_dxva2,
|
|
|
|
#endif
|
2017-03-04 23:57:47 +00:00
|
|
|
#if CONFIG_OPENCL
|
|
|
|
&ff_hwcontext_type_opencl,
|
|
|
|
#endif
|
2016-09-28 18:12:29 +00:00
|
|
|
#if CONFIG_QSV
|
2016-01-13 13:25:58 +00:00
|
|
|
&ff_hwcontext_type_qsv,
|
|
|
|
#endif
|
2016-02-15 19:32:45 +00:00
|
|
|
#if CONFIG_VAAPI
|
|
|
|
&ff_hwcontext_type_vaapi,
|
|
|
|
#endif
|
2015-12-07 14:04:57 +00:00
|
|
|
#if CONFIG_VDPAU
|
|
|
|
&ff_hwcontext_type_vdpau,
|
2017-05-15 09:27:24 +00:00
|
|
|
#endif
|
|
|
|
#if CONFIG_VIDEOTOOLBOX
|
|
|
|
&ff_hwcontext_type_videotoolbox,
|
2017-12-04 01:32:21 +00:00
|
|
|
#endif
|
|
|
|
#if CONFIG_MEDIACODEC
|
|
|
|
&ff_hwcontext_type_mediacodec,
|
2019-08-28 20:58:10 +00:00
|
|
|
#endif
|
|
|
|
#if CONFIG_VULKAN
|
|
|
|
&ff_hwcontext_type_vulkan,
|
2015-12-07 14:04:57 +00:00
|
|
|
#endif
|
2015-11-20 18:47:29 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2017-03-04 23:57:29 +00:00
|
|
|
static const char *const hw_type_names[] = {
|
|
|
|
[AV_HWDEVICE_TYPE_CUDA] = "cuda",
|
2017-06-18 17:16:27 +00:00
|
|
|
[AV_HWDEVICE_TYPE_DRM] = "drm",
|
2017-03-04 23:57:29 +00:00
|
|
|
[AV_HWDEVICE_TYPE_DXVA2] = "dxva2",
|
2017-06-06 16:51:07 +00:00
|
|
|
[AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
|
2023-12-05 06:46:43 +00:00
|
|
|
[AV_HWDEVICE_TYPE_D3D12VA] = "d3d12va",
|
2017-03-04 23:57:47 +00:00
|
|
|
[AV_HWDEVICE_TYPE_OPENCL] = "opencl",
|
2017-03-04 23:57:29 +00:00
|
|
|
[AV_HWDEVICE_TYPE_QSV] = "qsv",
|
|
|
|
[AV_HWDEVICE_TYPE_VAAPI] = "vaapi",
|
|
|
|
[AV_HWDEVICE_TYPE_VDPAU] = "vdpau",
|
|
|
|
[AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
|
2017-12-04 01:32:21 +00:00
|
|
|
[AV_HWDEVICE_TYPE_MEDIACODEC] = "mediacodec",
|
2019-08-28 20:58:10 +00:00
|
|
|
[AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
|
2017-03-04 23:57:29 +00:00
|
|
|
};
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
typedef struct FFHWDeviceContext {
|
|
|
|
/**
|
|
|
|
* The public AVHWDeviceContext. See hwcontext.h for it.
|
|
|
|
*/
|
|
|
|
AVHWDeviceContext p;
|
|
|
|
|
|
|
|
const HWContextType *hw_type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For a derived device, a reference to the original device
|
|
|
|
* context it was derived from.
|
|
|
|
*/
|
|
|
|
AVBufferRef *source_device;
|
|
|
|
} FFHWDeviceContext;
|
|
|
|
|
2017-03-04 23:57:29 +00:00
|
|
|
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
|
|
|
|
if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
return AV_HWDEVICE_TYPE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
|
|
|
|
{
|
2017-03-23 19:49:39 +00:00
|
|
|
if (type > AV_HWDEVICE_TYPE_NONE &&
|
|
|
|
type < FF_ARRAY_ELEMS(hw_type_names))
|
2017-03-04 23:57:29 +00:00
|
|
|
return hw_type_names[type];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
|
|
|
|
{
|
|
|
|
enum AVHWDeviceType next;
|
|
|
|
int i, set = 0;
|
|
|
|
for (i = 0; hw_table[i]; i++) {
|
|
|
|
if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
|
|
|
|
continue;
|
|
|
|
if (!set || hw_table[i]->type < next) {
|
|
|
|
next = hw_table[i]->type;
|
|
|
|
set = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set ? next : AV_HWDEVICE_TYPE_NONE;
|
|
|
|
}
|
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
static const AVClass hwdevice_ctx_class = {
|
|
|
|
.class_name = "AVHWDeviceContext",
|
2024-01-19 12:33:28 +00:00
|
|
|
.item_name = av_default_item_name,
|
2015-11-20 18:47:29 +00:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void hwdevice_ctx_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *ctxi = (FFHWDeviceContext*)data;
|
|
|
|
AVHWDeviceContext *ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
/* uninit might still want access the hw context and the user
|
|
|
|
* free() callback might destroy it, so uninit has to be called first */
|
2024-02-11 17:09:27 +00:00
|
|
|
if (ctxi->hw_type->device_uninit)
|
|
|
|
ctxi->hw_type->device_uninit(ctx);
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
if (ctx->free)
|
|
|
|
ctx->free(ctx);
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
av_buffer_unref(&ctxi->source_device);
|
2017-03-04 23:57:28 +00:00
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
av_freep(&ctx->hwctx);
|
|
|
|
av_freep(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVBufferRef *av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *ctxi;
|
2015-11-20 18:47:29 +00:00
|
|
|
AVHWDeviceContext *ctx;
|
|
|
|
AVBufferRef *buf;
|
|
|
|
const HWContextType *hw_type = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; hw_table[i]; i++) {
|
|
|
|
if (hw_table[i]->type == type) {
|
|
|
|
hw_type = hw_table[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hw_type)
|
|
|
|
return NULL;
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
ctxi = av_mallocz(sizeof(*ctxi));
|
|
|
|
if (!ctxi)
|
2015-11-20 18:47:29 +00:00
|
|
|
return NULL;
|
2024-02-11 17:09:27 +00:00
|
|
|
ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
if (hw_type->device_hwctx_size) {
|
|
|
|
ctx->hwctx = av_mallocz(hw_type->device_hwctx_size);
|
|
|
|
if (!ctx->hwctx)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
|
|
|
|
hwdevice_ctx_free, NULL,
|
|
|
|
AV_BUFFER_FLAG_READONLY);
|
|
|
|
if (!buf)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ctx->type = type;
|
|
|
|
ctx->av_class = &hwdevice_ctx_class;
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
ctxi->hw_type = hw_type;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_freep(&ctx->hwctx);
|
|
|
|
av_freep(&ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwdevice_ctx_init(AVBufferRef *ref)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *ctxi = (FFHWDeviceContext*)ref->data;
|
|
|
|
AVHWDeviceContext *ctx = &ctxi->p;
|
2024-02-20 12:08:54 +00:00
|
|
|
int ret = 0;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
if (ctxi->hw_type->device_init)
|
|
|
|
ret = ctxi->hw_type->device_init(ctx);
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass hwframe_ctx_class = {
|
|
|
|
.class_name = "AVHWFramesContext",
|
2024-01-19 12:33:28 +00:00
|
|
|
.item_name = av_default_item_name,
|
2015-11-20 18:47:29 +00:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void hwframe_ctx_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctxi = (FFHWFramesContext*)data;
|
|
|
|
AVHWFramesContext *ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->pool_internal)
|
|
|
|
av_buffer_pool_uninit(&ctxi->pool_internal);
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->hw_type->frames_uninit)
|
|
|
|
ctxi->hw_type->frames_uninit(ctx);
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2017-06-26 19:28:07 +00:00
|
|
|
if (ctx->free)
|
|
|
|
ctx->free(ctx);
|
2016-10-25 19:38:47 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
av_buffer_unref(&ctxi->source_frames);
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
av_buffer_unref(&ctx->device_ref);
|
|
|
|
|
|
|
|
av_freep(&ctx->hwctx);
|
|
|
|
av_freep(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)device_ref_in->data;
|
|
|
|
const HWContextType *hw_type = device_ctx->hw_type;
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctxi;
|
2015-11-20 18:47:29 +00:00
|
|
|
AVHWFramesContext *ctx;
|
2016-02-17 17:22:55 +00:00
|
|
|
AVBufferRef *buf, *device_ref = NULL;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ctxi = av_mallocz(sizeof(*ctxi));
|
|
|
|
if (!ctxi)
|
2015-11-20 18:47:29 +00:00
|
|
|
return NULL;
|
2024-02-11 21:16:32 +00:00
|
|
|
ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
if (hw_type->frames_hwctx_size) {
|
|
|
|
ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
|
|
|
|
if (!ctx->hwctx)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_ref = av_buffer_ref(device_ref_in);
|
|
|
|
if (!device_ref)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
buf = av_buffer_create((uint8_t*)ctx, sizeof(*ctx),
|
|
|
|
hwframe_ctx_free, NULL,
|
|
|
|
AV_BUFFER_FLAG_READONLY);
|
|
|
|
if (!buf)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ctx->av_class = &hwframe_ctx_class;
|
|
|
|
ctx->device_ref = device_ref;
|
2024-02-11 17:09:27 +00:00
|
|
|
ctx->device_ctx = &device_ctx->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
ctx->format = AV_PIX_FMT_NONE;
|
2016-04-15 08:15:54 +00:00
|
|
|
ctx->sw_format = AV_PIX_FMT_NONE;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ctxi->hw_type = hw_type;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
fail:
|
2024-02-11 18:08:44 +00:00
|
|
|
av_buffer_unref(&device_ref);
|
2015-11-20 18:47:29 +00:00
|
|
|
av_freep(&ctx->hwctx);
|
|
|
|
av_freep(&ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hwframe_pool_prealloc(AVBufferRef *ref)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data;
|
|
|
|
AVFrame **frames;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
2021-09-14 19:31:53 +00:00
|
|
|
frames = av_calloc(ctx->initial_pool_size, sizeof(*frames));
|
2015-11-20 18:47:29 +00:00
|
|
|
if (!frames)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++) {
|
|
|
|
frames[i] = av_frame_alloc();
|
|
|
|
if (!frames[i])
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = av_hwframe_get_buffer(ref, frames[i], 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++)
|
|
|
|
av_frame_free(&frames[i]);
|
|
|
|
av_freep(&frames);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_ctx_init(AVBufferRef *ref)
|
|
|
|
{
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctxi = (FFHWFramesContext*)ref->data;
|
|
|
|
AVHWFramesContext *ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
const enum AVPixelFormat *pix_fmt;
|
|
|
|
int ret;
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->source_frames) {
|
2016-10-25 19:38:47 +00:00
|
|
|
/* A derived frame context is already initialised. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
/* validate the pixel format */
|
2024-02-11 21:16:32 +00:00
|
|
|
for (pix_fmt = ctxi->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
|
2015-11-20 18:47:29 +00:00
|
|
|
if (*pix_fmt == ctx->format)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"The hardware pixel format '%s' is not supported by the device type '%s'\n",
|
2024-02-11 21:16:32 +00:00
|
|
|
av_get_pix_fmt_name(ctx->format), ctxi->hw_type->name);
|
2015-11-20 18:47:29 +00:00
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* validate the dimensions */
|
|
|
|
ret = av_image_check_size(ctx->width, ctx->height, 0, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* format-specific init */
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->hw_type->frames_init) {
|
|
|
|
ret = ctxi->hw_type->frames_init(ctx);
|
2015-11-20 18:47:29 +00:00
|
|
|
if (ret < 0)
|
2024-02-20 12:08:55 +00:00
|
|
|
return ret;
|
2015-11-20 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->pool_internal && !ctx->pool)
|
|
|
|
ctx->pool = ctxi->pool_internal;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
|
|
|
/* preallocate the frames in the pool, if requested */
|
|
|
|
if (ctx->initial_pool_size > 0) {
|
|
|
|
ret = hwframe_pool_prealloc(ref);
|
|
|
|
if (ret < 0)
|
2024-02-20 12:08:55 +00:00
|
|
|
return ret;
|
2015-11-20 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
|
|
|
|
enum AVHWFrameTransferDirection dir,
|
|
|
|
enum AVPixelFormat **formats, int flags)
|
|
|
|
{
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (!ctxi->hw_type->transfer_get_formats)
|
2015-11-20 18:47:29 +00:00
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
return ctxi->hw_type->transfer_get_formats(&ctxi->p, dir, formats);
|
2015-11-20 18:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
2022-10-22 05:29:35 +00:00
|
|
|
AVHWFramesContext *ctx;
|
2015-11-20 18:47:29 +00:00
|
|
|
AVFrame *frame_tmp;
|
|
|
|
int ret = 0;
|
|
|
|
|
2022-10-22 05:29:35 +00:00
|
|
|
if (!src->hw_frames_ctx)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
|
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
frame_tmp = av_frame_alloc();
|
|
|
|
if (!frame_tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
/* if the format is set, use that
|
|
|
|
* otherwise pick the first supported one */
|
|
|
|
if (dst->format >= 0) {
|
|
|
|
frame_tmp->format = dst->format;
|
|
|
|
} else {
|
|
|
|
enum AVPixelFormat *formats;
|
|
|
|
|
|
|
|
ret = av_hwframe_transfer_get_formats(src->hw_frames_ctx,
|
|
|
|
AV_HWFRAME_TRANSFER_DIRECTION_FROM,
|
|
|
|
&formats, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
frame_tmp->format = formats[0];
|
|
|
|
av_freep(&formats);
|
|
|
|
}
|
2016-04-15 08:44:02 +00:00
|
|
|
frame_tmp->width = ctx->width;
|
|
|
|
frame_tmp->height = ctx->height;
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2017-02-08 08:51:17 +00:00
|
|
|
ret = av_frame_get_buffer(frame_tmp, 0);
|
2015-11-20 18:47:29 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = av_hwframe_transfer_data(frame_tmp, src, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2016-04-15 08:44:02 +00:00
|
|
|
frame_tmp->width = src->width;
|
|
|
|
frame_tmp->height = src->height;
|
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
av_frame_move_ref(dst, frame_tmp);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_frame_free(&frame_tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dst->buf[0])
|
|
|
|
return transfer_data_alloc(dst, src, flags);
|
|
|
|
|
2019-10-24 01:01:52 +00:00
|
|
|
/*
|
|
|
|
* Hardware -> Hardware Transfer.
|
|
|
|
* Unlike Software -> Hardware or Hardware -> Software, the transfer
|
|
|
|
* function could be provided by either the src or dst, depending on
|
|
|
|
* the specific combination of hardware.
|
|
|
|
*/
|
|
|
|
if (src->hw_frames_ctx && dst->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *src_ctx =
|
|
|
|
(FFHWFramesContext*)src->hw_frames_ctx->data;
|
|
|
|
FFHWFramesContext *dst_ctx =
|
|
|
|
(FFHWFramesContext*)dst->hw_frames_ctx->data;
|
2019-10-24 01:01:52 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (src_ctx->source_frames) {
|
2019-10-24 01:01:52 +00:00
|
|
|
av_log(src_ctx, AV_LOG_ERROR,
|
|
|
|
"A device with a derived frame context cannot be used as "
|
|
|
|
"the source of a HW -> HW transfer.");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (dst_ctx->source_frames) {
|
2019-10-24 01:01:52 +00:00
|
|
|
av_log(src_ctx, AV_LOG_ERROR,
|
|
|
|
"A device with a derived frame context cannot be used as "
|
|
|
|
"the destination of a HW -> HW transfer.");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
2015-11-20 18:47:29 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = src_ctx->hw_type->transfer_data_from(&src_ctx->p, dst, src);
|
2019-10-24 01:01:52 +00:00
|
|
|
if (ret == AVERROR(ENOSYS))
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = dst_ctx->hw_type->transfer_data_to(&dst_ctx->p, dst, src);
|
2015-11-20 18:47:29 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-10-24 01:01:52 +00:00
|
|
|
} else {
|
|
|
|
if (src->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctx = (FFHWFramesContext*)src->hw_frames_ctx->data;
|
2019-10-24 01:01:52 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = ctx->hw_type->transfer_data_from(&ctx->p, dst, src);
|
2019-10-24 01:01:52 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else if (dst->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctx = (FFHWFramesContext*)dst->hw_frames_ctx->data;
|
2019-10-24 01:01:52 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = ctx->hw_type->transfer_data_to(&ctx->p, dst, src);
|
2019-10-24 01:01:52 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 18:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
|
|
|
|
{
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
|
|
|
|
AVHWFramesContext *ctx = &ctxi->p;
|
2015-11-20 18:47:29 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (ctxi->source_frames) {
|
2016-10-25 19:38:47 +00:00
|
|
|
// This is a derived frame context, so we allocate in the source
|
|
|
|
// and map the frame immediately.
|
|
|
|
AVFrame *src_frame;
|
|
|
|
|
2017-03-04 23:57:40 +00:00
|
|
|
frame->format = ctx->format;
|
|
|
|
frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
|
|
|
|
if (!frame->hw_frames_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-10-25 19:38:47 +00:00
|
|
|
src_frame = av_frame_alloc();
|
|
|
|
if (!src_frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = av_hwframe_get_buffer(ctxi->source_frames,
|
2016-10-25 19:38:47 +00:00
|
|
|
src_frame, 0);
|
2018-01-26 23:12:18 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&src_frame);
|
2016-10-25 19:38:47 +00:00
|
|
|
return ret;
|
2018-01-26 23:12:18 +00:00
|
|
|
}
|
2016-10-25 19:38:47 +00:00
|
|
|
|
2017-03-04 23:57:40 +00:00
|
|
|
ret = av_hwframe_map(frame, src_frame,
|
2024-02-11 21:16:32 +00:00
|
|
|
ctxi->source_allocation_map_flags);
|
2016-10-25 19:38:47 +00:00
|
|
|
if (ret) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
|
|
|
|
"frame context: %d.\n", ret);
|
|
|
|
av_frame_free(&src_frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the source frame immediately - the mapped frame still
|
|
|
|
// contains a reference to it.
|
|
|
|
av_frame_free(&src_frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (!ctxi->hw_type->frames_get_buffer)
|
2015-11-20 18:47:29 +00:00
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
if (!ctx->pool)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
frame->hw_frames_ctx = av_buffer_ref(hwframe_ref);
|
|
|
|
if (!frame->hw_frames_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
ret = ctxi->hw_type->frames_get_buffer(ctx, frame);
|
2015-11-20 18:47:29 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_buffer_unref(&frame->hw_frames_ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:31:52 +00:00
|
|
|
frame->extended_data = frame->data;
|
|
|
|
|
2015-11-20 18:47:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-18 23:25:52 +00:00
|
|
|
|
|
|
|
void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
|
|
|
|
const HWContextType *hw_type = ctx->hw_type;
|
2016-02-18 23:25:52 +00:00
|
|
|
|
|
|
|
if (hw_type->device_hwconfig_size == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return av_mallocz(hw_type->device_hwconfig_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVHWFramesConstraints *av_hwdevice_get_hwframe_constraints(AVBufferRef *ref,
|
|
|
|
const void *hwconfig)
|
|
|
|
{
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *ctx = (FFHWDeviceContext*)ref->data;
|
|
|
|
const HWContextType *hw_type = ctx->hw_type;
|
2016-02-18 23:25:52 +00:00
|
|
|
AVHWFramesConstraints *constraints;
|
|
|
|
|
|
|
|
if (!hw_type->frames_get_constraints)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
constraints = av_mallocz(sizeof(*constraints));
|
|
|
|
if (!constraints)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
constraints->min_width = constraints->min_height = 0;
|
|
|
|
constraints->max_width = constraints->max_height = INT_MAX;
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
if (hw_type->frames_get_constraints(&ctx->p, hwconfig, constraints) >= 0) {
|
2016-02-18 23:25:52 +00:00
|
|
|
return constraints;
|
|
|
|
} else {
|
|
|
|
av_hwframe_constraints_free(&constraints);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
|
|
|
|
{
|
|
|
|
if (*constraints) {
|
|
|
|
av_freep(&(*constraints)->valid_hw_formats);
|
|
|
|
av_freep(&(*constraints)->valid_sw_formats);
|
|
|
|
}
|
|
|
|
av_freep(constraints);
|
|
|
|
}
|
2016-05-19 13:59:25 +00:00
|
|
|
|
|
|
|
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
|
|
|
|
const char *device, AVDictionary *opts, int flags)
|
|
|
|
{
|
|
|
|
AVBufferRef *device_ref = NULL;
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *device_ctx;
|
2016-05-19 13:59:25 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
device_ref = av_hwdevice_ctx_alloc(type);
|
|
|
|
if (!device_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2024-02-11 17:09:27 +00:00
|
|
|
device_ctx = (FFHWDeviceContext*)device_ref->data;
|
2016-05-19 13:59:25 +00:00
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
if (!device_ctx->hw_type->device_create) {
|
2016-05-19 13:59:25 +00:00
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-02-11 17:09:27 +00:00
|
|
|
ret = device_ctx->hw_type->device_create(&device_ctx->p, device,
|
|
|
|
opts, flags);
|
2016-05-19 13:59:25 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = av_hwdevice_ctx_init(device_ref);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*pdevice_ref = device_ref;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_buffer_unref(&device_ref);
|
|
|
|
*pdevice_ref = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-25 19:38:47 +00:00
|
|
|
|
2020-05-20 19:58:03 +00:00
|
|
|
int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr,
|
|
|
|
enum AVHWDeviceType type,
|
|
|
|
AVBufferRef *src_ref,
|
|
|
|
AVDictionary *options, int flags)
|
2017-03-04 23:57:28 +00:00
|
|
|
{
|
|
|
|
AVBufferRef *dst_ref = NULL, *tmp_ref;
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *dst_ctx;
|
2017-03-04 23:57:28 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
tmp_ref = src_ref;
|
|
|
|
while (tmp_ref) {
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
|
|
|
|
if (tmp_ctx->p.type == type) {
|
2017-03-04 23:57:28 +00:00
|
|
|
dst_ref = av_buffer_ref(tmp_ref);
|
|
|
|
if (!dst_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
2024-02-11 17:09:27 +00:00
|
|
|
tmp_ref = tmp_ctx->source_device;
|
2017-03-04 23:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst_ref = av_hwdevice_ctx_alloc(type);
|
|
|
|
if (!dst_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2024-02-11 17:09:27 +00:00
|
|
|
dst_ctx = (FFHWDeviceContext*)dst_ref->data;
|
2017-03-04 23:57:28 +00:00
|
|
|
|
|
|
|
tmp_ref = src_ref;
|
|
|
|
while (tmp_ref) {
|
2024-02-11 17:09:27 +00:00
|
|
|
FFHWDeviceContext *tmp_ctx = (FFHWDeviceContext*)tmp_ref->data;
|
|
|
|
if (dst_ctx->hw_type->device_derive) {
|
|
|
|
ret = dst_ctx->hw_type->device_derive(&dst_ctx->p,
|
|
|
|
&tmp_ctx->p,
|
|
|
|
options, flags);
|
2017-03-04 23:57:28 +00:00
|
|
|
if (ret == 0) {
|
2024-02-11 17:09:27 +00:00
|
|
|
dst_ctx->source_device = av_buffer_ref(src_ref);
|
|
|
|
if (!dst_ctx->source_device) {
|
2017-03-04 23:57:28 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-05-14 21:58:56 +00:00
|
|
|
ret = av_hwdevice_ctx_init(dst_ref);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2017-03-04 23:57:28 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (ret != AVERROR(ENOSYS))
|
|
|
|
goto fail;
|
|
|
|
}
|
2024-02-11 17:09:27 +00:00
|
|
|
tmp_ref = tmp_ctx->source_device;
|
2017-03-04 23:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
done:
|
|
|
|
*dst_ref_ptr = dst_ref;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_buffer_unref(&dst_ref);
|
|
|
|
*dst_ref_ptr = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:58:03 +00:00
|
|
|
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
|
|
|
|
enum AVHWDeviceType type,
|
|
|
|
AVBufferRef *src_ref, int flags)
|
|
|
|
{
|
|
|
|
return av_hwdevice_ctx_create_derived_opts(dst_ref_ptr, type, src_ref,
|
|
|
|
NULL, flags);
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:38:47 +00:00
|
|
|
static void ff_hwframe_unmap(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
|
|
|
|
AVHWFramesContext *ctx = opaque;
|
|
|
|
|
|
|
|
if (hwmap->unmap)
|
|
|
|
hwmap->unmap(ctx, hwmap);
|
|
|
|
|
|
|
|
av_frame_free(&hwmap->source);
|
|
|
|
|
|
|
|
av_buffer_unref(&hwmap->hw_frames_ctx);
|
|
|
|
|
|
|
|
av_free(hwmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
|
|
|
|
AVFrame *dst, const AVFrame *src,
|
|
|
|
void (*unmap)(AVHWFramesContext *ctx,
|
|
|
|
HWMapDescriptor *hwmap),
|
|
|
|
void *priv)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
|
|
|
|
HWMapDescriptor *hwmap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
hwmap = av_mallocz(sizeof(*hwmap));
|
|
|
|
if (!hwmap) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hwmap->source = av_frame_alloc();
|
|
|
|
if (!hwmap->source) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = av_frame_ref(hwmap->source, src);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
|
|
|
|
if (!hwmap->hw_frames_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hwmap->unmap = unmap;
|
|
|
|
hwmap->priv = priv;
|
|
|
|
|
|
|
|
dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
|
|
|
|
&ff_hwframe_unmap, ctx, 0);
|
|
|
|
if (!dst->buf[0]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (hwmap) {
|
|
|
|
av_buffer_unref(&hwmap->hw_frames_ctx);
|
|
|
|
av_frame_free(&hwmap->source);
|
|
|
|
}
|
|
|
|
av_free(hwmap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
2022-01-19 13:37:51 +00:00
|
|
|
AVBufferRef *orig_dst_frames = dst->hw_frames_ctx;
|
|
|
|
enum AVPixelFormat orig_dst_fmt = dst->format;
|
2016-10-25 19:38:47 +00:00
|
|
|
HWMapDescriptor *hwmap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (src->hw_frames_ctx && dst->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
|
|
|
|
FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
|
2016-10-25 19:38:47 +00:00
|
|
|
|
|
|
|
if ((src_frames == dst_frames &&
|
2024-02-11 21:16:32 +00:00
|
|
|
src->format == dst_frames->p.sw_format &&
|
|
|
|
dst->format == dst_frames->p.format) ||
|
|
|
|
(src_frames->source_frames &&
|
|
|
|
src_frames->source_frames->data ==
|
2016-10-25 19:38:47 +00:00
|
|
|
(uint8_t*)dst_frames)) {
|
|
|
|
// This is an unmap operation. We don't need to directly
|
|
|
|
// do anything here other than fill in the original frame,
|
|
|
|
// because the real unmap will be invoked when the last
|
|
|
|
// reference to the mapped frame disappears.
|
|
|
|
if (!src->buf[0]) {
|
|
|
|
av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
|
|
|
|
"found when attempting unmap.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
hwmap = (HWMapDescriptor*)src->buf[0]->data;
|
2023-09-07 10:02:26 +00:00
|
|
|
return av_frame_replace(dst, hwmap->source);
|
2016-10-25 19:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
|
2016-10-25 19:38:47 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (src_frames->p.format == src->format &&
|
|
|
|
src_frames->hw_type->map_from) {
|
|
|
|
ret = src_frames->hw_type->map_from(&src_frames->p,
|
|
|
|
dst, src, flags);
|
2022-01-19 13:37:51 +00:00
|
|
|
if (ret >= 0)
|
2016-10-25 19:38:47 +00:00
|
|
|
return ret;
|
2022-01-19 13:37:51 +00:00
|
|
|
else if (ret != AVERROR(ENOSYS))
|
|
|
|
goto fail;
|
2016-10-25 19:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst->hw_frames_ctx) {
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
|
2016-10-25 19:38:47 +00:00
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (dst_frames->p.format == dst->format &&
|
|
|
|
dst_frames->hw_type->map_to) {
|
|
|
|
ret = dst_frames->hw_type->map_to(&dst_frames->p,
|
|
|
|
dst, src, flags);
|
2022-01-19 13:37:51 +00:00
|
|
|
if (ret >= 0)
|
2016-10-25 19:38:47 +00:00
|
|
|
return ret;
|
2022-01-19 13:37:51 +00:00
|
|
|
else if (ret != AVERROR(ENOSYS))
|
|
|
|
goto fail;
|
2016-10-25 19:38:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR(ENOSYS);
|
2022-01-19 13:37:51 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
// if the caller provided dst frames context, it should be preserved
|
|
|
|
// by this function
|
|
|
|
av_assert0(orig_dst_frames == NULL ||
|
|
|
|
orig_dst_frames == dst->hw_frames_ctx);
|
|
|
|
|
|
|
|
// preserve user-provided dst frame fields, but clean
|
|
|
|
// anything we might have set
|
|
|
|
dst->hw_frames_ctx = NULL;
|
|
|
|
av_frame_unref(dst);
|
|
|
|
|
|
|
|
dst->hw_frames_ctx = orig_dst_frames;
|
|
|
|
dst->format = orig_dst_fmt;
|
|
|
|
|
|
|
|
return ret;
|
2016-10-25 19:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
|
|
|
|
enum AVPixelFormat format,
|
|
|
|
AVBufferRef *derived_device_ctx,
|
|
|
|
AVBufferRef *source_frame_ctx,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
AVBufferRef *dst_ref = NULL;
|
2024-02-11 21:16:32 +00:00
|
|
|
FFHWFramesContext *dsti = NULL;
|
|
|
|
FFHWFramesContext *srci = (FFHWFramesContext*)source_frame_ctx->data;
|
|
|
|
AVHWFramesContext *dst, *src = &srci->p;
|
2016-10-25 19:38:47 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
if (srci->source_frames) {
|
2016-10-25 19:38:47 +00:00
|
|
|
AVHWFramesContext *src_src =
|
2024-02-11 21:16:32 +00:00
|
|
|
(AVHWFramesContext*)srci->source_frames->data;
|
2016-10-25 19:38:47 +00:00
|
|
|
AVHWDeviceContext *dst_dev =
|
|
|
|
(AVHWDeviceContext*)derived_device_ctx->data;
|
|
|
|
|
|
|
|
if (src_src->device_ctx == dst_dev) {
|
|
|
|
// This is actually an unmapping, so we just return a
|
|
|
|
// reference to the source frame context.
|
2024-02-11 21:16:32 +00:00
|
|
|
*derived_frame_ctx = av_buffer_ref(srci->source_frames);
|
2016-10-25 19:38:47 +00:00
|
|
|
if (!*derived_frame_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
|
|
|
|
if (!dst_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
dsti = (FFHWFramesContext*)dst_ref->data;
|
|
|
|
dst = &dsti->p;
|
2016-10-25 19:38:47 +00:00
|
|
|
|
|
|
|
dst->format = format;
|
|
|
|
dst->sw_format = src->sw_format;
|
|
|
|
dst->width = src->width;
|
|
|
|
dst->height = src->height;
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
dsti->source_frames = av_buffer_ref(source_frame_ctx);
|
|
|
|
if (!dsti->source_frames) {
|
2016-10-25 19:38:47 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-02-11 21:16:32 +00:00
|
|
|
dsti->source_allocation_map_flags =
|
2017-03-04 23:57:40 +00:00
|
|
|
flags & (AV_HWFRAME_MAP_READ |
|
|
|
|
AV_HWFRAME_MAP_WRITE |
|
|
|
|
AV_HWFRAME_MAP_OVERWRITE |
|
|
|
|
AV_HWFRAME_MAP_DIRECT);
|
|
|
|
|
2017-03-04 23:57:39 +00:00
|
|
|
ret = AVERROR(ENOSYS);
|
2024-02-11 21:16:32 +00:00
|
|
|
if (srci->hw_type->frames_derive_from)
|
|
|
|
ret = srci->hw_type->frames_derive_from(dst, src, flags);
|
2017-03-04 23:57:39 +00:00
|
|
|
if (ret == AVERROR(ENOSYS) &&
|
2024-02-11 21:16:32 +00:00
|
|
|
dsti->hw_type->frames_derive_to)
|
|
|
|
ret = dsti->hw_type->frames_derive_to(dst, src, flags);
|
2017-03-04 23:57:39 +00:00
|
|
|
if (ret == AVERROR(ENOSYS))
|
|
|
|
ret = 0;
|
2016-10-25 19:38:47 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*derived_frame_ctx = dst_ref;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2024-02-11 21:16:32 +00:00
|
|
|
if (dsti)
|
|
|
|
av_buffer_unref(&dsti->source_frames);
|
2016-10-25 19:38:47 +00:00
|
|
|
av_buffer_unref(&dst_ref);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-04-20 04:30:03 +00:00
|
|
|
|
|
|
|
int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
|
|
|
|
{
|
|
|
|
HWMapDescriptor *hwmap = (HWMapDescriptor*)dst->buf[0]->data;
|
2023-09-07 10:02:26 +00:00
|
|
|
return av_frame_replace(hwmap->source, src);
|
2018-04-20 04:30:03 +00:00
|
|
|
}
|