FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 407 1059 38.4%
Functions: 15 27 55.6%
Branches: 205 629 32.6%

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