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
25 #include "ac3_parser.h"
27 #include "ac3dec_data.h"
29 /** gain adaptive quantization mode */
37 #define EAC3_SR_CODE_REDUCED 3
40 void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
43 int wrapflag[SPX_MAX_BANDS]={0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
44 int rms_energy[SPX_MAX_BANDS];
46 /* Set copy index mapping table. Set wrap flags to apply a notch filter at
47 wrap points later on. */
48 bin = s->spx_copy_start_freq;
49 num_copy_sections = 0;
50 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
51 int bandsize = s->spx_band_sizes[bnd];
52 if ((bin + bandsize) > s->spx_start_freq) {
53 copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
54 bin = s->spx_copy_start_freq;
57 for (i = 0; i < bandsize; i++) {
58 if (bin == s->spx_start_freq) {
59 copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
60 bin = s->spx_copy_start_freq;
65 copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
67 for (ch = 1; ch <= s->fbw_channels; ch++) {
68 if (!s->channel_in_spx[ch])
71 /* Copy coeffs from normal bands to extension bands */
72 bin = s->spx_start_freq;
73 for (bnd = 0; bnd < num_copy_sections; bnd++) {
74 memcpy(&s->fixed_coeffs[ch][bin],
75 &s->fixed_coeffs[ch][s->spx_copy_start_freq],
76 copy_sizes[bnd]*sizeof(int));
77 bin += copy_sizes[bnd];
80 /* Calculate RMS energy for each SPX band. */
81 bin = s->spx_start_freq;
82 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
83 int bandsize = s->spx_band_sizes[bnd];
85 for (i = 0; i < bandsize; i++) {
86 int64_t coeff = s->fixed_coeffs[ch][bin++];
87 accum += coeff * coeff;
89 rms_energy[bnd] = ff_sqrt((accum >> 15) / bandsize) * M_SQRT_POW2_15;
92 /* Apply a notch filter at transitions between normal and extension
93 bands and at all wrap points. */
94 if (s->spx_atten_code[ch] >= 0) {
95 const int32_t *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
96 /* apply notch filter at baseband / extension region border */
97 bin = s->spx_start_freq - 2;
98 for (i = 0; i < 5; i++) {
99 s->fixed_coeffs[ch][bin] = ((int64_t)atten_tab[2-abs(i-2)] *
100 (int64_t)s->fixed_coeffs[ch][bin]) >> 23;
103 /* apply notch at all other wrap points */
104 bin += s->spx_band_sizes[0];
105 for (bnd = 1; bnd < s->num_spx_bands; bnd++) {
108 for (i = 0; i < 5; i++) {
109 s->fixed_coeffs[ch][bin] = (atten_tab[2-abs(i-2)] *
110 (int64_t)s->fixed_coeffs[ch][bin]) >> 23;
114 bin += s->spx_band_sizes[bnd];
118 /* Apply noise-blended coefficient scaling based on previously
119 calculated RMS energy, blending factors, and SPX coordinates for
121 bin = s->spx_start_freq;
122 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
123 int64_t nscale, sscale, spxco;
124 nscale = (s->spx_noise_blend [ch][bnd] * rms_energy[bnd]) >> 23;
125 nscale = (nscale * 14529495) >> 23;
126 sscale = s->spx_signal_blend[ch][bnd];
127 spxco = s->spx_coords[ch][bnd];
128 for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
129 int64_t noise = (nscale * (((int)av_lfg_get(&s->dith_state))>>8)) >> 23;
130 int64_t signal = (sscale * s->fixed_coeffs[ch][bin]) >> 23;
131 s->fixed_coeffs[ch][bin++] = ((noise + signal) * spxco) >> 23;
137 /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
138 #define COEFF_0 10273905LL
140 /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
141 #define COEFF_1 11863283LL
143 /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
144 #define COEFF_2 3070444LL
147 * Calculate 6-point IDCT of the pre-mantissas.
148 * All calculations are 24-bit fixed-point.
150 static void idct6(int pre_mant[6])
153 int even0, even1, even2, odd0, odd1, odd2;
155 odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
157 even2 = ( pre_mant[2] * COEFF_0) >> 23;
158 tmp = ( pre_mant[4] * COEFF_1) >> 23;
159 odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
161 even0 = pre_mant[0] + (tmp >> 1);
162 even1 = pre_mant[0] - tmp;
169 odd0 = tmp + pre_mant[1] + pre_mant[3];
170 odd2 = tmp + pre_mant[5] - pre_mant[3];
172 pre_mant[0] = even0 + odd0;
173 pre_mant[1] = even1 + odd1;
174 pre_mant[2] = even2 + odd2;
175 pre_mant[3] = even2 - odd2;
176 pre_mant[4] = even1 - odd1;
177 pre_mant[5] = even0 - odd0;
180 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
183 int end_bap, gaq_mode;
184 GetBitContext *gbc = &s->gbc;
185 int gaq_gain[AC3_MAX_COEFS];
187 gaq_mode = get_bits(gbc, 2);
188 end_bap = (gaq_mode < 2) ? 12 : 17;
190 /* if GAQ gain is used, decode gain codes for bins with hebap between
193 if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
194 /* read 1-bit GAQ gain codes */
195 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
196 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
197 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
199 } else if (gaq_mode == EAC3_GAQ_124) {
200 /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
202 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
203 if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
205 int group_code = get_bits(gbc, 5);
206 if (group_code > 26) {
207 av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
210 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
211 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
212 gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
220 for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
221 int hebap = s->bap[ch][bin];
222 int bits = ff_eac3_bits_vs_hebap[hebap];
224 /* zero-mantissa dithering */
225 for (blk = 0; blk < 6; blk++) {
226 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
228 } else if (hebap < 8) {
229 /* Vector Quantization */
230 int v = get_bits(gbc, bits);
231 for (blk = 0; blk < 6; blk++) {
232 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
235 /* Gain Adaptive Quantization */
237 if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
238 log_gain = gaq_gain[gs++];
242 gbits = bits - log_gain;
244 for (blk = 0; blk < 6; blk++) {
245 int mant = get_sbits(gbc, gbits);
246 if (mant == -(1 << (gbits-1))) {
249 mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
250 /* remap mantissa value to correct for asymmetric quantization */
252 b = 32768 >> (log_gain+8);
254 b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
255 mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
257 /* small mantissa, no GAQ, or Gk=1 */
260 /* remap mantissa value for no GAQ or Gk=1 */
261 mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
264 s->pre_mantissa[ch][bin][blk] = mant;
267 idct6(s->pre_mantissa[ch][bin]);
271 int ff_eac3_parse_header(AC3DecodeContext *s)
274 int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
275 int parse_transient_proc_info;
277 GetBitContext *gbc = &s->gbc;
279 /* An E-AC-3 stream can have multiple independent streams which the
280 application can select from. each independent stream can also contain
281 dependent streams which are used to add or replace channels. */
282 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
283 av_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
284 return AC3_PARSE_ERROR_FRAME_TYPE;
285 } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
286 av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
287 return AC3_PARSE_ERROR_FRAME_TYPE;
290 /* The substream id indicates which substream this frame belongs to. each
291 independent stream has its own substream id, and the dependent streams
292 associated to an independent stream have matching substream id's. */
293 if (s->substreamid) {
294 /* only decode substream with id=0. skip any additional substreams. */
295 av_log_missing_feature(s->avctx, "Additional substreams", 1);
296 return AC3_PARSE_ERROR_FRAME_TYPE;
299 if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
300 /* The E-AC-3 specification does not tell how to handle reduced sample
301 rates in bit allocation. The best assumption would be that it is
302 handled like AC-3 DolbyNet, but we cannot be sure until we have a
303 sample which utilizes this feature. */
304 av_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
307 skip_bits(gbc, 5); // skip bitstream id
309 /* volume control params */
310 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
311 skip_bits(gbc, 5); // skip dialog normalization
312 if (get_bits1(gbc)) {
313 skip_bits(gbc, 8); // skip compression gain word
317 /* dependent stream channel map */
318 if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
319 if (get_bits1(gbc)) {
320 skip_bits(gbc, 16); // skip custom channel map
324 /* mixing metadata */
325 if (get_bits1(gbc)) {
326 /* center and surround mix levels */
327 if (s->channel_mode > AC3_CHMODE_STEREO) {
328 skip_bits(gbc, 2); // skip preferred stereo downmix mode
329 if (s->channel_mode & 1) {
330 /* if three front channels exist */
331 skip_bits(gbc, 3); //skip Lt/Rt center mix level
332 s->center_mix_level = get_bits(gbc, 3);
334 if (s->channel_mode & 4) {
335 /* if a surround channel exists */
336 skip_bits(gbc, 3); //skip Lt/Rt surround mix level
337 s->surround_mix_level = get_bits(gbc, 3);
342 if (s->lfe_on && get_bits1(gbc)) {
343 // TODO: use LFE mix level
344 skip_bits(gbc, 5); // skip LFE mix level code
347 /* info for mixing with other streams and substreams */
348 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
349 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
350 // TODO: apply program scale factor
351 if (get_bits1(gbc)) {
352 skip_bits(gbc, 6); // skip program scale factor
355 if (get_bits1(gbc)) {
356 skip_bits(gbc, 6); // skip external program scale factor
358 /* skip mixing parameter data */
359 switch(get_bits(gbc, 2)) {
360 case 1: skip_bits(gbc, 5); break;
361 case 2: skip_bits(gbc, 12); break;
363 int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
364 skip_bits_long(gbc, mix_data_size);
368 /* skip pan information for mono or dual mono source */
369 if (s->channel_mode < AC3_CHMODE_STEREO) {
370 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
371 if (get_bits1(gbc)) {
372 /* note: this is not in the ATSC A/52B specification
373 reference: ETSI TS 102 366 V1.1.1
374 section: E.1.3.1.25 */
375 skip_bits(gbc, 8); // skip pan mean direction index
376 skip_bits(gbc, 6); // skip reserved paninfo bits
380 /* skip mixing configuration information */
381 if (get_bits1(gbc)) {
382 for (blk = 0; blk < s->num_blocks; blk++) {
383 if (s->num_blocks == 1 || get_bits1(gbc)) {
391 /* informational metadata */
392 if (get_bits1(gbc)) {
393 skip_bits(gbc, 3); // skip bit stream mode
394 skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
395 if (s->channel_mode == AC3_CHMODE_STEREO) {
396 skip_bits(gbc, 4); // skip Dolby surround and headphone mode
398 if (s->channel_mode >= AC3_CHMODE_2F2R) {
399 skip_bits(gbc, 2); // skip Dolby surround EX mode
401 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
402 if (get_bits1(gbc)) {
403 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
406 if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
407 skip_bits1(gbc); // skip source sample rate code
411 /* converter synchronization flag
412 If frames are less than six blocks, this bit should be turned on
413 once every 6 blocks to indicate the start of a frame set.
414 reference: RFC 4598, Section 2.1.3 Frame Sets */
415 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
416 skip_bits1(gbc); // skip converter synchronization flag
419 /* original frame size code if this stream was converted from AC-3 */
420 if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
421 (s->num_blocks == 6 || get_bits1(gbc))) {
422 skip_bits(gbc, 6); // skip frame size code
425 /* additional bitstream info */
426 if (get_bits1(gbc)) {
427 int addbsil = get_bits(gbc, 6);
428 for (i = 0; i < addbsil + 1; i++) {
429 skip_bits(gbc, 8); // skip additional bit stream info
433 /* audio frame syntax flags, strategy data, and per-frame data */
435 if (s->num_blocks == 6) {
436 ac3_exponent_strategy = get_bits1(gbc);
437 parse_aht_info = get_bits1(gbc);
439 /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
441 ac3_exponent_strategy = 1;
445 s->snr_offset_strategy = get_bits(gbc, 2);
446 parse_transient_proc_info = get_bits1(gbc);
448 s->block_switch_syntax = get_bits1(gbc);
449 if (!s->block_switch_syntax)
450 memset(s->block_switch, 0, sizeof(s->block_switch));
452 s->dither_flag_syntax = get_bits1(gbc);
453 if (!s->dither_flag_syntax) {
454 for (ch = 1; ch <= s->fbw_channels; ch++)
455 s->dither_flag[ch] = 1;
457 s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
459 s->bit_allocation_syntax = get_bits1(gbc);
460 if (!s->bit_allocation_syntax) {
461 /* set default bit allocation parameters */
462 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
463 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
464 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
465 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
466 s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
469 s->fast_gain_syntax = get_bits1(gbc);
470 s->dba_syntax = get_bits1(gbc);
471 s->skip_syntax = get_bits1(gbc);
472 parse_spx_atten_data = get_bits1(gbc);
474 /* coupling strategy occurance and coupling use per block */
476 if (s->channel_mode > 1) {
477 for (blk = 0; blk < s->num_blocks; blk++) {
478 s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
479 if (s->cpl_strategy_exists[blk]) {
480 s->cpl_in_use[blk] = get_bits1(gbc);
482 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
484 num_cpl_blocks += s->cpl_in_use[blk];
487 memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
490 /* exponent strategy data */
491 if (ac3_exponent_strategy) {
492 /* AC-3-style exponent strategy syntax */
493 for (blk = 0; blk < s->num_blocks; blk++) {
494 for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
495 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
499 /* LUT-based exponent strategy syntax */
500 for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
501 int frmchexpstr = get_bits(gbc, 5);
502 for (blk = 0; blk < 6; blk++) {
503 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
507 /* LFE exponent strategy */
509 for (blk = 0; blk < s->num_blocks; blk++) {
510 s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
513 /* original exponent strategies if this stream was converted from AC-3 */
514 if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
515 (s->num_blocks == 6 || get_bits1(gbc))) {
516 skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
519 /* determine which channels use AHT */
520 if (parse_aht_info) {
521 /* For AHT to be used, all non-zero blocks must reuse exponents from
522 the first block. Furthermore, for AHT to be used in the coupling
523 channel, all blocks must use coupling and use the same coupling
525 s->channel_uses_aht[CPL_CH]=0;
526 for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
528 for (blk = 1; blk < 6; blk++) {
529 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
530 (!ch && s->cpl_strategy_exists[blk])) {
535 s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
538 memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
541 /* per-frame SNR offset */
542 if (!s->snr_offset_strategy) {
543 int csnroffst = (get_bits(gbc, 6) - 15) << 4;
544 int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
545 for (ch = 0; ch <= s->channels; ch++)
546 s->snr_offset[ch] = snroffst;
549 /* transient pre-noise processing data */
550 if (parse_transient_proc_info) {
551 for (ch = 1; ch <= s->fbw_channels; ch++) {
552 if (get_bits1(gbc)) { // channel in transient processing
553 skip_bits(gbc, 10); // skip transient processing location
554 skip_bits(gbc, 8); // skip transient processing length
559 /* spectral extension attenuation data */
560 for (ch = 1; ch <= s->fbw_channels; ch++) {
561 if (parse_spx_atten_data && get_bits1(gbc))
562 s->spx_atten_code[ch] = get_bits(gbc, 5);
564 s->spx_atten_code[ch] = -1;
567 /* block start information */
568 if (s->num_blocks > 1 && get_bits1(gbc)) {
569 /* reference: Section E2.3.2.27
570 nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
571 The spec does not say what this data is or what it's used for.
572 It is likely the offset of each block within the frame. */
573 int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
574 skip_bits(gbc, block_start_bits);
577 /* syntax state initialization */
578 for (ch = 1; ch <= s->fbw_channels; ch++) {
579 s->first_spx_coords[ch] = 1;
580 s->first_cpl_coords[ch] = 1;
582 s->first_cpl_leak = 1;