avutil/hwcontext: Allocate AVHWFramesCtx jointly with its internals

This is possible because the lifetime of these structures coincide.
It has the advantage of allowing to remove AVHWFramesInternal
from the public header; given that AVHWFramesInternal.priv is no more,
most accesses to AVHWFramesInternal are no more; indeed, the only
field accessed of it outside of hwcontext.c is the internal frame pool,
making this commit very simple.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-02-11 22:16:32 +01:00 committed by James Almer
parent a0b9b96269
commit 3e669b24e2
13 changed files with 119 additions and 115 deletions

View file

@ -225,23 +225,23 @@ static const AVClass hwframe_ctx_class = {
static void hwframe_ctx_free(void *opaque, uint8_t *data) static void hwframe_ctx_free(void *opaque, uint8_t *data)
{ {
AVHWFramesContext *ctx = (AVHWFramesContext*)data; FFHWFramesContext *ctxi = (FFHWFramesContext*)data;
AVHWFramesContext *ctx = &ctxi->p;
if (ctx->internal->pool_internal) if (ctxi->pool_internal)
av_buffer_pool_uninit(&ctx->internal->pool_internal); av_buffer_pool_uninit(&ctxi->pool_internal);
if (ctx->internal->hw_type->frames_uninit) if (ctxi->hw_type->frames_uninit)
ctx->internal->hw_type->frames_uninit(ctx); ctxi->hw_type->frames_uninit(ctx);
if (ctx->free) if (ctx->free)
ctx->free(ctx); ctx->free(ctx);
av_buffer_unref(&ctx->internal->source_frames); av_buffer_unref(&ctxi->source_frames);
av_buffer_unref(&ctx->device_ref); av_buffer_unref(&ctx->device_ref);
av_freep(&ctx->hwctx); av_freep(&ctx->hwctx);
av_freep(&ctx->internal);
av_freep(&ctx); av_freep(&ctx);
} }
@ -249,16 +249,14 @@ AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
{ {
FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)device_ref_in->data; FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)device_ref_in->data;
const HWContextType *hw_type = device_ctx->hw_type; const HWContextType *hw_type = device_ctx->hw_type;
FFHWFramesContext *ctxi;
AVHWFramesContext *ctx; AVHWFramesContext *ctx;
AVBufferRef *buf, *device_ref = NULL; AVBufferRef *buf, *device_ref = NULL;
ctx = av_mallocz(sizeof(*ctx)); ctxi = av_mallocz(sizeof(*ctxi));
if (!ctx) if (!ctxi)
return NULL; return NULL;
ctx = &ctxi->p;
ctx->internal = av_mallocz(sizeof(*ctx->internal));
if (!ctx->internal)
goto fail;
if (hw_type->frames_hwctx_size) { if (hw_type->frames_hwctx_size) {
ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size); ctx->hwctx = av_mallocz(hw_type->frames_hwctx_size);
@ -282,13 +280,12 @@ AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
ctx->format = AV_PIX_FMT_NONE; ctx->format = AV_PIX_FMT_NONE;
ctx->sw_format = AV_PIX_FMT_NONE; ctx->sw_format = AV_PIX_FMT_NONE;
ctx->internal->hw_type = hw_type; ctxi->hw_type = hw_type;
return buf; return buf;
fail: fail:
av_buffer_unref(&device_ref); av_buffer_unref(&device_ref);
av_freep(&ctx->internal);
av_freep(&ctx->hwctx); av_freep(&ctx->hwctx);
av_freep(&ctx); av_freep(&ctx);
return NULL; return NULL;
@ -324,24 +321,25 @@ fail:
int av_hwframe_ctx_init(AVBufferRef *ref) int av_hwframe_ctx_init(AVBufferRef *ref)
{ {
AVHWFramesContext *ctx = (AVHWFramesContext*)ref->data; FFHWFramesContext *ctxi = (FFHWFramesContext*)ref->data;
AVHWFramesContext *ctx = &ctxi->p;
const enum AVPixelFormat *pix_fmt; const enum AVPixelFormat *pix_fmt;
int ret; int ret;
if (ctx->internal->source_frames) { if (ctxi->source_frames) {
/* A derived frame context is already initialised. */ /* A derived frame context is already initialised. */
return 0; return 0;
} }
/* validate the pixel format */ /* validate the pixel format */
for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) { for (pix_fmt = ctxi->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
if (*pix_fmt == ctx->format) if (*pix_fmt == ctx->format)
break; break;
} }
if (*pix_fmt == AV_PIX_FMT_NONE) { if (*pix_fmt == AV_PIX_FMT_NONE) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"The hardware pixel format '%s' is not supported by the device type '%s'\n", "The hardware pixel format '%s' is not supported by the device type '%s'\n",
av_get_pix_fmt_name(ctx->format), ctx->internal->hw_type->name); av_get_pix_fmt_name(ctx->format), ctxi->hw_type->name);
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
} }
@ -351,14 +349,14 @@ int av_hwframe_ctx_init(AVBufferRef *ref)
return ret; return ret;
/* format-specific init */ /* format-specific init */
if (ctx->internal->hw_type->frames_init) { if (ctxi->hw_type->frames_init) {
ret = ctx->internal->hw_type->frames_init(ctx); ret = ctxi->hw_type->frames_init(ctx);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
if (ctx->internal->pool_internal && !ctx->pool) if (ctxi->pool_internal && !ctx->pool)
ctx->pool = ctx->internal->pool_internal; ctx->pool = ctxi->pool_internal;
/* preallocate the frames in the pool, if requested */ /* preallocate the frames in the pool, if requested */
if (ctx->initial_pool_size > 0) { if (ctx->initial_pool_size > 0) {
@ -374,12 +372,12 @@ int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
enum AVHWFrameTransferDirection dir, enum AVHWFrameTransferDirection dir,
enum AVPixelFormat **formats, int flags) enum AVPixelFormat **formats, int flags)
{ {
AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data; FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
if (!ctx->internal->hw_type->transfer_get_formats) if (!ctxi->hw_type->transfer_get_formats)
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
return ctx->internal->hw_type->transfer_get_formats(ctx, dir, formats); return ctxi->hw_type->transfer_get_formats(&ctxi->p, dir, formats);
} }
static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags) static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
@ -434,7 +432,6 @@ fail:
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags) int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
{ {
AVHWFramesContext *ctx;
int ret; int ret;
if (!dst->buf[0]) if (!dst->buf[0])
@ -447,41 +444,41 @@ int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
* the specific combination of hardware. * the specific combination of hardware.
*/ */
if (src->hw_frames_ctx && dst->hw_frames_ctx) { if (src->hw_frames_ctx && dst->hw_frames_ctx) {
AVHWFramesContext *src_ctx = FFHWFramesContext *src_ctx =
(AVHWFramesContext*)src->hw_frames_ctx->data; (FFHWFramesContext*)src->hw_frames_ctx->data;
AVHWFramesContext *dst_ctx = FFHWFramesContext *dst_ctx =
(AVHWFramesContext*)dst->hw_frames_ctx->data; (FFHWFramesContext*)dst->hw_frames_ctx->data;
if (src_ctx->internal->source_frames) { if (src_ctx->source_frames) {
av_log(src_ctx, AV_LOG_ERROR, av_log(src_ctx, AV_LOG_ERROR,
"A device with a derived frame context cannot be used as " "A device with a derived frame context cannot be used as "
"the source of a HW -> HW transfer."); "the source of a HW -> HW transfer.");
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
} }
if (dst_ctx->internal->source_frames) { if (dst_ctx->source_frames) {
av_log(src_ctx, AV_LOG_ERROR, av_log(src_ctx, AV_LOG_ERROR,
"A device with a derived frame context cannot be used as " "A device with a derived frame context cannot be used as "
"the destination of a HW -> HW transfer."); "the destination of a HW -> HW transfer.");
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
} }
ret = src_ctx->internal->hw_type->transfer_data_from(src_ctx, dst, src); ret = src_ctx->hw_type->transfer_data_from(&src_ctx->p, dst, src);
if (ret == AVERROR(ENOSYS)) if (ret == AVERROR(ENOSYS))
ret = dst_ctx->internal->hw_type->transfer_data_to(dst_ctx, dst, src); ret = dst_ctx->hw_type->transfer_data_to(&dst_ctx->p, dst, src);
if (ret < 0) if (ret < 0)
return ret; return ret;
} else { } else {
if (src->hw_frames_ctx) { if (src->hw_frames_ctx) {
ctx = (AVHWFramesContext*)src->hw_frames_ctx->data; FFHWFramesContext *ctx = (FFHWFramesContext*)src->hw_frames_ctx->data;
ret = ctx->internal->hw_type->transfer_data_from(ctx, dst, src); ret = ctx->hw_type->transfer_data_from(&ctx->p, dst, src);
if (ret < 0) if (ret < 0)
return ret; return ret;
} else if (dst->hw_frames_ctx) { } else if (dst->hw_frames_ctx) {
ctx = (AVHWFramesContext*)dst->hw_frames_ctx->data; FFHWFramesContext *ctx = (FFHWFramesContext*)dst->hw_frames_ctx->data;
ret = ctx->internal->hw_type->transfer_data_to(ctx, dst, src); ret = ctx->hw_type->transfer_data_to(&ctx->p, dst, src);
if (ret < 0) if (ret < 0)
return ret; return ret;
} else { } else {
@ -493,10 +490,11 @@ int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags) int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
{ {
AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data; FFHWFramesContext *ctxi = (FFHWFramesContext*)hwframe_ref->data;
AVHWFramesContext *ctx = &ctxi->p;
int ret; int ret;
if (ctx->internal->source_frames) { if (ctxi->source_frames) {
// This is a derived frame context, so we allocate in the source // This is a derived frame context, so we allocate in the source
// and map the frame immediately. // and map the frame immediately.
AVFrame *src_frame; AVFrame *src_frame;
@ -510,7 +508,7 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
if (!src_frame) if (!src_frame)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ret = av_hwframe_get_buffer(ctx->internal->source_frames, ret = av_hwframe_get_buffer(ctxi->source_frames,
src_frame, 0); src_frame, 0);
if (ret < 0) { if (ret < 0) {
av_frame_free(&src_frame); av_frame_free(&src_frame);
@ -518,7 +516,7 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
} }
ret = av_hwframe_map(frame, src_frame, ret = av_hwframe_map(frame, src_frame,
ctx->internal->source_allocation_map_flags); ctxi->source_allocation_map_flags);
if (ret) { if (ret) {
av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived " av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
"frame context: %d.\n", ret); "frame context: %d.\n", ret);
@ -533,7 +531,7 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
return 0; return 0;
} }
if (!ctx->internal->hw_type->frames_get_buffer) if (!ctxi->hw_type->frames_get_buffer)
return AVERROR(ENOSYS); return AVERROR(ENOSYS);
if (!ctx->pool) if (!ctx->pool)
@ -543,7 +541,7 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
if (!frame->hw_frames_ctx) if (!frame->hw_frames_ctx)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ret = ctx->internal->hw_type->frames_get_buffer(ctx, frame); ret = ctxi->hw_type->frames_get_buffer(ctx, frame);
if (ret < 0) { if (ret < 0) {
av_buffer_unref(&frame->hw_frames_ctx); av_buffer_unref(&frame->hw_frames_ctx);
return ret; return ret;
@ -781,19 +779,18 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
{ {
AVBufferRef *orig_dst_frames = dst->hw_frames_ctx; AVBufferRef *orig_dst_frames = dst->hw_frames_ctx;
enum AVPixelFormat orig_dst_fmt = dst->format; enum AVPixelFormat orig_dst_fmt = dst->format;
AVHWFramesContext *src_frames, *dst_frames;
HWMapDescriptor *hwmap; HWMapDescriptor *hwmap;
int ret; int ret;
if (src->hw_frames_ctx && dst->hw_frames_ctx) { if (src->hw_frames_ctx && dst->hw_frames_ctx) {
src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data; FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data; FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
if ((src_frames == dst_frames && if ((src_frames == dst_frames &&
src->format == dst_frames->sw_format && src->format == dst_frames->p.sw_format &&
dst->format == dst_frames->format) || dst->format == dst_frames->p.format) ||
(src_frames->internal->source_frames && (src_frames->source_frames &&
src_frames->internal->source_frames->data == src_frames->source_frames->data ==
(uint8_t*)dst_frames)) { (uint8_t*)dst_frames)) {
// This is an unmap operation. We don't need to directly // This is an unmap operation. We don't need to directly
// do anything here other than fill in the original frame, // do anything here other than fill in the original frame,
@ -810,11 +807,11 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
} }
if (src->hw_frames_ctx) { if (src->hw_frames_ctx) {
src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data; FFHWFramesContext *src_frames = (FFHWFramesContext*)src->hw_frames_ctx->data;
if (src_frames->format == src->format && if (src_frames->p.format == src->format &&
src_frames->internal->hw_type->map_from) { src_frames->hw_type->map_from) {
ret = src_frames->internal->hw_type->map_from(src_frames, ret = src_frames->hw_type->map_from(&src_frames->p,
dst, src, flags); dst, src, flags);
if (ret >= 0) if (ret >= 0)
return ret; return ret;
@ -824,11 +821,11 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
} }
if (dst->hw_frames_ctx) { if (dst->hw_frames_ctx) {
dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data; FFHWFramesContext *dst_frames = (FFHWFramesContext*)dst->hw_frames_ctx->data;
if (dst_frames->format == dst->format && if (dst_frames->p.format == dst->format &&
dst_frames->internal->hw_type->map_to) { dst_frames->hw_type->map_to) {
ret = dst_frames->internal->hw_type->map_to(dst_frames, ret = dst_frames->hw_type->map_to(&dst_frames->p,
dst, src, flags); dst, src, flags);
if (ret >= 0) if (ret >= 0)
return ret; return ret;
@ -863,21 +860,21 @@ int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
int flags) int flags)
{ {
AVBufferRef *dst_ref = NULL; AVBufferRef *dst_ref = NULL;
AVHWFramesContext *dst = NULL; FFHWFramesContext *dsti = NULL;
AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data; FFHWFramesContext *srci = (FFHWFramesContext*)source_frame_ctx->data;
AVHWFramesContext *dst, *src = &srci->p;
int ret; int ret;
if (src->internal->source_frames) { if (srci->source_frames) {
AVHWFramesContext *src_src = AVHWFramesContext *src_src =
(AVHWFramesContext*)src->internal->source_frames->data; (AVHWFramesContext*)srci->source_frames->data;
AVHWDeviceContext *dst_dev = AVHWDeviceContext *dst_dev =
(AVHWDeviceContext*)derived_device_ctx->data; (AVHWDeviceContext*)derived_device_ctx->data;
if (src_src->device_ctx == dst_dev) { if (src_src->device_ctx == dst_dev) {
// This is actually an unmapping, so we just return a // This is actually an unmapping, so we just return a
// reference to the source frame context. // reference to the source frame context.
*derived_frame_ctx = *derived_frame_ctx = av_buffer_ref(srci->source_frames);
av_buffer_ref(src->internal->source_frames);
if (!*derived_frame_ctx) { if (!*derived_frame_ctx) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
goto fail; goto fail;
@ -892,31 +889,32 @@ int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
goto fail; goto fail;
} }
dst = (AVHWFramesContext*)dst_ref->data; dsti = (FFHWFramesContext*)dst_ref->data;
dst = &dsti->p;
dst->format = format; dst->format = format;
dst->sw_format = src->sw_format; dst->sw_format = src->sw_format;
dst->width = src->width; dst->width = src->width;
dst->height = src->height; dst->height = src->height;
dst->internal->source_frames = av_buffer_ref(source_frame_ctx); dsti->source_frames = av_buffer_ref(source_frame_ctx);
if (!dst->internal->source_frames) { if (!dsti->source_frames) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
goto fail; goto fail;
} }
dst->internal->source_allocation_map_flags = dsti->source_allocation_map_flags =
flags & (AV_HWFRAME_MAP_READ | flags & (AV_HWFRAME_MAP_READ |
AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_WRITE |
AV_HWFRAME_MAP_OVERWRITE | AV_HWFRAME_MAP_OVERWRITE |
AV_HWFRAME_MAP_DIRECT); AV_HWFRAME_MAP_DIRECT);
ret = AVERROR(ENOSYS); ret = AVERROR(ENOSYS);
if (src->internal->hw_type->frames_derive_from) if (srci->hw_type->frames_derive_from)
ret = src->internal->hw_type->frames_derive_from(dst, src, flags); ret = srci->hw_type->frames_derive_from(dst, src, flags);
if (ret == AVERROR(ENOSYS) && if (ret == AVERROR(ENOSYS) &&
dst->internal->hw_type->frames_derive_to) dsti->hw_type->frames_derive_to)
ret = dst->internal->hw_type->frames_derive_to(dst, src, flags); ret = dsti->hw_type->frames_derive_to(dst, src, flags);
if (ret == AVERROR(ENOSYS)) if (ret == AVERROR(ENOSYS))
ret = 0; ret = 0;
if (ret) if (ret)
@ -926,8 +924,8 @@ int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
return 0; return 0;
fail: fail:
if (dst) if (dsti)
av_buffer_unref(&dst->internal->source_frames); av_buffer_unref(&dsti->source_frames);
av_buffer_unref(&dst_ref); av_buffer_unref(&dst_ref);
return ret; return ret;
} }

View file

@ -102,8 +102,6 @@ typedef struct AVHWDeviceContext {
void *user_opaque; void *user_opaque;
} AVHWDeviceContext; } AVHWDeviceContext;
typedef struct AVHWFramesInternal AVHWFramesInternal;
/** /**
* This struct describes a set or pool of "hardware" frames (i.e. those with * This struct describes a set or pool of "hardware" frames (i.e. those with
* data not located in normal system memory). All the frames in the pool are * data not located in normal system memory). All the frames in the pool are
@ -120,12 +118,6 @@ typedef struct AVHWFramesContext {
*/ */
const AVClass *av_class; const AVClass *av_class;
/**
* Private data used internally by libavutil. Must not be accessed in any
* way by the caller.
*/
AVHWFramesInternal *internal;
/** /**
* A reference to the parent AVHWDeviceContext. This reference is owned and * A reference to the parent AVHWDeviceContext. This reference is owned and
* managed by the enclosing AVHWFramesContext, but the caller may derive * managed by the enclosing AVHWFramesContext, but the caller may derive

View file

@ -170,8 +170,9 @@ static int cuda_frames_init(AVHWFramesContext *ctx)
if (size < 0) if (size < 0)
return size; return size;
ctx->internal->pool_internal = av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL); ffhwframesctx(ctx)->pool_internal =
if (!ctx->internal->pool_internal) av_buffer_pool_init2(size, ctx, cuda_pool_alloc, NULL);
if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }

View file

@ -319,9 +319,10 @@ static int d3d11va_frames_init(AVHWFramesContext *ctx)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
s->nb_surfaces = ctx->initial_pool_size; s->nb_surfaces = ctx->initial_pool_size;
ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(AVD3D11FrameDescriptor), ffhwframesctx(ctx)->pool_internal =
av_buffer_pool_init2(sizeof(AVD3D11FrameDescriptor),
ctx, d3d11va_pool_alloc, NULL); ctx, d3d11va_pool_alloc, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
return 0; return 0;

View file

@ -297,10 +297,10 @@ static int d3d12va_frames_init(AVHWFramesContext *ctx)
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(AVD3D12VAFrame), ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(AVD3D12VAFrame),
ctx, d3d12va_pool_alloc, NULL); ctx, d3d12va_pool_alloc, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
return 0; return 0;

View file

@ -208,9 +208,10 @@ static int dxva2_init_pool(AVHWFramesContext *ctx)
return AVERROR_UNKNOWN; return AVERROR_UNKNOWN;
} }
ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal), ffhwframesctx(ctx)->pool_internal =
av_buffer_pool_init2(sizeof(*s->surfaces_internal),
ctx, dxva2_pool_alloc, NULL); ctx, dxva2_pool_alloc, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
frames_hwctx->surfaces = s->surfaces_internal; frames_hwctx->surfaces = s->surfaces_internal;

View file

@ -90,7 +90,12 @@ typedef struct HWContextType {
AVHWFramesContext *src_ctx, int flags); AVHWFramesContext *src_ctx, int flags);
} HWContextType; } HWContextType;
struct AVHWFramesInternal { typedef struct FFHWFramesContext {
/**
* The public AVHWFramesContext. See hwcontext.h for it.
*/
AVHWFramesContext p;
const HWContextType *hw_type; const HWContextType *hw_type;
AVBufferPool *pool_internal; AVBufferPool *pool_internal;
@ -105,7 +110,12 @@ struct AVHWFramesInternal {
* frame context when trying to allocate in the derived context. * frame context when trying to allocate in the derived context.
*/ */
int source_allocation_map_flags; int source_allocation_map_flags;
}; } FFHWFramesContext;
static inline FFHWFramesContext *ffhwframesctx(AVHWFramesContext *ctx)
{
return (FFHWFramesContext*)ctx;
}
typedef struct HWMapDescriptor { typedef struct HWMapDescriptor {
/** /**

View file

@ -1725,10 +1725,10 @@ static int opencl_frames_init_command_queue(AVHWFramesContext *hwfc)
static int opencl_frames_init(AVHWFramesContext *hwfc) static int opencl_frames_init(AVHWFramesContext *hwfc)
{ {
if (!hwfc->pool) { if (!hwfc->pool) {
hwfc->internal->pool_internal = ffhwframesctx(hwfc)->pool_internal =
av_buffer_pool_init2(sizeof(cl_mem), hwfc, av_buffer_pool_init2(sizeof(cl_mem), hwfc,
&opencl_pool_alloc, NULL); &opencl_pool_alloc, NULL);
if (!hwfc->internal->pool_internal) if (!ffhwframesctx(hwfc)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }

View file

@ -605,9 +605,9 @@ static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
return ret; return ret;
#endif #endif
ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1), ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
ctx, qsv_pool_alloc, NULL); ctx, qsv_pool_alloc, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
frames_hwctx->surfaces = s->surfaces_internal; frames_hwctx->surfaces = s->surfaces_internal;

View file

@ -633,10 +633,10 @@ static int vaapi_frames_init(AVHWFramesContext *hwfc)
avfc->surface_ids = NULL; avfc->surface_ids = NULL;
} }
hwfc->internal->pool_internal = ffhwframesctx(hwfc)->pool_internal =
av_buffer_pool_init2(sizeof(VASurfaceID), hwfc, av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
&vaapi_pool_alloc, NULL); &vaapi_pool_alloc, NULL);
if (!hwfc->internal->pool_internal) { if (!ffhwframesctx(hwfc)->pool_internal) {
av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n"); av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
err = AVERROR(ENOMEM); err = AVERROR(ENOMEM);
goto fail; goto fail;
@ -654,7 +654,7 @@ static int vaapi_frames_init(AVHWFramesContext *hwfc)
goto fail; goto fail;
} }
} else { } else {
test_surface = av_buffer_pool_get(hwfc->internal->pool_internal); test_surface = av_buffer_pool_get(ffhwframesctx(hwfc)->pool_internal);
if (!test_surface) { if (!test_surface) {
av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from " av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
"internal buffer pool.\n"); "internal buffer pool.\n");

View file

@ -281,9 +281,10 @@ static int vdpau_frames_init(AVHWFramesContext *ctx)
} }
if (!ctx->pool) { if (!ctx->pool) {
ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx, ffhwframesctx(ctx)->pool_internal =
av_buffer_pool_init2(sizeof(VdpVideoSurface), ctx,
vdpau_pool_alloc, NULL); vdpau_pool_alloc, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }

View file

@ -286,9 +286,9 @@ static int vt_frames_init(AVHWFramesContext *ctx)
} }
if (!ctx->pool) { if (!ctx->pool) {
ctx->internal->pool_internal = av_buffer_pool_init2( ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(
sizeof(CVPixelBufferRef), ctx, vt_pool_alloc_buffer, NULL); sizeof(CVPixelBufferRef), ctx, vt_pool_alloc_buffer, NULL);
if (!ctx->internal->pool_internal) if (!ffhwframesctx(ctx)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }

View file

@ -2406,10 +2406,10 @@ static int vulkan_frames_init(AVHWFramesContext *hwfc)
/* If user did not specify a pool, hwfc->pool will be set to the internal one /* If user did not specify a pool, hwfc->pool will be set to the internal one
* in hwcontext.c just after this gets called */ * in hwcontext.c just after this gets called */
if (!hwfc->pool) { if (!hwfc->pool) {
hwfc->internal->pool_internal = av_buffer_pool_init2(sizeof(AVVkFrame), ffhwframesctx(hwfc)->pool_internal = av_buffer_pool_init2(sizeof(AVVkFrame),
hwfc, vulkan_pool_alloc, hwfc, vulkan_pool_alloc,
NULL); NULL);
if (!hwfc->internal->pool_internal) if (!ffhwframesctx(hwfc)->pool_internal)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }