FFmpeg coverage


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