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 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
482 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
485 if (output_configure(ac, layout_map, layout_map_tags,
486 2, OC_TRIAL_FRAME) < 0)
489 ac->oc[1].m4ac.chan_config = 2;
492 if (!ac->tags_mapped && type == TYPE_SCE && ac->oc[1].m4ac.chan_config == 2) {
493 uint8_t layout_map[MAX_ELEM_ID*4][3];
495 push_output_configuration(ac);
497 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
499 if (set_default_channel_config(ac->avctx, layout_map, &layout_map_tags,
502 if (output_configure(ac, layout_map, layout_map_tags,
503 1, OC_TRIAL_FRAME) < 0)
506 ac->oc[1].m4ac.chan_config = 1;
508 // For indexed channel configurations map the channels solely based on position.
509 switch (ac->oc[1].m4ac.chan_config) {
511 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
513 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
516 /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
517 instead of SCE[0] CPE[0] CPE[1] LFE[0]. If we seem to have
518 encountered such a stream, transfer the LFE[0] element to the SCE[1]'s mapping */
519 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
521 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
524 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
526 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
529 if (ac->tags_mapped == 2 && ac->oc[1].m4ac.chan_config == 4 && type == TYPE_SCE) {
531 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
535 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) && type == TYPE_CPE) {
537 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
538 } else if (ac->oc[1].m4ac.chan_config == 2) {
542 if (!ac->tags_mapped && type == TYPE_SCE) {
544 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
552 * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
554 * @param type speaker type/position for these channels
556 static void decode_channel_map(uint8_t layout_map[][3],
557 enum ChannelPosition type,
558 GetBitContext *gb, int n)
561 enum RawDataBlockType syn_ele;
563 case AAC_CHANNEL_FRONT:
564 case AAC_CHANNEL_BACK:
565 case AAC_CHANNEL_SIDE:
566 syn_ele = get_bits1(gb);
572 case AAC_CHANNEL_LFE:
576 layout_map[0][0] = syn_ele;
577 layout_map[0][1] = get_bits(gb, 4);
578 layout_map[0][2] = type;
584 * Decode program configuration element; reference: table 4.2.
586 * @return Returns error status. 0 - OK, !0 - error
588 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
589 uint8_t (*layout_map)[3],
592 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc, sampling_index;
596 skip_bits(gb, 2); // object_type
598 sampling_index = get_bits(gb, 4);
599 if (m4ac->sampling_index != sampling_index)
600 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");
602 num_front = get_bits(gb, 4);
603 num_side = get_bits(gb, 4);
604 num_back = get_bits(gb, 4);
605 num_lfe = get_bits(gb, 2);
606 num_assoc_data = get_bits(gb, 3);
607 num_cc = get_bits(gb, 4);
610 skip_bits(gb, 4); // mono_mixdown_tag
612 skip_bits(gb, 4); // stereo_mixdown_tag
615 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
617 if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
618 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
621 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
623 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
625 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
627 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
630 skip_bits_long(gb, 4 * num_assoc_data);
632 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
637 /* comment field, first byte is length */
638 comment_len = get_bits(gb, 8) * 8;
639 if (get_bits_left(gb) < comment_len) {
640 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
643 skip_bits_long(gb, comment_len);
648 * Decode GA "General Audio" specific configuration; reference: table 4.1.
650 * @param ac pointer to AACContext, may be null
651 * @param avctx pointer to AVCCodecContext, used for logging
653 * @return Returns error status. 0 - OK, !0 - error
655 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
657 MPEG4AudioConfig *m4ac,
660 int extension_flag, ret;
661 uint8_t layout_map[MAX_ELEM_ID*4][3];
664 if (get_bits1(gb)) { // frameLengthFlag
665 av_log_missing_feature(avctx, "960/120 MDCT window is", 1);
669 if (get_bits1(gb)) // dependsOnCoreCoder
670 skip_bits(gb, 14); // coreCoderDelay
671 extension_flag = get_bits1(gb);
673 if (m4ac->object_type == AOT_AAC_SCALABLE ||
674 m4ac->object_type == AOT_ER_AAC_SCALABLE)
675 skip_bits(gb, 3); // layerNr
677 if (channel_config == 0) {
678 skip_bits(gb, 4); // element_instance_tag
679 tags = decode_pce(avctx, m4ac, layout_map, gb);
683 if ((ret = set_default_channel_config(avctx, layout_map, &tags, channel_config)))
687 if (count_channels(layout_map, tags) > 1) {
689 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
692 if (ac && (ret = output_configure(ac, layout_map, tags,
693 channel_config, OC_GLOBAL_HDR)))
696 if (extension_flag) {
697 switch (m4ac->object_type) {
699 skip_bits(gb, 5); // numOfSubFrame
700 skip_bits(gb, 11); // layer_length
704 case AOT_ER_AAC_SCALABLE:
706 skip_bits(gb, 3); /* aacSectionDataResilienceFlag
707 * aacScalefactorDataResilienceFlag
708 * aacSpectralDataResilienceFlag
712 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
718 * Decode audio specific configuration; reference: table 1.13.
720 * @param ac pointer to AACContext, may be null
721 * @param avctx pointer to AVCCodecContext, used for logging
722 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
723 * @param data pointer to buffer holding an audio specific config
724 * @param bit_size size of audio specific config or data in bits
725 * @param sync_extension look for an appended sync extension
727 * @return Returns error status or number of consumed bits. <0 - error
729 static int decode_audio_specific_config(AACContext *ac,
730 AVCodecContext *avctx,
731 MPEG4AudioConfig *m4ac,
732 const uint8_t *data, int bit_size,
738 av_dlog(avctx, "audio specific config size %d\n", bit_size >> 3);
739 for (i = 0; i < bit_size >> 3; i++)
740 av_dlog(avctx, "%02x ", data[i]);
741 av_dlog(avctx, "\n");
743 init_get_bits(&gb, data, bit_size);
745 if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
747 if (m4ac->sampling_index > 12) {
748 av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
752 skip_bits_long(&gb, i);
754 switch (m4ac->object_type) {
758 if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
762 av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
763 m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
767 av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
768 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
769 m4ac->sample_rate, m4ac->sbr, m4ac->ps);
771 return get_bits_count(&gb);
775 * linear congruential pseudorandom number generator
777 * @param previous_val pointer to the current state of the generator
779 * @return Returns a 32-bit pseudorandom integer
781 static av_always_inline int lcg_random(int previous_val)
783 return previous_val * 1664525 + 1013904223;
786 static av_always_inline void reset_predict_state(PredictorState *ps)
796 static void reset_all_predictors(PredictorState *ps)
799 for (i = 0; i < MAX_PREDICTORS; i++)
800 reset_predict_state(&ps[i]);
803 static int sample_rate_idx (int rate)
805 if (92017 <= rate) return 0;
806 else if (75132 <= rate) return 1;
807 else if (55426 <= rate) return 2;
808 else if (46009 <= rate) return 3;
809 else if (37566 <= rate) return 4;
810 else if (27713 <= rate) return 5;
811 else if (23004 <= rate) return 6;
812 else if (18783 <= rate) return 7;
813 else if (13856 <= rate) return 8;
814 else if (11502 <= rate) return 9;
815 else if (9391 <= rate) return 10;
819 static void reset_predictor_group(PredictorState *ps, int group_num)
822 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
823 reset_predict_state(&ps[i]);
826 #define AAC_INIT_VLC_STATIC(num, size) \
827 INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
828 ff_aac_spectral_bits[num], sizeof( ff_aac_spectral_bits[num][0]), sizeof( ff_aac_spectral_bits[num][0]), \
829 ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), sizeof(ff_aac_spectral_codes[num][0]), \
832 static av_cold int aac_decode_init(AVCodecContext *avctx)
834 AACContext *ac = avctx->priv_data;
835 float output_scale_factor;
838 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
840 if (avctx->extradata_size > 0) {
841 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
843 avctx->extradata_size*8, 1) < 0)
847 uint8_t layout_map[MAX_ELEM_ID*4][3];
850 sr = sample_rate_idx(avctx->sample_rate);
851 ac->oc[1].m4ac.sampling_index = sr;
852 ac->oc[1].m4ac.channels = avctx->channels;
853 ac->oc[1].m4ac.sbr = -1;
854 ac->oc[1].m4ac.ps = -1;
856 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
857 if (ff_mpeg4audio_channels[i] == avctx->channels)
859 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
862 ac->oc[1].m4ac.chan_config = i;
864 if (ac->oc[1].m4ac.chan_config) {
865 int ret = set_default_channel_config(avctx, layout_map,
866 &layout_map_tags, ac->oc[1].m4ac.chan_config);
868 output_configure(ac, layout_map, layout_map_tags,
869 ac->oc[1].m4ac.chan_config, OC_GLOBAL_HDR);
870 else if (avctx->err_recognition & AV_EF_EXPLODE)
871 return AVERROR_INVALIDDATA;
875 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
876 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
877 output_scale_factor = 1.0 / 32768.0;
879 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
880 output_scale_factor = 1.0;
883 AAC_INIT_VLC_STATIC( 0, 304);
884 AAC_INIT_VLC_STATIC( 1, 270);
885 AAC_INIT_VLC_STATIC( 2, 550);
886 AAC_INIT_VLC_STATIC( 3, 300);
887 AAC_INIT_VLC_STATIC( 4, 328);
888 AAC_INIT_VLC_STATIC( 5, 294);
889 AAC_INIT_VLC_STATIC( 6, 306);
890 AAC_INIT_VLC_STATIC( 7, 268);
891 AAC_INIT_VLC_STATIC( 8, 510);
892 AAC_INIT_VLC_STATIC( 9, 366);
893 AAC_INIT_VLC_STATIC(10, 462);
897 ff_dsputil_init(&ac->dsp, avctx);
898 ff_fmt_convert_init(&ac->fmt_conv, avctx);
900 ac->random_state = 0x1f2e3d4c;
904 INIT_VLC_STATIC(&vlc_scalefactors,7,FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
905 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
906 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
909 ff_mdct_init(&ac->mdct, 11, 1, output_scale_factor/1024.0);
910 ff_mdct_init(&ac->mdct_small, 8, 1, output_scale_factor/128.0);
911 ff_mdct_init(&ac->mdct_ltp, 11, 0, -2.0/output_scale_factor);
912 // window initialization
913 ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
914 ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
915 ff_init_ff_sine_windows(10);
916 ff_init_ff_sine_windows( 7);
920 avcodec_get_frame_defaults(&ac->frame);
921 avctx->coded_frame = &ac->frame;
927 * Skip data_stream_element; reference: table 4.10.
929 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
931 int byte_align = get_bits1(gb);
932 int count = get_bits(gb, 8);
934 count += get_bits(gb, 8);
938 if (get_bits_left(gb) < 8 * count) {
939 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
942 skip_bits_long(gb, 8 * count);
946 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
951 ics->predictor_reset_group = get_bits(gb, 5);
952 if (ics->predictor_reset_group == 0 || ics->predictor_reset_group > 30) {
953 av_log(ac->avctx, AV_LOG_ERROR, "Invalid Predictor Reset Group.\n");
957 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
958 ics->prediction_used[sfb] = get_bits1(gb);
964 * Decode Long Term Prediction data; reference: table 4.xx.
966 static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
967 GetBitContext *gb, uint8_t max_sfb)
971 ltp->lag = get_bits(gb, 11);
972 ltp->coef = ltp_coef[get_bits(gb, 3)];
973 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
974 ltp->used[sfb] = get_bits1(gb);
978 * Decode Individual Channel Stream info; reference: table 4.6.
980 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
984 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
985 return AVERROR_INVALIDDATA;
987 ics->window_sequence[1] = ics->window_sequence[0];
988 ics->window_sequence[0] = get_bits(gb, 2);
989 ics->use_kb_window[1] = ics->use_kb_window[0];
990 ics->use_kb_window[0] = get_bits1(gb);
991 ics->num_window_groups = 1;
992 ics->group_len[0] = 1;
993 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
995 ics->max_sfb = get_bits(gb, 4);
996 for (i = 0; i < 7; i++) {
998 ics->group_len[ics->num_window_groups - 1]++;
1000 ics->num_window_groups++;
1001 ics->group_len[ics->num_window_groups - 1] = 1;
1004 ics->num_windows = 8;
1005 ics->swb_offset = ff_swb_offset_128[ac->oc[1].m4ac.sampling_index];
1006 ics->num_swb = ff_aac_num_swb_128[ac->oc[1].m4ac.sampling_index];
1007 ics->tns_max_bands = ff_tns_max_bands_128[ac->oc[1].m4ac.sampling_index];
1008 ics->predictor_present = 0;
1010 ics->max_sfb = get_bits(gb, 6);
1011 ics->num_windows = 1;
1012 ics->swb_offset = ff_swb_offset_1024[ac->oc[1].m4ac.sampling_index];
1013 ics->num_swb = ff_aac_num_swb_1024[ac->oc[1].m4ac.sampling_index];
1014 ics->tns_max_bands = ff_tns_max_bands_1024[ac->oc[1].m4ac.sampling_index];
1015 ics->predictor_present = get_bits1(gb);
1016 ics->predictor_reset_group = 0;
1017 if (ics->predictor_present) {
1018 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1019 if (decode_prediction(ac, ics, gb)) {
1022 } else if (ac->oc[1].m4ac.object_type == AOT_AAC_LC) {
1023 av_log(ac->avctx, AV_LOG_ERROR, "Prediction is not allowed in AAC-LC.\n");
1026 if ((ics->ltp.present = get_bits(gb, 1)))
1027 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1032 if (ics->max_sfb > ics->num_swb) {
1033 av_log(ac->avctx, AV_LOG_ERROR,
1034 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
1035 ics->max_sfb, ics->num_swb);
1042 return AVERROR_INVALIDDATA;
1046 * Decode band types (section_data payload); reference: table 4.46.
1048 * @param band_type array of the used band type
1049 * @param band_type_run_end array of the last scalefactor band of a band type run
1051 * @return Returns error status. 0 - OK, !0 - error
1053 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1054 int band_type_run_end[120], GetBitContext *gb,
1055 IndividualChannelStream *ics)
1058 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1059 for (g = 0; g < ics->num_window_groups; g++) {
1061 while (k < ics->max_sfb) {
1062 uint8_t sect_end = k;
1064 int sect_band_type = get_bits(gb, 4);
1065 if (sect_band_type == 12) {
1066 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1070 sect_len_incr = get_bits(gb, bits);
1071 sect_end += sect_len_incr;
1072 if (get_bits_left(gb) < 0) {
1073 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1076 if (sect_end > ics->max_sfb) {
1077 av_log(ac->avctx, AV_LOG_ERROR,
1078 "Number of bands (%d) exceeds limit (%d).\n",
1079 sect_end, ics->max_sfb);
1082 } while (sect_len_incr == (1 << bits) - 1);
1083 for (; k < sect_end; k++) {
1084 band_type [idx] = sect_band_type;
1085 band_type_run_end[idx++] = sect_end;
1093 * Decode scalefactors; reference: table 4.47.
1095 * @param global_gain first scalefactor value as scalefactors are differentially coded
1096 * @param band_type array of the used band type
1097 * @param band_type_run_end array of the last scalefactor band of a band type run
1098 * @param sf array of scalefactors or intensity stereo positions
1100 * @return Returns error status. 0 - OK, !0 - error
1102 static int decode_scalefactors(AACContext *ac, float sf[120], GetBitContext *gb,
1103 unsigned int global_gain,
1104 IndividualChannelStream *ics,
1105 enum BandType band_type[120],
1106 int band_type_run_end[120])
1109 int offset[3] = { global_gain, global_gain - 90, 0 };
1112 for (g = 0; g < ics->num_window_groups; g++) {
1113 for (i = 0; i < ics->max_sfb;) {
1114 int run_end = band_type_run_end[idx];
1115 if (band_type[idx] == ZERO_BT) {
1116 for (; i < run_end; i++, idx++)
1118 } else if ((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
1119 for (; i < run_end; i++, idx++) {
1120 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1121 clipped_offset = av_clip(offset[2], -155, 100);
1122 if (offset[2] != clipped_offset) {
1123 av_log_ask_for_sample(ac->avctx, "Intensity stereo "
1124 "position clipped (%d -> %d).\nIf you heard an "
1125 "audible artifact, there may be a bug in the "
1126 "decoder. ", offset[2], clipped_offset);
1128 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1130 } else if (band_type[idx] == NOISE_BT) {
1131 for (; i < run_end; i++, idx++) {
1132 if (noise_flag-- > 0)
1133 offset[1] += get_bits(gb, 9) - 256;
1135 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1136 clipped_offset = av_clip(offset[1], -100, 155);
1137 if (offset[1] != clipped_offset) {
1138 av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
1139 "(%d -> %d).\nIf you heard an audible "
1140 "artifact, there may be a bug in the decoder. ",
1141 offset[1], clipped_offset);
1143 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1146 for (; i < run_end; i++, idx++) {
1147 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1148 if (offset[0] > 255U) {
1149 av_log(ac->avctx, AV_LOG_ERROR,
1150 "Scalefactor (%d) out of range.\n", offset[0]);
1153 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1162 * Decode pulse data; reference: table 4.7.
1164 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1165 const uint16_t *swb_offset, int num_swb)
1168 pulse->num_pulse = get_bits(gb, 2) + 1;
1169 pulse_swb = get_bits(gb, 6);
1170 if (pulse_swb >= num_swb)
1172 pulse->pos[0] = swb_offset[pulse_swb];
1173 pulse->pos[0] += get_bits(gb, 5);
1174 if (pulse->pos[0] > 1023)
1176 pulse->amp[0] = get_bits(gb, 4);
1177 for (i = 1; i < pulse->num_pulse; i++) {
1178 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1179 if (pulse->pos[i] > 1023)
1181 pulse->amp[i] = get_bits(gb, 4);
1187 * Decode Temporal Noise Shaping data; reference: table 4.48.
1189 * @return Returns error status. 0 - OK, !0 - error
1191 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
1192 GetBitContext *gb, const IndividualChannelStream *ics)
1194 int w, filt, i, coef_len, coef_res, coef_compress;
1195 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1196 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1197 for (w = 0; w < ics->num_windows; w++) {
1198 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1199 coef_res = get_bits1(gb);
1201 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1203 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1205 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1206 av_log(ac->avctx, AV_LOG_ERROR, "TNS filter order %d is greater than maximum %d.\n",
1207 tns->order[w][filt], tns_max_order);
1208 tns->order[w][filt] = 0;
1211 if (tns->order[w][filt]) {
1212 tns->direction[w][filt] = get_bits1(gb);
1213 coef_compress = get_bits1(gb);
1214 coef_len = coef_res + 3 - coef_compress;
1215 tmp2_idx = 2 * coef_compress + coef_res;
1217 for (i = 0; i < tns->order[w][filt]; i++)
1218 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1227 * Decode Mid/Side data; reference: table 4.54.
1229 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1230 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1231 * [3] reserved for scalable AAC
1233 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1237 if (ms_present == 1) {
1238 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
1239 cpe->ms_mask[idx] = get_bits1(gb);
1240 } else if (ms_present == 2) {
1241 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
1246 static inline float *VMUL2(float *dst, const float *v, unsigned idx,
1250 *dst++ = v[idx & 15] * s;
1251 *dst++ = v[idx>>4 & 15] * s;
1257 static inline float *VMUL4(float *dst, const float *v, unsigned idx,
1261 *dst++ = v[idx & 3] * s;
1262 *dst++ = v[idx>>2 & 3] * s;
1263 *dst++ = v[idx>>4 & 3] * s;
1264 *dst++ = v[idx>>6 & 3] * s;
1270 static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
1271 unsigned sign, const float *scale)
1273 union av_intfloat32 s0, s1;
1275 s0.f = s1.f = *scale;
1276 s0.i ^= sign >> 1 << 31;
1279 *dst++ = v[idx & 15] * s0.f;
1280 *dst++ = v[idx>>4 & 15] * s1.f;
1287 static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
1288 unsigned sign, const float *scale)
1290 unsigned nz = idx >> 12;
1291 union av_intfloat32 s = { .f = *scale };
1292 union av_intfloat32 t;
1294 t.i = s.i ^ (sign & 1U<<31);
1295 *dst++ = v[idx & 3] * t.f;
1297 sign <<= nz & 1; nz >>= 1;
1298 t.i = s.i ^ (sign & 1U<<31);
1299 *dst++ = v[idx>>2 & 3] * t.f;
1301 sign <<= nz & 1; nz >>= 1;
1302 t.i = s.i ^ (sign & 1U<<31);
1303 *dst++ = v[idx>>4 & 3] * t.f;
1305 sign <<= nz & 1; nz >>= 1;
1306 t.i = s.i ^ (sign & 1U<<31);
1307 *dst++ = v[idx>>6 & 3] * t.f;
1314 * Decode spectral data; reference: table 4.50.
1315 * Dequantize and scale spectral data; reference: 4.6.3.3.
1317 * @param coef array of dequantized, scaled spectral data
1318 * @param sf array of scalefactors or intensity stereo positions
1319 * @param pulse_present set if pulses are present
1320 * @param pulse pointer to pulse data struct
1321 * @param band_type array of the used band type
1323 * @return Returns error status. 0 - OK, !0 - error
1325 static int decode_spectrum_and_dequant(AACContext *ac, float coef[1024],
1326 GetBitContext *gb, const float sf[120],
1327 int pulse_present, const Pulse *pulse,
1328 const IndividualChannelStream *ics,
1329 enum BandType band_type[120])
1331 int i, k, g, idx = 0;
1332 const int c = 1024 / ics->num_windows;
1333 const uint16_t *offsets = ics->swb_offset;
1334 float *coef_base = coef;
1336 for (g = 0; g < ics->num_windows; g++)
1337 memset(coef + g * 128 + offsets[ics->max_sfb], 0, sizeof(float) * (c - offsets[ics->max_sfb]));
1339 for (g = 0; g < ics->num_window_groups; g++) {
1340 unsigned g_len = ics->group_len[g];
1342 for (i = 0; i < ics->max_sfb; i++, idx++) {
1343 const unsigned cbt_m1 = band_type[idx] - 1;
1344 float *cfo = coef + offsets[i];
1345 int off_len = offsets[i + 1] - offsets[i];
1348 if (cbt_m1 >= INTENSITY_BT2 - 1) {
1349 for (group = 0; group < g_len; group++, cfo+=128) {
1350 memset(cfo, 0, off_len * sizeof(float));
1352 } else if (cbt_m1 == NOISE_BT - 1) {
1353 for (group = 0; group < g_len; group++, cfo+=128) {
1357 for (k = 0; k < off_len; k++) {
1358 ac->random_state = lcg_random(ac->random_state);
1359 cfo[k] = ac->random_state;
1362 band_energy = ac->dsp.scalarproduct_float(cfo, cfo, off_len);
1363 scale = sf[idx] / sqrtf(band_energy);
1364 ac->dsp.vector_fmul_scalar(cfo, cfo, scale, off_len);
1367 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1368 const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1369 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1370 OPEN_READER(re, gb);
1372 switch (cbt_m1 >> 1) {
1374 for (group = 0; group < g_len; group++, cfo+=128) {
1382 UPDATE_CACHE(re, gb);
1383 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1384 cb_idx = cb_vector_idx[code];
1385 cf = VMUL4(cf, vq, cb_idx, sf + idx);
1391 for (group = 0; group < g_len; group++, cfo+=128) {
1401 UPDATE_CACHE(re, gb);
1402 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1403 cb_idx = cb_vector_idx[code];
1404 nnz = cb_idx >> 8 & 15;
1405 bits = nnz ? GET_CACHE(re, gb) : 0;
1406 LAST_SKIP_BITS(re, gb, nnz);
1407 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1413 for (group = 0; group < g_len; group++, cfo+=128) {
1421 UPDATE_CACHE(re, gb);
1422 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1423 cb_idx = cb_vector_idx[code];
1424 cf = VMUL2(cf, vq, cb_idx, sf + idx);
1431 for (group = 0; group < g_len; group++, cfo+=128) {
1441 UPDATE_CACHE(re, gb);
1442 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1443 cb_idx = cb_vector_idx[code];
1444 nnz = cb_idx >> 8 & 15;
1445 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1446 LAST_SKIP_BITS(re, gb, nnz);
1447 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1453 for (group = 0; group < g_len; group++, cfo+=128) {
1455 uint32_t *icf = (uint32_t *) cf;
1465 UPDATE_CACHE(re, gb);
1466 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1474 cb_idx = cb_vector_idx[code];
1477 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1478 LAST_SKIP_BITS(re, gb, nnz);
1480 for (j = 0; j < 2; j++) {
1484 /* The total length of escape_sequence must be < 22 bits according
1485 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1486 UPDATE_CACHE(re, gb);
1487 b = GET_CACHE(re, gb);
1488 b = 31 - av_log2(~b);
1491 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1495 SKIP_BITS(re, gb, b + 1);
1497 n = (1 << b) + SHOW_UBITS(re, gb, b);
1498 LAST_SKIP_BITS(re, gb, b);
1499 *icf++ = cbrt_tab[n] | (bits & 1U<<31);
1502 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1503 *icf++ = (bits & 1U<<31) | v;
1510 ac->dsp.vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1514 CLOSE_READER(re, gb);
1520 if (pulse_present) {
1522 for (i = 0; i < pulse->num_pulse; i++) {
1523 float co = coef_base[ pulse->pos[i] ];
1524 while (offsets[idx + 1] <= pulse->pos[i])
1526 if (band_type[idx] != NOISE_BT && sf[idx]) {
1527 float ico = -pulse->amp[i];
1530 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1532 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1539 static av_always_inline float flt16_round(float pf)
1541 union av_intfloat32 tmp;
1543 tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
1547 static av_always_inline float flt16_even(float pf)
1549 union av_intfloat32 tmp;
1551 tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
1555 static av_always_inline float flt16_trunc(float pf)
1557 union av_intfloat32 pun;
1559 pun.i &= 0xFFFF0000U;
1563 static av_always_inline void predict(PredictorState *ps, float *coef,
1566 const float a = 0.953125; // 61.0 / 64
1567 const float alpha = 0.90625; // 29.0 / 32
1571 float r0 = ps->r0, r1 = ps->r1;
1572 float cor0 = ps->cor0, cor1 = ps->cor1;
1573 float var0 = ps->var0, var1 = ps->var1;
1575 k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
1576 k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
1578 pv = flt16_round(k1 * r0 + k2 * r1);
1585 ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
1586 ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
1587 ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
1588 ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
1590 ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
1591 ps->r0 = flt16_trunc(a * e0);
1595 * Apply AAC-Main style frequency domain prediction.
1597 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1601 if (!sce->ics.predictor_initialized) {
1602 reset_all_predictors(sce->predictor_state);
1603 sce->ics.predictor_initialized = 1;
1606 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1607 for (sfb = 0; sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]; sfb++) {
1608 for (k = sce->ics.swb_offset[sfb]; k < sce->ics.swb_offset[sfb + 1]; k++) {
1609 predict(&sce->predictor_state[k], &sce->coeffs[k],
1610 sce->ics.predictor_present && sce->ics.prediction_used[sfb]);
1613 if (sce->ics.predictor_reset_group)
1614 reset_predictor_group(sce->predictor_state, sce->ics.predictor_reset_group);
1616 reset_all_predictors(sce->predictor_state);
1620 * Decode an individual_channel_stream payload; reference: table 4.44.
1622 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1623 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1625 * @return Returns error status. 0 - OK, !0 - error
1627 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1628 GetBitContext *gb, int common_window, int scale_flag)
1631 TemporalNoiseShaping *tns = &sce->tns;
1632 IndividualChannelStream *ics = &sce->ics;
1633 float *out = sce->coeffs;
1634 int global_gain, pulse_present = 0;
1636 /* This assignment is to silence a GCC warning about the variable being used
1637 * uninitialized when in fact it always is.
1639 pulse.num_pulse = 0;
1641 global_gain = get_bits(gb, 8);
1643 if (!common_window && !scale_flag) {
1644 if (decode_ics_info(ac, ics, gb) < 0)
1645 return AVERROR_INVALIDDATA;
1648 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
1650 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
1655 if ((pulse_present = get_bits1(gb))) {
1656 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1657 av_log(ac->avctx, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
1660 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1661 av_log(ac->avctx, AV_LOG_ERROR, "Pulse data corrupt or invalid.\n");
1665 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
1667 if (get_bits1(gb)) {
1668 av_log_missing_feature(ac->avctx, "SSR", 1);
1673 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
1676 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1677 apply_prediction(ac, sce);
1683 * Mid/Side stereo decoding; reference: 4.6.8.1.3.
1685 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
1687 const IndividualChannelStream *ics = &cpe->ch[0].ics;
1688 float *ch0 = cpe->ch[0].coeffs;
1689 float *ch1 = cpe->ch[1].coeffs;
1690 int g, i, group, idx = 0;
1691 const uint16_t *offsets = ics->swb_offset;
1692 for (g = 0; g < ics->num_window_groups; g++) {
1693 for (i = 0; i < ics->max_sfb; i++, idx++) {
1694 if (cpe->ms_mask[idx] &&
1695 cpe->ch[0].band_type[idx] < NOISE_BT && cpe->ch[1].band_type[idx] < NOISE_BT) {
1696 for (group = 0; group < ics->group_len[g]; group++) {
1697 ac->dsp.butterflies_float(ch0 + group * 128 + offsets[i],
1698 ch1 + group * 128 + offsets[i],
1699 offsets[i+1] - offsets[i]);
1703 ch0 += ics->group_len[g] * 128;
1704 ch1 += ics->group_len[g] * 128;
1709 * intensity stereo decoding; reference: 4.6.8.2.3
1711 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1712 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1713 * [3] reserved for scalable AAC
1715 static void apply_intensity_stereo(AACContext *ac, ChannelElement *cpe, int ms_present)
1717 const IndividualChannelStream *ics = &cpe->ch[1].ics;
1718 SingleChannelElement *sce1 = &cpe->ch[1];
1719 float *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
1720 const uint16_t *offsets = ics->swb_offset;
1721 int g, group, i, idx = 0;
1724 for (g = 0; g < ics->num_window_groups; g++) {
1725 for (i = 0; i < ics->max_sfb;) {
1726 if (sce1->band_type[idx] == INTENSITY_BT || sce1->band_type[idx] == INTENSITY_BT2) {
1727 const int bt_run_end = sce1->band_type_run_end[idx];
1728 for (; i < bt_run_end; i++, idx++) {
1729 c = -1 + 2 * (sce1->band_type[idx] - 14);
1731 c *= 1 - 2 * cpe->ms_mask[idx];
1732 scale = c * sce1->sf[idx];
1733 for (group = 0; group < ics->group_len[g]; group++)
1734 ac->dsp.vector_fmul_scalar(coef1 + group * 128 + offsets[i],
1735 coef0 + group * 128 + offsets[i],
1737 offsets[i + 1] - offsets[i]);
1740 int bt_run_end = sce1->band_type_run_end[idx];
1741 idx += bt_run_end - i;
1745 coef0 += ics->group_len[g] * 128;
1746 coef1 += ics->group_len[g] * 128;
1751 * Decode a channel_pair_element; reference: table 4.4.
1753 * @return Returns error status. 0 - OK, !0 - error
1755 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
1757 int i, ret, common_window, ms_present = 0;
1759 common_window = get_bits1(gb);
1760 if (common_window) {
1761 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1762 return AVERROR_INVALIDDATA;
1763 i = cpe->ch[1].ics.use_kb_window[0];
1764 cpe->ch[1].ics = cpe->ch[0].ics;
1765 cpe->ch[1].ics.use_kb_window[1] = i;
1766 if (cpe->ch[1].ics.predictor_present && (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1767 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1768 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1769 ms_present = get_bits(gb, 2);
1770 if (ms_present == 3) {
1771 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1773 } else if (ms_present)
1774 decode_mid_side_stereo(cpe, gb, ms_present);
1776 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1778 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1781 if (common_window) {
1783 apply_mid_side_stereo(ac, cpe);
1784 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1785 apply_prediction(ac, &cpe->ch[0]);
1786 apply_prediction(ac, &cpe->ch[1]);
1790 apply_intensity_stereo(ac, cpe, ms_present);
1794 static const float cce_scale[] = {
1795 1.09050773266525765921, //2^(1/8)
1796 1.18920711500272106672, //2^(1/4)
1802 * Decode coupling_channel_element; reference: table 4.8.
1804 * @return Returns error status. 0 - OK, !0 - error
1806 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
1812 SingleChannelElement *sce = &che->ch[0];
1813 ChannelCoupling *coup = &che->coup;
1815 coup->coupling_point = 2 * get_bits1(gb);
1816 coup->num_coupled = get_bits(gb, 3);
1817 for (c = 0; c <= coup->num_coupled; c++) {
1819 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
1820 coup->id_select[c] = get_bits(gb, 4);
1821 if (coup->type[c] == TYPE_CPE) {
1822 coup->ch_select[c] = get_bits(gb, 2);
1823 if (coup->ch_select[c] == 3)
1826 coup->ch_select[c] = 2;
1828 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
1830 sign = get_bits(gb, 1);
1831 scale = cce_scale[get_bits(gb, 2)];
1833 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
1836 for (c = 0; c < num_gain; c++) {
1840 float gain_cache = 1.;
1842 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
1843 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
1844 gain_cache = powf(scale, -gain);
1846 if (coup->coupling_point == AFTER_IMDCT) {
1847 coup->gain[c][0] = gain_cache;
1849 for (g = 0; g < sce->ics.num_window_groups; g++) {
1850 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
1851 if (sce->band_type[idx] != ZERO_BT) {
1853 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
1861 gain_cache = powf(scale, -t) * s;
1864 coup->gain[c][idx] = gain_cache;
1874 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1876 * @return Returns number of bytes consumed.
1878 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1882 int num_excl_chan = 0;
1885 for (i = 0; i < 7; i++)
1886 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1887 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1889 return num_excl_chan / 7;
1893 * Decode dynamic range information; reference: table 4.52.
1895 * @param cnt length of TYPE_FIL syntactic element in bytes
1897 * @return Returns number of bytes consumed.
1899 static int decode_dynamic_range(DynamicRangeControl *che_drc,
1900 GetBitContext *gb, int cnt)
1903 int drc_num_bands = 1;
1906 /* pce_tag_present? */
1907 if (get_bits1(gb)) {
1908 che_drc->pce_instance_tag = get_bits(gb, 4);
1909 skip_bits(gb, 4); // tag_reserved_bits
1913 /* excluded_chns_present? */
1914 if (get_bits1(gb)) {
1915 n += decode_drc_channel_exclusions(che_drc, gb);
1918 /* drc_bands_present? */
1919 if (get_bits1(gb)) {
1920 che_drc->band_incr = get_bits(gb, 4);
1921 che_drc->interpolation_scheme = get_bits(gb, 4);
1923 drc_num_bands += che_drc->band_incr;
1924 for (i = 0; i < drc_num_bands; i++) {
1925 che_drc->band_top[i] = get_bits(gb, 8);
1930 /* prog_ref_level_present? */
1931 if (get_bits1(gb)) {
1932 che_drc->prog_ref_level = get_bits(gb, 7);
1933 skip_bits1(gb); // prog_ref_level_reserved_bits
1937 for (i = 0; i < drc_num_bands; i++) {
1938 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1939 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1947 * Decode extension data (incomplete); reference: table 4.51.
1949 * @param cnt length of TYPE_FIL syntactic element in bytes
1951 * @return Returns number of bytes consumed
1953 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
1954 ChannelElement *che, enum RawDataBlockType elem_type)
1958 switch (get_bits(gb, 4)) { // extension type
1959 case EXT_SBR_DATA_CRC:
1963 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1965 } else if (!ac->oc[1].m4ac.sbr) {
1966 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1967 skip_bits_long(gb, 8 * cnt - 4);
1969 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
1970 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1971 skip_bits_long(gb, 8 * cnt - 4);
1973 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
1974 ac->oc[1].m4ac.sbr = 1;
1975 ac->oc[1].m4ac.ps = 1;
1976 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
1977 ac->oc[1].m4ac.chan_config, ac->oc[1].status);
1979 ac->oc[1].m4ac.sbr = 1;
1981 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
1983 case EXT_DYNAMIC_RANGE:
1984 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
1988 case EXT_DATA_ELEMENT:
1990 skip_bits_long(gb, 8 * cnt - 4);
1997 * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
1999 * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2000 * @param coef spectral coefficients
2002 static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
2003 IndividualChannelStream *ics, int decode)
2005 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2007 int bottom, top, order, start, end, size, inc;
2008 float lpc[TNS_MAX_ORDER];
2009 float tmp[TNS_MAX_ORDER];
2011 for (w = 0; w < ics->num_windows; w++) {
2012 bottom = ics->num_swb;
2013 for (filt = 0; filt < tns->n_filt[w]; filt++) {
2015 bottom = FFMAX(0, top - tns->length[w][filt]);
2016 order = tns->order[w][filt];
2021 compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
2023 start = ics->swb_offset[FFMIN(bottom, mmm)];
2024 end = ics->swb_offset[FFMIN( top, mmm)];
2025 if ((size = end - start) <= 0)
2027 if (tns->direction[w][filt]) {
2037 for (m = 0; m < size; m++, start += inc)
2038 for (i = 1; i <= FFMIN(m, order); i++)
2039 coef[start] -= coef[start - i * inc] * lpc[i - 1];
2042 for (m = 0; m < size; m++, start += inc) {
2043 tmp[0] = coef[start];
2044 for (i = 1; i <= FFMIN(m, order); i++)
2045 coef[start] += tmp[i] * lpc[i - 1];
2046 for (i = order; i > 0; i--)
2047 tmp[i] = tmp[i - 1];
2055 * Apply windowing and MDCT to obtain the spectral
2056 * coefficient from the predicted sample by LTP.
2058 static void windowing_and_mdct_ltp(AACContext *ac, float *out,
2059 float *in, IndividualChannelStream *ics)
2061 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2062 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2063 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2064 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2066 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2067 ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
2069 memset(in, 0, 448 * sizeof(float));
2070 ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
2072 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2073 ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2075 ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2076 memset(in + 1024 + 576, 0, 448 * sizeof(float));
2078 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2082 * Apply the long term prediction
2084 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
2086 const LongTermPrediction *ltp = &sce->ics.ltp;
2087 const uint16_t *offsets = sce->ics.swb_offset;
2090 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2091 float *predTime = sce->ret;
2092 float *predFreq = ac->buf_mdct;
2093 int16_t num_samples = 2048;
2095 if (ltp->lag < 1024)
2096 num_samples = ltp->lag + 1024;
2097 for (i = 0; i < num_samples; i++)
2098 predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
2099 memset(&predTime[i], 0, (2048 - i) * sizeof(float));
2101 windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2103 if (sce->tns.present)
2104 apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2106 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2108 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2109 sce->coeffs[i] += predFreq[i];
2114 * Update the LTP buffer for next frame
2116 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
2118 IndividualChannelStream *ics = &sce->ics;
2119 float *saved = sce->saved;
2120 float *saved_ltp = sce->coeffs;
2121 const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2122 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2125 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2126 memcpy(saved_ltp, saved, 512 * sizeof(float));
2127 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2128 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2129 for (i = 0; i < 64; i++)
2130 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2131 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2132 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(float));
2133 memset(saved_ltp + 576, 0, 448 * sizeof(float));
2134 ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2135 for (i = 0; i < 64; i++)
2136 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * swindow[63 - i];
2137 } else { // LONG_STOP or ONLY_LONG
2138 ac->dsp.vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2139 for (i = 0; i < 512; i++)
2140 saved_ltp[i + 512] = ac->buf_mdct[1023 - i] * lwindow[511 - i];
2143 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2144 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2145 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2149 * Conduct IMDCT and windowing.
2151 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
2153 IndividualChannelStream *ics = &sce->ics;
2154 float *in = sce->coeffs;
2155 float *out = sce->ret;
2156 float *saved = sce->saved;
2157 const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
2158 const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
2159 const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
2160 float *buf = ac->buf_mdct;
2161 float *temp = ac->temp;
2165 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2166 for (i = 0; i < 1024; i += 128)
2167 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2169 ac->mdct.imdct_half(&ac->mdct, buf, in);
2171 /* window overlapping
2172 * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2173 * and long to short transitions are considered to be short to short
2174 * transitions. This leaves just two cases (long to long and short to short)
2175 * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2177 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2178 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2179 ac->dsp.vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2181 memcpy( out, saved, 448 * sizeof(float));
2183 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2184 ac->dsp.vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2185 ac->dsp.vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2186 ac->dsp.vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2187 ac->dsp.vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2188 ac->dsp.vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2189 memcpy( out + 448 + 4*128, temp, 64 * sizeof(float));
2191 ac->dsp.vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2192 memcpy( out + 576, buf + 64, 448 * sizeof(float));
2197 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2198 memcpy( saved, temp + 64, 64 * sizeof(float));
2199 ac->dsp.vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2200 ac->dsp.vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2201 ac->dsp.vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2202 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2203 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2204 memcpy( saved, buf + 512, 448 * sizeof(float));
2205 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(float));
2206 } else { // LONG_STOP or ONLY_LONG
2207 memcpy( saved, buf + 512, 512 * sizeof(float));
2212 * Apply dependent channel coupling (applied before IMDCT).
2214 * @param index index into coupling gain array
2216 static void apply_dependent_coupling(AACContext *ac,
2217 SingleChannelElement *target,
2218 ChannelElement *cce, int index)
2220 IndividualChannelStream *ics = &cce->ch[0].ics;
2221 const uint16_t *offsets = ics->swb_offset;
2222 float *dest = target->coeffs;
2223 const float *src = cce->ch[0].coeffs;
2224 int g, i, group, k, idx = 0;
2225 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2226 av_log(ac->avctx, AV_LOG_ERROR,
2227 "Dependent coupling is not supported together with LTP\n");
2230 for (g = 0; g < ics->num_window_groups; g++) {
2231 for (i = 0; i < ics->max_sfb; i++, idx++) {
2232 if (cce->ch[0].band_type[idx] != ZERO_BT) {
2233 const float gain = cce->coup.gain[index][idx];
2234 for (group = 0; group < ics->group_len[g]; group++) {
2235 for (k = offsets[i]; k < offsets[i + 1]; k++) {
2237 dest[group * 128 + k] += gain * src[group * 128 + k];
2242 dest += ics->group_len[g] * 128;
2243 src += ics->group_len[g] * 128;
2248 * Apply independent channel coupling (applied after IMDCT).
2250 * @param index index into coupling gain array
2252 static void apply_independent_coupling(AACContext *ac,
2253 SingleChannelElement *target,
2254 ChannelElement *cce, int index)
2257 const float gain = cce->coup.gain[index][0];
2258 const float *src = cce->ch[0].ret;
2259 float *dest = target->ret;
2260 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
2262 for (i = 0; i < len; i++)
2263 dest[i] += gain * src[i];
2267 * channel coupling transformation interface
2269 * @param apply_coupling_method pointer to (in)dependent coupling function
2271 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
2272 enum RawDataBlockType type, int elem_id,
2273 enum CouplingPoint coupling_point,
2274 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2278 for (i = 0; i < MAX_ELEM_ID; i++) {
2279 ChannelElement *cce = ac->che[TYPE_CCE][i];
2282 if (cce && cce->coup.coupling_point == coupling_point) {
2283 ChannelCoupling *coup = &cce->coup;
2285 for (c = 0; c <= coup->num_coupled; c++) {
2286 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2287 if (coup->ch_select[c] != 1) {
2288 apply_coupling_method(ac, &cc->ch[0], cce, index);
2289 if (coup->ch_select[c] != 0)
2292 if (coup->ch_select[c] != 2)
2293 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2295 index += 1 + (coup->ch_select[c] == 3);
2302 * Convert spectral data to float samples, applying all supported tools as appropriate.
2304 static void spectral_to_sample(AACContext *ac)
2307 for (type = 3; type >= 0; type--) {
2308 for (i = 0; i < MAX_ELEM_ID; i++) {
2309 ChannelElement *che = ac->che[type][i];
2311 if (type <= TYPE_CPE)
2312 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
2313 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2314 if (che->ch[0].ics.predictor_present) {
2315 if (che->ch[0].ics.ltp.present)
2316 apply_ltp(ac, &che->ch[0]);
2317 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2318 apply_ltp(ac, &che->ch[1]);
2321 if (che->ch[0].tns.present)
2322 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2323 if (che->ch[1].tns.present)
2324 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2325 if (type <= TYPE_CPE)
2326 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
2327 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2328 imdct_and_windowing(ac, &che->ch[0]);
2329 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2330 update_ltp(ac, &che->ch[0]);
2331 if (type == TYPE_CPE) {
2332 imdct_and_windowing(ac, &che->ch[1]);
2333 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2334 update_ltp(ac, &che->ch[1]);
2336 if (ac->oc[1].m4ac.sbr > 0) {
2337 ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2340 if (type <= TYPE_CCE)
2341 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
2347 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
2350 AACADTSHeaderInfo hdr_info;
2351 uint8_t layout_map[MAX_ELEM_ID*4][3];
2352 int layout_map_tags;
2354 size = avpriv_aac_parse_header(gb, &hdr_info);
2356 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2357 // This is 2 for "VLB " audio in NSV files.
2358 // See samples/nsv/vlb_audio.
2359 av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame is", 0);
2360 ac->warned_num_aac_frames = 1;
2362 push_output_configuration(ac);
2363 if (hdr_info.chan_config) {
2364 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2365 if (set_default_channel_config(ac->avctx, layout_map,
2366 &layout_map_tags, hdr_info.chan_config))
2368 if (output_configure(ac, layout_map, layout_map_tags,
2369 hdr_info.chan_config,
2370 FFMAX(ac->oc[1].status, OC_TRIAL_FRAME)))
2373 ac->oc[1].m4ac.chan_config = 0;
2375 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2376 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2377 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2378 if (ac->oc[0].status != OC_LOCKED ||
2379 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2380 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2381 ac->oc[1].m4ac.sbr = -1;
2382 ac->oc[1].m4ac.ps = -1;
2384 if (!hdr_info.crc_absent)
2390 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2391 int *got_frame_ptr, GetBitContext *gb)
2393 AACContext *ac = avctx->priv_data;
2394 ChannelElement *che = NULL, *che_prev = NULL;
2395 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2397 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2399 if (show_bits(gb, 12) == 0xfff) {
2400 if (parse_adts_frame_header(ac, gb) < 0) {
2401 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2405 if (ac->oc[1].m4ac.sampling_index > 12) {
2406 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2412 ac->tags_mapped = 0;
2414 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2415 elem_id = get_bits(gb, 4);
2417 if (elem_type < TYPE_DSE) {
2418 if (!(che=get_che(ac, elem_type, elem_id))) {
2419 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2420 elem_type, elem_id);
2427 switch (elem_type) {
2430 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2435 err = decode_cpe(ac, gb, che);
2440 err = decode_cce(ac, gb, che);
2444 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2449 err = skip_data_stream_element(ac, gb);
2453 uint8_t layout_map[MAX_ELEM_ID*4][3];
2455 push_output_configuration(ac);
2456 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
2462 av_log(avctx, AV_LOG_ERROR,
2463 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2464 pop_output_configuration(ac);
2466 err = output_configure(ac, layout_map, tags, 0, OC_TRIAL_PCE);
2468 ac->oc[1].m4ac.chan_config = 0;
2476 elem_id += get_bits(gb, 8) - 1;
2477 if (get_bits_left(gb) < 8 * elem_id) {
2478 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2483 elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
2484 err = 0; /* FIXME */
2488 err = -1; /* should not happen, but keeps compiler happy */
2493 elem_type_prev = elem_type;
2498 if (get_bits_left(gb) < 3) {
2499 av_log(avctx, AV_LOG_ERROR, overread_err);
2505 spectral_to_sample(ac);
2507 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2508 samples <<= multiplier;
2511 /* get output buffer */
2512 ac->frame.nb_samples = samples;
2513 if ((err = avctx->get_buffer(avctx, &ac->frame)) < 0) {
2514 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2519 if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
2520 ac->fmt_conv.float_interleave((float *)ac->frame.data[0],
2521 (const float **)ac->output_data,
2522 samples, avctx->channels);
2524 ac->fmt_conv.float_to_int16_interleave((int16_t *)ac->frame.data[0],
2525 (const float **)ac->output_data,
2526 samples, avctx->channels);
2528 *(AVFrame *)data = ac->frame;
2530 *got_frame_ptr = !!samples;
2532 if (ac->oc[1].status && audio_found) {
2533 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2534 avctx->frame_size = samples;
2535 ac->oc[1].status = OC_LOCKED;
2540 pop_output_configuration(ac);
2544 static int aac_decode_frame(AVCodecContext *avctx, void *data,
2545 int *got_frame_ptr, AVPacket *avpkt)
2547 AACContext *ac = avctx->priv_data;
2548 const uint8_t *buf = avpkt->data;
2549 int buf_size = avpkt->size;
2554 int new_extradata_size;
2555 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2556 AV_PKT_DATA_NEW_EXTRADATA,
2557 &new_extradata_size);
2559 if (new_extradata && 0) {
2560 av_free(avctx->extradata);
2561 avctx->extradata = av_mallocz(new_extradata_size +
2562 FF_INPUT_BUFFER_PADDING_SIZE);
2563 if (!avctx->extradata)
2564 return AVERROR(ENOMEM);
2565 avctx->extradata_size = new_extradata_size;
2566 memcpy(avctx->extradata, new_extradata, new_extradata_size);
2567 push_output_configuration(ac);
2568 if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
2570 avctx->extradata_size*8, 1) < 0) {
2571 pop_output_configuration(ac);
2572 return AVERROR_INVALIDDATA;
2576 init_get_bits(&gb, buf, buf_size * 8);
2578 if ((err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb)) < 0)
2581 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2582 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2583 if (buf[buf_offset])
2586 return buf_size > buf_offset ? buf_consumed : buf_size;
2589 static av_cold int aac_decode_close(AVCodecContext *avctx)
2591 AACContext *ac = avctx->priv_data;
2594 for (i = 0; i < MAX_ELEM_ID; i++) {
2595 for (type = 0; type < 4; type++) {
2596 if (ac->che[type][i])
2597 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2598 av_freep(&ac->che[type][i]);
2602 ff_mdct_end(&ac->mdct);
2603 ff_mdct_end(&ac->mdct_small);
2604 ff_mdct_end(&ac->mdct_ltp);
2609 #define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
2611 struct LATMContext {
2612 AACContext aac_ctx; ///< containing AACContext
2613 int initialized; ///< initilized after a valid extradata was seen
2616 int audio_mux_version_A; ///< LATM syntax version
2617 int frame_length_type; ///< 0/1 variable/fixed frame length
2618 int frame_length; ///< frame length for fixed frame length
2621 static inline uint32_t latm_get_value(GetBitContext *b)
2623 int length = get_bits(b, 2);
2625 return get_bits_long(b, (length+1)*8);
2628 static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
2629 GetBitContext *gb, int asclen)
2631 AACContext *ac = &latmctx->aac_ctx;
2632 AVCodecContext *avctx = ac->avctx;
2633 MPEG4AudioConfig m4ac = { 0 };
2634 int config_start_bit = get_bits_count(gb);
2635 int sync_extension = 0;
2636 int bits_consumed, esize;
2640 asclen = FFMIN(asclen, get_bits_left(gb));
2642 asclen = get_bits_left(gb);
2644 if (config_start_bit % 8) {
2645 av_log_missing_feature(latmctx->aac_ctx.avctx, "audio specific "
2646 "config not byte aligned.\n", 1);
2647 return AVERROR_INVALIDDATA;
2650 return AVERROR_INVALIDDATA;
2651 bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
2652 gb->buffer + (config_start_bit / 8),
2653 asclen, sync_extension);
2655 if (bits_consumed < 0)
2656 return AVERROR_INVALIDDATA;
2658 if (ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
2659 ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
2661 av_log(avctx, AV_LOG_INFO, "audio config changed\n");
2662 latmctx->initialized = 0;
2664 esize = (bits_consumed+7) / 8;
2666 if (avctx->extradata_size < esize) {
2667 av_free(avctx->extradata);
2668 avctx->extradata = av_malloc(esize + FF_INPUT_BUFFER_PADDING_SIZE);
2669 if (!avctx->extradata)
2670 return AVERROR(ENOMEM);
2673 avctx->extradata_size = esize;
2674 memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
2675 memset(avctx->extradata+esize, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2677 skip_bits_long(gb, bits_consumed);
2679 return bits_consumed;
2682 static int read_stream_mux_config(struct LATMContext *latmctx,
2685 int ret, audio_mux_version = get_bits(gb, 1);
2687 latmctx->audio_mux_version_A = 0;
2688 if (audio_mux_version)
2689 latmctx->audio_mux_version_A = get_bits(gb, 1);
2691 if (!latmctx->audio_mux_version_A) {
2693 if (audio_mux_version)
2694 latm_get_value(gb); // taraFullness
2696 skip_bits(gb, 1); // allStreamSameTimeFraming
2697 skip_bits(gb, 6); // numSubFrames
2699 if (get_bits(gb, 4)) { // numPrograms
2700 av_log_missing_feature(latmctx->aac_ctx.avctx,
2701 "multiple programs are not supported\n", 1);
2702 return AVERROR_PATCHWELCOME;
2705 // for each program (which there is only on in DVB)
2707 // for each layer (which there is only on in DVB)
2708 if (get_bits(gb, 3)) { // numLayer
2709 av_log_missing_feature(latmctx->aac_ctx.avctx,
2710 "multiple layers are not supported\n", 1);
2711 return AVERROR_PATCHWELCOME;
2714 // for all but first stream: use_same_config = get_bits(gb, 1);
2715 if (!audio_mux_version) {
2716 if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
2719 int ascLen = latm_get_value(gb);
2720 if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
2723 skip_bits_long(gb, ascLen);
2726 latmctx->frame_length_type = get_bits(gb, 3);
2727 switch (latmctx->frame_length_type) {
2729 skip_bits(gb, 8); // latmBufferFullness
2732 latmctx->frame_length = get_bits(gb, 9);
2737 skip_bits(gb, 6); // CELP frame length table index
2741 skip_bits(gb, 1); // HVXC frame length table index
2745 if (get_bits(gb, 1)) { // other data
2746 if (audio_mux_version) {
2747 latm_get_value(gb); // other_data_bits
2751 esc = get_bits(gb, 1);
2757 if (get_bits(gb, 1)) // crc present
2758 skip_bits(gb, 8); // config_crc
2764 static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
2768 if (ctx->frame_length_type == 0) {
2769 int mux_slot_length = 0;
2771 tmp = get_bits(gb, 8);
2772 mux_slot_length += tmp;
2773 } while (tmp == 255);
2774 return mux_slot_length;
2775 } else if (ctx->frame_length_type == 1) {
2776 return ctx->frame_length;
2777 } else if (ctx->frame_length_type == 3 ||
2778 ctx->frame_length_type == 5 ||
2779 ctx->frame_length_type == 7) {
2780 skip_bits(gb, 2); // mux_slot_length_coded
2785 static int read_audio_mux_element(struct LATMContext *latmctx,
2789 uint8_t use_same_mux = get_bits(gb, 1);
2790 if (!use_same_mux) {
2791 if ((err = read_stream_mux_config(latmctx, gb)) < 0)
2793 } else if (!latmctx->aac_ctx.avctx->extradata) {
2794 av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
2795 "no decoder config found\n");
2796 return AVERROR(EAGAIN);
2798 if (latmctx->audio_mux_version_A == 0) {
2799 int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
2800 if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
2801 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
2802 return AVERROR_INVALIDDATA;
2803 } else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
2804 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2805 "frame length mismatch %d << %d\n",
2806 mux_slot_length_bytes * 8, get_bits_left(gb));
2807 return AVERROR_INVALIDDATA;
2814 static int latm_decode_frame(AVCodecContext *avctx, void *out,
2815 int *got_frame_ptr, AVPacket *avpkt)
2817 struct LATMContext *latmctx = avctx->priv_data;
2821 init_get_bits(&gb, avpkt->data, avpkt->size * 8);
2823 // check for LOAS sync word
2824 if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
2825 return AVERROR_INVALIDDATA;
2827 muxlength = get_bits(&gb, 13) + 3;
2828 // not enough data, the parser should have sorted this
2829 if (muxlength > avpkt->size)
2830 return AVERROR_INVALIDDATA;
2832 if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
2835 if (!latmctx->initialized) {
2836 if (!avctx->extradata) {
2840 push_output_configuration(&latmctx->aac_ctx);
2841 if ((err = decode_audio_specific_config(
2842 &latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
2843 avctx->extradata, avctx->extradata_size*8, 1)) < 0) {
2844 pop_output_configuration(&latmctx->aac_ctx);
2847 latmctx->initialized = 1;
2851 if (show_bits(&gb, 12) == 0xfff) {
2852 av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
2853 "ADTS header detected, probably as result of configuration "
2855 return AVERROR_INVALIDDATA;
2858 if ((err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb)) < 0)
2864 static av_cold int latm_decode_init(AVCodecContext *avctx)
2866 struct LATMContext *latmctx = avctx->priv_data;
2867 int ret = aac_decode_init(avctx);
2869 if (avctx->extradata_size > 0)
2870 latmctx->initialized = !ret;
2876 AVCodec ff_aac_decoder = {
2878 .type = AVMEDIA_TYPE_AUDIO,
2880 .priv_data_size = sizeof(AACContext),
2881 .init = aac_decode_init,
2882 .close = aac_decode_close,
2883 .decode = aac_decode_frame,
2884 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
2885 .sample_fmts = (const enum AVSampleFormat[]) {
2886 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
2888 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2889 .channel_layouts = aac_channel_layout,
2894 Note: This decoder filter is intended to decode LATM streams transferred
2895 in MPEG transport streams which only contain one program.
2896 To do a more complex LATM demuxing a separate LATM demuxer should be used.
2898 AVCodec ff_aac_latm_decoder = {
2900 .type = AVMEDIA_TYPE_AUDIO,
2901 .id = CODEC_ID_AAC_LATM,
2902 .priv_data_size = sizeof(struct LATMContext),
2903 .init = latm_decode_init,
2904 .close = aac_decode_close,
2905 .decode = latm_decode_frame,
2906 .long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Codec LATM syntax)"),
2907 .sample_fmts = (const enum AVSampleFormat[]) {
2908 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
2910 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2911 .channel_layouts = aac_channel_layout,