LCOV - code coverage report
Current view: top level - libavcodec - aacdec_template.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1453 1794 81.0 %
Date: 2017-12-14 01:15:32 Functions: 59 61 96.7 %

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

Generated by: LCOV version 1.13