LCOV - code coverage report
Current view: top level - src/libavcodec - aacdec_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1345 1652 81.4 %
Date: 2017-01-24 04:42:20 Functions: 56 58 96.6 %

          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         505 : static int count_channels(uint8_t (*layout)[3], int tags)
     104             : {
     105         505 :     int i, sum = 0;
     106        1572 :     for (i = 0; i < tags; i++) {
     107        1067 :         int syn_ele = layout[i][0];
     108        1067 :         int pos     = layout[i][2];
     109        2134 :         sum += (1 + (syn_ele == TYPE_CPE)) *
     110        1067 :                (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
     111             :     }
     112         505 :     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        4974 : static av_cold int che_configure(AACContext *ac,
     128             :                                  enum ChannelPosition che_pos,
     129             :                                  int type, int id, int *channels)
     130             : {
     131        4974 :     if (*channels >= MAX_CHANNELS)
     132           0 :         return AVERROR_INVALIDDATA;
     133        4974 :     if (che_pos) {
     134        4974 :         if (!ac->che[type][id]) {
     135         329 :             if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
     136           0 :                 return AVERROR(ENOMEM);
     137         329 :             AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr);
     138             :         }
     139        4974 :         if (type != TYPE_CCE) {
     140        4966 :             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        4966 :             ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
     145        4966 :             if (type == TYPE_CPE ||
     146         762 :                 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
     147        4501 :                 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        4974 :     return 0;
     156             : }
     157             : 
     158       49532 : static int frame_configure_elements(AVCodecContext *avctx)
     159             : {
     160       49532 :     AACContext *ac = avctx->priv_data;
     161             :     int type, id, ch, ret;
     162             : 
     163             :     /* set channel pointers to internal buffers by default */
     164      247660 :     for (type = 0; type < 4; type++) {
     165     3368176 :         for (id = 0; id < MAX_ELEM_ID; id++) {
     166     3170048 :             ChannelElement *che = ac->che[type][id];
     167     3170048 :             if (che) {
     168       62912 :                 che->ch[0].ret = che->ch[0].ret_buf;
     169       62912 :                 che->ch[1].ret = che->ch[1].ret_buf;
     170             :             }
     171             :         }
     172             :     }
     173             : 
     174             :     /* get output buffer */
     175       49532 :     av_frame_unref(ac->frame);
     176       49532 :     if (!avctx->channels)
     177           0 :         return 1;
     178             : 
     179       49532 :     ac->frame->nb_samples = 2048;
     180       49532 :     if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
     181           0 :         return ret;
     182             : 
     183             :     /* map output channel pointers to AVFrame data */
     184      143684 :     for (ch = 0; ch < avctx->channels; ch++) {
     185       94152 :         if (ac->output_element[ch])
     186       94152 :             ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
     187             :     }
     188             : 
     189       49532 :     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        4141 : 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        4141 :     if (layout_map[offset][0] == TYPE_CPE) {
     204        8274 :         e2c_vec[offset] = (struct elem_to_channel) {
     205        4137 :             .av_position  = left | right,
     206             :             .syn_ele      = TYPE_CPE,
     207        4137 :             .elem_id      = layout_map[offset][1],
     208             :             .aac_position = pos
     209             :         };
     210        4137 :         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       14271 : static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
     229             :                                  int *current)
     230             : {
     231       14271 :     int num_pos_channels = 0;
     232       14271 :     int first_cpe        = 0;
     233       14271 :     int sce_parity       = 0;
     234             :     int i;
     235       19170 :     for (i = *current; i < tags; i++) {
     236        5100 :         if (layout_map[i][2] != pos)
     237         201 :             break;
     238        4899 :         if (layout_map[i][0] == TYPE_CPE) {
     239        4137 :             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        4137 :             num_pos_channels += 2;
     247        4137 :             first_cpe         = 1;
     248             :         } else {
     249         762 :             num_pos_channels++;
     250         762 :             sce_parity ^= 1;
     251             :         }
     252             :     }
     253       14271 :     if (sce_parity &&
     254         683 :         ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
     255           0 :         return -1;
     256       14271 :     *current = i;
     257       14271 :     return num_pos_channels;
     258             : }
     259             : 
     260        4757 : static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
     261             : {
     262             :     int i, n, total_non_cc_elements;
     263        4757 :     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        4757 :     if (FF_ARRAY_ELEMS(e2c_vec) < tags)
     268           0 :         return 0;
     269             : 
     270        4757 :     i = 0;
     271        4757 :     num_front_channels =
     272             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
     273        4757 :     if (num_front_channels < 0)
     274           0 :         return 0;
     275        4757 :     num_side_channels =
     276             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
     277        4757 :     if (num_side_channels < 0)
     278           0 :         return 0;
     279        4757 :     num_back_channels =
     280             :         count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
     281        4757 :     if (num_back_channels < 0)
     282           0 :         return 0;
     283             : 
     284        4757 :     if (num_side_channels == 0 && num_back_channels >= 4) {
     285           0 :         num_side_channels = 2;
     286           0 :         num_back_channels -= 2;
     287             :     }
     288             : 
     289        4757 :     i = 0;
     290        4757 :     if (num_front_channels & 1) {
     291        1508 :         e2c_vec[i] = (struct elem_to_channel) {
     292             :             .av_position  = AV_CH_FRONT_CENTER,
     293             :             .syn_ele      = TYPE_SCE,
     294         754 :             .elem_id      = layout_map[i][1],
     295             :             .aac_position = AAC_CHANNEL_FRONT
     296             :         };
     297         754 :         i++;
     298         754 :         num_front_channels--;
     299             :     }
     300        4757 :     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        4757 :     if (num_front_channels >= 2) {
     308        4074 :         i += assign_pair(e2c_vec, layout_map, i,
     309             :                          AV_CH_FRONT_LEFT,
     310             :                          AV_CH_FRONT_RIGHT,
     311             :                          AAC_CHANNEL_FRONT);
     312        4074 :         num_front_channels -= 2;
     313             :     }
     314        9514 :     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        4757 :     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        9514 :     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        9514 :     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        4757 :     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        4757 :     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        4757 :     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        9514 :     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        4757 :     total_non_cc_elements = n = i;
     383             :     do {
     384        4836 :         int next_n = 0;
     385        5179 :         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        4836 :         n = next_n;
     391        4836 :     } while (n > 0);
     392             : 
     393        4757 :     layout = 0;
     394        9723 :     for (i = 0; i < total_non_cc_elements; i++) {
     395        4966 :         layout_map[i][0] = e2c_vec[i].syn_ele;
     396        4966 :         layout_map[i][1] = e2c_vec[i].elem_id;
     397        4966 :         layout_map[i][2] = e2c_vec[i].aac_position;
     398        4966 :         if (e2c_vec[i].av_position != UINT64_MAX) {
     399        4966 :             layout |= e2c_vec[i].av_position;
     400             :         }
     401             :     }
     402             : 
     403        4757 :     return layout;
     404             : }
     405             : 
     406             : /**
     407             :  * Save current output configuration if and only if it has been locked.
     408             :  */
     409        4526 : static void push_output_configuration(AACContext *ac) {
     410        4526 :     if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
     411        4526 :         ac->oc[0] = ac->oc[1];
     412             :     }
     413        4526 :     ac->oc[1].status = OC_NONE;
     414        4526 : }
     415             : 
     416             : /**
     417             :  * Restore the previous output configuration if and only if the current
     418             :  * configuration is unlocked.
     419             :  */
     420           0 : static void pop_output_configuration(AACContext *ac) {
     421           0 :     if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
     422           0 :         ac->oc[1] = ac->oc[0];
     423           0 :         ac->avctx->channels = ac->oc[1].channels;
     424           0 :         ac->avctx->channel_layout = ac->oc[1].channel_layout;
     425           0 :         output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
     426             :                          ac->oc[1].status, 0);
     427             :     }
     428           0 : }
     429             : 
     430             : /**
     431             :  * Configure output channel order based on the current program
     432             :  * configuration element.
     433             :  *
     434             :  * @return  Returns error status. 0 - OK, !0 - error
     435             :  */
     436        4757 : static int output_configure(AACContext *ac,
     437             :                             uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
     438             :                             enum OCStatus oc_type, int get_new_frame)
     439             : {
     440        4757 :     AVCodecContext *avctx = ac->avctx;
     441        4757 :     int i, channels = 0, ret;
     442        4757 :     uint64_t layout = 0;
     443        4757 :     uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
     444        4757 :     uint8_t type_counts[TYPE_END] = { 0 };
     445             : 
     446        4757 :     if (ac->oc[1].layout_map != layout_map) {
     447        4745 :         memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
     448        4745 :         ac->oc[1].layout_map_tags = tags;
     449             :     }
     450        9731 :     for (i = 0; i < tags; i++) {
     451        4974 :         int type =         layout_map[i][0];
     452        4974 :         int id =           layout_map[i][1];
     453        4974 :         id_map[type][id] = type_counts[type]++;
     454        4974 :         if (id_map[type][id] >= MAX_ELEM_ID) {
     455           0 :             avpriv_request_sample(ac->avctx, "Too large remapped id");
     456           0 :             return AVERROR_PATCHWELCOME;
     457             :         }
     458             :     }
     459             :     // Try to sniff a reasonable channel order, otherwise output the
     460             :     // channels in the order the PCE declared them.
     461        4757 :     if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
     462        4757 :         layout = sniff_channel_order(layout_map, tags);
     463        9731 :     for (i = 0; i < tags; i++) {
     464        4974 :         int type =     layout_map[i][0];
     465        4974 :         int id =       layout_map[i][1];
     466        4974 :         int iid =      id_map[type][id];
     467        4974 :         int position = layout_map[i][2];
     468             :         // Allocate or free elements depending on if they are in the
     469             :         // current program configuration.
     470        4974 :         ret = che_configure(ac, position, type, iid, &channels);
     471        4974 :         if (ret < 0)
     472           0 :             return ret;
     473        4974 :         ac->tag_che_map[type][id] = ac->che[type][iid];
     474             :     }
     475        4757 :     if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
     476         364 :         if (layout == AV_CH_FRONT_CENTER) {
     477         364 :             layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
     478             :         } else {
     479           0 :             layout = 0;
     480             :         }
     481             :     }
     482             : 
     483        4757 :     if (layout) avctx->channel_layout = layout;
     484        4757 :                             ac->oc[1].channel_layout = layout;
     485        4757 :     avctx->channels       = ac->oc[1].channels       = channels;
     486        4757 :     ac->oc[1].status = oc_type;
     487             : 
     488        4757 :     if (get_new_frame) {
     489          12 :         if ((ret = frame_configure_elements(ac->avctx)) < 0)
     490           0 :             return ret;
     491             :     }
     492             : 
     493        4757 :     return 0;
     494             : }
     495             : 
     496           0 : static void flush(AVCodecContext *avctx)
     497             : {
     498           0 :     AACContext *ac= avctx->priv_data;
     499             :     int type, i, j;
     500             : 
     501           0 :     for (type = 3; type >= 0; type--) {
     502           0 :         for (i = 0; i < MAX_ELEM_ID; i++) {
     503           0 :             ChannelElement *che = ac->che[type][i];
     504           0 :             if (che) {
     505           0 :                 for (j = 0; j <= 1; j++) {
     506           0 :                     memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
     507             :                 }
     508             :             }
     509             :         }
     510             :     }
     511           0 : }
     512             : 
     513             : /**
     514             :  * Set up channel positions based on a default channel configuration
     515             :  * as specified in table 1.17.
     516             :  *
     517             :  * @return  Returns error status. 0 - OK, !0 - error
     518             :  */
     519        5025 : static int set_default_channel_config(AVCodecContext *avctx,
     520             :                                       uint8_t (*layout_map)[3],
     521             :                                       int *tags,
     522             :                                       int channel_config)
     523             : {
     524        5025 :     if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
     525             :         channel_config > 12) {
     526           0 :         av_log(avctx, AV_LOG_ERROR,
     527             :                "invalid default channel configuration (%d)\n",
     528             :                channel_config);
     529           0 :         return AVERROR_INVALIDDATA;
     530             :     }
     531        5025 :     *tags = tags_per_config[channel_config];
     532        5025 :     memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
     533        5025 :            *tags * sizeof(*layout_map));
     534             : 
     535             :     /*
     536             :      * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
     537             :      * However, at least Nero AAC encoder encodes 7.1 streams using the default
     538             :      * channel config 7, mapping the side channels of the original audio stream
     539             :      * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
     540             :      * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
     541             :      * the incorrect streams as if they were correct (and as the encoder intended).
     542             :      *
     543             :      * As actual intended 7.1(wide) streams are very rare, default to assuming a
     544             :      * 7.1 layout was intended.
     545             :      */
     546        5025 :     if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
     547           0 :         av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
     548             :                " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
     549             :                " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
     550           0 :         layout_map[2][2] = AAC_CHANNEL_SIDE;
     551             :     }
     552             : 
     553        5025 :     return 0;
     554             : }
     555             : 
     556       62566 : static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
     557             : {
     558             :     /* For PCE based channel configurations map the channels solely based
     559             :      * on tags. */
     560       62566 :     if (!ac->oc[1].m4ac.chan_config) {
     561       23630 :         return ac->tag_che_map[type][elem_id];
     562             :     }
     563             :     // Allow single CPE stereo files to be signalled with mono configuration.
     564       59646 :     if (!ac->tags_mapped && type == TYPE_CPE &&
     565       20710 :         ac->oc[1].m4ac.chan_config == 1) {
     566             :         uint8_t layout_map[MAX_ELEM_ID*4][3];
     567             :         int layout_map_tags;
     568           0 :         push_output_configuration(ac);
     569             : 
     570           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
     571             : 
     572           0 :         if (set_default_channel_config(ac->avctx, layout_map,
     573             :                                        &layout_map_tags, 2) < 0)
     574           0 :             return NULL;
     575           0 :         if (output_configure(ac, layout_map, layout_map_tags,
     576             :                              OC_TRIAL_FRAME, 1) < 0)
     577           0 :             return NULL;
     578             : 
     579           0 :         ac->oc[1].m4ac.chan_config = 2;
     580           0 :         ac->oc[1].m4ac.ps = 0;
     581             :     }
     582             :     // And vice-versa
     583       54702 :     if (!ac->tags_mapped && type == TYPE_SCE &&
     584       15766 :         ac->oc[1].m4ac.chan_config == 2) {
     585             :         uint8_t layout_map[MAX_ELEM_ID * 4][3];
     586             :         int layout_map_tags;
     587           0 :         push_output_configuration(ac);
     588             : 
     589           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
     590             : 
     591           0 :         if (set_default_channel_config(ac->avctx, layout_map,
     592             :                                        &layout_map_tags, 1) < 0)
     593           0 :             return NULL;
     594           0 :         if (output_configure(ac, layout_map, layout_map_tags,
     595             :                              OC_TRIAL_FRAME, 1) < 0)
     596           0 :             return NULL;
     597             : 
     598           0 :         ac->oc[1].m4ac.chan_config = 1;
     599           0 :         if (ac->oc[1].m4ac.sbr)
     600           0 :             ac->oc[1].m4ac.ps = -1;
     601             :     }
     602             :     /* For indexed channel configurations map the channels solely based
     603             :      * on position. */
     604       38936 :     switch (ac->oc[1].m4ac.chan_config) {
     605             :     case 12:
     606             :     case 7:
     607           0 :         if (ac->tags_mapped == 3 && type == TYPE_CPE) {
     608           0 :             ac->tags_mapped++;
     609           0 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
     610             :         }
     611             :     case 11:
     612           0 :         if (ac->tags_mapped == 2 &&
     613           0 :             ac->oc[1].m4ac.chan_config == 11 &&
     614             :             type == TYPE_SCE) {
     615           0 :             ac->tags_mapped++;
     616           0 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
     617             :         }
     618             :     case 6:
     619             :         /* Some streams incorrectly code 5.1 audio as
     620             :          * SCE[0] CPE[0] CPE[1] SCE[1]
     621             :          * instead of
     622             :          * SCE[0] CPE[0] CPE[1] LFE[0].
     623             :          * If we seem to have encountered such a stream, transfer
     624             :          * the LFE[0] element to the SCE[1]'s mapping */
     625        3280 :         if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
     626         820 :             if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
     627           0 :                 av_log(ac->avctx, AV_LOG_WARNING,
     628             :                    "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
     629             :                    type == TYPE_SCE ? "SCE" : "LFE", elem_id);
     630           0 :                 ac->warned_remapping_once++;
     631             :             }
     632         820 :             ac->tags_mapped++;
     633         820 :             return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
     634             :         }
     635             :     case 5:
     636        2460 :         if (ac->tags_mapped == 2 && type == TYPE_CPE) {
     637         820 :             ac->tags_mapped++;
     638         820 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
     639             :         }
     640             :     case 4:
     641             :         /* Some streams incorrectly code 4.0 audio as
     642             :          * SCE[0] CPE[0] LFE[0]
     643             :          * instead of
     644             :          * SCE[0] CPE[0] SCE[1].
     645             :          * If we seem to have encountered such a stream, transfer
     646             :          * the SCE[1] element to the LFE[0]'s mapping */
     647        1640 :         if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
     648           0 :             if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
     649           0 :                 av_log(ac->avctx, AV_LOG_WARNING,
     650             :                    "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
     651             :                    type == TYPE_SCE ? "SCE" : "LFE", elem_id);
     652           0 :                 ac->warned_remapping_once++;
     653             :             }
     654           0 :             ac->tags_mapped++;
     655           0 :             return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
     656             :         }
     657        1640 :         if (ac->tags_mapped == 2 &&
     658           0 :             ac->oc[1].m4ac.chan_config == 4 &&
     659             :             type == TYPE_SCE) {
     660           0 :             ac->tags_mapped++;
     661           0 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
     662             :         }
     663             :     case 3:
     664             :     case 2:
     665       22350 :         if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
     666             :             type == TYPE_CPE) {
     667       21530 :             ac->tags_mapped++;
     668       21530 :             return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
     669         820 :         } else if (ac->oc[1].m4ac.chan_config == 2) {
     670           0 :             return NULL;
     671             :         }
     672             :     case 1:
     673       15766 :         if (!ac->tags_mapped && type == TYPE_SCE) {
     674       15766 :             ac->tags_mapped++;
     675       15766 :             return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
     676             :         }
     677             :     default:
     678           0 :         return NULL;
     679             :     }
     680             : }
     681             : 
     682             : /**
     683             :  * Decode an array of 4 bit element IDs, optionally interleaved with a
     684             :  * stereo/mono switching bit.
     685             :  *
     686             :  * @param type speaker type/position for these channels
     687             :  */
     688         195 : static void decode_channel_map(uint8_t layout_map[][3],
     689             :                                enum ChannelPosition type,
     690             :                                GetBitContext *gb, int n)
     691             : {
     692         481 :     while (n--) {
     693             :         enum RawDataBlockType syn_ele;
     694          91 :         switch (type) {
     695             :         case AAC_CHANNEL_FRONT:
     696             :         case AAC_CHANNEL_BACK:
     697             :         case AAC_CHANNEL_SIDE:
     698          71 :             syn_ele = get_bits1(gb);
     699          71 :             break;
     700             :         case AAC_CHANNEL_CC:
     701           8 :             skip_bits1(gb);
     702           8 :             syn_ele = TYPE_CCE;
     703           8 :             break;
     704             :         case AAC_CHANNEL_LFE:
     705          12 :             syn_ele = TYPE_LFE;
     706          12 :             break;
     707             :         default:
     708             :             // AAC_CHANNEL_OFF has no channel map
     709           0 :             av_assert0(0);
     710             :         }
     711          91 :         layout_map[0][0] = syn_ele;
     712          91 :         layout_map[0][1] = get_bits(gb, 4);
     713          91 :         layout_map[0][2] = type;
     714          91 :         layout_map++;
     715             :     }
     716         195 : }
     717             : 
     718             : /**
     719             :  * Decode program configuration element; reference: table 4.2.
     720             :  *
     721             :  * @return  Returns error status. 0 - OK, !0 - error
     722             :  */
     723          39 : static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
     724             :                       uint8_t (*layout_map)[3],
     725             :                       GetBitContext *gb)
     726             : {
     727             :     int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
     728             :     int sampling_index;
     729             :     int comment_len;
     730             :     int tags;
     731             : 
     732          39 :     skip_bits(gb, 2);  // object_type
     733             : 
     734          39 :     sampling_index = get_bits(gb, 4);
     735          39 :     if (m4ac->sampling_index != sampling_index)
     736           0 :         av_log(avctx, AV_LOG_WARNING,
     737             :                "Sample rate index in program config element does not "
     738             :                "match the sample rate index configured by the container.\n");
     739             : 
     740          39 :     num_front       = get_bits(gb, 4);
     741          39 :     num_side        = get_bits(gb, 4);
     742          39 :     num_back        = get_bits(gb, 4);
     743          39 :     num_lfe         = get_bits(gb, 2);
     744          39 :     num_assoc_data  = get_bits(gb, 3);
     745          39 :     num_cc          = get_bits(gb, 4);
     746             : 
     747          39 :     if (get_bits1(gb))
     748           0 :         skip_bits(gb, 4); // mono_mixdown_tag
     749          39 :     if (get_bits1(gb))
     750           0 :         skip_bits(gb, 4); // stereo_mixdown_tag
     751             : 
     752          39 :     if (get_bits1(gb))
     753           0 :         skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
     754             : 
     755          39 :     if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
     756           0 :         av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
     757           0 :         return -1;
     758             :     }
     759          39 :     decode_channel_map(layout_map       , AAC_CHANNEL_FRONT, gb, num_front);
     760          39 :     tags = num_front;
     761          39 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE,  gb, num_side);
     762          39 :     tags += num_side;
     763          39 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK,  gb, num_back);
     764          39 :     tags += num_back;
     765          39 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE,   gb, num_lfe);
     766          39 :     tags += num_lfe;
     767             : 
     768          39 :     skip_bits_long(gb, 4 * num_assoc_data);
     769             : 
     770          39 :     decode_channel_map(layout_map + tags, AAC_CHANNEL_CC,    gb, num_cc);
     771          39 :     tags += num_cc;
     772             : 
     773          39 :     align_get_bits(gb);
     774             : 
     775             :     /* comment field, first byte is length */
     776          39 :     comment_len = get_bits(gb, 8) * 8;
     777          39 :     if (get_bits_left(gb) < comment_len) {
     778           0 :         av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
     779           0 :         return AVERROR_INVALIDDATA;
     780             :     }
     781          39 :     skip_bits_long(gb, comment_len);
     782          39 :     return tags;
     783             : }
     784             : 
     785             : /**
     786             :  * Decode GA "General Audio" specific configuration; reference: table 4.1.
     787             :  *
     788             :  * @param   ac          pointer to AACContext, may be null
     789             :  * @param   avctx       pointer to AVCCodecContext, used for logging
     790             :  *
     791             :  * @return  Returns error status. 0 - OK, !0 - error
     792             :  */
     793         505 : static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
     794             :                                      GetBitContext *gb,
     795             :                                      MPEG4AudioConfig *m4ac,
     796             :                                      int channel_config)
     797             : {
     798             :     int extension_flag, ret, ep_config, res_flags;
     799             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
     800         505 :     int tags = 0;
     801             : 
     802         505 :     if (get_bits1(gb)) { // frameLengthFlag
     803           0 :         avpriv_request_sample(avctx, "960/120 MDCT window");
     804           0 :         return AVERROR_PATCHWELCOME;
     805             :     }
     806         505 :     m4ac->frame_length_short = 0;
     807             : 
     808         505 :     if (get_bits1(gb))       // dependsOnCoreCoder
     809           0 :         skip_bits(gb, 14);   // coreCoderDelay
     810         505 :     extension_flag = get_bits1(gb);
     811             : 
     812        1010 :     if (m4ac->object_type == AOT_AAC_SCALABLE ||
     813         505 :         m4ac->object_type == AOT_ER_AAC_SCALABLE)
     814           0 :         skip_bits(gb, 3);     // layerNr
     815             : 
     816         505 :     if (channel_config == 0) {
     817          39 :         skip_bits(gb, 4);  // element_instance_tag
     818          39 :         tags = decode_pce(avctx, m4ac, layout_map, gb);
     819          39 :         if (tags < 0)
     820           0 :             return tags;
     821             :     } else {
     822         466 :         if ((ret = set_default_channel_config(avctx, layout_map,
     823             :                                               &tags, channel_config)))
     824           0 :             return ret;
     825             :     }
     826             : 
     827         505 :     if (count_channels(layout_map, tags) > 1) {
     828         426 :         m4ac->ps = 0;
     829          79 :     } else if (m4ac->sbr == 1 && m4ac->ps == -1)
     830          16 :         m4ac->ps = 1;
     831             : 
     832         505 :     if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
     833           0 :         return ret;
     834             : 
     835         505 :     if (extension_flag) {
     836           4 :         switch (m4ac->object_type) {
     837             :         case AOT_ER_BSAC:
     838           0 :             skip_bits(gb, 5);    // numOfSubFrame
     839           0 :             skip_bits(gb, 11);   // layer_length
     840           0 :             break;
     841             :         case AOT_ER_AAC_LC:
     842             :         case AOT_ER_AAC_LTP:
     843             :         case AOT_ER_AAC_SCALABLE:
     844             :         case AOT_ER_AAC_LD:
     845           4 :             res_flags = get_bits(gb, 3);
     846           4 :             if (res_flags) {
     847           0 :                 avpriv_report_missing_feature(avctx,
     848             :                                               "AAC data resilience (flags %x)",
     849             :                                               res_flags);
     850           0 :                 return AVERROR_PATCHWELCOME;
     851             :             }
     852           4 :             break;
     853             :         }
     854           4 :         skip_bits1(gb);    // extensionFlag3 (TBD in version 3)
     855             :     }
     856         505 :     switch (m4ac->object_type) {
     857             :     case AOT_ER_AAC_LC:
     858             :     case AOT_ER_AAC_LTP:
     859             :     case AOT_ER_AAC_SCALABLE:
     860             :     case AOT_ER_AAC_LD:
     861           4 :         ep_config = get_bits(gb, 2);
     862           4 :         if (ep_config) {
     863           0 :             avpriv_report_missing_feature(avctx,
     864             :                                           "epConfig %d", ep_config);
     865           0 :             return AVERROR_PATCHWELCOME;
     866             :         }
     867             :     }
     868         505 :     return 0;
     869             : }
     870             : 
     871          10 : static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
     872             :                                      GetBitContext *gb,
     873             :                                      MPEG4AudioConfig *m4ac,
     874             :                                      int channel_config)
     875             : {
     876             :     int ret, ep_config, res_flags;
     877             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
     878          10 :     int tags = 0;
     879          10 :     const int ELDEXT_TERM = 0;
     880             : 
     881          10 :     m4ac->ps  = 0;
     882          10 :     m4ac->sbr = 0;
     883             : #if USE_FIXED
     884           2 :     if (get_bits1(gb)) { // frameLengthFlag
     885           0 :         avpriv_request_sample(avctx, "960/120 MDCT window");
     886           0 :         return AVERROR_PATCHWELCOME;
     887             :     }
     888             : #else
     889           8 :     m4ac->frame_length_short = get_bits1(gb);
     890             : #endif
     891          10 :     res_flags = get_bits(gb, 3);
     892          10 :     if (res_flags) {
     893           0 :         avpriv_report_missing_feature(avctx,
     894             :                                       "AAC data resilience (flags %x)",
     895             :                                       res_flags);
     896           0 :         return AVERROR_PATCHWELCOME;
     897             :     }
     898             : 
     899          10 :     if (get_bits1(gb)) { // ldSbrPresentFlag
     900           0 :         avpriv_report_missing_feature(avctx,
     901             :                                       "Low Delay SBR");
     902           0 :         return AVERROR_PATCHWELCOME;
     903             :     }
     904             : 
     905          20 :     while (get_bits(gb, 4) != ELDEXT_TERM) {
     906           0 :         int len = get_bits(gb, 4);
     907           0 :         if (len == 15)
     908           0 :             len += get_bits(gb, 8);
     909           0 :         if (len == 15 + 255)
     910           0 :             len += get_bits(gb, 16);
     911           0 :         if (get_bits_left(gb) < len * 8 + 4) {
     912           0 :             av_log(avctx, AV_LOG_ERROR, overread_err);
     913           0 :             return AVERROR_INVALIDDATA;
     914             :         }
     915           0 :         skip_bits_long(gb, 8 * len);
     916             :     }
     917             : 
     918          10 :     if ((ret = set_default_channel_config(avctx, layout_map,
     919             :                                           &tags, channel_config)))
     920           0 :         return ret;
     921             : 
     922          10 :     if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
     923           0 :         return ret;
     924             : 
     925          10 :     ep_config = get_bits(gb, 2);
     926          10 :     if (ep_config) {
     927           0 :         avpriv_report_missing_feature(avctx,
     928             :                                       "epConfig %d", ep_config);
     929           0 :         return AVERROR_PATCHWELCOME;
     930             :     }
     931          10 :     return 0;
     932             : }
     933             : 
     934             : /**
     935             :  * Decode audio specific configuration; reference: table 1.13.
     936             :  *
     937             :  * @param   ac          pointer to AACContext, may be null
     938             :  * @param   avctx       pointer to AVCCodecContext, used for logging
     939             :  * @param   m4ac        pointer to MPEG4AudioConfig, used for parsing
     940             :  * @param   data        pointer to buffer holding an audio specific config
     941             :  * @param   bit_size    size of audio specific config or data in bits
     942             :  * @param   sync_extension look for an appended sync extension
     943             :  *
     944             :  * @return  Returns error status or number of consumed bits. <0 - error
     945             :  */
     946         515 : static int decode_audio_specific_config(AACContext *ac,
     947             :                                         AVCodecContext *avctx,
     948             :                                         MPEG4AudioConfig *m4ac,
     949             :                                         const uint8_t *data, int64_t bit_size,
     950             :                                         int sync_extension)
     951             : {
     952             :     GetBitContext gb;
     953             :     int i, ret;
     954             : 
     955         515 :     if (bit_size < 0 || bit_size > INT_MAX) {
     956           0 :         av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
     957           0 :         return AVERROR_INVALIDDATA;
     958             :     }
     959             : 
     960             :     ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
     961         515 :     for (i = 0; i < bit_size >> 3; i++)
     962             :         ff_dlog(avctx, "%02x ", data[i]);
     963             :     ff_dlog(avctx, "\n");
     964             : 
     965         515 :     if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
     966           0 :         return ret;
     967             : 
     968         515 :     if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
     969             :                                           sync_extension)) < 0)
     970           0 :         return AVERROR_INVALIDDATA;
     971         515 :     if (m4ac->sampling_index > 12) {
     972           0 :         av_log(avctx, AV_LOG_ERROR,
     973             :                "invalid sampling rate index %d\n",
     974             :                m4ac->sampling_index);
     975           0 :         return AVERROR_INVALIDDATA;
     976             :     }
     977         519 :     if (m4ac->object_type == AOT_ER_AAC_LD &&
     978           8 :         (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
     979           0 :         av_log(avctx, AV_LOG_ERROR,
     980             :                "invalid low delay sampling rate index %d\n",
     981             :                m4ac->sampling_index);
     982           0 :         return AVERROR_INVALIDDATA;
     983             :     }
     984             : 
     985         515 :     skip_bits_long(&gb, i);
     986             : 
     987         515 :     switch (m4ac->object_type) {
     988             :     case AOT_AAC_MAIN:
     989             :     case AOT_AAC_LC:
     990             :     case AOT_AAC_LTP:
     991             :     case AOT_ER_AAC_LC:
     992             :     case AOT_ER_AAC_LD:
     993         505 :         if ((ret = decode_ga_specific_config(ac, avctx, &gb,
     994             :                                             m4ac, m4ac->chan_config)) < 0)
     995           0 :             return ret;
     996         505 :         break;
     997             :     case AOT_ER_AAC_ELD:
     998          10 :         if ((ret = decode_eld_specific_config(ac, avctx, &gb,
     999             :                                               m4ac, m4ac->chan_config)) < 0)
    1000           0 :             return ret;
    1001          10 :         break;
    1002             :     default:
    1003           0 :         avpriv_report_missing_feature(avctx,
    1004             :                                       "Audio object type %s%d",
    1005           0 :                                       m4ac->sbr == 1 ? "SBR+" : "",
    1006             :                                       m4ac->object_type);
    1007           0 :         return AVERROR(ENOSYS);
    1008             :     }
    1009             : 
    1010             :     ff_dlog(avctx,
    1011             :             "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
    1012             :             m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
    1013             :             m4ac->sample_rate, m4ac->sbr,
    1014             :             m4ac->ps);
    1015             : 
    1016         515 :     return get_bits_count(&gb);
    1017             : }
    1018             : 
    1019             : /**
    1020             :  * linear congruential pseudorandom number generator
    1021             :  *
    1022             :  * @param   previous_val    pointer to the current state of the generator
    1023             :  *
    1024             :  * @return  Returns a 32-bit pseudorandom integer
    1025             :  */
    1026     6270268 : static av_always_inline int lcg_random(unsigned previous_val)
    1027             : {
    1028     6270268 :     union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
    1029     6270268 :     return v.s;
    1030             : }
    1031             : 
    1032          40 : static void reset_all_predictors(PredictorState *ps)
    1033             : {
    1034             :     int i;
    1035       26920 :     for (i = 0; i < MAX_PREDICTORS; i++)
    1036       26880 :         reset_predict_state(&ps[i]);
    1037          40 : }
    1038             : 
    1039          59 : static int sample_rate_idx (int rate)
    1040             : {
    1041          59 :          if (92017 <= rate) return 0;
    1042          59 :     else if (75132 <= rate) return 1;
    1043          59 :     else if (55426 <= rate) return 2;
    1044          59 :     else if (46009 <= rate) return 3;
    1045          58 :     else if (37566 <= rate) return 4;
    1046          39 :     else if (27713 <= rate) return 5;
    1047          36 :     else if (23004 <= rate) return 6;
    1048          36 :     else if (18783 <= rate) return 7;
    1049          36 :     else if (13856 <= rate) return 8;
    1050          32 :     else if (11502 <= rate) return 9;
    1051          32 :     else if (9391  <= rate) return 10;
    1052          32 :     else                    return 11;
    1053             : }
    1054             : 
    1055         512 : static void reset_predictor_group(PredictorState *ps, int group_num)
    1056             : {
    1057             :     int i;
    1058       12030 :     for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
    1059       11518 :         reset_predict_state(&ps[i]);
    1060         512 : }
    1061             : 
    1062             : #define AAC_INIT_VLC_STATIC(num, size)                                     \
    1063             :     INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num],     \
    1064             :          ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]),  \
    1065             :                                     sizeof(ff_aac_spectral_bits[num][0]),  \
    1066             :         ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
    1067             :                                     sizeof(ff_aac_spectral_codes[num][0]), \
    1068             :         size);
    1069             : 
    1070             : static void aacdec_init(AACContext *ac);
    1071             : 
    1072         151 : static av_cold void aac_static_table_init(void)
    1073             : {
    1074         151 :     AAC_INIT_VLC_STATIC( 0, 304);
    1075         151 :     AAC_INIT_VLC_STATIC( 1, 270);
    1076         151 :     AAC_INIT_VLC_STATIC( 2, 550);
    1077         151 :     AAC_INIT_VLC_STATIC( 3, 300);
    1078         151 :     AAC_INIT_VLC_STATIC( 4, 328);
    1079         151 :     AAC_INIT_VLC_STATIC( 5, 294);
    1080         151 :     AAC_INIT_VLC_STATIC( 6, 306);
    1081         151 :     AAC_INIT_VLC_STATIC( 7, 268);
    1082         151 :     AAC_INIT_VLC_STATIC( 8, 510);
    1083         151 :     AAC_INIT_VLC_STATIC( 9, 366);
    1084         151 :     AAC_INIT_VLC_STATIC(10, 462);
    1085             : 
    1086         151 :     AAC_RENAME(ff_aac_sbr_init)();
    1087             : 
    1088         151 :     ff_aac_tableinit();
    1089             : 
    1090         151 :     INIT_VLC_STATIC(&vlc_scalefactors, 7,
    1091             :                     FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
    1092             :                     ff_aac_scalefactor_bits,
    1093             :                     sizeof(ff_aac_scalefactor_bits[0]),
    1094             :                     sizeof(ff_aac_scalefactor_bits[0]),
    1095             :                     ff_aac_scalefactor_code,
    1096             :                     sizeof(ff_aac_scalefactor_code[0]),
    1097             :                     sizeof(ff_aac_scalefactor_code[0]),
    1098             :                     352);
    1099             : 
    1100             :     // window initialization
    1101         151 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_1024), 4.0, 1024);
    1102         151 :     AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_128), 6.0, 128);
    1103         151 :     AAC_RENAME(ff_init_ff_sine_windows)(10);
    1104         151 :     AAC_RENAME(ff_init_ff_sine_windows)( 9);
    1105         151 :     AAC_RENAME(ff_init_ff_sine_windows)( 7);
    1106             : 
    1107         151 :     AAC_RENAME(ff_cbrt_tableinit)();
    1108         151 : }
    1109             : 
    1110             : static AVOnce aac_table_init = AV_ONCE_INIT;
    1111             : 
    1112         251 : static av_cold int aac_decode_init(AVCodecContext *avctx)
    1113             : {
    1114         251 :     AACContext *ac = avctx->priv_data;
    1115             :     int ret;
    1116             : 
    1117         251 :     ret = ff_thread_once(&aac_table_init, &aac_static_table_init);
    1118         251 :     if (ret != 0)
    1119           0 :         return AVERROR_UNKNOWN;
    1120             : 
    1121         251 :     ac->avctx = avctx;
    1122         251 :     ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
    1123             : 
    1124         251 :     aacdec_init(ac);
    1125             : #if USE_FIXED
    1126          17 :     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
    1127             : #else
    1128         234 :     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    1129             : #endif /* USE_FIXED */
    1130             : 
    1131         251 :     if (avctx->extradata_size > 0) {
    1132         192 :         if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
    1133         192 :                                                 avctx->extradata,
    1134         192 :                                                 avctx->extradata_size * 8LL,
    1135             :                                                 1)) < 0)
    1136           0 :             return ret;
    1137             :     } else {
    1138             :         int sr, i;
    1139             :         uint8_t layout_map[MAX_ELEM_ID*4][3];
    1140             :         int layout_map_tags;
    1141             : 
    1142          59 :         sr = sample_rate_idx(avctx->sample_rate);
    1143          59 :         ac->oc[1].m4ac.sampling_index = sr;
    1144          59 :         ac->oc[1].m4ac.channels = avctx->channels;
    1145          59 :         ac->oc[1].m4ac.sbr = -1;
    1146          59 :         ac->oc[1].m4ac.ps = -1;
    1147             : 
    1148         113 :         for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
    1149         113 :             if (ff_mpeg4audio_channels[i] == avctx->channels)
    1150          59 :                 break;
    1151          59 :         if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
    1152           0 :             i = 0;
    1153             :         }
    1154          59 :         ac->oc[1].m4ac.chan_config = i;
    1155             : 
    1156          59 :         if (ac->oc[1].m4ac.chan_config) {
    1157          27 :             int ret = set_default_channel_config(avctx, layout_map,
    1158             :                 &layout_map_tags, ac->oc[1].m4ac.chan_config);
    1159          27 :             if (!ret)
    1160          27 :                 output_configure(ac, layout_map, layout_map_tags,
    1161             :                                  OC_GLOBAL_HDR, 0);
    1162           0 :             else if (avctx->err_recognition & AV_EF_EXPLODE)
    1163           0 :                 return AVERROR_INVALIDDATA;
    1164             :         }
    1165             :     }
    1166             : 
    1167         251 :     if (avctx->channels > MAX_CHANNELS) {
    1168           0 :         av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
    1169           0 :         return AVERROR_INVALIDDATA;
    1170             :     }
    1171             : 
    1172             : #if USE_FIXED
    1173          17 :     ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
    1174             : #else
    1175         234 :     ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
    1176             : #endif /* USE_FIXED */
    1177         251 :     if (!ac->fdsp) {
    1178           0 :         return AVERROR(ENOMEM);
    1179             :     }
    1180             : 
    1181         251 :     ac->random_state = 0x1f2e3d4c;
    1182             : 
    1183         251 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct,       11, 1, 1.0 / RANGE15(1024.0));
    1184         251 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld,    10, 1, 1.0 / RANGE15(512.0));
    1185         251 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small,  8, 1, 1.0 / RANGE15(128.0));
    1186         251 :     AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp,   11, 0, RANGE15(-2.0));
    1187             : #if !USE_FIXED
    1188         234 :     ret = ff_imdct15_init(&ac->mdct480, 5);
    1189         234 :     if (ret < 0)
    1190           0 :         return ret;
    1191             : #endif
    1192             : 
    1193         251 :     return 0;
    1194             : }
    1195             : 
    1196             : /**
    1197             :  * Skip data_stream_element; reference: table 4.10.
    1198             :  */
    1199        2434 : static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
    1200             : {
    1201        2434 :     int byte_align = get_bits1(gb);
    1202        2434 :     int count = get_bits(gb, 8);
    1203        2434 :     if (count == 255)
    1204           0 :         count += get_bits(gb, 8);
    1205        2434 :     if (byte_align)
    1206        2067 :         align_get_bits(gb);
    1207             : 
    1208        2434 :     if (get_bits_left(gb) < 8 * count) {
    1209           0 :         av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
    1210           0 :         return AVERROR_INVALIDDATA;
    1211             :     }
    1212        2434 :     skip_bits_long(gb, 8 * count);
    1213        2434 :     return 0;
    1214             : }
    1215             : 
    1216        1421 : static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
    1217             :                              GetBitContext *gb)
    1218             : {
    1219             :     int sfb;
    1220        1421 :     if (get_bits1(gb)) {
    1221         355 :         ics->predictor_reset_group = get_bits(gb, 5);
    1222         710 :         if (ics->predictor_reset_group == 0 ||
    1223         355 :             ics->predictor_reset_group > 30) {
    1224           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    1225             :                    "Invalid Predictor Reset Group.\n");
    1226           0 :             return AVERROR_INVALIDDATA;
    1227             :         }
    1228             :     }
    1229       47596 :     for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
    1230       46175 :         ics->prediction_used[sfb] = get_bits1(gb);
    1231             :     }
    1232        1421 :     return 0;
    1233             : }
    1234             : 
    1235             : /**
    1236             :  * Decode Long Term Prediction data; reference: table 4.xx.
    1237             :  */
    1238         805 : static void decode_ltp(LongTermPrediction *ltp,
    1239             :                        GetBitContext *gb, uint8_t max_sfb)
    1240             : {
    1241             :     int sfb;
    1242             : 
    1243         805 :     ltp->lag  = get_bits(gb, 11);
    1244         805 :     ltp->coef = ltp_coef[get_bits(gb, 3)];
    1245       32682 :     for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
    1246       31877 :         ltp->used[sfb] = get_bits1(gb);
    1247         805 : }
    1248             : 
    1249             : /**
    1250             :  * Decode Individual Channel Stream info; reference: table 4.6.
    1251             :  */
    1252       62808 : static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
    1253             :                            GetBitContext *gb)
    1254             : {
    1255       62808 :     const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
    1256       62808 :     const int aot = m4ac->object_type;
    1257       62808 :     const int sampling_index = m4ac->sampling_index;
    1258       62808 :     if (aot != AOT_ER_AAC_ELD) {
    1259       46443 :         if (get_bits1(gb)) {
    1260           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
    1261           0 :             if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
    1262           0 :                 return AVERROR_INVALIDDATA;
    1263             :         }
    1264       46443 :         ics->window_sequence[1] = ics->window_sequence[0];
    1265       46443 :         ics->window_sequence[0] = get_bits(gb, 2);
    1266       48867 :         if (aot == AOT_ER_AAC_LD &&
    1267        2424 :             ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
    1268           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    1269             :                    "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
    1270           0 :                    "window sequence %d found.\n", ics->window_sequence[0]);
    1271           0 :             ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
    1272           0 :             return AVERROR_INVALIDDATA;
    1273             :         }
    1274       46443 :         ics->use_kb_window[1]   = ics->use_kb_window[0];
    1275       46443 :         ics->use_kb_window[0]   = get_bits1(gb);
    1276             :     }
    1277       62808 :     ics->num_window_groups  = 1;
    1278       62808 :     ics->group_len[0]       = 1;
    1279       62808 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    1280             :         int i;
    1281        1107 :         ics->max_sfb = get_bits(gb, 4);
    1282        8856 :         for (i = 0; i < 7; i++) {
    1283        7749 :             if (get_bits1(gb)) {
    1284        4971 :                 ics->group_len[ics->num_window_groups - 1]++;
    1285             :             } else {
    1286        2778 :                 ics->num_window_groups++;
    1287        2778 :                 ics->group_len[ics->num_window_groups - 1] = 1;
    1288             :             }
    1289             :         }
    1290        1107 :         ics->num_windows       = 8;
    1291        1107 :         ics->swb_offset        =    ff_swb_offset_128[sampling_index];
    1292        1107 :         ics->num_swb           =   ff_aac_num_swb_128[sampling_index];
    1293        1107 :         ics->tns_max_bands     = ff_tns_max_bands_128[sampling_index];
    1294        1107 :         ics->predictor_present = 0;
    1295             :     } else {
    1296       61701 :         ics->max_sfb           = get_bits(gb, 6);
    1297       61701 :         ics->num_windows       = 1;
    1298       61701 :         if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
    1299       18789 :             if (m4ac->frame_length_short) {
    1300        3559 :                 ics->swb_offset    =     ff_swb_offset_480[sampling_index];
    1301        3559 :                 ics->num_swb       =    ff_aac_num_swb_480[sampling_index];
    1302        3559 :                 ics->tns_max_bands =  ff_tns_max_bands_480[sampling_index];
    1303             :             } else {
    1304       15230 :                 ics->swb_offset    =     ff_swb_offset_512[sampling_index];
    1305       15230 :                 ics->num_swb       =    ff_aac_num_swb_512[sampling_index];
    1306       15230 :                 ics->tns_max_bands =  ff_tns_max_bands_512[sampling_index];
    1307             :             }
    1308       37578 :             if (!ics->num_swb || !ics->swb_offset)
    1309           0 :                 return AVERROR_BUG;
    1310             :         } else {
    1311       42912 :             ics->swb_offset    =    ff_swb_offset_1024[sampling_index];
    1312       42912 :             ics->num_swb       =   ff_aac_num_swb_1024[sampling_index];
    1313       42912 :             ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
    1314             :         }
    1315       61701 :         if (aot != AOT_ER_AAC_ELD) {
    1316       45336 :             ics->predictor_present     = get_bits1(gb);
    1317       45336 :             ics->predictor_reset_group = 0;
    1318             :         }
    1319       61701 :         if (ics->predictor_present) {
    1320        1978 :             if (aot == AOT_AAC_MAIN) {
    1321        1421 :                 if (decode_prediction(ac, ics, gb)) {
    1322           0 :                     goto fail;
    1323             :                 }
    1324         557 :             } else if (aot == AOT_AAC_LC ||
    1325             :                        aot == AOT_ER_AAC_LC) {
    1326           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    1327             :                        "Prediction is not allowed in AAC-LC.\n");
    1328           0 :                 goto fail;
    1329             :             } else {
    1330         557 :                 if (aot == AOT_ER_AAC_LD) {
    1331           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1332             :                            "LTP in ER AAC LD not yet implemented.\n");
    1333           0 :                     return AVERROR_PATCHWELCOME;
    1334             :                 }
    1335         557 :                 if ((ics->ltp.present = get_bits(gb, 1)))
    1336         382 :                     decode_ltp(&ics->ltp, gb, ics->max_sfb);
    1337             :             }
    1338             :         }
    1339             :     }
    1340             : 
    1341       62808 :     if (ics->max_sfb > ics->num_swb) {
    1342           0 :         av_log(ac->avctx, AV_LOG_ERROR,
    1343             :                "Number of scalefactor bands in group (%d) "
    1344             :                "exceeds limit (%d).\n",
    1345           0 :                ics->max_sfb, ics->num_swb);
    1346           0 :         goto fail;
    1347             :     }
    1348             : 
    1349       62808 :     return 0;
    1350             : fail:
    1351           0 :     ics->max_sfb = 0;
    1352           0 :     return AVERROR_INVALIDDATA;
    1353             : }
    1354             : 
    1355             : /**
    1356             :  * Decode band types (section_data payload); reference: table 4.46.
    1357             :  *
    1358             :  * @param   band_type           array of the used band type
    1359             :  * @param   band_type_run_end   array of the last scalefactor band of a band type run
    1360             :  *
    1361             :  * @return  Returns error status. 0 - OK, !0 - error
    1362             :  */
    1363       93422 : static int decode_band_types(AACContext *ac, enum BandType band_type[120],
    1364             :                              int band_type_run_end[120], GetBitContext *gb,
    1365             :                              IndividualChannelStream *ics)
    1366             : {
    1367       93422 :     int g, idx = 0;
    1368       93422 :     const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
    1369      190769 :     for (g = 0; g < ics->num_window_groups; g++) {
    1370       97347 :         int k = 0;
    1371      648003 :         while (k < ics->max_sfb) {
    1372      453309 :             uint8_t sect_end = k;
    1373             :             int sect_len_incr;
    1374      453309 :             int sect_band_type = get_bits(gb, 4);
    1375      453309 :             if (sect_band_type == 12) {
    1376           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
    1377           0 :                 return AVERROR_INVALIDDATA;
    1378             :             }
    1379             :             do {
    1380      470701 :                 sect_len_incr = get_bits(gb, bits);
    1381      470701 :                 sect_end += sect_len_incr;
    1382      470701 :                 if (get_bits_left(gb) < 0) {
    1383           0 :                     av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
    1384           0 :                     return AVERROR_INVALIDDATA;
    1385             :                 }
    1386      470701 :                 if (sect_end > ics->max_sfb) {
    1387           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1388             :                            "Number of bands (%d) exceeds limit (%d).\n",
    1389           0 :                            sect_end, ics->max_sfb);
    1390           0 :                     return AVERROR_INVALIDDATA;
    1391             :                 }
    1392      470701 :             } while (sect_len_incr == (1 << bits) - 1);
    1393     3895037 :             for (; k < sect_end; k++) {
    1394     3441728 :                 band_type        [idx]   = sect_band_type;
    1395     3441728 :                 band_type_run_end[idx++] = sect_end;
    1396             :             }
    1397             :         }
    1398             :     }
    1399       93422 :     return 0;
    1400             : }
    1401             : 
    1402             : /**
    1403             :  * Decode scalefactors; reference: table 4.47.
    1404             :  *
    1405             :  * @param   global_gain         first scalefactor value as scalefactors are differentially coded
    1406             :  * @param   band_type           array of the used band type
    1407             :  * @param   band_type_run_end   array of the last scalefactor band of a band type run
    1408             :  * @param   sf                  array of scalefactors or intensity stereo positions
    1409             :  *
    1410             :  * @return  Returns error status. 0 - OK, !0 - error
    1411             :  */
    1412       93422 : static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
    1413             :                                unsigned int global_gain,
    1414             :                                IndividualChannelStream *ics,
    1415             :                                enum BandType band_type[120],
    1416             :                                int band_type_run_end[120])
    1417             : {
    1418       93422 :     int g, i, idx = 0;
    1419       93422 :     int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
    1420             :     int clipped_offset;
    1421       93422 :     int noise_flag = 1;
    1422      190769 :     for (g = 0; g < ics->num_window_groups; g++) {
    1423      648003 :         for (i = 0; i < ics->max_sfb;) {
    1424      453309 :             int run_end = band_type_run_end[idx];
    1425      453309 :             if (band_type[idx] == ZERO_BT) {
    1426      568037 :                 for (; i < run_end; i++, idx++)
    1427      508188 :                     sf[idx] = FIXR(0.);
    1428      786993 :             } else if ((band_type[idx] == INTENSITY_BT) ||
    1429      383825 :                        (band_type[idx] == INTENSITY_BT2)) {
    1430       75635 :                 for (; i < run_end; i++, idx++) {
    1431       65927 :                     offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1432       65927 :                     clipped_offset = av_clip(offset[2], -155, 100);
    1433       65927 :                     if (offset[2] != clipped_offset) {
    1434           0 :                         avpriv_request_sample(ac->avctx,
    1435             :                                               "If you heard an audible artifact, there may be a bug in the decoder. "
    1436             :                                               "Clipped intensity stereo position (%d -> %d)",
    1437             :                                               offset[2], clipped_offset);
    1438             :                     }
    1439             : #if USE_FIXED
    1440       17856 :                     sf[idx] = 100 - clipped_offset;
    1441             : #else
    1442       48071 :                     sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
    1443             : #endif /* USE_FIXED */
    1444             :                 }
    1445      383752 :             } else if (band_type[idx] == NOISE_BT) {
    1446      300390 :                 for (; i < run_end; i++, idx++) {
    1447      291188 :                     if (noise_flag-- > 0)
    1448        6827 :                         offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
    1449             :                     else
    1450      284361 :                         offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1451      291188 :                     clipped_offset = av_clip(offset[1], -100, 155);
    1452      291188 :                     if (offset[1] != clipped_offset) {
    1453           0 :                         avpriv_request_sample(ac->avctx,
    1454             :                                               "If you heard an audible artifact, there may be a bug in the decoder. "
    1455             :                                               "Clipped noise gain (%d -> %d)",
    1456             :                                               offset[1], clipped_offset);
    1457             :                     }
    1458             : #if USE_FIXED
    1459      137931 :                     sf[idx] = -(100 + clipped_offset);
    1460             : #else
    1461      153257 :                     sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
    1462             : #endif /* USE_FIXED */
    1463             :                 }
    1464             :             } else {
    1465     2950975 :                 for (; i < run_end; i++, idx++) {
    1466     2576425 :                     offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
    1467     2576425 :                     if (offset[0] > 255U) {
    1468           0 :                         av_log(ac->avctx, AV_LOG_ERROR,
    1469             :                                "Scalefactor (%d) out of range.\n", offset[0]);
    1470           0 :                         return AVERROR_INVALIDDATA;
    1471             :                     }
    1472             : #if USE_FIXED
    1473      769442 :                     sf[idx] = -offset[0];
    1474             : #else
    1475     1806983 :                     sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
    1476             : #endif /* USE_FIXED */
    1477             :                 }
    1478             :             }
    1479             :         }
    1480             :     }
    1481       93422 :     return 0;
    1482             : }
    1483             : 
    1484             : /**
    1485             :  * Decode pulse data; reference: table 4.7.
    1486             :  */
    1487         712 : static int decode_pulses(Pulse *pulse, GetBitContext *gb,
    1488             :                          const uint16_t *swb_offset, int num_swb)
    1489             : {
    1490             :     int i, pulse_swb;
    1491         712 :     pulse->num_pulse = get_bits(gb, 2) + 1;
    1492         712 :     pulse_swb        = get_bits(gb, 6);
    1493         712 :     if (pulse_swb >= num_swb)
    1494           0 :         return -1;
    1495         712 :     pulse->pos[0]    = swb_offset[pulse_swb];
    1496         712 :     pulse->pos[0]   += get_bits(gb, 5);
    1497         712 :     if (pulse->pos[0] >= swb_offset[num_swb])
    1498           0 :         return -1;
    1499         712 :     pulse->amp[0]    = get_bits(gb, 4);
    1500        1780 :     for (i = 1; i < pulse->num_pulse; i++) {
    1501        1068 :         pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
    1502        1068 :         if (pulse->pos[i] >= swb_offset[num_swb])
    1503           0 :             return -1;
    1504        1068 :         pulse->amp[i] = get_bits(gb, 4);
    1505             :     }
    1506         712 :     return 0;
    1507             : }
    1508             : 
    1509             : /**
    1510             :  * Decode Temporal Noise Shaping data; reference: table 4.48.
    1511             :  *
    1512             :  * @return  Returns error status. 0 - OK, !0 - error
    1513             :  */
    1514        6794 : static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
    1515             :                       GetBitContext *gb, const IndividualChannelStream *ics)
    1516             : {
    1517             :     int w, filt, i, coef_len, coef_res, coef_compress;
    1518        6794 :     const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
    1519        6794 :     const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
    1520       18061 :     for (w = 0; w < ics->num_windows; w++) {
    1521       11267 :         if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
    1522        7782 :             coef_res = get_bits1(gb);
    1523             : 
    1524       16903 :             for (filt = 0; filt < tns->n_filt[w]; filt++) {
    1525             :                 int tmp2_idx;
    1526        9121 :                 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
    1527             : 
    1528        9121 :                 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
    1529           0 :                     av_log(ac->avctx, AV_LOG_ERROR,
    1530             :                            "TNS filter order %d is greater than maximum %d.\n",
    1531             :                            tns->order[w][filt], tns_max_order);
    1532           0 :                     tns->order[w][filt] = 0;
    1533           0 :                     return AVERROR_INVALIDDATA;
    1534             :                 }
    1535        9121 :                 if (tns->order[w][filt]) {
    1536        8689 :                     tns->direction[w][filt] = get_bits1(gb);
    1537        8689 :                     coef_compress = get_bits1(gb);
    1538        8689 :                     coef_len = coef_res + 3 - coef_compress;
    1539        8689 :                     tmp2_idx = 2 * coef_compress + coef_res;
    1540             : 
    1541       66326 :                     for (i = 0; i < tns->order[w][filt]; i++)
    1542       57637 :                         tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
    1543             :                 }
    1544             :             }
    1545             :         }
    1546             :     }
    1547        6794 :     return 0;
    1548             : }
    1549             : 
    1550             : /**
    1551             :  * Decode Mid/Side data; reference: table 4.54.
    1552             :  *
    1553             :  * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
    1554             :  *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
    1555             :  *                      [3] reserved for scalable AAC
    1556             :  */
    1557       26675 : static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
    1558             :                                    int ms_present)
    1559             : {
    1560             :     int idx;
    1561       26675 :     int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
    1562       26675 :     if (ms_present == 1) {
    1563      496124 :         for (idx = 0; idx < max_idx; idx++)
    1564      483733 :             cpe->ms_mask[idx] = get_bits1(gb);
    1565       14284 :     } else if (ms_present == 2) {
    1566       14284 :         memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
    1567             :     }
    1568       26675 : }
    1569             : 
    1570             : /**
    1571             :  * Decode spectral data; reference: table 4.50.
    1572             :  * Dequantize and scale spectral data; reference: 4.6.3.3.
    1573             :  *
    1574             :  * @param   coef            array of dequantized, scaled spectral data
    1575             :  * @param   sf              array of scalefactors or intensity stereo positions
    1576             :  * @param   pulse_present   set if pulses are present
    1577             :  * @param   pulse           pointer to pulse data struct
    1578             :  * @param   band_type       array of the used band type
    1579             :  *
    1580             :  * @return  Returns error status. 0 - OK, !0 - error
    1581             :  */
    1582       93422 : static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
    1583             :                                        GetBitContext *gb, const INTFLOAT sf[120],
    1584             :                                        int pulse_present, const Pulse *pulse,
    1585             :                                        const IndividualChannelStream *ics,
    1586             :                                        enum BandType band_type[120])
    1587             : {
    1588       93422 :     int i, k, g, idx = 0;
    1589       93422 :     const int c = 1024 / ics->num_windows;
    1590       93422 :     const uint16_t *offsets = ics->swb_offset;
    1591       93422 :     INTFLOAT *coef_base = coef;
    1592             : 
    1593      198331 :     for (g = 0; g < ics->num_windows; g++)
    1594      104909 :         memset(coef + g * 128 + offsets[ics->max_sfb], 0,
    1595      104909 :                sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
    1596             : 
    1597      190769 :     for (g = 0; g < ics->num_window_groups; g++) {
    1598       97347 :         unsigned g_len = ics->group_len[g];
    1599             : 
    1600     3539075 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    1601     3441728 :             const unsigned cbt_m1 = band_type[idx] - 1;
    1602     3441728 :             INTFLOAT *cfo = coef + offsets[i];
    1603     3441728 :             int off_len = offsets[i + 1] - offsets[i];
    1604             :             int group;
    1605             : 
    1606     3441728 :             if (cbt_m1 >= INTENSITY_BT2 - 1) {
    1607     1166562 :                 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1608      592447 :                     memset(cfo, 0, off_len * sizeof(*cfo));
    1609             :                 }
    1610     2867613 :             } else if (cbt_m1 == NOISE_BT - 1) {
    1611      582429 :                 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1612             : #if !USE_FIXED
    1613             :                     float scale;
    1614             : #endif /* !USE_FIXED */
    1615             :                     INTFLOAT band_energy;
    1616             : 
    1617     6561509 :                     for (k = 0; k < off_len; k++) {
    1618     6270268 :                         ac->random_state  = lcg_random(ac->random_state);
    1619             : #if USE_FIXED
    1620     2883476 :                         cfo[k] = ac->random_state >> 3;
    1621             : #else
    1622     3386792 :                         cfo[k] = ac->random_state;
    1623             : #endif /* USE_FIXED */
    1624             :                     }
    1625             : 
    1626             : #if USE_FIXED
    1627      137931 :                     band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
    1628      137931 :                     band_energy = fixed_sqrt(band_energy, 31);
    1629      137931 :                     noise_scale(cfo, sf[idx], band_energy, off_len);
    1630             : #else
    1631      153310 :                     band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
    1632      153310 :                     scale = sf[idx] / sqrtf(band_energy);
    1633      153310 :                     ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
    1634             : #endif /* USE_FIXED */
    1635             :                 }
    1636             :             } else {
    1637             : #if !USE_FIXED
    1638     1806983 :                 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
    1639             : #endif /* !USE_FIXED */
    1640     2576425 :                 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
    1641     2576425 :                 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
    1642     2576425 :                 OPEN_READER(re, gb);
    1643             : 
    1644     2576425 :                 switch (cbt_m1 >> 1) {
    1645             :                 case 0:
    1646     1304814 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1647      666514 :                         INTFLOAT *cf = cfo;
    1648      666514 :                         int len = off_len;
    1649             : 
    1650             :                         do {
    1651             :                             int code;
    1652             :                             unsigned cb_idx;
    1653             : 
    1654     3308807 :                             UPDATE_CACHE(re, gb);
    1655     3308807 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1656     3308807 :                             cb_idx = cb_vector_idx[code];
    1657             : #if USE_FIXED
    1658      975941 :                             cf = DEC_SQUAD(cf, cb_idx);
    1659             : #else
    1660     2332866 :                             cf = VMUL4(cf, vq, cb_idx, sf + idx);
    1661             : #endif /* USE_FIXED */
    1662     3308807 :                         } while (len -= 4);
    1663             :                     }
    1664      638300 :                     break;
    1665             : 
    1666             :                 case 1:
    1667     1138899 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1668      575360 :                         INTFLOAT *cf = cfo;
    1669      575360 :                         int len = off_len;
    1670             : 
    1671             :                         do {
    1672             :                             int code;
    1673             :                             unsigned nnz;
    1674             :                             unsigned cb_idx;
    1675             :                             uint32_t bits;
    1676             : 
    1677     2482589 :                             UPDATE_CACHE(re, gb);
    1678     2482589 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1679     2482589 :                             cb_idx = cb_vector_idx[code];
    1680     2482589 :                             nnz = cb_idx >> 8 & 15;
    1681     2482589 :                             bits = nnz ? GET_CACHE(re, gb) : 0;
    1682     2482589 :                             LAST_SKIP_BITS(re, gb, nnz);
    1683             : #if USE_FIXED
    1684      816171 :                             cf = DEC_UQUAD(cf, cb_idx, bits);
    1685             : #else
    1686     1666418 :                             cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
    1687             : #endif /* USE_FIXED */
    1688     2482589 :                         } while (len -= 4);
    1689             :                     }
    1690      563539 :                     break;
    1691             : 
    1692             :                 case 2:
    1693      840582 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1694      426045 :                         INTFLOAT *cf = cfo;
    1695      426045 :                         int len = off_len;
    1696             : 
    1697             :                         do {
    1698             :                             int code;
    1699             :                             unsigned cb_idx;
    1700             : 
    1701     2651166 :                             UPDATE_CACHE(re, gb);
    1702     2651166 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1703     2651166 :                             cb_idx = cb_vector_idx[code];
    1704             : #if USE_FIXED
    1705      668708 :                             cf = DEC_SPAIR(cf, cb_idx);
    1706             : #else
    1707     1982458 :                             cf = VMUL2(cf, vq, cb_idx, sf + idx);
    1708             : #endif /* USE_FIXED */
    1709     2651166 :                         } while (len -= 2);
    1710             :                     }
    1711      414537 :                     break;
    1712             : 
    1713             :                 case 3:
    1714             :                 case 4:
    1715     1108032 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1716      562323 :                         INTFLOAT *cf = cfo;
    1717      562323 :                         int len = off_len;
    1718             : 
    1719             :                         do {
    1720             :                             int code;
    1721             :                             unsigned nnz;
    1722             :                             unsigned cb_idx;
    1723             :                             unsigned sign;
    1724             : 
    1725     2886488 :                             UPDATE_CACHE(re, gb);
    1726     2886488 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1727     2886488 :                             cb_idx = cb_vector_idx[code];
    1728     2886488 :                             nnz = cb_idx >> 8 & 15;
    1729     2886488 :                             sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
    1730     2886488 :                             LAST_SKIP_BITS(re, gb, nnz);
    1731             : #if USE_FIXED
    1732      699408 :                             cf = DEC_UPAIR(cf, cb_idx, sign);
    1733             : #else
    1734     2187080 :                             cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
    1735             : #endif /* USE_FIXED */
    1736     2886488 :                         } while (len -= 2);
    1737             :                     }
    1738      545709 :                     break;
    1739             : 
    1740             :                 default:
    1741      838839 :                     for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
    1742             : #if USE_FIXED
    1743      119212 :                         int *icf = cfo;
    1744             :                         int v;
    1745             : #else
    1746      305287 :                         float *cf = cfo;
    1747      305287 :                         uint32_t *icf = (uint32_t *) cf;
    1748             : #endif /* USE_FIXED */
    1749      424499 :                         int len = off_len;
    1750             : 
    1751             :                         do {
    1752             :                             int code;
    1753             :                             unsigned nzt, nnz;
    1754             :                             unsigned cb_idx;
    1755             :                             uint32_t bits;
    1756             :                             int j;
    1757             : 
    1758     1407180 :                             UPDATE_CACHE(re, gb);
    1759     1407180 :                             GET_VLC(code, re, gb, vlc_tab, 8, 2);
    1760             : 
    1761     1407180 :                             if (!code) {
    1762       68755 :                                 *icf++ = 0;
    1763       68755 :                                 *icf++ = 0;
    1764       68755 :                                 continue;
    1765             :                             }
    1766             : 
    1767     1338425 :                             cb_idx = cb_vector_idx[code];
    1768     1338425 :                             nnz = cb_idx >> 12;
    1769     1338425 :                             nzt = cb_idx >> 8;
    1770     1338425 :                             bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
    1771     1338425 :                             LAST_SKIP_BITS(re, gb, nnz);
    1772             : 
    1773     4015275 :                             for (j = 0; j < 2; j++) {
    1774     2676850 :                                 if (nzt & 1<<j) {
    1775             :                                     uint32_t b;
    1776             :                                     int n;
    1777             :                                     /* The total length of escape_sequence must be < 22 bits according
    1778             :                                        to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
    1779      381451 :                                     UPDATE_CACHE(re, gb);
    1780      381451 :                                     b = GET_CACHE(re, gb);
    1781      381451 :                                     b = 31 - av_log2(~b);
    1782             : 
    1783      381451 :                                     if (b > 8) {
    1784           0 :                                         av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
    1785           0 :                                         return AVERROR_INVALIDDATA;
    1786             :                                     }
    1787             : 
    1788      381451 :                                     SKIP_BITS(re, gb, b + 1);
    1789      381451 :                                     b += 4;
    1790      381451 :                                     n = (1 << b) + SHOW_UBITS(re, gb, b);
    1791      381451 :                                     LAST_SKIP_BITS(re, gb, b);
    1792             : #if USE_FIXED
    1793       64945 :                                     v = n;
    1794       64945 :                                     if (bits & 1U<<31)
    1795       32333 :                                         v = -v;
    1796       64945 :                                     *icf++ = v;
    1797             : #else
    1798      316506 :                                     *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
    1799             : #endif /* USE_FIXED */
    1800      381451 :                                     bits <<= 1;
    1801             :                                 } else {
    1802             : #if USE_FIXED
    1803      539047 :                                     v = cb_idx & 15;
    1804      539047 :                                     if (bits & 1U<<31)
    1805      256213 :                                         v = -v;
    1806      539047 :                                     *icf++ = v;
    1807             : #else
    1808     1756352 :                                     unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
    1809     1756352 :                                     *icf++ = (bits & 1U<<31) | v;
    1810             : #endif /* USE_FIXED */
    1811     2295399 :                                     bits <<= !!v;
    1812             :                                 }
    1813     2676850 :                                 cb_idx >>= 4;
    1814             :                             }
    1815     1407180 :                         } while (len -= 2);
    1816             : #if !USE_FIXED
    1817      305287 :                         ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
    1818             : #endif /* !USE_FIXED */
    1819             :                     }
    1820             :                 }
    1821             : 
    1822     2576425 :                 CLOSE_READER(re, gb);
    1823             :             }
    1824             :         }
    1825       97347 :         coef += g_len << 7;
    1826             :     }
    1827             : 
    1828       93422 :     if (pulse_present) {
    1829         712 :         idx = 0;
    1830        2492 :         for (i = 0; i < pulse->num_pulse; i++) {
    1831        1780 :             INTFLOAT co = coef_base[ pulse->pos[i] ];
    1832        6214 :             while (offsets[idx + 1] <= pulse->pos[i])
    1833        2654 :                 idx++;
    1834        1780 :             if (band_type[idx] != NOISE_BT && sf[idx]) {
    1835        1728 :                 INTFLOAT ico = -pulse->amp[i];
    1836             : #if USE_FIXED
    1837         864 :                 if (co) {
    1838         730 :                     ico = co + (co > 0 ? -ico : ico);
    1839             :                 }
    1840         864 :                 coef_base[ pulse->pos[i] ] = ico;
    1841             : #else
    1842         864 :                 if (co) {
    1843         730 :                     co /= sf[idx];
    1844         730 :                     ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
    1845             :                 }
    1846         864 :                 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
    1847             : #endif /* USE_FIXED */
    1848             :             }
    1849             :         }
    1850             :     }
    1851             : #if USE_FIXED
    1852       28250 :     coef = coef_base;
    1853       28250 :     idx = 0;
    1854       57486 :     for (g = 0; g < ics->num_window_groups; g++) {
    1855       29236 :         unsigned g_len = ics->group_len[g];
    1856             : 
    1857     1047661 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    1858     1018425 :             const unsigned cbt_m1 = band_type[idx] - 1;
    1859     1018425 :             int *cfo = coef + offsets[i];
    1860     1018425 :             int off_len = offsets[i + 1] - offsets[i];
    1861             :             int group;
    1862             : 
    1863     1018425 :             if (cbt_m1 < NOISE_BT - 1) {
    1864     1550552 :                 for (group = 0; group < (int)g_len; group++, cfo+=128) {
    1865      781110 :                     ac->vector_pow43(cfo, off_len);
    1866      781110 :                     ac->subband_scale(cfo, cfo, sf[idx], 34, off_len);
    1867             :                 }
    1868             :             }
    1869             :         }
    1870       29236 :         coef += g_len << 7;
    1871             :     }
    1872             : #endif /* USE_FIXED */
    1873       93422 :     return 0;
    1874             : }
    1875             : 
    1876             : /**
    1877             :  * Apply AAC-Main style frequency domain prediction.
    1878             :  */
    1879        7388 : static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
    1880             : {
    1881             :     int sfb, k;
    1882             : 
    1883        7388 :     if (!sce->ics.predictor_initialized) {
    1884          20 :         reset_all_predictors(sce->predictor_state);
    1885          20 :         sce->ics.predictor_initialized = 1;
    1886             :     }
    1887             : 
    1888        7388 :     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    1889      269934 :         for (sfb = 0;
    1890      262566 :              sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
    1891      255198 :              sfb++) {
    1892     4558332 :             for (k = sce->ics.swb_offset[sfb];
    1893     4303134 :                  k < sce->ics.swb_offset[sfb + 1];
    1894     4047936 :                  k++) {
    1895     4935552 :                 predict(&sce->predictor_state[k], &sce->coeffs[k],
    1896     4047936 :                         sce->ics.predictor_present &&
    1897      887616 :                         sce->ics.prediction_used[sfb]);
    1898             :             }
    1899             :         }
    1900        7368 :         if (sce->ics.predictor_reset_group)
    1901         512 :             reset_predictor_group(sce->predictor_state,
    1902             :                                   sce->ics.predictor_reset_group);
    1903             :     } else
    1904          20 :         reset_all_predictors(sce->predictor_state);
    1905        7388 : }
    1906             : 
    1907             : /**
    1908             :  * Decode an individual_channel_stream payload; reference: table 4.44.
    1909             :  *
    1910             :  * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
    1911             :  * @param   scale_flag      scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
    1912             :  *
    1913             :  * @return  Returns error status. 0 - OK, !0 - error
    1914             :  */
    1915       93422 : static int decode_ics(AACContext *ac, SingleChannelElement *sce,
    1916             :                       GetBitContext *gb, int common_window, int scale_flag)
    1917             : {
    1918             :     Pulse pulse;
    1919       93422 :     TemporalNoiseShaping    *tns = &sce->tns;
    1920       93422 :     IndividualChannelStream *ics = &sce->ics;
    1921       93422 :     INTFLOAT *out = sce->coeffs;
    1922       93422 :     int global_gain, eld_syntax, er_syntax, pulse_present = 0;
    1923             :     int ret;
    1924             : 
    1925       93422 :     eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    1926      280266 :     er_syntax  = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
    1927      186844 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
    1928      276630 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
    1929       89786 :                  ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    1930             : 
    1931             :     /* This assignment is to silence a GCC warning about the variable being used
    1932             :      * uninitialized when in fact it always is.
    1933             :      */
    1934       93422 :     pulse.num_pulse = 0;
    1935             : 
    1936       93422 :     global_gain = get_bits(gb, 8);
    1937             : 
    1938       93422 :     if (!common_window && !scale_flag) {
    1939       32194 :         if (decode_ics_info(ac, ics, gb) < 0)
    1940           0 :             return AVERROR_INVALIDDATA;
    1941             :     }
    1942             : 
    1943       93422 :     if ((ret = decode_band_types(ac, sce->band_type,
    1944       93422 :                                  sce->band_type_run_end, gb, ics)) < 0)
    1945           0 :         return ret;
    1946       93422 :     if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
    1947       93422 :                                   sce->band_type, sce->band_type_run_end)) < 0)
    1948           0 :         return ret;
    1949             : 
    1950       93422 :     pulse_present = 0;
    1951       93422 :     if (!scale_flag) {
    1952       93422 :         if (!eld_syntax && (pulse_present = get_bits1(gb))) {
    1953         712 :             if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    1954           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    1955             :                        "Pulse tool not allowed in eight short sequence.\n");
    1956           0 :                 return AVERROR_INVALIDDATA;
    1957             :             }
    1958         712 :             if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
    1959           0 :                 av_log(ac->avctx, AV_LOG_ERROR,
    1960             :                        "Pulse data corrupt or invalid.\n");
    1961           0 :                 return AVERROR_INVALIDDATA;
    1962             :             }
    1963             :         }
    1964       93422 :         tns->present = get_bits1(gb);
    1965       93422 :         if (tns->present && !er_syntax)
    1966        5530 :             if (decode_tns(ac, tns, gb, ics) < 0)
    1967           0 :                 return AVERROR_INVALIDDATA;
    1968       93422 :         if (!eld_syntax && get_bits1(gb)) {
    1969           0 :             avpriv_request_sample(ac->avctx, "SSR");
    1970           0 :             return AVERROR_PATCHWELCOME;
    1971             :         }
    1972             :         // I see no textual basis in the spec for this occurring after SSR gain
    1973             :         // control, but this is what both reference and real implmentations do
    1974       93422 :         if (tns->present && er_syntax)
    1975        1264 :             if (decode_tns(ac, tns, gb, ics) < 0)
    1976           0 :                 return AVERROR_INVALIDDATA;
    1977             :     }
    1978             : 
    1979       93422 :     if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
    1980       93422 :                                     &pulse, ics, sce->band_type) < 0)
    1981           0 :         return AVERROR_INVALIDDATA;
    1982             : 
    1983       93422 :     if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
    1984        6094 :         apply_prediction(ac, sce);
    1985             : 
    1986       93422 :     return 0;
    1987             : }
    1988             : 
    1989             : /**
    1990             :  * Mid/Side stereo decoding; reference: 4.6.8.1.3.
    1991             :  */
    1992       26675 : static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
    1993             : {
    1994       26675 :     const IndividualChannelStream *ics = &cpe->ch[0].ics;
    1995       26675 :     INTFLOAT *ch0 = cpe->ch[0].coeffs;
    1996       26675 :     INTFLOAT *ch1 = cpe->ch[1].coeffs;
    1997       26675 :     int g, i, group, idx = 0;
    1998       26675 :     const uint16_t *offsets = ics->swb_offset;
    1999       54379 :     for (g = 0; g < ics->num_window_groups; g++) {
    2000     1026489 :         for (i = 0; i < ics->max_sfb; i++, idx++) {
    2001     1790441 :             if (cpe->ms_mask[idx] &&
    2002     1583312 :                 cpe->ch[0].band_type[idx] < NOISE_BT &&
    2003      791656 :                 cpe->ch[1].band_type[idx] < NOISE_BT) {
    2004             : #if USE_FIXED
    2005      492774 :                 for (group = 0; group < ics->group_len[g]; group++) {
    2006      743571 :                     ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
    2007      247857 :                                                 ch1 + group * 128 + offsets[i],
    2008      247857 :                                                 offsets[i+1] - offsets[i]);
    2009             : #else
    2010     1115226 :                 for (group = 0; group < ics->group_len[g]; group++) {
    2011     1709571 :                     ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
    2012      569857 :                                                ch1 + group * 128 + offsets[i],
    2013      569857 :                                                offsets[i+1] - offsets[i]);
    2014             : #endif /* USE_FIXED */
    2015             :                 }
    2016             :             }
    2017             :         }
    2018       27704 :         ch0 += ics->group_len[g] * 128;
    2019       27704 :         ch1 += ics->group_len[g] * 128;
    2020             :     }
    2021       26675 : }
    2022             : 
    2023             : /**
    2024             :  * intensity stereo decoding; reference: 4.6.8.2.3
    2025             :  *
    2026             :  * @param   ms_present  Indicates mid/side stereo presence. [0] mask is all 0s;
    2027             :  *                      [1] mask is decoded from bitstream; [2] mask is all 1s;
    2028             :  *                      [3] reserved for scalable AAC
    2029             :  */
    2030       30856 : static void apply_intensity_stereo(AACContext *ac,
    2031             :                                    ChannelElement *cpe, int ms_present)
    2032             : {
    2033       30856 :     const IndividualChannelStream *ics = &cpe->ch[1].ics;
    2034       30856 :     SingleChannelElement         *sce1 = &cpe->ch[1];
    2035       30856 :     INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
    2036       30856 :     const uint16_t *offsets = ics->swb_offset;
    2037       30856 :     int g, group, i, idx = 0;
    2038             :     int c;
    2039             :     INTFLOAT scale;
    2040       62989 :     for (g = 0; g < ics->num_window_groups; g++) {
    2041      221818 :         for (i = 0; i < ics->max_sfb;) {
    2042      305469 :             if (sce1->band_type[idx] == INTENSITY_BT ||
    2043      157625 :                 sce1->band_type[idx] == INTENSITY_BT2) {
    2044        9708 :                 const int bt_run_end = sce1->band_type_run_end[idx];
    2045       75635 :                 for (; i < bt_run_end; i++, idx++) {
    2046       65927 :                     c = -1 + 2 * (sce1->band_type[idx] - 14);
    2047       65927 :                     if (ms_present)
    2048       59420 :                         c *= 1 - 2 * cpe->ms_mask[idx];
    2049       65927 :                     scale = c * sce1->sf[idx];
    2050      132171 :                     for (group = 0; group < ics->group_len[g]; group++)
    2051             : #if USE_FIXED
    2052       53865 :                         ac->subband_scale(coef1 + group * 128 + offsets[i],
    2053       17955 :                                       coef0 + group * 128 + offsets[i],
    2054             :                                       scale,
    2055             :                                       23,
    2056       17955 :                                       offsets[i + 1] - offsets[i]);
    2057             : #else
    2058      144867 :                         ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
    2059       48289 :                                                     coef0 + group * 128 + offsets[i],
    2060             :                                                     scale,
    2061       48289 :                                                     offsets[i + 1] - offsets[i]);
    2062             : #endif /* USE_FIXED */
    2063             :                 }
    2064             :             } else {
    2065      147844 :                 int bt_run_end = sce1->band_type_run_end[idx];
    2066      147844 :                 idx += bt_run_end - i;
    2067      147844 :                 i    = bt_run_end;
    2068             :             }
    2069             :         }
    2070       32133 :         coef0 += ics->group_len[g] * 128;
    2071       32133 :         coef1 += ics->group_len[g] * 128;
    2072             :     }
    2073       30856 : }
    2074             : 
    2075             : /**
    2076             :  * Decode a channel_pair_element; reference: table 4.4.
    2077             :  *
    2078             :  * @return  Returns error status. 0 - OK, !0 - error
    2079             :  */
    2080       30856 : static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
    2081             : {
    2082       30856 :     int i, ret, common_window, ms_present = 0;
    2083       30856 :     int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
    2084             : 
    2085       30856 :     common_window = eld_syntax || get_bits1(gb);
    2086       30856 :     if (common_window) {
    2087       30614 :         if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
    2088           0 :             return AVERROR_INVALIDDATA;
    2089       30614 :         i = cpe->ch[1].ics.use_kb_window[0];
    2090       30614 :         cpe->ch[1].ics = cpe->ch[0].ics;
    2091       30614 :         cpe->ch[1].ics.use_kb_window[1] = i;
    2092       31353 :         if (cpe->ch[1].ics.predictor_present &&
    2093         739 :             (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
    2094         557 :             if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
    2095         423 :                 decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
    2096       30614 :         ms_present = get_bits(gb, 2);
    2097       30614 :         if (ms_present == 3) {
    2098           0 :             av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
    2099           0 :             return AVERROR_INVALIDDATA;
    2100       30614 :         } else if (ms_present)
    2101       26675 :             decode_mid_side_stereo(cpe, gb, ms_present);
    2102             :     }
    2103       30856 :     if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
    2104           0 :         return ret;
    2105       30856 :     if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
    2106           0 :         return ret;
    2107             : 
    2108       30856 :     if (common_window) {
    2109       30614 :         if (ms_present)
    2110       26675 :             apply_mid_side_stereo(ac, cpe);
    2111       30614 :         if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
    2112         647 :             apply_prediction(ac, &cpe->ch[0]);
    2113         647 :             apply_prediction(ac, &cpe->ch[1]);
    2114             :         }
    2115             :     }
    2116             : 
    2117       30856 :     apply_intensity_stereo(ac, cpe, ms_present);
    2118       30856 :     return 0;
    2119             : }
    2120             : 
    2121             : static const float cce_scale[] = {
    2122             :     1.09050773266525765921, //2^(1/8)
    2123             :     1.18920711500272106672, //2^(1/4)
    2124             :     M_SQRT2,
    2125             :     2,
    2126             : };
    2127             : 
    2128             : /**
    2129             :  * Decode coupling_channel_element; reference: table 4.8.
    2130             :  *
    2131             :  * @return  Returns error status. 0 - OK, !0 - error
    2132             :  */
    2133        1174 : static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
    2134             : {
    2135        1174 :     int num_gain = 0;
    2136             :     int c, g, sfb, ret;
    2137             :     int sign;
    2138             :     INTFLOAT scale;
    2139        1174 :     SingleChannelElement *sce = &che->ch[0];
    2140        1174 :     ChannelCoupling     *coup = &che->coup;
    2141             : 
    2142        1174 :     coup->coupling_point = 2 * get_bits1(gb);
    2143        1174 :     coup->num_coupled = get_bits(gb, 3);
    2144        3915 :     for (c = 0; c <= coup->num_coupled; c++) {
    2145        2741 :         num_gain++;
    2146        2741 :         coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
    2147        2741 :         coup->id_select[c] = get_bits(gb, 4);
    2148        2741 :         if (coup->type[c] == TYPE_CPE) {
    2149        2348 :             coup->ch_select[c] = get_bits(gb, 2);
    2150        2348 :             if (coup->ch_select[c] == 3)
    2151        1967 :                 num_gain++;
    2152             :         } else
    2153         393 :             coup->ch_select[c] = 2;
    2154             :     }
    2155        1174 :     coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
    2156             : 
    2157        1174 :     sign  = get_bits(gb, 1);
    2158        1174 :     scale = AAC_RENAME(cce_scale)[get_bits(gb, 2)];
    2159             : 
    2160        1174 :     if ((ret = decode_ics(ac, sce, gb, 0, 0)))
    2161           0 :         return ret;
    2162             : 
    2163        5882 :     for (c = 0; c < num_gain; c++) {
    2164        4708 :         int idx  = 0;
    2165        4708 :         int cge  = 1;
    2166        4708 :         int gain = 0;
    2167        4708 :         INTFLOAT gain_cache = FIXR10(1.);
    2168        4708 :         if (c) {
    2169        3534 :             cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
    2170        3534 :             gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
    2171        3534 :             gain_cache = GET_GAIN(scale, gain);
    2172             :         }
    2173        4708 :         if (coup->coupling_point == AFTER_IMDCT) {
    2174        1965 :             coup->gain[c][0] = gain_cache;
    2175             :         } else {
    2176        5486 :             for (g = 0; g < sce->ics.num_window_groups; g++) {
    2177      112463 :                 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
    2178      109720 :                     if (sce->band_type[idx] != ZERO_BT) {
    2179        5196 :                         if (!cge) {
    2180        2879 :                             int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
    2181        2879 :                             if (t) {
    2182        2724 :                                 int s = 1;
    2183        2724 :                                 t = gain += t;
    2184        2724 :                                 if (sign) {
    2185        1666 :                                     s  -= 2 * (t & 0x1);
    2186        1666 :                                     t >>= 1;
    2187             :                                 }
    2188        2724 :                                 gain_cache = GET_GAIN(scale, t) * s;
    2189             :                             }
    2190             :                         }
    2191        5196 :                         coup->gain[c][idx] = gain_cache;
    2192             :                     }
    2193             :                 }
    2194             :             }
    2195             :         }
    2196             :     }
    2197        1174 :     return 0;
    2198             : }
    2199             : 
    2200             : /**
    2201             :  * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
    2202             :  *
    2203             :  * @return  Returns number of bytes consumed.
    2204             :  */
    2205           4 : static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
    2206             :                                          GetBitContext *gb)
    2207             : {
    2208             :     int i;
    2209           4 :     int num_excl_chan = 0;
    2210             : 
    2211             :     do {
    2212          32 :         for (i = 0; i < 7; i++)
    2213          28 :             che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
    2214           4 :     } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
    2215             : 
    2216           4 :     return num_excl_chan / 7;
    2217             : }
    2218             : 
    2219             : /**
    2220             :  * Decode dynamic range information; reference: table 4.52.
    2221             :  *
    2222             :  * @return  Returns number of bytes consumed.
    2223             :  */
    2224         880 : static int decode_dynamic_range(DynamicRangeControl *che_drc,
    2225             :                                 GetBitContext *gb)
    2226             : {
    2227         880 :     int n             = 1;
    2228         880 :     int drc_num_bands = 1;
    2229             :     int i;
    2230             : 
    2231             :     /* pce_tag_present? */
    2232         880 :     if (get_bits1(gb)) {
    2233           0 :         che_drc->pce_instance_tag  = get_bits(gb, 4);
    2234           0 :         skip_bits(gb, 4); // tag_reserved_bits
    2235           0 :         n++;
    2236             :     }
    2237             : 
    2238             :     /* excluded_chns_present? */
    2239         880 :     if (get_bits1(gb)) {
    2240           4 :         n += decode_drc_channel_exclusions(che_drc, gb);
    2241             :     }
    2242             : 
    2243             :     /* drc_bands_present? */
    2244         880 :     if (get_bits1(gb)) {
    2245          35 :         che_drc->band_incr            = get_bits(gb, 4);
    2246          35 :         che_drc->interpolation_scheme = get_bits(gb, 4);
    2247          35 :         n++;
    2248          35 :         drc_num_bands += che_drc->band_incr;
    2249         140 :         for (i = 0; i < drc_num_bands; i++) {
    2250         105 :             che_drc->band_top[i] = get_bits(gb, 8);
    2251         105 :             n++;
    2252             :         }
    2253             :     }
    2254             : 
    2255             :     /* prog_ref_level_present? */
    2256         880 :     if (get_bits1(gb)) {
    2257         878 :         che_drc->prog_ref_level = get_bits(gb, 7);
    2258         878 :         skip_bits1(gb); // prog_ref_level_reserved_bits
    2259         878 :         n++;
    2260             :     }
    2261             : 
    2262        1830 :     for (i = 0; i < drc_num_bands; i++) {
    2263         950 :         che_drc->dyn_rng_sgn[i] = get_bits1(gb);
    2264         950 :         che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
    2265         950 :         n++;
    2266             :     }
    2267             : 
    2268         880 :     return n;
    2269             : }
    2270             : 
    2271        8484 : static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
    2272             :     uint8_t buf[256];
    2273             :     int i, major, minor;
    2274             : 
    2275        8484 :     if (len < 13+7*8)
    2276         248 :         goto unknown;
    2277             : 
    2278        8236 :     get_bits(gb, 13); len -= 13;
    2279             : 
    2280     1064828 :     for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
    2281     1056592 :         buf[i] = get_bits(gb, 8);
    2282             : 
    2283        8236 :     buf[i] = 0;
    2284        8236 :     if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
    2285           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
    2286             : 
    2287        8236 :     if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
    2288          18 :         ac->avctx->internal->skip_samples = 1024;
    2289             :     }
    2290             : 
    2291             : unknown:
    2292        8484 :     skip_bits_long(gb, len);
    2293             : 
    2294        8484 :     return 0;
    2295             : }
    2296             : 
    2297             : /**
    2298             :  * Decode extension data (incomplete); reference: table 4.51.
    2299             :  *
    2300             :  * @param   cnt length of TYPE_FIL syntactic element in bytes
    2301             :  *
    2302             :  * @return Returns number of bytes consumed
    2303             :  */
    2304       23060 : static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
    2305             :                                     ChannelElement *che, enum RawDataBlockType elem_type)
    2306             : {
    2307       23060 :     int crc_flag = 0;
    2308       23060 :     int res = cnt;
    2309       23060 :     int type = get_bits(gb, 4);
    2310             : 
    2311       23060 :     if (ac->avctx->debug & FF_DEBUG_STARTCODE)
    2312           0 :         av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
    2313             : 
    2314       23060 :     switch (type) { // extension type
    2315             :     case EXT_SBR_DATA_CRC:
    2316           0 :         crc_flag++;
    2317             :     case EXT_SBR_DATA:
    2318       10794 :         if (!che) {
    2319           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
    2320           0 :             return res;
    2321       10794 :         } else if (!ac->oc[1].m4ac.sbr) {
    2322           0 :             av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
    2323           0 :             skip_bits_long(gb, 8 * cnt - 4);
    2324           0 :             return res;
    2325       10794 :         } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
    2326           0 :             av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
    2327           0 :             skip_bits_long(gb, 8 * cnt - 4);
    2328           0 :             return res;
    2329       10794 :         } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
    2330          12 :             ac->oc[1].m4ac.sbr = 1;
    2331          12 :             ac->oc[1].m4ac.ps = 1;
    2332          12 :             ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
    2333          12 :             output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
    2334             :                              ac->oc[1].status, 1);
    2335             :         } else {
    2336       10782 :             ac->oc[1].m4ac.sbr = 1;
    2337       10782 :             ac->avctx->profile = FF_PROFILE_AAC_HE;
    2338             :         }
    2339       10794 :         res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
    2340       10794 :         break;
    2341             :     case EXT_DYNAMIC_RANGE:
    2342         880 :         res = decode_dynamic_range(&ac->che_drc, gb);
    2343         880 :         break;
    2344             :     case EXT_FILL:
    2345        8484 :         decode_fill(ac, gb, 8 * cnt - 4);
    2346        8484 :         break;
    2347             :     case EXT_FILL_DATA:
    2348             :     case EXT_DATA_ELEMENT:
    2349             :     default:
    2350        2902 :         skip_bits_long(gb, 8 * cnt - 4);
    2351        2902 :         break;
    2352             :     };
    2353       23060 :     return res;
    2354             : }
    2355             : 
    2356             : /**
    2357             :  * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
    2358             :  *
    2359             :  * @param   decode  1 if tool is used normally, 0 if tool is used in LTP.
    2360             :  * @param   coef    spectral coefficients
    2361             :  */
    2362        6800 : static void apply_tns(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
    2363             :                       IndividualChannelStream *ics, int decode)
    2364             : {
    2365        6800 :     const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
    2366             :     int w, filt, m, i;
    2367             :     int bottom, top, order, start, end, size, inc;
    2368             :     INTFLOAT lpc[TNS_MAX_ORDER];
    2369             :     INTFLOAT tmp[TNS_MAX_ORDER+1];
    2370             : 
    2371       18073 :     for (w = 0; w < ics->num_windows; w++) {
    2372       11273 :         bottom = ics->num_swb;
    2373       20400 :         for (filt = 0; filt < tns->n_filt[w]; filt++) {
    2374        9127 :             top    = bottom;
    2375        9127 :             bottom = FFMAX(0, top - tns->length[w][filt]);
    2376        9127 :             order  = tns->order[w][filt];
    2377        9127 :             if (order == 0)
    2378         432 :                 continue;
    2379             : 
    2380             :             // tns_decode_coef
    2381        8695 :             AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
    2382             : 
    2383        8695 :             start = ics->swb_offset[FFMIN(bottom, mmm)];
    2384        8695 :             end   = ics->swb_offset[FFMIN(   top, mmm)];
    2385        8695 :             if ((size = end - start) <= 0)
    2386          14 :                 continue;
    2387        8681 :             if (tns->direction[w][filt]) {
    2388        3513 :                 inc = -1;
    2389        3513 :                 start = end - 1;
    2390             :             } else {
    2391        5168 :                 inc = 1;
    2392             :             }
    2393        8681 :             start += w * 128;
    2394             : 
    2395        8681 :             if (decode) {
    2396             :                 // ar filter
    2397     2721567 :                 for (m = 0; m < size; m++, start += inc)
    2398    23807323 :                     for (i = 1; i <= FFMIN(m, order); i++)
    2399    21094431 :                         coef[start] -= AAC_MUL26(coef[start - i * inc], lpc[i - 1]);
    2400             :             } else {
    2401             :                 // ma filter
    2402        3702 :                 for (m = 0; m < size; m++, start += inc) {
    2403        3696 :                     tmp[0] = coef[start];
    2404       39112 :                     for (i = 1; i <= FFMIN(m, order); i++)
    2405       35416 :                         coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
    2406       39424 :                     for (i = order; i > 0; i--)
    2407       35728 :                         tmp[i] = tmp[i - 1];
    2408             :                 }
    2409             :             }
    2410             :         }
    2411             :     }
    2412        6800 : }
    2413             : 
    2414             : /**
    2415             :  *  Apply windowing and MDCT to obtain the spectral
    2416             :  *  coefficient from the predicted sample by LTP.
    2417             :  */
    2418         805 : static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
    2419             :                                    INTFLOAT *in, IndividualChannelStream *ics)
    2420             : {
    2421         805 :     const INTFLOAT *lwindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2422         805 :     const INTFLOAT *swindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2423         805 :     const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2424         805 :     const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2425             : 
    2426         805 :     if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
    2427         803 :         ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
    2428             :     } else {
    2429           2 :         memset(in, 0, 448 * sizeof(*in));
    2430           2 :         ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
    2431             :     }
    2432         805 :     if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
    2433         805 :         ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
    2434             :     } else {
    2435           0 :         ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
    2436           0 :         memset(in + 1024 + 576, 0, 448 * sizeof(*in));
    2437             :     }
    2438         805 :     ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
    2439         805 : }
    2440             : 
    2441             : /**
    2442             :  * Apply the long term prediction
    2443             :  */
    2444         805 : static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
    2445             : {
    2446         805 :     const LongTermPrediction *ltp = &sce->ics.ltp;
    2447         805 :     const uint16_t *offsets = sce->ics.swb_offset;
    2448             :     int i, sfb;
    2449             : 
    2450         805 :     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
    2451         805 :         INTFLOAT *predTime = sce->ret;
    2452         805 :         INTFLOAT *predFreq = ac->buf_mdct;
    2453         805 :         int16_t num_samples = 2048;
    2454             : 
    2455         805 :         if (ltp->lag < 1024)
    2456           1 :             num_samples = ltp->lag + 1024;
    2457     1649441 :         for (i = 0; i < num_samples; i++)
    2458     1648636 :             predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
    2459         805 :         memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
    2460             : 
    2461         805 :         ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
    2462             : 
    2463         805 :         if (sce->tns.present)
    2464           6 :             ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
    2465             : 
    2466       32682 :         for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
    2467       31877 :             if (ltp->used[sfb])
    2468      370719 :                 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
    2469      347812 :                     sce->coeffs[i] += predFreq[i];
    2470             :     }
    2471         805 : }
    2472             : 
    2473             : /**
    2474             :  * Update the LTP buffer for next frame
    2475             :  */
    2476        4392 : static void update_ltp(AACContext *ac, SingleChannelElement *sce)
    2477             : {
    2478        4392 :     IndividualChannelStream *ics = &sce->ics;
    2479        4392 :     INTFLOAT *saved     = sce->saved;
    2480        4392 :     INTFLOAT *saved_ltp = sce->coeffs;
    2481        4392 :     const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2482        4392 :     const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2483             :     int i;
    2484             : 
    2485        4392 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2486          20 :         memcpy(saved_ltp,       saved, 512 * sizeof(*saved_ltp));
    2487          20 :         memset(saved_ltp + 576, 0,     448 * sizeof(*saved_ltp));
    2488          20 :         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
    2489             : 
    2490        1300 :         for (i = 0; i < 64; i++)
    2491        1280 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
    2492        4372 :     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    2493          17 :         memcpy(saved_ltp,       ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
    2494          17 :         memset(saved_ltp + 576, 0,                  448 * sizeof(*saved_ltp));
    2495          17 :         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
    2496             : 
    2497        1105 :         for (i = 0; i < 64; i++)
    2498        1088 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
    2499             :     } else { // LONG_STOP or ONLY_LONG
    2500        4355 :         ac->fdsp->vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     &lwindow[512],     512);
    2501             : 
    2502     2234115 :         for (i = 0; i < 512; i++)
    2503     2229760 :             saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
    2504             :     }
    2505             : 
    2506        4392 :     memcpy(sce->ltp_state,      sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
    2507        4392 :     memcpy(sce->ltp_state+1024, sce->ret,            1024 * sizeof(*sce->ltp_state));
    2508        4392 :     memcpy(sce->ltp_state+2048, saved_ltp,           1024 * sizeof(*sce->ltp_state));
    2509        4392 : }
    2510             : 
    2511             : /**
    2512             :  * Conduct IMDCT and windowing.
    2513             :  */
    2514       62407 : static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
    2515             : {
    2516       62407 :     IndividualChannelStream *ics = &sce->ics;
    2517       62407 :     INTFLOAT *in    = sce->coeffs;
    2518       62407 :     INTFLOAT *out   = sce->ret;
    2519       62407 :     INTFLOAT *saved = sce->saved;
    2520       62407 :     const INTFLOAT *swindow      = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2521       62407 :     const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
    2522       62407 :     const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
    2523       62407 :     INTFLOAT *buf  = ac->buf_mdct;
    2524       62407 :     INTFLOAT *temp = ac->temp;
    2525             :     int i;
    2526             : 
    2527             :     // imdct
    2528       62407 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2529       14769 :         for (i = 0; i < 1024; i += 128)
    2530       13128 :             ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
    2531             :     } else {
    2532       60766 :         ac->mdct.imdct_half(&ac->mdct, buf, in);
    2533             : #if USE_FIXED
    2534    16811025 :         for (i=0; i<1024; i++)
    2535    16794624 :           buf[i] = (buf[i] + 4) >> 3;
    2536             : #endif /* USE_FIXED */
    2537             :     }
    2538             : 
    2539             :     /* window overlapping
    2540             :      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
    2541             :      * and long to short transitions are considered to be short to short
    2542             :      * transitions. This leaves just two cases (long to long and short to short)
    2543             :      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
    2544             :      */
    2545      122086 :     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
    2546       60886 :             (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
    2547       59677 :         ac->fdsp->vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 512);
    2548             :     } else {
    2549        2730 :         memcpy(                         out,               saved,            448 * sizeof(*out));
    2550             : 
    2551        2730 :         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2552        1641 :             ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448,      buf + 0*128, swindow_prev, 64);
    2553        1641 :             ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow,      64);
    2554        1641 :             ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow,      64);
    2555        1641 :             ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow,      64);
    2556        1641 :             ac->fdsp->vector_fmul_window(temp,              buf + 3*128 + 64, buf + 4*128, swindow,      64);
    2557        1641 :             memcpy(                     out + 448 + 4*128, temp, 64 * sizeof(*out));
    2558             :         } else {
    2559        1089 :             ac->fdsp->vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, 64);
    2560        1089 :             memcpy(                     out + 576,         buf + 64,         448 * sizeof(*out));
    2561             :         }
    2562             :     }
    2563             : 
    2564             :     // buffer update
    2565       62407 :     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
    2566        1641 :         memcpy(                     saved,       temp + 64,         64 * sizeof(*saved));
    2567        1641 :         ac->fdsp->vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 64);
    2568        1641 :         ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
    2569        1641 :         ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
    2570        1641 :         memcpy(                     saved + 448, buf + 7*128 + 64,  64 * sizeof(*saved));
    2571       60766 :     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
    2572        1205 :         memcpy(                     saved,       buf + 512,        448 * sizeof(*saved));
    2573        1205 :         memcpy(                     saved + 448, buf + 7*128 + 64,  64 * sizeof(*saved));
    2574             :     } else { // LONG_STOP or ONLY_LONG
    2575       59561 :         memcpy(                     saved,       buf + 512,        512 * sizeof(*saved));
    2576             :     }
    2577       62407 : }
    2578             : 
    2579        3636 : static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
    2580             : {
    2581        3636 :     IndividualChannelStream *ics = &sce->ics;
    2582        3636 :     INTFLOAT *in    = sce->coeffs;
    2583        3636 :     INTFLOAT *out   = sce->ret;
    2584        3636 :     INTFLOAT *saved = sce->saved;
    2585        3636 :     INTFLOAT *buf  = ac->buf_mdct;
    2586             : #if USE_FIXED
    2587             :     int i;
    2588             : #endif /* USE_FIXED */
    2589             : 
    2590             :     // imdct
    2591        3636 :     ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
    2592             : 
    2593             : #if USE_FIXED
    2594     1857300 :     for (i = 0; i < 1024; i++)
    2595     1855488 :         buf[i] = (buf[i] + 2) >> 2;
    2596             : #endif /* USE_FIXED */
    2597             : 
    2598             :     // window overlapping
    2599        3636 :     if (ics->use_kb_window[1]) {
    2600             :         // AAC LD uses a low overlap sine window instead of a KBD window
    2601          62 :         memcpy(out, saved, 192 * sizeof(*out));
    2602          62 :         ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME(ff_sine_128), 64);
    2603          62 :         memcpy(                     out + 320, buf + 64, 192 * sizeof(*out));
    2604             :     } else {
    2605        3574 :         ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME(ff_sine_512), 256);
    2606             :     }
    2607             : 
    2608             :     // buffer update
    2609        3636 :     memcpy(saved, buf + 256, 256 * sizeof(*saved));
    2610        3636 : }
    2611             : 
    2612       26598 : static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
    2613             : {
    2614       26598 :     INTFLOAT *in    = sce->coeffs;
    2615       26598 :     INTFLOAT *out   = sce->ret;
    2616       26598 :     INTFLOAT *saved = sce->saved;
    2617       26598 :     INTFLOAT *buf  = ac->buf_mdct;
    2618             :     int i;
    2619       26598 :     const int n  = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
    2620       26598 :     const int n2 = n >> 1;
    2621       26598 :     const int n4 = n >> 2;
    2622       26598 :     const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
    2623             :                                            AAC_RENAME(ff_aac_eld_window_512);
    2624             : 
    2625             :     // Inverse transform, mapped to the conventional IMDCT by
    2626             :     // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
    2627             :     // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
    2628             :     // International Conference on Audio, Language and Image Processing, ICALIP 2008.
    2629             :     // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
    2630     3374198 :     for (i = 0; i < n2; i+=2) {
    2631             :         INTFLOAT temp;
    2632     3347600 :         temp =  in[i    ]; in[i    ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
    2633     3347600 :         temp = -in[i + 1]; in[i + 1] =  in[n - 2 - i]; in[n - 2 - i] = temp;
    2634             :     }
    2635             : #if !USE_FIXED
    2636       16861 :     if (n == 480)
    2637        7118 :         ac->mdct480->imdct_half(ac->mdct480, buf, in, 1, -1.f/(16*1024*960));
    2638             :     else
    2639             : #endif
    2640       19480 :         ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
    2641             : 
    2642             : #if USE_FIXED
    2643     9980425 :     for (i = 0; i < 1024; i++)
    2644     9970688 :       buf[i] = (buf[i] + 1) >> 1;
    2645             : #endif /* USE_FIXED */
    2646             : 
    2647     6721798 :     for (i = 0; i < n; i+=2) {
    2648     6695200 :         buf[i] = -buf[i];
    2649             :     }
    2650             :     // Like with the regular IMDCT at this point we still have the middle half
    2651             :     // of a transform but with even symmetry on the left and odd symmetry on
    2652             :     // the right
    2653             : 
    2654             :     // window overlapping
    2655             :     // The spec says to use samples [0..511] but the reference decoder uses
    2656             :     // samples [128..639].
    2657     3374198 :     for (i = n4; i < n2; i ++) {
    2658    10042800 :         out[i - n4] = AAC_MUL31(   buf[    n2 - 1 - i] , window[i       - n4]) +
    2659     6695200 :                       AAC_MUL31( saved[        i + n2] , window[i +   n - n4]) +
    2660     6695200 :                       AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
    2661     3347600 :                       AAC_MUL31(-saved[  2*n + n2 + i] , window[i + 3*n - n4]);
    2662             :     }
    2663     6721798 :     for (i = 0; i < n2; i ++) {
    2664    20085600 :         out[n4 + i] = AAC_MUL31(   buf[              i] , window[i + n2       - n4]) +
    2665    13390400 :                       AAC_MUL31(-saved[      n - 1 - i] , window[i + n2 +   n - n4]) +
    2666    13390400 :                       AAC_MUL31(-saved[          n + i] , window[i + n2 + 2*n - n4]) +
    2667     6695200 :                       AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
    2668             :     }
    2669     3374198 :     for (i = 0; i < n4; i ++) {
    2670    10042800 :         out[n2 + n4 + i] = AAC_MUL31(   buf[    i + n2] , window[i +   n - n4]) +
    2671     6695200 :                            AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
    2672     3347600 :                            AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
    2673             :     }
    2674             : 
    2675             :     // buffer update
    2676       26598 :     memmove(saved + n, saved, 2 * n * sizeof(*saved));
    2677       26598 :     memcpy( saved,       buf,     n * sizeof(*saved));
    2678       26598 : }
    2679             : 
    2680             : /**
    2681             :  * channel coupling transformation interface
    2682             :  *
    2683             :  * @param   apply_coupling_method   pointer to (in)dependent coupling function
    2684             :  */
    2685      174838 : static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
    2686             :                                    enum RawDataBlockType type, int elem_id,
    2687             :                                    enum CouplingPoint coupling_point,
    2688             :                                    void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
    2689             : {
    2690             :     int i, c;
    2691             : 
    2692     2972246 :     for (i = 0; i < MAX_ELEM_ID; i++) {
    2693     2797408 :         ChannelElement *cce = ac->che[TYPE_CCE][i];
    2694     2797408 :         int index = 0;
    2695             : 
    2696     2797408 :         if (cce && cce->coup.coupling_point == coupling_point) {
    2697        3915 :             ChannelCoupling *coup = &cce->coup;
    2698             : 
    2699       13317 :             for (c = 0; c <= coup->num_coupled; c++) {
    2700        9402 :                 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
    2701        2741 :                     if (coup->ch_select[c] != 1) {
    2702        2561 :                         apply_coupling_method(ac, &cc->ch[0], cce, index);
    2703        2561 :                         if (coup->ch_select[c] != 0)
    2704        2561 :                             index++;
    2705             :                     }
    2706        5482 :                     if (coup->ch_select[c] != 2)
    2707        2147 :                         apply_coupling_method(ac, &cc->ch[1], cce, index++);
    2708             :                 } else
    2709        6661 :                     index += 1 + (coup->ch_select[c] == 3);
    2710             :             }
    2711             :         }
    2712             :     }
    2713      174838 : }
    2714             : 
    2715             : /**
    2716             :  * Convert spectral data to samples, applying all supported tools as appropriate.
    2717             :  */
    2718       49520 : static void spectral_to_sample(AACContext *ac, int samples)
    2719             : {
    2720             :     int i, type;
    2721             :     void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
    2722       49520 :     switch (ac->oc[1].m4ac.object_type) {
    2723             :     case AOT_ER_AAC_LD:
    2724         606 :         imdct_and_window = imdct_and_windowing_ld;
    2725         606 :         break;
    2726             :     case AOT_ER_AAC_ELD:
    2727       16365 :         imdct_and_window = imdct_and_windowing_eld;
    2728       16365 :         break;
    2729             :     default:
    2730       32549 :         imdct_and_window = ac->imdct_and_windowing;
    2731             :     }
    2732      247600 :     for (type = 3; type >= 0; type--) {
    2733     3367360 :         for (i = 0; i < MAX_ELEM_ID; i++) {
    2734     3169280 :             ChannelElement *che = ac->che[type][i];
    2735     3169280 :             if (che && che->present) {
    2736       62566 :                 if (type <= TYPE_CPE)
    2737       57888 :                     apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
    2738       62566 :                 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
    2739        2196 :                     if (che->ch[0].ics.predictor_present) {
    2740         557 :                         if (che->ch[0].ics.ltp.present)
    2741         382 :                             ac->apply_ltp(ac, &che->ch[0]);
    2742         557 :                         if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
    2743         423 :                             ac->apply_ltp(ac, &che->ch[1]);
    2744             :                     }
    2745             :                 }
    2746       62566 :                 if (che->ch[0].tns.present)
    2747        4516 :                     ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
    2748       62566 :                 if (che->ch[1].tns.present)
    2749        2278 :                     ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
    2750       62566 :                 if (type <= TYPE_CPE)
    2751       57888 :                     apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
    2752       62566 :                 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
    2753       61785 :                     imdct_and_window(ac, &che->ch[0]);
    2754       61785 :                     if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    2755        2196 :                         ac->update_ltp(ac, &che->ch[0]);
    2756       61785 :                     if (type == TYPE_CPE) {
    2757       30856 :                         imdct_and_window(ac, &che->ch[1]);
    2758       30856 :                         if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
    2759        2196 :                             ac->update_ltp(ac, &che->ch[1]);
    2760             :                     }
    2761       61785 :                     if (ac->oc[1].m4ac.sbr > 0) {
    2762       12304 :                         AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
    2763             :                     }
    2764             :                 }
    2765       62566 :                 if (type <= TYPE_CCE)
    2766       59062 :                     apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
    2767             : 
    2768             : #if USE_FIXED
    2769             :                 {
    2770             :                     int j;
    2771             :                     /* preparation for resampler */
    2772    19510397 :                     for(j = 0; j<samples; j++){
    2773    19491328 :                         che->ch[0].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[0].ret[j]<<7)+0x8000;
    2774    19491328 :                         if(type == TYPE_CPE)
    2775     9700352 :                             che->ch[1].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[1].ret[j]<<7)+0x8000;
    2776             :                     }
    2777             :                 }
    2778             : #endif /* USE_FIXED */
    2779       62566 :                 che->present = 0;
    2780     3106714 :             } else if (che) {
    2781         329 :                 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
    2782             :             }
    2783             :         }
    2784             :     }
    2785       49520 : }
    2786             : 
    2787        4522 : static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
    2788             : {
    2789             :     int size;
    2790             :     AACADTSHeaderInfo hdr_info;
    2791             :     uint8_t layout_map[MAX_ELEM_ID*4][3];
    2792             :     int layout_map_tags, ret;
    2793             : 
    2794        4522 :     size = avpriv_aac_parse_header(gb, &hdr_info);
    2795        4522 :     if (size > 0) {
    2796        4522 :         if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
    2797             :             // This is 2 for "VLB " audio in NSV files.
    2798             :             // See samples/nsv/vlb_audio.
    2799           0 :             avpriv_report_missing_feature(ac->avctx,
    2800             :                                           "More than one AAC RDB per ADTS frame");
    2801           0 :             ac->warned_num_aac_frames = 1;
    2802             :         }
    2803        4522 :         push_output_configuration(ac);
    2804        4522 :         if (hdr_info.chan_config) {
    2805        4522 :             ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
    2806        4522 :             if ((ret = set_default_channel_config(ac->avctx,
    2807             :                                                   layout_map,
    2808             :                                                   &layout_map_tags,
    2809        4522 :                                                   hdr_info.chan_config)) < 0)
    2810           0 :                 return ret;
    2811        4522 :             if ((ret = output_configure(ac, layout_map, layout_map_tags,
    2812        4522 :                                         FFMAX(ac->oc[1].status,
    2813             :                                               OC_TRIAL_FRAME), 0)) < 0)
    2814           0 :                 return ret;
    2815             :         } else {
    2816           0 :             ac->oc[1].m4ac.chan_config = 0;
    2817             :             /**
    2818             :              * dual mono frames in Japanese DTV can have chan_config 0
    2819             :              * WITHOUT specifying PCE.
    2820             :              *  thus, set dual mono as default.
    2821             :              */
    2822           0 :             if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
    2823           0 :                 layout_map_tags = 2;
    2824           0 :                 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
    2825           0 :                 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
    2826           0 :                 layout_map[0][1] = 0;
    2827           0 :                 layout_map[1][1] = 1;
    2828           0 :                 if (output_configure(ac, layout_map, layout_map_tags,
    2829             :                                      OC_TRIAL_FRAME, 0))
    2830           0 :                     return -7;
    2831             :             }
    2832             :         }
    2833        4522 :         ac->oc[1].m4ac.sample_rate     = hdr_info.sample_rate;
    2834        4522 :         ac->oc[1].m4ac.sampling_index  = hdr_info.sampling_index;
    2835        4522 :         ac->oc[1].m4ac.object_type     = hdr_info.object_type;
    2836        4522 :         ac->oc[1].m4ac.frame_length_short = 0;
    2837        8990 :         if (ac->oc[0].status != OC_LOCKED ||
    2838        8936 :             ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
    2839        4468 :             ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
    2840          54 :             ac->oc[1].m4ac.sbr = -1;
    2841          54 :             ac->oc[1].m4ac.ps  = -1;
    2842             :         }
    2843        4522 :         if (!hdr_info.crc_absent)
    2844           0 :             skip_bits(gb, 16);
    2845             :     }
    2846        4522 :     return size;
    2847             : }
    2848             : 
    2849       16971 : static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
    2850             :                                int *got_frame_ptr, GetBitContext *gb)
    2851             : {
    2852       16971 :     AACContext *ac = avctx->priv_data;
    2853       16971 :     const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
    2854             :     ChannelElement *che;
    2855             :     int err, i;
    2856       16971 :     int samples = m4ac->frame_length_short ? 960 : 1024;
    2857       16971 :     int chan_config = m4ac->chan_config;
    2858       16971 :     int aot = m4ac->object_type;
    2859             : 
    2860       16971 :     if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
    2861       16971 :         samples >>= 1;
    2862             : 
    2863       16971 :     ac->frame = data;
    2864             : 
    2865       16971 :     if ((err = frame_configure_elements(avctx)) < 0)
    2866           0 :         return err;
    2867             : 
    2868             :     // The FF_PROFILE_AAC_* defines are all object_type - 1
    2869             :     // This may lead to an undefined profile being signaled
    2870       16971 :     ac->avctx->profile = aot - 1;
    2871             : 
    2872       16971 :     ac->tags_mapped = 0;
    2873             : 
    2874       16971 :     if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
    2875           0 :         avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
    2876             :                               chan_config);
    2877           0 :         return AVERROR_INVALIDDATA;
    2878             :     }
    2879       35760 :     for (i = 0; i < tags_per_config[chan_config]; i++) {
    2880       18789 :         const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
    2881       18789 :         const int elem_id   = aac_channel_layout_map[chan_config-1][i][1];
    2882       18789 :         if (!(che=get_che(ac, elem_type, elem_id))) {
    2883           0 :             av_log(ac->avctx, AV_LOG_ERROR,
    2884             :                    "channel element %d.%d is not allocated\n",
    2885             :                    elem_type, elem_id);
    2886           0 :             return AVERROR_INVALIDDATA;
    2887             :         }
    2888       18789 :         che->present = 1;
    2889       18789 :         if (aot != AOT_ER_AAC_ELD)
    2890        2424 :             skip_bits(gb, 4);
    2891       18789 :         switch (elem_type) {
    2892             :         case TYPE_SCE:
    2893        6738 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    2894        6738 :             break;
    2895             :         case TYPE_CPE:
    2896       11445 :             err = decode_cpe(ac, gb, che);
    2897       11445 :             break;
    2898             :         case TYPE_LFE:
    2899         606 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    2900         606 :             break;
    2901             :         }
    2902       18789 :         if (err < 0)
    2903           0 :             return err;
    2904             :     }
    2905             : 
    2906       16971 :     spectral_to_sample(ac, samples);
    2907             : 
    2908       16971 :     if (!ac->frame->data[0] && samples) {
    2909           0 :         av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
    2910           0 :         return AVERROR_INVALIDDATA;
    2911             :     }
    2912             : 
    2913       16971 :     ac->frame->nb_samples = samples;
    2914       16971 :     ac->frame->sample_rate = avctx->sample_rate;
    2915       16971 :     *got_frame_ptr = 1;
    2916             : 
    2917       16971 :     skip_bits_long(gb, get_bits_left(gb));
    2918       16971 :     return 0;
    2919             : }
    2920             : 
    2921       32549 : static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
    2922             :                                 int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
    2923             : {
    2924       32549 :     AACContext *ac = avctx->priv_data;
    2925       32549 :     ChannelElement *che = NULL, *che_prev = NULL;
    2926       32549 :     enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
    2927             :     int err, elem_id;
    2928       32549 :     int samples = 0, multiplier, audio_found = 0, pce_found = 0;
    2929       32549 :     int is_dmono, sce_count = 0;
    2930             : 
    2931       32549 :     ac->frame = data;
    2932             : 
    2933       32549 :     if (show_bits(gb, 12) == 0xfff) {
    2934        4522 :         if ((err = parse_adts_frame_header(ac, gb)) < 0) {
    2935           0 :             av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
    2936           0 :             goto fail;
    2937             :         }
    2938        4522 :         if (ac->oc[1].m4ac.sampling_index > 12) {
    2939           0 :             av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
    2940           0 :             err = AVERROR_INVALIDDATA;
    2941           0 :             goto fail;
    2942             :         }
    2943             :     }
    2944             : 
    2945       32549 :     if ((err = frame_configure_elements(avctx)) < 0)
    2946           0 :         goto fail;
    2947             : 
    2948             :     // The FF_PROFILE_AAC_* defines are all object_type - 1
    2949             :     // This may lead to an undefined profile being signaled
    2950       32549 :     ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
    2951             : 
    2952       32549 :     ac->tags_mapped = 0;
    2953             :     // parse
    2954      135863 :     while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
    2955       70765 :         elem_id = get_bits(gb, 4);
    2956             : 
    2957       70765 :         if (avctx->debug & FF_DEBUG_STARTCODE)
    2958           0 :             av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
    2959             : 
    2960       70765 :         if (!avctx->channels && elem_type != TYPE_PCE) {
    2961           0 :             err = AVERROR_INVALIDDATA;
    2962           0 :             goto fail;
    2963             :         }
    2964             : 
    2965       70765 :         if (elem_type < TYPE_DSE) {
    2966       43777 :             if (!(che=get_che(ac, elem_type, elem_id))) {
    2967           0 :                 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
    2968             :                        elem_type, elem_id);
    2969           0 :                 err = AVERROR_INVALIDDATA;
    2970           0 :                 goto fail;
    2971             :             }
    2972       43777 :             samples = 1024;
    2973       43777 :             che->present = 1;
    2974             :         }
    2975             : 
    2976       70765 :         switch (elem_type) {
    2977             : 
    2978             :         case TYPE_SCE:
    2979       20294 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    2980       20294 :             audio_found = 1;
    2981       20294 :             sce_count++;
    2982       20294 :             break;
    2983             : 
    2984             :         case TYPE_CPE:
    2985       19411 :             err = decode_cpe(ac, gb, che);
    2986       19411 :             audio_found = 1;
    2987       19411 :             break;
    2988             : 
    2989             :         case TYPE_CCE:
    2990        1174 :             err = decode_cce(ac, gb, che);
    2991        1174 :             break;
    2992             : 
    2993             :         case TYPE_LFE:
    2994        2898 :             err = decode_ics(ac, &che->ch[0], gb, 0, 0);
    2995        2898 :             audio_found = 1;
    2996        2898 :             break;
    2997             : 
    2998             :         case TYPE_DSE:
    2999        2434 :             err = skip_data_stream_element(ac, gb);
    3000        2434 :             break;
    3001             : 
    3002             :         case TYPE_PCE: {
    3003             :             uint8_t layout_map[MAX_ELEM_ID*4][3];
    3004             :             int tags;
    3005           0 :             push_output_configuration(ac);
    3006           0 :             tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
    3007           0 :             if (tags < 0) {
    3008           0 :                 err = tags;
    3009           0 :                 break;
    3010             :             }
    3011           0 :             if (pce_found) {
    3012           0 :                 av_log(avctx, AV_LOG_ERROR,
    3013             :                        "Not evaluating a further program_config_element as this construct is dubious at best.\n");
    3014             :             } else {
    3015           0 :                 err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
    3016           0 :                 if (!err)
    3017           0 :                     ac->oc[1].m4ac.chan_config = 0;
    3018           0 :                 pce_found = 1;
    3019             :             }
    3020           0 :             break;
    3021             :         }
    3022             : 
    3023             :         case TYPE_FIL:
    3024       24554 :             if (elem_id == 15)
    3025       16006 :                 elem_id += get_bits(gb, 8) - 1;
    3026       24554 :             if (get_bits_left(gb) < 8 * elem_id) {
    3027           0 :                     av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
    3028           0 :                     err = AVERROR_INVALIDDATA;
    3029           0 :                     goto fail;
    3030             :             }
    3031       72168 :             while (elem_id > 0)
    3032       23060 :                 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
    3033       24554 :             err = 0; /* FIXME */
    3034       24554 :             break;
    3035             : 
    3036             :         default:
    3037           0 :             err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
    3038           0 :             break;
    3039             :         }
    3040             : 
    3041       70765 :         if (elem_type < TYPE_DSE) {
    3042       43777 :             che_prev      = che;
    3043       43777 :             che_prev_type = elem_type;
    3044             :         }
    3045             : 
    3046       70765 :         if (err)
    3047           0 :             goto fail;
    3048             : 
    3049       70765 :         if (get_bits_left(gb) < 3) {
    3050           0 :             av_log(avctx, AV_LOG_ERROR, overread_err);
    3051           0 :             err = AVERROR_INVALIDDATA;
    3052           0 :             goto fail;
    3053             :         }
    3054             :     }
    3055             : 
    3056       32549 :     if (!avctx->channels) {
    3057           0 :         *got_frame_ptr = 0;
    3058           0 :         return 0;
    3059             :     }
    3060             : 
    3061       32549 :     multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
    3062       32549 :     samples <<= multiplier;
    3063             : 
    3064       32549 :     spectral_to_sample(ac, samples);
    3065             : 
    3066       32549 :     if (ac->oc[1].status && audio_found) {
    3067       32549 :         avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
    3068       32549 :         avctx->frame_size = samples;
    3069       32549 :         ac->oc[1].status = OC_LOCKED;
    3070             :     }
    3071             : 
    3072       32549 :     if (multiplier) {
    3073             :         int side_size;
    3074        5476 :         const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
    3075        5476 :         if (side && side_size>=4)
    3076          24 :             AV_WL32(side, 2*AV_RL32(side));
    3077             :     }
    3078             : 
    3079       32549 :     if (!ac->frame->data[0] && samples) {
    3080           0 :         av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
    3081           0 :         err = AVERROR_INVALIDDATA;
    3082           0 :         goto fail;
    3083             :     }
    3084             : 
    3085       32549 :     if (samples) {
    3086       32549 :         ac->frame->nb_samples = samples;
    3087       32549 :         ac->frame->sample_rate = avctx->sample_rate;
    3088             :     } else
    3089           0 :         av_frame_unref(ac->frame);
    3090       32549 :     *got_frame_ptr = !!samples;
    3091             : 
    3092             :     /* for dual-mono audio (SCE + SCE) */
    3093       32549 :     is_dmono = ac->dmono_mode && sce_count == 2 &&
    3094           0 :                ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
    3095       32549 :     if (is_dmono) {
    3096           0 :         if (ac->dmono_mode == 1)
    3097           0 :             ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
    3098           0 :         else if (ac->dmono_mode == 2)
    3099           0 :             ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
    3100             :     }
    3101             : 
    3102       32549 :     return 0;
    3103             : fail:
    3104           0 :     pop_output_configuration(ac);
    3105           0 :     return err;
    3106             : }
    3107             : 
    3108       49008 : static int aac_decode_frame(AVCodecContext *avctx, void *data,
    3109             :                             int *got_frame_ptr, AVPacket *avpkt)
    3110             : {
    3111       49008 :     AACContext *ac = avctx->priv_data;
    3112       49008 :     const uint8_t *buf = avpkt->data;
    3113       49008 :     int buf_size = avpkt->size;
    3114             :     GetBitContext gb;
    3115             :     int buf_consumed;
    3116             :     int buf_offset;
    3117             :     int err;
    3118             :     int new_extradata_size;
    3119       49008 :     const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
    3120             :                                        AV_PKT_DATA_NEW_EXTRADATA,
    3121             :                                        &new_extradata_size);
    3122             :     int jp_dualmono_size;
    3123       49008 :     const uint8_t *jp_dualmono   = av_packet_get_side_data(avpkt,
    3124             :                                        AV_PKT_DATA_JP_DUALMONO,
    3125             :                                        &jp_dualmono_size);
    3126             : 
    3127             :     if (new_extradata && 0) {
    3128             :         av_free(avctx->extradata);
    3129             :         avctx->extradata = av_mallocz(new_extradata_size +
    3130             :                                       AV_INPUT_BUFFER_PADDING_SIZE);
    3131             :         if (!avctx->extradata)
    3132             :             return AVERROR(ENOMEM);
    3133             :         avctx->extradata_size = new_extradata_size;
    3134             :         memcpy(avctx->extradata, new_extradata, new_extradata_size);
    3135             :         push_output_configuration(ac);
    3136             :         if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
    3137             :                                          avctx->extradata,
    3138             :                                          avctx->extradata_size*8LL, 1) < 0) {
    3139             :             pop_output_configuration(ac);
    3140             :             return AVERROR_INVALIDDATA;
    3141             :         }
    3142             :     }
    3143             : 
    3144       49008 :     ac->dmono_mode = 0;
    3145       49008 :     if (jp_dualmono && jp_dualmono_size > 0)
    3146           0 :         ac->dmono_mode =  1 + *jp_dualmono;
    3147       49008 :     if (ac->force_dmono_mode >= 0)
    3148       14703 :         ac->dmono_mode = ac->force_dmono_mode;
    3149             : 
    3150       49008 :     if (INT_MAX / 8 <= buf_size)
    3151           0 :         return AVERROR_INVALIDDATA;
    3152             : 
    3153       49008 :     if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
    3154           0 :         return err;
    3155             : 
    3156       49008 :     switch (ac->oc[1].m4ac.object_type) {
    3157             :     case AOT_ER_AAC_LC:
    3158             :     case AOT_ER_AAC_LTP:
    3159             :     case AOT_ER_AAC_LD:
    3160             :     case AOT_ER_AAC_ELD:
    3161       16971 :         err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
    3162       16971 :         break;
    3163             :     default:
    3164       32037 :         err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
    3165             :     }
    3166       49008 :     if (err < 0)
    3167           0 :         return err;
    3168             : 
    3169       49008 :     buf_consumed = (get_bits_count(&gb) + 7) >> 3;
    3170       49008 :     for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
    3171           0 :         if (buf[buf_offset])
    3172           0 :             break;
    3173             : 
    3174       49008 :     return buf_size > buf_offset ? buf_consumed : buf_size;
    3175             : }
    3176             : 
    3177         251 : static av_cold int aac_decode_close(AVCodecContext *avctx)
    3178             : {
    3179         251 :     AACContext *ac = avctx->priv_data;
    3180             :     int i, type;
    3181             : 
    3182        4267 :     for (i = 0; i < MAX_ELEM_ID; i++) {
    3183       20080 :         for (type = 0; type < 4; type++) {
    3184       16064 :             if (ac->che[type][i])
    3185         329 :                 AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][i]->sbr);
    3186       16064 :             av_freep(&ac->che[type][i]);
    3187             :         }
    3188             :     }
    3189             : 
    3190         251 :     ff_mdct_end(&ac->mdct);
    3191         251 :     ff_mdct_end(&ac->mdct_small);
    3192         251 :     ff_mdct_end(&ac->mdct_ld);
    3193         251 :     ff_mdct_end(&ac->mdct_ltp);
    3194             : #if !USE_FIXED
    3195         234 :     ff_imdct15_uninit(&ac->mdct480);
    3196             : #endif
    3197         251 :     av_freep(&ac->fdsp);
    3198         251 :     return 0;
    3199             : }
    3200             : 
    3201         251 : static void aacdec_init(AACContext *c)
    3202             : {
    3203         251 :     c->imdct_and_windowing                      = imdct_and_windowing;
    3204         251 :     c->apply_ltp                                = apply_ltp;
    3205         251 :     c->apply_tns                                = apply_tns;
    3206         251 :     c->windowing_and_mdct_ltp                   = windowing_and_mdct_ltp;
    3207         251 :     c->update_ltp                               = update_ltp;
    3208             : #if USE_FIXED
    3209          17 :     c->vector_pow43                             = vector_pow43;
    3210          17 :     c->subband_scale                            = subband_scale;
    3211             : #endif
    3212             : 
    3213             : #if !USE_FIXED
    3214             :     if(ARCH_MIPS)
    3215             :         ff_aacdec_init_mips(c);
    3216             : #endif /* !USE_FIXED */
    3217         251 : }
    3218             : /**
    3219             :  * AVOptions for Japanese DTV specific extensions (ADTS only)
    3220             :  */
    3221             : #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
    3222             : static const AVOption options[] = {
    3223             :     {"dual_mono_mode", "Select the channel to decode for dual mono",
    3224             :      offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
    3225             :      AACDEC_FLAGS, "dual_mono_mode"},
    3226             : 
    3227             :     {"auto", "autoselection",            0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3228             :     {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3229             :     {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3230             :     {"both", "Select both channels",     0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
    3231             : 
    3232             :     {NULL},
    3233             : };
    3234             : 
    3235             : static const AVClass aac_decoder_class = {
    3236             :     .class_name = "AAC decoder",
    3237             :     .item_name  = av_default_item_name,
    3238             :     .option     = options,
    3239             :     .version    = LIBAVUTIL_VERSION_INT,
    3240             : };

Generated by: LCOV version 1.12