/src/ffmpeg/libavcodec/ac3.c
Line | Count | Source |
1 | | /* |
2 | | * Common code between the AC-3 encoder and decoder |
3 | | * Copyright (c) 2000 Fabrice Bellard |
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 | | * Common code between the AC-3 encoder and decoder. |
25 | | */ |
26 | | |
27 | | #include "libavutil/error.h" |
28 | | #include "libavutil/macros.h" |
29 | | |
30 | | #include "ac3.h" |
31 | | #include "ac3defs.h" |
32 | | |
33 | | /** |
34 | | * Starting frequency coefficient bin for each critical band. |
35 | | */ |
36 | | const uint8_t ff_ac3_band_start_tab[AC3_CRITICAL_BANDS+1] = { |
37 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
38 | | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
39 | | 20, 21, 22, 23, 24, 25, 26, 27, 28, 31, |
40 | | 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, |
41 | | 79, 85, 97, 109, 121, 133, 157, 181, 205, 229, 253 |
42 | | }; |
43 | | |
44 | | /** |
45 | | * Map each frequency coefficient bin to the critical band that contains it. |
46 | | */ |
47 | | const uint8_t ff_ac3_bin_to_band_tab[253] = { |
48 | | 0, |
49 | | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
50 | | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
51 | | 25, 26, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, |
52 | | 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, |
53 | | 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, |
54 | | 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, |
55 | | 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, |
56 | | 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, |
57 | | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, |
58 | | 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, |
59 | | 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, |
60 | | 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, |
61 | | 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, |
62 | | 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, |
63 | | 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, |
64 | | 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, |
65 | | 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, |
66 | | 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
67 | | 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, |
68 | | 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, |
69 | | 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49 |
70 | | }; |
71 | | |
72 | | static const uint8_t ac3_log_add_tab[260]= { |
73 | | 0x40,0x3f,0x3e,0x3d,0x3c,0x3b,0x3a,0x39,0x38,0x37, |
74 | | 0x36,0x35,0x34,0x34,0x33,0x32,0x31,0x30,0x2f,0x2f, |
75 | | 0x2e,0x2d,0x2c,0x2c,0x2b,0x2a,0x29,0x29,0x28,0x27, |
76 | | 0x26,0x26,0x25,0x24,0x24,0x23,0x23,0x22,0x21,0x21, |
77 | | 0x20,0x20,0x1f,0x1e,0x1e,0x1d,0x1d,0x1c,0x1c,0x1b, |
78 | | 0x1b,0x1a,0x1a,0x19,0x19,0x18,0x18,0x17,0x17,0x16, |
79 | | 0x16,0x15,0x15,0x15,0x14,0x14,0x13,0x13,0x13,0x12, |
80 | | 0x12,0x12,0x11,0x11,0x11,0x10,0x10,0x10,0x0f,0x0f, |
81 | | 0x0f,0x0e,0x0e,0x0e,0x0d,0x0d,0x0d,0x0d,0x0c,0x0c, |
82 | | 0x0c,0x0c,0x0b,0x0b,0x0b,0x0b,0x0a,0x0a,0x0a,0x0a, |
83 | | 0x0a,0x09,0x09,0x09,0x09,0x09,0x08,0x08,0x08,0x08, |
84 | | 0x08,0x08,0x07,0x07,0x07,0x07,0x07,0x07,0x06,0x06, |
85 | | 0x06,0x06,0x06,0x06,0x06,0x06,0x05,0x05,0x05,0x05, |
86 | | 0x05,0x05,0x05,0x05,0x04,0x04,0x04,0x04,0x04,0x04, |
87 | | 0x04,0x04,0x04,0x04,0x04,0x03,0x03,0x03,0x03,0x03, |
88 | | 0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x02, |
89 | | 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, |
90 | | 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x01,0x01, |
91 | | 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, |
92 | | 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, |
93 | | 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, |
94 | | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
95 | | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
96 | | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
97 | | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
98 | | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
99 | | }; |
100 | | |
101 | | static const uint16_t ac3_hearing_threshold_tab[AC3_CRITICAL_BANDS][3]= { |
102 | | { 0x04d0,0x04f0,0x0580 }, |
103 | | { 0x04d0,0x04f0,0x0580 }, |
104 | | { 0x0440,0x0460,0x04b0 }, |
105 | | { 0x0400,0x0410,0x0450 }, |
106 | | { 0x03e0,0x03e0,0x0420 }, |
107 | | { 0x03c0,0x03d0,0x03f0 }, |
108 | | { 0x03b0,0x03c0,0x03e0 }, |
109 | | { 0x03b0,0x03b0,0x03d0 }, |
110 | | { 0x03a0,0x03b0,0x03c0 }, |
111 | | { 0x03a0,0x03a0,0x03b0 }, |
112 | | { 0x03a0,0x03a0,0x03b0 }, |
113 | | { 0x03a0,0x03a0,0x03b0 }, |
114 | | { 0x03a0,0x03a0,0x03a0 }, |
115 | | { 0x0390,0x03a0,0x03a0 }, |
116 | | { 0x0390,0x0390,0x03a0 }, |
117 | | { 0x0390,0x0390,0x03a0 }, |
118 | | { 0x0380,0x0390,0x03a0 }, |
119 | | { 0x0380,0x0380,0x03a0 }, |
120 | | { 0x0370,0x0380,0x03a0 }, |
121 | | { 0x0370,0x0380,0x03a0 }, |
122 | | { 0x0360,0x0370,0x0390 }, |
123 | | { 0x0360,0x0370,0x0390 }, |
124 | | { 0x0350,0x0360,0x0390 }, |
125 | | { 0x0350,0x0360,0x0390 }, |
126 | | { 0x0340,0x0350,0x0380 }, |
127 | | { 0x0340,0x0350,0x0380 }, |
128 | | { 0x0330,0x0340,0x0380 }, |
129 | | { 0x0320,0x0340,0x0370 }, |
130 | | { 0x0310,0x0320,0x0360 }, |
131 | | { 0x0300,0x0310,0x0350 }, |
132 | | { 0x02f0,0x0300,0x0340 }, |
133 | | { 0x02f0,0x02f0,0x0330 }, |
134 | | { 0x02f0,0x02f0,0x0320 }, |
135 | | { 0x02f0,0x02f0,0x0310 }, |
136 | | { 0x0300,0x02f0,0x0300 }, |
137 | | { 0x0310,0x0300,0x02f0 }, |
138 | | { 0x0340,0x0320,0x02f0 }, |
139 | | { 0x0390,0x0350,0x02f0 }, |
140 | | { 0x03e0,0x0390,0x0300 }, |
141 | | { 0x0420,0x03e0,0x0310 }, |
142 | | { 0x0460,0x0420,0x0330 }, |
143 | | { 0x0490,0x0450,0x0350 }, |
144 | | { 0x04a0,0x04a0,0x03c0 }, |
145 | | { 0x0460,0x0490,0x0410 }, |
146 | | { 0x0440,0x0460,0x0470 }, |
147 | | { 0x0440,0x0440,0x04a0 }, |
148 | | { 0x0520,0x0480,0x0460 }, |
149 | | { 0x0800,0x0630,0x0440 }, |
150 | | { 0x0840,0x0840,0x0450 }, |
151 | | { 0x0840,0x0840,0x04e0 }, |
152 | | }; |
153 | | |
154 | | static inline int calc_lowcomp1(int a, int b0, int b1, int c) |
155 | 26.3M | { |
156 | 26.3M | if ((b0 + 256) == b1) { |
157 | 3.92M | a = c; |
158 | 22.3M | } else if (b0 > b1) { |
159 | 3.72M | a = FFMAX(a - 64, 0); |
160 | 3.72M | } |
161 | 26.3M | return a; |
162 | 26.3M | } |
163 | | |
164 | | static inline int calc_lowcomp(int a, int b0, int b1, int bin) |
165 | 22.7M | { |
166 | 22.7M | if (bin < 7) { |
167 | 6.28M | return calc_lowcomp1(a, b0, b1, 384); |
168 | 16.4M | } else if (bin < 20) { |
169 | 14.2M | return calc_lowcomp1(a, b0, b1, 320); |
170 | 14.2M | } else { |
171 | 2.19M | return FFMAX(a - 128, 0); |
172 | 2.19M | } |
173 | 22.7M | } |
174 | | |
175 | | void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd, |
176 | | int16_t *band_psd) |
177 | 1.76M | { |
178 | 1.76M | int bin, band; |
179 | | |
180 | | /* exponent mapping to PSD */ |
181 | 126M | for (bin = start; bin < end; bin++) { |
182 | 124M | psd[bin]=(3072 - (exp[bin] << 7)); |
183 | 124M | } |
184 | | |
185 | | /* PSD integration */ |
186 | 1.76M | bin = start; |
187 | 1.76M | band = ff_ac3_bin_to_band_tab[start]; |
188 | 47.2M | do { |
189 | 47.2M | int v = psd[bin++]; |
190 | 47.2M | int band_end = FFMIN(ff_ac3_band_start_tab[band+1], end); |
191 | 124M | for (; bin < band_end; bin++) { |
192 | 77.5M | int max = FFMAX(v, psd[bin]); |
193 | | /* logadd */ |
194 | 77.5M | int adr = FFMIN(max - ((v + psd[bin] + 1) >> 1), 255); |
195 | 77.5M | v = max + ac3_log_add_tab[adr]; |
196 | 77.5M | } |
197 | 47.2M | band_psd[band++] = v; |
198 | 47.2M | } while (end > ff_ac3_band_start_tab[band]); |
199 | 1.76M | } |
200 | | |
201 | | int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd, |
202 | | int start, int end, int fast_gain, int is_lfe, |
203 | | int dba_mode, int dba_nsegs, uint8_t *dba_offsets, |
204 | | uint8_t *dba_lengths, uint8_t *dba_values, |
205 | | int16_t *mask) |
206 | 1.96M | { |
207 | 1.96M | int16_t excite[AC3_CRITICAL_BANDS]; /* excitation */ |
208 | 1.96M | int band; |
209 | 1.96M | int band_start, band_end, begin, end1; |
210 | 1.96M | int lowcomp, fastleak, slowleak; |
211 | | |
212 | 1.96M | if (end <= 0) |
213 | 32.7k | return AVERROR_INVALIDDATA; |
214 | | |
215 | | /* excitation function */ |
216 | 1.92M | band_start = ff_ac3_bin_to_band_tab[start]; |
217 | 1.92M | band_end = ff_ac3_bin_to_band_tab[end-1] + 1; |
218 | | |
219 | 1.92M | if (band_start == 0) { |
220 | 1.78M | lowcomp = 0; |
221 | 1.78M | lowcomp = calc_lowcomp1(lowcomp, band_psd[0], band_psd[1], 384); |
222 | 1.78M | excite[0] = band_psd[0] - fast_gain - lowcomp; |
223 | 1.78M | lowcomp = calc_lowcomp1(lowcomp, band_psd[1], band_psd[2], 384); |
224 | 1.78M | excite[1] = band_psd[1] - fast_gain - lowcomp; |
225 | 1.78M | begin = 7; |
226 | 2.21M | for (band = 2; band < 7; band++) { |
227 | 2.20M | if (!(is_lfe && band == 6)) |
228 | 2.20M | lowcomp = calc_lowcomp1(lowcomp, band_psd[band], band_psd[band+1], 384); |
229 | 2.20M | fastleak = band_psd[band] - fast_gain; |
230 | 2.20M | slowleak = band_psd[band] - s->slow_gain; |
231 | 2.20M | excite[band] = fastleak - lowcomp; |
232 | 2.20M | if (!(is_lfe && band == 6)) { |
233 | 2.20M | if (band_psd[band] <= band_psd[band+1]) { |
234 | 1.77M | begin = band + 1; |
235 | 1.77M | break; |
236 | 1.77M | } |
237 | 2.20M | } |
238 | 2.20M | } |
239 | | |
240 | 1.78M | end1 = FFMIN(band_end, 22); |
241 | 24.9M | for (band = begin; band < end1; band++) { |
242 | 23.1M | if (!(is_lfe && band == 6)) |
243 | 22.7M | lowcomp = calc_lowcomp(lowcomp, band_psd[band], band_psd[band+1], band); |
244 | 23.1M | fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain); |
245 | 23.1M | slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain); |
246 | 23.1M | excite[band] = FFMAX(fastleak - lowcomp, slowleak); |
247 | 23.1M | } |
248 | 1.78M | begin = 22; |
249 | 1.78M | } else { |
250 | | /* coupling channel */ |
251 | 142k | begin = band_start; |
252 | 142k | fastleak = (s->cpl_fast_leak << 8) + 768; |
253 | 142k | slowleak = (s->cpl_slow_leak << 8) + 768; |
254 | 142k | } |
255 | | |
256 | 25.6M | for (band = begin; band < band_end; band++) { |
257 | 23.7M | fastleak = FFMAX(fastleak - s->fast_decay, band_psd[band] - fast_gain); |
258 | 23.7M | slowleak = FFMAX(slowleak - s->slow_decay, band_psd[band] - s->slow_gain); |
259 | 23.7M | excite[band] = FFMAX(fastleak, slowleak); |
260 | 23.7M | } |
261 | | |
262 | | /* compute masking curve */ |
263 | | |
264 | 54.5M | for (band = band_start; band < band_end; band++) { |
265 | 52.6M | int tmp = s->db_per_bit - band_psd[band]; |
266 | 52.6M | if (tmp > 0) { |
267 | 15.2M | excite[band] += tmp >> 2; |
268 | 15.2M | } |
269 | 52.6M | mask[band] = FFMAX(ac3_hearing_threshold_tab[band >> s->sr_shift][s->sr_code], excite[band]); |
270 | 52.6M | } |
271 | | |
272 | | /* delta bit allocation */ |
273 | | |
274 | 1.92M | if (dba_mode == DBA_REUSE || dba_mode == DBA_NEW) { |
275 | 35.3k | int i, seg, delta; |
276 | 35.3k | if (dba_nsegs > 8) |
277 | 0 | return -1; |
278 | 35.3k | band = band_start; |
279 | 94.1k | for (seg = 0; seg < dba_nsegs; seg++) { |
280 | 75.5k | band += dba_offsets[seg]; |
281 | 75.5k | if (band >= AC3_CRITICAL_BANDS || dba_lengths[seg] > AC3_CRITICAL_BANDS-band) |
282 | 16.8k | return -1; |
283 | 58.7k | if (dba_values[seg] >= 4) { |
284 | 19.0k | delta = (dba_values[seg] - 3) * 128; |
285 | 39.6k | } else { |
286 | 39.6k | delta = (dba_values[seg] - 4) * 128; |
287 | 39.6k | } |
288 | 367k | for (i = 0; i < dba_lengths[seg]; i++) { |
289 | 308k | mask[band++] += delta; |
290 | 308k | } |
291 | 58.7k | } |
292 | 35.3k | } |
293 | 1.91M | return 0; |
294 | 1.92M | } |