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