LCOV - code coverage report
Current view: top level - libavcodec - aacdec_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1457 1811 80.5 %
Date: 2018-05-20 11:54:08 Functions: 59 62 95.2 %

          Line data    Source code
       1             : /*
       2             :  * AAC decoder
       3             :  * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
       4             :  * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
       5             :  * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
       6             :  *
       7             :  * AAC LATM decoder
       8             :  * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
       9             :  * Copyright (c) 2010      Janne Grunau <janne-libav@jannau.net>
      10             :  *
      11             :  * AAC decoder fixed-point implementation
      12             :  * Copyright (c) 2013
      13             :  *      MIPS Technologies, Inc., California.
      14             :  *
      15             :  * This file is part of FFmpeg.
      16             :  *
      17             :  * FFmpeg is free software; you can redistribute it and/or
      18             :  * modify it under the terms of the GNU Lesser General Public
      19             :  * License as published by the Free Software Foundation; either
      20             :  * version 2.1 of the License, or (at your option) any later version.
      21             :  *
      22             :  * FFmpeg is distributed in the hope that it will be useful,
      23             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      24             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      25             :  * Lesser General Public License for more details.
      26             :  *
      27             :  * You should have received a copy of the GNU Lesser General Public
      28             :  * License along with FFmpeg; if not, write to the Free Software
      29             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      30             :  */
      31             : 
      32             : /**
      33             :  * @file
      34             :  * AAC decoder
      35             :  * @author Oded Shimon  ( ods15 ods15 dyndns org )
      36             :  * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
      37             :  *
      38             :  * AAC decoder fixed-point implementation
      39             :  * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
      40             :  * @author Nedeljko Babic ( nedeljko.babic imgtec com )
      41             :  */
      42             : 
      43             : /*
      44             :  * supported tools
      45             :  *
      46             :  * Support?                     Name
      47             :  * N (code in SoC repo)         gain control
      48             :  * Y                            block switching
      49             :  * Y                            window shapes - standard
      50             :  * N                            window shapes - Low Delay
      51             :  * Y                            filterbank - standard
      52             :  * N (code in SoC repo)         filterbank - Scalable Sample Rate
      53             :  * Y                            Temporal Noise Shaping
      54             :  * Y                            Long Term Prediction
      55             :  * Y                            intensity stereo
      56             :  * Y                            channel coupling
      57             :  * Y                            frequency domain prediction
      58             :  * Y                            Perceptual Noise Substitution
      59             :  * Y                            Mid/Side stereo
      60             :  * N                            Scalable Inverse AAC Quantization
      61             :  * N                            Frequency Selective Switch
      62             :  * N                            upsampling filter
      63             :  * Y                            quantization & coding - AAC
      64             :  * N                            quantization & coding - TwinVQ
      65             :  * N                            quantization & coding - BSAC
      66             :  * N                            AAC Error Resilience tools
      67             :  * N                            Error Resilience payload syntax
      68             :  * N                            Error Protection tool
      69             :  * N                            CELP
      70             :  * N                            Silence Compression
      71             :  * N                            HVXC
      72             :  * N                            HVXC 4kbits/s VR
      73             :  * N                            Structured Audio tools
      74             :  * N                            Structured Audio Sample Bank Format
      75             :  * N                            MIDI
      76             :  * N                            Harmonic and Individual Lines plus Noise
      77             :  * N                            Text-To-Speech Interface
      78             :  * Y                            Spectral Band Replication
      79             :  * Y (not in this code)         Layer-1
      80             :  * Y (not in this code)         Layer-2
      81             :  * Y (not in this code)         Layer-3
      82             :  * N                            SinuSoidal Coding (Transient, Sinusoid, Noise)
      83             :  * Y                            Parametric Stereo
      84             :  * N                            Direct Stream Transfer
      85             :  * Y  (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
      86             :  *
      87             :  * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
      88             :  *       - HE AAC v2 comprises LC AAC with Spectral Band Replication and
      89             :            Parametric Stereo.
      90             :  */
      91             : 
      92             : #include "libavutil/thread.h"
      93             : 
      94             : static VLC vlc_scalefactors;
      95             : static VLC vlc_spectral[11];
      96             : 
      97             : static int output_configure(AACContext *ac,
      98             :                             uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
      99             :                             enum OCStatus oc_type, int get_new_frame);
     100             : 
     101             : #define overread_err "Input buffer exhausted before END element found\n"
     102             : 
     103         524 : static int count_channels(uint8_t (*layout)[3], int tags)
     104             : {
     105         524 :     int i, sum = 0;
     106        1610 :     for (i = 0; i < tags; i++) {
     107        1086 :         int syn_ele = layout[i][0];
     108        1086 :         int pos     = layout[i][2];
     109        2172 :         sum += (1 + (syn_ele == TYPE_CPE)) *
     110        1086 :                (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
     111             :     }
     112         524 :     return sum;
     113             : }
     114             : 
     115             : /**
     116             :  * Check for the channel element in the current channel position configuration.
     117             :  * If it exists, make sure the appropriate element is allocated and map the
     118             :  * channel order to match the internal FFmpeg channel layout.
     119             :  *
     120             :  * @param   che_pos current channel position configuration
     121             :  * @param   type channel element type
     122             :  * @param   id channel element id
     123             :  * @param   channels count of the number of channels in the configuration
     124             :  *
     125             :  * @return  Returns error status. 0 - OK, !0 - error
     126             :  */
     127        4580 : static av_cold int che_configure(AACContext *ac,
     128             :                                  enum ChannelPosition che_pos,
     129             :                                  int type, int id, int *channels)
     130             : {
     131        4580 :     if (*channels >= MAX_CHANNELS)
     132           0 :         return AVERROR_INVALIDDATA;
     133        4580 :     if (che_pos) {
     134        4580 :         if (!ac->che[type][id]) {
     135         345 :             if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
     136           0 :                 return AVERROR(ENOMEM);
     137         345 :             AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr, type);
     138             :         }
     139        4580 :         if (type != TYPE_CCE) {
     140        4572 :             if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
     141           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
     142           0 :                 return AVERROR_INVALIDDATA;
     143             :             }
     144        4572 :             ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
     145        4572 :             if (type == TYPE_CPE ||
     146         771 :                 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
     147        4100 :                 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
     148             :             }
     149             :         }
     150             :     } else {
     151           0 :         if (ac->che[type][id])
     152           0 :             AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][id]->sbr);
     153           0 :         av_freep(&ac->che[type][id]);
     154             :     }
     155        4580 :     return 0;
     156             : }
     157             : 
     158       49856 : static int frame_configure_elements(AVCodecContext *avctx)
     159             : {
     160       49856 :     AACContext *ac = avctx->priv_data;
     161             :     int type, id, ch, ret;
     162             : 
     163             :     /* set channel pointers to internal buffers by default */
     164      249280 :     for (type = 0; type < 4; type++) {
     165     3390208 :         for (id = 0; id < MAX_ELEM_ID; id++) {
     166     3190784 :             ChannelElement *che = ac->che[type][id];
     167     3190784 :             if (che) {
     168       63236 :                 che->ch[0].ret = che->ch[0].ret_buf;
     169       63236 :                 che->ch[1].ret = che->ch[1].ret_buf;
     170             :             }
     171             :         }
     172             :     }
     173             : 
     174             :     /* get output buffer */
     175       49856 :     av_frame_unref(ac->frame);
     176       49856 :     if (!avctx->channels)
     177           0 :         return 1;
     178             : 
     179       49856 :     ac->frame->nb_samples = 2048;
     180       49856 :     if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
     181           0 :         return ret;
     182             : 
     183             :     /* map output channel pointers to AVFrame data */
     184      144262 :     for (ch = 0; ch < avctx->channels; ch++) {
     185       94406 :         if (ac->output_element[ch])
     186       94406 :             ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
     187             :     }
     188             : 
     189       49856 :     return 0;
     190             : }
     191             : 
     192             : struct elem_to_channel {
     193             :     uint64_t av_position;
     194             :     uint8_t syn_ele;
     195             :     uint8_t elem_id;
     196             :     uint8_t aac_position;
     197             : };
     198             : 
     199        3738 : static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
     200             :                        uint8_t (*layout_map)[3], int offset, uint64_t left,
     201             :                        uint64_t right, int pos)
     202             : {
     203        3738 :     if (layout_map[offset][0] == TYPE_CPE) {
     204        7468 :         e2c_vec[offset] = (struct elem_to_channel) {
     205        3734 :             .av_position  = left | right,
     206             :             .syn_ele      = TYPE_CPE,
     207        3734 :             .elem_id      = layout_map[offset][1],
     208             :             .aac_position = pos
     209             :         };
     210        3734 :         return 1;
     211             :     } else {
     212           8 :         e2c_vec[offset] = (struct elem_to_channel) {
     213             :             .av_position  = left,
     214             :             .syn_ele      = TYPE_SCE,
     215           4 :             .elem_id      = layout_map[offset][1],
     216             :             .aac_position = pos
     217             :         };
     218           8 :         e2c_vec[offset + 1] = (struct elem_to_channel) {
     219             :             .av_position  = right,
     220             :             .syn_ele      = TYPE_SCE,
     221           4 :             .elem_id      = layout_map[offset + 1][1],
     222             :             .aac_position = pos
     223             :         };
     224           4 :         return 2;
     225             :     }
     226             : }
     227             : 
     228       13089 : static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
     229             :                                  int *current)
     230             : {
     231       13089 :     int num_pos_channels = 0;
     232       13089 :     int first_cpe        = 0;
     233       13089 :     int sce_parity       = 0;
     234             :     int i;
     235       17594 :     for (i = *current; i < tags; i++) {
     236        4706 :         if (layout_map[i][2] != pos)
     237         201 :             break;
     238        4505 :         if (layout_map[i][0] == TYPE_CPE) {
     239        3734 :             if (sce_parity) {
     240          71 :                 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
     241          71 :                     sce_parity = 0;
     242             :                 } else {
     243           0 :                     return -1;
     244             :                 }
     245             :             }
     246        3734 :             num_pos_channels += 2;
     247        3734 :             first_cpe         = 1;
     248             :         } else {
     249         771 :             num_pos_channels++;
     250         771 :             sce_parity ^= 1;
     251             :         }
     252             :     }
     253       13089 :     if (sce_parity &&
     254         692 :         ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
     255           0 :         return -1;
     256       13089 :     *current = i;
     257       13089 :     return num_pos_channels;
     258             : }
     259             : 
     260        4363 : static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
     261             : {
     262             :     int i, n, total_non_cc_elements;
     263        4363 :     struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
     264             :     int num_front_channels, num_side_channels, num_back_channels;
     265             :     uint64_t layout;
     266             : 
     267        4363 :     if (FF_ARRAY_ELEMS(e2c_vec) < tags)
     268           0 :         return 0;
     269             : 
     270        4363 :     i = 0;
     271        4363 :     num_front_channels =
     272             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
     273        4363 :     if (num_front_channels < 0)
     274           0 :         return 0;
     275        4363 :     num_side_channels =
     276             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
     277        4363 :     if (num_side_channels < 0)
     278           0 :         return 0;
     279        4363 :     num_back_channels =
     280             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
     281        4363 :     if (num_back_channels < 0)
     282           0 :         return 0;
     283             : 
     284        4363 :     if (num_side_channels == 0 && num_back_channels >= 4) {
     285           0 :         num_side_channels = 2;
     286           0 :         num_back_channels -= 2;
     287             :     }
     288             : 
     289        4363 :     i = 0;
     290        4363 :     if (num_front_channels & 1) {
     291        1526 :         e2c_vec[i] = (struct elem_to_channel) {
     292             :             .av_position  = AV_CH_FRONT_CENTER,
     293             :             .syn_ele      = TYPE_SCE,
     294         763 :             .elem_id      = layout_map[i][1],
     295             :             .aac_position = AAC_CHANNEL_FRONT
     296             :         };
     297         763 :         i++;
     298         763 :         num_front_channels--;
     299             :     }
     300        4363 :     if (num_front_channels >= 4) {
     301           8 :         i += assign_pair(e2c_vec, layout_map, i,
     302             :                          AV_CH_FRONT_LEFT_OF_CENTER,
     303             :                          AV_CH_FRONT_RIGHT_OF_CENTER,
     304             :                          AAC_CHANNEL_FRONT);
     305           8 :         num_front_channels -= 2;
     306             :     }
     307        4363 :     if (num_front_channels >= 2) {
     308        3671 :         i += assign_pair(e2c_vec, layout_map, i,
     309             :                          AV_CH_FRONT_LEFT,
     310             :                          AV_CH_FRONT_RIGHT,
     311             :                          AAC_CHANNEL_FRONT);
     312        3671 :         num_front_channels -= 2;
     313             :     }
     314        8726 :     while (num_front_channels >= 2) {
     315           0 :         i += assign_pair(e2c_vec, layout_map, i,
     316             :                          UINT64_MAX,
     317             :                          UINT64_MAX,
     318             :                          AAC_CHANNEL_FRONT);
     319           0 :         num_front_channels -= 2;
     320             :     }
     321             : 
     322        4363 :     if (num_side_channels >= 2) {
     323           0 :         i += assign_pair(e2c_vec, layout_map, i,
     324             :                          AV_CH_SIDE_LEFT,
     325             :                          AV_CH_SIDE_RIGHT,
     326             :                          AAC_CHANNEL_FRONT);
     327           0 :         num_side_channels -= 2;
     328             :     }
     329        8726 :     while (num_side_channels >= 2) {
     330           0 :         i += assign_pair(e2c_vec, layout_map, i,
     331             :                          UINT64_MAX,
     332             :                          UINT64_MAX,
     333             :                          AAC_CHANNEL_SIDE);
     334           0 :         num_side_channels -= 2;
     335             :     }
     336             : 
     337        8726 :     while (num_back_channels >= 4) {
     338           0 :         i += assign_pair(e2c_vec, layout_map, i,
     339             :                          UINT64_MAX,
     340             :                          UINT64_MAX,
     341             :                          AAC_CHANNEL_BACK);
     342           0 :         num_back_channels -= 2;
     343             :     }
     344        4363 :     if (num_back_channels >= 2) {
     345          59 :         i += assign_pair(e2c_vec, layout_map, i,
     346             :                          AV_CH_BACK_LEFT,
     347             :                          AV_CH_BACK_RIGHT,
     348             :                          AAC_CHANNEL_BACK);
     349          59 :         num_back_channels -= 2;
     350             :     }
     351        4363 :     if (num_back_channels) {
     352           0 :         e2c_vec[i] = (struct elem_to_channel) {
     353             :             .av_position  = AV_CH_BACK_CENTER,
     354             :             .syn_ele      = TYPE_SCE,
     355           0 :             .elem_id      = layout_map[i][1],
     356             :             .aac_position = AAC_CHANNEL_BACK
     357             :         };
     358           0 :         i++;
     359           0 :         num_back_channels--;
     360             :     }
     361             : 
     362        4363 :     if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
     363         134 :         e2c_vec[i] = (struct elem_to_channel) {
     364             :             .av_position  = AV_CH_LOW_FREQUENCY,
     365             :             .syn_ele      = TYPE_LFE,
     366          67 :             .elem_id      = layout_map[i][1],
     367             :             .aac_position = AAC_CHANNEL_LFE
     368             :         };
     369          67 :         i++;
     370             :     }
     371        8726 :     while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
     372           0 :         e2c_vec[i] = (struct elem_to_channel) {
     373             :             .av_position  = UINT64_MAX,
     374             :             .syn_ele      = TYPE_LFE,
     375           0 :             .elem_id      = layout_map[i][1],
     376             :             .aac_position = AAC_CHANNEL_LFE
     377             :         };
     378           0 :         i++;
     379             :     }
     380             : 
     381             :     // Must choose a stable sort
     382        4363 :     total_non_cc_elements = n = i;
     383             :     do {
     384        4442 :         int next_n = 0;
     385        4785 :         for (i = 1; i < n; i++)
     386         343 :             if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
     387         146 :                 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
     388         146 :                 next_n = i;
     389             :             }
     390        4442 :         n = next_n;
     391        4442 :     } while (n > 0);
     392             : 
     393        4363 :     layout = 0;
     394        8935 :     for (i = 0; i < total_non_cc_elements; i++) {
     395        4572 :         layout_map[i][0] = e2c_vec[i].syn_ele;
     396        4572 :         layout_map[i][1] = e2c_vec[i].elem_id;
     397        4572 :         layout_map[i][2] = e2c_vec[i].aac_position;
     398        4572 :         if (e2c_vec[i].av_position != UINT64_MAX) {
     399        4572 :             layout |= e2c_vec[i].av_position;
     400             :         }
     401             :     }
     402             : 
     403        4363 :     return layout;
     404             : }
     405             : 
     406             : /**
     407             :  * Save current output configuration if and only if it has been locked.
     408             :  */
     409        4118 : static int push_output_configuration(AACContext *ac) {
     410        4118 :     int pushed = 0;
     411             : 
     412        4118 :     if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
     413        4118 :         ac->oc[0] = ac->oc[1];
     414        4118 :         pushed = 1;
     415             :     }
     416        4118 :     ac->oc[1].status = OC_NONE;
     417        4118 :     return pushed;
     418             : }
     419             : 
     420             : /**
     421             :  * Restore the previous output configuration if and only if the current
     422             :  * configuration is unlocked.
     423             :  */
     424           0 : static void pop_output_configuration(AACContext *ac) {
     425           0 :     if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
     426           0 :         ac->oc[1] = ac->oc[0];
     427           0 :         ac->avctx->channels = ac->oc[1].channels;
     428           0 :         ac->avctx->channel_layout = ac->oc[1].channel_layout;
     429           0 :         output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
     430             :                          ac->oc[1].status, 0);
     431             :     }
     432           0 : }
     433             : 
     434             : /**
     435             :  * Configure output channel order based on the current program
     436             :  * configuration element.
     437             :  *
     438             :  * @return  Returns error status. 0 - OK, !0 - error
     439             :  */
     440        4363 : static int output_configure(AACContext *ac,
     441             :                             uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
     442             :                             enum OCStatus oc_type, int get_new_frame)
     443             : {
     444        4363 :     AVCodecContext *avctx = ac->avctx;
     445        4363 :     int i, channels = 0, ret;
     446        4363 :     uint64_t layout = 0;
     447        4363 :     uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
     448        4363 :     uint8_t type_counts[TYPE_END] = { 0 };
     449             : 
     450        4363 :     if (ac->oc[1].layout_map != layout_map) {
     451        4351 :         memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
     452        4351 :         ac->oc[1].layout_map_tags = tags;
     453             :     }
     454        8943 :     for (i = 0; i < tags; i++) {
     455        4580 :         int type =         layout_map[i][0];
     456        4580 :         int id =           layout_map[i][1];
     457        4580 :         id_map[type][id] = type_counts[type]++;
     458        4580 :         if (id_map[type][id] >= MAX_ELEM_ID) {
     459           0 :             avpriv_request_sample(ac->avctx, "Too large remapped id");
     460           0 :             return AVERROR_PATCHWELCOME;
     461             :         }
     462             :     }
     463             :     // Try to sniff a reasonable channel order, otherwise output the
     464             :     // channels in the order the PCE declared them.
     465        4363 :     if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
     466        4363 :         layout = sniff_channel_order(layout_map, tags);
     467        8943 :     for (i = 0; i < tags; i++) {
     468        4580 :         int type =     layout_map[i][0];
     469        4580 :         int id =       layout_map[i][1];
     470        4580 :         int iid =      id_map[type][id];
     471        4580 :         int position = layout_map[i][2];
     472             :         // Allocate or free elements depending on if they are in the
     473             :         // current program configuration.
     474        4580 :         ret = che_configure(ac, position, type, iid, &channels);
     475        4580 :         if (ret < 0)
     476           0 :             return ret;
     477        4580 :         ac->tag_che_map[type][id] = ac->che[type][iid];
     478             :     }
     479        4363 :     if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
     480         366 :         if (layout == AV_CH_FRONT_CENTER) {
     481         366 :             layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
     482             :         } else {
     483           0 :             layout = 0;
     484             :         }
     485             :     }
     486             : 
     487        4363 :     if (layout) avctx->channel_layout = layout;
     488        4363 :                             ac->oc[1].channel_layout = layout;
     489        4363 :     avctx->channels       = ac->oc[1].channels       = channels;
     490        4363 :     ac->oc[1].status = oc_type;
     491             : 
     492        4363 :     if (get_new_frame) {
     493          12 :         if ((ret = frame_configure_elements(ac->avctx)) < 0)
     494           0 :             return ret;
     495             :     }
     496             : 
     497        4363 :     return 0;
     498             : }
     499             : 
     500           0 : static void flush(AVCodecContext *avctx)
     501             : {
     502           0 :     AACContext *ac= avctx->priv_data;
     503             :     int type, i, j;
     504             : 
     505           0 :     for (type = 3; type >= 0; type--) {
     506           0 :         for (i = 0; i < MAX_ELEM_ID; i++) {
     507           0 :             ChannelElement *che = ac->che[type][i];
     508           0 :             if (che) {
     509           0 :                 for (j = 0; j <= 1; j++) {
     510           0 :                     memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
     511             :                 }
     512             :             }
     513             :         }
     514             :     }
     515           0 : }
     516             : 
     517             : /**
     518             :  * Set up channel positions based on a default channel configuration
     519             :  * as specified in table 1.17.
     520             :  *
     521             :  * @return  Returns error status. 0 - OK, !0 - error
     522             :  */
     523        4629 : static int set_default_channel_config(AVCodecContext *avctx,
     524             :                                       uint8_t (*layout_map)[3],
     525             :                                       int *tags,
     526             :                                       int channel_config)
     527             : {
     528        4629 :     if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
     529             :         channel_config > 12) {
     530           0 :         av_log(avctx, AV_LOG_ERROR,
     531             :                "invalid default channel configuration (%d)\n",
     532             :                channel_config);
     533           0 :         return AVERROR_INVALIDDATA;
     534             :     }
     535        4629 :     *tags = tags_per_config[channel_config];
     536        4629 :     memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
     537        4629 :            *tags * sizeof(*layout_map));
     538             : 
     539             :     /*
     540             :      * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
     541             :      * However, at least Nero AAC encoder encodes 7.1 streams using the default
     542             :      * channel config 7, mapping the side channels of the original audio stream
     543             :      * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
     544             :      * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
     545             :      * the incorrect streams as if they were correct (and as the encoder intended).
     546             :      *
     547             :      * As actual intended 7.1(wide) streams are very rare, default to assuming a
     548             :      * 7.1 layout was intended.
     549             :      */
     550        4629 :     if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
     551           0 :         av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
     552             :                " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
     553             :                " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
     554           0 :         layout_map[2][2] = AAC_CHANNEL_SIDE;
     555             :     }
     556             : 
     557        4629 :     return 0;
     558             : }
     559             : 
     560       62890 : static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
     561             : {
     562             :     /* For PCE based channel configurations map the channels solely based
     563             :      * on tags. */
     564       62890 :     if (!ac->oc[1].m4ac.chan_config) {
     565       24124 :         return ac->tag_che_map[type][elem_id];
     566             :     }
     567             :     // Allow single CPE stereo files to be signalled with mono configuration.
     568       59299 :     if (!ac->tags_mapped && type == TYPE_CPE &&
     569       20533 :         ac->oc[1].m4ac.chan_config == 1) {
     570             :         uint8_t layout_map[MAX_ELEM_ID*4][3];
     571             :         int layout_map_tags;
     572           0 :         push_output_configuration(ac);
     573             : 
     574           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
     575             : 
     576           0 :         if (set_default_channel_config(ac->avctx, layout_map,
     577             :                                        &layout_map_tags, 2) < 0)
     578           0 :             return NULL;
     579           0 :         if (output_configure(ac, layout_map, layout_map_tags,
     580             :                              OC_TRIAL_FRAME, 1) < 0)
     581           0 :             return NULL;
     582             : 
     583           0 :         ac->oc[1].m4ac.chan_config = 2;
     584           0 :         ac->oc[1].m4ac.ps = 0;
     585             :     }
     586             :     // And vice-versa
     587       54539 :     if (!ac->tags_mapped && type == TYPE_SCE &&
     588       15773 :         ac->oc[1].m4ac.chan_config == 2) {
     589             :         uint8_t layout_map[MAX_ELEM_ID * 4][3];
     590             :         int layout_map_tags;
     591           0 :         push_output_configuration(ac);
     592             : 
     593           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
     594             : 
     595           0 :         if (set_default_channel_config(ac->avctx, layout_map,
     596             :                                        &layout_map_tags, 1) < 0)
     597           0 :             return NULL;
     598           0 :         if (output_configure(ac, layout_map, layout_map_tags,
     599             :                              OC_TRIAL_FRAME, 1) < 0)
     600           0 :             return NULL;
     601             : 
     602           0 :         ac->oc[1].m4ac.chan_config = 1;
     603           0 :         if (ac->oc[1].m4ac.sbr)
     604           0 :             ac->oc[1].m4ac.ps = -1;
     605             :     }
     606             :     /* For indexed channel configurations map the channels solely based
     607             :      * on position. */
     608       38766 :     switch (ac->oc[1].m4ac.chan_config) {
     609           0 :     case 12:
     610             :     case 7:
     611           0 :         if (ac->tags_mapped == 3 && type == TYPE_CPE) {
     612           0 :             ac->tags_mapped++;
     613           0 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
     614             :         }
     615             :     case 11:
     616           0 :         if (ac->tags_mapped == 2 &&
     617           0 :             ac->oc[1].m4ac.chan_config == 11 &&
     618             :             type == TYPE_SCE) {
     619           0 :             ac->tags_mapped++;
     620           0 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
     621             :         }
     622             :     case 6:
     623             :         /* Some streams incorrectly code 5.1 audio as
     624             :          * SCE[0] CPE[0] CPE[1] SCE[1]
     625             :          * instead of
     626             :          * SCE[0] CPE[0] CPE[1] LFE[0].
     627             :          * If we seem to have encountered such a stream, transfer
     628             :          * the LFE[0] element to the SCE[1]'s mapping */
     629        3280 :         if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
     630         820 :             if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
     631           0 :                 av_log(ac->avctx, AV_LOG_WARNING,
     632             :                    "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
     633             :                    type == TYPE_SCE ? "SCE" : "LFE", elem_id);
     634           0 :                 ac->warned_remapping_once++;
     635             :             }
     636         820 :             ac->tags_mapped++;
     637         820 :             return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
     638             :         }
     639             :     case 5:
     640        2460 :         if (ac->tags_mapped == 2 && type == TYPE_CPE) {
     641         820 :             ac->tags_mapped++;
     642         820 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
     643             :         }
     644             :     case 4:
     645             :         /* Some streams incorrectly code 4.0 audio as
     646             :          * SCE[0] CPE[0] LFE[0]
     647             :          * instead of
     648             :          * SCE[0] CPE[0] SCE[1].
     649             :          * If we seem to have encountered such a stream, transfer
     650             :          * the SCE[1] element to the LFE[0]'s mapping */
     651        1640 :         if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
     652           0 :             if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
     653           0 :                 av_log(ac->avctx, AV_LOG_WARNING,
     654             :                    "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
     655             :                    type == TYPE_SCE ? "SCE" : "LFE", elem_id);
     656           0 :                 ac->warned_remapping_once++;
     657             :             }
     658           0 :             ac->tags_mapped++;
     659           0 :             return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
     660             :         }
     661        1640 :         if (ac->tags_mapped == 2 &&
     662           0 :             ac->oc[1].m4ac.chan_config == 4 &&
     663             :             type == TYPE_SCE) {
     664           0 :             ac->tags_mapped++;
     665           0 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
     666             :         }
     667             :     case 3:
     668             :     case 2:
     669       22173 :         if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
     670             :             type == TYPE_CPE) {
     671       21353 :             ac->tags_mapped++;
     672       21353 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
     673         820 :         } else if (ac->oc[1].m4ac.chan_config == 2) {
     674           0 :             return NULL;
     675             :         }
     676             :     case 1:
     677       15773 :         if (!ac->tags_mapped && type == TYPE_SCE) {
     678       15773 :             ac->tags_mapped++;
     679       15773 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
     680             :         }
     681             :     default:
     682           0 :         return NULL;
     683             :     }
     684             : }
     685             : 
     686             : /**
     687             :  * Decode an array of 4 bit element IDs, optionally interleaved with a
     688             :  * stereo/mono switching bit.
     689             :  *
     690             :  * @param type speaker type/position for these channels
     691             :  */
     692         215 : static void decode_channel_map(uint8_t layout_map[][3],
     693             :                                enum ChannelPosition type,
     694             :                                GetBitContext *gb, int n)
     695             : {
     696         525 :     while (n--) {
     697             :         enum RawDataBlockType syn_ele;
     698          95 :         switch (type) {
     699          75 :         case AAC_CHANNEL_FRONT:
     700             :         case AAC_CHANNEL_BACK:
     701             :         case AAC_CHANNEL_SIDE:
     702          75 :             syn_ele = get_bits1(gb);
     703          75 :             break;
     704           8 :         case AAC_CHANNEL_CC:
     705           8 :             skip_bits1(gb);
     706           8 :             syn_ele = TYPE_CCE;
     707           8 :             break;
     708          12 :         case AAC_CHANNEL_LFE:
     709          12 :             syn_ele = TYPE_LFE;
     710          12 :             break;
     711           0 :         default:
     712             :             // AAC_CHANNEL_OFF has no channel map
     713           0 :             av_assert0(0);
     714             :         }
     715          95 :         layout_map[0][0] = syn_ele;
     716          95 :         layout_map[0][1] = get_bits(gb, 4);
     717          95 :         layout_map[0][2] = type;
     718          95 :         layout_map++;
     719             :     }
     720         215 : }
     721             : 
     722          43 : static inline void relative_align_get_bits(GetBitContext *gb,
     723             :                                            int reference_position) {
     724          43 :     int n = (reference_position - get_bits_count(gb) & 7);
     725          43 :     if (n)
     726          39 :         skip_bits(gb, n);
     727          43 : }
     728             : 
     729             : /**
     730             :  * Decode program configuration element; reference: table 4.2.
     731             :  *
     732             :  * @return  Returns error status. 0 - OK, !0 - error
     733             :  */
     734          43 : static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
     735             :                       uint8_t (*layout_map)[3],
     736             :                       GetBitContext *gb, int byte_align_ref)
     737             : {
     738             :     int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
     739             :     int sampling_index;
     740             :     int comment_len;
     741             :     int tags;
     742             : 
     743          43 :     skip_bits(gb, 2);  // object_type
     744             : 
     745          43 :     sampling_index = get_bits(gb, 4);
     746          43 :     if (m4ac->sampling_index != sampling_index)
     747           0 :         av_log(avctx, AV_LOG_WARNING,
     748             :                "Sample rate index in program config element does not "
     749             :                "match the sample rate index configured by the container.\n");
     750             : 
     751          43 :     num_front       = get_bits(gb, 4);
     752          43 :     num_side        = get_bits(gb, 4);
     753          43 :     num_back        = get_bits(gb, 4);
     754          43 :     num_lfe         = get_bits(gb, 2);
     755          43 :     num_assoc_data  = get_bits(gb, 3);
     756          43 :     num_cc          = get_bits(gb, 4);
     757             : 
     758          43 :     if (get_bits1(gb))
     759           0 :         skip_bits(gb, 4); // mono_mixdown_tag
     760          43 :     if (get_bits1(gb))
     761           0 :         skip_bits(gb, 4); // stereo_mixdown_tag
     762             : 
     763          43 :     if (get_bits1(gb))
     764           0 :         skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
     765             : 
     766          43 :     if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
     767           0 :         av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
     768           0 :         return -1;
     769             :     }
     770          43 :     decode_channel_map(layout_map       , AAC_CHANNEL_FRONT, gb, num_front);
     771          43 :     tags = num_front;
     772          43 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE,  gb, num_side);
     773          43 :     tags += num_side;
     774          43 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK,  gb, num_back);
     775          43 :     tags += num_back;
     776          43 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE,   gb, num_lfe);
     777          43 :     tags += num_lfe;
     778             : 
     779          43 :     skip_bits_long(gb, 4 * num_assoc_data);
     780             : 
     781          43 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_CC,    gb, num_cc);
     782          43 :     tags += num_cc;
     783             : 
     784          43 :     relative_align_get_bits(gb, byte_align_ref);
     785             : 
     786             :     /* comment field, first byte is length */
     787          43 :     comment_len = get_bits(gb, 8) * 8;
     788          43 :     if (get_bits_left(gb) < comment_len) {
     789           0 :         av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
     790           0 :         return AVERROR_INVALIDDATA;
     791             :     }
     792          43 :     skip_bits_long(gb, comment_len);
     793          43 :     return tags;
     794             : }
     795             : 
     796             : /**
     797             :  * Decode GA "General Audio" specific configuration; reference: table 4.1.
     798             :  *
     799             :  * @param   ac          pointer to AACContext, may be null
     800             :  * @param   avctx       pointer to AVCCodecContext, used for logging
     801             :  *
     802             :  * @return  Returns error status. 0 - OK, !0 - error
     803             :  */
     804         524 : static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
     805             :                                      GetBitContext *gb,
     806             :                                      int get_bit_alignment,
     807             :                                      MPEG4AudioConfig *m4ac,
     808             :                                      int channel_config)
     809             : {
     810             :     int extension_flag, ret, ep_config, res_flags;
     811             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
     812         524 :     int tags = 0;
     813             : 
     814             : #if USE_FIXED
     815          17 :     if (get_bits1(gb)) { // frameLengthFlag
     816           0 :         avpriv_report_missing_feature(avctx, "Fixed point 960/120 MDCT window");
     817           0 :         return AVERROR_PATCHWELCOME;
     818             :     }
     819          17 :     m4ac->frame_length_short = 0;
     820             : #else
     821         507 :     m4ac->frame_length_short = get_bits1(gb);
     822         507 :     if (m4ac->frame_length_short && m4ac->sbr == 1) {
     823           0 :       avpriv_report_missing_feature(avctx, "SBR with 960 frame length");
     824           0 :       if (ac) ac->warned_960_sbr = 1;
     825           0 :       m4ac->sbr = 0;
     826           0 :       m4ac->ps = 0;
     827             :     }
     828             : #endif
     829             : 
     830         524 :     if (get_bits1(gb))       // dependsOnCoreCoder
     831           0 :         skip_bits(gb, 14);   // coreCoderDelay
     832         524 :     extension_flag = get_bits1(gb);
     833             : 
     834        1048 :     if (m4ac->object_type == AOT_AAC_SCALABLE ||
     835         524 :         m4ac->object_type == AOT_ER_AAC_SCALABLE)
     836           0 :         skip_bits(gb, 3);     // layerNr
     837             : 
     838         524 :     if (channel_config == 0) {
     839          43 :         skip_bits(gb, 4);  // element_instance_tag
     840          43 :         tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
     841          43 :         if (tags < 0)
     842           0 :             return tags;
     843             :     } else {
     844         481 :         if ((ret = set_default_channel_config(avctx, layout_map,
     845             :                                               &tags, channel_config)))
     846           0 :             return ret;
     847             :     }
     848             : 
     849         524 :     if (count_channels(layout_map, tags) > 1) {
     850         436 :         m4ac->ps = 0;
     851          88 :     } else if (m4ac->sbr == 1 && m4ac->ps == -1)
     852          18 :         m4ac->ps = 1;
     853             : 
     854         524 :     if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
     855           0 :         return ret;
     856             : 
     857         524 :     if (extension_flag) {
     858           4 :         switch (m4ac->object_type) {
     859           0 :         case AOT_ER_BSAC:
     860           0 :             skip_bits(gb, 5);    // numOfSubFrame
     861           0 :             skip_bits(gb, 11);   // layer_length
     862           0 :             break;
     863           4 :         case AOT_ER_AAC_LC:
     864             :         case AOT_ER_AAC_LTP:
     865             :         case AOT_ER_AAC_SCALABLE:
     866             :         case AOT_ER_AAC_LD:
     867           4 :             res_flags = get_bits(gb, 3);
     868           4 :             if (res_flags) {
     869           0 :                 avpriv_report_missing_feature(avctx,
     870             :                                               "AAC data resilience (flags %x)",
     871             :                                               res_flags);
     872           0 :                 return AVERROR_PATCHWELCOME;
     873             :             }
     874           4 :             break;
     875             :         }
     876           4 :         skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
     877             :     }
     878         524 :     switch (m4ac->object_type) {
     879           4 :     case AOT_ER_AAC_LC:
     880             :     case AOT_ER_AAC_LTP:
     881             :     case AOT_ER_AAC_SCALABLE:
     882             :     case AOT_ER_AAC_LD:
     883           4 :         ep_config = get_bits(gb, 2);
     884           4 :         if (ep_config) {
     885           0 :             avpriv_report_missing_feature(avctx,
     886             :                                           "epConfig %d", ep_config);
     887           0 :             return AVERROR_PATCHWELCOME;
     888             :         }
     889             :     }
     890         524 :     return 0;
     891             : }
     892             : 
     893          10 : static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
     894             :                                      GetBitContext *gb,
     895             :                                      MPEG4AudioConfig *m4ac,
     896             :                                      int channel_config)
     897             : {
     898             :     int ret, ep_config, res_flags;
     899             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
     900          10 :     int tags = 0;
     901          10 :     const int ELDEXT_TERM = 0;
     902             : 
     903          10 :     m4ac->ps  = 0;
     904          10 :     m4ac->sbr = 0;
     905             : #if USE_FIXED
     906           2 :     if (get_bits1(gb)) { // frameLengthFlag
     907           0 :         avpriv_request_sample(avctx, "960/120 MDCT window");
     908           0 :         return AVERROR_PATCHWELCOME;
     909             :     }
     910             : #else
     911           8 :     m4ac->frame_length_short = get_bits1(gb);
     912             : #endif
     913          10 :     res_flags = get_bits(gb, 3);
     914          10 :     if (res_flags) {
     915           0 :         avpriv_report_missing_feature(avctx,
     916             :                                       "AAC data resilience (flags %x)",
     917             :                                       res_flags);
     918           0 :         return AVERROR_PATCHWELCOME;
     919             :     }
     920             : 
     921          10 :     if (get_bits1(gb)) { // ldSbrPresentFlag
     922           0 :         avpriv_report_missing_feature(avctx,
     923             :                                       "Low Delay SBR");
     924           0 :         return AVERROR_PATCHWELCOME;
     925             :     }
     926             : 
     927          20 :     while (get_bits(gb, 4) != ELDEXT_TERM) {
     928           0 :         int len = get_bits(gb, 4);
     929           0 :         if (len == 15)
     930           0 :             len += get_bits(gb, 8);
     931           0 :         if (len == 15 + 255)
     932           0 :             len += get_bits(gb, 16);
     933           0 :         if (get_bits_left(gb) < len * 8 + 4) {
     934           0 :             av_log(avctx, AV_LOG_ERROR, overread_err);
     935           0 :             return AVERROR_INVALIDDATA;
     936             :         }
     937           0 :         skip_bits_long(gb, 8 * len);
     938             :     }
     939             : 
     940          10 :     if ((ret = set_default_channel_config(avctx, layout_map,
     941             :                                           &tags, channel_config)))
     942           0 :         return ret;
     943             : 
     944          10 :     if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
     945           0 :         return ret;
     946             : 
     947          10 :     ep_config = get_bits(gb, 2);
     948          10 :     if (ep_config) {
     949           0 :         avpriv_report_missing_feature(avctx,
     950             :                                       "epConfig %d", ep_config);
     951           0 :         return AVERROR_PATCHWELCOME;
     952             :     }
     953          10 :     return 0;
     954             : }
     955             : 
     956             : /**
     957             :  * Decode audio specific configuration; reference: table 1.13.
     958             :  *
     959             :  * @param   ac          pointer to AACContext, may be null
     960             :  * @param   avctx       pointer to AVCCodecContext, used for logging
     961             :  * @param   m4ac        pointer to MPEG4AudioConfig, used for parsing
     962             :  * @param   gb          buffer holding an audio specific config
     963             :  * @param   get_bit_alignment relative alignment for byte align operations
     964             :  * @param   sync_extension look for an appended sync extension
     965             :  *
     966             :  * @return  Returns error status or number of consumed bits. <0 - error
     967             :  */
     968         534 : static int decode_audio_specific_config_gb(AACContext *ac,
     969             :                                            AVCodecContext *avctx,
     970             :                                            MPEG4AudioConfig *m4ac,
     971             :                                            GetBitContext *gb,
     972             :                                            int get_bit_alignment,
     973             :                                            int sync_extension)
     974             : {
     975             :     int i, ret;
     976         534 :     GetBitContext gbc = *gb;
     977             : 
     978         534 :     if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension)) < 0)
     979           0 :         return AVERROR_INVALIDDATA;
     980             : 
     981         534 :     if (m4ac->sampling_index > 12) {
     982           0 :         av_log(avctx, AV_LOG_ERROR,
     983             :                "invalid sampling rate index %d\n",
     984             :                m4ac->sampling_index);
     985           0 :         return AVERROR_INVALIDDATA;
     986             :     }
     987         538 :     if (m4ac->object_type == AOT_ER_AAC_LD &&
     988           8 :         (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
     989           0 :         av_log(avctx, AV_LOG_ERROR,
     990             :                "invalid low delay sampling rate index %d\n",
     991             :                m4ac->sampling_index);
     992           0 :         return AVERROR_INVALIDDATA;
     993             :     }
     994             : 
     995         534 :     skip_bits_long(gb, i);
     996             : 
     997         534 :     switch (m4ac->object_type) {
     998         524 :     case AOT_AAC_MAIN:
     999             :     case AOT_AAC_LC:
    1000             :     case AOT_AAC_SSR:
    1001             :     case AOT_AAC_LTP:
    1002             :     case AOT_ER_AAC_LC:
    1003             :     case AOT_ER_AAC_LD:
    1004         524 :         if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
    1005             :                                             m4ac, m4ac->chan_config)) < 0)
    1006           0 :             return ret;
    1007         524 :         break;
    1008          10 :     case AOT_ER_AAC_ELD:
    1009          10 :         if ((ret = decode_eld_specific_config(ac, avctx, gb,
    1010             :                                               m4ac, m4ac->chan_config)) < 0)
    1011           0 :             return ret;
    1012          10 :         break;
    1013           0 :     default:
    1014           0 :         avpriv_report_missing_feature(avctx,
    1015             :                                       "Audio object type %s%d",
    1016           0 :                                       m4ac->sbr == 1 ? "SBR+" : "",
    1017             :                                       m4ac->object_type);
    1018           0 :         return AVERROR(ENOSYS);
    1019             :     }
    1020             : 
    1021             :     ff_dlog(avctx,
    1022             :             "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
    1023             :             m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
    1024             :             m4ac->sample_rate, m4ac->sbr,
    1025             :             m4ac->ps);
    1026             : 
    1027         534 :     return get_bits_count(gb);
    1028             : }
    1029             : 
    1030         213 : static int decode_audio_specific_config(AACContext *ac,
    1031             :                                         AVCodecContext *avctx,
    1032             :                                         MPEG4AudioConfig *m4ac,
    1033             :                                         const uint8_t *data, int64_t bit_size,
    1034             :                                         int sync_extension)
    1035             : {
    1036             :     int i, ret;
    1037             :     GetBitContext gb;
    1038             : 
    1039         213 :     if (bit_size < 0 || bit_size > INT_MAX) {
    1040           0 :         av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
    1041           0 :         return AVERROR_INVALIDDATA;
    1042             :     }
    1043             : 
    1044             :     ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
    1045         213 :     for (i = 0; i < bit_size >> 3; i++)
    1046             :         ff_dlog(avctx, "%02x ", data[i]);
    1047             :     ff_dlog(avctx, "\n");
    1048             : 
    1049         213 :     if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
    1050           0 :         return ret;
    1051             : 
    1052         213 :     return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
    1053             :                                            sync_extension);
    1054             : }
    1055             : 
    1056             : /**
    1057             :  * linear congruential pseudorandom number generator
    1058             :  *
    1059             :  * @param   previous_val    pointer to the current state of the generator
    1060             :  *
    1061             :  * @return  Returns a 32-bit pseudorandom integer
    1062             :  */
    1063     5948804 : static av_always_inline int lcg_random(unsigned previous_val)
    1064             : {
    1065     5948804 :     union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
    1066     5948804 :     return v.s;
    1067             : }
    1068             : 
    1069          40 : static void reset_all_predictors(PredictorState *ps)
    1070             : {
    1071             :     int i;
    1072       26920 :     for (i = 0; i < MAX_PREDICTORS; i++)
    1073       26880 :         reset_predict_state(&ps[i]);
    1074          40 : }
    1075             : 
    1076          61 : static int sample_rate_idx (int rate)
    1077             : {
    1078          61 :          if (92017 <= rate) return 0;
    1079          61 :     else if (75132 <= rate) return 1;
    1080          61 :     else if (55426 <= rate) return 2;
    1081          61 :     else if (46009 <= rate) return 3;
    1082          60 :     else if (37566 <= rate) return 4;
    1083          42 :     else if (27713 <= rate) return 5;
    1084          39 :     else if (23004 <= rate) return 6;
    1085          39 :     else if (18783 <= rate) return 7;
    1086          39 :     else if (13856 <= rate) return 8;
    1087          35 :     else if (11502 <= rate) return 9;
    1088          35 :     else if (9391  <= rate) return 10;
    1089          35 :     else                    return 11;
    1090             : }
    1091             : 
    1092         516 : static void reset_predictor_group(PredictorState *ps, int group_num)
    1093             : {
    1094             :     int i;
    1095       12141 :     for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
    1096       11625 :         reset_predict_state(&ps[i]);
    1097         516 : }
    1098             : 
    1099             : #define AAC_INIT_VLC_STATIC(num, size)                                     \
    1100             :     INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num],     \
    1101             :          ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]),  \
    1102             :                                     sizeof(ff_aac_spectral_bits[num][0]),  \
    1103             :         ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
    1104             :                                     sizeof(ff_aac_spectral_codes[num][0]), \
    1105             :         size);
    1106             : 
    1107             : static void aacdec_init(AACContext *ac);
    1108             : 
    1109         162 : static av_cold void aac_static_table_init(void)
    1110             : {
    1111         162 :     AAC_INIT_VLC_STATIC( 0, 304);
    1112         162 :     AAC_INIT_VLC_STATIC( 1, 270);
    1113         162 :     AAC_INIT_VLC_STATIC( 2, 550);
    1114         162 :     AAC_INIT_VLC_STATIC( 3, 300);
    1115         162 :     AAC_INIT_VLC_STATIC( 4, 328);
    1116         162 :     AAC_INIT_VLC_STATIC( 5, 294);
    1117         162 :     AAC_INIT_VLC_STATIC( 6, 306);
    1118         162 :     AAC_INIT_VLC_STATIC( 7, 268);
    1119         162 :     AAC_INIT_VLC_STATIC( 8, 510);
    1120         162 :     AAC_INIT_VLC_STATIC( 9, 366);
    1121         162 :     AAC_INIT_VLC_STATIC(10, 462);
    1122             : 
    1123         162 :     AAC_RENAME(ff_aac_sbr_init)();
    1124             : 
    1125         162 :     ff_aac_tableinit();
    1126             : 
    1127         162 :     INIT_VLC_STATIC(&vlc_scalefactors, 7,
    1128             :                     FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
    1129             :                     ff_aac_scalefactor_bits,
    1130             :                     sizeof(ff_aac_scalefactor_bits[0]),
    1131             :                     sizeof(ff_aac_scalefactor_bits[0]),
    1132             :                     ff_aac_scalefactor_code,
    1133             :                     sizeof(ff_aac_scalefactor_code[0]),
    1134             :                     sizeof(ff_aac_scalefactor_code[0]),
    1135             :                     352);
    1136             : 
    1137             :     // window initialization
    1138         162 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_1024), 4.0, 1024);
    1139         162 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_128), 6.0, 128);
    1140             : #if !USE_FIXED
    1141         146 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_960), 4.0, 960);
    1142         146 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_120), 6.0, 120);
    1143         146 :     AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_960), 960);
    1144         146 :     AAC_RENAME(ff_sine_window_init)(AAC_RENAME(ff_sine_120), 120);
    1145             : #endif
    1146         162 :     AAC_RENAME(ff_init_ff_sine_windows)(10);
    1147         162 :     AAC_RENAME(ff_init_ff_sine_windows)( 9);
    1148         162 :     AAC_RENAME(ff_init_ff_sine_windows)( 7);
    1149             : 
    1150         162 :     AAC_RENAME(ff_cbrt_tableinit)();
    1151         162 : }
    1152             : 
    1153             : static AVOnce aac_table_init = AV_ONCE_INIT;
    1154             : 
    1155         267 : static av_cold int aac_decode_init(AVCodecContext *avctx)
    1156             : {
    1157         267 :     AACContext *ac = avctx->priv_data;
    1158             :     int ret;
    1159             : 
    1160         267 :     ret = ff_thread_once(&aac_table_init, &aac_static_table_init);
    1161         267 :     if (ret != 0)
    1162           0 :         return AVERROR_UNKNOWN;
    1163             : 
    1164         267 :     ac->avctx = avctx;
    1165         267 :     ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
    1166             : 
    1167         267 :     aacdec_init(ac);
    1168             : #if USE_FIXED
    1169          19 :     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
    1170             : #else
    1171         248 :     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    1172             : #endif /* USE_FIXED */
    1173             : 
    1174         267 :     if (avctx->extradata_size > 0) {
    1175         206 :         if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
    1176         206 :                                                 avctx->extradata,
    1177         206 :                                                 avctx->extradata_size * 8LL,
    1178             :                                                 1)) < 0)
    1179           0 :             return ret;
    1180             :     } else {
    1181             :         int sr, i;
    1182             :         uint8_t layout_map[MAX_ELEM_ID*4][3];
    1183             :         int layout_map_tags;
    1184             : 
    1185          61 :         sr = sample_rate_idx(avctx->sample_rate);
    1186          61 :         ac->oc[1].m4ac.sampling_index = sr;
    1187          61 :         ac->oc[1].m4ac.channels = avctx->channels;
    1188          61 :         ac->oc[1].m4ac.sbr = -1;
    1189          61 :         ac->oc[1].m4ac.ps = -1;
    1190             : 
    1191         113 :         for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
    1192         113 :             if (ff_mpeg4audio_channels[i] == avctx->channels)
    1193          61 :                 break;
    1194          61 :         if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
    1195           0 :             i = 0;
    1196             :         }
    1197          61 :         ac->oc[1].m4ac.chan_config = i;
    1198             : 
    1199          61 :         if (ac->oc[1].m4ac.chan_config) {
    1200          26 :             int ret = set_default_channel_config(avctx, layout_map,
    1201             :                 &layout_map_tags, ac->oc[1].m4ac.chan_config);
    1202          26 :             if (!ret)
    1203          26 :                 output_configure(ac, layout_map, layout_map_tags,
    1204             :                                  OC_GLOBAL_HDR, 0);
    1205           0 :             else if (avctx->err_recognition & AV_EF_EXPLODE)
    1206           0 :                 return AVERROR_INVALIDDATA;
    1207             :         }
    1208             :     }
    1209             : 
    1210         267 :     if (avctx->channels > MAX_CHANNELS) {
    1211           0 :         av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
    1212           0 :         return AVERROR_INVALIDDATA;
    1213             :     }
    1214             : 
    1215             : #if USE_FIXED
    1216          19 :     ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
    1217             : #else
    1218         248 :     ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
    1219             : #endif /* USE_FIXED */
    1220         267 :     if (!ac->fdsp) {
    1221           0 :         return AVERROR(ENOMEM);
    1222             :     }
    1223             : 
    1224         267 :     ac->random_state = 0x1f2e3d4c;
    1225             : 
    1226         267 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct,       11, 1, 1.0 / RANGE15(1024.0));
    1227         267 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld,    10, 1, 1.0 / RANGE15(512.0));
    1228         267 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small,  8, 1, 1.0 / RANGE15(128.0));
    1229         267 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp,   11, 0, RANGE15(-2.0));
    1230             : #if !USE_FIXED
    1231         248 :     ret = ff_mdct15_init(&ac->mdct120, 1, 3, 1.0f/(16*1024*120*2));
    1232         248 :     if (ret < 0)
    1233           0 :         return ret;
    1234         248 :     ret = ff_mdct15_init(&ac->mdct480, 1, 5, 1.0f/(16*1024*960));
    1235         248 :     if (ret < 0)
    1236           0 :         return ret;
    1237         248 :     ret = ff_mdct15_init(&ac->mdct960, 1, 6, 1.0f/(16*1024*960*2));
    1238         248 :     if (ret < 0)
    1239           0 :         return ret;
    1240             : #endif
    1241             : 
    1242         267 :     return 0;
    1243             : }
    1244             : 
    1245             : /**
    1246             :  * Skip data_stream_element; reference: table 4.10.
    1247             :  */
    1248        2821 : static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
    1249             : {
    1250        2821 :     int byte_align = get_bits1(gb);
    1251        2821 :     int count = get_bits(gb, 8);
    1252        2821 :     if (count == 255)
    1253           0 :         count += get_bits(gb, 8);
    1254        2821 :     if (byte_align)
    1255        2454 :         align_get_bits(gb);
    1256             : 
    1257        2821 :     if (get_bits_left(gb) < 8 * count) {
    1258           0 :         av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
    1259           0 :         return AVERROR_INVALIDDATA;
    1260             :     }
    1261        2821 :     skip_bits_long(gb, 8 * count);
    1262        2821 :     return 0;
    1263             : }
    1264             : 
    1265        1425 : static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
    1266             :                              GetBitContext *gb)
    1267             : {
    1268             :     int sfb;
    1269        1425 :     if (get_bits1(gb)) {
    1270         357 :         ics->predictor_reset_group = get_bits(gb, 5);
    1271         714 :         if (ics->predictor_reset_group == 0 ||
    1272         357 :             ics->predictor_reset_group > 30) {
    1273           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    1274             :                    "Invalid Predictor Reset Group.\n");
    1275           0 :             return AVERROR_INVALIDDATA;
    1276             :         }
    1277             :     }
    1278       47760 :     for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
    1279       46335 :         ics->prediction_used[sfb] = get_bits1(gb);
    1280             :     }
    1281        1425 :     return 0;
    1282             : }
    1283             : 
    1284             : /**
    1285             :  * Decode Long Term Prediction data; reference: table 4.xx.
    1286             :  */
    1287         786 : static void decode_ltp(LongTermPrediction *ltp,
    1288             :                        GetBitContext *gb, uint8_t max_sfb)
    1289             : {
    1290             :     int sfb;
    1291             : 
    1292         786 :     ltp->lag  = get_bits(gb, 11);
    1293         786 :     ltp->coef = ltp_coef[get_bits(gb, 3)];
    1294       32226 :     for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
    1295       31440 :         ltp->used[sfb] = get_bits1(gb);
    1296         786 : }
    1297             : 
    1298             : /**
    1299             :  * Decode Individual Channel Stream info; reference: table 4.6.
    1300             :  */
    1301       63112 : static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
    1302             :                            GetBitContext *gb)
    1303             : {
    1304       63112 :     const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
    1305       63112 :     const int aot = m4ac->object_type;
    1306       63112 :     const int sampling_index = m4ac->sampling_index;
    1307       63112 :     int ret_fail = AVERROR_INVALIDDATA;
    1308             : 
    1309       63112 :     if (aot != AOT_ER_AAC_ELD) {
    1310       46747 :         if (get_bits1(gb)) {
    1311           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
    1312           0 :             if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
    1313           0 :                 return AVERROR_INVALIDDATA;
    1314             :         }
    1315       46747 :         ics->window_sequence[1] = ics->window_sequence[0];
    1316       46747 :         ics->window_sequence[0] = get_bits(gb, 2);
    1317       49171 :         if (aot == AOT_ER_AAC_LD &&
    1318        2424 :             ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
    1319           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    1320             :                    "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
    1321           0 :                    "window sequence %d found.\n", ics->window_sequence[0]);
    1322           0 :             ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
    1323           0 :             return AVERROR_INVALIDDATA;
    1324             :         }
    1325       46747 :         ics->use_kb_window[1]   = ics->use_kb_window[0];
    1326       46747 :         ics->use_kb_window[0]   = get_bits1(gb);
    1327             :     }
    1328       63112 :     ics->num_window_groups  = 1;
    1329       63112 :     ics->group_len[0]       = 1;
    1330       63112 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    1331             :         int i;
    1332        1328 :         ics->max_sfb = get_bits(gb, 4);
    1333       10624 :         for (i = 0; i < 7; i++) {
    1334        9296 :             if (get_bits1(gb)) {
    1335        5868 :                 ics->group_len[ics->num_window_groups - 1]++;
    1336             :             } else {
    1337        3428 :                 ics->num_window_groups++;
    1338        3428 :                 ics->group_len[ics->num_window_groups - 1] = 1;
    1339             :             }
    1340             :         }
    1341        1328 :         ics->num_windows       = 8;
    1342        1328 :         if (m4ac->frame_length_short) {
    1343           1 :             ics->swb_offset    =  ff_swb_offset_120[sampling_index];
    1344           1 :             ics->num_swb       = ff_aac_num_swb_120[sampling_index];
    1345             :         } else {
    1346        1327 :             ics->swb_offset    =  ff_swb_offset_128[sampling_index];
    1347        1327 :             ics->num_swb       = ff_aac_num_swb_128[sampling_index];
    1348             :         }
    1349        1328 :         ics->tns_max_bands     = ff_tns_max_bands_128[sampling_index];
    1350        1328 :         ics->predictor_present = 0;
    1351             :     } else {
    1352       61784 :         ics->max_sfb           = get_bits(gb, 6);
    1353       61784 :         ics->num_windows       = 1;
    1354       61784 :         if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
    1355       18789 :             if (m4ac->frame_length_short) {
    1356        3559 :                 ics->swb_offset    =     ff_swb_offset_480[sampling_index];
    1357        3559 :                 ics->num_swb       =    ff_aac_num_swb_480[sampling_index];
    1358        3559 :                 ics->tns_max_bands =  ff_tns_max_bands_480[sampling_index];
    1359             :             } else {
    1360       15230 :                 ics->swb_offset    =     ff_swb_offset_512[sampling_index];
    1361       15230 :                 ics->num_swb       =    ff_aac_num_swb_512[sampling_index];
    1362       15230 :                 ics->tns_max_bands =  ff_tns_max_bands_512[sampling_index];
    1363             :             }
    1364       37578 :             if (!ics->num_swb || !ics->swb_offset) {
    1365           0 :                 ret_fail = AVERROR_BUG;
    1366           0 :                 goto fail;
    1367             :             }
    1368             :         } else {
    1369       42995 :             if (m4ac->frame_length_short) {
    1370         386 :                 ics->num_swb    = ff_aac_num_swb_960[sampling_index];
    1371         386 :                 ics->swb_offset = ff_swb_offset_960[sampling_index];
    1372             :             } else {
    1373       42609 :                 ics->num_swb    = ff_aac_num_swb_1024[sampling_index];
    1374       42609 :                 ics->swb_offset = ff_swb_offset_1024[sampling_index];
    1375             :             }
    1376       42995 :             ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
    1377             :         }
    1378       61784 :         if (aot != AOT_ER_AAC_ELD) {
    1379       45419 :             ics->predictor_present     = get_bits1(gb);
    1380       45419 :             ics->predictor_reset_group = 0;
    1381             :         }
    1382       61784 :         if (ics->predictor_present) {
    1383        1963 :             if (aot == AOT_AAC_MAIN) {
    1384        1425 :                 if (decode_prediction(ac, ics, gb)) {
    1385           0 :                     goto fail;
    1386             :                 }
    1387         538 :             } else if (aot == AOT_AAC_LC ||
    1388             :                        aot == AOT_ER_AAC_LC) {
    1389           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    1390             :                        "Prediction is not allowed in AAC-LC.\n");
    1391           0 :                 goto fail;
    1392             :             } else {
    1393         538 :                 if (aot == AOT_ER_AAC_LD) {
    1394           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1395             :                            "LTP in ER AAC LD not yet implemented.\n");
    1396           0 :                     ret_fail = AVERROR_PATCHWELCOME;
    1397           0 :                     goto fail;
    1398             :                 }
    1399         538 :                 if ((ics->ltp.present = get_bits(gb, 1)))
    1400         382 :                     decode_ltp(&ics->ltp, gb, ics->max_sfb);
    1401             :             }
    1402             :         }
    1403             :     }
    1404             : 
    1405       63112 :     if (ics->max_sfb > ics->num_swb) {
    1406           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1407             :                "Number of scalefactor bands in group (%d) "
    1408             :                "exceeds limit (%d).\n",
    1409           0 :                ics->max_sfb, ics->num_swb);
    1410           0 :         goto fail;
    1411             :     }
    1412             : 
    1413       63112 :     return 0;
    1414           0 : fail:
    1415           0 :     ics->max_sfb = 0;
    1416           0 :     return ret_fail;
    1417             : }
    1418             : 
    1419             : /**
    1420             :  * Decode band types (section_data payload); reference: table 4.46.
    1421             :  *
    1422             :  * @param   band_type           array of the used band type
    1423             :  * @param   band_type_run_end   array of the last scalefactor band of a band type run
    1424             :  *
    1425             :  * @return  Returns error status. 0 - OK, !0 - error
    1426             :  */
    1427       93569 : static int decode_band_types(AACContext *ac, enum BandType band_type[120],
    1428             :                              int band_type_run_end[120], GetBitContext *gb,
    1429             :                              IndividualChannelStream *ics)
    1430             : {
    1431       93569 :     int g, idx = 0;
    1432       93569 :     const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
    1433      192376 :     for (g = 0; g < ics->num_window_groups; g++) {
    1434       98807 :         int k = 0;
    1435      659525 :         while (k < ics->max_sfb) {
    1436      461911 :             uint8_t sect_end = k;
    1437             :             int sect_len_incr;
    1438      461911 :             int sect_band_type = get_bits(gb, 4);
    1439      461911 :             if (sect_band_type == 12) {
    1440           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
    1441           0 :                 return AVERROR_INVALIDDATA;
    1442             :             }
    1443             :             do {
    1444      480641 :                 sect_len_incr = get_bits(gb, bits);
    1445      480641 :                 sect_end += sect_len_incr;
    1446      480641 :                 if (get_bits_left(gb) < 0) {
    1447           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
    1448           0 :                     return AVERROR_INVALIDDATA;
    1449             :                 }
    1450      480641 :                 if (sect_end > ics->max_sfb) {
    1451           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1452             :                            "Number of bands (%d) exceeds limit (%d).\n",
    1453           0 :                            sect_end, ics->max_sfb);
    1454           0 :                     return AVERROR_INVALIDDATA;
    1455             :                 }
    1456      480641 :             } while (sect_len_incr == (1 << bits) - 1);
    1457     3925553 :             for (; k < sect_end; k++) {
    1458     3463642 :                 band_type        [idx]   = sect_band_type;
    1459     3463642 :                 band_type_run_end[idx++] = sect_end;
    1460             :             }
    1461             :         }
    1462             :     }
    1463       93569 :     return 0;
    1464             : }
    1465             : 
    1466             : /**
    1467             :  * Decode scalefactors; reference: table 4.47.
    1468             :  *
    1469             :  * @param   global_gain         first scalefactor value as scalefactors are differentially coded
    1470             :  * @param   band_type           array of the used band type
    1471             :  * @param   band_type_run_end   array of the last scalefactor band of a band type run
    1472             :  * @param   sf                  array of scalefactors or intensity stereo positions
    1473             :  *
    1474             :  * @return  Returns error status. 0 - OK, !0 - error
    1475             :  */
    1476       93569 : static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
    1477             :                                unsigned int global_gain,
    1478             :                                IndividualChannelStream *ics,
    1479             :                                enum BandType band_type[120],
    1480             :                                int band_type_run_end[120])
    1481             : {
    1482       93569 :     int g, i, idx = 0;
    1483       93569 :     int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
    1484             :     int clipped_offset;
    1485       93569 :     int noise_flag = 1;
    1486      192376 :     for (g = 0; g < ics->num_window_groups; g++) {
    1487      659525 :         for (i = 0; i < ics->max_sfb;) {
    1488      461911 :             int run_end = band_type_run_end[idx];
    1489      461911 :             if (band_type[idx] == ZERO_BT) {
    1490      597603 :                 for (; i < run_end; i++, idx++)
    1491      534619 :                     sf[idx] = FIXR(0.);
    1492      797970 :             } else if ((band_type[idx] == INTENSITY_BT) ||
    1493      389553 :                        (band_type[idx] == INTENSITY_BT2)) {
    1494       75268 :                 for (; i < run_end; i++, idx++) {
    1495       65778 :                     offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1496       65778 :                     clipped_offset = av_clip(offset[2], -155, 100);
    1497       65778 :                     if (offset[2] != clipped_offset) {
    1498           0 :                         avpriv_request_sample(ac->avctx,
    1499             :                                               "If you heard an audible artifact, there may be a bug in the decoder. "
    1500             :                                               "Clipped intensity stereo position (%d -> %d)",
    1501             :                                               offset[2], clipped_offset);
    1502             :                     }
    1503             : #if USE_FIXED
    1504       17856 :                     sf[idx] = 100 - clipped_offset;
    1505             : #else
    1506       47922 :                     sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
    1507             : #endif /* USE_FIXED */
    1508             :                 }
    1509      389437 :             } else if (band_type[idx] == NOISE_BT) {
    1510      289566 :                 for (; i < run_end; i++, idx++) {
    1511      281905 :                     if (noise_flag-- > 0)
    1512        6716 :                         offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
    1513             :                     else
    1514      275189 :                         offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1515      281905 :                     clipped_offset = av_clip(offset[1], -100, 155);
    1516      281905 :                     if (offset[1] != clipped_offset) {
    1517           0 :                         avpriv_request_sample(ac->avctx,
    1518             :                                               "If you heard an audible artifact, there may be a bug in the decoder. "
    1519             :                                               "Clipped noise gain (%d -> %d)",
    1520             :                                               offset[1], clipped_offset);
    1521             :                     }
    1522             : #if USE_FIXED
    1523      137931 :                     sf[idx] = -(100 + clipped_offset);
    1524             : #else
    1525      143974 :                     sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
    1526             : #endif /* USE_FIXED */
    1527             :                 }
    1528             :             } else {
    1529     2963116 :                 for (; i < run_end; i++, idx++) {
    1530     2581340 :                     offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1531     2581340 :                     if (offset[0] > 255U) {
    1532           0 :                         av_log(ac->avctx, AV_LOG_ERROR,
    1533             :                                "Scalefactor (%d) out of range.\n", offset[0]);
    1534           0 :                         return AVERROR_INVALIDDATA;
    1535             :                     }
    1536             : #if USE_FIXED
    1537      772587 :                     sf[idx] = -offset[0];
    1538             : #else
    1539     1808753 :                     sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
    1540             : #endif /* USE_FIXED */
    1541             :                 }
    1542             :             }
    1543             :         }
    1544             :     }
    1545       93569 :     return 0;
    1546             : }
    1547             : 
    1548             : /**
    1549             :  * Decode pulse data; reference: table 4.7.
    1550             :  */
    1551        1098 : static int decode_pulses(Pulse *pulse, GetBitContext *gb,
    1552             :                          const uint16_t *swb_offset, int num_swb)
    1553             : {
    1554             :     int i, pulse_swb;
    1555        1098 :     pulse->num_pulse = get_bits(gb, 2) + 1;
    1556        1098 :     pulse_swb        = get_bits(gb, 6);
    1557        1098 :     if (pulse_swb >= num_swb)
    1558           0 :         return -1;
    1559        1098 :     pulse->pos[0]    = swb_offset[pulse_swb];
    1560        1098 :     pulse->pos[0]   += get_bits(gb, 5);
    1561        1098 :     if (pulse->pos[0] >= swb_offset[num_swb])
    1562           0 :         return -1;
    1563        1098 :     pulse->amp[0]    = get_bits(gb, 4);
    1564        2744 :     for (i = 1; i < pulse->num_pulse; i++) {
    1565        1646 :         pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
    1566        1646 :         if (pulse->pos[i] >= swb_offset[num_swb])
    1567           0 :             return -1;
    1568        1646 :         pulse->amp[i] = get_bits(gb, 4);
    1569             :     }
    1570        1098 :     return 0;
    1571             : }
    1572             : 
    1573             : /**
    1574             :  * Decode Temporal Noise Shaping data; reference: table 4.48.
    1575             :  *
    1576             :  * @return  Returns error status. 0 - OK, !0 - error
    1577             :  */
    1578        6927 : static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
    1579             :                       GetBitContext *gb, const IndividualChannelStream *ics)
    1580             : {
    1581             :     int w, filt, i, coef_len, coef_res, coef_compress;
    1582        6927 :     const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
    1583        6927 :     const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
    1584       18467 :     for (w = 0; w < ics->num_windows; w++) {
    1585       11540 :         if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
    1586        7918 :             coef_res = get_bits1(gb);
    1587             : 
    1588       17297 :             for (filt = 0; filt < tns->n_filt[w]; filt++) {
    1589             :                 int tmp2_idx;
    1590        9379 :                 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
    1591             : 
    1592        9379 :                 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
    1593           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1594             :                            "TNS filter order %d is greater than maximum %d.\n",
    1595             :                            tns->order[w][filt], tns_max_order);
    1596           0 :                     tns->order[w][filt] = 0;
    1597           0 :                     return AVERROR_INVALIDDATA;
    1598             :                 }
    1599        9379 :                 if (tns->order[w][filt]) {
    1600        8859 :                     tns->direction[w][filt] = get_bits1(gb);
    1601        8859 :                     coef_compress = get_bits1(gb);
    1602        8859 :                     coef_len = coef_res + 3 - coef_compress;
    1603        8859 :                     tmp2_idx = 2 * coef_compress + coef_res;
    1604             : 
    1605       67167 :                     for (i = 0; i < tns->order[w][filt]; i++)
    1606       58308 :                         tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
    1607             :                 }
    1608             :             }
    1609             :         }
    1610             :     }
    1611        6927 :     return 0;
    1612             : }
    1613             : 
    1614             : /**
    1615             :  * Decode Mid/Side data; reference: table 4.54.
    1616             :  *
    1617             :  * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
    1618             :  *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
    1619             :  *                      [3] reserved for scalable AAC
    1620             :  */
    1621       26980 : static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
    1622             :                                    int ms_present)
    1623             : {
    1624             :     int idx;
    1625       26980 :     int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
    1626       26980 :     if (ms_present == 1) {
    1627      510726 :         for (idx = 0; idx < max_idx; idx++)
    1628      498034 :             cpe->ms_mask[idx] = get_bits1(gb);
    1629       14288 :     } else if (ms_present == 2) {
    1630       14288 :         memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
    1631             :     }
    1632       26980 : }
    1633             : 
    1634             : /**
    1635             :  * Decode spectral data; reference: table 4.50.
    1636             :  * Dequantize and scale spectral data; reference: 4.6.3.3.
    1637             :  *
    1638             :  * @param   coef            array of dequantized, scaled spectral data
    1639             :  * @param   sf              array of scalefactors or intensity stereo positions
    1640             :  * @param   pulse_present   set if pulses are present
    1641             :  * @param   pulse           pointer to pulse data struct
    1642             :  * @param   band_type       array of the used band type
    1643             :  *
    1644             :  * @return  Returns error status. 0 - OK, !0 - error
    1645             :  */
    1646       93569 : static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
    1647             :                                        GetBitContext *gb, const INTFLOAT sf[120],
    1648             :                                        int pulse_present, const Pulse *pulse,
    1649             :                                        const IndividualChannelStream *ics,
    1650             :                                        enum BandType band_type[120])
    1651             : {
    1652       93569 :     int i, k, g, idx = 0;
    1653       93569 :     const int c = 1024 / ics->num_windows;
    1654       93569 :     const uint16_t *offsets = ics->swb_offset;
    1655       93569 :     INTFLOAT *coef_base = coef;
    1656             : 
    1657      201740 :     for (g = 0; g < ics->num_windows; g++)
    1658      108171 :         memset(coef + g * 128 + offsets[ics->max_sfb], 0,
    1659      108171 :                sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
    1660             : 
    1661      192376 :     for (g = 0; g < ics->num_window_groups; g++) {
    1662       98807 :         unsigned g_len = ics->group_len[g];
    1663             : 
    1664     3562449 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    1665     3463642 :             const unsigned cbt_m1 = band_type[idx] - 1;
    1666     3463642 :             INTFLOAT *cfo = coef + offsets[i];
    1667     3463642 :             int off_len = offsets[i + 1] - offsets[i];
    1668             :             int group;
    1669             : 
    1670     3463642 :             if (cbt_m1 >= INTENSITY_BT2 - 1) {
    1671     1238838 :                 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1672      638441 :                     memset(cfo, 0, off_len * sizeof(*cfo));
    1673             :                 }
    1674     2863245 :             } else if (cbt_m1 == NOISE_BT - 1) {
    1675      563829 :                 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1676             : #if !USE_FIXED
    1677             :                     float scale;
    1678             : #endif /* !USE_FIXED */
    1679             :                     INTFLOAT band_energy;
    1680             : 
    1681     6230728 :                     for (k = 0; k < off_len; k++) {
    1682     5948804 :                         ac->random_state  = lcg_random(ac->random_state);
    1683             : #if USE_FIXED
    1684     2883476 :                         cfo[k] = ac->random_state >> 3;
    1685             : #else
    1686     3065328 :                         cfo[k] = ac->random_state;
    1687             : #endif /* USE_FIXED */
    1688             :                     }
    1689             : 
    1690             : #if USE_FIXED
    1691      137931 :                     band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
    1692      137931 :                     band_energy = fixed_sqrt(band_energy, 31);
    1693      137931 :                     noise_scale(cfo, sf[idx], band_energy, off_len);
    1694             : #else
    1695      143993 :                     band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
    1696      143993 :                     scale = sf[idx] / sqrtf(band_energy);
    1697      143993 :                     ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
    1698             : #endif /* USE_FIXED */
    1699             :                 }
    1700             :             } else {
    1701             : #if !USE_FIXED
    1702     1808753 :                 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
    1703             : #endif /* !USE_FIXED */
    1704     2581340 :                 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
    1705     2581340 :                 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
    1706     2581340 :                 OPEN_READER(re, gb);
    1707             : 
    1708     2581340 :                 switch (cbt_m1 >> 1) {
    1709      622847 :                 case 0:
    1710     1273031 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1711      650184 :                         INTFLOAT *cf = cfo;
    1712      650184 :                         int len = off_len;
    1713             : 
    1714             :                         do {
    1715             :                             int code;
    1716             :                             unsigned cb_idx;
    1717             : 
    1718     3234892 :                             UPDATE_CACHE(re, gb);
    1719     3234892 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1720     3234892 :                             cb_idx = cb_vector_idx[code];
    1721             : #if USE_FIXED
    1722      978157 :                             cf = DEC_SQUAD(cf, cb_idx);
    1723             : #else
    1724     2256735 :                             cf = VMUL4(cf, vq, cb_idx, sf + idx);
    1725             : #endif /* USE_FIXED */
    1726     3234892 :                         } while (len -= 4);
    1727             :                     }
    1728      622847 :                     break;
    1729             : 
    1730      577957 :                 case 1:
    1731     1168422 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1732      590465 :                         INTFLOAT *cf = cfo;
    1733      590465 :                         int len = off_len;
    1734             : 
    1735             :                         do {
    1736             :                             int code;
    1737             :                             unsigned nnz;
    1738             :                             unsigned cb_idx;
    1739             :                             uint32_t bits;
    1740             : 
    1741     2638266 :                             UPDATE_CACHE(re, gb);
    1742     2638266 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1743     2638266 :                             cb_idx = cb_vector_idx[code];
    1744     2638266 :                             nnz = cb_idx >> 8 & 15;
    1745     2638266 :                             bits = nnz ? GET_CACHE(re, gb) : 0;
    1746     2638266 :                             LAST_SKIP_BITS(re, gb, nnz);
    1747             : #if USE_FIXED
    1748      816691 :                             cf = DEC_UQUAD(cf, cb_idx, bits);
    1749             : #else
    1750     1821575 :                             cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
    1751             : #endif /* USE_FIXED */
    1752     2638266 :                         } while (len -= 4);
    1753             :                     }
    1754      577957 :                     break;
    1755             : 
    1756      401051 :                 case 2:
    1757      814793 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1758      413742 :                         INTFLOAT *cf = cfo;
    1759      413742 :                         int len = off_len;
    1760             : 
    1761             :                         do {
    1762             :                             int code;
    1763             :                             unsigned cb_idx;
    1764             : 
    1765     2508696 :                             UPDATE_CACHE(re, gb);
    1766     2508696 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1767     2508696 :                             cb_idx = cb_vector_idx[code];
    1768             : #if USE_FIXED
    1769      672272 :                             cf = DEC_SPAIR(cf, cb_idx);
    1770             : #else
    1771     1836424 :                             cf = VMUL2(cf, vq, cb_idx, sf + idx);
    1772             : #endif /* USE_FIXED */
    1773     2508696 :                         } while (len -= 2);
    1774             :                     }
    1775      401051 :                     break;
    1776             : 
    1777      542878 :                 case 3:
    1778             :                 case 4:
    1779     1104196 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1780      561318 :                         INTFLOAT *cf = cfo;
    1781      561318 :                         int len = off_len;
    1782             : 
    1783             :                         do {
    1784             :                             int code;
    1785             :                             unsigned nnz;
    1786             :                             unsigned cb_idx;
    1787             :                             unsigned sign;
    1788             : 
    1789     2966498 :                             UPDATE_CACHE(re, gb);
    1790     2966498 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1791     2966498 :                             cb_idx = cb_vector_idx[code];
    1792     2966498 :                             nnz = cb_idx >> 8 & 15;
    1793     2966498 :                             sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
    1794     2966498 :                             LAST_SKIP_BITS(re, gb, nnz);
    1795             : #if USE_FIXED
    1796      703742 :                             cf = DEC_UPAIR(cf, cb_idx, sign);
    1797             : #else
    1798     2262756 :                             cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
    1799             : #endif /* USE_FIXED */
    1800     2966498 :                         } while (len -= 2);
    1801             :                     }
    1802      542878 :                     break;
    1803             : 
    1804      436607 :                 default:
    1805      883063 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1806             : #if USE_FIXED
    1807      119275 :                         int *icf = cfo;
    1808             :                         int v;
    1809             : #else
    1810      327181 :                         float *cf = cfo;
    1811      327181 :                         uint32_t *icf = (uint32_t *) cf;
    1812             : #endif /* USE_FIXED */
    1813      446456 :                         int len = off_len;
    1814             : 
    1815             :                         do {
    1816             :                             int code;
    1817             :                             unsigned nzt, nnz;
    1818             :                             unsigned cb_idx;
    1819             :                             uint32_t bits;
    1820             :                             int j;
    1821             : 
    1822     1473268 :                             UPDATE_CACHE(re, gb);
    1823     1473268 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1824             : 
    1825     1473268 :                             if (!code) {
    1826       68905 :                                 *icf++ = 0;
    1827       68905 :                                 *icf++ = 0;
    1828       68905 :                                 continue;
    1829             :                             }
    1830             : 
    1831     1404363 :                             cb_idx = cb_vector_idx[code];
    1832     1404363 :                             nnz = cb_idx >> 12;
    1833     1404363 :                             nzt = cb_idx >> 8;
    1834     1404363 :                             bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
    1835     1404363 :                             LAST_SKIP_BITS(re, gb, nnz);
    1836             : 
    1837     4213089 :                             for (j = 0; j < 2; j++) {
    1838     2808726 :                                 if (nzt & 1<<j) {
    1839             :                                     uint32_t b;
    1840             :                                     int n;
    1841             :                                     /* The total length of escape_sequence must be < 22 bits according
    1842             :                                        to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
    1843      366465 :                                     UPDATE_CACHE(re, gb);
    1844      366465 :                                     b = GET_CACHE(re, gb);
    1845      366465 :                                     b = 31 - av_log2(~b);
    1846             : 
    1847      366465 :                                     if (b > 8) {
    1848           0 :                                         av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
    1849           0 :                                         return AVERROR_INVALIDDATA;
    1850             :                                     }
    1851             : 
    1852      366465 :                                     SKIP_BITS(re, gb, b + 1);
    1853      366465 :                                     b += 4;
    1854      366465 :                                     n = (1 << b) + SHOW_UBITS(re, gb, b);
    1855      366465 :                                     LAST_SKIP_BITS(re, gb, b);
    1856             : #if USE_FIXED
    1857       64951 :                                     v = n;
    1858       64951 :                                     if (bits & 1U<<31)
    1859       32336 :                                         v = -v;
    1860       64951 :                                     *icf++ = v;
    1861             : #else
    1862      301514 :                                     *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
    1863             : #endif /* USE_FIXED */
    1864      366465 :                                     bits <<= 1;
    1865             :                                 } else {
    1866             : #if USE_FIXED
    1867      539259 :                                     v = cb_idx & 15;
    1868      539259 :                                     if (bits & 1U<<31)
    1869      256298 :                                         v = -v;
    1870      539259 :                                     *icf++ = v;
    1871             : #else
    1872     1903002 :                                     unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
    1873     1903002 :                                     *icf++ = (bits & 1U<<31) | v;
    1874             : #endif /* USE_FIXED */
    1875     2442261 :                                     bits <<= !!v;
    1876             :                                 }
    1877     2808726 :                                 cb_idx >>= 4;
    1878             :                             }
    1879     1473268 :                         } while (len -= 2);
    1880             : #if !USE_FIXED
    1881      327181 :                         ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
    1882             : #endif /* !USE_FIXED */
    1883             :                     }
    1884             :                 }
    1885             : 
    1886     2581340 :                 CLOSE_READER(re, gb);
    1887             :             }
    1888             :         }
    1889       98807 :         coef += g_len << 7;
    1890             :     }
    1891             : 
    1892       93569 :     if (pulse_present) {
    1893        1098 :         idx = 0;
    1894        3842 :         for (i = 0; i < pulse->num_pulse; i++) {
    1895        2744 :             INTFLOAT co = coef_base[ pulse->pos[i] ];
    1896        9584 :             while (offsets[idx + 1] <= pulse->pos[i])
    1897        4096 :                 idx++;
    1898        2744 :             if (band_type[idx] != NOISE_BT && sf[idx]) {
    1899        2668 :                 INTFLOAT ico = -pulse->amp[i];
    1900             : #if USE_FIXED
    1901         864 :                 if (co) {
    1902         730 :                     ico = co + (co > 0 ? -ico : ico);
    1903             :                 }
    1904         864 :                 coef_base[ pulse->pos[i] ] = ico;
    1905             : #else
    1906        1804 :                 if (co) {
    1907        1564 :                     co /= sf[idx];
    1908        1564 :                     ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
    1909             :                 }
    1910        1804 :                 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
    1911             : #endif /* USE_FIXED */
    1912             :             }
    1913             :         }
    1914             :     }
    1915             : #if USE_FIXED
    1916       28710 :     coef = coef_base;
    1917       28710 :     idx = 0;
    1918       59752 :     for (g = 0; g < ics->num_window_groups; g++) {
    1919       31042 :         unsigned g_len = ics->group_len[g];
    1920             : 
    1921     1071091 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    1922     1040049 :             const unsigned cbt_m1 = band_type[idx] - 1;
    1923     1040049 :             int *cfo = coef + offsets[i];
    1924     1040049 :             int off_len = offsets[i + 1] - offsets[i];
    1925             :             int group;
    1926             : 
    1927     1040049 :             if (cbt_m1 < NOISE_BT - 1) {
    1928     1560445 :                 for (group = 0; group < (int)g_len; group++, cfo+=128) {
    1929      787858 :                     ac->vector_pow43(cfo, off_len);
    1930      787858 :                     ac->subband_scale(cfo, cfo, sf[idx], 34, off_len);
    1931             :                 }
    1932             :             }
    1933             :         }
    1934       31042 :         coef += g_len << 7;
    1935             :     }
    1936             : #endif /* USE_FIXED */
    1937       93569 :     return 0;
    1938             : }
    1939             : 
    1940             : /**
    1941             :  * Apply AAC-Main style frequency domain prediction.
    1942             :  */
    1943        7388 : static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
    1944             : {
    1945             :     int sfb, k;
    1946             : 
    1947        7388 :     if (!sce->ics.predictor_initialized) {
    1948          20 :         reset_all_predictors(sce->predictor_state);
    1949          20 :         sce->ics.predictor_initialized = 1;
    1950             :     }
    1951             : 
    1952        7388 :     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    1953      269934 :         for (sfb = 0;
    1954      262566 :              sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
    1955      255198 :              sfb++) {
    1956     4558332 :             for (k = sce->ics.swb_offset[sfb];
    1957     4303134 :                  k < sce->ics.swb_offset[sfb + 1];
    1958     4047936 :                  k++) {
    1959     4940928 :                 predict(&sce->predictor_state[k], &sce->coeffs[k],
    1960     4047936 :                         sce->ics.predictor_present &&
    1961      892992 :                         sce->ics.prediction_used[sfb]);
    1962             :             }
    1963             :         }
    1964        7368 :         if (sce->ics.predictor_reset_group)
    1965         516 :             reset_predictor_group(sce->predictor_state,
    1966             :                                   sce->ics.predictor_reset_group);
    1967             :     } else
    1968          20 :         reset_all_predictors(sce->predictor_state);
    1969        7388 : }
    1970             : 
    1971           0 : static void decode_gain_control(SingleChannelElement * sce, GetBitContext * gb)
    1972             : {
    1973             :     // wd_num, wd_test, aloc_size
    1974             :     static const uint8_t gain_mode[4][3] = {
    1975             :         {1, 0, 5},  // ONLY_LONG_SEQUENCE = 0,
    1976             :         {2, 1, 2},  // LONG_START_SEQUENCE,
    1977             :         {8, 0, 2},  // EIGHT_SHORT_SEQUENCE,
    1978             :         {2, 1, 5},  // LONG_STOP_SEQUENCE
    1979             :     };
    1980             : 
    1981           0 :     const int mode = sce->ics.window_sequence[0];
    1982             :     uint8_t bd, wd, ad;
    1983             : 
    1984             :     // FIXME: Store the gain control data on |sce| and do something with it.
    1985           0 :     uint8_t max_band = get_bits(gb, 2);
    1986           0 :     for (bd = 0; bd < max_band; bd++) {
    1987           0 :         for (wd = 0; wd < gain_mode[mode][0]; wd++) {
    1988           0 :             uint8_t adjust_num = get_bits(gb, 3);
    1989           0 :             for (ad = 0; ad < adjust_num; ad++) {
    1990           0 :                 skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
    1991             :                                      ? 4
    1992           0 :                                      : gain_mode[mode][2]));
    1993             :             }
    1994             :         }
    1995             :     }
    1996           0 : }
    1997             : 
    1998             : /**
    1999             :  * Decode an individual_channel_stream payload; reference: table 4.44.
    2000             :  *
    2001             :  * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
    2002             :  * @param   scale_flag      scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
    2003             :  *
    2004             :  * @return  Returns error status. 0 - OK, !0 - error
    2005             :  */
    2006       93569 : static int decode_ics(AACContext *ac, SingleChannelElement *sce,
    2007             :                       GetBitContext *gb, int common_window, int scale_flag)
    2008             : {
    2009             :     Pulse pulse;
    2010       93569 :     TemporalNoiseShaping    *tns = &sce->tns;
    2011       93569 :     IndividualChannelStream *ics = &sce->ics;
    2012       93569 :     INTFLOAT *out = sce->coeffs;
    2013       93569 :     int global_gain, eld_syntax, er_syntax, pulse_present = 0;
    2014             :     int ret;
    2015             : 
    2016       93569 :     eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    2017      280707 :     er_syntax  = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
    2018      187138 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
    2019      277071 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
    2020       89933 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    2021             : 
    2022             :     /* This assignment is to silence a GCC warning about the variable being used
    2023             :      * uninitialized when in fact it always is.
    2024             :      */
    2025       93569 :     pulse.num_pulse = 0;
    2026             : 
    2027       93569 :     global_gain = get_bits(gb, 8);
    2028             : 
    2029       93569 :     if (!common_window && !scale_flag) {
    2030       32655 :         ret = decode_ics_info(ac, ics, gb);
    2031       32655 :         if (ret < 0)
    2032           0 :             goto fail;
    2033             :     }
    2034             : 
    2035       93569 :     if ((ret = decode_band_types(ac, sce->band_type,
    2036       93569 :                                  sce->band_type_run_end, gb, ics)) < 0)
    2037           0 :         goto fail;
    2038       93569 :     if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
    2039       93569 :                                   sce->band_type, sce->band_type_run_end)) < 0)
    2040           0 :         goto fail;
    2041             : 
    2042       93569 :     pulse_present = 0;
    2043       93569 :     if (!scale_flag) {
    2044       93569 :         if (!eld_syntax && (pulse_present = get_bits1(gb))) {
    2045        1098 :             if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2046           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    2047             :                        "Pulse tool not allowed in eight short sequence.\n");
    2048           0 :                 ret = AVERROR_INVALIDDATA;
    2049           0 :                 goto fail;
    2050             :             }
    2051        1098 :             if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
    2052           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    2053             :                        "Pulse data corrupt or invalid.\n");
    2054           0 :                 ret = AVERROR_INVALIDDATA;
    2055           0 :                 goto fail;
    2056             :             }
    2057             :         }
    2058       93569 :         tns->present = get_bits1(gb);
    2059       93569 :         if (tns->present && !er_syntax) {
    2060        5663 :             ret = decode_tns(ac, tns, gb, ics);
    2061        5663 :             if (ret < 0)
    2062           0 :                 goto fail;
    2063             :         }
    2064       93569 :         if (!eld_syntax && get_bits1(gb)) {
    2065           0 :             decode_gain_control(sce, gb);
    2066           0 :             if (!ac->warned_gain_control) {
    2067           0 :                 avpriv_report_missing_feature(ac->avctx, "Gain control");
    2068           0 :                 ac->warned_gain_control = 1;
    2069             :             }
    2070             :         }
    2071             :         // I see no textual basis in the spec for this occurring after SSR gain
    2072             :         // control, but this is what both reference and real implmentations do
    2073       93569 :         if (tns->present && er_syntax) {
    2074        1264 :             ret = decode_tns(ac, tns, gb, ics);
    2075        1264 :             if (ret < 0)
    2076           0 :                 goto fail;
    2077             :         }
    2078             :     }
    2079             : 
    2080       93569 :     ret = decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
    2081       93569 :                                     &pulse, ics, sce->band_type);
    2082       93569 :     if (ret < 0)
    2083           0 :         goto fail;
    2084             : 
    2085       93569 :     if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
    2086        6094 :         apply_prediction(ac, sce);
    2087             : 
    2088       93569 :     return 0;
    2089           0 : fail:
    2090           0 :     tns->present = 0;
    2091           0 :     return ret;
    2092             : }
    2093             : 
    2094             : /**
    2095             :  * Mid/Side stereo decoding; reference: 4.6.8.1.3.
    2096             :  */
    2097       26980 : static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
    2098             : {
    2099       26980 :     const IndividualChannelStream *ics = &cpe->ch[0].ics;
    2100       26980 :     INTFLOAT *ch0 = cpe->ch[0].coeffs;
    2101       26980 :     INTFLOAT *ch1 = cpe->ch[1].coeffs;
    2102       26980 :     int g, i, group, idx = 0;
    2103       26980 :     const uint16_t *offsets = ics->swb_offset;
    2104       55663 :     for (g = 0; g < ics->num_window_groups; g++) {
    2105     1041956 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    2106     1807516 :             if (cpe->ms_mask[idx] &&
    2107     1588486 :                 cpe->ch[0].band_type[idx] < NOISE_BT &&
    2108      794243 :                 cpe->ch[1].band_type[idx] < NOISE_BT) {
    2109             : #if USE_FIXED
    2110      500617 :                 for (group = 0; group < ics->group_len[g]; group++) {
    2111      759474 :                     ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
    2112      253158 :                                                 ch1 + group * 128 + offsets[i],
    2113      253158 :                                                 offsets[i+1] - offsets[i]);
    2114             : #else
    2115     1113172 :                 for (group = 0; group < ics->group_len[g]; group++) {
    2116     1706490 :                     ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
    2117      568830 :                                                ch1 + group * 128 + offsets[i],
    2118      568830 :                                                offsets[i+1] - offsets[i]);
    2119             : #endif /* USE_FIXED */
    2120             :                 }
    2121             :             }
    2122             :         }
    2123       28683 :         ch0 += ics->group_len[g] * 128;
    2124       28683 :         ch1 += ics->group_len[g] * 128;
    2125             :     }
    2126       26980 : }
    2127             : 
    2128             : /**
    2129             :  * intensity stereo decoding; reference: 4.6.8.2.3
    2130             :  *
    2131             :  * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
    2132             :  *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
    2133             :  *                      [3] reserved for scalable AAC
    2134             :  */
    2135       30679 : static void apply_intensity_stereo(AACContext *ac,
    2136             :                                    ChannelElement *cpe, int ms_present)
    2137             : {
    2138       30679 :     const IndividualChannelStream *ics = &cpe->ch[1].ics;
    2139       30679 :     SingleChannelElement         *sce1 = &cpe->ch[1];
    2140       30679 :     INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
    2141       30679 :     const uint16_t *offsets = ics->swb_offset;
    2142       30679 :     int g, group, i, idx = 0;
    2143             :     int c;
    2144             :     INTFLOAT scale;
    2145       63286 :     for (g = 0; g < ics->num_window_groups; g++) {
    2146      225724 :         for (i = 0; i < ics->max_sfb;) {
    2147      311646 :             if (sce1->band_type[idx] == INTENSITY_BT ||
    2148      160626 :                 sce1->band_type[idx] == INTENSITY_BT2) {
    2149        9490 :                 const int bt_run_end = sce1->band_type_run_end[idx];
    2150       75268 :                 for (; i < bt_run_end; i++, idx++) {
    2151       65778 :                     c = -1 + 2 * (sce1->band_type[idx] - 14);
    2152       65778 :                     if (ms_present)
    2153       61107 :                         c *= 1 - 2 * cpe->ms_mask[idx];
    2154       65778 :                     scale = c * sce1->sf[idx];
    2155      131873 :                     for (group = 0; group < ics->group_len[g]; group++)
    2156             : #if USE_FIXED
    2157       53865 :                         ac->subband_scale(coef1 + group * 128 + offsets[i],
    2158       17955 :                                       coef0 + group * 128 + offsets[i],
    2159             :                                       scale,
    2160             :                                       23,
    2161       17955 :                                       offsets[i + 1] - offsets[i]);
    2162             : #else
    2163      144420 :                         ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
    2164       48140 :                                                     coef0 + group * 128 + offsets[i],
    2165             :                                                     scale,
    2166       48140 :                                                     offsets[i + 1] - offsets[i]);
    2167             : #endif /* USE_FIXED */
    2168             :                 }
    2169             :             } else {
    2170      151020 :                 int bt_run_end = sce1->band_type_run_end[idx];
    2171      151020 :                 idx += bt_run_end - i;
    2172      151020 :                 i    = bt_run_end;
    2173             :             }
    2174             :         }
    2175       32607 :         coef0 += ics->group_len[g] * 128;
    2176       32607 :         coef1 += ics->group_len[g] * 128;
    2177             :     }
    2178       30679 : }
    2179             : 
    2180             : /**
    2181             :  * Decode a channel_pair_element; reference: table 4.4.
    2182             :  *
    2183             :  * @return  Returns error status. 0 - OK, !0 - error
    2184             :  */
    2185       30679 : static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
    2186             : {
    2187       30679 :     int i, ret, common_window, ms_present = 0;
    2188       30679 :     int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    2189             : 
    2190       30679 :     common_window = eld_syntax || get_bits1(gb);
    2191       30679 :     if (common_window) {
    2192       30457 :         if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
    2193           0 :             return AVERROR_INVALIDDATA;
    2194       30457 :         i = cpe->ch[1].ics.use_kb_window[0];
    2195       30457 :         cpe->ch[1].ics = cpe->ch[0].ics;
    2196       30457 :         cpe->ch[1].ics.use_kb_window[1] = i;
    2197       31181 :         if (cpe->ch[1].ics.predictor_present &&
    2198         724 :             (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
    2199         538 :             if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
    2200         404 :                 decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
    2201       30457 :         ms_present = get_bits(gb, 2);
    2202       30457 :         if (ms_present == 3) {
    2203           0 :             av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
    2204           0 :             return AVERROR_INVALIDDATA;
    2205       30457 :         } else if (ms_present)
    2206       26980 :             decode_mid_side_stereo(cpe, gb, ms_present);
    2207             :     }
    2208       30679 :     if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
    2209           0 :         return ret;
    2210       30679 :     if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
    2211           0 :         return ret;
    2212             : 
    2213       30679 :     if (common_window) {
    2214       30457 :         if (ms_present)
    2215       26980 :             apply_mid_side_stereo(ac, cpe);
    2216       30457 :         if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
    2217         647 :             apply_prediction(ac, &cpe->ch[0]);
    2218         647 :             apply_prediction(ac, &cpe->ch[1]);
    2219             :         }
    2220             :     }
    2221             : 
    2222       30679 :     apply_intensity_stereo(ac, cpe, ms_present);
    2223       30679 :     return 0;
    2224             : }
    2225             : 
    2226             : static const float cce_scale[] = {
    2227             :     1.09050773266525765921, //2^(1/8)
    2228             :     1.18920711500272106672, //2^(1/4)
    2229             :     M_SQRT2,
    2230             :     2,
    2231             : };
    2232             : 
    2233             : /**
    2234             :  * Decode coupling_channel_element; reference: table 4.8.
    2235             :  *
    2236             :  * @return  Returns error status. 0 - OK, !0 - error
    2237             :  */
    2238        1174 : static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
    2239             : {
    2240        1174 :     int num_gain = 0;
    2241             :     int c, g, sfb, ret;
    2242             :     int sign;
    2243             :     INTFLOAT scale;
    2244        1174 :     SingleChannelElement *sce = &che->ch[0];
    2245        1174 :     ChannelCoupling     *coup = &che->coup;
    2246             : 
    2247        1174 :     coup->coupling_point = 2 * get_bits1(gb);
    2248        1174 :     coup->num_coupled = get_bits(gb, 3);
    2249        3915 :     for (c = 0; c <= coup->num_coupled; c++) {
    2250        2741 :         num_gain++;
    2251        2741 :         coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
    2252        2741 :         coup->id_select[c] = get_bits(gb, 4);
    2253        2741 :         if (coup->type[c] == TYPE_CPE) {
    2254        2348 :             coup->ch_select[c] = get_bits(gb, 2);
    2255        2348 :             if (coup->ch_select[c] == 3)
    2256        1967 :                 num_gain++;
    2257             :         } else
    2258         393 :             coup->ch_select[c] = 2;
    2259             :     }
    2260        1174 :     coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
    2261             : 
    2262        1174 :     sign  = get_bits(gb, 1);
    2263             : #if USE_FIXED
    2264         130 :     scale = get_bits(gb, 2);
    2265             : #else
    2266        1044 :     scale = cce_scale[get_bits(gb, 2)];
    2267             : #endif
    2268             : 
    2269        1174 :     if ((ret = decode_ics(ac, sce, gb, 0, 0)))
    2270           0 :         return ret;
    2271             : 
    2272        5882 :     for (c = 0; c < num_gain; c++) {
    2273        4708 :         int idx  = 0;
    2274        4708 :         int cge  = 1;
    2275        4708 :         int gain = 0;
    2276        4708 :         INTFLOAT gain_cache = FIXR10(1.);
    2277        4708 :         if (c) {
    2278        3534 :             cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
    2279        3534 :             gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
    2280        3534 :             gain_cache = GET_GAIN(scale, gain);
    2281             : #if USE_FIXED
    2282         520 :             if ((abs(gain_cache)-1024) >> 3 > 30)
    2283           0 :                 return AVERROR(ERANGE);
    2284             : #endif
    2285             :         }
    2286        4708 :         if (coup->coupling_point == AFTER_IMDCT) {
    2287        1965 :             coup->gain[c][0] = gain_cache;
    2288             :         } else {
    2289        5486 :             for (g = 0; g < sce->ics.num_window_groups; g++) {
    2290      112463 :                 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
    2291      109720 :                     if (sce->band_type[idx] != ZERO_BT) {
    2292        5196 :                         if (!cge) {
    2293        2879 :                             int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    2294        2879 :                             if (t) {
    2295        2724 :                                 int s = 1;
    2296        2724 :                                 t = gain += t;
    2297        2724 :                                 if (sign) {
    2298        1666 :                                     s  -= 2 * (t & 0x1);
    2299        1666 :                                     t >>= 1;
    2300             :                                 }
    2301        2724 :                                 gain_cache = GET_GAIN(scale, t) * s;
    2302             : #if USE_FIXED
    2303           0 :                                 if ((abs(gain_cache)-1024) >> 3 > 30)
    2304           0 :                                     return AVERROR(ERANGE);
    2305             : #endif
    2306             :                             }
    2307             :                         }
    2308        5196 :                         coup->gain[c][idx] = gain_cache;
    2309             :                     }
    2310             :                 }
    2311             :             }
    2312             :         }
    2313             :     }
    2314        1174 :     return 0;
    2315             : }
    2316             : 
    2317             : /**
    2318             :  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
    2319             :  *
    2320             :  * @return  Returns number of bytes consumed.
    2321             :  */
    2322           4 : static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
    2323             :                                          GetBitContext *gb)
    2324             : {
    2325             :     int i;
    2326           4 :     int num_excl_chan = 0;
    2327             : 
    2328             :     do {
    2329          32 :         for (i = 0; i < 7; i++)
    2330          28 :             che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
    2331           4 :     } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
    2332             : 
    2333           4 :     return num_excl_chan / 7;
    2334             : }
    2335             : 
    2336             : /**
    2337             :  * Decode dynamic range information; reference: table 4.52.
    2338             :  *
    2339             :  * @return  Returns number of bytes consumed.
    2340             :  */
    2341         880 : static int decode_dynamic_range(DynamicRangeControl *che_drc,
    2342             :                                 GetBitContext *gb)
    2343             : {
    2344         880 :     int n             = 1;
    2345         880 :     int drc_num_bands = 1;
    2346             :     int i;
    2347             : 
    2348             :     /* pce_tag_present? */
    2349         880 :     if (get_bits1(gb)) {
    2350           0 :         che_drc->pce_instance_tag  = get_bits(gb, 4);
    2351           0 :         skip_bits(gb, 4); // tag_reserved_bits
    2352           0 :         n++;
    2353             :     }
    2354             : 
    2355             :     /* excluded_chns_present? */
    2356         880 :     if (get_bits1(gb)) {
    2357           4 :         n += decode_drc_channel_exclusions(che_drc, gb);
    2358             :     }
    2359             : 
    2360             :     /* drc_bands_present? */
    2361         880 :     if (get_bits1(gb)) {
    2362          35 :         che_drc->band_incr            = get_bits(gb, 4);
    2363          35 :         che_drc->interpolation_scheme = get_bits(gb, 4);
    2364          35 :         n++;
    2365          35 :         drc_num_bands += che_drc->band_incr;
    2366         140 :         for (i = 0; i < drc_num_bands; i++) {
    2367         105 :             che_drc->band_top[i] = get_bits(gb, 8);
    2368         105 :             n++;
    2369             :         }
    2370             :     }
    2371             : 
    2372             :     /* prog_ref_level_present? */
    2373         880 :     if (get_bits1(gb)) {
    2374         878 :         che_drc->prog_ref_level = get_bits(gb, 7);
    2375         878 :         skip_bits1(gb); // prog_ref_level_reserved_bits
    2376         878 :         n++;
    2377             :     }
    2378             : 
    2379        1830 :     for (i = 0; i < drc_num_bands; i++) {
    2380         950 :         che_drc->dyn_rng_sgn[i] = get_bits1(gb);
    2381         950 :         che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
    2382         950 :         n++;
    2383             :     }
    2384             : 
    2385         880 :     return n;
    2386             : }
    2387             : 
    2388        8965 : static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
    2389             :     uint8_t buf[256];
    2390             :     int i, major, minor;
    2391             : 
    2392        8965 :     if (len < 13+7*8)
    2393         380 :         goto unknown;
    2394             : 
    2395        8585 :     get_bits(gb, 13); len -= 13;
    2396             : 
    2397     1136630 :     for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
    2398     1128045 :         buf[i] = get_bits(gb, 8);
    2399             : 
    2400        8585 :     buf[i] = 0;
    2401        8585 :     if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
    2402           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
    2403             : 
    2404        8585 :     if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
    2405          19 :         ac->avctx->internal->skip_samples = 1024;
    2406             :     }
    2407             : 
    2408       17531 : unknown:
    2409        8965 :     skip_bits_long(gb, len);
    2410             : 
    2411        8965 :     return 0;
    2412             : }
    2413             : 
    2414             : /**
    2415             :  * Decode extension data (incomplete); reference: table 4.51.
    2416             :  *
    2417             :  * @param   cnt length of TYPE_FIL syntactic element in bytes
    2418             :  *
    2419             :  * @return Returns number of bytes consumed
    2420             :  */
    2421       23696 : static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
    2422             :                                     ChannelElement *che, enum RawDataBlockType elem_type)
    2423             : {
    2424       23696 :     int crc_flag = 0;
    2425       23696 :     int res = cnt;
    2426       23696 :     int type = get_bits(gb, 4);
    2427             : 
    2428       23696 :     if (ac->avctx->debug & FF_DEBUG_STARTCODE)
    2429           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
    2430             : 
    2431       23696 :     switch (type) { // extension type
    2432           0 :     case EXT_SBR_DATA_CRC:
    2433           0 :         crc_flag++;
    2434       10901 :     case EXT_SBR_DATA:
    2435       10901 :         if (!che) {
    2436           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
    2437           0 :             return res;
    2438       10901 :         } else if (ac->oc[1].m4ac.frame_length_short) {
    2439           0 :             if (!ac->warned_960_sbr)
    2440           0 :               avpriv_report_missing_feature(ac->avctx,
    2441             :                                             "SBR with 960 frame length");
    2442           0 :             ac->warned_960_sbr = 1;
    2443           0 :             skip_bits_long(gb, 8 * cnt - 4);
    2444           0 :             return res;
    2445       10901 :         } else if (!ac->oc[1].m4ac.sbr) {
    2446           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
    2447           0 :             skip_bits_long(gb, 8 * cnt - 4);
    2448           0 :             return res;
    2449       10901 :         } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
    2450           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
    2451           0 :             skip_bits_long(gb, 8 * cnt - 4);
    2452           0 :             return res;
    2453       10901 :         } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
    2454          12 :             ac->oc[1].m4ac.sbr = 1;
    2455          12 :             ac->oc[1].m4ac.ps = 1;
    2456          12 :             ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
    2457          12 :             output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
    2458             :                              ac->oc[1].status, 1);
    2459             :         } else {
    2460       10889 :             ac->oc[1].m4ac.sbr = 1;
    2461       10889 :             ac->avctx->profile = FF_PROFILE_AAC_HE;
    2462             :         }
    2463       10901 :         res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
    2464       10901 :         break;
    2465         880 :     case EXT_DYNAMIC_RANGE:
    2466         880 :         res = decode_dynamic_range(&ac->che_drc, gb);
    2467         880 :         break;
    2468        8965 :     case EXT_FILL:
    2469        8965 :         decode_fill(ac, gb, 8 * cnt - 4);
    2470        8965 :         break;
    2471        2950 :     case EXT_FILL_DATA:
    2472             :     case EXT_DATA_ELEMENT:
    2473             :     default:
    2474        2950 :         skip_bits_long(gb, 8 * cnt - 4);
    2475        2950 :         break;
    2476             :     };
    2477       23696 :     return res;
    2478             : }
    2479             : 
    2480             : /**
    2481             :  * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
    2482             :  *
    2483             :  * @param   decode  1 if tool is used normally, 0 if tool is used in LTP.
    2484             :  * @param   coef    spectral coefficients
    2485             :  */
    2486        6933 : static void apply_tns(INTFLOAT coef_param[1024], TemporalNoiseShaping *tns,
    2487             :                       IndividualChannelStream *ics, int decode)
    2488             : {
    2489        6933 :     const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
    2490             :     int w, filt, m, i;
    2491             :     int bottom, top, order, start, end, size, inc;
    2492             :     INTFLOAT lpc[TNS_MAX_ORDER];
    2493             :     INTFLOAT tmp[TNS_MAX_ORDER+1];
    2494        6933 :     UINTFLOAT *coef = coef_param;
    2495             : 
    2496       18479 :     for (w = 0; w < ics->num_windows; w++) {
    2497       11546 :         bottom = ics->num_swb;
    2498       20931 :         for (filt = 0; filt < tns->n_filt[w]; filt++) {
    2499        9385 :             top    = bottom;
    2500        9385 :             bottom = FFMAX(0, top - tns->length[w][filt]);
    2501        9385 :             order  = tns->order[w][filt];
    2502        9385 :             if (order == 0)
    2503         520 :                 continue;
    2504             : 
    2505             :             // tns_decode_coef
    2506        8865 :             AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
    2507             : 
    2508        8865 :             start = ics->swb_offset[FFMIN(bottom, mmm)];
    2509        8865 :             end   = ics->swb_offset[FFMIN(   top, mmm)];
    2510        8865 :             if ((size = end - start) <= 0)
    2511          14 :                 continue;
    2512        8851 :             if (tns->direction[w][filt]) {
    2513        3586 :                 inc = -1;
    2514        3586 :                 start = end - 1;
    2515             :             } else {
    2516        5265 :                 inc = 1;
    2517             :             }
    2518        8851 :             start += w * 128;
    2519             : 
    2520        8851 :             if (decode) {
    2521             :                 // ar filter
    2522     2738921 :                 for (m = 0; m < size; m++, start += inc)
    2523    23883725 :                     for (i = 1; i <= FFMIN(m, order); i++)
    2524    21153649 :                         coef[start] -= AAC_MUL26((INTFLOAT)coef[start - i * inc], lpc[i - 1]);
    2525             :             } else {
    2526             :                 // ma filter
    2527        3702 :                 for (m = 0; m < size; m++, start += inc) {
    2528        3696 :                     tmp[0] = coef[start];
    2529       39112 :                     for (i = 1; i <= FFMIN(m, order); i++)
    2530       35416 :                         coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
    2531       39424 :                     for (i = order; i > 0; i--)
    2532       35728 :                         tmp[i] = tmp[i - 1];
    2533             :                 }
    2534             :             }
    2535             :         }
    2536             :     }
    2537        6933 : }
    2538             : 
    2539             : /**
    2540             :  *  Apply windowing and MDCT to obtain the spectral
    2541             :  *  coefficient from the predicted sample by LTP.
    2542             :  */
    2543         786 : static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
    2544             :                                    INTFLOAT *in, IndividualChannelStream *ics)
    2545             : {
    2546         786 :     const INTFLOAT *lwindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2547         786 :     const INTFLOAT *swindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2548         786 :     const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2549         786 :     const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2550             : 
    2551         786 :     if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
    2552         784 :         ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
    2553             :     } else {
    2554           2 :         memset(in, 0, 448 * sizeof(*in));
    2555           2 :         ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
    2556             :     }
    2557         786 :     if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
    2558         786 :         ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
    2559             :     } else {
    2560           0 :         ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
    2561           0 :         memset(in + 1024 + 576, 0, 448 * sizeof(*in));
    2562             :     }
    2563         786 :     ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
    2564         786 : }
    2565             : 
    2566             : /**
    2567             :  * Apply the long term prediction
    2568             :  */
    2569         786 : static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
    2570             : {
    2571         786 :     const LongTermPrediction *ltp = &sce->ics.ltp;
    2572         786 :     const uint16_t *offsets = sce->ics.swb_offset;
    2573             :     int i, sfb;
    2574             : 
    2575         786 :     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    2576         786 :         INTFLOAT *predTime = sce->ret;
    2577         786 :         INTFLOAT *predFreq = ac->buf_mdct;
    2578         786 :         int16_t num_samples = 2048;
    2579             : 
    2580         786 :         if (ltp->lag < 1024)
    2581           0 :             num_samples = ltp->lag + 1024;
    2582     1610514 :         for (i = 0; i < num_samples; i++)
    2583     1609728 :             predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
    2584         786 :         memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
    2585             : 
    2586         786 :         ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
    2587             : 
    2588         786 :         if (sce->tns.present)
    2589           6 :             ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
    2590             : 
    2591       32226 :         for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
    2592       31440 :             if (ltp->used[sfb])
    2593      370412 :                 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
    2594      347544 :                     sce->coeffs[i] += (UINTFLOAT)predFreq[i];
    2595             :     }
    2596         786 : }
    2597             : 
    2598             : /**
    2599             :  * Update the LTP buffer for next frame
    2600             :  */
    2601        3568 : static void update_ltp(AACContext *ac, SingleChannelElement *sce)
    2602             : {
    2603        3568 :     IndividualChannelStream *ics = &sce->ics;
    2604        3568 :     INTFLOAT *saved     = sce->saved;
    2605        3568 :     INTFLOAT *saved_ltp = sce->coeffs;
    2606        3568 :     const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2607        3568 :     const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2608             :     int i;
    2609             : 
    2610        3568 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2611           4 :         memcpy(saved_ltp,       saved, 512 * sizeof(*saved_ltp));
    2612           4 :         memset(saved_ltp + 576, 0,     448 * sizeof(*saved_ltp));
    2613           4 :         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
    2614             : 
    2615         260 :         for (i = 0; i < 64; i++)
    2616         256 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
    2617        3564 :     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    2618           4 :         memcpy(saved_ltp,       ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
    2619           4 :         memset(saved_ltp + 576, 0,                  448 * sizeof(*saved_ltp));
    2620           4 :         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
    2621             : 
    2622         260 :         for (i = 0; i < 64; i++)
    2623         256 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
    2624             :     } else { // LONG_STOP or ONLY_LONG
    2625        3560 :         ac->fdsp->vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     &lwindow[512],     512);
    2626             : 
    2627     1826280 :         for (i = 0; i < 512; i++)
    2628     1822720 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
    2629             :     }
    2630             : 
    2631        3568 :     memcpy(sce->ltp_state,      sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
    2632        3568 :     memcpy(sce->ltp_state+1024, sce->ret,            1024 * sizeof(*sce->ltp_state));
    2633        3568 :     memcpy(sce->ltp_state+2048, saved_ltp,           1024 * sizeof(*sce->ltp_state));
    2634        3568 : }
    2635             : 
    2636             : /**
    2637             :  * Conduct IMDCT and windowing.
    2638             :  */
    2639       62167 : static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
    2640             : {
    2641       62167 :     IndividualChannelStream *ics = &sce->ics;
    2642       62167 :     INTFLOAT *in    = sce->coeffs;
    2643       62167 :     INTFLOAT *out   = sce->ret;
    2644       62167 :     INTFLOAT *saved = sce->saved;
    2645       62167 :     const INTFLOAT *swindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2646       62167 :     const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2647       62167 :     const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2648       62167 :     INTFLOAT *buf  = ac->buf_mdct;
    2649       62167 :     INTFLOAT *temp = ac->temp;
    2650             :     int i;
    2651             : 
    2652             :     // imdct
    2653       62167 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2654       18765 :         for (i = 0; i < 1024; i += 128)
    2655       16680 :             ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
    2656             :     } else {
    2657       60082 :         ac->mdct.imdct_half(&ac->mdct, buf, in);
    2658             : #if USE_FIXED
    2659    16815125 :         for (i=0; i<1024; i++)
    2660    16798720 :           buf[i] = (buf[i] + 4) >> 3;
    2661             : #endif /* USE_FIXED */
    2662             :     }
    2663             : 
    2664             :     /* window overlapping
    2665             :      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
    2666             :      * and long to short transitions are considered to be short to short
    2667             :      * transitions. This leaves just two cases (long to long and short to short)
    2668             :      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
    2669             :      */
    2670      121170 :     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
    2671       60208 :             (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
    2672       59001 :         ac->fdsp->vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 512);
    2673             :     } else {
    2674        3166 :         memcpy(                         out,               saved,            448 * sizeof(*out));
    2675             : 
    2676        3166 :         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2677        2085 :             ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, 64);
    2678        2085 :             ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      64);
    2679        2085 :             ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      64);
    2680        2085 :             ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      64);
    2681        2085 :             ac->fdsp->vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      64);
    2682        2085 :             memcpy(                     out + 448 + 4*128, temp, 64 * sizeof(*out));
    2683             :         } else {
    2684        1081 :             ac->fdsp->vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, 64);
    2685        1081 :             memcpy(                     out + 576,         buf + 64,         448 * sizeof(*out));
    2686             :         }
    2687             :     }
    2688             : 
    2689             :     // buffer update
    2690       62167 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2691        2085 :         memcpy(                     saved,       temp + 64,         64 * sizeof(*saved));
    2692        2085 :         ac->fdsp->vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 64);
    2693        2085 :         ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
    2694        2085 :         ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
    2695        2085 :         memcpy(                     saved + 448, buf + 7*128 + 64,  64 * sizeof(*saved));
    2696       60082 :     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    2697        1203 :         memcpy(                     saved,       buf + 512,        448 * sizeof(*saved));
    2698        1203 :         memcpy(                     saved + 448, buf + 7*128 + 64,  64 * sizeof(*saved));
    2699             :     } else { // LONG_STOP or ONLY_LONG
    2700       58879 :         memcpy(                     saved,       buf + 512,        512 * sizeof(*saved));
    2701             :     }
    2702       62167 : }
    2703             : 
    2704             : /**
    2705             :  * Conduct IMDCT and windowing.
    2706             :  */
    2707         387 : static void imdct_and_windowing_960(AACContext *ac, SingleChannelElement *sce)
    2708             : {
    2709             : #if !USE_FIXED
    2710         387 :     IndividualChannelStream *ics = &sce->ics;
    2711         387 :     INTFLOAT *in    = sce->coeffs;
    2712         387 :     INTFLOAT *out   = sce->ret;
    2713         387 :     INTFLOAT *saved = sce->saved;
    2714         387 :     const INTFLOAT *swindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_120) : AAC_RENAME(ff_sine_120);
    2715         387 :     const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_960) : AAC_RENAME(ff_sine_960);
    2716         387 :     const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_120) : AAC_RENAME(ff_sine_120);
    2717         387 :     INTFLOAT *buf  = ac->buf_mdct;
    2718         387 :     INTFLOAT *temp = ac->temp;
    2719             :     int i;
    2720             : 
    2721             :     // imdct
    2722         387 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2723           9 :         for (i = 0; i < 8; i++)
    2724           8 :             ac->mdct120->imdct_half(ac->mdct120, buf + i * 120, in + i * 128, 1);
    2725             :     } else {
    2726         386 :         ac->mdct960->imdct_half(ac->mdct960, buf, in, 1);
    2727             :     }
    2728             : 
    2729             :     /* window overlapping
    2730             :      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
    2731             :      * and long to short transitions are considered to be short to short
    2732             :      * transitions. This leaves just two cases (long to long and short to short)
    2733             :      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
    2734             :      */
    2735             : 
    2736         772 :     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
    2737         387 :         (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
    2738         385 :         ac->fdsp->vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 480);
    2739             :     } else {
    2740           2 :         memcpy(                          out,               saved,            420 * sizeof(*out));
    2741             : 
    2742           2 :         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2743           1 :             ac->fdsp->vector_fmul_window(out + 420 + 0*120, saved + 420,      buf + 0*120, swindow_prev, 60);
    2744           1 :             ac->fdsp->vector_fmul_window(out + 420 + 1*120, buf + 0*120 + 60, buf + 1*120, swindow,      60);
    2745           1 :             ac->fdsp->vector_fmul_window(out + 420 + 2*120, buf + 1*120 + 60, buf + 2*120, swindow,      60);
    2746           1 :             ac->fdsp->vector_fmul_window(out + 420 + 3*120, buf + 2*120 + 60, buf + 3*120, swindow,      60);
    2747           1 :             ac->fdsp->vector_fmul_window(temp,              buf + 3*120 + 60, buf + 4*120, swindow,      60);
    2748           1 :             memcpy(                      out + 420 + 4*120, temp, 60 * sizeof(*out));
    2749             :         } else {
    2750           1 :             ac->fdsp->vector_fmul_window(out + 420,         saved + 420,      buf,         swindow_prev, 60);
    2751           1 :             memcpy(                      out + 540,         buf + 60,         420 * sizeof(*out));
    2752             :         }
    2753             :     }
    2754             : 
    2755             :     // buffer update
    2756         387 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2757           1 :         memcpy(                      saved,       temp + 60,         60 * sizeof(*saved));
    2758           1 :         ac->fdsp->vector_fmul_window(saved + 60,  buf + 4*120 + 60, buf + 5*120, swindow, 60);
    2759           1 :         ac->fdsp->vector_fmul_window(saved + 180, buf + 5*120 + 60, buf + 6*120, swindow, 60);
    2760           1 :         ac->fdsp->vector_fmul_window(saved + 300, buf + 6*120 + 60, buf + 7*120, swindow, 60);
    2761           1 :         memcpy(                      saved + 420, buf + 7*120 + 60,  60 * sizeof(*saved));
    2762         386 :     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    2763           2 :         memcpy(                      saved,       buf + 480,        420 * sizeof(*saved));
    2764           2 :         memcpy(                      saved + 420, buf + 7*120 + 60,  60 * sizeof(*saved));
    2765             :     } else { // LONG_STOP or ONLY_LONG
    2766         384 :         memcpy(                      saved,       buf + 480,        480 * sizeof(*saved));
    2767             :     }
    2768             : #endif
    2769         387 : }
    2770        3636 : static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
    2771             : {
    2772        3636 :     IndividualChannelStream *ics = &sce->ics;
    2773        3636 :     INTFLOAT *in    = sce->coeffs;
    2774        3636 :     INTFLOAT *out   = sce->ret;
    2775        3636 :     INTFLOAT *saved = sce->saved;
    2776        3636 :     INTFLOAT *buf  = ac->buf_mdct;
    2777             : #if USE_FIXED
    2778             :     int i;
    2779             : #endif /* USE_FIXED */
    2780             : 
    2781             :     // imdct
    2782        3636 :     ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
    2783             : 
    2784             : #if USE_FIXED
    2785     1857300 :     for (i = 0; i < 1024; i++)
    2786     1855488 :         buf[i] = (buf[i] + 2) >> 2;
    2787             : #endif /* USE_FIXED */
    2788             : 
    2789             :     // window overlapping
    2790        3636 :     if (ics->use_kb_window[1]) {
    2791             :         // AAC LD uses a low overlap sine window instead of a KBD window
    2792          62 :         memcpy(out, saved, 192 * sizeof(*out));
    2793          62 :         ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME(ff_sine_128), 64);
    2794          62 :         memcpy(                     out + 320, buf + 64, 192 * sizeof(*out));
    2795             :     } else {
    2796        3574 :         ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME(ff_sine_512), 256);
    2797             :     }
    2798             : 
    2799             :     // buffer update
    2800        3636 :     memcpy(saved, buf + 256, 256 * sizeof(*saved));
    2801        3636 : }
    2802             : 
    2803       26598 : static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
    2804             : {
    2805       26598 :     INTFLOAT *in    = sce->coeffs;
    2806       26598 :     INTFLOAT *out   = sce->ret;
    2807       26598 :     INTFLOAT *saved = sce->saved;
    2808       26598 :     INTFLOAT *buf  = ac->buf_mdct;
    2809             :     int i;
    2810       26598 :     const int n  = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
    2811       26598 :     const int n2 = n >> 1;
    2812       26598 :     const int n4 = n >> 2;
    2813       26598 :     const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
    2814             :                                            AAC_RENAME(ff_aac_eld_window_512);
    2815             : 
    2816             :     // Inverse transform, mapped to the conventional IMDCT by
    2817             :     // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
    2818             :     // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
    2819             :     // International Conference on Audio, Language and Image Processing, ICALIP 2008.
    2820             :     // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
    2821     3374198 :     for (i = 0; i < n2; i+=2) {
    2822             :         INTFLOAT temp;
    2823     3347600 :         temp =  in[i    ]; in[i    ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
    2824     3347600 :         temp = -in[i + 1]; in[i + 1] =  in[n - 2 - i]; in[n - 2 - i] = temp;
    2825             :     }
    2826             : #if !USE_FIXED
    2827       16861 :     if (n == 480)
    2828        7118 :         ac->mdct480->imdct_half(ac->mdct480, buf, in, 1);
    2829             :     else
    2830             : #endif
    2831       19480 :         ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
    2832             : 
    2833             : #if USE_FIXED
    2834     9980425 :     for (i = 0; i < 1024; i++)
    2835     9970688 :       buf[i] = (buf[i] + 1) >> 1;
    2836             : #endif /* USE_FIXED */
    2837             : 
    2838     6721798 :     for (i = 0; i < n; i+=2) {
    2839     6695200 :         buf[i] = -buf[i];
    2840             :     }
    2841             :     // Like with the regular IMDCT at this point we still have the middle half
    2842             :     // of a transform but with even symmetry on the left and odd symmetry on
    2843             :     // the right
    2844             : 
    2845             :     // window overlapping
    2846             :     // The spec says to use samples [0..511] but the reference decoder uses
    2847             :     // samples [128..639].
    2848     3374198 :     for (i = n4; i < n2; i ++) {
    2849    13390400 :         out[i - n4] = AAC_MUL31(   buf[    n2 - 1 - i] , window[i       - n4]) +
    2850     6695200 :                       AAC_MUL31( saved[        i + n2] , window[i +   n - n4]) +
    2851     6695200 :                       AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
    2852     3347600 :                       AAC_MUL31(-saved[  2*n + n2 + i] , window[i + 3*n - n4]);
    2853             :     }
    2854     6721798 :     for (i = 0; i < n2; i ++) {
    2855    26780800 :         out[n4 + i] = AAC_MUL31(   buf[              i] , window[i + n2       - n4]) +
    2856    13390400 :                       AAC_MUL31(-saved[      n - 1 - i] , window[i + n2 +   n - n4]) +
    2857    13390400 :                       AAC_MUL31(-saved[          n + i] , window[i + n2 + 2*n - n4]) +
    2858     6695200 :                       AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
    2859             :     }
    2860     3374198 :     for (i = 0; i < n4; i ++) {
    2861    13390400 :         out[n2 + n4 + i] = AAC_MUL31(   buf[    i + n2] , window[i +   n - n4]) +
    2862     6695200 :                            AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
    2863     3347600 :                            AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
    2864             :     }
    2865             : 
    2866             :     // buffer update
    2867       26598 :     memmove(saved + n, saved, 2 * n * sizeof(*saved));
    2868       26598 :     memcpy( saved,       buf,     n * sizeof(*saved));
    2869       26598 : }
    2870             : 
    2871             : /**
    2872             :  * channel coupling transformation interface
    2873             :  *
    2874             :  * @param   apply_coupling_method   pointer to (in)dependent coupling function
    2875             :  */
    2876      175810 : static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
    2877             :                                    enum RawDataBlockType type, int elem_id,
    2878             :                                    enum CouplingPoint coupling_point,
    2879             :                                    void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
    2880             : {
    2881             :     int i, c;
    2882             : 
    2883     2988770 :     for (i = 0; i < MAX_ELEM_ID; i++) {
    2884     2812960 :         ChannelElement *cce = ac->che[TYPE_CCE][i];
    2885     2812960 :         int index = 0;
    2886             : 
    2887     2812960 :         if (cce && cce->coup.coupling_point == coupling_point) {
    2888        3915 :             ChannelCoupling *coup = &cce->coup;
    2889             : 
    2890       13317 :             for (c = 0; c <= coup->num_coupled; c++) {
    2891        9402 :                 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
    2892        2741 :                     if (coup->ch_select[c] != 1) {
    2893        2561 :                         apply_coupling_method(ac, &cc->ch[0], cce, index);
    2894        2561 :                         if (coup->ch_select[c] != 0)
    2895        2561 :                             index++;
    2896             :                     }
    2897        5482 :                     if (coup->ch_select[c] != 2)
    2898        2147 :                         apply_coupling_method(ac, &cc->ch[1], cce, index++);
    2899             :                 } else
    2900        6661 :                     index += 1 + (coup->ch_select[c] == 3);
    2901             :             }
    2902             :         }
    2903             :     }
    2904      175810 : }
    2905             : 
    2906             : /**
    2907             :  * Convert spectral data to samples, applying all supported tools as appropriate.
    2908             :  */
    2909       49844 : static void spectral_to_sample(AACContext *ac, int samples)
    2910             : {
    2911             :     int i, type;
    2912             :     void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
    2913       49844 :     switch (ac->oc[1].m4ac.object_type) {
    2914         606 :     case AOT_ER_AAC_LD:
    2915         606 :         imdct_and_window = imdct_and_windowing_ld;
    2916         606 :         break;
    2917       16365 :     case AOT_ER_AAC_ELD:
    2918       16365 :         imdct_and_window = imdct_and_windowing_eld;
    2919       16365 :         break;
    2920       32873 :     default:
    2921       32873 :         if (ac->oc[1].m4ac.frame_length_short)
    2922         387 :             imdct_and_window = imdct_and_windowing_960;
    2923             :         else
    2924       32486 :             imdct_and_window = ac->imdct_and_windowing;
    2925             :     }
    2926      249220 :     for (type = 3; type >= 0; type--) {
    2927     3389392 :         for (i = 0; i < MAX_ELEM_ID; i++) {
    2928     3190016 :             ChannelElement *che = ac->che[type][i];
    2929     3190016 :             if (che && che->present) {
    2930       62890 :                 if (type <= TYPE_CPE)
    2931       58212 :                     apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
    2932       62890 :                 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
    2933        1784 :                     if (che->ch[0].ics.predictor_present) {
    2934         538 :                         if (che->ch[0].ics.ltp.present)
    2935         382 :                             ac->apply_ltp(ac, &che->ch[0]);
    2936         538 :                         if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
    2937         404 :                             ac->apply_ltp(ac, &che->ch[1]);
    2938             :                     }
    2939             :                 }
    2940       62890 :                 if (che->ch[0].tns.present)
    2941        4633 :                     ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
    2942       62890 :                 if (che->ch[1].tns.present)
    2943        2294 :                     ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
    2944       62890 :                 if (type <= TYPE_CPE)
    2945       58212 :                     apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
    2946       62890 :                 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
    2947       62109 :                     imdct_and_window(ac, &che->ch[0]);
    2948       62109 :                     if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    2949        1784 :                         ac->update_ltp(ac, &che->ch[0]);
    2950       62109 :                     if (type == TYPE_CPE) {
    2951       30679 :                         imdct_and_window(ac, &che->ch[1]);
    2952       30679 :                         if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    2953        1784 :                             ac->update_ltp(ac, &che->ch[1]);
    2954             :                     }
    2955       62109 :                     if (ac->oc[1].m4ac.sbr > 0) {
    2956       12411 :                         AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
    2957             :                     }
    2958             :                 }
    2959       62890 :                 if (type <= TYPE_CCE)
    2960       59386 :                     apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
    2961             : 
    2962             : #if USE_FIXED
    2963             :                 {
    2964             :                     int j;
    2965             :                     /* preparation for resampler */
    2966    19746147 :                     for(j = 0; j<samples; j++){
    2967    19726848 :                         che->ch[0].ret[j] = (int32_t)av_clip64((int64_t)che->ch[0].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
    2968    19726848 :                         if(type == TYPE_CPE)
    2969     9935872 :                             che->ch[1].ret[j] = (int32_t)av_clip64((int64_t)che->ch[1].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
    2970             :                     }
    2971             :                 }
    2972             : #endif /* USE_FIXED */
    2973       62890 :                 che->present = 0;
    2974     3127126 :             } else if (che) {
    2975         329 :                 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
    2976             :             }
    2977             :         }
    2978             :     }
    2979       49844 : }
    2980             : 
    2981        4112 : static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
    2982             : {
    2983             :     int size;
    2984             :     AACADTSHeaderInfo hdr_info;
    2985             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
    2986             :     int layout_map_tags, ret;
    2987             : 
    2988        4112 :     size = ff_adts_header_parse(gb, &hdr_info);
    2989        4112 :     if (size > 0) {
    2990        4112 :         if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
    2991             :             // This is 2 for "VLB " audio in NSV files.
    2992             :             // See samples/nsv/vlb_audio.
    2993           0 :             avpriv_report_missing_feature(ac->avctx,
    2994             :                                           "More than one AAC RDB per ADTS frame");
    2995           0 :             ac->warned_num_aac_frames = 1;
    2996             :         }
    2997        4112 :         push_output_configuration(ac);
    2998        4112 :         if (hdr_info.chan_config) {
    2999        4112 :             ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
    3000        4112 :             if ((ret = set_default_channel_config(ac->avctx,
    3001             :                                                   layout_map,
    3002             :                                                   &layout_map_tags,
    3003        4112 :                                                   hdr_info.chan_config)) < 0)
    3004           0 :                 return ret;
    3005        4112 :             if ((ret = output_configure(ac, layout_map, layout_map_tags,
    3006        4112 :                                         FFMAX(ac->oc[1].status,
    3007             :                                               OC_TRIAL_FRAME), 0)) < 0)
    3008           0 :                 return ret;
    3009             :         } else {
    3010           0 :             ac->oc[1].m4ac.chan_config = 0;
    3011             :             /**
    3012             :              * dual mono frames in Japanese DTV can have chan_config 0
    3013             :              * WITHOUT specifying PCE.
    3014             :              *  thus, set dual mono as default.
    3015             :              */
    3016           0 :             if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
    3017           0 :                 layout_map_tags = 2;
    3018           0 :                 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
    3019           0 :                 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
    3020           0 :                 layout_map[0][1] = 0;
    3021           0 :                 layout_map[1][1] = 1;
    3022           0 :                 if (output_configure(ac, layout_map, layout_map_tags,
    3023             :                                      OC_TRIAL_FRAME, 0))
    3024           0 :                     return -7;
    3025             :             }
    3026             :         }
    3027        4112 :         ac->oc[1].m4ac.sample_rate     = hdr_info.sample_rate;
    3028        4112 :         ac->oc[1].m4ac.sampling_index  = hdr_info.sampling_index;
    3029        4112 :         ac->oc[1].m4ac.object_type     = hdr_info.object_type;
    3030        4112 :         ac->oc[1].m4ac.frame_length_short = 0;
    3031        8170 :         if (ac->oc[0].status != OC_LOCKED ||
    3032        8116 :             ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
    3033        4058 :             ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
    3034          54 :             ac->oc[1].m4ac.sbr = -1;
    3035          54 :             ac->oc[1].m4ac.ps  = -1;
    3036             :         }
    3037        4112 :         if (!hdr_info.crc_absent)
    3038           1 :             skip_bits(gb, 16);
    3039             :     }
    3040        4112 :     return size;
    3041             : }
    3042             : 
    3043       16971 : static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
    3044             :                                int *got_frame_ptr, GetBitContext *gb)
    3045             : {
    3046       16971 :     AACContext *ac = avctx->priv_data;
    3047       16971 :     const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
    3048             :     ChannelElement *che;
    3049             :     int err, i;
    3050       16971 :     int samples = m4ac->frame_length_short ? 960 : 1024;
    3051       16971 :     int chan_config = m4ac->chan_config;
    3052       16971 :     int aot = m4ac->object_type;
    3053             : 
    3054       16971 :     if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
    3055       16971 :         samples >>= 1;
    3056             : 
    3057       16971 :     ac->frame = data;
    3058             : 
    3059       16971 :     if ((err = frame_configure_elements(avctx)) < 0)
    3060           0 :         return err;
    3061             : 
    3062             :     // The FF_PROFILE_AAC_* defines are all object_type - 1
    3063             :     // This may lead to an undefined profile being signaled
    3064       16971 :     ac->avctx->profile = aot - 1;
    3065             : 
    3066       16971 :     ac->tags_mapped = 0;
    3067             : 
    3068       16971 :     if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
    3069           0 :         avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
    3070             :                               chan_config);
    3071           0 :         return AVERROR_INVALIDDATA;
    3072             :     }
    3073       35760 :     for (i = 0; i < tags_per_config[chan_config]; i++) {
    3074       18789 :         const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
    3075       18789 :         const int elem_id   = aac_channel_layout_map[chan_config-1][i][1];
    3076       18789 :         if (!(che=get_che(ac, elem_type, elem_id))) {
    3077           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    3078             :                    "channel element %d.%d is not allocated\n",
    3079             :                    elem_type, elem_id);
    3080           0 :             return AVERROR_INVALIDDATA;
    3081             :         }
    3082       18789 :         che->present = 1;
    3083       18789 :         if (aot != AOT_ER_AAC_ELD)
    3084        2424 :             skip_bits(gb, 4);
    3085       18789 :         switch (elem_type) {
    3086        6738 :         case TYPE_SCE:
    3087        6738 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    3088        6738 :             break;
    3089       11445 :         case TYPE_CPE:
    3090       11445 :             err = decode_cpe(ac, gb, che);
    3091       11445 :             break;
    3092         606 :         case TYPE_LFE:
    3093         606 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    3094         606 :             break;
    3095             :         }
    3096       18789 :         if (err < 0)
    3097           0 :             return err;
    3098             :     }
    3099             : 
    3100       16971 :     spectral_to_sample(ac, samples);
    3101             : 
    3102       16971 :     if (!ac->frame->data[0] && samples) {
    3103           0 :         av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
    3104           0 :         return AVERROR_INVALIDDATA;
    3105             :     }
    3106             : 
    3107       16971 :     ac->frame->nb_samples = samples;
    3108       16971 :     ac->frame->sample_rate = avctx->sample_rate;
    3109       16971 :     *got_frame_ptr = 1;
    3110             : 
    3111       16971 :     skip_bits_long(gb, get_bits_left(gb));
    3112       16971 :     return 0;
    3113             : }
    3114             : 
    3115       32873 : static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
    3116             :                                 int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
    3117             : {
    3118       32873 :     AACContext *ac = avctx->priv_data;
    3119       32873 :     ChannelElement *che = NULL, *che_prev = NULL;
    3120       32873 :     enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
    3121             :     int err, elem_id;
    3122       32873 :     int samples = 0, multiplier, audio_found = 0, pce_found = 0;
    3123       32873 :     int is_dmono, sce_count = 0;
    3124             :     int payload_alignment;
    3125             : 
    3126       32873 :     ac->frame = data;
    3127             : 
    3128       32873 :     if (show_bits(gb, 12) == 0xfff) {
    3129        4112 :         if ((err = parse_adts_frame_header(ac, gb)) < 0) {
    3130           0 :             av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
    3131           0 :             goto fail;
    3132             :         }
    3133        4112 :         if (ac->oc[1].m4ac.sampling_index > 12) {
    3134           0 :             av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
    3135           0 :             err = AVERROR_INVALIDDATA;
    3136           0 :             goto fail;
    3137             :         }
    3138             :     }
    3139             : 
    3140       32873 :     if ((err = frame_configure_elements(avctx)) < 0)
    3141           0 :         goto fail;
    3142             : 
    3143             :     // The FF_PROFILE_AAC_* defines are all object_type - 1
    3144             :     // This may lead to an undefined profile being signaled
    3145       32873 :     ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
    3146             : 
    3147       32873 :     payload_alignment = get_bits_count(gb);
    3148       32873 :     ac->tags_mapped = 0;
    3149             :     // parse
    3150      137885 :     while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
    3151       72139 :         elem_id = get_bits(gb, 4);
    3152             : 
    3153       72139 :         if (avctx->debug & FF_DEBUG_STARTCODE)
    3154           0 :             av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
    3155             : 
    3156       72139 :         if (!avctx->channels && elem_type != TYPE_PCE) {
    3157           0 :             err = AVERROR_INVALIDDATA;
    3158           0 :             goto fail;
    3159             :         }
    3160             : 
    3161       72139 :         if (elem_type < TYPE_DSE) {
    3162       44101 :             if (!(che=get_che(ac, elem_type, elem_id))) {
    3163           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
    3164             :                        elem_type, elem_id);
    3165           0 :                 err = AVERROR_INVALIDDATA;
    3166           0 :                 goto fail;
    3167             :             }
    3168       44101 :             samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
    3169       44101 :             che->present = 1;
    3170             :         }
    3171             : 
    3172       72139 :         switch (elem_type) {
    3173             : 
    3174       20795 :         case TYPE_SCE:
    3175       20795 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    3176       20795 :             audio_found = 1;
    3177       20795 :             sce_count++;
    3178       20795 :             break;
    3179             : 
    3180       19234 :         case TYPE_CPE:
    3181       19234 :             err = decode_cpe(ac, gb, che);
    3182       19234 :             audio_found = 1;
    3183       19234 :             break;
    3184             : 
    3185        1174 :         case TYPE_CCE:
    3186        1174 :             err = decode_cce(ac, gb, che);
    3187        1174 :             break;
    3188             : 
    3189        2898 :         case TYPE_LFE:
    3190        2898 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    3191        2898 :             audio_found = 1;
    3192        2898 :             break;
    3193             : 
    3194        2821 :         case TYPE_DSE:
    3195        2821 :             err = skip_data_stream_element(ac, gb);
    3196        2821 :             break;
    3197             : 
    3198           0 :         case TYPE_PCE: {
    3199             :             uint8_t layout_map[MAX_ELEM_ID*4][3];
    3200             :             int tags;
    3201             : 
    3202           0 :             int pushed = push_output_configuration(ac);
    3203           0 :             if (pce_found && !pushed) {
    3204           0 :                 err = AVERROR_INVALIDDATA;
    3205           0 :                 goto fail;
    3206             :             }
    3207             : 
    3208           0 :             tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
    3209             :                               payload_alignment);
    3210           0 :             if (tags < 0) {
    3211           0 :                 err = tags;
    3212           0 :                 break;
    3213             :             }
    3214           0 :             if (pce_found) {
    3215           0 :                 av_log(avctx, AV_LOG_ERROR,
    3216             :                        "Not evaluating a further program_config_element as this construct is dubious at best.\n");
    3217           0 :                 pop_output_configuration(ac);
    3218             :             } else {
    3219           0 :                 err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
    3220           0 :                 if (!err)
    3221           0 :                     ac->oc[1].m4ac.chan_config = 0;
    3222           0 :                 pce_found = 1;
    3223             :             }
    3224           0 :             break;
    3225             :         }
    3226             : 
    3227       25217 :         case TYPE_FIL:
    3228       25217 :             if (elem_id == 15)
    3229       16453 :                 elem_id += get_bits(gb, 8) - 1;
    3230       25217 :             if (get_bits_left(gb) < 8 * elem_id) {
    3231           0 :                     av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
    3232           0 :                     err = AVERROR_INVALIDDATA;
    3233           0 :                     goto fail;
    3234             :             }
    3235       74130 :             while (elem_id > 0)
    3236       23696 :                 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
    3237       25217 :             err = 0; /* FIXME */
    3238       25217 :             break;
    3239             : 
    3240           0 :         default:
    3241           0 :             err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
    3242           0 :             break;
    3243             :         }
    3244             : 
    3245       72139 :         if (elem_type < TYPE_DSE) {
    3246       44101 :             che_prev      = che;
    3247       44101 :             che_prev_type = elem_type;
    3248             :         }
    3249             : 
    3250       72139 :         if (err)
    3251           0 :             goto fail;
    3252             : 
    3253       72139 :         if (get_bits_left(gb) < 3) {
    3254           0 :             av_log(avctx, AV_LOG_ERROR, overread_err);
    3255           0 :             err = AVERROR_INVALIDDATA;
    3256           0 :             goto fail;
    3257             :         }
    3258             :     }
    3259             : 
    3260       32873 :     if (!avctx->channels) {
    3261           0 :         *got_frame_ptr = 0;
    3262           0 :         return 0;
    3263             :     }
    3264             : 
    3265       32873 :     multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
    3266       32873 :     samples <<= multiplier;
    3267             : 
    3268       32873 :     spectral_to_sample(ac, samples);
    3269             : 
    3270       32873 :     if (ac->oc[1].status && audio_found) {
    3271       32873 :         avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
    3272       32873 :         avctx->frame_size = samples;
    3273       32873 :         ac->oc[1].status = OC_LOCKED;
    3274             :     }
    3275             : 
    3276       32873 :     if (multiplier)
    3277        5583 :         avctx->internal->skip_samples_multiplier = 2;
    3278             : 
    3279       32873 :     if (!ac->frame->data[0] && samples) {
    3280           0 :         av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
    3281           0 :         err = AVERROR_INVALIDDATA;
    3282           0 :         goto fail;
    3283             :     }
    3284             : 
    3285       32873 :     if (samples) {
    3286       32873 :         ac->frame->nb_samples = samples;
    3287       32873 :         ac->frame->sample_rate = avctx->sample_rate;
    3288             :     } else
    3289           0 :         av_frame_unref(ac->frame);
    3290       32873 :     *got_frame_ptr = !!samples;
    3291             : 
    3292             :     /* for dual-mono audio (SCE + SCE) */
    3293       32873 :     is_dmono = ac->dmono_mode && sce_count == 2 &&
    3294           0 :                ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
    3295       32873 :     if (is_dmono) {
    3296           0 :         if (ac->dmono_mode == 1)
    3297           0 :             ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
    3298           0 :         else if (ac->dmono_mode == 2)
    3299           0 :             ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
    3300             :     }
    3301             : 
    3302       32873 :     return 0;
    3303           0 : fail:
    3304           0 :     pop_output_configuration(ac);
    3305           0 :     return err;
    3306             : }
    3307             : 
    3308       49330 : static int aac_decode_frame(AVCodecContext *avctx, void *data,
    3309             :                             int *got_frame_ptr, AVPacket *avpkt)
    3310             : {
    3311       49330 :     AACContext *ac = avctx->priv_data;
    3312       49330 :     const uint8_t *buf = avpkt->data;
    3313       49330 :     int buf_size = avpkt->size;
    3314             :     GetBitContext gb;
    3315             :     int buf_consumed;
    3316             :     int buf_offset;
    3317             :     int err;
    3318             :     int new_extradata_size;
    3319       49330 :     const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
    3320             :                                        AV_PKT_DATA_NEW_EXTRADATA,
    3321             :                                        &new_extradata_size);
    3322             :     int jp_dualmono_size;
    3323       49330 :     const uint8_t *jp_dualmono   = av_packet_get_side_data(avpkt,
    3324             :                                        AV_PKT_DATA_JP_DUALMONO,
    3325             :                                        &jp_dualmono_size);
    3326             : 
    3327       49330 :     if (new_extradata) {
    3328             :         /* discard previous configuration */
    3329           1 :         ac->oc[1].status = OC_NONE;
    3330           2 :         err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
    3331             :                                            new_extradata,
    3332           1 :                                            new_extradata_size * 8LL, 1);
    3333           1 :         if (err < 0) {
    3334           0 :             return err;
    3335             :         }
    3336             :     }
    3337             : 
    3338       49330 :     ac->dmono_mode = 0;
    3339       49330 :     if (jp_dualmono && jp_dualmono_size > 0)
    3340           0 :         ac->dmono_mode =  1 + *jp_dualmono;
    3341       49330 :     if (ac->force_dmono_mode >= 0)
    3342       14933 :         ac->dmono_mode = ac->force_dmono_mode;
    3343             : 
    3344       49330 :     if (INT_MAX / 8 <= buf_size)
    3345           0 :         return AVERROR_INVALIDDATA;
    3346             : 
    3347       49330 :     if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
    3348           0 :         return err;
    3349             : 
    3350       49330 :     switch (ac->oc[1].m4ac.object_type) {
    3351       16971 :     case AOT_ER_AAC_LC:
    3352             :     case AOT_ER_AAC_LTP:
    3353             :     case AOT_ER_AAC_LD:
    3354             :     case AOT_ER_AAC_ELD:
    3355       16971 :         err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
    3356       16971 :         break;
    3357       32359 :     default:
    3358       32359 :         err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
    3359             :     }
    3360       49330 :     if (err < 0)
    3361           0 :         return err;
    3362             : 
    3363       49330 :     buf_consumed = (get_bits_count(&gb) + 7) >> 3;
    3364       49330 :     for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
    3365           0 :         if (buf[buf_offset])
    3366           0 :             break;
    3367             : 
    3368       49330 :     return buf_size > buf_offset ? buf_consumed : buf_size;
    3369             : }
    3370             : 
    3371         267 : static av_cold int aac_decode_close(AVCodecContext *avctx)
    3372             : {
    3373         267 :     AACContext *ac = avctx->priv_data;
    3374             :     int i, type;
    3375             : 
    3376        4539 :     for (i = 0; i < MAX_ELEM_ID; i++) {
    3377       21360 :         for (type = 0; type < 4; type++) {
    3378       17088 :             if (ac->che[type][i])
    3379         345 :                 AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][i]->sbr);
    3380       17088 :             av_freep(&ac->che[type][i]);
    3381             :         }
    3382             :     }
    3383             : 
    3384         267 :     ff_mdct_end(&ac->mdct);
    3385         267 :     ff_mdct_end(&ac->mdct_small);
    3386         267 :     ff_mdct_end(&ac->mdct_ld);
    3387         267 :     ff_mdct_end(&ac->mdct_ltp);
    3388             : #if !USE_FIXED
    3389         248 :     ff_mdct15_uninit(&ac->mdct120);
    3390         248 :     ff_mdct15_uninit(&ac->mdct480);
    3391         248 :     ff_mdct15_uninit(&ac->mdct960);
    3392             : #endif
    3393         267 :     av_freep(&ac->fdsp);
    3394         267 :     return 0;
    3395             : }
    3396             : 
    3397         267 : static void aacdec_init(AACContext *c)
    3398             : {
    3399         267 :     c->imdct_and_windowing                      = imdct_and_windowing;
    3400         267 :     c->apply_ltp                                = apply_ltp;
    3401         267 :     c->apply_tns                                = apply_tns;
    3402         267 :     c->windowing_and_mdct_ltp                   = windowing_and_mdct_ltp;
    3403         267 :     c->update_ltp                               = update_ltp;
    3404             : #if USE_FIXED
    3405          19 :     c->vector_pow43                             = vector_pow43;
    3406          19 :     c->subband_scale                            = subband_scale;
    3407             : #endif
    3408             : 
    3409             : #if !USE_FIXED
    3410             :     if(ARCH_MIPS)
    3411             :         ff_aacdec_init_mips(c);
    3412             : #endif /* !USE_FIXED */
    3413         267 : }
    3414             : /**
    3415             :  * AVOptions for Japanese DTV specific extensions (ADTS only)
    3416             :  */
    3417             : #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
    3418             : static const AVOption options[] = {
    3419             :     {"dual_mono_mode", "Select the channel to decode for dual mono",
    3420             :      offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
    3421             :      AACDEC_FLAGS, "dual_mono_mode"},
    3422             : 
    3423             :     {"auto", "autoselection",            0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3424             :     {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3425             :     {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3426             :     {"both", "Select both channels",     0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3427             : 
    3428             :     {NULL},
    3429             : };
    3430             : 
    3431             : static const AVClass aac_decoder_class = {
    3432             :     .class_name = "AAC decoder",
    3433             :     .item_name  = av_default_item_name,
    3434             :     .option     = options,
    3435             :     .version    = LIBAVUTIL_VERSION_INT,
    3436             : };

Generated by: LCOV version 1.13