diff mbox series

[FFmpeg-devel,43/61] avcodec/aacdec_template: Deduplicate VLCs

Message ID GV1P250MB073791606C5E1DF0653084C98FC3A@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 3b080fe7af04587d4f270540740bea1f92af02ed
Headers show
Series [FFmpeg-devel,01/61] avcodec/vlc: Add functions to init static VLCElem[] without VLC | expand

Commit Message

Andreas Rheinhardt Sept. 26, 2023, 10:17 p.m. UTC
They (as well as their init code) are currently duplicated
for the floating- and fixed-point decoders.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/aacdec_common.c   | 43 ++++++++++++++++++++++++++++++++++++
 libavcodec/aacdec_template.c | 42 ++++++-----------------------------
 libavcodec/aacdectab.h       | 10 +++++++++
 3 files changed, 60 insertions(+), 35 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/aacdec_common.c b/libavcodec/aacdec_common.c
index c75c6fadc6..45f1986258 100644
--- a/libavcodec/aacdec_common.c
+++ b/libavcodec/aacdec_common.c
@@ -27,6 +27,11 @@ 
 
 #include "aac.h"
 #include "aacdectab.h"
+#include "aactab.h"
+#include "vlc.h"
+
+#include "libavutil/attributes.h"
+#include "libavutil/thread.h"
 
 const int8_t ff_tags_per_config[16] = { 0, 1, 1, 2, 3, 3, 4, 5, 0, 0, 0, 5, 5, 16, 5, 0 };
 
@@ -117,3 +122,41 @@  const AVChannelLayout ff_aac_ch_layout[] = {
     AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK,
     { 0 },
 };
+
+VLC ff_vlc_scalefactors;
+VLC ff_vlc_spectral[11];
+
+static av_cold void aacdec_common_init(void)
+{
+    static VLCElem vlc_buf[304 + 270 + 550 + 300 + 328 +
+                           294 + 306 + 268 + 510 + 366 + 462];
+    for (unsigned i = 0, offset = 0; i < 11; i++) {
+        ff_vlc_spectral[i].table           = &vlc_buf[offset];
+        ff_vlc_spectral[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
+        ff_vlc_init_sparse(&ff_vlc_spectral[i], 8, ff_aac_spectral_sizes[i],
+                           ff_aac_spectral_bits[i],       sizeof(ff_aac_spectral_bits[i][0]),
+                                                          sizeof(ff_aac_spectral_bits[i][0]),
+                           ff_aac_spectral_codes[i],      sizeof(ff_aac_spectral_codes[i][0]),
+                                                          sizeof(ff_aac_spectral_codes[i][0]),
+                           ff_aac_codebook_vector_idx[i], sizeof(ff_aac_codebook_vector_idx[i][0]),
+                                                          sizeof(ff_aac_codebook_vector_idx[i][0]),
+                 VLC_INIT_STATIC_OVERLONG);
+        offset += ff_vlc_spectral[i].table_size;
+    }
+
+    VLC_INIT_STATIC(&ff_vlc_scalefactors, 7,
+                    FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
+                    ff_aac_scalefactor_bits,
+                    sizeof(ff_aac_scalefactor_bits[0]),
+                    sizeof(ff_aac_scalefactor_bits[0]),
+                    ff_aac_scalefactor_code,
+                    sizeof(ff_aac_scalefactor_code[0]),
+                    sizeof(ff_aac_scalefactor_code[0]),
+                    352);
+}
+
+av_cold void ff_aacdec_common_init_once(void)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, aacdec_common_init);
+}
diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c
index 3fb9a01da5..27be1f896d 100644
--- a/libavcodec/aacdec_template.c
+++ b/libavcodec/aacdec_template.c
@@ -94,9 +94,6 @@ 
 #include "decode.h"
 #include "internal.h"
 
-static VLC vlc_scalefactors;
-static VLC vlc_spectral[11];
-
 static int output_configure(AACContext *ac,
                             uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
                             enum OCStatus oc_type, int get_new_frame);
@@ -1128,36 +1125,11 @@  static void aacdec_init(AACContext *ac);
 
 static av_cold void aac_static_table_init(void)
 {
-    static VLCElem vlc_buf[304 + 270 + 550 + 300 + 328 +
-                           294 + 306 + 268 + 510 + 366 + 462];
-    for (unsigned i = 0, offset = 0; i < 11; i++) {
-        vlc_spectral[i].table           = &vlc_buf[offset];
-        vlc_spectral[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
-        ff_vlc_init_sparse(&vlc_spectral[i], 8, ff_aac_spectral_sizes[i],
-                           ff_aac_spectral_bits[i],       sizeof(ff_aac_spectral_bits[i][0]),
-                                                          sizeof(ff_aac_spectral_bits[i][0]),
-                           ff_aac_spectral_codes[i],      sizeof(ff_aac_spectral_codes[i][0]),
-                                                          sizeof(ff_aac_spectral_codes[i][0]),
-                           ff_aac_codebook_vector_idx[i], sizeof(ff_aac_codebook_vector_idx[i][0]),
-                                                          sizeof(ff_aac_codebook_vector_idx[i][0]),
-                 VLC_INIT_STATIC_OVERLONG);
-        offset += vlc_spectral[i].table_size;
-    }
-
     AAC_RENAME(ff_aac_sbr_init)();
 
+    ff_aacdec_common_init_once();
     ff_aac_tableinit();
 
-    VLC_INIT_STATIC(&vlc_scalefactors, 7,
-                    FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
-                    ff_aac_scalefactor_bits,
-                    sizeof(ff_aac_scalefactor_bits[0]),
-                    sizeof(ff_aac_scalefactor_bits[0]),
-                    ff_aac_scalefactor_code,
-                    sizeof(ff_aac_scalefactor_code[0]),
-                    sizeof(ff_aac_scalefactor_code[0]),
-                    352);
-
     // window initialization
     AAC_RENAME(avpriv_kbd_window_init)(AAC_RENAME(aac_kbd_long_960), 4.0, 960);
     AAC_RENAME(avpriv_kbd_window_init)(AAC_RENAME(aac_kbd_short_120), 6.0, 120);
@@ -1526,7 +1498,7 @@  static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *
             } else if ((band_type[idx] == INTENSITY_BT) ||
                        (band_type[idx] == INTENSITY_BT2)) {
                 for (; i < run_end; i++, idx++) {
-                    offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
+                    offset[2] += get_vlc2(gb, ff_vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
                     clipped_offset = av_clip(offset[2], -155, 100);
                     if (offset[2] != clipped_offset) {
                         avpriv_request_sample(ac->avctx,
@@ -1545,7 +1517,7 @@  static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *
                     if (noise_flag-- > 0)
                         offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
                     else
-                        offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
+                        offset[1] += get_vlc2(gb, ff_vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
                     clipped_offset = av_clip(offset[1], -100, 155);
                     if (offset[1] != clipped_offset) {
                         avpriv_request_sample(ac->avctx,
@@ -1561,7 +1533,7 @@  static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *
                 }
             } else {
                 for (; i < run_end; i++, idx++) {
-                    offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
+                    offset[0] += get_vlc2(gb, ff_vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
                     if (offset[0] > 255U) {
                         av_log(ac->avctx, AV_LOG_ERROR,
                                "Scalefactor (%d) out of range.\n", offset[0]);
@@ -1734,7 +1706,7 @@  static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
 #if !USE_FIXED
                 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
 #endif /* !USE_FIXED */
-                const VLCElem *vlc_tab = vlc_spectral[cbt_m1].table;
+                const VLCElem *vlc_tab = ff_vlc_spectral[cbt_m1].table;
                 OPEN_READER(re, gb);
 
                 switch (cbt_m1 >> 1) {
@@ -2308,7 +2280,7 @@  static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
         INTFLOAT gain_cache = FIXR10(1.);
         if (c) {
             cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
-            gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
+            gain = cge ? get_vlc2(gb, ff_vlc_scalefactors.table, 7, 3) - 60: 0;
             gain_cache = GET_GAIN(scale, gain);
 #if USE_FIXED
             if ((abs(gain_cache)-1024) >> 3 > 30)
@@ -2322,7 +2294,7 @@  static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
                 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
                     if (sce->band_type[idx] != ZERO_BT) {
                         if (!cge) {
-                            int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
+                            int t = get_vlc2(gb, ff_vlc_scalefactors.table, 7, 3) - 60;
                             if (t) {
                                 int s = 1;
                                 t = gain += t;
diff --git a/libavcodec/aacdectab.h b/libavcodec/aacdectab.h
index db6fd0a04f..a009e9f46e 100644
--- a/libavcodec/aacdectab.h
+++ b/libavcodec/aacdectab.h
@@ -30,8 +30,17 @@ 
 
 #include <stdint.h>
 
+#include "vlc.h"
+
+#include "libavutil/attributes_internal.h"
 #include "libavutil/channel_layout.h"
 
+FF_VISIBILITY_PUSH_HIDDEN
+void ff_aacdec_common_init_once(void);
+
+extern VLC ff_vlc_scalefactors;
+extern VLC ff_vlc_spectral[11];
+
 extern const int8_t ff_tags_per_config[16];
 
 extern const uint8_t ff_aac_channel_layout_map[16][16][3];
@@ -43,5 +52,6 @@  extern const uint64_t ff_aac_channel_layout[];
 #endif
 
 extern const AVChannelLayout ff_aac_ch_layout[];
+FF_VISIBILITY_POP_HIDDEN
 
 #endif /* AVCODEC_AACDECTAB_H */