From 749b698708288909ca14b51b51621ac837046daf Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Fri, 23 May 2025 15:58:03 +0200 Subject: [PATCH 1/5] avcodec/ac3dec: Hardcode tables to save space The code to initialize the ungrouped bap mantissa tables (bap 3 or 5) takes more bytes of .text than the tables itself; they have therefore been hardcoded. For GCC (14, -O3, albeit in an av_cold function), the initialization code takes 99B each for the fixed and floating point decoders (the code is currently duplicated), whereas the hardcoded tables only take 96B. For Clang 19 it were 374B each (I don't now what Clang was doing there). Signed-off-by: Andreas Rheinhardt --- libavcodec/ac3dec.c | 16 ++-------------- libavcodec/ac3dec_data.c | 39 ++++++++++++++++++++++++++++++++++++++- libavcodec/ac3dec_data.h | 9 +++++++++ 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index 49b170c235..dbe7f0b57c 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -55,9 +55,7 @@ static uint8_t ungroup_3_in_7_bits_tab[128][3]; /** tables for ungrouping mantissas */ static int b1_mantissas[32][3]; static int b2_mantissas[128][3]; -static int b3_mantissas[8]; static int b4_mantissas[128][2]; -static int b5_mantissas[16]; /** * Quantization table: levels for symmetric. bits for asymmetric. @@ -155,16 +153,6 @@ static av_cold void ac3_tables_init(void) b4_mantissas[i][0] = symmetric_dequant(i / 11, 11); b4_mantissas[i][1] = symmetric_dequant(i % 11, 11); } - /* generate ungrouped mantissa tables - reference: Tables 7.21 and 7.23 */ - for (i = 0; i < 7; i++) { - /* bap=3 mantissas */ - b3_mantissas[i] = symmetric_dequant(i, 7); - } - for (i = 0; i < 15; i++) { - /* bap=5 mantissas */ - b5_mantissas[i] = symmetric_dequant(i, 15); - } #if (!USE_FIXED) /* generate dynamic range table @@ -595,7 +583,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma } break; case 3: - mantissa = b3_mantissas[get_bits(gbc, 3)]; + mantissa = ff_ac3_bap3_mantissas[get_bits(gbc, 3)]; break; case 4: if (m->b4) { @@ -609,7 +597,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma } break; case 5: - mantissa = b5_mantissas[get_bits(gbc, 4)]; + mantissa = ff_ac3_bap5_mantissas[get_bits(gbc, 4)]; break; default: /* 6 to 15 */ /* Shift mantissa and sign-extend it. */ diff --git a/libavcodec/ac3dec_data.c b/libavcodec/ac3dec_data.c index a3794ab223..527b3353f2 100644 --- a/libavcodec/ac3dec_data.c +++ b/libavcodec/ac3dec_data.c @@ -21,7 +21,7 @@ /** * @file - * Tables taken directly from the AC-3 spec. + * Tables taken directly from the AC-3 spec or derived from it. */ #include "ac3dec_data.h" @@ -42,6 +42,43 @@ const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3] = { { 3, 0, 1 }, { 3, 0, 2 }, { 3, 1, 0 }, { 3, 1, 1 } }; +/** + * Ungrouped mantissa tables; the extra entry is padding to avoid range checks + */ +#define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels) +/** + * Table 7.21 + */ +const int ff_ac3_bap3_mantissas[7 + 1] = { + SYMMETRIC_DEQUANT(0, 7), + SYMMETRIC_DEQUANT(1, 7), + SYMMETRIC_DEQUANT(2, 7), + SYMMETRIC_DEQUANT(3, 7), + SYMMETRIC_DEQUANT(4, 7), + SYMMETRIC_DEQUANT(5, 7), + SYMMETRIC_DEQUANT(6, 7), +}; +/** + * Table 7.23 + */ +const int ff_ac3_bap5_mantissas[15 + 1] = { + SYMMETRIC_DEQUANT(0, 15), + SYMMETRIC_DEQUANT(1, 15), + SYMMETRIC_DEQUANT(2, 15), + SYMMETRIC_DEQUANT(3, 15), + SYMMETRIC_DEQUANT(4, 15), + SYMMETRIC_DEQUANT(5, 15), + SYMMETRIC_DEQUANT(6, 15), + SYMMETRIC_DEQUANT(7, 15), + SYMMETRIC_DEQUANT(8, 15), + SYMMETRIC_DEQUANT(9, 15), + SYMMETRIC_DEQUANT(10, 15), + SYMMETRIC_DEQUANT(11, 15), + SYMMETRIC_DEQUANT(12, 15), + SYMMETRIC_DEQUANT(13, 15), + SYMMETRIC_DEQUANT(14, 15), +}; + const uint8_t ff_eac3_hebap_tab[64] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, diff --git a/libavcodec/ac3dec_data.h b/libavcodec/ac3dec_data.h index 975b52ef2c..4f3a23f6c7 100644 --- a/libavcodec/ac3dec_data.h +++ b/libavcodec/ac3dec_data.h @@ -24,9 +24,18 @@ #include +#include "libavutil/attributes_internal.h" + +FF_VISIBILITY_PUSH_HIDDEN + extern const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3]; +extern const int ff_ac3_bap3_mantissas[ 7 + 1]; +extern const int ff_ac3_bap5_mantissas[15 + 1]; + extern const uint8_t ff_eac3_hebap_tab[64]; extern const uint8_t ff_eac3_default_spx_band_struct[17]; +FF_VISIBILITY_POP_HIDDEN + #endif /* AVCODEC_AC3DEC_DATA_H */ -- 2.45.2