3 * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4 * Copyright (c) 2008 Justin Ruggles
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * There are several features of E-AC-3 that this decoder does not yet support.
27 * No known samples exist. If any ever surface, this feature should not be
28 * too difficult to implement.
30 * Reduced Sample Rates
31 * No known samples exist. The spec also does not give clear information
32 * on how this is to be implemented.
35 * Only the independent stream is currently decoded. Any dependent
36 * streams are skipped. We have only come across two examples of this, and
37 * they are both just test streams, one for HD-DVD and the other for
40 * Transient Pre-noise Processing
41 * This is side information which a decoder should use to reduce artifacts
42 * caused by transients. There are samples which are known to have this
43 * information, but this decoder currently ignores it.
49 #include "aac_ac3_parser.h"
51 #include "ac3_parser.h"
53 #include "ac3dec_data.h"
54 #include "eac3_data.h"
56 /** gain adaptive quantization mode */
64 #define EAC3_SR_CODE_REDUCED 3
66 static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
69 uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
70 float rms_energy[SPX_MAX_BANDS];
72 /* Set copy index mapping table. Set wrap flags to apply a notch filter at
73 wrap points later on. */
74 bin = s->spx_dst_start_freq;
75 num_copy_sections = 0;
76 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
78 int bandsize = s->spx_band_sizes[bnd];
79 if (bin + bandsize > s->spx_src_start_freq) {
80 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
81 bin = s->spx_dst_start_freq;
84 for (i = 0; i < bandsize; i += copysize) {
85 if (bin == s->spx_src_start_freq) {
86 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
87 bin = s->spx_dst_start_freq;
89 copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin);
93 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
95 for (ch = 1; ch <= s->fbw_channels; ch++) {
96 if (!s->channel_uses_spx[ch])
99 /* Copy coeffs from normal bands to extension bands */
100 bin = s->spx_src_start_freq;
101 for (i = 0; i < num_copy_sections; i++) {
102 memcpy(&s->transform_coeffs[ch][bin],
103 &s->transform_coeffs[ch][s->spx_dst_start_freq],
104 copy_sizes[i]*sizeof(INTFLOAT));
105 bin += copy_sizes[i];
108 /* Calculate RMS energy for each SPX band. */
109 bin = s->spx_src_start_freq;
110 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
111 int bandsize = s->spx_band_sizes[bnd];
113 for (i = 0; i < bandsize; i++) {
114 float coeff = s->transform_coeffs[ch][bin++];
115 accum += coeff * coeff;
117 rms_energy[bnd] = sqrtf(accum / bandsize);
120 /* Apply a notch filter at transitions between normal and extension
121 bands and at all wrap points. */
122 if (s->spx_atten_code[ch] >= 0) {
123 const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
124 bin = s->spx_src_start_freq - 2;
125 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
127 INTFLOAT *coeffs = &s->transform_coeffs[ch][bin];
128 coeffs[0] *= atten_tab[0];
129 coeffs[1] *= atten_tab[1];
130 coeffs[2] *= atten_tab[2];
131 coeffs[3] *= atten_tab[1];
132 coeffs[4] *= atten_tab[0];
134 bin += s->spx_band_sizes[bnd];
138 /* Apply noise-blended coefficient scaling based on previously
139 calculated RMS energy, blending factors, and SPX coordinates for
141 bin = s->spx_src_start_freq;
142 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
143 float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN);
144 float sscale = s->spx_signal_blend[ch][bnd];
146 // spx_noise_blend and spx_signal_blend are both FP.23
147 nscale *= 1.0 / (1<<23);
148 sscale *= 1.0 / (1<<23);
150 for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
151 float noise = nscale * (int32_t)av_lfg_get(&s->dith_state);
152 s->transform_coeffs[ch][bin] *= sscale;
153 s->transform_coeffs[ch][bin++] += noise;
160 /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
161 #define COEFF_0 10273905LL
163 /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
164 #define COEFF_1 11863283LL
166 /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
167 #define COEFF_2 3070444LL
170 * Calculate 6-point IDCT of the pre-mantissas.
171 * All calculations are 24-bit fixed-point.
173 static void idct6(int pre_mant[6])
176 int even0, even1, even2, odd0, odd1, odd2;
178 odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
180 even2 = ( pre_mant[2] * COEFF_0) >> 23;
181 tmp = ( pre_mant[4] * COEFF_1) >> 23;
182 odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
184 even0 = pre_mant[0] + (tmp >> 1);
185 even1 = pre_mant[0] - tmp;
192 odd0 = tmp + pre_mant[1] + pre_mant[3];
193 odd2 = tmp + pre_mant[5] - pre_mant[3];
195 pre_mant[0] = even0 + odd0;
196 pre_mant[1] = even1 + odd1;
197 pre_mant[2] = even2 + odd2;
198 pre_mant[3] = even2 - odd2;
199 pre_mant[4] = even1 - odd1;
200 pre_mant[5] = even0 - odd0;
203 static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
206 int end_bap, gaq_mode;
207 GetBitContext *gbc = &s->gbc;
208 int gaq_gain[AC3_MAX_COEFS];
210 gaq_mode = get_bits(gbc, 2);
211 end_bap = (gaq_mode < 2) ? 12 : 17;
213 /* if GAQ gain is used, decode gain codes for bins with hebap between
216 if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
217 /* read 1-bit GAQ gain codes */
218 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
219 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
220 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
222 } else if (gaq_mode == EAC3_GAQ_124) {
223 /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
225 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
226 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
228 int group_code = get_bits(gbc, 5);
229 if (group_code > 26) {
230 av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
233 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
234 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
235 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
243 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
244 int hebap = s->bap[ch][bin];
245 int bits = ff_eac3_bits_vs_hebap[hebap];
247 /* zero-mantissa dithering */
248 for (blk = 0; blk < 6; blk++) {
249 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
251 } else if (hebap < 8) {
252 /* Vector Quantization */
253 int v = get_bits(gbc, bits);
254 for (blk = 0; blk < 6; blk++) {
255 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] * (1 << 8);
258 /* Gain Adaptive Quantization */
260 if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
261 log_gain = gaq_gain[gs++];
265 gbits = bits - log_gain;
267 for (blk = 0; blk < 6; blk++) {
268 int mant = get_sbits(gbc, gbits);
269 if (log_gain && mant == -(1 << (gbits-1))) {
272 int mbits = bits - (2 - log_gain);
273 mant = get_sbits(gbc, mbits);
274 mant = ((unsigned)mant) << (23 - (mbits - 1));
275 /* remap mantissa value to correct for asymmetric quantization */
277 b = 1 << (23 - log_gain);
279 b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] * (1 << 8);
280 mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b;
282 /* small mantissa, no GAQ, or Gk=1 */
283 mant *= (1 << 24 - bits);
285 /* remap mantissa value for no GAQ or Gk=1 */
286 mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15;
289 s->pre_mantissa[ch][bin][blk] = mant;
292 idct6(s->pre_mantissa[ch][bin]);
296 static int ff_eac3_parse_header(AC3DecodeContext *s)
299 int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
300 int parse_transient_proc_info;
302 GetBitContext *gbc = &s->gbc;
304 /* An E-AC-3 stream can have multiple independent streams which the
305 application can select from. each independent stream can also contain
306 dependent streams which are used to add or replace channels. */
307 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
308 if (!s->eac3_frame_dependent_found) {
309 s->eac3_frame_dependent_found = 1;
310 avpriv_request_sample(s->avctx, "Dependent substream decoding");
312 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
313 } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
314 av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
315 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
318 /* The substream id indicates which substream this frame belongs to. each
319 independent stream has its own substream id, and the dependent streams
320 associated to an independent stream have matching substream id's. */
321 if (s->substreamid) {
322 /* only decode substream with id=0. skip any additional substreams. */
323 if (!s->eac3_subsbtreamid_found) {
324 s->eac3_subsbtreamid_found = 1;
325 avpriv_request_sample(s->avctx, "Additional substreams");
327 return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
330 if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
331 /* The E-AC-3 specification does not tell how to handle reduced sample
332 rates in bit allocation. The best assumption would be that it is
333 handled like AC-3 DolbyNet, but we cannot be sure until we have a
334 sample which utilizes this feature. */
335 avpriv_request_sample(s->avctx, "Reduced sampling rate");
336 return AVERROR_PATCHWELCOME;
338 skip_bits(gbc, 5); // skip bitstream id
340 /* volume control params */
341 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
342 s->dialog_normalization[i] = -get_bits(gbc, 5);
343 if (s->dialog_normalization[i] == 0) {
344 s->dialog_normalization[i] = -31;
346 if (s->target_level != 0) {
347 s->level_gain[i] = powf(2.0f,
348 (float)(s->target_level - s->dialog_normalization[i])/6.0f);
350 s->compression_exists[i] = get_bits1(gbc);
351 if (s->compression_exists[i]) {
352 s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8));
356 /* dependent stream channel map */
357 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
358 if (get_bits1(gbc)) {
359 skip_bits(gbc, 16); // skip custom channel map
363 /* mixing metadata */
364 if (get_bits1(gbc)) {
365 /* center and surround mix levels */
366 if (s->channel_mode > AC3_CHMODE_STEREO) {
367 s->preferred_downmix = get_bits(gbc, 2);
368 if (s->channel_mode & 1) {
369 /* if three front channels exist */
370 s->center_mix_level_ltrt = get_bits(gbc, 3);
371 s->center_mix_level = get_bits(gbc, 3);
373 if (s->channel_mode & 4) {
374 /* if a surround channel exists */
375 s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7);
376 s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7);
381 if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) {
382 s->lfe_mix_level = get_bits(gbc, 5);
385 /* info for mixing with other streams and substreams */
386 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
387 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
388 // TODO: apply program scale factor
389 if (get_bits1(gbc)) {
390 skip_bits(gbc, 6); // skip program scale factor
393 if (get_bits1(gbc)) {
394 skip_bits(gbc, 6); // skip external program scale factor
396 /* skip mixing parameter data */
397 switch(get_bits(gbc, 2)) {
398 case 1: skip_bits(gbc, 5); break;
399 case 2: skip_bits(gbc, 12); break;
401 int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
402 skip_bits_long(gbc, mix_data_size);
406 /* skip pan information for mono or dual mono source */
407 if (s->channel_mode < AC3_CHMODE_STEREO) {
408 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
409 if (get_bits1(gbc)) {
410 /* note: this is not in the ATSC A/52B specification
411 reference: ETSI TS 102 366 V1.1.1
412 section: E.1.3.1.25 */
413 skip_bits(gbc, 8); // skip pan mean direction index
414 skip_bits(gbc, 6); // skip reserved paninfo bits
418 /* skip mixing configuration information */
419 if (get_bits1(gbc)) {
420 for (blk = 0; blk < s->num_blocks; blk++) {
421 if (s->num_blocks == 1 || get_bits1(gbc)) {
429 /* informational metadata */
430 if (get_bits1(gbc)) {
431 s->bitstream_mode = get_bits(gbc, 3);
432 skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
433 if (s->channel_mode == AC3_CHMODE_STEREO) {
434 s->dolby_surround_mode = get_bits(gbc, 2);
435 s->dolby_headphone_mode = get_bits(gbc, 2);
437 if (s->channel_mode >= AC3_CHMODE_2F2R) {
438 s->dolby_surround_ex_mode = get_bits(gbc, 2);
440 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
441 if (get_bits1(gbc)) {
442 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
445 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
446 skip_bits1(gbc); // skip source sample rate code
450 /* converter synchronization flag
451 If frames are less than six blocks, this bit should be turned on
452 once every 6 blocks to indicate the start of a frame set.
453 reference: RFC 4598, Section 2.1.3 Frame Sets */
454 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
455 skip_bits1(gbc); // skip converter synchronization flag
458 /* original frame size code if this stream was converted from AC-3 */
459 if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
460 (s->num_blocks == 6 || get_bits1(gbc))) {
461 skip_bits(gbc, 6); // skip frame size code
464 /* additional bitstream info */
465 if (get_bits1(gbc)) {
466 int addbsil = get_bits(gbc, 6);
467 for (i = 0; i < addbsil + 1; i++) {
468 skip_bits(gbc, 8); // skip additional bit stream info
472 /* audio frame syntax flags, strategy data, and per-frame data */
474 if (s->num_blocks == 6) {
475 ac3_exponent_strategy = get_bits1(gbc);
476 parse_aht_info = get_bits1(gbc);
478 /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
480 ac3_exponent_strategy = 1;
484 s->snr_offset_strategy = get_bits(gbc, 2);
485 parse_transient_proc_info = get_bits1(gbc);
487 s->block_switch_syntax = get_bits1(gbc);
488 if (!s->block_switch_syntax)
489 memset(s->block_switch, 0, sizeof(s->block_switch));
491 s->dither_flag_syntax = get_bits1(gbc);
492 if (!s->dither_flag_syntax) {
493 for (ch = 1; ch <= s->fbw_channels; ch++)
494 s->dither_flag[ch] = 1;
496 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
498 s->bit_allocation_syntax = get_bits1(gbc);
499 if (!s->bit_allocation_syntax) {
500 /* set default bit allocation parameters */
501 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
502 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
503 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
504 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
505 s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
508 s->fast_gain_syntax = get_bits1(gbc);
509 s->dba_syntax = get_bits1(gbc);
510 s->skip_syntax = get_bits1(gbc);
511 parse_spx_atten_data = get_bits1(gbc);
513 /* coupling strategy occurrence and coupling use per block */
515 if (s->channel_mode > 1) {
516 for (blk = 0; blk < s->num_blocks; blk++) {
517 s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
518 if (s->cpl_strategy_exists[blk]) {
519 s->cpl_in_use[blk] = get_bits1(gbc);
521 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
523 num_cpl_blocks += s->cpl_in_use[blk];
526 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
529 /* exponent strategy data */
530 if (ac3_exponent_strategy) {
531 /* AC-3-style exponent strategy syntax */
532 for (blk = 0; blk < s->num_blocks; blk++) {
533 for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
534 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
538 /* LUT-based exponent strategy syntax */
539 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
540 int frmchexpstr = get_bits(gbc, 5);
541 for (blk = 0; blk < 6; blk++) {
542 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
546 /* LFE exponent strategy */
548 for (blk = 0; blk < s->num_blocks; blk++) {
549 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
552 /* original exponent strategies if this stream was converted from AC-3 */
553 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
554 (s->num_blocks == 6 || get_bits1(gbc))) {
555 skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
558 /* determine which channels use AHT */
559 if (parse_aht_info) {
560 /* For AHT to be used, all non-zero blocks must reuse exponents from
561 the first block. Furthermore, for AHT to be used in the coupling
562 channel, all blocks must use coupling and use the same coupling
564 s->channel_uses_aht[CPL_CH]=0;
565 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
567 for (blk = 1; blk < 6; blk++) {
568 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
569 (!ch && s->cpl_strategy_exists[blk])) {
574 s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
577 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
580 /* per-frame SNR offset */
581 if (!s->snr_offset_strategy) {
582 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
583 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
584 for (ch = 0; ch <= s->channels; ch++)
585 s->snr_offset[ch] = snroffst;
588 /* transient pre-noise processing data */
589 if (parse_transient_proc_info) {
590 for (ch = 1; ch <= s->fbw_channels; ch++) {
591 if (get_bits1(gbc)) { // channel in transient processing
592 skip_bits(gbc, 10); // skip transient processing location
593 skip_bits(gbc, 8); // skip transient processing length
598 /* spectral extension attenuation data */
599 for (ch = 1; ch <= s->fbw_channels; ch++) {
600 if (parse_spx_atten_data && get_bits1(gbc)) {
601 s->spx_atten_code[ch] = get_bits(gbc, 5);
603 s->spx_atten_code[ch] = -1;
607 /* block start information */
608 if (s->num_blocks > 1 && get_bits1(gbc)) {
609 /* reference: Section E2.3.2.27
610 nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
611 The spec does not say what this data is or what it's used for.
612 It is likely the offset of each block within the frame. */
613 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
614 skip_bits_long(gbc, block_start_bits);
615 avpriv_request_sample(s->avctx, "Block start info");
618 /* syntax state initialization */
619 for (ch = 1; ch <= s->fbw_channels; ch++) {
620 s->first_spx_coords[ch] = 1;
621 s->first_cpl_coords[ch] = 1;
623 s->first_cpl_leak = 1;