LCOV - code coverage report
Current view: top level - libavcodec - ac3dec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 794 958 82.9 %
Date: 2018-05-20 11:54:08 Functions: 23 23 100.0 %

          Line data    Source code
       1             : /*
       2             :  * AC-3 Audio Decoder
       3             :  * This code was developed as part of Google Summer of Code 2006.
       4             :  * E-AC-3 support was added as part of Google Summer of Code 2007.
       5             :  *
       6             :  * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com)
       7             :  * Copyright (c) 2007-2008 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
       8             :  * Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
       9             :  *
      10             :  * This file is part of FFmpeg.
      11             :  *
      12             :  * FFmpeg is free software; you can redistribute it and/or
      13             :  * modify it under the terms of the GNU Lesser General Public
      14             :  * License as published by the Free Software Foundation; either
      15             :  * version 2.1 of the License, or (at your option) any later version.
      16             :  *
      17             :  * FFmpeg is distributed in the hope that it will be useful,
      18             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      19             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      20             :  * Lesser General Public License for more details.
      21             :  *
      22             :  * You should have received a copy of the GNU Lesser General Public
      23             :  * License along with FFmpeg; if not, write to the Free Software
      24             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      25             :  */
      26             : 
      27             : #include <stdio.h>
      28             : #include <stddef.h>
      29             : #include <math.h>
      30             : #include <string.h>
      31             : 
      32             : #include "libavutil/channel_layout.h"
      33             : #include "libavutil/crc.h"
      34             : #include "libavutil/downmix_info.h"
      35             : #include "libavutil/opt.h"
      36             : #include "bswapdsp.h"
      37             : #include "internal.h"
      38             : #include "aac_ac3_parser.h"
      39             : #include "ac3_parser_internal.h"
      40             : #include "ac3dec.h"
      41             : #include "ac3dec_data.h"
      42             : #include "kbdwin.h"
      43             : 
      44             : /**
      45             :  * table for ungrouping 3 values in 7 bits.
      46             :  * used for exponents and bap=2 mantissas
      47             :  */
      48             : static uint8_t ungroup_3_in_7_bits_tab[128][3];
      49             : 
      50             : /** tables for ungrouping mantissas */
      51             : static int b1_mantissas[32][3];
      52             : static int b2_mantissas[128][3];
      53             : static int b3_mantissas[8];
      54             : static int b4_mantissas[128][2];
      55             : static int b5_mantissas[16];
      56             : 
      57             : /**
      58             :  * Quantization table: levels for symmetric. bits for asymmetric.
      59             :  * reference: Table 7.18 Mapping of bap to Quantizer
      60             :  */
      61             : static const uint8_t quantization_tab[16] = {
      62             :     0, 3, 5, 7, 11, 15,
      63             :     5, 6, 7, 8, 9, 10, 11, 12, 14, 16
      64             : };
      65             : 
      66             : #if (!USE_FIXED)
      67             : /** dynamic range table. converts codes to scale factors. */
      68             : static float dynamic_range_tab[256];
      69             : float ff_ac3_heavy_dynamic_range_tab[256];
      70             : #endif
      71             : 
      72             : /** Adjustments in dB gain */
      73             : static const float gain_levels[9] = {
      74             :     LEVEL_PLUS_3DB,
      75             :     LEVEL_PLUS_1POINT5DB,
      76             :     LEVEL_ONE,
      77             :     LEVEL_MINUS_1POINT5DB,
      78             :     LEVEL_MINUS_3DB,
      79             :     LEVEL_MINUS_4POINT5DB,
      80             :     LEVEL_MINUS_6DB,
      81             :     LEVEL_ZERO,
      82             :     LEVEL_MINUS_9DB
      83             : };
      84             : 
      85             : /** Adjustments in dB gain (LFE, +10 to -21 dB) */
      86             : static const float gain_levels_lfe[32] = {
      87             :     3.162275, 2.818382, 2.511886, 2.238719, 1.995261, 1.778278, 1.584893,
      88             :     1.412536, 1.258924, 1.122018, 1.000000, 0.891251, 0.794328, 0.707946,
      89             :     0.630957, 0.562341, 0.501187, 0.446683, 0.398107, 0.354813, 0.316227,
      90             :     0.281838, 0.251188, 0.223872, 0.199526, 0.177828, 0.158489, 0.141253,
      91             :     0.125892, 0.112201, 0.100000, 0.089125
      92             : };
      93             : 
      94             : /**
      95             :  * Table for default stereo downmixing coefficients
      96             :  * reference: Section 7.8.2 Downmixing Into Two Channels
      97             :  */
      98             : static const uint8_t ac3_default_coeffs[8][5][2] = {
      99             :     { { 2, 7 }, { 7, 2 },                               },
     100             :     { { 4, 4 },                                         },
     101             :     { { 2, 7 }, { 7, 2 },                               },
     102             :     { { 2, 7 }, { 5, 5 }, { 7, 2 },                     },
     103             :     { { 2, 7 }, { 7, 2 }, { 6, 6 },                     },
     104             :     { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 8, 8 },           },
     105             :     { { 2, 7 }, { 7, 2 }, { 6, 7 }, { 7, 6 },           },
     106             :     { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
     107             : };
     108             : 
     109             : static const uint64_t custom_channel_map_locations[16][2] = {
     110             :     { 1, AV_CH_FRONT_LEFT },
     111             :     { 1, AV_CH_FRONT_CENTER },
     112             :     { 1, AV_CH_FRONT_RIGHT },
     113             :     { 1, AV_CH_SIDE_LEFT },
     114             :     { 1, AV_CH_SIDE_RIGHT },
     115             :     { 0, AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER },
     116             :     { 0, AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT },
     117             :     { 0, AV_CH_BACK_CENTER },
     118             :     { 0, AV_CH_TOP_CENTER },
     119             :     { 0, AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT },
     120             :     { 0, AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT },
     121             :     { 0, AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT},
     122             :     { 0, AV_CH_TOP_FRONT_CENTER },
     123             :     { 0, AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT },
     124             :     { 0, AV_CH_LOW_FREQUENCY_2 },
     125             :     { 1, AV_CH_LOW_FREQUENCY },
     126             : };
     127             : 
     128             : /**
     129             :  * Symmetrical Dequantization
     130             :  * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
     131             :  *            Tables 7.19 to 7.23
     132             :  */
     133             : static inline int
     134       38658 : symmetric_dequant(int code, int levels)
     135             : {
     136       38658 :     return ((code - (levels >> 1)) * (1 << 24)) / levels;
     137             : }
     138             : 
     139             : /*
     140             :  * Initialize tables at runtime.
     141             :  */
     142          51 : static av_cold void ac3_tables_init(void)
     143             : {
     144             :     int i;
     145             : 
     146             :     /* generate table for ungrouping 3 values in 7 bits
     147             :        reference: Section 7.1.3 Exponent Decoding */
     148        6579 :     for (i = 0; i < 128; i++) {
     149        6528 :         ungroup_3_in_7_bits_tab[i][0] =  i / 25;
     150        6528 :         ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
     151        6528 :         ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
     152             :     }
     153             : 
     154             :     /* generate grouped mantissa tables
     155             :        reference: Section 7.3.5 Ungrouping of Mantissas */
     156        1683 :     for (i = 0; i < 32; i++) {
     157             :         /* bap=1 mantissas */
     158        1632 :         b1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
     159        1632 :         b1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
     160        1632 :         b1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
     161             :     }
     162        6579 :     for (i = 0; i < 128; i++) {
     163             :         /* bap=2 mantissas */
     164        6528 :         b2_mantissas[i][0] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][0], 5);
     165        6528 :         b2_mantissas[i][1] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][1], 5);
     166        6528 :         b2_mantissas[i][2] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][2], 5);
     167             : 
     168             :         /* bap=4 mantissas */
     169        6528 :         b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
     170        6528 :         b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
     171             :     }
     172             :     /* generate ungrouped mantissa tables
     173             :        reference: Tables 7.21 and 7.23 */
     174         408 :     for (i = 0; i < 7; i++) {
     175             :         /* bap=3 mantissas */
     176         357 :         b3_mantissas[i] = symmetric_dequant(i, 7);
     177             :     }
     178         816 :     for (i = 0; i < 15; i++) {
     179             :         /* bap=5 mantissas */
     180         765 :         b5_mantissas[i] = symmetric_dequant(i, 15);
     181             :     }
     182             : 
     183             : #if (!USE_FIXED)
     184             :     /* generate dynamic range table
     185             :        reference: Section 7.7.1 Dynamic Range Control */
     186       12079 :     for (i = 0; i < 256; i++) {
     187       12032 :         int v = (i >> 5) - ((i >> 7) << 3) - 5;
     188       12032 :         dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
     189             :     }
     190             : 
     191             :     /* generate compr dynamic range table
     192             :        reference: Section 7.7.2 Heavy Compression */
     193       12079 :     for (i = 0; i < 256; i++) {
     194       12032 :         int v = (i >> 4) - ((i >> 7) << 4) - 4;
     195       12032 :         ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
     196             :     }
     197             : #endif
     198          51 : }
     199             : 
     200             : /**
     201             :  * AVCodec initialization
     202             :  */
     203          51 : static av_cold int ac3_decode_init(AVCodecContext *avctx)
     204             : {
     205          51 :     AC3DecodeContext *s = avctx->priv_data;
     206             :     int i;
     207             : 
     208          51 :     s->avctx = avctx;
     209             : 
     210          51 :     ac3_tables_init();
     211          51 :     ff_mdct_init(&s->imdct_256, 8, 1, 1.0);
     212          51 :     ff_mdct_init(&s->imdct_512, 9, 1, 1.0);
     213          51 :     AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256);
     214          51 :     ff_bswapdsp_init(&s->bdsp);
     215             : 
     216             : #if (USE_FIXED)
     217           4 :     s->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     218             : #else
     219          47 :     s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     220          47 :     ff_fmt_convert_init(&s->fmt_conv, avctx);
     221             : #endif
     222             : 
     223          51 :     ff_ac3dsp_init(&s->ac3dsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
     224          51 :     av_lfg_init(&s->dith_state, 0);
     225             : 
     226             :     if (USE_FIXED)
     227           4 :         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
     228             :     else
     229          47 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
     230             : 
     231             :     /* allow downmixing to stereo or mono */
     232          76 :     if (avctx->channels > 1 &&
     233          25 :         avctx->request_channel_layout == AV_CH_LAYOUT_MONO)
     234           4 :         avctx->channels = 1;
     235          57 :     else if (avctx->channels > 2 &&
     236          10 :              avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
     237           3 :         avctx->channels = 2;
     238          51 :     s->downmixed = 1;
     239             : 
     240         408 :     for (i = 0; i < AC3_MAX_CHANNELS; i++) {
     241         357 :         s->xcfptr[i] = s->transform_coeffs[i];
     242         357 :         s->dlyptr[i] = s->delay[i];
     243             :     }
     244             : 
     245          51 :     return 0;
     246             : }
     247             : 
     248             : /**
     249             :  * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream.
     250             :  * GetBitContext within AC3DecodeContext must point to
     251             :  * the start of the synchronized AC-3 bitstream.
     252             :  */
     253        1222 : static int ac3_parse_header(AC3DecodeContext *s)
     254             : {
     255        1222 :     GetBitContext *gbc = &s->gbc;
     256             :     int i;
     257             : 
     258             :     /* read the rest of the bsi. read twice for dual mono mode. */
     259        1222 :     i = !s->channel_mode;
     260             :     do {
     261        1222 :         s->dialog_normalization[(!s->channel_mode)-i] = -get_bits(gbc, 5);
     262        1222 :         if (s->dialog_normalization[(!s->channel_mode)-i] == 0) {
     263           0 :             s->dialog_normalization[(!s->channel_mode)-i] = -31;
     264             :         }
     265        1222 :         if (s->target_level != 0) {
     266           0 :             s->level_gain[(!s->channel_mode)-i] = powf(2.0f,
     267           0 :                 (float)(s->target_level -
     268           0 :                 s->dialog_normalization[(!s->channel_mode)-i])/6.0f);
     269             :         }
     270        1222 :         if (s->compression_exists[(!s->channel_mode)-i] = get_bits1(gbc)) {
     271        1898 :             s->heavy_dynamic_range[(!s->channel_mode)-i] =
     272        1898 :                 AC3_HEAVY_RANGE(get_bits(gbc, 8));
     273             :         }
     274        1222 :         if (get_bits1(gbc))
     275           0 :             skip_bits(gbc, 8); //skip language code
     276        1222 :         if (get_bits1(gbc))
     277         542 :             skip_bits(gbc, 7); //skip audio production information
     278        1222 :     } while (i--);
     279             : 
     280        1222 :     skip_bits(gbc, 2); //skip copyright bit and original bitstream bit
     281             : 
     282             :     /* skip the timecodes or parse the Alternate Bit Stream Syntax */
     283        1222 :     if (s->bitstream_id != 6) {
     284        1063 :         if (get_bits1(gbc))
     285           0 :             skip_bits(gbc, 14); //skip timecode1
     286        1063 :         if (get_bits1(gbc))
     287           0 :             skip_bits(gbc, 14); //skip timecode2
     288             :     } else {
     289         159 :         if (get_bits1(gbc)) {
     290         159 :             s->preferred_downmix       = get_bits(gbc, 2);
     291         159 :             s->center_mix_level_ltrt   = get_bits(gbc, 3);
     292         159 :             s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7);
     293         159 :             s->center_mix_level        = get_bits(gbc, 3);
     294         159 :             s->surround_mix_level      = av_clip(get_bits(gbc, 3), 3, 7);
     295             :         }
     296         159 :         if (get_bits1(gbc)) {
     297         159 :             s->dolby_surround_ex_mode = get_bits(gbc, 2);
     298         159 :             s->dolby_headphone_mode   = get_bits(gbc, 2);
     299         159 :             skip_bits(gbc, 10); // skip adconvtyp (1), xbsi2 (8), encinfo (1)
     300             :         }
     301             :     }
     302             : 
     303             :     /* skip additional bitstream info */
     304        1222 :     if (get_bits1(gbc)) {
     305           0 :         i = get_bits(gbc, 6);
     306             :         do {
     307           0 :             skip_bits(gbc, 8);
     308           0 :         } while (i--);
     309             :     }
     310             : 
     311        1222 :     return 0;
     312             : }
     313             : 
     314             : /**
     315             :  * Common function to parse AC-3 or E-AC-3 frame header
     316             :  */
     317        2423 : static int parse_frame_header(AC3DecodeContext *s)
     318             : {
     319             :     AC3HeaderInfo hdr;
     320             :     int err;
     321             : 
     322        2423 :     err = ff_ac3_parse_header(&s->gbc, &hdr);
     323        2423 :     if (err)
     324           1 :         return err;
     325             : 
     326             :     /* get decoding parameters from header info */
     327        2422 :     s->bit_alloc_params.sr_code     = hdr.sr_code;
     328        2422 :     s->bitstream_id                 = hdr.bitstream_id;
     329        2422 :     s->bitstream_mode               = hdr.bitstream_mode;
     330        2422 :     s->channel_mode                 = hdr.channel_mode;
     331        2422 :     s->lfe_on                       = hdr.lfe_on;
     332        2422 :     s->bit_alloc_params.sr_shift    = hdr.sr_shift;
     333        2422 :     s->sample_rate                  = hdr.sample_rate;
     334        2422 :     s->bit_rate                     = hdr.bit_rate;
     335        2422 :     s->channels                     = hdr.channels;
     336        2422 :     s->fbw_channels                 = s->channels - s->lfe_on;
     337        2422 :     s->lfe_ch                       = s->fbw_channels + 1;
     338        2422 :     s->frame_size                   = hdr.frame_size;
     339        2422 :     s->superframe_size             += hdr.frame_size;
     340        2422 :     s->preferred_downmix            = AC3_DMIXMOD_NOTINDICATED;
     341        2422 :     s->center_mix_level             = hdr.center_mix_level;
     342        2422 :     s->center_mix_level_ltrt        = 4; // -3.0dB
     343        2422 :     s->surround_mix_level           = hdr.surround_mix_level;
     344        2422 :     s->surround_mix_level_ltrt      = 4; // -3.0dB
     345        2422 :     s->lfe_mix_level_exists         = 0;
     346        2422 :     s->num_blocks                   = hdr.num_blocks;
     347        2422 :     s->frame_type                   = hdr.frame_type;
     348        2422 :     s->substreamid                  = hdr.substreamid;
     349        2422 :     s->dolby_surround_mode          = hdr.dolby_surround_mode;
     350        2422 :     s->dolby_surround_ex_mode       = AC3_DSUREXMOD_NOTINDICATED;
     351        2422 :     s->dolby_headphone_mode         = AC3_DHEADPHONMOD_NOTINDICATED;
     352             : 
     353        2422 :     if (s->lfe_on) {
     354         585 :         s->start_freq[s->lfe_ch]     = 0;
     355         585 :         s->end_freq[s->lfe_ch]       = 7;
     356         585 :         s->num_exp_groups[s->lfe_ch] = 2;
     357         585 :         s->channel_in_cpl[s->lfe_ch] = 0;
     358             :     }
     359             : 
     360        2422 :     if (s->bitstream_id <= 10) {
     361        1222 :         s->eac3                  = 0;
     362        1222 :         s->snr_offset_strategy   = 2;
     363        1222 :         s->block_switch_syntax   = 1;
     364        1222 :         s->dither_flag_syntax    = 1;
     365        1222 :         s->bit_allocation_syntax = 1;
     366        1222 :         s->fast_gain_syntax      = 0;
     367        1222 :         s->first_cpl_leak        = 0;
     368        1222 :         s->dba_syntax            = 1;
     369        1222 :         s->skip_syntax           = 1;
     370        1222 :         memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
     371        1222 :         return ac3_parse_header(s);
     372             :     } else if (CONFIG_EAC3_DECODER) {
     373        1200 :         s->eac3 = 1;
     374        1200 :         return ff_eac3_parse_header(s);
     375             :     } else {
     376             :         av_log(s->avctx, AV_LOG_ERROR, "E-AC-3 support not compiled in\n");
     377             :         return AVERROR(ENOSYS);
     378             :     }
     379             : }
     380             : 
     381             : /**
     382             :  * Set stereo downmixing coefficients based on frame header info.
     383             :  * reference: Section 7.8.2 Downmixing Into Two Channels
     384             :  */
     385         410 : static int set_downmix_coeffs(AC3DecodeContext *s)
     386             : {
     387             :     int i;
     388         410 :     float cmix = gain_levels[s->  center_mix_level];
     389         410 :     float smix = gain_levels[s->surround_mix_level];
     390             :     float norm0, norm1;
     391             :     float downmix_coeffs[2][AC3_MAX_CHANNELS];
     392             : 
     393         410 :     if (!s->downmix_coeffs[0]) {
     394           7 :         s->downmix_coeffs[0] = av_malloc_array(2 * AC3_MAX_CHANNELS,
     395             :                                                sizeof(**s->downmix_coeffs));
     396           7 :         if (!s->downmix_coeffs[0])
     397           0 :             return AVERROR(ENOMEM);
     398           7 :         s->downmix_coeffs[1] = s->downmix_coeffs[0] + AC3_MAX_CHANNELS;
     399             :     }
     400             : 
     401        2274 :     for (i = 0; i < s->fbw_channels; i++) {
     402        1864 :         downmix_coeffs[0][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
     403        1864 :         downmix_coeffs[1][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
     404             :     }
     405         410 :     if (s->channel_mode > 1 && s->channel_mode & 1) {
     406         410 :         downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
     407             :     }
     408         410 :     if (s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
     409         186 :         int nf = s->channel_mode - 2;
     410         186 :         downmix_coeffs[0][nf] = downmix_coeffs[1][nf] = smix * LEVEL_MINUS_3DB;
     411             :     }
     412         410 :     if (s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
     413         224 :         int nf = s->channel_mode - 4;
     414         224 :         downmix_coeffs[0][nf] = downmix_coeffs[1][nf+1] = smix;
     415             :     }
     416             : 
     417             :     /* renormalize */
     418         410 :     norm0 = norm1 = 0.0;
     419        2274 :     for (i = 0; i < s->fbw_channels; i++) {
     420        1864 :         norm0 += downmix_coeffs[0][i];
     421        1864 :         norm1 += downmix_coeffs[1][i];
     422             :     }
     423         410 :     norm0 = 1.0f / norm0;
     424         410 :     norm1 = 1.0f / norm1;
     425        2274 :     for (i = 0; i < s->fbw_channels; i++) {
     426        1864 :         downmix_coeffs[0][i] *= norm0;
     427        1864 :         downmix_coeffs[1][i] *= norm1;
     428             :     }
     429             : 
     430         410 :     if (s->output_mode == AC3_CHMODE_MONO) {
     431        1292 :         for (i = 0; i < s->fbw_channels; i++)
     432        3168 :             downmix_coeffs[0][i] = (downmix_coeffs[0][i] +
     433        2112 :                                     downmix_coeffs[1][i]) * LEVEL_MINUS_3DB;
     434             :     }
     435        2274 :     for (i = 0; i < s->fbw_channels; i++) {
     436        1864 :         s->downmix_coeffs[0][i] = FIXR12(downmix_coeffs[0][i]);
     437        1864 :         s->downmix_coeffs[1][i] = FIXR12(downmix_coeffs[1][i]);
     438             :     }
     439             : 
     440         410 :     return 0;
     441             : }
     442             : 
     443             : /**
     444             :  * Decode the grouped exponents according to exponent strategy.
     445             :  * reference: Section 7.1.3 Exponent Decoding
     446             :  */
     447       13377 : static int decode_exponents(AC3DecodeContext *s,
     448             :                             GetBitContext *gbc, int exp_strategy, int ngrps,
     449             :                             uint8_t absexp, int8_t *dexps)
     450             : {
     451             :     int i, j, grp, group_size;
     452             :     int dexp[256];
     453             :     int expacc, prevexp;
     454             : 
     455             :     /* unpack groups */
     456       13377 :     group_size = exp_strategy + (exp_strategy == EXP_D45);
     457      478758 :     for (grp = 0, i = 0; grp < ngrps; grp++) {
     458      465381 :         expacc = get_bits(gbc, 7);
     459      465381 :         if (expacc >= 125) {
     460           0 :             av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc);
     461           0 :             return AVERROR_INVALIDDATA;
     462             :         }
     463      465381 :         dexp[i++] = ungroup_3_in_7_bits_tab[expacc][0];
     464      465381 :         dexp[i++] = ungroup_3_in_7_bits_tab[expacc][1];
     465      465381 :         dexp[i++] = ungroup_3_in_7_bits_tab[expacc][2];
     466             :     }
     467             : 
     468             :     /* convert to absolute exps and expand groups */
     469       13377 :     prevexp = absexp;
     470     1409520 :     for (i = 0, j = 0; i < ngrps * 3; i++) {
     471     1396143 :         prevexp += dexp[i] - 2;
     472     1396143 :         if (prevexp > 24U) {
     473           0 :             av_log(s->avctx, AV_LOG_ERROR, "exponent %d is out-of-range\n", prevexp);
     474           0 :             return -1;
     475             :         }
     476     1396143 :         switch (group_size) {
     477      103407 :         case 4: dexps[j++] = prevexp;
     478      103407 :                 dexps[j++] = prevexp;
     479      269445 :         case 2: dexps[j++] = prevexp;
     480     1396143 :         case 1: dexps[j++] = prevexp;
     481             :         }
     482             :     }
     483       13377 :     return 0;
     484             : }
     485             : 
     486             : /**
     487             :  * Generate transform coefficients for each coupled channel in the coupling
     488             :  * range using the coupling coefficients and coupling coordinates.
     489             :  * reference: Section 7.4.3 Coupling Coordinate Format
     490             :  */
     491        7274 : static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
     492             : {
     493             :     int bin, band, ch;
     494             : 
     495        7274 :     bin = s->start_freq[CPL_CH];
     496       33070 :     for (band = 0; band < s->num_cpl_bands; band++) {
     497       25796 :         int band_start = bin;
     498       25796 :         int band_end = bin + s->cpl_band_sizes[band];
     499       87288 :         for (ch = 1; ch <= s->fbw_channels; ch++) {
     500       61492 :             if (s->channel_in_cpl[ch]) {
     501       61492 :                 int cpl_coord = s->cpl_coords[ch][band] << 5;
     502     1456204 :                 for (bin = band_start; bin < band_end; bin++) {
     503     1394712 :                     s->fixed_coeffs[ch][bin] =
     504     1394712 :                         MULH(s->fixed_coeffs[CPL_CH][bin] * (1 << 4), cpl_coord);
     505             :                 }
     506       61492 :                 if (ch == 2 && s->phase_flags[band]) {
     507           0 :                     for (bin = band_start; bin < band_end; bin++)
     508           0 :                         s->fixed_coeffs[2][bin] = -s->fixed_coeffs[2][bin];
     509             :                 }
     510             :             }
     511             :         }
     512       25796 :         bin = band_end;
     513             :     }
     514        7274 : }
     515             : 
     516             : /**
     517             :  * Grouped mantissas for 3-level 5-level and 11-level quantization
     518             :  */
     519             : typedef struct mant_groups {
     520             :     int b1_mant[2];
     521             :     int b2_mant[2];
     522             :     int b4_mant;
     523             :     int b1;
     524             :     int b2;
     525             :     int b4;
     526             : } mant_groups;
     527             : 
     528             : /**
     529             :  * Decode the transform coefficients for a particular channel
     530             :  * reference: Section 7.3 Quantization and Decoding of Mantissas
     531             :  */
     532       47270 : static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, mant_groups *m)
     533             : {
     534       47270 :     int start_freq = s->start_freq[ch_index];
     535       47270 :     int end_freq   = s->end_freq[ch_index];
     536       47270 :     uint8_t *baps  = s->bap[ch_index];
     537       47270 :     int8_t *exps   = s->dexps[ch_index];
     538       47270 :     int32_t *coeffs = s->fixed_coeffs[ch_index];
     539       47270 :     int dither     = (ch_index == CPL_CH) || s->dither_flag[ch_index];
     540       47270 :     GetBitContext *gbc = &s->gbc;
     541             :     int freq;
     542             : 
     543     6668546 :     for (freq = start_freq; freq < end_freq; freq++) {
     544     6621276 :         int bap = baps[freq];
     545             :         int mantissa;
     546     6621276 :         switch (bap) {
     547     2028799 :         case 0:
     548             :             /* random noise with approximate range of -0.707 to 0.707 */
     549     2028799 :             if (dither)
     550     2024548 :                 mantissa = (((av_lfg_get(&s->dith_state)>>8)*181)>>8) - 5931008;
     551             :             else
     552        4251 :                 mantissa = 0;
     553     2028799 :             break;
     554     1367766 :         case 1:
     555     1367766 :             if (m->b1) {
     556      908028 :                 m->b1--;
     557      908028 :                 mantissa = m->b1_mant[m->b1];
     558             :             } else {
     559      459738 :                 int bits      = get_bits(gbc, 5);
     560      459738 :                 mantissa      = b1_mantissas[bits][0];
     561      459738 :                 m->b1_mant[1] = b1_mantissas[bits][1];
     562      459738 :                 m->b1_mant[0] = b1_mantissas[bits][2];
     563      459738 :                 m->b1         = 2;
     564             :             }
     565     1367766 :             break;
     566      635898 :         case 2:
     567      635898 :             if (m->b2) {
     568      420313 :                 m->b2--;
     569      420313 :                 mantissa = m->b2_mant[m->b2];
     570             :             } else {
     571      215585 :                 int bits      = get_bits(gbc, 7);
     572      215585 :                 mantissa      = b2_mantissas[bits][0];
     573      215585 :                 m->b2_mant[1] = b2_mantissas[bits][1];
     574      215585 :                 m->b2_mant[0] = b2_mantissas[bits][2];
     575      215585 :                 m->b2         = 2;
     576             :             }
     577      635898 :             break;
     578      735620 :         case 3:
     579      735620 :             mantissa = b3_mantissas[get_bits(gbc, 3)];
     580      735620 :             break;
     581      478310 :         case 4:
     582      478310 :             if (m->b4) {
     583      236175 :                 m->b4 = 0;
     584      236175 :                 mantissa = m->b4_mant;
     585             :             } else {
     586      242135 :                 int bits   = get_bits(gbc, 7);
     587      242135 :                 mantissa   = b4_mantissas[bits][0];
     588      242135 :                 m->b4_mant = b4_mantissas[bits][1];
     589      242135 :                 m->b4      = 1;
     590             :             }
     591      478310 :             break;
     592      394678 :         case 5:
     593      394678 :             mantissa = b5_mantissas[get_bits(gbc, 4)];
     594      394678 :             break;
     595      980205 :         default: /* 6 to 15 */
     596             :             /* Shift mantissa and sign-extend it. */
     597      980205 :             if (bap > 15) {
     598           0 :                 av_log(s->avctx, AV_LOG_ERROR, "bap %d is invalid in plain AC-3\n", bap);
     599           0 :                 bap = 15;
     600             :             }
     601      980205 :             mantissa = (unsigned)get_sbits(gbc, quantization_tab[bap]) << (24 - quantization_tab[bap]);
     602      980205 :             break;
     603             :         }
     604     6621276 :         coeffs[freq] = mantissa >> exps[freq];
     605             :     }
     606       47270 : }
     607             : 
     608             : /**
     609             :  * Remove random dithering from coupling range coefficients with zero-bit
     610             :  * mantissas for coupled channels which do not use dithering.
     611             :  * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
     612             :  */
     613       14000 : static void remove_dithering(AC3DecodeContext *s) {
     614             :     int ch, i;
     615             : 
     616       55854 :     for (ch = 1; ch <= s->fbw_channels; ch++) {
     617       41854 :         if (!s->dither_flag[ch] && s->channel_in_cpl[ch]) {
     618           0 :             for (i = s->start_freq[CPL_CH]; i < s->end_freq[CPL_CH]; i++) {
     619           0 :                 if (!s->bap[CPL_CH][i])
     620           0 :                     s->fixed_coeffs[ch][i] = 0;
     621             :             }
     622             :         }
     623             :     }
     624       14000 : }
     625             : 
     626       52118 : static inline void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk,
     627             :                                               int ch, mant_groups *m)
     628             : {
     629       52118 :     if (!s->channel_uses_aht[ch]) {
     630       47270 :         ac3_decode_transform_coeffs_ch(s, ch, m);
     631             :     } else {
     632             :         /* if AHT is used, mantissas for all blocks are encoded in the first
     633             :            block of the frame. */
     634             :         int bin;
     635        4848 :         if (CONFIG_EAC3_DECODER && !blk)
     636         808 :             ff_eac3_decode_transform_coeffs_aht_ch(s, ch);
     637      597684 :         for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
     638      592836 :             s->fixed_coeffs[ch][bin] = s->pre_mantissa[ch][bin][blk] >> s->dexps[ch][bin];
     639             :         }
     640             :     }
     641       52118 : }
     642             : 
     643             : /**
     644             :  * Decode the transform coefficients.
     645             :  */
     646       14000 : static inline void decode_transform_coeffs(AC3DecodeContext *s, int blk)
     647             : {
     648             :     int ch, end;
     649       14000 :     int got_cplchan = 0;
     650             :     mant_groups m;
     651             : 
     652       14000 :     m.b1 = m.b2 = m.b4 = 0;
     653             : 
     654       58844 :     for (ch = 1; ch <= s->channels; ch++) {
     655             :         /* transform coefficients for full-bandwidth channel */
     656       44844 :         decode_transform_coeffs_ch(s, blk, ch, &m);
     657             :         /* transform coefficients for coupling channel come right after the
     658             :            coefficients for the first coupled channel*/
     659       44844 :         if (s->channel_in_cpl[ch])  {
     660       19498 :             if (!got_cplchan) {
     661        7274 :                 decode_transform_coeffs_ch(s, blk, CPL_CH, &m);
     662        7274 :                 calc_transform_coeffs_cpl(s);
     663        7274 :                 got_cplchan = 1;
     664             :             }
     665       19498 :             end = s->end_freq[CPL_CH];
     666             :         } else {
     667       25346 :             end = s->end_freq[ch];
     668             :         }
     669             :         do
     670     3420096 :             s->fixed_coeffs[ch][end] = 0;
     671     3420096 :         while (++end < 256);
     672             :     }
     673             : 
     674             :     /* zero the dithered coefficients for appropriate channels */
     675       14000 :     remove_dithering(s);
     676       14000 : }
     677             : 
     678             : /**
     679             :  * Stereo rematrixing.
     680             :  * reference: Section 7.5.4 Rematrixing : Decoding Technique
     681             :  */
     682        8568 : static void do_rematrixing(AC3DecodeContext *s)
     683             : {
     684             :     int bnd, i;
     685             :     int end, bndend;
     686             : 
     687        8568 :     end = FFMIN(s->end_freq[1], s->end_freq[2]);
     688             : 
     689       42840 :     for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++) {
     690       34272 :         if (s->rematrixing_flags[bnd]) {
     691       23228 :             bndend = FFMIN(end, ff_ac3_rematrix_band_tab[bnd + 1]);
     692      635252 :             for (i = ff_ac3_rematrix_band_tab[bnd]; i < bndend; i++) {
     693      612024 :                 int tmp0 = s->fixed_coeffs[1][i];
     694      612024 :                 s->fixed_coeffs[1][i] += s->fixed_coeffs[2][i];
     695      612024 :                 s->fixed_coeffs[2][i]  = tmp0 - s->fixed_coeffs[2][i];
     696             :             }
     697             :         }
     698             :     }
     699        8568 : }
     700             : 
     701             : /**
     702             :  * Inverse MDCT Transform.
     703             :  * Convert frequency domain coefficients to time-domain audio samples.
     704             :  * reference: Section 7.9.4 Transformation Equations
     705             :  */
     706       14000 : static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
     707             : {
     708             :     int ch;
     709             : 
     710       49936 :     for (ch = 1; ch <= channels; ch++) {
     711       35936 :         if (s->block_switch[ch]) {
     712             :             int i;
     713           6 :             FFTSample *x = s->tmp_output + 128;
     714         774 :             for (i = 0; i < 128; i++)
     715         768 :                 x[i] = s->transform_coeffs[ch][2 * i];
     716           6 :             s->imdct_256.imdct_half(&s->imdct_256, s->tmp_output, x);
     717             : #if USE_FIXED
     718           4 :             s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
     719           2 :                                        s->tmp_output, s->window, 128, 8);
     720             : #else
     721           8 :             s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
     722           4 :                                        s->tmp_output, s->window, 128);
     723             : #endif
     724         774 :             for (i = 0; i < 128; i++)
     725         768 :                 x[i] = s->transform_coeffs[ch][2 * i + 1];
     726           6 :             s->imdct_256.imdct_half(&s->imdct_256, s->delay[ch - 1 + offset], x);
     727             :         } else {
     728       35930 :             s->imdct_512.imdct_half(&s->imdct_512, s->tmp_output, s->transform_coeffs[ch]);
     729             : #if USE_FIXED
     730        5846 :             s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
     731        2923 :                                        s->tmp_output, s->window, 128, 8);
     732             : #else
     733       66014 :             s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
     734       33007 :                                        s->tmp_output, s->window, 128);
     735             : #endif
     736       35930 :             memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(FFTSample));
     737             :         }
     738             :     }
     739       14000 : }
     740             : 
     741             : /**
     742             :  * Upmix delay samples from stereo to original channel layout.
     743             :  */
     744           6 : static void ac3_upmix_delay(AC3DecodeContext *s)
     745             : {
     746           6 :     int channel_data_size = sizeof(s->delay[0]);
     747           6 :     switch (s->channel_mode) {
     748           2 :     case AC3_CHMODE_DUALMONO:
     749             :     case AC3_CHMODE_STEREO:
     750             :         /* upmix mono to stereo */
     751           2 :         memcpy(s->delay[1], s->delay[0], channel_data_size);
     752           2 :         break;
     753           0 :     case AC3_CHMODE_2F2R:
     754           0 :         memset(s->delay[3], 0, channel_data_size);
     755           0 :     case AC3_CHMODE_2F1R:
     756           0 :         memset(s->delay[2], 0, channel_data_size);
     757           0 :         break;
     758           0 :     case AC3_CHMODE_3F2R:
     759           0 :         memset(s->delay[4], 0, channel_data_size);
     760           4 :     case AC3_CHMODE_3F1R:
     761           4 :         memset(s->delay[3], 0, channel_data_size);
     762           4 :     case AC3_CHMODE_3F:
     763           4 :         memcpy(s->delay[2], s->delay[1], channel_data_size);
     764           4 :         memset(s->delay[1], 0, channel_data_size);
     765           4 :         break;
     766             :     }
     767           6 : }
     768             : 
     769             : /**
     770             :  * Decode band structure for coupling, spectral extension, or enhanced coupling.
     771             :  * The band structure defines how many subbands are in each band.  For each
     772             :  * subband in the range, 1 means it is combined with the previous band, and 0
     773             :  * means that it starts a new band.
     774             :  *
     775             :  * @param[in] gbc bit reader context
     776             :  * @param[in] blk block number
     777             :  * @param[in] eac3 flag to indicate E-AC-3
     778             :  * @param[in] ecpl flag to indicate enhanced coupling
     779             :  * @param[in] start_subband subband number for start of range
     780             :  * @param[in] end_subband subband number for end of range
     781             :  * @param[in] default_band_struct default band structure table
     782             :  * @param[out] num_bands number of bands (optionally NULL)
     783             :  * @param[out] band_sizes array containing the number of bins in each band (optionally NULL)
     784             :  * @param[in,out] band_struct current band structure
     785             :  */
     786        1751 : static void decode_band_structure(GetBitContext *gbc, int blk, int eac3,
     787             :                                   int ecpl, int start_subband, int end_subband,
     788             :                                   const uint8_t *default_band_struct,
     789             :                                   int *num_bands, uint8_t *band_sizes,
     790             :                                   uint8_t *band_struct, int band_struct_size)
     791             : {
     792        1751 :     int subbnd, bnd, n_subbands, n_bands=0;
     793             :     uint8_t bnd_sz[22];
     794             : 
     795        1751 :     n_subbands = end_subband - start_subband;
     796             : 
     797        1751 :     if (!blk)
     798        1751 :         memcpy(band_struct, default_band_struct, band_struct_size);
     799             : 
     800        1751 :     av_assert0(band_struct_size >= start_subband + n_subbands);
     801             : 
     802        1751 :     band_struct += start_subband + 1;
     803             : 
     804             :     /* decode band structure from bitstream or use default */
     805        1751 :     if (!eac3 || get_bits1(gbc)) {
     806        6348 :         for (subbnd = 0; subbnd < n_subbands - 1; subbnd++) {
     807        5002 :             band_struct[subbnd] = get_bits1(gbc);
     808             :         }
     809             :     }
     810             : 
     811             :     /* calculate number of bands and band sizes based on band structure.
     812             :        note that the first 4 subbands in enhanced coupling span only 6 bins
     813             :        instead of 12. */
     814        1751 :     if (num_bands || band_sizes ) {
     815        1751 :         n_bands = n_subbands;
     816        1751 :         bnd_sz[0] = ecpl ? 6 : 12;
     817        8897 :         for (bnd = 0, subbnd = 1; subbnd < n_subbands; subbnd++) {
     818        7146 :             int subbnd_size = (ecpl && subbnd < 4) ? 6 : 12;
     819        7146 :             if (band_struct[subbnd - 1]) {
     820        3375 :                 n_bands--;
     821        3375 :                 bnd_sz[bnd] += subbnd_size;
     822             :             } else {
     823        3771 :                 bnd_sz[++bnd] = subbnd_size;
     824             :             }
     825             :         }
     826             :     }
     827             : 
     828             :     /* set optional output params */
     829        1751 :     if (num_bands)
     830        1751 :         *num_bands = n_bands;
     831        1751 :     if (band_sizes)
     832        1751 :         memcpy(band_sizes, bnd_sz, n_bands);
     833        1751 : }
     834             : 
     835         538 : static inline int spx_strategy(AC3DecodeContext *s, int blk)
     836             : {
     837         538 :     GetBitContext *bc = &s->gbc;
     838         538 :     int fbw_channels = s->fbw_channels;
     839             :     int dst_start_freq, dst_end_freq, src_start_freq,
     840             :         start_subband, end_subband, ch;
     841             : 
     842             :     /* determine which channels use spx */
     843         538 :     if (s->channel_mode == AC3_CHMODE_MONO) {
     844           0 :         s->channel_uses_spx[1] = 1;
     845             :     } else {
     846        1758 :         for (ch = 1; ch <= fbw_channels; ch++)
     847        1220 :             s->channel_uses_spx[ch] = get_bits1(bc);
     848             :     }
     849             : 
     850             :     /* get the frequency bins of the spx copy region and the spx start
     851             :        and end subbands */
     852         538 :     dst_start_freq = get_bits(bc, 2);
     853         538 :     start_subband  = get_bits(bc, 3) + 2;
     854         538 :     if (start_subband > 7)
     855         490 :         start_subband += start_subband - 7;
     856         538 :     end_subband    = get_bits(bc, 3) + 5;
     857             : #if USE_FIXED
     858           0 :     s->spx_dst_end_freq = end_freq_inv_tab[end_subband-5];
     859             : #endif
     860         538 :     if (end_subband   > 7)
     861         538 :         end_subband   += end_subband   - 7;
     862         538 :     dst_start_freq = dst_start_freq * 12 + 25;
     863         538 :     src_start_freq = start_subband  * 12 + 25;
     864         538 :     dst_end_freq   = end_subband    * 12 + 25;
     865             : 
     866             :     /* check validity of spx ranges */
     867         538 :     if (start_subband >= end_subband) {
     868           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
     869             :                "range (%d >= %d)\n", start_subband, end_subband);
     870           0 :         return AVERROR_INVALIDDATA;
     871             :     }
     872         538 :     if (dst_start_freq >= src_start_freq) {
     873           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
     874             :                "copy start bin (%d >= %d)\n", dst_start_freq, src_start_freq);
     875           0 :         return AVERROR_INVALIDDATA;
     876             :     }
     877             : 
     878         538 :     s->spx_dst_start_freq = dst_start_freq;
     879         538 :     s->spx_src_start_freq = src_start_freq;
     880             :     if (!USE_FIXED)
     881         538 :         s->spx_dst_end_freq   = dst_end_freq;
     882             : 
     883         538 :     decode_band_structure(bc, blk, s->eac3, 0,
     884             :                           start_subband, end_subband,
     885             :                           ff_eac3_default_spx_band_struct,
     886             :                           &s->num_spx_bands,
     887         538 :                           s->spx_band_sizes,
     888         538 :                           s->spx_band_struct, sizeof(s->spx_band_struct));
     889         538 :     return 0;
     890             : }
     891             : 
     892        3228 : static inline void spx_coordinates(AC3DecodeContext *s)
     893             : {
     894        3228 :     GetBitContext *bc = &s->gbc;
     895        3228 :     int fbw_channels = s->fbw_channels;
     896             :     int ch, bnd;
     897             : 
     898       10548 :     for (ch = 1; ch <= fbw_channels; ch++) {
     899        7320 :         if (s->channel_uses_spx[ch]) {
     900        7320 :             if (s->first_spx_coords[ch] || get_bits1(bc)) {
     901             :                 INTFLOAT spx_blend;
     902             :                 int bin, master_spx_coord;
     903             : 
     904        1959 :                 s->first_spx_coords[ch] = 0;
     905        1959 :                 spx_blend = AC3_SPX_BLEND(get_bits(bc, 5));
     906        1959 :                 master_spx_coord = get_bits(bc, 2) * 3;
     907             : 
     908        1959 :                 bin = s->spx_src_start_freq;
     909        6876 :                 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
     910        4917 :                     int bandsize = s->spx_band_sizes[bnd];
     911             :                     int spx_coord_exp, spx_coord_mant;
     912             :                     INTFLOAT nratio, sblend, nblend;
     913             : #if USE_FIXED
     914             :                     /* calculate blending factors */
     915           0 :                     int64_t accu = ((bin << 23) + (bandsize << 22))
     916           0 :                                  * (int64_t)s->spx_dst_end_freq;
     917           0 :                     nratio = (int)(accu >> 32);
     918           0 :                     nratio -= spx_blend << 18;
     919             : 
     920           0 :                     if (nratio < 0) {
     921           0 :                         nblend = 0;
     922           0 :                         sblend = 0x800000;
     923           0 :                     } else if (nratio > 0x7fffff) {
     924           0 :                         nblend = 14529495; // sqrt(3) in FP.23
     925           0 :                         sblend = 0;
     926             :                     } else {
     927           0 :                         nblend = fixed_sqrt(nratio, 23);
     928           0 :                         accu = (int64_t)nblend * 1859775393;
     929           0 :                         nblend = (int)((accu + (1<<29)) >> 30);
     930           0 :                         sblend = fixed_sqrt(0x800000 - nratio, 23);
     931             :                     }
     932             : #else
     933             :                     float spx_coord;
     934             : 
     935             :                     /* calculate blending factors */
     936        4917 :                     nratio = ((float)((bin + (bandsize >> 1))) / s->spx_dst_end_freq) - spx_blend;
     937        4917 :                     nratio = av_clipf(nratio, 0.0f, 1.0f);
     938        4917 :                     nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3)
     939             :                                                    // to give unity variance
     940        4917 :                     sblend = sqrtf(1.0f - nratio);
     941             : #endif
     942        4917 :                     bin += bandsize;
     943             : 
     944             :                     /* decode spx coordinates */
     945        4917 :                     spx_coord_exp  = get_bits(bc, 4);
     946        4917 :                     spx_coord_mant = get_bits(bc, 2);
     947        4917 :                     if (spx_coord_exp == 15) spx_coord_mant <<= 1;
     948        4199 :                     else                     spx_coord_mant += 4;
     949        4917 :                     spx_coord_mant <<= (25 - spx_coord_exp - master_spx_coord);
     950             : 
     951             :                     /* multiply noise and signal blending factors by spx coordinate */
     952             : #if USE_FIXED
     953           0 :                     accu = (int64_t)nblend * spx_coord_mant;
     954           0 :                     s->spx_noise_blend[ch][bnd]  = (int)((accu + (1<<22)) >> 23);
     955           0 :                     accu = (int64_t)sblend * spx_coord_mant;
     956           0 :                     s->spx_signal_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
     957             : #else
     958        4917 :                     spx_coord = spx_coord_mant * (1.0f / (1 << 23));
     959        4917 :                     s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
     960        4917 :                     s->spx_signal_blend[ch][bnd] = sblend * spx_coord;
     961             : #endif
     962             :                 }
     963             :             }
     964             :         } else {
     965           0 :             s->first_spx_coords[ch] = 1;
     966             :         }
     967             :     }
     968        3228 : }
     969             : 
     970        2417 : static inline int coupling_strategy(AC3DecodeContext *s, int blk,
     971             :                                     uint8_t *bit_alloc_stages)
     972             : {
     973        2417 :     GetBitContext *bc = &s->gbc;
     974        2417 :     int fbw_channels = s->fbw_channels;
     975        2417 :     int channel_mode = s->channel_mode;
     976             :     int ch;
     977             : 
     978        2417 :     memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
     979        2417 :     if (!s->eac3)
     980        1217 :         s->cpl_in_use[blk] = get_bits1(bc);
     981        2417 :     if (s->cpl_in_use[blk]) {
     982             :         /* coupling in use */
     983             :         int cpl_start_subband, cpl_end_subband;
     984             : 
     985        1213 :         if (channel_mode < AC3_CHMODE_STEREO) {
     986           0 :             av_log(s->avctx, AV_LOG_ERROR, "coupling not allowed in mono or dual-mono\n");
     987           0 :             return AVERROR_INVALIDDATA;
     988             :         }
     989             : 
     990             :         /* check for enhanced coupling */
     991        1213 :         if (s->eac3 && get_bits1(bc)) {
     992             :             /* TODO: parse enhanced coupling strategy info */
     993           0 :             avpriv_request_sample(s->avctx, "Enhanced coupling");
     994           0 :             return AVERROR_PATCHWELCOME;
     995             :         }
     996             : 
     997             :         /* determine which channels are coupled */
     998        1213 :         if (s->eac3 && s->channel_mode == AC3_CHMODE_STEREO) {
     999         405 :             s->channel_in_cpl[1] = 1;
    1000         405 :             s->channel_in_cpl[2] = 1;
    1001             :         } else {
    1002        3249 :             for (ch = 1; ch <= fbw_channels; ch++)
    1003        2441 :                 s->channel_in_cpl[ch] = get_bits1(bc);
    1004             :         }
    1005             : 
    1006             :         /* phase flags in use */
    1007        1213 :         if (channel_mode == AC3_CHMODE_STEREO)
    1008         938 :             s->phase_flags_in_use = get_bits1(bc);
    1009             : 
    1010             :         /* coupling frequency range */
    1011        1213 :         cpl_start_subband = get_bits(bc, 4);
    1012        2426 :         cpl_end_subband = s->spx_in_use ? (s->spx_src_start_freq - 37) / 12 :
    1013        1213 :                                           get_bits(bc, 4) + 3;
    1014        1213 :         if (cpl_start_subband >= cpl_end_subband) {
    1015           0 :             av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n",
    1016             :                    cpl_start_subband, cpl_end_subband);
    1017           0 :             return AVERROR_INVALIDDATA;
    1018             :         }
    1019        1213 :         s->start_freq[CPL_CH] = cpl_start_subband * 12 + 37;
    1020        1213 :         s->end_freq[CPL_CH]   = cpl_end_subband   * 12 + 37;
    1021             : 
    1022        1213 :         decode_band_structure(bc, blk, s->eac3, 0, cpl_start_subband,
    1023             :                               cpl_end_subband,
    1024             :                               ff_eac3_default_cpl_band_struct,
    1025        1213 :                               &s->num_cpl_bands, s->cpl_band_sizes,
    1026        1213 :                               s->cpl_band_struct, sizeof(s->cpl_band_struct));
    1027             :     } else {
    1028             :         /* coupling not in use */
    1029        5341 :         for (ch = 1; ch <= fbw_channels; ch++) {
    1030        4137 :             s->channel_in_cpl[ch] = 0;
    1031        4137 :             s->first_cpl_coords[ch] = 1;
    1032             :         }
    1033        1204 :         s->first_cpl_leak = s->eac3;
    1034        1204 :         s->phase_flags_in_use = 0;
    1035             :     }
    1036             : 
    1037        2417 :     return 0;
    1038             : }
    1039             : 
    1040        7274 : static inline int coupling_coordinates(AC3DecodeContext *s, int blk)
    1041             : {
    1042        7274 :     GetBitContext *bc = &s->gbc;
    1043        7274 :     int fbw_channels = s->fbw_channels;
    1044             :     int ch, bnd;
    1045        7274 :     int cpl_coords_exist = 0;
    1046             : 
    1047       26772 :     for (ch = 1; ch <= fbw_channels; ch++) {
    1048       19498 :         if (s->channel_in_cpl[ch]) {
    1049       25254 :             if ((s->eac3 && s->first_cpl_coords[ch]) || get_bits1(bc)) {
    1050             :                 int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
    1051        5756 :                 s->first_cpl_coords[ch] = 0;
    1052        5756 :                 cpl_coords_exist = 1;
    1053        5756 :                 master_cpl_coord = 3 * get_bits(bc, 2);
    1054       21080 :                 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
    1055       15324 :                     cpl_coord_exp  = get_bits(bc, 4);
    1056       15324 :                     cpl_coord_mant = get_bits(bc, 4);
    1057       15324 :                     if (cpl_coord_exp == 15)
    1058        2510 :                         s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
    1059             :                     else
    1060       12814 :                         s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
    1061       15324 :                     s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
    1062             :                 }
    1063       13742 :             } else if (!blk) {
    1064           0 :                 av_log(s->avctx, AV_LOG_ERROR, "new coupling coordinates must "
    1065             :                        "be present in block 0\n");
    1066           0 :                 return AVERROR_INVALIDDATA;
    1067             :             }
    1068             :         } else {
    1069             :             /* channel not in coupling */
    1070           0 :             s->first_cpl_coords[ch] = 1;
    1071             :         }
    1072             :     }
    1073             :     /* phase flags */
    1074        7274 :     if (s->channel_mode == AC3_CHMODE_STEREO && cpl_coords_exist) {
    1075        4830 :         for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
    1076        3864 :             s->phase_flags[bnd] = s->phase_flags_in_use ? get_bits1(bc) : 0;
    1077             :         }
    1078             :     }
    1079             : 
    1080        7274 :     return 0;
    1081             : }
    1082             : 
    1083             : /**
    1084             :  * Decode a single audio block from the AC-3 bitstream.
    1085             :  */
    1086       14000 : static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
    1087             : {
    1088       14000 :     int fbw_channels = s->fbw_channels;
    1089       14000 :     int channel_mode = s->channel_mode;
    1090             :     int i, bnd, seg, ch, ret;
    1091             :     int different_transforms;
    1092             :     int downmix_output;
    1093             :     int cpl_in_use;
    1094       14000 :     GetBitContext *gbc = &s->gbc;
    1095       14000 :     uint8_t bit_alloc_stages[AC3_MAX_CHANNELS] = { 0 };
    1096             : 
    1097             :     /* block switch flags */
    1098       14000 :     different_transforms = 0;
    1099       14000 :     if (s->block_switch_syntax) {
    1100       32658 :         for (ch = 1; ch <= fbw_channels; ch++) {
    1101       25368 :             s->block_switch[ch] = get_bits1(gbc);
    1102       25368 :             if (ch > 1 && s->block_switch[ch] != s->block_switch[1])
    1103           6 :                 different_transforms = 1;
    1104             :         }
    1105             :     }
    1106             : 
    1107             :     /* dithering flags */
    1108       14000 :     if (s->dither_flag_syntax) {
    1109       32658 :         for (ch = 1; ch <= fbw_channels; ch++) {
    1110       25368 :             s->dither_flag[ch] = get_bits1(gbc);
    1111             :         }
    1112             :     }
    1113             : 
    1114             :     /* dynamic range */
    1115       14000 :     i = !s->channel_mode;
    1116             :     do {
    1117       14000 :         if (get_bits1(gbc)) {
    1118             :             /* Allow asymmetric application of DRC when drc_scale > 1.
    1119             :                Amplification of quiet sounds is enhanced */
    1120        2723 :             int range_bits = get_bits(gbc, 8);
    1121        2723 :             INTFLOAT range = AC3_RANGE(range_bits);
    1122        2723 :             if (range_bits <= 127 || s->drc_scale <= 1.0)
    1123        2625 :                 s->dynamic_range[i] = AC3_DYNAMIC_RANGE(range);
    1124             :             else
    1125          98 :                 s->dynamic_range[i] = range;
    1126       11277 :         } else if (blk == 0) {
    1127         703 :             s->dynamic_range[i] = AC3_DYNAMIC_RANGE1;
    1128             :         }
    1129       14000 :     } while (i--);
    1130             : 
    1131             :     /* spectral extension strategy */
    1132       14000 :     if (s->eac3 && (!blk || get_bits1(gbc))) {
    1133        1200 :         s->spx_in_use = get_bits1(gbc);
    1134        1200 :         if (s->spx_in_use) {
    1135         538 :             if ((ret = spx_strategy(s, blk)) < 0)
    1136           0 :                 return ret;
    1137             :         }
    1138             :     }
    1139       14000 :     if (!s->eac3 || !s->spx_in_use) {
    1140       10772 :         s->spx_in_use = 0;
    1141       45306 :         for (ch = 1; ch <= fbw_channels; ch++) {
    1142       34534 :             s->channel_uses_spx[ch] = 0;
    1143       34534 :             s->first_spx_coords[ch] = 1;
    1144             :         }
    1145             :     }
    1146             : 
    1147             :     /* spectral extension coordinates */
    1148       14000 :     if (s->spx_in_use)
    1149        3228 :         spx_coordinates(s);
    1150             : 
    1151             :     /* coupling strategy */
    1152       14000 :     if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
    1153        2417 :         if ((ret = coupling_strategy(s, blk, bit_alloc_stages)) < 0)
    1154           0 :             return ret;
    1155       11583 :     } else if (!s->eac3) {
    1156        6073 :         if (!blk) {
    1157           0 :             av_log(s->avctx, AV_LOG_ERROR, "new coupling strategy must "
    1158             :                    "be present in block 0\n");
    1159           0 :             return AVERROR_INVALIDDATA;
    1160             :         } else {
    1161        6073 :             s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
    1162             :         }
    1163             :     }
    1164       14000 :     cpl_in_use = s->cpl_in_use[blk];
    1165             : 
    1166             :     /* coupling coordinates */
    1167       14000 :     if (cpl_in_use) {
    1168        7274 :         if ((ret = coupling_coordinates(s, blk)) < 0)
    1169           0 :             return ret;
    1170             :     }
    1171             : 
    1172             :     /* stereo rematrixing strategy and band structure */
    1173       14000 :     if (channel_mode == AC3_CHMODE_STEREO) {
    1174       11404 :         if ((s->eac3 && !blk) || get_bits1(gbc)) {
    1175        2836 :             s->num_rematrixing_bands = 4;
    1176        2836 :             if (cpl_in_use && s->start_freq[CPL_CH] <= 61) {
    1177           0 :                 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
    1178        2836 :             } else if (s->spx_in_use && s->spx_src_start_freq <= 61) {
    1179           0 :                 s->num_rematrixing_bands--;
    1180             :             }
    1181       14180 :             for (bnd = 0; bnd < s->num_rematrixing_bands; bnd++)
    1182       11344 :                 s->rematrixing_flags[bnd] = get_bits1(gbc);
    1183        5732 :         } else if (!blk) {
    1184           0 :             av_log(s->avctx, AV_LOG_WARNING, "Warning: "
    1185             :                    "new rematrixing strategy not present in block 0\n");
    1186           0 :             s->num_rematrixing_bands = 0;
    1187             :         }
    1188             :     }
    1189             : 
    1190             :     /* exponent strategies for each channel */
    1191       66118 :     for (ch = !cpl_in_use; ch <= s->channels; ch++) {
    1192       52118 :         if (!s->eac3)
    1193       32816 :             s->exp_strategy[blk][ch] = get_bits(gbc, 2 - (ch == s->lfe_ch));
    1194       52118 :         if (s->exp_strategy[blk][ch] != EXP_REUSE)
    1195       13377 :             bit_alloc_stages[ch] = 3;
    1196             :     }
    1197             : 
    1198             :     /* channel bandwidth */
    1199       55854 :     for (ch = 1; ch <= fbw_channels; ch++) {
    1200       41854 :         s->start_freq[ch] = 0;
    1201       41854 :         if (s->exp_strategy[blk][ch] != EXP_REUSE) {
    1202             :             int group_size;
    1203       10377 :             int prev = s->end_freq[ch];
    1204       10377 :             if (s->channel_in_cpl[ch])
    1205        4078 :                 s->end_freq[ch] = s->start_freq[CPL_CH];
    1206        6299 :             else if (s->channel_uses_spx[ch])
    1207        1959 :                 s->end_freq[ch] = s->spx_src_start_freq;
    1208             :             else {
    1209        4340 :                 int bandwidth_code = get_bits(gbc, 6);
    1210        4340 :                 if (bandwidth_code > 60) {
    1211           0 :                     av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code);
    1212           0 :                     return AVERROR_INVALIDDATA;
    1213             :                 }
    1214        4340 :                 s->end_freq[ch] = bandwidth_code * 3 + 73;
    1215             :             }
    1216       10377 :             group_size = 3 << (s->exp_strategy[blk][ch] - 1);
    1217       10377 :             s->num_exp_groups[ch] = (s->end_freq[ch] + group_size-4) / group_size;
    1218       10377 :             if (blk > 0 && s->end_freq[ch] != prev)
    1219           4 :                 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
    1220             :         }
    1221             :     }
    1222       14000 :     if (cpl_in_use && s->exp_strategy[blk][CPL_CH] != EXP_REUSE) {
    1223        4786 :         s->num_exp_groups[CPL_CH] = (s->end_freq[CPL_CH] - s->start_freq[CPL_CH]) /
    1224        2393 :                                     (3 << (s->exp_strategy[blk][CPL_CH] - 1));
    1225             :     }
    1226             : 
    1227             :     /* decode exponents for each channel */
    1228       66118 :     for (ch = !cpl_in_use; ch <= s->channels; ch++) {
    1229       52118 :         if (s->exp_strategy[blk][ch] != EXP_REUSE) {
    1230       13377 :             s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
    1231       26754 :             if (decode_exponents(s, gbc, s->exp_strategy[blk][ch],
    1232       13377 :                                  s->num_exp_groups[ch], s->dexps[ch][0],
    1233       13377 :                                  &s->dexps[ch][s->start_freq[ch]+!!ch])) {
    1234           0 :                 return AVERROR_INVALIDDATA;
    1235             :             }
    1236       13377 :             if (ch != CPL_CH && ch != s->lfe_ch)
    1237       10377 :                 skip_bits(gbc, 2); /* skip gainrng */
    1238             :         }
    1239             :     }
    1240             : 
    1241             :     /* bit allocation information */
    1242       14000 :     if (s->bit_allocation_syntax) {
    1243        8516 :         if (get_bits1(gbc)) {
    1244        1535 :             s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
    1245        1535 :             s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
    1246        1535 :             s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
    1247        1535 :             s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
    1248        1535 :             s->bit_alloc_params.floor  = ff_ac3_floor_tab[get_bits(gbc, 3)];
    1249        8428 :             for (ch = !cpl_in_use; ch <= s->channels; ch++)
    1250        6893 :                 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
    1251        6981 :         } else if (!blk) {
    1252           0 :             av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must "
    1253             :                    "be present in block 0\n");
    1254           0 :             return AVERROR_INVALIDDATA;
    1255             :         }
    1256             :     }
    1257             : 
    1258             :     /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
    1259       14000 :     if (!s->eac3 || !blk) {
    1260        9705 :         if (s->snr_offset_strategy && get_bits1(gbc)) {
    1261        1215 :             int snr = 0;
    1262             :             int csnr;
    1263        1215 :             csnr = (get_bits(gbc, 6) - 15) << 4;
    1264        6685 :             for (i = ch = !cpl_in_use; ch <= s->channels; ch++) {
    1265             :                 /* snr offset */
    1266        5470 :                 if (ch == i || s->snr_offset_strategy == 2)
    1267        5470 :                     snr = (csnr + get_bits(gbc, 4)) << 2;
    1268             :                 /* run at least last bit allocation stage if snr offset changes */
    1269        5470 :                 if (blk && s->snr_offset[ch] != snr) {
    1270           0 :                     bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 1);
    1271             :                 }
    1272        5470 :                 s->snr_offset[ch] = snr;
    1273             : 
    1274             :                 /* fast gain (normal AC-3 only) */
    1275        5470 :                 if (!s->eac3) {
    1276        5470 :                     int prev = s->fast_gain[ch];
    1277        5470 :                     s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
    1278             :                     /* run last 2 bit allocation stages if fast gain changes */
    1279        5470 :                     if (blk && prev != s->fast_gain[ch])
    1280           0 :                         bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
    1281             :                 }
    1282             :             }
    1283        7275 :         } else if (!s->eac3 && !blk) {
    1284           0 :             av_log(s->avctx, AV_LOG_ERROR, "new snr offsets must be present in block 0\n");
    1285           0 :             return AVERROR_INVALIDDATA;
    1286             :         }
    1287             :     }
    1288             : 
    1289             :     /* fast gain (E-AC-3 only) */
    1290       14954 :     if (s->fast_gain_syntax && get_bits1(gbc)) {
    1291        4770 :         for (ch = !cpl_in_use; ch <= s->channels; ch++) {
    1292        3816 :             int prev = s->fast_gain[ch];
    1293        3816 :             s->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gbc, 3)];
    1294             :             /* run last 2 bit allocation stages if fast gain changes */
    1295        3816 :             if (blk && prev != s->fast_gain[ch])
    1296           0 :                 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
    1297             :         }
    1298       13046 :     } else if (s->eac3 && !blk) {
    1299        4112 :         for (ch = !cpl_in_use; ch <= s->channels; ch++)
    1300        3071 :             s->fast_gain[ch] = ff_ac3_fast_gain_tab[4];
    1301             :     }
    1302             : 
    1303             :     /* E-AC-3 to AC-3 converter SNR offset */
    1304       14000 :     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && get_bits1(gbc)) {
    1305         685 :         skip_bits(gbc, 10); // skip converter snr offset
    1306             :     }
    1307             : 
    1308             :     /* coupling leak information */
    1309       14000 :     if (cpl_in_use) {
    1310        8533 :         if (s->first_cpl_leak || get_bits1(gbc)) {
    1311        1259 :             int fl = get_bits(gbc, 3);
    1312        1259 :             int sl = get_bits(gbc, 3);
    1313             :             /* run last 2 bit allocation stages for coupling channel if
    1314             :                coupling leak changes */
    1315        1260 :             if (blk && (fl != s->bit_alloc_params.cpl_fast_leak ||
    1316           1 :                 sl != s->bit_alloc_params.cpl_slow_leak)) {
    1317          46 :                 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
    1318             :             }
    1319        1259 :             s->bit_alloc_params.cpl_fast_leak = fl;
    1320        1259 :             s->bit_alloc_params.cpl_slow_leak = sl;
    1321        6015 :         } else if (!s->eac3 && !blk) {
    1322           0 :             av_log(s->avctx, AV_LOG_ERROR, "new coupling leak info must "
    1323             :                    "be present in block 0\n");
    1324           0 :             return AVERROR_INVALIDDATA;
    1325             :         }
    1326        7274 :         s->first_cpl_leak = 0;
    1327             :     }
    1328             : 
    1329             :     /* delta bit allocation information */
    1330       14000 :     if (s->dba_syntax && get_bits1(gbc)) {
    1331             :         /* delta bit allocation exists (strategy) */
    1332           0 :         for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
    1333           0 :             s->dba_mode[ch] = get_bits(gbc, 2);
    1334           0 :             if (s->dba_mode[ch] == DBA_RESERVED) {
    1335           0 :                 av_log(s->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
    1336           0 :                 return AVERROR_INVALIDDATA;
    1337             :             }
    1338           0 :             bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
    1339             :         }
    1340             :         /* channel delta offset, len and bit allocation */
    1341           0 :         for (ch = !cpl_in_use; ch <= fbw_channels; ch++) {
    1342           0 :             if (s->dba_mode[ch] == DBA_NEW) {
    1343           0 :                 s->dba_nsegs[ch] = get_bits(gbc, 3) + 1;
    1344           0 :                 for (seg = 0; seg < s->dba_nsegs[ch]; seg++) {
    1345           0 :                     s->dba_offsets[ch][seg] = get_bits(gbc, 5);
    1346           0 :                     s->dba_lengths[ch][seg] = get_bits(gbc, 4);
    1347           0 :                     s->dba_values[ch][seg]  = get_bits(gbc, 3);
    1348             :                 }
    1349             :                 /* run last 2 bit allocation stages if new dba values */
    1350           0 :                 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
    1351             :             }
    1352             :         }
    1353       14000 :     } else if (blk == 0) {
    1354       12794 :         for (ch = 0; ch <= s->channels; ch++) {
    1355       10379 :             s->dba_mode[ch] = DBA_NONE;
    1356             :         }
    1357             :     }
    1358             : 
    1359             :     /* Bit allocation */
    1360       66118 :     for (ch = !cpl_in_use; ch <= s->channels; ch++) {
    1361       52118 :         if (bit_alloc_stages[ch] > 2) {
    1362             :             /* Exponent mapping into PSD and PSD integration */
    1363       13377 :             ff_ac3_bit_alloc_calc_psd(s->dexps[ch],
    1364             :                                       s->start_freq[ch], s->end_freq[ch],
    1365       13377 :                                       s->psd[ch], s->band_psd[ch]);
    1366             :         }
    1367       52118 :         if (bit_alloc_stages[ch] > 1) {
    1368             :             /* Compute excitation function, Compute masking curve, and
    1369             :                Apply delta bit allocation */
    1370       27038 :             if (ff_ac3_bit_alloc_calc_mask(&s->bit_alloc_params, s->band_psd[ch],
    1371             :                                            s->start_freq[ch],  s->end_freq[ch],
    1372       13519 :                                            s->fast_gain[ch],   (ch == s->lfe_ch),
    1373             :                                            s->dba_mode[ch],    s->dba_nsegs[ch],
    1374       13519 :                                            s->dba_offsets[ch], s->dba_lengths[ch],
    1375       13519 :                                            s->dba_values[ch],  s->mask[ch])) {
    1376           0 :                 av_log(s->avctx, AV_LOG_ERROR, "error in bit allocation\n");
    1377           0 :                 return AVERROR_INVALIDDATA;
    1378             :             }
    1379             :         }
    1380       52118 :         if (bit_alloc_stages[ch] > 0) {
    1381             :             /* Compute bit allocation */
    1382       27038 :             const uint8_t *bap_tab = s->channel_uses_aht[ch] ?
    1383       13519 :                                      ff_eac3_hebap_tab : ff_ac3_bap_tab;
    1384       27038 :             s->ac3dsp.bit_alloc_calc_bap(s->mask[ch], s->psd[ch],
    1385             :                                       s->start_freq[ch], s->end_freq[ch],
    1386             :                                       s->snr_offset[ch],
    1387             :                                       s->bit_alloc_params.floor,
    1388       13519 :                                       bap_tab, s->bap[ch]);
    1389             :         }
    1390             :     }
    1391             : 
    1392             :     /* unused dummy data */
    1393       14000 :     if (s->skip_syntax && get_bits1(gbc)) {
    1394        1242 :         int skipl = get_bits(gbc, 9);
    1395        1242 :         skip_bits_long(gbc, 8 * skipl);
    1396             :     }
    1397             : 
    1398             :     /* unpack the transform coefficients
    1399             :        this also uncouples channels if coupling is in use. */
    1400       14000 :     decode_transform_coeffs(s, blk);
    1401             : 
    1402             :     /* TODO: generate enhanced coupling coordinates and uncouple */
    1403             : 
    1404             :     /* recover coefficients if rematrixing is in use */
    1405       14000 :     if (s->channel_mode == AC3_CHMODE_STEREO)
    1406        8568 :         do_rematrixing(s);
    1407             : 
    1408             :     /* apply scaling to coefficients (headroom, dynrng) */
    1409       58844 :     for (ch = 1; ch <= s->channels; ch++) {
    1410       44844 :         int audio_channel = 0;
    1411             :         INTFLOAT gain;
    1412       44844 :         if (s->channel_mode == AC3_CHMODE_DUALMONO && ch <= 2)
    1413           0 :             audio_channel = 2-ch;
    1414       44844 :         if (s->heavy_compression && s->compression_exists[audio_channel])
    1415           0 :             gain = s->heavy_dynamic_range[audio_channel];
    1416             :         else
    1417       44844 :             gain = s->dynamic_range[audio_channel];
    1418             : 
    1419             : #if USE_FIXED
    1420        7008 :         scale_coefs(s->transform_coeffs[ch], s->fixed_coeffs[ch], gain, 256);
    1421             : #else
    1422       37836 :         if (s->target_level != 0)
    1423           0 :           gain = gain * s->level_gain[audio_channel];
    1424       37836 :         gain *= 1.0 / 4194304.0f;
    1425       75672 :         s->fmt_conv.int32_to_float_fmul_scalar(s->transform_coeffs[ch],
    1426       37836 :                                                s->fixed_coeffs[ch], gain, 256);
    1427             : #endif
    1428             :     }
    1429             : 
    1430             :     /* apply spectral extension to high frequency bins */
    1431       14000 :     if (CONFIG_EAC3_DECODER && s->spx_in_use) {
    1432        3228 :         ff_eac3_apply_spectral_extension(s);
    1433             :     }
    1434             : 
    1435             :     /* downmix and MDCT. order depends on whether block switching is used for
    1436             :        any channel in this block. this is because coefficients for the long
    1437             :        and short transforms cannot be mixed. */
    1438       18872 :     downmix_output = s->channels != s->out_channels &&
    1439        2436 :                      !((s->output_mode & AC3_OUTPUT_LFEON) &&
    1440           0 :                      s->fbw_channels == s->out_channels);
    1441       14000 :     if (different_transforms) {
    1442             :         /* the delay samples have already been downmixed, so we upmix the delay
    1443             :            samples in order to reconstruct all channels before downmixing. */
    1444           6 :         if (s->downmixed) {
    1445           6 :             s->downmixed = 0;
    1446           6 :             ac3_upmix_delay(s);
    1447             :         }
    1448             : 
    1449           6 :         do_imdct(s, s->channels, offset);
    1450             : 
    1451           6 :         if (downmix_output) {
    1452             : #if USE_FIXED
    1453           1 :             ac3_downmix_c_fixed16(s->outptr, s->downmix_coeffs,
    1454             :                               s->out_channels, s->fbw_channels, 256);
    1455             : #else
    1456           2 :             ff_ac3dsp_downmix(&s->ac3dsp, s->outptr, s->downmix_coeffs,
    1457             :                               s->out_channels, s->fbw_channels, 256);
    1458             : #endif
    1459             :         }
    1460             :     } else {
    1461       13994 :         if (downmix_output) {
    1462        2433 :             AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->xcfptr + 1, s->downmix_coeffs,
    1463             :                                           s->out_channels, s->fbw_channels, 256);
    1464             :         }
    1465             : 
    1466       13994 :         if (downmix_output && !s->downmixed) {
    1467           3 :             s->downmixed = 1;
    1468           3 :             AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->dlyptr, s->downmix_coeffs,
    1469             :                                           s->out_channels, s->fbw_channels, 128);
    1470             :         }
    1471             : 
    1472       13994 :         do_imdct(s, s->out_channels, offset);
    1473             :     }
    1474             : 
    1475       14000 :     return 0;
    1476             : }
    1477             : 
    1478             : /**
    1479             :  * Decode a single AC-3 frame.
    1480             :  */
    1481        2264 : static int ac3_decode_frame(AVCodecContext * avctx, void *data,
    1482             :                             int *got_frame_ptr, AVPacket *avpkt)
    1483             : {
    1484        2264 :     AVFrame *frame     = data;
    1485        2264 :     const uint8_t *buf = avpkt->data;
    1486        2264 :     int buf_size, full_buf_size = avpkt->size;
    1487        2264 :     AC3DecodeContext *s = avctx->priv_data;
    1488             :     int blk, ch, err, offset, ret;
    1489        2264 :     int got_independent_frame = 0;
    1490             :     const uint8_t *channel_map;
    1491             :     uint8_t extended_channel_map[EAC3_MAX_CHANNELS];
    1492             :     const SHORTFLOAT *output[AC3_MAX_CHANNELS];
    1493             :     enum AVMatrixEncoding matrix_encoding;
    1494             :     AVDownmixInfo *downmix_info;
    1495             : 
    1496        2264 :     s->superframe_size = 0;
    1497             : 
    1498        2264 :     buf_size = full_buf_size;
    1499             :     /* copy input buffer to decoder context to avoid reading past the end
    1500             :        of the buffer, which can be caused by a damaged input stream. */
    1501        2264 :     if (buf_size >= 2 && AV_RB16(buf) == 0x770B) {
    1502             :         // seems to be byte-swapped AC-3
    1503           0 :         int cnt = FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE) >> 1;
    1504           0 :         s->bdsp.bswap16_buf((uint16_t *) s->input_buffer,
    1505             :                             (const uint16_t *) buf, cnt);
    1506             :     } else
    1507        2264 :         memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
    1508             : 
    1509             :     /* if consistent noise generation is enabled, seed the linear feedback generator
    1510             :      * with the contents of the AC-3 frame so that the noise is identical across
    1511             :      * decodes given the same AC-3 frame data, for use with non-linear edititing software. */
    1512        2264 :     if (s->consistent_noise_generation)
    1513           0 :         av_lfg_init_from_data(&s->dith_state, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
    1514             : 
    1515        2264 :     buf = s->input_buffer;
    1516        2423 : dependent_frame:
    1517             :     /* initialize the GetBitContext with the start of valid AC-3 Frame */
    1518        2423 :     if ((ret = init_get_bits8(&s->gbc, buf, buf_size)) < 0)
    1519           0 :         return ret;
    1520             : 
    1521             :     /* parse the syncinfo */
    1522        2423 :     err = parse_frame_header(s);
    1523             : 
    1524        2423 :     if (err) {
    1525           1 :         switch (err) {
    1526           1 :         case AAC_AC3_PARSE_ERROR_SYNC:
    1527           1 :             av_log(avctx, AV_LOG_ERROR, "frame sync error\n");
    1528           1 :             return AVERROR_INVALIDDATA;
    1529           0 :         case AAC_AC3_PARSE_ERROR_BSID:
    1530           0 :             av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");
    1531           0 :             break;
    1532           0 :         case AAC_AC3_PARSE_ERROR_SAMPLE_RATE:
    1533           0 :             av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
    1534           0 :             break;
    1535           0 :         case AAC_AC3_PARSE_ERROR_FRAME_SIZE:
    1536           0 :             av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");
    1537           0 :             break;
    1538           0 :         case AAC_AC3_PARSE_ERROR_FRAME_TYPE:
    1539             :             /* skip frame if CRC is ok. otherwise use error concealment. */
    1540             :             /* TODO: add support for substreams */
    1541           0 :             if (s->substreamid) {
    1542           0 :                 av_log(avctx, AV_LOG_DEBUG,
    1543             :                        "unsupported substream %d: skipping frame\n",
    1544             :                        s->substreamid);
    1545           0 :                 *got_frame_ptr = 0;
    1546           0 :                 return buf_size;
    1547             :             } else {
    1548           0 :                 av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");
    1549             :             }
    1550           0 :             break;
    1551           0 :         case AAC_AC3_PARSE_ERROR_CRC:
    1552             :         case AAC_AC3_PARSE_ERROR_CHANNEL_CFG:
    1553           0 :             break;
    1554           0 :         default: // Normal AVERROR do not try to recover.
    1555           0 :             *got_frame_ptr = 0;
    1556           0 :             return err;
    1557             :         }
    1558             :     } else {
    1559             :         /* check that reported frame size fits in input buffer */
    1560        2422 :         if (s->frame_size > buf_size) {
    1561           7 :             av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
    1562           7 :             err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;
    1563        2415 :         } else if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {
    1564             :             /* check for crc mismatch */
    1565           0 :             if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2],
    1566           0 :                        s->frame_size - 2)) {
    1567           0 :                 av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");
    1568           0 :                 if (avctx->err_recognition & AV_EF_EXPLODE)
    1569           0 :                     return AVERROR_INVALIDDATA;
    1570           0 :                 err = AAC_AC3_PARSE_ERROR_CRC;
    1571             :             }
    1572             :         }
    1573             :     }
    1574             : 
    1575        2422 :     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT && !got_independent_frame) {
    1576           0 :         av_log(avctx, AV_LOG_WARNING, "Ignoring dependent frame without independent frame.\n");
    1577           0 :         *got_frame_ptr = 0;
    1578           0 :         return FFMIN(full_buf_size, s->frame_size);
    1579             :     }
    1580             : 
    1581             :     /* channel config */
    1582        2422 :     if (!err || (s->channels && s->out_channels != s->channels)) {
    1583        2419 :         s->out_channels = s->channels;
    1584        2419 :         s->output_mode  = s->channel_mode;
    1585        2419 :         if (s->lfe_on)
    1586         584 :             s->output_mode |= AC3_OUTPUT_LFEON;
    1587        4838 :         if (s->channels > 1 &&
    1588        2419 :             avctx->request_channel_layout == AV_CH_LAYOUT_MONO) {
    1589         236 :             s->out_channels = 1;
    1590         236 :             s->output_mode  = AC3_CHMODE_MONO;
    1591        2938 :         } else if (s->channels > 2 &&
    1592         755 :                    avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
    1593         174 :             s->out_channels = 2;
    1594         174 :             s->output_mode  = AC3_CHMODE_STEREO;
    1595             :         }
    1596             : 
    1597        2419 :         s->loro_center_mix_level   = gain_levels[s->  center_mix_level];
    1598        2419 :         s->loro_surround_mix_level = gain_levels[s->surround_mix_level];
    1599        2419 :         s->ltrt_center_mix_level   = LEVEL_MINUS_3DB;
    1600        2419 :         s->ltrt_surround_mix_level = LEVEL_MINUS_3DB;
    1601             :         /* set downmixing coefficients if needed */
    1602        4838 :         if (s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
    1603           0 :                 s->fbw_channels == s->out_channels)) {
    1604         410 :             if ((ret = set_downmix_coeffs(s)) < 0) {
    1605           0 :                 av_log(avctx, AV_LOG_ERROR, "error setting downmix coeffs\n");
    1606           0 :                 return ret;
    1607             :             }
    1608             :         }
    1609           3 :     } else if (!s->channels) {
    1610           0 :         av_log(avctx, AV_LOG_ERROR, "unable to determine channel mode\n");
    1611           0 :         return AVERROR_INVALIDDATA;
    1612             :     }
    1613        2422 :     avctx->channels = s->out_channels;
    1614        2422 :     avctx->channel_layout = avpriv_ac3_channel_layout_tab[s->output_mode & ~AC3_OUTPUT_LFEON];
    1615        2422 :     if (s->output_mode & AC3_OUTPUT_LFEON)
    1616         361 :         avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
    1617             : 
    1618             :     /* set audio service type based on bitstream mode for AC-3 */
    1619        2422 :     avctx->audio_service_type = s->bitstream_mode;
    1620        2422 :     if (s->bitstream_mode == 0x7 && s->channels > 1)
    1621           0 :         avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
    1622             : 
    1623             :     /* decode the audio blocks */
    1624        2422 :     channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
    1625        2422 :     offset = s->frame_type == EAC3_FRAME_TYPE_DEPENDENT ? AC3_MAX_CHANNELS : 0;
    1626       19376 :     for (ch = 0; ch < AC3_MAX_CHANNELS; ch++) {
    1627       16954 :         output[ch] = s->output[ch + offset];
    1628       16954 :         s->outptr[ch] = s->output[ch + offset];
    1629             :     }
    1630       10420 :     for (ch = 0; ch < s->channels; ch++) {
    1631        7998 :         if (ch < s->out_channels)
    1632        6494 :             s->outptr[channel_map[ch]] = s->output_buffer[ch + offset];
    1633             :     }
    1634       16464 :     for (blk = 0; blk < s->num_blocks; blk++) {
    1635       14042 :         if (!err && decode_audio_block(s, blk, offset)) {
    1636           0 :             av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");
    1637           0 :             err = 1;
    1638             :         }
    1639       14042 :         if (err)
    1640         138 :             for (ch = 0; ch < s->out_channels; ch++)
    1641          96 :                 memcpy(s->output_buffer[ch + offset] + AC3_BLOCK_SIZE*blk, output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
    1642       50066 :         for (ch = 0; ch < s->out_channels; ch++)
    1643       36024 :             output[ch] = s->outptr[channel_map[ch]];
    1644       50066 :         for (ch = 0; ch < s->out_channels; ch++) {
    1645       36024 :             if (!ch || channel_map[ch])
    1646       36024 :                 s->outptr[channel_map[ch]] += AC3_BLOCK_SIZE;
    1647             :         }
    1648             :     }
    1649             : 
    1650             :     /* keep last block for error concealment in next frame */
    1651        8916 :     for (ch = 0; ch < s->out_channels; ch++)
    1652        6494 :         memcpy(s->output[ch + offset], output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
    1653             : 
    1654             :     /* check if there is dependent frame */
    1655        2422 :     if (buf_size > s->frame_size) {
    1656             :         AC3HeaderInfo hdr;
    1657             :         int err;
    1658             : 
    1659         159 :         if ((ret = init_get_bits8(&s->gbc, buf + s->frame_size, buf_size - s->frame_size)) < 0)
    1660           0 :             return ret;
    1661             : 
    1662         159 :         err = ff_ac3_parse_header(&s->gbc, &hdr);
    1663         159 :         if (err)
    1664           0 :             return err;
    1665             : 
    1666         159 :         if (hdr.frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
    1667         159 :             if (hdr.num_blocks != s->num_blocks || s->sample_rate != hdr.sample_rate) {
    1668           0 :                 av_log(avctx, AV_LOG_WARNING, "Ignoring non-compatible dependent frame.\n");
    1669             :             } else {
    1670         159 :                 buf += s->frame_size;
    1671         159 :                 buf_size -= s->frame_size;
    1672         159 :                 s->prev_output_mode = s->output_mode;
    1673         159 :                 s->prev_bit_rate = s->bit_rate;
    1674         159 :                 got_independent_frame = 1;
    1675         159 :                 goto dependent_frame;
    1676             :             }
    1677             :         }
    1678             :     }
    1679             : 
    1680        2263 :     frame->decode_error_flags = err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0;
    1681             : 
    1682             :     /* if frame is ok, set audio parameters */
    1683        2263 :     if (!err) {
    1684        2256 :         avctx->sample_rate = s->sample_rate;
    1685        2256 :         avctx->bit_rate    = s->bit_rate + s->prev_bit_rate;
    1686             :     }
    1687             : 
    1688       38471 :     for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++)
    1689       36208 :         extended_channel_map[ch] = ch;
    1690             : 
    1691        2263 :     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
    1692         159 :         uint64_t ich_layout = avpriv_ac3_channel_layout_tab[s->prev_output_mode & ~AC3_OUTPUT_LFEON];
    1693             :         uint64_t channel_layout;
    1694         159 :         int extend = 0;
    1695             : 
    1696         159 :         if (s->prev_output_mode & AC3_OUTPUT_LFEON)
    1697         159 :             ich_layout |= AV_CH_LOW_FREQUENCY;
    1698             : 
    1699         159 :         channel_layout = ich_layout;
    1700        2703 :         for (ch = 0; ch < 16; ch++) {
    1701        2544 :             if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
    1702         477 :                 channel_layout |= custom_channel_map_locations[ch][1];
    1703             :             }
    1704             :         }
    1705             : 
    1706         159 :         avctx->channel_layout = channel_layout;
    1707         159 :         avctx->channels = av_get_channel_layout_nb_channels(channel_layout);
    1708             : 
    1709        2703 :         for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++) {
    1710        2544 :             if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
    1711         477 :                 if (custom_channel_map_locations[ch][0]) {
    1712         318 :                     int index = av_get_channel_layout_channel_index(channel_layout,
    1713             :                                                                     custom_channel_map_locations[ch][1]);
    1714         318 :                     if (index < 0)
    1715           0 :                         return AVERROR_INVALIDDATA;
    1716         318 :                     extended_channel_map[index] = offset + channel_map[extend++];
    1717             :                 } else {
    1718             :                     int i;
    1719             : 
    1720       10335 :                     for (i = 0; i < 64; i++) {
    1721       10176 :                         if ((1LL << i) & custom_channel_map_locations[ch][1]) {
    1722         318 :                             int index = av_get_channel_layout_channel_index(channel_layout,
    1723         318 :                                                                             1LL << i);
    1724         318 :                             if (index < 0)
    1725           0 :                                 return AVERROR_INVALIDDATA;
    1726         318 :                             extended_channel_map[index] = offset + channel_map[extend++];
    1727             :                         }
    1728             :                     }
    1729             :                 }
    1730             :             }
    1731             :         }
    1732             :     }
    1733             : 
    1734             :     /* get output buffer */
    1735        2263 :     frame->nb_samples = s->num_blocks * AC3_BLOCK_SIZE;
    1736        2263 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
    1737           0 :         return ret;
    1738             : 
    1739        8439 :     for (ch = 0; ch < avctx->channels; ch++) {
    1740        6176 :         int map = extended_channel_map[ch];
    1741        6176 :         memcpy((SHORTFLOAT *)frame->data[ch], s->output_buffer[map],
    1742        6176 :                s->num_blocks * AC3_BLOCK_SIZE * sizeof(SHORTFLOAT));
    1743             :     }
    1744             : 
    1745             :     /*
    1746             :      * AVMatrixEncoding
    1747             :      *
    1748             :      * Check whether the input layout is compatible, and make sure we're not
    1749             :      * downmixing (else the matrix encoding is no longer applicable).
    1750             :      */
    1751        2263 :     matrix_encoding = AV_MATRIX_ENCODING_NONE;
    1752        3693 :     if (s->channel_mode == AC3_CHMODE_STEREO &&
    1753        1430 :         s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
    1754        2860 :         if (s->dolby_surround_mode == AC3_DSURMOD_ON)
    1755         490 :             matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
    1756         940 :         else if (s->dolby_headphone_mode == AC3_DHEADPHONMOD_ON)
    1757           0 :             matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
    1758        1259 :     } else if (s->channel_mode >= AC3_CHMODE_2F2R &&
    1759         426 :                s->channel_mode == (s->output_mode & ~AC3_OUTPUT_LFEON)) {
    1760         202 :         switch (s->dolby_surround_ex_mode) {
    1761           0 :         case AC3_DSUREXMOD_ON: // EX or PLIIx
    1762           0 :             matrix_encoding = AV_MATRIX_ENCODING_DOLBYEX;
    1763           0 :             break;
    1764           0 :         case AC3_DSUREXMOD_PLIIZ:
    1765           0 :             matrix_encoding = AV_MATRIX_ENCODING_DPLIIZ;
    1766           0 :             break;
    1767         202 :         default: // not indicated or off
    1768         202 :             break;
    1769             :         }
    1770         631 :     }
    1771        2263 :     if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
    1772           0 :         return ret;
    1773             : 
    1774             :     /* AVDownmixInfo */
    1775        2263 :     if ((downmix_info = av_downmix_info_update_side_data(frame))) {
    1776        2263 :         switch (s->preferred_downmix) {
    1777          48 :         case AC3_DMIXMOD_LTRT:
    1778          48 :             downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_LTRT;
    1779          48 :             break;
    1780           0 :         case AC3_DMIXMOD_LORO:
    1781           0 :             downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_LORO;
    1782           0 :             break;
    1783          98 :         case AC3_DMIXMOD_DPLII:
    1784          98 :             downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_DPLII;
    1785          98 :             break;
    1786        2117 :         default:
    1787        2117 :             downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_UNKNOWN;
    1788        2117 :             break;
    1789             :         }
    1790        2263 :         downmix_info->center_mix_level        = gain_levels[s->       center_mix_level];
    1791        2263 :         downmix_info->center_mix_level_ltrt   = gain_levels[s->  center_mix_level_ltrt];
    1792        2263 :         downmix_info->surround_mix_level      = gain_levels[s->     surround_mix_level];
    1793        2263 :         downmix_info->surround_mix_level_ltrt = gain_levels[s->surround_mix_level_ltrt];
    1794        2263 :         if (s->lfe_mix_level_exists)
    1795         146 :             downmix_info->lfe_mix_level       = gain_levels_lfe[s->lfe_mix_level];
    1796             :         else
    1797        2117 :             downmix_info->lfe_mix_level       = 0.0; // -inf dB
    1798             :     } else
    1799           0 :         return AVERROR(ENOMEM);
    1800             : 
    1801        2263 :     *got_frame_ptr = 1;
    1802             : 
    1803        2263 :     return FFMIN(full_buf_size, s->superframe_size);
    1804             : }
    1805             : 
    1806             : /**
    1807             :  * Uninitialize the AC-3 decoder.
    1808             :  */
    1809          51 : static av_cold int ac3_decode_end(AVCodecContext *avctx)
    1810             : {
    1811          51 :     AC3DecodeContext *s = avctx->priv_data;
    1812          51 :     ff_mdct_end(&s->imdct_512);
    1813          51 :     ff_mdct_end(&s->imdct_256);
    1814          51 :     av_freep(&s->fdsp);
    1815          51 :     av_freep(&s->downmix_coeffs[0]);
    1816             : 
    1817          51 :     return 0;
    1818             : }
    1819             : 
    1820             : #define OFFSET(x) offsetof(AC3DecodeContext, x)
    1821             : #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)

Generated by: LCOV version 1.13