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 }
154 if (type != TYPE_CCE) {
155 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
156 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
157 return AVERROR_INVALIDDATA;
158 }
159 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
160 if (type == TYPE_CPE ||
161 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
162 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
163 }
164 }
165 } else {
166 if (ac->che[type][id]) {
167 for (int i = 0; i < FF_ARRAY_ELEMS(ac->tag_che_map); i++) {
168 for (int j = 0; j < MAX_ELEM_ID; j++) {
169 if (ac->tag_che_map[i][j] == ac->che[type][id])
170 ac->tag_che_map[i][j] = NULL;
171 }
172 }
173 ac->proc.sbr_ctx_close(ac->che[type][id]);
174 }
175 av_freep(&ac->che[type][id]);
176 memset(ac->output_element, 0, sizeof(ac->output_element));
177 }
178 return 0;
179}
180
182{
183 AACDecContext *ac = avctx->priv_data;
184 int type, id, ch, ret;
185
186 /* set channel pointers to internal buffers by default */
187 for (type = 0; type < 4; type++) {
188 for (id = 0; id < MAX_ELEM_ID; id++) {
189 ChannelElement *che = ac->che[type][id];
190 if (che) {
191 che->ch[0].output = che->ch[0].ret_buf;
192 che->ch[1].output = che->ch[1].ret_buf;
193 }
194 }
195 }
196
197 /* get output buffer */
199 if (!avctx->ch_layout.nb_channels)
200 return 1;
201
202 ac->frame->nb_samples = 2048;
203 if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
204 return ret;
205
206 /* map output channel pointers to AVFrame data */
207 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
208 if (ac->output_element[ch])
209 ac->output_element[ch]->output = (void *)ac->frame->extended_data[ch];
210 }
211
212 return 0;
213}
214
216 uint64_t av_position;
217 uint8_t syn_ele;
218 uint8_t elem_id;
220};
221
222static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
223 uint8_t (*layout_map)[3], int offset, uint64_t left,
224 uint64_t right, int pos, uint64_t *layout)
225{
226 if (layout_map[offset][0] == TYPE_CPE) {
227 e2c_vec[offset] = (struct elem_to_channel) {
228 .av_position = left | right,
229 .syn_ele = TYPE_CPE,
230 .elem_id = layout_map[offset][1],
231 .aac_position = pos
232 };
233 if (e2c_vec[offset].av_position != UINT64_MAX)
234 *layout |= e2c_vec[offset].av_position;
235
236 return 1;
237 } else {
238 e2c_vec[offset] = (struct elem_to_channel) {
239 .av_position = left,
240 .syn_ele = TYPE_SCE,
241 .elem_id = layout_map[offset][1],
242 .aac_position = pos
243 };
244 e2c_vec[offset + 1] = (struct elem_to_channel) {
245 .av_position = right,
246 .syn_ele = TYPE_SCE,
247 .elem_id = layout_map[offset + 1][1],
248 .aac_position = pos
249 };
250 if (left != UINT64_MAX)
251 *layout |= left;
252
253 if (right != UINT64_MAX)
254 *layout |= right;
255
256 return 2;
257 }
258}
259
260static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
261 int current)
262{
263 int num_pos_channels = 0;
264 int first_cpe = 0;
265 int sce_parity = 0;
266 int i;
267 for (i = current; i < tags; i++) {
268 if (layout_map[i][2] != pos)
269 break;
270 if (layout_map[i][0] == TYPE_CPE) {
271 if (sce_parity) {
272 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
273 sce_parity = 0;
274 } else {
275 return -1;
276 }
277 }
278 num_pos_channels += 2;
279 first_cpe = 1;
280 } else {
281 num_pos_channels++;
282 sce_parity ^= (pos != AAC_CHANNEL_LFE);
283 }
284 }
285 if (sce_parity &&
286 (pos == AAC_CHANNEL_FRONT && first_cpe))
287 return -1;
288
289 return num_pos_channels;
290}
291
292static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t (*layout_map)[3],
293 uint64_t *layout, int tags, int layer, int pos, int *current)
294{
295 int i = *current, j = 0;
296 int nb_channels = count_paired_channels(layout_map, tags, pos, i);
297
298 if (nb_channels < 0 || nb_channels > 5)
299 return 0;
300
301 if (pos == AAC_CHANNEL_LFE) {
302 while (nb_channels) {
303 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE)
304 return -1;
305 e2c_vec[i] = (struct elem_to_channel) {
306 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][j],
307 .syn_ele = layout_map[i][0],
308 .elem_id = layout_map[i][1],
309 .aac_position = pos
310 };
311 *layout |= e2c_vec[i].av_position;
312 i++;
313 j++;
314 nb_channels--;
315 }
316 *current = i;
317
318 return 0;
319 }
320
321 while (nb_channels & 1) {
322 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_NONE)
323 return -1;
324 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_UNUSED)
325 break;
326 e2c_vec[i] = (struct elem_to_channel) {
327 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][0],
328 .syn_ele = layout_map[i][0],
329 .elem_id = layout_map[i][1],
330 .aac_position = pos
331 };
332 *layout |= e2c_vec[i].av_position;
333 i++;
334 nb_channels--;
335 }
336
337 j = (pos != AAC_CHANNEL_SIDE) && nb_channels <= 3 ? 3 : 1;
338 while (nb_channels >= 2) {
339 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE ||
340 ff_aac_channel_map[layer][pos - 1][j+1] == AV_CHAN_NONE)
341 return -1;
342 i += assign_pair(e2c_vec, layout_map, i,
343 1ULL << ff_aac_channel_map[layer][pos - 1][j],
344 1ULL << ff_aac_channel_map[layer][pos - 1][j+1],
345 pos, layout);
346 j += 2;
347 nb_channels -= 2;
348 }
349 while (nb_channels & 1) {
350 if (ff_aac_channel_map[layer][pos - 1][5] == AV_CHAN_NONE)
351 return -1;
352 e2c_vec[i] = (struct elem_to_channel) {
353 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][5],
354 .syn_ele = layout_map[i][0],
355 .elem_id = layout_map[i][1],
356 .aac_position = pos
357 };
358 *layout |= e2c_vec[i].av_position;
359 i++;
360 nb_channels--;
361 }
362 if (nb_channels)
363 return -1;
364
365 *current = i;
366
367 return 0;
368}
369
370static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
371{
372 int i, n, total_non_cc_elements;
373 struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
374 uint64_t layout = 0;
375
376 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
377 return 0;
378
379 for (n = 0, i = 0; n < 3 && i < tags; n++) {
380 int ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_FRONT, &i);
381 if (ret < 0)
382 return 0;
383 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_SIDE, &i);
384 if (ret < 0)
385 return 0;
386 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_BACK, &i);
387 if (ret < 0)
388 return 0;
389 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_LFE, &i);
390 if (ret < 0)
391 return 0;
392 }
393
394 total_non_cc_elements = n = i;
395
397 // For 22.2 reorder the result as needed
398 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
399 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
400 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
401 FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
402 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
403 FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
404 FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
405 FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelfth
406 FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelfth (final), TpSiL & TpSiR thirteenth (final)
407 } else {
408 // For everything else, utilize the AV channel position define as a
409 // stable sort.
410 do {
411 int next_n = 0;
412 for (i = 1; i < n; i++)
413 if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
414 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
415 next_n = i;
416 }
417 n = next_n;
418 } while (n > 0);
419
420 }
421
422 for (i = 0; i < total_non_cc_elements; i++) {
423 layout_map[i][0] = e2c_vec[i].syn_ele;
424 layout_map[i][1] = e2c_vec[i].elem_id;
425 layout_map[i][2] = e2c_vec[i].aac_position;
426 }
427
428 return layout;
429}
430
432{
433 int i;
434
435 for (i = 0; i < src->usac.nb_elems; i++) {
436 AACUsacElemConfig *src_e = &src->usac.elems[i];
437 AACUsacElemConfig *dst_e = &dst->usac.elems[i];
438 /* dst_e->ext.pl_buf is guaranteed to be set to src_e->ext.pl_buf
439 * upon this function's return */
440 av_refstruct_replace(&dst_e->ext.pl_buf, src_e->ext.pl_buf);
441 }
442
443 /* Unref all additional buffers to close leaks */
444 for (; i < dst->usac.nb_elems; i++)
445 av_refstruct_unref(&dst->usac.elems[i].ext.pl_buf);
446
447 /* Set all other properties */
448 *dst = *src;
449}
450
451/**
452 * Save current output configuration if and only if it has been locked.
453 */
455{
456 int pushed = 0;
457
458 if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
459 copy_oc(&ac->oc[0], &ac->oc[1]);
460 pushed = 1;
461 }
462 ac->oc[1].status = OC_NONE;
463 return pushed;
464}
465
466/**
467 * Restore the previous output configuration if and only if the current
468 * configuration is unlocked.
469 */
471{
472 if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
473 copy_oc(&ac->oc[1], &ac->oc[0]);
474
476 ac->oc[1].status, 0);
477 }
478}
479
480/**
481 * Configure output channel order based on the current program
482 * configuration element.
483 *
484 * @return Returns error status. 0 - OK, !0 - error
485 */
487 uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
488 enum OCStatus oc_type, int get_new_frame)
489{
490 AVCodecContext *avctx = ac->avctx;
491 int i, channels = 0, ret;
492 uint64_t layout = 0;
493 uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
494 uint8_t type_counts[TYPE_END] = { 0 };
495
496 if (get_new_frame && !ac->frame)
497 return AVERROR_INVALIDDATA;
498
499 if (ac->oc[1].layout_map != layout_map) {
500 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
501 ac->oc[1].layout_map_tags = tags;
502 }
503 for (i = 0; i < tags; i++) {
504 int type = layout_map[i][0];
505 int id = layout_map[i][1];
506 id_map[type][id] = type_counts[type]++;
507 if (id_map[type][id] >= MAX_ELEM_ID) {
508 avpriv_request_sample(ac->avctx, "Too large remapped id");
510 }
511 }
512 // Try to sniff a reasonable channel order, otherwise output the
513 // channels in the order the PCE declared them.
515 layout = sniff_channel_order(layout_map, tags);
516 for (i = 0; i < tags; i++) {
517 int type = layout_map[i][0];
518 int id = layout_map[i][1];
519 int iid = id_map[type][id];
520 int position = layout_map[i][2];
521 // Allocate or free elements depending on if they are in the
522 // current program configuration.
523 ret = che_configure(ac, position, type, iid, &channels);
524 if (ret < 0)
525 return ret;
526 ac->tag_che_map[type][id] = ac->che[type][iid];
527 }
528 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
529 if (layout == AV_CH_FRONT_CENTER) {
531 } else {
532 layout = 0;
533 }
534 }
535
537 if (layout)
539 else {
542 }
543
545 ac->oc[1].status = oc_type;
546
547 if (get_new_frame) {
548 if ((ret = frame_configure_elements(ac->avctx)) < 0)
549 return ret;
550 }
551
552 return 0;
553}
554
555static av_cold void flush(AVCodecContext *avctx)
556{
557 AACDecContext *ac= avctx->priv_data;
558 int type, i, j;
559
560 for (type = 3; type >= 0; type--) {
561 for (i = 0; i < MAX_ELEM_ID; i++) {
562 ChannelElement *che = ac->che[type][i];
563 if (che) {
564 for (j = 0; j <= 1; j++) {
565 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
566 }
567 }
568 }
569 }
570
571#if CONFIG_AAC_DECODER
572 ff_aac_usac_reset_state(ac, &ac->oc[1]);
573#endif
574}
575
576/**
577 * Set up channel positions based on a default channel configuration
578 * as specified in table 1.17.
579 *
580 * @return Returns error status. 0 - OK, !0 - error
581 */
583 uint8_t (*layout_map)[3],
584 int *tags,
585 int channel_config)
586{
587 if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
588 channel_config > 14) {
589 av_log(avctx, AV_LOG_ERROR,
590 "invalid default channel configuration (%d)\n",
591 channel_config);
592 return AVERROR_INVALIDDATA;
593 }
594 *tags = ff_tags_per_config[channel_config];
595 memcpy(layout_map, ff_aac_channel_layout_map[channel_config - 1],
596 *tags * sizeof(*layout_map));
597
598 /*
599 * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
600 * However, at least Nero AAC encoder encodes 7.1 streams using the default
601 * channel config 7, mapping the side channels of the original audio stream
602 * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
603 * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
604 * the incorrect streams as if they were correct (and as the encoder intended).
605 *
606 * As actual intended 7.1(wide) streams are very rare, default to assuming a
607 * 7.1 layout was intended.
608 */
609 if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
610 layout_map[2][2] = AAC_CHANNEL_BACK;
611
612 if (!ac || !ac->warned_71_wide++) {
613 av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
614 " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
615 " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
616 }
617 }
618
619 return 0;
620}
621
623{
624 /* For PCE based channel configurations map the channels solely based
625 * on tags. */
626 if (!ac->oc[1].m4ac.chan_config) {
627 return ac->tag_che_map[type][elem_id];
628 }
629 // Allow single CPE stereo files to be signalled with mono configuration.
630 if (!ac->tags_mapped && type == TYPE_CPE &&
631 ac->oc[1].m4ac.chan_config == 1) {
632 uint8_t layout_map[MAX_ELEM_ID*4][3];
633 int layout_map_tags;
635
636 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
637
638 if (ff_aac_set_default_channel_config(ac, ac->avctx, layout_map,
639 &layout_map_tags, 2) < 0)
640 return NULL;
641 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
642 OC_TRIAL_FRAME, 1) < 0)
643 return NULL;
644
645 ac->oc[1].m4ac.chan_config = 2;
646 ac->oc[1].m4ac.ps = 0;
647 }
648 // And vice-versa
649 if (!ac->tags_mapped && type == TYPE_SCE &&
650 ac->oc[1].m4ac.chan_config == 2) {
651 uint8_t layout_map[MAX_ELEM_ID * 4][3];
652 int layout_map_tags;
654
655 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
656
657 layout_map_tags = 2;
658 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
659 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
660 layout_map[0][1] = 0;
661 layout_map[1][1] = 1;
662 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
663 OC_TRIAL_FRAME, 1) < 0)
664 return NULL;
665
666 if (ac->oc[1].m4ac.sbr)
667 ac->oc[1].m4ac.ps = -1;
668 }
669 /* For indexed channel configurations map the channels solely based
670 * on position. */
671 switch (ac->oc[1].m4ac.chan_config) {
672 case 14:
673 if (ac->tags_mapped > 2 && ((type == TYPE_CPE && elem_id < 3) ||
674 (type == TYPE_LFE && elem_id < 1))) {
675 ac->tags_mapped++;
676 return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
677 }
679 case 13:
680 if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
681 (type == TYPE_SCE && elem_id < 6) ||
682 (type == TYPE_LFE && elem_id < 2))) {
683 ac->tags_mapped++;
684 return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
685 }
687 case 12:
688 case 7:
689 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
690 ac->tags_mapped++;
691 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
692 }
694 case 11:
695 if (ac->tags_mapped == 3 && type == TYPE_SCE) {
696 ac->tags_mapped++;
697 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
698 }
700 case 6:
701 /* Some streams incorrectly code 5.1 audio as
702 * SCE[0] CPE[0] CPE[1] SCE[1]
703 * instead of
704 * SCE[0] CPE[0] CPE[1] LFE[0].
705 * If we seem to have encountered such a stream, transfer
706 * the LFE[0] element to the SCE[1]'s mapping */
707 if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
708 if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
710 "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
711 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
713 }
714 ac->tags_mapped++;
715 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
716 }
718 case 5:
719 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
720 ac->tags_mapped++;
721 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
722 }
724 case 4:
725 /* Some streams incorrectly code 4.0 audio as
726 * SCE[0] CPE[0] LFE[0]
727 * instead of
728 * SCE[0] CPE[0] SCE[1].
729 * If we seem to have encountered such a stream, transfer
730 * the SCE[1] element to the LFE[0]'s mapping */
731 if (ac->tags_mapped == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
732 if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
734 "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
735 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
737 }
738 ac->tags_mapped++;
739 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
740 }
741 if (ac->tags_mapped == 2 &&
742 ac->oc[1].m4ac.chan_config == 4 &&
743 type == TYPE_SCE) {
744 ac->tags_mapped++;
745 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
746 }
748 case 3:
749 case 2:
750 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
751 type == TYPE_CPE) {
752 ac->tags_mapped++;
753 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
754 } else if (ac->tags_mapped == 1 && ac->oc[1].m4ac.chan_config == 2 &&
755 type == TYPE_SCE) {
756 ac->tags_mapped++;
757 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
758 }
760 case 1:
761 if (!ac->tags_mapped && type == TYPE_SCE) {
762 ac->tags_mapped++;
763 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
764 }
766 default:
767 return NULL;
768 }
769}
770
771/**
772 * Decode an array of 4 bit element IDs, optionally interleaved with a
773 * stereo/mono switching bit.
774 *
775 * @param type speaker type/position for these channels
776 */
777static void decode_channel_map(uint8_t layout_map[][3],
779 GetBitContext *gb, int n)
780{
781 while (n--) {
783 switch (type) {
785 case AAC_CHANNEL_BACK:
786 case AAC_CHANNEL_SIDE:
787 syn_ele = get_bits1(gb);
788 break;
789 case AAC_CHANNEL_CC:
790 skip_bits1(gb);
792 break;
793 case AAC_CHANNEL_LFE:
795 break;
796 default:
797 // AAC_CHANNEL_OFF has no channel map
798 av_assert0(0);
799 }
800 layout_map[0][0] = syn_ele;
801 layout_map[0][1] = get_bits(gb, 4);
802 layout_map[0][2] = type;
803 layout_map++;
804 }
805}
806
808 int reference_position) {
809 int n = (reference_position - get_bits_count(gb) & 7);
810 if (n)
811 skip_bits(gb, n);
812}
813
814/**
815 * Decode program configuration element; reference: table 4.2.
816 *
817 * @return Returns error status. 0 - OK, !0 - error
818 */
820 uint8_t (*layout_map)[3],
821 GetBitContext *gb, int byte_align_ref)
822{
823 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
824 int sampling_index;
825 int comment_len;
826 int tags;
827
828 skip_bits(gb, 2); // object_type
829
830 sampling_index = get_bits(gb, 4);
831 if (m4ac->sampling_index != sampling_index)
832 av_log(avctx, AV_LOG_WARNING,
833 "Sample rate index (%d) in program config element does not "
834 "match the sample rate index (%d) configured by the container.\n", sampling_index, m4ac->sampling_index);
835
836 num_front = get_bits(gb, 4);
837 num_side = get_bits(gb, 4);
838 num_back = get_bits(gb, 4);
839 num_lfe = get_bits(gb, 2);
840 num_assoc_data = get_bits(gb, 3);
841 num_cc = get_bits(gb, 4);
842
843 if (get_bits1(gb))
844 skip_bits(gb, 4); // mono_mixdown_tag
845 if (get_bits1(gb))
846 skip_bits(gb, 4); // stereo_mixdown_tag
847
848 if (get_bits1(gb))
849 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
850
851 if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
852 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
853 return -1;
854 }
855 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
856 tags = num_front;
857 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
858 tags += num_side;
859 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
860 tags += num_back;
861 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
862 tags += num_lfe;
863
864 skip_bits_long(gb, 4 * num_assoc_data);
865
866 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
867 tags += num_cc;
868
869 relative_align_get_bits(gb, byte_align_ref);
870
871 /* comment field, first byte is length */
872 comment_len = get_bits(gb, 8) * 8;
873 if (get_bits_left(gb) < comment_len) {
874 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
875 return AVERROR_INVALIDDATA;
876 }
877
878 // Height extension
879 int height_ext = 0;
880 if (comment_len >= 16 + (num_front * 2 + num_side * 2 + num_back * 2))
881 height_ext = show_bits(gb, 8) == 0xAC;
882 if (height_ext) {
883 uint8_t height_map[4 /* ChannelPosition */][16 /* Channel */]; // 0 == base, 1 == top, 2 == bottom.
884 uint8_t tag[6 /* ChannelPosition */ ][16 /* Channel */][3];
885 int i, invalid = 0, height_tags = 0;
886
887 skip_bits(gb, 8);
888
889 // Read height extension bits to height_map, which define which layer each element belongs to.
890 // Also make a copy of layout_map that will then be used to rearrange it.
891 for (i = 0; i < num_front; i++) {
892 int height = get_bits(gb, 2);
893 invalid |= height > 2;
894 height_map [AAC_CHANNEL_FRONT][i] = height;
895 memcpy(&tag[AAC_CHANNEL_FRONT][i], layout_map + height_tags, sizeof(*layout_map));
896 height_tags++;
897 }
898 for (i = 0; i < num_side; i++) {
899 int height = get_bits(gb, 2);
900 invalid |= height > 2;
901 height_map [AAC_CHANNEL_SIDE][i] = height;
902 memcpy(&tag[AAC_CHANNEL_SIDE][i], layout_map + height_tags, sizeof(*layout_map));
903 height_tags++;
904 }
905 for (i = 0; i < num_back; i++) {
906 int height = get_bits(gb, 2);
907 invalid |= height > 2;
908 height_map [AAC_CHANNEL_BACK][i] = height;
909 memcpy(&tag[AAC_CHANNEL_BACK][i], layout_map + height_tags, sizeof(*layout_map));
910 height_tags++;
911 }
912 for (i = 0; i < num_lfe; i++) {
913 memcpy(&tag[AAC_CHANNEL_LFE][i], layout_map + height_tags, sizeof(*layout_map));
914 height_tags++;
915 }
916 for (i = 0; i < num_cc; i++) {
917 memcpy(&tag[AAC_CHANNEL_CC][i], layout_map + height_tags, sizeof(*layout_map));
918 height_tags++;
919 }
920 av_assert0(height_tags == tags);
921
922 if (!invalid) {
923 height_tags = 0;
924 // For each height layer, check that an element belongs to it and copy it back to layout_map.
925 // We need to take into account LFE and CC elements that may be present.
926 for (i = 0; i < 3; i++) {
927 for (int j = 0; j < num_front; j++) {
928 if (height_map[AAC_CHANNEL_FRONT][j] == i) {
929 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_FRONT][j], sizeof(*layout_map));
930 height_tags++;
931 }
932 }
933 for (int j = 0; j < num_side; j++) {
934 if (height_map[AAC_CHANNEL_SIDE][j] == i) {
935 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_SIDE][j], sizeof(*layout_map));
936 height_tags++;
937 }
938 }
939 for (int j = 0; j < num_back; j++) {
940 if (height_map[AAC_CHANNEL_BACK][j] == i) {
941 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_BACK][j], sizeof(*layout_map));
942 height_tags++;
943 }
944 }
945 if (i == 0) { // Base height, copy LFE and CC elements before moving to Top and Bottom
946 for (int j = 0; j < num_lfe; j++) {
947 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_LFE][j], sizeof(*layout_map));
948 height_tags++;
949 }
950 for (int j = 0; j < num_cc; j++) {
951 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_CC][j], sizeof(*layout_map));
952 height_tags++;
953 }
954 }
955 }
956 av_assert0(height_tags == tags);
957 }
958
959 comment_len -= 8 + (num_front * 2 + num_side * 2 + num_back * 2);
960 }
961
962 skip_bits_long(gb, comment_len);
963 return tags;
964}
965
966/**
967 * Decode GA "General Audio" specific configuration; reference: table 4.1.
968 *
969 * @param ac pointer to AACDecContext, may be null
970 * @param avctx pointer to AVCCodecContext, used for logging
971 *
972 * @return Returns error status. 0 - OK, !0 - error
973 */
975 GetBitContext *gb,
976 int get_bit_alignment,
977 MPEG4AudioConfig *m4ac,
978 int channel_config)
979{
980 int extension_flag, ret, ep_config, res_flags;
981 uint8_t layout_map[MAX_ELEM_ID*4][3];
982 int tags = 0;
983
984 m4ac->frame_length_short = get_bits1(gb);
985
986 if (get_bits1(gb)) // dependsOnCoreCoder
987 skip_bits(gb, 14); // coreCoderDelay
988 extension_flag = get_bits1(gb);
989
990 if (m4ac->object_type == AOT_AAC_SCALABLE ||
992 skip_bits(gb, 3); // layerNr
993
994 if (channel_config == 0) {
995 skip_bits(gb, 4); // element_instance_tag
996 tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
997 if (tags < 0)
998 return tags;
999 } else {
1000 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1001 &tags, channel_config)))
1002 return ret;
1003 }
1004
1005 if (count_channels(layout_map, tags) > 1) {
1006 m4ac->ps = 0;
1007 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
1008 m4ac->ps = 1;
1009
1010 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1011 return ret;
1012
1013 if (extension_flag) {
1014 switch (m4ac->object_type) {
1015 case AOT_ER_BSAC:
1016 skip_bits(gb, 5); // numOfSubFrame
1017 skip_bits(gb, 11); // layer_length
1018 break;
1019 case AOT_ER_AAC_LC:
1020 case AOT_ER_AAC_LTP:
1022 case AOT_ER_AAC_LD:
1023 res_flags = get_bits(gb, 3);
1024 if (res_flags) {
1026 "AAC data resilience (flags %x)",
1027 res_flags);
1028 return AVERROR_PATCHWELCOME;
1029 }
1030 break;
1031 }
1032 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
1033 }
1034 switch (m4ac->object_type) {
1035 case AOT_ER_AAC_LC:
1036 case AOT_ER_AAC_LTP:
1038 case AOT_ER_AAC_LD:
1039 ep_config = get_bits(gb, 2);
1040 if (ep_config) {
1042 "epConfig %d", ep_config);
1043 return AVERROR_PATCHWELCOME;
1044 }
1045 }
1046 return 0;
1047}
1048
1050 GetBitContext *gb,
1051 MPEG4AudioConfig *m4ac,
1052 int channel_config)
1053{
1054 int ret, ep_config, res_flags;
1055 uint8_t layout_map[MAX_ELEM_ID*4][3];
1056 int tags = 0;
1057 const int ELDEXT_TERM = 0;
1058
1059 m4ac->ps = 0;
1060 m4ac->sbr = 0;
1061 m4ac->frame_length_short = get_bits1(gb);
1062
1063 res_flags = get_bits(gb, 3);
1064 if (res_flags) {
1066 "AAC data resilience (flags %x)",
1067 res_flags);
1068 return AVERROR_PATCHWELCOME;
1069 }
1070
1071 if (get_bits1(gb)) { // ldSbrPresentFlag
1073 "Low Delay SBR");
1074 return AVERROR_PATCHWELCOME;
1075 }
1076
1077 while (get_bits(gb, 4) != ELDEXT_TERM) {
1078 int len = get_bits(gb, 4);
1079 if (len == 15)
1080 len += get_bits(gb, 8);
1081 if (len == 15 + 255)
1082 len += get_bits(gb, 16);
1083 if (get_bits_left(gb) < len * 8 + 4) {
1085 return AVERROR_INVALIDDATA;
1086 }
1087 skip_bits_long(gb, 8 * len);
1088 }
1089
1090 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1091 &tags, channel_config)))
1092 return ret;
1093
1094 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1095 return ret;
1096
1097 ep_config = get_bits(gb, 2);
1098 if (ep_config) {
1100 "epConfig %d", ep_config);
1101 return AVERROR_PATCHWELCOME;
1102 }
1103 return 0;
1104}
1105
1106/**
1107 * Decode audio specific configuration; reference: table 1.13.
1108 *
1109 * @param ac pointer to AACDecContext, may be null
1110 * @param avctx pointer to AVCCodecContext, used for logging
1111 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1112 * @param gb buffer holding an audio specific config
1113 * @param get_bit_alignment relative alignment for byte align operations
1114 * @param sync_extension look for an appended sync extension
1115 *
1116 * @return Returns error status or number of consumed bits. <0 - error
1117 */
1119 AVCodecContext *avctx,
1121 GetBitContext *gb,
1122 int get_bit_alignment,
1123 int sync_extension)
1124{
1125 int i, ret;
1126 GetBitContext gbc = *gb;
1127 MPEG4AudioConfig *m4ac = &oc->m4ac;
1128 MPEG4AudioConfig m4ac_bak = *m4ac;
1129
1130 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1131 *m4ac = m4ac_bak;
1132 return AVERROR_INVALIDDATA;
1133 }
1134
1135 if (m4ac->sampling_index > 12) {
1136 av_log(avctx, AV_LOG_ERROR,
1137 "invalid sampling rate index %d\n",
1138 m4ac->sampling_index);
1139 *m4ac = m4ac_bak;
1140 return AVERROR_INVALIDDATA;
1141 }
1142 if (m4ac->object_type == AOT_ER_AAC_LD &&
1143 (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
1144 av_log(avctx, AV_LOG_ERROR,
1145 "invalid low delay sampling rate index %d\n",
1146 m4ac->sampling_index);
1147 *m4ac = m4ac_bak;
1148 return AVERROR_INVALIDDATA;
1149 }
1150
1151 skip_bits_long(gb, i);
1152
1153 switch (m4ac->object_type) {
1154 case AOT_AAC_MAIN:
1155 case AOT_AAC_LC:
1156 case AOT_AAC_SSR:
1157 case AOT_AAC_LTP:
1158 case AOT_ER_AAC_LC:
1159 case AOT_ER_AAC_LD:
1160 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1161 &oc->m4ac, m4ac->chan_config)) < 0)
1162 return ret;
1163 break;
1164 case AOT_ER_AAC_ELD:
1165 if ((ret = decode_eld_specific_config(ac, avctx, gb,
1166 &oc->m4ac, m4ac->chan_config)) < 0)
1167 return ret;
1168 break;
1169#if CONFIG_AAC_DECODER
1170 case AOT_USAC:
1171 if ((ret = ff_aac_usac_config_decode(ac, avctx, gb,
1172 oc, m4ac->chan_config)) < 0)
1173 return ret;
1174 break;
1175#endif
1176 default:
1178 "Audio object type %s%d",
1179 m4ac->sbr == 1 ? "SBR+" : "",
1180 m4ac->object_type);
1181 return AVERROR(ENOSYS);
1182 }
1183
1184 ff_dlog(avctx,
1185 "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1186 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1187 m4ac->sample_rate, m4ac->sbr,
1188 m4ac->ps);
1189
1190 return get_bits_count(gb);
1191}
1192
1194 AVCodecContext *avctx,
1196 const uint8_t *data, int64_t bit_size,
1197 int sync_extension)
1198{
1199 int i, ret;
1200 GetBitContext gb;
1201
1202 if (bit_size < 0 || bit_size > INT_MAX) {
1203 av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1204 return AVERROR_INVALIDDATA;
1205 }
1206
1207 ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1208 for (i = 0; i < bit_size >> 3; i++)
1209 ff_dlog(avctx, "%02x ", data[i]);
1210 ff_dlog(avctx, "\n");
1211
1212 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1213 return ret;
1214
1215 return decode_audio_specific_config_gb(ac, avctx, oc, &gb, 0,
1216 sync_extension);
1217}
1218
1220{
1221 AACDecContext *ac = avctx->priv_data;
1222
1223 for (int i = 0; i < 2; i++) {
1224 OutputConfiguration *oc = &ac->oc[i];
1225 AACUSACConfig *usac = &oc->usac;
1226 for (int j = 0; j < usac->nb_elems; j++) {
1227 AACUsacElemConfig *ec = &usac->elems[j];
1229 }
1230
1232 }
1233
1234 for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {
1235 for (int i = 0; i < MAX_ELEM_ID; i++) {
1236 if (ac->che[type][i]) {
1237 ac->proc.sbr_ctx_close(ac->che[type][i]);
1238 av_freep(&ac->che[type][i]);
1239 }
1240 }
1241 }
1242
1243 av_tx_uninit(&ac->mdct96);
1244 av_tx_uninit(&ac->mdct120);
1245 av_tx_uninit(&ac->mdct128);
1246 av_tx_uninit(&ac->mdct480);
1247 av_tx_uninit(&ac->mdct512);
1248 av_tx_uninit(&ac->mdct768);
1249 av_tx_uninit(&ac->mdct960);
1250 av_tx_uninit(&ac->mdct1024);
1251 av_tx_uninit(&ac->mdct_ltp);
1252
1253 // Compiler will optimize this branch away.
1254 if (ac->is_fixed)
1255 av_freep(&ac->RENAME_FIXED(fdsp));
1256 else
1257 av_freep(&ac->fdsp);
1258
1259 return 0;
1260}
1261
1263{
1264 AACDecContext *ac = avctx->priv_data;
1265 int is_fixed = ac->is_fixed, ret;
1266 float scale_fixed, scale_float;
1267 const float *const scalep = is_fixed ? &scale_fixed : &scale_float;
1268 enum AVTXType tx_type = is_fixed ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT;
1269
1270#define MDCT_INIT(s, fn, len, sval) \
1271 scale_fixed = (sval) * 128.0f; \
1272 scale_float = (sval) / 32768.0f; \
1273 ret = av_tx_init(&s, &fn, tx_type, 1, len, scalep, 0); \
1274 if (ret < 0) \
1275 return ret
1276
1277 MDCT_INIT(ac->mdct96, ac->mdct96_fn, 96, 1.0/96);
1278 MDCT_INIT(ac->mdct120, ac->mdct120_fn, 120, 1.0/120);
1279 MDCT_INIT(ac->mdct128, ac->mdct128_fn, 128, 1.0/128);
1280 MDCT_INIT(ac->mdct480, ac->mdct480_fn, 480, 1.0/480);
1281 MDCT_INIT(ac->mdct512, ac->mdct512_fn, 512, 1.0/512);
1282 MDCT_INIT(ac->mdct768, ac->mdct768_fn, 768, 1.0/768);
1283 MDCT_INIT(ac->mdct960, ac->mdct960_fn, 960, 1.0/960);
1284 MDCT_INIT(ac->mdct1024, ac->mdct1024_fn, 1024, 1.0/1024);
1285#undef MDCT_INIT
1286
1287 /* LTP forward MDCT */
1288 scale_fixed = -1.0;
1289 scale_float = -32786.0*2 + 36;
1290 ret = av_tx_init(&ac->mdct_ltp, &ac->mdct_ltp_fn, tx_type, 0, 1024, scalep, 0);
1291 if (ret < 0)
1292 return ret;
1293
1294 return 0;
1295}
1296
1298{
1299 AACDecContext *ac = avctx->priv_data;
1300 int ret;
1301
1302 if (avctx->sample_rate > 96000)
1303 return AVERROR_INVALIDDATA;
1304
1306
1307 ac->avctx = avctx;
1308 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1309
1310 if (avctx->extradata_size > 0) {
1311 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
1312 avctx->extradata,
1313 avctx->extradata_size * 8LL,
1314 1)) < 0)
1315 return ret;
1316 } else {
1317 int sr, i;
1318 uint8_t layout_map[MAX_ELEM_ID*4][3];
1319 int layout_map_tags;
1320
1322 ac->oc[1].m4ac.sampling_index = sr;
1323 ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
1324 ac->oc[1].m4ac.sbr = -1;
1325 ac->oc[1].m4ac.ps = -1;
1326
1327 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1329 break;
1331 i = 0;
1332 }
1333 ac->oc[1].m4ac.chan_config = i;
1334
1335 if (ac->oc[1].m4ac.chan_config) {
1336 ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1337 &layout_map_tags,
1338 ac->oc[1].m4ac.chan_config);
1339 if (!ret)
1340 ff_aac_output_configure(ac, layout_map, layout_map_tags,
1341 OC_GLOBAL_HDR, 0);
1342 else if (avctx->err_recognition & AV_EF_EXPLODE)
1343 return AVERROR_INVALIDDATA;
1344 }
1345 }
1346
1347 if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
1348 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1349 return AVERROR_INVALIDDATA;
1350 }
1351
1352 ac->random_state = 0x1f2e3d4c;
1353
1354 return init_dsp(avctx);
1355}
1356
1357/**
1358 * Skip data_stream_element; reference: table 4.10.
1359 */
1361{
1362 int byte_align = get_bits1(gb);
1363 int count = get_bits(gb, 8);
1364 if (count == 255)
1365 count += get_bits(gb, 8);
1366 if (byte_align)
1367 align_get_bits(gb);
1368
1369 if (get_bits_left(gb) < 8 * count) {
1370 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1371 return AVERROR_INVALIDDATA;
1372 }
1373 skip_bits_long(gb, 8 * count);
1374 return 0;
1375}
1376
1378 GetBitContext *gb)
1379{
1380 int sfb;
1381 if (get_bits1(gb)) {
1382 ics->predictor_reset_group = get_bits(gb, 5);
1383 if (ics->predictor_reset_group == 0 ||
1384 ics->predictor_reset_group > 30) {
1386 "Invalid Predictor Reset Group.\n");
1387 return AVERROR_INVALIDDATA;
1388 }
1389 }
1390 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1391 ics->prediction_used[sfb] = get_bits1(gb);
1392 }
1393 return 0;
1394}
1395
1396/**
1397 * Decode Long Term Prediction data; reference: table 4.xx.
1398 */
1400 GetBitContext *gb, uint8_t max_sfb)
1401{
1402 int sfb;
1403
1404 ltp->lag = get_bits(gb, 11);
1405 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1406 ltp->coef_fixed = Q30(ff_ltp_coef[get_bits(gb, 3)]);
1407 else if (CONFIG_AAC_DECODER)
1408 ltp->coef = ff_ltp_coef[get_bits(gb, 3)];
1409
1410 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1411 ltp->used[sfb] = get_bits1(gb);
1412}
1413
1414/**
1415 * Decode Individual Channel Stream info; reference: table 4.6.
1416 */
1418 GetBitContext *gb)
1419{
1420 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1421 const int aot = m4ac->object_type;
1422 const int sampling_index = m4ac->sampling_index;
1423 int ret_fail = AVERROR_INVALIDDATA;
1424
1425 if (aot != AOT_ER_AAC_ELD) {
1426 if (get_bits1(gb)) {
1427 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1429 return AVERROR_INVALIDDATA;
1430 }
1431 ics->window_sequence[1] = ics->window_sequence[0];
1432 ics->window_sequence[0] = get_bits(gb, 2);
1433 if (aot == AOT_ER_AAC_LD &&
1436 "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1437 "window sequence %d found.\n", ics->window_sequence[0]);
1439 return AVERROR_INVALIDDATA;
1440 }
1441 ics->use_kb_window[1] = ics->use_kb_window[0];
1442 ics->use_kb_window[0] = get_bits1(gb);
1443 }
1445 ics->num_window_groups = 1;
1446 ics->group_len[0] = 1;
1447 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1448 int i;
1449 ics->max_sfb = get_bits(gb, 4);
1450 for (i = 0; i < 7; i++) {
1451 if (get_bits1(gb)) {
1452 ics->group_len[ics->num_window_groups - 1]++;
1453 } else {
1454 ics->num_window_groups++;
1455 ics->group_len[ics->num_window_groups - 1] = 1;
1456 }
1457 }
1458 ics->num_windows = 8;
1459 if (m4ac->frame_length_short) {
1460 ics->swb_offset = ff_swb_offset_120[sampling_index];
1461 ics->num_swb = ff_aac_num_swb_120[sampling_index];
1462 } else {
1463 ics->swb_offset = ff_swb_offset_128[sampling_index];
1464 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1465 }
1466 ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1467 ics->predictor_present = 0;
1468 } else {
1469 ics->max_sfb = get_bits(gb, 6);
1470 ics->num_windows = 1;
1471 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1472 if (m4ac->frame_length_short) {
1473 ics->swb_offset = ff_swb_offset_480[sampling_index];
1474 ics->num_swb = ff_aac_num_swb_480[sampling_index];
1475 ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1476 } else {
1477 ics->swb_offset = ff_swb_offset_512[sampling_index];
1478 ics->num_swb = ff_aac_num_swb_512[sampling_index];
1479 ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1480 }
1481 if (!ics->num_swb || !ics->swb_offset) {
1482 ret_fail = AVERROR_BUG;
1483 goto fail;
1484 }
1485 } else {
1486 if (m4ac->frame_length_short) {
1487 ics->num_swb = ff_aac_num_swb_960[sampling_index];
1488 ics->swb_offset = ff_swb_offset_960[sampling_index];
1489 } else {
1490 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1491 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1492 }
1493 ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1494 }
1495 if (aot != AOT_ER_AAC_ELD) {
1496 ics->predictor_present = get_bits1(gb);
1497 ics->predictor_reset_group = 0;
1498 }
1499 if (ics->predictor_present) {
1500 if (aot == AOT_AAC_MAIN) {
1501 if (decode_prediction(ac, ics, gb)) {
1502 goto fail;
1503 }
1504 } else if (aot == AOT_AAC_LC ||
1505 aot == AOT_ER_AAC_LC) {
1507 "Prediction is not allowed in AAC-LC.\n");
1508 goto fail;
1509 } else {
1510 if (aot == AOT_ER_AAC_LD) {
1512 "LTP in ER AAC LD not yet implemented.\n");
1513 ret_fail = AVERROR_PATCHWELCOME;
1514 goto fail;
1515 }
1516 if ((ics->ltp.present = get_bits(gb, 1)))
1517 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1518 }
1519 }
1520 }
1521
1522 if (ics->max_sfb > ics->num_swb) {
1524 "Number of scalefactor bands in group (%d) "
1525 "exceeds limit (%d).\n",
1526 ics->max_sfb, ics->num_swb);
1527 goto fail;
1528 }
1529
1530 return 0;
1531fail:
1532 ics->max_sfb = 0;
1533 return ret_fail;
1534}
1535
1536/**
1537 * Decode band types (section_data payload); reference: table 4.46.
1538 *
1539 * @param band_type array of the used band type
1540 * @param band_type_run_end array of the last scalefactor band of a band type run
1541 *
1542 * @return Returns error status. 0 - OK, !0 - error
1543 */
1545 GetBitContext *gb)
1546{
1547 IndividualChannelStream *ics = &sce->ics;
1548 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1549
1550 for (int g = 0; g < ics->num_window_groups; g++) {
1551 int k = 0;
1552 while (k < ics->max_sfb) {
1553 uint8_t sect_end = k;
1554 int sect_len_incr;
1555 int sect_band_type = get_bits(gb, 4);
1556 if (sect_band_type == 12) {
1557 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1558 return AVERROR_INVALIDDATA;
1559 }
1560 do {
1561 sect_len_incr = get_bits(gb, bits);
1562 sect_end += sect_len_incr;
1563 if (get_bits_left(gb) < 0) {
1564 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1565 return AVERROR_INVALIDDATA;
1566 }
1567 if (sect_end > ics->max_sfb) {
1569 "Number of bands (%d) exceeds limit (%d).\n",
1570 sect_end, ics->max_sfb);
1571 return AVERROR_INVALIDDATA;
1572 }
1573 } while (sect_len_incr == (1 << bits) - 1);
1574 for (; k < sect_end; k++)
1575 sce->band_type[g*ics->max_sfb + k] = sect_band_type;
1576 }
1577 }
1578 return 0;
1579}
1580
1581/**
1582 * Decode scalefactors; reference: table 4.47.
1583 *
1584 * @param global_gain first scalefactor value as scalefactors are differentially coded
1585 * @param band_type array of the used band type
1586 * @param band_type_run_end array of the last scalefactor band of a band type run
1587 * @param sf array of scalefactors or intensity stereo positions
1588 *
1589 * @return Returns error status. 0 - OK, !0 - error
1590 */
1592 GetBitContext *gb, unsigned int global_gain)
1593{
1594 IndividualChannelStream *ics = &sce->ics;
1595 int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1596 int clipped_offset;
1597 int noise_flag = 1;
1598
1599 for (int g = 0; g < ics->num_window_groups; g++) {
1600 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1601 switch (sce->band_type[g*ics->max_sfb + sfb]) {
1602 case ZERO_BT:
1603 sce->sfo[g*ics->max_sfb + sfb] = 0;
1604 break;
1605 case INTENSITY_BT: /* fallthrough */
1606 case INTENSITY_BT2:
1608 clipped_offset = av_clip(offset[2], -155, 100);
1609 if (offset[2] != clipped_offset) {
1611 "If you heard an audible artifact, there may be a bug in the decoder. "
1612 "Clipped intensity stereo position (%d -> %d)",
1613 offset[2], clipped_offset);
1614 }
1615 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset - 100;
1616 break;
1617 case NOISE_BT:
1618 if (noise_flag-- > 0)
1620 else
1622 clipped_offset = av_clip(offset[1], -100, 155);
1623 if (offset[1] != clipped_offset) {
1625 "If you heard an audible artifact, there may be a bug in the decoder. "
1626 "Clipped noise gain (%d -> %d)",
1627 offset[1], clipped_offset);
1628 }
1629 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset;
1630 break;
1631 default:
1633 if (offset[0] > 255U) {
1635 "Scalefactor (%d) out of range.\n", offset[0]);
1636 return AVERROR_INVALIDDATA;
1637 }
1638 sce->sfo[g*ics->max_sfb + sfb] = offset[0] - 100;
1639 break;
1640 }
1641 }
1642 }
1643
1644 return 0;
1645}
1646
1647/**
1648 * Decode pulse data; reference: table 4.7.
1649 */
1650static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1651 const uint16_t *swb_offset, int num_swb)
1652{
1653 int i, pulse_swb;
1654 pulse->num_pulse = get_bits(gb, 2) + 1;
1655 pulse_swb = get_bits(gb, 6);
1656 if (pulse_swb >= num_swb)
1657 return -1;
1658 pulse->pos[0] = swb_offset[pulse_swb];
1659 pulse->pos[0] += get_bits(gb, 5);
1660 if (pulse->pos[0] >= swb_offset[num_swb])
1661 return -1;
1662 pulse->amp[0] = get_bits(gb, 4);
1663 for (i = 1; i < pulse->num_pulse; i++) {
1664 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1665 if (pulse->pos[i] >= swb_offset[num_swb])
1666 return -1;
1667 pulse->amp[i] = get_bits(gb, 4);
1668 }
1669 return 0;
1670}
1671
1672/**
1673 * Decode Temporal Noise Shaping data; reference: table 4.48.
1674 *
1675 * @return Returns error status. 0 - OK, !0 - error
1676 */
1678 GetBitContext *gb, const IndividualChannelStream *ics)
1679{
1680 int tns_max_order = INT32_MAX;
1681 const int is_usac = ac->oc[1].m4ac.object_type == AOT_USAC;
1682 int w, filt, i, coef_len, coef_res, coef_compress;
1683 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1684
1685 /* USAC doesn't seem to have a limit */
1686 if (!is_usac)
1687 tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1688
1689 for (w = 0; w < ics->num_windows; w++) {
1690 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1691 coef_res = get_bits1(gb);
1692
1693 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1694 int tmp2_idx;
1695 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1696
1697 if (is_usac)
1698 tns->order[w][filt] = get_bits(gb, 4 - is8);
1699 else
1700 tns->order[w][filt] = get_bits(gb, 5 - (2 * is8));
1701
1702 if (tns->order[w][filt] > tns_max_order) {
1704 "TNS filter order %d is greater than maximum %d.\n",
1705 tns->order[w][filt], tns_max_order);
1706 tns->order[w][filt] = 0;
1707 return AVERROR_INVALIDDATA;
1708 }
1709 if (tns->order[w][filt]) {
1710 tns->direction[w][filt] = get_bits1(gb);
1711 coef_compress = get_bits1(gb);
1712 coef_len = coef_res + 3 - coef_compress;
1713 tmp2_idx = 2 * coef_compress + coef_res;
1714
1715 for (i = 0; i < tns->order[w][filt]; i++) {
1716 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1717 tns->coef_fixed[w][filt][i] = Q31(ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]);
1718 else if (CONFIG_AAC_DECODER)
1719 tns->coef[w][filt][i] = ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1720 }
1721 }
1722 }
1723 }
1724 }
1725 return 0;
1726}
1727
1728/**
1729 * Decode Mid/Side data; reference: table 4.54.
1730 *
1731 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1732 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1733 * [3] reserved for scalable AAC
1734 */
1736 int ms_present)
1737{
1738 int idx;
1739 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1740 cpe->max_sfb_ste = cpe->ch[0].ics.max_sfb;
1741 if (ms_present == 1) {
1742 for (idx = 0; idx < max_idx; idx++)
1743 cpe->ms_mask[idx] = get_bits1(gb);
1744 } else if (ms_present == 2) {
1745 memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1746 }
1747}
1748
1750{
1751 // wd_num, wd_test, aloc_size
1752 static const uint8_t gain_mode[4][3] = {
1753 {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
1754 {2, 1, 2}, // LONG_START_SEQUENCE,
1755 {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
1756 {2, 1, 5}, // LONG_STOP_SEQUENCE
1757 };
1758
1759 const int mode = sce->ics.window_sequence[0];
1760 uint8_t bd, wd, ad;
1761
1762 // FIXME: Store the gain control data on |sce| and do something with it.
1763 uint8_t max_band = get_bits(gb, 2);
1764 for (bd = 0; bd < max_band; bd++) {
1765 for (wd = 0; wd < gain_mode[mode][0]; wd++) {
1766 uint8_t adjust_num = get_bits(gb, 3);
1767 for (ad = 0; ad < adjust_num; ad++) {
1768 skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
1769 ? 4
1770 : gain_mode[mode][2]));
1771 }
1772 }
1773 }
1774}
1775
1776/**
1777 * Decode an individual_channel_stream payload; reference: table 4.44.
1778 *
1779 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1780 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1781 *
1782 * @return Returns error status. 0 - OK, !0 - error
1783 */
1785 GetBitContext *gb, int common_window, int scale_flag)
1786{
1787 Pulse pulse;
1788 TemporalNoiseShaping *tns = &sce->tns;
1789 IndividualChannelStream *ics = &sce->ics;
1790 int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1791 int ret;
1792
1793 eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1794 er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1795 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1796 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1798
1799 /* This assignment is to silence a GCC warning about the variable being used
1800 * uninitialized when in fact it always is.
1801 */
1802 pulse.num_pulse = 0;
1803
1804 global_gain = get_bits(gb, 8);
1805
1806 if (!common_window && !scale_flag) {
1807 ret = decode_ics_info(ac, ics, gb);
1808 if (ret < 0)
1809 goto fail;
1810 }
1811
1812 if ((ret = decode_band_types(ac, sce, gb)) < 0)
1813 goto fail;
1814 if ((ret = decode_scalefactors(ac, sce, gb, global_gain)) < 0)
1815 goto fail;
1816
1817 ac->dsp.dequant_scalefactors(sce);
1818
1819 pulse_present = 0;
1820 if (!scale_flag) {
1821 if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1822 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1824 "Pulse tool not allowed in eight short sequence.\n");
1825 ret = AVERROR_INVALIDDATA;
1826 goto fail;
1827 }
1828 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1830 "Pulse data corrupt or invalid.\n");
1831 ret = AVERROR_INVALIDDATA;
1832 goto fail;
1833 }
1834 }
1835 tns->present = get_bits1(gb);
1836 if (tns->present && !er_syntax) {
1837 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1838 if (ret < 0)
1839 goto fail;
1840 }
1841 if (!eld_syntax && get_bits1(gb)) {
1842 decode_gain_control(sce, gb);
1843 if (!ac->warned_gain_control) {
1844 avpriv_report_missing_feature(ac->avctx, "Gain control");
1845 ac->warned_gain_control = 1;
1846 }
1847 }
1848 // I see no textual basis in the spec for this occurring after SSR gain
1849 // control, but this is what both reference and real implementations do
1850 if (tns->present && er_syntax) {
1851 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1852 if (ret < 0)
1853 goto fail;
1854 }
1855 }
1856
1857 ret = ac->proc.decode_spectrum_and_dequant(ac, gb,
1858 pulse_present ? &pulse : NULL,
1859 sce);
1860 if (ret < 0)
1861 goto fail;
1862
1863 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1864 ac->dsp.apply_prediction(ac, sce);
1865
1866 return 0;
1867fail:
1868 memset(sce->sfo, 0, sizeof(sce->sfo));
1869 tns->present = 0;
1870 return ret;
1871}
1872
1873/**
1874 * Decode a channel_pair_element; reference: table 4.4.
1875 *
1876 * @return Returns error status. 0 - OK, !0 - error
1877 */
1879{
1880 int i, ret, common_window, ms_present = 0;
1881 int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1882
1883 common_window = eld_syntax || get_bits1(gb);
1884 if (common_window) {
1885 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1886 return AVERROR_INVALIDDATA;
1887 i = cpe->ch[1].ics.use_kb_window[0];
1888 cpe->ch[1].ics = cpe->ch[0].ics;
1889 cpe->ch[1].ics.use_kb_window[1] = i;
1890 if (cpe->ch[1].ics.predictor_present &&
1891 (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1892 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1893 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1894 ms_present = get_bits(gb, 2);
1895 if (ms_present == 3) {
1896 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1897 return AVERROR_INVALIDDATA;
1898 } else if (ms_present)
1899 decode_mid_side_stereo(cpe, gb, ms_present);
1900 }
1901 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1902 return ret;
1903 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1904 return ret;
1905
1906 if (common_window) {
1907 if (ms_present)
1908 ac->dsp.apply_mid_side_stereo(ac, cpe);
1909 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1910 ac->dsp.apply_prediction(ac, &cpe->ch[0]);
1911 ac->dsp.apply_prediction(ac, &cpe->ch[1]);
1912 }
1913 }
1914
1915 ac->dsp.apply_intensity_stereo(ac, cpe, ms_present);
1916 return 0;
1917}
1918
1919/**
1920 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1921 *
1922 * @return Returns number of bytes consumed.
1923 */
1925 GetBitContext *gb)
1926{
1927 int i;
1928 int num_excl_chan = 0;
1929
1930 do {
1931 for (i = 0; i < 7; i++)
1932 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1933 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
1934
1935 return num_excl_chan / 7;
1936}
1937
1938/**
1939 * Decode dynamic range information; reference: table 4.52.
1940 *
1941 * @return Returns number of bytes consumed.
1942 */
1944 GetBitContext *gb)
1945{
1946 int n = 1;
1947 int drc_num_bands = 1;
1948 int i;
1949
1950 /* pce_tag_present? */
1951 if (get_bits1(gb)) {
1952 che_drc->pce_instance_tag = get_bits(gb, 4);
1953 skip_bits(gb, 4); // tag_reserved_bits
1954 n++;
1955 }
1956
1957 /* excluded_chns_present? */
1958 if (get_bits1(gb)) {
1959 n += decode_drc_channel_exclusions(che_drc, gb);
1960 }
1961
1962 /* drc_bands_present? */
1963 if (get_bits1(gb)) {
1964 che_drc->band_incr = get_bits(gb, 4);
1965 che_drc->interpolation_scheme = get_bits(gb, 4);
1966 n++;
1967 drc_num_bands += che_drc->band_incr;
1968 for (i = 0; i < drc_num_bands; i++) {
1969 che_drc->band_top[i] = get_bits(gb, 8);
1970 n++;
1971 }
1972 }
1973
1974 /* prog_ref_level_present? */
1975 if (get_bits1(gb)) {
1976 che_drc->prog_ref_level = get_bits(gb, 7);
1977 skip_bits1(gb); // prog_ref_level_reserved_bits
1978 n++;
1979 }
1980
1981 for (i = 0; i < drc_num_bands; i++) {
1982 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1983 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1984 n++;
1985 }
1986
1987 return n;
1988}
1989
1990static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len) {
1991 uint8_t buf[256];
1992 int i, major, minor;
1993
1994 if (len < 13+7*8)
1995 goto unknown;
1996
1997 get_bits(gb, 13); len -= 13;
1998
1999 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2000 buf[i] = get_bits(gb, 8);
2001
2002 buf[i] = 0;
2003 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2004 av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2005
2006 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2007 ac->avctx->internal->skip_samples = 1024;
2008 }
2009
2010unknown:
2011 skip_bits_long(gb, len);
2012
2013 return 0;
2014}
2015
2016/**
2017 * Decode extension data (incomplete); reference: table 4.51.
2018 *
2019 * @param cnt length of TYPE_FIL syntactic element in bytes
2020 *
2021 * @return Returns number of bytes consumed
2022 */
2024 ChannelElement *che, enum RawDataBlockType elem_type)
2025{
2026 int crc_flag = 0;
2027 int res = cnt;
2028 int type = get_bits(gb, 4);
2029
2030 if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2031 av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2032
2033 switch (type) { // extension type
2034 case EXT_SBR_DATA_CRC:
2035 crc_flag++;
2037 case EXT_SBR_DATA:
2038 if (!che) {
2039 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2040 return res;
2041 } else if (!ac->oc[1].m4ac.sbr) {
2042 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2043 skip_bits_long(gb, 8 * cnt - 4);
2044 return res;
2045 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2046 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2047 skip_bits_long(gb, 8 * cnt - 4);
2048 return res;
2049 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED &&
2050 ac->avctx->ch_layout.nb_channels == 1) {
2051 ac->oc[1].m4ac.sbr = 1;
2052 ac->oc[1].m4ac.ps = 1;
2055 ac->oc[1].status, 1);
2056 } else {
2057 ac->oc[1].m4ac.sbr = 1;
2059 }
2060
2061 ac->proc.sbr_decode_extension(ac, che, gb, crc_flag, cnt, elem_type,
2062 ac->oc[1].m4ac.frame_length_short);
2063
2064 if (ac->oc[1].m4ac.ps == 1 && !ac->warned_he_aac_mono) {
2065 av_log(ac->avctx, AV_LOG_VERBOSE, "Treating HE-AAC mono as stereo.\n");
2066 ac->warned_he_aac_mono = 1;
2067 }
2068 break;
2069 case EXT_DYNAMIC_RANGE:
2070 res = decode_dynamic_range(&ac->che_drc, gb);
2071 break;
2072 case EXT_FILL:
2073 decode_fill(ac, gb, 8 * cnt - 4);
2074 break;
2075 case EXT_FILL_DATA:
2076 case EXT_DATA_ELEMENT:
2077 default:
2078 skip_bits_long(gb, 8 * cnt - 4);
2079 break;
2080 };
2081 return res;
2082}
2083
2084/**
2085 * channel coupling transformation interface
2086 *
2087 * @param apply_coupling_method pointer to (in)dependent coupling function
2088 */
2090 enum RawDataBlockType type, int elem_id,
2091 enum CouplingPoint coupling_point,
2092 void (*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2093{
2094 int i, c;
2095
2096 for (i = 0; i < MAX_ELEM_ID; i++) {
2097 ChannelElement *cce = ac->che[TYPE_CCE][i];
2098 int index = 0;
2099
2100 if (cce && cce->coup.coupling_point == coupling_point) {
2101 ChannelCoupling *coup = &cce->coup;
2102
2103 for (c = 0; c <= coup->num_coupled; c++) {
2104 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2105 if (coup->ch_select[c] != 1) {
2106 apply_coupling_method(ac, &cc->ch[0], cce, index);
2107 if (coup->ch_select[c] != 0)
2108 index++;
2109 }
2110 if (coup->ch_select[c] != 2)
2111 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2112 } else
2113 index += 1 + (coup->ch_select[c] == 3);
2114 }
2115 }
2116 }
2117}
2118
2119/**
2120 * Convert spectral data to samples, applying all supported tools as appropriate.
2121 */
2122static void spectral_to_sample(AACDecContext *ac, int samples)
2123{
2124 int i, type;
2126 switch (ac->oc[1].m4ac.object_type) {
2127 case AOT_ER_AAC_LD:
2129 break;
2130 case AOT_ER_AAC_ELD:
2132 break;
2133 default:
2134 if (ac->oc[1].m4ac.frame_length_short)
2136 else
2138 }
2139 for (type = 3; type >= 0; type--) {
2140 for (i = 0; i < MAX_ELEM_ID; i++) {
2141 ChannelElement *che = ac->che[type][i];
2142 if (che && che->present) {
2143 if (type <= TYPE_CPE)
2145 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2146 if (che->ch[0].ics.predictor_present) {
2147 if (che->ch[0].ics.ltp.present)
2148 ac->dsp.apply_ltp(ac, &che->ch[0]);
2149 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2150 ac->dsp.apply_ltp(ac, &che->ch[1]);
2151 }
2152 }
2153 if (che->ch[0].tns.present)
2154 ac->dsp.apply_tns(che->ch[0].coeffs,
2155 &che->ch[0].tns, &che->ch[0].ics, 1);
2156 if (che->ch[1].tns.present)
2157 ac->dsp.apply_tns(che->ch[1].coeffs,
2158 &che->ch[1].tns, &che->ch[1].ics, 1);
2159 if (type <= TYPE_CPE)
2161 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2162 imdct_and_window(ac, &che->ch[0]);
2163 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2164 ac->dsp.update_ltp(ac, &che->ch[0]);
2165 if (type == TYPE_CPE) {
2166 imdct_and_window(ac, &che->ch[1]);
2167 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2168 ac->dsp.update_ltp(ac, &che->ch[1]);
2169 }
2170 if (ac->oc[1].m4ac.sbr > 0) {
2171 ac->proc.sbr_apply(ac, che, type,
2172 ac->oc[1].m4ac.frame_length_short,
2173 che->ch[0].output,
2174 che->ch[1].output);
2175 }
2176 }
2177 if (type <= TYPE_CCE)
2179 ac->dsp.clip_output(ac, che, type, samples);
2180 che->present = 0;
2181 } else if (che) {
2182 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2183 memset(che->ch[0].output, 0, samples * sizeof(*che->ch[0].output));
2184 if (type == TYPE_CPE)
2185 memset(che->ch[1].output, 0, samples * sizeof(*che->ch[1].output));
2186 }
2187 }
2188 }
2189}
2190
2192{
2193 int size;
2194 AACADTSHeaderInfo hdr_info;
2195 uint8_t layout_map[MAX_ELEM_ID*4][3];
2196 int layout_map_tags, ret;
2197
2198 size = ff_adts_header_parse(gb, &hdr_info);
2199 if (size > 0) {
2200 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2201 // This is 2 for "VLB " audio in NSV files.
2202 // See samples/nsv/vlb_audio.
2204 "More than one AAC RDB per ADTS frame");
2205 ac->warned_num_aac_frames = 1;
2206 }
2208 if (hdr_info.chan_config) {
2209 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2210 if ((ret = ff_aac_set_default_channel_config(ac, ac->avctx,
2211 layout_map,
2212 &layout_map_tags,
2213 hdr_info.chan_config)) < 0)
2214 return ret;
2215 if ((ret = ff_aac_output_configure(ac, layout_map, layout_map_tags,
2216 FFMAX(ac->oc[1].status,
2217 OC_TRIAL_FRAME), 0)) < 0)
2218 return ret;
2219 } else {
2220 ac->oc[1].m4ac.chan_config = 0;
2221 /**
2222 * dual mono frames in Japanese DTV can have chan_config 0
2223 * WITHOUT specifying PCE.
2224 * thus, set dual mono as default.
2225 */
2226 if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2227 layout_map_tags = 2;
2228 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2229 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2230 layout_map[0][1] = 0;
2231 layout_map[1][1] = 1;
2232 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
2233 OC_TRIAL_FRAME, 0))
2234 return -7;
2235 }
2236 }
2237 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2238 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2239 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2240 ac->oc[1].m4ac.frame_length_short = 0;
2241 if (ac->oc[0].status != OC_LOCKED ||
2242 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2243 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2244 ac->oc[1].m4ac.sbr = -1;
2245 ac->oc[1].m4ac.ps = -1;
2246 }
2247 if (!hdr_info.crc_absent)
2248 skip_bits(gb, 16);
2249 }
2250 return size;
2251}
2252
2254 int *got_frame_ptr, GetBitContext *gb)
2255{
2256 AACDecContext *ac = avctx->priv_data;
2257 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2258 ChannelElement *che;
2259 int err, i;
2260 int samples = m4ac->frame_length_short ? 960 : 1024;
2261 int chan_config = m4ac->chan_config;
2262 int aot = m4ac->object_type;
2263
2264 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
2265 samples >>= 1;
2266
2267 ac->frame = frame;
2268
2269 if ((err = frame_configure_elements(avctx)) < 0)
2270 return err;
2271
2272 // The AV_PROFILE_AAC_* defines are all object_type - 1
2273 // This may lead to an undefined profile being signaled
2274 ac->avctx->profile = aot - 1;
2275
2276 ac->tags_mapped = 0;
2277
2278 if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
2279 avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2280 chan_config);
2281 return AVERROR_INVALIDDATA;
2282 }
2283 for (i = 0; i < ff_tags_per_config[chan_config]; i++) {
2284 const int elem_type = ff_aac_channel_layout_map[chan_config-1][i][0];
2285 const int elem_id = ff_aac_channel_layout_map[chan_config-1][i][1];
2286 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2288 "channel element %d.%d is not allocated\n",
2289 elem_type, elem_id);
2290 return AVERROR_INVALIDDATA;
2291 }
2292 che->present = 1;
2293 if (aot != AOT_ER_AAC_ELD)
2294 skip_bits(gb, 4);
2295 switch (elem_type) {
2296 case TYPE_SCE:
2297 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2298 break;
2299 case TYPE_CPE:
2300 err = decode_cpe(ac, gb, che);
2301 break;
2302 case TYPE_LFE:
2303 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2304 break;
2305 }
2306 if (err < 0)
2307 return err;
2308 }
2309
2310 spectral_to_sample(ac, samples);
2311
2312 if (!ac->frame->data[0] && samples) {
2313 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2314 return AVERROR_INVALIDDATA;
2315 }
2316
2317 ac->frame->nb_samples = samples;
2318 ac->frame->sample_rate = avctx->sample_rate;
2320 *got_frame_ptr = 1;
2321
2323 return 0;
2324}
2325
2327 GetBitContext *gb, int *got_frame_ptr)
2328{
2329 int err;
2330 int is_dmono;
2331 int elem_id;
2332 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
2333 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
2334 ChannelElement *che = NULL, *che_prev = NULL;
2335 int samples = 0, multiplier, audio_found = 0, pce_found = 0, sce_count = 0;
2336 AVFrame *frame = ac->frame;
2337
2338 int payload_alignment = get_bits_count(gb);
2339 // parse
2340 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2341 elem_id = get_bits(gb, 4);
2342
2343 if (avctx->debug & FF_DEBUG_STARTCODE)
2344 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2345
2346 if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE)
2347 return AVERROR_INVALIDDATA;
2348
2349 if (elem_type < TYPE_DSE) {
2350 if (che_presence[elem_type][elem_id]) {
2351 int error = che_presence[elem_type][elem_id] > 1;
2352 av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
2353 elem_type, elem_id);
2354 if (error)
2355 return AVERROR_INVALIDDATA;
2356 }
2357 che_presence[elem_type][elem_id]++;
2358
2359 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2360 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2361 elem_type, elem_id);
2362 return AVERROR_INVALIDDATA;
2363 }
2364 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
2365 che->present = 1;
2366 }
2367
2368 switch (elem_type) {
2369
2370 case TYPE_SCE:
2371 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2372 audio_found = 1;
2373 sce_count++;
2374 break;
2375
2376 case TYPE_CPE:
2377 err = decode_cpe(ac, gb, che);
2378 audio_found = 1;
2379 break;
2380
2381 case TYPE_CCE:
2382 err = ac->proc.decode_cce(ac, gb, che);
2383 break;
2384
2385 case TYPE_LFE:
2386 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2387 audio_found = 1;
2388 break;
2389
2390 case TYPE_DSE:
2391 err = skip_data_stream_element(ac, gb);
2392 break;
2393
2394 case TYPE_PCE: {
2395 uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
2396 int tags;
2397
2398 int pushed = push_output_configuration(ac);
2399 if (pce_found && !pushed)
2400 return AVERROR_INVALIDDATA;
2401
2402 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
2403 payload_alignment);
2404 if (tags < 0) {
2405 err = tags;
2406 break;
2407 }
2408 if (pce_found) {
2409 av_log(avctx, AV_LOG_ERROR,
2410 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2412 } else {
2413 err = ff_aac_output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2414 if (!err)
2415 ac->oc[1].m4ac.chan_config = 0;
2416 pce_found = 1;
2417 }
2418 break;
2419 }
2420
2421 case TYPE_FIL:
2422 if (elem_id == 15)
2423 elem_id += get_bits(gb, 8) - 1;
2424 if (get_bits_left(gb) < 8 * elem_id) {
2425 av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2426 return AVERROR_INVALIDDATA;
2427 }
2428 err = 0;
2429 while (elem_id > 0) {
2430 int ret = decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
2431 if (ret < 0) {
2432 err = ret;
2433 break;
2434 }
2435 elem_id -= ret;
2436 }
2437 break;
2438
2439 default:
2440 err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
2441 break;
2442 }
2443
2444 if (elem_type < TYPE_DSE) {
2445 che_prev = che;
2446 che_prev_type = elem_type;
2447 }
2448
2449 if (err)
2450 return err;
2451
2452 if (get_bits_left(gb) < 3) {
2454 return AVERROR_INVALIDDATA;
2455 }
2456 }
2457
2458 if (!avctx->ch_layout.nb_channels)
2459 return 0;
2460
2461 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2462 samples <<= multiplier;
2463
2464 spectral_to_sample(ac, samples);
2465
2466 if (ac->oc[1].status && audio_found) {
2467 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2468 avctx->frame_size = samples;
2469 ac->oc[1].status = OC_LOCKED;
2470 }
2471
2472 if (samples && avctx->sample_rate <= 0) {
2473 av_log(avctx, AV_LOG_ERROR,
2474 "Cannot output a frame without a valid sample rate\n");
2475 return AVERROR_INVALIDDATA;
2476 }
2477
2478 if (!ac->frame->data[0] && samples) {
2479 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2480 return AVERROR_INVALIDDATA;
2481 }
2482
2483 if (samples) {
2484 ac->frame->nb_samples = samples;
2485 ac->frame->sample_rate = avctx->sample_rate;
2487 *got_frame_ptr = 1;
2488 } else {
2489 av_frame_unref(ac->frame);
2490 *got_frame_ptr = 0;
2491 }
2492
2493 /* for dual-mono audio (SCE + SCE) */
2494 is_dmono = ac->dmono_mode && sce_count == 2 &&
2497 if (is_dmono) {
2498 if (ac->dmono_mode == 1)
2499 frame->data[1] = frame->data[0];
2500 else if (ac->dmono_mode == 2)
2501 frame->data[0] = frame->data[1];
2502 }
2503
2504 return 0;
2505}
2506
2508 int *got_frame_ptr, GetBitContext *gb,
2509 const AVPacket *avpkt)
2510{
2511 int err;
2512 AACDecContext *ac = avctx->priv_data;
2513
2514 ac->frame = frame;
2515 *got_frame_ptr = 0;
2516
2517 // USAC can't be packed into ADTS due to field size limitations.
2518 if (show_bits(gb, 12) == 0xfff && ac->oc[1].m4ac.object_type != AOT_USAC) {
2519 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2520 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2521 goto fail;
2522 }
2523 if (ac->oc[1].m4ac.sampling_index > 12) {
2524 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2525 err = AVERROR_INVALIDDATA;
2526 goto fail;
2527 }
2528 }
2529
2530 if ((err = frame_configure_elements(avctx)) < 0)
2531 goto fail;
2532
2533 // The AV_PROFILE_AAC_* defines are all object_type - 1
2534 // This may lead to an undefined profile being signaled
2535 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2536
2537 ac->tags_mapped = 0;
2538
2539 if (ac->oc[1].m4ac.object_type == AOT_USAC) {
2540 if (ac->is_fixed) {
2542 "AAC USAC fixed-point decoding");
2543 return AVERROR_PATCHWELCOME;
2544 }
2545#if CONFIG_AAC_DECODER
2546 err = ff_aac_usac_decode_frame(avctx, ac, gb, got_frame_ptr);
2547 if (err < 0)
2548 goto fail;
2549#endif
2550 } else {
2551 err = decode_frame_ga(avctx, ac, gb, got_frame_ptr);
2552 if (err < 0)
2553 goto fail;
2554 }
2555
2556 return err;
2557
2558fail:
2560 return err;
2561}
2562
2564 int *got_frame_ptr, AVPacket *avpkt)
2565{
2566 AACDecContext *ac = avctx->priv_data;
2567 const uint8_t *buf = avpkt->data;
2568 int buf_size = avpkt->size;
2569 GetBitContext gb;
2570 int buf_consumed;
2571 int buf_offset;
2572 int err;
2573 size_t new_extradata_size;
2574 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2576 &new_extradata_size);
2577 size_t jp_dualmono_size;
2578 const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
2580 &jp_dualmono_size);
2581
2582 if (new_extradata) {
2583 /* discard previous configuration */
2584 ac->oc[1].status = OC_NONE;
2585 err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
2586 new_extradata,
2587 new_extradata_size * 8LL, 1);
2588 if (err < 0) {
2589 return err;
2590 }
2591 }
2592
2593 ac->dmono_mode = 0;
2594 if (jp_dualmono && jp_dualmono_size > 0)
2595 ac->dmono_mode = 1 + *jp_dualmono;
2596 if (ac->force_dmono_mode >= 0)
2597 ac->dmono_mode = ac->force_dmono_mode;
2598
2599 if (INT_MAX / 8 <= buf_size)
2600 return AVERROR_INVALIDDATA;
2601
2602 if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
2603 return err;
2604
2605 switch (ac->oc[1].m4ac.object_type) {
2606 case AOT_ER_AAC_LC:
2607 case AOT_ER_AAC_LTP:
2608 case AOT_ER_AAC_LD:
2609 case AOT_ER_AAC_ELD:
2610 err = aac_decode_er_frame(avctx, frame, got_frame_ptr, &gb);
2611 break;
2612 default:
2613 err = aac_decode_frame_int(avctx, frame, got_frame_ptr, &gb, avpkt);
2614 }
2615 if (err < 0)
2616 return err;
2617
2618 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2619 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2620 if (buf[buf_offset])
2621 break;
2622
2623 return buf_size > buf_offset ? buf_consumed : buf_size;
2624}
2625
2626#if CONFIG_AAC_LATM_DECODER
2627#include "aacdec_latm.h"
2628#endif
2629
2630#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2631#define OFF(field) offsetof(AACDecContext, field)
2632static const AVOption options[] = {
2633 /**
2634 * AVOptions for Japanese DTV specific extensions (ADTS only)
2635 */
2636 {"dual_mono_mode", "Select the channel to decode for dual mono",
2637 OFF(force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
2638 AACDEC_FLAGS, .unit = "dual_mono_mode"},
2639
2640 {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2641 {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2642 {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2643 {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2644
2645 { "channel_order", "Order in which the channels are to be exported",
2646 OFF(output_channel_order), AV_OPT_TYPE_INT,
2647 { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, AACDEC_FLAGS, .unit = "channel_order" },
2648 { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
2649 { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2650 { "coded", "order in which the channels are coded in the bitstream",
2651 0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2652
2653 { "target_level", "Target output loudness in dBFS for xHE-AAC normalization (0 = disabled)",
2654 OFF(target_level), AV_OPT_TYPE_INT, { .i64 = 0 }, -70, 0, AACDEC_FLAGS },
2655
2656 {NULL},
2657};
2658
2659static const AVClass decoder_class = {
2660 .class_name = "AAC decoder",
2661 .item_name = av_default_item_name,
2662 .option = options,
2663 .version = LIBAVUTIL_VERSION_INT,
2664};
2665
2666#if CONFIG_AAC_DECODER
2667const FFCodec ff_aac_decoder = {
2668 .p.name = "aac",
2669 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2670 .p.type = AVMEDIA_TYPE_AUDIO,
2671 .p.id = AV_CODEC_ID_AAC,
2672 .p.priv_class = &decoder_class,
2673 .priv_data_size = sizeof(AACDecContext),
2678 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2679 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2681 .flush = flush,
2683};
2684#endif
2685
2686#if CONFIG_AAC_FIXED_DECODER
2688 .p.name = "aac_fixed",
2689 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2690 .p.type = AVMEDIA_TYPE_AUDIO,
2691 .p.id = AV_CODEC_ID_AAC,
2692 .p.priv_class = &decoder_class,
2693 .priv_data_size = sizeof(AACDecContext),
2698 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2699 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2702 .flush = flush,
2703};
2704#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:75
av_cold void ff_aacdec_common_init_once(void)
Definition aacdec_tab.c:781
const uint8_t ff_aac_channel_layout_map[16][16][3]
Definition aacdec_tab.c:40
const int8_t ff_tags_per_config[16]
Definition aacdec_tab.c:38
VLCElem ff_vlc_scalefactors[352]
Definition aacdec_tab.c:111
const AVChannelLayout ff_aac_ch_layout[]
Definition aacdec_tab.c:96
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
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#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:1401
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1394
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:1781
#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:610
enum AVCodecID id
Definition dts2pts.c:607
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
error code definitions
static struct @111144215057303131116103221376075045141373005341 current
static const uint8_t bits[8]
Definition fastaudio.c:100
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:459
@ 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:496
#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:241
@ 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
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
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:1193
static void spectral_to_sample(AACDecContext *ac, int samples)
Convert spectral data to samples, applying all supported tools as appropriate.
Definition aacdec.c:2122
static void relative_align_get_bits(GetBitContext *gb, int reference_position)
Definition aacdec.c:807
static int skip_data_stream_element(AACDecContext *ac, GetBitContext *gb)
Skip data_stream_element; reference: table 4.10.
Definition aacdec.c:1360
#define AACDEC_FLAGS
Definition aacdec.c:2630
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:486
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:2023
static uint64_t sniff_channel_order(uint8_t(*layout_map)[3], int tags)
Definition aacdec.c:370
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:777
static av_cold int init_dsp(AVCodecContext *avctx)
Definition aacdec.c:1262
static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb)
Definition aacdec.c:2253
static int count_paired_channels(uint8_t(*layout_map)[3], int tags, int pos, int current)
Definition aacdec.c:260
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:1118
#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:1735
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1219
static int decode_eld_specific_config(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, MPEG4AudioConfig *m4ac, int channel_config)
Definition aacdec.c:1049
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:1784
ChannelElement * ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
Definition aacdec.c:622
static int decode_band_types(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb)
Decode band types (section_data payload); reference: table 4.46.
Definition aacdec.c:1544
static int frame_configure_elements(AVCodecContext *avctx)
Definition aacdec.c:181
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:974
static int decode_frame_ga(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
Definition aacdec.c:2326
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:1924
static const AVClass decoder_class
Definition aacdec.c:2659
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:1677
static void copy_oc(OutputConfiguration *dst, OutputConfiguration *src)
Definition aacdec.c:431
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:1650
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:292
static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext *gb)
Decode dynamic range information; reference: table 4.52.
Definition aacdec.c:1943
static void decode_gain_control(SingleChannelElement *sce, GetBitContext *gb)
Definition aacdec.c:1749
static int push_output_configuration(AACDecContext *ac)
Save current output configuration if and only if it has been locked.
Definition aacdec.c:454
av_cold int ff_aac_decode_init(AVCodecContext *avctx)
Definition aacdec.c:1297
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:819
static int decode_prediction(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Definition aacdec.c:1377
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:582
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:2089
static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, GetBitContext *gb, const AVPacket *avpkt)
Definition aacdec.c:2507
static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition aacdec.c:2563
static int decode_ics_info(AACDecContext *ac, IndividualChannelStream *ics, GetBitContext *gb)
Decode Individual Channel Stream info; reference: table 4.6.
Definition aacdec.c:1417
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:222
static int parse_adts_frame_header(AACDecContext *ac, GetBitContext *gb)
Definition aacdec.c:2191
static int decode_scalefactors(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, unsigned int global_gain)
Decode scalefactors; reference: table 4.47.
Definition aacdec.c:1591
static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len)
Definition aacdec.c:1990
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:1399
#define OFF(field)
Definition aacdec.c:2631
static int decode_cpe(AACDecContext *ac, GetBitContext *gb, ChannelElement *cpe)
Decode a channel_pair_element; reference: table 4.4.
Definition aacdec.c:1878
#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:470
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:97
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:2128
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:149
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:500
AACDecProc proc
Definition aacdec.h:505
AVTXContext * mdct96
Definition aacdec.h:535
av_tx_fn mdct960_fn
Definition aacdec.h:551
av_tx_fn mdct768_fn
Definition aacdec.h:550
av_tx_fn mdct128_fn
Definition aacdec.h:547
int random_state
Definition aacdec.h:558
AACDecDSP dsp
Definition aacdec.h:504
av_tx_fn mdct1024_fn
Definition aacdec.h:552
int tags_mapped
Definition aacdec.h:518
av_tx_fn mdct480_fn
Definition aacdec.h:548
AVTXContext * mdct128
Definition aacdec.h:537
AVFloatDSPContext * fdsp
Definition aacdec.h:556
struct AVCodecContext * avctx
Definition aacdec.h:502
AVTXContext * mdct960
Definition aacdec.h:541
AVTXContext * mdct768
Definition aacdec.h:540
AVTXContext * mdct480
Definition aacdec.h:538
av_tx_fn mdct120_fn
Definition aacdec.h:546
AVTXContext * mdct512
Definition aacdec.h:539
ChannelElement * che[4][MAX_ELEM_ID]
Definition aacdec.h:516
int warned_he_aac_mono
Definition aacdec.h:590
ChannelElement * tag_che_map[4][MAX_ELEM_ID]
Definition aacdec.h:517
int warned_remapping_once
Definition aacdec.h:519
DynamicRangeControl che_drc
Definition aacdec.h:510
enum AACOutputChannelOrder output_channel_order
Definition aacdec.h:577
unsigned warned_71_wide
Definition aacdec.h:588
av_tx_fn mdct96_fn
Definition aacdec.h:545
AVTXContext * mdct1024
Definition aacdec.h:542
int warned_num_aac_frames
Definition aacdec.h:587
struct AVFrame * frame
Definition aacdec.h:507
int dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition aacdec.h:574
SingleChannelElement * output_element[MAX_CHANNELS]
Points to each SingleChannelElement.
Definition aacdec.h:565
av_tx_fn mdct512_fn
Definition aacdec.h:549
OutputConfiguration oc[2]
Definition aacdec.h:586
int warned_gain_control
Definition aacdec.h:589
int force_dmono_mode
0->not dmono, 1->use first channel, 2->use second channel
Definition aacdec.h:573
AVTXContext * mdct_ltp
Definition aacdec.h:543
av_tx_fn mdct_ltp_fn
Definition aacdec.h:553
AVTXContext * mdct120
Definition aacdec.h:536
void(* apply_intensity_stereo)(AACDecContext *ac, ChannelElement *cpe, int ms_present)
Definition aacdec.h:470
void(* imdct_and_windowing_eld)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:492
void(* imdct_and_windowing_960)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:490
void(* imdct_and_windowing_ld)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:491
void(* apply_independent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition aacdec.h:484
void(* apply_prediction)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:479
void(* apply_tns)(void *_coef_param, TemporalNoiseShaping *tns, IndividualChannelStream *ics, int decode)
Definition aacdec.h:473
void(* imdct_and_windowing)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:488
void(* update_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:477
void(* apply_dependent_coupling)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index)
Definition aacdec.h:481
void(* apply_ltp)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:476
void(* clip_output)(AACDecContext *ac, ChannelElement *che, int type, int samples)
Definition aacdec.h:494
void(* apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
Definition aacdec.h:469
void(* dequant_scalefactors)(SingleChannelElement *sce)
Definition aacdec.h:467
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
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:1055
int debug
debug
Definition avcodec.h:1393
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1376
int profile
profile
Definition avcodec.h:1641
int sample_rate
samples per second
Definition avcodec.h:1040
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:1068
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:1417
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:218
uint64_t av_position
Definition aacdec.c:216
uint8_t aac_position
Definition aacdec.c:219
uint8_t syn_ele
Definition aacdec.c:217
Definition swscale.c:71
#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
#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]