FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 408 1059 38.5%
Functions: 15 27 55.6%
Branches: 204 627 32.5%

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