LCOV - code coverage report
Current view: top level - libavcodec - eac3dec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 244 300 81.3 %
Date: 2017-12-15 11:05:35 Functions: 4 4 100.0 %

          Line data    Source code
       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             :  * Dependent Streams
      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
      38             :  *     Blu-ray.
      39             :  *
      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.
      44             :  */
      45             : 
      46             : 
      47             : #include "avcodec.h"
      48             : #include "internal.h"
      49             : #include "aac_ac3_parser.h"
      50             : #include "ac3.h"
      51             : #include "ac3dec.h"
      52             : #include "ac3dec_data.h"
      53             : #include "eac3_data.h"
      54             : 
      55             : /** gain adaptive quantization mode */
      56             : typedef enum {
      57             :     EAC3_GAQ_NO =0,
      58             :     EAC3_GAQ_12,
      59             :     EAC3_GAQ_14,
      60             :     EAC3_GAQ_124
      61             : } EAC3GaqMode;
      62             : 
      63             : #define EAC3_SR_CODE_REDUCED  3
      64             : 
      65        3210 : static void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
      66             : {
      67             :     int bin, bnd, ch, i;
      68        3210 :     uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
      69             :     float rms_energy[SPX_MAX_BANDS];
      70             : 
      71             :     /* Set copy index mapping table. Set wrap flags to apply a notch filter at
      72             :        wrap points later on. */
      73        3210 :     bin = s->spx_dst_start_freq;
      74        3210 :     num_copy_sections = 0;
      75       10476 :     for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
      76             :         int copysize;
      77        7266 :         int bandsize = s->spx_band_sizes[bnd];
      78        7266 :         if (bin + bandsize > s->spx_src_start_freq) {
      79           0 :             copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
      80           0 :             bin = s->spx_dst_start_freq;
      81           0 :             wrapflag[bnd] = 1;
      82             :         }
      83       14532 :         for (i = 0; i < bandsize; i += copysize) {
      84        7266 :             if (bin == s->spx_src_start_freq) {
      85           0 :                 copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
      86           0 :                 bin = s->spx_dst_start_freq;
      87             :             }
      88        7266 :             copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin);
      89        7266 :             bin += copysize;
      90             :         }
      91             :     }
      92        3210 :     copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq;
      93             : 
      94       10476 :     for (ch = 1; ch <= s->fbw_channels; ch++) {
      95        7266 :         if (!s->channel_uses_spx[ch])
      96           0 :             continue;
      97             : 
      98             :         /* Copy coeffs from normal bands to extension bands */
      99        7266 :         bin = s->spx_src_start_freq;
     100       14532 :         for (i = 0; i < num_copy_sections; i++) {
     101       14532 :             memcpy(&s->transform_coeffs[ch][bin],
     102        7266 :                    &s->transform_coeffs[ch][s->spx_dst_start_freq],
     103        7266 :                    copy_sizes[i]*sizeof(INTFLOAT));
     104        7266 :             bin += copy_sizes[i];
     105             :         }
     106             : 
     107             :         /* Calculate RMS energy for each SPX band. */
     108        7266 :         bin = s->spx_src_start_freq;
     109       26028 :         for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
     110       18762 :             int bandsize = s->spx_band_sizes[bnd];
     111       18762 :             float accum = 0.0f;
     112      260826 :             for (i = 0; i < bandsize; i++) {
     113      242064 :                 float coeff = s->transform_coeffs[ch][bin++];
     114      242064 :                 accum += coeff * coeff;
     115             :             }
     116       18762 :             rms_energy[bnd] = sqrtf(accum / bandsize);
     117             :         }
     118             : 
     119             :         /* Apply a notch filter at transitions between normal and extension
     120             :            bands and at all wrap points. */
     121        7266 :         if (s->spx_atten_code[ch] >= 0) {
     122        6252 :             const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
     123        6252 :             bin = s->spx_src_start_freq - 2;
     124       19944 :             for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
     125       13692 :                 if (wrapflag[bnd]) {
     126        6252 :                     INTFLOAT *coeffs = &s->transform_coeffs[ch][bin];
     127        6252 :                     coeffs[0] *= atten_tab[0];
     128        6252 :                     coeffs[1] *= atten_tab[1];
     129        6252 :                     coeffs[2] *= atten_tab[2];
     130        6252 :                     coeffs[3] *= atten_tab[1];
     131        6252 :                     coeffs[4] *= atten_tab[0];
     132             :                 }
     133       13692 :                 bin += s->spx_band_sizes[bnd];
     134             :             }
     135             :         }
     136             : 
     137             :         /* Apply noise-blended coefficient scaling based on previously
     138             :            calculated RMS energy, blending factors, and SPX coordinates for
     139             :            each band. */
     140        7266 :         bin = s->spx_src_start_freq;
     141       26028 :         for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
     142       18762 :             float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN);
     143       18762 :             float sscale = s->spx_signal_blend[ch][bnd];
     144             : #if USE_FIXED
     145             :             // spx_noise_blend and spx_signal_blend are both FP.23
     146           0 :             nscale *= 1.0 / (1<<23);
     147           0 :             sscale *= 1.0 / (1<<23);
     148             : #endif
     149      260826 :             for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
     150      242064 :                 float noise  = nscale * (int32_t)av_lfg_get(&s->dith_state);
     151      242064 :                 s->transform_coeffs[ch][bin]   *= sscale;
     152      242064 :                 s->transform_coeffs[ch][bin++] += noise;
     153             :             }
     154             :         }
     155             :     }
     156        3210 : }
     157             : 
     158             : 
     159             : /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
     160             : #define COEFF_0 10273905LL
     161             : 
     162             : /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
     163             : #define COEFF_1 11863283LL
     164             : 
     165             : /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
     166             : #define COEFF_2  3070444LL
     167             : 
     168             : /**
     169             :  * Calculate 6-point IDCT of the pre-mantissas.
     170             :  * All calculations are 24-bit fixed-point.
     171             :  */
     172       97722 : static void idct6(int pre_mant[6])
     173             : {
     174             :     int tmp;
     175             :     int even0, even1, even2, odd0, odd1, odd2;
     176             : 
     177       97722 :     odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
     178             : 
     179       97722 :     even2 = ( pre_mant[2]                * COEFF_0) >> 23;
     180       97722 :     tmp   = ( pre_mant[4]                * COEFF_1) >> 23;
     181       97722 :     odd0  = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
     182             : 
     183       97722 :     even0 = pre_mant[0] + (tmp >> 1);
     184       97722 :     even1 = pre_mant[0] - tmp;
     185             : 
     186       97722 :     tmp = even0;
     187       97722 :     even0 = tmp + even2;
     188       97722 :     even2 = tmp - even2;
     189             : 
     190       97722 :     tmp = odd0;
     191       97722 :     odd0 = tmp + pre_mant[1] + pre_mant[3];
     192       97722 :     odd2 = tmp + pre_mant[5] - pre_mant[3];
     193             : 
     194       97722 :     pre_mant[0] = even0 + odd0;
     195       97722 :     pre_mant[1] = even1 + odd1;
     196       97722 :     pre_mant[2] = even2 + odd2;
     197       97722 :     pre_mant[3] = even2 - odd2;
     198       97722 :     pre_mant[4] = even1 - odd1;
     199       97722 :     pre_mant[5] = even0 - odd0;
     200       97722 : }
     201             : 
     202         798 : static void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
     203             : {
     204             :     int bin, blk, gs;
     205             :     int end_bap, gaq_mode;
     206         798 :     GetBitContext *gbc = &s->gbc;
     207             :     int gaq_gain[AC3_MAX_COEFS];
     208             : 
     209         798 :     gaq_mode = get_bits(gbc, 2);
     210         798 :     end_bap = (gaq_mode < 2) ? 12 : 17;
     211             : 
     212             :     /* if GAQ gain is used, decode gain codes for bins with hebap between
     213             :        8 and end_bap */
     214         798 :     gs = 0;
     215        1483 :     if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
     216             :         /* read 1-bit GAQ gain codes */
     217       86096 :         for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
     218       85411 :             if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
     219       30712 :                 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
     220             :         }
     221         113 :     } else if (gaq_mode == EAC3_GAQ_124) {
     222             :         /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
     223          52 :         int gc = 2;
     224        6560 :         for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
     225        6508 :             if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
     226        1905 :                 if (gc++ == 2) {
     227         655 :                     int group_code = get_bits(gbc, 5);
     228         655 :                     if (group_code > 26) {
     229           0 :                         av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
     230           0 :                         group_code = 26;
     231             :                     }
     232         655 :                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
     233         655 :                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
     234         655 :                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
     235         655 :                     gc = 0;
     236             :                 }
     237             :             }
     238             :         }
     239             :     }
     240             : 
     241         798 :     gs=0;
     242       98520 :     for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
     243       97722 :         int hebap = s->bap[ch][bin];
     244       97722 :         int bits = ff_eac3_bits_vs_hebap[hebap];
     245       97722 :         if (!hebap) {
     246             :             /* zero-mantissa dithering */
     247      221074 :             for (blk = 0; blk < 6; blk++) {
     248      189492 :                 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
     249             :             }
     250       66140 :         } else if (hebap < 8) {
     251             :             /* Vector Quantization */
     252       30589 :             int v = get_bits(gbc, bits);
     253      214123 :             for (blk = 0; blk < 6; blk++) {
     254      183534 :                 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] * (1 << 8);
     255             :             }
     256             :         } else {
     257             :             /* Gain Adaptive Quantization */
     258             :             int gbits, log_gain;
     259       35551 :             if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
     260       32617 :                 log_gain = gaq_gain[gs++];
     261             :             } else {
     262        2934 :                 log_gain = 0;
     263             :             }
     264       35551 :             gbits = bits - log_gain;
     265             : 
     266      248857 :             for (blk = 0; blk < 6; blk++) {
     267      213306 :                 int mant = get_sbits(gbc, gbits);
     268      237326 :                 if (log_gain && mant == -(1 << (gbits-1))) {
     269             :                     /* large mantissa */
     270             :                     int b;
     271       24020 :                     int mbits = bits - (2 - log_gain);
     272       24020 :                     mant = get_sbits(gbc, mbits);
     273       24020 :                     mant = ((unsigned)mant) << (23 - (mbits - 1));
     274             :                     /* remap mantissa value to correct for asymmetric quantization */
     275       24020 :                     if (mant >= 0)
     276       11944 :                         b = 1 << (23 - log_gain);
     277             :                     else
     278       12076 :                         b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] * (1 << 8);
     279       24020 :                     mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b;
     280             :                 } else {
     281             :                     /* small mantissa, no GAQ, or Gk=1 */
     282      189286 :                     mant *= (1 << 24 - bits);
     283      189286 :                     if (!log_gain) {
     284             :                         /* remap mantissa value for no GAQ or Gk=1 */
     285      113220 :                         mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15;
     286             :                     }
     287             :                 }
     288      213306 :                 s->pre_mantissa[ch][bin][blk] = mant;
     289             :             }
     290             :         }
     291       97722 :         idct6(s->pre_mantissa[ch][bin]);
     292             :     }
     293         798 : }
     294             : 
     295        1035 : static int ff_eac3_parse_header(AC3DecodeContext *s)
     296             : {
     297             :     int i, blk, ch;
     298             :     int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
     299             :     int parse_transient_proc_info;
     300             :     int num_cpl_blocks;
     301        1035 :     GetBitContext *gbc = &s->gbc;
     302             : 
     303             :     /* An E-AC-3 stream can have multiple independent streams which the
     304             :        application can select from. each independent stream can also contain
     305             :        dependent streams which are used to add or replace channels. */
     306        1035 :     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
     307           0 :         if (!s->eac3_frame_dependent_found) {
     308           0 :             s->eac3_frame_dependent_found = 1;
     309           0 :             avpriv_request_sample(s->avctx, "Dependent substream decoding");
     310             :         }
     311           0 :         return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     312        1035 :     } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
     313           0 :         av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
     314           0 :         return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     315             :     }
     316             : 
     317             :     /* The substream id indicates which substream this frame belongs to. each
     318             :        independent stream has its own substream id, and the dependent streams
     319             :        associated to an independent stream have matching substream id's. */
     320        1035 :     if (s->substreamid) {
     321             :         /* only decode substream with id=0. skip any additional substreams. */
     322           0 :         if (!s->eac3_subsbtreamid_found) {
     323           0 :             s->eac3_subsbtreamid_found = 1;
     324           0 :             avpriv_request_sample(s->avctx, "Additional substreams");
     325             :         }
     326           0 :         return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     327             :     }
     328             : 
     329        1035 :     if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
     330             :         /* The E-AC-3 specification does not tell how to handle reduced sample
     331             :            rates in bit allocation.  The best assumption would be that it is
     332             :            handled like AC-3 DolbyNet, but we cannot be sure until we have a
     333             :            sample which utilizes this feature. */
     334           0 :         avpriv_request_sample(s->avctx, "Reduced sampling rate");
     335           0 :         return AVERROR_PATCHWELCOME;
     336             :     }
     337        1035 :     skip_bits(gbc, 5); // skip bitstream id
     338             : 
     339             :     /* volume control params */
     340        2070 :     for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
     341        1035 :         s->dialog_normalization[i] = -get_bits(gbc, 5);
     342        1035 :         if (s->dialog_normalization[i] == 0) {
     343           0 :             s->dialog_normalization[i] = -31;
     344             :         }
     345        1035 :         if (s->target_level != 0) {
     346           0 :             s->level_gain[i] = powf(2.0f,
     347           0 :                 (float)(s->target_level - s->dialog_normalization[i])/6.0f);
     348             :         }
     349        1035 :         s->compression_exists[i] = get_bits1(gbc);
     350        1035 :         if (s->compression_exists[i]) {
     351         762 :             s->heavy_dynamic_range[i] = AC3_HEAVY_RANGE(get_bits(gbc, 8));
     352             :         }
     353             :     }
     354             : 
     355             :     /* dependent stream channel map */
     356        1035 :     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
     357           0 :         if (get_bits1(gbc)) {
     358           0 :             skip_bits(gbc, 16); // skip custom channel map
     359             :         }
     360             :     }
     361             : 
     362             :     /* mixing metadata */
     363        1035 :     if (get_bits1(gbc)) {
     364             :         /* center and surround mix levels */
     365         632 :         if (s->channel_mode > AC3_CHMODE_STEREO) {
     366         144 :             s->preferred_downmix = get_bits(gbc, 2);
     367         144 :             if (s->channel_mode & 1) {
     368             :                 /* if three front channels exist */
     369         144 :                 s->center_mix_level_ltrt = get_bits(gbc, 3);
     370         144 :                 s->center_mix_level      = get_bits(gbc, 3);
     371             :             }
     372         144 :             if (s->channel_mode & 4) {
     373             :                 /* if a surround channel exists */
     374         144 :                 s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7);
     375         144 :                 s->surround_mix_level      = av_clip(get_bits(gbc, 3), 3, 7);
     376             :             }
     377             :         }
     378             : 
     379             :         /* lfe mix level */
     380         632 :         if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) {
     381         144 :             s->lfe_mix_level = get_bits(gbc, 5);
     382             :         }
     383             : 
     384             :         /* info for mixing with other streams and substreams */
     385         632 :         if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
     386        1264 :             for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
     387             :                 // TODO: apply program scale factor
     388         632 :                 if (get_bits1(gbc)) {
     389           0 :                     skip_bits(gbc, 6);  // skip program scale factor
     390             :                 }
     391             :             }
     392         632 :             if (get_bits1(gbc)) {
     393           0 :                 skip_bits(gbc, 6);  // skip external program scale factor
     394             :             }
     395             :             /* skip mixing parameter data */
     396         632 :             switch(get_bits(gbc, 2)) {
     397           0 :                 case 1: skip_bits(gbc, 5);  break;
     398           0 :                 case 2: skip_bits(gbc, 12); break;
     399           0 :                 case 3: {
     400           0 :                     int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
     401           0 :                     skip_bits_long(gbc, mix_data_size);
     402           0 :                     break;
     403             :                 }
     404             :             }
     405             :             /* skip pan information for mono or dual mono source */
     406         632 :             if (s->channel_mode < AC3_CHMODE_STEREO) {
     407           0 :                 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
     408           0 :                     if (get_bits1(gbc)) {
     409             :                         /* note: this is not in the ATSC A/52B specification
     410             :                            reference: ETSI TS 102 366 V1.1.1
     411             :                                       section: E.1.3.1.25 */
     412           0 :                         skip_bits(gbc, 8);  // skip pan mean direction index
     413           0 :                         skip_bits(gbc, 6);  // skip reserved paninfo bits
     414             :                     }
     415             :                 }
     416             :             }
     417             :             /* skip mixing configuration information */
     418         632 :             if (get_bits1(gbc)) {
     419           0 :                 for (blk = 0; blk < s->num_blocks; blk++) {
     420           0 :                     if (s->num_blocks == 1 || get_bits1(gbc)) {
     421           0 :                         skip_bits(gbc, 5);
     422             :                     }
     423             :                 }
     424             :             }
     425             :         }
     426             :     }
     427             : 
     428             :     /* informational metadata */
     429        1035 :     if (get_bits1(gbc)) {
     430         762 :         s->bitstream_mode = get_bits(gbc, 3);
     431         762 :         skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
     432         762 :         if (s->channel_mode == AC3_CHMODE_STEREO) {
     433         618 :             s->dolby_surround_mode  = get_bits(gbc, 2);
     434         618 :             s->dolby_headphone_mode = get_bits(gbc, 2);
     435             :         }
     436         762 :         if (s->channel_mode >= AC3_CHMODE_2F2R) {
     437         144 :             s->dolby_surround_ex_mode = get_bits(gbc, 2);
     438             :         }
     439        1524 :         for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
     440         762 :             if (get_bits1(gbc)) {
     441         762 :                 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
     442             :             }
     443             :         }
     444         762 :         if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
     445         762 :             skip_bits1(gbc); // skip source sample rate code
     446             :         }
     447             :     }
     448             : 
     449             :     /* converter synchronization flag
     450             :        If frames are less than six blocks, this bit should be turned on
     451             :        once every 6 blocks to indicate the start of a frame set.
     452             :        reference: RFC 4598, Section 2.1.3  Frame Sets */
     453        1035 :     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
     454          97 :         skip_bits1(gbc); // skip converter synchronization flag
     455             :     }
     456             : 
     457             :     /* original frame size code if this stream was converted from AC-3 */
     458        1035 :     if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
     459           0 :             (s->num_blocks == 6 || get_bits1(gbc))) {
     460           0 :         skip_bits(gbc, 6); // skip frame size code
     461             :     }
     462             : 
     463             :     /* additional bitstream info */
     464        1035 :     if (get_bits1(gbc)) {
     465           0 :         int addbsil = get_bits(gbc, 6);
     466           0 :         for (i = 0; i < addbsil + 1; i++) {
     467           0 :             skip_bits(gbc, 8); // skip additional bit stream info
     468             :         }
     469             :     }
     470             : 
     471             :     /* audio frame syntax flags, strategy data, and per-frame data */
     472             : 
     473        1035 :     if (s->num_blocks == 6) {
     474         938 :         ac3_exponent_strategy = get_bits1(gbc);
     475         938 :         parse_aht_info        = get_bits1(gbc);
     476             :     } else {
     477             :         /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
     478             :            do not use AHT */
     479          97 :         ac3_exponent_strategy = 1;
     480          97 :         parse_aht_info = 0;
     481             :     }
     482             : 
     483        1035 :     s->snr_offset_strategy    = get_bits(gbc, 2);
     484        1035 :     parse_transient_proc_info = get_bits1(gbc);
     485             : 
     486        1035 :     s->block_switch_syntax = get_bits1(gbc);
     487        1035 :     if (!s->block_switch_syntax)
     488        1035 :         memset(s->block_switch, 0, sizeof(s->block_switch));
     489             : 
     490        1035 :     s->dither_flag_syntax = get_bits1(gbc);
     491        1035 :     if (!s->dither_flag_syntax) {
     492        3537 :         for (ch = 1; ch <= s->fbw_channels; ch++)
     493        2502 :             s->dither_flag[ch] = 1;
     494             :     }
     495        1035 :     s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
     496             : 
     497        1035 :     s->bit_allocation_syntax = get_bits1(gbc);
     498        1035 :     if (!s->bit_allocation_syntax) {
     499             :         /* set default bit allocation parameters */
     500         810 :         s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
     501         810 :         s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
     502         810 :         s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab [1];
     503         810 :         s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
     504         810 :         s->bit_alloc_params.floor      = ff_ac3_floor_tab     [7];
     505             :     }
     506             : 
     507        1035 :     s->fast_gain_syntax  = get_bits1(gbc);
     508        1035 :     s->dba_syntax        = get_bits1(gbc);
     509        1035 :     s->skip_syntax       = get_bits1(gbc);
     510        1035 :     parse_spx_atten_data = get_bits1(gbc);
     511             : 
     512             :     /* coupling strategy occurrence and coupling use per block */
     513        1035 :     num_cpl_blocks = 0;
     514        1035 :     if (s->channel_mode > 1) {
     515        6760 :         for (blk = 0; blk < s->num_blocks; blk++) {
     516        5725 :             s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
     517        5725 :             if (s->cpl_strategy_exists[blk]) {
     518        1035 :                 s->cpl_in_use[blk] = get_bits1(gbc);
     519             :             } else {
     520        4690 :                 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
     521             :             }
     522        5725 :             num_cpl_blocks += s->cpl_in_use[blk];
     523             :         }
     524             :     } else {
     525           0 :         memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
     526             :     }
     527             : 
     528             :     /* exponent strategy data */
     529        1035 :     if (ac3_exponent_strategy) {
     530             :         /* AC-3-style exponent strategy syntax */
     531         194 :         for (blk = 0; blk < s->num_blocks; blk++) {
     532         582 :             for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
     533         485 :                 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
     534             :             }
     535             :         }
     536             :     } else {
     537             :         /* LUT-based exponent strategy syntax */
     538        3358 :         for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
     539        2420 :             int frmchexpstr = get_bits(gbc, 5);
     540       16940 :             for (blk = 0; blk < 6; blk++) {
     541       14520 :                 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
     542             :             }
     543             :         }
     544             :     }
     545             :     /* LFE exponent strategy */
     546        1035 :     if (s->lfe_on) {
     547         523 :         for (blk = 0; blk < s->num_blocks; blk++) {
     548         379 :             s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
     549             :         }
     550             :     }
     551             :     /* original exponent strategies if this stream was converted from AC-3 */
     552        2070 :     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
     553        1132 :             (s->num_blocks == 6 || get_bits1(gbc))) {
     554         954 :         skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
     555             :     }
     556             : 
     557             :     /* determine which channels use AHT */
     558        1035 :     if (parse_aht_info) {
     559             :         /* For AHT to be used, all non-zero blocks must reuse exponents from
     560             :            the first block.  Furthermore, for AHT to be used in the coupling
     561             :            channel, all blocks must use coupling and use the same coupling
     562             :            strategy. */
     563         441 :         s->channel_uses_aht[CPL_CH]=0;
     564        1511 :         for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
     565        1070 :             int use_aht = 1;
     566        5442 :             for (blk = 1; blk < 6; blk++) {
     567        4644 :                 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
     568           0 :                         (!ch && s->cpl_strategy_exists[blk])) {
     569         272 :                     use_aht = 0;
     570         272 :                     break;
     571             :                 }
     572             :             }
     573        1070 :             s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
     574             :         }
     575             :     } else {
     576         594 :         memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
     577             :     }
     578             : 
     579             :     /* per-frame SNR offset */
     580        1035 :     if (!s->snr_offset_strategy) {
     581        1035 :         int csnroffst = (get_bits(gbc, 6) - 15) << 4;
     582        1035 :         int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
     583        4716 :         for (ch = 0; ch <= s->channels; ch++)
     584        3681 :             s->snr_offset[ch] = snroffst;
     585             :     }
     586             : 
     587             :     /* transient pre-noise processing data */
     588        1035 :     if (parse_transient_proc_info) {
     589           0 :         for (ch = 1; ch <= s->fbw_channels; ch++) {
     590           0 :             if (get_bits1(gbc)) { // channel in transient processing
     591           0 :                 skip_bits(gbc, 10); // skip transient processing location
     592           0 :                 skip_bits(gbc, 8);  // skip transient processing length
     593             :             }
     594             :         }
     595             :     }
     596             : 
     597             :     /* spectral extension attenuation data */
     598        3537 :     for (ch = 1; ch <= s->fbw_channels; ch++) {
     599        2502 :         if (parse_spx_atten_data && get_bits1(gbc)) {
     600        1042 :             s->spx_atten_code[ch] = get_bits(gbc, 5);
     601             :         } else {
     602        1460 :             s->spx_atten_code[ch] = -1;
     603             :         }
     604             :     }
     605             : 
     606             :     /* block start information */
     607        1035 :     if (s->num_blocks > 1 && get_bits1(gbc)) {
     608             :         /* reference: Section E2.3.2.27
     609             :            nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
     610             :            The spec does not say what this data is or what it's used for.
     611             :            It is likely the offset of each block within the frame. */
     612           0 :         int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
     613           0 :         skip_bits_long(gbc, block_start_bits);
     614           0 :         avpriv_request_sample(s->avctx, "Block start info");
     615             :     }
     616             : 
     617             :     /* syntax state initialization */
     618        3537 :     for (ch = 1; ch <= s->fbw_channels; ch++) {
     619        2502 :         s->first_spx_coords[ch] = 1;
     620        2502 :         s->first_cpl_coords[ch] = 1;
     621             :     }
     622        1035 :     s->first_cpl_leak = 1;
     623             : 
     624        1035 :     return 0;
     625             : }

Generated by: LCOV version 1.13