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

avcodec/ac3dec: Deduplicate mantissas and their init code

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-05-24 18:39:26 +02:00
parent 9416ffd8b8
commit 20caa5cf2d
3 changed files with 90 additions and 70 deletions

View File

@ -46,17 +46,6 @@
#include "decode.h" #include "decode.h"
#include "kbdwin.h" #include "kbdwin.h"
/**
* table for ungrouping 3 values in 7 bits.
* used for exponents and bap=2 mantissas
*/
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 b4_mantissas[128][2];
/** /**
* Quantization table: levels for symmetric. bits for asymmetric. * Quantization table: levels for symmetric. bits for asymmetric.
* reference: Table 7.18 Mapping of bap to Quantizer * reference: Table 7.18 Mapping of bap to Quantizer
@ -109,67 +98,28 @@ static const uint8_t ac3_default_coeffs[8][5][2] = {
{ { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, }, { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
}; };
/** #if (!USE_FIXED)
* Symmetrical Dequantization
* reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
* Tables 7.19 to 7.23
*/
static inline int
symmetric_dequant(int code, int levels)
{
return ((code - (levels >> 1)) * (1 << 24)) / levels;
}
/* /*
* Initialize tables at runtime. * Initialize tables at runtime.
*/ */
static av_cold void ac3_tables_init(void) static av_cold void ac3_float_tables_init(void)
{ {
int i;
/* generate table for ungrouping 3 values in 7 bits
reference: Section 7.1.3 Exponent Decoding */
for (i = 0; i < 128; i++) {
ungroup_3_in_7_bits_tab[i][0] = i / 25;
ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
}
/* generate grouped mantissa tables
reference: Section 7.3.5 Ungrouping of Mantissas */
for (i = 0; i < 32; i++) {
/* bap=1 mantissas */
b1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
b1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
b1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
}
for (i = 0; i < 128; i++) {
/* bap=2 mantissas */
b2_mantissas[i][0] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][0], 5);
b2_mantissas[i][1] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][1], 5);
b2_mantissas[i][2] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][2], 5);
/* bap=4 mantissas */
b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
}
#if (!USE_FIXED)
/* generate dynamic range table /* generate dynamic range table
reference: Section 7.7.1 Dynamic Range Control */ reference: Section 7.7.1 Dynamic Range Control */
for (i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
int v = (i >> 5) - ((i >> 7) << 3) - 5; int v = (i >> 5) - ((i >> 7) << 3) - 5;
dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20); dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
} }
/* generate compr dynamic range table /* generate compr dynamic range table
reference: Section 7.7.2 Heavy Compression */ reference: Section 7.7.2 Heavy Compression */
for (i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
int v = (i >> 4) - ((i >> 7) << 4) - 4; int v = (i >> 4) - ((i >> 7) << 4) - 4;
ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10); ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
} }
#endif ff_ac3_init_static();
} }
#endif
static void ac3_downmix(AVCodecContext *avctx) static void ac3_downmix(AVCodecContext *avctx)
{ {
@ -194,7 +144,6 @@ static void ac3_downmix(AVCodecContext *avctx)
*/ */
static av_cold int ac3_decode_init(AVCodecContext *avctx) static av_cold int ac3_decode_init(AVCodecContext *avctx)
{ {
static AVOnce init_static_once = AV_ONCE_INIT;
AC3DecodeContext *s = avctx->priv_data; AC3DecodeContext *s = avctx->priv_data;
const float scale = 1.0f; const float scale = 1.0f;
int i, ret; int i, ret;
@ -235,7 +184,12 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx)
s->dlyptr[i] = s->delay[i]; s->dlyptr[i] = s->delay[i];
} }
ff_thread_once(&init_static_once, ac3_tables_init); #if USE_FIXED
ff_ac3_init_static();
#else
static AVOnce init_static_once = AV_ONCE_INIT;
ff_thread_once(&init_static_once, ac3_float_tables_init);
#endif
return 0; return 0;
} }
@ -467,9 +421,9 @@ static int decode_exponents(AC3DecodeContext *s,
av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc); av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][0]; dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][0];
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][1]; dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][1];
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][2]; dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][2];
} }
/* convert to absolute exps and expand groups */ /* convert to absolute exps and expand groups */
@ -564,9 +518,9 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
mantissa = m->b1_mant[m->b1]; mantissa = m->b1_mant[m->b1];
} else { } else {
int bits = get_bits(gbc, 5); int bits = get_bits(gbc, 5);
mantissa = b1_mantissas[bits][0]; mantissa = ff_ac3_bap1_mantissas[bits][0];
m->b1_mant[1] = b1_mantissas[bits][1]; m->b1_mant[1] = ff_ac3_bap1_mantissas[bits][1];
m->b1_mant[0] = b1_mantissas[bits][2]; m->b1_mant[0] = ff_ac3_bap1_mantissas[bits][2];
m->b1 = 2; m->b1 = 2;
} }
break; break;
@ -576,9 +530,9 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
mantissa = m->b2_mant[m->b2]; mantissa = m->b2_mant[m->b2];
} else { } else {
int bits = get_bits(gbc, 7); int bits = get_bits(gbc, 7);
mantissa = b2_mantissas[bits][0]; mantissa = ff_ac3_bap2_mantissas[bits][0];
m->b2_mant[1] = b2_mantissas[bits][1]; m->b2_mant[1] = ff_ac3_bap2_mantissas[bits][1];
m->b2_mant[0] = b2_mantissas[bits][2]; m->b2_mant[0] = ff_ac3_bap2_mantissas[bits][2];
m->b2 = 2; m->b2 = 2;
} }
break; break;
@ -591,8 +545,8 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
mantissa = m->b4_mant; mantissa = m->b4_mant;
} else { } else {
int bits = get_bits(gbc, 7); int bits = get_bits(gbc, 7);
mantissa = b4_mantissas[bits][0]; mantissa = ff_ac3_bap4_mantissas[bits][0];
m->b4_mant = b4_mantissas[bits][1]; m->b4_mant = ff_ac3_bap4_mantissas[bits][1];
m->b4 = 1; m->b4 = 1;
} }
break; break;

View File

@ -25,6 +25,7 @@
*/ */
#include "ac3dec_data.h" #include "ac3dec_data.h"
#include "libavutil/thread.h"
/** /**
* Table used to ungroup 3 values stored in 5 bits. * Table used to ungroup 3 values stored in 5 bits.
@ -43,9 +44,20 @@ const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3] = {
}; };
/** /**
* Ungrouped mantissa tables; the extra entry is padding to avoid range checks * table for ungrouping 3 values in 7 bits.
* used for exponents and bap=2 mantissas
*/
uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3];
/**
* Symmetrical Dequantization
* reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
* Tables 7.19 to 7.23
*/ */
#define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels) #define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels)
/**
* Ungrouped mantissa tables; the extra entry is padding to avoid range checks
*/
/** /**
* Table 7.21 * Table 7.21
*/ */
@ -79,6 +91,52 @@ const int ff_ac3_bap5_mantissas[15 + 1] = {
SYMMETRIC_DEQUANT(14, 15), SYMMETRIC_DEQUANT(14, 15),
}; };
int ff_ac3_bap1_mantissas[32][3];
int ff_ac3_bap2_mantissas[128][3];
int ff_ac3_bap4_mantissas[128][2];
static inline int
symmetric_dequant(int code, int levels)
{
return SYMMETRIC_DEQUANT(code, levels);
}
static av_cold void ac3_init_static(void)
{
/* generate table for ungrouping 3 values in 7 bits
reference: Section 7.1.3 Exponent Decoding */
for (int i = 0; i < 128; ++i) {
ff_ac3_ungroup_3_in_7_bits_tab[i][0] = i / 25;
ff_ac3_ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
ff_ac3_ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
}
/* generate grouped mantissa tables
reference: Section 7.3.5 Ungrouping of Mantissas */
for (int i = 0; i < 32; ++i) {
/* bap=1 mantissas */
ff_ac3_bap1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
ff_ac3_bap1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
ff_ac3_bap1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
}
for (int i = 0; i < 128; ++i) {
/* bap=2 mantissas */
ff_ac3_bap2_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][0], 5);
ff_ac3_bap2_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][1], 5);
ff_ac3_bap2_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][2], 5);
/* bap=4 mantissas */
ff_ac3_bap4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
ff_ac3_bap4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
}
}
av_cold void ff_ac3_init_static(void)
{
static AVOnce ac3_init_static_once = AV_ONCE_INIT;
ff_thread_once(&ac3_init_static_once, ac3_init_static);
}
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

@ -29,13 +29,21 @@
FF_VISIBILITY_PUSH_HIDDEN 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 uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3];
extern const int ff_ac3_bap3_mantissas[ 7 + 1]; extern const int ff_ac3_bap3_mantissas[ 7 + 1];
extern const int ff_ac3_bap5_mantissas[15 + 1]; extern const int ff_ac3_bap5_mantissas[15 + 1];
/** tables for ungrouping mantissas */
extern int ff_ac3_bap1_mantissas[32][3];
extern int ff_ac3_bap2_mantissas[128][3];
extern int ff_ac3_bap4_mantissas[128][2];
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];
void ff_ac3_init_static(void);
FF_VISIBILITY_POP_HIDDEN FF_VISIBILITY_POP_HIDDEN
#endif /* AVCODEC_AC3DEC_DATA_H */ #endif /* AVCODEC_AC3DEC_DATA_H */