FFmpeg
Loading...
Searching...
No Matches
ac3dec.c
Go to the documentation of this file.
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
36#include "libavutil/crc.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. */
52static float dynamic_range_tab[256];
54/** scale factor for each decoded exponent: 2^-exp */
55static 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 */
67{
68 /* generate dynamic range table
69 reference: Section 7.7.1 Dynamic Range Control */
70 for (int i = 0; i < 256; i++) {
71 int v = (i >> 5) - ((i >> 7) << 3) - 5;
72 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 for (int i = 0; i < 256; i++) {
78 int v = (i >> 4) - ((i >> 7) << 4) - 4;
79 ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
80 }
82}
83#endif
84
85static void ac3_downmix(AVCodecContext *avctx)
86{
87 AC3DecodeContext *s = avctx->priv_data;
90
91 /* allow downmixing to stereo or mono */
92 if (avctx->ch_layout.nb_channels > 1 &&
93 !av_channel_layout_compare(&s->downmix_layout, &mono)) {
96 } else if (avctx->ch_layout.nb_channels > 2 &&
97 !av_channel_layout_compare(&s->downmix_layout, &stereo)) {
100 }
101}
102
103/**
104 * AVCodec initialization
105 */
107{
108 AC3DecodeContext *s = avctx->priv_data;
109 const float scale = 1.0f;
110 int i, ret;
111
112 s->avctx = avctx;
113
114 if ((ret = av_tx_init(&s->tx_128, &s->tx_fn_128, IMDCT_TYPE, 1, 128, &scale, 0)))
115 return ret;
116
117 if ((ret = av_tx_init(&s->tx_256, &s->tx_fn_256, IMDCT_TYPE, 1, 256, &scale, 0)))
118 return ret;
119
120 AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256);
121 ff_bswapdsp_init(&s->bdsp);
122
123#if (USE_FIXED)
125#else
127#endif
128 if (!s->fdsp)
129 return AVERROR(ENOMEM);
130
131 ff_ac3dsp_init(&s->ac3dsp);
132 av_lfg_init(&s->dith_state, 0);
133
134 if (USE_FIXED)
136 else
138
139 ac3_downmix(avctx);
140 s->downmixed = 1;
141
142 for (i = 0; i < AC3_MAX_CHANNELS; i++) {
143 s->xcfptr[i] = s->transform_coeffs[i];
144 s->dlyptr[i] = s->delay[i];
145 }
146
147#if USE_FIXED
149#else
150 static AVOnce init_static_once = AV_ONCE_INIT;
151 ff_thread_once(&init_static_once, ac3_float_tables_init);
152#endif
153
154 return 0;
155}
156
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 */
170static int parse_frame_header(AC3DecodeContext *s)
171{
172 AC3HeaderInfo hdr;
173 int err;
174
175 err = ff_ac3_parse_header(&s->gbc, &hdr);
176 if (err)
177 return err;
178
179 /* get decoding parameters from header info */
180 s->bit_alloc_params.sr_code = hdr.sr_code;
181 s->bitstream_id = hdr.bitstream_id;
182 s->bitstream_mode = hdr.bitstream_mode;
183 s->channel_mode = hdr.channel_mode;
184 s->lfe_on = hdr.lfe_on;
185 s->bit_alloc_params.sr_shift = hdr.sr_shift;
186 s->sample_rate = hdr.sample_rate;
187 s->bit_rate = hdr.bit_rate;
188 s->channels = hdr.channels;
189 s->fbw_channels = s->channels - s->lfe_on;
190 s->lfe_ch = s->fbw_channels + 1;
191 s->frame_size = hdr.frame_size;
192 s->superframe_size += hdr.frame_size;
193 s->preferred_downmix = AC3_DMIXMOD_NOTINDICATED;
194 if (hdr.bitstream_id <= 10) {
195 s->center_mix_level = hdr.center_mix_level;
196 s->surround_mix_level = hdr.surround_mix_level;
197 }
198 s->center_mix_level_ltrt = 4; // -3.0dB
199 s->surround_mix_level_ltrt = 4; // -3.0dB
200 s->lfe_mix_level_exists = 0;
201 s->num_blocks = hdr.num_blocks;
202 s->frame_type = hdr.frame_type;
203 s->substreamid = hdr.substreamid;
204 s->dolby_surround_mode = hdr.dolby_surround_mode;
205 s->dolby_surround_ex_mode = AC3_DSUREXMOD_NOTINDICATED;
206 s->dolby_headphone_mode = AC3_DHEADPHONMOD_NOTINDICATED;
207
208 if (s->lfe_on) {
209 s->start_freq[s->lfe_ch] = 0;
210 s->end_freq[s->lfe_ch] = 7;
211 s->num_exp_groups[s->lfe_ch] = 2;
212 s->channel_in_cpl[s->lfe_ch] = 0;
213 }
214
215 if (s->bitstream_id <= 10) {
216 s->eac3 = 0;
217 s->snr_offset_strategy = 2;
218 s->block_switch_syntax = 1;
219 s->dither_flag_syntax = 1;
220 s->bit_allocation_syntax = 1;
221 s->fast_gain_syntax = 0;
222 s->first_cpl_leak = 0;
223 s->dba_syntax = 1;
224 s->skip_syntax = 1;
225 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
226 /* volume control params */
227 for (int i = 0; i < (s->channel_mode ? 1 : 2); i++) {
228 s->dialog_normalization[i] = hdr.dialog_normalization[i];
229 if (s->dialog_normalization[i] == 0) {
230 s->dialog_normalization[i] = -31;
231 }
232 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 s->compression_exists[i] = hdr.compression_exists[i];
237 if (s->compression_exists[i]) {
238 s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(hdr.heavy_dynamic_range[i]);
239 }
240 }
241 return 0;
242 } else if (CONFIG_EAC3_DECODER) {
243 s->eac3 = 1;
244 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 */
255static int set_downmix_coeffs(AC3DecodeContext *s)
256{
257 int i;
258 float cmix = ff_ac3_gain_levels[s-> center_mix_level];
259 float smix = ff_ac3_gain_levels[s->surround_mix_level];
260 float norm0, norm1;
261 float downmix_coeffs[2][AC3_MAX_CHANNELS];
262
263 if (!s->downmix_coeffs[0]) {
264 s->downmix_coeffs[0] = av_malloc_array(2 * AC3_MAX_CHANNELS,
265 sizeof(**s->downmix_coeffs));
266 if (!s->downmix_coeffs[0])
267 return AVERROR(ENOMEM);
268 s->downmix_coeffs[1] = s->downmix_coeffs[0] + AC3_MAX_CHANNELS;
269 }
270
271 for (i = 0; i < s->fbw_channels; i++) {
272 downmix_coeffs[0][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][0]];
273 downmix_coeffs[1][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][1]];
274 }
275 if (s->channel_mode > 1 && s->channel_mode & 1) {
276 downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
277 }
278 if (s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
279 int nf = s->channel_mode - 2;
280 downmix_coeffs[0][nf] = downmix_coeffs[1][nf] = smix * LEVEL_MINUS_3DB;
281 }
282 if (s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
283 int nf = s->channel_mode - 4;
284 downmix_coeffs[0][nf] = downmix_coeffs[1][nf+1] = smix;
285 }
286
287 /* renormalize */
288 norm0 = norm1 = 0.0;
289 for (i = 0; i < s->fbw_channels; i++) {
290 norm0 += downmix_coeffs[0][i];
291 norm1 += downmix_coeffs[1][i];
292 }
293 norm0 = 1.0f / norm0;
294 norm1 = 1.0f / norm1;
295 for (i = 0; i < s->fbw_channels; i++) {
296 downmix_coeffs[0][i] *= norm0;
297 downmix_coeffs[1][i] *= norm1;
298 }
299
300 if (s->output_mode == AC3_CHMODE_MONO) {
301 for (i = 0; i < s->fbw_channels; i++)
302 downmix_coeffs[0][i] = (downmix_coeffs[0][i] +
303 downmix_coeffs[1][i]) * LEVEL_MINUS_3DB;
304 }
305 for (i = 0; i < s->fbw_channels; i++) {
306 s->downmix_coeffs[0][i] = FIXR12(downmix_coeffs[0][i]);
307 s->downmix_coeffs[1][i] = FIXR12(downmix_coeffs[1][i]);
308 }
309
310 return 0;
311}
312
313/**
314 * Decode the grouped exponents according to exponent strategy.
315 * reference: Section 7.1.3 Exponent Decoding
316 */
317static 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 group_size = exp_strategy + (exp_strategy == EXP_D45);
327 for (grp = 0, i = 0; grp < ngrps; grp++) {
328 expacc = get_bits(gbc, 7);
329 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 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][0];
334 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][1];
335 dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][2];
336 }
337
338 /* convert to absolute exps and expand groups */
339 prevexp = absexp;
340 for (i = 0, j = 0; i < ngrps * 3; i++) {
341 prevexp += dexp[i] - 2;
342 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 switch (group_size) {
347 case 4: dexps[j++] = prevexp;
348 dexps[j++] = prevexp;
350 case 2: dexps[j++] = prevexp;
352 case 1: dexps[j++] = prevexp;
353 }
354 }
355 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 */
363static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
364{
365 int bin, band, ch;
366
367 bin = s->start_freq[CPL_CH];
368 for (band = 0; band < s->num_cpl_bands; band++) {
369 int band_start = bin;
370 int band_end = bin + s->cpl_band_sizes[band];
371 for (ch = 1; ch <= s->fbw_channels; ch++) {
372 if (s->channel_in_cpl[ch]) {
373#if USE_FIXED
374 int cpl_coord = s->cpl_coords[ch][band] << 5;
375#else
376 float cpl_coord = s->cpl_coords[ch][band] * (1.0f / (1 << 23));
377#endif
378 for (bin = band_start; bin < band_end; bin++) {
379#if USE_FIXED
380 s->coeffs[ch][bin] =
381 MULH(s->coeffs[CPL_CH][bin] * (1 << 4), cpl_coord);
382#else
383 s->coeffs[ch][bin] = s->coeffs[CPL_CH][bin] * cpl_coord;
384#endif
385 }
386 if (ch == 2 && s->phase_flags[band]) {
387 for (bin = band_start; bin < band_end; bin++)
388 s->coeffs[2][bin] = -s->coeffs[2][bin];
389 }
390 }
391 }
392 bin = band_end;
393 }
394}
395
396/**
397 * Grouped mantissas for 3-level 5-level and 11-level quantization
398 */
399typedef struct mant_groups {
400 int b1_mant[2];
401 int b2_mant[2];
403 int b1;
404 int b2;
405 int b4;
407
408static av_always_inline INTFLOAT dequantize_coeff(int mantissa, int exponent,
409 int coeff_bits)
410{
411#if USE_FIXED
412 return (mantissa * (1 << coeff_bits)) >> exponent;
413#else
414 return mantissa * scale_factors[exponent];
415#endif
416}
417
418#if USE_FIXED
420{
421 int scaled = mantissa * (1 << AC3_FIXED_COEFF_BITS);
422 int round = 1 << (AC3_FIXED_EXPONENT_MAX - 1);
423
424 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 */
432static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
433{
434 int start_freq = s->start_freq[ch_index];
435 int end_freq = s->end_freq[ch_index];
436 uint8_t *baps = s->bap[ch_index];
437 int8_t *exps = s->dexps[ch_index];
438 INTFLOAT *coeffs = s->coeffs[ch_index];
439 int dither = (ch_index == CPL_CH) || s->dither_flag[ch_index];
440#if USE_FIXED
441 int coeff_bits = fixed_coeff_bits(s);
442#else
443 int coeff_bits = 0;
444#endif
445 GetBitContext *gbc = &s->gbc;
446 int freq;
447
448 for (freq = start_freq; freq < end_freq; freq++) {
449 int bap = baps[freq];
450 int mantissa;
451 switch (bap) {
452 case 0:
453 /* random noise with approximate range of -0.707 to 0.707 */
454 if (dither) {
455 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 if (coeff_bits && exps[freq] == AC3_FIXED_EXPONENT_MAX) {
460 coeffs[freq] = dequantize_dexp24_dither(mantissa);
461 continue;
462 }
463#endif
464 } else {
465 mantissa = 0;
466 }
467 break;
468 case 1:
469 if (m->b1) {
470 m->b1--;
471 mantissa = m->b1_mant[m->b1];
472 } else {
473 int bits = get_bits(gbc, 5);
474 mantissa = ff_ac3_bap1_mantissas[bits][0];
477 m->b1 = 2;
478 }
479 break;
480 case 2:
481 if (m->b2) {
482 m->b2--;
483 mantissa = m->b2_mant[m->b2];
484 } else {
485 int bits = get_bits(gbc, 7);
486 mantissa = ff_ac3_bap2_mantissas[bits][0];
489 m->b2 = 2;
490 }
491 break;
492 case 3:
493 mantissa = ff_ac3_bap3_mantissas[get_bits(gbc, 3)];
494 break;
495 case 4:
496 if (m->b4) {
497 m->b4 = 0;
498 mantissa = m->b4_mant;
499 } else {
500 int bits = get_bits(gbc, 7);
501 mantissa = ff_ac3_bap4_mantissas[bits][0];
503 m->b4 = 1;
504 }
505 break;
506 case 5:
507 mantissa = ff_ac3_bap5_mantissas[get_bits(gbc, 4)];
508 break;
509 default: /* 6 to 15 */
510 /* Shift mantissa and sign-extend it. */
511 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 mantissa = (unsigned)get_sbits(gbc, ff_ac3_quantization_tab[bap]) << (24 - ff_ac3_quantization_tab[bap]);
516 break;
517 }
518 coeffs[freq] = dequantize_coeff(mantissa, exps[freq], coeff_bits);
519 }
520}
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 */
527static void remove_dithering(AC3DecodeContext *s) {
528 int ch, i;
529
530 for (ch = 1; ch <= s->fbw_channels; ch++) {
531 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}
539
540static inline void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk,
541 int ch, mant_groups *m)
542{
543 if (!s->channel_uses_aht[ch]) {
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 if (CONFIG_EAC3_DECODER && !blk)
551 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
552 s->coeffs[ch][bin] = dequantize_coeff(
553 s->pre_mantissa[ch][bin][blk], s->dexps[ch][bin], 0);
554 }
555 }
556}
557
558/**
559 * Decode the transform coefficients.
560 */
561static inline void decode_transform_coeffs(AC3DecodeContext *s, int blk)
562{
563 int ch, end;
564 int got_cplchan = 0;
565 mant_groups m;
566
567 m.b1 = m.b2 = m.b4 = 0;
568
569 for (ch = 1; ch <= s->channels; ch++) {
570 /* transform coefficients for full-bandwidth channel */
572 /* transform coefficients for coupling channel come right after the
573 coefficients for the first coupled channel*/
574 if (s->channel_in_cpl[ch]) {
575 if (!got_cplchan) {
578 got_cplchan = 1;
579 }
580 end = s->end_freq[CPL_CH];
581 } else {
582 end = s->end_freq[ch];
583 }
584 do
585 s->coeffs[ch][end] = 0;
586 while (++end < 256);
587 }
588
589 /* zero the dithered coefficients for appropriate channels */
591}
592
593/**
594 * Stereo rematrixing.
595 * reference: Section 7.5.4 Rematrixing : Decoding Technique
596 */
597static void do_rematrixing(AC3DecodeContext *s)
598{
599 int bnd, i;
600 int end, bndend;
601
602 end = FFMIN(s->end_freq[1], s->end_freq[2]);
603
604 for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
605 if (s->rematrixing_flags[bnd]) {
606 bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd + 1]);
607 for (i = ff_ac3_rematrix_band_tab[bnd]; i < bndend; i++) {
608 INTFLOAT tmp0 = s->coeffs[1][i];
609 s->coeffs[1][i] += s->coeffs[2][i];
610 s->coeffs[2][i] = tmp0 - s->coeffs[2][i];
611 }
612 }
613 }
614}
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 */
621static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
622{
623 int ch;
624#if USE_FIXED
625 int window_bits = 8 + fixed_coeff_bits(s);
626#endif
627
628 for (ch = 1; ch <= channels; ch++) {
629 if (s->block_switch[ch]) {
630 int i;
631 INTFLOAT *x = s->tmp_output + 128;
632 for (i = 0; i < 128; i++)
633 x[i] = s->transform_coeffs[ch][2 * i];
634 s->tx_fn_128(s->tx_128, s->tmp_output, x, sizeof(INTFLOAT));
635#if USE_FIXED
636 s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
637 s->tmp_output, s->window, 128, window_bits);
638#else
639 s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
640 s->tmp_output, s->window, 128);
641#endif
642 for (i = 0; i < 128; i++)
643 x[i] = s->transform_coeffs[ch][2 * i + 1];
644 s->tx_fn_128(s->tx_128, s->delay[ch - 1 + offset], x, sizeof(INTFLOAT));
645 } else {
646 s->tx_fn_256(s->tx_256, s->tmp_output, s->transform_coeffs[ch], sizeof(INTFLOAT));
647#if USE_FIXED
648 s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
649 s->tmp_output, s->window, 128, window_bits);
650#else
651 s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
652 s->tmp_output, s->window, 128);
653#endif
654 memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(INTFLOAT));
655 }
656 }
657}
658
659/**
660 * Upmix delay samples from stereo to original channel layout.
661 */
662static void ac3_upmix_delay(AC3DecodeContext *s)
663{
664 int channel_data_size = sizeof(s->delay[0]);
665 switch (s->channel_mode) {
668 /* upmix mono to stereo */
669 memcpy(s->delay[1], s->delay[0], channel_data_size);
670 break;
671 case AC3_CHMODE_2F2R:
672 memset(s->delay[3], 0, channel_data_size);
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);
680 case AC3_CHMODE_3F1R:
681 memset(s->delay[3], 0, channel_data_size);
683 case AC3_CHMODE_3F:
684 memcpy(s->delay[2], s->delay[1], channel_data_size);
685 memset(s->delay[1], 0, channel_data_size);
686 break;
687 }
688}
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 */
707static 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 int subbnd, bnd, n_subbands, n_bands=0;
714 uint8_t bnd_sz[22];
715
716 n_subbands = end_subband - start_subband;
717
718 if (!blk)
719 memcpy(band_struct, default_band_struct, band_struct_size);
720
721 av_assert0(band_struct_size >= start_subband + n_subbands);
722
723 band_struct += start_subband + 1;
724
725 /* decode band structure from bitstream or use default */
726 if (!eac3 || get_bits1(gbc)) {
727 for (subbnd = 0; subbnd < n_subbands - 1; subbnd++) {
728 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 if (num_bands || band_sizes ) {
736 n_bands = n_subbands;
737 bnd_sz[0] = ecpl ? 6 : 12;
738 for (bnd = 0, subbnd = 1; subbnd < n_subbands; subbnd++) {
739 int subbnd_size = (ecpl && subbnd < 4) ? 6 : 12;
740 if (band_struct[subbnd - 1]) {
741 n_bands--;
742 bnd_sz[bnd] += subbnd_size;
743 } else {
744 bnd_sz[++bnd] = subbnd_size;
745 }
746 }
747 }
748
749 /* set optional output params */
750 if (num_bands)
751 *num_bands = n_bands;
752 if (band_sizes)
753 memcpy(band_sizes, bnd_sz, n_bands);
754}
755
756static inline int spx_strategy(AC3DecodeContext *s, int blk)
757{
758 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 if (s->channel_mode == AC3_CHMODE_MONO) {
764 s->channel_uses_spx[1] = 1;
765 } else {
766 unsigned channel_uses_spx = get_bits(bc, s->fbw_channels);
767 for (int ch = s->fbw_channels; ch >= 1; --ch) {
768 s->channel_uses_spx[ch] = channel_uses_spx & 1;
769 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 dst_start_freq = get_bits(bc, 2);
776 start_subband = get_bits(bc, 3) + 2;
777 if (start_subband > 7)
778 start_subband += start_subband - 7;
779 end_subband = get_bits(bc, 3) + 5;
780#if USE_FIXED
781 s->spx_dst_end_freq = end_freq_inv_tab[end_subband-5];
782#endif
783 if (end_subband > 7)
784 end_subband += end_subband - 7;
785 dst_start_freq = dst_start_freq * 12 + 25;
786 src_start_freq = start_subband * 12 + 25;
787 dst_end_freq = end_subband * 12 + 25;
788
789 /* check validity of spx ranges */
790 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 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 s->spx_dst_start_freq = dst_start_freq;
802 s->spx_src_start_freq = src_start_freq;
803 if (!USE_FIXED)
804 s->spx_dst_end_freq = dst_end_freq;
805
806 decode_band_structure(bc, blk, s->eac3, 0,
807 start_subband, end_subband,
809 &s->num_spx_bands,
810 s->spx_band_sizes,
811 s->spx_band_struct, sizeof(s->spx_band_struct));
812 return 0;
813}
814
815static inline void spx_coordinates(AC3DecodeContext *s)
816{
817 GetBitContext *bc = &s->gbc;
818 int fbw_channels = s->fbw_channels;
819 int ch, bnd;
820
821 for (ch = 1; ch <= fbw_channels; ch++) {
822 if (s->channel_uses_spx[ch]) {
823 if (s->first_spx_coords[ch] || get_bits1(bc)) {
824 INTFLOAT spx_blend;
825 int bin, master_spx_coord;
826
827 s->first_spx_coords[ch] = 0;
828 spx_blend = AC3_SPX_BLEND(get_bits(bc, 5));
829 master_spx_coord = get_bits(bc, 2) * 3;
830
831 bin = s->spx_src_start_freq;
832 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
833 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 int64_t accu = ((bin << 23) + (bandsize << 22))
839 * (int64_t)s->spx_dst_end_freq;
840 nratio = (int)(accu >> 32);
841 nratio -= spx_blend << 18;
842
843 if (nratio < 0) {
844 nblend = 0;
845 sblend = 0x800000;
846 } else if (nratio > 0x7fffff) {
847 nblend = 14529495; // sqrt(3) in FP.23
848 sblend = 0;
849 } else {
850 nblend = fixed_sqrt(nratio, 23);
851 accu = (int64_t)nblend * 1859775393;
852 nblend = (int)((accu + (1<<29)) >> 30);
853 sblend = fixed_sqrt(0x800000 - nratio, 23);
854 }
855#else
856 float spx_coord;
857
858 /* calculate blending factors */
859 nratio = ((float)((bin + (bandsize >> 1))) / s->spx_dst_end_freq) - spx_blend;
860 nratio = av_clipf(nratio, 0.0f, 1.0f);
861 nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3)
862 // to give unity variance
863 sblend = sqrtf(1.0f - nratio);
864#endif
865 bin += bandsize;
866
867 /* decode spx coordinates */
868 spx_coord_exp = get_bits(bc, 4);
869 spx_coord_mant = get_bits(bc, 2);
870 if (spx_coord_exp == 15) spx_coord_mant <<= 1;
871 else spx_coord_mant += 4;
872 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 accu = (int64_t)nblend * spx_coord_mant;
877 s->spx_noise_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
878 accu = (int64_t)sblend * spx_coord_mant;
879 s->spx_signal_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
880#else
881 spx_coord = spx_coord_mant * (1.0f / (1 << 23));
882 s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
883 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}
892
893static inline int coupling_strategy(AC3DecodeContext *s, int blk,
894 uint8_t *bit_alloc_stages)
895{
896 GetBitContext *bc = &s->gbc;
897 int fbw_channels = s->fbw_channels;
898 int channel_mode = s->channel_mode;
899 int ch;
900
901 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
902 if (!s->eac3)
903 s->cpl_in_use[blk] = get_bits1(bc);
904 if (s->cpl_in_use[blk]) {
905 /* coupling in use */
906 int cpl_start_subband, cpl_end_subband;
907
908 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 if (s->eac3 && get_bits1(bc)) {
915 /* TODO: parse enhanced coupling strategy info */
916 avpriv_request_sample(s->avctx, "Enhanced coupling");
918 }
919
920 /* determine which channels are coupled */
921 if (s->eac3 && s->channel_mode == AC3_CHMODE_STEREO) {
922 s->channel_in_cpl[1] = 1;
923 s->channel_in_cpl[2] = 1;
924 } else {
925 for (ch = 1; ch <= fbw_channels; ch++)
926 s->channel_in_cpl[ch] = get_bits1(bc);
927 }
928
929 /* phase flags in use */
930 if (channel_mode == AC3_CHMODE_STEREO)
931 s->phase_flags_in_use = get_bits1(bc);
932
933 /* coupling frequency range */
934 cpl_start_subband = get_bits(bc, 4);
935 cpl_end_subband = s->spx_in_use ? (s->spx_src_start_freq - 37) / 12 :
936 get_bits(bc, 4) + 3;
937 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 s->start_freq[CPL_CH] = cpl_start_subband * 12 + 37;
943 s->end_freq[CPL_CH] = cpl_end_subband * 12 + 37;
944
945 decode_band_structure(bc, blk, s->eac3, 0, cpl_start_subband,
946 cpl_end_subband,
948 &s->num_cpl_bands, s->cpl_band_sizes,
949 s->cpl_band_struct, sizeof(s->cpl_band_struct));
950 } else {
951 /* coupling not in use */
952 for (ch = 1; ch <= fbw_channels; ch++) {
953 s->channel_in_cpl[ch] = 0;
954 s->first_cpl_coords[ch] = 1;
955 }
956 s->first_cpl_leak = s->eac3;
957 s->phase_flags_in_use = 0;
958 }
959
960 return 0;
961}
962
963static inline int coupling_coordinates(AC3DecodeContext *s, int blk)
964{
965 GetBitContext *bc = &s->gbc;
966 int fbw_channels = s->fbw_channels;
967 int ch, bnd;
968 int cpl_coords_exist = 0;
969
970 for (ch = 1; ch <= fbw_channels; ch++) {
971 if (s->channel_in_cpl[ch]) {
972 if ((s->eac3 && s->first_cpl_coords[ch]) || get_bits1(bc)) {
973 int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
974 s->first_cpl_coords[ch] = 0;
975 cpl_coords_exist = 1;
976 master_cpl_coord = 3 * get_bits(bc, 2);
977 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
978 cpl_coord_exp = get_bits(bc, 4);
979 cpl_coord_mant = get_bits(bc, 4);
980 if (cpl_coord_exp == 15)
981 s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
982 else
983 s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
984 s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
985 }
986 } 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 if (s->channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
998 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
999 s->phase_flags[bnd] = s->phase_flags_in_use ? get_bits1(bc) : 0;
1000 }
1001 }
1002
1003 return 0;
1004}
1005
1006/**
1007 * Decode a single audio block from the AC-3 bitstream.
1008 */
1009static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
1010{
1011 int fbw_channels = s->fbw_channels;
1012 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 GetBitContext *gbc = &s->gbc;
1018 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS] = { 0 };
1019
1020 /* block switch flags */
1021 different_transforms = 0;
1022 if (s->block_switch_syntax) {
1023 for (ch = 1; ch <= fbw_channels; ch++) {
1024 s->block_switch[ch] = get_bits1(gbc);
1025 if (ch > 1 && s->block_switch[ch] != s->block_switch[1])
1026 different_transforms = 1;
1027 }
1028 }
1029
1030 /* dithering flags */
1031 if (s->dither_flag_syntax) {
1032 for (ch = 1; ch <= fbw_channels; ch++) {
1033 s->dither_flag[ch] = get_bits1(gbc);
1034 }
1035 }
1036
1037 /* dynamic range */
1038 i = !s->channel_mode;
1039 do {
1040 if (get_bits1(gbc)) {
1041 /* Allow asymmetric application of DRC when drc_scale > 1.
1042 Amplification of quiet sounds is enhanced */
1043 int range_bits = get_bits(gbc, 8);
1044 INTFLOAT range = AC3_RANGE(range_bits);
1045 if (range_bits <= 127 || s->drc_scale <= 1.0)
1046 s->dynamic_range[i] = AC3_DYNAMIC_RANGE(range);
1047 else
1048 s->dynamic_range[i] = range;
1049 } else if (blk == 0) {
1050 s->dynamic_range[i] = AC3_DYNAMIC_RANGE1;
1051 }
1052 } while (i--);
1053
1054 /* spectral extension strategy */
1055 if (s->eac3 && (!blk || get_bits1(gbc))) {
1056 s->spx_in_use = get_bits1(gbc);
1057 if (s->spx_in_use) {
1058 if ((ret = spx_strategy(s, blk)) < 0)
1059 return ret;
1060 }
1061 }
1062 if (!s->eac3 || !s->spx_in_use) {
1063 s->spx_in_use = 0;
1064 for (ch = 1; ch <= fbw_channels; ch++) {
1065 s->channel_uses_spx[ch] = 0;
1066 s->first_spx_coords[ch] = 1;
1067 }
1068 }
1069
1070 /* spectral extension coordinates */
1071 if (s->spx_in_use)
1073
1074 /* coupling strategy */
1075 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
1076 if ((ret = coupling_strategy(s, blk, bit_alloc_stages)) < 0)
1077 return ret;
1078 } else if (!s->eac3) {
1079 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 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
1085 }
1086 }
1087 cpl_in_use = s->cpl_in_use[blk];
1088
1089 /* coupling coordinates */
1090 if (cpl_in_use) {
1091 if ((ret = coupling_coordinates(s, blk)) < 0)
1092 return ret;
1093 }
1094
1095 /* stereo rematrixing strategy and band structure */
1096 if (channel_mode == AC3_CHMODE_STEREO) {
1097 if ((s->eac3 && !blk) || get_bits1(gbc)) {
1098 s->num_rematrixing_bands = 4;
1099 if (cpl_in_use && s->start_freq[CPL_CH] <= 61) {
1100 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
1101 } else if (s->spx_in_use && s->spx_src_start_freq <= 61) {
1102 s->num_rematrixing_bands--;
1103 }
1104 for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++)
1105 s->rematrixing_flags[bnd] = get_bits1(gbc);
1106 } 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 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1115 if (!s->eac3)
1116 s->exp_strategy[blk][ch] = get_bits(gbc, 2 - (ch == s->lfe_ch));
1117 if (s->exp_strategy[blk][ch] != EXP_REUSE)
1118 bit_alloc_stages[ch] = 3;
1119 }
1120
1121 /* channel bandwidth */
1122 for (ch = 1; ch <= fbw_channels; ch++) {
1123 s->start_freq[ch] = 0;
1124 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
1125 int group_size;
1126 int prev = s->end_freq[ch];
1127 if (s->channel_in_cpl[ch])
1128 s->end_freq[ch] = s->start_freq[CPL_CH];
1129 else if (s->channel_uses_spx[ch])
1130 s->end_freq[ch] = s->spx_src_start_freq;
1131 else {
1132 int bandwidth_code = get_bits(gbc, 6);
1133 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 s->end_freq[ch] = bandwidth_code * 3 + 73;
1138 }
1139 group_size = 3 << (s->exp_strategy[blk][ch] - 1);
1140 s->num_exp_groups[ch] = (s->end_freq[ch] + group_size-4) / group_size;
1141 if (blk > 0 && s->end_freq[ch] != prev)
1142 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
1143 }
1144 }
1145 if (cpl_in_use && s->exp_strategy[blk][CPL_CH] != EXP_REUSE) {
1146 s->num_exp_groups[CPL_CH] = (s->end_freq[CPL_CH] - s->start_freq[CPL_CH]) /
1147 (3 << (s->exp_strategy[blk][CPL_CH] - 1));
1148 }
1149
1150 /* decode exponents for each channel */
1151 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1152 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
1153 s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
1154 if (decode_exponents(s, gbc, s->exp_strategy[blk][ch],
1155 s->num_exp_groups[ch], s->dexps[ch][0],
1156 &s->dexps[ch][s->start_freq[ch]+!!ch])) {
1157 return AVERROR_INVALIDDATA;
1158 }
1159 if (ch != CPL_CH && ch != s->lfe_ch)
1160 skip_bits(gbc, 2); /* skip gainrng */
1161 }
1162 }
1163
1164 /* bit allocation information */
1165 if (s->bit_allocation_syntax) {
1166 if (get_bits1(gbc)) {
1167 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1168 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1169 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
1170 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
1171 s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)];
1172 for (ch = !cpl_in_use; ch <= s->channels; ch++)
1173 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1174 } 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 if (!s->eac3 || !blk) {
1183 if (s->snr_offset_strategy && get_bits1(gbc)) {
1184 int snr = 0;
1185 int csnr;
1186 csnr = (get_bits(gbc, 6) - 15) << 4;
1187 for (i = ch = !cpl_in_use; ch <= s->channels; ch++) {
1188 /* snr offset */
1189 if (ch == i || s->snr_offset_strategy == 2)
1190 snr = (csnr + get_bits(gbc, 4)) << 2;
1191 /* run at least last bit allocation stage if snr offset changes */
1192 if (blk && s->snr_offset[ch] != snr) {
1193 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 1);
1194 }
1195 s->snr_offset[ch] = snr;
1196
1197 /* fast gain (normal AC-3 only) */
1198 if (!s->eac3) {
1199 int prev = s->fast_gain[ch];
1200 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 if (blk && prev != s->fast_gain[ch])
1203 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1204 }
1205 }
1206 } 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 if (s->fast_gain_syntax && get_bits1(gbc)) {
1214 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1215 int prev = s->fast_gain[ch];
1216 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 if (blk && prev != s->fast_gain[ch])
1219 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1220 }
1221 } else if (s->eac3 && !blk) {
1222 for (ch = !cpl_in_use; ch <= s->channels; ch++)
1223 s->fast_gain[ch] = ff_ac3_fast_gain_tab[4];
1224 }
1225
1226 /* E-AC-3 to AC-3 converter SNR offset */
1227 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && get_bits1(gbc)) {
1228 skip_bits(gbc, 10); // skip converter snr offset
1229 }
1230
1231 /* coupling leak information */
1232 if (cpl_in_use) {
1233 if (s->first_cpl_leak || get_bits1(gbc)) {
1234 int fl = get_bits(gbc, 3);
1235 int sl = get_bits(gbc, 3);
1236 /* run last 2 bit allocation stages for coupling channel if
1237 coupling leak changes */
1238 if (blk && (fl != s->bit_alloc_params.cpl_fast_leak ||
1239 sl != s->bit_alloc_params.cpl_slow_leak)) {
1240 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
1241 }
1242 s->bit_alloc_params.cpl_fast_leak = fl;
1243 s->bit_alloc_params.cpl_slow_leak = sl;
1244 } 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 s->first_cpl_leak = 0;
1250 }
1251
1252 /* delta bit allocation information */
1253 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 } else if (blk == 0) {
1277 for (ch = 0; ch <= s->channels; ch++) {
1278 s->dba_mode[ch] = DBA_NONE;
1279 }
1280 }
1281
1282 /* Bit allocation */
1283 for (ch = !cpl_in_use; ch <= s->channels; ch++) {
1284 if (bit_alloc_stages[ch] > 2) {
1285 /* Exponent mapping into PSD and PSD integration */
1286 ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
1287 s->start_freq[ch], s->end_freq[ch],
1288 s->psd[ch], s->band_psd[ch]);
1289 }
1290 if (bit_alloc_stages[ch] > 1) {
1291 /* Compute excitation function, Compute masking curve, and
1292 Apply delta bit allocation */
1293 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 s->fast_gain[ch], (ch == s->lfe_ch),
1296 s->dba_mode[ch], s->dba_nsegs[ch],
1297 s->dba_offsets[ch], s->dba_lengths[ch],
1298 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 if (bit_alloc_stages[ch] > 0) {
1304 /* Compute bit allocation */
1305 const uint8_t *bap_tab = s->channel_uses_aht[ch] ?
1307 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 bap_tab, s->bap[ch]);
1312 }
1313 }
1314
1315 /* unused dummy data */
1316 if (s->skip_syntax && get_bits1(gbc)) {
1317 int skipl = get_bits(gbc, 9);
1318 skip_bits_long(gbc, 8 * skipl);
1319 }
1320
1321 /* unpack the transform coefficients
1322 this also uncouples channels if coupling is in use. */
1324
1325 /* TODO: generate enhanced coupling coordinates and uncouple */
1326
1327 /* recover coefficients if rematrixing is in use */
1328 if (s->channel_mode == AC3_CHMODE_STEREO)
1330
1331 /* apply scaling to coefficients (headroom, dynrng) */
1332 for (ch = 1; ch <= s->channels; ch++) {
1333 int audio_channel = 0;
1334 INTFLOAT gain;
1335 if (s->channel_mode == AC3_CHMODE_DUALMONO && ch <= 2)
1336 audio_channel = 2-ch;
1337 if (s->heavy_compression && s->compression_exists[audio_channel])
1338 gain = s->heavy_dynamic_range[audio_channel];
1339 else
1340 gain = s->dynamic_range[audio_channel];
1341
1342#if USE_FIXED
1343 if (fixed_coeff_bits(s))
1344 scale_coefs_q2(s->transform_coeffs[ch], s->coeffs[ch], gain,
1345 256);
1346 else
1347 scale_coefs(s->transform_coeffs[ch], s->coeffs[ch], gain, 256);
1348#else
1349 if (s->target_level != 0)
1350 gain = gain * s->level_gain[audio_channel];
1351 gain *= 1.0f / 4194304.0f;
1352 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 if (CONFIG_EAC3_DECODER && s->spx_in_use) {
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 downmix_output = s->channels != s->out_channels &&
1366 !((s->output_mode & AC3_OUTPUT_LFEON) &&
1367 s->fbw_channels == s->out_channels);
1368 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 if (s->downmixed) {
1372 s->downmixed = 0;
1374 }
1375
1376 do_imdct(s, s->channels, offset);
1377
1378 if (downmix_output) {
1379#if USE_FIXED
1380 ac3_downmix_c_fixed16(s->outptr, s->downmix_coeffs,
1381 s->out_channels, s->fbw_channels, 256);
1382#else
1383 ff_ac3dsp_downmix(&s->ac3dsp, s->outptr, s->downmix_coeffs,
1384 s->out_channels, s->fbw_channels, 256);
1385#endif
1386 }
1387 } else {
1388 if (downmix_output) {
1389 AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->xcfptr + 1, s->downmix_coeffs,
1390 s->out_channels, s->fbw_channels, 256);
1391 }
1392
1393 if (downmix_output && !s->downmixed) {
1394 s->downmixed = 1;
1395 AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->dlyptr, s->downmix_coeffs,
1396 s->out_channels, s->fbw_channels, 128);
1397 }
1398
1399 do_imdct(s, s->out_channels, offset);
1400 }
1401
1402 return 0;
1403}
1404
1405/**
1406 * Decode a single AC-3 frame.
1407 */
1409 int *got_frame_ptr, AVPacket *avpkt)
1410{
1411 const uint8_t *buf = avpkt->data;
1412 int buf_size, full_buf_size = avpkt->size;
1413 AC3DecodeContext *s = avctx->priv_data;
1414 int blk, ch, err, offset, ret;
1415 int i;
1416 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 s->superframe_size = 0;
1424
1425 buf_size = full_buf_size;
1426 i = ff_ac3_find_syncword(buf, buf_size);
1427 if (i < 0 || i > 10)
1428 return i;
1429 buf += i;
1430 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 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 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 if (s->consistent_noise_generation)
1446 av_lfg_init_from_data(&s->dith_state, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
1447
1448 buf = s->input_buffer;
1449dependent_frame:
1450 /* initialize the GetBitContext with the start of valid AC-3 Frame */
1451 if ((ret = init_get_bits8(&s->gbc, buf, buf_size)) < 0)
1452 return ret;
1453
1454 /* parse the syncinfo */
1455 err = parse_frame_header(s);
1456
1457 if (err) {
1458 switch (err) {
1460 av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
1461 return AVERROR_INVALIDDATA;
1463 av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
1464 break;
1466 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
1467 break;
1469 av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
1470 break;
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;
1485 av_log(avctx, AV_LOG_ERROR, "invalid channel map\n");
1486 return AVERROR_INVALIDDATA;
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 if (s->frame_size > buf_size) {
1496 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1498 } 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 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 if (!err || (s->channels && s->out_channels != s->channels)) {
1518 s->out_channels = s->channels;
1519 s->output_mode = s->channel_mode;
1520 if (s->lfe_on)
1521 s->output_mode |= AC3_OUTPUT_LFEON;
1522 if (s->channels > 1 &&
1524 s->out_channels = 1;
1525 s->output_mode = AC3_CHMODE_MONO;
1526 } else if (s->channels > 2 &&
1528 s->out_channels = 2;
1529 s->output_mode = AC3_CHMODE_STEREO;
1530 }
1531
1532 s->loro_center_mix_level = ff_ac3_gain_levels[s-> center_mix_level];
1533 s->loro_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level];
1534 s->ltrt_center_mix_level = ff_ac3_gain_levels[s-> center_mix_level_ltrt];
1535 s->ltrt_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
1536 switch (s->preferred_downmix) {
1537 case AC3_DMIXMOD_LTRT:
1538 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_LTRT;
1539 break;
1540 case AC3_DMIXMOD_LORO:
1541 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_LORO;
1542 break;
1543 case AC3_DMIXMOD_DPLII:
1544 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_DPLII;
1545 break;
1546 default:
1547 s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_UNKNOWN;
1548 break;
1549 }
1550 /* set downmixing coefficients if needed */
1551 if (s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
1552 s->fbw_channels == s->out_channels)) {
1553 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 } else if (!s->channels) {
1559 av_log(avctx, AV_LOG_ERROR, "unable to determine channel mode\n");
1560 return AVERROR_INVALIDDATA;
1561 }
1562
1563 mask = ff_ac3_channel_layout_tab[s->output_mode & ~AC3_OUTPUT_LFEON];
1564 if (s->output_mode & AC3_OUTPUT_LFEON)
1566
1569
1570 /* set audio service type based on bitstream mode for AC-3 */
1571 avctx->audio_service_type = s->bitstream_mode;
1572 if (s->bitstream_mode == 0x7 && s->channels > 1)
1574
1575 /* decode the audio blocks */
1576 channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
1577 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 if (!err) {
1586 const int coeff_bits = fixed_coeff_bits(s);
1587 const int slot = offset ? 1 : 0;
1588
1589 if (s->delay_coeff_bits[slot] != coeff_bits) {
1590 memset(s->delay[offset], 0, AC3_MAX_CHANNELS * sizeof(s->delay[0]));
1591 s->delay_coeff_bits[slot] = coeff_bits;
1592 }
1593 }
1594#endif
1595 for (ch = 0; ch < AC3_MAX_CHANNELS; ch++) {
1596 output[ch] = s->output[ch + offset];
1597 s->outptr[ch] = s->output[ch + offset];
1598 }
1599 for (ch = 0; ch < s->channels; ch++) {
1600 if (ch < s->out_channels)
1601 s->outptr[channel_map[ch]] = s->output_buffer[ch + offset];
1602 }
1603 for (blk = 0; blk < s->num_blocks; blk++) {
1604 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 if (err)
1609 for (ch = 0; ch < s->out_channels; ch++)
1610 memcpy(s->output_buffer[ch + offset] + AC3_BLOCK_SIZE*blk, output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
1611 for (ch = 0; ch < s->out_channels; ch++)
1612 output[ch] = s->outptr[channel_map[ch]];
1613 for (ch = 0; ch < s->out_channels; ch++) {
1614 if (!ch || channel_map[ch])
1615 s->outptr[channel_map[ch]] += AC3_BLOCK_SIZE;
1616 }
1617 }
1618
1619 /* keep last block for error concealment in next frame */
1620 for (ch = 0; ch < s->out_channels; ch++)
1621 memcpy(s->output[ch + offset], output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
1622
1623 /* check if there is dependent frame */
1624 if (buf_size > s->frame_size) {
1625 AC3HeaderInfo hdr;
1626 int err;
1627
1628 if (buf_size - s->frame_size <= 16) {
1629 skip = buf_size - s->frame_size;
1630 goto skip;
1631 }
1632
1633 if ((ret = init_get_bits8(&s->gbc, buf + s->frame_size, buf_size - s->frame_size)) < 0)
1634 return ret;
1635
1636 err = ff_ac3_parse_header(&s->gbc, &hdr);
1637 if (err)
1638 return err;
1639
1641 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 buf += s->frame_size;
1645 buf_size -= s->frame_size;
1646 s->prev_output_mode = s->output_mode;
1647 s->prev_bit_rate = s->bit_rate;
1648 got_independent_frame = 1;
1649 goto dependent_frame;
1650 }
1651 }
1652 }
1653skip:
1654
1655 frame->decode_error_flags = err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0;
1656
1657 /* if frame is ok, set audio parameters */
1658 if (!err) {
1659 avctx->sample_rate = s->sample_rate;
1660 avctx->bit_rate = s->bit_rate + s->prev_bit_rate;
1661 avctx->profile = s->eac3_extension_type_a == 1 ? AV_PROFILE_EAC3_DDP_ATMOS : AV_PROFILE_UNKNOWN;
1662 }
1663
1664 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 for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++)
1670 extended_channel_map[ch] = ch;
1671
1672 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
1673 uint64_t ich_layout = ff_ac3_channel_layout_tab[s->prev_output_mode & ~AC3_OUTPUT_LFEON];
1674 int channel_map_size = ff_ac3_channels_tab[s->output_mode & ~AC3_OUTPUT_LFEON] + s->lfe_on;
1675 uint64_t channel_layout;
1676 int extend = 0;
1677
1678 if (s->prev_output_mode & AC3_OUTPUT_LFEON)
1679 ich_layout |= AV_CH_LOW_FREQUENCY;
1680
1681 channel_layout = ich_layout;
1682 for (ch = 0; ch < 16; ch++) {
1683 if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
1684 channel_layout |= ff_eac3_custom_channel_map_locations[ch][1];
1685 }
1686 }
1687 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
1694 av_channel_layout_from_mask(&avctx->ch_layout, channel_layout);
1695
1696 for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++) {
1697 if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
1701 if (index < 0)
1702 return AVERROR_INVALIDDATA;
1703 if (extend >= channel_map_size)
1704 break;
1705
1706 extended_channel_map[index] = offset + channel_map[extend++];
1707 } else {
1708 int i;
1709
1710 for (i = 0; i < 64; i++) {
1711 if ((1ULL << i) & ff_eac3_custom_channel_map_locations[ch][1]) {
1713 if (index < 0)
1714 return AVERROR_INVALIDDATA;
1715 if (extend >= channel_map_size)
1716 break;
1717
1718 extended_channel_map[index] = offset + channel_map[extend++];
1719 }
1720 }
1721 }
1722 }
1723 }
1724
1725 ac3_downmix(avctx);
1726 }
1727
1728 /* get output buffer */
1729 frame->nb_samples = s->num_blocks * AC3_BLOCK_SIZE;
1730 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1731 return ret;
1732
1733 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
1734 int map = extended_channel_map[ch];
1735 av_assert0(ch>=AV_NUM_DATA_POINTERS || frame->extended_data[ch] == frame->data[ch]);
1736 memcpy((SHORTFLOAT *)frame->extended_data[ch],
1737 s->output_buffer[map],
1738 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 matrix_encoding = AV_MATRIX_ENCODING_NONE;
1748 if (s->channel_mode == AC3_CHMODE_STEREO &&
1749 s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
1750 if (s->dolby_surround_mode == AC3_DSURMOD_ON)
1751 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1752 else if (s->dolby_headphone_mode == AC3_DHEADPHONMOD_ON)
1753 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1754 } else if (s->channel_mode >= AC3_CHMODE_2F2R &&
1755 s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
1756 switch (s->dolby_surround_ex_mode) {
1757 case AC3_DSUREXMOD_ON: // EX or PLIIx
1758 matrix_encoding = AV_MATRIX_ENCODING_DOLBYEX;
1759 break;
1761 matrix_encoding = AV_MATRIX_ENCODING_DPLIIZ;
1762 break;
1763 default: // not indicated or off
1764 break;
1765 }
1766 }
1767 if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
1768 (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1769 return ret;
1770
1771 /* AVDownmixInfo */
1772 if ( (s->channel_mode > AC3_CHMODE_STEREO) &&
1773 ((s->output_mode & ~AC3_OUTPUT_LFEON) > AC3_CHMODE_STEREO)) {
1775 if (!downmix_info)
1776 return AVERROR(ENOMEM);
1777 switch (s->preferred_downmix) {
1778 case AC3_DMIXMOD_LTRT:
1780 break;
1781 case AC3_DMIXMOD_LORO:
1783 break;
1784 case AC3_DMIXMOD_DPLII:
1786 break;
1787 default:
1789 break;
1790 }
1791 downmix_info->center_mix_level = ff_ac3_gain_levels[s-> center_mix_level];
1792 downmix_info->center_mix_level_ltrt = ff_ac3_gain_levels[s-> center_mix_level_ltrt];
1793 downmix_info->surround_mix_level = ff_ac3_gain_levels[s-> surround_mix_level];
1794 downmix_info->surround_mix_level_ltrt = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
1795 if (s->lfe_mix_level_exists)
1796 downmix_info->lfe_mix_level = ff_eac3_gain_levels_lfe[s->lfe_mix_level];
1797 else
1798 downmix_info->lfe_mix_level = 0.0; // -inf dB
1799 }
1800
1801 *got_frame_ptr = 1;
1802
1803 if (!s->superframe_size)
1804 return FFMIN(full_buf_size, s->frame_size + skip);
1805
1806 return FFMIN(full_buf_size, s->superframe_size + skip);
1807}
1808
1809/**
1810 * Uninitialize the AC-3 decoder.
1811 */
1813{
1814 AC3DecodeContext *s = avctx->priv_data;
1815 av_tx_uninit(&s->tx_256);
1816 av_tx_uninit(&s->tx_128);
1817 av_freep(&s->fdsp);
1818 av_freep(&s->downmix_coeffs[0]);
1819
1820 return 0;
1821}
1822
1823#define OFFSET(x) offsetof(AC3DecodeContext, x)
1824#define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
float SHORTFLOAT
int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd, int start, int end, int fast_gain, int is_lfe, int dba_mode, int dba_nsegs, uint8_t *dba_offsets, uint8_t *dba_lengths, uint8_t *dba_values, int16_t *mask)
Calculate the masking curve.
Definition ac3.c:201
void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd, int16_t *band_psd)
Calculate the log power-spectral density of the input signal.
Definition ac3.c:175
#define AC3_RENAME(x)
Definition ac3.h:67
#define AC3_SPX_BLEND(x)
Definition ac3.h:73
#define AC3_RANGE(x)
Definition ac3.h:70
#define FIXR12(x)
Definition ac3.h:63
#define AC3_DYNAMIC_RANGE(x)
Definition ac3.h:72
#define AC3_DYNAMIC_RANGE1
Definition ac3.h:74
#define AC3_HEAVY_RANGE(x)
Definition ac3.h:71
const uint16_t ff_ac3_channel_layout_tab[8]
Map audio coding mode (acmod) to channel layout mask.
int ff_ac3_find_syncword(const uint8_t *buf, int buf_size)
@ AC3_PARSE_ERROR_CRC
@ AC3_PARSE_ERROR_FRAME_TYPE
@ AC3_PARSE_ERROR_SAMPLE_RATE
@ AC3_PARSE_ERROR_BSID
@ AC3_PARSE_ERROR_CHANNEL_MAP
@ AC3_PARSE_ERROR_SYNC
@ AC3_PARSE_ERROR_FRAME_SIZE
int ff_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
Parse AC-3 frame header.
uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3]
table for ungrouping 3 values in 7 bits.
Definition ac3dec_data.c:50
const uint8_t ff_ac3_quantization_tab[16]
Quantization table: levels for symmetric.
const uint8_t ff_eac3_default_spx_band_struct[17]
Table E2.15 Default Spectral Extension Banding Structure.
int ff_ac3_bap1_mantissas[32][3]
tables for ungrouping mantissas
Definition ac3dec_data.c:94
const uint8_t ff_ac3_default_coeffs[8][5][2]
Table for default stereo downmixing coefficients reference: Section 7.8.2 Downmixing Into Two Channel...
int ff_ac3_bap4_mantissas[128][2]
Definition ac3dec_data.c:96
const int ff_ac3_bap3_mantissas[7+1]
Ungrouped mantissa tables; the extra entry is padding to avoid range checks.
Definition ac3dec_data.c:64
av_cold void ff_ac3_init_static(void)
const float ff_eac3_gain_levels_lfe[32]
Adjustments in dB gain (LFE, +10 to -21 dB)
const uint8_t ff_eac3_hebap_tab[64]
const int ff_ac3_bap5_mantissas[15+1]
Table 7.23.
Definition ac3dec_data.c:76
int ff_ac3_bap2_mantissas[128][3]
Definition ac3dec_data.c:95
#define AC3_FIXED_EXPONENT_MAX
#define AC3_FIXED_COEFF_BITS
static void ac3_downmix_c_fixed16(int16_t **samples, int16_t **matrix, int out_ch, int in_ch, int len)
Downmix samples from original signal to stereo or mono (this is for 16-bit samples and fixed point de...
#define IMDCT_TYPE
static void scale_coefs_q2(int32_t *dst, const int32_t *src, int dynrng, int len)
static const int end_freq_inv_tab[8]
static av_always_inline int fixed_coeff_bits(const AC3DecodeContext *s)
static void scale_coefs(int32_t *dst, const int32_t *src, int dynrng, int len)
#define EXP_REUSE
Definition ac3defs.h:51
#define EXP_D45
Definition ac3defs.h:56
@ AC3_DHEADPHONMOD_NOTINDICATED
Definition ac3defs.h:96
@ AC3_DHEADPHONMOD_ON
Definition ac3defs.h:98
#define CPL_CH
coupling channel index
Definition ac3defs.h:27
#define EAC3_MAX_CHANNELS
maximum number of channels in EAC3
Definition ac3defs.h:25
#define AC3_MAX_CHANNELS
maximum number of channels, including coupling channel
Definition ac3defs.h:26
#define LEVEL_MINUS_3DB
Definition ac3defs.h:43
@ AC3_DSUREXMOD_ON
Definition ac3defs.h:90
@ AC3_DSUREXMOD_PLIIZ
Definition ac3defs.h:91
@ AC3_DSUREXMOD_NOTINDICATED
Definition ac3defs.h:88
#define AC3_BLOCK_SIZE
Definition ac3defs.h:30
@ AC3_CHMODE_MONO
Definition ac3defs.h:69
@ AC3_CHMODE_STEREO
Definition ac3defs.h:70
@ AC3_CHMODE_2F1R
Definition ac3defs.h:72
@ AC3_CHMODE_DUALMONO
Definition ac3defs.h:68
@ AC3_CHMODE_3F
Definition ac3defs.h:71
@ AC3_CHMODE_3F1R
Definition ac3defs.h:73
@ AC3_CHMODE_2F2R
Definition ac3defs.h:74
@ AC3_CHMODE_3F2R
Definition ac3defs.h:75
@ EAC3_FRAME_TYPE_DEPENDENT
Definition ac3defs.h:112
@ EAC3_FRAME_TYPE_INDEPENDENT
Definition ac3defs.h:111
@ DBA_RESERVED
Definition ac3defs.h:63
@ DBA_NEW
Definition ac3defs.h:61
@ DBA_NONE
Definition ac3defs.h:62
@ AC3_DMIXMOD_DPLII
Definition ac3defs.h:107
@ AC3_DMIXMOD_NOTINDICATED
Definition ac3defs.h:104
@ AC3_DMIXMOD_LTRT
Definition ac3defs.h:105
@ AC3_DMIXMOD_LORO
Definition ac3defs.h:106
@ AC3_DSURMOD_ON
Definition ac3defs.h:82
const uint8_t ff_ac3_rematrix_band_tab[5]
Table of bin locations for rematrixing bands reference: Section 7.5.2 Rematrixing : Frequency Band De...
Definition ac3tab.c:108
const uint8_t ff_ac3_channels_tab[8]
Map audio coding mode (acmod) to number of full-bandwidth channels.
Definition ac3tab.c:81
const uint8_t ff_ac3_dec_channel_map[8][2][6]
Table to remap channels from AC-3 order to SMPTE order.
Definition ac3tab.c:89
const uint8_t ff_ac3_fast_decay_tab[4]
Definition ac3tab.c:131
const uint16_t ff_ac3_fast_gain_tab[8]
Definition ac3tab.c:147
const uint16_t ff_ac3_slow_gain_tab[4]
Definition ac3tab.c:135
const uint8_t ff_eac3_default_cpl_band_struct[18]
Table E2.16 Default Coupling Banding Structure.
Definition ac3tab.c:113
const uint8_t ff_ac3_slow_decay_tab[4]
Definition ac3tab.c:127
const float ff_ac3_gain_levels[9]
Adjustments in dB gain.
Definition ac3tab.c:152
const uint64_t ff_eac3_custom_channel_map_locations[16][2]
Definition ac3tab.c:164
const int16_t ff_ac3_floor_tab[8]
Definition ac3tab.c:143
const uint16_t ff_ac3_db_per_bit_tab[4]
Definition ac3tab.c:139
const uint8_t ff_ac3_bap_tab[64]
Definition ac3tab.c:117
channels
Definition aptx.h:31
static const uint8_t channel_map[8][8]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define av_popcount64
Definition common.h:157
#define av_clipf
Definition common.h:145
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static __device__ float sqrtf(float a)
#define INTFLOAT
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding)
Add or update AV_FRAME_DATA_MATRIXENCODING side data.
Definition utils.c:121
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition defs.h:48
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
#define AV_PROFILE_EAC3_DDP_ATMOS
Definition defs.h:96
#define AV_EF_CAREFUL
consider things that violate the spec, are fast to calculate and have not been seen in the wild as er...
Definition defs.h:54
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition defs.h:244
static AVFrame * frame
static const uint8_t bap_tab[64]
Definition dolby_e.c:599
audio downmix medatata
static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
Definition eac3dec.c:195
static int ff_eac3_parse_header(AC3DecodeContext *s, const AC3HeaderInfo *hdr)
Definition eac3dec.c:288
static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
Definition eac3dec.c:56
static const uint8_t bits[8]
Definition fastaudio.c:100
static av_always_inline int fixed_sqrt(int x, int bits)
Calculate the square root.
Definition fixed_dsp.h:176
#define FF_DECODE_ERROR_INVALID_BITSTREAM
Definition frame.h:760
#define AV_NUM_DATA_POINTERS
Definition frame.h:473
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CH_LOW_FREQUENCY
AVDownmixInfo * av_downmix_info_update_side_data(AVFrame *frame)
Get a frame's AV_FRAME_DATA_DOWNMIX_INFO side data for editing.
@ AV_DOWNMIX_TYPE_UNKNOWN
Not indicated.
@ AV_DOWNMIX_TYPE_LTRT
Lt/Rt 2-channel downmix, Dolby Surround compatible.
@ AV_DOWNMIX_TYPE_LORO
Lo/Ro 2-channel downmix (Stereo).
@ AV_DOWNMIX_TYPE_DPLII
Lt/Rt 2-channel downmix, Dolby Pro Logic II compatible.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVMatrixEncoding
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
@ AV_MATRIX_ENCODING_NONE
@ AV_MATRIX_ENCODING_DOLBY
@ AV_MATRIX_ENCODING_DPLIIZ
@ AV_MATRIX_ENCODING_DOLBYEX
@ AV_MATRIX_ENCODING_DOLBYHEADPHONE
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_16_ANSI
Definition crc.h:50
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define ff_ctzll
Definition intmath.h:125
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
int index
Definition gxfenc.c:90
const VDPAUPixFmtMap * map
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_RB16(p)
frame_type
av_cold void ff_kbd_window_init(float *window, float alpha, int n)
Generate a Kaiser-Bessel Derived Window.
Definition kbdwin.c:54
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length)
Seed the state of the ALFG using binary data.
Definition lfg.c:64
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
unsigned offset
Definition libaomenc.c:763
#define USE_FIXED
Definition aacdec.c:34
static av_always_inline INTFLOAT dequantize_coeff(int mantissa, int exponent, int coeff_bits)
Definition ac3dec.c:408
float ff_ac3_heavy_dynamic_range_tab[256]
Definition ac3dec.c:53
static int coupling_coordinates(AC3DecodeContext *s, int blk)
Definition ac3dec.c:963
static void remove_dithering(AC3DecodeContext *s)
Remove random dithering from coupling range coefficients with zero-bit mantissas for coupled channels...
Definition ac3dec.c:527
static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
Decode a single audio block from the AC-3 bitstream.
Definition ac3dec.c:1009
static float dynamic_range_tab[256]
dynamic range table.
Definition ac3dec.c:52
static const float scale_factors[25]
scale factor for each decoded exponent: 2^-exp
Definition ac3dec.c:55
static int coupling_strategy(AC3DecodeContext *s, int blk, uint8_t *bit_alloc_stages)
Definition ac3dec.c:893
static int set_downmix_coeffs(AC3DecodeContext *s)
Set stereo downmixing coefficients based on frame header info.
Definition ac3dec.c:255
static void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk, int ch, mant_groups *m)
Definition ac3dec.c:540
static av_cold void ac3_decode_flush(AVCodecContext *avctx)
Definition ac3dec.c:157
static av_cold int ac3_decode_end(AVCodecContext *avctx)
Uninitialize the AC-3 decoder.
Definition ac3dec.c:1812
static void ac3_upmix_delay(AC3DecodeContext *s)
Upmix delay samples from stereo to original channel layout.
Definition ac3dec.c:662
static av_cold void ac3_float_tables_init(void)
Definition ac3dec.c:66
static void spx_coordinates(AC3DecodeContext *s)
Definition ac3dec.c:815
static void decode_transform_coeffs(AC3DecodeContext *s, int blk)
Decode the transform coefficients.
Definition ac3dec.c:561
static int decode_exponents(AC3DecodeContext *s, GetBitContext *gbc, int exp_strategy, int ngrps, uint8_t absexp, int8_t *dexps)
Decode the grouped exponents according to exponent strategy.
Definition ac3dec.c:317
static av_always_inline int dequantize_dexp24_dither(int mantissa)
Definition ac3dec.c:419
static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
Generate transform coefficients for each coupled channel in the coupling range using the coupling coe...
Definition ac3dec.c:363
static void decode_band_structure(GetBitContext *gbc, int blk, int eac3, int ecpl, int start_subband, int end_subband, const uint8_t *default_band_struct, int *num_bands, uint8_t *band_sizes, uint8_t *band_struct, int band_struct_size)
Decode band structure for coupling, spectral extension, or enhanced coupling.
Definition ac3dec.c:707
static int parse_frame_header(AC3DecodeContext *s)
Common function to parse AC-3 or E-AC-3 frame header.
Definition ac3dec.c:170
static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode a single AC-3 frame.
Definition ac3dec.c:1408
static void do_rematrixing(AC3DecodeContext *s)
Stereo rematrixing.
Definition ac3dec.c:597
static void ac3_downmix(AVCodecContext *avctx)
Definition ac3dec.c:85
static int spx_strategy(AC3DecodeContext *s, int blk)
Definition ac3dec.c:756
static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
Decode the transform coefficients for a particular channel reference: Section 7.3 Quantization and De...
Definition ac3dec.c:432
static void do_imdct(AC3DecodeContext *s, int channels, int offset)
Inverse MDCT Transform.
Definition ac3dec.c:621
static av_cold int ac3_decode_init(AVCodecContext *avctx)
AVCodec initialization.
Definition ac3dec.c:106
void ff_ac3dsp_downmix(AC3DSPContext *c, float **samples, float **matrix, int out_ch, int in_ch, int len)
Definition ac3dsp.c:344
av_cold void ff_ac3dsp_init(AC3DSPContext *c)
Definition ac3dsp.c:377
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition bswapdsp.c:37
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
AVFixedDSPContext * avpriv_alloc_fixed_dsp(int bit_exact)
Allocate and initialize a fixed DSP context.
Definition fixed_dsp.c:151
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
static av_always_inline av_const double round(double x)
Definition libm.h:446
#define powf(x, y)
Definition libm.h:52
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
enum AVColorRange range
Memory handling functions.
AVOptions.
#define MULH
Definition mathops.h:42
#define blk(i)
Definition sha.c:55
Coded AC-3 header values up to the lfeon element, plus derived values.
int8_t dialog_normalization[2]
int center_mix_level
Center mix level index.
uint8_t heavy_dynamic_range[2]
int substreamid
substream identification
uint8_t compression_exists[2]
int num_blocks
number of audio blocks
int surround_mix_level
Surround mix level index.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition avcodec.h:1089
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1637
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1417
This structure describes optional metadata relevant to a downmix procedure.
double lfe_mix_level
Absolute scale factor representing the level at which the LFE data is mixed into L/R channels during ...
double surround_mix_level_ltrt
Absolute scale factor representing the nominal level of the surround channels during an Lt/Rt compati...
double surround_mix_level
Absolute scale factor representing the nominal level of the surround channels during a regular downmi...
double center_mix_level
Absolute scale factor representing the nominal level of the center channel during a regular downmix.
enum AVDownmixType preferred_downmix_type
Type of downmix preferred by the mastering engineer.
double center_mix_level_ltrt
Absolute scale factor representing the nominal level of the center channel during an Lt/Rt compatible...
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Grouped mantissas for 3-level 5-level and 11-level quantization.
Definition ac3dec.c:399
int b2_mant[2]
Definition ac3dec.c:401
int b4_mant
Definition ac3dec.c:402
int b1_mant[2]
Definition ac3dec.c:400
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
static const uint16_t dither[8][8]
Definition vf_gradfun.c:46