PR #25910 opened by Kacper Michajłow (kasper93)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25910
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25910.patch


From 61c648d06962e8df7a812416d2da9fc74f3695a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 11:49:37 +0100
Subject: [PATCH 1/4] avcodec/dxva2_h264: write RefPicList positions directly
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Without the Intel ClearVideo workaround the slice RefPicList entries are
positions in RefFrameList, or 127 when get_refpic_index() does not find
the reference. Write them into bPicEntry directly and leave
fill_picture_entry() for surface indices.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavcodec/dxva2_h264.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/libavcodec/dxva2_h264.c b/libavcodec/dxva2_h264.c
index dd9dccbf8c0..9184ba88891 100644
--- a/libavcodec/dxva2_h264.c
+++ b/libavcodec/dxva2_h264.c
@@ -253,13 +253,12 @@ static void fill_slice_long(AVCodecContext *avctx, 
DXVA_Slice_H264_Long *slice,
             if (list < sl->list_count && i < sl->ref_count[list]) {
                 const H264Picture *r = sl->ref_list[list][i].parent;
                 unsigned plane;
-                unsigned index;
+                unsigned index = ff_dxva2_get_surface_index(avctx, ctx, r->f, 
0);
+                unsigned flag = sl->ref_list[list][i].reference == 
PICT_BOTTOM_FIELD;
                 if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & 
FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO)
-                    index = ff_dxva2_get_surface_index(avctx, ctx, r->f, 0);
+                    fill_picture_entry(&slice->RefPicList[list][i], index, 
flag);
                 else
-                    index = get_refpic_index(pp, 
ff_dxva2_get_surface_index(avctx, ctx, r->f, 0));
-                fill_picture_entry(&slice->RefPicList[list][i], index,
-                                   sl->ref_list[list][i].reference == 
PICT_BOTTOM_FIELD);
+                    slice->RefPicList[list][i].bPicEntry = 
get_refpic_index(pp, index) | (flag << 7);
                 for (plane = 0; plane < 3; plane++) {
                     int w, o;
                     if (plane == 0 && sl->pwt.luma_weight_flag[list]) {
-- 
2.54.0


From f0c271f756a42054a6aee1c26b760b43a9f75320 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 11:50:28 +0100
Subject: [PATCH 2/4] avcodec/dxva2_{h264,hevc,vp9}: reject the invalid surface
 index 127
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The DXVA specifications of H.264, HEVC and VP8/VP9 reserve the
Index7Bits value 127 for entries without a valid surface, but the
asserts in fill_picture_entry() accept it. VP9 decoding into surface 127
reset the driver on AMD RX 9070 XT (32.0.32015.12012), and H.264 and
HEVC decoded wrong.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavcodec/dxva2_h264.c | 2 +-
 libavcodec/dxva2_hevc.c | 2 +-
 libavcodec/dxva2_vp9.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavcodec/dxva2_h264.c b/libavcodec/dxva2_h264.c
index 9184ba88891..4bf5f560d95 100644
--- a/libavcodec/dxva2_h264.c
+++ b/libavcodec/dxva2_h264.c
@@ -44,7 +44,7 @@ struct dxva2_picture_context {
 static void fill_picture_entry(DXVA_PicEntry_H264 *pic,
                                unsigned index, unsigned flag)
 {
-    av_assert0((index&0x7f) == index && (flag&0x01) == flag);
+    av_assert0(index < 0x7f && (flag&0x01) == flag);
     pic->bPicEntry = index | (flag << 7);
 }
 
diff --git a/libavcodec/dxva2_hevc.c b/libavcodec/dxva2_hevc.c
index 85698dfcce2..3b5b9833ae7 100644
--- a/libavcodec/dxva2_hevc.c
+++ b/libavcodec/dxva2_hevc.c
@@ -43,7 +43,7 @@ struct hevc_dxva2_picture_context {
 static void fill_picture_entry(DXVA_PicEntry_HEVC *pic,
                                unsigned index, unsigned flag)
 {
-    av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
+    av_assert0(index < 0x7f && (flag & 0x01) == flag);
     pic->bPicEntry = index | (flag << 7);
 }
 
diff --git a/libavcodec/dxva2_vp9.c b/libavcodec/dxva2_vp9.c
index cffa95e1d13..a6aa521ed68 100644
--- a/libavcodec/dxva2_vp9.c
+++ b/libavcodec/dxva2_vp9.c
@@ -39,7 +39,7 @@ struct vp9_dxva2_picture_context {
 static void fill_picture_entry(DXVA_PicEntry_VPx *pic,
                                unsigned index, unsigned flag)
 {
-    av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
+    av_assert0(index < 0x7f && (flag & 0x01) == flag);
     pic->bPicEntry = index | (flag << 7);
 }
 
-- 
2.54.0


From fb61f369ecaafbba0a194fa09ef7206590ac2e64 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 11:25:02 +0100
Subject: [PATCH 3/4] avcodec/dxva2: limit the surface pool to 127 surfaces
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The DXVA specifications of H.264, HEVC and VP8/VP9 index surfaces with
7 bits and reserve 127 as the invalid index. Larger pools fail the
av_assert0() in fill_picture_entry().

The DXVA AV1 specification uses 8 bit indices with 255 as the invalid
one, but on AMD RX 9070 XT (32.0.32015.12012) AV1 inter frames decoded
wrong whenever a texture index of 127 or higher was passed. The same
array slices decoded correctly with the indices shifted below 127. Use
127 for every codec.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavcodec/dxva2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/dxva2.c b/libavcodec/dxva2.c
index 6919d52680a..5a23cfc4ffd 100644
--- a/libavcodec/dxva2.c
+++ b/libavcodec/dxva2.c
@@ -692,6 +692,11 @@ int ff_dxva2_common_frame_params(AVCodecContext *avctx,
     return 0;
 }
 
+/* The H.264, HEVC and VP9 picture parameters index surfaces with 7 bits and
+ * reserve 127 as the invalid index. The AV1 specification uses 8 bits with 255
+ * as the invalid index, but indices >= 127 don't work on real hardware. */
+#define DXVA_MAX_SURFACES 127
+
 int ff_dxva2_decode_init(AVCodecContext *avctx)
 {
     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
@@ -720,6 +725,13 @@ int ff_dxva2_decode_init(AVCodecContext *avctx)
         goto fail;
     }
 
+    if (frames_ctx->initial_pool_size > DXVA_MAX_SURFACES) {
+        av_log(avctx, AV_LOG_ERROR, "The pool has %d surfaces, DXVA can 
address at most %d\n",
+               frames_ctx->initial_pool_size, DXVA_MAX_SURFACES);
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
 #if CONFIG_D3D11VA
     if (sctx->pix_fmt == AV_PIX_FMT_D3D11) {
         AVD3D11VADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
-- 
2.54.0


From 5dfebb34a1653d99607b0024ceeccccb10959716 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= <[email protected]>
Date: Tue, 10 Nov 2026 12:04:31 +0100
Subject: [PATCH 4/4] avcodec/dxva2_h264: mark missing RefPicList references
 not available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

For a RefPicList entry with Index7Bits 127 the DXVA H.264 specification
reads AssociatedFlag as a non-existing (1) or a not available (0)
picture, and references to a not available picture decode from mid-gray
samples. get_refpic_index() returns 127 for a reference that
RefFrameList does not hold, such as the one h264_refs.c substitutes for
a missing reference, and the bottom field flag turned it into a
non-existing picture in field pictures. Always use 0 for such entries.

Signed-off-by: Kacper Michajłow <[email protected]>
---
 libavcodec/dxva2_h264.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libavcodec/dxva2_h264.c b/libavcodec/dxva2_h264.c
index 4bf5f560d95..daed437816a 100644
--- a/libavcodec/dxva2_h264.c
+++ b/libavcodec/dxva2_h264.c
@@ -255,10 +255,12 @@ static void fill_slice_long(AVCodecContext *avctx, 
DXVA_Slice_H264_Long *slice,
                 unsigned plane;
                 unsigned index = ff_dxva2_get_surface_index(avctx, ctx, r->f, 
0);
                 unsigned flag = sl->ref_list[list][i].reference == 
PICT_BOTTOM_FIELD;
-                if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & 
FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO)
+                if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & 
FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO) {
                     fill_picture_entry(&slice->RefPicList[list][i], index, 
flag);
-                else
-                    slice->RefPicList[list][i].bPicEntry = 
get_refpic_index(pp, index) | (flag << 7);
+                } else {
+                    unsigned pos = get_refpic_index(pp, index);
+                    slice->RefPicList[list][i].bPicEntry = pos == 0x7f ? 0x7f 
: pos | (flag << 7);
+                }
                 for (plane = 0; plane < 3; plane++) {
                     int w, o;
                     if (plane == 0 && sl->pwt.luma_weight_flag[list]) {
-- 
2.54.0

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

Reply via email to