From e5280f836450aa63caf33fea913bf7fcf5cba404 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 13 Mar 2025 21:04:39 +0100
Subject: [PATCH 07/17] avcodec/hqxvlc: Make cbp_vlc static

This is trivial as well as beneficial because frame threads
now use the same table, saving cache/memory.

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

diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c
index 7ff689407d..e73a023c29 100644
--- a/libavcodec/hqx.c
+++ b/libavcodec/hqx.c
@@ -68,7 +68,6 @@ typedef struct HQXContext {
     unsigned int data_size;
     uint32_t slice_off[17];
 
-    VLC cbp_vlc;
     VLC dc_vlc[3];
 } HQXContext;
 
@@ -224,12 +223,12 @@ static int hqx_decode_422a(HQXContext *ctx, int slice_no, int x, int y)
     int i, ret;
     int cbp;
 
-    cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1);
-
     for (i = 0; i < 12; i++)
         memset(slice->block[i], 0, sizeof(**slice->block) * 64);
     for (i = 0; i < 12; i++)
         slice->block[i][0] = -0x800;
+
+    cbp = get_vlc2(gb, cbp_vlc, HQX_CBP_VLC_BITS, 1);
     if (cbp) {
         if (ctx->interlaced)
             flag = get_bits1(gb);
@@ -310,12 +309,12 @@ static int hqx_decode_444a(HQXContext *ctx, int slice_no, int x, int y)
     int i, ret;
     int cbp;
 
-    cbp = get_vlc2(gb, ctx->cbp_vlc.table, HQX_CBP_VLC_BITS, 1);
-
     for (i = 0; i < 16; i++)
         memset(slice->block[i], 0, sizeof(**slice->block) * 64);
     for (i = 0; i < 16; i++)
         slice->block[i][0] = -0x800;
+
+    cbp = get_vlc2(gb, cbp_vlc, HQX_CBP_VLC_BITS, 1);
     if (cbp) {
         if (ctx->interlaced)
             flag = get_bits1(gb);
@@ -543,7 +542,6 @@ static av_cold int hqx_decode_close(AVCodecContext *avctx)
     int i;
     HQXContext *ctx = avctx->priv_data;
 
-    ff_vlc_free(&ctx->cbp_vlc);
     for (i = 0; i < 3; i++) {
         ff_vlc_free(&ctx->dc_vlc[i]);
     }
@@ -555,10 +553,7 @@ static av_cold int hqx_decode_init(AVCodecContext *avctx)
 {
     static AVOnce init_static_once = AV_ONCE_INIT;
     HQXContext *ctx = avctx->priv_data;
-    int ret = vlc_init(&ctx->cbp_vlc, HQX_CBP_VLC_BITS, FF_ARRAY_ELEMS(cbp_vlc_lens),
-                       cbp_vlc_lens, 1, 1, cbp_vlc_bits, 1, 1, 0);
-    if (ret < 0)
-        return ret;
+    int ret;
 
     INIT_DC_TABLE(0, dc9);
     INIT_DC_TABLE(1, dc10);
diff --git a/libavcodec/hqxvlc.h b/libavcodec/hqxvlc.h
index 6ac2bab01e..aa7fbcdc59 100644
--- a/libavcodec/hqxvlc.h
+++ b/libavcodec/hqxvlc.h
@@ -1529,7 +1529,7 @@ static const uint8_t hqx_ac_lens[] = {
 
 static const uint16_t hqx_ac_nb_elems[] = { 815, 907, 512, 354, 257, 194 };
 
-static RL_VLC_ELEM hqx_ac_rl_vlc[15630];
+static VLCElem cbp_vlc[(1 << HQX_CBP_VLC_BITS) + 15630 /* RL_VLC_ELEMS for hqx_ac */];
 
 #define INIT_DC_TABLE(idx, name)                                              \
     do {                                                                      \
@@ -1543,10 +1543,13 @@ static RL_VLC_ELEM hqx_ac_rl_vlc[15630];
 
 static av_cold av_unused void hqx_init_static(void)
 {
-    VLCInitState state = VLC_INIT_STATE(hqx_ac_rl_vlc);
+    VLCInitState state = VLC_INIT_STATE(cbp_vlc);
     const uint8_t *lens = hqx_ac_lens;
     const int16_t *run_level = hqx_ac_run_level;
 
+    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);
+
     for (int i = 0; i < NUM_HQX_AC; ++i) {
         RL_VLC_ELEM *lut = state.table;
         unsigned nb_codes = state.size;
-- 
2.45.2