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