1/*
2 * E-AC-3 decoder
3 * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4 * Copyright (c) 2008 Justin Ruggles
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "avcodec.h"
24#include "internal.h"
25#include "aac_ac3_parser.h"
26#include "ac3.h"
27#include "ac3_parser.h"
28#include "ac3dec.h"
29#include "ac3dec_data.h"
30
31/** gain adaptive quantization mode */
32typedef enum {
33    EAC3_GAQ_NO =0,
34    EAC3_GAQ_12,
35    EAC3_GAQ_14,
36    EAC3_GAQ_124
37} EAC3GaqMode;
38
39#define EAC3_SR_CODE_REDUCED  3
40
41/** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
42#define COEFF_0 10273905LL
43
44/** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
45#define COEFF_1 11863283LL
46
47/** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
48#define COEFF_2  3070444LL
49
50/**
51 * Calculate 6-point IDCT of the pre-mantissas.
52 * All calculations are 24-bit fixed-point.
53 */
54static void idct6(int pre_mant[6])
55{
56    int tmp;
57    int even0, even1, even2, odd0, odd1, odd2;
58
59    odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
60
61    even2 = ( pre_mant[2]                * COEFF_0) >> 23;
62    tmp   = ( pre_mant[4]                * COEFF_1) >> 23;
63    odd0  = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
64
65    even0 = pre_mant[0] + (tmp >> 1);
66    even1 = pre_mant[0] - tmp;
67
68    tmp = even0;
69    even0 = tmp + even2;
70    even2 = tmp - even2;
71
72    tmp = odd0;
73    odd0 = tmp + pre_mant[1] + pre_mant[3];
74    odd2 = tmp + pre_mant[5] - pre_mant[3];
75
76    pre_mant[0] = even0 + odd0;
77    pre_mant[1] = even1 + odd1;
78    pre_mant[2] = even2 + odd2;
79    pre_mant[3] = even2 - odd2;
80    pre_mant[4] = even1 - odd1;
81    pre_mant[5] = even0 - odd0;
82}
83
84void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
85{
86    int bin, blk, gs;
87    int end_bap, gaq_mode;
88    GetBitContext *gbc = &s->gbc;
89    int gaq_gain[AC3_MAX_COEFS];
90
91    gaq_mode = get_bits(gbc, 2);
92    end_bap = (gaq_mode < 2) ? 12 : 17;
93
94    /* if GAQ gain is used, decode gain codes for bins with hebap between
95       8 and end_bap */
96    gs = 0;
97    if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
98        /* read 1-bit GAQ gain codes */
99        for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
100            if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
101                gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
102        }
103    } else if (gaq_mode == EAC3_GAQ_124) {
104        /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
105        int gc = 2;
106        for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
107            if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
108                if (gc++ == 2) {
109                    int group_code = get_bits(gbc, 5);
110                    if (group_code > 26) {
111                        av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
112                        group_code = 26;
113                    }
114                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
115                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
116                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
117                    gc = 0;
118                }
119            }
120        }
121    }
122
123    gs=0;
124    for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
125        int hebap = s->bap[ch][bin];
126        int bits = ff_eac3_bits_vs_hebap[hebap];
127        if (!hebap) {
128            /* zero-mantissa dithering */
129            for (blk = 0; blk < 6; blk++) {
130                s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
131            }
132        } else if (hebap < 8) {
133            /* Vector Quantization */
134            int v = get_bits(gbc, bits);
135            for (blk = 0; blk < 6; blk++) {
136                s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
137            }
138        } else {
139            /* Gain Adaptive Quantization */
140            int gbits, log_gain;
141            if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
142                log_gain = gaq_gain[gs++];
143            } else {
144                log_gain = 0;
145            }
146            gbits = bits - log_gain;
147
148            for (blk = 0; blk < 6; blk++) {
149                int mant = get_sbits(gbc, gbits);
150                if (mant == -(1 << (gbits-1))) {
151                    /* large mantissa */
152                    int b;
153                    mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
154                    /* remap mantissa value to correct for asymmetric quantization */
155                    if (mant >= 0)
156                        b = 32768 >> (log_gain+8);
157                    else
158                        b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
159                    mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
160                } else {
161                    /* small mantissa, no GAQ, or Gk=1 */
162                    mant <<= 24 - bits;
163                    if (!log_gain) {
164                        /* remap mantissa value for no GAQ or Gk=1 */
165                        mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
166                    }
167                }
168                s->pre_mantissa[ch][bin][blk] = mant;
169            }
170        }
171        idct6(s->pre_mantissa[ch][bin]);
172    }
173}
174
175int ff_eac3_parse_header(AC3DecodeContext *s)
176{
177    int i, blk, ch;
178    int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
179    int parse_transient_proc_info;
180    int num_cpl_blocks;
181    GetBitContext *gbc = &s->gbc;
182
183    /* An E-AC-3 stream can have multiple independent streams which the
184       application can select from. each independent stream can also contain
185       dependent streams which are used to add or replace channels. */
186    if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
187        ff_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
188        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
189    } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
190        av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
191        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
192    }
193
194    /* The substream id indicates which substream this frame belongs to. each
195       independent stream has its own substream id, and the dependent streams
196       associated to an independent stream have matching substream id's. */
197    if (s->substreamid) {
198        /* only decode substream with id=0. skip any additional substreams. */
199        ff_log_missing_feature(s->avctx, "Additional substreams", 1);
200        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
201    }
202
203    if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
204        /* The E-AC-3 specification does not tell how to handle reduced sample
205           rates in bit allocation.  The best assumption would be that it is
206           handled like AC-3 DolbyNet, but we cannot be sure until we have a
207           sample which utilizes this feature. */
208        ff_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
209        return -1;
210    }
211    skip_bits(gbc, 5); // skip bitstream id
212
213    /* volume control params */
214    for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
215        skip_bits(gbc, 5); // skip dialog normalization
216        if (get_bits1(gbc)) {
217            skip_bits(gbc, 8); // skip compression gain word
218        }
219    }
220
221    /* dependent stream channel map */
222    if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
223        if (get_bits1(gbc)) {
224            skip_bits(gbc, 16); // skip custom channel map
225        }
226    }
227
228    /* mixing metadata */
229    if (get_bits1(gbc)) {
230        /* center and surround mix levels */
231        if (s->channel_mode > AC3_CHMODE_STEREO) {
232            skip_bits(gbc, 2);  // skip preferred stereo downmix mode
233            if (s->channel_mode & 1) {
234                /* if three front channels exist */
235                skip_bits(gbc, 3); //skip Lt/Rt center mix level
236                s->center_mix_level = get_bits(gbc, 3);
237            }
238            if (s->channel_mode & 4) {
239                /* if a surround channel exists */
240                skip_bits(gbc, 3); //skip Lt/Rt surround mix level
241                s->surround_mix_level = get_bits(gbc, 3);
242            }
243        }
244
245        /* lfe mix level */
246        if (s->lfe_on && get_bits1(gbc)) {
247            // TODO: use LFE mix level
248            skip_bits(gbc, 5); // skip LFE mix level code
249        }
250
251        /* info for mixing with other streams and substreams */
252        if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
253            for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
254                // TODO: apply program scale factor
255                if (get_bits1(gbc)) {
256                    skip_bits(gbc, 6);  // skip program scale factor
257                }
258            }
259            if (get_bits1(gbc)) {
260                skip_bits(gbc, 6);  // skip external program scale factor
261            }
262            /* skip mixing parameter data */
263            switch(get_bits(gbc, 2)) {
264                case 1: skip_bits(gbc, 5);  break;
265                case 2: skip_bits(gbc, 12); break;
266                case 3: {
267                    int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
268                    skip_bits_long(gbc, mix_data_size);
269                    break;
270                }
271            }
272            /* skip pan information for mono or dual mono source */
273            if (s->channel_mode < AC3_CHMODE_STEREO) {
274                for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
275                    if (get_bits1(gbc)) {
276                        /* note: this is not in the ATSC A/52B specification
277                           reference: ETSI TS 102 366 V1.1.1
278                                      section: E.1.3.1.25 */
279                        skip_bits(gbc, 8);  // skip pan mean direction index
280                        skip_bits(gbc, 6);  // skip reserved paninfo bits
281                    }
282                }
283            }
284            /* skip mixing configuration information */
285            if (get_bits1(gbc)) {
286                for (blk = 0; blk < s->num_blocks; blk++) {
287                    if (s->num_blocks == 1 || get_bits1(gbc)) {
288                        skip_bits(gbc, 5);
289                    }
290                }
291            }
292        }
293    }
294
295    /* informational metadata */
296    if (get_bits1(gbc)) {
297        skip_bits(gbc, 3); // skip bit stream mode
298        skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
299        if (s->channel_mode == AC3_CHMODE_STEREO) {
300            skip_bits(gbc, 4); // skip Dolby surround and headphone mode
301        }
302        if (s->channel_mode >= AC3_CHMODE_2F2R) {
303            skip_bits(gbc, 2); // skip Dolby surround EX mode
304        }
305        for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
306            if (get_bits1(gbc)) {
307                skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
308            }
309        }
310        if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
311            skip_bits1(gbc); // skip source sample rate code
312        }
313    }
314
315    /* converter synchronization flag
316       If frames are less than six blocks, this bit should be turned on
317       once every 6 blocks to indicate the start of a frame set.
318       reference: RFC 4598, Section 2.1.3  Frame Sets */
319    if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
320        skip_bits1(gbc); // skip converter synchronization flag
321    }
322
323    /* original frame size code if this stream was converted from AC-3 */
324    if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
325            (s->num_blocks == 6 || get_bits1(gbc))) {
326        skip_bits(gbc, 6); // skip frame size code
327    }
328
329    /* additional bitstream info */
330    if (get_bits1(gbc)) {
331        int addbsil = get_bits(gbc, 6);
332        for (i = 0; i < addbsil + 1; i++) {
333            skip_bits(gbc, 8); // skip additional bit stream info
334        }
335    }
336
337    /* audio frame syntax flags, strategy data, and per-frame data */
338
339    if (s->num_blocks == 6) {
340        ac3_exponent_strategy = get_bits1(gbc);
341        parse_aht_info        = get_bits1(gbc);
342    } else {
343        /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
344           do not use AHT */
345        ac3_exponent_strategy = 1;
346        parse_aht_info = 0;
347    }
348
349    s->snr_offset_strategy    = get_bits(gbc, 2);
350    parse_transient_proc_info = get_bits1(gbc);
351
352    s->block_switch_syntax = get_bits1(gbc);
353    if (!s->block_switch_syntax)
354        memset(s->block_switch, 0, sizeof(s->block_switch));
355
356    s->dither_flag_syntax = get_bits1(gbc);
357    if (!s->dither_flag_syntax) {
358        for (ch = 1; ch <= s->fbw_channels; ch++)
359            s->dither_flag[ch] = 1;
360    }
361    s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
362
363    s->bit_allocation_syntax = get_bits1(gbc);
364    if (!s->bit_allocation_syntax) {
365        /* set default bit allocation parameters */
366        s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
367        s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
368        s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab [1];
369        s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
370        s->bit_alloc_params.floor      = ff_ac3_floor_tab     [7];
371    }
372
373    s->fast_gain_syntax  = get_bits1(gbc);
374    s->dba_syntax        = get_bits1(gbc);
375    s->skip_syntax       = get_bits1(gbc);
376    parse_spx_atten_data = get_bits1(gbc);
377
378    /* coupling strategy occurance and coupling use per block */
379    num_cpl_blocks = 0;
380    if (s->channel_mode > 1) {
381        for (blk = 0; blk < s->num_blocks; blk++) {
382            s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
383            if (s->cpl_strategy_exists[blk]) {
384                s->cpl_in_use[blk] = get_bits1(gbc);
385            } else {
386                s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
387            }
388            num_cpl_blocks += s->cpl_in_use[blk];
389        }
390    } else {
391        memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
392    }
393
394    /* exponent strategy data */
395    if (ac3_exponent_strategy) {
396        /* AC-3-style exponent strategy syntax */
397        for (blk = 0; blk < s->num_blocks; blk++) {
398            for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
399                s->exp_strategy[blk][ch] = get_bits(gbc, 2);
400            }
401        }
402    } else {
403        /* LUT-based exponent strategy syntax */
404        for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
405            int frmchexpstr = get_bits(gbc, 5);
406            for (blk = 0; blk < 6; blk++) {
407                s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
408            }
409        }
410    }
411    /* LFE exponent strategy */
412    if (s->lfe_on) {
413        for (blk = 0; blk < s->num_blocks; blk++) {
414            s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
415        }
416    }
417    /* original exponent strategies if this stream was converted from AC-3 */
418    if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
419            (s->num_blocks == 6 || get_bits1(gbc))) {
420        skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
421    }
422
423    /* determine which channels use AHT */
424    if (parse_aht_info) {
425        /* For AHT to be used, all non-zero blocks must reuse exponents from
426           the first block.  Furthermore, for AHT to be used in the coupling
427           channel, all blocks must use coupling and use the same coupling
428           strategy. */
429        s->channel_uses_aht[CPL_CH]=0;
430        for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
431            int use_aht = 1;
432            for (blk = 1; blk < 6; blk++) {
433                if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
434                        (!ch && s->cpl_strategy_exists[blk])) {
435                    use_aht = 0;
436                    break;
437                }
438            }
439            s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
440        }
441    } else {
442        memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
443    }
444
445    /* per-frame SNR offset */
446    if (!s->snr_offset_strategy) {
447        int csnroffst = (get_bits(gbc, 6) - 15) << 4;
448        int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
449        for (ch = 0; ch <= s->channels; ch++)
450            s->snr_offset[ch] = snroffst;
451    }
452
453    /* transient pre-noise processing data */
454    if (parse_transient_proc_info) {
455        for (ch = 1; ch <= s->fbw_channels; ch++) {
456            if (get_bits1(gbc)) { // channel in transient processing
457                skip_bits(gbc, 10); // skip transient processing location
458                skip_bits(gbc, 8);  // skip transient processing length
459            }
460        }
461    }
462
463    /* spectral extension attenuation data */
464    if (parse_spx_atten_data) {
465        ff_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
466        for (ch = 1; ch <= s->fbw_channels; ch++) {
467            if (get_bits1(gbc)) { // channel has spx attenuation
468                skip_bits(gbc, 5); // skip spx attenuation code
469            }
470        }
471    }
472
473    /* block start information */
474    if (s->num_blocks > 1 && get_bits1(gbc)) {
475        /* reference: Section E2.3.2.27
476           nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
477           The spec does not say what this data is or what it's used for.
478           It is likely the offset of each block within the frame. */
479        int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
480        skip_bits_long(gbc, block_start_bits);
481        ff_log_missing_feature(s->avctx, "Block start info", 1);
482    }
483
484    /* syntax state initialization */
485    for (ch = 1; ch <= s->fbw_channels; ch++) {
486        s->first_cpl_coords[ch] = 1;
487    }
488    s->first_cpl_leak = 1;
489
490    return 0;
491}
492