From 77c85aaf2e80cd759cb146efb076b0bc16bb6435 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 13 Mar 2025 21:43:37 +0100
Subject: [PATCH 09/17] avcodec/hqxvlc: Make dc9, dc10 VLC tables static

It allows to share them between frame threads.
dc11 can unfortunately not be made static without increasing
LOCALBUF_ELEMS in vlc.c.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/hqx.c    | 17 +++++++----------
 libavcodec/hqxvlc.h | 18 +++++++++++-------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c
index aab28a2156..e5a727a609 100644
--- a/libavcodec/hqx.c
+++ b/libavcodec/hqx.c
@@ -70,7 +70,7 @@ typedef struct HQXContext {
 
     const VLCElem *dc_vlc;
 
-    VLC dc_vlcs[3];
+    VLC dc11_vlc;
 } HQXContext;
 
 #define HQX_HEADER_SIZE 59
@@ -481,7 +481,7 @@ static int hqx_decode_frame(AVCodecContext *avctx, AVFrame *frame,
         av_log(avctx, AV_LOG_ERROR, "Invalid DC precision 8.\n");
         return AVERROR_INVALIDDATA;
     }
-    ctx->dc_vlc = ctx->dc_vlcs[dcb_code - 1].table;
+    ctx->dc_vlc = dcb_code == 3 ? ctx->dc11_vlc.table : dc_vlc[dcb_code - 1];
     ctx->dcb    = dcb_code + 8;
     ret = av_image_check_size(ctx->width, ctx->height, 0, avctx);
     if (ret < 0) {
@@ -539,12 +539,9 @@ static int hqx_decode_frame(AVCodecContext *avctx, AVFrame *frame,
 
 static av_cold int hqx_decode_close(AVCodecContext *avctx)
 {
-    int i;
     HQXContext *ctx = avctx->priv_data;
 
-    for (i = 0; i < 3; i++) {
-        ff_vlc_free(&ctx->dc_vlcs[i]);
-    }
+    ff_vlc_free(&ctx->dc11_vlc);
 
     return 0;
 }
@@ -553,11 +550,11 @@ static av_cold int hqx_decode_init(AVCodecContext *avctx)
 {
     static AVOnce init_static_once = AV_ONCE_INIT;
     HQXContext *ctx = avctx->priv_data;
-    int ret;
+    int ret = vlc_init(&ctx->dc11_vlc, HQX_DC_VLC_BITS, FF_ARRAY_ELEMS(dc11_vlc_lens),
+                       dc11_vlc_lens, 1, 1, dc11_vlc_bits, 2, 2, 0);
 
-    INIT_DC_TABLE(0, dc9);
-    INIT_DC_TABLE(1, dc10);
-    INIT_DC_TABLE(2, dc11);
+    if (ret < 0)
+        return ret;
 
     ff_hqxdsp_init(&ctx->hqxdsp);
 
diff --git a/libavcodec/hqxvlc.h b/libavcodec/hqxvlc.h
index 4bdc3cd191..28c55b20e0 100644
--- a/libavcodec/hqxvlc.h
+++ b/libavcodec/hqxvlc.h
@@ -1529,16 +1529,17 @@ static const uint8_t hqx_ac_lens[] = {
 
 static const uint16_t hqx_ac_nb_elems[] = { 815, 907, 512, 354, 257, 194 };
 
-static VLCElem cbp_vlc[(1 << HQX_CBP_VLC_BITS) + 15630 /* RL_VLC_ELEMS for hqx_ac */];
+static VLCElem cbp_vlc[(1 << HQX_CBP_VLC_BITS) + 896 /* dc9 */ + 1344 /* dc10 */
+                       + 15630 /* RL_VLC_ELEMS for hqx_ac */];
+
+static const VLCElem *dc_vlc[2];
 
 #define INIT_DC_TABLE(idx, name)                                              \
     do {                                                                      \
-        ret = vlc_init(&ctx->dc_vlcs[idx], HQX_DC_VLC_BITS,                   \
-                       FF_ARRAY_ELEMS(name ## _vlc_lens),                     \
-                       name ## _vlc_lens, 1, 1,                               \
-                       name ## _vlc_bits, 2, 2, 0);                           \
-        if (ret < 0)                                                          \
-            return ret;                                                       \
+         dc_vlc[idx] = ff_vlc_init_tables(&state, HQX_DC_VLC_BITS,            \
+                                          FF_ARRAY_ELEMS(name ## _vlc_lens),  \
+                                          name ## _vlc_lens, 1, 1,            \
+                                          name ## _vlc_bits, 2, 2, 0);        \
     } while (0)
 
 static av_cold av_unused void hqx_init_static(void)
@@ -1550,6 +1551,9 @@ static av_cold av_unused void hqx_init_static(void)
     ff_vlc_init_tables(&state, HQX_CBP_VLC_BITS, FF_ARRAY_ELEMS(cbp_vlc_lens),
                        cbp_vlc_lens, 1, 1, cbp_vlc_bits, 1, 1, 0);
 
+    INIT_DC_TABLE(0, dc9);
+    INIT_DC_TABLE(1, dc10);
+
     for (int i = 0; i < NUM_HQX_AC; ++i) {
         RL_VLC_ELEM *lut = state.table;
         unsigned nb_codes = state.size;
-- 
2.45.2