Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/eac3dec.c |
Date: | 2022-07-04 00:18:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 255 | 303 | 84.2% |
Branches: | 193 | 256 | 75.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * E-AC-3 decoder | ||
3 | * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com> | ||
4 | * Copyright (c) 2008 Justin Ruggles | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /* | ||
24 | * There are several features of E-AC-3 that this decoder does not yet support. | ||
25 | * | ||
26 | * Enhanced Coupling | ||
27 | * No known samples exist. If any ever surface, this feature should not be | ||
28 | * too difficult to implement. | ||
29 | * | ||
30 | * Reduced Sample Rates | ||
31 | * No known samples exist. The spec also does not give clear information | ||
32 | * on how this is to be implemented. | ||
33 | * | ||
34 | * Transient Pre-noise Processing | ||
35 | * This is side information which a decoder should use to reduce artifacts | ||
36 | * caused by transients. There are samples which are known to have this | ||
37 | * information, but this decoder currently ignores it. | ||
38 | */ | ||
39 | |||
40 | |||
41 | #include "avcodec.h" | ||
42 | #include "aac_ac3_parser.h" | ||
43 | #include "ac3.h" | ||
44 | #include "ac3dec.h" | ||
45 | #include "ac3dec_data.h" | ||
46 | #include "eac3_data.h" | ||
47 | |||
48 | /** gain adaptive quantization mode */ | ||
49 | typedef enum { | ||
50 | EAC3_GAQ_NO =0, | ||
51 | EAC3_GAQ_12, | ||
52 | EAC3_GAQ_14, | ||
53 | EAC3_GAQ_124 | ||
54 | } EAC3GaqMode; | ||
55 | |||
56 | #define EAC3_SR_CODE_REDUCED 3 | ||
57 | |||
58 | 3234 | static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s) | |
59 | { | ||
60 | int bin, bnd, ch, i; | ||
61 | 3234 | uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS]; | |
62 | float rms_energy[SPX_MAX_BANDS]; | ||
63 | |||
64 | /* Set copy index mapping table. Set wrap flags to apply a notch filter at | ||
65 | wrap points later on. */ | ||
66 | 3234 | bin = s->spx_dst_start_freq; | |
67 | 3234 | num_copy_sections = 0; | |
68 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 3234 times.
|
10566 | for (bnd = 0; bnd < s->num_spx_bands; bnd++) { |
69 | int copysize; | ||
70 | 7332 | int bandsize = s->spx_band_sizes[bnd]; | |
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7332 times.
|
7332 | if (bin + bandsize > s->spx_src_start_freq) { |
72 | ✗ | copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; | |
73 | ✗ | bin = s->spx_dst_start_freq; | |
74 | ✗ | wrapflag[bnd] = 1; | |
75 | } | ||
76 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 7332 times.
|
14664 | for (i = 0; i < bandsize; i += copysize) { |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7332 times.
|
7332 | if (bin == s->spx_src_start_freq) { |
78 | ✗ | copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; | |
79 | ✗ | bin = s->spx_dst_start_freq; | |
80 | } | ||
81 | 7332 | copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin); | |
82 | 7332 | bin += copysize; | |
83 | } | ||
84 | } | ||
85 | 3234 | copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; | |
86 | |||
87 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 3234 times.
|
10566 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7332 times.
|
7332 | if (!s->channel_uses_spx[ch]) |
89 | ✗ | continue; | |
90 | |||
91 | /* Copy coeffs from normal bands to extension bands */ | ||
92 | 7332 | bin = s->spx_src_start_freq; | |
93 |
2/2✓ Branch 0 taken 7332 times.
✓ Branch 1 taken 7332 times.
|
14664 | for (i = 0; i < num_copy_sections; i++) { |
94 | 7332 | memcpy(&s->transform_coeffs[ch][bin], | |
95 | 7332 | &s->transform_coeffs[ch][s->spx_dst_start_freq], | |
96 | 7332 | copy_sizes[i]*sizeof(INTFLOAT)); | |
97 | 7332 | bin += copy_sizes[i]; | |
98 | } | ||
99 | |||
100 | /* Calculate RMS energy for each SPX band. */ | ||
101 | 7332 | bin = s->spx_src_start_freq; | |
102 |
2/2✓ Branch 0 taken 18984 times.
✓ Branch 1 taken 7332 times.
|
26316 | for (bnd = 0; bnd < s->num_spx_bands; bnd++) { |
103 | 18984 | int bandsize = s->spx_band_sizes[bnd]; | |
104 | 18984 | float accum = 0.0f; | |
105 |
2/2✓ Branch 0 taken 245088 times.
✓ Branch 1 taken 18984 times.
|
264072 | for (i = 0; i < bandsize; i++) { |
106 | 245088 | float coeff = s->transform_coeffs[ch][bin++]; | |
107 | 245088 | accum += coeff * coeff; | |
108 | } | ||
109 | 18984 | rms_energy[bnd] = sqrtf(accum / bandsize); | |
110 | } | ||
111 | |||
112 | /* Apply a notch filter at transitions between normal and extension | ||
113 | bands and at all wrap points. */ | ||
114 |
2/2✓ Branch 0 taken 6294 times.
✓ Branch 1 taken 1038 times.
|
7332 | if (s->spx_atten_code[ch] >= 0) { |
115 | 6294 | const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]]; | |
116 | 6294 | bin = s->spx_src_start_freq - 2; | |
117 |
2/2✓ Branch 0 taken 13794 times.
✓ Branch 1 taken 6294 times.
|
20088 | for (bnd = 0; bnd < s->num_spx_bands; bnd++) { |
118 |
2/2✓ Branch 0 taken 6294 times.
✓ Branch 1 taken 7500 times.
|
13794 | if (wrapflag[bnd]) { |
119 | 6294 | INTFLOAT *coeffs = &s->transform_coeffs[ch][bin]; | |
120 | 6294 | coeffs[0] *= atten_tab[0]; | |
121 | 6294 | coeffs[1] *= atten_tab[1]; | |
122 | 6294 | coeffs[2] *= atten_tab[2]; | |
123 | 6294 | coeffs[3] *= atten_tab[1]; | |
124 | 6294 | coeffs[4] *= atten_tab[0]; | |
125 | } | ||
126 | 13794 | bin += s->spx_band_sizes[bnd]; | |
127 | } | ||
128 | } | ||
129 | |||
130 | /* Apply noise-blended coefficient scaling based on previously | ||
131 | calculated RMS energy, blending factors, and SPX coordinates for | ||
132 | each band. */ | ||
133 | 7332 | bin = s->spx_src_start_freq; | |
134 |
2/2✓ Branch 0 taken 18984 times.
✓ Branch 1 taken 7332 times.
|
26316 | for (bnd = 0; bnd < s->num_spx_bands; bnd++) { |
135 | 18984 | float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN); | |
136 | 18984 | float sscale = s->spx_signal_blend[ch][bnd]; | |
137 | #if USE_FIXED | ||
138 | // spx_noise_blend and spx_signal_blend are both FP.23 | ||
139 | ✗ | nscale *= 1.0 / (1<<23); | |
140 | ✗ | sscale *= 1.0 / (1<<23); | |
141 | #endif | ||
142 |
2/2✓ Branch 0 taken 245088 times.
✓ Branch 1 taken 18984 times.
|
264072 | for (i = 0; i < s->spx_band_sizes[bnd]; i++) { |
143 | 245088 | float noise = nscale * (int32_t)av_lfg_get(&s->dith_state); | |
144 | 245088 | s->transform_coeffs[ch][bin] *= sscale; | |
145 | 245088 | s->transform_coeffs[ch][bin++] += noise; | |
146 | } | ||
147 | } | ||
148 | } | ||
149 | 3234 | } | |
150 | |||
151 | |||
152 | /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */ | ||
153 | #define COEFF_0 10273905LL | ||
154 | |||
155 | /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */ | ||
156 | #define COEFF_1 11863283LL | ||
157 | |||
158 | /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */ | ||
159 | #define COEFF_2 3070444LL | ||
160 | |||
161 | /** | ||
162 | * Calculate 6-point IDCT of the pre-mantissas. | ||
163 | * All calculations are 24-bit fixed-point. | ||
164 | */ | ||
165 | 99072 | static void idct6(int pre_mant[6]) | |
166 | { | ||
167 | int tmp; | ||
168 | int even0, even1, even2, odd0, odd1, odd2; | ||
169 | |||
170 | 99072 | odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5]; | |
171 | |||
172 | 99072 | even2 = ( pre_mant[2] * COEFF_0) >> 23; | |
173 | 99072 | tmp = ( pre_mant[4] * COEFF_1) >> 23; | |
174 | 99072 | odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23; | |
175 | |||
176 | 99072 | even0 = pre_mant[0] + (tmp >> 1); | |
177 | 99072 | even1 = pre_mant[0] - tmp; | |
178 | |||
179 | 99072 | tmp = even0; | |
180 | 99072 | even0 = tmp + even2; | |
181 | 99072 | even2 = tmp - even2; | |
182 | |||
183 | 99072 | tmp = odd0; | |
184 | 99072 | odd0 = tmp + pre_mant[1] + pre_mant[3]; | |
185 | 99072 | odd2 = tmp + pre_mant[5] - pre_mant[3]; | |
186 | |||
187 | 99072 | pre_mant[0] = even0 + odd0; | |
188 | 99072 | pre_mant[1] = even1 + odd1; | |
189 | 99072 | pre_mant[2] = even2 + odd2; | |
190 | 99072 | pre_mant[3] = even2 - odd2; | |
191 | 99072 | pre_mant[4] = even1 - odd1; | |
192 | 99072 | pre_mant[5] = even0 - odd0; | |
193 | 99072 | } | |
194 | |||
195 | 810 | static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch) | |
196 | { | ||
197 | int bin, blk, gs; | ||
198 | int end_bap, gaq_mode; | ||
199 | 810 | GetBitContext *gbc = &s->gbc; | |
200 | int gaq_gain[AC3_MAX_COEFS]; | ||
201 | |||
202 | 810 | gaq_mode = get_bits(gbc, 2); | |
203 |
2/2✓ Branch 0 taken 612 times.
✓ Branch 1 taken 198 times.
|
810 | end_bap = (gaq_mode < 2) ? 12 : 17; |
204 | |||
205 | /* if GAQ gain is used, decode gain codes for bins with hebap between | ||
206 | 8 and end_bap */ | ||
207 | 810 | gs = 0; | |
208 |
4/4✓ Branch 0 taken 259 times.
✓ Branch 1 taken 551 times.
✓ Branch 2 taken 146 times.
✓ Branch 3 taken 113 times.
|
810 | if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) { |
209 | /* read 1-bit GAQ gain codes */ | ||
210 |
2/2✓ Branch 0 taken 86761 times.
✓ Branch 1 taken 697 times.
|
87458 | for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) { |
211 |
4/4✓ Branch 0 taken 32207 times.
✓ Branch 1 taken 54554 times.
✓ Branch 2 taken 31300 times.
✓ Branch 3 taken 907 times.
|
86761 | if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap) |
212 | 31300 | gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1); | |
213 | } | ||
214 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 61 times.
|
113 | } else if (gaq_mode == EAC3_GAQ_124) { |
215 | /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */ | ||
216 | 52 | int gc = 2; | |
217 |
2/2✓ Branch 0 taken 6508 times.
✓ Branch 1 taken 52 times.
|
6560 | for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) { |
218 |
3/4✓ Branch 0 taken 1905 times.
✓ Branch 1 taken 4603 times.
✓ Branch 2 taken 1905 times.
✗ Branch 3 not taken.
|
6508 | if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) { |
219 |
2/2✓ Branch 0 taken 655 times.
✓ Branch 1 taken 1250 times.
|
1905 | if (gc++ == 2) { |
220 | 655 | int group_code = get_bits(gbc, 5); | |
221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 655 times.
|
655 | if (group_code > 26) { |
222 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n"); | |
223 | ✗ | group_code = 26; | |
224 | } | ||
225 | 655 | gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0]; | |
226 | 655 | gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1]; | |
227 | 655 | gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2]; | |
228 | 655 | gc = 0; | |
229 | } | ||
230 | } | ||
231 | } | ||
232 | } | ||
233 | |||
234 | 810 | gs=0; | |
235 |
2/2✓ Branch 0 taken 99072 times.
✓ Branch 1 taken 810 times.
|
99882 | for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) { |
236 | 99072 | int hebap = s->bap[ch][bin]; | |
237 | 99072 | int bits = ff_eac3_bits_vs_hebap[hebap]; | |
238 |
2/2✓ Branch 0 taken 31922 times.
✓ Branch 1 taken 67150 times.
|
99072 | if (!hebap) { |
239 | /* zero-mantissa dithering */ | ||
240 |
2/2✓ Branch 0 taken 191532 times.
✓ Branch 1 taken 31922 times.
|
223454 | for (blk = 0; blk < 6; blk++) { |
241 | 191532 | s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000; | |
242 | } | ||
243 |
2/2✓ Branch 0 taken 31011 times.
✓ Branch 1 taken 36139 times.
|
67150 | } else if (hebap < 8) { |
244 | /* Vector Quantization */ | ||
245 | 31011 | int v = get_bits(gbc, bits); | |
246 |
2/2✓ Branch 0 taken 186066 times.
✓ Branch 1 taken 31011 times.
|
217077 | for (blk = 0; blk < 6; blk++) { |
247 | 186066 | s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] * (1 << 8); | |
248 | } | ||
249 | } else { | ||
250 | /* Gain Adaptive Quantization */ | ||
251 | int gbits, log_gain; | ||
252 |
4/4✓ Branch 0 taken 34112 times.
✓ Branch 1 taken 2027 times.
✓ Branch 2 taken 33205 times.
✓ Branch 3 taken 907 times.
|
36139 | if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) { |
253 | 33205 | log_gain = gaq_gain[gs++]; | |
254 | } else { | ||
255 | 2934 | log_gain = 0; | |
256 | } | ||
257 | 36139 | gbits = bits - log_gain; | |
258 | |||
259 |
2/2✓ Branch 0 taken 216834 times.
✓ Branch 1 taken 36139 times.
|
252973 | for (blk = 0; blk < 6; blk++) { |
260 | 216834 | int mant = get_sbits(gbc, gbits); | |
261 |
4/4✓ Branch 0 taken 101928 times.
✓ Branch 1 taken 114906 times.
✓ Branch 2 taken 24448 times.
✓ Branch 3 taken 77480 times.
|
216834 | if (log_gain && mant == -(1 << (gbits-1))) { |
262 | /* large mantissa */ | ||
263 | int b; | ||
264 | 24448 | int mbits = bits - (2 - log_gain); | |
265 | 24448 | mant = get_sbits(gbc, mbits); | |
266 | 24448 | mant = ((unsigned)mant) << (23 - (mbits - 1)); | |
267 | /* remap mantissa value to correct for asymmetric quantization */ | ||
268 |
2/2✓ Branch 0 taken 12146 times.
✓ Branch 1 taken 12302 times.
|
24448 | if (mant >= 0) |
269 | 12146 | b = 1 << (23 - log_gain); | |
270 | else | ||
271 | 12302 | b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] * (1 << 8); | |
272 | 24448 | mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b; | |
273 | } else { | ||
274 | /* small mantissa, no GAQ, or Gk=1 */ | ||
275 | 192386 | mant *= (1 << 24 - bits); | |
276 |
2/2✓ Branch 0 taken 114906 times.
✓ Branch 1 taken 77480 times.
|
192386 | if (!log_gain) { |
277 | /* remap mantissa value for no GAQ or Gk=1 */ | ||
278 | 114906 | mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15; | |
279 | } | ||
280 | } | ||
281 | 216834 | s->pre_mantissa[ch][bin][blk] = mant; | |
282 | } | ||
283 | } | ||
284 | 99072 | idct6(s->pre_mantissa[ch][bin]); | |
285 | } | ||
286 | 810 | } | |
287 | |||
288 | 1201 | static int ff_eac3_parse_header(AC3DecodeContext *s) | |
289 | { | ||
290 | int i, blk, ch; | ||
291 | int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data; | ||
292 | int parse_transient_proc_info; | ||
293 | int num_cpl_blocks; | ||
294 | 1201 | GetBitContext *gbc = &s->gbc; | |
295 | |||
296 | /* An E-AC-3 stream can have multiple independent streams which the | ||
297 | application can select from. each independent stream can also contain | ||
298 | dependent streams which are used to add or replace channels. */ | ||
299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) { |
300 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n"); | |
301 | ✗ | return AAC_AC3_PARSE_ERROR_FRAME_TYPE; | |
302 | } | ||
303 | |||
304 | /* The substream id indicates which substream this frame belongs to. each | ||
305 | independent stream has its own substream id, and the dependent streams | ||
306 | associated to an independent stream have matching substream id's. */ | ||
307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->substreamid) { |
308 | /* only decode substream with id=0. skip any additional substreams. */ | ||
309 | ✗ | if (!s->eac3_subsbtreamid_found) { | |
310 | ✗ | s->eac3_subsbtreamid_found = 1; | |
311 | ✗ | avpriv_request_sample(s->avctx, "Additional substreams"); | |
312 | } | ||
313 | ✗ | return AAC_AC3_PARSE_ERROR_FRAME_TYPE; | |
314 | } | ||
315 | |||
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) { |
317 | /* The E-AC-3 specification does not tell how to handle reduced sample | ||
318 | rates in bit allocation. The best assumption would be that it is | ||
319 | handled like AC-3 DolbyNet, but we cannot be sure until we have a | ||
320 | sample which utilizes this feature. */ | ||
321 | ✗ | avpriv_request_sample(s->avctx, "Reduced sampling rate"); | |
322 | ✗ | return AVERROR_PATCHWELCOME; | |
323 | } | ||
324 | 1201 | skip_bits(gbc, 5); // skip bitstream id | |
325 | |||
326 | /* volume control params */ | ||
327 |
3/4✓ Branch 0 taken 2402 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1201 times.
✓ Branch 3 taken 1201 times.
|
2402 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { |
328 | 1201 | s->dialog_normalization[i] = -get_bits(gbc, 5); | |
329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->dialog_normalization[i] == 0) { |
330 | ✗ | s->dialog_normalization[i] = -31; | |
331 | } | ||
332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->target_level != 0) { |
333 | ✗ | s->level_gain[i] = powf(2.0f, | |
334 | ✗ | (float)(s->target_level - s->dialog_normalization[i])/6.0f); | |
335 | } | ||
336 | 1201 | s->compression_exists[i] = get_bits1(gbc); | |
337 |
2/2✓ Branch 0 taken 927 times.
✓ Branch 1 taken 274 times.
|
1201 | if (s->compression_exists[i]) { |
338 | 927 | s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8)); | |
339 | } | ||
340 | } | ||
341 | |||
342 | /* dependent stream channel map */ | ||
343 |
2/2✓ Branch 0 taken 159 times.
✓ Branch 1 taken 1042 times.
|
1201 | if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) { |
344 |
1/2✓ Branch 1 taken 159 times.
✗ Branch 2 not taken.
|
159 | if (get_bits1(gbc)) { |
345 | 159 | int64_t channel_layout = 0; | |
346 | 159 | int channel_map = get_bits(gbc, 16); | |
347 | 159 | av_log(s->avctx, AV_LOG_DEBUG, "channel_map: %0X\n", channel_map); | |
348 | |||
349 |
2/2✓ Branch 0 taken 2544 times.
✓ Branch 1 taken 159 times.
|
2703 | for (i = 0; i < 16; i++) |
350 |
2/2✓ Branch 0 taken 477 times.
✓ Branch 1 taken 2067 times.
|
2544 | if (channel_map & (1 << (EAC3_MAX_CHANNELS - i - 1))) |
351 | 477 | channel_layout |= ff_eac3_custom_channel_map_locations[i][1]; | |
352 | |||
353 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
|
159 | if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) { |
354 | ✗ | return AVERROR_INVALIDDATA; | |
355 | } | ||
356 | 159 | s->channel_map = channel_map; | |
357 | } | ||
358 | } | ||
359 | |||
360 | /* mixing metadata */ | ||
361 |
2/2✓ Branch 1 taken 637 times.
✓ Branch 2 taken 564 times.
|
1201 | if (get_bits1(gbc)) { |
362 | /* center and surround mix levels */ | ||
363 |
2/2✓ Branch 0 taken 146 times.
✓ Branch 1 taken 491 times.
|
637 | if (s->channel_mode > AC3_CHMODE_STEREO) { |
364 | 146 | s->preferred_downmix = get_bits(gbc, 2); | |
365 |
1/2✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
|
146 | if (s->channel_mode & 1) { |
366 | /* if three front channels exist */ | ||
367 | 146 | s->center_mix_level_ltrt = get_bits(gbc, 3); | |
368 | 146 | s->center_mix_level = get_bits(gbc, 3); | |
369 | } | ||
370 |
1/2✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
|
146 | if (s->channel_mode & 4) { |
371 | /* if a surround channel exists */ | ||
372 | 146 | s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); | |
373 | 146 | s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); | |
374 | } | ||
375 | } | ||
376 | |||
377 | /* lfe mix level */ | ||
378 |
3/4✓ Branch 0 taken 146 times.
✓ Branch 1 taken 491 times.
✓ Branch 3 taken 146 times.
✗ Branch 4 not taken.
|
637 | if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) { |
379 | 146 | s->lfe_mix_level = get_bits(gbc, 5); | |
380 | } | ||
381 | |||
382 | /* info for mixing with other streams and substreams */ | ||
383 |
1/2✓ Branch 0 taken 637 times.
✗ Branch 1 not taken.
|
637 | if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) { |
384 |
3/4✓ Branch 0 taken 1274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 637 times.
✓ Branch 3 taken 637 times.
|
1274 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { |
385 | // TODO: apply program scale factor | ||
386 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 637 times.
|
637 | if (get_bits1(gbc)) { |
387 | ✗ | skip_bits(gbc, 6); // skip program scale factor | |
388 | } | ||
389 | } | ||
390 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 637 times.
|
637 | if (get_bits1(gbc)) { |
391 | ✗ | skip_bits(gbc, 6); // skip external program scale factor | |
392 | } | ||
393 | /* skip mixing parameter data */ | ||
394 |
1/4✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 637 times.
|
637 | switch(get_bits(gbc, 2)) { |
395 | ✗ | case 1: skip_bits(gbc, 5); break; | |
396 | ✗ | case 2: skip_bits(gbc, 12); break; | |
397 | ✗ | case 3: { | |
398 | ✗ | int mix_data_size = (get_bits(gbc, 5) + 2) << 3; | |
399 | ✗ | skip_bits_long(gbc, mix_data_size); | |
400 | ✗ | break; | |
401 | } | ||
402 | } | ||
403 | /* skip pan information for mono or dual mono source */ | ||
404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 637 times.
|
637 | if (s->channel_mode < AC3_CHMODE_STEREO) { |
405 | ✗ | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { | |
406 | ✗ | if (get_bits1(gbc)) { | |
407 | /* note: this is not in the ATSC A/52B specification | ||
408 | reference: ETSI TS 102 366 V1.1.1 | ||
409 | section: E.1.3.1.25 */ | ||
410 | ✗ | skip_bits(gbc, 8); // skip pan mean direction index | |
411 | ✗ | skip_bits(gbc, 6); // skip reserved paninfo bits | |
412 | } | ||
413 | } | ||
414 | } | ||
415 | /* skip mixing configuration information */ | ||
416 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 637 times.
|
637 | if (get_bits1(gbc)) { |
417 | ✗ | for (blk = 0; blk < s->num_blocks; blk++) { | |
418 | ✗ | if (s->num_blocks == 1 || get_bits1(gbc)) { | |
419 | ✗ | skip_bits(gbc, 5); | |
420 | } | ||
421 | } | ||
422 | } | ||
423 | } | ||
424 | } | ||
425 | |||
426 | /* informational metadata */ | ||
427 |
2/2✓ Branch 1 taken 768 times.
✓ Branch 2 taken 433 times.
|
1201 | if (get_bits1(gbc)) { |
428 | 768 | s->bitstream_mode = get_bits(gbc, 3); | |
429 | 768 | skip_bits(gbc, 2); // skip copyright bit and original bitstream bit | |
430 |
2/2✓ Branch 0 taken 622 times.
✓ Branch 1 taken 146 times.
|
768 | if (s->channel_mode == AC3_CHMODE_STEREO) { |
431 | 622 | s->dolby_surround_mode = get_bits(gbc, 2); | |
432 | 622 | s->dolby_headphone_mode = get_bits(gbc, 2); | |
433 | } | ||
434 |
2/2✓ Branch 0 taken 146 times.
✓ Branch 1 taken 622 times.
|
768 | if (s->channel_mode >= AC3_CHMODE_2F2R) { |
435 | 146 | s->dolby_surround_ex_mode = get_bits(gbc, 2); | |
436 | } | ||
437 |
3/4✓ Branch 0 taken 1536 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 768 times.
✓ Branch 3 taken 768 times.
|
1536 | for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { |
438 |
1/2✓ Branch 1 taken 768 times.
✗ Branch 2 not taken.
|
768 | if (get_bits1(gbc)) { |
439 | 768 | skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type | |
440 | } | ||
441 | } | ||
442 |
1/2✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
|
768 | if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) { |
443 | 768 | skip_bits1(gbc); // skip source sample rate code | |
444 | } | ||
445 | } | ||
446 | |||
447 | /* converter synchronization flag | ||
448 | If frames are less than six blocks, this bit should be turned on | ||
449 | once every 6 blocks to indicate the start of a frame set. | ||
450 | reference: RFC 4598, Section 2.1.3 Frame Sets */ | ||
451 |
4/4✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 944 times.
|
1201 | if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) { |
452 | 98 | skip_bits1(gbc); // skip converter synchronization flag | |
453 | } | ||
454 | |||
455 | /* original frame size code if this stream was converted from AC-3 */ | ||
456 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT && |
457 | ✗ | (s->num_blocks == 6 || get_bits1(gbc))) { | |
458 | ✗ | skip_bits(gbc, 6); // skip frame size code | |
459 | } | ||
460 | |||
461 | /* additional bitstream info */ | ||
462 |
2/2✓ Branch 1 taken 159 times.
✓ Branch 2 taken 1042 times.
|
1201 | if (get_bits1(gbc)) { |
463 | 159 | int addbsil = get_bits(gbc, 6); | |
464 |
2/2✓ Branch 0 taken 318 times.
✓ Branch 1 taken 159 times.
|
477 | for (i = 0; i < addbsil + 1; i++) { |
465 | 318 | skip_bits(gbc, 8); // skip additional bit stream info | |
466 | } | ||
467 | } | ||
468 | |||
469 | /* audio frame syntax flags, strategy data, and per-frame data */ | ||
470 | |||
471 |
2/2✓ Branch 0 taken 1103 times.
✓ Branch 1 taken 98 times.
|
1201 | if (s->num_blocks == 6) { |
472 | 1103 | ac3_exponent_strategy = get_bits1(gbc); | |
473 | 1103 | parse_aht_info = get_bits1(gbc); | |
474 | } else { | ||
475 | /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and | ||
476 | do not use AHT */ | ||
477 | 98 | ac3_exponent_strategy = 1; | |
478 | 98 | parse_aht_info = 0; | |
479 | } | ||
480 | |||
481 | 1201 | s->snr_offset_strategy = get_bits(gbc, 2); | |
482 | 1201 | parse_transient_proc_info = get_bits1(gbc); | |
483 | |||
484 | 1201 | s->block_switch_syntax = get_bits1(gbc); | |
485 |
1/2✓ Branch 0 taken 1201 times.
✗ Branch 1 not taken.
|
1201 | if (!s->block_switch_syntax) |
486 | 1201 | memset(s->block_switch, 0, sizeof(s->block_switch)); | |
487 | |||
488 | 1201 | s->dither_flag_syntax = get_bits1(gbc); | |
489 |
1/2✓ Branch 0 taken 1201 times.
✗ Branch 1 not taken.
|
1201 | if (!s->dither_flag_syntax) { |
490 |
2/2✓ Branch 0 taken 3158 times.
✓ Branch 1 taken 1201 times.
|
4359 | for (ch = 1; ch <= s->fbw_channels; ch++) |
491 | 3158 | s->dither_flag[ch] = 1; | |
492 | } | ||
493 | 1201 | s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0; | |
494 | |||
495 | 1201 | s->bit_allocation_syntax = get_bits1(gbc); | |
496 |
2/2✓ Branch 0 taken 915 times.
✓ Branch 1 taken 286 times.
|
1201 | if (!s->bit_allocation_syntax) { |
497 | /* set default bit allocation parameters */ | ||
498 | 915 | s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2]; | |
499 | 915 | s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1]; | |
500 | 915 | s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1]; | |
501 | 915 | s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2]; | |
502 | 915 | s->bit_alloc_params.floor = ff_ac3_floor_tab [7]; | |
503 | } | ||
504 | |||
505 | 1201 | s->fast_gain_syntax = get_bits1(gbc); | |
506 | 1201 | s->dba_syntax = get_bits1(gbc); | |
507 | 1201 | s->skip_syntax = get_bits1(gbc); | |
508 | 1201 | parse_spx_atten_data = get_bits1(gbc); | |
509 | |||
510 | /* coupling strategy occurrence and coupling use per block */ | ||
511 | 1201 | num_cpl_blocks = 0; | |
512 |
1/2✓ Branch 0 taken 1201 times.
✗ Branch 1 not taken.
|
1201 | if (s->channel_mode > 1) { |
513 |
2/2✓ Branch 0 taken 6716 times.
✓ Branch 1 taken 1201 times.
|
7917 | for (blk = 0; blk < s->num_blocks; blk++) { |
514 |
3/4✓ Branch 0 taken 5515 times.
✓ Branch 1 taken 1201 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5515 times.
|
6716 | s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc)); |
515 |
2/2✓ Branch 0 taken 1201 times.
✓ Branch 1 taken 5515 times.
|
6716 | if (s->cpl_strategy_exists[blk]) { |
516 | 1201 | s->cpl_in_use[blk] = get_bits1(gbc); | |
517 | } else { | ||
518 | 5515 | s->cpl_in_use[blk] = s->cpl_in_use[blk-1]; | |
519 | } | ||
520 | 6716 | num_cpl_blocks += s->cpl_in_use[blk]; | |
521 | } | ||
522 | } else { | ||
523 | ✗ | memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use)); | |
524 | } | ||
525 | |||
526 | /* exponent strategy data */ | ||
527 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 1103 times.
|
1201 | if (ac3_exponent_strategy) { |
528 | /* AC-3-style exponent strategy syntax */ | ||
529 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 98 times.
|
196 | for (blk = 0; blk < s->num_blocks; blk++) { |
530 |
2/2✓ Branch 0 taken 490 times.
✓ Branch 1 taken 98 times.
|
588 | for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) { |
531 | 490 | s->exp_strategy[blk][ch] = get_bits(gbc, 2); | |
532 | } | ||
533 | } | ||
534 | } else { | ||
535 | /* LUT-based exponent strategy syntax */ | ||
536 |
5/6✓ Branch 0 taken 1103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 698 times.
✓ Branch 3 taken 405 times.
✓ Branch 4 taken 3073 times.
✓ Branch 5 taken 1103 times.
|
4176 | for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) { |
537 | 3073 | int frmchexpstr = get_bits(gbc, 5); | |
538 |
2/2✓ Branch 0 taken 18438 times.
✓ Branch 1 taken 3073 times.
|
21511 | for (blk = 0; blk < 6; blk++) { |
539 | 18438 | s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk]; | |
540 | } | ||
541 | } | ||
542 | } | ||
543 | /* LFE exponent strategy */ | ||
544 |
2/2✓ Branch 0 taken 146 times.
✓ Branch 1 taken 1055 times.
|
1201 | if (s->lfe_on) { |
545 |
2/2✓ Branch 0 taken 386 times.
✓ Branch 1 taken 146 times.
|
532 | for (blk = 0; blk < s->num_blocks; blk++) { |
546 | 386 | s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc); | |
547 | } | ||
548 | } | ||
549 | /* original exponent strategies if this stream was converted from AC-3 */ | ||
550 |
2/2✓ Branch 0 taken 1042 times.
✓ Branch 1 taken 159 times.
|
1201 | if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && |
551 |
4/4✓ Branch 0 taken 98 times.
✓ Branch 1 taken 944 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 82 times.
|
1042 | (s->num_blocks == 6 || get_bits1(gbc))) { |
552 | 960 | skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy | |
553 | } | ||
554 | |||
555 | /* determine which channels use AHT */ | ||
556 |
2/2✓ Branch 0 taken 445 times.
✓ Branch 1 taken 756 times.
|
1201 | if (parse_aht_info) { |
557 | /* For AHT to be used, all non-zero blocks must reuse exponents from | ||
558 | the first block. Furthermore, for AHT to be used in the coupling | ||
559 | channel, all blocks must use coupling and use the same coupling | ||
560 | strategy. */ | ||
561 | 445 | s->channel_uses_aht[CPL_CH]=0; | |
562 |
2/2✓ Branch 0 taken 1082 times.
✓ Branch 1 taken 445 times.
|
1527 | for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) { |
563 | 1082 | int use_aht = 1; | |
564 |
2/2✓ Branch 0 taken 4704 times.
✓ Branch 1 taken 810 times.
|
5514 | for (blk = 1; blk < 6; blk++) { |
565 |
3/4✓ Branch 0 taken 4432 times.
✓ Branch 1 taken 272 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4432 times.
|
4704 | if ((s->exp_strategy[blk][ch] != EXP_REUSE) || |
566 | ✗ | (!ch && s->cpl_strategy_exists[blk])) { | |
567 | 272 | use_aht = 0; | |
568 | 272 | break; | |
569 | } | ||
570 | } | ||
571 |
3/4✓ Branch 0 taken 810 times.
✓ Branch 1 taken 272 times.
✓ Branch 3 taken 810 times.
✗ Branch 4 not taken.
|
1082 | s->channel_uses_aht[ch] = use_aht && get_bits1(gbc); |
572 | } | ||
573 | } else { | ||
574 | 756 | memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht)); | |
575 | } | ||
576 | |||
577 | /* per-frame SNR offset */ | ||
578 |
1/2✓ Branch 0 taken 1201 times.
✗ Branch 1 not taken.
|
1201 | if (!s->snr_offset_strategy) { |
579 | 1201 | int csnroffst = (get_bits(gbc, 6) - 15) << 4; | |
580 | 1201 | int snroffst = (csnroffst + get_bits(gbc, 4)) << 2; | |
581 |
2/2✓ Branch 0 taken 4505 times.
✓ Branch 1 taken 1201 times.
|
5706 | for (ch = 0; ch <= s->channels; ch++) |
582 | 4505 | s->snr_offset[ch] = snroffst; | |
583 | } | ||
584 | |||
585 | /* transient pre-noise processing data */ | ||
586 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1201 times.
|
1201 | if (parse_transient_proc_info) { |
587 | ✗ | for (ch = 1; ch <= s->fbw_channels; ch++) { | |
588 | ✗ | if (get_bits1(gbc)) { // channel in transient processing | |
589 | ✗ | skip_bits(gbc, 10); // skip transient processing location | |
590 | ✗ | skip_bits(gbc, 8); // skip transient processing length | |
591 | } | ||
592 | } | ||
593 | } | ||
594 | |||
595 | /* spectral extension attenuation data */ | ||
596 |
2/2✓ Branch 0 taken 3158 times.
✓ Branch 1 taken 1201 times.
|
4359 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
597 |
4/4✓ Branch 0 taken 1222 times.
✓ Branch 1 taken 1936 times.
✓ Branch 3 taken 1049 times.
✓ Branch 4 taken 173 times.
|
3158 | if (parse_spx_atten_data && get_bits1(gbc)) { |
598 | 1049 | s->spx_atten_code[ch] = get_bits(gbc, 5); | |
599 | } else { | ||
600 | 2109 | s->spx_atten_code[ch] = -1; | |
601 | } | ||
602 | } | ||
603 | |||
604 | /* block start information */ | ||
605 |
3/4✓ Branch 0 taken 1103 times.
✓ Branch 1 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1103 times.
|
1201 | if (s->num_blocks > 1 && get_bits1(gbc)) { |
606 | /* reference: Section E2.3.2.27 | ||
607 | nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame))) | ||
608 | The spec does not say what this data is or what it's used for. | ||
609 | It is likely the offset of each block within the frame. */ | ||
610 | ✗ | int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2)); | |
611 | ✗ | skip_bits_long(gbc, block_start_bits); | |
612 | ✗ | avpriv_request_sample(s->avctx, "Block start info"); | |
613 | } | ||
614 | |||
615 | /* syntax state initialization */ | ||
616 |
2/2✓ Branch 0 taken 3158 times.
✓ Branch 1 taken 1201 times.
|
4359 | for (ch = 1; ch <= s->fbw_channels; ch++) { |
617 | 3158 | s->first_spx_coords[ch] = 1; | |
618 | 3158 | s->first_cpl_coords[ch] = 1; | |
619 | } | ||
620 | 1201 | s->first_cpl_leak = 1; | |
621 | |||
622 | 1201 | return 0; | |
623 | } | ||
624 |