FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ac3dec.c
Date: 2026-08-28 18:35:09
Exec Total Coverage
Lines: 834 999 83.5%
Functions: 24 25 96.0%
Branches: 562 717 78.4%

Line Branch Exec Source
1 /*
2 * AC-3 Audio Decoder
3 * This code was developed as part of Google Summer of Code 2006.
4 * E-AC-3 support was added as part of Google Summer of Code 2007.
5 *
6 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com)
7 * Copyright (c) 2007-2008 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
8 * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include "config_components.h"
28
29 #include <stdio.h>
30 #include <stddef.h>
31 #include <math.h>
32 #include <string.h>
33
34 #include "libavutil/attributes.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/crc.h"
37 #include "libavutil/downmix_info.h"
38 #include "libavutil/intmath.h"
39 #include "libavutil/mem.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/thread.h"
42 #include "bswapdsp.h"
43 #include "ac3_parser_internal.h"
44 #include "ac3dec.h"
45 #include "ac3dec_data.h"
46 #include "ac3defs.h"
47 #include "decode.h"
48 #include "kbdwin.h"
49
50 #if (!USE_FIXED)
51 /** dynamic range table. converts codes to scale factors. */
52 static float dynamic_range_tab[256];
53 float ff_ac3_heavy_dynamic_range_tab[256];
54 /** scale factor for each decoded exponent: 2^-exp */
55 static const float scale_factors[25] = {
56 0x1p-0f, 0x1p-1f, 0x1p-2f, 0x1p-3f, 0x1p-4f,
57 0x1p-5f, 0x1p-6f, 0x1p-7f, 0x1p-8f, 0x1p-9f,
58 0x1p-10f, 0x1p-11f, 0x1p-12f, 0x1p-13f, 0x1p-14f,
59 0x1p-15f, 0x1p-16f, 0x1p-17f, 0x1p-18f, 0x1p-19f,
60 0x1p-20f, 0x1p-21f, 0x1p-22f, 0x1p-23f, 0x1p-24f,
61 };
62
63 /*
64 * Initialize tables at runtime.
65 */
66 51 static av_cold void ac3_float_tables_init(void)
67 {
68 /* generate dynamic range table
69 reference: Section 7.7.1 Dynamic Range Control */
70
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 51 times.
13107 for (int i = 0; i < 256; i++) {
71 13056 int v = (i >> 5) - ((i >> 7) << 3) - 5;
72 13056 dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
73 }
74
75 /* generate compr dynamic range table
76 reference: Section 7.7.2 Heavy Compression */
77
2/2
✓ Branch 0 taken 13056 times.
✓ Branch 1 taken 51 times.
13107 for (int i = 0; i < 256; i++) {
78 13056 int v = (i >> 4) - ((i >> 7) << 4) - 4;
79 13056 ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
80 }
81 51 ff_ac3_init_static();
82 51 }
83 #endif
84
85 439 static void ac3_downmix(AVCodecContext *avctx)
86 {
87 439 AC3DecodeContext *s = avctx->priv_data;
88 439 const AVChannelLayout mono = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
89 439 const AVChannelLayout stereo = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
90
91 /* allow downmixing to stereo or mono */
92
4/4
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 383 times.
826 if (avctx->ch_layout.nb_channels > 1 &&
93 387 !av_channel_layout_compare(&s->downmix_layout, &mono)) {
94 4 av_channel_layout_uninit(&avctx->ch_layout);
95 4 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
96
4/4
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 339 times.
777 } else if (avctx->ch_layout.nb_channels > 2 &&
97 342 !av_channel_layout_compare(&s->downmix_layout, &stereo)) {
98 3 av_channel_layout_uninit(&avctx->ch_layout);
99 3 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
100 }
101 439 }
102
103 /**
104 * AVCodec initialization
105 */
106 120 static av_cold int ac3_decode_init(AVCodecContext *avctx)
107 {
108 120 AC3DecodeContext *s = avctx->priv_data;
109 120 const float scale = 1.0f;
110 int i, ret;
111
112 120 s->avctx = avctx;
113
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if ((ret = av_tx_init(&s->tx_128, &s->tx_fn_128, IMDCT_TYPE, 1, 128, &scale, 0)))
115 return ret;
116
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if ((ret = av_tx_init(&s->tx_256, &s->tx_fn_256, IMDCT_TYPE, 1, 256, &scale, 0)))
118 return ret;
119
120 120 AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256);
121 120 ff_bswapdsp_init(&s->bdsp);
122
123 #if (USE_FIXED)
124 15 s->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
125 #else
126 105 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
127 #endif
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (!s->fdsp)
129 return AVERROR(ENOMEM);
130
131 120 ff_ac3dsp_init(&s->ac3dsp);
132 120 av_lfg_init(&s->dith_state, 0);
133
134 if (USE_FIXED)
135 15 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
136 else
137 105 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
138
139 120 ac3_downmix(avctx);
140 120 s->downmixed = 1;
141
142
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 120 times.
960 for (i = 0; i < AC3_MAX_CHANNELS; i++) {
143 840 s->xcfptr[i] = s->transform_coeffs[i];
144 840 s->dlyptr[i] = s->delay[i];
145 }
146
147 #if USE_FIXED
148 15 ff_ac3_init_static();
149 #else
150 static AVOnce init_static_once = AV_ONCE_INIT;
151 105 ff_thread_once(&init_static_once, ac3_float_tables_init);
152 #endif
153
154 120 return 0;
155 }
156
157 static av_cold void ac3_decode_flush(AVCodecContext *avctx)
158 {
159 AC3DecodeContext *s = avctx->priv_data;
160
161 memset(&s->frame_type, 0, sizeof(*s) - offsetof(AC3DecodeContext, frame_type));
162
163 AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256);
164 av_lfg_init(&s->dith_state, 0);
165 }
166
167 /**
168 * Common function to parse AC-3 or E-AC-3 frame header
169 */
170 3861 static int parse_frame_header(AC3DecodeContext *s)
171 {
172 AC3HeaderInfo hdr;
173 int err;
174
175 3861 err = ff_ac3_parse_header(&s->gbc, &hdr);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3861 times.
3861 if (err)
177 return err;
178
179 /* get decoding parameters from header info */
180 3861 s->bit_alloc_params.sr_code = hdr.sr_code;
181 3861 s->bitstream_id = hdr.bitstream_id;
182 3861 s->bitstream_mode = hdr.bitstream_mode;
183 3861 s->channel_mode = hdr.channel_mode;
184 3861 s->lfe_on = hdr.lfe_on;
185 3861 s->bit_alloc_params.sr_shift = hdr.sr_shift;
186 3861 s->sample_rate = hdr.sample_rate;
187 3861 s->bit_rate = hdr.bit_rate;
188 3861 s->channels = hdr.channels;
189 3861 s->fbw_channels = s->channels - s->lfe_on;
190 3861 s->lfe_ch = s->fbw_channels + 1;
191 3861 s->frame_size = hdr.frame_size;
192 3861 s->superframe_size += hdr.frame_size;
193 3861 s->preferred_downmix = AC3_DMIXMOD_NOTINDICATED;
194
2/2
✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 2163 times.
3861 if (hdr.bitstream_id <= 10) {
195 1698 s->center_mix_level = hdr.center_mix_level;
196 1698 s->surround_mix_level = hdr.surround_mix_level;
197 }
198 3861 s->center_mix_level_ltrt = 4; // -3.0dB
199 3861 s->surround_mix_level_ltrt = 4; // -3.0dB
200 3861 s->lfe_mix_level_exists = 0;
201 3861 s->num_blocks = hdr.num_blocks;
202 3861 s->frame_type = hdr.frame_type;
203 3861 s->substreamid = hdr.substreamid;
204 3861 s->dolby_surround_mode = hdr.dolby_surround_mode;
205 3861 s->dolby_surround_ex_mode = AC3_DSUREXMOD_NOTINDICATED;
206 3861 s->dolby_headphone_mode = AC3_DHEADPHONMOD_NOTINDICATED;
207
208
2/2
✓ Branch 0 taken 908 times.
✓ Branch 1 taken 2953 times.
3861 if (s->lfe_on) {
209 908 s->start_freq[s->lfe_ch] = 0;
210 908 s->end_freq[s->lfe_ch] = 7;
211 908 s->num_exp_groups[s->lfe_ch] = 2;
212 908 s->channel_in_cpl[s->lfe_ch] = 0;
213 }
214
215
2/2
✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 2163 times.
3861 if (s->bitstream_id <= 10) {
216 1698 s->eac3 = 0;
217 1698 s->snr_offset_strategy = 2;
218 1698 s->block_switch_syntax = 1;
219 1698 s->dither_flag_syntax = 1;
220 1698 s->bit_allocation_syntax = 1;
221 1698 s->fast_gain_syntax = 0;
222 1698 s->first_cpl_leak = 0;
223 1698 s->dba_syntax = 1;
224 1698 s->skip_syntax = 1;
225 1698 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
226 /* volume control params */
227
3/4
✓ Branch 0 taken 3396 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1698 times.
✓ Branch 3 taken 1698 times.
3396 for (int i = 0; i < (s->channel_mode ? 1 : 2); i++) {
228 1698 s->dialog_normalization[i] = hdr.dialog_normalization[i];
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1698 times.
1698 if (s->dialog_normalization[i] == 0) {
230 s->dialog_normalization[i] = -31;
231 }
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1698 times.
1698 if (s->target_level != 0) {
233 s->level_gain[i] = powf(2.0f,
234 (float)(s->target_level - s->dialog_normalization[i])/6.0f);
235 }
236 1698 s->compression_exists[i] = hdr.compression_exists[i];
237
2/2
✓ Branch 0 taken 1148 times.
✓ Branch 1 taken 550 times.
1698 if (s->compression_exists[i]) {
238 1148 s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(hdr.heavy_dynamic_range[i]);
239 }
240 }
241 1698 return 0;
242 } else if (CONFIG_EAC3_DECODER) {
243 2163 s->eac3 = 1;
244 2163 return ff_eac3_parse_header(s, &hdr);
245 } else {
246 av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n");
247 return AVERROR(ENOSYS);
248 }
249 }
250
251 /**
252 * Set stereo downmixing coefficients based on frame header info.
253 * reference: Section 7.8.2 Downmixing Into Two Channels
254 */
255 417 static int set_downmix_coeffs(AC3DecodeContext *s)
256 {
257 int i;
258 417 float cmix = ff_ac3_gain_levels[s-> center_mix_level];
259 417 float smix = ff_ac3_gain_levels[s->surround_mix_level];
260 float norm0, norm1;
261 float downmix_coeffs[2][AC3_MAX_CHANNELS];
262
263
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 403 times.
417 if (!s->downmix_coeffs[0]) {
264 14 s->downmix_coeffs[0] = av_malloc_array(2 * AC3_MAX_CHANNELS,
265 sizeof(**s->downmix_coeffs));
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!s->downmix_coeffs[0])
267 return AVERROR(ENOMEM);
268 14 s->downmix_coeffs[1] = s->downmix_coeffs[0] + AC3_MAX_CHANNELS;
269 }
270
271
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 417 times.
2313 for (i = 0; i < s->fbw_channels; i++) {
272 1896 downmix_coeffs[0][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][0]];
273 1896 downmix_coeffs[1][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][1]];
274 }
275
2/4
✓ Branch 0 taken 417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 417 times.
✗ Branch 3 not taken.
417 if (s->channel_mode > 1 && s->channel_mode & 1) {
276 417 downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
277 }
278
3/4
✓ Branch 0 taken 417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 189 times.
✓ Branch 3 taken 228 times.
417 if (s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
279 189 int nf = s->channel_mode - 2;
280 189 downmix_coeffs[0][nf] = downmix_coeffs[1][nf] = smix * LEVEL_MINUS_3DB;
281 }
282
3/4
✓ Branch 0 taken 417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 189 times.
417 if (s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
283 228 int nf = s->channel_mode - 4;
284 228 downmix_coeffs[0][nf] = downmix_coeffs[1][nf+1] = smix;
285 }
286
287 /* renormalize */
288 417 norm0 = norm1 = 0.0;
289
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 417 times.
2313 for (i = 0; i < s->fbw_channels; i++) {
290 1896 norm0 += downmix_coeffs[0][i];
291 1896 norm1 += downmix_coeffs[1][i];
292 }
293 417 norm0 = 1.0f / norm0;
294 417 norm1 = 1.0f / norm1;
295
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 417 times.
2313 for (i = 0; i < s->fbw_channels; i++) {
296 1896 downmix_coeffs[0][i] *= norm0;
297 1896 downmix_coeffs[1][i] *= norm1;
298 }
299
300
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 177 times.
417 if (s->output_mode == AC3_CHMODE_MONO) {
301
2/2
✓ Branch 0 taken 1074 times.
✓ Branch 1 taken 240 times.
1314 for (i = 0; i < s->fbw_channels; i++)
302 1074 downmix_coeffs[0][i] = (downmix_coeffs[0][i] +
303 1074 downmix_coeffs[1][i]) * LEVEL_MINUS_3DB;
304 }
305
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 417 times.
2313 for (i = 0; i < s->fbw_channels; i++) {
306 1896 s->downmix_coeffs[0][i] = FIXR12(downmix_coeffs[0][i]);
307 1896 s->downmix_coeffs[1][i] = FIXR12(downmix_coeffs[1][i]);
308 }
309
310 417 return 0;
311 }
312
313 /**
314 * Decode the grouped exponents according to exponent strategy.
315 * reference: Section 7.1.3 Exponent Decoding
316 */
317 20113 static int decode_exponents(AC3DecodeContext *s,
318 GetBitContext *gbc, int exp_strategy, int ngrps,
319 uint8_t absexp, int8_t *dexps)
320 {
321 int i, j, grp, group_size;
322 int dexp[256];
323 int expacc, prevexp;
324
325 /* unpack groups */
326 20113 group_size = exp_strategy + (exp_strategy == EXP_D45);
327
2/2
✓ Branch 0 taken 738079 times.
✓ Branch 1 taken 20113 times.
758192 for (grp = 0, i = 0; grp < ngrps; grp++) {
328 738079 expacc = get_bits(gbc, 7);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 738079 times.
738079 if (expacc >= 125) {
330 av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc);
331 return AVERROR_INVALIDDATA;
332 }
333 738079 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][0];
334 738079 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][1];
335 738079 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][2];
336 }
337
338 /* convert to absolute exps and expand groups */
339 20113 prevexp = absexp;
340
2/2
✓ Branch 0 taken 2214237 times.
✓ Branch 1 taken 20113 times.
2234350 for (i = 0, j = 0; i < ngrps * 3; i++) {
341 2214237 prevexp += dexp[i] - 2;
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2214237 times.
2214237 if (prevexp > 24U) {
343 av_log(s->avctx, AV_LOG_ERROR, "exponent %d is out-of-range\n", prevexp);
344 return AVERROR_INVALIDDATA;
345 }
346
3/4
✓ Branch 0 taken 153177 times.
✓ Branch 1 taken 265038 times.
✓ Branch 2 taken 1796022 times.
✗ Branch 3 not taken.
2214237 switch (group_size) {
347 153177 case 4: dexps[j++] = prevexp;
348 153177 dexps[j++] = prevexp;
349 av_fallthrough;
350 418215 case 2: dexps[j++] = prevexp;
351 av_fallthrough;
352 2214237 case 1: dexps[j++] = prevexp;
353 }
354 }
355 20113 return 0;
356 }
357
358 /**
359 * Generate transform coefficients for each coupled channel in the coupling
360 * range using the coupling coefficients and coupling coordinates.
361 * reference: Section 7.4.3 Coupling Coordinate Format
362 */
363 9050 static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
364 {
365 int bin, band, ch;
366
367 9050 bin = s->start_freq[CPL_CH];
368
2/2
✓ Branch 0 taken 34166 times.
✓ Branch 1 taken 9050 times.
43216 for (band = 0; band < s->num_cpl_bands; band++) {
369 34166 int band_start = bin;
370 34166 int band_end = bin + s->cpl_band_sizes[band];
371
2/2
✓ Branch 0 taken 78646 times.
✓ Branch 1 taken 34166 times.
112812 for (ch = 1; ch <= s->fbw_channels; ch++) {
372
1/2
✓ Branch 0 taken 78646 times.
✗ Branch 1 not taken.
78646 if (s->channel_in_cpl[ch]) {
373 #if USE_FIXED
374 19112 int cpl_coord = s->cpl_coords[ch][band] << 5;
375 #else
376 59534 float cpl_coord = s->cpl_coords[ch][band] * (1.0f / (1 << 23));
377 #endif
378
2/2
✓ Branch 0 taken 1758600 times.
✓ Branch 1 taken 78646 times.
1837246 for (bin = band_start; bin < band_end; bin++) {
379 #if USE_FIXED
380 535824 s->coeffs[ch][bin] =
381 535824 MULH(s->coeffs[CPL_CH][bin] * (1 << 4), cpl_coord);
382 #else
383 1222776 s->coeffs[ch][bin] = s->coeffs[CPL_CH][bin] * cpl_coord;
384 #endif
385 }
386
4/4
✓ Branch 0 taken 34166 times.
✓ Branch 1 taken 44480 times.
✓ Branch 2 taken 2880 times.
✓ Branch 3 taken 31286 times.
78646 if (ch == 2 && s->phase_flags[band]) {
387
2/2
✓ Branch 0 taken 41472 times.
✓ Branch 1 taken 2880 times.
44352 for (bin = band_start; bin < band_end; bin++)
388 41472 s->coeffs[2][bin] = -s->coeffs[2][bin];
389 }
390 }
391 }
392 34166 bin = band_end;
393 }
394 9050 }
395
396 /**
397 * Grouped mantissas for 3-level 5-level and 11-level quantization
398 */
399 typedef struct mant_groups {
400 int b1_mant[2];
401 int b2_mant[2];
402 int b4_mant;
403 int b1;
404 int b2;
405 int b4;
406 } mant_groups;
407
408 10950535 static av_always_inline INTFLOAT dequantize_coeff(int mantissa, int exponent,
409 int coeff_bits)
410 {
411 #if USE_FIXED
412 4494901 return (mantissa * (1 << coeff_bits)) >> exponent;
413 #else
414 6455634 return mantissa * scale_factors[exponent];
415 #endif
416 }
417
418 #if USE_FIXED
419 305879 static av_always_inline int dequantize_dexp24_dither(int mantissa)
420 {
421 305879 int scaled = mantissa * (1 << AC3_FIXED_COEFF_BITS);
422 305879 int round = 1 << (AC3_FIXED_EXPONENT_MAX - 1);
423
424 305879 return (scaled + round - (scaled < 0)) >> AC3_FIXED_EXPONENT_MAX;
425 }
426 #endif
427
428 /**
429 * Decode the transform coefficients for a particular channel
430 * reference: Section 7.3 Quantization and Decoding of Mantissas
431 */
432 68204 static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
433 {
434 68204 int start_freq = s->start_freq[ch_index];
435 68204 int end_freq = s->end_freq[ch_index];
436 68204 uint8_t *baps = s->bap[ch_index];
437 68204 int8_t *exps = s->dexps[ch_index];
438 68204 INTFLOAT *coeffs = s->coeffs[ch_index];
439
4/4
✓ Branch 0 taken 59154 times.
✓ Branch 1 taken 9050 times.
✓ Branch 2 taken 55124 times.
✓ Branch 3 taken 4030 times.
68204 int dither = (ch_index == CPL_CH) || s->dither_flag[ch_index];
440 #if USE_FIXED
441 25708 int coeff_bits = fixed_coeff_bits(s);
442 #else
443 42496 int coeff_bits = 0;
444 #endif
445 68204 GetBitContext *gbc = &s->gbc;
446 int freq;
447
448
2/2
✓ Branch 0 taken 10065954 times.
✓ Branch 1 taken 68204 times.
10134158 for (freq = start_freq; freq < end_freq; freq++) {
449 10065954 int bap = baps[freq];
450 int mantissa;
451
7/7
✓ Branch 0 taken 3220246 times.
✓ Branch 1 taken 1996443 times.
✓ Branch 2 taken 956377 times.
✓ Branch 3 taken 1044444 times.
✓ Branch 4 taken 669252 times.
✓ Branch 5 taken 573266 times.
✓ Branch 6 taken 1605926 times.
10065954 switch (bap) {
452 3220246 case 0:
453 /* random noise with approximate range of -0.707 to 0.707 */
454
2/2
✓ Branch 0 taken 3215496 times.
✓ Branch 1 taken 4750 times.
3220246 if (dither) {
455 3215496 mantissa = (((av_lfg_get(&s->dith_state)>>8)*181)>>8) - 5931008;
456 #if USE_FIXED
457 /* At dexp 24 the dither is below half a Q0 step. Keep two
458 * fractional bits so it is not truncated to -1 or 0. */
459
4/4
✓ Branch 0 taken 990618 times.
✓ Branch 1 taken 432429 times.
✓ Branch 2 taken 305879 times.
✓ Branch 3 taken 684739 times.
1423047 if (coeff_bits && exps[freq] == AC3_FIXED_EXPONENT_MAX) {
460 305879 coeffs[freq] = dequantize_dexp24_dither(mantissa);
461 305879 continue;
462 }
463 #endif
464 } else {
465 4750 mantissa = 0;
466 }
467 2914367 break;
468 1996443 case 1:
469
2/2
✓ Branch 0 taken 1325259 times.
✓ Branch 1 taken 671184 times.
1996443 if (m->b1) {
470 1325259 m->b1--;
471 1325259 mantissa = m->b1_mant[m->b1];
472 } else {
473 671184 int bits = get_bits(gbc, 5);
474 671184 mantissa = ff_ac3_bap1_mantissas[bits][0];
475 671184 m->b1_mant[1] = ff_ac3_bap1_mantissas[bits][1];
476 671184 m->b1_mant[0] = ff_ac3_bap1_mantissas[bits][2];
477 671184 m->b1 = 2;
478 }
479 1996443 break;
480 956377 case 2:
481
2/2
✓ Branch 0 taken 632076 times.
✓ Branch 1 taken 324301 times.
956377 if (m->b2) {
482 632076 m->b2--;
483 632076 mantissa = m->b2_mant[m->b2];
484 } else {
485 324301 int bits = get_bits(gbc, 7);
486 324301 mantissa = ff_ac3_bap2_mantissas[bits][0];
487 324301 m->b2_mant[1] = ff_ac3_bap2_mantissas[bits][1];
488 324301 m->b2_mant[0] = ff_ac3_bap2_mantissas[bits][2];
489 324301 m->b2 = 2;
490 }
491 956377 break;
492 1044444 case 3:
493 1044444 mantissa = ff_ac3_bap3_mantissas[get_bits(gbc, 3)];
494 1044444 break;
495 669252 case 4:
496
2/2
✓ Branch 0 taken 330282 times.
✓ Branch 1 taken 338970 times.
669252 if (m->b4) {
497 330282 m->b4 = 0;
498 330282 mantissa = m->b4_mant;
499 } else {
500 338970 int bits = get_bits(gbc, 7);
501 338970 mantissa = ff_ac3_bap4_mantissas[bits][0];
502 338970 m->b4_mant = ff_ac3_bap4_mantissas[bits][1];
503 338970 m->b4 = 1;
504 }
505 669252 break;
506 573266 case 5:
507 573266 mantissa = ff_ac3_bap5_mantissas[get_bits(gbc, 4)];
508 573266 break;
509 1605926 default: /* 6 to 15 */
510 /* Shift mantissa and sign-extend it. */
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1605926 times.
1605926 if (bap > 15) {
512 av_log(s->avctx, AV_LOG_ERROR, "bap %d is invalid in plain AC-3\n", bap);
513 bap = 15;
514 }
515 1605926 mantissa = (unsigned)get_sbits(gbc, ff_ac3_quantization_tab[bap]) << (24 - ff_ac3_quantization_tab[bap]);
516 1605926 break;
517 }
518 9760075 coeffs[freq] = dequantize_coeff(mantissa, exps[freq], coeff_bits);
519 }
520 68204 }
521
522 /**
523 * Remove random dithering from coupling range coefficients with zero-bit
524 * mantissas for coupled channels which do not use dithering.
525 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
526 */
527 22144 static void remove_dithering(AC3DecodeContext *s) {
528 int ch, i;
529
530
2/2
✓ Branch 0 taken 64448 times.
✓ Branch 1 taken 22144 times.
86592 for (ch = 1; ch <= s->fbw_channels; ch++) {
531
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 64436 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
64448 if (!s->dither_flag[ch] && s->channel_in_cpl[ch]) {
532 for (i = s->start_freq[CPL_CH]; i < s->end_freq[CPL_CH]; i++) {
533 if (!s->bap[CPL_CH][i])
534 s->coeffs[ch][i] = 0;
535 }
536 }
537 }
538 22144 }
539
540 77936 static inline void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk,
541 int ch, mant_groups *m)
542 {
543
2/2
✓ Branch 0 taken 68204 times.
✓ Branch 1 taken 9732 times.
77936 if (!s->channel_uses_aht[ch]) {
544 68204 ac3_decode_transform_coeffs_ch(s, ch, m);
545 } else {
546 /* if AHT is used, mantissas for all blocks are encoded in the first
547 block of the frame. */
548 int bin;
549
2/2
✓ Branch 0 taken 1622 times.
✓ Branch 1 taken 8110 times.
9732 if (CONFIG_EAC3_DECODER && !blk)
550 1622 ff_eac3_decode_transform_coeffs_aht_ch(s, ch);
551
2/2
✓ Branch 0 taken 1190460 times.
✓ Branch 1 taken 9732 times.
1200192 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
552 1190460 s->coeffs[ch][bin] = dequantize_coeff(
553 1190460 s->pre_mantissa[ch][bin][blk], s->dexps[ch][bin], 0);
554 }
555 }
556 77936 }
557
558 /**
559 * Decode the transform coefficients.
560 */
561 22144 static inline void decode_transform_coeffs(AC3DecodeContext *s, int blk)
562 {
563 int ch, end;
564 22144 int got_cplchan = 0;
565 mant_groups m;
566
567 22144 m.b1 = m.b2 = m.b4 = 0;
568
569
2/2
✓ Branch 0 taken 68886 times.
✓ Branch 1 taken 22144 times.
91030 for (ch = 1; ch <= s->channels; ch++) {
570 /* transform coefficients for full-bandwidth channel */
571 68886 decode_transform_coeffs_ch(s, blk, ch, &m);
572 /* transform coefficients for coupling channel come right after the
573 coefficients for the first coupled channel*/
574
2/2
✓ Branch 0 taken 23266 times.
✓ Branch 1 taken 45620 times.
68886 if (s->channel_in_cpl[ch]) {
575
2/2
✓ Branch 0 taken 9050 times.
✓ Branch 1 taken 14216 times.
23266 if (!got_cplchan) {
576 9050 decode_transform_coeffs_ch(s, blk, CPL_CH, &m);
577 9050 calc_transform_coeffs_cpl(s);
578 9050 got_cplchan = 1;
579 }
580 23266 end = s->end_freq[CPL_CH];
581 } else {
582 45620 end = s->end_freq[ch];
583 }
584 do
585 5344554 s->coeffs[ch][end] = 0;
586
2/2
✓ Branch 0 taken 5275668 times.
✓ Branch 1 taken 68886 times.
5344554 while (++end < 256);
587 }
588
589 /* zero the dithered coefficients for appropriate channels */
590 22144 remove_dithering(s);
591 22144 }
592
593 /**
594 * Stereo rematrixing.
595 * reference: Section 7.5.4 Rematrixing : Decoding Technique
596 */
597 14274 static void do_rematrixing(AC3DecodeContext *s)
598 {
599 int bnd, i;
600 int end, bndend;
601
602 14274 end = FFMIN(s->end_freq[1], s->end_freq[2]);
603
604
2/2
✓ Branch 0 taken 56490 times.
✓ Branch 1 taken 14274 times.
70764 for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
605
2/2
✓ Branch 0 taken 38591 times.
✓ Branch 1 taken 17899 times.
56490 if (s->rematrixing_flags[bnd]) {
606 38591 bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd + 1]);
607
2/2
✓ Branch 0 taken 1020108 times.
✓ Branch 1 taken 38591 times.
1058699 for (i = ff_ac3_rematrix_band_tab[bnd]; i < bndend; i++) {
608 1020108 INTFLOAT tmp0 = s->coeffs[1][i];
609 1020108 s->coeffs[1][i] += s->coeffs[2][i];
610 1020108 s->coeffs[2][i] = tmp0 - s->coeffs[2][i];
611 }
612 }
613 }
614 14274 }
615
616 /**
617 * Inverse MDCT Transform.
618 * Convert frequency domain coefficients to time-domain audio samples.
619 * reference: Section 7.9.4 Transformation Equations
620 */
621 22144 static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
622 {
623 int ch;
624 #if USE_FIXED
625 8852 int window_bits = 8 + fixed_coeff_bits(s);
626 #endif
627
628
2/2
✓ Branch 0 taken 59822 times.
✓ Branch 1 taken 22144 times.
81966 for (ch = 1; ch <= channels; ch++) {
629
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 59816 times.
59822 if (s->block_switch[ch]) {
630 int i;
631 6 INTFLOAT *x = s->tmp_output + 128;
632
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 6 times.
774 for (i = 0; i < 128; i++)
633 768 x[i] = s->transform_coeffs[ch][2 * i];
634 6 s->tx_fn_128(s->tx_128, s->tmp_output, x, sizeof(INTFLOAT));
635 #if USE_FIXED
636 2 s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
637 2 s->tmp_output, s->window, 128, window_bits);
638 #else
639 4 s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
640 4 s->tmp_output, s->window, 128);
641 #endif
642
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 6 times.
774 for (i = 0; i < 128; i++)
643 768 x[i] = s->transform_coeffs[ch][2 * i + 1];
644 6 s->tx_fn_128(s->tx_128, s->delay[ch - 1 + offset], x, sizeof(INTFLOAT));
645 } else {
646 59816 s->tx_fn_256(s->tx_256, s->tmp_output, s->transform_coeffs[ch], sizeof(INTFLOAT));
647 #if USE_FIXED
648 24235 s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
649 24235 s->tmp_output, s->window, 128, window_bits);
650 #else
651 35581 s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
652 35581 s->tmp_output, s->window, 128);
653 #endif
654 59816 memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(INTFLOAT));
655 }
656 }
657 22144 }
658
659 /**
660 * Upmix delay samples from stereo to original channel layout.
661 */
662 6 static void ac3_upmix_delay(AC3DecodeContext *s)
663 {
664 6 int channel_data_size = sizeof(s->delay[0]);
665
2/7
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
6 switch (s->channel_mode) {
666 2 case AC3_CHMODE_DUALMONO:
667 case AC3_CHMODE_STEREO:
668 /* upmix mono to stereo */
669 2 memcpy(s->delay[1], s->delay[0], channel_data_size);
670 2 break;
671 case AC3_CHMODE_2F2R:
672 memset(s->delay[3], 0, channel_data_size);
673 av_fallthrough;
674 case AC3_CHMODE_2F1R:
675 memset(s->delay[2], 0, channel_data_size);
676 break;
677 case AC3_CHMODE_3F2R:
678 memset(s->delay[4], 0, channel_data_size);
679 av_fallthrough;
680 4 case AC3_CHMODE_3F1R:
681 4 memset(s->delay[3], 0, channel_data_size);
682 av_fallthrough;
683 4 case AC3_CHMODE_3F:
684 4 memcpy(s->delay[2], s->delay[1], channel_data_size);
685 4 memset(s->delay[1], 0, channel_data_size);
686 4 break;
687 }
688 6 }
689
690 /**
691 * Decode band structure for coupling, spectral extension, or enhanced coupling.
692 * The band structure defines how many subbands are in each band. For each
693 * subband in the range, 1 means it is combined with the previous band, and 0
694 * means that it starts a new band.
695 *
696 * @param[in] gbc bit reader context
697 * @param[in] blk block number
698 * @param[in] eac3 flag to indicate E-AC-3
699 * @param[in] ecpl flag to indicate enhanced coupling
700 * @param[in] start_subband subband number for start of range
701 * @param[in] end_subband subband number for end of range
702 * @param[in] default_band_struct default band structure table
703 * @param[out] num_bands number of bands (optionally NULL)
704 * @param[out] band_sizes array containing the number of bins in each band (optionally NULL)
705 * @param[in,out] band_struct current band structure
706 */
707 2588 static void decode_band_structure(GetBitContext *gbc, int blk, int eac3,
708 int ecpl, int start_subband, int end_subband,
709 const uint8_t *default_band_struct,
710 int *num_bands, uint8_t *band_sizes,
711 uint8_t *band_struct, int band_struct_size)
712 {
713 2588 int subbnd, bnd, n_subbands, n_bands=0;
714 uint8_t bnd_sz[22];
715
716 2588 n_subbands = end_subband - start_subband;
717
718
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (!blk)
719 2588 memcpy(band_struct, default_band_struct, band_struct_size);
720
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 av_assert0(band_struct_size >= start_subband + n_subbands);
722
723 2588 band_struct += start_subband + 1;
724
725 /* decode band structure from bitstream or use default */
726
4/4
✓ Branch 0 taken 1648 times.
✓ Branch 1 taken 940 times.
✓ Branch 3 taken 1079 times.
✓ Branch 4 taken 569 times.
2588 if (!eac3 || get_bits1(gbc)) {
727
2/2
✓ Branch 0 taken 6603 times.
✓ Branch 1 taken 2019 times.
8622 for (subbnd = 0; subbnd < n_subbands - 1; subbnd++) {
728 6603 band_struct[subbnd] = get_bits1(gbc);
729 }
730 }
731
732 /* calculate number of bands and band sizes based on band structure.
733 note that the first 4 subbands in enhanced coupling span only 6 bins
734 instead of 12. */
735
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2588 if (num_bands || band_sizes ) {
736 2588 n_bands = n_subbands;
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2588 times.
2588 bnd_sz[0] = ecpl ? 6 : 12;
738
2/2
✓ Branch 0 taken 10026 times.
✓ Branch 1 taken 2588 times.
12614 for (bnd = 0, subbnd = 1; subbnd < n_subbands; subbnd++) {
739
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10026 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10026 int subbnd_size = (ecpl && subbnd < 4) ? 6 : 12;
740
2/2
✓ Branch 0 taken 4471 times.
✓ Branch 1 taken 5555 times.
10026 if (band_struct[subbnd - 1]) {
741 4471 n_bands--;
742 4471 bnd_sz[bnd] += subbnd_size;
743 } else {
744 5555 bnd_sz[++bnd] = subbnd_size;
745 }
746 }
747 }
748
749 /* set optional output params */
750
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (num_bands)
751 2588 *num_bands = n_bands;
752
1/2
✓ Branch 0 taken 2588 times.
✗ Branch 1 not taken.
2588 if (band_sizes)
753 2588 memcpy(band_sizes, bnd_sz, n_bands);
754 2588 }
755
756 1079 static inline int spx_strategy(AC3DecodeContext *s, int blk)
757 {
758 1079 GetBitContext *bc = &s->gbc;
759 int dst_start_freq, dst_end_freq, src_start_freq,
760 start_subband, end_subband;
761
762 /* determine which channels use spx */
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1079 times.
1079 if (s->channel_mode == AC3_CHMODE_MONO) {
764 s->channel_uses_spx[1] = 1;
765 } else {
766 1079 unsigned channel_uses_spx = get_bits(bc, s->fbw_channels);
767
2/2
✓ Branch 0 taken 2446 times.
✓ Branch 1 taken 1079 times.
3525 for (int ch = s->fbw_channels; ch >= 1; --ch) {
768 2446 s->channel_uses_spx[ch] = channel_uses_spx & 1;
769 2446 channel_uses_spx >>= 1;
770 }
771 }
772
773 /* get the frequency bins of the spx copy region and the spx start
774 and end subbands */
775 1079 dst_start_freq = get_bits(bc, 2);
776 1079 start_subband = get_bits(bc, 3) + 2;
777
2/2
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 96 times.
1079 if (start_subband > 7)
778 983 start_subband += start_subband - 7;
779 1079 end_subband = get_bits(bc, 3) + 5;
780 #if USE_FIXED
781 537 s->spx_dst_end_freq = end_freq_inv_tab[end_subband-5];
782 #endif
783
1/2
✓ Branch 0 taken 1079 times.
✗ Branch 1 not taken.
1079 if (end_subband > 7)
784 1079 end_subband += end_subband - 7;
785 1079 dst_start_freq = dst_start_freq * 12 + 25;
786 1079 src_start_freq = start_subband * 12 + 25;
787 1079 dst_end_freq = end_subband * 12 + 25;
788
789 /* check validity of spx ranges */
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1079 times.
1079 if (start_subband >= end_subband) {
791 av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
792 "range (%d >= %d)\n", start_subband, end_subband);
793 return AVERROR_INVALIDDATA;
794 }
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1079 times.
1079 if (dst_start_freq >= src_start_freq) {
796 av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
797 "copy start bin (%d >= %d)\n", dst_start_freq, src_start_freq);
798 return AVERROR_INVALIDDATA;
799 }
800
801 1079 s->spx_dst_start_freq = dst_start_freq;
802 1079 s->spx_src_start_freq = src_start_freq;
803 if (!USE_FIXED)
804 542 s->spx_dst_end_freq = dst_end_freq;
805
806 1079 decode_band_structure(bc, blk, s->eac3, 0,
807 start_subband, end_subband,
808 ff_eac3_default_spx_band_struct,
809 &s->num_spx_bands,
810 1079 s->spx_band_sizes,
811 1079 s->spx_band_struct, sizeof(s->spx_band_struct));
812 1079 return 0;
813 }
814
815 6474 static inline void spx_coordinates(AC3DecodeContext *s)
816 {
817 6474 GetBitContext *bc = &s->gbc;
818 6474 int fbw_channels = s->fbw_channels;
819 int ch, bnd;
820
821
2/2
✓ Branch 0 taken 14676 times.
✓ Branch 1 taken 6474 times.
21150 for (ch = 1; ch <= fbw_channels; ch++) {
822
1/2
✓ Branch 0 taken 14676 times.
✗ Branch 1 not taken.
14676 if (s->channel_uses_spx[ch]) {
823
4/4
✓ Branch 0 taken 12230 times.
✓ Branch 1 taken 2446 times.
✓ Branch 3 taken 1478 times.
✓ Branch 4 taken 10752 times.
14676 if (s->first_spx_coords[ch] || get_bits1(bc)) {
824 INTFLOAT spx_blend;
825 int bin, master_spx_coord;
826
827 3924 s->first_spx_coords[ch] = 0;
828 3924 spx_blend = AC3_SPX_BLEND(get_bits(bc, 5));
829 3924 master_spx_coord = get_bits(bc, 2) * 3;
830
831 3924 bin = s->spx_src_start_freq;
832
2/2
✓ Branch 0 taken 9846 times.
✓ Branch 1 taken 3924 times.
13770 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
833 9846 int bandsize = s->spx_band_sizes[bnd];
834 int spx_coord_exp, spx_coord_mant;
835 INTFLOAT nratio, sblend, nblend;
836 #if USE_FIXED
837 /* calculate blending factors */
838 4913 int64_t accu = ((bin << 23) + (bandsize << 22))
839 4913 * (int64_t)s->spx_dst_end_freq;
840 4913 nratio = (int)(accu >> 32);
841 4913 nratio -= spx_blend << 18;
842
843
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4900 times.
4913 if (nratio < 0) {
844 13 nblend = 0;
845 13 sblend = 0x800000;
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4900 times.
4900 } else if (nratio > 0x7fffff) {
847 nblend = 14529495; // sqrt(3) in FP.23
848 sblend = 0;
849 } else {
850 4900 nblend = fixed_sqrt(nratio, 23);
851 4900 accu = (int64_t)nblend * 1859775393;
852 4900 nblend = (int)((accu + (1<<29)) >> 30);
853 4900 sblend = fixed_sqrt(0x800000 - nratio, 23);
854 }
855 #else
856 float spx_coord;
857
858 /* calculate blending factors */
859 4933 nratio = ((float)((bin + (bandsize >> 1))) / s->spx_dst_end_freq) - spx_blend;
860 4933 nratio = av_clipf(nratio, 0.0f, 1.0f);
861 4933 nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3)
862 // to give unity variance
863 4933 sblend = sqrtf(1.0f - nratio);
864 #endif
865 9846 bin += bandsize;
866
867 /* decode spx coordinates */
868 9846 spx_coord_exp = get_bits(bc, 4);
869 9846 spx_coord_mant = get_bits(bc, 2);
870
2/2
✓ Branch 0 taken 1436 times.
✓ Branch 1 taken 8410 times.
9846 if (spx_coord_exp == 15) spx_coord_mant <<= 1;
871 8410 else spx_coord_mant += 4;
872 9846 spx_coord_mant <<= (25 - spx_coord_exp - master_spx_coord);
873
874 /* multiply noise and signal blending factors by spx coordinate */
875 #if USE_FIXED
876 4913 accu = (int64_t)nblend * spx_coord_mant;
877 4913 s->spx_noise_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
878 4913 accu = (int64_t)sblend * spx_coord_mant;
879 4913 s->spx_signal_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
880 #else
881 4933 spx_coord = spx_coord_mant * (1.0f / (1 << 23));
882 4933 s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
883 4933 s->spx_signal_blend[ch][bnd] = sblend * spx_coord;
884 #endif
885 }
886 }
887 } else {
888 s->first_spx_coords[ch] = 1;
889 }
890 }
891 6474 }
892
893 3856 static inline int coupling_strategy(AC3DecodeContext *s, int blk,
894 uint8_t *bit_alloc_stages)
895 {
896 3856 GetBitContext *bc = &s->gbc;
897 3856 int fbw_channels = s->fbw_channels;
898 3856 int channel_mode = s->channel_mode;
899 int ch;
900
901 3856 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
902
2/2
✓ Branch 0 taken 1693 times.
✓ Branch 1 taken 2163 times.
3856 if (!s->eac3)
903 1693 s->cpl_in_use[blk] = get_bits1(bc);
904
2/2
✓ Branch 0 taken 1509 times.
✓ Branch 1 taken 2347 times.
3856 if (s->cpl_in_use[blk]) {
905 /* coupling in use */
906 int cpl_start_subband, cpl_end_subband;
907
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1509 times.
1509 if (channel_mode < AC3_CHMODE_STEREO) {
909 av_log(s->avctx, AV_LOG_ERROR, "coupling not allowed in mono or dual-mono\n");
910 return AVERROR_INVALIDDATA;
911 }
912
913 /* check for enhanced coupling */
914
3/4
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 940 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 569 times.
1509 if (s->eac3 && get_bits1(bc)) {
915 /* TODO: parse enhanced coupling strategy info */
916 avpriv_request_sample(s->avctx, "Enhanced coupling");
917 return AVERROR_PATCHWELCOME;
918 }
919
920 /* determine which channels are coupled */
921
3/4
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 940 times.
✓ Branch 2 taken 569 times.
✗ Branch 3 not taken.
1509 if (s->eac3 && s->channel_mode == AC3_CHMODE_STEREO) {
922 569 s->channel_in_cpl[1] = 1;
923 569 s->channel_in_cpl[2] = 1;
924 } else {
925
2/2
✓ Branch 0 taken 2741 times.
✓ Branch 1 taken 940 times.
3681 for (ch = 1; ch <= fbw_channels; ch++)
926 2741 s->channel_in_cpl[ch] = get_bits1(bc);
927 }
928
929 /* phase flags in use */
930
2/2
✓ Branch 0 taken 1222 times.
✓ Branch 1 taken 287 times.
1509 if (channel_mode == AC3_CHMODE_STEREO)
931 1222 s->phase_flags_in_use = get_bits1(bc);
932
933 /* coupling frequency range */
934 1509 cpl_start_subband = get_bits(bc, 4);
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1509 times.
1509 cpl_end_subband = s->spx_in_use ? (s->spx_src_start_freq - 37) / 12 :
936 1509 get_bits(bc, 4) + 3;
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1509 times.
1509 if (cpl_start_subband >= cpl_end_subband) {
938 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n",
939 cpl_start_subband, cpl_end_subband);
940 return AVERROR_INVALIDDATA;
941 }
942 1509 s->start_freq[CPL_CH] = cpl_start_subband * 12 + 37;
943 1509 s->end_freq[CPL_CH] = cpl_end_subband * 12 + 37;
944
945 1509 decode_band_structure(bc, blk, s->eac3, 0, cpl_start_subband,
946 cpl_end_subband,
947 ff_eac3_default_cpl_band_struct,
948 1509 &s->num_cpl_bands, s->cpl_band_sizes,
949 1509 s->cpl_band_struct, sizeof(s->cpl_band_struct));
950 } else {
951 /* coupling not in use */
952
2/2
✓ Branch 0 taken 7683 times.
✓ Branch 1 taken 2347 times.
10030 for (ch = 1; ch <= fbw_channels; ch++) {
953 7683 s->channel_in_cpl[ch] = 0;
954 7683 s->first_cpl_coords[ch] = 1;
955 }
956 2347 s->first_cpl_leak = s->eac3;
957 2347 s->phase_flags_in_use = 0;
958 }
959
960 3856 return 0;
961 }
962
963 9050 static inline int coupling_coordinates(AC3DecodeContext *s, int blk)
964 {
965 9050 GetBitContext *bc = &s->gbc;
966 9050 int fbw_channels = s->fbw_channels;
967 int ch, bnd;
968 9050 int cpl_coords_exist = 0;
969
970
2/2
✓ Branch 0 taken 23266 times.
✓ Branch 1 taken 9050 times.
32316 for (ch = 1; ch <= fbw_channels; ch++) {
971
1/2
✓ Branch 0 taken 23266 times.
✗ Branch 1 not taken.
23266 if (s->channel_in_cpl[ch]) {
972
6/6
✓ Branch 0 taken 6828 times.
✓ Branch 1 taken 16438 times.
✓ Branch 2 taken 5690 times.
✓ Branch 3 taken 1138 times.
✓ Branch 5 taken 5328 times.
✓ Branch 6 taken 16800 times.
29732 if ((s->eac3 && s->first_cpl_coords[ch]) || get_bits1(bc)) {
973 int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
974 6466 s->first_cpl_coords[ch] = 0;
975 6466 cpl_coords_exist = 1;
976 6466 master_cpl_coord = 3 * get_bits(bc, 2);
977
2/2
✓ Branch 0 taken 18351 times.
✓ Branch 1 taken 6466 times.
24817 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
978 18351 cpl_coord_exp = get_bits(bc, 4);
979 18351 cpl_coord_mant = get_bits(bc, 4);
980
2/2
✓ Branch 0 taken 2610 times.
✓ Branch 1 taken 15741 times.
18351 if (cpl_coord_exp == 15)
981 2610 s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
982 else
983 15741 s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
984 18351 s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
985 }
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16800 times.
16800 } else if (!blk) {
987 av_log(s->avctx, AV_LOG_ERROR, "new coupling coordinates must "
988 "be present in block 0\n");
989 return AVERROR_INVALIDDATA;
990 }
991 } else {
992 /* channel not in coupling */
993 s->first_cpl_coords[ch] = 1;
994 }
995 }
996 /* phase flags */
997
4/4
✓ Branch 0 taken 7328 times.
✓ Branch 1 taken 1722 times.
✓ Branch 2 taken 1252 times.
✓ Branch 3 taken 6076 times.
9050 if (s->channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
998
2/2
✓ Branch 0 taken 5244 times.
✓ Branch 1 taken 1252 times.
6496 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
999
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 4572 times.
5244 s->phase_flags[bnd] = s->phase_flags_in_use ? get_bits1(bc) : 0;
1000 }
1001 }
1002
1003 9050 return 0;
1004 }
1005
1006 /**
1007 * Decode a single audio block from the AC-3 bitstream.
1008 */
1009 22144 static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
1010 {
1011 22144 int fbw_channels = s->fbw_channels;
1012 22144 int channel_mode = s->channel_mode;
1013 int i, bnd, seg, ch, ret;
1014 int different_transforms;
1015 int downmix_output;
1016 int cpl_in_use;
1017 22144 GetBitContext *gbc = &s->gbc;
1018 22144 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS] = { 0 };
1019
1020 /* block switch flags */
1021 22144 different_transforms = 0;
1022
2/2
✓ Branch 0 taken 10146 times.
✓ Branch 1 taken 11998 times.
22144 if (s->block_switch_syntax) {
1023
2/2
✓ Branch 0 taken 34308 times.
✓ Branch 1 taken 10146 times.
44454 for (ch = 1; ch <= fbw_channels; ch++) {
1024 34308 s->block_switch[ch] = get_bits1(gbc);
1025
4/4
✓ Branch 0 taken 24162 times.
✓ Branch 1 taken 10146 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 24156 times.
34308 if (ch > 1 && s->block_switch[ch] != s->block_switch[1])
1026 6 different_transforms = 1;
1027 }
1028 }
1029
1030 /* dithering flags */
1031
2/2
✓ Branch 0 taken 10146 times.
✓ Branch 1 taken 11998 times.
22144 if (s->dither_flag_syntax) {
1032
2/2
✓ Branch 0 taken 34308 times.
✓ Branch 1 taken 10146 times.
44454 for (ch = 1; ch <= fbw_channels; ch++) {
1033 34308 s->dither_flag[ch] = get_bits1(gbc);
1034 }
1035 }
1036
1037 /* dynamic range */
1038 22144 i = !s->channel_mode;
1039 do {
1040
2/2
✓ Branch 1 taken 4633 times.
✓ Branch 2 taken 17511 times.
22144 if (get_bits1(gbc)) {
1041 /* Allow asymmetric application of DRC when drc_scale > 1.
1042 Amplification of quiet sounds is enhanced */
1043 4633 int range_bits = get_bits(gbc, 8);
1044 4633 INTFLOAT range = AC3_RANGE(range_bits);
1045
4/4
✓ Branch 0 taken 2018 times.
✓ Branch 1 taken 2615 times.
✓ Branch 2 taken 1092 times.
✓ Branch 3 taken 926 times.
4633 if (range_bits <= 127 || s->drc_scale <= 1.0)
1046 3707 s->dynamic_range[i] = AC3_DYNAMIC_RANGE(range);
1047 else
1048 926 s->dynamic_range[i] = range;
1049
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 16344 times.
17511 } else if (blk == 0) {
1050 1167 s->dynamic_range[i] = AC3_DYNAMIC_RANGE1;
1051 }
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22144 times.
22144 } while (i--);
1053
1054 /* spectral extension strategy */
1055
5/6
✓ Branch 0 taken 11998 times.
✓ Branch 1 taken 10146 times.
✓ Branch 2 taken 9835 times.
✓ Branch 3 taken 2163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9835 times.
22144 if (s->eac3 && (!blk || get_bits1(gbc))) {
1056 2163 s->spx_in_use = get_bits1(gbc);
1057
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 1084 times.
2163 if (s->spx_in_use) {
1058
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1079 times.
1079 if ((ret = spx_strategy(s, blk)) < 0)
1059 return ret;
1060 }
1061 }
1062
4/4
✓ Branch 0 taken 11998 times.
✓ Branch 1 taken 10146 times.
✓ Branch 2 taken 5524 times.
✓ Branch 3 taken 6474 times.
22144 if (!s->eac3 || !s->spx_in_use) {
1063 15670 s->spx_in_use = 0;
1064
2/2
✓ Branch 0 taken 49772 times.
✓ Branch 1 taken 15670 times.
65442 for (ch = 1; ch <= fbw_channels; ch++) {
1065 49772 s->channel_uses_spx[ch] = 0;
1066 49772 s->first_spx_coords[ch] = 1;
1067 }
1068 }
1069
1070 /* spectral extension coordinates */
1071
2/2
✓ Branch 0 taken 6474 times.
✓ Branch 1 taken 15670 times.
22144 if (s->spx_in_use)
1072 6474 spx_coordinates(s);
1073
1074 /* coupling strategy */
1075
4/4
✓ Branch 0 taken 11998 times.
✓ Branch 1 taken 10146 times.
✓ Branch 3 taken 3856 times.
✓ Branch 4 taken 18288 times.
22144 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
1076
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3856 times.
3856 if ((ret = coupling_strategy(s, blk, bit_alloc_stages)) < 0)
1077 return ret;
1078
2/2
✓ Branch 0 taken 8453 times.
✓ Branch 1 taken 9835 times.
18288 } else if (!s->eac3) {
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8453 times.
8453 if (!blk) {
1080 av_log(s->avctx, AV_LOG_ERROR, "new coupling strategy must "
1081 "be present in block 0\n");
1082 return AVERROR_INVALIDDATA;
1083 } else {
1084 8453 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
1085 }
1086 }
1087 22144 cpl_in_use = s->cpl_in_use[blk];
1088
1089 /* coupling coordinates */
1090
2/2
✓ Branch 0 taken 9050 times.
✓ Branch 1 taken 13094 times.
22144 if (cpl_in_use) {
1091
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9050 times.
9050 if ((ret = coupling_coordinates(s, blk)) < 0)
1092 return ret;
1093 }
1094
1095 /* stereo rematrixing strategy and band structure */
1096
2/2
✓ Branch 0 taken 14274 times.
✓ Branch 1 taken 7870 times.
22144 if (channel_mode == AC3_CHMODE_STEREO) {
1097
6/6
✓ Branch 0 taken 9312 times.
✓ Branch 1 taken 4962 times.
✓ Branch 2 taken 7760 times.
✓ Branch 3 taken 1552 times.
✓ Branch 5 taken 2252 times.
✓ Branch 6 taken 10470 times.
14274 if ((s->eac3 && !blk) || get_bits1(gbc)) {
1098 3804 s->num_rematrixing_bands = 4;
1099
4/4
✓ Branch 0 taken 2625 times.
✓ Branch 1 taken 1179 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 2522 times.
3804 if (cpl_in_use && s->start_freq[CPL_CH] <= 61) {
1100
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 102 times.
103 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
1101
3/4
✓ Branch 0 taken 1003 times.
✓ Branch 1 taken 2698 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1003 times.
3701 } else if (s->spx_in_use && s->spx_src_start_freq <= 61) {
1102 s->num_rematrixing_bands--;
1103 }
1104
2/2
✓ Branch 0 taken 15112 times.
✓ Branch 1 taken 3804 times.
18916 for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++)
1105 15112 s->rematrixing_flags[bnd] = get_bits1(gbc);
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10470 times.
10470 } else if (!blk) {
1107 av_log(s->avctx, AV_LOG_WARNING, "Warning: "
1108 "new rematrixing strategy not present in block 0\n");
1109 s->num_rematrixing_bands = 0;
1110 }
1111 }
1112
1113 /* exponent strategies for each channel */
1114
2/2
✓ Branch 0 taken 77936 times.
✓ Branch 1 taken 22144 times.
100080 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1115
2/2
✓ Branch 0 taken 43610 times.
✓ Branch 1 taken 34326 times.
77936 if (!s->eac3)
1116
2/2
✓ Branch 0 taken 3666 times.
✓ Branch 1 taken 39944 times.
43610 s->exp_strategy[blk][ch] = get_bits(gbc, 2 - (ch == s->lfe_ch));
1117
2/2
✓ Branch 0 taken 20113 times.
✓ Branch 1 taken 57823 times.
77936 if (s->exp_strategy[blk][ch] != EXP_REUSE)
1118 20113 bit_alloc_stages[ch] = 3;
1119 }
1120
1121 /* channel bandwidth */
1122
2/2
✓ Branch 0 taken 64448 times.
✓ Branch 1 taken 22144 times.
86592 for (ch = 1; ch <= fbw_channels; ch++) {
1123 64448 s->start_freq[ch] = 0;
1124
2/2
✓ Branch 0 taken 16364 times.
✓ Branch 1 taken 48084 times.
64448 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
1125 int group_size;
1126 16364 int prev = s->end_freq[ch];
1127
2/2
✓ Branch 0 taken 4970 times.
✓ Branch 1 taken 11394 times.
16364 if (s->channel_in_cpl[ch])
1128 4970 s->end_freq[ch] = s->start_freq[CPL_CH];
1129
2/2
✓ Branch 0 taken 3924 times.
✓ Branch 1 taken 7470 times.
11394 else if (s->channel_uses_spx[ch])
1130 3924 s->end_freq[ch] = s->spx_src_start_freq;
1131 else {
1132 7470 int bandwidth_code = get_bits(gbc, 6);
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7470 times.
7470 if (bandwidth_code > 60) {
1134 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code);
1135 return AVERROR_INVALIDDATA;
1136 }
1137 7470 s->end_freq[ch] = bandwidth_code * 3 + 73;
1138 }
1139 16364 group_size = 3 << (s->exp_strategy[blk][ch] - 1);
1140 16364 s->num_exp_groups[ch] = (s->end_freq[ch] + group_size-4) / group_size;
1141
4/4
✓ Branch 0 taken 4806 times.
✓ Branch 1 taken 11558 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4802 times.
16364 if (blk > 0 && s->end_freq[ch] != prev)
1142 4 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
1143 }
1144 }
1145
4/4
✓ Branch 0 taken 9050 times.
✓ Branch 1 taken 13094 times.
✓ Branch 2 taken 2787 times.
✓ Branch 3 taken 6263 times.
22144 if (cpl_in_use && s->exp_strategy[blk][CPL_CH] != EXP_REUSE) {
1146 2787 s->num_exp_groups[CPL_CH] = (s->end_freq[CPL_CH] - s->start_freq[CPL_CH]) /
1147 2787 (3 << (s->exp_strategy[blk][CPL_CH] - 1));
1148 }
1149
1150 /* decode exponents for each channel */
1151
2/2
✓ Branch 0 taken 77936 times.
✓ Branch 1 taken 22144 times.
100080 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1152
2/2
✓ Branch 0 taken 20113 times.
✓ Branch 1 taken 57823 times.
77936 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
1153 20113 s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20113 times.
20113 if (decode_exponents(s, gbc, s->exp_strategy[blk][ch],
1155 20113 s->num_exp_groups[ch], s->dexps[ch][0],
1156 20113 &s->dexps[ch][s->start_freq[ch]+!!ch])) {
1157 return AVERROR_INVALIDDATA;
1158 }
1159
4/4
✓ Branch 0 taken 17326 times.
✓ Branch 1 taken 2787 times.
✓ Branch 2 taken 16364 times.
✓ Branch 3 taken 962 times.
20113 if (ch != CPL_CH && ch != s->lfe_ch)
1160 16364 skip_bits(gbc, 2); /* skip gainrng */
1161 }
1162 }
1163
1164 /* bit allocation information */
1165
2/2
✓ Branch 0 taken 12604 times.
✓ Branch 1 taken 9540 times.
22144 if (s->bit_allocation_syntax) {
1166
2/2
✓ Branch 1 taken 2320 times.
✓ Branch 2 taken 10284 times.
12604 if (get_bits1(gbc)) {
1167 2320 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1168 2320 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1169 2320 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
1170 2320 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
1171 2320 s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)];
1172
2/2
✓ Branch 0 taken 10075 times.
✓ Branch 1 taken 2320 times.
12395 for (ch = !cpl_in_use; ch <= s->channels; ch++)
1173 10075 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10284 times.
10284 } else if (!blk) {
1175 av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must "
1176 "be present in block 0\n");
1177 return AVERROR_INVALIDDATA;
1178 }
1179 }
1180
1181 /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
1182
4/4
✓ Branch 0 taken 11998 times.
✓ Branch 1 taken 10146 times.
✓ Branch 2 taken 2163 times.
✓ Branch 3 taken 9835 times.
22144 if (!s->eac3 || !blk) {
1183
4/4
✓ Branch 0 taken 10146 times.
✓ Branch 1 taken 2163 times.
✓ Branch 3 taken 1691 times.
✓ Branch 4 taken 8455 times.
14000 if (s->snr_offset_strategy && get_bits1(gbc)) {
1184 1691 int snr = 0;
1185 int csnr;
1186 1691 csnr = (get_bits(gbc, 6) - 15) << 4;
1187
2/2
✓ Branch 0 taken 7269 times.
✓ Branch 1 taken 1691 times.
8960 for (i = ch = !cpl_in_use; ch <= s->channels; ch++) {
1188 /* snr offset */
1189
3/4
✓ Branch 0 taken 5578 times.
✓ Branch 1 taken 1691 times.
✓ Branch 2 taken 5578 times.
✗ Branch 3 not taken.
7269 if (ch == i || s->snr_offset_strategy == 2)
1190 7269 snr = (csnr + get_bits(gbc, 4)) << 2;
1191 /* run at least last bit allocation stage if snr offset changes */
1192
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7269 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7269 if (blk && s->snr_offset[ch] != snr) {
1193 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 1);
1194 }
1195 7269 s->snr_offset[ch] = snr;
1196
1197 /* fast gain (normal AC-3 only) */
1198
1/2
✓ Branch 0 taken 7269 times.
✗ Branch 1 not taken.
7269 if (!s->eac3) {
1199 7269 int prev = s->fast_gain[ch];
1200 7269 s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
1201 /* run last 2 bit allocation stages if fast gain changes */
1202
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7269 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7269 if (blk && prev != s->fast_gain[ch])
1203 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1204 }
1205 }
1206
3/4
✓ Branch 0 taken 8455 times.
✓ Branch 1 taken 2163 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8455 times.
10618 } else if (!s->eac3 && !blk) {
1207 av_log(s->avctx, AV_LOG_ERROR, "new snr offsets must be present in block 0\n");
1208 return AVERROR_INVALIDDATA;
1209 }
1210 }
1211
1212 /* fast gain (E-AC-3 only) */
1213
3/4
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 20230 times.
✓ Branch 3 taken 1914 times.
✗ Branch 4 not taken.
22144 if (s->fast_gain_syntax && get_bits1(gbc)) {
1214
2/2
✓ Branch 0 taken 7656 times.
✓ Branch 1 taken 1914 times.
9570 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1215 7656 int prev = s->fast_gain[ch];
1216 7656 s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
1217 /* run last 2 bit allocation stages if fast gain changes */
1218
3/4
✓ Branch 0 taken 6380 times.
✓ Branch 1 taken 1276 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6380 times.
7656 if (blk && prev != s->fast_gain[ch])
1219 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1220 }
1221
4/4
✓ Branch 0 taken 10084 times.
✓ Branch 1 taken 10146 times.
✓ Branch 2 taken 1844 times.
✓ Branch 3 taken 8240 times.
20230 } else if (s->eac3 && !blk) {
1222
2/2
✓ Branch 0 taken 5425 times.
✓ Branch 1 taken 1844 times.
7269 for (ch = !cpl_in_use; ch <= s->channels; ch++)
1223 5425 s->fast_gain[ch] = ff_ac3_fast_gain_tab[4];
1224 }
1225
1226 /* E-AC-3 to AC-3 converter SNR offset */
1227
4/4
✓ Branch 0 taken 10084 times.
✓ Branch 1 taken 12060 times.
✓ Branch 3 taken 1373 times.
✓ Branch 4 taken 8711 times.
22144 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && get_bits1(gbc)) {
1228 1373 skip_bits(gbc, 10); // skip converter snr offset
1229 }
1230
1231 /* coupling leak information */
1232
2/2
✓ Branch 0 taken 9050 times.
✓ Branch 1 taken 13094 times.
22144 if (cpl_in_use) {
1233
4/4
✓ Branch 0 taken 8481 times.
✓ Branch 1 taken 569 times.
✓ Branch 3 taken 1032 times.
✓ Branch 4 taken 7449 times.
9050 if (s->first_cpl_leak || get_bits1(gbc)) {
1234 1601 int fl = get_bits(gbc, 3);
1235 1601 int sl = get_bits(gbc, 3);
1236 /* run last 2 bit allocation stages for coupling channel if
1237 coupling leak changes */
1238
4/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1509 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 90 times.
1601 if (blk && (fl != s->bit_alloc_params.cpl_fast_leak ||
1239
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sl != s->bit_alloc_params.cpl_slow_leak)) {
1240 92 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
1241 }
1242 1601 s->bit_alloc_params.cpl_fast_leak = fl;
1243 1601 s->bit_alloc_params.cpl_slow_leak = sl;
1244
3/4
✓ Branch 0 taken 4696 times.
✓ Branch 1 taken 2753 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4696 times.
7449 } else if (!s->eac3 && !blk) {
1245 av_log(s->avctx, AV_LOG_ERROR, "new coupling leak info must "
1246 "be present in block 0\n");
1247 return AVERROR_INVALIDDATA;
1248 }
1249 9050 s->first_cpl_leak = 0;
1250 }
1251
1252 /* delta bit allocation information */
1253
3/4
✓ Branch 0 taken 10146 times.
✓ Branch 1 taken 11998 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10146 times.
22144 if (s->dba_syntax && get_bits1(gbc)) {
1254 /* delta bit allocation exists (strategy) */
1255 for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
1256 s->dba_mode[ch] = get_bits(gbc, 2);
1257 if (s->dba_mode[ch] == DBA_RESERVED) {
1258 av_log(s->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1259 return AVERROR_INVALIDDATA;
1260 }
1261 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1262 }
1263 /* channel delta offset, len and bit allocation */
1264 for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
1265 if (s->dba_mode[ch] == DBA_NEW) {
1266 s->dba_nsegs[ch] = get_bits(gbc, 3) + 1;
1267 for (seg = 0; seg < s->dba_nsegs[ch]; seg++) {
1268 s->dba_offsets[ch][seg] = get_bits(gbc, 5);
1269 s->dba_lengths[ch][seg] = get_bits(gbc, 4);
1270 s->dba_values[ch][seg] = get_bits(gbc, 3);
1271 }
1272 /* run last 2 bit allocation stages if new dba values */
1273 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1274 }
1275 }
1276
2/2
✓ Branch 0 taken 3854 times.
✓ Branch 1 taken 18290 times.
22144 } else if (blk == 0) {
1277
2/2
✓ Branch 0 taken 16315 times.
✓ Branch 1 taken 3854 times.
20169 for (ch = 0; ch <= s->channels; ch++) {
1278 16315 s->dba_mode[ch] = DBA_NONE;
1279 }
1280 }
1281
1282 /* Bit allocation */
1283
2/2
✓ Branch 0 taken 77936 times.
✓ Branch 1 taken 22144 times.
100080 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1284
2/2
✓ Branch 0 taken 20113 times.
✓ Branch 1 taken 57823 times.
77936 if (bit_alloc_stages[ch] > 2) {
1285 /* Exponent mapping into PSD and PSD integration */
1286 20113 ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
1287 s->start_freq[ch], s->end_freq[ch],
1288 20113 s->psd[ch], s->band_psd[ch]);
1289 }
1290
2/2
✓ Branch 0 taken 20361 times.
✓ Branch 1 taken 57575 times.
77936 if (bit_alloc_stages[ch] > 1) {
1291 /* Compute excitation function, Compute masking curve, and
1292 Apply delta bit allocation */
1293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20361 times.
20361 if (ff_ac3_bit_alloc_calc_mask(&s->bit_alloc_params, s->band_psd[ch],
1294 s->start_freq[ch], s->end_freq[ch],
1295 20361 s->fast_gain[ch], (ch == s->lfe_ch),
1296 s->dba_mode[ch], s->dba_nsegs[ch],
1297 20361 s->dba_offsets[ch], s->dba_lengths[ch],
1298 20361 s->dba_values[ch], s->mask[ch])) {
1299 av_log(s->avctx, AV_LOG_ERROR, "error in bit allocation\n");
1300 return AVERROR_INVALIDDATA;
1301 }
1302 }
1303
2/2
✓ Branch 0 taken 20361 times.
✓ Branch 1 taken 57575 times.
77936 if (bit_alloc_stages[ch] > 0) {
1304 /* Compute bit allocation */
1305 40722 const uint8_t *bap_tab = s->channel_uses_aht[ch] ?
1306
2/2
✓ Branch 0 taken 1686 times.
✓ Branch 1 taken 18675 times.
20361 ff_eac3_hebap_tab : ff_ac3_bap_tab;
1307 20361 s->ac3dsp.bit_alloc_calc_bap(s->mask[ch], s->psd[ch],
1308 s->start_freq[ch], s->end_freq[ch],
1309 s->snr_offset[ch],
1310 s->bit_alloc_params.floor,
1311 20361 bap_tab, s->bap[ch]);
1312 }
1313 }
1314
1315 /* unused dummy data */
1316
4/4
✓ Branch 0 taken 12060 times.
✓ Branch 1 taken 10084 times.
✓ Branch 3 taken 1640 times.
✓ Branch 4 taken 10420 times.
22144 if (s->skip_syntax && get_bits1(gbc)) {
1317 1640 int skipl = get_bits(gbc, 9);
1318 1640 skip_bits_long(gbc, 8 * skipl);
1319 }
1320
1321 /* unpack the transform coefficients
1322 this also uncouples channels if coupling is in use. */
1323 22144 decode_transform_coeffs(s, blk);
1324
1325 /* TODO: generate enhanced coupling coordinates and uncouple */
1326
1327 /* recover coefficients if rematrixing is in use */
1328
2/2
✓ Branch 0 taken 14274 times.
✓ Branch 1 taken 7870 times.
22144 if (s->channel_mode == AC3_CHMODE_STEREO)
1329 14274 do_rematrixing(s);
1330
1331 /* apply scaling to coefficients (headroom, dynrng) */
1332
2/2
✓ Branch 0 taken 68886 times.
✓ Branch 1 taken 22144 times.
91030 for (ch = 1; ch <= s->channels; ch++) {
1333 68886 int audio_channel = 0;
1334 INTFLOAT gain;
1335
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 68886 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
68886 if (s->channel_mode == AC3_CHMODE_DUALMONO && ch <= 2)
1336 audio_channel = 2-ch;
1337
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 68886 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
68886 if (s->heavy_compression && s->compression_exists[audio_channel])
1338 gain = s->heavy_dynamic_range[audio_channel];
1339 else
1340 68886 gain = s->dynamic_range[audio_channel];
1341
1342 #if USE_FIXED
1343
2/2
✓ Branch 1 taken 14772 times.
✓ Branch 2 taken 13548 times.
28320 if (fixed_coeff_bits(s))
1344 14772 scale_coefs_q2(s->transform_coeffs[ch], s->coeffs[ch], gain,
1345 256);
1346 else
1347 13548 scale_coefs(s->transform_coeffs[ch], s->coeffs[ch], gain, 256);
1348 #else
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40566 times.
40566 if (s->target_level != 0)
1350 gain = gain * s->level_gain[audio_channel];
1351 40566 gain *= 1.0f / 4194304.0f;
1352 40566 s->fdsp->vector_fmul_scalar(s->transform_coeffs[ch], s->coeffs[ch],
1353 gain, 256);
1354 #endif
1355 }
1356
1357 /* apply spectral extension to high frequency bins */
1358
2/2
✓ Branch 0 taken 6474 times.
✓ Branch 1 taken 15670 times.
22144 if (CONFIG_EAC3_DECODER && s->spx_in_use) {
1359 6474 ff_eac3_apply_spectral_extension(s);
1360 }
1361
1362 /* downmix and MDCT. order depends on whether block switching is used for
1363 any channel in this block. this is because coefficients for the long
1364 and short transforms cannot be mixed. */
1365
2/2
✓ Branch 0 taken 2478 times.
✓ Branch 1 taken 19666 times.
24622 downmix_output = s->channels != s->out_channels &&
1366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2478 times.
2478 !((s->output_mode & AC3_OUTPUT_LFEON) &&
1367 s->fbw_channels == s->out_channels);
1368
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22138 times.
22144 if (different_transforms) {
1369 /* the delay samples have already been downmixed, so we upmix the delay
1370 samples in order to reconstruct all channels before downmixing. */
1371
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (s->downmixed) {
1372 6 s->downmixed = 0;
1373 6 ac3_upmix_delay(s);
1374 }
1375
1376 6 do_imdct(s, s->channels, offset);
1377
1378
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (downmix_output) {
1379 #if USE_FIXED
1380 1 ac3_downmix_c_fixed16(s->outptr, s->downmix_coeffs,
1381 s->out_channels, s->fbw_channels, 256);
1382 #else
1383 2 ff_ac3dsp_downmix(&s->ac3dsp, s->outptr, s->downmix_coeffs,
1384 s->out_channels, s->fbw_channels, 256);
1385 #endif
1386 }
1387 } else {
1388
2/2
✓ Branch 0 taken 2475 times.
✓ Branch 1 taken 19663 times.
22138 if (downmix_output) {
1389 2475 AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->xcfptr + 1, s->downmix_coeffs,
1390 s->out_channels, s->fbw_channels, 256);
1391 }
1392
1393
4/4
✓ Branch 0 taken 2475 times.
✓ Branch 1 taken 19663 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2472 times.
22138 if (downmix_output && !s->downmixed) {
1394 3 s->downmixed = 1;
1395 3 AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->dlyptr, s->downmix_coeffs,
1396 s->out_channels, s->fbw_channels, 128);
1397 }
1398
1399 22138 do_imdct(s, s->out_channels, offset);
1400 }
1401
1402 22144 return 0;
1403 }
1404
1405 /**
1406 * Decode a single AC-3 frame.
1407 */
1408 3546 static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1409 int *got_frame_ptr, AVPacket *avpkt)
1410 {
1411 3546 const uint8_t *buf = avpkt->data;
1412 3546 int buf_size, full_buf_size = avpkt->size;
1413 3546 AC3DecodeContext *s = avctx->priv_data;
1414 int blk, ch, err, offset, ret;
1415 int i;
1416 3546 int skip = 0, got_independent_frame = 0;
1417 const uint8_t *channel_map;
1418 uint8_t extended_channel_map[EAC3_MAX_CHANNELS];
1419 const SHORTFLOAT *output[AC3_MAX_CHANNELS];
1420 enum AVMatrixEncoding matrix_encoding;
1421 uint64_t mask;
1422
1423 3546 s->superframe_size = 0;
1424
1425 3546 buf_size = full_buf_size;
1426 3546 i = ff_ac3_find_syncword(buf, buf_size);
1427
3/4
✓ Branch 0 taken 3542 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3542 times.
3546 if (i < 0 || i > 10)
1428 4 return i;
1429 3542 buf += i;
1430 3542 buf_size -= i;
1431
1432 /* copy input buffer to decoder context to avoid reading past the end
1433 of the buffer, which can be caused by a damaged input stream. */
1434
2/4
✓ Branch 0 taken 3542 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3542 times.
3542 if (buf_size >= 2 && AV_RB16(buf) == 0x770B) {
1435 // seems to be byte-swapped AC-3
1436 int cnt = FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE) >> 1;
1437 s->bdsp.bswap16_buf((uint16_t *) s->input_buffer,
1438 (const uint16_t *) buf, cnt);
1439 } else
1440 3542 memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
1441
1442 /* if consistent noise generation is enabled, seed the linear feedback generator
1443 * with the contents of the AC-3 frame so that the noise is identical across
1444 * decodes given the same AC-3 frame data, for use with non-linear edititing software. */
1445
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3512 times.
3542 if (s->consistent_noise_generation)
1446 30 av_lfg_init_from_data(&s->dith_state, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
1447
1448 3542 buf = s->input_buffer;
1449 3861 dependent_frame:
1450 /* initialize the GetBitContext with the start of valid AC-3 Frame */
1451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3861 times.
3861 if ((ret = init_get_bits8(&s->gbc, buf, buf_size)) < 0)
1452 return ret;
1453
1454 /* parse the syncinfo */
1455 3861 err = parse_frame_header(s);
1456
1457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3861 times.
3861 if (err) {
1458 switch (err) {
1459 case AC3_PARSE_ERROR_SYNC:
1460 av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
1461 return AVERROR_INVALIDDATA;
1462 case AC3_PARSE_ERROR_BSID:
1463 av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
1464 break;
1465 case AC3_PARSE_ERROR_SAMPLE_RATE:
1466 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1467 break;
1468 case AC3_PARSE_ERROR_FRAME_SIZE:
1469 av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
1470 break;
1471 case AC3_PARSE_ERROR_FRAME_TYPE:
1472 /* skip frame if CRC is ok. otherwise use error concealment. */
1473 /* TODO: add support for substreams */
1474 if (s->substreamid) {
1475 av_log(avctx, AV_LOG_DEBUG,
1476 "unsupported substream %d: skipping frame\n",
1477 s->substreamid);
1478 *got_frame_ptr = 0;
1479 return buf_size;
1480 } else {
1481 av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
1482 }
1483 break;
1484 case AC3_PARSE_ERROR_CHANNEL_MAP:
1485 av_log(avctx, AV_LOG_ERROR, "invalid channel map\n");
1486 return AVERROR_INVALIDDATA;
1487 case AC3_PARSE_ERROR_CRC:
1488 break;
1489 default: // Normal AVERROR do not try to recover.
1490 *got_frame_ptr = 0;
1491 return err;
1492 }
1493 } else {
1494 /* check that reported frame size fits in input buffer */
1495
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3854 times.
3861 if (s->frame_size > buf_size) {
1496 7 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1497 7 err = AC3_PARSE_ERROR_FRAME_SIZE;
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3854 times.
3854 } else if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {
1499 /* check for crc mismatch */
1500 if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2],
1501 s->frame_size - 2)) {
1502 av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
1503 if (avctx->err_recognition & AV_EF_EXPLODE)
1504 return AVERROR_INVALIDDATA;
1505 err = AC3_PARSE_ERROR_CRC;
1506 }
1507 }
1508 }
1509
1510
3/4
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 3542 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 319 times.
3861 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT && !got_independent_frame) {
1511 av_log(avctx, AV_LOG_WARNING, "Ignoring dependent frame without independent frame.\n");
1512 *got_frame_ptr = 0;
1513 return FFMIN(full_buf_size, s->frame_size);
1514 }
1515
1516 /* channel config */
1517
5/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3854 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
3861 if (!err || (s->channels && s->out_channels != s->channels)) {
1518 3858 s->out_channels = s->channels;
1519 3858 s->output_mode = s->channel_mode;
1520
2/2
✓ Branch 0 taken 907 times.
✓ Branch 1 taken 2951 times.
3858 if (s->lfe_on)
1521 907 s->output_mode |= AC3_OUTPUT_LFEON;
1522
4/4
✓ Branch 0 taken 3857 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 240 times.
✓ Branch 3 taken 3617 times.
7715 if (s->channels > 1 &&
1523 3857 !av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO)) {
1524 240 s->out_channels = 1;
1525 240 s->output_mode = AC3_CHMODE_MONO;
1526
4/4
✓ Branch 0 taken 1238 times.
✓ Branch 1 taken 2380 times.
✓ Branch 2 taken 177 times.
✓ Branch 3 taken 1061 times.
4856 } else if (s->channels > 2 &&
1527 1238 !av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) {
1528 177 s->out_channels = 2;
1529 177 s->output_mode = AC3_CHMODE_STEREO;
1530 }
1531
1532 3858 s->loro_center_mix_level = ff_ac3_gain_levels[s-> center_mix_level];
1533 3858 s->loro_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level];
1534 3858 s->ltrt_center_mix_level = ff_ac3_gain_levels[s-> center_mix_level_ltrt];
1535 3858 s->ltrt_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
1536
3/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 3566 times.
3858 switch (s->preferred_downmix) {
1537 96 case AC3_DMIXMOD_LTRT:
1538 96 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_LTRT;
1539 96 break;
1540 case AC3_DMIXMOD_LORO:
1541 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_LORO;
1542 break;
1543 196 case AC3_DMIXMOD_DPLII:
1544 196 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_DPLII;
1545 196 break;
1546 3566 default:
1547 3566 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_UNKNOWN;
1548 3566 break;
1549 }
1550 /* set downmixing coefficients if needed */
1551
3/4
✓ Branch 0 taken 417 times.
✓ Branch 1 taken 3441 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 417 times.
3858 if (s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
1552 s->fbw_channels == s->out_channels)) {
1553
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 417 times.
417 if ((ret = set_downmix_coeffs(s)) < 0) {
1554 av_log(avctx, AV_LOG_ERROR, "error setting downmix coeffs\n");
1555 return ret;
1556 }
1557 }
1558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!s->channels) {
1559 av_log(avctx, AV_LOG_ERROR, "unable to determine channel mode\n");
1560 return AVERROR_INVALIDDATA;
1561 }
1562
1563 3861 mask = ff_ac3_channel_layout_tab[s->output_mode & ~AC3_OUTPUT_LFEON];
1564
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 3181 times.
3861 if (s->output_mode & AC3_OUTPUT_LFEON)
1565 680 mask |= AV_CH_LOW_FREQUENCY;
1566
1567 3861 av_channel_layout_uninit(&avctx->ch_layout);
1568 3861 av_channel_layout_from_mask(&avctx->ch_layout, mask);
1569
1570 /* set audio service type based on bitstream mode for AC-3 */
1571 3861 avctx->audio_service_type = s->bitstream_mode;
1572
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3861 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3861 if (s->bitstream_mode == 0x7 && s->channels > 1)
1573 avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
1574
1575 /* decode the audio blocks */
1576 3861 channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
1577
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 3542 times.
3861 offset = s->frame_type == EAC3_FRAME_TYPE_DEPENDENT ? AC3_MAX_CHANNELS : 0;
1578 #if USE_FIXED
1579 /* delay[] holds overlap samples scaled by the coefficient format that was
1580 * in use when they were produced. The independent and the dependent
1581 * substream own disjoint delay slots and may legitimately use different
1582 * formats, so only drop the overlap of the substream whose format really
1583 * changed, as happens when a malformed or explicitly forced stream
1584 * switches between E-AC-3 and AC-3. */
1585
2/2
✓ Branch 0 taken 1557 times.
✓ Branch 1 taken 3 times.
1560 if (!err) {
1586 1557 const int coeff_bits = fixed_coeff_bits(s);
1587 1557 const int slot = offset ? 1 : 0;
1588
1589
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1550 times.
1557 if (s->delay_coeff_bits[slot] != coeff_bits) {
1590 7 memset(s->delay[offset], 0, AC3_MAX_CHANNELS * sizeof(s->delay[0]));
1591 7 s->delay_coeff_bits[slot] = coeff_bits;
1592 }
1593 }
1594 #endif
1595
2/2
✓ Branch 0 taken 27027 times.
✓ Branch 1 taken 3861 times.
30888 for (ch = 0; ch < AC3_MAX_CHANNELS; ch++) {
1596 27027 output[ch] = s->output[ch + offset];
1597 27027 s->outptr[ch] = s->output[ch + offset];
1598 }
1599
2/2
✓ Branch 0 taken 12495 times.
✓ Branch 1 taken 3861 times.
16356 for (ch = 0; ch < s->channels; ch++) {
1600
2/2
✓ Branch 0 taken 10965 times.
✓ Branch 1 taken 1530 times.
12495 if (ch < s->out_channels)
1601 10965 s->outptr[channel_map[ch]] = s->output_buffer[ch + offset];
1602 }
1603
2/2
✓ Branch 0 taken 22186 times.
✓ Branch 1 taken 3861 times.
26047 for (blk = 0; blk < s->num_blocks; blk++) {
1604
3/4
✓ Branch 0 taken 22144 times.
✓ Branch 1 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22144 times.
22186 if (!err && decode_audio_block(s, blk, offset)) {
1605 av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");
1606 err = 1;
1607 }
1608
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 22144 times.
22186 if (err)
1609
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 42 times.
138 for (ch = 0; ch < s->out_channels; ch++)
1610 96 memcpy(s->output_buffer[ch + offset] + AC3_BLOCK_SIZE*blk, output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
1611
2/2
✓ Branch 0 taken 59910 times.
✓ Branch 1 taken 22186 times.
82096 for (ch = 0; ch < s->out_channels; ch++)
1612 59910 output[ch] = s->outptr[channel_map[ch]];
1613
2/2
✓ Branch 0 taken 59910 times.
✓ Branch 1 taken 22186 times.
82096 for (ch = 0; ch < s->out_channels; ch++) {
1614
3/4
✓ Branch 0 taken 37724 times.
✓ Branch 1 taken 22186 times.
✓ Branch 2 taken 37724 times.
✗ Branch 3 not taken.
59910 if (!ch || channel_map[ch])
1615 59910 s->outptr[channel_map[ch]] += AC3_BLOCK_SIZE;
1616 }
1617 }
1618
1619 /* keep last block for error concealment in next frame */
1620
2/2
✓ Branch 0 taken 10965 times.
✓ Branch 1 taken 3861 times.
14826 for (ch = 0; ch < s->out_channels; ch++)
1621 10965 memcpy(s->output[ch + offset], output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
1622
1623 /* check if there is dependent frame */
1624
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 3542 times.
3861 if (buf_size > s->frame_size) {
1625 AC3HeaderInfo hdr;
1626 int err;
1627
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319 times.
319 if (buf_size - s->frame_size <= 16) {
1629 skip = buf_size - s->frame_size;
1630 goto skip;
1631 }
1632
1633
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 319 times.
319 if ((ret = init_get_bits8(&s->gbc, buf + s->frame_size, buf_size - s->frame_size)) < 0)
1634 return ret;
1635
1636 319 err = ff_ac3_parse_header(&s->gbc, &hdr);
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319 times.
319 if (err)
1638 return err;
1639
1640
1/2
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
319 if (hdr.frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
1641
2/4
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 319 times.
319 if (hdr.num_blocks != s->num_blocks || s->sample_rate != hdr.sample_rate) {
1642 av_log(avctx, AV_LOG_WARNING, "Ignoring non-compatible dependent frame.\n");
1643 } else {
1644 319 buf += s->frame_size;
1645 319 buf_size -= s->frame_size;
1646 319 s->prev_output_mode = s->output_mode;
1647 319 s->prev_bit_rate = s->bit_rate;
1648 319 got_independent_frame = 1;
1649 319 goto dependent_frame;
1650 }
1651 }
1652 }
1653 3542 skip:
1654
1655 3542 frame->decode_error_flags = err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0;
1656
1657 /* if frame is ok, set audio parameters */
1658
2/2
✓ Branch 0 taken 3535 times.
✓ Branch 1 taken 7 times.
3542 if (!err) {
1659 3535 avctx->sample_rate = s->sample_rate;
1660 3535 avctx->bit_rate = s->bit_rate + s->prev_bit_rate;
1661
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 3216 times.
3535 avctx->profile = s->eac3_extension_type_a == 1 ? AV_PROFILE_EAC3_DDP_ATMOS : AV_PROFILE_UNKNOWN;
1662 }
1663
1664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3542 times.
3542 if (!avctx->sample_rate) {
1665 av_log(avctx, AV_LOG_ERROR, "Could not determine the sample rate\n");
1666 return AVERROR_INVALIDDATA;
1667 }
1668
1669
2/2
✓ Branch 0 taken 56672 times.
✓ Branch 1 taken 3542 times.
60214 for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++)
1670 56672 extended_channel_map[ch] = ch;
1671
1672
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 3223 times.
3542 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
1673 319 uint64_t ich_layout = ff_ac3_channel_layout_tab[s->prev_output_mode & ~AC3_OUTPUT_LFEON];
1674 319 int channel_map_size = ff_ac3_channels_tab[s->output_mode & ~AC3_OUTPUT_LFEON] + s->lfe_on;
1675 uint64_t channel_layout;
1676 319 int extend = 0;
1677
1678
1/2
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
319 if (s->prev_output_mode & AC3_OUTPUT_LFEON)
1679 319 ich_layout |= AV_CH_LOW_FREQUENCY;
1680
1681 319 channel_layout = ich_layout;
1682
2/2
✓ Branch 0 taken 5104 times.
✓ Branch 1 taken 319 times.
5423 for (ch = 0; ch < 16; ch++) {
1683
2/2
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 4147 times.
5104 if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
1684 957 channel_layout |= ff_eac3_custom_channel_map_locations[ch][1];
1685 }
1686 }
1687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 319 times.
319 if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) {
1688 av_log(avctx, AV_LOG_ERROR, "Too many channels (%d) coded\n",
1689 av_popcount64(channel_layout));
1690 return AVERROR_INVALIDDATA;
1691 }
1692
1693 319 av_channel_layout_uninit(&avctx->ch_layout);
1694 319 av_channel_layout_from_mask(&avctx->ch_layout, channel_layout);
1695
1696
2/2
✓ Branch 0 taken 5104 times.
✓ Branch 1 taken 319 times.
5423 for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++) {
1697
2/2
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 4147 times.
5104 if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
1698
2/2
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 319 times.
957 if (ff_eac3_custom_channel_map_locations[ch][0]) {
1699 638 int index = av_channel_layout_index_from_channel(&avctx->ch_layout,
1700 638 ff_ctzll(ff_eac3_custom_channel_map_locations[ch][1]));
1701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 if (index < 0)
1702 return AVERROR_INVALIDDATA;
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 if (extend >= channel_map_size)
1704 break;
1705
1706 638 extended_channel_map[index] = offset + channel_map[extend++];
1707 } else {
1708 int i;
1709
1710
2/2
✓ Branch 0 taken 20416 times.
✓ Branch 1 taken 319 times.
20735 for (i = 0; i < 64; i++) {
1711
2/2
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 19778 times.
20416 if ((1ULL << i) & ff_eac3_custom_channel_map_locations[ch][1]) {
1712 638 int index = av_channel_layout_index_from_channel(&avctx->ch_layout, i);
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 if (index < 0)
1714 return AVERROR_INVALIDDATA;
1715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 if (extend >= channel_map_size)
1716 break;
1717
1718 638 extended_channel_map[index] = offset + channel_map[extend++];
1719 }
1720 }
1721 }
1722 }
1723 }
1724
1725 319 ac3_downmix(avctx);
1726 }
1727
1728 /* get output buffer */
1729 3542 frame->nb_samples = s->num_blocks * AC3_BLOCK_SIZE;
1730
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3542 times.
3542 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1731 return ret;
1732
1733
2/2
✓ Branch 0 taken 10327 times.
✓ Branch 1 taken 3542 times.
13869 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
1734 10327 int map = extended_channel_map[ch];
1735
2/4
✓ Branch 0 taken 10327 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10327 times.
10327 av_assert0(ch>=AV_NUM_DATA_POINTERS || frame->extended_data[ch] == frame->data[ch]);
1736 10327 memcpy((SHORTFLOAT *)frame->extended_data[ch],
1737 10327 s->output_buffer[map],
1738 10327 s->num_blocks * AC3_BLOCK_SIZE * sizeof(SHORTFLOAT));
1739 }
1740
1741 /*
1742 * AVMatrixEncoding
1743 *
1744 * Check whether the input layout is compatible, and make sure we're not
1745 * downmixing (else the matrix encoding is no longer applicable).
1746 */
1747 3542 matrix_encoding = AV_MATRIX_ENCODING_NONE;
1748
2/2
✓ Branch 0 taken 2381 times.
✓ Branch 1 taken 1161 times.
3542 if (s->channel_mode == AC3_CHMODE_STEREO &&
1749
1/2
✓ Branch 0 taken 2381 times.
✗ Branch 1 not taken.
2381 s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
1750
2/2
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 1398 times.
2381 if (s->dolby_surround_mode == AC3_DSURMOD_ON)
1751 983 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1398 times.
1398 else if (s->dolby_headphone_mode == AC3_DHEADPHONMOD_ON)
1753 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1754
2/2
✓ Branch 0 taken 589 times.
✓ Branch 1 taken 572 times.
1161 } else if (s->channel_mode >= AC3_CHMODE_2F2R &&
1755
2/2
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 228 times.
589 s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
1756
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 361 times.
361 switch (s->dolby_surround_ex_mode) {
1757 case AC3_DSUREXMOD_ON: // EX or PLIIx
1758 matrix_encoding = AV_MATRIX_ENCODING_DOLBYEX;
1759 break;
1760 case AC3_DSUREXMOD_PLIIZ:
1761 matrix_encoding = AV_MATRIX_ENCODING_DPLIIZ;
1762 break;
1763 361 default: // not indicated or off
1764 361 break;
1765 }
1766 }
1767
3/4
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 2559 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 983 times.
4525 if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
1768 983 (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1769 return ret;
1770
1771 /* AVDownmixInfo */
1772
2/2
✓ Branch 0 taken 1160 times.
✓ Branch 1 taken 2382 times.
3542 if ( (s->channel_mode > AC3_CHMODE_STEREO) &&
1773
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 417 times.
1160 ((s->output_mode & ~AC3_OUTPUT_LFEON) > AC3_CHMODE_STEREO)) {
1774 743 AVDownmixInfo *downmix_info = av_downmix_info_update_side_data(frame);
1775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 743 times.
743 if (!downmix_info)
1776 return AVERROR(ENOMEM);
1777
3/4
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 451 times.
743 switch (s->preferred_downmix) {
1778 96 case AC3_DMIXMOD_LTRT:
1779 96 downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_LTRT;
1780 96 break;
1781 case AC3_DMIXMOD_LORO:
1782 downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_LORO;
1783 break;
1784 196 case AC3_DMIXMOD_DPLII:
1785 196 downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_DPLII;
1786 196 break;
1787 451 default:
1788 451 downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_UNKNOWN;
1789 451 break;
1790 }
1791 743 downmix_info->center_mix_level = ff_ac3_gain_levels[s-> center_mix_level];
1792 743 downmix_info->center_mix_level_ltrt = ff_ac3_gain_levels[s-> center_mix_level_ltrt];
1793 743 downmix_info->surround_mix_level = ff_ac3_gain_levels[s-> surround_mix_level];
1794 743 downmix_info->surround_mix_level_ltrt = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
1795
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 451 times.
743 if (s->lfe_mix_level_exists)
1796 292 downmix_info->lfe_mix_level = ff_eac3_gain_levels_lfe[s->lfe_mix_level];
1797 else
1798 451 downmix_info->lfe_mix_level = 0.0; // -inf dB
1799 }
1800
1801 3542 *got_frame_ptr = 1;
1802
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3542 times.
3542 if (!s->superframe_size)
1804 return FFMIN(full_buf_size, s->frame_size + skip);
1805
1806 3542 return FFMIN(full_buf_size, s->superframe_size + skip);
1807 }
1808
1809 /**
1810 * Uninitialize the AC-3 decoder.
1811 */
1812 120 static av_cold int ac3_decode_end(AVCodecContext *avctx)
1813 {
1814 120 AC3DecodeContext *s = avctx->priv_data;
1815 120 av_tx_uninit(&s->tx_256);
1816 120 av_tx_uninit(&s->tx_128);
1817 120 av_freep(&s->fdsp);
1818 120 av_freep(&s->downmix_coeffs[0]);
1819
1820 120 return 0;
1821 }
1822
1823 #define OFFSET(x) offsetof(AC3DecodeContext, x)
1824 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1825