/src/ffmpeg/libavcodec/ac3dec_data.c
Line | Count | Source |
1 | | /* |
2 | | * AC-3 and E-AC-3 decoder tables |
3 | | * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com> |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * Tables taken directly from the AC-3 spec or derived from it. |
25 | | */ |
26 | | |
27 | | #include "ac3dec_data.h" |
28 | | #include "libavutil/thread.h" |
29 | | |
30 | | /** |
31 | | * Table used to ungroup 3 values stored in 5 bits. |
32 | | * Used by bap=1 mantissas and GAQ. |
33 | | * ff_ac3_ungroup_3_in_5_bits_tab[i] = { i/9, (i%9)/3, (i%9)%3 } |
34 | | */ |
35 | | const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3] = { |
36 | | { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, { 0, 1, 0 }, |
37 | | { 0, 1, 1 }, { 0, 1, 2 }, { 0, 2, 0 }, { 0, 2, 1 }, |
38 | | { 0, 2, 2 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 0, 2 }, |
39 | | { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 2, 0 }, |
40 | | { 1, 2, 1 }, { 1, 2, 2 }, { 2, 0, 0 }, { 2, 0, 1 }, |
41 | | { 2, 0, 2 }, { 2, 1, 0 }, { 2, 1, 1 }, { 2, 1, 2 }, |
42 | | { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, { 3, 0, 0 }, |
43 | | { 3, 0, 1 }, { 3, 0, 2 }, { 3, 1, 0 }, { 3, 1, 1 } |
44 | | }; |
45 | | |
46 | | /** |
47 | | * table for ungrouping 3 values in 7 bits. |
48 | | * used for exponents and bap=2 mantissas |
49 | | */ |
50 | | uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3]; |
51 | | |
52 | | /** |
53 | | * Symmetrical Dequantization |
54 | | * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization |
55 | | * Tables 7.19 to 7.23 |
56 | | */ |
57 | 2.20k | #define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels) |
58 | | /** |
59 | | * Ungrouped mantissa tables; the extra entry is padding to avoid range checks |
60 | | */ |
61 | | /** |
62 | | * Table 7.21 |
63 | | */ |
64 | | const int ff_ac3_bap3_mantissas[7 + 1] = { |
65 | | SYMMETRIC_DEQUANT(0, 7), |
66 | | SYMMETRIC_DEQUANT(1, 7), |
67 | | SYMMETRIC_DEQUANT(2, 7), |
68 | | SYMMETRIC_DEQUANT(3, 7), |
69 | | SYMMETRIC_DEQUANT(4, 7), |
70 | | SYMMETRIC_DEQUANT(5, 7), |
71 | | SYMMETRIC_DEQUANT(6, 7), |
72 | | }; |
73 | | /** |
74 | | * Table 7.23 |
75 | | */ |
76 | | const int ff_ac3_bap5_mantissas[15 + 1] = { |
77 | | SYMMETRIC_DEQUANT(0, 15), |
78 | | SYMMETRIC_DEQUANT(1, 15), |
79 | | SYMMETRIC_DEQUANT(2, 15), |
80 | | SYMMETRIC_DEQUANT(3, 15), |
81 | | SYMMETRIC_DEQUANT(4, 15), |
82 | | SYMMETRIC_DEQUANT(5, 15), |
83 | | SYMMETRIC_DEQUANT(6, 15), |
84 | | SYMMETRIC_DEQUANT(7, 15), |
85 | | SYMMETRIC_DEQUANT(8, 15), |
86 | | SYMMETRIC_DEQUANT(9, 15), |
87 | | SYMMETRIC_DEQUANT(10, 15), |
88 | | SYMMETRIC_DEQUANT(11, 15), |
89 | | SYMMETRIC_DEQUANT(12, 15), |
90 | | SYMMETRIC_DEQUANT(13, 15), |
91 | | SYMMETRIC_DEQUANT(14, 15), |
92 | | }; |
93 | | |
94 | | int ff_ac3_bap1_mantissas[32][3]; |
95 | | int ff_ac3_bap2_mantissas[128][3]; |
96 | | int ff_ac3_bap4_mantissas[128][2]; |
97 | | |
98 | | static inline int |
99 | | symmetric_dequant(int code, int levels) |
100 | 2.20k | { |
101 | 2.20k | return SYMMETRIC_DEQUANT(code, levels); |
102 | 2.20k | } |
103 | | |
104 | | static av_cold void ac3_init_static(void) |
105 | 3 | { |
106 | | /* generate table for ungrouping 3 values in 7 bits |
107 | | reference: Section 7.1.3 Exponent Decoding */ |
108 | 387 | for (int i = 0; i < 128; ++i) { |
109 | 384 | ff_ac3_ungroup_3_in_7_bits_tab[i][0] = i / 25; |
110 | 384 | ff_ac3_ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5; |
111 | 384 | ff_ac3_ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5; |
112 | 384 | } |
113 | | |
114 | | /* generate grouped mantissa tables |
115 | | reference: Section 7.3.5 Ungrouping of Mantissas */ |
116 | 99 | for (int i = 0; i < 32; ++i) { |
117 | | /* bap=1 mantissas */ |
118 | 96 | ff_ac3_bap1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3); |
119 | 96 | ff_ac3_bap1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3); |
120 | 96 | ff_ac3_bap1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3); |
121 | 96 | } |
122 | 387 | for (int i = 0; i < 128; ++i) { |
123 | | /* bap=2 mantissas */ |
124 | 384 | ff_ac3_bap2_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][0], 5); |
125 | 384 | ff_ac3_bap2_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][1], 5); |
126 | 384 | ff_ac3_bap2_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][2], 5); |
127 | | |
128 | | /* bap=4 mantissas */ |
129 | 384 | ff_ac3_bap4_mantissas[i][0] = symmetric_dequant(i / 11, 11); |
130 | 384 | ff_ac3_bap4_mantissas[i][1] = symmetric_dequant(i % 11, 11); |
131 | 384 | } |
132 | 3 | } |
133 | | |
134 | | av_cold void ff_ac3_init_static(void) |
135 | 6.69k | { |
136 | 6.69k | static AVOnce ac3_init_static_once = AV_ONCE_INIT; |
137 | 6.69k | ff_thread_once(&ac3_init_static_once, ac3_init_static); |
138 | 6.69k | } |
139 | | |
140 | | /** |
141 | | * Quantization table: levels for symmetric. bits for asymmetric. |
142 | | * reference: Table 7.18 Mapping of bap to Quantizer |
143 | | */ |
144 | | const uint8_t ff_ac3_quantization_tab[16] = { |
145 | | 0, 3, 5, 7, 11, 15, |
146 | | 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 |
147 | | }; |
148 | | |
149 | | /** |
150 | | * Table for default stereo downmixing coefficients |
151 | | * reference: Section 7.8.2 Downmixing Into Two Channels |
152 | | */ |
153 | | const uint8_t ff_ac3_default_coeffs[8][5][2] = { |
154 | | { { 2, 7 }, { 7, 2 }, }, |
155 | | { { 4, 4 }, }, |
156 | | { { 2, 7 }, { 7, 2 }, }, |
157 | | { { 2, 7 }, { 5, 5 }, { 7, 2 }, }, |
158 | | { { 2, 7 }, { 7, 2 }, { 6, 6 }, }, |
159 | | { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 8, 8 }, }, |
160 | | { { 2, 7 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, }, |
161 | | { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, }, |
162 | | }; |
163 | | |
164 | | const uint8_t ff_eac3_hebap_tab[64] = { |
165 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, |
166 | | 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, |
167 | | 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, |
168 | | 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, |
169 | | 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, |
170 | | 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, |
171 | | 19, 19, 19, 19, |
172 | | }; |
173 | | |
174 | | /** |
175 | | * Table E2.15 Default Spectral Extension Banding Structure |
176 | | */ |
177 | | const uint8_t ff_eac3_default_spx_band_struct[17] = |
178 | | { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; |
179 | | |
180 | | /** Adjustments in dB gain (LFE, +10 to -21 dB) */ |
181 | | const float ff_eac3_gain_levels_lfe[32] = { |
182 | | 3.162275, 2.818382, 2.511886, 2.238719, 1.995261, 1.778278, 1.584893, |
183 | | 1.412536, 1.258924, 1.122018, 1.000000, 0.891251, 0.794328, 0.707946, |
184 | | 0.630957, 0.562341, 0.501187, 0.446683, 0.398107, 0.354813, 0.316227, |
185 | | 0.281838, 0.251188, 0.223872, 0.199526, 0.177828, 0.158489, 0.141253, |
186 | | 0.125892, 0.112201, 0.100000, 0.089125 |
187 | | }; |