Commit d41cae61f2 for ffmpeg

commit d41cae61f279ebf20e464b262bc862494b81bae6
Author: Julius Bairaktaris <julius@bairaktaris.de>
Date:   Wed Oct 28 14:48:14 2026 +0100

    avcodec/amfdec: re-init the frames context when the surface format differs

    With AMF-surface output, amf_decode_init() creates the frames context
    from AMF_VIDEO_DECODER_OUTPUT_FORMAT, falling back to NV12 when the
    decoder has not detected a format yet. The context is only re-created
    on AMF_RESOLUTION_CHANGED, so it keeps NV12 when the decoder then
    outputs P010 at the same size. The AV1 decoder does not report its
    format before decoding, so every 10-bit AV1 stream is output as P010
    surfaces in an NV12 frames context, and hwdownload to p010le fails with
    EINVAL.

    Re-create the frames context, keeping its size, when an output surface's
    format differs from its sw_format.

    Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: 10-bit AV1
    (raw OBU and Matroska) with -hwaccel amf -hwaccel_output_format amf and
    hwdownload to p010le now decodes, with the same framemd5 as software
    output from av1_amf; 8-bit AV1, h264_amf, hevc_amf (8 and 10 bit) and
    vp9_amf are unchanged.

    Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>

diff --git a/libavcodec/amfdec.c b/libavcodec/amfdec.c
index 00a17360f5..017327f6bc 100644
--- a/libavcodec/amfdec.c
+++ b/libavcodec/amfdec.c
@@ -512,6 +512,19 @@ static int amf_amfsurface_to_avframe(AVCodecContext *avctx, AMFSurface* surface,
     int                 format_amf;

     if (avctx->hw_device_ctx && ((AVHWDeviceContext*)avctx->hw_device_ctx->data)->type == AV_HWDEVICE_TYPE_AMF) {
+        AVHWFramesContext *frames_ctx;
+        enum AVPixelFormat sw_format;
+
+        if (!avctx->hw_frames_ctx)
+            return AVERROR(EINVAL);
+        frames_ctx = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
+        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);
+            if (ret < 0)
+                return ret;
+        }
+
         // prepare frame similar to  ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);

         ret = ff_decode_frame_props(avctx, frame);