GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/eac3dec.c Lines: 255 303 84.2 %
Date: 2019-11-22 03:34:36 Branches: 193 256 75.4 %

Line Branch Exec Source
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
/*
24
 * There are several features of E-AC-3 that this decoder does not yet support.
25
 *
26
 * Enhanced Coupling
27
 *     No known samples exist.  If any ever surface, this feature should not be
28
 *     too difficult to implement.
29
 *
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.
33
 *
34
 * Transient Pre-noise Processing
35
 *     This is side information which a decoder should use to reduce artifacts
36
 *     caused by transients.  There are samples which are known to have this
37
 *     information, but this decoder currently ignores it.
38
 */
39
40
41
#include "avcodec.h"
42
#include "internal.h"
43
#include "aac_ac3_parser.h"
44
#include "ac3.h"
45
#include "ac3dec.h"
46
#include "ac3dec_data.h"
47
#include "eac3_data.h"
48
49
/** gain adaptive quantization mode */
50
typedef enum {
51
    EAC3_GAQ_NO =0,
52
    EAC3_GAQ_12,
53
    EAC3_GAQ_14,
54
    EAC3_GAQ_124
55
} EAC3GaqMode;
56
57
#define EAC3_SR_CODE_REDUCED  3
58
59
3228
static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
60
{
61
    int bin, bnd, ch, i;
62
3228
    uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
63
    float rms_energy[SPX_MAX_BANDS];
64
65
    /* Set copy index mapping table. Set wrap flags to apply a notch filter at
66
       wrap points later on. */
67
3228
    bin = s->spx_dst_start_freq;
68
3228
    num_copy_sections = 0;
69
10548
    for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
70
        int copysize;
71
7320
        int bandsize = s->spx_band_sizes[bnd];
72
7320
        if (bin + bandsize > s->spx_src_start_freq) {
73
            copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
74
            bin = s->spx_dst_start_freq;
75
            wrapflag[bnd] = 1;
76
        }
77
14640
        for (i = 0; i < bandsize; i += copysize) {
78
7320
            if (bin == s->spx_src_start_freq) {
79
                copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
80
                bin = s->spx_dst_start_freq;
81
            }
82
7320
            copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin);
83
7320
            bin += copysize;
84
        }
85
    }
86
3228
    copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
87
88
10548
    for (ch = 1; ch <= s->fbw_channels; ch++) {
89
7320
        if (!s->channel_uses_spx[ch])
90
            continue;
91
92
        /* Copy coeffs from normal bands to extension bands */
93
7320
        bin = s->spx_src_start_freq;
94
14640
        for (i = 0; i < num_copy_sections; i++) {
95
7320
            memcpy(&s->transform_coeffs[ch][bin],
96
7320
                   &s->transform_coeffs[ch][s->spx_dst_start_freq],
97
7320
                   copy_sizes[i]*sizeof(INTFLOAT));
98
7320
            bin += copy_sizes[i];
99
        }
100
101
        /* Calculate RMS energy for each SPX band. */
102
7320
        bin = s->spx_src_start_freq;
103
26280
        for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
104
18960
            int bandsize = s->spx_band_sizes[bnd];
105
18960
            float accum = 0.0f;
106
263760
            for (i = 0; i < bandsize; i++) {
107
244800
                float coeff = s->transform_coeffs[ch][bin++];
108
244800
                accum += coeff * coeff;
109
            }
110
18960
            rms_energy[bnd] = sqrtf(accum / bandsize);
111
        }
112
113
        /* Apply a notch filter at transitions between normal and extension
114
           bands and at all wrap points. */
115
7320
        if (s->spx_atten_code[ch] >= 0) {
116
6282
            const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
117
6282
            bin = s->spx_src_start_freq - 2;
118
20052
            for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
119
13770
                if (wrapflag[bnd]) {
120
6282
                    INTFLOAT *coeffs = &s->transform_coeffs[ch][bin];
121
6282
                    coeffs[0] *= atten_tab[0];
122
6282
                    coeffs[1] *= atten_tab[1];
123
6282
                    coeffs[2] *= atten_tab[2];
124
6282
                    coeffs[3] *= atten_tab[1];
125
6282
                    coeffs[4] *= atten_tab[0];
126
                }
127
13770
                bin += s->spx_band_sizes[bnd];
128
            }
129
        }
130
131
        /* Apply noise-blended coefficient scaling based on previously
132
           calculated RMS energy, blending factors, and SPX coordinates for
133
           each band. */
134
7320
        bin = s->spx_src_start_freq;
135
26280
        for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
136
18960
            float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN);
137
18960
            float sscale = s->spx_signal_blend[ch][bnd];
138
#if USE_FIXED
139
            // spx_noise_blend and spx_signal_blend are both FP.23
140
            nscale *= 1.0 / (1<<23);
141
            sscale *= 1.0 / (1<<23);
142
#endif
143
263760
            for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
144
244800
                float noise  = nscale * (int32_t)av_lfg_get(&s->dith_state);
145
244800
                s->transform_coeffs[ch][bin]   *= sscale;
146
244800
                s->transform_coeffs[ch][bin++] += noise;
147
            }
148
        }
149
    }
150
3228
}
151
152
153
/** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
154
#define COEFF_0 10273905LL
155
156
/** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
157
#define COEFF_1 11863283LL
158
159
/** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
160
#define COEFF_2  3070444LL
161
162
/**
163
 * Calculate 6-point IDCT of the pre-mantissas.
164
 * All calculations are 24-bit fixed-point.
165
 */
166
98806
static void idct6(int pre_mant[6])
167
{
168
    int tmp;
169
    int even0, even1, even2, odd0, odd1, odd2;
170
171
98806
    odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
172
173
98806
    even2 = ( pre_mant[2]                * COEFF_0) >> 23;
174
98806
    tmp   = ( pre_mant[4]                * COEFF_1) >> 23;
175
98806
    odd0  = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
176
177
98806
    even0 = pre_mant[0] + (tmp >> 1);
178
98806
    even1 = pre_mant[0] - tmp;
179
180
98806
    tmp = even0;
181
98806
    even0 = tmp + even2;
182
98806
    even2 = tmp - even2;
183
184
98806
    tmp = odd0;
185
98806
    odd0 = tmp + pre_mant[1] + pre_mant[3];
186
98806
    odd2 = tmp + pre_mant[5] - pre_mant[3];
187
188
98806
    pre_mant[0] = even0 + odd0;
189
98806
    pre_mant[1] = even1 + odd1;
190
98806
    pre_mant[2] = even2 + odd2;
191
98806
    pre_mant[3] = even2 - odd2;
192
98806
    pre_mant[4] = even1 - odd1;
193
98806
    pre_mant[5] = even0 - odd0;
194
98806
}
195
196
808
static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
197
{
198
    int bin, blk, gs;
199
    int end_bap, gaq_mode;
200
808
    GetBitContext *gbc = &s->gbc;
201
    int gaq_gain[AC3_MAX_COEFS];
202
203
808
    gaq_mode = get_bits(gbc, 2);
204
808
    end_bap = (gaq_mode < 2) ? 12 : 17;
205
206
    /* if GAQ gain is used, decode gain codes for bins with hebap between
207
       8 and end_bap */
208
808
    gs = 0;
209

808
    if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
210
        /* read 1-bit GAQ gain codes */
211
87190
        for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
212

86495
            if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
213
31184
                gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
214
        }
215
113
    } else if (gaq_mode == EAC3_GAQ_124) {
216
        /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
217
52
        int gc = 2;
218
6560
        for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
219

6508
            if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
220
1905
                if (gc++ == 2) {
221
655
                    int group_code = get_bits(gbc, 5);
222
655
                    if (group_code > 26) {
223
                        av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
224
                        group_code = 26;
225
                    }
226
655
                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
227
655
                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
228
655
                    gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
229
655
                    gc = 0;
230
                }
231
            }
232
        }
233
    }
234
235
808
    gs=0;
236
99614
    for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
237
98806
        int hebap = s->bap[ch][bin];
238
98806
        int bits = ff_eac3_bits_vs_hebap[hebap];
239
98806
        if (!hebap) {
240
            /* zero-mantissa dithering */
241
223020
            for (blk = 0; blk < 6; blk++) {
242
191160
                s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
243
            }
244
66946
        } else if (hebap < 8) {
245
            /* Vector Quantization */
246
30923
            int v = get_bits(gbc, bits);
247
216461
            for (blk = 0; blk < 6; blk++) {
248
185538
                s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] * (1 << 8);
249
            }
250
        } else {
251
            /* Gain Adaptive Quantization */
252
            int gbits, log_gain;
253

36023
            if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
254
33089
                log_gain = gaq_gain[gs++];
255
            } else {
256
2934
                log_gain = 0;
257
            }
258
36023
            gbits = bits - log_gain;
259
260
252161
            for (blk = 0; blk < 6; blk++) {
261
216138
                int mant = get_sbits(gbc, gbits);
262

216138
                if (log_gain && mant == -(1 << (gbits-1))) {
263
                    /* large mantissa */
264
                    int b;
265
24357
                    int mbits = bits - (2 - log_gain);
266
24357
                    mant = get_sbits(gbc, mbits);
267
24357
                    mant = ((unsigned)mant) << (23 - (mbits - 1));
268
                    /* remap mantissa value to correct for asymmetric quantization */
269
24357
                    if (mant >= 0)
270
12102
                        b = 1 << (23 - log_gain);
271
                    else
272
12255
                        b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] * (1 << 8);
273
24357
                    mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b;
274
                } else {
275
                    /* small mantissa, no GAQ, or Gk=1 */
276
191781
                    mant *= (1 << 24 - bits);
277
191781
                    if (!log_gain) {
278
                        /* remap mantissa value for no GAQ or Gk=1 */
279
114612
                        mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15;
280
                    }
281
                }
282
216138
                s->pre_mantissa[ch][bin][blk] = mant;
283
            }
284
        }
285
98806
        idct6(s->pre_mantissa[ch][bin]);
286
    }
287
808
}
288
289
1200
static int ff_eac3_parse_header(AC3DecodeContext *s)
290
{
291
    int i, blk, ch;
292
    int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
293
    int parse_transient_proc_info;
294
    int num_cpl_blocks;
295
1200
    GetBitContext *gbc = &s->gbc;
296
297
    /* An E-AC-3 stream can have multiple independent streams which the
298
       application can select from. each independent stream can also contain
299
       dependent streams which are used to add or replace channels. */
300
1200
    if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
301
        av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
302
        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
303
    }
304
305
    /* The substream id indicates which substream this frame belongs to. each
306
       independent stream has its own substream id, and the dependent streams
307
       associated to an independent stream have matching substream id's. */
308
1200
    if (s->substreamid) {
309
        /* only decode substream with id=0. skip any additional substreams. */
310
        if (!s->eac3_subsbtreamid_found) {
311
            s->eac3_subsbtreamid_found = 1;
312
            avpriv_request_sample(s->avctx, "Additional substreams");
313
        }
314
        return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
315
    }
316
317
1200
    if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
318
        /* The E-AC-3 specification does not tell how to handle reduced sample
319
           rates in bit allocation.  The best assumption would be that it is
320
           handled like AC-3 DolbyNet, but we cannot be sure until we have a
321
           sample which utilizes this feature. */
322
        avpriv_request_sample(s->avctx, "Reduced sampling rate");
323
        return AVERROR_PATCHWELCOME;
324
    }
325
1200
    skip_bits(gbc, 5); // skip bitstream id
326
327
    /* volume control params */
328

2400
    for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
329
1200
        s->dialog_normalization[i] = -get_bits(gbc, 5);
330
1200
        if (s->dialog_normalization[i] == 0) {
331
            s->dialog_normalization[i] = -31;
332
        }
333
1200
        if (s->target_level != 0) {
334
            s->level_gain[i] = powf(2.0f,
335
                (float)(s->target_level - s->dialog_normalization[i])/6.0f);
336
        }
337
1200
        s->compression_exists[i] = get_bits1(gbc);
338
1200
        if (s->compression_exists[i]) {
339
926
            s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8));
340
        }
341
    }
342
343
    /* dependent stream channel map */
344
1200
    if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
345
159
        if (get_bits1(gbc)) {
346
159
            int64_t channel_layout = 0;
347
159
            int channel_map = get_bits(gbc, 16);
348
159
            av_log(s->avctx, AV_LOG_DEBUG, "channel_map: %0X\n", channel_map);
349
350
2703
            for (i = 0; i < 16; i++)
351
2544
                if (channel_map & (1 << (EAC3_MAX_CHANNELS - i - 1)))
352
477
                    channel_layout |= ff_eac3_custom_channel_map_locations[i][1];
353
354
159
            if (av_popcount64(channel_layout) > EAC3_MAX_CHANNELS) {
355
                return AVERROR_INVALIDDATA;
356
            }
357
159
            s->channel_map = channel_map;
358
        }
359
    }
360
361
    /* mixing metadata */
362
1200
    if (get_bits1(gbc)) {
363
        /* center and surround mix levels */
364
636
        if (s->channel_mode > AC3_CHMODE_STEREO) {
365
146
            s->preferred_downmix = get_bits(gbc, 2);
366
146
            if (s->channel_mode & 1) {
367
                /* if three front channels exist */
368
146
                s->center_mix_level_ltrt = get_bits(gbc, 3);
369
146
                s->center_mix_level      = get_bits(gbc, 3);
370
            }
371
146
            if (s->channel_mode & 4) {
372
                /* if a surround channel exists */
373
146
                s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7);
374
146
                s->surround_mix_level      = av_clip(get_bits(gbc, 3), 3, 7);
375
            }
376
        }
377
378
        /* lfe mix level */
379

636
        if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) {
380
146
            s->lfe_mix_level = get_bits(gbc, 5);
381
        }
382
383
        /* info for mixing with other streams and substreams */
384
636
        if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
385

1272
            for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
386
                // TODO: apply program scale factor
387
636
                if (get_bits1(gbc)) {
388
                    skip_bits(gbc, 6);  // skip program scale factor
389
                }
390
            }
391
636
            if (get_bits1(gbc)) {
392
                skip_bits(gbc, 6);  // skip external program scale factor
393
            }
394
            /* skip mixing parameter data */
395

636
            switch(get_bits(gbc, 2)) {
396
                case 1: skip_bits(gbc, 5);  break;
397
                case 2: skip_bits(gbc, 12); break;
398
                case 3: {
399
                    int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
400
                    skip_bits_long(gbc, mix_data_size);
401
                    break;
402
                }
403
            }
404
            /* skip pan information for mono or dual mono source */
405
636
            if (s->channel_mode < AC3_CHMODE_STEREO) {
406
                for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
407
                    if (get_bits1(gbc)) {
408
                        /* note: this is not in the ATSC A/52B specification
409
                           reference: ETSI TS 102 366 V1.1.1
410
                                      section: E.1.3.1.25 */
411
                        skip_bits(gbc, 8);  // skip pan mean direction index
412
                        skip_bits(gbc, 6);  // skip reserved paninfo bits
413
                    }
414
                }
415
            }
416
            /* skip mixing configuration information */
417
636
            if (get_bits1(gbc)) {
418
                for (blk = 0; blk < s->num_blocks; blk++) {
419
                    if (s->num_blocks == 1 || get_bits1(gbc)) {
420
                        skip_bits(gbc, 5);
421
                    }
422
                }
423
            }
424
        }
425
    }
426
427
    /* informational metadata */
428
1200
    if (get_bits1(gbc)) {
429
767
        s->bitstream_mode = get_bits(gbc, 3);
430
767
        skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
431
767
        if (s->channel_mode == AC3_CHMODE_STEREO) {
432
621
            s->dolby_surround_mode  = get_bits(gbc, 2);
433
621
            s->dolby_headphone_mode = get_bits(gbc, 2);
434
        }
435
767
        if (s->channel_mode >= AC3_CHMODE_2F2R) {
436
146
            s->dolby_surround_ex_mode = get_bits(gbc, 2);
437
        }
438

1534
        for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
439
767
            if (get_bits1(gbc)) {
440
767
                skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
441
            }
442
        }
443
767
        if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
444
767
            skip_bits1(gbc); // skip source sample rate code
445
        }
446
    }
447
448
    /* converter synchronization flag
449
       If frames are less than six blocks, this bit should be turned on
450
       once every 6 blocks to indicate the start of a frame set.
451
       reference: RFC 4598, Section 2.1.3  Frame Sets */
452

1200
    if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
453
98
        skip_bits1(gbc); // skip converter synchronization flag
454
    }
455
456
    /* original frame size code if this stream was converted from AC-3 */
457
1200
    if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
458
            (s->num_blocks == 6 || get_bits1(gbc))) {
459
        skip_bits(gbc, 6); // skip frame size code
460
    }
461
462
    /* additional bitstream info */
463
1200
    if (get_bits1(gbc)) {
464
159
        int addbsil = get_bits(gbc, 6);
465
477
        for (i = 0; i < addbsil + 1; i++) {
466
318
            skip_bits(gbc, 8); // skip additional bit stream info
467
        }
468
    }
469
470
    /* audio frame syntax flags, strategy data, and per-frame data */
471
472
1200
    if (s->num_blocks == 6) {
473
1102
        ac3_exponent_strategy = get_bits1(gbc);
474
1102
        parse_aht_info        = get_bits1(gbc);
475
    } else {
476
        /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
477
           do not use AHT */
478
98
        ac3_exponent_strategy = 1;
479
98
        parse_aht_info = 0;
480
    }
481
482
1200
    s->snr_offset_strategy    = get_bits(gbc, 2);
483
1200
    parse_transient_proc_info = get_bits1(gbc);
484
485
1200
    s->block_switch_syntax = get_bits1(gbc);
486
1200
    if (!s->block_switch_syntax)
487
1200
        memset(s->block_switch, 0, sizeof(s->block_switch));
488
489
1200
    s->dither_flag_syntax = get_bits1(gbc);
490
1200
    if (!s->dither_flag_syntax) {
491
4356
        for (ch = 1; ch <= s->fbw_channels; ch++)
492
3156
            s->dither_flag[ch] = 1;
493
    }
494
1200
    s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
495
496
1200
    s->bit_allocation_syntax = get_bits1(gbc);
497
1200
    if (!s->bit_allocation_syntax) {
498
        /* set default bit allocation parameters */
499
914
        s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
500
914
        s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
501
914
        s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab [1];
502
914
        s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
503
914
        s->bit_alloc_params.floor      = ff_ac3_floor_tab     [7];
504
    }
505
506
1200
    s->fast_gain_syntax  = get_bits1(gbc);
507
1200
    s->dba_syntax        = get_bits1(gbc);
508
1200
    s->skip_syntax       = get_bits1(gbc);
509
1200
    parse_spx_atten_data = get_bits1(gbc);
510
511
    /* coupling strategy occurrence and coupling use per block */
512
1200
    num_cpl_blocks = 0;
513
1200
    if (s->channel_mode > 1) {
514
7910
        for (blk = 0; blk < s->num_blocks; blk++) {
515

6710
            s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
516
6710
            if (s->cpl_strategy_exists[blk]) {
517
1200
                s->cpl_in_use[blk] = get_bits1(gbc);
518
            } else {
519
5510
                s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
520
            }
521
6710
            num_cpl_blocks += s->cpl_in_use[blk];
522
        }
523
    } else {
524
        memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
525
    }
526
527
    /* exponent strategy data */
528
1200
    if (ac3_exponent_strategy) {
529
        /* AC-3-style exponent strategy syntax */
530
196
        for (blk = 0; blk < s->num_blocks; blk++) {
531
588
            for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
532
490
                s->exp_strategy[blk][ch] = get_bits(gbc, 2);
533
            }
534
        }
535
    } else {
536
        /* LUT-based exponent strategy syntax */
537

4173
        for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
538
3071
            int frmchexpstr = get_bits(gbc, 5);
539
21497
            for (blk = 0; blk < 6; blk++) {
540
18426
                s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
541
            }
542
        }
543
    }
544
    /* LFE exponent strategy */
545
1200
    if (s->lfe_on) {
546
532
        for (blk = 0; blk < s->num_blocks; blk++) {
547
386
            s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
548
        }
549
    }
550
    /* original exponent strategies if this stream was converted from AC-3 */
551
1200
    if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
552

1041
            (s->num_blocks == 6 || get_bits1(gbc))) {
553
959
        skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
554
    }
555
556
    /* determine which channels use AHT */
557
1200
    if (parse_aht_info) {
558
        /* For AHT to be used, all non-zero blocks must reuse exponents from
559
           the first block.  Furthermore, for AHT to be used in the coupling
560
           channel, all blocks must use coupling and use the same coupling
561
           strategy. */
562
444
        s->channel_uses_aht[CPL_CH]=0;
563
1524
        for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
564
1080
            int use_aht = 1;
565
5502
            for (blk = 1; blk < 6; blk++) {
566

4694
                if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
567
                        (!ch && s->cpl_strategy_exists[blk])) {
568
272
                    use_aht = 0;
569
272
                    break;
570
                }
571
            }
572

1080
            s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
573
        }
574
    } else {
575
756
        memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
576
    }
577
578
    /* per-frame SNR offset */
579
1200
    if (!s->snr_offset_strategy) {
580
1200
        int csnroffst = (get_bits(gbc, 6) - 15) << 4;
581
1200
        int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
582
5702
        for (ch = 0; ch <= s->channels; ch++)
583
4502
            s->snr_offset[ch] = snroffst;
584
    }
585
586
    /* transient pre-noise processing data */
587
1200
    if (parse_transient_proc_info) {
588
        for (ch = 1; ch <= s->fbw_channels; ch++) {
589
            if (get_bits1(gbc)) { // channel in transient processing
590
                skip_bits(gbc, 10); // skip transient processing location
591
                skip_bits(gbc, 8);  // skip transient processing length
592
            }
593
        }
594
    }
595
596
    /* spectral extension attenuation data */
597
4356
    for (ch = 1; ch <= s->fbw_channels; ch++) {
598

3156
        if (parse_spx_atten_data && get_bits1(gbc)) {
599
1047
            s->spx_atten_code[ch] = get_bits(gbc, 5);
600
        } else {
601
2109
            s->spx_atten_code[ch] = -1;
602
        }
603
    }
604
605
    /* block start information */
606

1200
    if (s->num_blocks > 1 && get_bits1(gbc)) {
607
        /* reference: Section E2.3.2.27
608
           nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
609
           The spec does not say what this data is or what it's used for.
610
           It is likely the offset of each block within the frame. */
611
        int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
612
        skip_bits_long(gbc, block_start_bits);
613
        avpriv_request_sample(s->avctx, "Block start info");
614
    }
615
616
    /* syntax state initialization */
617
4356
    for (ch = 1; ch <= s->fbw_channels; ch++) {
618
3156
        s->first_spx_coords[ch] = 1;
619
3156
        s->first_cpl_coords[ch] = 1;
620
    }
621
1200
    s->first_cpl_leak = 1;
622
623
1200
    return 0;
624
}