FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_usac.c
Date: 2026-10-29 10:55:59
Exec Total Coverage
Lines: 794 1321 60.1%
Functions: 28 36 77.8%
Branches: 407 824 49.4%

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