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