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>
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 * @author Oded Shimon ( ods15 ods15 dyndns org )
32 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
39 * N (code in SoC repo) gain control
41 * Y window shapes - standard
42 * N window shapes - Low Delay
43 * Y filterbank - standard
44 * N (code in SoC repo) filterbank - Scalable Sample Rate
45 * Y Temporal Noise Shaping
46 * Y Long Term Prediction
49 * Y frequency domain prediction
50 * Y Perceptual Noise Substitution
52 * N Scalable Inverse AAC Quantization
53 * N Frequency Selective Switch
55 * Y quantization & coding - AAC
56 * N quantization & coding - TwinVQ
57 * N quantization & coding - BSAC
58 * N AAC Error Resilience tools
59 * N Error Resilience payload syntax
60 * N Error Protection tool
62 * N Silence Compression
65 * N Structured Audio tools
66 * N Structured Audio Sample Bank Format
68 * N Harmonic and Individual Lines plus Noise
69 * N Text-To-Speech Interface
70 * Y Spectral Band Replication
71 * Y (not in this code) Layer-1
72 * Y (not in this code) Layer-2
73 * Y (not in this code) Layer-3
74 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
76 * N Direct Stream Transfer
77 * Y Enhanced AAC Low Delay (ER AAC ELD)
79 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
80 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
84 #include "libavutil/float_dsp.h"
85 #include "libavutil/opt.h"
97 #include "aacdectab.h"
98 #include "cbrt_tablegen.h"
101 #include "mpeg4audio.h"
102 #include "aacadtsdec.h"
103 #include "libavutil/intfloat.h"
111 # include "arm/aac.h"
113 # include "mips/aacdec_mips.h"
116 static VLC vlc_scalefactors;
117 static VLC vlc_spectral[11];
119 static int output_configure(AACContext *ac,
120 uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
121 enum OCStatus oc_type, int get_new_frame);
123 #define overread_err "Input buffer exhausted before END element found\n"
125 static int count_channels(uint8_t (*layout)[3], int tags)
128 for (i = 0; i < tags; i++) {
129 int syn_ele = layout[i][0];
130 int pos = layout[i][2];
131 sum += (1 + (syn_ele == TYPE_CPE)) *
132 (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
138 * Check for the channel element in the current channel position configuration.
139 * If it exists, make sure the appropriate element is allocated and map the
140 * channel order to match the internal FFmpeg channel layout.
142 * @param che_pos current channel position configuration
143 * @param type channel element type
144 * @param id channel element id
145 * @param channels count of the number of channels in the configuration
147 * @return Returns error status. 0 - OK, !0 - error
149 static av_cold int che_configure(AACContext *ac,
150 enum ChannelPosition che_pos,
151 int type, int id, int *channels)
153 if (*channels >= MAX_CHANNELS)
154 return AVERROR_INVALIDDATA;
156 if (!ac->che[type][id]) {
157 if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
158 return AVERROR(ENOMEM);
159 ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
161 if (type != TYPE_CCE) {
162 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
163 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
164 return AVERROR_INVALIDDATA;
166 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
167 if (type == TYPE_CPE ||
168 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
169 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
173 if (ac->che[type][id])
174 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
175 av_freep(&ac->che[type][id]);
180 static int frame_configure_elements(AVCodecContext *avctx)
182 AACContext *ac = avctx->priv_data;
183 int type, id, ch, ret;
185 /* set channel pointers to internal buffers by default */
186 for (type = 0; type < 4; type++) {
187 for (id = 0; id < MAX_ELEM_ID; id++) {
188 ChannelElement *che = ac->che[type][id];
190 che->ch[0].ret = che->ch[0].ret_buf;
191 che->ch[1].ret = che->ch[1].ret_buf;
196 /* get output buffer */
197 av_frame_unref(ac->frame);
198 if (!avctx->channels)
201 ac->frame->nb_samples = 2048;
202 if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
205 /* map output channel pointers to AVFrame data */
206 for (ch = 0; ch < avctx->channels; ch++) {
207 if (ac->output_element[ch])
208 ac->output_element[ch]->ret = (float *)ac->frame->extended_data[ch];
214 struct elem_to_channel {
215 uint64_t av_position;
218 uint8_t aac_position;
221 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
222 uint8_t (*layout_map)[3], int offset, uint64_t left,
223 uint64_t right, int pos)
225 if (layout_map[offset][0] == TYPE_CPE) {
226 e2c_vec[offset] = (struct elem_to_channel) {
227 .av_position = left | right,
229 .elem_id = layout_map[offset][1],
234 e2c_vec[offset] = (struct elem_to_channel) {
237 .elem_id = layout_map[offset][1],
240 e2c_vec[offset + 1] = (struct elem_to_channel) {
241 .av_position = right,
243 .elem_id = layout_map[offset + 1][1],
250 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
253 int num_pos_channels = 0;
257 for (i = *current; i < tags; i++) {
258 if (layout_map[i][2] != pos)
260 if (layout_map[i][0] == TYPE_CPE) {
262 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
268 num_pos_channels += 2;
276 ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
279 return num_pos_channels;
282 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
284 int i, n, total_non_cc_elements;
285 struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
286 int num_front_channels, num_side_channels, num_back_channels;
289 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
294 count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
295 if (num_front_channels < 0)
298 count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
299 if (num_side_channels < 0)
302 count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
303 if (num_back_channels < 0)
306 if (num_side_channels == 0 && num_back_channels >= 4) {
307 num_side_channels = 2;
308 num_back_channels -= 2;
312 if (num_front_channels & 1) {
313 e2c_vec[i] = (struct elem_to_channel) {
314 .av_position = AV_CH_FRONT_CENTER,
316 .elem_id = layout_map[i][1],
317 .aac_position = AAC_CHANNEL_FRONT
320 num_front_channels--;
322 if (num_front_channels >= 4) {
323 i += assign_pair(e2c_vec, layout_map, i,
324 AV_CH_FRONT_LEFT_OF_CENTER,
325 AV_CH_FRONT_RIGHT_OF_CENTER,
327 num_front_channels -= 2;
329 if (num_front_channels >= 2) {
330 i += assign_pair(e2c_vec, layout_map, i,
334 num_front_channels -= 2;
336 while (num_front_channels >= 2) {
337 i += assign_pair(e2c_vec, layout_map, i,
341 num_front_channels -= 2;
344 if (num_side_channels >= 2) {
345 i += assign_pair(e2c_vec, layout_map, i,
349 num_side_channels -= 2;
351 while (num_side_channels >= 2) {
352 i += assign_pair(e2c_vec, layout_map, i,
356 num_side_channels -= 2;
359 while (num_back_channels >= 4) {
360 i += assign_pair(e2c_vec, layout_map, i,
364 num_back_channels -= 2;
366 if (num_back_channels >= 2) {
367 i += assign_pair(e2c_vec, layout_map, i,
371 num_back_channels -= 2;
373 if (num_back_channels) {
374 e2c_vec[i] = (struct elem_to_channel) {
375 .av_position = AV_CH_BACK_CENTER,
377 .elem_id = layout_map[i][1],
378 .aac_position = AAC_CHANNEL_BACK
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,
388 .elem_id = layout_map[i][1],
389 .aac_position = AAC_CHANNEL_LFE
393 while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
394 e2c_vec[i] = (struct elem_to_channel) {
395 .av_position = UINT64_MAX,
397 .elem_id = layout_map[i][1],
398 .aac_position = AAC_CHANNEL_LFE
403 // Must choose a stable sort
404 total_non_cc_elements = n = i;
407 for (i = 1; i < n; i++)
408 if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
409 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
416 for (i = 0; i < total_non_cc_elements; i++) {
417 layout_map[i][0] = e2c_vec[i].syn_ele;
418 layout_map[i][1] = e2c_vec[i].elem_id;
419 layout_map[i][2] = e2c_vec[i].aac_position;
420 if (e2c_vec[i].av_position != UINT64_MAX) {
421 layout |= e2c_vec[i].av_position;
429 * Save current output configuration if and only if it has been locked.
431 static void push_output_configuration(AACContext *ac) {
432 if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
433 ac->oc[0] = ac->oc[1];
435 ac->oc[1].status = OC_NONE;
439 * Restore the previous output configuration if and only if the current
440 * configuration is unlocked.
442 static void pop_output_configuration(AACContext *ac) {
443 if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
444 ac->oc[1] = ac->oc[0];
445 ac->avctx->channels = ac->oc[1].channels;
446 ac->avctx->channel_layout = ac->oc[1].channel_layout;
447 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
448 ac->oc[1].status, 0);
453 * Configure output channel order based on the current program
454 * configuration element.
456 * @return Returns error status. 0 - OK, !0 - error
458 static int output_configure(AACContext *ac,
459 uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
460 enum OCStatus oc_type, int get_new_frame)
462 AVCodecContext *avctx = ac->avctx;
463 int i, channels = 0, ret;
465 uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
466 uint8_t type_counts[TYPE_END] = { 0 };
468 if (ac->oc[1].layout_map != layout_map) {
469 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
470 ac->oc[1].layout_map_tags = tags;
472 for (i = 0; i < tags; i++) {
473 int type = layout_map[i][0];
474 int id = layout_map[i][1];
475 id_map[type][id] = type_counts[type]++;
477 // Try to sniff a reasonable channel order, otherwise output the
478 // channels in the order the PCE declared them.
479 if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
480 layout = sniff_channel_order(layout_map, tags);
481 for (i = 0; i < tags; i++) {
482 int type = layout_map[i][0];
483 int id = layout_map[i][1];
484 int iid = id_map[type][id];
485 int position = layout_map[i][2];
486 // Allocate or free elements depending on if they are in the
487 // current program configuration.
488 ret = che_configure(ac, position, type, iid, &channels);
491 ac->tag_che_map[type][id] = ac->che[type][iid];
493 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
494 if (layout == AV_CH_FRONT_CENTER) {
495 layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
501 if (layout) avctx->channel_layout = layout;
502 ac->oc[1].channel_layout = layout;
503 avctx->channels = ac->oc[1].channels = channels;
504 ac->oc[1].status = oc_type;
507 if ((ret = frame_configure_elements(ac->avctx)) < 0)
514 static void flush(AVCodecContext *avctx)
516 AACContext *ac= avctx->priv_data;
519 for (type = 3; type >= 0; type--) {
520 for (i = 0; i < MAX_ELEM_ID; i++) {
521 ChannelElement *che = ac->che[type][i];
523 for (j = 0; j <= 1; j++) {
524 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
532 * Set up channel positions based on a default channel configuration
533 * as specified in table 1.17.
535 * @return Returns error status. 0 - OK, !0 - error
537 static int set_default_channel_config(AVCodecContext *avctx,
538 uint8_t (*layout_map)[3],
542 if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
543 channel_config > 12) {
544 av_log(avctx, AV_LOG_ERROR,
545 "invalid default channel configuration (%d)\n",
547 return AVERROR_INVALIDDATA;
549 *tags = tags_per_config[channel_config];
550 memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
551 *tags * sizeof(*layout_map));
554 * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
555 * However, at least Nero AAC encoder encodes 7.1 streams using the default
556 * channel config 7, mapping the side channels of the original audio stream
557 * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
558 * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
559 * the incorrect streams as if they were correct (and as the encoder intended).
561 * As actual intended 7.1(wide) streams are very rare, default to assuming a
562 * 7.1 layout was intended.
564 if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
565 av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
566 " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
567 " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
568 layout_map[2][2] = AAC_CHANNEL_SIDE;
574 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
576 /* For PCE based channel configurations map the channels solely based
578 if (!ac->oc[1].m4ac.chan_config) {
579 return ac->tag_che_map[type][elem_id];
581 // Allow single CPE stereo files to be signalled with mono configuration.
582 if (!ac->tags_mapped && type == TYPE_CPE &&
583 ac->oc[1].m4ac.chan_config == 1) {
584 uint8_t layout_map[MAX_ELEM_ID*4][3];
586 push_output_configuration(ac);
588 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
590 if (set_default_channel_config(ac->avctx, layout_map,
591 &layout_map_tags, 2) < 0)
593 if (output_configure(ac, layout_map, layout_map_tags,
594 OC_TRIAL_FRAME, 1) < 0)
597 ac->oc[1].m4ac.chan_config = 2;
598 ac->oc[1].m4ac.ps = 0;
601 if (!ac->tags_mapped && type == TYPE_SCE &&
602 ac->oc[1].m4ac.chan_config == 2) {
603 uint8_t layout_map[MAX_ELEM_ID * 4][3];
605 push_output_configuration(ac);
607 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
609 if (set_default_channel_config(ac->avctx, layout_map,
610 &layout_map_tags, 1) < 0)
612 if (output_configure(ac, layout_map, layout_map_tags,
613 OC_TRIAL_FRAME, 1) < 0)
616 ac->oc[1].m4ac.chan_config = 1;
617 if (ac->oc[1].m4ac.sbr)
618 ac->oc[1].m4ac.ps = -1;
620 /* For indexed channel configurations map the channels solely based
622 switch (ac->oc[1].m4ac.chan_config) {
625 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
627 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
630 if (ac->tags_mapped == 2 &&
631 ac->oc[1].m4ac.chan_config == 11 &&
634 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
637 /* Some streams incorrectly code 5.1 audio as
638 * SCE[0] CPE[0] CPE[1] SCE[1]
640 * SCE[0] CPE[0] CPE[1] LFE[0].
641 * If we seem to have encountered such a stream, transfer
642 * the LFE[0] element to the SCE[1]'s mapping */
643 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
644 if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
645 av_log(ac->avctx, AV_LOG_WARNING,
646 "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
647 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
648 ac->warned_remapping_once++;
651 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
654 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
656 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
659 /* Some streams incorrectly code 4.0 audio as
660 * SCE[0] CPE[0] LFE[0]
662 * SCE[0] CPE[0] SCE[1].
663 * If we seem to have encountered such a stream, transfer
664 * the SCE[1] element to the LFE[0]'s mapping */
665 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
666 if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
667 av_log(ac->avctx, AV_LOG_WARNING,
668 "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
669 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
670 ac->warned_remapping_once++;
673 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
675 if (ac->tags_mapped == 2 &&
676 ac->oc[1].m4ac.chan_config == 4 &&
679 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
683 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
686 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
687 } else if (ac->oc[1].m4ac.chan_config == 2) {
691 if (!ac->tags_mapped && type == TYPE_SCE) {
693 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
701 * Decode an array of 4 bit element IDs, optionally interleaved with a
702 * stereo/mono switching bit.
704 * @param type speaker type/position for these channels
706 static void decode_channel_map(uint8_t layout_map[][3],
707 enum ChannelPosition type,
708 GetBitContext *gb, int n)
711 enum RawDataBlockType syn_ele;
713 case AAC_CHANNEL_FRONT:
714 case AAC_CHANNEL_BACK:
715 case AAC_CHANNEL_SIDE:
716 syn_ele = get_bits1(gb);
722 case AAC_CHANNEL_LFE:
726 // AAC_CHANNEL_OFF has no channel map
729 layout_map[0][0] = syn_ele;
730 layout_map[0][1] = get_bits(gb, 4);
731 layout_map[0][2] = type;
737 * Decode program configuration element; reference: table 4.2.
739 * @return Returns error status. 0 - OK, !0 - error
741 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
742 uint8_t (*layout_map)[3],
745 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
750 skip_bits(gb, 2); // object_type
752 sampling_index = get_bits(gb, 4);
753 if (m4ac->sampling_index != sampling_index)
754 av_log(avctx, AV_LOG_WARNING,
755 "Sample rate index in program config element does not "
756 "match the sample rate index configured by the container.\n");
758 num_front = get_bits(gb, 4);
759 num_side = get_bits(gb, 4);
760 num_back = get_bits(gb, 4);
761 num_lfe = get_bits(gb, 2);
762 num_assoc_data = get_bits(gb, 3);
763 num_cc = get_bits(gb, 4);
766 skip_bits(gb, 4); // mono_mixdown_tag
768 skip_bits(gb, 4); // stereo_mixdown_tag
771 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
773 if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
774 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
777 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
779 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
781 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
783 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
786 skip_bits_long(gb, 4 * num_assoc_data);
788 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
793 /* comment field, first byte is length */
794 comment_len = get_bits(gb, 8) * 8;
795 if (get_bits_left(gb) < comment_len) {
796 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
797 return AVERROR_INVALIDDATA;
799 skip_bits_long(gb, comment_len);
804 * Decode GA "General Audio" specific configuration; reference: table 4.1.
806 * @param ac pointer to AACContext, may be null
807 * @param avctx pointer to AVCCodecContext, used for logging
809 * @return Returns error status. 0 - OK, !0 - error
811 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
813 MPEG4AudioConfig *m4ac,
816 int extension_flag, ret, ep_config, res_flags;
817 uint8_t layout_map[MAX_ELEM_ID*4][3];
820 if (get_bits1(gb)) { // frameLengthFlag
821 avpriv_request_sample(avctx, "960/120 MDCT window");
822 return AVERROR_PATCHWELCOME;
824 m4ac->frame_length_short = 0;
826 if (get_bits1(gb)) // dependsOnCoreCoder
827 skip_bits(gb, 14); // coreCoderDelay
828 extension_flag = get_bits1(gb);
830 if (m4ac->object_type == AOT_AAC_SCALABLE ||
831 m4ac->object_type == AOT_ER_AAC_SCALABLE)
832 skip_bits(gb, 3); // layerNr
834 if (channel_config == 0) {
835 skip_bits(gb, 4); // element_instance_tag
836 tags = decode_pce(avctx, m4ac, layout_map, gb);
840 if ((ret = set_default_channel_config(avctx, layout_map,
841 &tags, channel_config)))
845 if (count_channels(layout_map, tags) > 1) {
847 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
850 if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
853 if (extension_flag) {
854 switch (m4ac->object_type) {
856 skip_bits(gb, 5); // numOfSubFrame
857 skip_bits(gb, 11); // layer_length
861 case AOT_ER_AAC_SCALABLE:
863 res_flags = get_bits(gb, 3);
865 avpriv_report_missing_feature(avctx,
866 "AAC data resilience (flags %x)",
868 return AVERROR_PATCHWELCOME;
872 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
874 switch (m4ac->object_type) {
877 case AOT_ER_AAC_SCALABLE:
879 ep_config = get_bits(gb, 2);
881 avpriv_report_missing_feature(avctx,
882 "epConfig %d", ep_config);
883 return AVERROR_PATCHWELCOME;
889 static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
891 MPEG4AudioConfig *m4ac,
894 int ret, ep_config, res_flags;
895 uint8_t layout_map[MAX_ELEM_ID*4][3];
897 const int ELDEXT_TERM = 0;
902 m4ac->frame_length_short = get_bits1(gb);
903 res_flags = get_bits(gb, 3);
905 avpriv_report_missing_feature(avctx,
906 "AAC data resilience (flags %x)",
908 return AVERROR_PATCHWELCOME;
911 if (get_bits1(gb)) { // ldSbrPresentFlag
912 avpriv_report_missing_feature(avctx,
914 return AVERROR_PATCHWELCOME;
917 while (get_bits(gb, 4) != ELDEXT_TERM) {
918 int len = get_bits(gb, 4);
920 len += get_bits(gb, 8);
922 len += get_bits(gb, 16);
923 if (get_bits_left(gb) < len * 8 + 4) {
924 av_log(avctx, AV_LOG_ERROR, overread_err);
925 return AVERROR_INVALIDDATA;
927 skip_bits_long(gb, 8 * len);
930 if ((ret = set_default_channel_config(avctx, layout_map,
931 &tags, channel_config)))
934 if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
937 ep_config = get_bits(gb, 2);
939 avpriv_report_missing_feature(avctx,
940 "epConfig %d", ep_config);
941 return AVERROR_PATCHWELCOME;
947 * Decode audio specific configuration; reference: table 1.13.
949 * @param ac pointer to AACContext, may be null
950 * @param avctx pointer to AVCCodecContext, used for logging
951 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
952 * @param data pointer to buffer holding an audio specific config
953 * @param bit_size size of audio specific config or data in bits
954 * @param sync_extension look for an appended sync extension
956 * @return Returns error status or number of consumed bits. <0 - error
958 static int decode_audio_specific_config(AACContext *ac,
959 AVCodecContext *avctx,
960 MPEG4AudioConfig *m4ac,
961 const uint8_t *data, int bit_size,
967 ff_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
968 for (i = 0; i < bit_size >> 3; i++)
969 ff_dlog(avctx, "%02x ", data[i]);
970 ff_dlog(avctx, "\n");
972 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
975 if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
976 sync_extension)) < 0)
977 return AVERROR_INVALIDDATA;
978 if (m4ac->sampling_index > 12) {
979 av_log(avctx, AV_LOG_ERROR,
980 "invalid sampling rate index %d\n",
981 m4ac->sampling_index);
982 return AVERROR_INVALIDDATA;
984 if (m4ac->object_type == AOT_ER_AAC_LD &&
985 (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
986 av_log(avctx, AV_LOG_ERROR,
987 "invalid low delay sampling rate index %d\n",
988 m4ac->sampling_index);
989 return AVERROR_INVALIDDATA;
992 skip_bits_long(&gb, i);
994 switch (m4ac->object_type) {
1000 if ((ret = decode_ga_specific_config(ac, avctx, &gb,
1001 m4ac, m4ac->chan_config)) < 0)
1004 case AOT_ER_AAC_ELD:
1005 if ((ret = decode_eld_specific_config(ac, avctx, &gb,
1006 m4ac, m4ac->chan_config)) < 0)
1010 avpriv_report_missing_feature(avctx,
1011 "Audio object type %s%d",
1012 m4ac->sbr == 1 ? "SBR+" : "",
1014 return AVERROR(ENOSYS);
1018 "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1019 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1020 m4ac->sample_rate, m4ac->sbr,
1023 return get_bits_count(&gb);
1027 * linear congruential pseudorandom number generator
1029 * @param previous_val pointer to the current state of the generator
1031 * @return Returns a 32-bit pseudorandom integer
1033 static av_always_inline int lcg_random(unsigned previous_val)
1035 union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
1039 static av_always_inline void reset_predict_state(PredictorState *ps)
1049 static void reset_all_predictors(PredictorState *ps)
1052 for (i = 0; i < MAX_PREDICTORS; i++)
1053 reset_predict_state(&ps[i]);
1056 static int sample_rate_idx (int rate)
1058 if (92017 <= rate) return 0;
1059 else if (75132 <= rate) return 1;
1060 else if (55426 <= rate) return 2;
1061 else if (46009 <= rate) return 3;
1062 else if (37566 <= rate) return 4;
1063 else if (27713 <= rate) return 5;
1064 else if (23004 <= rate) return 6;
1065 else if (18783 <= rate) return 7;
1066 else if (13856 <= rate) return 8;
1067 else if (11502 <= rate) return 9;
1068 else if (9391 <= rate) return 10;
1072 static void reset_predictor_group(PredictorState *ps, int group_num)
1075 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
1076 reset_predict_state(&ps[i]);
1079 #define AAC_INIT_VLC_STATIC(num, size) \
1080 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
1081 ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
1082 sizeof(ff_aac_spectral_bits[num][0]), \
1083 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
1084 sizeof(ff_aac_spectral_codes[num][0]), \
1087 static void aacdec_init(AACContext *ac);
1089 static av_cold int aac_decode_init(AVCodecContext *avctx)
1091 AACContext *ac = avctx->priv_data;
1095 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1099 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1101 if (avctx->extradata_size > 0) {
1102 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
1104 avctx->extradata_size * 8,
1109 uint8_t layout_map[MAX_ELEM_ID*4][3];
1110 int layout_map_tags;
1112 sr = sample_rate_idx(avctx->sample_rate);
1113 ac->oc[1].m4ac.sampling_index = sr;
1114 ac->oc[1].m4ac.channels = avctx->channels;
1115 ac->oc[1].m4ac.sbr = -1;
1116 ac->oc[1].m4ac.ps = -1;
1118 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1119 if (ff_mpeg4audio_channels[i] == avctx->channels)
1121 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
1124 ac->oc[1].m4ac.chan_config = i;
1126 if (ac->oc[1].m4ac.chan_config) {
1127 int ret = set_default_channel_config(avctx, layout_map,
1128 &layout_map_tags, ac->oc[1].m4ac.chan_config);
1130 output_configure(ac, layout_map, layout_map_tags,
1132 else if (avctx->err_recognition & AV_EF_EXPLODE)
1133 return AVERROR_INVALIDDATA;
1137 if (avctx->channels > MAX_CHANNELS) {
1138 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1139 return AVERROR_INVALIDDATA;
1142 AAC_INIT_VLC_STATIC( 0, 304);
1143 AAC_INIT_VLC_STATIC( 1, 270);
1144 AAC_INIT_VLC_STATIC( 2, 550);
1145 AAC_INIT_VLC_STATIC( 3, 300);
1146 AAC_INIT_VLC_STATIC( 4, 328);
1147 AAC_INIT_VLC_STATIC( 5, 294);
1148 AAC_INIT_VLC_STATIC( 6, 306);
1149 AAC_INIT_VLC_STATIC( 7, 268);
1150 AAC_INIT_VLC_STATIC( 8, 510);
1151 AAC_INIT_VLC_STATIC( 9, 366);
1152 AAC_INIT_VLC_STATIC(10, 462);
1156 ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
1158 return AVERROR(ENOMEM);
1161 ac->random_state = 0x1f2e3d4c;
1165 INIT_VLC_STATIC(&vlc_scalefactors, 7,
1166 FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
1167 ff_aac_scalefactor_bits,
1168 sizeof(ff_aac_scalefactor_bits[0]),
1169 sizeof(ff_aac_scalefactor_bits[0]),
1170 ff_aac_scalefactor_code,
1171 sizeof(ff_aac_scalefactor_code[0]),
1172 sizeof(ff_aac_scalefactor_code[0]),
1175 ff_mdct_init(&ac->mdct, 11, 1, 1.0 / (32768.0 * 1024.0));
1176 ff_mdct_init(&ac->mdct_ld, 10, 1, 1.0 / (32768.0 * 512.0));
1177 ff_mdct_init(&ac->mdct_small, 8, 1, 1.0 / (32768.0 * 128.0));
1178 ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0 * 32768.0);
1179 ret = ff_imdct15_init(&ac->mdct480, 5);
1183 // window initialization
1184 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
1185 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
1186 ff_init_ff_sine_windows(10);
1187 ff_init_ff_sine_windows( 9);
1188 ff_init_ff_sine_windows( 7);
1196 * Skip data_stream_element; reference: table 4.10.
1198 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
1200 int byte_align = get_bits1(gb);
1201 int count = get_bits(gb, 8);
1203 count += get_bits(gb, 8);
1207 if (get_bits_left(gb) < 8 * count) {
1208 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1209 return AVERROR_INVALIDDATA;
1211 skip_bits_long(gb, 8 * count);
1215 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
1219 if (get_bits1(gb)) {
1220 ics->predictor_reset_group = get_bits(gb, 5);
1221 if (ics->predictor_reset_group == 0 ||
1222 ics->predictor_reset_group > 30) {
1223 av_log(ac->avctx, AV_LOG_ERROR,
1224 "Invalid Predictor Reset Group.\n");
1225 return AVERROR_INVALIDDATA;
1228 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1229 ics->prediction_used[sfb] = get_bits1(gb);
1235 * Decode Long Term Prediction data; reference: table 4.xx.
1237 static void decode_ltp(LongTermPrediction *ltp,
1238 GetBitContext *gb, uint8_t max_sfb)
1242 ltp->lag = get_bits(gb, 11);
1243 ltp->coef = ltp_coef[get_bits(gb, 3)];
1244 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1245 ltp->used[sfb] = get_bits1(gb);
1249 * Decode Individual Channel Stream info; reference: table 4.6.
1251 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
1254 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1255 const int aot = m4ac->object_type;
1256 const int sampling_index = m4ac->sampling_index;
1257 if (aot != AOT_ER_AAC_ELD) {
1258 if (get_bits1(gb)) {
1259 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1260 if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
1261 return AVERROR_INVALIDDATA;
1263 ics->window_sequence[1] = ics->window_sequence[0];
1264 ics->window_sequence[0] = get_bits(gb, 2);
1265 if (aot == AOT_ER_AAC_LD &&
1266 ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1267 av_log(ac->avctx, AV_LOG_ERROR,
1268 "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1269 "window sequence %d found.\n", ics->window_sequence[0]);
1270 ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
1271 return AVERROR_INVALIDDATA;
1273 ics->use_kb_window[1] = ics->use_kb_window[0];
1274 ics->use_kb_window[0] = get_bits1(gb);
1276 ics->num_window_groups = 1;
1277 ics->group_len[0] = 1;
1278 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1280 ics->max_sfb = get_bits(gb, 4);
1281 for (i = 0; i < 7; i++) {
1282 if (get_bits1(gb)) {
1283 ics->group_len[ics->num_window_groups - 1]++;
1285 ics->num_window_groups++;
1286 ics->group_len[ics->num_window_groups - 1] = 1;
1289 ics->num_windows = 8;
1290 ics->swb_offset = ff_swb_offset_128[sampling_index];
1291 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1292 ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1293 ics->predictor_present = 0;
1295 ics->max_sfb = get_bits(gb, 6);
1296 ics->num_windows = 1;
1297 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1298 if (m4ac->frame_length_short) {
1299 ics->swb_offset = ff_swb_offset_480[sampling_index];
1300 ics->num_swb = ff_aac_num_swb_480[sampling_index];
1301 ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1303 ics->swb_offset = ff_swb_offset_512[sampling_index];
1304 ics->num_swb = ff_aac_num_swb_512[sampling_index];
1305 ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1307 if (!ics->num_swb || !ics->swb_offset)
1310 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1311 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1312 ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1314 if (aot != AOT_ER_AAC_ELD) {
1315 ics->predictor_present = get_bits1(gb);
1316 ics->predictor_reset_group = 0;
1318 if (ics->predictor_present) {
1319 if (aot == AOT_AAC_MAIN) {
1320 if (decode_prediction(ac, ics, gb)) {
1323 } else if (aot == AOT_AAC_LC ||
1324 aot == AOT_ER_AAC_LC) {
1325 av_log(ac->avctx, AV_LOG_ERROR,
1326 "Prediction is not allowed in AAC-LC.\n");
1329 if (aot == AOT_ER_AAC_LD) {
1330 av_log(ac->avctx, AV_LOG_ERROR,
1331 "LTP in ER AAC LD not yet implemented.\n");
1332 return AVERROR_PATCHWELCOME;
1334 if ((ics->ltp.present = get_bits(gb, 1)))
1335 decode_ltp(&ics->ltp, gb, ics->max_sfb);
1340 if (ics->max_sfb > ics->num_swb) {
1341 av_log(ac->avctx, AV_LOG_ERROR,
1342 "Number of scalefactor bands in group (%d) "
1343 "exceeds limit (%d).\n",
1344 ics->max_sfb, ics->num_swb);
1351 return AVERROR_INVALIDDATA;
1355 * Decode band types (section_data payload); reference: table 4.46.
1357 * @param band_type array of the used band type
1358 * @param band_type_run_end array of the last scalefactor band of a band type run
1360 * @return Returns error status. 0 - OK, !0 - error
1362 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1363 int band_type_run_end[120], GetBitContext *gb,
1364 IndividualChannelStream *ics)
1367 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1368 for (g = 0; g < ics->num_window_groups; g++) {
1370 while (k < ics->max_sfb) {
1371 uint8_t sect_end = k;
1373 int sect_band_type = get_bits(gb, 4);
1374 if (sect_band_type == 12) {
1375 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1376 return AVERROR_INVALIDDATA;
1379 sect_len_incr = get_bits(gb, bits);
1380 sect_end += sect_len_incr;
1381 if (get_bits_left(gb) < 0) {
1382 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1383 return AVERROR_INVALIDDATA;
1385 if (sect_end > ics->max_sfb) {
1386 av_log(ac->avctx, AV_LOG_ERROR,
1387 "Number of bands (%d) exceeds limit (%d).\n",
1388 sect_end, ics->max_sfb);
1389 return AVERROR_INVALIDDATA;
1391 } while (sect_len_incr == (1 << bits) - 1);
1392 for (; k < sect_end; k++) {
1393 band_type [idx] = sect_band_type;
1394 band_type_run_end[idx++] = sect_end;
1402 * Decode scalefactors; reference: table 4.47.
1404 * @param global_gain first scalefactor value as scalefactors are differentially coded
1405 * @param band_type array of the used band type
1406 * @param band_type_run_end array of the last scalefactor band of a band type run
1407 * @param sf array of scalefactors or intensity stereo positions
1409 * @return Returns error status. 0 - OK, !0 - error
1411 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
1412 unsigned int global_gain,
1413 IndividualChannelStream *ics,
1414 enum BandType band_type[120],
1415 int band_type_run_end[120])
1418 int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1421 for (g = 0; g < ics->num_window_groups; g++) {
1422 for (i = 0; i < ics->max_sfb;) {
1423 int run_end = band_type_run_end[idx];
1424 if (band_type[idx] == ZERO_BT) {
1425 for (; i < run_end; i++, idx++)
1427 } else if ((band_type[idx] == INTENSITY_BT) ||
1428 (band_type[idx] == INTENSITY_BT2)) {
1429 for (; i < run_end; i++, idx++) {
1430 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1431 clipped_offset = av_clip(offset[2], -155, 100);
1432 if (offset[2] != clipped_offset) {
1433 avpriv_request_sample(ac->avctx,
1434 "If you heard an audible artifact, there may be a bug in the decoder. "
1435 "Clipped intensity stereo position (%d -> %d)",
1436 offset[2], clipped_offset);
1438 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1440 } else if (band_type[idx] == NOISE_BT) {
1441 for (; i < run_end; i++, idx++) {
1442 if (noise_flag-- > 0)
1443 offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1445 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1446 clipped_offset = av_clip(offset[1], -100, 155);
1447 if (offset[1] != clipped_offset) {
1448 avpriv_request_sample(ac->avctx,
1449 "If you heard an audible artifact, there may be a bug in the decoder. "
1450 "Clipped noise gain (%d -> %d)",
1451 offset[1], clipped_offset);
1453 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1456 for (; i < run_end; i++, idx++) {
1457 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1458 if (offset[0] > 255U) {
1459 av_log(ac->avctx, AV_LOG_ERROR,
1460 "Scalefactor (%d) out of range.\n", offset[0]);
1461 return AVERROR_INVALIDDATA;
1463 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1472 * Decode pulse data; reference: table 4.7.
1474 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1475 const uint16_t *swb_offset, int num_swb)
1478 pulse->num_pulse = get_bits(gb, 2) + 1;
1479 pulse_swb = get_bits(gb, 6);
1480 if (pulse_swb >= num_swb)
1482 pulse->pos[0] = swb_offset[pulse_swb];
1483 pulse->pos[0] += get_bits(gb, 5);
1484 if (pulse->pos[0] >= swb_offset[num_swb])
1486 pulse->amp[0] = get_bits(gb, 4);
1487 for (i = 1; i < pulse->num_pulse; i++) {
1488 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1489 if (pulse->pos[i] >= swb_offset[num_swb])
1491 pulse->amp[i] = get_bits(gb, 4);
1497 * Decode Temporal Noise Shaping data; reference: table 4.48.
1499 * @return Returns error status. 0 - OK, !0 - error
1501 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
1502 GetBitContext *gb, const IndividualChannelStream *ics)
1504 int w, filt, i, coef_len, coef_res, coef_compress;
1505 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1506 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1507 for (w = 0; w < ics->num_windows; w++) {
1508 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1509 coef_res = get_bits1(gb);
1511 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1513 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1515 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1516 av_log(ac->avctx, AV_LOG_ERROR,
1517 "TNS filter order %d is greater than maximum %d.\n",
1518 tns->order[w][filt], tns_max_order);
1519 tns->order[w][filt] = 0;
1520 return AVERROR_INVALIDDATA;
1522 if (tns->order[w][filt]) {
1523 tns->direction[w][filt] = get_bits1(gb);
1524 coef_compress = get_bits1(gb);
1525 coef_len = coef_res + 3 - coef_compress;
1526 tmp2_idx = 2 * coef_compress + coef_res;
1528 for (i = 0; i < tns->order[w][filt]; i++)
1529 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1538 * Decode Mid/Side data; reference: table 4.54.
1540 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1541 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1542 * [3] reserved for scalable AAC
1544 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1548 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1549 if (ms_present == 1) {
1550 for (idx = 0; idx < max_idx; idx++)
1551 cpe->ms_mask[idx] = get_bits1(gb);
1552 } else if (ms_present == 2) {
1553 memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1558 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
1562 *dst++ = v[idx & 15] * s;
1563 *dst++ = v[idx>>4 & 15] * s;
1569 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
1573 *dst++ = v[idx & 3] * s;
1574 *dst++ = v[idx>>2 & 3] * s;
1575 *dst++ = v[idx>>4 & 3] * s;
1576 *dst++ = v[idx>>6 & 3] * s;
1582 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
1583 unsigned sign, const float *scale)
1585 union av_intfloat32 s0, s1;
1587 s0.f = s1.f = *scale;
1588 s0.i ^= sign >> 1 << 31;
1591 *dst++ = v[idx & 15] * s0.f;
1592 *dst++ = v[idx>>4 & 15] * s1.f;
1599 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
1600 unsigned sign, const float *scale)
1602 unsigned nz = idx >> 12;
1603 union av_intfloat32 s = { .f = *scale };
1604 union av_intfloat32 t;
1606 t.i = s.i ^ (sign & 1U<<31);
1607 *dst++ = v[idx & 3] * t.f;
1609 sign <<= nz & 1; nz >>= 1;
1610 t.i = s.i ^ (sign & 1U<<31);
1611 *dst++ = v[idx>>2 & 3] * t.f;
1613 sign <<= nz & 1; nz >>= 1;
1614 t.i = s.i ^ (sign & 1U<<31);
1615 *dst++ = v[idx>>4 & 3] * t.f;
1618 t.i = s.i ^ (sign & 1U<<31);
1619 *dst++ = v[idx>>6 & 3] * t.f;
1626 * Decode spectral data; reference: table 4.50.
1627 * Dequantize and scale spectral data; reference: 4.6.3.3.
1629 * @param coef array of dequantized, scaled spectral data
1630 * @param sf array of scalefactors or intensity stereo positions
1631 * @param pulse_present set if pulses are present
1632 * @param pulse pointer to pulse data struct
1633 * @param band_type array of the used band type
1635 * @return Returns error status. 0 - OK, !0 - error
1637 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
1638 GetBitContext *gb, const float sf[120],
1639 int pulse_present, const Pulse *pulse,
1640 const IndividualChannelStream *ics,
1641 enum BandType band_type[120])
1643 int i, k, g, idx = 0;
1644 const int c = 1024 / ics->num_windows;
1645 const uint16_t *offsets = ics->swb_offset;
1646 float *coef_base = coef;
1648 for (g = 0; g < ics->num_windows; g++)
1649 memset(coef + g * 128 + offsets[ics->max_sfb], 0,
1650 sizeof(float) * (c - offsets[ics->max_sfb]));
1652 for (g = 0; g < ics->num_window_groups; g++) {
1653 unsigned g_len = ics->group_len[g];
1655 for (i = 0; i < ics->max_sfb; i++, idx++) {
1656 const unsigned cbt_m1 = band_type[idx] - 1;
1657 float *cfo = coef + offsets[i];
1658 int off_len = offsets[i + 1] - offsets[i];
1661 if (cbt_m1 >= INTENSITY_BT2 - 1) {
1662 for (group = 0; group < g_len; group++, cfo+=128) {
1663 memset(cfo, 0, off_len * sizeof(float));
1665 } else if (cbt_m1 == NOISE_BT - 1) {
1666 for (group = 0; group < g_len; group++, cfo+=128) {
1670 for (k = 0; k < off_len; k++) {
1671 ac->random_state = lcg_random(ac->random_state);
1672 cfo[k] = ac->random_state;
1675 band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
1676 scale = sf[idx] / sqrtf(band_energy);
1677 ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
1680 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1681 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1682 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1683 OPEN_READER(re, gb);
1685 switch (cbt_m1 >> 1) {
1687 for (group = 0; group < g_len; group++, cfo+=128) {
1695 UPDATE_CACHE(re, gb);
1696 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1697 cb_idx = cb_vector_idx[code];
1698 cf = VMUL4(cf, vq, cb_idx, sf + idx);
1704 for (group = 0; group < g_len; group++, cfo+=128) {
1714 UPDATE_CACHE(re, gb);
1715 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1716 cb_idx = cb_vector_idx[code];
1717 nnz = cb_idx >> 8 & 15;
1718 bits = nnz ? GET_CACHE(re, gb) : 0;
1719 LAST_SKIP_BITS(re, gb, nnz);
1720 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1726 for (group = 0; group < g_len; group++, cfo+=128) {
1734 UPDATE_CACHE(re, gb);
1735 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1736 cb_idx = cb_vector_idx[code];
1737 cf = VMUL2(cf, vq, cb_idx, sf + idx);
1744 for (group = 0; group < g_len; group++, cfo+=128) {
1754 UPDATE_CACHE(re, gb);
1755 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1756 cb_idx = cb_vector_idx[code];
1757 nnz = cb_idx >> 8 & 15;
1758 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1759 LAST_SKIP_BITS(re, gb, nnz);
1760 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1766 for (group = 0; group < g_len; group++, cfo+=128) {
1768 uint32_t *icf = (uint32_t *) cf;
1778 UPDATE_CACHE(re, gb);
1779 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1787 cb_idx = cb_vector_idx[code];
1790 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1791 LAST_SKIP_BITS(re, gb, nnz);
1793 for (j = 0; j < 2; j++) {
1797 /* The total length of escape_sequence must be < 22 bits according
1798 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1799 UPDATE_CACHE(re, gb);
1800 b = GET_CACHE(re, gb);
1801 b = 31 - av_log2(~b);
1804 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1805 return AVERROR_INVALIDDATA;
1808 SKIP_BITS(re, gb, b + 1);
1810 n = (1 << b) + SHOW_UBITS(re, gb, b);
1811 LAST_SKIP_BITS(re, gb, b);
1812 *icf++ = cbrt_tab[n] | (bits & 1U<<31);
1815 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1816 *icf++ = (bits & 1U<<31) | v;
1823 ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1827 CLOSE_READER(re, gb);
1833 if (pulse_present) {
1835 for (i = 0; i < pulse->num_pulse; i++) {
1836 float co = coef_base[ pulse->pos[i] ];
1837 while (offsets[idx + 1] <= pulse->pos[i])
1839 if (band_type[idx] != NOISE_BT && sf[idx]) {
1840 float ico = -pulse->amp[i];
1843 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1845 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1852 static av_always_inline float flt16_round(float pf)
1854 union av_intfloat32 tmp;
1856 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1860 static av_always_inline float flt16_even(float pf)
1862 union av_intfloat32 tmp;
1864 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1868 static av_always_inline float flt16_trunc(float pf)
1870 union av_intfloat32 pun;
1872 pun.i &= 0xFFFF0000U;
1876 static av_always_inline void predict(PredictorState *ps, float *coef,
1879 const float a = 0.953125; // 61.0 / 64
1880 const float alpha = 0.90625; // 29.0 / 32
1884 float r0 = ps->r0, r1 = ps->r1;
1885 float cor0 = ps->cor0, cor1 = ps->cor1;
1886 float var0 = ps->var0, var1 = ps->var1;
1888 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
1889 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
1891 pv = flt16_round(k1 * r0 + k2 * r1);
1898 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
1899 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
1900 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
1901 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
1903 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
1904 ps->r0 = flt16_trunc(a * e0);
1908 * Apply AAC-Main style frequency domain prediction.
1910 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1914 if (!sce->ics.predictor_initialized) {
1915 reset_all_predictors(sce->predictor_state);
1916 sce->ics.predictor_initialized = 1;
1919 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1921 sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
1923 for (k = sce->ics.swb_offset[sfb];
1924 k < sce->ics.swb_offset[sfb + 1];
1926 predict(&sce->predictor_state[k], &sce->coeffs[k],
1927 sce->ics.predictor_present &&
1928 sce->ics.prediction_used[sfb]);
1931 if (sce->ics.predictor_reset_group)
1932 reset_predictor_group(sce->predictor_state,
1933 sce->ics.predictor_reset_group);
1935 reset_all_predictors(sce->predictor_state);
1939 * Decode an individual_channel_stream payload; reference: table 4.44.
1941 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1942 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1944 * @return Returns error status. 0 - OK, !0 - error
1946 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1947 GetBitContext *gb, int common_window, int scale_flag)
1950 TemporalNoiseShaping *tns = &sce->tns;
1951 IndividualChannelStream *ics = &sce->ics;
1952 float *out = sce->coeffs;
1953 int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1956 eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1957 er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1958 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1959 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1960 ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1962 /* This assignment is to silence a GCC warning about the variable being used
1963 * uninitialized when in fact it always is.
1965 pulse.num_pulse = 0;
1967 global_gain = get_bits(gb, 8);
1969 if (!common_window && !scale_flag) {
1970 if (decode_ics_info(ac, ics, gb) < 0)
1971 return AVERROR_INVALIDDATA;
1974 if ((ret = decode_band_types(ac, sce->band_type,
1975 sce->band_type_run_end, gb, ics)) < 0)
1977 if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
1978 sce->band_type, sce->band_type_run_end)) < 0)
1983 if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1984 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1985 av_log(ac->avctx, AV_LOG_ERROR,
1986 "Pulse tool not allowed in eight short sequence.\n");
1987 return AVERROR_INVALIDDATA;
1989 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1990 av_log(ac->avctx, AV_LOG_ERROR,
1991 "Pulse data corrupt or invalid.\n");
1992 return AVERROR_INVALIDDATA;
1995 tns->present = get_bits1(gb);
1996 if (tns->present && !er_syntax)
1997 if (decode_tns(ac, tns, gb, ics) < 0)
1998 return AVERROR_INVALIDDATA;
1999 if (!eld_syntax && get_bits1(gb)) {
2000 avpriv_request_sample(ac->avctx, "SSR");
2001 return AVERROR_PATCHWELCOME;
2003 // I see no textual basis in the spec for this occurring after SSR gain
2004 // control, but this is what both reference and real implmentations do
2005 if (tns->present && er_syntax)
2006 if (decode_tns(ac, tns, gb, ics) < 0)
2007 return AVERROR_INVALIDDATA;
2010 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
2011 &pulse, ics, sce->band_type) < 0)
2012 return AVERROR_INVALIDDATA;
2014 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
2015 apply_prediction(ac, sce);
2021 * Mid/Side stereo decoding; reference: 4.6.8.1.3.
2023 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
2025 const IndividualChannelStream *ics = &cpe->ch[0].ics;
2026 float *ch0 = cpe->ch[0].coeffs;
2027 float *ch1 = cpe->ch[1].coeffs;
2028 int g, i, group, idx = 0;
2029 const uint16_t *offsets = ics->swb_offset;
2030 for (g = 0; g < ics->num_window_groups; g++) {
2031 for (i = 0; i < ics->max_sfb; i++, idx++) {
2032 if (cpe->ms_mask[idx] &&
2033 cpe->ch[0].band_type[idx] < NOISE_BT &&
2034 cpe->ch[1].band_type[idx] < NOISE_BT) {
2035 for (group = 0; group < ics->group_len[g]; group++) {
2036 ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
2037 ch1 + group * 128 + offsets[i],
2038 offsets[i+1] - offsets[i]);
2042 ch0 += ics->group_len[g] * 128;
2043 ch1 += ics->group_len[g] * 128;
2048 * intensity stereo decoding; reference: 4.6.8.2.3
2050 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
2051 * [1] mask is decoded from bitstream; [2] mask is all 1s;
2052 * [3] reserved for scalable AAC
2054 static void apply_intensity_stereo(AACContext *ac,
2055 ChannelElement *cpe, int ms_present)
2057 const IndividualChannelStream *ics = &cpe->ch[1].ics;
2058 SingleChannelElement *sce1 = &cpe->ch[1];
2059 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
2060 const uint16_t *offsets = ics->swb_offset;
2061 int g, group, i, idx = 0;
2064 for (g = 0; g < ics->num_window_groups; g++) {
2065 for (i = 0; i < ics->max_sfb;) {
2066 if (sce1->band_type[idx] == INTENSITY_BT ||
2067 sce1->band_type[idx] == INTENSITY_BT2) {
2068 const int bt_run_end = sce1->band_type_run_end[idx];
2069 for (; i < bt_run_end; i++, idx++) {
2070 c = -1 + 2 * (sce1->band_type[idx] - 14);
2072 c *= 1 - 2 * cpe->ms_mask[idx];
2073 scale = c * sce1->sf[idx];
2074 for (group = 0; group < ics->group_len[g]; group++)
2075 ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
2076 coef0 + group * 128 + offsets[i],
2078 offsets[i + 1] - offsets[i]);
2081 int bt_run_end = sce1->band_type_run_end[idx];
2082 idx += bt_run_end - i;
2086 coef0 += ics->group_len[g] * 128;
2087 coef1 += ics->group_len[g] * 128;
2092 * Decode a channel_pair_element; reference: table 4.4.
2094 * @return Returns error status. 0 - OK, !0 - error
2096 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
2098 int i, ret, common_window, ms_present = 0;
2099 int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2101 common_window = eld_syntax || get_bits1(gb);
2102 if (common_window) {
2103 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
2104 return AVERROR_INVALIDDATA;
2105 i = cpe->ch[1].ics.use_kb_window[0];
2106 cpe->ch[1].ics = cpe->ch[0].ics;
2107 cpe->ch[1].ics.use_kb_window[1] = i;
2108 if (cpe->ch[1].ics.predictor_present &&
2109 (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
2110 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
2111 decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
2112 ms_present = get_bits(gb, 2);
2113 if (ms_present == 3) {
2114 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
2115 return AVERROR_INVALIDDATA;
2116 } else if (ms_present)
2117 decode_mid_side_stereo(cpe, gb, ms_present);
2119 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
2121 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
2124 if (common_window) {
2126 apply_mid_side_stereo(ac, cpe);
2127 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
2128 apply_prediction(ac, &cpe->ch[0]);
2129 apply_prediction(ac, &cpe->ch[1]);
2133 apply_intensity_stereo(ac, cpe, ms_present);
2137 static const float cce_scale[] = {
2138 1.09050773266525765921, //2^(1/8)
2139 1.18920711500272106672, //2^(1/4)
2145 * Decode coupling_channel_element; reference: table 4.8.
2147 * @return Returns error status. 0 - OK, !0 - error
2149 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
2155 SingleChannelElement *sce = &che->ch[0];
2156 ChannelCoupling *coup = &che->coup;
2158 coup->coupling_point = 2 * get_bits1(gb);
2159 coup->num_coupled = get_bits(gb, 3);
2160 for (c = 0; c <= coup->num_coupled; c++) {
2162 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
2163 coup->id_select[c] = get_bits(gb, 4);
2164 if (coup->type[c] == TYPE_CPE) {
2165 coup->ch_select[c] = get_bits(gb, 2);
2166 if (coup->ch_select[c] == 3)
2169 coup->ch_select[c] = 2;
2171 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
2173 sign = get_bits(gb, 1);
2174 scale = cce_scale[get_bits(gb, 2)];
2176 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
2179 for (c = 0; c < num_gain; c++) {
2183 float gain_cache = 1.0;
2185 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
2186 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
2187 gain_cache = powf(scale, -gain);
2189 if (coup->coupling_point == AFTER_IMDCT) {
2190 coup->gain[c][0] = gain_cache;
2192 for (g = 0; g < sce->ics.num_window_groups; g++) {
2193 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
2194 if (sce->band_type[idx] != ZERO_BT) {
2196 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
2204 gain_cache = powf(scale, -t) * s;
2207 coup->gain[c][idx] = gain_cache;
2217 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
2219 * @return Returns number of bytes consumed.
2221 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
2225 int num_excl_chan = 0;
2228 for (i = 0; i < 7; i++)
2229 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
2230 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
2232 return num_excl_chan / 7;
2236 * Decode dynamic range information; reference: table 4.52.
2238 * @return Returns number of bytes consumed.
2240 static int decode_dynamic_range(DynamicRangeControl *che_drc,
2244 int drc_num_bands = 1;
2247 /* pce_tag_present? */
2248 if (get_bits1(gb)) {
2249 che_drc->pce_instance_tag = get_bits(gb, 4);
2250 skip_bits(gb, 4); // tag_reserved_bits
2254 /* excluded_chns_present? */
2255 if (get_bits1(gb)) {
2256 n += decode_drc_channel_exclusions(che_drc, gb);
2259 /* drc_bands_present? */
2260 if (get_bits1(gb)) {
2261 che_drc->band_incr = get_bits(gb, 4);
2262 che_drc->interpolation_scheme = get_bits(gb, 4);
2264 drc_num_bands += che_drc->band_incr;
2265 for (i = 0; i < drc_num_bands; i++) {
2266 che_drc->band_top[i] = get_bits(gb, 8);
2271 /* prog_ref_level_present? */
2272 if (get_bits1(gb)) {
2273 che_drc->prog_ref_level = get_bits(gb, 7);
2274 skip_bits1(gb); // prog_ref_level_reserved_bits
2278 for (i = 0; i < drc_num_bands; i++) {
2279 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2280 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2287 static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
2289 int i, major, minor;
2294 get_bits(gb, 13); len -= 13;
2296 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2297 buf[i] = get_bits(gb, 8);
2300 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2301 av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2303 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2304 ac->avctx->internal->skip_samples = 1024;
2308 skip_bits_long(gb, len);
2314 * Decode extension data (incomplete); reference: table 4.51.
2316 * @param cnt length of TYPE_FIL syntactic element in bytes
2318 * @return Returns number of bytes consumed
2320 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
2321 ChannelElement *che, enum RawDataBlockType elem_type)
2325 int type = get_bits(gb, 4);
2327 if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2328 av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2330 switch (type) { // extension type
2331 case EXT_SBR_DATA_CRC:
2335 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2337 } else if (!ac->oc[1].m4ac.sbr) {
2338 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2339 skip_bits_long(gb, 8 * cnt - 4);
2341 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2342 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2343 skip_bits_long(gb, 8 * cnt - 4);
2345 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2346 ac->oc[1].m4ac.sbr = 1;
2347 ac->oc[1].m4ac.ps = 1;
2348 ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
2349 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2350 ac->oc[1].status, 1);
2352 ac->oc[1].m4ac.sbr = 1;
2353 ac->avctx->profile = FF_PROFILE_AAC_HE;
2355 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2357 case EXT_DYNAMIC_RANGE:
2358 res = decode_dynamic_range(&ac->che_drc, gb);
2361 decode_fill(ac, gb, 8 * cnt - 4);
2364 case EXT_DATA_ELEMENT:
2366 skip_bits_long(gb, 8 * cnt - 4);
2373 * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
2375 * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2376 * @param coef spectral coefficients
2378 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
2379 IndividualChannelStream *ics, int decode)
2381 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2383 int bottom, top, order, start, end, size, inc;
2384 float lpc[TNS_MAX_ORDER];
2385 float tmp[TNS_MAX_ORDER+1];
2387 for (w = 0; w < ics->num_windows; w++) {
2388 bottom = ics->num_swb;
2389 for (filt = 0; filt < tns->n_filt[w]; filt++) {
2391 bottom = FFMAX(0, top - tns->length[w][filt]);
2392 order = tns->order[w][filt];
2397 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
2399 start = ics->swb_offset[FFMIN(bottom, mmm)];
2400 end = ics->swb_offset[FFMIN( top, mmm)];
2401 if ((size = end - start) <= 0)
2403 if (tns->direction[w][filt]) {
2413 for (m = 0; m < size; m++, start += inc)
2414 for (i = 1; i <= FFMIN(m, order); i++)
2415 coef[start] -= coef[start - i * inc] * lpc[i - 1];
2418 for (m = 0; m < size; m++, start += inc) {
2419 tmp[0] = coef[start];
2420 for (i = 1; i <= FFMIN(m, order); i++)
2421 coef[start] += tmp[i] * lpc[i - 1];
2422 for (i = order; i > 0; i--)
2423 tmp[i] = tmp[i - 1];
2431 * Apply windowing and MDCT to obtain the spectral
2432 * coefficient from the predicted sample by LTP.
2434 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
2435 float *in, IndividualChannelStream *ics)
2437 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2438 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2439 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2440 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2442 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2443 ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
2445 memset(in, 0, 448 * sizeof(float));
2446 ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
2448 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2449 ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2451 ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2452 memset(in + 1024 + 576, 0, 448 * sizeof(float));
2454 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2458 * Apply the long term prediction
2460 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
2462 const LongTermPrediction *ltp = &sce->ics.ltp;
2463 const uint16_t *offsets = sce->ics.swb_offset;
2466 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2467 float *predTime = sce->ret;
2468 float *predFreq = ac->buf_mdct;
2469 int16_t num_samples = 2048;
2471 if (ltp->lag < 1024)
2472 num_samples = ltp->lag + 1024;
2473 for (i = 0; i < num_samples; i++)
2474 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
2475 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
2477 ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2479 if (sce->tns.present)
2480 ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2482 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2484 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2485 sce->coeffs[i] += predFreq[i];
2490 * Update the LTP buffer for next frame
2492 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
2494 IndividualChannelStream *ics = &sce->ics;
2495 float *saved = sce->saved;
2496 float *saved_ltp = sce->coeffs;
2497 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2498 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2501 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2502 memcpy(saved_ltp, saved, 512 * sizeof(float));
2503 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2504 ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2505 for (i = 0; i < 64; i++)
2506 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2507 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2508 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
2509 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2510 ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2511 for (i = 0; i < 64; i++)
2512 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2513 } else { // LONG_STOP or ONLY_LONG
2514 ac->fdsp->vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2515 for (i = 0; i < 512; i++)
2516 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
2519 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2520 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2521 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2525 * Conduct IMDCT and windowing.
2527 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
2529 IndividualChannelStream *ics = &sce->ics;
2530 float *in = sce->coeffs;
2531 float *out = sce->ret;
2532 float *saved = sce->saved;
2533 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2534 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2535 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2536 float *buf = ac->buf_mdct;
2537 float *temp = ac->temp;
2541 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2542 for (i = 0; i < 1024; i += 128)
2543 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2545 ac->mdct.imdct_half(&ac->mdct, buf, in);
2547 /* window overlapping
2548 * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2549 * and long to short transitions are considered to be short to short
2550 * transitions. This leaves just two cases (long to long and short to short)
2551 * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2553 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2554 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2555 ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2557 memcpy( out, saved, 448 * sizeof(float));
2559 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2560 ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2561 ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2562 ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2563 ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2564 ac->fdsp->vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2565 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
2567 ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2568 memcpy( out + 576, buf + 64, 448 * sizeof(float));
2573 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2574 memcpy( saved, temp + 64, 64 * sizeof(float));
2575 ac->fdsp->vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2576 ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2577 ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2578 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2579 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2580 memcpy( saved, buf + 512, 448 * sizeof(float));
2581 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2582 } else { // LONG_STOP or ONLY_LONG
2583 memcpy( saved, buf + 512, 512 * sizeof(float));
2587 static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
2589 IndividualChannelStream *ics = &sce->ics;
2590 float *in = sce->coeffs;
2591 float *out = sce->ret;
2592 float *saved = sce->saved;
2593 float *buf = ac->buf_mdct;
2596 ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2598 // window overlapping
2599 if (ics->use_kb_window[1]) {
2600 // AAC LD uses a low overlap sine window instead of a KBD window
2601 memcpy(out, saved, 192 * sizeof(float));
2602 ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, ff_sine_128, 64);
2603 memcpy( out + 320, buf + 64, 192 * sizeof(float));
2605 ac->fdsp->vector_fmul_window(out, saved, buf, ff_sine_512, 256);
2609 memcpy(saved, buf + 256, 256 * sizeof(float));
2612 static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
2614 float *in = sce->coeffs;
2615 float *out = sce->ret;
2616 float *saved = sce->saved;
2617 float *buf = ac->buf_mdct;
2619 const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
2620 const int n2 = n >> 1;
2621 const int n4 = n >> 2;
2622 const float *const window = n == 480 ? ff_aac_eld_window_480 :
2623 ff_aac_eld_window_512;
2625 // Inverse transform, mapped to the conventional IMDCT by
2626 // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
2627 // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
2628 // International Conference on Audio, Language and Image Processing, ICALIP 2008.
2629 // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
2630 for (i = 0; i < n2; i+=2) {
2632 temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
2633 temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
2636 ac->mdct480->imdct_half(ac->mdct480, buf, in, 1, -1.f/(16*1024*960));
2638 ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2639 for (i = 0; i < n; i+=2) {
2642 // Like with the regular IMDCT at this point we still have the middle half
2643 // of a transform but with even symmetry on the left and odd symmetry on
2646 // window overlapping
2647 // The spec says to use samples [0..511] but the reference decoder uses
2648 // samples [128..639].
2649 for (i = n4; i < n2; i ++) {
2650 out[i - n4] = buf[n2 - 1 - i] * window[i - n4] +
2651 saved[ i + n2] * window[i + n - n4] +
2652 -saved[ n + n2 - 1 - i] * window[i + 2*n - n4] +
2653 -saved[2*n + n2 + i] * window[i + 3*n - n4];
2655 for (i = 0; i < n2; i ++) {
2656 out[n4 + i] = buf[i] * window[i + n2 - n4] +
2657 -saved[ n - 1 - i] * window[i + n2 + n - n4] +
2658 -saved[ n + i] * window[i + n2 + 2*n - n4] +
2659 saved[2*n + n - 1 - i] * window[i + n2 + 3*n - n4];
2661 for (i = 0; i < n4; i ++) {
2662 out[n2 + n4 + i] = buf[ i + n2] * window[i + n - n4] +
2663 -saved[ n2 - 1 - i] * window[i + 2*n - n4] +
2664 -saved[ n + n2 + i] * window[i + 3*n - n4];
2668 memmove(saved + n, saved, 2 * n * sizeof(float));
2669 memcpy( saved, buf, n * sizeof(float));
2673 * Apply dependent channel coupling (applied before IMDCT).
2675 * @param index index into coupling gain array
2677 static void apply_dependent_coupling(AACContext *ac,
2678 SingleChannelElement *target,
2679 ChannelElement *cce, int index)
2681 IndividualChannelStream *ics = &cce->ch[0].ics;
2682 const uint16_t *offsets = ics->swb_offset;
2683 float *dest = target->coeffs;
2684 const float *src = cce->ch[0].coeffs;
2685 int g, i, group, k, idx = 0;
2686 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2687 av_log(ac->avctx, AV_LOG_ERROR,
2688 "Dependent coupling is not supported together with LTP\n");
2691 for (g = 0; g < ics->num_window_groups; g++) {
2692 for (i = 0; i < ics->max_sfb; i++, idx++) {
2693 if (cce->ch[0].band_type[idx] != ZERO_BT) {
2694 const float gain = cce->coup.gain[index][idx];
2695 for (group = 0; group < ics->group_len[g]; group++) {
2696 for (k = offsets[i]; k < offsets[i + 1]; k++) {
2698 dest[group * 128 + k] += gain * src[group * 128 + k];
2703 dest += ics->group_len[g] * 128;
2704 src += ics->group_len[g] * 128;
2709 * Apply independent channel coupling (applied after IMDCT).
2711 * @param index index into coupling gain array
2713 static void apply_independent_coupling(AACContext *ac,
2714 SingleChannelElement *target,
2715 ChannelElement *cce, int index)
2718 const float gain = cce->coup.gain[index][0];
2719 const float *src = cce->ch[0].ret;
2720 float *dest = target->ret;
2721 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
2723 for (i = 0; i < len; i++)
2724 dest[i] += gain * src[i];
2728 * channel coupling transformation interface
2730 * @param apply_coupling_method pointer to (in)dependent coupling function
2732 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
2733 enum RawDataBlockType type, int elem_id,
2734 enum CouplingPoint coupling_point,
2735 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2739 for (i = 0; i < MAX_ELEM_ID; i++) {
2740 ChannelElement *cce = ac->che[TYPE_CCE][i];
2743 if (cce && cce->coup.coupling_point == coupling_point) {
2744 ChannelCoupling *coup = &cce->coup;
2746 for (c = 0; c <= coup->num_coupled; c++) {
2747 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2748 if (coup->ch_select[c] != 1) {
2749 apply_coupling_method(ac, &cc->ch[0], cce, index);
2750 if (coup->ch_select[c] != 0)
2753 if (coup->ch_select[c] != 2)
2754 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2756 index += 1 + (coup->ch_select[c] == 3);
2763 * Convert spectral data to float samples, applying all supported tools as appropriate.
2765 static void spectral_to_sample(AACContext *ac)
2768 void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
2769 switch (ac->oc[1].m4ac.object_type) {
2771 imdct_and_window = imdct_and_windowing_ld;
2773 case AOT_ER_AAC_ELD:
2774 imdct_and_window = imdct_and_windowing_eld;
2777 imdct_and_window = ac->imdct_and_windowing;
2779 for (type = 3; type >= 0; type--) {
2780 for (i = 0; i < MAX_ELEM_ID; i++) {
2781 ChannelElement *che = ac->che[type][i];
2782 if (che && che->present) {
2783 if (type <= TYPE_CPE)
2784 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
2785 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2786 if (che->ch[0].ics.predictor_present) {
2787 if (che->ch[0].ics.ltp.present)
2788 ac->apply_ltp(ac, &che->ch[0]);
2789 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2790 ac->apply_ltp(ac, &che->ch[1]);
2793 if (che->ch[0].tns.present)
2794 ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2795 if (che->ch[1].tns.present)
2796 ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2797 if (type <= TYPE_CPE)
2798 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
2799 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2800 imdct_and_window(ac, &che->ch[0]);
2801 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2802 ac->update_ltp(ac, &che->ch[0]);
2803 if (type == TYPE_CPE) {
2804 imdct_and_window(ac, &che->ch[1]);
2805 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2806 ac->update_ltp(ac, &che->ch[1]);
2808 if (ac->oc[1].m4ac.sbr > 0) {
2809 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2812 if (type <= TYPE_CCE)
2813 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
2816 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2822 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
2825 AACADTSHeaderInfo hdr_info;
2826 uint8_t layout_map[MAX_ELEM_ID*4][3];
2827 int layout_map_tags, ret;
2829 size = avpriv_aac_parse_header(gb, &hdr_info);
2831 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2832 // This is 2 for "VLB " audio in NSV files.
2833 // See samples/nsv/vlb_audio.
2834 avpriv_report_missing_feature(ac->avctx,
2835 "More than one AAC RDB per ADTS frame");
2836 ac->warned_num_aac_frames = 1;
2838 push_output_configuration(ac);
2839 if (hdr_info.chan_config) {
2840 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2841 if ((ret = set_default_channel_config(ac->avctx,
2844 hdr_info.chan_config)) < 0)
2846 if ((ret = output_configure(ac, layout_map, layout_map_tags,
2847 FFMAX(ac->oc[1].status,
2848 OC_TRIAL_FRAME), 0)) < 0)
2851 ac->oc[1].m4ac.chan_config = 0;
2853 * dual mono frames in Japanese DTV can have chan_config 0
2854 * WITHOUT specifying PCE.
2855 * thus, set dual mono as default.
2857 if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2858 layout_map_tags = 2;
2859 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2860 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2861 layout_map[0][1] = 0;
2862 layout_map[1][1] = 1;
2863 if (output_configure(ac, layout_map, layout_map_tags,
2868 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2869 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2870 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2871 ac->oc[1].m4ac.frame_length_short = 0;
2872 if (ac->oc[0].status != OC_LOCKED ||
2873 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2874 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2875 ac->oc[1].m4ac.sbr = -1;
2876 ac->oc[1].m4ac.ps = -1;
2878 if (!hdr_info.crc_absent)
2884 static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
2885 int *got_frame_ptr, GetBitContext *gb)
2887 AACContext *ac = avctx->priv_data;
2888 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2889 ChannelElement *che;
2891 int samples = m4ac->frame_length_short ? 960 : 1024;
2892 int chan_config = m4ac->chan_config;
2893 int aot = m4ac->object_type;
2895 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
2900 if ((err = frame_configure_elements(avctx)) < 0)
2903 // The FF_PROFILE_AAC_* defines are all object_type - 1
2904 // This may lead to an undefined profile being signaled
2905 ac->avctx->profile = aot - 1;
2907 ac->tags_mapped = 0;
2909 if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
2910 avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2912 return AVERROR_INVALIDDATA;
2914 for (i = 0; i < tags_per_config[chan_config]; i++) {
2915 const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
2916 const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
2917 if (!(che=get_che(ac, elem_type, elem_id))) {
2918 av_log(ac->avctx, AV_LOG_ERROR,
2919 "channel element %d.%d is not allocated\n",
2920 elem_type, elem_id);
2921 return AVERROR_INVALIDDATA;
2924 if (aot != AOT_ER_AAC_ELD)
2926 switch (elem_type) {
2928 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2931 err = decode_cpe(ac, gb, che);
2934 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2941 spectral_to_sample(ac);
2943 ac->frame->nb_samples = samples;
2944 ac->frame->sample_rate = avctx->sample_rate;
2947 skip_bits_long(gb, get_bits_left(gb));
2951 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2952 int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
2954 AACContext *ac = avctx->priv_data;
2955 ChannelElement *che = NULL, *che_prev = NULL;
2956 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2958 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2959 int is_dmono, sce_count = 0;
2963 if (show_bits(gb, 12) == 0xfff) {
2964 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2965 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2968 if (ac->oc[1].m4ac.sampling_index > 12) {
2969 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2970 err = AVERROR_INVALIDDATA;
2975 if ((err = frame_configure_elements(avctx)) < 0)
2978 // The FF_PROFILE_AAC_* defines are all object_type - 1
2979 // This may lead to an undefined profile being signaled
2980 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2982 ac->tags_mapped = 0;
2984 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2985 elem_id = get_bits(gb, 4);
2987 if (avctx->debug & FF_DEBUG_STARTCODE)
2988 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2990 if (!avctx->channels && elem_type != TYPE_PCE) {
2991 err = AVERROR_INVALIDDATA;
2995 if (elem_type < TYPE_DSE) {
2996 if (!(che=get_che(ac, elem_type, elem_id))) {
2997 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2998 elem_type, elem_id);
2999 err = AVERROR_INVALIDDATA;
3006 switch (elem_type) {
3009 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3015 err = decode_cpe(ac, gb, che);
3020 err = decode_cce(ac, gb, che);
3024 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3029 err = skip_data_stream_element(ac, gb);
3033 uint8_t layout_map[MAX_ELEM_ID*4][3];
3035 push_output_configuration(ac);
3036 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
3042 av_log(avctx, AV_LOG_ERROR,
3043 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
3045 err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
3047 ac->oc[1].m4ac.chan_config = 0;
3055 elem_id += get_bits(gb, 8) - 1;
3056 if (get_bits_left(gb) < 8 * elem_id) {
3057 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
3058 err = AVERROR_INVALIDDATA;
3062 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
3063 err = 0; /* FIXME */
3067 err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
3072 elem_type_prev = elem_type;
3077 if (get_bits_left(gb) < 3) {
3078 av_log(avctx, AV_LOG_ERROR, overread_err);
3079 err = AVERROR_INVALIDDATA;
3084 if (!avctx->channels) {
3089 spectral_to_sample(ac);
3091 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
3092 samples <<= multiplier;
3094 if (ac->oc[1].status && audio_found) {
3095 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
3096 avctx->frame_size = samples;
3097 ac->oc[1].status = OC_LOCKED;
3102 const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
3103 if (side && side_size>=4)
3104 AV_WL32(side, 2*AV_RL32(side));
3107 if (!ac->frame->data[0] && samples) {
3108 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
3109 err = AVERROR_INVALIDDATA;
3114 ac->frame->nb_samples = samples;
3115 ac->frame->sample_rate = avctx->sample_rate;
3117 av_frame_unref(ac->frame);
3118 *got_frame_ptr = !!samples;
3120 /* for dual-mono audio (SCE + SCE) */
3121 is_dmono = ac->dmono_mode && sce_count == 2 &&
3122 ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
3124 if (ac->dmono_mode == 1)
3125 ((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
3126 else if (ac->dmono_mode == 2)
3127 ((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
3132 pop_output_configuration(ac);
3136 static int aac_decode_frame(AVCodecContext *avctx, void *data,
3137 int *got_frame_ptr, AVPacket *avpkt)
3139 AACContext *ac = avctx->priv_data;
3140 const uint8_t *buf = avpkt->data;
3141 int buf_size = avpkt->size;
3146 int new_extradata_size;
3147 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
3148 AV_PKT_DATA_NEW_EXTRADATA,
3149 &new_extradata_size);
3150 int jp_dualmono_size;
3151 const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
3152 AV_PKT_DATA_JP_DUALMONO,
3155 if (new_extradata && 0) {
3156 av_free(avctx->extradata);
3157 avctx->extradata = av_mallocz(new_extradata_size +
3158 FF_INPUT_BUFFER_PADDING_SIZE);
3159 if (!avctx->extradata)
3160 return AVERROR(ENOMEM);
3161 avctx->extradata_size = new_extradata_size;
3162 memcpy(avctx->extradata, new_extradata, new_extradata_size);
3163 push_output_configuration(ac);
3164 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
3166 avctx->extradata_size*8, 1) < 0) {
3167 pop_output_configuration(ac);
3168 return AVERROR_INVALIDDATA;
3173 if (jp_dualmono && jp_dualmono_size > 0)
3174 ac->dmono_mode = 1 + *jp_dualmono;
3175 if (ac->force_dmono_mode >= 0)
3176 ac->dmono_mode = ac->force_dmono_mode;
3178 if (INT_MAX / 8 <= buf_size)
3179 return AVERROR_INVALIDDATA;
3181 if ((err = init_get_bits(&gb, buf, buf_size * 8)) < 0)
3184 switch (ac->oc[1].m4ac.object_type) {
3186 case AOT_ER_AAC_LTP:
3188 case AOT_ER_AAC_ELD:
3189 err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
3192 err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
3197 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
3198 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
3199 if (buf[buf_offset])
3202 return buf_size > buf_offset ? buf_consumed : buf_size;
3205 static av_cold int aac_decode_close(AVCodecContext *avctx)
3207 AACContext *ac = avctx->priv_data;
3210 for (i = 0; i < MAX_ELEM_ID; i++) {
3211 for (type = 0; type < 4; type++) {
3212 if (ac->che[type][i])
3213 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
3214 av_freep(&ac->che[type][i]);
3218 ff_mdct_end(&ac->mdct);
3219 ff_mdct_end(&ac->mdct_small);
3220 ff_mdct_end(&ac->mdct_ld);
3221 ff_mdct_end(&ac->mdct_ltp);
3222 ff_imdct15_uninit(&ac->mdct480);
3223 av_freep(&ac->fdsp);
3228 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
3230 struct LATMContext {
3231 AACContext aac_ctx; ///< containing AACContext
3232 int initialized; ///< initialized after a valid extradata was seen
3235 int audio_mux_version_A; ///< LATM syntax version
3236 int frame_length_type; ///< 0/1 variable/fixed frame length
3237 int frame_length; ///< frame length for fixed frame length
3240 static inline uint32_t latm_get_value(GetBitContext *b)
3242 int length = get_bits(b, 2);
3244 return get_bits_long(b, (length+1)*8);
3247 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
3248 GetBitContext *gb, int asclen)
3250 AACContext *ac = &latmctx->aac_ctx;
3251 AVCodecContext *avctx = ac->avctx;
3252 MPEG4AudioConfig m4ac = { 0 };
3253 int config_start_bit = get_bits_count(gb);
3254 int sync_extension = 0;
3255 int bits_consumed, esize;
3259 asclen = FFMIN(asclen, get_bits_left(gb));
3261 asclen = get_bits_left(gb);
3263 if (config_start_bit % 8) {
3264 avpriv_request_sample(latmctx->aac_ctx.avctx,
3265 "Non-byte-aligned audio-specific config");
3266 return AVERROR_PATCHWELCOME;
3269 return AVERROR_INVALIDDATA;
3270 bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
3271 gb->buffer + (config_start_bit / 8),
3272 asclen, sync_extension);
3274 if (bits_consumed < 0)
3275 return AVERROR_INVALIDDATA;
3277 if (!latmctx->initialized ||
3278 ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
3279 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
3281 if(latmctx->initialized) {
3282 av_log(avctx, AV_LOG_INFO, "audio config changed\n");
3284 av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
3286 latmctx->initialized = 0;
3288 esize = (bits_consumed+7) / 8;
3290 if (avctx->extradata_size < esize) {
3291 av_free(avctx->extradata);
3292 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
3293 if (!avctx->extradata)
3294 return AVERROR(ENOMEM);
3297 avctx->extradata_size = esize;
3298 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
3299 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
3301 skip_bits_long(gb, bits_consumed);
3303 return bits_consumed;
3306 static int read_stream_mux_config(struct LATMContext *latmctx,
3309 int ret, audio_mux_version = get_bits(gb, 1);
3311 latmctx->audio_mux_version_A = 0;
3312 if (audio_mux_version)
3313 latmctx->audio_mux_version_A = get_bits(gb, 1);
3315 if (!latmctx->audio_mux_version_A) {
3317 if (audio_mux_version)
3318 latm_get_value(gb); // taraFullness
3320 skip_bits(gb, 1); // allStreamSameTimeFraming
3321 skip_bits(gb, 6); // numSubFrames
3323 if (get_bits(gb, 4)) { // numPrograms
3324 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
3325 return AVERROR_PATCHWELCOME;
3328 // for each program (which there is only one in DVB)
3330 // for each layer (which there is only one in DVB)
3331 if (get_bits(gb, 3)) { // numLayer
3332 avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
3333 return AVERROR_PATCHWELCOME;
3336 // for all but first stream: use_same_config = get_bits(gb, 1);
3337 if (!audio_mux_version) {
3338 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
3341 int ascLen = latm_get_value(gb);
3342 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
3345 skip_bits_long(gb, ascLen);
3348 latmctx->frame_length_type = get_bits(gb, 3);
3349 switch (latmctx->frame_length_type) {
3351 skip_bits(gb, 8); // latmBufferFullness
3354 latmctx->frame_length = get_bits(gb, 9);
3359 skip_bits(gb, 6); // CELP frame length table index
3363 skip_bits(gb, 1); // HVXC frame length table index
3367 if (get_bits(gb, 1)) { // other data
3368 if (audio_mux_version) {
3369 latm_get_value(gb); // other_data_bits
3373 esc = get_bits(gb, 1);
3379 if (get_bits(gb, 1)) // crc present
3380 skip_bits(gb, 8); // config_crc
3386 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
3390 if (ctx->frame_length_type == 0) {
3391 int mux_slot_length = 0;
3393 tmp = get_bits(gb, 8);
3394 mux_slot_length += tmp;
3395 } while (tmp == 255);
3396 return mux_slot_length;
3397 } else if (ctx->frame_length_type == 1) {
3398 return ctx->frame_length;
3399 } else if (ctx->frame_length_type == 3 ||
3400 ctx->frame_length_type == 5 ||
3401 ctx->frame_length_type == 7) {
3402 skip_bits(gb, 2); // mux_slot_length_coded
3407 static int read_audio_mux_element(struct LATMContext *latmctx,
3411 uint8_t use_same_mux = get_bits(gb, 1);
3412 if (!use_same_mux) {
3413 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
3415 } else if (!latmctx->aac_ctx.avctx->extradata) {
3416 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
3417 "no decoder config found\n");
3418 return AVERROR(EAGAIN);
3420 if (latmctx->audio_mux_version_A == 0) {
3421 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
3422 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
3423 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
3424 return AVERROR_INVALIDDATA;
3425 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
3426 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
3427 "frame length mismatch %d << %d\n",
3428 mux_slot_length_bytes * 8, get_bits_left(gb));
3429 return AVERROR_INVALIDDATA;
3436 static int latm_decode_frame(AVCodecContext *avctx, void *out,
3437 int *got_frame_ptr, AVPacket *avpkt)
3439 struct LATMContext *latmctx = avctx->priv_data;
3443 if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
3446 // check for LOAS sync word
3447 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
3448 return AVERROR_INVALIDDATA;
3450 muxlength = get_bits(&gb, 13) + 3;
3451 // not enough data, the parser should have sorted this out
3452 if (muxlength > avpkt->size)
3453 return AVERROR_INVALIDDATA;
3455 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
3458 if (!latmctx->initialized) {
3459 if (!avctx->extradata) {
3463 push_output_configuration(&latmctx->aac_ctx);
3464 if ((err = decode_audio_specific_config(
3465 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
3466 avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
3467 pop_output_configuration(&latmctx->aac_ctx);
3470 latmctx->initialized = 1;
3474 if (show_bits(&gb, 12) == 0xfff) {
3475 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
3476 "ADTS header detected, probably as result of configuration "
3478 return AVERROR_INVALIDDATA;
3481 switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
3483 case AOT_ER_AAC_LTP:
3485 case AOT_ER_AAC_ELD:
3486 err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
3489 err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
3497 static av_cold int latm_decode_init(AVCodecContext *avctx)
3499 struct LATMContext *latmctx = avctx->priv_data;
3500 int ret = aac_decode_init(avctx);
3502 if (avctx->extradata_size > 0)
3503 latmctx->initialized = !ret;
3508 static void aacdec_init(AACContext *c)
3510 c->imdct_and_windowing = imdct_and_windowing;
3511 c->apply_ltp = apply_ltp;
3512 c->apply_tns = apply_tns;
3513 c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
3514 c->update_ltp = update_ltp;
3517 ff_aacdec_init_mips(c);
3520 * AVOptions for Japanese DTV specific extensions (ADTS only)
3522 #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
3523 static const AVOption options[] = {
3524 {"dual_mono_mode", "Select the channel to decode for dual mono",
3525 offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
3526 AACDEC_FLAGS, "dual_mono_mode"},
3528 {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3529 {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3530 {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3531 {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3536 static const AVClass aac_decoder_class = {
3537 .class_name = "AAC decoder",
3538 .item_name = av_default_item_name,
3540 .version = LIBAVUTIL_VERSION_INT,
3543 static const AVProfile profiles[] = {
3544 { FF_PROFILE_AAC_MAIN, "Main" },
3545 { FF_PROFILE_AAC_LOW, "LC" },
3546 { FF_PROFILE_AAC_SSR, "SSR" },
3547 { FF_PROFILE_AAC_LTP, "LTP" },
3548 { FF_PROFILE_AAC_HE, "HE-AAC" },
3549 { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
3550 { FF_PROFILE_AAC_LD, "LD" },
3551 { FF_PROFILE_AAC_ELD, "ELD" },
3552 { FF_PROFILE_UNKNOWN },
3555 AVCodec ff_aac_decoder = {
3557 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
3558 .type = AVMEDIA_TYPE_AUDIO,
3559 .id = AV_CODEC_ID_AAC,
3560 .priv_data_size = sizeof(AACContext),
3561 .init = aac_decode_init,
3562 .close = aac_decode_close,
3563 .decode = aac_decode_frame,
3564 .sample_fmts = (const enum AVSampleFormat[]) {
3565 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
3567 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
3568 .channel_layouts = aac_channel_layout,
3570 .priv_class = &aac_decoder_class,
3571 .profiles = profiles,
3575 Note: This decoder filter is intended to decode LATM streams transferred
3576 in MPEG transport streams which only contain one program.
3577 To do a more complex LATM demuxing a separate LATM demuxer should be used.
3579 AVCodec ff_aac_latm_decoder = {
3581 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
3582 .type = AVMEDIA_TYPE_AUDIO,
3583 .id = AV_CODEC_ID_AAC_LATM,
3584 .priv_data_size = sizeof(struct LATMContext),
3585 .init = latm_decode_init,
3586 .close = aac_decode_close,
3587 .decode = latm_decode_frame,
3588 .sample_fmts = (const enum AVSampleFormat[]) {
3589 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
3591 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
3592 .channel_layouts = aac_channel_layout,
3594 .profiles = profiles,