PR #24688 opened by ArthurVasseur-Kyber
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24688
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24688.patch

# Summary of changes

AMF's `GetCaps()`/`GetInputCaps()` report which surface formats an encoder 
accepts and, per format, whether it's consumed natively or routed through AMF's 
internal colour-space converter first. Feeding it a merely-accepted 
(non-native) format has been observed to silently produce corrupted output 
instead of a clean failure - seen with BGRA/D3D11 input on an AMD Radeon 860M, 
where AMFCaps lists BGRA as accepted but non-native (`native=0`); only NV12  
comes back `native=1` there.

This checks `AMFCaps`/`GetInputCaps` right after `CreateComponent` and rejects 
non-native formats with a clear error instead of proceeding blind. No-op if the 
capability query itself isn't available (older drivers).


>From 1e859b0157a020cdec01aa5d18da182bb0ee9111 Mon Sep 17 00:00:00 2001
From: Arthur Vasseur <[email protected]>
Date: Thu, 24 Sep 2026 22:06:53 +0200
Subject: [PATCH] avcodec/amfenc: verify the encoder input format against
 AMFCaps

Feeding the encoder a surface format it doesn't consume natively can
silently produce corrupted output instead of a clean failure - seen
with BGRA/D3D11 on an AMD Radeon 860M, where AMFCaps lists BGRA as
accepted but non-native (native=0); only NV12 comes back native=1
there. Check AMFCaps/GetInputCaps after CreateComponent and reject
non-native formats. No-op if the capability query itself isn't
available (older drivers).

Signed-off-by: Arthur Vasseur <[email protected]>
---
 libavcodec/amfenc.c | 65 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 3ffca750d8..42585f1766 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -75,6 +75,67 @@ const enum AVPixelFormat ff_amf_pix_fmts[] = {
 
 static int64_t next_encoder_index = 0;
 
+static int amf_check_input_format(AVCodecContext *avctx, enum AVPixelFormat 
pix_fmt)
+{
+    AMFEncoderContext *ctx = avctx->priv_data;
+    AMFCaps    *encoder_caps = NULL;
+    AMFIOCaps  *input_caps = NULL;
+    AMF_RESULT  res;
+    amf_int32   nb_formats;
+    amf_int32   i;
+    int         ret = 0;
+
+    res = ctx->encoder->pVtbl->GetCaps(ctx->encoder, &encoder_caps);
+    if (res != AMF_OK || !encoder_caps) {
+        av_log(ctx, AV_LOG_WARNING,
+               "AMF: GetCaps failed (res=%d, encoder_caps=%p), skipping input 
format check\n",
+               res, encoder_caps);
+        return 0;
+    }
+
+    res = encoder_caps->pVtbl->GetInputCaps(encoder_caps, &input_caps);
+    if (res != AMF_OK || !input_caps) {
+        av_log(ctx, AV_LOG_WARNING,
+               "AMF: GetInputCaps failed (res=%d, input_caps=%p), skipping 
input format check\n",
+               res, input_caps);
+        encoder_caps->pVtbl->Release(encoder_caps);
+        return 0;
+    }
+
+    nb_formats = input_caps->pVtbl->GetNumOfFormats(input_caps);
+    av_log(ctx, AV_LOG_DEBUG,
+           "AMF input caps: %d format(s) reported, looking for 
AMF_SURFACE_FORMAT %d (pix_fmt %s)\n",
+           nb_formats, ctx->format, av_get_pix_fmt_name(pix_fmt));
+
+    for (i = 0; i < nb_formats; i++) {
+        AMF_SURFACE_FORMAT format = AMF_SURFACE_UNKNOWN;
+        amf_bool            native = 0;
+        AMF_RESULT          at_res = 
input_caps->pVtbl->GetFormatAt(input_caps, i, &format, &native);
+
+        av_log(ctx, AV_LOG_DEBUG,
+               "AMF input caps:   [%d] GetFormatAt res=%d format=%d 
native=%d\n",
+               i, at_res, format, native);
+
+        if (at_res == AMF_OK && format == ctx->format && native)
+            goto done;
+    }
+
+    if (nb_formats > 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Surface format %s is not natively supported as encoder input 
by "
+               "this GPU/driver (only non-native, internally-converted input 
is "
+               "available, if any); feed it a natively-supported format (e.g. "
+               "nv12) instead.\n",
+               av_get_pix_fmt_name(pix_fmt));
+        ret = AVERROR(EINVAL);
+    }
+
+done:
+    input_caps->pVtbl->Release(input_caps);
+    encoder_caps->pVtbl->Release(encoder_caps);
+    return ret;
+}
+
 static int amf_init_encoder(AVCodecContext *avctx)
 {
     AMFEncoderContext  *ctx = avctx->priv_data;
@@ -84,6 +145,7 @@ static int amf_init_encoder(AVCodecContext *avctx)
     AVHWDeviceContext  *hw_device_ctx = 
(AVHWDeviceContext*)ctx->device_ctx_ref->data;
     AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext 
*)hw_device_ctx->hwctx;
     int                 alloc_size;
+    int                 ret;
     wchar_t             name[512];
 
 
@@ -130,6 +192,9 @@ static int amf_init_encoder(AVCodecContext *avctx)
     res = 
amf_device_ctx->factory->pVtbl->CreateComponent(amf_device_ctx->factory, 
amf_device_ctx->context, codec_id, &ctx->encoder);
     AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_ENCODER_NOT_FOUND, 
"CreateComponent(%ls) failed with error %d\n", codec_id, res);
 
+    if ((ret = amf_check_input_format(avctx, pix_fmt)) < 0)
+        return ret;
+
     ctx->submitted_frame = 0;
     ctx->encoded_frame = 0;
     ctx->eof = 0;
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to