FFmpeg
Loading...
Searching...
No Matches
aacdec.c
Go to the documentation of this file.
1/*
2 * Common parts of the AAC decoders
3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5 * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
6 *
7 * AAC LATM decoder
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10 *
11 * AAC decoder fixed-point implementation
12 * Copyright (c) 2013
13 * MIPS Technologies, Inc., California.
14 *
15 * This file is part of FFmpeg.
16 *
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32/* We use several quantization functions here (Q31, Q30),
33 * for which we need this to be defined for them to work as expected. */
34#define USE_FIXED 1
35
36#include "config_components.h"
37
38#include <limits.h>
39#include <stddef.h>
40
41#include "aacdec.h"
42#include "aacdec_tab.h"
43#include "aacdec_usac.h"
44
45#include "libavcodec/aac.h"
47#include "libavcodec/aacsbr.h"
48#include "libavcodec/aactab.h"
50
51#include "libavcodec/avcodec.h"
52#include "libavcodec/internal.h"
54#include "libavcodec/decode.h"
55#include "libavcodec/profiles.h"
56
58#include "libavutil/error.h"
59#include "libavutil/log.h"
60#include "libavutil/macros.h"
61#include "libavutil/mem.h"
62#include "libavutil/opt.h"
63#include "libavutil/tx.h"
64#include "libavutil/version.h"
65#include "libavutil/refstruct.h"
66
67/*
68 * supported tools
69 *
70 * Support? Name
71 * N (code in SoC repo) gain control
72 * Y block switching
73 * Y window shapes - standard
74 * N window shapes - Low Delay
75 * Y filterbank - standard
76 * N (code in SoC repo) filterbank - Scalable Sample Rate
77 * Y Temporal Noise Shaping
78 * Y Long Term Prediction
79 * Y intensity stereo
80 * Y channel coupling
81 * Y frequency domain prediction
82 * Y Perceptual Noise Substitution
83 * Y Mid/Side stereo
84 * N Scalable Inverse AAC Quantization
85 * N Frequency Selective Switch
86 * N upsampling filter
87 * Y quantization & coding - AAC
88 * N quantization & coding - TwinVQ
89 * N quantization & coding - BSAC
90 * N AAC Error Resilience tools
91 * N Error Resilience payload syntax
92 * N Error Protection tool
93 * N CELP
94 * N Silence Compression
95 * N HVXC
96 * N HVXC 4kbits/s VR
97 * N Structured Audio tools
98 * N Structured Audio Sample Bank Format
99 * N MIDI
100 * N Harmonic and Individual Lines plus Noise
101 * N Text-To-Speech Interface
102 * Y Spectral Band Replication
103 * Y (not in this code) Layer-1
104 * Y (not in this code) Layer-2
105 * Y (not in this code) Layer-3
106 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
107 * Y Parametric Stereo
108 * N Direct Stream Transfer
109 * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
110 *
111 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
112 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
113 Parametric Stereo.
114 */
115
116#define overread_err "Input buffer exhausted before END element found\n"
117
118static int count_channels(uint8_t (*layout)[3], int tags)
119{
120 int i, sum = 0;
121 for (i = 0; i < tags; i++) {
122 int syn_ele = layout[i][0];
123 int pos = layout[i][2];
124 sum += (1 + (syn_ele == TYPE_CPE)) *
126 }
127 return sum;
128}
129
130/**
131 * Check for the channel element in the current channel position configuration.
132 * If it exists, make sure the appropriate element is allocated and map the
133 * channel order to match the internal FFmpeg channel layout.
134 *
135 * @param che_pos current channel position configuration
136 * @param type channel element type
137 * @param id channel element id
138 * @param channels count of the number of channels in the configuration
139 *
140 * @return Returns error status. 0 - OK, !0 - error
141 */
143 enum ChannelPosition che_pos,
144 int type, int id, int *channels)
145{
146 if (*channels >= MAX_CHANNELS)
147 return AVERROR_INVALIDDATA;
148 if (che_pos) {
149 if (!ac->che[type][id]) {
150 int ret = ac->proc.sbr_ctx_alloc_init(ac, &ac->che[type][id], type);
151 if (ret < 0)
152 return ret;
153 ac->che[type][id]->ch[0].output = ac->che[type][id]->ch[0].ret_buf;
154 ac->che[type][id]->ch[1].output = ac->che[type][id]->ch[1].ret_buf;
155 }
156 if (type != TYPE_CCE) {
157 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
158 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
159 return AVERROR_INVALIDDATA;
160 }
161 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
162 if (type == TYPE_CPE ||
163 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
164 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
165 }
166 }
167 } else {
168 if (ac->che[type][id]) {
169 for (int i = 0; i < FF_ARRAY_ELEMS(ac->tag_che_map); i++) {
170 for (int j = 0; j < MAX_ELEM_ID; j++) {
171 if (ac->tag_che_map[i][j] == ac->che[type][id])
172 ac->tag_che_map[i][j] = NULL;
173 }
174 }
175 ac->proc.sbr_ctx_close(ac->che[type][id]);
176 }
177 av_freep(&ac->che[type][id]);
178 memset(ac->output_element, 0, sizeof(ac->output_element));
179 }
180 return 0;
181}
182
184{
185 AACDecContext *ac = avctx->priv_data;
186 int type, id, ch, ret;
187
188 /* set channel pointers to internal buffers by default */
189 for (type = 0; type < 4; type++) {
190 for (id = 0; id < MAX_ELEM_ID; id++) {
191 ChannelElement *che = ac->che[type][id];
192 if (che) {
193 che->ch[0].output = che->ch[0].ret_buf;
194 che->ch[1].output = che->ch[1].ret_buf;
195 }
196 }
197 }
198
199 /* get output buffer */
201 if (!avctx->ch_layout.nb_channels)
202 return 1;
203
204 ac->frame->nb_samples = 2048;
205 if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
206 return ret;
207
208 /* map output channel pointers to AVFrame data */
209 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
210 if (ac->output_element[ch])
211 ac->output_element[ch]->output = (void *)ac->frame->extended_data[ch];
212 }
213
214 return 0;
215}
216
218 uint64_t av_position;
219 uint8_t syn_ele;
220 uint8_t elem_id;
222};
223
224static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
225 uint8_t (*layout_map)[3], int offset, uint64_t left,
226 uint64_t right, int pos, uint64_t *layout)
227{
228 if (layout_map[offset][0] == TYPE_CPE) {
229 e2c_vec[offset] = (struct elem_to_channel) {
230 .av_position = left | right,
231 .syn_ele = TYPE_CPE,
232 .elem_id = layout_map[offset][1],
233 .aac_position = pos
234 };
235 if (e2c_vec[offset].av_position != UINT64_MAX)
236 *layout |= e2c_vec[offset].av_position;
237
238 return 1;
239 } else {
240 e2c_vec[offset] = (struct elem_to_channel) {
241 .av_position = left,
242 .syn_ele = TYPE_SCE,
243 .elem_id = layout_map[offset][1],
244 .aac_position = pos
245 };
246 e2c_vec[offset + 1] = (struct elem_to_channel) {
247 .av_position = right,
248 .syn_ele = TYPE_SCE,
249 .elem_id = layout_map[offset + 1][1],
250 .aac_position = pos
251 };
252 if (left != UINT64_MAX)
253 *layout |= left;
254
255 if (right != UINT64_MAX)
256 *layout |= right;
257
258 return 2;
259 }
260}
261
262static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
263 int current)
264{
265 int num_pos_channels = 0;
266 int first_cpe = 0;
267 int sce_parity = 0;
268 int i;
269 for (i = current; i < tags; i++) {
270 if (layout_map[i][2] != pos)
271 break;
272 if (layout_map[i][0] == TYPE_CPE) {
273 if (sce_parity) {
274 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
275 sce_parity = 0;
276 } else {
277 return -1;
278 }
279 }
280 num_pos_channels += 2;
281 first_cpe = 1;
282 } else {
283 num_pos_channels++;
284 sce_parity ^= (pos != AAC_CHANNEL_LFE);
285 }
286 }
287 if (sce_parity &&
288 (pos == AAC_CHANNEL_FRONT && first_cpe))
289 return -1;
290
291 return num_pos_channels;
292}
293
294static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t (*layout_map)[3],
295 uint64_t *layout, int tags, int layer, int pos, int *current)
296{
297 int i = *current, j = 0;
298 int nb_channels = count_paired_channels(layout_map, tags, pos, i);
299
300 if (nb_channels < 0 || nb_channels > 5)
301 return 0;
302
303 if (pos == AAC_CHANNEL_LFE) {
304 while (nb_channels) {
305 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE)
306 return -1;
307 e2c_vec[i] = (struct elem_to_channel) {
308 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][j],
309 .syn_ele = layout_map[i][0],
310 .elem_id = layout_map[i][1],
311 .aac_position = pos
312 };
313 *layout |= e2c_vec[i].av_position;
314 i++;
315 j++;
316 nb_channels--;
317 }
318 *current = i;
319
320 return 0;
321 }
322
323 while (nb_channels & 1) {
324 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_NONE)
325 return -1;
326 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_UNUSED)
327 break;
328 e2c_vec[i] = (struct elem_to_channel) {
329 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][0],
330 .syn_ele = layout_map[i][0],
331 .elem_id = layout_map[i][1],
332 .aac_position = pos
333 };
334 *layout |= e2c_vec[i].av_position;
335 i++;
336 nb_channels--;
337 }
338
339 j = (pos != AAC_CHANNEL_SIDE) && nb_channels <= 3 ? 3 : 1;
340 while (nb_channels >= 2) {
341 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE ||
342 ff_aac_channel_map[layer][pos - 1][j+1] == AV_CHAN_NONE)
343 return -1;
344 i += assign_pair(e2c_vec, layout_map, i,
345 1ULL << ff_aac_channel_map[layer][pos - 1][j],
346 1ULL << ff_aac_channel_map[layer][pos - 1][j+1],
347 pos, layout);
348 j += 2;
349 nb_channels -= 2;
350 }
351 while (nb_channels & 1) {
352 if (ff_aac_channel_map[layer][pos - 1][5] == AV_CHAN_NONE)
353 return -1;
354 e2c_vec[i] = (struct elem_to_channel) {
355 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][5],
356 .syn_ele = layout_map[i][0],
357 .elem_id = layout_map[i][1],
358 .aac_position = pos
359 };
360 *layout |= e2c_vec[i].av_position;
361 i++;
362 nb_channels--;
363 }
364 if (nb_channels)
365 return -1;
366
367 *current = i;
368
369 return 0;
370}
371
372static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
373{
374 int i, n, total_non_cc_elements;
375 struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
376 uint64_t layout = 0;
377
378 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
379 return 0;
380
381 for (n = 0, i = 0; n < 3 && i < tags; n++) {
382 int ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_FRONT, &i);
383 if (ret < 0)
384 return 0;
385 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_SIDE, &i);
386 if (ret < 0)
387 return 0;
388 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_BACK, &i);
389 if (ret < 0)
390 return 0;
391 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_LFE, &i);
392 if (ret < 0)
393 return 0;
394 }
395
396 total_non_cc_elements = n = i;
397
399 // For 22.2 reorder the result as needed
400 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
401 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
402 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
403 FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
404 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
405 FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
406 FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
407 FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelfth
408 FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelfth (final), TpSiL & TpSiR thirteenth (final)
409 } else {
410 // For everything else, utilize the AV channel position define as a
411 // stable sort.
412 do {
413 int next_n = 0;
414 for (i = 1; i < n; i++)
415 if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
416 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
417 next_n = i;
418 }
419 n = next_n;
420 } while (n > 0);
421
422 }
423
424 for (i = 0; i < total_non_cc_elements; i++) {
425 layout_map[i][0] = e2c_vec[i].syn_ele;
426 layout_map[i][1] = e2c_vec[i].elem_id;
427 layout_map[i][2] = e2c_vec[i].aac_position;
428 }
429
430 return layout;
431}
432
434{
435 int i;
436
437 for (i = 0; i < src->usac.nb_elems; i++) {
438 AACUsacElemConfig *src_e = &src->usac.elems[i];
439 AACUsacElemConfig *dst_e = &dst->usac.elems[i];
440 /* dst_e->ext.pl_buf is guaranteed to be set to src_e->ext.pl_buf
441 * upon this function's return */
442 av_refstruct_replace(&dst_e->ext.pl_buf, src_e->ext.pl_buf);
443 }
444
445 /* Unref all additional buffers to close leaks */
446 for (; i < dst->usac.nb_elems; i++)
447 av_refstruct_unref(&dst->usac.elems[i].ext.pl_buf);
448
449 /* Set all other properties */
450 *dst = *src;
451}
452
453/**
454 * Save current output configuration if and only if it has been locked.
455 */
457{
458 int pushed = 0;
459
460 if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
461 copy_oc(&ac->oc[0], &ac->oc[1]);
462 pushed = 1;
463 }
464 ac->oc[1].status = OC_NONE;
465 return pushed;
466}
467
468/**
469 * Restore the previous output configuration if and only if the current
470 * configuration is unlocked.
471 */
473{
474 if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
475 copy_oc(&ac->oc[1], &ac->oc[0]);
476
478 ac->oc[1].status, 0);
479 }
480}
481
482/**
483 * Configure output channel order based on the current program
484 * configuration element.
485 *
486 * @return Returns error status. 0 - OK, !0 - error
487 */
489 uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
490 enum OCStatus oc_type, int get_new_frame)
491{
492 AVCodecContext *avctx = ac->avctx;
493 int i, channels = 0, ret;
494 uint64_t layout = 0;
495 uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
496 uint8_t type_counts[TYPE_END] = { 0 };
497
498 if (get_new_frame && !ac->frame)
499 return AVERROR_INVALIDDATA;
500
501 if (ac->oc[1].layout_map != layout_map) {
502 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
503 ac->oc[1].layout_map_tags = tags;
504 }
505 for (i = 0; i < tags; i++) {
506 int type = layout_map[i][0];
507 int id = layout_map[i][1];
508 id_map[type][id] = type_counts[type]++;
509 if (id_map[type][id] >= MAX_ELEM_ID) {
510 avpriv_request_sample(ac->avctx, "Too large remapped id");
512 }
513 }
514 // Try to sniff a reasonable channel order, otherwise output the
515 // channels in the order the PCE declared them.
517 layout = sniff_channel_order(layout_map, tags);
518 for (i = 0; i < tags; i++) {
519 int type = layout_map[i][0];
520 int id = layout_map[i][1];
521 int iid = id_map[type][id];
522 int position = layout_map[i][2];
523 // Allocate or free elements depending on if they are in the
524 // current program configuration.
525 ret = che_configure(ac, position, type, iid, &channels);
526 if (ret < 0)
527 return ret;
528 ac->tag_che_map[type][id] = ac->che[type][iid];
529 }
530 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
531 if (layout == AV_CH_FRONT_CENTER) {
533 } else {
534 layout = 0;
535 }
536 }
537
539 if (layout)
541 else {
544 }
545
547 ac->oc[1].status = oc_type;
548
549 if (get_new_frame) {
550 if ((ret = frame_configure_elements(ac->avctx)) < 0)
551 return ret;
552 }
553
554 return 0;
555}
556
557static av_cold void flush(AVCodecContext *avctx)
558{
559 AACDecContext *ac= avctx->priv_data;
560 int type, i, j;
561
562 for (type = 3; type >= 0; type--) {
563 for (i = 0; i < MAX_ELEM_ID; i++) {
564 ChannelElement *che = ac->che[type][i];
565 if (che) {
566 for (j = 0; j <= 1; j++) {
567 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
568 }
569 ac->proc.sbr_ctx_reset(ac, che);
570 }
571 }
572 }
573
574#if CONFIG_AAC_DECODER
575 ff_aac_usac_reset_state(ac, &ac->oc[1]);
576#endif
577}
578
579/**
580 * Set up channel positions based on a default channel configuration
581 * as specified in table 1.17.
582 *
583 * @return Returns error status. 0 - OK, !0 - error
584 */
586 uint8_t (*layout_map)[3],
587 int *tags,
588 int channel_config)
589{
590 if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
591 channel_config > 14) {
592 av_log(avctx, AV_LOG_ERROR,
593 "invalid default channel configuration (%d)\n",
594 channel_config);
595 return AVERROR_INVALIDDATA;
596 }
597 *tags = ff_tags_per_config[channel_config];
598 memcpy(layout_map, ff_aac_channel_layout_map[channel_config - 1],
599 *tags * sizeof(*layout_map));
600
601 /*
602 * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
603 * However, at least Nero AAC encoder encodes 7.1 streams using the default
604 * channel config 7, mapping the side channels of the original audio stream
605 * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
606 * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
607 * the incorrect streams as if they were correct (and as the encoder intended).
608 *
609 * As actual intended 7.1(wide) streams are very rare, default to assuming a
610 * 7.1 layout was intended.
611 */
612 if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
613 layout_map[2][2] = AAC_CHANNEL_BACK;
614
615 if (!ac || !ac->warned_71_wide++) {
616 av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
617 " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
618 " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
619 }
620 }
621
622 return 0;
623}
624
626{
627 /* For PCE based channel configurations map the channels solely based
628 * on tags. */
629 if (!ac->oc[1].m4ac.chan_config) {
630 return ac->tag_che_map[type][elem_id];
631 }
632 // Allow single CPE stereo files to be signalled with mono configuration.
633 if (!ac->tags_mapped && type == TYPE_CPE &&
634 ac->oc[1].m4ac.chan_config == 1) {
635 uint8_t layout_map[MAX_ELEM_ID*4][3];
636 int layout_map_tags;
638
639 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
640
641 if (ff_aac_set_default_channel_config(ac, ac->avctx, layout_map,
642 &layout_map_tags, 2) < 0)
643 return NULL;
644 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
645 OC_TRIAL_FRAME, 1) < 0)
646 return NULL;
647
648 ac->oc[1].m4ac.chan_config = 2;
649 ac->oc[1].m4ac.ps = 0;
650 }
651 // And vice-versa
652 if (!ac->tags_mapped && type == TYPE_SCE &&
653 ac->oc[1].m4ac.chan_config == 2) {
654 uint8_t layout_map[MAX_ELEM_ID * 4][3];
655 int layout_map_tags;
657
658 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
659
660 layout_map_tags = 2;
661 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
662 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
663 layout_map[0][1] = 0;
664 layout_map[1][1] = 1;
665 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
666 OC_TRIAL_FRAME, 1) < 0)
667 return NULL;
668
669 if (ac->oc[1].m4ac.sbr)
670 ac->oc[1].m4ac.ps = -1;
671 }
672 /* For indexed channel configurations map the channels solely based
673 * on position. */
674 switch (ac->oc[1].m4ac.chan_config) {
675 case 14:
676 if (ac->tags_mapped > 2 && ((type == TYPE_CPE && elem_id < 3) ||
677 (type == TYPE_LFE && elem_id < 1))) {
678 ac->tags_mapped++;
679 return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
680 }
682 case 13:
683 if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
684 (type == TYPE_SCE && elem_id < 6) ||
685 (type == TYPE_LFE && elem_id < 2))) {
686 ac->tags_mapped++;
687 return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
688 }
690 case 12:
691 case 7:
692 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
693 ac->tags_mapped++;
694 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
695 }
697 case 11:
698 if (ac->tags_mapped == 3 && type == TYPE_SCE) {
699 ac->tags_mapped++;
700 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
701 }
703 case 6:
704 /* Some streams incorrectly code 5.1 audio as
705 * SCE[0] CPE[0] CPE[1] SCE[1]
706 * instead of
707 * SCE[0] CPE[0] CPE[1] LFE[0].
708 * If we seem to have encountered such a stream, transfer
709 * the LFE[0] element to the SCE[1]'s mapping */
710 if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
711 if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
713 "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
714 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
716 }
717 ac->tags_mapped++;
718 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
719 }
721 case 5:
722 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
723 ac->tags_mapped++;
724 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
725 }
727 case 4:
728 /* Some streams incorrectly code 4.0 audio as
729 * SCE[0] CPE[0] LFE[0]
730 * instead of
731 * SCE[0] CPE[0] SCE[1].
732 * If we seem to have encountered such a stream, transfer
733 * the SCE[1] element to the LFE[0]'s mapping */
734 if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
735 if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
737 "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
738 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
740 }
741 ac->tags_mapped++;
742 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
743 }
744 if (ac->tags_mapped == 2 &&
745 ac->oc[1].m4ac.chan_config == 4 &&
746 type == TYPE_SCE) {
747 ac->tags_mapped++;
748 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
749 }
751 case 3:
752 case 2:
753 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
754 type == TYPE_CPE) {
755 ac->tags_mapped++;
756 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
757 } else if (ac->tags_mapped == 1 && ac->oc[1].m4ac.chan_config == 2 &&
758 type == TYPE_SCE) {
759 ac->tags_mapped++;
760 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
761 }
763 case 1:
764 if (!ac->tags_mapped && type == TYPE_SCE) {
765 ac->tags_mapped++;
766 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
767 }
769 default:
770 return NULL;
771 }
772}
773
774/**
775 * Decode an array of 4 bit element IDs, optionally interleaved with a
776 * stereo/mono switching bit.
777 *
778 * @param type speaker type/position for these channels
779 */
780static void decode_channel_map(uint8_t layout_map[][3],
782 GetBitContext *gb, int n)
783{
784 while (n--) {
786 switch (type) {
788 case AAC_CHANNEL_BACK:
789 case AAC_CHANNEL_SIDE:
790 syn_ele = get_bits1(gb);
791 break;
792 case AAC_CHANNEL_CC:
793 skip_bits1(gb);
795 break;
796 case AAC_CHANNEL_LFE:
798 break;
799 default:
800 // AAC_CHANNEL_OFF has no channel map
801 av_assert0(0);
802 }
803 layout_map[0][0] = syn_ele;
804 layout_map[0][1] = get_bits(gb, 4);
805 layout_map[0][2] = type;
806 layout_map++;
807 }
808}
809
811 int reference_position) {
812 int n = (reference_position - get_bits_count(gb) & 7);
813 if (n)
814 skip_bits(gb, n);
815}
816
817/**
818 * Decode program configuration element; reference: table 4.2.
819 *
820 * @return Returns error status. 0 - OK, !0 - error
821 */
823 uint8_t (*layout_map)[3],
824 GetBitContext *gb, int byte_align_ref)
825{
826 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
827 int sampling_index;
828 int comment_len;
829 int tags;
830
831 skip_bits(gb, 2); // object_type
832
833 sampling_index = get_bits(gb, 4);
834 if (m4ac->sampling_index != sampling_index)
835 av_log(avctx, AV_LOG_WARNING,
836 "Sample rate index (%d) in program config element does not "
837 "match the sample rate index (%d) configured by the container.\n", sampling_index, m4ac->sampling_index);
838
839 num_front = get_bits(gb, 4);
840 num_side = get_bits(gb, 4);
841 num_back = get_bits(gb, 4);
842 num_lfe = get_bits(gb, 2);
843 num_assoc_data = get_bits(gb, 3);
844 num_cc = get_bits(gb, 4);
845
846 if (get_bits1(gb))
847 skip_bits(gb, 4); // mono_mixdown_tag
848 if (get_bits1(gb))
849 skip_bits(gb, 4); // stereo_mixdown_tag
850
851 if (get_bits1(gb))
852 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
853
854 if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
855 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
856 return -1;
857 }
858 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
859 tags = num_front;
860 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
861 tags += num_side;
862 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
863 tags += num_back;
864 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
865 tags += num_lfe;
866
867 skip_bits_long(gb, 4 * num_assoc_data);
868
869 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
870 tags += num_cc;
871
872 relative_align_get_bits(gb, byte_align_ref);
873
874 /* comment field, first byte is length */
875 comment_len = get_bits(gb, 8) * 8;
876 if (get_bits_left(gb) < comment_len) {
877 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
878 return AVERROR_INVALIDDATA;
879 }
880
881 // Height extension
882 int height_ext = 0;
883 if (comment_len >= 16 + (num_front * 2 + num_side * 2 + num_back * 2))
884 height_ext = show_bits(gb, 8) == 0xAC;
885 if (height_ext) {
886 uint8_t height_map[4 /* ChannelPosition */][16 /* Channel */]; // 0 == base, 1 == top, 2 == bottom.
887 uint8_t tag[6 /* ChannelPosition */ ][16 /* Channel */][3];
888 int i, invalid = 0, height_tags = 0;
889
890 skip_bits(gb, 8);
891
892 // Read height extension bits to height_map, which define which layer each element belongs to.
893 // Also make a copy of layout_map that will then be used to rearrange it.
894 for (i = 0; i < num_front; i++) {
895 int height = get_bits(gb, 2);
896 invalid |= height > 2;
897 height_map [AAC_CHANNEL_FRONT][i] = height;
898 memcpy(&tag[AAC_CHANNEL_FRONT][i], layout_map + height_tags, sizeof(*layout_map));
899 height_tags++;
900 }
901 for (i = 0; i < num_side; i++) {
902 int height = get_bits(gb, 2);
903 invalid |= height > 2;
904 height_map [AAC_CHANNEL_SIDE][i] = height;
905 memcpy(&tag[AAC_CHANNEL_SIDE][i], layout_map + height_tags, sizeof(*layout_map));
906 height_tags++;
907 }
908 for (i = 0; i < num_back; i++) {
909 int height = get_bits(gb, 2);
910 invalid |= height > 2;
911 height_map [AAC_CHANNEL_BACK][i] = height;
912 memcpy(&tag[AAC_CHANNEL_BACK][i], layout_map + height_tags, sizeof(*layout_map));
913 height_tags++;
914 }
915 for (i = 0; i < num_lfe; i++) {
916 memcpy(&tag[AAC_CHANNEL_LFE][i], layout_map + height_tags, sizeof(*layout_map));
917 height_tags++;
918 }
919 for (i = 0; i < num_cc; i++) {
920 memcpy(&tag[AAC_CHANNEL_CC][i], layout_map + height_tags, sizeof(*layout_map));
921 height_tags++;
922 }
923 av_assert0(height_tags == tags);
924
925 if (!invalid) {
926 height_tags = 0;
927 // For each height layer, check that an element belongs to it and copy it back to layout_map.
928 // We need to take into account LFE and CC elements that may be present.
929 for (i = 0; i < 3; i++) {
930 for (int j = 0; j < num_front; j++) {
931 if (height_map[AAC_CHANNEL_FRONT][j] == i) {
932 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_FRONT][j], sizeof(*layout_map));
933 height_tags++;
934 }
935 }
936 for (int j = 0; j < num_side; j++) {
937 if (height_map[AAC_CHANNEL_SIDE][j] == i) {
938 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_SIDE][j], sizeof(*layout_map));
939 height_tags++;
940 }
941 }
942 for (int j = 0; j < num_back; j++) {
943 if (height_map[AAC_CHANNEL_BACK][j] == i) {
944 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_BACK][j], sizeof(*layout_map));
945 height_tags++;
946 }
947 }
948 if (i == 0) { // Base height, copy LFE and CC elements before moving to Top and Bottom
949 for (int j = 0; j < num_lfe; j++) {
950 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_LFE][j], sizeof(*layout_map));
951 height_tags++;
952 }
953 for (int j = 0; j < num_cc; j++) {
954 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_CC][j], sizeof(*layout_map));
955 height_tags++;
956 }
957 }
958 }
959 av_assert0(height_tags == tags);
960 }
961
962 comment_len -= 8 + (num_front * 2 + num_side * 2 + num_back * 2);
963 }
964
965 skip_bits_long(gb, comment_len);
966 return tags;
967}
968
969/**
970 * Decode GA "General Audio" specific configuration; reference: table 4.1.
971 *
972 * @param ac pointer to AACDecContext, may be null
973 * @param avctx pointer to AVCCodecContext, used for logging
974 *
975 * @return Returns error status. 0 - OK, !0 - error
976 */
978 GetBitContext *gb,
979 int get_bit_alignment,
980 MPEG4AudioConfig *m4ac,
981 int channel_config)
982{
983 int extension_flag, ret, ep_config, res_flags;
984 uint8_t layout_map[MAX_ELEM_ID*4][3];
985 int tags = 0;
986
987 m4ac->frame_length_short = get_bits1(gb);
988
989 if (get_bits1(gb)) // dependsOnCoreCoder
990 skip_bits(gb, 14); // coreCoderDelay
991 extension_flag = get_bits1(gb);
992
993 if (m4ac->object_type == AOT_AAC_SCALABLE ||
995 skip_bits(gb, 3); // layerNr
996
997 if (channel_config == 0) {
998 skip_bits(gb, 4); // element_instance_tag
999 tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
1000 if (tags < 0)
1001 return tags;
1002 } else {
1003 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1004 &tags, channel_config)))
1005 return ret;
1006 }
1007
1008 if (count_channels(layout_map, tags) > 1) {
1009 m4ac->ps = 0;
1010 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
1011 m4ac->ps = 1;
1012
1013 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1014 return ret;
1015
1016 if (extension_flag) {
1017 switch (m4ac->object_type) {
1018 case AOT_ER_BSAC:
1019 skip_bits(gb, 5); // numOfSubFrame
1020 skip_bits(gb, 11); // layer_length
1021 break;
1022 case AOT_ER_AAC_LC:
1023 case AOT_ER_AAC_LTP:
1025 case AOT_ER_AAC_LD:
1026 res_flags = get_bits(gb, 3);
1027 if (res_flags) {
1029 "AAC data resilience (flags %x)",
1030 res_flags);
1031 return AVERROR_PATCHWELCOME;
1032 }
1033 break;
1034 }
1035 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
1036 }
1037 switch (m4ac->object_type) {
1038 case AOT_ER_AAC_LC:
1039 case AOT_ER_AAC_LTP:
1041 case AOT_ER_AAC_LD:
1042 ep_config = get_bits(gb, 2);
1043 if (ep_config) {
1045 "epConfig %d", ep_config);
1046 return AVERROR_PATCHWELCOME;
1047 }
1048 }
1049 return 0;
1050}
1051
1053 GetBitContext *gb,
1054 MPEG4AudioConfig *m4ac,
1055 int channel_config)
1056{
1057 int ret, ep_config, res_flags;
1058 uint8_t layout_map[MAX_ELEM_ID*4][3];
1059 int tags = 0;
1060 const int ELDEXT_TERM = 0;
1061
1062 m4ac->ps = 0;
1063 m4ac->sbr = 0;
1064 m4ac->frame_length_short = get_bits1(gb);
1065
1066 res_flags = get_bits(gb, 3);
1067 if (res_flags) {
1069 "AAC data resilience (flags %x)",
1070 res_flags);
1071 return AVERROR_PATCHWELCOME;
1072 }
1073
1074 if (get_bits1(gb)) { // ldSbrPresentFlag
1076 "Low Delay SBR");
1077 return AVERROR_PATCHWELCOME;
1078 }
1079
1080 while (get_bits(gb, 4) != ELDEXT_TERM) {
1081 int len = get_bits(gb, 4);
1082 if (len == 15)
1083 len += get_bits(gb, 8);
1084 if (len == 15 + 255)
1085 len += get_bits(gb, 16);
1086 if (get_bits_left(gb) < len * 8 + 4) {
1088 return AVERROR_INVALIDDATA;
1089 }
1090 skip_bits_long(gb, 8 * len);
1091 }
1092
1093 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1094 &tags, channel_config)))
1095 return ret;
1096
1097 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1098 return ret;
1099
1100 ep_config = get_bits(gb, 2);
1101 if (ep_config) {
1103 "epConfig %d", ep_config);
1104 return AVERROR_PATCHWELCOME;
1105 }
1106 return 0;
1107}
1108
1109/**
1110 * Decode audio specific configuration; reference: table 1.13.
1111 *
1112 * @param ac pointer to AACDecContext, may be null
1113 * @param avctx pointer to AVCCodecContext, used for logging
1114 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1115 * @param gb buffer holding an audio specific config
1116 * @param get_bit_alignment relative alignment for byte align operations
1117 * @param sync_extension look for an appended sync extension
1118 *
1119 * @return Returns error status or number of consumed bits. <0 - error
1120 */
1122 AVCodecContext *avctx,
1124 GetBitContext *gb,
1125 int get_bit_alignment,
1126 int sync_extension)
1127{
1128 int i, ret;
1129 GetBitContext gbc = *gb;
1130 MPEG4AudioConfig *m4ac = &oc->m4ac;
1131 MPEG4AudioConfig m4ac_bak = *m4ac;
1132
1133 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1134 *m4ac = m4ac_bak;
1135 return AVERROR_INVALIDDATA;
1136 }
1137
1138 if (m4ac->sampling_index > 12) {
1139 av_log(avctx, AV_LOG_ERROR,
1140 "invalid sampling rate index %d\n",
1141 m4ac->sampling_index);
1142 *m4ac = m4ac_bak;
1143 return AVERROR_INVALIDDATA;
1144 }
1145 if (m4ac->object_type == AOT_ER_AAC_LD &&
1146 (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
1147 av_log(avctx, AV_LOG_ERROR,
1148 "invalid low delay sampling rate index %d\n",
1149 m4ac->sampling_index);
1150 *m4ac = m4ac_bak;
1151 return AVERROR_INVALIDDATA;
1152 }
1153
1154 skip_bits_long(gb, i);
1155
1156 switch (m4ac->object_type) {
1157 case AOT_AAC_MAIN:
1158 case AOT_AAC_LC:
1159 case AOT_AAC_SSR:
1160 case AOT_AAC_LTP:
1161 case AOT_ER_AAC_LC:
1162 case AOT_ER_AAC_LD:
1163 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1164 &oc->m4ac, m4ac->chan_config)) < 0)
1165 return ret;
1166 break;
1167 case AOT_ER_AAC_ELD:
1168 if ((ret = decode_eld_specific_config(ac, avctx, gb,
1169 &oc->m4ac, m4ac->chan_config)) < 0)
1170 return ret;
1171 break;
1172#if CONFIG_AAC_DECODER
1173 case AOT_USAC:
1174 if ((ret = ff_aac_usac_config_decode(ac, avctx, gb,
1175 oc, m4ac->chan_config)) < 0)
1176 return ret;
1177 break;
1178#endif
1179 default:
1181 "Audio object type %s%d",
1182 m4ac->sbr == 1 ? "SBR+" : "",
1183 m4ac->object_type);
1184 return AVERROR(ENOSYS);
1185 }
1186
1187 ff_dlog(avctx,
1188 "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1189 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1190 m4ac->sample_rate, m4ac->sbr,
1191 m4ac->ps);
1192
1193 return get_bits_count(gb);
1194}
1195
1197 AVCodecContext *avctx,
1199 const uint8_t *data, int64_t bit_size,
1200 int sync_extension)
1201{
1202 int i, ret;
1203 GetBitContext gb;
1204
1205 if (bit_size < 0 || bit_size > INT_MAX) {
1206 av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1207 return AVERROR_INVALIDDATA;
1208 }
1209
1210 ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1211 for (i = 0; i < bit_size >> 3; i++)
1212 ff_dlog(avctx, "%02x ", data[i]);
1213 ff_dlog(avctx, "\n");
1214
1215 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1216 return ret;
1217
1218 return decode_audio_specific_config_gb(ac, avctx, oc, &gb, 0,
1219 sync_extension);
1220}
1221
1223{
1224 AACDecContext *ac = avctx->priv_data;
1225
1226 for (int i = 0; i < 2; i++) {
1227 OutputConfiguration *oc = &ac->oc[i];
1228 AACUSACConfig *usac = &oc->usac;
1229 for (int j = 0; j < usac->nb_elems; j++) {
1230 AACUsacElemConfig *ec = &usac->elems[j];
1232 }
1233
1235 }
1236
1237 for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {
1238 for (int i = 0; i < MAX_ELEM_ID; i++) {
1239 if (ac->che[type][i]) {
1240 ac->proc.sbr_ctx_close(ac->che[type][i]);
1241 av_freep(&ac->che[type][i]);
1242 }
1243 }
1244 }
1245
1246 av_tx_uninit(&ac->mdct96);
1247 av_tx_uninit(&ac->mdct120);
1248 av_tx_uninit(&ac->mdct128);
1249 av_tx_uninit(&ac->mdct480);
1250 av_tx_uninit(&ac->mdct512);
1251 av_tx_uninit(&ac->mdct768);
1252 av_tx_uninit(&ac->mdct960);
1253 av_tx_uninit(&ac->mdct1024);
1254 av_tx_uninit(&ac->mdct_ltp);
1255
1256 // Compiler will optimize this branch away.
1257 if (ac->is_fixed)
1258 av_freep(&ac->RENAME_FIXED(fdsp));
1259 else
1260 av_freep(&ac->fdsp);
1261
1262 return 0;
1263}
1264
1266{
1267 AACDecContext *ac = avctx->priv_data;
1268 int is_fixed = ac->is_fixed, ret;
1269 float scale_fixed, scale_float;
1270 const float *const scalep = is_fixed ? &scale_fixed : &scale_float;
1271 enum AVTXType tx_type = is_fixed ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT;
1272
1273#define MDCT_INIT(s, fn, len, sval) \
1274 scale_fixed = (sval) * 128.0f; \
1275 scale_float = (sval) / 32768.0f; \
1276 ret = av_tx_init(&s, &fn, tx_type, 1, len, scalep, 0); \
1277 if (ret < 0) \
1278 return ret
1279
1280 MDCT_INIT(ac->mdct96, ac->mdct96_fn, 96, 1.0/96);
1281 MDCT_INIT(ac->mdct120, ac->mdct120_fn, 120, 1.0/120);
1282 MDCT_INIT(ac->mdct128, ac->mdct128_fn, 128, 1.0/128);
1283 MDCT_INIT(ac->mdct480, ac->mdct480_fn, 480, 1.0/480);
1284 MDCT_INIT(ac->mdct512, ac->mdct512_fn, 512, 1.0/512);
1285 MDCT_INIT(ac->mdct768, ac->mdct768_fn, 768, 1.0/768);
1286 MDCT_INIT(ac->mdct960, ac->mdct960_fn, 960, 1.0/960);
1287 MDCT_INIT(ac->mdct1024, ac->mdct1024_fn, 1024, 1.0/1024);
1288#undef MDCT_INIT
1289
1290 /* LTP forward MDCT */
1291 scale_fixed = -1.0;
1292 scale_float = -32786.0*2 + 36;
1293 ret = av_tx_init(&ac->mdct_ltp, &ac->mdct_ltp_fn, tx_type, 0, 1024, scalep, 0);
1294 if (ret < 0)
1295 return ret;
1296
1297 return 0;
1298}
1299
1300/**
1301 * The samples output after a seek before the output is reliable.
1302 * Frames overlap, so the frame before the first one is needed, and the
1303 * three before it for the low delay filterbank of ELD. SBR also keeps the
1304 * state of the frame before and codes its envelopes relative to it, so one
1305 * more frame is needed, of twice as many output samples when SBR doubles
1306 * the sample rate.
1307 */
1308static int seek_preroll(const MPEG4AudioConfig *m4ac)
1309{
1310 int frame = m4ac->frame_length_short ? 960 : 1024;
1311 int frames = 1 + (m4ac->sbr == 1);
1312 int upsampled = m4ac->sbr == 1 && m4ac->ext_sample_rate > m4ac->sample_rate;
1313
1314 if (m4ac->object_type == AOT_USAC)
1315 frame = m4ac->frame_length_short ? 768 : 1024;
1316 else if (m4ac->object_type == AOT_ER_AAC_LD || m4ac->object_type == AOT_ER_AAC_ELD)
1317 frame >>= 1;
1318 if (m4ac->object_type == AOT_ER_AAC_ELD)
1319 frames += 2;
1320 return (frame * frames) << upsampled;
1321}
1322
1324{
1325 AACDecContext *ac = avctx->priv_data;
1326 int ret;
1327
1328 if (avctx->sample_rate > 96000)
1329 return AVERROR_INVALIDDATA;
1330
1332
1333 ac->avctx = avctx;
1334 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1335
1336 if (avctx->extradata_size > 0) {
1337 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
1338 avctx->extradata,
1339 avctx->extradata_size * 8LL,
1340 1)) < 0)
1341 return ret;
1342 } else {
1343 int sr, i;
1344 uint8_t layout_map[MAX_ELEM_ID*4][3];
1345 int layout_map_tags;
1346
1348 ac->oc[1].m4ac.sampling_index = sr;
1349 ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
1350 ac->oc[1].m4ac.sbr = -1;
1351 ac->oc[1].m4ac.ps = -1;
1352
1353 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1355 break;
1357 i = 0;
1358 }
1359 ac->oc[1].m4ac.chan_config = i;
1360
1361 if (ac->oc[1].m4ac.chan_config) {
1362 ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1363 &layout_map_tags,
1364 ac->oc[1].m4ac.chan_config);
1365 if (!ret)
1366 ff_aac_output_configure(ac, layout_map, layout_map_tags,
1367 OC_GLOBAL_HDR, 0);
1368 else if (avctx->err_recognition & AV_EF_EXPLODE)
1369 return AVERROR_INVALIDDATA;
1370 }
1371 }
1372
1373 /* A larger pre-roll declared by the container is kept. */
1374 avctx->seek_preroll = FFMAX(avctx->seek_preroll, seek_preroll(&ac->oc[1].m4ac));
1375
1376 if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
1377 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1378 return AVERROR_INVALIDDATA;
1379 }
1380
1381 ac->random_state = 0x1f2e3d4c;
1382
1383 return init_dsp(avctx);
1384}
1385
1386/**
1387 * Skip data_stream_element; reference: table 4.10.
1388 */
1390{
1391 int byte_align = get_bits1(gb);
1392 int count = get_bits(gb, 8);
1393 if (count == 255)
1394 count += get_bits(gb, 8);
1395 if (byte_align)
1396 align_get_bits(gb);
1397
1398 if (get_bits_left(gb) < 8 * count) {
1399 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1400 return AVERROR_INVALIDDATA;
1401 }
1402 skip_bits_long(gb, 8 * count);
1403 return 0;
1404}
1405
1407 GetBitContext *gb)
1408{
1409 int sfb;
1410 if (get_bits1(gb)) {
1411 ics->predictor_reset_group = get_bits(gb, 5);
1412 if (ics->predictor_reset_group == 0 ||
1413 ics->predictor_reset_group > 30) {
1415 "Invalid Predictor Reset Group.\n");
1416 return AVERROR_INVALIDDATA;
1417 }
1418 }
1419 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1420 ics->prediction_used[sfb] = get_bits1(gb);
1421 }
1422 return 0;
1423}
1424
1425/**
1426 * Decode Long Term Prediction data; reference: table 4.xx.
1427 */
1429 GetBitContext *gb, uint8_t max_sfb)
1430{
1431 int sfb;
1432
1433 ltp->lag = get_bits(gb, 11);
1434 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1435 ltp->coef_fixed = Q30(ff_ltp_coef[get_bits(gb, 3)]);
1436 else if (CONFIG_AAC_DECODER)
1437 ltp->coef = ff_ltp_coef[get_bits(gb, 3)];
1438
1439 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1440 ltp->used[sfb] = get_bits1(gb);
1441}
1442
1443/**
1444 * Decode Individual Channel Stream info; reference: table 4.6.
1445 */
1447 GetBitContext *gb)
1448{
1449 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1450 const int aot = m4ac->object_type;
1451 const int sampling_index = m4ac->sampling_index;
1452 int ret_fail = AVERROR_INVALIDDATA;
1453
1454 if (aot != AOT_ER_AAC_ELD) {
1455 if (get_bits1(gb)) {
1456 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1458 return AVERROR_INVALIDDATA;
1459 }
1460 ics->window_sequence[1] = ics->window_sequence[0];
1461 ics->window_sequence[0] = get_bits(gb, 2);
1462 if (aot == AOT_ER_AAC_LD &&
1465 "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1466 "window sequence %d found.\n", ics->window_sequence[0]);
1468 return AVERROR_INVALIDDATA;
1469 }
1470 ics->use_kb_window[1] = ics->use_kb_window[0];
1471 ics->use_kb_window[0] = get_bits1(gb);
1472 }
1474 ics->num_window_groups = 1;
1475 ics->group_len[0] = 1;
1476 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1477 int i;
1478 ics->max_sfb = get_bits(gb, 4);
1479 for (i = 0; i < 7; i++) {
1480 if (get_bits1(gb)) {
1481 ics->group_len[ics->num_window_groups - 1]++;
1482 } else {
1483 ics->num_window_groups++;
1484 ics->group_len[ics->num_window_groups - 1] = 1;
1485 }
1486 }
1487 ics->num_windows = 8;
1488 if (m4ac->frame_length_short) {
1489 ics->swb_offset = ff_swb_offset_120[sampling_index];
1490 ics->num_swb = ff_aac_num_swb_120[sampling_index];
1491 } else {
1492 ics->swb_offset = ff_swb_offset_128[sampling_index];
1493 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1494 }
1495 ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1496 ics->predictor_present = 0;
1497 } else {
1498 ics->max_sfb = get_bits(gb, 6);
1499 ics->num_windows = 1;
1500 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1501 if (m4ac->frame_length_short) {
1502 ics->swb_offset = ff_swb_offset_480[sampling_index];
1503 ics->num_swb = ff_aac_num_swb_480[sampling_index];
1504 ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1505 } else {
1506 ics->swb_offset = ff_swb_offset_512[sampling_index];
1507 ics->num_swb = ff_aac_num_swb_512[sampling_index];
1508 ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1509 }
1510 if (!ics->num_swb || !ics->swb_offset) {
1512 "Unsupported sample rate index %d for LD/ELD\n",
1513 sampling_index);
1514 goto fail;
1515 }
1516 } else {
1517 if (m4ac->frame_length_short) {
1518 ics->num_swb = ff_aac_num_swb_960[sampling_index];
1519 ics->swb_offset = ff_swb_offset_960[sampling_index];
1520 } else {
1521 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1522 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1523 }
1524 ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1525 }
1526 if (aot != AOT_ER_AAC_ELD) {
1527 ics->predictor_present = get_bits1(gb);
1528 ics->predictor_reset_group = 0;
1529 }
1530 if (ics->predictor_present) {
1531 if (aot == AOT_AAC_MAIN) {
1532 if (decode_prediction(ac, ics, gb)) {
1533 goto fail;
1534 }
1535 } else if (aot == AOT_AAC_LC ||
1536 aot == AOT_ER_AAC_LC) {
1538 "Prediction is not allowed in AAC-LC.\n");
1539 goto fail;
1540 } else {
1541 if (aot == AOT_ER_AAC_LD) {
1543 "LTP in ER AAC LD not yet implemented.\n");
1544 ret_fail = AVERROR_PATCHWELCOME;
1545 goto fail;
1546 }
1547 if ((ics->ltp.present = get_bits(gb, 1)))
1548 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1549 }
1550 }
1551 }
1552
1553 if (ics->max_sfb > ics->num_swb) {
1555 "Number of scalefactor bands in group (%d) "
1556 "exceeds limit (%d).\n",
1557 ics->max_sfb, ics->num_swb);
1558 goto fail;
1559 }
1560
1561 return 0;
1562fail:
1563 ics->max_sfb = 0;
1564 return ret_fail;
1565}
1566
1567/**
1568 * Decode band types (section_data payload); reference: table 4.46.
1569 *
1570 * @param band_type array of the used band type
1571 * @param band_type_run_end array of the last scalefactor band of a band type run
1572 *
1573 * @return Returns error status. 0 - OK, !0 - error
1574 */
1576 GetBitContext *gb)
1577{
1578 IndividualChannelStream *ics = &sce->ics;
1579 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1580
1581 for (int g = 0; g < ics->num_window_groups; g++) {
1582 int k = 0;
1583 while (k < ics->max_sfb) {
1584 uint8_t sect_end = k;
1585 int sect_len_incr;
1586 int sect_band_type = get_bits(gb, 4);
1587 if (sect_band_type == 12) {
1588 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1589 return AVERROR_INVALIDDATA;
1590 }
1591 do {
1592 sect_len_incr = get_bits(gb, bits);
1593 sect_end += sect_len_incr;
1594 if (get_bits_left(gb) < 0) {
1595 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1596 return AVERROR_INVALIDDATA;
1597 }
1598 if (sect_end > ics->max_sfb) {
1600 "Number of bands (%d) exceeds limit (%d).\n",
1601 sect_end, ics->max_sfb);
1602 return AVERROR_INVALIDDATA;
1603 }
1604 } while (sect_len_incr == (1 << bits) - 1);
1605 for (; k < sect_end; k++)
1606 sce->band_type[g*ics->max_sfb + k] = sect_band_type;
1607 }
1608 }
1609 return 0;
1610}
1611
1612/**
1613 * Decode scalefactors; reference: table 4.47.
1614 *
1615 * @param global_gain first scalefactor value as scalefactors are differentially coded
1616 * @param band_type array of the used band type
1617 * @param band_type_run_end array of the last scalefactor band of a band type run
1618 * @param sf array of scalefactors or intensity stereo positions
1619 *
1620 * @return Returns error status. 0 - OK, !0 - error
1621 */
1623 GetBitContext *gb, unsigned int global_gain)
1624{
1625 IndividualChannelStream *ics = &sce->ics;
1626 int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1627 int clipped_offset;
1628 int noise_flag = 1;
1629
1630 for (int g = 0; g < ics->num_window_groups; g++) {
1631 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1632 switch (sce->band_type[g*ics->max_sfb + sfb]) {
1633 case ZERO_BT:
1634 sce->sfo[g*ics->max_sfb + sfb] = 0;
1635 break;
1636 case INTENSITY_BT: /* fallthrough */
1637 case INTENSITY_BT2:
1639 clipped_offset = av_clip(offset[2], -155, 100);
1640 if (offset[2] != clipped_offset) {
1642 "If you heard an audible artifact, there may be a bug in the decoder. "
1643 "Clipped intensity stereo position (%d -> %d)",
1644 offset[2], clipped_offset);
1645 }
1646 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset - 100;
1647 break;
1648 case NOISE_BT:
1649 if (noise_flag-- > 0)
1651 else
1653 clipped_offset = av_clip(offset[1], -100, 155);
1654 if (offset[1] != clipped_offset) {
1656 "If you heard an audible artifact, there may be a bug in the decoder. "
1657 "Clipped noise gain (%d -> %d)",
1658 offset[1], clipped_offset);
1659 }
1660 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset;
1661 break;
1662 default:
1664 if (offset[0] > 255U) {
1666 "Scalefactor (%d) out of range.\n", offset[0]);
1667 return AVERROR_INVALIDDATA;
1668 }
1669 sce->sfo[g*ics->max_sfb + sfb] = offset[0] - 100;
1670 break;
1671 }
1672 }
1673 }
1674
1675 return 0;
1676}
1677
1678/**
1679 * Decode pulse data; reference: table 4.7.
1680 */
1681static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1682 const uint16_t *swb_offset, int num_swb)
1683{
1684 int i, pulse_swb;
1685 pulse->num_pulse = get_bits(gb, 2) + 1;
1686 pulse_swb = get_bits(gb, 6);
1687 if (pulse_swb >= num_swb)
1688 return -1;
1689 pulse->pos[0] = swb_offset[pulse_swb];
1690 pulse->pos[0] += get_bits(gb, 5);
1691 if (pulse->pos[0] >= swb_offset[num_swb])
1692 return -1;
1693 pulse->amp[0] = get_bits(gb, 4);
1694 for (i = 1; i < pulse->num_pulse; i++) {
1695 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1696 if (pulse->pos[i] >= swb_offset[num_swb])
1697 return -1;
1698 pulse->amp[i] = get_bits(gb, 4);
1699 }
1700 return 0;
1701}
1702
1703/**
1704 * Decode Temporal Noise Shaping data; reference: table 4.48.
1705 *
1706 * @return Returns error status. 0 - OK, !0 - error
1707 */
1709 GetBitContext *gb, const IndividualChannelStream *ics)
1710{
1711 int tns_max_order = INT32_MAX;
1712 const int is_usac = ac->oc[1].m4ac.object_type == AOT_USAC;
1713 int w, filt, i, coef_len, coef_res, coef_compress;
1714 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1715
1716 /* USAC doesn't seem to have a limit */
1717 if (!is_usac)
1718 tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1719
1720 for (w = 0; w < ics->num_windows; w++) {
1721 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1722 coef_res = get_bits1(gb);
1723
1724 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1725 int tmp2_idx;
1726 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1727
1728 if (is_usac)
1729 tns->order[w][filt] = get_bits(gb, 4 - is8);
1730 else
1731 tns->order[w][filt] = get_bits(gb, 5 - (2 * is8));
1732
1733 if (tns->order[w][filt] > tns_max_order) {
1735 "TNS filter order %d is greater than maximum %d.\n",
1736 tns->order[w][filt], tns_max_order);
1737 tns->order[w][filt] = 0;
1738 return AVERROR_INVALIDDATA;
1739 }
1740 if (tns->order[w][filt]) {
1741 tns->direction[w][filt] = get_bits1(gb);
1742 coef_compress = get_bits1(gb);
1743 coef_len = coef_res + 3 - coef_compress;
1744 tmp2_idx = 2 * coef_compress + coef_res;
1745
1746 for (i = 0; i < tns->order[w][filt]; i++) {
1747 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1748 tns->coef_fixed[w][filt][i] = Q31(ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]);
1749 else if (CONFIG_AAC_DECODER)
1750 tns->coef[w][filt][i] = ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1751 }
1752 }
1753 }
1754 }
1755 }
1756 return 0;
1757}
1758
1759/**
1760 * Decode Mid/Side data; reference: table 4.54.
1761 *
1762 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1763 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1764 * [3] reserved for scalable AAC
1765 */
1767 int ms_present)
1768{
1769 int idx;
1770 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1771 cpe->max_sfb_ste = cpe->ch[0].ics.max_sfb;
1772 if (ms_present == 1) {
1773 for (idx = 0; idx < max_idx; idx++)
1774 cpe->ms_mask[idx] = get_bits1(gb);
1775 } else if (ms_present == 2) {
1776 memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1777 }
1778}
1779
1781{
1782 // wd_num, wd_test, aloc_size
1783 static const uint8_t gain_mode[4][3] = {
1784 {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
1785 {2, 1, 2}, // LONG_START_SEQUENCE,
1786 {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
1787 {2, 1, 5}, // LONG_STOP_SEQUENCE
1788 };
1789
1790 const int mode = sce->ics.window_sequence[0];
1791 uint8_t bd, wd, ad;
1792
1793 // FIXME: Store the gain control data on |sce| and do something with it.
1794 uint8_t max_band = get_bits(gb, 2);
1795 for (bd = 0; bd < max_band; bd++) {
1796 for (wd = 0; wd < gain_mode[mode][0]; wd++) {
1797 uint8_t adjust_num = get_bits(gb, 3);
1798 for (ad = 0; ad < adjust_num; ad++) {
1799 skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
1800 ? 4
1801 : gain_mode[mode][2]));
1802 }
1803 }
1804 }
1805}
1806
1807/**
1808 * Decode an individual_channel_stream payload; reference: table 4.44.
1809 *
1810 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1811 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1812 *
1813 * @return Returns error status. 0 - OK, !0 - error
1814 */
1816 GetBitContext *gb, int common_window, int scale_flag)
1817{
1818 Pulse pulse;
1819 TemporalNoiseShaping *tns = &sce->tns;
1820 IndividualChannelStream *ics = &sce->ics;
1821 int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1822 int ret;
1823
1824 eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1825 er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1826 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1827 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1829
1830 /* This assignment is to silence a GCC warning about the variable being used
1831 * uninitialized when in fact it always is.
1832 */
1833 pulse.num_pulse = 0;
1834
1835 global_gain = get_bits(gb, 8);
1836
1837 if (!common_window && !scale_flag) {
1838 ret = decode_ics_info(ac, ics, gb);
1839 if (ret < 0)
1840 goto fail;
1841 }
1842
1843 if ((ret = decode_band_types(ac, sce, gb)) < 0)
1844 goto fail;
1845 if ((ret = decode_scalefactors(ac, sce, gb, global_gain)) < 0)
1846 goto fail;
1847
1848 ac->dsp.dequant_scalefactors(sce);
1849
1850 pulse_present = 0;
1851 if (!scale_flag) {
1852 if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1853 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1855 "Pulse tool not allowed in eight short sequence.\n");
1857 goto fail;
1858 }
1859 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1861 "Pulse data corrupt or invalid.\n");
1863 goto fail;
1864 }
1865 }
1866 tns->present = get_bits1(gb);
1867 if (tns->present && !er_syntax) {
1868 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1869 if (ret < 0)
1870 goto fail;
1871 }
1872 if (!eld_syntax && get_bits1(gb)) {
1873 decode_gain_control(sce, gb);
1874 if (!ac->warned_gain_control) {
1875 avpriv_report_missing_feature(ac->avctx, "Gain control");
1876 ac->warned_gain_control = 1;
1877 }
1878 }
1879 // I see no textual basis in the spec for this occurring after SSR gain
1880 // control, but this is what both reference and real implementations do
1881 if (tns->present && er_syntax) {
1882 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1883 if (ret < 0)
1884 goto fail;
1885 }
1886 }
1887
1889 pulse_present ? &pulse : NULL,
1890 sce);
1891 if (ret < 0)
1892 goto fail;
1893
1894 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1895 ac->dsp.apply_prediction(ac, sce);
1896
1897 return 0;
1898fail:
1899 memset(sce->sfo, 0, sizeof(sce->sfo));
1900 tns->present = 0;
1901 return ret;
1902}
1903
1904/**
1905 * Decode a channel_pair_element; reference: table 4.4.
1906 *
1907 * @return Returns error status. 0 - OK, !0 - error
1908 */
1910{
1911 int i, ret, common_window, ms_present = 0;
1912 int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1913
1914 common_window = eld_syntax || get_bits1(gb);
1915 if (common_window) {
1916 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1917 return AVERROR_INVALIDDATA;
1918 i = cpe->ch[1].ics.use_kb_window[0];
1919 cpe->ch[1].ics = cpe->ch[0].ics;
1920 cpe->ch[1].ics.use_kb_window[1] = i;
1921 if (cpe->ch[1].ics.predictor_present &&
1922 (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1923 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1924 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1925 ms_present = get_bits(gb, 2);
1926 if (ms_present == 3) {
1927 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1928 return AVERROR_INVALIDDATA;
1929 } else if (ms_present)
1930 decode_mid_side_stereo(cpe, gb, ms_present);
1931 }
1932 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1933 return ret;
1934 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1935 return ret;
1936
1937 if (common_window) {
1938 if (ms_present)
1939 ac->dsp.apply_mid_side_stereo(ac, cpe);
1940 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1941 ac->dsp.apply_prediction(ac, &cpe->ch[0]);
1942 ac->dsp.apply_prediction(ac, &cpe->ch[1]);
1943 }
1944 }
1945
1946 ac->dsp.apply_intensity_stereo(ac, cpe, ms_present);
1947 return 0;
1948}
1949
1950/**
1951 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1952 *
1953 * @return Returns number of bytes consumed.
1954 */
1956 GetBitContext *gb)
1957{
1958 int i;
1959 int num_excl_chan = 0;
1960
1961 do {
1962 for (i = 0; i < 7; i++)
1963 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1964 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1965
1966 return num_excl_chan / 7;
1967}
1968
1969/**
1970 * Decode dynamic range information; reference: table 4.52.
1971 *
1972 * @return Returns number of bytes consumed.
1973 */
1975 GetBitContext *gb)
1976{
1977 int n = 1;
1978 int drc_num_bands = 1;
1979 int i;
1980
1981 /* pce_tag_present? */
1982 if (get_bits1(gb)) {
1983 che_drc->pce_instance_tag = get_bits(gb, 4);
1984 skip_bits(gb, 4); // tag_reserved_bits
1985 n++;
1986 }
1987
1988 /* excluded_chns_present? */
1989 if (get_bits1(gb)) {
1990 n += decode_drc_channel_exclusions(che_drc, gb);
1991 }
1992
1993 /* drc_bands_present? */
1994 if (get_bits1(gb)) {
1995 che_drc->band_incr = get_bits(gb, 4);
1996 che_drc->interpolation_scheme = get_bits(gb, 4);
1997 n++;
1998 drc_num_bands += che_drc->band_incr;
1999 for (i = 0; i < drc_num_bands; i++) {
2000 che_drc->band_top[i] = get_bits(gb, 8);
2001 n++;
2002 }
2003 }
2004
2005 /* prog_ref_level_present? */
2006 if (get_bits1(gb)) {
2007 che_drc->prog_ref_level = get_bits(gb, 7);
2008 skip_bits1(gb); // prog_ref_level_reserved_bits
2009 n++;
2010 }
2011
2012 for (i = 0; i < drc_num_bands; i++) {
2013 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2014 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2015 n++;
2016 }
2017
2018 return n;
2019}
2020
2021static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len) {
2022 uint8_t buf[256];
2023 int i, major, minor;
2024
2025 if (len < 13+7*8)
2026 goto unknown;
2027
2028 get_bits(gb, 13); len -= 13;
2029
2030 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2031 buf[i] = get_bits(gb, 8);
2032
2033 buf[i] = 0;
2034 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2035 av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2036
2037 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2038 ac->avctx->internal->skip_samples = 1024;
2039 }
2040
2041unknown:
2042 skip_bits_long(gb, len);
2043
2044 return 0;
2045}
2046
2047/**
2048 * Decode extension data (incomplete); reference: table 4.51.
2049 *
2050 * @param cnt length of TYPE_FIL syntactic element in bytes
2051 *
2052 * @return Returns number of bytes consumed
2053 */
2055 ChannelElement *che, enum RawDataBlockType elem_type)
2056{
2057 int crc_flag = 0;
2058 int res = cnt;
2059 int type = get_bits(gb, 4);
2060
2061 if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2062 av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2063
2064 switch (type) { // extension type
2065 case EXT_SBR_DATA_CRC:
2066 crc_flag++;
2068 case EXT_SBR_DATA:
2069 if (!che) {
2070 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2071 return res;
2072 } else if (!ac->oc[1].m4ac.sbr) {
2073 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2074 skip_bits_long(gb, 8 * cnt - 4);
2075 return res;
2076 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2077 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2078 skip_bits_long(gb, 8 * cnt - 4);
2079 return res;
2080 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED &&
2081 ac->avctx->ch_layout.nb_channels == 1) {
2082 ac->oc[1].m4ac.sbr = 1;
2083 ac->oc[1].m4ac.ps = 1;
2086 ac->oc[1].status, 1);
2087 } else {
2088 ac->oc[1].m4ac.sbr = 1;
2090 }
2091
2092 ac->proc.sbr_decode_extension(ac, che, gb, crc_flag, cnt, elem_type,
2093 ac->oc[1].m4ac.frame_length_short);
2094
2095 if (ac->oc[1].m4ac.ps == 1 && !ac->warned_he_aac_mono) {
2096 av_log(ac->avctx, AV_LOG_VERBOSE, "Treating HE-AAC mono as stereo.\n");
2097 ac->warned_he_aac_mono = 1;
2098 }
2099 break;
2100 case EXT_DYNAMIC_RANGE:
2101 res = decode_dynamic_range(&ac->che_drc, gb);
2102 break;
2103 case EXT_FILL:
2104 decode_fill(ac, gb, 8 * cnt - 4);
2105 break;
2106 case EXT_FILL_DATA:
2107 case EXT_DATA_ELEMENT:
2108 default:
2109 skip_bits_long(gb, 8 * cnt - 4);
2110 break;
2111 };
2112 return res;
2113}
2114
2115/**
2116 * channel coupling transformation interface
2117 *
2118 * @param apply_coupling_method pointer to (in)dependent coupling function
2119 */
2121 enum RawDataBlockType type, int elem_id,
2122 enum CouplingPoint coupling_point,
2123 void (*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2124{
2125 int i, c;
2126
2127 for (i = 0; i < MAX_ELEM_ID; i++) {
2128 ChannelElement *cce = ac->che[TYPE_CCE][i];
2129 int index = 0;
2130
2131 if (cce && cce->coup.coupling_point == coupling_point) {
2132 ChannelCoupling *coup = &cce->coup;
2133
2134 for (c = 0; c <= coup->num_coupled; c++) {
2135 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2136 if (coup->ch_select[c] != 1) {
2137 apply_coupling_method(ac, &cc->ch[0], cce, index);
2138 if (coup->ch_select[c] != 0)
2139 index++;
2140 }
2141 if (coup->ch_select[c] != 2)
2142 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2143 } else
2144 index += 1 + (coup->ch_select[c] == 3);
2145 }
2146 }
2147 }
2148}
2149
2150/**
2151 * Convert spectral data to samples, applying all supported tools as appropriate.
2152 */
2153static void spectral_to_sample(AACDecContext *ac, int samples)
2154{
2155 int i, type;
2157 switch (ac->oc[1].m4ac.object_type) {
2158 case AOT_ER_AAC_LD:
2160 break;
2161 case AOT_ER_AAC_ELD:
2163 break;
2164 default:
2165 if (ac->oc[1].m4ac.frame_length_short)
2167 else
2169 }
2170 for (type = 3; type >= 0; type--) {
2171 for (i = 0; i < MAX_ELEM_ID; i++) {
2172 ChannelElement *che = ac->che[type][i];
2173 if (che && che->present) {
2174 if (type <= TYPE_CPE)
2176 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2177 if (che->ch[0].ics.predictor_present) {
2178 if (che->ch[0].ics.ltp.present)
2179 ac->dsp.apply_ltp(ac, &che->ch[0]);
2180 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2181 ac->dsp.apply_ltp(ac, &che->ch[1]);
2182 }
2183 }
2184 if (che->ch[0].tns.present)
2185 ac->dsp.apply_tns(che->ch[0].coeffs,
2186 &che->ch[0].tns, &che->ch[0].ics, 1);
2187 if (che->ch[1].tns.present)
2188 ac->dsp.apply_tns(che->ch[1].coeffs,
2189 &che->ch[1].tns, &che->ch[1].ics, 1);
2190 if (type <= TYPE_CPE)
2192 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2193 imdct_and_window(ac, &che->ch[0]);
2194 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2195 ac->dsp.update_ltp(ac, &che->ch[0]);
2196 if (type == TYPE_CPE) {
2197 imdct_and_window(ac, &che->ch[1]);
2198 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2199 ac->dsp.update_ltp(ac, &che->ch[1]);
2200 }
2201 if (ac->oc[1].m4ac.sbr > 0) {
2202 ac->proc.sbr_apply(ac, che, type,
2203 ac->oc[1].m4ac.frame_length_short,
2204 che->ch[0].output,
2205 che->ch[1].output);
2206 }
2207 }
2208 if (type <= TYPE_CCE)
2210 ac->dsp.clip_output(ac, che, type, samples);
2211 che->present = 0;
2212 } else if (che) {
2213 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2214 memset(che->ch[0].output, 0, samples * sizeof(*che->ch[0].output));
2215 if (type == TYPE_CPE)
2216 memset(che->ch[1].output, 0, samples * sizeof(*che->ch[1].output));
2217 }
2218 }
2219 }
2220}
2221
2223{
2224 int size;
2225 AACADTSHeaderInfo hdr_info;
2226 uint8_t layout_map[MAX_ELEM_ID*4][3];
2227 int layout_map_tags, ret;
2228
2229 size = ff_adts_header_parse(gb, &hdr_info);
2230 if (size > 0) {
2231 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2232 // This is 2 for "VLB " audio in NSV files.
2233 // See samples/nsv/vlb_audio.
2235 "More than one AAC RDB per ADTS frame");
2236 ac->warned_num_aac_frames = 1;
2237 }
2239 if (hdr_info.chan_config) {
2240 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2242 layout_map,
2243 &layout_map_tags,
2244 hdr_info.chan_config)) < 0)
2245 return ret;
2246 if ((ret = ff_aac_output_configure(ac, layout_map, layout_map_tags,
2247 FFMAX(ac->oc[1].status,
2248 OC_TRIAL_FRAME), 0)) < 0)
2249 return ret;
2250 } else {
2251 ac->oc[1].m4ac.chan_config = 0;
2252 /**
2253 * dual mono frames in Japanese DTV can have chan_config 0
2254 * WITHOUT specifying PCE.
2255 * thus, set dual mono as default.
2256 */
2257 if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2258 layout_map_tags = 2;
2259 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2260 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2261 layout_map[0][1] = 0;
2262 layout_map[1][1] = 1;
2263 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
2264 OC_TRIAL_FRAME, 0))
2265 return -7;
2266 }
2267 }
2268 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2269 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2270 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2271 ac->oc[1].m4ac.frame_length_short = 0;
2272 if (ac->oc[0].status != OC_LOCKED ||
2273 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2274 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2275 ac->oc[1].m4ac.sbr = -1;
2276 ac->oc[1].m4ac.ps = -1;
2277 }
2278 if (!hdr_info.crc_absent)
2279 skip_bits(gb, 16);
2280 }
2281 return size;
2282}
2283
2285 int *got_frame_ptr, GetBitContext *gb)
2286{
2287 AACDecContext *ac = avctx->priv_data;
2288 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2289 ChannelElement *che;
2290 int err, i;
2291 int samples = m4ac->frame_length_short ? 960 : 1024;
2292 int chan_config = m4ac->chan_config;
2293 int aot = m4ac->object_type;
2294
2295 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
2296 samples >>= 1;
2297
2298 ac->frame = frame;
2299
2300 if ((err = frame_configure_elements(avctx)) < 0)
2301 return err;
2302
2303 // The AV_PROFILE_AAC_* defines are all object_type - 1
2304 // This may lead to an undefined profile being signaled
2305 ac->avctx->profile = aot - 1;
2306
2307 ac->tags_mapped = 0;
2308
2309 if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
2310 avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2311 chan_config);
2312 return AVERROR_INVALIDDATA;
2313 }
2314 for (i = 0; i < ff_tags_per_config[chan_config]; i++) {
2315 const int elem_type = ff_aac_channel_layout_map[chan_config-1][i][0];
2316 const int elem_id = ff_aac_channel_layout_map[chan_config-1][i][1];
2317 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2319 "channel element %d.%d is not allocated\n",
2320 elem_type, elem_id);
2321 return AVERROR_INVALIDDATA;
2322 }
2323 che->present = 1;
2324 if (aot != AOT_ER_AAC_ELD)
2325 skip_bits(gb, 4);
2326 switch (elem_type) {
2327 case TYPE_SCE:
2328 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2329 break;
2330 case TYPE_CPE:
2331 err = decode_cpe(ac, gb, che);
2332 break;
2333 case TYPE_LFE:
2334 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2335 break;
2336 }
2337 if (err < 0)
2338 return err;
2339 }
2340
2341 spectral_to_sample(ac, samples);
2342
2343 if (avctx->sample_rate <= 0)
2344 avctx->sample_rate = m4ac->sample_rate;
2345
2346 if (avctx->sample_rate <= 0) {
2347 av_log(avctx, AV_LOG_ERROR,
2348 "Cannot output a frame without a valid sample rate\n");
2349 return AVERROR_INVALIDDATA;
2350 }
2351
2352 if (!ac->frame->data[0] && samples) {
2353 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2354 return AVERROR_INVALIDDATA;
2355 }
2356
2357 ac->frame->nb_samples = samples;
2358 ac->frame->sample_rate = avctx->sample_rate;
2360 *got_frame_ptr = 1;
2361
2363 return 0;
2364}
2365
2367 GetBitContext *gb, int *got_frame_ptr)
2368{
2369 int err;
2370 int is_dmono;
2371 int elem_id;
2372 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
2373 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
2374 ChannelElement *che = NULL, *che_prev = NULL;
2375 int samples = 0, multiplier, audio_found = 0, pce_found = 0, sce_count = 0;
2376 AVFrame *frame = ac->frame;
2377
2378 int payload_alignment = get_bits_count(gb);
2379 // parse
2380 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2381 elem_id = get_bits(gb, 4);
2382
2383 if (avctx->debug & FF_DEBUG_STARTCODE)
2384 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2385
2386 if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE)
2387 return AVERROR_INVALIDDATA;
2388
2389 if (elem_type < TYPE_DSE) {
2390 if (che_presence[elem_type][elem_id]) {
2391 int error = che_presence[elem_type][elem_id] > 1;
2392 av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
2393 elem_type, elem_id);
2394 if (error)
2395 return AVERROR_INVALIDDATA;
2396 }
2397 che_presence[elem_type][elem_id]++;
2398
2399 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2400 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2401 elem_type, elem_id);
2402 return AVERROR_INVALIDDATA;
2403 }
2404 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
2405 che->present = 1;
2406 }
2407
2408 switch (elem_type) {
2409
2410 case TYPE_SCE:
2411 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2412 audio_found = 1;
2413 sce_count++;
2414 break;
2415
2416 case TYPE_CPE:
2417 err = decode_cpe(ac, gb, che);
2418 audio_found = 1;
2419 break;
2420
2421 case TYPE_CCE:
2422 err = ac->proc.decode_cce(ac, gb, che);
2423 break;
2424
2425 case TYPE_LFE:
2426 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2427 audio_found = 1;
2428 break;
2429
2430 case TYPE_DSE:
2431 err = skip_data_stream_element(ac, gb);
2432 break;
2433
2434 case TYPE_PCE: {
2435 uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
2436 int tags;
2437
2438 int pushed = push_output_configuration(ac);
2439 if (pce_found && !pushed)
2440 return AVERROR_INVALIDDATA;
2441
2442 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
2443 payload_alignment);
2444 if (tags < 0) {
2445 err = tags;
2446 break;
2447 }
2448 if (pce_found) {
2449 av_log(avctx, AV_LOG_ERROR,
2450 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2452 } else {
2453 err = ff_aac_output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2454 if (!err)
2455 ac->oc[1].m4ac.chan_config = 0;
2456 pce_found = 1;
2457 }
2458 break;
2459 }
2460
2461 case TYPE_FIL:
2462 if (elem_id == 15)
2463 elem_id += get_bits(gb, 8) - 1;
2464 if (get_bits_left(gb) < 8 * elem_id) {
2465 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2466 return AVERROR_INVALIDDATA;
2467 }
2468 err = 0;
2469 while (elem_id > 0) {
2470 int ret = decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
2471 if (ret < 0) {
2472 err = ret;
2473 break;
2474 }
2475 elem_id -= ret;
2476 }
2477 break;
2478
2479 default:
2480 err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
2481 break;
2482 }
2483
2484 if (elem_type < TYPE_DSE) {
2485 che_prev = che;
2486 che_prev_type = elem_type;
2487 }
2488
2489 if (err)
2490 return err;
2491
2492 if (get_bits_left(gb) < 3) {
2494 return AVERROR_INVALIDDATA;
2495 }
2496 }
2497
2498 if (!avctx->ch_layout.nb_channels)
2499 return 0;
2500
2501 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2502 samples <<= multiplier;
2503
2504 spectral_to_sample(ac, samples);
2505
2506 if (ac->oc[1].status && audio_found) {
2507 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2508 avctx->frame_size = samples;
2509 // Implicit SBR is only known once found in a frame.
2510 avctx->seek_preroll = FFMAX(avctx->seek_preroll, seek_preroll(&ac->oc[1].m4ac));
2511 ac->oc[1].status = OC_LOCKED;
2512 }
2513
2514 if (samples && avctx->sample_rate <= 0) {
2515 av_log(avctx, AV_LOG_ERROR,
2516 "Cannot output a frame without a valid sample rate\n");
2517 return AVERROR_INVALIDDATA;
2518 }
2519
2520 if (!ac->frame->data[0] && samples) {
2521 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2522 return AVERROR_INVALIDDATA;
2523 }
2524
2525 if (samples) {
2526 ac->frame->nb_samples = samples;
2527 ac->frame->sample_rate = avctx->sample_rate;
2529 *got_frame_ptr = 1;
2530 } else {
2531 av_frame_unref(ac->frame);
2532 *got_frame_ptr = 0;
2533 }
2534
2535 /* for dual-mono audio (SCE + SCE) */
2536 is_dmono = ac->dmono_mode && sce_count == 2 &&
2539 if (is_dmono) {
2540 if (ac->dmono_mode == 1)
2541 frame->data[1] = frame->data[0];
2542 else if (ac->dmono_mode == 2)
2543 frame->data[0] = frame->data[1];
2544 }
2545
2546 return 0;
2547}
2548
2550 int *got_frame_ptr, GetBitContext *gb,
2551 const AVPacket *avpkt)
2552{
2553 int err;
2554 AACDecContext *ac = avctx->priv_data;
2555
2556 ac->frame = frame;
2557 *got_frame_ptr = 0;
2558
2559 // USAC can't be packed into ADTS due to field size limitations.
2560 if (show_bits(gb, 12) == 0xfff && ac->oc[1].m4ac.object_type != AOT_USAC) {
2561 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2562 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2563 goto fail;
2564 }
2565 if (ac->oc[1].m4ac.sampling_index > 12) {
2566 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2568 goto fail;
2569 }
2570 }
2571
2572 if ((err = frame_configure_elements(avctx)) < 0)
2573 goto fail;
2574
2575 // The AV_PROFILE_AAC_* defines are all object_type - 1
2576 // This may lead to an undefined profile being signaled
2577 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2578
2579 ac->tags_mapped = 0;
2580
2581 if (ac->oc[1].m4ac.object_type == AOT_USAC) {
2582 if (ac->is_fixed) {
2584 "AAC USAC fixed-point decoding");
2585 return AVERROR_PATCHWELCOME;
2586 }
2587#if CONFIG_AAC_DECODER
2588 err = ff_aac_usac_decode_frame(avctx, ac, gb, got_frame_ptr);
2589 if (err < 0)
2590 goto fail;
2591#endif
2592 } else {
2593 err = decode_frame_ga(avctx, ac, gb, got_frame_ptr);
2594 if (err < 0)
2595 goto fail;
2596 }
2597
2598 return err;
2599
2600fail:
2602 return err;
2603}
2604
2606 int *got_frame_ptr, AVPacket *avpkt)
2607{
2608 AACDecContext *ac = avctx->priv_data;
2609 const uint8_t *buf = avpkt->data;
2610 int buf_size = avpkt->size;
2611 GetBitContext gb;
2612 int buf_consumed;
2613 int buf_offset;
2614 int err;
2615 size_t new_extradata_size;
2616 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2618 &new_extradata_size);
2619 size_t jp_dualmono_size;
2620 const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
2622 &jp_dualmono_size);
2623
2624 if (new_extradata) {
2625 /* discard previous configuration */
2626 ac->oc[1].status = OC_NONE;
2627 err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
2628 new_extradata,
2629 new_extradata_size * 8LL, 1);
2630 if (err < 0) {
2631 return err;
2632 }
2633 }
2634
2635 ac->dmono_mode = 0;
2636 if (jp_dualmono && jp_dualmono_size > 0)
2637 ac->dmono_mode = 1 + *jp_dualmono;
2638 if (ac->force_dmono_mode >= 0)
2639 ac->dmono_mode = ac->force_dmono_mode;
2640
2641 if (INT_MAX / 8 <= buf_size)
2642 return AVERROR_INVALIDDATA;
2643
2644 if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
2645 return err;
2646
2647 switch (ac->oc[1].m4ac.object_type) {
2648 case AOT_ER_AAC_LC:
2649 case AOT_ER_AAC_LTP:
2650 case AOT_ER_AAC_LD:
2651 case AOT_ER_AAC_ELD:
2652 err = aac_decode_er_frame(avctx, frame, got_frame_ptr, &gb);
2653 break;
2654 default:
2655 err = aac_decode_frame_int(avctx, frame, got_frame_ptr, &gb, avpkt);
2656 }
2657 ac->frame = NULL;
2658 if (err < 0)
2659 return err;
2660
2661 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2662 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2663 if (buf[buf_offset])
2664 break;
2665
2666 return buf_size > buf_offset ? buf_consumed : buf_size;
2667}
2668
2669#if CONFIG_AAC_LATM_DECODER
2670#include "aacdec_latm.h"
2671#endif
2672
2673#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2674#define OFF(field) offsetof(AACDecContext, field)
2675static const AVOption options[] = {
2676 /**
2677 * AVOptions for Japanese DTV specific extensions (ADTS only)
2678 */
2679 {"dual_mono_mode", "Select the channel to decode for dual mono",
2680 OFF(force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
2681 AACDEC_FLAGS, .unit = "dual_mono_mode"},
2682
2683 {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2684 {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2685 {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2686 {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2687
2688 { "channel_order", "Order in which the channels are to be exported",
2689 OFF(output_channel_order), AV_OPT_TYPE_INT,
2690 { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, AACDEC_FLAGS, .unit = "channel_order" },
2691 { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
2692 { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2693 { "coded", "order in which the channels are coded in the bitstream",
2694 0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2695
2696 { "target_level", "Target output loudness in dBFS for xHE-AAC normalization (0 = disabled)",
2697 OFF(target_level), AV_OPT_TYPE_INT, { .i64 = 0 }, -70, 0, AACDEC_FLAGS },
2698
2699 {NULL},
2700};
2701
2702static const AVClass decoder_class = {
2703 .class_name = "AAC decoder",
2704 .item_name = av_default_item_name,
2705 .option = options,
2706 .version = LIBAVUTIL_VERSION_INT,
2707};
2708
2709#if CONFIG_AAC_DECODER
2710const FFCodec ff_aac_decoder = {
2711 .p.name = "aac",
2712 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2713 .p.type = AVMEDIA_TYPE_AUDIO,
2714 .p.id = AV_CODEC_ID_AAC,
2715 .p.priv_class = &decoder_class,
2716 .priv_data_size = sizeof(AACDecContext),
2721 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2722 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2724 .flush = flush,
2726};
2727#endif
2728
2729#if CONFIG_AAC_FIXED_DECODER
2731 .p.name = "aac_fixed",
2732 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2733 .p.type = AVMEDIA_TYPE_AUDIO,
2734 .p.id = AV_CODEC_ID_AAC,
2735 .p.priv_class = &decoder_class,
2736 .priv_data_size = sizeof(AACDecContext),
2741 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2742 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2745 .flush = flush,
2746};
2747#endif
AAC definitions and structures.
@ EIGHT_SHORT_SEQUENCE
Definition aac.h:66
@ ONLY_LONG_SEQUENCE
Definition aac.h:64
static int ff_aac_sample_rate_idx(int rate)
Definition aac.h:110
@ EXT_DATA_ELEMENT
Definition aac.h:57
@ EXT_SBR_DATA_CRC
Definition aac.h:60
@ EXT_DYNAMIC_RANGE
Definition aac.h:58
@ EXT_FILL_DATA
Definition aac.h:56
@ EXT_FILL
Definition aac.h:55
@ EXT_SBR_DATA
Definition aac.h:59
#define NOISE_PRE
preamble for NOISE_BT, put in bitstream with the first noise band
Definition aac.h:99
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition aac.h:77
@ ZERO_BT
Scalefactors and spectral data are all zero.
Definition aac.h:71
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition aac.h:76
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition aac.h:75
ChannelPosition
Definition aac.h:80
@ AAC_CHANNEL_LFE
Definition aac.h:85
@ AAC_CHANNEL_SIDE
Definition aac.h:83
@ AAC_CHANNEL_OFF
Definition aac.h:81
@ AAC_CHANNEL_CC
Definition aac.h:86
@ AAC_CHANNEL_BACK
Definition aac.h:84
@ AAC_CHANNEL_FRONT
Definition aac.h:82
RawDataBlockType
Definition aac.h:43
@ TYPE_CCE
Definition aac.h:46
@ TYPE_PCE
Definition aac.h:49
@ TYPE_CPE
Definition aac.h:45
@ TYPE_SCE
Definition aac.h:44
@ TYPE_FIL
Definition aac.h:50
@ TYPE_LFE
Definition aac.h:47
@ TYPE_END
Definition aac.h:51
@ TYPE_DSE
Definition aac.h:48
#define MAX_LTP_LONG_SFB
Definition aac.h:37
#define NOISE_PRE_BITS
length of preamble
Definition aac.h:100
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition aac.h:95
#define MAX_CHANNELS
Definition aac.h:33
#define NOISE_OFFSET
subtracted from global gain, used as offset for the preamble
Definition aac.h:101
#define MAX_ELEM_ID
Definition aac.h:34
#define Q31(x)
#define Q30(x)
AAC decoder definitions and structures.
@ CHANNEL_ORDER_DEFAULT
Definition aacdec.h:62
@ CHANNEL_ORDER_CODED
Definition aacdec.h:63
CouplingPoint
The point during decoding at which channel coupling is applied.
Definition aacdec.h:69
@ BETWEEN_TNS_AND_IMDCT
Definition aacdec.h:71
@ AFTER_IMDCT
Definition aacdec.h:72
@ BEFORE_TNS
Definition aacdec.h:70
int ff_aac_decode_init_fixed(AVCodecContext *avctx)
Dequantization-related.
int ff_aac_decode_init_float(AVCodecContext *avctx)
OCStatus
Output configuration status.
Definition aacdec.h:53
@ OC_TRIAL_FRAME
Output configuration under trial specified by a frame header.
Definition aacdec.h:56
@ OC_TRIAL_PCE
Output configuration under trial specified by an inband PCE.
Definition aacdec.h:55
@ OC_LOCKED
Output configuration locked in place.
Definition aacdec.h:58
@ OC_GLOBAL_HDR
Output configuration set in a global header but not yet locked.
Definition aacdec.h:57
@ OC_NONE
Output unconfigured.
Definition aacdec.h:54
const int16_t ff_aac_channel_map[3][4][6]
Definition aacdec_tab.c:74
av_cold void ff_aacdec_common_init_once(void)
Definition aacdec_tab.c:610
const uint8_t ff_aac_channel_layout_map[16][16][3]
Definition aacdec_tab.c:39
const int8_t ff_tags_per_config[16]
Definition aacdec_tab.c:37
VLCElem ff_vlc_scalefactors[352]
Definition aacdec_tab.c:110
const AVChannelLayout ff_aac_ch_layout[]
Definition aacdec_tab.c:95
AAC decoder data.
int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, OutputConfiguration *oc, int channel_config)
AAC Spectral Band Replication function declarations.
const float ff_ltp_coef[8]
Definition aactab.c:110
const uint8_t ff_aac_num_swb_960[]
Definition aactab.c:153
const uint8_t ff_tns_max_bands_512[]
Definition aactab.c:1982
const uint8_t ff_tns_max_bands_1024[]
Definition aactab.c:1974
const uint16_t *const ff_swb_offset_128[]
Definition aactab.c:1940
const uint16_t *const ff_swb_offset_120[]
Definition aactab.c:1950
const uint8_t ff_aac_num_swb_480[]
Definition aactab.c:165
const uint8_t ff_aac_pred_sfb_max[]
Definition aactab.c:177
const uint16_t *const ff_swb_offset_1024[]
Definition aactab.c:1900
const uint16_t *const ff_swb_offset_960[]
Definition aactab.c:1908
const uint16_t *const ff_swb_offset_480[]
Definition aactab.c:1932
const uint8_t ff_aac_num_swb_1024[]
Definition aactab.c:149
const uint8_t ff_aac_num_swb_128[]
Definition aactab.c:169
const uint8_t ff_tns_max_bands_480[]
Definition aactab.c:1986
const uint8_t ff_aac_num_swb_512[]
Definition aactab.c:161
const uint8_t ff_tns_max_bands_128[]
Definition aactab.c:1990
const float *const ff_tns_tmp2_map[4]
Definition aactab.c:142
const uint16_t *const ff_swb_offset_512[]
Definition aactab.c:1924
AAC data declarations.
#define ff_aac_num_swb_120
Definition aactab.h:79
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse the ADTS frame header to the end of the variable header, which is the first 54 bits.
Definition adts_header.c:30
static const int8_t filt[NUMTAPS *2]
Definition af_earwax.c:40
const FFCodec ff_aac_fixed_decoder
const FFCodec ff_aac_decoder
static int frames
channels
Definition aptx.h:31
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_DEBUG_STARTCODE
Definition avcodec.h:1406
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1399
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
#define AV_PROFILE_AAC_HE
Definition defs.h:72
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition defs.h:49
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
Definition defs.h:59
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
#define AV_PROFILE_AAC_HE_V2
Definition defs.h:73
static AVFrame * frame
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:615
enum AVCodecID id
Definition dts2pts.c:612
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
error code definitions
static struct @111144215057303131116103221376075045141373005341 current
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
static int64_t seek_preroll(const AVFormatContext *ic)
The largest pre-roll of the audio streams, in AV_TIME_BASE units.
Definition ffplay.c:3123
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CH_LAYOUT_22POINT2
#define AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_FRONT_LEFT
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_PKT_DATA_JP_DUALMONO
An AV_PKT_DATA_JP_DUALMONO side data packet indicates that the packet may contain "dual mono" audio s...
Definition packet.h:163
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
@ AV_CHAN_UNUSED
Channel is empty can be safely skipped.
@ AV_CHAN_NONE
Invalid channel index.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:694
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:501
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:812
static int decode_audio_specific_config(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, const uint8_t *data, int64_t bit_size, int sync_extension)
Definition aacdec.c:1196
static void spectral_to_sample(AACDecContext *ac, int samples)
Convert spectral data to samples, applying all supported tools as appropriate.
Definition aacdec.c:2153
static void relative_align_get_bits(GetBitContext *gb, int reference_position)
Definition aacdec.c:810
static int skip_data_stream_element(AACDecContext *ac, GetBitContext *gb)
Skip data_stream_element; reference: table 4.10.
Definition aacdec.c:1389
#define AACDEC_FLAGS
Definition aacdec.c:2673
int ff_aac_output_configure(AACDecContext *ac, uint8_t layout_map[MAX_ELEM_ID *4][3], int tags, enum OCStatus oc_type, int get_new_frame)
Configure output channel order based on the current program configuration element.
Definition aacdec.c:488
static int decode_extension_payload(AACDecContext *ac, GetBitContext *gb, int cnt, ChannelElement *che, enum RawDataBlockType elem_type)
Decode extension data (incomplete); reference: table 4.51.
Definition aacdec.c:2054
static uint64_t sniff_channel_order(uint8_t(*layout_map)[3], int tags)
Definition aacdec.c:372
static void decode_channel_map(uint8_t layout_map[][3], enum ChannelPosition type, GetBitContext *gb, int n)
Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
Definition aacdec.c:780
static av_cold int init_dsp(AVCodecContext *avctx)
Definition aacdec.c:1265
static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb)
Definition aacdec.c:2284
static int count_paired_channels(uint8_t(*layout_map)[3], int tags, int pos, int current)
Definition aacdec.c:262
static av_cold int che_configure(AACDecContext *ac, enum ChannelPosition che_pos, int type, int id, int *channels)
Check for the channel element in the current channel position configuration.
Definition aacdec.c:142
static int decode_audio_specific_config_gb(AACDecContext *ac, AVCodecContext *avctx, OutputConfiguration *oc, GetBitContext *gb, int get_bit_alignment, int sync_extension)
Decode audio specific configuration; reference: table 1.13.
Definition aacdec.c:1121
#define overread_err
Definition aacdec.c:116
static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb, int ms_present)
Decode Mid/Side data; reference: table 4.54.
Definition aacdec.c:1766
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1222
static int decode_eld_specific_config(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, MPEG4AudioConfig *m4ac, int channel_config)
Definition aacdec.c:1052
int ff_aac_decode_ics(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, int common_window, int scale_flag)
Decode an individual_channel_stream payload; reference: table 4.44.
Definition aacdec.c:1815
ChannelElement * ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
Definition aacdec.c:625
static int seek_preroll(const MPEG4AudioConfig *m4ac)
The samples output after a seek before the output is reliable.
Definition aacdec.c:1308
static int decode_band_types(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb)
Decode band types (section_data payload); reference: table 4.46.
Definition aacdec.c:1575
static int frame_configure_elements(AVCodecContext *avctx)
Definition aacdec.c:183
static int decode_ga_specific_config(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, int get_bit_alignment, MPEG4AudioConfig *m4ac, int channel_config)
Decode GA "General Audio" specific configuration; reference: table 4.1.
Definition aacdec.c:977
static int decode_frame_ga(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
Definition aacdec.c:2366
static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext *gb)
Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4....
Definition aacdec.c:1955
static const AVClass decoder_class
Definition aacdec.c:2702
int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns, GetBitContext *gb, const IndividualChannelStream *ics)
Decode Temporal Noise Shaping data; reference: table 4.48.
Definition aacdec.c:1708
static void copy_oc(OutputConfiguration *dst, OutputConfiguration *src)
Definition aacdec.c:433
static int decode_pulses(Pulse *pulse, GetBitContext *gb, const uint16_t *swb_offset, int num_swb)
Decode pulse data; reference: table 4.7.
Definition aacdec.c:1681
static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t(*layout_map)[3], uint64_t *layout, int tags, int layer, int pos, int *current)
Definition aacdec.c:294
static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext *gb)
Decode dynamic range information; reference: table 4.52.
Definition aacdec.c:1974
static void decode_gain_control(SingleChannelElement *sce, GetBitContext *gb)
Definition aacdec.c:1780
static int push_output_configuration(AACDecContext *ac)
Save current output configuration if and only if it has been locked.
Definition aacdec.c:456
av_cold int ff_aac_decode_init(AVCodecContext *avctx)
Definition aacdec.c:1323
static int count_channels(uint8_t(*layout)[3], int tags)
Definition aacdec.c:118
static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac, uint8_t(*layout_map)[3], GetBitContext *gb, int byte_align_ref)
Decode program configuration element; reference: table 4.2.
Definition aacdec.c:822
static int decode_prediction(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Definition aacdec.c:1406
int ff_aac_set_default_channel_config(AACDecContext *ac, AVCodecContext *avctx, uint8_t(*layout_map)[3], int *tags, int channel_config)
Set up channel positions based on a default channel configuration as specified in table 1....
Definition aacdec.c:585
static void apply_channel_coupling(AACDecContext *ac, ChannelElement *cc, enum RawDataBlockType type, int elem_id, enum CouplingPoint coupling_point, void(*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
channel coupling transformation interface
Definition aacdec.c:2120
static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
Definition aacdec.c:2549
static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition aacdec.c:2605
static int decode_ics_info(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Decode Individual Channel Stream info; reference: table 4.6.
Definition aacdec.c:1446
static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t(*layout_map)[3], int offset, uint64_t left, uint64_t right, int pos, uint64_t *layout)
Definition aacdec.c:224
static int parse_adts_frame_header(AACDecContext *ac, GetBitContext *gb)
Definition aacdec.c:2222
static int decode_scalefactors(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, unsigned int global_gain)
Decode scalefactors; reference: table 4.47.
Definition aacdec.c:1622
static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len)
Definition aacdec.c:2021
static void decode_ltp(AACDecContext *ac, LongTermPrediction *ltp, GetBitContext *gb, uint8_t max_sfb)
Decode Long Term Prediction data; reference: table 4.xx.
Definition aacdec.c:1428
#define OFF(field)
Definition aacdec.c:2674
static int decode_cpe(AACDecContext *ac, GetBitContext *gb, ChannelElement *cpe)
Decode a channel_pair_element; reference: table 4.4.
Definition aacdec.c:1909
#define MDCT_INIT(s, fn, len, sval)
static void pop_output_configuration(AACDecContext *ac)
Restore the previous output configuration if and only if the current configuration is unlocked.
Definition aacdec.c:472
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
common internal api header.
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:98
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
Libavutil version macros.
uint8_t w
Definition llvidencdsp.c:39
Utility Preprocessor macros.
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
uint64_t layout
Memory handling functions.
uint32_t tag
Definition movenc.c:2256
int ff_mpeg4audio_get_config_gb(MPEG4AudioConfig *c, GetBitContext *gb, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a potentially unaligned GetBitContext to retrieve audio configura...
Definition mpeg4audio.c:92
const uint8_t ff_mpeg4audio_channels[15]
Definition mpeg4audio.c:59
@ AOT_AAC_LTP
Y Long Term Prediction.
Definition mpeg4audio.h:77
@ AOT_ER_AAC_LD
N Error Resilient Low Delay.
Definition mpeg4audio.h:95
@ AOT_ER_AAC_ELD
N Error Resilient Enhanced Low Delay.
Definition mpeg4audio.h:111
@ AOT_ER_AAC_LC
N Error Resilient Low Complexity.
Definition mpeg4audio.h:89
@ AOT_USAC
Y Unified Speech and Audio Coding.
Definition mpeg4audio.h:114
@ AOT_AAC_SCALABLE
N Scalable.
Definition mpeg4audio.h:79
@ AOT_ER_BSAC
N Error Resilient Bit-Sliced Arithmetic Coding.
Definition mpeg4audio.h:94
@ AOT_AAC_SSR
N (code in SoC repo) Scalable Sample Rate.
Definition mpeg4audio.h:76
@ AOT_AAC_LC
Y Low Complexity.
Definition mpeg4audio.h:75
@ AOT_ER_AAC_LTP
N Error Resilient Long Term Prediction.
Definition mpeg4audio.h:91
@ AOT_ER_AAC_SCALABLE
N Error Resilient Scalable.
Definition mpeg4audio.h:92
@ AOT_AAC_MAIN
Y Main.
Definition mpeg4audio.h:74
const char data[16]
Definition mxf.c:155
AVOptions.
const AVProfile ff_aac_profiles[]
Definition profiles.c:27
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition spdifenc.c:431
uint8_t num_aac_frames
Definition adts_header.h:43
uint32_t sample_rate
Definition adts_header.h:36
uint8_t sampling_index
Definition adts_header.h:41
main AAC decoding context
Definition aacdec.h:501
AACDecProc proc
Definition aacdec.h:506
AVTXContext * mdct96
Definition aacdec.h:536
av_tx_fn mdct960_fn
Definition aacdec.h:552
av_tx_fn mdct768_fn
Definition aacdec.h:551
av_tx_fn mdct128_fn
Definition aacdec.h:548
int random_state
Definition aacdec.h:559
AACDecDSP dsp
Definition aacdec.h:505
av_tx_fn mdct1024_fn
Definition aacdec.h:553
int tags_mapped
Definition aacdec.h:519
av_tx_fn mdct480_fn
Definition aacdec.h:549
AVTXContext * mdct128
Definition aacdec.h:538
AVFloatDSPContext * fdsp
Definition aacdec.h:557
struct AVCodecContext * avctx
Definition aacdec.h:503
AVTXContext * mdct960
Definition aacdec.h:542
AVTXContext * mdct768
Definition aacdec.h:541
AVTXContext * mdct480
Definition aacdec.h:539
av_tx_fn mdct120_fn
Definition aacdec.h:547
AVTXContext * mdct512
Definition aacdec.h:540
ChannelElement * che[4][MAX_ELEM_ID]
Definition aacdec.h:517
int warned_he_aac_mono
Definition aacdec.h:591
ChannelElement * tag_che_map[4][MAX_ELEM_ID]
Definition aacdec.h:518
int warned_remapping_once
Definition aacdec.h:520
DynamicRangeControl che_drc
Definition aacdec.h:511
enum AACOutputChannelOrder output_channel_order
Definition aacdec.h:578
unsigned warned_71_wide
Definition aacdec.h:589
av_tx_fn mdct96_fn
Definition aacdec.h:546
AVTXContext * mdct1024
Definition aacdec.h:543
int warned_num_aac_frames
Definition aacdec.h:588
struct AVFrame * frame
Definition aacdec.h:508
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition aacdec.h:575
SingleChannelElement * output_element[MAX_CHANNELS]
Points to each SingleChannelElement.
Definition aacdec.h:566
av_tx_fn mdct512_fn
Definition aacdec.h:550
OutputConfiguration oc[2]
Definition aacdec.h:587
int warned_gain_control
Definition aacdec.h:590
int force_dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition aacdec.h:574
AVTXContext * mdct_ltp
Definition aacdec.h:544
av_tx_fn mdct_ltp_fn
Definition aacdec.h:554
AVTXContext * mdct120
Definition aacdec.h:537
void(* apply_intensity_stereo)(AACDecContext *ac, ChannelElement *cpe, int ms_present)
Definition aacdec.h:471
void(* imdct_and_windowing_eld)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:493
void(* imdct_and_windowing_960)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:491
void(* imdct_and_windowing_ld)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:492
void(* apply_independent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition aacdec.h:485
void(* apply_prediction)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:480
void(* apply_tns)(void *_coef_param, TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition aacdec.h:474
void(* imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:489
void(* update_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:478
void(* apply_dependent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition aacdec.h:482
void(* apply_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:477
void(* clip_output)(AACDecContext *ac, ChannelElement *che, int type, int samples)
Definition aacdec.h:495
void(* apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
Definition aacdec.h:470
void(* dequant_scalefactors)(SingleChannelElement *sce)
Definition aacdec.h:468
void(* sbr_ctx_close)(ChannelElement *che)
Definition aacdec.h:460
void(* sbr_apply)(AACDecContext *ac, ChannelElement *che, int id_aac, int fl960, void *L, void *R)
Definition aacdec.h:458
int(* decode_cce)(AACDecContext *ac, GetBitContext *gb, ChannelElement *che)
Definition aacdec.h:453
int(* sbr_ctx_alloc_init)(AACDecContext *ac, ChannelElement **che, int id_aac)
Definition aacdec.h:455
int(* sbr_decode_extension)(AACDecContext *ac, ChannelElement *che, GetBitContext *gb, int crc, int cnt, int id_aac, int fl960)
Definition aacdec.h:456
void(* sbr_ctx_reset)(AACDecContext *ac, ChannelElement *che)
Definition aacdec.h:461
int(* decode_spectrum_and_dequant)(AACDecContext *ac, GetBitContext *gb, const Pulse *pulse, SingleChannelElement *sce)
Definition aacdec.h:448
AACUsacElemConfig elems[MAX_ELEM_ID]
Definition aacdec.h:401
struct AACUsacElemConfig::@253167335173233142055027357074223104054112322124 ext
uint8_t * pl_buf
Definition aacdec.h:385
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
int debug
debug
Definition avcodec.h:1398
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1381
int profile
profile
Definition avcodec.h:1646
int sample_rate
samples per second
Definition avcodec.h:1044
int seek_preroll
Number of samples to skip after a discontinuity.
Definition avcodec.h:1137
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1072
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1422
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition internal.h:125
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:559
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:723
int sample_rate
Sample rate of the audio data.
Definition frame.h:642
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:540
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
coupling parameters
Definition aacdec.h:203
int id_select[8]
element id
Definition aacdec.h:207
enum CouplingPoint coupling_point
The point during decoding at which coupling is applied.
Definition aacdec.h:204
int num_coupled
number of target elements
Definition aacdec.h:205
int ch_select[8]
[0] shared list of gains; [1] list of gains for right channel; [2] list of gains for left channel; [3...
Definition aacdec.h:208
enum RawDataBlockType type[8]
Type of channel element to be coupled - SCE or CPE.
Definition aacdec.h:206
channel element - generic struct for SCE/CPE/CCE/LFE
Definition aacdec.h:296
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition aacdec.h:300
SingleChannelElement ch[2]
Definition aacdec.h:302
uint8_t max_sfb_ste
(USAC) Maximum of both max_sfb values
Definition aacdec.h:299
ChannelCoupling coup
Definition aacdec.h:304
Dynamic Range Control - decoded from the bitstream but not processed further.
Definition aacdec.h:431
int interpolation_scheme
Indicates the interpolation scheme used in the SBR QMF domain.
Definition aacdec.h:437
int exclude_mask[MAX_CHANNELS]
Channels to be excluded from DRC processing.
Definition aacdec.h:435
int band_incr
Number of DRC bands greater than 1 having DRC info.
Definition aacdec.h:436
int dyn_rng_ctl[17]
DRC magnitude information.
Definition aacdec.h:434
int prog_ref_level
A reference level for the long-term program audio level for all channels combined.
Definition aacdec.h:439
int pce_instance_tag
Indicates with which program the DRC info is associated.
Definition aacdec.h:432
int band_top[17]
Indicates the top of the i-th DRC band in units of 4 spectral lines.
Definition aacdec.h:438
int dyn_rng_sgn[17]
DRC sign information; 0 - positive, 1 - negative.
Definition aacdec.h:433
Individual Channel Stream.
Definition aacdec.h:169
uint8_t max_sfb
number of scalefactor bands per group
Definition aacdec.h:170
int prev_num_window_groups
Previous frame's number of window groups.
Definition aacdec.h:174
int num_swb
number of scalefactor window bands
Definition aacdec.h:178
LongTermPrediction ltp
Definition aacdec.h:176
uint8_t prediction_used[41]
Definition aacdec.h:184
uint8_t group_len[8]
Definition aacdec.h:175
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sine window.
Definition aacdec.h:172
enum WindowSequence window_sequence[2]
Definition aacdec.h:171
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition aacdec.h:177
Long Term Prediction.
Definition aacdec.h:124
int8_t used[MAX_LTP_LONG_SFB]
Definition aacdec.h:128
int sbr
-1 implicit, 1 presence
Definition mpeg4audio.h:34
int ps
-1 implicit, 1 presence
Definition mpeg4audio.h:40
AACUSACConfig usac
Definition aacdec.h:425
enum OCStatus status
Definition aacdec.h:424
MPEG4AudioConfig m4ac
Definition aacdec.h:420
AVChannelLayout ch_layout
Definition aacdec.h:423
uint8_t layout_map[MAX_ELEM_ID *4][3]
Definition aacdec.h:421
Definition aac.h:103
int pos[4]
Definition aac.h:106
int amp[4]
Definition aac.h:107
int num_pulse
Definition aac.h:104
Single Channel Element - used for both SCE and LFE elements.
Definition aacdec.h:217
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition aacenc.h:123
int sfo[128]
scalefactor offsets
Definition aacdec.h:222
TemporalNoiseShaping tns
Definition aacdec.h:220
float ret_buf[2048]
PCM output buffer.
Definition aacenc.h:124
float * output
PCM output.
Definition aacdec.h:234
enum BandType band_type[128]
band types
Definition aacdec.h:221
IndividualChannelStream ics
Definition aacdec.h:218
Temporal Noise Shaping.
Definition aacdec.h:191
int direction[8][4]
Definition aacdec.h:195
int length[8][4]
Definition aacdec.h:194
int order[8][4]
Definition aacdec.h:196
float coef[8][4][TNS_MAX_ORDER]
Definition aacenc.h:105
uint8_t elem_id
Definition aacdec.c:220
uint64_t av_position
Definition aacdec.c:218
uint8_t aac_position
Definition aacdec.c:221
uint8_t syn_ele
Definition aacdec.c:219
Definition swscale.c:85
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
#define src
Definition vp8dsp.c:248
static const char * err(int ret)
Definition frame.c:35
#define height
Definition dsp.h:99
static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype, int wtype, float *in, float *prev, int ch)
Definition twinvq.c:329
int size
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
AVTXType
Definition tx.h:39
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
@ AV_TX_INT32_MDCT
Definition tx.h:70
const char * g
Definition vf_curves.c:128
int len
static double c[64]