1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

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 <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-05-23 15:58:03 +02:00
parent fa45e20029
commit 9416ffd8b8
3 changed files with 49 additions and 15 deletions

View File

@ -55,9 +55,7 @@ static uint8_t ungroup_3_in_7_bits_tab[128][3];
/** tables for ungrouping mantissas */ /** tables for ungrouping mantissas */
static int b1_mantissas[32][3]; static int b1_mantissas[32][3];
static int b2_mantissas[128][3]; static int b2_mantissas[128][3];
static int b3_mantissas[8];
static int b4_mantissas[128][2]; static int b4_mantissas[128][2];
static int b5_mantissas[16];
/** /**
* Quantization table: levels for symmetric. bits for asymmetric. * 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][0] = symmetric_dequant(i / 11, 11);
b4_mantissas[i][1] = 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) #if (!USE_FIXED)
/* generate dynamic range table /* generate dynamic range table
@ -595,7 +583,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
} }
break; break;
case 3: case 3:
mantissa = b3_mantissas[get_bits(gbc, 3)]; mantissa = ff_ac3_bap3_mantissas[get_bits(gbc, 3)];
break; break;
case 4: case 4:
if (m->b4) { if (m->b4) {
@ -609,7 +597,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
} }
break; break;
case 5: case 5:
mantissa = b5_mantissas[get_bits(gbc, 4)]; mantissa = ff_ac3_bap5_mantissas[get_bits(gbc, 4)];
break; break;
default: /* 6 to 15 */ default: /* 6 to 15 */
/* Shift mantissa and sign-extend it. */ /* Shift mantissa and sign-extend it. */

View File

@ -21,7 +21,7 @@
/** /**
* @file * @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" #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 } { 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] = { const uint8_t ff_eac3_hebap_tab[64] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11,

View File

@ -24,9 +24,18 @@
#include <stdint.h> #include <stdint.h>
#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 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_hebap_tab[64];
extern const uint8_t ff_eac3_default_spx_band_struct[17]; extern const uint8_t ff_eac3_default_spx_band_struct[17];
FF_VISIBILITY_POP_HIDDEN
#endif /* AVCODEC_AC3DEC_DATA_H */ #endif /* AVCODEC_AC3DEC_DATA_H */