3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
7 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
8 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10 * This file is part of FFmpeg.
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 * @author Oded Shimon ( ods15 ods15 dyndns org )
31 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
38 * N (code in SoC repo) gain control
40 * Y window shapes - standard
41 * N window shapes - Low Delay
42 * Y filterbank - standard
43 * N (code in SoC repo) filterbank - Scalable Sample Rate
44 * Y Temporal Noise Shaping
45 * Y Long Term Prediction
48 * Y frequency domain prediction
49 * Y Perceptual Noise Substitution
51 * N Scalable Inverse AAC Quantization
52 * N Frequency Selective Switch
54 * Y quantization & coding - AAC
55 * N quantization & coding - TwinVQ
56 * N quantization & coding - BSAC
57 * N AAC Error Resilience tools
58 * N Error Resilience payload syntax
59 * N Error Protection tool
61 * N Silence Compression
64 * N Structured Audio tools
65 * N Structured Audio Sample Bank Format
67 * N Harmonic and Individual Lines plus Noise
68 * N Text-To-Speech Interface
69 * Y Spectral Band Replication
70 * Y (not in this code) Layer-1
71 * Y (not in this code) Layer-2
72 * Y (not in this code) Layer-3
73 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
75 * N Direct Stream Transfer
77 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
78 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
88 #include "fmtconvert.h"
95 #include "aacdectab.h"
96 #include "cbrt_tablegen.h"
99 #include "mpeg4audio.h"
100 #include "aacadtsdec.h"
101 #include "libavutil/intfloat.h"
109 # include "arm/aac.h"
112 static VLC vlc_scalefactors;
113 static VLC vlc_spectral[11];
115 #define overread_err "Input buffer exhausted before END element found\n"
117 static int count_channels(uint8_t (*layout)[3], int tags)
120 for (i = 0; i < tags; i++) {
121 int syn_ele = layout[i][0];
122 int pos = layout[i][2];
123 sum += (1 + (syn_ele == TYPE_CPE)) *
124 (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
130 * Check for the channel element in the current channel position configuration.
131 * If it exists, make sure the appropriate element is allocated and map the
132 * channel order to match the internal FFmpeg channel layout.
134 * @param che_pos current channel position configuration
135 * @param type channel element type
136 * @param id channel element id
137 * @param channels count of the number of channels in the configuration
139 * @return Returns error status. 0 - OK, !0 - error
141 static av_cold int che_configure(AACContext *ac,
142 enum ChannelPosition che_pos,
143 int type, int id, int *channels)
146 if (!ac->che[type][id]) {
147 if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
148 return AVERROR(ENOMEM);
149 ff_aac_sbr_ctx_init(ac, &ac->che[type][id]->sbr);
151 if (type != TYPE_CCE) {
152 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
153 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
154 return AVERROR_INVALIDDATA;
156 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
157 if (type == TYPE_CPE ||
158 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
159 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
163 if (ac->che[type][id])
164 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
165 av_freep(&ac->che[type][id]);
170 struct elem_to_channel {
171 uint64_t av_position;
174 uint8_t aac_position;
177 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
178 uint8_t (*layout_map)[3], int offset, int tags, uint64_t left,
179 uint64_t right, int pos)
181 if (layout_map[offset][0] == TYPE_CPE) {
182 e2c_vec[offset] = (struct elem_to_channel) {
183 .av_position = left | right, .syn_ele = TYPE_CPE,
184 .elem_id = layout_map[offset ][1], .aac_position = pos };
187 e2c_vec[offset] = (struct elem_to_channel) {
188 .av_position = left, .syn_ele = TYPE_SCE,
189 .elem_id = layout_map[offset ][1], .aac_position = pos };
190 e2c_vec[offset + 1] = (struct elem_to_channel) {
191 .av_position = right, .syn_ele = TYPE_SCE,
192 .elem_id = layout_map[offset + 1][1], .aac_position = pos };
197 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos, int *current) {
198 int num_pos_channels = 0;
202 for (i = *current; i < tags; i++) {
203 if (layout_map[i][2] != pos)
205 if (layout_map[i][0] == TYPE_CPE) {
207 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
213 num_pos_channels += 2;
221 ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
224 return num_pos_channels;
227 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
229 int i, n, total_non_cc_elements;
230 struct elem_to_channel e2c_vec[4*MAX_ELEM_ID] = {{ 0 }};
231 int num_front_channels, num_side_channels, num_back_channels;
234 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
239 count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
240 if (num_front_channels < 0)
243 count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
244 if (num_side_channels < 0)
247 count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
248 if (num_back_channels < 0)
252 if (num_front_channels & 1) {
253 e2c_vec[i] = (struct elem_to_channel) {
254 .av_position = AV_CH_FRONT_CENTER, .syn_ele = TYPE_SCE,
255 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_FRONT };
257 num_front_channels--;
259 if (num_front_channels >= 4) {
260 i += assign_pair(e2c_vec, layout_map, i, tags,
261 AV_CH_FRONT_LEFT_OF_CENTER,
262 AV_CH_FRONT_RIGHT_OF_CENTER,
264 num_front_channels -= 2;
266 if (num_front_channels >= 2) {
267 i += assign_pair(e2c_vec, layout_map, i, tags,
271 num_front_channels -= 2;
273 while (num_front_channels >= 2) {
274 i += assign_pair(e2c_vec, layout_map, i, tags,
278 num_front_channels -= 2;
281 if (num_side_channels >= 2) {
282 i += assign_pair(e2c_vec, layout_map, i, tags,
286 num_side_channels -= 2;
288 while (num_side_channels >= 2) {
289 i += assign_pair(e2c_vec, layout_map, i, tags,
293 num_side_channels -= 2;
296 while (num_back_channels >= 4) {
297 i += assign_pair(e2c_vec, layout_map, i, tags,
301 num_back_channels -= 2;
303 if (num_back_channels >= 2) {
304 i += assign_pair(e2c_vec, layout_map, i, tags,
308 num_back_channels -= 2;
310 if (num_back_channels) {
311 e2c_vec[i] = (struct elem_to_channel) {
312 .av_position = AV_CH_BACK_CENTER, .syn_ele = TYPE_SCE,
313 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_BACK };
318 if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
319 e2c_vec[i] = (struct elem_to_channel) {
320 .av_position = AV_CH_LOW_FREQUENCY, .syn_ele = TYPE_LFE,
321 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
324 while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
325 e2c_vec[i] = (struct elem_to_channel) {
326 .av_position = UINT64_MAX, .syn_ele = TYPE_LFE,
327 .elem_id = layout_map[i][1], .aac_position = AAC_CHANNEL_LFE };
331 // Must choose a stable sort
332 total_non_cc_elements = n = i;
335 for (i = 1; i < n; i++) {
336 if (e2c_vec[i-1].av_position > e2c_vec[i].av_position) {
337 FFSWAP(struct elem_to_channel, e2c_vec[i-1], e2c_vec[i]);
345 for (i = 0; i < total_non_cc_elements; i++) {
346 layout_map[i][0] = e2c_vec[i].syn_ele;
347 layout_map[i][1] = e2c_vec[i].elem_id;
348 layout_map[i][2] = e2c_vec[i].aac_position;
349 if (e2c_vec[i].av_position != UINT64_MAX) {
350 layout |= e2c_vec[i].av_position;
358 * Save current output configuration if and only if it has been locked.
360 static void push_output_configuration(AACContext *ac) {
361 if (ac->oc[1].status == OC_LOCKED) {
362 ac->oc[0] = ac->oc[1];
364 ac->oc[1].status = OC_NONE;
368 * Restore the previous output configuration if and only if the current
369 * configuration is unlocked.
371 static void pop_output_configuration(AACContext *ac) {
372 if (ac->oc[1].status != OC_LOCKED) {
373 if (ac->oc[0].status == OC_LOCKED) {
374 ac->oc[1] = ac->oc[0];
375 ac->avctx->channels = ac->oc[1].channels;
376 ac->avctx->channel_layout = ac->oc[1].channel_layout;
382 * Configure output channel order based on the current program configuration element.
384 * @return Returns error status. 0 - OK, !0 - error
386 static int output_configure(AACContext *ac,
387 uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
388 int channel_config, enum OCStatus oc_type)
390 AVCodecContext *avctx = ac->avctx;
391 int i, channels = 0, ret;
394 if (ac->oc[1].layout_map != layout_map) {
395 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
396 ac->oc[1].layout_map_tags = tags;
399 // Try to sniff a reasonable channel order, otherwise output the
400 // channels in the order the PCE declared them.
401 if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
402 layout = sniff_channel_order(layout_map, tags);
403 for (i = 0; i < tags; i++) {
404 int type = layout_map[i][0];
405 int id = layout_map[i][1];
406 int position = layout_map[i][2];
407 // Allocate or free elements depending on if they are in the
408 // current program configuration.
409 ret = che_configure(ac, position, type, id, &channels);
413 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
414 if (layout == AV_CH_FRONT_CENTER) {
415 layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
421 memcpy(ac->tag_che_map, ac->che, 4 * MAX_ELEM_ID * sizeof(ac->che[0][0]));
422 if (layout) avctx->channel_layout = layout;
423 ac->oc[1].channel_layout = layout;
424 avctx->channels = ac->oc[1].channels = channels;
425 ac->oc[1].status = oc_type;
430 static void flush(AVCodecContext *avctx)
432 AACContext *ac= avctx->priv_data;
435 for (type = 3; type >= 0; type--) {
436 for (i = 0; i < MAX_ELEM_ID; i++) {
437 ChannelElement *che = ac->che[type][i];
439 for (j = 0; j <= 1; j++) {
440 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
448 * Set up channel positions based on a default channel configuration
449 * as specified in table 1.17.
451 * @return Returns error status. 0 - OK, !0 - error
453 static int set_default_channel_config(AVCodecContext *avctx,
454 uint8_t (*layout_map)[3],
458 if (channel_config < 1 || channel_config > 7) {
459 av_log(avctx, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
463 *tags = tags_per_config[channel_config];
464 memcpy(layout_map, aac_channel_layout_map[channel_config-1], *tags * sizeof(*layout_map));
468 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
470 // For PCE based channel configurations map the channels solely based on tags.
471 if (!ac->oc[1].m4ac.chan_config) {
472 return ac->tag_che_map[type][elem_id];
474 // Allow single CPE stereo files to be signalled with mono configuration.
475 if (!ac->tags_mapped && type == TYPE_CPE && ac->oc[1].m4ac.chan_config == 1) {
476 uint8_t layout_map[MAX_ELEM_ID*4][3];
478 push_output_configuration(ac);
480 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
483 if (output_configure(ac, layout_map, layout_map_tags,
484 2, OC_TRIAL_FRAME) < 0)
487 ac->oc[1].m4ac.chan_config = 2;
490 if (!ac->tags_mapped && type == TYPE_SCE && ac->oc[1].m4ac.chan_config == 2 && 0) {
491 uint8_t layout_map[MAX_ELEM_ID*4][3];
493 push_output_configuration(ac);
495 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
498 if (output_configure(ac, layout_map, layout_map_tags,
499 1, OC_TRIAL_FRAME) < 0)
502 ac->oc[1].m4ac.chan_config = 1;
504 // For indexed channel configurations map the channels solely based on position.
505 switch (ac->oc[1].m4ac.chan_config) {
507 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
509 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
512 /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
513 instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
514 encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
515 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
517 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
520 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
522 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
525 if (ac->tags_mapped == 2 && ac->oc[1].m4ac.chan_config == 4 && type == TYPE_SCE) {
527 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
531 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) && type == TYPE_CPE) {
533 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
534 } else if (ac->oc[1].m4ac.chan_config == 2) {
538 if (!ac->tags_mapped && type == TYPE_SCE) {
540 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
548 * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
550 * @param type speaker type/position for these channels
552 static void decode_channel_map(uint8_t layout_map[][3],
553 enum ChannelPosition type,
554 GetBitContext *gb, int n)
557 enum RawDataBlockType syn_ele;
559 case AAC_CHANNEL_FRONT:
560 case AAC_CHANNEL_BACK:
561 case AAC_CHANNEL_SIDE:
562 syn_ele = get_bits1(gb);
568 case AAC_CHANNEL_LFE:
572 layout_map[0][0] = syn_ele;
573 layout_map[0][1] = get_bits(gb, 4);
574 layout_map[0][2] = type;
580 * Decode program configuration element; reference: table 4.2.
582 * @return Returns error status. 0 - OK, !0 - error
584 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
585 uint8_t (*layout_map)[3],
588 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
592 skip_bits(gb, 2); // object_type
594 sampling_index = get_bits(gb, 4);
595 if (m4ac->sampling_index != sampling_index)
596 av_log(avctx, AV_LOG_WARNING, "Sample rate index in program config element does not match the sample rate index configured by the container.\n");
598 num_front = get_bits(gb, 4);
599 num_side = get_bits(gb, 4);
600 num_back = get_bits(gb, 4);
601 num_lfe = get_bits(gb, 2);
602 num_assoc_data = get_bits(gb, 3);
603 num_cc = get_bits(gb, 4);
606 skip_bits(gb, 4); // mono_mixdown_tag
608 skip_bits(gb, 4); // stereo_mixdown_tag
611 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
613 if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
614 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
617 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
619 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
621 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
623 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
626 skip_bits_long(gb, 4 * num_assoc_data);
628 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
633 /* comment field, first byte is length */
634 comment_len = get_bits(gb, 8) * 8;
635 if (get_bits_left(gb) < comment_len) {
636 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
639 skip_bits_long(gb, comment_len);
644 * Decode GA "General Audio" specific configuration; reference: table 4.1.
646 * @param ac pointer to AACContext, may be null
647 * @param avctx pointer to AVCCodecContext, used for logging
649 * @return Returns error status. 0 - OK, !0 - error
651 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
653 MPEG4AudioConfig *m4ac,
656 int extension_flag, ret;
657 uint8_t layout_map[MAX_ELEM_ID*4][3];
660 if (get_bits1(gb)) { // frameLengthFlag
661 av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
665 if (get_bits1(gb)) // dependsOnCoreCoder
666 skip_bits(gb, 14); // coreCoderDelay
667 extension_flag = get_bits1(gb);
669 if (m4ac->object_type == AOT_AAC_SCALABLE ||
670 m4ac->object_type == AOT_ER_AAC_SCALABLE)
671 skip_bits(gb, 3); // layerNr
673 if (channel_config == 0) {
674 skip_bits(gb, 4); // element_instance_tag
675 tags = decode_pce(avctx, m4ac, layout_map, gb);
679 if ((ret = set_default_channel_config(avctx, layout_map, &tags, channel_config)))
683 if (count_channels(layout_map, tags) > 1) {
685 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
688 if (ac && (ret = output_configure(ac, layout_map, tags,
689 channel_config, OC_GLOBAL_HDR)))
692 if (extension_flag) {
693 switch (m4ac->object_type) {
695 skip_bits(gb, 5); // numOfSubFrame
696 skip_bits(gb, 11); // layer_length
700 case AOT_ER_AAC_SCALABLE:
702 skip_bits(gb, 3); /* aacSectionDataResilienceFlag
703 * aacScalefactorDataResilienceFlag
704 * aacSpectralDataResilienceFlag
708 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
714 * Decode audio specific configuration; reference: table 1.13.
716 * @param ac pointer to AACContext, may be null
717 * @param avctx pointer to AVCCodecContext, used for logging
718 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
719 * @param data pointer to buffer holding an audio specific config
720 * @param bit_size size of audio specific config or data in bits
721 * @param sync_extension look for an appended sync extension
723 * @return Returns error status or number of consumed bits. <0 - error
725 static int decode_audio_specific_config(AACContext *ac,
726 AVCodecContext *avctx,
727 MPEG4AudioConfig *m4ac,
728 const uint8_t *data, int bit_size,
734 av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
735 for (i = 0; i < bit_size >> 3; i++)
736 av_dlog(avctx, "%02x ", data[i]);
737 av_dlog(avctx, "\n");
739 init_get_bits(&gb, data, bit_size);
741 if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
743 if (m4ac->sampling_index > 12) {
744 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
748 skip_bits_long(&gb, i);
750 switch (m4ac->object_type) {
754 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
758 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
759 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
763 av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
764 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
765 m4ac->sample_rate, m4ac->sbr, m4ac->ps);
767 return get_bits_count(&gb);
771 * linear congruential pseudorandom number generator
773 * @param previous_val pointer to the current state of the generator
775 * @return Returns a 32-bit pseudorandom integer
777 static av_always_inline int lcg_random(int previous_val)
779 return previous_val * 1664525 + 1013904223;
782 static av_always_inline void reset_predict_state(PredictorState *ps)
792 static void reset_all_predictors(PredictorState *ps)
795 for (i = 0; i < MAX_PREDICTORS; i++)
796 reset_predict_state(&ps[i]);
799 static int sample_rate_idx (int rate)
801 if (92017 <= rate) return 0;
802 else if (75132 <= rate) return 1;
803 else if (55426 <= rate) return 2;
804 else if (46009 <= rate) return 3;
805 else if (37566 <= rate) return 4;
806 else if (27713 <= rate) return 5;
807 else if (23004 <= rate) return 6;
808 else if (18783 <= rate) return 7;
809 else if (13856 <= rate) return 8;
810 else if (11502 <= rate) return 9;
811 else if (9391 <= rate) return 10;
815 static void reset_predictor_group(PredictorState *ps, int group_num)
818 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
819 reset_predict_state(&ps[i]);
822 #define AAC_INIT_VLC_STATIC(num, size) \
823 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
824 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
825 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
828 static av_cold int aac_decode_init(AVCodecContext *avctx)
830 AACContext *ac = avctx->priv_data;
831 float output_scale_factor;
834 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
836 if (avctx->extradata_size > 0) {
837 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
839 avctx->extradata_size*8, 1) < 0)
843 uint8_t layout_map[MAX_ELEM_ID*4][3];
846 sr = sample_rate_idx(avctx->sample_rate);
847 ac->oc[1].m4ac.sampling_index = sr;
848 ac->oc[1].m4ac.channels = avctx->channels;
849 ac->oc[1].m4ac.sbr = -1;
850 ac->oc[1].m4ac.ps = -1;
852 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
853 if (ff_mpeg4audio_channels[i] == avctx->channels)
855 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
858 ac->oc[1].m4ac.chan_config = i;
860 if (ac->oc[1].m4ac.chan_config) {
861 int ret = set_default_channel_config(avctx, layout_map,
862 &layout_map_tags, ac->oc[1].m4ac.chan_config);
864 output_configure(ac, layout_map, layout_map_tags,
865 ac->oc[1].m4ac.chan_config, OC_GLOBAL_HDR);
866 else if (avctx->err_recognition & AV_EF_EXPLODE)
867 return AVERROR_INVALIDDATA;
871 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
872 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
873 output_scale_factor = 1.0 / 32768.0;
875 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
876 output_scale_factor = 1.0;
879 AAC_INIT_VLC_STATIC( 0, 304);
880 AAC_INIT_VLC_STATIC( 1, 270);
881 AAC_INIT_VLC_STATIC( 2, 550);
882 AAC_INIT_VLC_STATIC( 3, 300);
883 AAC_INIT_VLC_STATIC( 4, 328);
884 AAC_INIT_VLC_STATIC( 5, 294);
885 AAC_INIT_VLC_STATIC( 6, 306);
886 AAC_INIT_VLC_STATIC( 7, 268);
887 AAC_INIT_VLC_STATIC( 8, 510);
888 AAC_INIT_VLC_STATIC( 9, 366);
889 AAC_INIT_VLC_STATIC(10, 462);
893 ff_dsputil_init(&ac->dsp, avctx);
894 ff_fmt_convert_init(&ac->fmt_conv, avctx);
896 ac->random_state = 0x1f2e3d4c;
900 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
901 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
902 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
905 ff_mdct_init(&ac->mdct, 11, 1, output_scale_factor/1024.0);
906 ff_mdct_init(&ac->mdct_small, 8, 1, output_scale_factor/128.0);
907 ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0/output_scale_factor);
908 // window initialization
909 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
910 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
911 ff_init_ff_sine_windows(10);
912 ff_init_ff_sine_windows( 7);
916 avcodec_get_frame_defaults(&ac->frame);
917 avctx->coded_frame = &ac->frame;
923 * Skip data_stream_element; reference: table 4.10.
925 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
927 int byte_align = get_bits1(gb);
928 int count = get_bits(gb, 8);
930 count += get_bits(gb, 8);
934 if (get_bits_left(gb) < 8 * count) {
935 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
938 skip_bits_long(gb, 8 * count);
942 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
947 ics->predictor_reset_group = get_bits(gb, 5);
948 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
949 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
953 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
954 ics->prediction_used[sfb] = get_bits1(gb);
960 * Decode Long Term Prediction data; reference: table 4.xx.
962 static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
963 GetBitContext *gb, uint8_t max_sfb)
967 ltp->lag = get_bits(gb, 11);
968 ltp->coef = ltp_coef[get_bits(gb, 3)];
969 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
970 ltp->used[sfb] = get_bits1(gb);
974 * Decode Individual Channel Stream info; reference: table 4.6.
976 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
980 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
981 return AVERROR_INVALIDDATA;
983 ics->window_sequence[1] = ics->window_sequence[0];
984 ics->window_sequence[0] = get_bits(gb, 2);
985 ics->use_kb_window[1] = ics->use_kb_window[0];
986 ics->use_kb_window[0] = get_bits1(gb);
987 ics->num_window_groups = 1;
988 ics->group_len[0] = 1;
989 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
991 ics->max_sfb = get_bits(gb, 4);
992 for (i = 0; i < 7; i++) {
994 ics->group_len[ics->num_window_groups - 1]++;
996 ics->num_window_groups++;
997 ics->group_len[ics->num_window_groups - 1] = 1;
1000 ics->num_windows = 8;
1001 ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
1002 ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
1003 ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
1004 ics->predictor_present = 0;
1006 ics->max_sfb = get_bits(gb, 6);
1007 ics->num_windows = 1;
1008 ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
1009 ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
1010 ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
1011 ics->predictor_present = get_bits1(gb);
1012 ics->predictor_reset_group = 0;
1013 if (ics->predictor_present) {
1014 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1015 if (decode_prediction(ac, ics, gb)) {
1018 } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
1019 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
1022 if ((ics->ltp.present = get_bits(gb, 1)))
1023 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1028 if (ics->max_sfb > ics->num_swb) {
1029 av_log(ac->avctx, AV_LOG_ERROR,
1030 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
1031 ics->max_sfb, ics->num_swb);
1038 return AVERROR_INVALIDDATA;
1042 * Decode band types (section_data payload); reference: table 4.46.
1044 * @param band_type array of the used band type
1045 * @param band_type_run_end array of the last scalefactor band of a band type run
1047 * @return Returns error status. 0 - OK, !0 - error
1049 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1050 int band_type_run_end[120], GetBitContext *gb,
1051 IndividualChannelStream *ics)
1054 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1055 for (g = 0; g < ics->num_window_groups; g++) {
1057 while (k < ics->max_sfb) {
1058 uint8_t sect_end = k;
1060 int sect_band_type = get_bits(gb, 4);
1061 if (sect_band_type == 12) {
1062 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1066 sect_len_incr = get_bits(gb, bits);
1067 sect_end += sect_len_incr;
1068 if (get_bits_left(gb) < 0) {
1069 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1072 if (sect_end > ics->max_sfb) {
1073 av_log(ac->avctx, AV_LOG_ERROR,
1074 "Number of bands (%d) exceeds limit (%d).\n",
1075 sect_end, ics->max_sfb);
1078 } while (sect_len_incr == (1 << bits) - 1);
1079 for (; k < sect_end; k++) {
1080 band_type [idx] = sect_band_type;
1081 band_type_run_end[idx++] = sect_end;
1089 * Decode scalefactors; reference: table 4.47.
1091 * @param global_gain first scalefactor value as scalefactors are differentially coded
1092 * @param band_type array of the used band type
1093 * @param band_type_run_end array of the last scalefactor band of a band type run
1094 * @param sf array of scalefactors or intensity stereo positions
1096 * @return Returns error status. 0 - OK, !0 - error
1098 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
1099 unsigned int global_gain,
1100 IndividualChannelStream *ics,
1101 enum BandType band_type[120],
1102 int band_type_run_end[120])
1105 int offset[3] = { global_gain, global_gain - 90, 0 };
1108 for (g = 0; g < ics->num_window_groups; g++) {
1109 for (i = 0; i < ics->max_sfb;) {
1110 int run_end = band_type_run_end[idx];
1111 if (band_type[idx] == ZERO_BT) {
1112 for (; i < run_end; i++, idx++)
1114 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
1115 for (; i < run_end; i++, idx++) {
1116 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1117 clipped_offset = av_clip(offset[2], -155, 100);
1118 if (offset[2] != clipped_offset) {
1119 av_log_ask_for_sample(ac->avctx, "Intensity stereo "
1120 "position clipped (%d -> %d).\nIf you heard an "
1121 "audible artifact, there may be a bug in the "
1122 "decoder. ", offset[2], clipped_offset);
1124 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1126 } else if (band_type[idx] == NOISE_BT) {
1127 for (; i < run_end; i++, idx++) {
1128 if (noise_flag-- > 0)
1129 offset[1] += get_bits(gb, 9) - 256;
1131 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1132 clipped_offset = av_clip(offset[1], -100, 155);
1133 if (offset[1] != clipped_offset) {
1134 av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
1135 "(%d -> %d).\nIf you heard an audible "
1136 "artifact, there may be a bug in the decoder. ",
1137 offset[1], clipped_offset);
1139 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1142 for (; i < run_end; i++, idx++) {
1143 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1144 if (offset[0] > 255U) {
1145 av_log(ac->avctx, AV_LOG_ERROR,
1146 "Scalefactor (%d) out of range.\n", offset[0]);
1149 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1158 * Decode pulse data; reference: table 4.7.
1160 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1161 const uint16_t *swb_offset, int num_swb)
1164 pulse->num_pulse = get_bits(gb, 2) + 1;
1165 pulse_swb = get_bits(gb, 6);
1166 if (pulse_swb >= num_swb)
1168 pulse->pos[0] = swb_offset[pulse_swb];
1169 pulse->pos[0] += get_bits(gb, 5);
1170 if (pulse->pos[0] > 1023)
1172 pulse->amp[0] = get_bits(gb, 4);
1173 for (i = 1; i < pulse->num_pulse; i++) {
1174 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1175 if (pulse->pos[i] > 1023)
1177 pulse->amp[i] = get_bits(gb, 4);
1183 * Decode Temporal Noise Shaping data; reference: table 4.48.
1185 * @return Returns error status. 0 - OK, !0 - error
1187 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
1188 GetBitContext *gb, const IndividualChannelStream *ics)
1190 int w, filt, i, coef_len, coef_res, coef_compress;
1191 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1192 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1193 for (w = 0; w < ics->num_windows; w++) {
1194 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1195 coef_res = get_bits1(gb);
1197 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1199 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1201 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1202 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
1203 tns->order[w][filt], tns_max_order);
1204 tns->order[w][filt] = 0;
1207 if (tns->order[w][filt]) {
1208 tns->direction[w][filt] = get_bits1(gb);
1209 coef_compress = get_bits1(gb);
1210 coef_len = coef_res + 3 - coef_compress;
1211 tmp2_idx = 2 * coef_compress + coef_res;
1213 for (i = 0; i < tns->order[w][filt]; i++)
1214 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1223 * Decode Mid/Side data; reference: table 4.54.
1225 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1226 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1227 * [3] reserved for scalable AAC
1229 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1233 if (ms_present == 1) {
1234 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
1235 cpe->ms_mask[idx] = get_bits1(gb);
1236 } else if (ms_present == 2) {
1237 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
1242 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
1246 *dst++ = v[idx & 15] * s;
1247 *dst++ = v[idx>>4 & 15] * s;
1253 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
1257 *dst++ = v[idx & 3] * s;
1258 *dst++ = v[idx>>2 & 3] * s;
1259 *dst++ = v[idx>>4 & 3] * s;
1260 *dst++ = v[idx>>6 & 3] * s;
1266 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
1267 unsigned sign, const float *scale)
1269 union av_intfloat32 s0, s1;
1271 s0.f = s1.f = *scale;
1272 s0.i ^= sign >> 1 << 31;
1275 *dst++ = v[idx & 15] * s0.f;
1276 *dst++ = v[idx>>4 & 15] * s1.f;
1283 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
1284 unsigned sign, const float *scale)
1286 unsigned nz = idx >> 12;
1287 union av_intfloat32 s = { .f = *scale };
1288 union av_intfloat32 t;
1290 t.i = s.i ^ (sign & 1U<<31);
1291 *dst++ = v[idx & 3] * t.f;
1293 sign <<= nz & 1; nz >>= 1;
1294 t.i = s.i ^ (sign & 1U<<31);
1295 *dst++ = v[idx>>2 & 3] * t.f;
1297 sign <<= nz & 1; nz >>= 1;
1298 t.i = s.i ^ (sign & 1U<<31);
1299 *dst++ = v[idx>>4 & 3] * t.f;
1301 sign <<= nz & 1; nz >>= 1;
1302 t.i = s.i ^ (sign & 1U<<31);
1303 *dst++ = v[idx>>6 & 3] * t.f;
1310 * Decode spectral data; reference: table 4.50.
1311 * Dequantize and scale spectral data; reference: 4.6.3.3.
1313 * @param coef array of dequantized, scaled spectral data
1314 * @param sf array of scalefactors or intensity stereo positions
1315 * @param pulse_present set if pulses are present
1316 * @param pulse pointer to pulse data struct
1317 * @param band_type array of the used band type
1319 * @return Returns error status. 0 - OK, !0 - error
1321 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
1322 GetBitContext *gb, const float sf[120],
1323 int pulse_present, const Pulse *pulse,
1324 const IndividualChannelStream *ics,
1325 enum BandType band_type[120])
1327 int i, k, g, idx = 0;
1328 const int c = 1024 / ics->num_windows;
1329 const uint16_t *offsets = ics->swb_offset;
1330 float *coef_base = coef;
1332 for (g = 0; g < ics->num_windows; g++)
1333 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
1335 for (g = 0; g < ics->num_window_groups; g++) {
1336 unsigned g_len = ics->group_len[g];
1338 for (i = 0; i < ics->max_sfb; i++, idx++) {
1339 const unsigned cbt_m1 = band_type[idx] - 1;
1340 float *cfo = coef + offsets[i];
1341 int off_len = offsets[i + 1] - offsets[i];
1344 if (cbt_m1 >= INTENSITY_BT2 - 1) {
1345 for (group = 0; group < g_len; group++, cfo+=128) {
1346 memset(cfo, 0, off_len * sizeof(float));
1348 } else if (cbt_m1 == NOISE_BT - 1) {
1349 for (group = 0; group < g_len; group++, cfo+=128) {
1353 for (k = 0; k < off_len; k++) {
1354 ac->random_state = lcg_random(ac->random_state);
1355 cfo[k] = ac->random_state;
1358 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
1359 scale = sf[idx] / sqrtf(band_energy);
1360 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1363 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1364 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1365 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1366 OPEN_READER(re, gb);
1368 switch (cbt_m1 >> 1) {
1370 for (group = 0; group < g_len; group++, cfo+=128) {
1378 UPDATE_CACHE(re, gb);
1379 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1380 cb_idx = cb_vector_idx[code];
1381 cf = VMUL4(cf, vq, cb_idx, sf + idx);
1387 for (group = 0; group < g_len; group++, cfo+=128) {
1397 UPDATE_CACHE(re, gb);
1398 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1399 cb_idx = cb_vector_idx[code];
1400 nnz = cb_idx >> 8 & 15;
1401 bits = nnz ? GET_CACHE(re, gb) : 0;
1402 LAST_SKIP_BITS(re, gb, nnz);
1403 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1409 for (group = 0; group < g_len; group++, cfo+=128) {
1417 UPDATE_CACHE(re, gb);
1418 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1419 cb_idx = cb_vector_idx[code];
1420 cf = VMUL2(cf, vq, cb_idx, sf + idx);
1427 for (group = 0; group < g_len; group++, cfo+=128) {
1437 UPDATE_CACHE(re, gb);
1438 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1439 cb_idx = cb_vector_idx[code];
1440 nnz = cb_idx >> 8 & 15;
1441 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1442 LAST_SKIP_BITS(re, gb, nnz);
1443 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1449 for (group = 0; group < g_len; group++, cfo+=128) {
1451 uint32_t *icf = (uint32_t *) cf;
1461 UPDATE_CACHE(re, gb);
1462 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1470 cb_idx = cb_vector_idx[code];
1473 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1474 LAST_SKIP_BITS(re, gb, nnz);
1476 for (j = 0; j < 2; j++) {
1480 /* The total length of escape_sequence must be < 22 bits according
1481 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1482 UPDATE_CACHE(re, gb);
1483 b = GET_CACHE(re, gb);
1484 b = 31 - av_log2(~b);
1487 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1491 SKIP_BITS(re, gb, b + 1);
1493 n = (1 << b) + SHOW_UBITS(re, gb, b);
1494 LAST_SKIP_BITS(re, gb, b);
1495 *icf++ = cbrt_tab[n] | (bits & 1U<<31);
1498 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1499 *icf++ = (bits & 1U<<31) | v;
1506 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1510 CLOSE_READER(re, gb);
1516 if (pulse_present) {
1518 for (i = 0; i < pulse->num_pulse; i++) {
1519 float co = coef_base[ pulse->pos[i] ];
1520 while (offsets[idx + 1] <= pulse->pos[i])
1522 if (band_type[idx] != NOISE_BT && sf[idx]) {
1523 float ico = -pulse->amp[i];
1526 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1528 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1535 static av_always_inline float flt16_round(float pf)
1537 union av_intfloat32 tmp;
1539 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1543 static av_always_inline float flt16_even(float pf)
1545 union av_intfloat32 tmp;
1547 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1551 static av_always_inline float flt16_trunc(float pf)
1553 union av_intfloat32 pun;
1555 pun.i &= 0xFFFF0000U;
1559 static av_always_inline void predict(PredictorState *ps, float *coef,
1562 const float a = 0.953125; // 61.0 / 64
1563 const float alpha = 0.90625; // 29.0 / 32
1567 float r0 = ps->r0, r1 = ps->r1;
1568 float cor0 = ps->cor0, cor1 = ps->cor1;
1569 float var0 = ps->var0, var1 = ps->var1;
1571 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
1572 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
1574 pv = flt16_round(k1 * r0 + k2 * r1);
1581 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
1582 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
1583 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
1584 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
1586 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
1587 ps->r0 = flt16_trunc(a * e0);
1591 * Apply AAC-Main style frequency domain prediction.
1593 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1597 if (!sce->ics.predictor_initialized) {
1598 reset_all_predictors(sce->predictor_state);
1599 sce->ics.predictor_initialized = 1;
1602 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1603 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
1604 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
1605 predict(&sce->predictor_state[k], &sce->coeffs[k],
1606 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
1609 if (sce->ics.predictor_reset_group)
1610 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
1612 reset_all_predictors(sce->predictor_state);
1616 * Decode an individual_channel_stream payload; reference: table 4.44.
1618 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1619 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1621 * @return Returns error status. 0 - OK, !0 - error
1623 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1624 GetBitContext *gb, int common_window, int scale_flag)
1627 TemporalNoiseShaping *tns = &sce->tns;
1628 IndividualChannelStream *ics = &sce->ics;
1629 float *out = sce->coeffs;
1630 int global_gain, pulse_present = 0;
1632 /* This assignment is to silence a GCC warning about the variable being used
1633 * uninitialized when in fact it always is.
1635 pulse.num_pulse = 0;
1637 global_gain = get_bits(gb, 8);
1639 if (!common_window && !scale_flag) {
1640 if (decode_ics_info(ac, ics, gb) < 0)
1641 return AVERROR_INVALIDDATA;
1644 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
1646 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
1651 if ((pulse_present = get_bits1(gb))) {
1652 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1653 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
1656 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1657 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
1661 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1663 if (get_bits1(gb)) {
1664 av_log_missing_feature(ac->avctx, "SSR", 1);
1669 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
1672 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1673 apply_prediction(ac, sce);
1679 * Mid/Side stereo decoding; reference: 4.6.8.1.3.
1681 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
1683 const IndividualChannelStream *ics = &cpe->ch[0].ics;
1684 float *ch0 = cpe->ch[0].coeffs;
1685 float *ch1 = cpe->ch[1].coeffs;
1686 int g, i, group, idx = 0;
1687 const uint16_t *offsets = ics->swb_offset;
1688 for (g = 0; g < ics->num_window_groups; g++) {
1689 for (i = 0; i < ics->max_sfb; i++, idx++) {
1690 if (cpe->ms_mask[idx] &&
1691 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
1692 for (group = 0; group < ics->group_len[g]; group++) {
1693 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
1694 ch1 + group * 128 + offsets[i],
1695 offsets[i+1] - offsets[i]);
1699 ch0 += ics->group_len[g] * 128;
1700 ch1 += ics->group_len[g] * 128;
1705 * intensity stereo decoding; reference: 4.6.8.2.3
1707 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1708 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1709 * [3] reserved for scalable AAC
1711 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
1713 const IndividualChannelStream *ics = &cpe->ch[1].ics;
1714 SingleChannelElement *sce1 = &cpe->ch[1];
1715 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1716 const uint16_t *offsets = ics->swb_offset;
1717 int g, group, i, idx = 0;
1720 for (g = 0; g < ics->num_window_groups; g++) {
1721 for (i = 0; i < ics->max_sfb;) {
1722 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
1723 const int bt_run_end = sce1->band_type_run_end[idx];
1724 for (; i < bt_run_end; i++, idx++) {
1725 c = -1 + 2 * (sce1->band_type[idx] - 14);
1727 c *= 1 - 2 * cpe->ms_mask[idx];
1728 scale = c * sce1->sf[idx];
1729 for (group = 0; group < ics->group_len[g]; group++)
1730 ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
1731 coef0 + group * 128 + offsets[i],
1733 offsets[i + 1] - offsets[i]);
1736 int bt_run_end = sce1->band_type_run_end[idx];
1737 idx += bt_run_end - i;
1741 coef0 += ics->group_len[g] * 128;
1742 coef1 += ics->group_len[g] * 128;
1747 * Decode a channel_pair_element; reference: table 4.4.
1749 * @return Returns error status. 0 - OK, !0 - error
1751 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
1753 int i, ret, common_window, ms_present = 0;
1755 common_window = get_bits1(gb);
1756 if (common_window) {
1757 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1758 return AVERROR_INVALIDDATA;
1759 i = cpe->ch[1].ics.use_kb_window[0];
1760 cpe->ch[1].ics = cpe->ch[0].ics;
1761 cpe->ch[1].ics.use_kb_window[1] = i;
1762 if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1763 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1764 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1765 ms_present = get_bits(gb, 2);
1766 if (ms_present == 3) {
1767 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1769 } else if (ms_present)
1770 decode_mid_side_stereo(cpe, gb, ms_present);
1772 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1774 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1777 if (common_window) {
1779 apply_mid_side_stereo(ac, cpe);
1780 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1781 apply_prediction(ac, &cpe->ch[0]);
1782 apply_prediction(ac, &cpe->ch[1]);
1786 apply_intensity_stereo(ac, cpe, ms_present);
1790 static const float cce_scale[] = {
1791 1.09050773266525765921, //2^(1/8)
1792 1.18920711500272106672, //2^(1/4)
1798 * Decode coupling_channel_element; reference: table 4.8.
1800 * @return Returns error status. 0 - OK, !0 - error
1802 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
1808 SingleChannelElement *sce = &che->ch[0];
1809 ChannelCoupling *coup = &che->coup;
1811 coup->coupling_point = 2 * get_bits1(gb);
1812 coup->num_coupled = get_bits(gb, 3);
1813 for (c = 0; c <= coup->num_coupled; c++) {
1815 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1816 coup->id_select[c] = get_bits(gb, 4);
1817 if (coup->type[c] == TYPE_CPE) {
1818 coup->ch_select[c] = get_bits(gb, 2);
1819 if (coup->ch_select[c] == 3)
1822 coup->ch_select[c] = 2;
1824 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1826 sign = get_bits(gb, 1);
1827 scale = cce_scale[get_bits(gb, 2)];
1829 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1832 for (c = 0; c < num_gain; c++) {
1836 float gain_cache = 1.;
1838 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1839 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1840 gain_cache = powf(scale, -gain);
1842 if (coup->coupling_point == AFTER_IMDCT) {
1843 coup->gain[c][0] = gain_cache;
1845 for (g = 0; g < sce->ics.num_window_groups; g++) {
1846 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1847 if (sce->band_type[idx] != ZERO_BT) {
1849 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1857 gain_cache = powf(scale, -t) * s;
1860 coup->gain[c][idx] = gain_cache;
1870 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1872 * @return Returns number of bytes consumed.
1874 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1878 int num_excl_chan = 0;
1881 for (i = 0; i < 7; i++)
1882 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1883 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1885 return num_excl_chan / 7;
1889 * Decode dynamic range information; reference: table 4.52.
1891 * @param cnt length of TYPE_FIL syntactic element in bytes
1893 * @return Returns number of bytes consumed.
1895 static int decode_dynamic_range(DynamicRangeControl *che_drc,
1896 GetBitContext *gb, int cnt)
1899 int drc_num_bands = 1;
1902 /* pce_tag_present? */
1903 if (get_bits1(gb)) {
1904 che_drc->pce_instance_tag = get_bits(gb, 4);
1905 skip_bits(gb, 4); // tag_reserved_bits
1909 /* excluded_chns_present? */
1910 if (get_bits1(gb)) {
1911 n += decode_drc_channel_exclusions(che_drc, gb);
1914 /* drc_bands_present? */
1915 if (get_bits1(gb)) {
1916 che_drc->band_incr = get_bits(gb, 4);
1917 che_drc->interpolation_scheme = get_bits(gb, 4);
1919 drc_num_bands += che_drc->band_incr;
1920 for (i = 0; i < drc_num_bands; i++) {
1921 che_drc->band_top[i] = get_bits(gb, 8);
1926 /* prog_ref_level_present? */
1927 if (get_bits1(gb)) {
1928 che_drc->prog_ref_level = get_bits(gb, 7);
1929 skip_bits1(gb); // prog_ref_level_reserved_bits
1933 for (i = 0; i < drc_num_bands; i++) {
1934 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1935 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1943 * Decode extension data (incomplete); reference: table 4.51.
1945 * @param cnt length of TYPE_FIL syntactic element in bytes
1947 * @return Returns number of bytes consumed
1949 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
1950 ChannelElement *che, enum RawDataBlockType elem_type)
1954 switch (get_bits(gb, 4)) { // extension type
1955 case EXT_SBR_DATA_CRC:
1959 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1961 } else if (!ac->oc[1].m4ac.sbr) {
1962 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1963 skip_bits_long(gb, 8 * cnt - 4);
1965 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
1966 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1967 skip_bits_long(gb, 8 * cnt - 4);
1969 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
1970 ac->oc[1].m4ac.sbr = 1;
1971 ac->oc[1].m4ac.ps = 1;
1972 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
1973 ac->oc[1].m4ac.chan_config, ac->oc[1].status);
1975 ac->oc[1].m4ac.sbr = 1;
1977 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
1979 case EXT_DYNAMIC_RANGE:
1980 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
1984 case EXT_DATA_ELEMENT:
1986 skip_bits_long(gb, 8 * cnt - 4);
1993 * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
1995 * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
1996 * @param coef spectral coefficients
1998 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
1999 IndividualChannelStream *ics, int decode)
2001 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2003 int bottom, top, order, start, end, size, inc;
2004 float lpc[TNS_MAX_ORDER];
2005 float tmp[TNS_MAX_ORDER];
2007 for (w = 0; w < ics->num_windows; w++) {
2008 bottom = ics->num_swb;
2009 for (filt = 0; filt < tns->n_filt[w]; filt++) {
2011 bottom = FFMAX(0, top - tns->length[w][filt]);
2012 order = tns->order[w][filt];
2017 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
2019 start = ics->swb_offset[FFMIN(bottom, mmm)];
2020 end = ics->swb_offset[FFMIN( top, mmm)];
2021 if ((size = end - start) <= 0)
2023 if (tns->direction[w][filt]) {
2033 for (m = 0; m < size; m++, start += inc)
2034 for (i = 1; i <= FFMIN(m, order); i++)
2035 coef[start] -= coef[start - i * inc] * lpc[i - 1];
2038 for (m = 0; m < size; m++, start += inc) {
2039 tmp[0] = coef[start];
2040 for (i = 1; i <= FFMIN(m, order); i++)
2041 coef[start] += tmp[i] * lpc[i - 1];
2042 for (i = order; i > 0; i--)
2043 tmp[i] = tmp[i - 1];
2051 * Apply windowing and MDCT to obtain the spectral
2052 * coefficient from the predicted sample by LTP.
2054 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
2055 float *in, IndividualChannelStream *ics)
2057 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2058 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2059 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2060 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2062 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2063 ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
2065 memset(in, 0, 448 * sizeof(float));
2066 ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
2068 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2069 ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2071 ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2072 memset(in + 1024 + 576, 0, 448 * sizeof(float));
2074 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2078 * Apply the long term prediction
2080 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
2082 const LongTermPrediction *ltp = &sce->ics.ltp;
2083 const uint16_t *offsets = sce->ics.swb_offset;
2086 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2087 float *predTime = sce->ret;
2088 float *predFreq = ac->buf_mdct;
2089 int16_t num_samples = 2048;
2091 if (ltp->lag < 1024)
2092 num_samples = ltp->lag + 1024;
2093 for (i = 0; i < num_samples; i++)
2094 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
2095 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
2097 windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2099 if (sce->tns.present)
2100 apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2102 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2104 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2105 sce->coeffs[i] += predFreq[i];
2110 * Update the LTP buffer for next frame
2112 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
2114 IndividualChannelStream *ics = &sce->ics;
2115 float *saved = sce->saved;
2116 float *saved_ltp = sce->coeffs;
2117 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2118 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2121 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2122 memcpy(saved_ltp, saved, 512 * sizeof(float));
2123 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2124 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2125 for (i = 0; i < 64; i++)
2126 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2127 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2128 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
2129 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2130 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2131 for (i = 0; i < 64; i++)
2132 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2133 } else { // LONG_STOP or ONLY_LONG
2134 ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2135 for (i = 0; i < 512; i++)
2136 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
2139 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2140 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2141 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2145 * Conduct IMDCT and windowing.
2147 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
2149 IndividualChannelStream *ics = &sce->ics;
2150 float *in = sce->coeffs;
2151 float *out = sce->ret;
2152 float *saved = sce->saved;
2153 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2154 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2155 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2156 float *buf = ac->buf_mdct;
2157 float *temp = ac->temp;
2161 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2162 for (i = 0; i < 1024; i += 128)
2163 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2165 ac->mdct.imdct_half(&ac->mdct, buf, in);
2167 /* window overlapping
2168 * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2169 * and long to short transitions are considered to be short to short
2170 * transitions. This leaves just two cases (long to long and short to short)
2171 * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2173 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2174 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2175 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2177 memcpy( out, saved, 448 * sizeof(float));
2179 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2180 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2181 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2182 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2183 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2184 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2185 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
2187 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2188 memcpy( out + 576, buf + 64, 448 * sizeof(float));
2193 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2194 memcpy( saved, temp + 64, 64 * sizeof(float));
2195 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2196 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2197 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2198 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2199 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2200 memcpy( saved, buf + 512, 448 * sizeof(float));
2201 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2202 } else { // LONG_STOP or ONLY_LONG
2203 memcpy( saved, buf + 512, 512 * sizeof(float));
2208 * Apply dependent channel coupling (applied before IMDCT).
2210 * @param index index into coupling gain array
2212 static void apply_dependent_coupling(AACContext *ac,
2213 SingleChannelElement *target,
2214 ChannelElement *cce, int index)
2216 IndividualChannelStream *ics = &cce->ch[0].ics;
2217 const uint16_t *offsets = ics->swb_offset;
2218 float *dest = target->coeffs;
2219 const float *src = cce->ch[0].coeffs;
2220 int g, i, group, k, idx = 0;
2221 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2222 av_log(ac->avctx, AV_LOG_ERROR,
2223 "Dependent coupling is not supported together with LTP\n");
2226 for (g = 0; g < ics->num_window_groups; g++) {
2227 for (i = 0; i < ics->max_sfb; i++, idx++) {
2228 if (cce->ch[0].band_type[idx] != ZERO_BT) {
2229 const float gain = cce->coup.gain[index][idx];
2230 for (group = 0; group < ics->group_len[g]; group++) {
2231 for (k = offsets[i]; k < offsets[i + 1]; k++) {
2233 dest[group * 128 + k] += gain * src[group * 128 + k];
2238 dest += ics->group_len[g] * 128;
2239 src += ics->group_len[g] * 128;
2244 * Apply independent channel coupling (applied after IMDCT).
2246 * @param index index into coupling gain array
2248 static void apply_independent_coupling(AACContext *ac,
2249 SingleChannelElement *target,
2250 ChannelElement *cce, int index)
2253 const float gain = cce->coup.gain[index][0];
2254 const float *src = cce->ch[0].ret;
2255 float *dest = target->ret;
2256 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
2258 for (i = 0; i < len; i++)
2259 dest[i] += gain * src[i];
2263 * channel coupling transformation interface
2265 * @param apply_coupling_method pointer to (in)dependent coupling function
2267 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
2268 enum RawDataBlockType type, int elem_id,
2269 enum CouplingPoint coupling_point,
2270 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2274 for (i = 0; i < MAX_ELEM_ID; i++) {
2275 ChannelElement *cce = ac->che[TYPE_CCE][i];
2278 if (cce && cce->coup.coupling_point == coupling_point) {
2279 ChannelCoupling *coup = &cce->coup;
2281 for (c = 0; c <= coup->num_coupled; c++) {
2282 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2283 if (coup->ch_select[c] != 1) {
2284 apply_coupling_method(ac, &cc->ch[0], cce, index);
2285 if (coup->ch_select[c] != 0)
2288 if (coup->ch_select[c] != 2)
2289 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2291 index += 1 + (coup->ch_select[c] == 3);
2298 * Convert spectral data to float samples, applying all supported tools as appropriate.
2300 static void spectral_to_sample(AACContext *ac)
2303 for (type = 3; type >= 0; type--) {
2304 for (i = 0; i < MAX_ELEM_ID; i++) {
2305 ChannelElement *che = ac->che[type][i];
2307 if (type <= TYPE_CPE)
2308 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
2309 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2310 if (che->ch[0].ics.predictor_present) {
2311 if (che->ch[0].ics.ltp.present)
2312 apply_ltp(ac, &che->ch[0]);
2313 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2314 apply_ltp(ac, &che->ch[1]);
2317 if (che->ch[0].tns.present)
2318 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2319 if (che->ch[1].tns.present)
2320 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2321 if (type <= TYPE_CPE)
2322 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
2323 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2324 imdct_and_windowing(ac, &che->ch[0]);
2325 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2326 update_ltp(ac, &che->ch[0]);
2327 if (type == TYPE_CPE) {
2328 imdct_and_windowing(ac, &che->ch[1]);
2329 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2330 update_ltp(ac, &che->ch[1]);
2332 if (ac->oc[1].m4ac.sbr > 0) {
2333 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2336 if (type <= TYPE_CCE)
2337 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
2343 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
2346 AACADTSHeaderInfo hdr_info;
2347 uint8_t layout_map[MAX_ELEM_ID*4][3];
2348 int layout_map_tags;
2350 size = avpriv_aac_parse_header(gb, &hdr_info);
2352 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2353 // This is 2 for "VLB " audio in NSV files.
2354 // See samples/nsv/vlb_audio.
2355 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
2356 ac->warned_num_aac_frames = 1;
2358 push_output_configuration(ac);
2359 if (hdr_info.chan_config) {
2360 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2361 if (set_default_channel_config(ac->avctx, layout_map,
2362 &layout_map_tags, hdr_info.chan_config))
2364 if (output_configure(ac, layout_map, layout_map_tags,
2365 hdr_info.chan_config,
2366 FFMAX(ac->oc[1].status, OC_TRIAL_FRAME)))
2369 ac->oc[1].m4ac.chan_config = 0;
2371 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2372 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2373 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2374 if (ac->oc[0].status != OC_LOCKED ||
2375 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2376 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2377 ac->oc[1].m4ac.sbr = -1;
2378 ac->oc[1].m4ac.ps = -1;
2380 if (!hdr_info.crc_absent)
2386 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2387 int *got_frame_ptr, GetBitContext *gb)
2389 AACContext *ac = avctx->priv_data;
2390 ChannelElement *che = NULL, *che_prev = NULL;
2391 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2393 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2395 if (show_bits(gb, 12) == 0xfff) {
2396 if (parse_adts_frame_header(ac, gb) < 0) {
2397 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2401 if (ac->oc[1].m4ac.sampling_index > 12) {
2402 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2408 ac->tags_mapped = 0;
2410 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2411 elem_id = get_bits(gb, 4);
2413 if (elem_type < TYPE_DSE) {
2414 if (!(che=get_che(ac, elem_type, elem_id))) {
2415 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2416 elem_type, elem_id);
2423 switch (elem_type) {
2426 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2431 err = decode_cpe(ac, gb, che);
2436 err = decode_cce(ac, gb, che);
2440 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2445 err = skip_data_stream_element(ac, gb);
2449 uint8_t layout_map[MAX_ELEM_ID*4][3];
2451 push_output_configuration(ac);
2452 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
2458 av_log(avctx, AV_LOG_ERROR,
2459 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2460 pop_output_configuration(ac);
2462 err = output_configure(ac, layout_map, tags, 0, OC_TRIAL_PCE);
2464 ac->oc[1].m4ac.chan_config = 0;
2472 elem_id += get_bits(gb, 8) - 1;
2473 if (get_bits_left(gb) < 8 * elem_id) {
2474 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2479 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
2480 err = 0; /* FIXME */
2484 err = -1; /* should not happen, but keeps compiler happy */
2489 elem_type_prev = elem_type;
2494 if (get_bits_left(gb) < 3) {
2495 av_log(avctx, AV_LOG_ERROR, overread_err);
2501 spectral_to_sample(ac);
2503 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2504 samples <<= multiplier;
2507 /* get output buffer */
2508 ac->frame.nb_samples = samples;
2509 if ((err = avctx->get_buffer(avctx, &ac->frame)) < 0) {
2510 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2515 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
2516 ac->fmt_conv.float_interleave((float *)ac->frame.data[0],
2517 (const float **)ac->output_data,
2518 samples, avctx->channels);
2520 ac->fmt_conv.float_to_int16_interleave((int16_t *)ac->frame.data[0],
2521 (const float **)ac->output_data,
2522 samples, avctx->channels);
2524 *(AVFrame *)data = ac->frame;
2526 *got_frame_ptr = !!samples;
2528 if (ac->oc[1].status && audio_found) {
2529 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2530 avctx->frame_size = samples;
2531 ac->oc[1].status = OC_LOCKED;
2536 pop_output_configuration(ac);
2540 static int aac_decode_frame(AVCodecContext *avctx, void *data,
2541 int *got_frame_ptr, AVPacket *avpkt)
2543 AACContext *ac = avctx->priv_data;
2544 const uint8_t *buf = avpkt->data;
2545 int buf_size = avpkt->size;
2550 int new_extradata_size;
2551 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2552 AV_PKT_DATA_NEW_EXTRADATA,
2553 &new_extradata_size);
2555 if (new_extradata) {
2556 av_free(avctx->extradata);
2557 avctx->extradata = av_mallocz(new_extradata_size +
2558 FF_INPUT_BUFFER_PADDING_SIZE);
2559 if (!avctx->extradata)
2560 return AVERROR(ENOMEM);
2561 avctx->extradata_size = new_extradata_size;
2562 memcpy(avctx->extradata, new_extradata, new_extradata_size);
2563 push_output_configuration(ac);
2564 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
2566 avctx->extradata_size*8, 1) < 0) {
2567 pop_output_configuration(ac);
2568 return AVERROR_INVALIDDATA;
2572 init_get_bits(&gb, buf, buf_size * 8);
2574 if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb)) < 0)
2577 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2578 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2579 if (buf[buf_offset])
2582 return buf_size > buf_offset ? buf_consumed : buf_size;
2585 static av_cold int aac_decode_close(AVCodecContext *avctx)
2587 AACContext *ac = avctx->priv_data;
2590 for (i = 0; i < MAX_ELEM_ID; i++) {
2591 for (type = 0; type < 4; type++) {
2592 if (ac->che[type][i])
2593 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2594 av_freep(&ac->che[type][i]);
2598 ff_mdct_end(&ac->mdct);
2599 ff_mdct_end(&ac->mdct_small);
2600 ff_mdct_end(&ac->mdct_ltp);
2605 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
2607 struct LATMContext {
2608 AACContext aac_ctx; ///< containing AACContext
2609 int initialized; ///< initilized after a valid extradata was seen
2612 int audio_mux_version_A; ///< LATM syntax version
2613 int frame_length_type; ///< 0/1 variable/fixed frame length
2614 int frame_length; ///< frame length for fixed frame length
2617 static inline uint32_t latm_get_value(GetBitContext *b)
2619 int length = get_bits(b, 2);
2621 return get_bits_long(b, (length+1)*8);
2624 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
2625 GetBitContext *gb, int asclen)
2627 AACContext *ac = &latmctx->aac_ctx;
2628 AVCodecContext *avctx = ac->avctx;
2629 MPEG4AudioConfig m4ac = { 0 };
2630 int config_start_bit = get_bits_count(gb);
2631 int sync_extension = 0;
2632 int bits_consumed, esize;
2636 asclen = FFMIN(asclen, get_bits_left(gb));
2638 asclen = get_bits_left(gb);
2640 if (config_start_bit % 8) {
2641 av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
2642 "config not byte aligned.\n", 1);
2643 return AVERROR_INVALIDDATA;
2646 return AVERROR_INVALIDDATA;
2647 bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
2648 gb->buffer + (config_start_bit / 8),
2649 asclen, sync_extension);
2651 if (bits_consumed < 0)
2652 return AVERROR_INVALIDDATA;
2654 if (ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
2655 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
2657 av_log(avctx, AV_LOG_INFO, "audio config changed\n");
2658 latmctx->initialized = 0;
2660 esize = (bits_consumed+7) / 8;
2662 if (avctx->extradata_size < esize) {
2663 av_free(avctx->extradata);
2664 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
2665 if (!avctx->extradata)
2666 return AVERROR(ENOMEM);
2669 avctx->extradata_size = esize;
2670 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
2671 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2673 skip_bits_long(gb, bits_consumed);
2675 return bits_consumed;
2678 static int read_stream_mux_config(struct LATMContext *latmctx,
2681 int ret, audio_mux_version = get_bits(gb, 1);
2683 latmctx->audio_mux_version_A = 0;
2684 if (audio_mux_version)
2685 latmctx->audio_mux_version_A = get_bits(gb, 1);
2687 if (!latmctx->audio_mux_version_A) {
2689 if (audio_mux_version)
2690 latm_get_value(gb); // taraFullness
2692 skip_bits(gb, 1); // allStreamSameTimeFraming
2693 skip_bits(gb, 6); // numSubFrames
2695 if (get_bits(gb, 4)) { // numPrograms
2696 av_log_missing_feature(latmctx->aac_ctx.avctx,
2697 "multiple programs are not supported\n", 1);
2698 return AVERROR_PATCHWELCOME;
2701 // for each program (which there is only on in DVB)
2703 // for each layer (which there is only on in DVB)
2704 if (get_bits(gb, 3)) { // numLayer
2705 av_log_missing_feature(latmctx->aac_ctx.avctx,
2706 "multiple layers are not supported\n", 1);
2707 return AVERROR_PATCHWELCOME;
2710 // for all but first stream: use_same_config = get_bits(gb, 1);
2711 if (!audio_mux_version) {
2712 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
2715 int ascLen = latm_get_value(gb);
2716 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
2719 skip_bits_long(gb, ascLen);
2722 latmctx->frame_length_type = get_bits(gb, 3);
2723 switch (latmctx->frame_length_type) {
2725 skip_bits(gb, 8); // latmBufferFullness
2728 latmctx->frame_length = get_bits(gb, 9);
2733 skip_bits(gb, 6); // CELP frame length table index
2737 skip_bits(gb, 1); // HVXC frame length table index
2741 if (get_bits(gb, 1)) { // other data
2742 if (audio_mux_version) {
2743 latm_get_value(gb); // other_data_bits
2747 esc = get_bits(gb, 1);
2753 if (get_bits(gb, 1)) // crc present
2754 skip_bits(gb, 8); // config_crc
2760 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
2764 if (ctx->frame_length_type == 0) {
2765 int mux_slot_length = 0;
2767 tmp = get_bits(gb, 8);
2768 mux_slot_length += tmp;
2769 } while (tmp == 255);
2770 return mux_slot_length;
2771 } else if (ctx->frame_length_type == 1) {
2772 return ctx->frame_length;
2773 } else if (ctx->frame_length_type == 3 ||
2774 ctx->frame_length_type == 5 ||
2775 ctx->frame_length_type == 7) {
2776 skip_bits(gb, 2); // mux_slot_length_coded
2781 static int read_audio_mux_element(struct LATMContext *latmctx,
2785 uint8_t use_same_mux = get_bits(gb, 1);
2786 if (!use_same_mux) {
2787 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
2789 } else if (!latmctx->aac_ctx.avctx->extradata) {
2790 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
2791 "no decoder config found\n");
2792 return AVERROR(EAGAIN);
2794 if (latmctx->audio_mux_version_A == 0) {
2795 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
2796 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
2797 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
2798 return AVERROR_INVALIDDATA;
2799 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
2800 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2801 "frame length mismatch %d << %d\n",
2802 mux_slot_length_bytes * 8, get_bits_left(gb));
2803 return AVERROR_INVALIDDATA;
2810 static int latm_decode_frame(AVCodecContext *avctx, void *out,
2811 int *got_frame_ptr, AVPacket *avpkt)
2813 struct LATMContext *latmctx = avctx->priv_data;
2817 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
2819 // check for LOAS sync word
2820 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
2821 return AVERROR_INVALIDDATA;
2823 muxlength = get_bits(&gb, 13) + 3;
2824 // not enough data, the parser should have sorted this
2825 if (muxlength > avpkt->size)
2826 return AVERROR_INVALIDDATA;
2828 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
2831 if (!latmctx->initialized) {
2832 if (!avctx->extradata) {
2836 push_output_configuration(&latmctx->aac_ctx);
2837 if ((err = decode_audio_specific_config(
2838 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
2839 avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
2840 pop_output_configuration(&latmctx->aac_ctx);
2843 latmctx->initialized = 1;
2847 if (show_bits(&gb, 12) == 0xfff) {
2848 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2849 "ADTS header detected, probably as result of configuration "
2851 return AVERROR_INVALIDDATA;
2854 if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)
2860 static av_cold int latm_decode_init(AVCodecContext *avctx)
2862 struct LATMContext *latmctx = avctx->priv_data;
2863 int ret = aac_decode_init(avctx);
2865 if (avctx->extradata_size > 0)
2866 latmctx->initialized = !ret;
2872 AVCodec ff_aac_decoder = {
2874 .type = AVMEDIA_TYPE_AUDIO,
2876 .priv_data_size = sizeof(AACContext),
2877 .init = aac_decode_init,
2878 .close = aac_decode_close,
2879 .decode = aac_decode_frame,
2880 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
2881 .sample_fmts = (const enum AVSampleFormat[]) {
2882 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
2884 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2885 .channel_layouts = aac_channel_layout,
2889 Note: This decoder filter is intended to decode LATM streams transferred
2890 in MPEG transport streams which only contain one program.
2891 To do a more complex LATM demuxing a separate LATM demuxer should be used.
2893 AVCodec ff_aac_latm_decoder = {
2895 .type = AVMEDIA_TYPE_AUDIO,
2896 .id = CODEC_ID_AAC_LATM,
2897 .priv_data_size = sizeof(struct LATMContext),
2898 .init = latm_decode_init,
2899 .close = aac_decode_close,
2900 .decode = latm_decode_frame,
2901 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
2902 .sample_fmts = (const enum AVSampleFormat[]) {
2903 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
2905 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2906 .channel_layouts = aac_channel_layout,