Commit 95bf06561d for ffmpeg
commit 95bf06561d506f9a81bc4ac5302c3c6a22155ab5
Author: Julius Bairaktaris <julius@bairaktaris.de>
Date: Sat Oct 31 12:37:03 2026 +0100
avcodec/amfdec: re-init the frames context when the surface size differs
When the caller opens the decoder without dimensions, amf_decode_init()
creates the frames context with a 1280x720 fallback, and nothing updates
it once the decoder reports the real size. Larger surfaces were then
wrapped in a frame whose frames context is smaller than the picture.
av_hwframe_transfer_data() allocates its destination from the frames
context, copies only that much, and then labels the result with the
picture's size, so every reader of the downloaded frame accessed memory
past the end of its buffers and the top-left 1280x720 was all it held.
Re-create the frames context, in the same place as the format check,
when it is smaller than the surface or than the size the frame is
labelled with (avctx->width/height), using the larger of the two, so a
download never allocates less than the frame claims. A surface without
a valid plane size is rejected instead of tearing down the context, and
a context that a failed re-creation left unset is re-created from the
next surface instead of failing every following frame.
Reported by the Forgejo_Fairy review of #25561.
Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029, with a test
program that opens the decoder without dimensions and downloads every
frame of 1920x1080 streams. Before, h264_amf, hevc_amf and av1_amf
crashed when the first downloaded frame was read, and every download of
10-bit hevc_amf failed with EINVAL. Now all frames decode with the same
output as when the dimensions are set. The ffmpeg CLI, which always sets
the dimensions, gives unchanged framemd5 for h264_amf, hevc_amf
(including a resolution change), av1_amf and vp9_amf.
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
diff --git a/libavcodec/amfdec.c b/libavcodec/amfdec.c
index 017327f6bc..5c0bae4f7c 100644
--- a/libavcodec/amfdec.c
+++ b/libavcodec/amfdec.c
@@ -514,13 +514,19 @@ static int amf_amfsurface_to_avframe(AVCodecContext *avctx, AMFSurface* surface,
if (avctx->hw_device_ctx && ((AVHWDeviceContext*)avctx->hw_device_ctx->data)->type == AV_HWDEVICE_TYPE_AMF) {
AVHWFramesContext *frames_ctx;
enum AVPixelFormat sw_format;
+ int width, height;
- if (!avctx->hw_frames_ctx)
- return AVERROR(EINVAL);
- frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
+ frames_ctx = avctx->hw_frames_ctx ?
+ (AVHWFramesContext *)avctx->hw_frames_ctx->data : NULL;
sw_format = av_amf_to_av_format(surface->pVtbl->GetFormat(surface));
- if (sw_format != frames_ctx->sw_format) {
- ret = amf_reinit_frames_context(avctx, sw_format, frames_ctx->width, frames_ctx->height);
+ plane = surface->pVtbl->GetPlaneAt(surface, 0);
+ if (!plane || plane->pVtbl->GetWidth(plane) <= 0 || plane->pVtbl->GetHeight(plane) <= 0)
+ return AVERROR_EXTERNAL;
+ width = FFMAX(plane->pVtbl->GetWidth(plane), avctx->width);
+ height = FFMAX(plane->pVtbl->GetHeight(plane), avctx->height);
+ if (!frames_ctx || sw_format != frames_ctx->sw_format ||
+ width > frames_ctx->width || height > frames_ctx->height) {
+ ret = amf_reinit_frames_context(avctx, sw_format, width, height);
if (ret < 0)
return ret;
}