Commit 6ea799a675 for ffmpeg
commit 6ea799a67599514eae2ac5e8c58db498546520b1
Author: Julius Bairaktaris <julius@bairaktaris.de>
Date: Sun Nov 1 12:05:55 2026 +0100
avcodec/amfdec: detect the initial size before converting the first surface
When the decoder is opened without dimensions, amf_init_dimensions()
reads AMF_VIDEO_DECODER_CURRENT_SIZE only after the first output frame
has been converted. The frames context size check for that frame
therefore sees zero dimensions, and the frame is labelled with the size
only afterwards, through the generic code filling in avctx->width and
height, so the check never covers the size the frame ends up with.
Detect the size before the first surface is converted.
Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029, with a test
program that opens the decoder with and without dimensions: h264_amf,
vp9_amf and av1_amf (1080p), hevc_amf (8-bit 800p, 10-bit 640x360) give
the same pts, sizes and downloaded picture hashes for every frame in
both cases, with system memory and AMF surface output.
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
diff --git a/libavcodec/amfdec.c b/libavcodec/amfdec.c
index 5c0bae4f7c..498ddfd3c2 100644
--- a/libavcodec/amfdec.c
+++ b/libavcodec/amfdec.c
@@ -503,6 +503,25 @@ static int amf_attach_hdr_metadata(AVCodecContext *avctx, AMFSurface *surface, A
return ret;
}
+static void amf_init_dimensions(AVCodecContext *avctx)
+{
+ AMFDecoderContext *ctx = avctx->priv_data;
+ AMFVariantStruct size_var = {0};
+ AMF_RESULT res = AMF_OK;
+
+ res = ctx->decoder->pVtbl->GetProperty(ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
+ if (res == AMF_OK && size_var.sizeValue.width > 0 && size_var.sizeValue.height > 0) {
+ avctx->width = size_var.sizeValue.width;
+ avctx->height = size_var.sizeValue.height;
+ avctx->coded_width = size_var.sizeValue.width;
+ avctx->coded_height = size_var.sizeValue.height;
+
+ ctx->dimensions_initialized = 1;
+
+ av_log(avctx, AV_LOG_DEBUG, "AMF: detected initial decoder size %dx%d\n", avctx->width, avctx->height);
+ }
+}
+
static int amf_amfsurface_to_avframe(AVCodecContext *avctx, AMFSurface* surface, AVFrame *frame)
{
AMFVariantStruct var = {0};
@@ -511,6 +530,9 @@ static int amf_amfsurface_to_avframe(AVCodecContext *avctx, AMFSurface* surface,
int ret;
int format_amf;
+ if (!((AMFDecoderContext *)avctx->priv_data)->dimensions_initialized)
+ amf_init_dimensions(avctx);
+
if (avctx->hw_device_ctx && ((AVHWDeviceContext*)avctx->hw_device_ctx->data)->type == AV_HWDEVICE_TYPE_AMF) {
AVHWFramesContext *frames_ctx;
enum AVPixelFormat sw_format;
@@ -673,25 +695,6 @@ static AMF_RESULT amf_buffer_from_packet(AVCodecContext *avctx, const AVPacket*
return amf_update_buffer_properties(avctx, buf, pkt);
}
-static void amf_init_dimensions(AVCodecContext *avctx)
-{
- AMFDecoderContext *ctx = avctx->priv_data;
- AMFVariantStruct size_var = {0};
- AMF_RESULT res = AMF_OK;
-
- res = ctx->decoder->pVtbl->GetProperty(ctx->decoder, AMF_VIDEO_DECODER_CURRENT_SIZE, &size_var);
- if (res == AMF_OK && size_var.sizeValue.width > 0 && size_var.sizeValue.height > 0) {
- avctx->width = size_var.sizeValue.width;
- avctx->height = size_var.sizeValue.height;
- avctx->coded_width = size_var.sizeValue.width;
- avctx->coded_height = size_var.sizeValue.height;
-
- ctx->dimensions_initialized = 1;
-
- av_log(avctx, AV_LOG_DEBUG, "AMF: detected initial decoder size %dx%d\n", avctx->width, avctx->height);
- }
-}
-
static int amf_decode_frame(AVCodecContext *avctx, struct AVFrame *frame)
{
AMFDecoderContext *ctx = avctx->priv_data;
@@ -753,11 +756,9 @@ static int amf_decode_frame(AVCodecContext *avctx, struct AVFrame *frame)
}
res = amf_receive_frame(avctx, frame);
- if (res == AMF_OK) {
+ if (res == AMF_OK)
got_frame = 1;
- if (!ctx->dimensions_initialized)
- amf_init_dimensions(avctx);
- } else if (res == AMF_REPEAT)
+ else if (res == AMF_REPEAT)
// decoder has no output yet
res = AMF_OK;
else if (res == AMF_EOF) {