FFmpeg coverage


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