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