FFmpeg
Loading...
Searching...
No Matches
aacdec_usac.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "aacdec_usac.h"
22#include "aacdec_tab.h"
23#include "aacdec_lpd.h"
24#include "aacdec_ac.h"
25
26#include "libavcodec/aacsbr.h"
27#include "libavcodec/aactab.h"
29#include "libavcodec/unary.h"
30
31#include "libavutil/mem.h"
32#include "libavutil/refstruct.h"
33
34#include "aacdec_usac_mps212.h"
35
36/* Number of scalefactor bands per complex prediction band, equal to 2. */
37#define SFB_PER_PRED_BAND 2
38
39static inline uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3)
40{
41 uint32_t val = get_bits(gb, nb1), val2;
42 if (val < ((1 << nb1) - 1))
43 return val;
44
45 val += val2 = get_bits(gb, nb2);
46 if (nb3 && (val2 == ((1 << nb2) - 1)))
47 val += get_bits(gb, nb3);
48
49 return val;
50}
51
52/* ISO/IEC 23003-3, Table 74: bsOutputChannelPos */
53static const enum AVChannel usac_ch_pos_to_av[64] = {
58 [4] = AV_CHAN_SIDE_LEFT, // +110 degrees, Ls|LS|kAudioChannelLabel_LeftSurround
59 [5] = AV_CHAN_SIDE_RIGHT, // -110 degrees, Rs|RS|kAudioChannelLabel_RightSurround
62 [8] = AV_CHAN_BACK_LEFT, // +135 degrees, Lsr|BL|kAudioChannelLabel_RearSurroundLeft
63 [9] = AV_CHAN_BACK_RIGHT, // -135 degrees, Rsr|BR|kAudioChannelLabel_RearSurroundRight
67 [13] = AV_CHAN_SIDE_SURROUND_LEFT, // +90 degrees, Lss|SL|kAudioChannelLabel_LeftSideSurround
68 [14] = AV_CHAN_SIDE_SURROUND_RIGHT, // -90 degrees, Rss|SR|kAudioChannelLabel_RightSideSurround
69 [15] = AV_CHAN_WIDE_LEFT, // +60 degrees, Lw|FLw|kAudioChannelLabel_LeftWide
70 [16] = AV_CHAN_WIDE_RIGHT, // -60 degrees, Rw|FRw|kAudioChannelLabel_RightWide
79 [25] = AV_CHAN_TOP_CENTER,
84 [30] = AV_CHAN_TOP_SURROUND_LEFT, ///< +110 degrees, Lvs, TpLS
85 [31] = AV_CHAN_TOP_SURROUND_RIGHT, ///< -110 degrees, Rvs, TpRS
86};
87
88/* ISO/IEC 23003-4, Table A.48: bit width of bsMethodValue depends on methodDef. */
89static int methodvalue_width(int method_def)
90{
91 switch (method_def) {
92 case 7: return 5; /* mixing level */
93 case 8: return 2; /* room type */
94 default: return 8; /* loudness (0..6, 9) + reserved */
95 }
96}
97
98/* ISO/IEC 23003-4, Table 58/60: loudnessInfo(), loudnessInfoV1().
99 * The only difference in V1 is the added eqSetId field. */
101 GetBitContext *gb, int v1)
102{
103 info->drc_set_id = get_bits(gb, 6);
104 info->eq_set_id = v1 ? get_bits(gb, 6) : 0;
105 info->downmix_id = get_bits(gb, 7);
106
107 if ((info->sample_peak.present = get_bits1(gb))) /* samplePeakLevelPresent */
108 info->sample_peak.lvl = get_bits(gb, 12);
109
110 if ((info->true_peak.present = get_bits1(gb))) { /* truePeakLevelPresent */
111 info->true_peak.lvl = get_bits(gb, 12);
112 info->true_peak.measurement = get_bits(gb, 4);
113 info->true_peak.reliability = get_bits(gb, 2);
114 }
115
116 info->nb_measurements = get_bits(gb, 4);
117 for (int i = 0; i < info->nb_measurements; i++) {
118 info->measurements[i].method_def = get_bits(gb, 4);
119 info->measurements[i].method_val =
121 info->measurements[i].measurement = get_bits(gb, 4);
122 info->measurements[i].reliability = get_bits(gb, 2);
123 }
124
125 return 0;
126}
127
128/* ISO/IEC 23003-4, Table 61: loudnessInfoSetExtension(), UNIDRCLOUDEXT_EQ */
130 GetBitContext *gb)
131{
132 int ret;
133 int nb_album = get_bits(gb, 6); /* loudnessInfoV1AlbumCount */
134 int nb_info = get_bits(gb, 6); /* loudnessInfoV1Count */
135
136 for (int i = 0; i < nb_album; i++) {
138 ret = decode_loudness_info(ac, &tmp, gb, 1);
139 if (ret < 0)
140 return ret;
142 usac->loudness.album_info[usac->loudness.nb_album++] = tmp;
143 }
144
145 for (int i = 0; i < nb_info; i++) {
147 ret = decode_loudness_info(ac, &tmp, gb, 1);
148 if (ret < 0)
149 return ret;
150 if (usac->loudness.nb_info < FF_ARRAY_ELEMS(usac->loudness.info))
151 usac->loudness.info[usac->loudness.nb_info++] = tmp;
152 }
153
154 return 0;
155}
156
157/* Pick the bsMethodValue of a program- or anchor-loudness measurement.
158 * Per ISO/IEC 23003-4 6.1.2.5, downmixId, drcSetId and eqSetId identify the
159 * signal a loudnessInfo() applies to; only downmixId == 0 (base layout)
160 * together with drcSetId == 0 and eqSetId == 0 (no DRC/EQ) describes the
161 * unprocessed signal we output, so measurements for any other
162 * downmix/DRC/EQ set must not be used. */
164{
165 for (int i = 0; i < usac->loudness.nb_info; i++) {
166 const AACUSACLoudnessInfo *info = &usac->loudness.info[i];
167 if (info->downmix_id != 0 || info->drc_set_id != 0 || info->eq_set_id != 0)
168 continue;
169 for (int j = 0; j < info->nb_measurements; j++) {
170 int method = info->measurements[j].method_def;
171 if (method == 1 || method == 2)
172 return info->measurements[j].method_val;
173 }
174 }
175 return -1;
176}
177
179 GetBitContext *gb)
180{
181 int ret;
182
183 usac->loudness.nb_album = get_bits(gb, 6); /* loudnessInfoAlbumCount */
184 usac->loudness.nb_info = get_bits(gb, 6); /* loudnessInfoCount */
185
186 for (int i = 0; i < usac->loudness.nb_album; i++) {
187 ret = decode_loudness_info(ac, &usac->loudness.album_info[i], gb, 0);
188 if (ret < 0)
189 return ret;
190 }
191
192 for (int i = 0; i < usac->loudness.nb_info; i++) {
193 ret = decode_loudness_info(ac, &usac->loudness.info[i], gb, 0);
194 if (ret < 0)
195 return ret;
196 }
197
198 if (get_bits1(gb)) { /* loudnessInfoSetExtPresent */
200 while ((type = get_bits(gb, 4)) != UNIDRCLOUDEXT_TERM) {
201 uint8_t size_bits = get_bits(gb, 4) + 4; /* bitSizeLen */
202 uint32_t bit_size = get_bits_long(gb, size_bits) + 1; /* bitSize */
203 int start = get_bits_count(gb);
204 int skip;
205 switch (type) {
206 case UNIDRCLOUDEXT_EQ:
207 ret = decode_loudness_set_v1(ac, usac, gb);
208 if (ret < 0)
209 return ret;
210 break;
211 default:
212 break;
213 }
214 /* The extension size is explicit, so unparsed (or unknown)
215 * data can be skipped without desynchronizing. */
216 skip = bit_size - (get_bits_count(gb) - start);
217 if (skip < 0)
218 return AVERROR_INVALIDDATA;
219 skip_bits_long(gb, skip);
220 }
221 }
222
223 return 0;
224}
225
228{
229 uint8_t header_extra1;
230 uint8_t header_extra2;
231
232 e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */
233 e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */
234 e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */
235 if (e->sbr.harmonic_sbr || e->sbr.bs_intertes || e->sbr.bs_pvc) {
236 avpriv_report_missing_feature(ac->avctx, "AAC USAC eSBR");
238 }
239
240 e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */
241 e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */
242
243 header_extra1 = get_bits1(gb); /* dflt_header_extra1 */
244 header_extra2 = get_bits1(gb); /* dflt_header_extra2 */
245
246 e->sbr.dflt.freq_scale = 2;
247 e->sbr.dflt.alter_scale = 1;
248 e->sbr.dflt.noise_bands = 2;
249 if (header_extra1) {
250 e->sbr.dflt.freq_scale = get_bits(gb, 2); /* dflt_freq_scale */
251 e->sbr.dflt.alter_scale = get_bits1(gb); /* dflt_alter_scale */
252 e->sbr.dflt.noise_bands = get_bits(gb, 2); /* dflt_noise_bands */
253 }
254
255 e->sbr.dflt.limiter_bands = 2;
256 e->sbr.dflt.limiter_gains = 2;
257 e->sbr.dflt.interpol_freq = 1;
258 e->sbr.dflt.smoothing_mode = 1;
259 if (header_extra2) {
260 e->sbr.dflt.limiter_bands = get_bits(gb, 2); /* dflt_limiter_bands */
261 e->sbr.dflt.limiter_gains = get_bits(gb, 2); /* dflt_limiter_gains */
262 e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */
263 e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */
264 }
265
266 return 0;
267}
268
270 GetBitContext *gb,
271 int sbr_ratio)
272{
273 e->tw_mdct = get_bits1(gb); /* tw_mdct */
274 e->noise_fill = get_bits1(gb);
275 e->sbr.ratio = sbr_ratio;
276}
277
280{
281 e->stereo_config_index = 0;
282 if (e->sbr.ratio) {
283 int ret = decode_usac_sbr_data(ac, e, gb);
284 if (ret < 0)
285 return ret;
286 e->stereo_config_index = get_bits(gb, 2);
287 }
288
289 if (e->stereo_config_index) {
290 e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */
291 if (!e->mps.freq_res)
292 return AVERROR_INVALIDDATA; /* value 0 is reserved */
293
294 int numBands = ((int[]){0,28,20,14,10,7,5,4})[e->mps.freq_res]; // ISO/IEC 23003-1:2007, 5.2, Table 39
295
296 e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */
297 e->mps.temp_shape_config = get_bits(gb, 2); /* bsTempShapeConfig */
298 e->mps.decorr_config = get_bits(gb, 2); /* bsDecorrConfig */
299 e->mps.high_rate_mode = get_bits1(gb); /* bsHighRateMode */
300 e->mps.phase_coding = get_bits1(gb); /* bsPhaseCoding */
301
303 int otts_bands_phase = ((int[]){0,10,10,7,5,3,2,2})[e->mps.freq_res]; // Table 109: Default value of bsOttBandsPhase
304 if (e->mps.otts_bands_phase_present) { /* bsOttBandsPhasePresent */
305 otts_bands_phase = get_bits(gb, 5); /* bsOttBandsPhase */
306 if (otts_bands_phase > numBands)
307 return AVERROR_INVALIDDATA;
308 }
309 e->mps.otts_bands_phase = otts_bands_phase;
310
311 e->mps.residual_coding = e->stereo_config_index >= 2; /* bsResidualCoding */
312 if (e->mps.residual_coding) {
313 int residual_bands = get_bits(gb, 5); /* bsResidualBands */
314 if (residual_bands > numBands)
315 return AVERROR_INVALIDDATA;
316 e->mps.residual_bands = residual_bands;
317
320 e->mps.pseudo_lr = get_bits1(gb); /* bsPseudoLr */
321 }
322 if (e->mps.temp_shape_config == 2)
323 e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */
324 }
325
326 return 0;
327}
328
329/* ISO/IEC 23003-4, Table 62: channelLayout() */
331{
332 int base_channel_count = get_bits(gb, 7); /* baseChannelCount */
333 if (get_bits1(gb)) { /* layoutSignallingPresent */
334 if (get_bits(gb, 8) == 0) /* definedLayout == 0 */
335 for (int i = 0; i < base_channel_count; i++)
336 skip_bits(gb, 7); /* speakerPosition */
337 }
338 return base_channel_count;
339}
340
341/* ISO/IEC 23003-4, Table 63: downmixInstructions() */
342static void skip_drc_downmix_instructions(GetBitContext *gb, int base_channel_count)
343{
344 int target_channel_count;
345 skip_bits(gb, 7); /* downmixId */
346 target_channel_count = get_bits(gb, 7); /* targetChannelCount */
347 skip_bits(gb, 8); /* targetLayout */
348 if (get_bits1(gb)) /* downmixCoefficientsPresent */
349 skip_bits_long(gb, 4 * target_channel_count * base_channel_count);
350}
351
352/* ISO/IEC 23003-4, Table 70: drcInstructionsBasic(), common with the
353 * uniDrc variant up to the loudness-target fields. */
355{
356 int set_effects;
357
358 skip_bits(gb, 6); /* drcSetId */
359 skip_bits(gb, 4); /* drcLocation */
360 skip_bits(gb, 7); /* downmixId */
361 if (get_bits1(gb)) { /* additionalDownmixIdPresent */
362 int add_downmix_cnt = get_bits(gb, 3); /* additionalDownmixIdCount */
363 for (int j = 0; j < add_downmix_cnt; j++)
364 skip_bits(gb, 7); /* additionalDownmixId */
365 }
366
367 set_effects = get_bits(gb, 16); /* drcSetEffect */
368 if ((set_effects & (3 << 10)) == 0) {
369 if (get_bits1(gb)) /* limiterPeakTargetPresent */
370 skip_bits(gb, 8); /* bsLimiterPeakTarget */
371 }
372
373 if (get_bits1(gb)) { /* drcSetTargetLoudnessPresent */
374 e->drc.loudness.upper = get_bits(gb, 6); /* bsDrcSetTargetLoudnessValueUpper */
375 if (get_bits1(gb)) /* drcSetTargetLoudnessValueLowerPresent */
376 e->drc.loudness.lower = get_bits(gb, 6); /* bsDrcSetTargetLoudnessValueLower */
377 }
378}
379
380/* ISO/IEC 23003-4, Table 57: uniDrcConfig() */
382 GetBitContext *gb)
383{
384 int nb_downmix_instr, nb_coeff_basic = 0, nb_instr_basic = 0;
385 int nb_coeff_uni, nb_instr_uni;
386 int base_channel_count;
387
388 e->drc.loudness.lower = -1;
389 e->drc.loudness.upper = -1;
390
391 if (get_bits1(gb)) /* sampleRatePresent */
392 skip_bits(gb, 18); /* bsSampleRate */
393
394 nb_downmix_instr = get_bits(gb, 7); /* downmixInstructionsCount */
395
396 if (get_bits1(gb)) { /* drcDescriptionBasicPresent */
397 nb_coeff_basic = get_bits(gb, 3); /* drcCoefficientsBasicCount */
398 nb_instr_basic = get_bits(gb, 4); /* drcInstructionsBasicCount */
399 }
400
401 nb_coeff_uni = get_bits(gb, 3); /* drcCoefficientsUniDrcCount */
402 nb_instr_uni = get_bits(gb, 6); /* drcInstructionsUniDrcCount */
403
404 if (nb_coeff_uni || nb_instr_uni) {
406 "AAC USAC uniDrc DRC processing");
408 }
409
410 base_channel_count = decode_drc_channel_layout(gb);
411
412 for (int i = 0; i < nb_downmix_instr; i++)
413 skip_drc_downmix_instructions(gb, base_channel_count);
414
415 for (int i = 0; i < nb_coeff_basic; i++)
416 skip_bits(gb, 4 + 7); /* drcLocation, drcCharacteristic */
417
418 for (int i = 0; i < nb_instr_basic; i++)
420
421 if (get_bits1(gb)) { /* uniDrcConfigExtPresent */
422 enum AACUSACDRCExt type;
423 while ((type = get_bits(gb, 4)) != UNIDRCCONFEXT_TERM) {
424 uint8_t size_bits = get_bits(gb, 4) + 4; /* bitSizeLen */
425 uint32_t bit_size = get_bits_long(gb, size_bits) + 1; /* extBitSize */
426 switch (type) {
427 default:
428 skip_bits_long(gb, bit_size);
429 break;
430 }
431 }
432 }
433
434 return 0;
435}
436
438 GetBitContext *gb)
439{
440 int len = 0, ext_config_len;
441
442 e->ext.type = get_escaped_value(gb, 4, 8, 16); /* usacExtElementType */
443 ext_config_len = get_escaped_value(gb, 4, 8, 16); /* usacExtElementConfigLength */
444
445 if (get_bits1(gb)) /* usacExtElementDefaultLengthPresent */
446 len = get_escaped_value(gb, 8, 16, 0) + 1;
447
448 e->ext.default_len = len;
449 e->ext.payload_frag = get_bits1(gb); /* usacExtElementPayloadFrag */
450
451 av_log(ac->avctx, AV_LOG_DEBUG, "Extension present: type %i, len %i\n",
452 e->ext.type, ext_config_len);
453
454 switch (e->ext.type) {
455#if 0 /* Skip unsupported values */
456 case ID_EXT_ELE_MPEGS:
457 break;
458 case ID_EXT_ELE_SAOC:
459 break;
460#endif
461 case ID_EXT_ELE_UNI_DRC: {
462 int start = get_bits_count(gb);
463 int ret = decode_drc_config(ac, e, gb);
464 int skip = 8*ext_config_len - (get_bits_count(gb) - start);
465 if (ret == AVERROR_PATCHWELCOME) {
466 /* Unsupported uniDrcConfig(): ignore the DRC metadata and treat
467 * the element as fill so the stream stays decodable. */
469 ret = 0;
470 }
471 if (ret < 0)
472 return ret;
473 if (skip < 0)
474 return AVERROR_INVALIDDATA;
475 /* The config is byte-padded to usacExtElementConfigLength */
476 skip_bits_long(gb, skip);
477 break;
478 }
479 case ID_EXT_ELE_FILL:
480 break; /* This is what the spec does */
482 /* No configuration needed - fallthrough (len should be 0) */
483 default:
484 skip_bits(gb, 8*ext_config_len);
486 break;
487 };
488
489 return 0;
490}
491
493{
494 AACUSACConfig *usac = &oc->usac;
495 int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
496
497 ChannelElement *che;
499 int id, ch;
500
501 /* Initialize state */
502 for (int i = 0; i < usac->nb_elems; i++) {
503 AACUsacElemConfig *e = &usac->elems[i];
504 if (e->type == ID_USAC_EXT)
505 continue;
506
507 switch (e->type) {
508 case ID_USAC_SCE:
509 ch = 1;
510 type = TYPE_SCE;
511 id = elem_id[0]++;
512 break;
513 case ID_USAC_CPE:
514 ch = 2;
515 type = TYPE_CPE;
516 id = elem_id[1]++;
517 break;
518 case ID_USAC_LFE:
519 ch = 1;
520 type = TYPE_LFE;
521 id = elem_id[2]++;
522 break;
523 }
524
525 che = ff_aac_get_che(ac, type, id);
526 if (che) {
527 AACUsacStereo *us = &che->us;
528 memset(us, 0, sizeof(*us));
529
530 if (e->sbr.ratio)
531 ff_aac_sbr_config_usac(ac, che, e);
532
533 for (int j = 0; j < ch; j++) {
534 SingleChannelElement *sce = &che->ch[j];
535 AACUsacElemData *ue = &sce->ue;
536
537 memset(ue, 0, sizeof(*ue));
538
539 if (!ch)
540 ue->noise.seed = 0x3039;
541 else
542 che->ch[1].ue.noise.seed = 0x10932;
543 }
544 }
545 }
546
547 return 0;
548}
549
550/* UsacConfig */
553 int channel_config)
554{
555 int ret;
556 uint8_t freq_idx;
557 uint8_t channel_config_idx;
558 int nb_channels = 0;
559 int ratio_mult, ratio_dec;
560 int samplerate;
561 int sbr_ratio;
562 MPEG4AudioConfig *m4ac = &oc->m4ac;
563 AACUSACConfig *usac = &oc->usac;
564 int elem_id[3 /* SCE, CPE, LFE */];
565 int nb_elems;
566
567 int map_pos_set = 0;
568 int nb_elements = 0;
569 uint8_t layout_map[MAX_ELEM_ID*4][3] = { 0 };
570
571 if (!ac)
573
574 memset(usac, 0, sizeof(*usac));
575 usac->loudness.input_method_val = -1;
576
577 freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */
578 if (freq_idx == 0x1f) {
579 samplerate = get_bits(gb, 24); /* usacSamplingFrequency */
580 if (samplerate == 0)
581 return AVERROR(EINVAL);
582 } else {
583 samplerate = ff_aac_usac_samplerate[freq_idx];
584 if (samplerate < 0)
585 return AVERROR(EINVAL);
586 }
587
588 usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */
589 m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 ||
590 usac->core_sbr_frame_len_idx == 2;
591
592 usac->core_frame_len = (usac->core_sbr_frame_len_idx == 0 ||
593 usac->core_sbr_frame_len_idx == 2) ? 768 : 1024;
594
595 sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
596 usac->core_sbr_frame_len_idx == 3 ? 3 :
597 usac->core_sbr_frame_len_idx == 4 ? 1 :
598 0;
599
600 if (sbr_ratio == 2) {
601 ratio_mult = 8;
602 ratio_dec = 3;
603 } else if (sbr_ratio == 3) {
604 ratio_mult = 2;
605 ratio_dec = 1;
606 } else if (sbr_ratio == 4) {
607 ratio_mult = 4;
608 ratio_dec = 1;
609 } else {
610 ratio_mult = 1;
611 ratio_dec = 1;
612 }
613
614 avctx->sample_rate = samplerate;
615 m4ac->ext_sample_rate = samplerate;
616 m4ac->sample_rate = (samplerate * ratio_dec) / ratio_mult;
617
619 m4ac->sbr = sbr_ratio > 0;
620
621 channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */
622 if (!channel_config_idx) {
623 /* UsacChannelConfig() */
624 nb_channels = get_escaped_value(gb, 5, 8, 16); /* numOutChannels */
625 if (nb_channels > 64)
626 return AVERROR(EINVAL);
627
629
630 ret = av_channel_layout_custom_init(&ac->oc[1].ch_layout, nb_channels);
631 if (ret < 0)
632 return ret;
633
634 for (int i = 0; i < nb_channels; i++) {
635 AVChannelCustom *cm = &ac->oc[1].ch_layout.u.map[i];
636 cm->id = usac_ch_pos_to_av[get_bits(gb, 5)]; /* bsOutputChannelPos */
637 }
638
642 if (ret < 0)
643 return ret;
644
645 ret = av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
646 if (ret < 0)
647 return ret;
648 } else {
649 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
650 &nb_elements, channel_config_idx)))
651 return ret;
652
653 /* Fill in the number of expected channels */
654 for (int i = 0; i < nb_elements; i++)
655 nb_channels += layout_map[i][0] == TYPE_CPE ? 2 : 1;
656
657 map_pos_set = 1;
658 }
659
660 /* UsacDecoderConfig */
661 elem_id[0] = elem_id[1] = elem_id[2] = 0;
662 nb_elems = get_escaped_value(gb, 4, 8, 16) + 1;
663 if (nb_elems > 64) {
664 av_log(ac->avctx, AV_LOG_ERROR, "Too many elements: %i\n",
665 nb_elems);
666 return AVERROR(EINVAL);
667 }
668
669 for (int i = 0; i < nb_elems; i++) {
670 int map_count = elem_id[0] + elem_id[1] + elem_id[2];
671 AACUsacElemConfig *e = &usac->elems[i];
672 memset(e, 0, sizeof(*e));
673
674 e->type = get_bits(gb, 2); /* usacElementType */
675 if (e->type != ID_USAC_EXT && (map_count + 1) > nb_channels) {
676 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels for the channel "
677 "configuration\n");
678 return AVERROR(EINVAL);
679 }
680 if (map_pos_set && e->type != ID_USAC_EXT &&
681 (map_count >= nb_elements ||
682 layout_map[map_count][0] != (e->type == ID_USAC_LFE ? TYPE_LFE : e->type))) {
683 av_log(ac->avctx, AV_LOG_ERROR, "Element %d does not match the "
684 "channel configuration\n", i);
685 usac->nb_elems = 0;
686 return AVERROR_INVALIDDATA;
687 }
688
689 av_log(ac->avctx, AV_LOG_DEBUG, "Element present: idx %i, type %i\n",
690 i, e->type);
691
692 switch (e->type) {
693 case ID_USAC_SCE: /* SCE */
694 /* UsacCoreConfig */
695 decode_usac_element_core(e, gb, sbr_ratio);
696 if (e->sbr.ratio > 0) {
697 ret = decode_usac_sbr_data(ac, e, gb);
698 if (ret < 0)
699 return ret;
700 }
701 layout_map[map_count][0] = TYPE_SCE;
702 layout_map[map_count][1] = elem_id[0]++;
703 if (!map_pos_set)
704 layout_map[map_count][2] = AAC_CHANNEL_FRONT;
705
706 break;
707 case ID_USAC_CPE: /* UsacChannelPairElementConf */
708 /* UsacCoreConfig */
709 decode_usac_element_core(e, gb, sbr_ratio);
710 ret = decode_usac_element_pair(ac, e, gb);
711 if (ret < 0)
712 return ret;
713 layout_map[map_count][0] = TYPE_CPE;
714 layout_map[map_count][1] = elem_id[1]++;
715 if (!map_pos_set)
716 layout_map[map_count][2] = AAC_CHANNEL_FRONT;
717
718 break;
719 case ID_USAC_LFE: /* LFE */
720 /* LFE has no need for any configuration */
721 e->tw_mdct = 0;
722 e->noise_fill = 0;
723 layout_map[map_count][0] = TYPE_LFE;
724 layout_map[map_count][1] = elem_id[2]++;
725 if (!map_pos_set)
726 layout_map[map_count][2] = AAC_CHANNEL_LFE;
727
728 break;
729 case ID_USAC_EXT: /* EXT */
730 ret = decode_usac_extension(ac, e, gb);
731 if (ret < 0)
732 return ret;
733 break;
734 };
735 }
736
737 if (map_pos_set && elem_id[0] + elem_id[1] + elem_id[2] != nb_elements) {
738 av_log(ac->avctx, AV_LOG_ERROR, "Element count does not match the "
739 "channel configuration\n");
740 usac->nb_elems = 0;
741 return AVERROR_INVALIDDATA;
742 }
743
744 ret = ff_aac_output_configure(ac, layout_map, elem_id[0] + elem_id[1] + elem_id[2],
745 OC_GLOBAL_HDR, 0);
746 if (ret < 0) {
747 av_log(avctx, AV_LOG_ERROR, "Unable to parse channel config!\n");
748 return ret;
749 }
750
751 if (get_bits1(gb)) { /* usacConfigExtensionPresent */
752 int invalid;
753 int nb_extensions = get_escaped_value(gb, 2, 4, 8) + 1; /* numConfigExtensions */
754 for (int i = 0; i < nb_extensions; i++) {
755 int type = get_escaped_value(gb, 4, 8, 16);
756 int len = get_escaped_value(gb, 4, 8, 16);
757 switch (type) {
759 ret = decode_loudness_set(ac, usac, gb);
760 if (ret < 0)
761 return ret;
762 break;
764 usac->stream_identifier = get_bits(gb, 16);
765 break;
766 case ID_CONFIG_EXT_FILL: /* fallthrough */
767 invalid = 0;
768 while (len--) {
769 if (get_bits(gb, 8) != 0xA5)
770 invalid++;
771 }
772 if (invalid)
773 av_log(avctx, AV_LOG_WARNING, "Invalid fill bytes: %i\n",
774 invalid);
775 break;
776 default:
777 while (len--)
778 skip_bits(gb, 8);
779 break;
780 }
781 }
782 }
783
785
787 if (usac->loudness.input_method_val >= 0)
788 av_log(avctx, AV_LOG_VERBOSE,
789 "USAC input loudness: %.2f LKFS (bsMethodValue=%d)\n",
790 -57.75f + 0.25f * usac->loudness.input_method_val,
792
793 usac->nb_elems = nb_elems;
794 ret = ff_aac_usac_reset_state(ac, oc);
795 if (ret < 0)
796 return ret;
797
798 return 0;
799}
800
803 GetBitContext *gb, uint8_t global_gain)
804{
805 IndividualChannelStream *ics = &sce->ics;
806
807 /* Decode all scalefactors. */
808 int offset_sf = global_gain;
809 for (int g = 0; g < ics->num_window_groups; g++) {
810 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
811 if (g || sfb)
812 offset_sf += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
813 if (offset_sf > 255U) {
815 "Scalefactor (%d) out of range.\n", offset_sf);
816 return AVERROR_INVALIDDATA;
817 }
818
819 sce->sfo[g*ics->max_sfb + sfb] = offset_sf - 100;
820 }
821 }
822
823 return 0;
824}
825
826/**
827 * Decode and dequantize arithmetically coded, uniformly quantized value
828 *
829 * @param coef array of dequantized, scaled spectral data
830 * @param sf array of scalefactors or intensity stereo positions
831 *
832 * @return Returns error status. 0 - OK, !0 - error
833 */
834static int decode_spectrum_ac(AACDecContext *s, float coef[1024],
836 int reset, uint16_t len, uint16_t N)
837{
838 AACArith ac;
839 int i, a, b;
840 uint32_t c;
841
842 int gb_count;
843 GetBitContext gb2;
844
845 c = ff_aac_ac_map_process(state, reset, N);
846
847 if (!len) {
849 return 0;
850 }
851
852 ff_aac_ac_init(&ac, gb);
853
854 /* Backup reader for rolling back by 14 bits at the end */
855 gb2 = *gb;
856 gb_count = get_bits_count(&gb2);
857
858 for (i = 0; i < len/2; i++) {
859 /* MSB */
860 int lvl, esc_nb, m;
862 for (lvl=esc_nb=0;;) {
863 uint32_t pki = ff_aac_ac_get_pk(c + (esc_nb << 17));
864 m = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_msb_cdfs[pki],
866 if (m < FF_AAC_AC_ESCAPE)
867 break;
868 lvl++;
869
870 /* Cargo-culted value. */
871 if (lvl > 23)
872 return AVERROR(EINVAL);
873
874 if ((esc_nb = lvl) > 7)
875 esc_nb = 7;
876 }
877
878 b = m >> 2;
879 a = m - (b << 2);
880
881 /* ARITH_STOP detection */
882 if (!m) {
883 if (esc_nb)
884 break;
885 a = b = 0;
886 }
887
888 /* LSB */
889 for (int l = lvl; l > 0; l--) {
890 int lsbidx = !a ? 1 : (!b ? 0 : 2);
891 uint8_t r = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_lsb_cdfs[lsbidx],
893 a = (a << 1) | (r & 1);
894 b = (b << 1) | ((r >> 1) & 1);
895 }
896
897 /* Dequantize coeffs here */
898 coef[2*i + 0] = a * cbrt(a);
899 coef[2*i + 1] = b * cbrt(b);
901 }
902
903 if (len > 1) {
904 /* "Rewind" bitstream back by 14 bits */
905 int gb_count2 = get_bits_count(&gb2);
906 skip_bits(gb, gb_count2 - gb_count - 14);
907 } else {
908 *gb = gb2;
909 }
910
912
913 for (; i < N/2; i++) {
914 coef[2*i + 0] = 0;
915 coef[2*i + 1] = 0;
916 }
917
918 /* Signs */
919 for (i = 0; i < len; i++) {
920 if (coef[i]) {
921 if (!get_bits1(gb)) /* s */
922 coef[i] *= -1;
923 }
924 }
925
926 return 0;
927}
928
931 int num_window_groups,
932 int prev_num_window_groups,
933 int indep_flag)
934{
935 int delta_code_time;
936 IndividualChannelStream *ics = &cpe->ch[0].ics;
937
938 if (!get_bits1(gb)) { /* cplx_pred_all */
939 for (int g = 0; g < num_window_groups; g++) {
940 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
941 const uint8_t val = get_bits1(gb);
942 us->pred_used[g*cpe->max_sfb_ste + sfb] = val;
943 if ((sfb + 1) < cpe->max_sfb_ste)
944 us->pred_used[g*cpe->max_sfb_ste + sfb + 1] = val;
945 }
946 }
947 } else {
948 for (int g = 0; g < num_window_groups; g++)
949 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
950 us->pred_used[g*cpe->max_sfb_ste + sfb] = 1;
951 }
952
953 us->pred_dir = get_bits1(gb);
954 us->complex_coef = get_bits1(gb);
955
956 us->use_prev_frame = 0;
957 if (us->complex_coef && !indep_flag)
958 us->use_prev_frame = get_bits1(gb);
959
960 delta_code_time = 0;
961 if (!indep_flag)
962 delta_code_time = get_bits1(gb);
963
964 /* TODO: shouldn't be needed */
965 for (int g = 0; g < num_window_groups; g++) {
966 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
967 float last_alpha_q_re = 0;
968 float last_alpha_q_im = 0;
969 if (delta_code_time) {
970 if (g) {
971 /* Transient, after the first group - use the current frame,
972 * previous window, alpha values. */
973 last_alpha_q_re = us->alpha_q_re[(g - 1)*cpe->max_sfb_ste + sfb];
974 last_alpha_q_im = us->alpha_q_im[(g - 1)*cpe->max_sfb_ste + sfb];
975 } else if (!g &&
978 /* The spec doesn't explicitly mention this, but it doesn't make
979 * any other sense otherwise! */
980 const int wg = prev_num_window_groups - 1;
981 last_alpha_q_re = us->prev_alpha_q_re[wg*cpe->max_sfb_ste + sfb];
982 last_alpha_q_im = us->prev_alpha_q_im[wg*cpe->max_sfb_ste + sfb];
983 } else {
984 last_alpha_q_re = us->prev_alpha_q_re[g*cpe->max_sfb_ste + sfb];
985 last_alpha_q_im = us->prev_alpha_q_im[g*cpe->max_sfb_ste + sfb];
986 }
987 } else {
988 if (sfb) {
989 last_alpha_q_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb - 1];
990 last_alpha_q_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb - 1];
991 }
992 }
993
994 if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
995 int val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
996 last_alpha_q_re += val * 0.1f;
997 if (us->complex_coef) {
998 val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60;
999 last_alpha_q_im += val * 0.1f;
1000 }
1001 us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = last_alpha_q_re;
1002 us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = last_alpha_q_im;
1003 } else {
1004 us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = 0;
1005 us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = 0;
1006 }
1007
1008 if ((sfb + 1) < cpe->max_sfb_ste) {
1009 us->alpha_q_re[g*cpe->max_sfb_ste + sfb + 1] =
1010 us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
1011 us->alpha_q_im[g*cpe->max_sfb_ste + sfb + 1] =
1012 us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
1013 }
1014 }
1015 }
1016
1017 return 0;
1018}
1019
1021 AACUSACConfig *usac)
1022{
1023 AACUsacElemData *ue = &sce->ue;
1024 IndividualChannelStream *ics = &sce->ics;
1025 const int sampling_index = ac->oc[1].m4ac.sampling_index;
1026
1027 /* Setup window parameters */
1029 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1030 if (usac->core_frame_len == 768) {
1031 ics->swb_offset = ff_swb_offset_96[sampling_index];
1032 ics->num_swb = ff_aac_num_swb_96[sampling_index];
1033 } else {
1034 ics->swb_offset = ff_swb_offset_128[sampling_index];
1035 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1036 }
1037 ics->tns_max_bands = ff_tns_max_bands_usac_128[sampling_index];
1038
1039 /* Setup scalefactor grouping. 7 bit mask. */
1040 ics->num_window_groups = 0;
1041 for (int j = 0; j < 7; j++) {
1042 ics->group_len[j] = 1;
1043 if (ue->scale_factor_grouping & (1 << (6 - j)))
1044 ics->group_len[ics->num_window_groups] += 1;
1045 else
1046 ics->num_window_groups++;
1047 }
1048
1049 ics->group_len[7] = 1;
1050 ics->num_window_groups++;
1051 ics->num_windows = 8;
1052 } else {
1053 if (usac->core_frame_len == 768) {
1054 ics->swb_offset = ff_swb_offset_768[sampling_index];
1055 ics->num_swb = ff_aac_num_swb_768[sampling_index];
1056 } else {
1057 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1058 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1059 }
1060 ics->tns_max_bands = ff_tns_max_bands_usac_1024[sampling_index];
1061
1062 ics->group_len[0] = 1;
1063 ics->num_window_groups = 1;
1064 ics->num_windows = 1;
1065 }
1066
1067 if (ics->max_sfb > ics->num_swb) {
1069 "Number of scalefactor bands in group (%d) "
1070 "exceeds limit (%d).\n",
1071 ics->max_sfb, ics->num_swb);
1072 ics->max_sfb = 0;
1073 return AVERROR(EINVAL);
1074 }
1075
1076 /* Just some defaults for the band types */
1077 for (int i = 0; i < FF_ARRAY_ELEMS(sce->band_type); i++)
1078 sce->band_type[i] = ESC_BT;
1079
1080 return 0;
1081}
1082
1085 GetBitContext *gb, int indep_flag)
1086{
1087 int ret, tns_active;
1088
1089 AACUsacStereo *us = &cpe->us;
1090 SingleChannelElement *sce1 = &cpe->ch[0];
1091 SingleChannelElement *sce2 = &cpe->ch[1];
1092 IndividualChannelStream *ics1 = &sce1->ics;
1093 IndividualChannelStream *ics2 = &sce2->ics;
1094 AACUsacElemData *ue1 = &sce1->ue;
1095 AACUsacElemData *ue2 = &sce2->ue;
1096
1097 us->common_window = 0;
1098 us->common_tw = 0;
1099
1100 /* Alpha values must always be zeroed out for the current frame,
1101 * as they are propagated to the next frame and may be used. */
1102 memset(us->alpha_q_re, 0, sizeof(us->alpha_q_re));
1103 memset(us->alpha_q_im, 0, sizeof(us->alpha_q_im));
1104
1105 if (!(!ue1->core_mode && !ue2->core_mode))
1106 return 0;
1107
1108 tns_active = get_bits1(gb);
1109 us->common_window = get_bits1(gb);
1110
1111 if (!us->common_window || indep_flag) {
1112 memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
1113 memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
1114 }
1115
1116 if (us->common_window) {
1117 /* ics_info() */
1118 ics1->window_sequence[1] = ics1->window_sequence[0];
1119 ics2->window_sequence[1] = ics2->window_sequence[0];
1120 ics1->window_sequence[0] = ics2->window_sequence[0] = get_bits(gb, 2);
1121
1122 ics1->use_kb_window[1] = ics1->use_kb_window[0];
1123 ics2->use_kb_window[1] = ics2->use_kb_window[0];
1124 ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb);
1125
1126 /* If there's a change in the transform sequence, zero out last frame's
1127 * stereo prediction coefficients */
1128 if ((ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE &&
1136 memset(us->prev_alpha_q_re, 0, sizeof(us->prev_alpha_q_re));
1137 memset(us->prev_alpha_q_im, 0, sizeof(us->prev_alpha_q_im));
1138 }
1139
1140 if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1141 ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4);
1143 } else {
1144 ics1->max_sfb = ics2->max_sfb = get_bits(gb, 6);
1145 }
1146
1147 if (!get_bits1(gb)) { /* common_max_sfb */
1148 if (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE)
1149 ics2->max_sfb = get_bits(gb, 4);
1150 else
1151 ics2->max_sfb = get_bits(gb, 6);
1152 }
1153
1154 ret = setup_sce(ac, sce1, usac);
1155 if (ret < 0) {
1156 ics2->max_sfb = 0;
1157 return ret;
1158 }
1159
1160 ret = setup_sce(ac, sce2, usac);
1161 if (ret < 0)
1162 return ret;
1163
1164 cpe->max_sfb_ste = FFMAX(ics1->max_sfb, ics2->max_sfb);
1165
1166 us->ms_mask_mode = get_bits(gb, 2); /* ms_mask_present */
1167 memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
1168 if (us->ms_mask_mode == 1) {
1169 for (int g = 0; g < ics1->num_window_groups; g++)
1170 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++)
1171 cpe->ms_mask[g*cpe->max_sfb_ste + sfb] = get_bits1(gb);
1172 } else if (us->ms_mask_mode == 2) {
1173 memset(cpe->ms_mask, 0xFF, sizeof(cpe->ms_mask));
1174 } else if ((us->ms_mask_mode == 3) && !ec->stereo_config_index) {
1175 ret = decode_usac_stereo_cplx(ac, us, cpe, gb,
1176 ics1->num_window_groups,
1178 indep_flag);
1179 if (ret < 0)
1180 return ret;
1181 }
1182 }
1183
1184 if (ec->tw_mdct) {
1185 us->common_tw = get_bits1(gb);
1187 "AAC USAC timewarping");
1188 return AVERROR_PATCHWELCOME;
1189 }
1190
1191 us->tns_on_lr = 0;
1192 ue1->tns_data_present = ue2->tns_data_present = 0;
1193 if (tns_active) {
1194 int common_tns = 0;
1195 if (us->common_window)
1196 common_tns = get_bits1(gb);
1197
1198 us->tns_on_lr = get_bits1(gb);
1199 if (common_tns) {
1200 ret = ff_aac_decode_tns(ac, &sce1->tns, gb, ics1);
1201 if (ret < 0)
1202 return ret;
1203 memcpy(&sce2->tns, &sce1->tns, sizeof(sce1->tns));
1204 sce2->tns.present = 1;
1205 sce1->tns.present = 1;
1206 ue1->tns_data_present = 0;
1207 ue2->tns_data_present = 0;
1208 } else {
1209 if (get_bits1(gb)) {
1210 ue1->tns_data_present = 1;
1211 ue2->tns_data_present = 1;
1212 } else {
1213 ue2->tns_data_present = get_bits1(gb);
1215 }
1216 }
1217 }
1218
1219 return 0;
1220}
1221
1222/* 7.2.4 Generation of random signs for spectral noise filling
1223 * This function is exactly defined, though we've helped the definition
1224 * along with being slightly faster. */
1225static inline float noise_random_sign(unsigned int *seed)
1226{
1227 unsigned int new_seed = *seed = ((*seed) * 69069) + 5;
1228 if (((new_seed) & 0x10000) > 0)
1229 return -1.f;
1230 return +1.f;
1231}
1232
1235{
1236 float *coef;
1237 IndividualChannelStream *ics = &sce->ics;
1238
1239 float noise_val = powf(2, ((float)ue->noise.level - 14.0f)/3.0f);
1240 int noise_offset = ue->noise.offset - 16;
1241 int band_off;
1242
1245
1246 coef = sce->coeffs;
1247 for (int g = 0; g < ics->num_window_groups; g++) {
1248 unsigned g_len = ics->group_len[g];
1249
1250 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1251 float *cb = coef + ics->swb_offset[sfb];
1252 int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1253 int band_quantized_to_zero = 1;
1254
1255 if (ics->swb_offset[sfb] < band_off)
1256 continue;
1257
1258 for (int group = 0; group < (unsigned)g_len; group++, cb += 128) {
1259 for (int z = 0; z < cb_len; z++) {
1260 if (cb[z] == 0)
1261 cb[z] = noise_random_sign(&sce->ue.noise.seed) * noise_val;
1262 else
1263 band_quantized_to_zero = 0;
1264 }
1265 }
1266
1267 if (band_quantized_to_zero) {
1268 sce->sfo[g*ics->max_sfb + sfb] = FFMAX(sce->sfo[g*ics->max_sfb + sfb] + noise_offset, -200);
1269 }
1270 }
1271 coef += g_len << 7;
1272 }
1273}
1274
1277{
1278 IndividualChannelStream *ics = &sce->ics;
1279 float *coef;
1280
1281 /* Synthesise noise */
1282 if (ue->noise.level)
1283 apply_noise_fill(ac, sce, ue);
1284
1285 /* Noise filling may apply an offset to the scalefactor offset */
1286 ac->dsp.dequant_scalefactors(sce);
1287
1288 /* Apply scalefactors */
1289 coef = sce->coeffs;
1290 for (int g = 0; g < ics->num_window_groups; g++) {
1291 unsigned g_len = ics->group_len[g];
1292
1293 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1294 float *cb = coef + ics->swb_offset[sfb];
1295 int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb];
1296 float sf = sce->sf[g*ics->max_sfb + sfb];
1297
1298 for (int group = 0; group < (unsigned)g_len; group++, cb += 128)
1299 ac->fdsp->vector_fmul_scalar(cb, cb, sf, cb_len);
1300 }
1301 coef += g_len << 7;
1302 }
1303}
1304
1306 float *dmix_re)
1307{
1308 IndividualChannelStream *ics = &cpe->ch[0].ics;
1309 int sign = !cpe->us.pred_dir ? +1 : -1;
1310 float *coef1 = cpe->ch[0].coeffs;
1311 float *coef2 = cpe->ch[1].coeffs;
1312
1313 for (int g = 0; g < ics->num_window_groups; g++) {
1314 unsigned g_len = ics->group_len[g];
1315 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1316 int off = ics->swb_offset[sfb];
1317 int cb_len = ics->swb_offset[sfb + 1] - off;
1318
1319 float *c1 = coef1 + off;
1320 float *c2 = coef2 + off;
1321 float *dm = dmix_re + off;
1322
1323 for (int group = 0; group < (unsigned)g_len;
1324 group++, c1 += 128, c2 += 128, dm += 128) {
1325 for (int z = 0; z < cb_len; z++)
1326 dm[z] = 0.5*(c1[z] + sign*c2[z]);
1327 }
1328 }
1329
1330 coef1 += g_len << 7;
1331 coef2 += g_len << 7;
1332 dmix_re += g_len << 7;
1333 }
1334}
1335
1337 float *dmix_re)
1338{
1339 AACUsacStereo *us = &cpe->us;
1340 IndividualChannelStream *ics = &cpe->ch[0].ics;
1341 int sign = !cpe->us.pred_dir ? +1 : -1;
1342 float *coef1 = cpe->ch[0].coeffs;
1343 float *coef2 = cpe->ch[1].coeffs;
1344
1345 for (int g = 0; g < ics->num_window_groups; g++) {
1346 unsigned g_len = ics->group_len[g];
1347 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1348 int off = ics->swb_offset[sfb];
1349 int cb_len = ics->swb_offset[sfb + 1] - off;
1350
1351 float *c1 = coef1 + off;
1352 float *c2 = coef2 + off;
1353 float *dm = dmix_re + off;
1354
1355 if (us->pred_used[g*cpe->max_sfb_ste + sfb]) {
1356 for (int group = 0; group < (unsigned)g_len;
1357 group++, c1 += 128, c2 += 128, dm += 128) {
1358 for (int z = 0; z < cb_len; z++)
1359 dm[z] = 0.5*(c1[z] + sign*c2[z]);
1360 }
1361 } else {
1362 for (int group = 0; group < (unsigned)g_len;
1363 group++, c1 += 128, c2 += 128, dm += 128) {
1364 for (int z = 0; z < cb_len; z++)
1365 dm[z] = c1[z];
1366 }
1367 }
1368 }
1369
1370 coef1 += g_len << 7;
1371 coef2 += g_len << 7;
1372 dmix_re += g_len << 7;
1373 }
1374}
1375
1376static void complex_stereo_interpolate_imag(float *im, float *re, const float f[7],
1377 int len, int factor_even, int factor_odd)
1378{
1379 int i = 0;
1380 float s;
1381
1382 s = f[6]*re[2] + f[5]*re[1] + f[4]*re[0] +
1383 f[3]*re[0] +
1384 f[2]*re[1] + f[1]*re[2] + f[0]*re[3];
1385 im[i] += s*factor_even;
1386
1387 i = 1;
1388 s = f[6]*re[1] + f[5]*re[0] + f[4]*re[0] +
1389 f[3]*re[1] +
1390 f[2]*re[2] + f[1]*re[3] + f[0]*re[4];
1391 im[i] += s*factor_odd;
1392
1393 i = 2;
1394 s = f[6]*re[0] + f[5]*re[0] + f[4]*re[1] +
1395 f[3]*re[2] +
1396 f[2]*re[3] + f[1]*re[4] + f[0]*re[5];
1397
1398 im[i] += s*factor_even;
1399 for (i = 3; i < len - 4; i += 2) {
1400 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1401 f[3]*re[i] +
1402 f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+3];
1403 im[i+0] += s*factor_odd;
1404
1405 s = f[6]*re[i-2] + f[5]*re[i-1] + f[4]*re[i] +
1406 f[3]*re[i+1] +
1407 f[2]*re[i+2] + f[1]*re[i+3] + f[0]*re[i+4];
1408 im[i+1] += s*factor_even;
1409 }
1410
1411 i = len - 3;
1412 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1413 f[3]*re[i] +
1414 f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+2];
1415 im[i] += s*factor_odd;
1416
1417 i = len - 2;
1418 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1419 f[3]*re[i] +
1420 f[2]*re[i+1] + f[1]*re[i+1] + f[0]*re[i];
1421 im[i] += s*factor_even;
1422
1423 i = len - 1;
1424 s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] +
1425 f[3]*re[i] +
1426 f[2]*re[i] + f[1]*re[i-1] + f[0]*re[i-2];
1427 im[i] += s*factor_odd;
1428}
1429
1431{
1432 AACUsacStereo *us = &cpe->us;
1433 IndividualChannelStream *ics = &cpe->ch[0].ics;
1434 float *coef1 = cpe->ch[0].coeffs;
1435 float *coef2 = cpe->ch[1].coeffs;
1436 float *dmix_im = us->dmix_im;
1437
1438 for (int g = 0; g < ics->num_window_groups; g++) {
1439 unsigned g_len = ics->group_len[g];
1440 for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) {
1441 int off = ics->swb_offset[sfb];
1442 int cb_len = ics->swb_offset[sfb + 1] - off;
1443
1444 float *c1 = coef1 + off;
1445 float *c2 = coef2 + off;
1446 float *dm_im = dmix_im + off;
1447 float alpha_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb];
1448 float alpha_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb];
1449
1450 if (!us->pred_used[g*cpe->max_sfb_ste + sfb])
1451 continue;
1452
1453 if (!cpe->us.pred_dir) {
1454 for (int group = 0; group < (unsigned)g_len;
1455 group++, c1 += 128, c2 += 128, dm_im += 128) {
1456 for (int z = 0; z < cb_len; z++) {
1457 float side;
1458 side = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1459 c2[z] = c1[z] - side;
1460 c1[z] = c1[z] + side;
1461 }
1462 }
1463 } else {
1464 for (int group = 0; group < (unsigned)g_len;
1465 group++, c1 += 128, c2 += 128, dm_im += 128) {
1466 for (int z = 0; z < cb_len; z++) {
1467 float mid;
1468 mid = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z];
1469 c2[z] = mid - c1[z];
1470 c1[z] = mid + c1[z];
1471 }
1472 }
1473 }
1474 }
1475
1476 coef1 += g_len << 7;
1477 coef2 += g_len << 7;
1478 dmix_im += g_len << 7;
1479 }
1480}
1481
1482static const float *complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
1483{
1484 int win, shape;
1485 if (!is_prev) {
1486 switch (cpe->ch[0].ics.window_sequence[0]) {
1487 default:
1488 case ONLY_LONG_SEQUENCE:
1490 win = 0;
1491 break;
1493 win = 1;
1494 break;
1495 case LONG_STOP_SEQUENCE:
1496 win = 2;
1497 break;
1498 }
1499
1500 if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1501 cpe->ch[0].ics.use_kb_window[1] == 0)
1502 shape = 0;
1503 else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1504 cpe->ch[0].ics.use_kb_window[1] == 1)
1505 shape = 1;
1506 else if (cpe->ch[0].ics.use_kb_window[0] == 0 &&
1507 cpe->ch[0].ics.use_kb_window[1] == 1)
1508 shape = 2;
1509 else if (cpe->ch[0].ics.use_kb_window[0] == 1 &&
1510 cpe->ch[0].ics.use_kb_window[1] == 0)
1511 shape = 3;
1512 else
1513 shape = 3;
1514 } else {
1516 shape = cpe->ch[0].ics.use_kb_window[1];
1517 }
1518
1519 return ff_aac_usac_mdst_filt_cur[win][shape];
1520}
1521
1523 ChannelElement *cpe, int nb_channels)
1524{
1525 AACUsacStereo *us = &cpe->us;
1526
1527 for (int ch = 0; ch < nb_channels; ch++) {
1528 SingleChannelElement *sce = &cpe->ch[ch];
1529 AACUsacElemData *ue = &sce->ue;
1530
1531 if (!ue->core_mode)
1532 spectrum_scale(ac, sce, ue);
1533 }
1534
1535 if (nb_channels > 1 && us->common_window) {
1536 for (int ch = 0; ch < nb_channels; ch++) {
1537 SingleChannelElement *sce = &cpe->ch[ch];
1538
1539 /* Apply TNS, if the tns_on_lr bit is not set. */
1540 if (sce->tns.present && !us->tns_on_lr)
1541 ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1542 }
1543
1544 if (us->ms_mask_mode == 3) {
1545 const float *filt;
1546 complex_stereo_downmix_cur(ac, cpe, us->dmix_re);
1547 complex_stereo_downmix_prev(ac, cpe, us->prev_dmix_re);
1548
1550 complex_stereo_interpolate_imag(us->dmix_im, us->dmix_re, filt,
1551 usac->core_frame_len, 1, 1);
1552 if (us->use_prev_frame) {
1554 complex_stereo_interpolate_imag(us->dmix_im, us->prev_dmix_re, filt,
1555 usac->core_frame_len, -1, 1);
1556 }
1557
1558 apply_complex_stereo(ac, cpe);
1559 } else if (us->ms_mask_mode > 0) {
1560 ac->dsp.apply_mid_side_stereo(ac, cpe);
1561 }
1562 }
1563
1564 /* Save coefficients and alpha values for prediction reasons */
1565 if (nb_channels > 1) {
1566 AACUsacStereo *us2 = &cpe->us;
1567 for (int ch = 0; ch < nb_channels; ch++) {
1568 SingleChannelElement *sce = &cpe->ch[ch];
1569 memcpy(sce->prev_coeffs, sce->coeffs, sizeof(sce->coeffs));
1570 }
1571 memcpy(us2->prev_alpha_q_re, us2->alpha_q_re, sizeof(us2->alpha_q_re));
1572 memcpy(us2->prev_alpha_q_im, us2->alpha_q_im, sizeof(us2->alpha_q_im));
1573 }
1574
1575 for (int ch = 0; ch < nb_channels; ch++) {
1576 SingleChannelElement *sce = &cpe->ch[ch];
1577
1578 /* Apply TNS, if it hasn't been applied yet. */
1579 if (sce->tns.present && ((nb_channels == 1) || (us->tns_on_lr)))
1580 ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1);
1581
1582 if (!sce->ue.core_mode)
1583 ac->oc[1].m4ac.frame_length_short ? ac->dsp.imdct_and_windowing_768(ac, sce) :
1584 ac->dsp.imdct_and_windowing(ac, sce);
1585 }
1586}
1587
1588static const uint8_t mps_fr_nb_bands[8] = {
1589 255 /* Reserved */, 28, 20, 14, 10, 7, 5, 4,
1590};
1591
1592static const uint8_t mps_fr_stride_smg[4] = {
1593 1, 2, 5, 28,
1594};
1595
1596static void decode_tsd(GetBitContext *gb, int *data,
1597 int nb_tr_slots, int nb_slots)
1598{
1599 int nb_bits = av_log2(nb_slots / (nb_tr_slots + 1));
1600 int s = get_bits(gb, nb_bits);
1601 for (int k = 0; k < nb_slots; k++)
1602 data[k]=0;
1603
1604 int p = nb_tr_slots + 1;
1605 for (int k = nb_slots - 1; k >= 0; k--) {
1606 if (p > k) {
1607 for (; k >= 0; k--)
1608 data[k] = 1;
1609 break;
1610 }
1611 int64_t c = k - p + 1;
1612 for (int h = 2; h <= p && c <= s; h++) {
1613 c += c*(k-p)/h;
1614 }
1615 if (s >= c) {
1616 s -= c;
1617 data[k] = 1;
1618 p--;
1619 if (!p)
1620 break;
1621 }
1622 }
1623}
1624
1627 GetBitContext *gb, int frame_indep_flag)
1628{
1629 int err;
1630 int nb_bands = mps_fr_nb_bands[ec->mps.freq_res];
1631
1632 /* Framing info */
1633 mps->framing_type = 0;
1634 mps->nb_param_sets = 2;
1635 if (ec->mps.high_rate_mode) {
1636 mps->framing_type = get_bits1(gb);
1637 mps->nb_param_sets = get_bits(gb, 3) + 1;
1638 }
1639 int param_slot_bits = usac->core_sbr_frame_len_idx == 4 ? 6 : 5;
1640 int nb_time_slots = usac->core_sbr_frame_len_idx == 4 ? 64 : 32;
1641
1642 if (mps->framing_type)
1643 for (int i = 0; i < mps->nb_param_sets; i++)
1644 mps->param_sets[i] = get_bits(gb, param_slot_bits);
1645
1646 int indep = frame_indep_flag;
1647 if (!frame_indep_flag)
1648 indep = get_bits1(gb);
1649
1650 int extend_frame = mps->param_sets[mps->nb_param_sets - 1] !=
1651 (nb_time_slots - 1);
1652
1653 /* CLD */
1654 err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_CLD], MPS_CLD,
1655 0, 0, nb_bands,
1656 indep, indep, mps->nb_param_sets);
1657 if (err < 0) {
1658 av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT CLD data!\n");
1659 return err;
1660 }
1662 0, 0, nb_bands, mps->nb_param_sets,
1663 mps->param_sets, extend_frame);
1664
1665 /* ICC */
1666 err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_ICC], MPS_ICC, 0, 0, nb_bands,
1667 indep, indep, mps->nb_param_sets);
1668 if (err < 0) {
1669 av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT ICC data!\n");
1670 return err;
1671 }
1673 0, 0, nb_bands, mps->nb_param_sets,
1674 mps->param_sets, extend_frame);
1675
1676 /* IPD */
1677 if (ec->mps.phase_coding) {
1678 if (get_bits1(gb)) {
1679 mps->opd_smoothing_mode = get_bits1(gb);
1680 err = ff_aac_ec_data_dec(gb, &mps->ott[MPS_IPD], MPS_IPD, 0, 0,
1682 indep, indep, mps->nb_param_sets);
1684 0, 0, nb_bands, mps->nb_param_sets,
1685 mps->param_sets, extend_frame);
1686 if (err < 0) {
1687 av_log(ac->avctx, AV_LOG_ERROR, "Error parsing OTT IPD data!\n");
1688 return err;
1689 }
1690 }
1691 }
1692
1693 /* SMG data */
1694 memset(mps->smooth_mode, 0, sizeof(mps->smooth_mode));
1695 if (ec->mps.high_rate_mode) {
1696 for (int i = 0; i < mps->nb_param_sets; i++) {
1697 mps->smooth_mode[i] = get_bits(gb, 2);
1698 if (mps->smooth_mode[i] >= 2)
1699 mps->smooth_time[i] = get_bits(gb, 2);
1700 if (mps->smooth_mode[i] >= 3) {
1701 mps->freq_res_stride_smg[i] = get_bits(gb, 2);
1702 int nb_data_bands = (nb_bands - 1);
1703 nb_data_bands /= (mps_fr_stride_smg[mps->freq_res_stride_smg[i]] + 1);
1704 for (int j = 0; j < nb_data_bands; j++)
1705 mps->smg_data[i][j] = get_bits1(gb);
1706 }
1707 }
1708 }
1709
1710 /* Temp shape data */
1711 mps->tsd_enable = 0;
1712 if (ec->mps.temp_shape_config == 3) {
1713 mps->tsd_enable = get_bits1(gb);
1714 } else if (ec->mps.temp_shape_config) {
1715 mps->temp_shape_enable = get_bits1(gb);
1716 if (mps->temp_shape_enable) {
1717 for (int i = 0; i < 2; i++)
1718 mps->temp_shape_enable_ch[i] = get_bits1(gb);
1719 if (ec->mps.temp_shape_config == 2) {
1720 err = ff_aac_huff_dec_reshape(gb, mps->temp_shape_data, 16);
1721 if (err < 0) {
1723 "Error parsing TSD reshape data!\n");
1724 return err;
1725 }
1726 }
1727 }
1728 }
1729
1730 /* TSD data */
1731 if (mps->tsd_enable) {
1732 mps->tsd_num_tr_slots = get_bits(gb, param_slot_bits - 1);
1733 int tsd_pos[64];
1734 decode_tsd(gb, tsd_pos, mps->tsd_num_tr_slots, nb_time_slots);
1735 for (int i = 0; i < nb_time_slots; i++) {
1736 mps->tsd_phase_data[i] = 0;
1737 if (tsd_pos[i])
1738 mps->tsd_phase_data[i] = get_bits(gb, 3);
1739 }
1740 }
1741
1742 return 0;
1743}
1744
1747 GetBitContext *gb, int indep_flag, int nb_channels)
1748{
1749 int ret;
1750 int arith_reset_flag;
1751 AACUsacStereo *us = &che->us;
1752 int core_nb_channels = nb_channels;
1753
1754 /* Local symbols */
1755 uint8_t global_gain;
1756
1757 us->common_window = 0;
1758
1759 for (int ch = 0; ch < core_nb_channels; ch++) {
1760 SingleChannelElement *sce = &che->ch[ch];
1761 AACUsacElemData *ue = &sce->ue;
1762
1763 sce->tns.present = 0;
1764 ue->tns_data_present = 0;
1765
1766 ue->core_mode = get_bits1(gb);
1767 }
1768
1769 if (nb_channels > 1 && ec->stereo_config_index == 1)
1770 core_nb_channels = 1;
1771
1772 if (core_nb_channels == 2) {
1773 ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag);
1774 if (ret)
1775 return ret;
1776 }
1777
1778 for (int ch = 0; ch < core_nb_channels; ch++) {
1779 SingleChannelElement *sce = &che->ch[ch];
1780 IndividualChannelStream *ics = &sce->ics;
1781 AACUsacElemData *ue = &sce->ue;
1782
1783 if (ue->core_mode) { /* lpd_channel_stream */
1784 ret = ff_aac_ldp_parse_channel_stream(ac, usac, ue, gb);
1785 if (ret < 0)
1786 return ret;
1787 continue;
1788 }
1789
1790 if ((core_nb_channels == 1) ||
1791 (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode))
1792 ue->tns_data_present = get_bits1(gb);
1793
1794 /* fd_channel_stream */
1795 global_gain = get_bits(gb, 8);
1796
1797 ue->noise.level = 0;
1798 if (ec->noise_fill) {
1799 ue->noise.level = get_bits(gb, 3);
1800 ue->noise.offset = get_bits(gb, 5);
1801 }
1802
1803 if (!us->common_window) {
1804 /* ics_info() */
1805 ics->window_sequence[1] = ics->window_sequence[0];
1806 ics->window_sequence[0] = get_bits(gb, 2);
1807 ics->use_kb_window[1] = ics->use_kb_window[0];
1808 ics->use_kb_window[0] = get_bits1(gb);
1809 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1810 ics->max_sfb = get_bits(gb, 4);
1811 ue->scale_factor_grouping = get_bits(gb, 7);
1812 } else {
1813 ics->max_sfb = get_bits(gb, 6);
1814 }
1815
1816 ret = setup_sce(ac, sce, usac);
1817 if (ret < 0)
1818 return ret;
1819 }
1820
1821 if (ec->tw_mdct && !us->common_tw) {
1822 /* tw_data() */
1823 if (get_bits1(gb)) { /* tw_data_present */
1824 /* Time warping is not supported in baseline profile streams. */
1826 "AAC USAC timewarping");
1827 return AVERROR_PATCHWELCOME;
1828 }
1829 }
1830
1831 ret = decode_usac_scale_factors(ac, sce, gb, global_gain);
1832 if (ret < 0)
1833 return ret;
1834
1835 if (ue->tns_data_present) {
1836 sce->tns.present = 1;
1837 ret = ff_aac_decode_tns(ac, &sce->tns, gb, ics);
1838 if (ret < 0)
1839 return ret;
1840 }
1841
1842 /* ac_spectral_data */
1843 arith_reset_flag = indep_flag;
1844 if (!arith_reset_flag)
1845 arith_reset_flag = get_bits1(gb);
1846
1847 /* Decode coeffs */
1848 memset(&sce->coeffs[0], 0, 1024*sizeof(float));
1849 for (int win = 0; win < ics->num_windows; win++) {
1850 int lg = ics->swb_offset[ics->max_sfb];
1851 int N;
1853 N = usac->core_frame_len / 8;
1854 else
1855 N = usac->core_frame_len;
1856
1857 ret = decode_spectrum_ac(ac, sce->coeffs + win*128, gb, &ue->ac,
1858 arith_reset_flag && (win == 0), lg, N);
1859 if (ret < 0)
1860 return ret;
1861 }
1862
1863 if (get_bits1(gb)) { /* fac_data_present */
1864 const uint16_t len_8 = usac->core_frame_len / 8;
1865 const uint16_t len_16 = usac->core_frame_len / 16;
1866 const uint16_t fac_len = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ?
1867 len_16 : len_8;
1868 ret = ff_aac_parse_fac_data(ue, gb, 1, fac_len);
1869 if (ret < 0)
1870 return ret;
1871 }
1872 }
1873
1874 if (ec->sbr.ratio) {
1875 int sbr_ch = nb_channels;
1876 if (nb_channels == 2 &&
1877 !(ec->stereo_config_index == 0 || ec->stereo_config_index == 3))
1878 sbr_ch = 1;
1879
1880 ret = ff_aac_sbr_decode_usac_data(ac, che, ec, gb, sbr_ch, indep_flag);
1881 if (ret < 0)
1882 return ret;
1883 }
1884
1885 if (ec->stereo_config_index) {
1886 ret = parse_mps212(ac, usac, &us->mps, ec, gb, indep_flag);
1887 if (ret < 0)
1888 return ret;
1889 }
1890
1891 spectrum_decode(ac, usac, che, core_nb_channels);
1892
1893 if (ac->oc[1].m4ac.sbr > 0) {
1894 ac->proc.sbr_apply(ac, che, nb_channels == 2 ? TYPE_CPE : TYPE_SCE, 0,
1895 che->ch[0].output,
1896 che->ch[1].output);
1897 }
1898
1899 return 0;
1900}
1901
1903{
1904 int ret = 0;
1905 GetBitContext gbc;
1906 OutputConfiguration *oc = &ac->oc[1];
1907 MPEG4AudioConfig *m4ac = &oc->m4ac;
1908 MPEG4AudioConfig m4ac_bak = oc->m4ac;
1909 uint8_t temp_data[512];
1910 uint8_t *tmp_buf = temp_data;
1911 size_t tmp_buf_size = sizeof(temp_data);
1912
1913 av_unused int crossfade;
1914 int num_preroll_frames;
1915
1916 int config_len = get_escaped_value(gb, 4, 4, 8);
1917
1918 /* Implementations are free to pad the config to any length, so use a
1919 * different reader for this. */
1920 gbc = *gb;
1921 ret = ff_aac_usac_config_decode(ac, ac->avctx, &gbc, oc, m4ac->chan_config);
1922 if (ret < 0) {
1923 *m4ac = m4ac_bak;
1924 return ret;
1925 } else {
1926 ac->oc[1].m4ac.chan_config = 0;
1927 }
1928
1929 /* 7.18.3.3 Bitrate adaption
1930 * If configuration didn't change after applying preroll, continue
1931 * without decoding it. */
1932 if (!memcmp(m4ac, &m4ac_bak, sizeof(m4ac_bak)))
1933 return 0;
1934
1935 skip_bits_long(gb, config_len*8);
1936
1937 crossfade = get_bits1(gb); /* applyCrossfade */
1938 skip_bits1(gb); /* reserved */
1939 num_preroll_frames = get_escaped_value(gb, 2, 4, 0); /* numPreRollFrames */
1940
1941 for (int i = 0; i < num_preroll_frames; i++) {
1942 int got_frame_ptr = 0;
1943 int au_len = get_escaped_value(gb, 16, 16, 0);
1944
1945 if (au_len*8 > tmp_buf_size) {
1946 uint8_t *tmp2;
1947 tmp_buf = tmp_buf == temp_data ? NULL : tmp_buf;
1948 tmp2 = av_realloc_array(tmp_buf, au_len, 8);
1949 if (!tmp2) {
1950 if (tmp_buf != temp_data)
1951 av_free(tmp_buf);
1952 return AVERROR(ENOMEM);
1953 }
1954 tmp_buf = tmp2;
1955 }
1956
1957 /* Byte alignment is not guaranteed. */
1958 for (int j = 0; j < au_len; j++)
1959 tmp_buf[j] = get_bits(gb, 8);
1960
1961 ret = init_get_bits8(&gbc, tmp_buf, au_len);
1962 if (ret < 0)
1963 break;
1964
1965 ret = ff_aac_usac_decode_frame(ac->avctx, ac, &gbc, &got_frame_ptr);
1966 if (ret < 0)
1967 break;
1968 }
1969
1970 if (tmp_buf != temp_data)
1971 av_free(tmp_buf);
1972
1973 return 0;
1974}
1975
1977 GetBitContext *gb)
1978{
1979 uint8_t pl_frag_start = 1;
1980 uint8_t pl_frag_end = 1;
1981 uint32_t len;
1982
1983 if (!get_bits1(gb)) /* usacExtElementPresent */
1984 return 0;
1985
1986 if (get_bits1(gb)) { /* usacExtElementUseDefaultLength */
1987 len = e->ext.default_len;
1988 } else {
1989 len = get_bits(gb, 8); /* usacExtElementPayloadLength */
1990 if (len == 255)
1991 len += get_bits(gb, 16) - 2;
1992 }
1993
1994 if (!len)
1995 return 0;
1996
1997 if (e->ext.payload_frag) {
1998 pl_frag_start = get_bits1(gb); /* usacExtElementStart */
1999 pl_frag_end = get_bits1(gb); /* usacExtElementStop */
2000 }
2001
2002 if (pl_frag_start)
2003 e->ext.pl_data_offset = 0;
2004
2005 /* If an extension starts and ends this packet, we can directly use it below.
2006 * Otherwise, we have to copy it to a buffer and accumulate it. */
2007 if (!(pl_frag_start && pl_frag_end)) {
2008 /* Reallocate the data */
2009 uint8_t *tmp_buf = av_refstruct_alloc_ext(e->ext.pl_data_offset + len,
2011 NULL, NULL);
2012 if (!tmp_buf)
2013 return AVERROR(ENOMEM);
2014
2015 /* Copy the data over only if we had saved data to begin with */
2016 if (e->ext.pl_buf)
2017 memcpy(tmp_buf, e->ext.pl_buf, e->ext.pl_data_offset);
2018
2020 e->ext.pl_buf = tmp_buf;
2021
2022 /* Readout data to a buffer */
2023 for (int i = 0; i < len; i++)
2024 e->ext.pl_buf[e->ext.pl_data_offset + i] = get_bits(gb, 8);
2025 }
2026
2027 e->ext.pl_data_offset += len;
2028
2029 if (pl_frag_end) {
2030 int ret = 0;
2031 int start_bits = get_bits_count(gb);
2032 const int pl_len = e->ext.pl_data_offset;
2033 GetBitContext *gb2 = gb;
2034 GetBitContext gbc;
2035 if (!(pl_frag_start && pl_frag_end)) {
2036 ret = init_get_bits8(&gbc, e->ext.pl_buf, pl_len);
2037 if (ret < 0)
2038 return ret;
2039
2040 gb2 = &gbc;
2041 }
2042
2043 switch (e->ext.type) {
2044 case ID_EXT_ELE_FILL:
2045 /* Filler elements have no usable payload */
2046 break;
2048 ret = parse_audio_preroll(ac, gb2);
2049 break;
2050 case ID_EXT_ELE_UNI_DRC:
2051 /* uniDrcGain() payload: DRC is not applied, just consume the
2052 * bits via skip_bits_long below. */
2053 break;
2054 default:
2055 /* This should never happen */
2056 av_assert0(0);
2057 }
2059 if (ret < 0)
2060 return ret;
2061
2062 skip_bits_long(gb, pl_len*8 - (get_bits_count(gb) - start_bits));
2063 }
2064
2065 return 0;
2066}
2067
2069 GetBitContext *gb, int *got_frame_ptr)
2070{
2071 int ret, is_dmono = 0;
2072 int indep_flag, samples = 0;
2073 int audio_found = 0;
2074 int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 };
2075 AVFrame *frame = ac->frame;
2076
2077 int ratio_mult, ratio_dec;
2078 AACUSACConfig *usac = &ac->oc[1].usac;
2079 int sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 :
2080 usac->core_sbr_frame_len_idx == 3 ? 3 :
2081 usac->core_sbr_frame_len_idx == 4 ? 1 :
2082 0;
2083
2084 if (sbr_ratio == 2) {
2085 ratio_mult = 8;
2086 ratio_dec = 3;
2087 } else if (sbr_ratio == 3) {
2088 ratio_mult = 2;
2089 ratio_dec = 1;
2090 } else if (sbr_ratio == 4) {
2091 ratio_mult = 4;
2092 ratio_dec = 1;
2093 } else {
2094 ratio_mult = 1;
2095 ratio_dec = 1;
2096 }
2097
2098 ret = ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2099 ac->oc[1].status, 0);
2100 if (ret < 0)
2101 return ret;
2102
2104
2105 indep_flag = get_bits1(gb);
2106
2107 for (int i = 0; i < ac->oc[1].usac.nb_elems; i++) {
2108 int layout_id;
2109 int layout_type;
2110 AACUsacElemConfig *e = &ac->oc[1].usac.elems[i];
2111 ChannelElement *che;
2112
2113 if (e->type == ID_USAC_SCE) {
2114 layout_id = elem_id[0]++;
2115 layout_type = TYPE_SCE;
2116 che = ff_aac_get_che(ac, TYPE_SCE, layout_id);
2117 } else if (e->type == ID_USAC_CPE) {
2118 layout_id = elem_id[1]++;
2119 layout_type = TYPE_CPE;
2120 che = ff_aac_get_che(ac, TYPE_CPE, layout_id);
2121 } else if (e->type == ID_USAC_LFE) {
2122 layout_id = elem_id[2]++;
2123 layout_type = TYPE_LFE;
2124 che = ff_aac_get_che(ac, TYPE_LFE, layout_id);
2125 }
2126
2127 if (e->type != ID_USAC_EXT && !che) {
2129 "channel element %d.%d is not allocated\n",
2130 layout_type, layout_id);
2131 return AVERROR_INVALIDDATA;
2132 }
2133
2134 switch (e->type) {
2135 case ID_USAC_LFE:
2136 /* Fallthrough */
2137 case ID_USAC_SCE:
2138 ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
2139 indep_flag, 1);
2140 if (ret < 0)
2141 return ret;
2142
2143 audio_found = 1;
2144 che->present = 1;
2145 break;
2146 case ID_USAC_CPE:
2147 ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb,
2148 indep_flag, 2);
2149 if (ret < 0)
2150 return ret;
2151
2152 audio_found = 1;
2153 che->present = 1;
2154 break;
2155 case ID_USAC_EXT:
2156 ret = parse_ext_ele(ac, e, gb);
2157 if (ret < 0)
2158 return ret;
2159 break;
2160 }
2161 }
2162
2163 if (audio_found)
2164 samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024;
2165
2166 samples = (samples * ratio_mult) / ratio_dec;
2167
2168 if (ac->oc[1].status && audio_found) {
2169 avctx->sample_rate = ac->oc[1].m4ac.ext_sample_rate;
2170 avctx->frame_size = samples;
2171 ac->oc[1].status = OC_LOCKED;
2172 }
2173
2174 if (!frame->data[0] && samples) {
2175 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2176 return AVERROR_INVALIDDATA;
2177 }
2178
2179 if (samples) {
2180 frame->nb_samples = samples;
2181 frame->sample_rate = avctx->sample_rate;
2182 frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
2183 *got_frame_ptr = 1;
2184 } else {
2185 av_frame_unref(ac->frame);
2186 frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0;
2187 *got_frame_ptr = 0;
2188 }
2189
2190 if (samples && ac->target_level) {
2191 int method_val = usac->loudness.input_method_val;
2192 if (method_val < 0) {
2193 if (!ac->warned_loudness_missing) {
2194 av_log(avctx, AV_LOG_WARNING,
2195 "target_level set but no program/anchor loudness "
2196 "measurement available; normalization skipped\n");
2198 }
2199 } else {
2200 /* Per ISO/IEC 23003-4 Table A.48: L = -57.75 + 0.25 * μ */
2201 float input_loudness = -57.75f + 0.25f * method_val;
2202 float gain_dB = (float)ac->target_level - input_loudness;
2203 float gain = powf(10.0f, gain_dB / 20.0f);
2204
2205 for (int ch = 0; ch < frame->ch_layout.nb_channels; ch++)
2206 ac->fdsp->vector_fmul_scalar((float *)frame->extended_data[ch],
2207 (float *)frame->extended_data[ch],
2208 gain, frame->nb_samples);
2209 }
2210 }
2211
2212 /* for dual-mono audio (SCE + SCE) */
2213 is_dmono = ac->dmono_mode && elem_id[0] == 2 &&
2216 if (is_dmono) {
2217 if (ac->dmono_mode == 1)
2218 frame->data[1] = frame->data[0];
2219 else if (ac->dmono_mode == 2)
2220 frame->data[0] = frame->data[1];
2221 }
2222
2223 return 0;
2224}
@ EIGHT_SHORT_SEQUENCE
Definition aac.h:66
@ LONG_STOP_SEQUENCE
Definition aac.h:67
@ ONLY_LONG_SEQUENCE
Definition aac.h:64
@ LONG_START_SEQUENCE
Definition aac.h:65
static int ff_aac_sample_rate_idx(int rate)
Definition aac.h:110
@ ESC_BT
Spectral data are coded with an escape sequence.
Definition aac.h:73
@ AAC_CHANNEL_LFE
Definition aac.h:85
@ AAC_CHANNEL_FRONT
Definition aac.h:82
RawDataBlockType
Definition aac.h:43
@ TYPE_CPE
Definition aac.h:45
@ TYPE_SCE
Definition aac.h:44
@ TYPE_LFE
Definition aac.h:47
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition aac.h:95
#define MAX_ELEM_ID
Definition aac.h:34
@ ID_EXT_ELE_AUDIOPREROLL
Definition aacdec.h:92
@ ID_EXT_ELE_SAOC
Definition aacdec.h:91
@ ID_EXT_ELE_UNI_DRC
Definition aacdec.h:93
@ ID_EXT_ELE_MPEGS
Definition aacdec.h:90
@ ID_EXT_ELE_FILL
Definition aacdec.h:89
AACUSACDRCExt
Definition aacdec.h:101
@ UNIDRCCONFEXT_TERM
Definition aacdec.h:102
@ ID_USAC_EXT
Definition aacdec.h:79
@ ID_USAC_CPE
Definition aacdec.h:77
@ ID_USAC_SCE
Definition aacdec.h:76
@ ID_USAC_LFE
Definition aacdec.h:78
AACUSACLoudnessExt
Definition aacdec.h:96
@ UNIDRCLOUDEXT_TERM
Definition aacdec.h:97
@ UNIDRCLOUDEXT_EQ
Definition aacdec.h:98
@ ID_CONFIG_EXT_LOUDNESS_INFO
Definition aacdec.h:84
@ ID_CONFIG_EXT_STREAM_ID
Definition aacdec.h:85
@ ID_CONFIG_EXT_FILL
Definition aacdec.h:83
@ 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
uint32_t ff_aac_ac_get_pk(uint32_t c)
Definition aacdec_ac.c:73
uint32_t ff_aac_ac_get_context(AACArithState *state, uint32_t c, int i, int N)
Definition aacdec_ac.c:57
uint16_t ff_aac_ac_decode(AACArith *ac, GetBitContext *gb, const uint16_t *cdf, uint16_t cdf_len)
Definition aacdec_ac.c:110
void ff_aac_ac_update_context(AACArithState *state, int idx, uint16_t a, uint16_t b)
Definition aacdec_ac.c:91
uint32_t ff_aac_ac_map_process(AACArithState *state, int reset, int N)
Definition aacdec_ac.c:25
void ff_aac_ac_finish(AACArithState *state, int offset, int N)
Definition aacdec_ac.c:196
void ff_aac_ac_init(AACArith *ac, GetBitContext *gb)
Definition aacdec_ac.c:103
#define FF_AAC_AC_ESCAPE
Definition aacdec_ac.h:40
int ff_aac_ldp_parse_channel_stream(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemData *ce, GetBitContext *gb)
Definition aacdec_lpd.c:112
int ff_aac_parse_fac_data(AACUsacElemData *ce, GetBitContext *gb, int use_gain, int len)
Definition aacdec_lpd.c:93
VLCElem ff_vlc_scalefactors[352]
Definition aacdec_tab.c:111
AAC decoder data.
static void decode_drc_instructions_basic(AACUsacElemConfig *e, GetBitContext *gb)
static int decode_usac_element_pair(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
static void complex_stereo_downmix_cur(AACDecContext *ac, ChannelElement *cpe, float *dmix_re)
int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac, GetBitContext *gb, int *got_frame_ptr)
static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemConfig *ec, ChannelElement *cpe, GetBitContext *gb, int indep_flag)
static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac, AACUsacElemConfig *ec, ChannelElement *che, GetBitContext *gb, int indep_flag, int nb_channels)
static int parse_audio_preroll(AACDecContext *ac, GetBitContext *gb)
static int decode_drc_channel_layout(GetBitContext *gb)
static void apply_complex_stereo(AACDecContext *ac, ChannelElement *cpe)
static void skip_drc_downmix_instructions(GetBitContext *gb, int base_channel_count)
static const float * complex_stereo_get_filter(ChannelElement *cpe, int is_prev)
static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac, GetBitContext *gb)
int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc)
static int decode_usac_scale_factors(AACDecContext *ac, SingleChannelElement *sce, GetBitContext *gb, uint8_t global_gain)
static int decode_loudness_set_v1(AACDecContext *ac, AACUSACConfig *usac, GetBitContext *gb)
int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx, GetBitContext *gb, OutputConfiguration *oc, int channel_config)
static int methodvalue_width(int method_def)
Definition aacdec_usac.c:89
static void complex_stereo_interpolate_imag(float *im, float *re, const float f[7], int len, int factor_even, int factor_odd)
#define SFB_PER_PRED_BAND
Definition aacdec_usac.c:37
static const uint8_t mps_fr_nb_bands[8]
static void complex_stereo_downmix_prev(AACDecContext *ac, ChannelElement *cpe, float *dmix_re)
static int decode_spectrum_ac(AACDecContext *s, float coef[1024], GetBitContext *gb, AACArithState *state, int reset, uint16_t len, uint16_t N)
Decode and dequantize arithmetically coded, uniformly quantized value.
static int decode_usac_sbr_data(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
static int decode_drc_config(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
static void decode_tsd(GetBitContext *gb, int *data, int nb_tr_slots, int nb_slots)
static uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3)
Definition aacdec_usac.c:39
static void decode_usac_element_core(AACUsacElemConfig *e, GetBitContext *gb, int sbr_ratio)
static void spectrum_scale(AACDecContext *ac, SingleChannelElement *sce, AACUsacElemData *ue)
static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
static int parse_ext_ele(AACDecContext *ac, AACUsacElemConfig *e, GetBitContext *gb)
static int setup_sce(AACDecContext *ac, SingleChannelElement *sce, AACUSACConfig *usac)
static int decode_loudness_info(AACDecContext *ac, AACUSACLoudnessInfo *info, GetBitContext *gb, int v1)
static int parse_mps212(AACDecContext *ac, AACUSACConfig *usac, AACUsacMPSData *mps, AACUsacElemConfig *ec, GetBitContext *gb, int frame_indep_flag)
static const uint8_t mps_fr_stride_smg[4]
static void spectrum_decode(AACDecContext *ac, AACUSACConfig *usac, ChannelElement *cpe, int nb_channels)
static int decode_usac_stereo_cplx(AACDecContext *ac, AACUsacStereo *us, ChannelElement *cpe, GetBitContext *gb, int num_window_groups, int prev_num_window_groups, int indep_flag)
static void apply_noise_fill(AACDecContext *ac, SingleChannelElement *sce, AACUsacElemData *ue)
static enum AVChannel usac_ch_pos_to_av[64]
Definition aacdec_usac.c:53
static float noise_random_sign(unsigned int *seed)
static int select_loudness_measurement(const AACUSACConfig *usac)
int ff_aac_ec_data_dec(GetBitContext *gb, AACMPSLosslessData *ld, enum AACMPSDataType data_type, int default_val, int start_band, int end_band, int frame_indep_flag, int indep_flag, int nb_param_sets)
int ff_aac_huff_dec_reshape(GetBitContext *gb, int16_t *out_data, int nb_val)
int ff_aac_map_index_data(AACMPSLosslessData *ld, enum AACMPSDataType data_type, int dst_idx[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS], int default_value, int start_band, int stop_band, int nb_param_sets, const int *param_set_idx, int extend_frame)
@ MPS_IPD
@ MPS_CLD
@ MPS_ICC
AAC Spectral Band Replication function declarations.
int ff_aac_sbr_config_usac(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue)
Due to channel allocation not being known upon SBR parameter transmission, supply the parameters sepa...
int ff_aac_sbr_decode_usac_data(AACDecContext *ac, ChannelElement *che, AACUsacElemConfig *ue, GetBitContext *gb, int sbr_ch, int indep_flag)
Decode frame SBR data, USAC.
const int ff_aac_usac_samplerate[32]
Definition aactab.c:3877
const uint16_t ff_aac_ac_lsb_cdfs[3][4]
Definition aactab.c:1331
const uint16_t ff_aac_ac_msb_cdfs[64][17]
Definition aactab.c:1200
const uint8_t ff_aac_num_swb_96[]
Definition aactab.c:173
const uint16_t *const ff_swb_offset_128[]
Definition aactab.c:1940
const uint16_t *const ff_swb_offset_1024[]
Definition aactab.c:1900
const uint8_t ff_aac_num_swb_768[]
Definition aactab.c:157
const uint16_t *const ff_swb_offset_96[]
Definition aactab.c:1958
const float ff_aac_usac_mdst_filt_cur[4][4][7]
Definition aactab.c:3885
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_usac_128[]
Definition aactab.c:1994
const uint8_t ff_usac_noise_fill_start_offset[2][2]
Definition aactab.c:1999
const uint16_t *const ff_swb_offset_768[]
Definition aactab.c:1916
const uint8_t ff_tns_max_bands_usac_1024[]
Definition aactab.c:1978
AAC data declarations.
static double val(void *priv, double ch)
Definition aeval.c:77
static const int8_t filt[NUMTAPS *2]
Definition af_earwax.c:40
#define N
Definition af_mcompand.c:54
static float win(SuperEqualizerContext *s, float n, int N)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define ue(name, range_min, range_max)
Definition cbs_h264.c:61
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_PROFILE_AAC_USAC
Definition defs.h:76
static AVFrame * frame
enum AVCodecID id
Definition dts2pts.c:607
#define cm
Definition dvbsubdec.c:40
static struct @346255127015250356166251341105367306144006377143 state
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
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 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 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
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
The specified retype target order is ignored and the simplest possible (canonical) order is used for ...
#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.
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
Initialize a custom channel layout with the specified number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
@ AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_BACK_RIGHT
@ AV_CHAN_TOP_SURROUND_RIGHT
-110 degrees, Rvs, TpRS
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_LEFT
@ AV_CHAN_SIDE_SURROUND_RIGHT
-90 degrees, Rss, SiR
@ AV_CHAN_TOP_BACK_LEFT
@ AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
@ AV_CHAN_BACK_CENTER
@ AV_CHAN_TOP_SIDE_RIGHT
@ AV_CHAN_FRONT_CENTER
@ AV_CHAN_TOP_SURROUND_LEFT
+110 degrees, Lvs, TpLS
@ AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_SURROUND_LEFT
+90 degrees, Lss, SiL
@ AV_CHAN_WIDE_LEFT
@ AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_TOP_BACK_CENTER
@ AV_CHAN_BACK_LEFT
@ AV_CHAN_SIDE_LEFT
@ AV_CHAN_SURROUND_DIRECT_RIGHT
@ AV_CHAN_BOTTOM_FRONT_CENTER
@ AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_BACK_RIGHT
@ AV_CHAN_BOTTOM_FRONT_RIGHT
@ AV_CHAN_TOP_SIDE_LEFT
@ AV_CHAN_WIDE_RIGHT
@ AV_CHAN_BOTTOM_FRONT_LEFT
@ AV_CHAN_SURROUND_DIRECT_LEFT
@ AV_CHAN_TOP_CENTER
@ AV_CHAN_TOP_FRONT_CENTER
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:694
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
uint32_t type
Definition jpegmpfenc.c:80
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
ChannelElement * ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
Definition aacdec.c:623
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
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
#define us(width, name, range_min, range_max, subs,...)
Definition cbs_apv.c:70
#define av_unused
Definition attributes.h:164
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
const char data[16]
Definition mxf.c:149
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
#define AV_REFSTRUCT_FLAG_NO_ZEROING
If this flag is set in av_refstruct_alloc_ext_c(), the object will not be initially zeroed.
Definition refstruct.h:67
#define FF_ARRAY_ELEMS(a)
main AAC decoding context
Definition aacdec.h:500
AACDecProc proc
Definition aacdec.h:505
AACDecDSP dsp
Definition aacdec.h:504
AVFloatDSPContext * fdsp
Definition aacdec.h:556
int target_level
Target output loudness in dBFS, used for xHE-AAC loudness normalization based on the parsed loudnessI...
Definition aacdec.h:583
int warned_loudness_missing
Definition aacdec.h:584
struct AVCodecContext * avctx
Definition aacdec.h:502
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
OutputConfiguration oc[2]
Definition aacdec.h:586
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(* apply_mid_side_stereo)(AACDecContext *ac, ChannelElement *cpe)
Definition aacdec.h:469
void(* imdct_and_windowing_768)(AACDecContext *ac, SingleChannelElement *sce)
Definition aacdec.h:489
void(* dequant_scalefactors)(SingleChannelElement *sce)
Definition aacdec.h:467
void(* sbr_apply)(AACDecContext *ac, ChannelElement *che, int id_aac, int fl960, void *L, void *R)
Definition aacdec.h:458
uint8_t nb_info
Definition aacdec.h:407
uint16_t core_frame_len
Definition aacdec.h:398
AACUSACLoudnessInfo album_info[64]
Definition aacdec.h:406
uint8_t nb_album
Definition aacdec.h:405
uint8_t core_sbr_frame_len_idx
Definition aacdec.h:397
AACUSACLoudnessInfo info[64]
Definition aacdec.h:408
struct AACUSACConfig::@255231064241100350351340257372241321307313006210 loudness
uint16_t stream_identifier
Definition aacdec.h:399
int input_method_val
Raw bsMethodValue (μ) of the program/anchor-loudness measurement selected for normalization at config...
Definition aacdec.h:415
AACUsacElemConfig elems[MAX_ELEM_ID]
Definition aacdec.h:401
struct AACUSACLoudnessInfo::@144211065203337024143363352034022367376254316171 measurements[16]
struct AACUSACLoudnessInfo::@321071037316376126376256250004061141151267156374 sample_peak
uint8_t reliability
Definition aacdec.h:321
uint8_t method_def
Definition aacdec.h:327
uint8_t measurement
Definition aacdec.h:320
uint8_t downmix_id
Definition aacdec.h:312
uint8_t nb_measurements
Definition aacdec.h:325
struct AACUSACLoudnessInfo::@243062262052376245203144120027045262312013170327 true_peak
uint8_t eq_set_id
Definition aacdec.h:311
uint8_t method_val
Definition aacdec.h:328
uint8_t drc_set_id
Definition aacdec.h:310
struct AACUsacElemConfig::@355171322144321232200235357333175172075212042356::@272034025153077076322075376027006155226106336120 dflt
uint8_t residual_bands
Definition aacdec.h:375
uint8_t otts_bands_phase
Definition aacdec.h:373
struct AACUsacElemConfig::@155117225007163044355101232061326353222222330310 drc
uint8_t stereo_config_index
Definition aacdec.h:340
uint8_t high_rate_mode
Definition aacdec.h:369
uint8_t start_freq
Definition aacdec.h:350
uint8_t payload_frag
Definition aacdec.h:382
enum AACUsacElem type
Definition aacdec.h:335
struct AACUsacElemConfig::@031364347270230120164276243152035317262270312044 mps
uint8_t phase_coding
Definition aacdec.h:370
uint32_t pl_data_offset
Definition aacdec.h:384
uint32_t default_len
Definition aacdec.h:383
uint8_t decorr_config
Definition aacdec.h:368
uint8_t alter_scale
Definition aacdec.h:354
uint8_t fixed_gain
Definition aacdec.h:366
uint8_t temp_shape_config
Definition aacdec.h:367
uint8_t interpol_freq
Definition aacdec.h:359
uint8_t stop_freq
Definition aacdec.h:351
struct AACUsacElemConfig::@253167335173233142055027357074223104054112322124 ext
uint8_t bs_pvc
Definition aacdec.h:347
uint8_t * pl_buf
Definition aacdec.h:385
uint8_t residual_coding
Definition aacdec.h:374
uint8_t env_quant_mode
Definition aacdec.h:377
uint8_t bs_intertes
Definition aacdec.h:346
uint8_t noise_bands
Definition aacdec.h:355
uint8_t limiter_bands
Definition aacdec.h:357
uint8_t noise_fill
Definition aacdec.h:338
uint8_t freq_res
Definition aacdec.h:365
uint8_t otts_bands_phase_present
Definition aacdec.h:372
uint8_t harmonic_sbr
Definition aacdec.h:345
uint8_t freq_scale
Definition aacdec.h:353
uint8_t smoothing_mode
Definition aacdec.h:360
struct AACUsacElemConfig::@155117225007163044355101232061326353222222330310::@250177363014367151273127351060337350061157103052 loudness
uint8_t pseudo_lr
Definition aacdec.h:376
struct AACUsacElemConfig::@355171322144321232200235357333175172075212042356 sbr
uint8_t limiter_gains
Definition aacdec.h:358
uint8_t tw_mdct
Definition aacdec.h:337
uint8_t scale_factor_grouping
Definition aacdec.h:134
uint8_t core_mode
Definition aacdec.h:133
struct AACUsacElemData::@012163364245105102247341275343124375031174205057 noise
uint8_t tns_data_present
Definition aacdec.h:135
unsigned int seed
Definition aacdec.h:153
int smooth_mode[MPS_MAX_PARAM_SETS]
Definition aacdec.h:251
int nb_param_sets
Definition aacdec.h:242
int smooth_time[MPS_MAX_PARAM_SETS]
Definition aacdec.h:252
int framing_type
Definition aacdec.h:241
int16_t temp_shape_data[MPS_MAX_TIME_SLOTS]
Definition aacdec.h:260
int param_sets[MPS_MAX_PARAM_SETS]
Definition aacdec.h:243
int ott_idx[MPS_ELE_NB][MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS]
Definition aacdec.h:247
bool opd_smoothing_mode
Definition aacdec.h:248
int tsd_num_tr_slots
Definition aacdec.h:262
bool tsd_enable
Definition aacdec.h:257
AACMPSLosslessData ott[MPS_ELE_NB]
Definition aacdec.h:246
bool temp_shape_enable
Definition aacdec.h:258
int freq_res_stride_smg[MPS_MAX_PARAM_SETS]
Definition aacdec.h:253
bool smg_data[MPS_MAX_PARAM_SETS][MPS_MAX_PARAM_BANDS]
Definition aacdec.h:254
int tsd_phase_data[64]
Definition aacdec.h:263
bool temp_shape_enable_ch[2]
Definition aacdec.h:259
uint8_t pred_dir
Definition aacdec.h:276
An AVChannelCustom defines a single channel within a custom order layout.
An AVChannelLayout holds information about the channel layout of audio data.
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int profile
profile
Definition avcodec.h:1641
int sample_rate
samples per second
Definition avcodec.h:1040
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition float_dsp.h:85
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
channel element - generic struct for SCE/CPE/CCE/LFE
Definition aacdec.h:296
AACUsacStereo us
Definition aacdec.h:306
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
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
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
int sbr
-1 implicit, 1 presence
Definition mpeg4audio.h:34
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
Single Channel Element - used for both SCE and LFE elements.
Definition aacdec.h:217
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition aacenc.h:123
int sfo[128]
scalefactor offsets
Definition aacdec.h:222
TemporalNoiseShaping tns
Definition aacdec.h:220
AACUsacElemData ue
USAC element data.
Definition aacdec.h:219
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
#define cbrt
Definition tablegen.h:35
#define av_free(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
const char * g
Definition vf_curves.c:128
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static unsigned int seed
Definition videogen.c:78
int len
static double c[64]