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