Commit 66770cfc38 for ffmpeg
commit 66770cfc386c84e27b1c98c9f34f3fd34f362fb4
Author: Julius Bairaktaris <julius@bairaktaris.de>
Date: Sat Oct 24 12:14:12 2026 +0200
avutil/hwcontext_amf: download without converting the source surface
amf_transfer_data_from() converted the source surface to host memory in
place. The surface is shared by every reference to the frame, so a
download through one reference moved the surface of all others to host
memory, and the runtime handed its released texture to the next
allocation. Copy the surface into a new host surface with Duplicate()
instead, under the device lock that the Convert() call held, and read
host surfaces directly.
Tested on Windows 11, RX 9070 XT, driver 32.0.32015.2008: after a
download through a second reference the surface stays AMF_MEMORY_DX11,
and its texture is no longer handed to new surfaces. 1200 frames of
vqe_amf,hwdownload take 1.47 s against 1.49 s before (median of 3).
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 994411383b..25758077b6 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -504,6 +504,8 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
{
AMFSurface* surface = (AMFSurface*)src->data[0];
AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
+ const AMFGuid guid = IID_AMFSurface();
+ AMFData *data;
AMFPlane *plane;
uint8_t *src_data[4];
int src_linesize[4];
@@ -516,10 +518,17 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
if (src->hw_frames_ctx->data != (uint8_t *)ctx || dst->format != ctx->sw_format)
return AVERROR(EINVAL);
- amf_ctx->lock(amf_ctx->lock_ctx);
- ret = surface->pVtbl->Convert(surface, AMF_MEMORY_HOST);
- amf_ctx->unlock(amf_ctx->lock_ctx);
- AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, "Convert(amf::AMF_MEMORY_HOST) failed with error %d\n", ret);
+ if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_HOST) {
+ surface->pVtbl->Acquire(surface);
+ } else {
+ amf_ctx->lock(amf_ctx->lock_ctx);
+ ret = surface->pVtbl->Duplicate(surface, AMF_MEMORY_HOST, &data);
+ amf_ctx->unlock(amf_ctx->lock_ctx);
+ AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, "Duplicate(AMF_MEMORY_HOST) failed with error %d\n", ret);
+ ret = data->pVtbl->QueryInterface(data, &guid, (void**)&surface);
+ data->pVtbl->Release(data);
+ AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, "QueryInterface(AMFSurface) failed with error %d\n", ret);
+ }
planes = (int)surface->pVtbl->GetPlanesCount(surface);
av_assert0(planes < FF_ARRAY_ELEMS(src_data));
@@ -532,6 +541,7 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
av_image_copy2(dst->data, dst->linesize,
src_data, src_linesize, dst->format,
w, h);
+ surface->pVtbl->Release(surface);
return 0;
}