From beff76ed8d0fd4b2c5d42bc4bbd7367fc9f76c5e Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Wed, 9 Apr 2025 19:01:18 +0200
Subject: [PATCH 06/10] avcodec/hq_hqa: Include implicit +1 run in RL VLC table

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/hq_hqa.c     | 5 ++---
 libavcodec/hq_hqadata.h | 2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/libavcodec/hq_hqa.c b/libavcodec/hq_hqa.c
index 4f1e57279d..f67d6286b0 100644
--- a/libavcodec/hq_hqa.c
+++ b/libavcodec/hq_hqa.c
@@ -88,7 +88,7 @@ static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
     }
 
     OPEN_READER(re, gb);
-    for (int pos = 1;;) {
+    for (int pos = 0;;) {
         int level, run;
         UPDATE_CACHE(re, gb);
         GET_RL_VLC(level, run, re, gb, hq_ac_rvlc, 9, 2, 0);
@@ -101,7 +101,6 @@ static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
         if (pos >= 64)
             break;
         block[ff_zigzag_direct[pos]] = (int)(level * (unsigned)q[pos]) >> 12;
-        pos++;
     }
     CLOSE_READER(re, gb);
 
@@ -387,7 +386,7 @@ static av_cold void hq_init_static(void)
 
         if (len > 0) {
             level = hq_ac_syms[sym];
-            run   = hq_ac_skips[sym];
+            run   = hq_ac_skips[sym] + 1;
         } else if (len < 0) { // More bits needed
             run   = 0;
         } else { // Invalid code
diff --git a/libavcodec/hq_hqadata.h b/libavcodec/hq_hqadata.h
index 8948c3a64a..e723cefaac 100644
--- a/libavcodec/hq_hqadata.h
+++ b/libavcodec/hq_hqadata.h
@@ -1155,7 +1155,7 @@ static const uint8_t hq_quant_map[NUM_HQ_QUANTS][2][4] =
     { { QMAT3A, QMAT48, QMAT4C, QMAT4C }, { QMAT3B, QMAT49, QMAT4D, QMAT4D } },
 };
 
-#define HQ_AC_INVALID_RUN 128
+#define HQ_AC_INVALID_RUN 0
 
 static const uint8_t hq_ac_bits[NUM_HQ_AC_ENTRIES] = {
      3,  3,  4,  4,  4,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,
-- 
2.45.2