FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 416 1277 32.6%
Functions: 16 35 45.7%
Branches: 211 780 27.1%

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