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