Commit cd38f85437 for ffmpeg

commit cd38f85437a07c4697b7d4fe1b9e1189715a1cb5
Author: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date:   Thu Dec 3 07:39:44 2026 +0100

    avcodec/proresdec: Fix permuting qmat arrays

    When using scantable scan and IDCT permutation perm,
    the decoder (in decode_ac_coeffs()) puts the ith coefficient
    into the array element with index k:=perm[scan[i]], whereas
    the C reference puts it into the one with index j:=scan[i].
    The entry qmat[j] from the original qmat array (not permutated
    according to perm) belongs to this entry; for the permutated
    qmat entries qmat' one needs to have
    qmat'[perm[j]] = qmat'[k] = qmat[j].
    Yet qmat' is currently initialized via
    qmat'[j] = qmat[perm[j]]. This doesn't matter for self-inverse
    permutations like the ones currently used, but it matters
    in general. So fix it.

    Also change undoing the permutation in vulkan_prores.c accordingly.

    Reviewed-by: Marcos Ashton Iglesias <marcosashiglesias@gmail.com>
    Reviewed-by: Kyle Swanson <k@ylo.ph>
    Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>

diff --git a/libavcodec/proresdec.c b/libavcodec/proresdec.c
index 13eea7b5dd..ceb445f7be 100644
--- a/libavcodec/proresdec.c
+++ b/libavcodec/proresdec.c
@@ -199,6 +199,13 @@ static int describes_colour(int value, const char *name)
     return value != 0 && value != 2 && value != 3 && name;
 }

+static void permute_qmat(uint8_t *perm_qmat, const uint8_t *qmat,
+                         const uint8_t *perm)
+{
+    for (int i = 0; i < 64; ++i)
+        perm_qmat[perm[i]] = qmat[i];
+}
+
 static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
                                const int data_size, AVCodecContext *avctx)
 {
@@ -313,7 +320,7 @@ static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
             return AVERROR_INVALIDDATA;
         }
-        ff_permute_scantable(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);
+        permute_qmat(ctx->qmat_luma, ptr, ctx->prodsp.idct_permutation);
         ptr += 64;
     } else {
         memset(ctx->qmat_luma, 4, 64);
@@ -324,7 +331,7 @@ static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
             av_log(avctx, AV_LOG_ERROR, "Header truncated\n");
             return AVERROR_INVALIDDATA;
         }
-        ff_permute_scantable(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);
+        permute_qmat(ctx->qmat_chroma, ptr, ctx->prodsp.idct_permutation);
     } else {
         memcpy(ctx->qmat_chroma, ctx->qmat_luma, 64);
     }
diff --git a/libavcodec/vulkan_prores.c b/libavcodec/vulkan_prores.c
index 1ccbb5288b..a03598f943 100644
--- a/libavcodec/vulkan_prores.c
+++ b/libavcodec/vulkan_prores.c
@@ -195,15 +195,15 @@ static int vk_prores_end_frame(AVCodecContext *avctx)
     };

     /* The decoder permutes the quantization matrices to match the layout
-     * expected by its IDCT (transposed on x86), undo it here. */
+     * expected by its IDCT (pre-permuted on x86), undo it here. */
     {
         uint8_t *qmat_luma   = metadata->mapped_mem + pp->qmat_off;
         uint8_t *qmat_chroma = qmat_luma + sizeof(pr->qmat_luma);
         const uint8_t *perm  = pr->prodsp.idct_permutation;

         for (i = 0; i < 64; i++) {
-            qmat_luma  [perm[i]] = pr->qmat_luma  [i];
-            qmat_chroma[perm[i]] = pr->qmat_chroma[i];
+            qmat_luma  [i] = pr->qmat_luma  [perm[i]];
+            qmat_chroma[i] = pr->qmat_chroma[perm[i]];
         }
     }