From b22c57983d74453c0ea94d8214f8c4eb4273374b Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Thu, 13 Mar 2025 20:42:30 +0100
Subject: [PATCH 06/17] avcodec/hqx: Include hqxvlc directly

This avoids having to expose HQXContext in a header
and allows to make several symbols static.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/Makefile               |  2 +-
 libavcodec/hqx.c                  | 49 ++++++++++++++++--
 libavcodec/hqx.h                  | 82 -------------------------------
 libavcodec/{hqxvlc.c => hqxvlc.h} | 54 +++++++++++---------
 4 files changed, 79 insertions(+), 108 deletions(-)
 delete mode 100644 libavcodec/hqx.h
 rename libavcodec/{hqxvlc.c => hqxvlc.h} (99%)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3c7043a617..584938ac85 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -467,7 +467,7 @@ OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER)    += v4l2_m2m_enc.o
 OBJS-$(CONFIG_HEVC_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
 OBJS-$(CONFIG_HNM4_VIDEO_DECODER)      += hnm4video.o
 OBJS-$(CONFIG_HQ_HQA_DECODER)          += hq_hqa.o hq_hqadsp.o canopus.o
-OBJS-$(CONFIG_HQX_DECODER)             += hqx.o hqxvlc.o hqxdsp.o canopus.o
+OBJS-$(CONFIG_HQX_DECODER)             += hqx.o hqxdsp.o canopus.o
 OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o huffyuvdec.o
 OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o huffyuvenc.o
 OBJS-$(CONFIG_HYMT_DECODER)            += huffyuv.o huffyuvdec.o
diff --git a/libavcodec/hqx.c b/libavcodec/hqx.c
index 32acca79ac..7ff689407d 100644
--- a/libavcodec/hqx.c
+++ b/libavcodec/hqx.c
@@ -20,17 +20,21 @@
 
 #include <inttypes.h>
 
+#include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem_internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/thread.h"
 
 #include "avcodec.h"
 #include "canopus.h"
 #include "codec_internal.h"
 #include "get_bits.h"
 #include "thread.h"
+#include "vlc.h"
 
-#include "hqx.h"
 #include "hqxdsp.h"
+#include "hqxvlc.h"
 
 /* HQX has four modes - 422, 444, 422alpha and 444alpha - all 12-bit */
 enum HQXFormat {
@@ -40,6 +44,34 @@ enum HQXFormat {
     HQX_444A,
 };
 
+struct HQXContext;
+
+typedef int (*mb_decode_func)(struct HQXContext *ctx,
+                              int slice_no, int x, int y);
+
+typedef struct HQXSlice {
+    GetBitContext gb;
+    DECLARE_ALIGNED(16, int16_t, block)[16][64];
+} HQXSlice;
+
+typedef struct HQXContext {
+    HQXDSPContext hqxdsp;
+    HQXSlice slice[16];
+
+    AVFrame *pic;
+    mb_decode_func decode_func;
+
+    int format, dcb, width, height;
+    int interlaced;
+
+    const uint8_t *src;
+    unsigned int data_size;
+    uint32_t slice_off[17];
+
+    VLC cbp_vlc;
+    VLC dc_vlc[3];
+} HQXContext;
+
 #define HQX_HEADER_SIZE 59
 
 /* macroblock selects a group of 4 possible quants and
@@ -138,7 +170,7 @@ static int decode_block(GetBitContext *gb, VLC *vlc,
         ac_idx = HQX_AC_Q0;
 
     do {
-        hqx_get_ac(gb, &ff_hqx_ac[ac_idx], &run, &lev);
+        hqx_get_ac(gb, &hqx_ac[ac_idx], &run, &lev);
         pos += run;
         if (pos > 63)
             break;
@@ -521,11 +553,22 @@ static av_cold int hqx_decode_close(AVCodecContext *avctx)
 
 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;
+
+    INIT_DC_TABLE(0, dc9);
+    INIT_DC_TABLE(1, dc10);
+    INIT_DC_TABLE(2, dc11);
 
     ff_hqxdsp_init(&ctx->hqxdsp);
 
-    return ff_hqx_init_vlcs(ctx);
+    ff_thread_once(&init_static_once, hqx_init_static);
+
+    return 0;
 }
 
 const FFCodec ff_hqx_decoder = {
diff --git a/libavcodec/hqx.h b/libavcodec/hqx.h
deleted file mode 100644
index 4f313a1dc3..0000000000
--- a/libavcodec/hqx.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Canopus HQX decoder
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef AVCODEC_HQX_H
-#define AVCODEC_HQX_H
-
-#include <stdint.h>
-
-#include "libavutil/frame.h"
-#include "libavutil/mem_internal.h"
-
-#include "get_bits.h"
-#include "hqxdsp.h"
-
-enum HQXACMode {
-    HQX_AC_Q0 = 0,
-    HQX_AC_Q8,
-    HQX_AC_Q16,
-    HQX_AC_Q32,
-    HQX_AC_Q64,
-    HQX_AC_Q128,
-    NUM_HQX_AC
-};
-
-typedef struct HQXAC {
-    int bits;
-    const RL_VLC_ELEM *lut;
-} HQXAC;
-
-struct HQXContext;
-
-typedef int (*mb_decode_func)(struct HQXContext *ctx,
-                              int slice_no, int x, int y);
-
-typedef struct HQXSlice {
-    GetBitContext gb;
-    DECLARE_ALIGNED(16, int16_t, block)[16][64];
-} HQXSlice;
-
-typedef struct HQXContext {
-    HQXDSPContext hqxdsp;
-    HQXSlice slice[16];
-
-    AVFrame *pic;
-    mb_decode_func decode_func;
-
-    int format, dcb, width, height;
-    int interlaced;
-
-    const uint8_t *src;
-    unsigned int data_size;
-    uint32_t slice_off[17];
-
-    VLC cbp_vlc;
-    VLC dc_vlc[3];
-} HQXContext;
-
-#define HQX_CBP_VLC_BITS 5
-#define HQX_DC_VLC_BITS 9
-
-extern HQXAC ff_hqx_ac[NUM_HQX_AC];
-
-int ff_hqx_init_vlcs(HQXContext *ctx);
-
-#endif /* AVCODEC_HQX_H */
diff --git a/libavcodec/hqxvlc.c b/libavcodec/hqxvlc.h
similarity index 99%
rename from libavcodec/hqxvlc.c
rename to libavcodec/hqxvlc.h
index 63ca5cc33c..6ac2bab01e 100644
--- a/libavcodec/hqxvlc.c
+++ b/libavcodec/hqxvlc.h
@@ -18,8 +18,33 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "hqx.h"
-#include "libavutil/thread.h"
+#ifndef AVCODEC_HQXVLC_H
+#define AVCODEC_HQXVLC_H
+
+#include <stdint.h>
+
+#include "vlc.h"
+
+#include "libavutil/attributes.h"
+#include "libavutil/macros.h"
+
+#define HQX_CBP_VLC_BITS 5
+#define HQX_DC_VLC_BITS 9
+
+enum HQXACMode {
+    HQX_AC_Q0 = 0,
+    HQX_AC_Q8,
+    HQX_AC_Q16,
+    HQX_AC_Q32,
+    HQX_AC_Q64,
+    HQX_AC_Q128,
+    NUM_HQX_AC
+};
+
+typedef struct HQXAC {
+    int bits;
+    const RL_VLC_ELEM *lut;
+} HQXAC;
 
 static const uint8_t cbp_vlc_bits[16] = {
     0x04, 0x1C, 0x1D, 0x09, 0x1E, 0x0B, 0x1B, 0x08,
@@ -721,7 +746,7 @@ static const uint8_t dc11_vlc_lens[2048] = {
 };
 
 
-HQXAC ff_hqx_ac[NUM_HQX_AC] = {
+static HQXAC hqx_ac[NUM_HQX_AC] = {
     { 10 }, { 11 }, { 11 }, { 11 }, { 12 }, { 11 },
 };
 
@@ -1516,7 +1541,7 @@ static RL_VLC_ELEM hqx_ac_rl_vlc[15630];
             return ret;                                                       \
     } while (0)
 
-static av_cold void hqx_init_static(void)
+static av_cold av_unused void hqx_init_static(void)
 {
     VLCInitState state = VLC_INIT_STATE(hqx_ac_rl_vlc);
     const uint8_t *lens = hqx_ac_lens;
@@ -1526,8 +1551,8 @@ static av_cold void hqx_init_static(void)
         RL_VLC_ELEM *lut = state.table;
         unsigned nb_codes = state.size;
 
-        ff_hqx_ac[i].lut =
-            ff_vlc_init_tables_from_lengths(&state, ff_hqx_ac[i].bits,
+        hqx_ac[i].lut =
+            ff_vlc_init_tables_from_lengths(&state, hqx_ac[i].bits,
                                             hqx_ac_nb_elems[i], lens, 1,
                                             run_level, 2, 2, 0, 0);
 
@@ -1554,19 +1579,4 @@ static av_cold void hqx_init_static(void)
     }
 }
 
-av_cold int ff_hqx_init_vlcs(HQXContext *ctx)
-{
-    static AVOnce init_static_once = AV_ONCE_INIT;
-    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;
-
-    INIT_DC_TABLE(0, dc9);
-    INIT_DC_TABLE(1, dc10);
-    INIT_DC_TABLE(2, dc11);
-
-    ff_thread_once(&init_static_once, hqx_init_static);
-
-    return 0;
-}
+#endif /* AVCODEC_HQXVLC_H*/
-- 
2.45.2