FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacdec_template.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 1473 1885 78.1%
Branches: 931 1314 70.9%

Line Branch Exec Source
1 /*
2 * AAC decoder
3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5 * Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
6 *
7 * AAC LATM decoder
8 * Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
9 * Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
10 *
11 * AAC decoder fixed-point implementation
12 * Copyright (c) 2013
13 * MIPS Technologies, Inc., California.
14 *
15 * This file is part of FFmpeg.
16 *
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32 /**
33 * @file
34 * AAC decoder
35 * @author Oded Shimon ( ods15 ods15 dyndns org )
36 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
37 *
38 * AAC decoder fixed-point implementation
39 * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
40 * @author Nedeljko Babic ( nedeljko.babic imgtec com )
41 */
42
43 /*
44 * supported tools
45 *
46 * Support? Name
47 * N (code in SoC repo) gain control
48 * Y block switching
49 * Y window shapes - standard
50 * N window shapes - Low Delay
51 * Y filterbank - standard
52 * N (code in SoC repo) filterbank - Scalable Sample Rate
53 * Y Temporal Noise Shaping
54 * Y Long Term Prediction
55 * Y intensity stereo
56 * Y channel coupling
57 * Y frequency domain prediction
58 * Y Perceptual Noise Substitution
59 * Y Mid/Side stereo
60 * N Scalable Inverse AAC Quantization
61 * N Frequency Selective Switch
62 * N upsampling filter
63 * Y quantization & coding - AAC
64 * N quantization & coding - TwinVQ
65 * N quantization & coding - BSAC
66 * N AAC Error Resilience tools
67 * N Error Resilience payload syntax
68 * N Error Protection tool
69 * N CELP
70 * N Silence Compression
71 * N HVXC
72 * N HVXC 4kbits/s VR
73 * N Structured Audio tools
74 * N Structured Audio Sample Bank Format
75 * N MIDI
76 * N Harmonic and Individual Lines plus Noise
77 * N Text-To-Speech Interface
78 * Y Spectral Band Replication
79 * Y (not in this code) Layer-1
80 * Y (not in this code) Layer-2
81 * Y (not in this code) Layer-3
82 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
83 * Y Parametric Stereo
84 * N Direct Stream Transfer
85 * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
86 *
87 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
88 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
89 Parametric Stereo.
90 */
91
92 #include "libavutil/channel_layout.h"
93 #include "libavutil/thread.h"
94
95 static VLC vlc_scalefactors;
96 static VLC vlc_spectral[11];
97
98 static int output_configure(AACContext *ac,
99 uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
100 enum OCStatus oc_type, int get_new_frame);
101
102 #define overread_err "Input buffer exhausted before END element found\n"
103
104 533 static int count_channels(uint8_t (*layout)[3], int tags)
105 {
106 533 int i, sum = 0;
107
2/2
✓ Branch 0 taken 1095 times.
✓ Branch 1 taken 533 times.
1628 for (i = 0; i < tags; i++) {
108 1095 int syn_ele = layout[i][0];
109 1095 int pos = layout[i][2];
110
2/2
✓ Branch 0 taken 621 times.
✓ Branch 1 taken 474 times.
2190 sum += (1 + (syn_ele == TYPE_CPE)) *
111
3/4
✓ Branch 0 taken 1095 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1087 times.
✓ Branch 3 taken 8 times.
1095 (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
112 }
113 533 return sum;
114 }
115
116 /**
117 * Check for the channel element in the current channel position configuration.
118 * If it exists, make sure the appropriate element is allocated and map the
119 * channel order to match the internal FFmpeg channel layout.
120 *
121 * @param che_pos current channel position configuration
122 * @param type channel element type
123 * @param id channel element id
124 * @param channels count of the number of channels in the configuration
125 *
126 * @return Returns error status. 0 - OK, !0 - error
127 */
128 4587 static av_cold int che_configure(AACContext *ac,
129 enum ChannelPosition che_pos,
130 int type, int id, int *channels)
131 {
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4587 times.
4587 if (*channels >= MAX_CHANNELS)
133 return AVERROR_INVALIDDATA;
134
1/2
✓ Branch 0 taken 4587 times.
✗ Branch 1 not taken.
4587 if (che_pos) {
135
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 4235 times.
4587 if (!ac->che[type][id]) {
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 352 times.
352 if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
137 return AVERROR(ENOMEM);
138 352 AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr, type);
139 }
140
2/2
✓ Branch 0 taken 4579 times.
✓ Branch 1 taken 8 times.
4587 if (type != TYPE_CCE) {
141
7/8
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 3739 times.
✓ Branch 2 taken 773 times.
✓ Branch 3 taken 67 times.
✓ Branch 4 taken 366 times.
✓ Branch 5 taken 407 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4579 times.
4579 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
142 av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
143 return AVERROR_INVALIDDATA;
144 }
145 4579 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
146
4/4
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 3739 times.
✓ Branch 2 taken 773 times.
✓ Branch 3 taken 67 times.
4579 if (type == TYPE_CPE ||
147
2/2
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 407 times.
773 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
148 4105 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
149 }
150 }
151 } else {
152 if (ac->che[type][id])
153 AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][id]->sbr);
154 av_freep(&ac->che[type][id]);
155 }
156 4587 return 0;
157 }
158
159 49860 static int frame_configure_elements(AVCodecContext *avctx)
160 {
161 49860 AACContext *ac = avctx->priv_data;
162 int type, id, ch, ret;
163
164 /* set channel pointers to internal buffers by default */
165
2/2
✓ Branch 0 taken 199440 times.
✓ Branch 1 taken 49860 times.
249300 for (type = 0; type < 4; type++) {
166
2/2
✓ Branch 0 taken 3191040 times.
✓ Branch 1 taken 199440 times.
3390480 for (id = 0; id < MAX_ELEM_ID; id++) {
167 3191040 ChannelElement *che = ac->che[type][id];
168
2/2
✓ Branch 0 taken 63240 times.
✓ Branch 1 taken 3127800 times.
3191040 if (che) {
169 63240 che->ch[0].ret = che->ch[0].ret_buf;
170 63240 che->ch[1].ret = che->ch[1].ret_buf;
171 }
172 }
173 }
174
175 /* get output buffer */
176 49860 av_frame_unref(ac->frame);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49860 times.
49860 if (!avctx->channels)
178 return 1;
179
180 49860 ac->frame->nb_samples = 2048;
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49860 times.
49860 if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
182 return ret;
183
184 /* map output channel pointers to AVFrame data */
185
2/2
✓ Branch 0 taken 94413 times.
✓ Branch 1 taken 49860 times.
144273 for (ch = 0; ch < avctx->channels; ch++) {
186
1/2
✓ Branch 0 taken 94413 times.
✗ Branch 1 not taken.
94413 if (ac->output_element[ch])
187 94413 ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
188 }
189
190 49860 return 0;
191 }
192
193 struct elem_to_channel {
194 uint64_t av_position;
195 uint8_t syn_ele;
196 uint8_t elem_id;
197 uint8_t aac_position;
198 };
199
200 3743 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
201 uint8_t (*layout_map)[3], int offset, uint64_t left,
202 uint64_t right, int pos, uint64_t *layout)
203 {
204
2/2
✓ Branch 0 taken 3739 times.
✓ Branch 1 taken 4 times.
3743 if (layout_map[offset][0] == TYPE_CPE) {
205 3739 e2c_vec[offset] = (struct elem_to_channel) {
206 3739 .av_position = left | right,
207 .syn_ele = TYPE_CPE,
208 3739 .elem_id = layout_map[offset][1],
209 .aac_position = pos
210 };
211
1/2
✓ Branch 0 taken 3739 times.
✗ Branch 1 not taken.
3739 if (e2c_vec[offset].av_position != UINT64_MAX)
212 3739 *layout |= e2c_vec[offset].av_position;
213
214 3739 return 1;
215 } else {
216 4 e2c_vec[offset] = (struct elem_to_channel) {
217 .av_position = left,
218 .syn_ele = TYPE_SCE,
219 4 .elem_id = layout_map[offset][1],
220 .aac_position = pos
221 };
222 4 e2c_vec[offset + 1] = (struct elem_to_channel) {
223 .av_position = right,
224 .syn_ele = TYPE_SCE,
225 4 .elem_id = layout_map[offset + 1][1],
226 .aac_position = pos
227 };
228
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (left != UINT64_MAX)
229 4 *layout |= left;
230
231
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (right != UINT64_MAX)
232 4 *layout |= right;
233
234 4 return 2;
235 }
236 }
237
238 13110 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
239 int *current)
240 {
241 13110 int num_pos_channels = 0;
242 13110 int first_cpe = 0;
243 13110 int sce_parity = 0;
244 int i;
245
2/2
✓ Branch 0 taken 4713 times.
✓ Branch 1 taken 12909 times.
17622 for (i = *current; i < tags; i++) {
246
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 4512 times.
4713 if (layout_map[i][2] != pos)
247 201 break;
248
2/2
✓ Branch 0 taken 3739 times.
✓ Branch 1 taken 773 times.
4512 if (layout_map[i][0] == TYPE_CPE) {
249
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 3668 times.
3739 if (sce_parity) {
250
2/4
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
71 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
251 71 sce_parity = 0;
252 } else {
253 return -1;
254 }
255 }
256 3739 num_pos_channels += 2;
257 3739 first_cpe = 1;
258 } else {
259 773 num_pos_channels++;
260 773 sce_parity ^= 1;
261 }
262 }
263
3/4
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 12416 times.
✓ Branch 2 taken 694 times.
✗ Branch 3 not taken.
13110 if (sce_parity &&
264
2/4
✓ Branch 0 taken 694 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 694 times.
694 ((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
265 return -1;
266 13110 *current = i;
267 13110 return num_pos_channels;
268 }
269
270 #define PREFIX_FOR_22POINT2 (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_LOW_FREQUENCY_2)
271 4370 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
272 {
273 int i, n, total_non_cc_elements;
274 4370 struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
275 int num_front_channels, num_side_channels, num_back_channels;
276 4370 uint64_t layout = 0;
277
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
279 return 0;
280
281 4370 i = 0;
282 num_front_channels =
283 4370 count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (num_front_channels < 0)
285 return 0;
286 num_side_channels =
287 4370 count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (num_side_channels < 0)
289 return 0;
290 num_back_channels =
291 4370 count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (num_back_channels < 0)
293 return 0;
294
295
2/4
✓ Branch 0 taken 4370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4370 times.
4370 if (num_side_channels == 0 && num_back_channels >= 4) {
296 num_side_channels = 2;
297 num_back_channels -= 2;
298 }
299
300 4370 i = 0;
301
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 3605 times.
4370 if (num_front_channels & 1) {
302 765 e2c_vec[i] = (struct elem_to_channel) {
303 .av_position = AV_CH_FRONT_CENTER,
304 .syn_ele = TYPE_SCE,
305 765 .elem_id = layout_map[i][1],
306 .aac_position = AAC_CHANNEL_FRONT
307 };
308 765 layout |= e2c_vec[i].av_position;
309 765 i++;
310 765 num_front_channels--;
311 }
312
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4362 times.
4370 if (num_front_channels >= 4) {
313 8 i += assign_pair(e2c_vec, layout_map, i,
314 AV_CH_FRONT_LEFT_OF_CENTER,
315 AV_CH_FRONT_RIGHT_OF_CENTER,
316 AAC_CHANNEL_FRONT, &layout);
317 8 num_front_channels -= 2;
318 }
319
2/2
✓ Branch 0 taken 3676 times.
✓ Branch 1 taken 694 times.
4370 if (num_front_channels >= 2) {
320 3676 i += assign_pair(e2c_vec, layout_map, i,
321 AV_CH_FRONT_LEFT,
322 AV_CH_FRONT_RIGHT,
323 AAC_CHANNEL_FRONT, &layout);
324 3676 num_front_channels -= 2;
325 }
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 while (num_front_channels >= 2) {
327 i += assign_pair(e2c_vec, layout_map, i,
328 UINT64_MAX,
329 UINT64_MAX,
330 AAC_CHANNEL_FRONT, &layout);
331 num_front_channels -= 2;
332 }
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (num_side_channels >= 2) {
335 i += assign_pair(e2c_vec, layout_map, i,
336 AV_CH_SIDE_LEFT,
337 AV_CH_SIDE_RIGHT,
338 AAC_CHANNEL_FRONT, &layout);
339 num_side_channels -= 2;
340 }
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 while (num_side_channels >= 2) {
342 i += assign_pair(e2c_vec, layout_map, i,
343 UINT64_MAX,
344 UINT64_MAX,
345 AAC_CHANNEL_SIDE, &layout);
346 num_side_channels -= 2;
347 }
348
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 while (num_back_channels >= 4) {
350 i += assign_pair(e2c_vec, layout_map, i,
351 UINT64_MAX,
352 UINT64_MAX,
353 AAC_CHANNEL_BACK, &layout);
354 num_back_channels -= 2;
355 }
356
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 4311 times.
4370 if (num_back_channels >= 2) {
357 59 i += assign_pair(e2c_vec, layout_map, i,
358 AV_CH_BACK_LEFT,
359 AV_CH_BACK_RIGHT,
360 AAC_CHANNEL_BACK, &layout);
361 59 num_back_channels -= 2;
362 }
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (num_back_channels) {
364 e2c_vec[i] = (struct elem_to_channel) {
365 .av_position = AV_CH_BACK_CENTER,
366 .syn_ele = TYPE_SCE,
367 .elem_id = layout_map[i][1],
368 .aac_position = AAC_CHANNEL_BACK
369 };
370 layout |= e2c_vec[i].av_position;
371 i++;
372 num_back_channels--;
373 }
374
375
3/4
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 4303 times.
✓ Branch 2 taken 67 times.
✗ Branch 3 not taken.
4370 if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
376 67 e2c_vec[i] = (struct elem_to_channel) {
377 .av_position = AV_CH_LOW_FREQUENCY,
378 .syn_ele = TYPE_LFE,
379 67 .elem_id = layout_map[i][1],
380 .aac_position = AAC_CHANNEL_LFE
381 };
382 67 layout |= e2c_vec[i].av_position;
383 67 i++;
384 }
385
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4362 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
4370 if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
386 e2c_vec[i] = (struct elem_to_channel) {
387 .av_position = AV_CH_LOW_FREQUENCY_2,
388 .syn_ele = TYPE_LFE,
389 .elem_id = layout_map[i][1],
390 .aac_position = AAC_CHANNEL_LFE
391 };
392 layout |= e2c_vec[i].av_position;
393 i++;
394 }
395
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4362 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
4370 while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
396 e2c_vec[i] = (struct elem_to_channel) {
397 .av_position = UINT64_MAX,
398 .syn_ele = TYPE_LFE,
399 .elem_id = layout_map[i][1],
400 .aac_position = AAC_CHANNEL_LFE
401 };
402 i++;
403 }
404
405 // The previous checks would end up at 8 at this point for 22.2
406
1/6
✓ Branch 0 taken 4370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4370 if (layout == PREFIX_FOR_22POINT2 && tags == 16 && i == 8) {
407 const uint8_t (*reference_layout_map)[3] = aac_channel_layout_map[12];
408 for (int j = 0; j < tags; j++) {
409 if (layout_map[j][0] != reference_layout_map[j][0] ||
410 layout_map[j][2] != reference_layout_map[j][2])
411 goto end_of_layout_definition;
412 }
413
414 e2c_vec[i] = (struct elem_to_channel) {
415 .av_position = AV_CH_TOP_FRONT_CENTER,
416 .syn_ele = layout_map[i][0],
417 .elem_id = layout_map[i][1],
418 .aac_position = layout_map[i][2]
419 }; layout |= e2c_vec[i].av_position; i++;
420 i += assign_pair(e2c_vec, layout_map, i,
421 AV_CH_TOP_FRONT_LEFT,
422 AV_CH_TOP_FRONT_RIGHT,
423 AAC_CHANNEL_FRONT,
424 &layout);
425 i += assign_pair(e2c_vec, layout_map, i,
426 AV_CH_TOP_SIDE_LEFT,
427 AV_CH_TOP_SIDE_RIGHT,
428 AAC_CHANNEL_SIDE,
429 &layout);
430 e2c_vec[i] = (struct elem_to_channel) {
431 .av_position = AV_CH_TOP_CENTER,
432 .syn_ele = layout_map[i][0],
433 .elem_id = layout_map[i][1],
434 .aac_position = layout_map[i][2]
435 }; layout |= e2c_vec[i].av_position; i++;
436 i += assign_pair(e2c_vec, layout_map, i,
437 AV_CH_TOP_BACK_LEFT,
438 AV_CH_TOP_BACK_RIGHT,
439 AAC_CHANNEL_BACK,
440 &layout);
441 e2c_vec[i] = (struct elem_to_channel) {
442 .av_position = AV_CH_TOP_BACK_CENTER,
443 .syn_ele = layout_map[i][0],
444 .elem_id = layout_map[i][1],
445 .aac_position = layout_map[i][2]
446 }; layout |= e2c_vec[i].av_position; i++;
447 e2c_vec[i] = (struct elem_to_channel) {
448 .av_position = AV_CH_BOTTOM_FRONT_CENTER,
449 .syn_ele = layout_map[i][0],
450 .elem_id = layout_map[i][1],
451 .aac_position = layout_map[i][2]
452 }; layout |= e2c_vec[i].av_position; i++;
453 i += assign_pair(e2c_vec, layout_map, i,
454 AV_CH_BOTTOM_FRONT_LEFT,
455 AV_CH_BOTTOM_FRONT_RIGHT,
456 AAC_CHANNEL_FRONT,
457 &layout);
458 }
459
460 4370 end_of_layout_definition:
461
462 4370 total_non_cc_elements = n = i;
463
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4370 times.
4370 if (layout == AV_CH_LAYOUT_22POINT2) {
465 // For 22.2 reorder the result as needed
466 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
467 FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
468 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
469 FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
470 FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
471 FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
472 FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
473 FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelth
474 FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelth (final), TpSiL & TpSiR thirteenth (final)
475 } else {
476 // For everything else, utilize the AV channel position define as a
477 // stable sort.
478 do {
479 4449 int next_n = 0;
480
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 4449 times.
4792 for (i = 1; i < n; i++)
481
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 197 times.
343 if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
482 146 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
483 146 next_n = i;
484 }
485 4449 n = next_n;
486
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 4370 times.
4449 } while (n > 0);
487
488 }
489
490
2/2
✓ Branch 0 taken 4579 times.
✓ Branch 1 taken 4370 times.
8949 for (i = 0; i < total_non_cc_elements; i++) {
491 4579 layout_map[i][0] = e2c_vec[i].syn_ele;
492 4579 layout_map[i][1] = e2c_vec[i].elem_id;
493 4579 layout_map[i][2] = e2c_vec[i].aac_position;
494 }
495
496 4370 return layout;
497 }
498
499 /**
500 * Save current output configuration if and only if it has been locked.
501 */
502 4118 static int push_output_configuration(AACContext *ac) {
503 4118 int pushed = 0;
504
505
3/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 4059 times.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
4118 if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
506 4118 ac->oc[0] = ac->oc[1];
507 4118 pushed = 1;
508 }
509 4118 ac->oc[1].status = OC_NONE;
510 4118 return pushed;
511 }
512
513 /**
514 * Restore the previous output configuration if and only if the current
515 * configuration is unlocked.
516 */
517 static void pop_output_configuration(AACContext *ac) {
518 if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
519 ac->oc[1] = ac->oc[0];
520 ac->avctx->channels = ac->oc[1].channels;
521 ac->avctx->channel_layout = ac->oc[1].channel_layout;
522 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
523 ac->oc[1].status, 0);
524 }
525 }
526
527 /**
528 * Configure output channel order based on the current program
529 * configuration element.
530 *
531 * @return Returns error status. 0 - OK, !0 - error
532 */
533 4370 static int output_configure(AACContext *ac,
534 uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
535 enum OCStatus oc_type, int get_new_frame)
536 {
537 4370 AVCodecContext *avctx = ac->avctx;
538 4370 int i, channels = 0, ret;
539 4370 uint64_t layout = 0;
540 4370 uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
541 4370 uint8_t type_counts[TYPE_END] = { 0 };
542
543
2/2
✓ Branch 0 taken 4358 times.
✓ Branch 1 taken 12 times.
4370 if (ac->oc[1].layout_map != layout_map) {
544 4358 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
545 4358 ac->oc[1].layout_map_tags = tags;
546 }
547
2/2
✓ Branch 0 taken 4587 times.
✓ Branch 1 taken 4370 times.
8957 for (i = 0; i < tags; i++) {
548 4587 int type = layout_map[i][0];
549 4587 int id = layout_map[i][1];
550 4587 id_map[type][id] = type_counts[type]++;
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4587 times.
4587 if (id_map[type][id] >= MAX_ELEM_ID) {
552 avpriv_request_sample(ac->avctx, "Too large remapped id");
553 return AVERROR_PATCHWELCOME;
554 }
555 }
556 // Try to sniff a reasonable channel order, otherwise output the
557 // channels in the order the PCE declared them.
558
1/2
✓ Branch 0 taken 4370 times.
✗ Branch 1 not taken.
4370 if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
559 4370 layout = sniff_channel_order(layout_map, tags);
560
2/2
✓ Branch 0 taken 4587 times.
✓ Branch 1 taken 4370 times.
8957 for (i = 0; i < tags; i++) {
561 4587 int type = layout_map[i][0];
562 4587 int id = layout_map[i][1];
563 4587 int iid = id_map[type][id];
564 4587 int position = layout_map[i][2];
565 // Allocate or free elements depending on if they are in the
566 // current program configuration.
567 4587 ret = che_configure(ac, position, type, iid, &channels);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4587 times.
4587 if (ret < 0)
569 return ret;
570 4587 ac->tag_che_map[type][id] = ac->che[type][iid];
571 }
572
3/4
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 4004 times.
✓ Branch 2 taken 366 times.
✗ Branch 3 not taken.
4370 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
573
1/2
✓ Branch 0 taken 366 times.
✗ Branch 1 not taken.
366 if (layout == AV_CH_FRONT_CENTER) {
574 366 layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
575 } else {
576 layout = 0;
577 }
578 }
579
580
1/2
✓ Branch 0 taken 4370 times.
✗ Branch 1 not taken.
4370 if (layout) avctx->channel_layout = layout;
581 4370 ac->oc[1].channel_layout = layout;
582 4370 avctx->channels = ac->oc[1].channels = channels;
583 4370 ac->oc[1].status = oc_type;
584
585
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4358 times.
4370 if (get_new_frame) {
586
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if ((ret = frame_configure_elements(ac->avctx)) < 0)
587 return ret;
588 }
589
590 4370 return 0;
591 }
592
593 static void flush(AVCodecContext *avctx)
594 {
595 AACContext *ac= avctx->priv_data;
596 int type, i, j;
597
598 for (type = 3; type >= 0; type--) {
599 for (i = 0; i < MAX_ELEM_ID; i++) {
600 ChannelElement *che = ac->che[type][i];
601 if (che) {
602 for (j = 0; j <= 1; j++) {
603 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
604 }
605 }
606 }
607 }
608 }
609
610 /**
611 * Set up channel positions based on a default channel configuration
612 * as specified in table 1.17.
613 *
614 * @return Returns error status. 0 - OK, !0 - error
615 */
616 4636 static int set_default_channel_config(AACContext *ac, AVCodecContext *avctx,
617 uint8_t (*layout_map)[3],
618 int *tags,
619 int channel_config)
620 {
621
3/8
✓ Branch 0 taken 4636 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4636 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4636 times.
4636 if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
622 channel_config > 13) {
623 av_log(avctx, AV_LOG_ERROR,
624 "invalid default channel configuration (%d)\n",
625 channel_config);
626 return AVERROR_INVALIDDATA;
627 }
628 4636 *tags = tags_per_config[channel_config];
629 4636 memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
630 4636 *tags * sizeof(*layout_map));
631
632 /*
633 * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
634 * However, at least Nero AAC encoder encodes 7.1 streams using the default
635 * channel config 7, mapping the side channels of the original audio stream
636 * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
637 * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
638 * the incorrect streams as if they were correct (and as the encoder intended).
639 *
640 * As actual intended 7.1(wide) streams are very rare, default to assuming a
641 * 7.1 layout was intended.
642 */
643
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4636 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4636 if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
644 layout_map[2][2] = AAC_CHANNEL_SIDE;
645
646 if (!ac || !ac->warned_71_wide++) {
647 av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
648 " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
649 " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
650 }
651 }
652
653 4636 return 0;
654 }
655
656 62894 static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
657 {
658 /* For PCE based channel configurations map the channels solely based
659 * on tags. */
660
2/2
✓ Branch 0 taken 24124 times.
✓ Branch 1 taken 38770 times.
62894 if (!ac->oc[1].m4ac.chan_config) {
661 24124 return ac->tag_che_map[type][elem_id];
662 }
663 // Allow single CPE stereo files to be signalled with mono configuration.
664
4/4
✓ Branch 0 taken 36310 times.
✓ Branch 1 taken 2460 times.
✓ Branch 2 taken 20536 times.
✓ Branch 3 taken 15774 times.
38770 if (!ac->tags_mapped && type == TYPE_CPE &&
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20536 times.
20536 ac->oc[1].m4ac.chan_config == 1) {
666 uint8_t layout_map[MAX_ELEM_ID*4][3];
667 int layout_map_tags;
668 push_output_configuration(ac);
669
670 av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
671
672 if (set_default_channel_config(ac, ac->avctx, layout_map,
673 &layout_map_tags, 2) < 0)
674 return NULL;
675 if (output_configure(ac, layout_map, layout_map_tags,
676 OC_TRIAL_FRAME, 1) < 0)
677 return NULL;
678
679 ac->oc[1].m4ac.chan_config = 2;
680 ac->oc[1].m4ac.ps = 0;
681 }
682 // And vice-versa
683
4/4
✓ Branch 0 taken 36310 times.
✓ Branch 1 taken 2460 times.
✓ Branch 2 taken 15774 times.
✓ Branch 3 taken 20536 times.
38770 if (!ac->tags_mapped && type == TYPE_SCE &&
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15774 times.
15774 ac->oc[1].m4ac.chan_config == 2) {
685 uint8_t layout_map[MAX_ELEM_ID * 4][3];
686 int layout_map_tags;
687 push_output_configuration(ac);
688
689 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
690
691 if (set_default_channel_config(ac, ac->avctx, layout_map,
692 &layout_map_tags, 1) < 0)
693 return NULL;
694 if (output_configure(ac, layout_map, layout_map_tags,
695 OC_TRIAL_FRAME, 1) < 0)
696 return NULL;
697
698 ac->oc[1].m4ac.chan_config = 1;
699 if (ac->oc[1].m4ac.sbr)
700 ac->oc[1].m4ac.ps = -1;
701 }
702 /* For indexed channel configurations map the channels solely based
703 * on position. */
704
3/9
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3280 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 20536 times.
✓ Branch 7 taken 14954 times.
✗ Branch 8 not taken.
38770 switch (ac->oc[1].m4ac.chan_config) {
705 case 13:
706 if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
707 (type == TYPE_SCE && elem_id < 6) ||
708 (type == TYPE_LFE && elem_id < 2))) {
709 ac->tags_mapped++;
710 return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
711 }
712 case 12:
713 case 7:
714 if (ac->tags_mapped == 3 && type == TYPE_CPE) {
715 ac->tags_mapped++;
716 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
717 }
718 case 11:
719 if (ac->tags_mapped == 2 &&
720 ac->oc[1].m4ac.chan_config == 11 &&
721 type == TYPE_SCE) {
722 ac->tags_mapped++;
723 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
724 }
725 case 6:
726 /* Some streams incorrectly code 5.1 audio as
727 * SCE[0] CPE[0] CPE[1] SCE[1]
728 * instead of
729 * SCE[0] CPE[0] CPE[1] LFE[0].
730 * If we seem to have encountered such a stream, transfer
731 * the LFE[0] element to the SCE[1]'s mapping */
732
3/6
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 2460 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 820 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3280 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
733
3/6
✓ Branch 0 taken 820 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 820 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 820 times.
820 if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
734 av_log(ac->avctx, AV_LOG_WARNING,
735 "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
736 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
737 ac->warned_remapping_once++;
738 }
739 820 ac->tags_mapped++;
740 820 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
741 }
742 case 5:
743
3/4
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 1640 times.
✓ Branch 2 taken 820 times.
✗ Branch 3 not taken.
2460 if (ac->tags_mapped == 2 && type == TYPE_CPE) {
744 820 ac->tags_mapped++;
745 820 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
746 }
747 case 4:
748 /* Some streams incorrectly code 4.0 audio as
749 * SCE[0] CPE[0] LFE[0]
750 * instead of
751 * SCE[0] CPE[0] SCE[1].
752 * If we seem to have encountered such a stream, transfer
753 * the SCE[1] element to the LFE[0]'s mapping */
754
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1640 if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
755 if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
756 av_log(ac->avctx, AV_LOG_WARNING,
757 "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
758 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
759 ac->warned_remapping_once++;
760 }
761 ac->tags_mapped++;
762 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
763 }
764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if (ac->tags_mapped == 2 &&
765 ac->oc[1].m4ac.chan_config == 4 &&
766 type == TYPE_SCE) {
767 ac->tags_mapped++;
768 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
769 }
770 case 3:
771 case 2:
772
3/4
✓ Branch 0 taken 21356 times.
✓ Branch 1 taken 820 times.
✓ Branch 2 taken 21356 times.
✗ Branch 3 not taken.
22176 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
773 type == TYPE_CPE) {
774 21356 ac->tags_mapped++;
775 21356 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 820 times.
820 } else if (ac->oc[1].m4ac.chan_config == 2) {
777 return NULL;
778 }
779 case 1:
780
2/4
✓ Branch 0 taken 15774 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15774 times.
✗ Branch 3 not taken.
15774 if (!ac->tags_mapped && type == TYPE_SCE) {
781 15774 ac->tags_mapped++;
782 15774 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
783 }
784 default:
785 return NULL;
786 }
787 }
788
789 /**
790 * Decode an array of 4 bit element IDs, optionally interleaved with a
791 * stereo/mono switching bit.
792 *
793 * @param type speaker type/position for these channels
794 */
795 215 static void decode_channel_map(uint8_t layout_map[][3],
796 enum ChannelPosition type,
797 GetBitContext *gb, int n)
798 {
799
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 215 times.
310 while (n--) {
800 enum RawDataBlockType syn_ele;
801
3/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
95 switch (type) {
802 75 case AAC_CHANNEL_FRONT:
803 case AAC_CHANNEL_BACK:
804 case AAC_CHANNEL_SIDE:
805 75 syn_ele = get_bits1(gb);
806 75 break;
807 8 case AAC_CHANNEL_CC:
808 8 skip_bits1(gb);
809 8 syn_ele = TYPE_CCE;
810 8 break;
811 12 case AAC_CHANNEL_LFE:
812 12 syn_ele = TYPE_LFE;
813 12 break;
814 default:
815 // AAC_CHANNEL_OFF has no channel map
816 av_assert0(0);
817 }
818 95 layout_map[0][0] = syn_ele;
819 95 layout_map[0][1] = get_bits(gb, 4);
820 95 layout_map[0][2] = type;
821 95 layout_map++;
822 }
823 215 }
824
825 43 static inline void relative_align_get_bits(GetBitContext *gb,
826 int reference_position) {
827 43 int n = (reference_position - get_bits_count(gb) & 7);
828
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 4 times.
43 if (n)
829 39 skip_bits(gb, n);
830 43 }
831
832 /**
833 * Decode program configuration element; reference: table 4.2.
834 *
835 * @return Returns error status. 0 - OK, !0 - error
836 */
837 43 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
838 uint8_t (*layout_map)[3],
839 GetBitContext *gb, int byte_align_ref)
840 {
841 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
842 int sampling_index;
843 int comment_len;
844 int tags;
845
846 43 skip_bits(gb, 2); // object_type
847
848 43 sampling_index = get_bits(gb, 4);
849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (m4ac->sampling_index != sampling_index)
850 av_log(avctx, AV_LOG_WARNING,
851 "Sample rate index in program config element does not "
852 "match the sample rate index configured by the container.\n");
853
854 43 num_front = get_bits(gb, 4);
855 43 num_side = get_bits(gb, 4);
856 43 num_back = get_bits(gb, 4);
857 43 num_lfe = get_bits(gb, 2);
858 43 num_assoc_data = get_bits(gb, 3);
859 43 num_cc = get_bits(gb, 4);
860
861
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (get_bits1(gb))
862 skip_bits(gb, 4); // mono_mixdown_tag
863
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (get_bits1(gb))
864 skip_bits(gb, 4); // stereo_mixdown_tag
865
866
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (get_bits1(gb))
867 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
868
869
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
870 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
871 return -1;
872 }
873 43 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
874 43 tags = num_front;
875 43 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
876 43 tags += num_side;
877 43 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
878 43 tags += num_back;
879 43 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
880 43 tags += num_lfe;
881
882 43 skip_bits_long(gb, 4 * num_assoc_data);
883
884 43 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
885 43 tags += num_cc;
886
887 43 relative_align_get_bits(gb, byte_align_ref);
888
889 /* comment field, first byte is length */
890 43 comment_len = get_bits(gb, 8) * 8;
891
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (get_bits_left(gb) < comment_len) {
892 av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
893 return AVERROR_INVALIDDATA;
894 }
895 43 skip_bits_long(gb, comment_len);
896 43 return tags;
897 }
898
899 /**
900 * Decode GA "General Audio" specific configuration; reference: table 4.1.
901 *
902 * @param ac pointer to AACContext, may be null
903 * @param avctx pointer to AVCCodecContext, used for logging
904 *
905 * @return Returns error status. 0 - OK, !0 - error
906 */
907 533 static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
908 GetBitContext *gb,
909 int get_bit_alignment,
910 MPEG4AudioConfig *m4ac,
911 int channel_config)
912 {
913 int extension_flag, ret, ep_config, res_flags;
914 uint8_t layout_map[MAX_ELEM_ID*4][3];
915 533 int tags = 0;
916
917 #if USE_FIXED
918
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits1(gb)) { // frameLengthFlag
919 avpriv_report_missing_feature(avctx, "Fixed point 960/120 MDCT window");
920 return AVERROR_PATCHWELCOME;
921 }
922 17 m4ac->frame_length_short = 0;
923 #else
924 516 m4ac->frame_length_short = get_bits1(gb);
925
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 514 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
516 if (m4ac->frame_length_short && m4ac->sbr == 1) {
926 avpriv_report_missing_feature(avctx, "SBR with 960 frame length");
927 if (ac) ac->warned_960_sbr = 1;
928 m4ac->sbr = 0;
929 m4ac->ps = 0;
930 }
931 #endif
932
933
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 533 times.
533 if (get_bits1(gb)) // dependsOnCoreCoder
934 skip_bits(gb, 14); // coreCoderDelay
935 533 extension_flag = get_bits1(gb);
936
937
1/2
✓ Branch 0 taken 533 times.
✗ Branch 1 not taken.
533 if (m4ac->object_type == AOT_AAC_SCALABLE ||
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533 times.
533 m4ac->object_type == AOT_ER_AAC_SCALABLE)
939 skip_bits(gb, 3); // layerNr
940
941
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 490 times.
533 if (channel_config == 0) {
942 43 skip_bits(gb, 4); // element_instance_tag
943 43 tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (tags < 0)
945 return tags;
946 } else {
947
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 490 times.
490 if ((ret = set_default_channel_config(ac, avctx, layout_map,
948 &tags, channel_config)))
949 return ret;
950 }
951
952
2/2
✓ Branch 1 taken 443 times.
✓ Branch 2 taken 90 times.
533 if (count_channels(layout_map, tags) > 1) {
953 443 m4ac->ps = 0;
954
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 16 times.
90 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
955 18 m4ac->ps = 1;
956
957
3/4
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 321 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
533 if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
958 return ret;
959
960
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 529 times.
533 if (extension_flag) {
961
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 switch (m4ac->object_type) {
962 case AOT_ER_BSAC:
963 skip_bits(gb, 5); // numOfSubFrame
964 skip_bits(gb, 11); // layer_length
965 break;
966 4 case AOT_ER_AAC_LC:
967 case AOT_ER_AAC_LTP:
968 case AOT_ER_AAC_SCALABLE:
969 case AOT_ER_AAC_LD:
970 4 res_flags = get_bits(gb, 3);
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (res_flags) {
972 avpriv_report_missing_feature(avctx,
973 "AAC data resilience (flags %x)",
974 res_flags);
975 return AVERROR_PATCHWELCOME;
976 }
977 4 break;
978 }
979 4 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
980 }
981
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 529 times.
533 switch (m4ac->object_type) {
982 4 case AOT_ER_AAC_LC:
983 case AOT_ER_AAC_LTP:
984 case AOT_ER_AAC_SCALABLE:
985 case AOT_ER_AAC_LD:
986 4 ep_config = get_bits(gb, 2);
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ep_config) {
988 avpriv_report_missing_feature(avctx,
989 "epConfig %d", ep_config);
990 return AVERROR_PATCHWELCOME;
991 }
992 }
993 533 return 0;
994 }
995
996 10 static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
997 GetBitContext *gb,
998 MPEG4AudioConfig *m4ac,
999 int channel_config)
1000 {
1001 int ret, ep_config, res_flags;
1002 uint8_t layout_map[MAX_ELEM_ID*4][3];
1003 10 int tags = 0;
1004 10 const int ELDEXT_TERM = 0;
1005
1006 10 m4ac->ps = 0;
1007 10 m4ac->sbr = 0;
1008 #if USE_FIXED
1009
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits1(gb)) { // frameLengthFlag
1010 avpriv_request_sample(avctx, "960/120 MDCT window");
1011 return AVERROR_PATCHWELCOME;
1012 }
1013 #else
1014 8 m4ac->frame_length_short = get_bits1(gb);
1015 #endif
1016 10 res_flags = get_bits(gb, 3);
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (res_flags) {
1018 avpriv_report_missing_feature(avctx,
1019 "AAC data resilience (flags %x)",
1020 res_flags);
1021 return AVERROR_PATCHWELCOME;
1022 }
1023
1024
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (get_bits1(gb)) { // ldSbrPresentFlag
1025 avpriv_report_missing_feature(avctx,
1026 "Low Delay SBR");
1027 return AVERROR_PATCHWELCOME;
1028 }
1029
1030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 while (get_bits(gb, 4) != ELDEXT_TERM) {
1031 int len = get_bits(gb, 4);
1032 if (len == 15)
1033 len += get_bits(gb, 8);
1034 if (len == 15 + 255)
1035 len += get_bits(gb, 16);
1036 if (get_bits_left(gb) < len * 8 + 4) {
1037 av_log(avctx, AV_LOG_ERROR, overread_err);
1038 return AVERROR_INVALIDDATA;
1039 }
1040 skip_bits_long(gb, 8 * len);
1041 }
1042
1043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = set_default_channel_config(ac, avctx, layout_map,
1044 &tags, channel_config)))
1045 return ret;
1046
1047
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
10 if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1048 return ret;
1049
1050 10 ep_config = get_bits(gb, 2);
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ep_config) {
1052 avpriv_report_missing_feature(avctx,
1053 "epConfig %d", ep_config);
1054 return AVERROR_PATCHWELCOME;
1055 }
1056 10 return 0;
1057 }
1058
1059 /**
1060 * Decode audio specific configuration; reference: table 1.13.
1061 *
1062 * @param ac pointer to AACContext, may be null
1063 * @param avctx pointer to AVCCodecContext, used for logging
1064 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1065 * @param gb buffer holding an audio specific config
1066 * @param get_bit_alignment relative alignment for byte align operations
1067 * @param sync_extension look for an appended sync extension
1068 *
1069 * @return Returns error status or number of consumed bits. <0 - error
1070 */
1071 543 static int decode_audio_specific_config_gb(AACContext *ac,
1072 AVCodecContext *avctx,
1073 MPEG4AudioConfig *m4ac,
1074 GetBitContext *gb,
1075 int get_bit_alignment,
1076 int sync_extension)
1077 {
1078 int i, ret;
1079 543 GetBitContext gbc = *gb;
1080 543 MPEG4AudioConfig m4ac_bak = *m4ac;
1081
1082
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 543 times.
543 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1083 *m4ac = m4ac_bak;
1084 return AVERROR_INVALIDDATA;
1085 }
1086
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 if (m4ac->sampling_index > 12) {
1088 av_log(avctx, AV_LOG_ERROR,
1089 "invalid sampling rate index %d\n",
1090 m4ac->sampling_index);
1091 *m4ac = m4ac_bak;
1092 return AVERROR_INVALIDDATA;
1093 }
1094
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 539 times.
543 if (m4ac->object_type == AOT_ER_AAC_LD &&
1095
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 (m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
1096 av_log(avctx, AV_LOG_ERROR,
1097 "invalid low delay sampling rate index %d\n",
1098 m4ac->sampling_index);
1099 *m4ac = m4ac_bak;
1100 return AVERROR_INVALIDDATA;
1101 }
1102
1103 543 skip_bits_long(gb, i);
1104
1105
2/3
✓ Branch 0 taken 533 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
543 switch (m4ac->object_type) {
1106 533 case AOT_AAC_MAIN:
1107 case AOT_AAC_LC:
1108 case AOT_AAC_SSR:
1109 case AOT_AAC_LTP:
1110 case AOT_ER_AAC_LC:
1111 case AOT_ER_AAC_LD:
1112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 533 times.
533 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1113 m4ac, m4ac->chan_config)) < 0)
1114 return ret;
1115 533 break;
1116 10 case AOT_ER_AAC_ELD:
1117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = decode_eld_specific_config(ac, avctx, gb,
1118 m4ac, m4ac->chan_config)) < 0)
1119 return ret;
1120 10 break;
1121 default:
1122 avpriv_report_missing_feature(avctx,
1123 "Audio object type %s%d",
1124 m4ac->sbr == 1 ? "SBR+" : "",
1125 m4ac->object_type);
1126 return AVERROR(ENOSYS);
1127 }
1128
1129 ff_dlog(avctx,
1130 "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1131 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1132 m4ac->sample_rate, m4ac->sbr,
1133 m4ac->ps);
1134
1135 543 return get_bits_count(gb);
1136 }
1137
1138 222 static int decode_audio_specific_config(AACContext *ac,
1139 AVCodecContext *avctx,
1140 MPEG4AudioConfig *m4ac,
1141 const uint8_t *data, int64_t bit_size,
1142 int sync_extension)
1143 {
1144 int i, ret;
1145 GetBitContext gb;
1146
1147
2/4
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 222 times.
222 if (bit_size < 0 || bit_size > INT_MAX) {
1148 av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1149 return AVERROR_INVALIDDATA;
1150 }
1151
1152 ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1153
2/2
✓ Branch 0 taken 1349 times.
✓ Branch 1 taken 222 times.
1571 for (i = 0; i < bit_size >> 3; i++)
1154 ff_dlog(avctx, "%02x ", data[i]);
1155 ff_dlog(avctx, "\n");
1156
1157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 222 times.
222 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1158 return ret;
1159
1160 222 return decode_audio_specific_config_gb(ac, avctx, m4ac, &gb, 0,
1161 sync_extension);
1162 }
1163
1164 /**
1165 * linear congruential pseudorandom number generator
1166 *
1167 * @param previous_val pointer to the current state of the generator
1168 *
1169 * @return Returns a 32-bit pseudorandom integer
1170 */
1171 5944200 static av_always_inline int lcg_random(unsigned previous_val)
1172 {
1173 5944200 union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
1174 5944200 return v.s;
1175 }
1176
1177 40 static void reset_all_predictors(PredictorState *ps)
1178 {
1179 int i;
1180
2/2
✓ Branch 0 taken 26880 times.
✓ Branch 1 taken 40 times.
26920 for (i = 0; i < MAX_PREDICTORS; i++)
1181 26880 reset_predict_state(&ps[i]);
1182 40 }
1183
1184 59 static int sample_rate_idx (int rate)
1185 {
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (92017 <= rate) return 0;
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 else if (75132 <= rate) return 1;
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 else if (55426 <= rate) return 2;
1189
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
59 else if (46009 <= rate) return 3;
1190
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 42 times.
58 else if (37566 <= rate) return 4;
1191
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42 else if (27713 <= rate) return 5;
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 else if (23004 <= rate) return 6;
1193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 else if (18783 <= rate) return 7;
1194
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 35 times.
39 else if (13856 <= rate) return 8;
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 else if (11502 <= rate) return 9;
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 else if (9391 <= rate) return 10;
1197 35 else return 11;
1198 }
1199
1200 518 static void reset_predictor_group(PredictorState *ps, int group_num)
1201 {
1202 int i;
1203
2/2
✓ Branch 0 taken 11662 times.
✓ Branch 1 taken 518 times.
12180 for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
1204 11662 reset_predict_state(&ps[i]);
1205 518 }
1206
1207 static void aacdec_init(AACContext *ac);
1208
1209 167 static av_cold void aac_static_table_init(void)
1210 {
1211 static VLC_TYPE vlc_buf[304 + 270 + 550 + 300 + 328 +
1212 294 + 306 + 268 + 510 + 366 + 462][2];
1213
2/2
✓ Branch 0 taken 1837 times.
✓ Branch 1 taken 167 times.
2004 for (unsigned i = 0, offset = 0; i < 11; i++) {
1214 1837 vlc_spectral[i].table = &vlc_buf[offset];
1215 1837 vlc_spectral[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
1216 1837 ff_init_vlc_sparse(&vlc_spectral[i], 8, ff_aac_spectral_sizes[i],
1217 1837 ff_aac_spectral_bits[i], sizeof(ff_aac_spectral_bits[i][0]),
1218 sizeof(ff_aac_spectral_bits[i][0]),
1219 1837 ff_aac_spectral_codes[i], sizeof(ff_aac_spectral_codes[i][0]),
1220 sizeof(ff_aac_spectral_codes[i][0]),
1221 1837 ff_aac_codebook_vector_idx[i], sizeof(ff_aac_codebook_vector_idx[i][0]),
1222 sizeof(ff_aac_codebook_vector_idx[i][0]),
1223 INIT_VLC_STATIC_OVERLONG);
1224 1837 offset += vlc_spectral[i].table_size;
1225 }
1226
1227 167 AAC_RENAME(ff_aac_sbr_init)();
1228
1229 167 ff_aac_tableinit();
1230
1231 167 INIT_VLC_STATIC(&vlc_scalefactors, 7,
1232 FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
1233 ff_aac_scalefactor_bits,
1234 sizeof(ff_aac_scalefactor_bits[0]),
1235 sizeof(ff_aac_scalefactor_bits[0]),
1236 ff_aac_scalefactor_code,
1237 sizeof(ff_aac_scalefactor_code[0]),
1238 sizeof(ff_aac_scalefactor_code[0]),
1239 352);
1240
1241 // window initialization
1242 #if !USE_FIXED
1243 151 AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(aac_kbd_long_960), 4.0, 960);
1244 151 AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(aac_kbd_short_120), 6.0, 120);
1245 151 AAC_RENAME(ff_sine_window_init)(AAC_RENAME(sine_960), 960);
1246 151 AAC_RENAME(ff_sine_window_init)(AAC_RENAME(sine_120), 120);
1247 151 AAC_RENAME(ff_init_ff_sine_windows)(9);
1248 151 ff_aac_float_common_init();
1249 #else
1250 16 AAC_RENAME(ff_kbd_window_init)(AAC_RENAME2(aac_kbd_long_1024), 4.0, 1024);
1251 16 AAC_RENAME(ff_kbd_window_init)(AAC_RENAME2(aac_kbd_short_128), 6.0, 128);
1252 16 init_sine_windows_fixed();
1253 #endif
1254
1255 167 AAC_RENAME(ff_cbrt_tableinit)();
1256 167 }
1257
1258 static AVOnce aac_table_init = AV_ONCE_INIT;
1259
1260 274 static av_cold int aac_decode_init(AVCodecContext *avctx)
1261 {
1262 274 AACContext *ac = avctx->priv_data;
1263 int ret;
1264
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (avctx->sample_rate > 96000)
1266 return AVERROR_INVALIDDATA;
1267
1268 274 ret = ff_thread_once(&aac_table_init, &aac_static_table_init);
1269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (ret != 0)
1270 return AVERROR_UNKNOWN;
1271
1272 274 ac->avctx = avctx;
1273 274 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1274
1275 274 aacdec_init(ac);
1276 #if USE_FIXED
1277 19 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1278 #else
1279 255 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1280 #endif /* USE_FIXED */
1281
1282
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 59 times.
274 if (avctx->extradata_size > 0) {
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
1284 215 avctx->extradata,
1285 215 avctx->extradata_size * 8LL,
1286 1)) < 0)
1287 return ret;
1288 } else {
1289 int sr, i;
1290 uint8_t layout_map[MAX_ELEM_ID*4][3];
1291 int layout_map_tags;
1292
1293 59 sr = sample_rate_idx(avctx->sample_rate);
1294 59 ac->oc[1].m4ac.sampling_index = sr;
1295 59 ac->oc[1].m4ac.channels = avctx->channels;
1296 59 ac->oc[1].m4ac.sbr = -1;
1297 59 ac->oc[1].m4ac.ps = -1;
1298
1299
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1300
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 48 times.
107 if (ff_mpeg4audio_channels[i] == avctx->channels)
1301 59 break;
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
1303 i = 0;
1304 }
1305 59 ac->oc[1].m4ac.chan_config = i;
1306
1307
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 35 times.
59 if (ac->oc[1].m4ac.chan_config) {
1308 24 int ret = set_default_channel_config(ac, avctx, layout_map,
1309 &layout_map_tags, ac->oc[1].m4ac.chan_config);
1310
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!ret)
1311 24 output_configure(ac, layout_map, layout_map_tags,
1312 OC_GLOBAL_HDR, 0);
1313 else if (avctx->err_recognition & AV_EF_EXPLODE)
1314 return AVERROR_INVALIDDATA;
1315 }
1316 }
1317
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (avctx->channels > MAX_CHANNELS) {
1319 av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1320 return AVERROR_INVALIDDATA;
1321 }
1322
1323 #if USE_FIXED
1324 19 ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1325 #else
1326 255 ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1327 #endif /* USE_FIXED */
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 274 times.
274 if (!ac->fdsp) {
1329 return AVERROR(ENOMEM);
1330 }
1331
1332 274 ac->random_state = 0x1f2e3d4c;
1333
1334 274 AAC_RENAME_32(ff_mdct_init)(&ac->mdct, 11, 1, 1.0 / RANGE15(1024.0));
1335 274 AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld, 10, 1, 1.0 / RANGE15(512.0));
1336 274 AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small, 8, 1, 1.0 / RANGE15(128.0));
1337 274 AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp, 11, 0, RANGE15(-2.0));
1338 #if !USE_FIXED
1339 255 ret = ff_mdct15_init(&ac->mdct120, 1, 3, 1.0f/(16*1024*120*2));
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (ret < 0)
1341 return ret;
1342 255 ret = ff_mdct15_init(&ac->mdct480, 1, 5, 1.0f/(16*1024*960));
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (ret < 0)
1344 return ret;
1345 255 ret = ff_mdct15_init(&ac->mdct960, 1, 6, 1.0f/(16*1024*960*2));
1346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (ret < 0)
1347 return ret;
1348 #endif
1349
1350 274 return 0;
1351 }
1352
1353 /**
1354 * Skip data_stream_element; reference: table 4.10.
1355 */
1356 2821 static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
1357 {
1358 2821 int byte_align = get_bits1(gb);
1359 2821 int count = get_bits(gb, 8);
1360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2821 times.
2821 if (count == 255)
1361 count += get_bits(gb, 8);
1362
2/2
✓ Branch 0 taken 2454 times.
✓ Branch 1 taken 367 times.
2821 if (byte_align)
1363 2454 align_get_bits(gb);
1364
1365
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2821 times.
2821 if (get_bits_left(gb) < 8 * count) {
1366 av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1367 return AVERROR_INVALIDDATA;
1368 }
1369 2821 skip_bits_long(gb, 8 * count);
1370 2821 return 0;
1371 }
1372
1373 1426 static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
1374 GetBitContext *gb)
1375 {
1376 int sfb;
1377
2/2
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 1067 times.
1426 if (get_bits1(gb)) {
1378 359 ics->predictor_reset_group = get_bits(gb, 5);
1379
1/2
✓ Branch 0 taken 359 times.
✗ Branch 1 not taken.
359 if (ics->predictor_reset_group == 0 ||
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 359 times.
359 ics->predictor_reset_group > 30) {
1381 av_log(ac->avctx, AV_LOG_ERROR,
1382 "Invalid Predictor Reset Group.\n");
1383 return AVERROR_INVALIDDATA;
1384 }
1385 }
1386
2/2
✓ Branch 0 taken 46375 times.
✓ Branch 1 taken 1426 times.
47801 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1387 46375 ics->prediction_used[sfb] = get_bits1(gb);
1388 }
1389 1426 return 0;
1390 }
1391
1392 /**
1393 * Decode Long Term Prediction data; reference: table 4.xx.
1394 */
1395 786 static void decode_ltp(LongTermPrediction *ltp,
1396 GetBitContext *gb, uint8_t max_sfb)
1397 {
1398 int sfb;
1399
1400 786 ltp->lag = get_bits(gb, 11);
1401 786 ltp->coef = ltp_coef[get_bits(gb, 3)];
1402
2/2
✓ Branch 0 taken 31440 times.
✓ Branch 1 taken 786 times.
32226 for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1403 31440 ltp->used[sfb] = get_bits1(gb);
1404 786 }
1405
1406 /**
1407 * Decode Individual Channel Stream info; reference: table 4.6.
1408 */
1409 63116 static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
1410 GetBitContext *gb)
1411 {
1412 63116 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1413 63116 const int aot = m4ac->object_type;
1414 63116 const int sampling_index = m4ac->sampling_index;
1415 63116 int ret_fail = AVERROR_INVALIDDATA;
1416
1417
2/2
✓ Branch 0 taken 46751 times.
✓ Branch 1 taken 16365 times.
63116 if (aot != AOT_ER_AAC_ELD) {
1418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46751 times.
46751 if (get_bits1(gb)) {
1419 av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1420 if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
1421 return AVERROR_INVALIDDATA;
1422 }
1423 46751 ics->window_sequence[1] = ics->window_sequence[0];
1424 46751 ics->window_sequence[0] = get_bits(gb, 2);
1425
2/2
✓ Branch 0 taken 2424 times.
✓ Branch 1 taken 44327 times.
46751 if (aot == AOT_ER_AAC_LD &&
1426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2424 times.
2424 ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1427 av_log(ac->avctx, AV_LOG_ERROR,
1428 "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1429 "window sequence %d found.\n", ics->window_sequence[0]);
1430 ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
1431 return AVERROR_INVALIDDATA;
1432 }
1433 46751 ics->use_kb_window[1] = ics->use_kb_window[0];
1434 46751 ics->use_kb_window[0] = get_bits1(gb);
1435 }
1436 63116 ics->num_window_groups = 1;
1437 63116 ics->group_len[0] = 1;
1438
2/2
✓ Branch 0 taken 1328 times.
✓ Branch 1 taken 61788 times.
63116 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1439 int i;
1440 1328 ics->max_sfb = get_bits(gb, 4);
1441
2/2
✓ Branch 0 taken 9296 times.
✓ Branch 1 taken 1328 times.
10624 for (i = 0; i < 7; i++) {
1442
2/2
✓ Branch 1 taken 5868 times.
✓ Branch 2 taken 3428 times.
9296 if (get_bits1(gb)) {
1443 5868 ics->group_len[ics->num_window_groups - 1]++;
1444 } else {
1445 3428 ics->num_window_groups++;
1446 3428 ics->group_len[ics->num_window_groups - 1] = 1;
1447 }
1448 }
1449 1328 ics->num_windows = 8;
1450
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1327 times.
1328 if (m4ac->frame_length_short) {
1451 1 ics->swb_offset = ff_swb_offset_120[sampling_index];
1452 1 ics->num_swb = ff_aac_num_swb_120[sampling_index];
1453 } else {
1454 1327 ics->swb_offset = ff_swb_offset_128[sampling_index];
1455 1327 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1456 }
1457 1328 ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1458 1328 ics->predictor_present = 0;
1459 } else {
1460 61788 ics->max_sfb = get_bits(gb, 6);
1461 61788 ics->num_windows = 1;
1462
4/4
✓ Branch 0 taken 59364 times.
✓ Branch 1 taken 2424 times.
✓ Branch 2 taken 16365 times.
✓ Branch 3 taken 42999 times.
61788 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1463
2/2
✓ Branch 0 taken 3559 times.
✓ Branch 1 taken 15230 times.
18789 if (m4ac->frame_length_short) {
1464 3559 ics->swb_offset = ff_swb_offset_480[sampling_index];
1465 3559 ics->num_swb = ff_aac_num_swb_480[sampling_index];
1466 3559 ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1467 } else {
1468 15230 ics->swb_offset = ff_swb_offset_512[sampling_index];
1469 15230 ics->num_swb = ff_aac_num_swb_512[sampling_index];
1470 15230 ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1471 }
1472
2/4
✓ Branch 0 taken 18789 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18789 times.
18789 if (!ics->num_swb || !ics->swb_offset) {
1473 ret_fail = AVERROR_BUG;
1474 goto fail;
1475 }
1476 } else {
1477
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 42613 times.
42999 if (m4ac->frame_length_short) {
1478 386 ics->num_swb = ff_aac_num_swb_960[sampling_index];
1479 386 ics->swb_offset = ff_swb_offset_960[sampling_index];
1480 } else {
1481 42613 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1482 42613 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1483 }
1484 42999 ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1485 }
1486
2/2
✓ Branch 0 taken 45423 times.
✓ Branch 1 taken 16365 times.
61788 if (aot != AOT_ER_AAC_ELD) {
1487 45423 ics->predictor_present = get_bits1(gb);
1488 45423 ics->predictor_reset_group = 0;
1489 }
1490
2/2
✓ Branch 0 taken 1964 times.
✓ Branch 1 taken 59824 times.
61788 if (ics->predictor_present) {
1491
2/2
✓ Branch 0 taken 1426 times.
✓ Branch 1 taken 538 times.
1964 if (aot == AOT_AAC_MAIN) {
1492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1426 times.
1426 if (decode_prediction(ac, ics, gb)) {
1493 goto fail;
1494 }
1495
2/4
✓ Branch 0 taken 538 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 538 times.
538 } else if (aot == AOT_AAC_LC ||
1496 aot == AOT_ER_AAC_LC) {
1497 av_log(ac->avctx, AV_LOG_ERROR,
1498 "Prediction is not allowed in AAC-LC.\n");
1499 goto fail;
1500 } else {
1501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538 times.
538 if (aot == AOT_ER_AAC_LD) {
1502 av_log(ac->avctx, AV_LOG_ERROR,
1503 "LTP in ER AAC LD not yet implemented.\n");
1504 ret_fail = AVERROR_PATCHWELCOME;
1505 goto fail;
1506 }
1507
2/2
✓ Branch 1 taken 382 times.
✓ Branch 2 taken 156 times.
538 if ((ics->ltp.present = get_bits(gb, 1)))
1508 382 decode_ltp(&ics->ltp, gb, ics->max_sfb);
1509 }
1510 }
1511 }
1512
1513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63116 times.
63116 if (ics->max_sfb > ics->num_swb) {
1514 av_log(ac->avctx, AV_LOG_ERROR,
1515 "Number of scalefactor bands in group (%d) "
1516 "exceeds limit (%d).\n",
1517 ics->max_sfb, ics->num_swb);
1518 goto fail;
1519 }
1520
1521 63116 return 0;
1522 fail:
1523 ics->max_sfb = 0;
1524 return ret_fail;
1525 }
1526
1527 /**
1528 * Decode band types (section_data payload); reference: table 4.46.
1529 *
1530 * @param band_type array of the used band type
1531 * @param band_type_run_end array of the last scalefactor band of a band type run
1532 *
1533 * @return Returns error status. 0 - OK, !0 - error
1534 */
1535 93576 static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1536 int band_type_run_end[120], GetBitContext *gb,
1537 IndividualChannelStream *ics)
1538 {
1539 93576 int g, idx = 0;
1540
2/2
✓ Branch 0 taken 2086 times.
✓ Branch 1 taken 91490 times.
93576 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1541
2/2
✓ Branch 0 taken 98814 times.
✓ Branch 1 taken 93576 times.
192390 for (g = 0; g < ics->num_window_groups; g++) {
1542 98814 int k = 0;
1543
2/2
✓ Branch 0 taken 463189 times.
✓ Branch 1 taken 98814 times.
562003 while (k < ics->max_sfb) {
1544 463189 uint8_t sect_end = k;
1545 int sect_len_incr;
1546 463189 int sect_band_type = get_bits(gb, 4);
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463189 times.
463189 if (sect_band_type == 12) {
1548 av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1549 return AVERROR_INVALIDDATA;
1550 }
1551 do {
1552 481880 sect_len_incr = get_bits(gb, bits);
1553 481880 sect_end += sect_len_incr;
1554
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 481880 times.
481880 if (get_bits_left(gb) < 0) {
1555 av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1556 return AVERROR_INVALIDDATA;
1557 }
1558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481880 times.
481880 if (sect_end > ics->max_sfb) {
1559 av_log(ac->avctx, AV_LOG_ERROR,
1560 "Number of bands (%d) exceeds limit (%d).\n",
1561 sect_end, ics->max_sfb);
1562 return AVERROR_INVALIDDATA;
1563 }
1564
2/2
✓ Branch 0 taken 18691 times.
✓ Branch 1 taken 463189 times.
481880 } while (sect_len_incr == (1 << bits) - 1);
1565
2/2
✓ Branch 0 taken 3464366 times.
✓ Branch 1 taken 463189 times.
3927555 for (; k < sect_end; k++) {
1566 3464366 band_type [idx] = sect_band_type;
1567 3464366 band_type_run_end[idx++] = sect_end;
1568 }
1569 }
1570 }
1571 93576 return 0;
1572 }
1573
1574 /**
1575 * Decode scalefactors; reference: table 4.47.
1576 *
1577 * @param global_gain first scalefactor value as scalefactors are differentially coded
1578 * @param band_type array of the used band type
1579 * @param band_type_run_end array of the last scalefactor band of a band type run
1580 * @param sf array of scalefactors or intensity stereo positions
1581 *
1582 * @return Returns error status. 0 - OK, !0 - error
1583 */
1584 93576 static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
1585 unsigned int global_gain,
1586 IndividualChannelStream *ics,
1587 enum BandType band_type[120],
1588 int band_type_run_end[120])
1589 {
1590 93576 int g, i, idx = 0;
1591 93576 int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1592 int clipped_offset;
1593 93576 int noise_flag = 1;
1594
2/2
✓ Branch 0 taken 98814 times.
✓ Branch 1 taken 93576 times.
192390 for (g = 0; g < ics->num_window_groups; g++) {
1595
2/2
✓ Branch 0 taken 463189 times.
✓ Branch 1 taken 98814 times.
562003 for (i = 0; i < ics->max_sfb;) {
1596 463189 int run_end = band_type_run_end[idx];
1597
2/2
✓ Branch 0 taken 63204 times.
✓ Branch 1 taken 399985 times.
463189 if (band_type[idx] == ZERO_BT) {
1598
2/2
✓ Branch 0 taken 533534 times.
✓ Branch 1 taken 63204 times.
596738 for (; i < run_end; i++, idx++)
1599 533534 sf[idx] = FIXR(0.);
1600
2/2
✓ Branch 0 taken 390513 times.
✓ Branch 1 taken 9472 times.
399985 } else if ((band_type[idx] == INTENSITY_BT) ||
1601
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 390402 times.
390513 (band_type[idx] == INTENSITY_BT2)) {
1602
2/2
✓ Branch 0 taken 66207 times.
✓ Branch 1 taken 9583 times.
75790 for (; i < run_end; i++, idx++) {
1603 66207 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1604 66207 clipped_offset = av_clip(offset[2], -155, 100);
1605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66207 times.
66207 if (offset[2] != clipped_offset) {
1606 avpriv_request_sample(ac->avctx,
1607 "If you heard an audible artifact, there may be a bug in the decoder. "
1608 "Clipped intensity stereo position (%d -> %d)",
1609 offset[2], clipped_offset);
1610 }
1611 #if USE_FIXED
1612 17856 sf[idx] = 100 - clipped_offset;
1613 #else
1614 48351 sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1615 #endif /* USE_FIXED */
1616 }
1617
2/2
✓ Branch 0 taken 7825 times.
✓ Branch 1 taken 382577 times.
390402 } else if (band_type[idx] == NOISE_BT) {
1618
2/2
✓ Branch 0 taken 281615 times.
✓ Branch 1 taken 7825 times.
289440 for (; i < run_end; i++, idx++) {
1619
2/2
✓ Branch 0 taken 6715 times.
✓ Branch 1 taken 274900 times.
281615 if (noise_flag-- > 0)
1620 6715 offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1621 else
1622 274900 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1623 281615 clipped_offset = av_clip(offset[1], -100, 155);
1624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281615 times.
281615 if (offset[1] != clipped_offset) {
1625 avpriv_request_sample(ac->avctx,
1626 "If you heard an audible artifact, there may be a bug in the decoder. "
1627 "Clipped noise gain (%d -> %d)",
1628 offset[1], clipped_offset);
1629 }
1630 #if USE_FIXED
1631 137931 sf[idx] = -(100 + clipped_offset);
1632 #else
1633 143684 sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1634 #endif /* USE_FIXED */
1635 }
1636 } else {
1637
2/2
✓ Branch 0 taken 2583010 times.
✓ Branch 1 taken 382577 times.
2965587 for (; i < run_end; i++, idx++) {
1638 2583010 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2583010 times.
2583010 if (offset[0] > 255U) {
1640 av_log(ac->avctx, AV_LOG_ERROR,
1641 "Scalefactor (%d) out of range.\n", offset[0]);
1642 return AVERROR_INVALIDDATA;
1643 }
1644 #if USE_FIXED
1645 772587 sf[idx] = -offset[0];
1646 #else
1647 1810423 sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1648 #endif /* USE_FIXED */
1649 }
1650 }
1651 }
1652 }
1653 93576 return 0;
1654 }
1655
1656 /**
1657 * Decode pulse data; reference: table 4.7.
1658 */
1659 1098 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1660 const uint16_t *swb_offset, int num_swb)
1661 {
1662 int i, pulse_swb;
1663 1098 pulse->num_pulse = get_bits(gb, 2) + 1;
1664 1098 pulse_swb = get_bits(gb, 6);
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (pulse_swb >= num_swb)
1666 return -1;
1667 1098 pulse->pos[0] = swb_offset[pulse_swb];
1668 1098 pulse->pos[0] += get_bits(gb, 5);
1669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (pulse->pos[0] >= swb_offset[num_swb])
1670 return -1;
1671 1098 pulse->amp[0] = get_bits(gb, 4);
1672
2/2
✓ Branch 0 taken 1646 times.
✓ Branch 1 taken 1098 times.
2744 for (i = 1; i < pulse->num_pulse; i++) {
1673 1646 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1646 times.
1646 if (pulse->pos[i] >= swb_offset[num_swb])
1675 return -1;
1676 1646 pulse->amp[i] = get_bits(gb, 4);
1677 }
1678 1098 return 0;
1679 }
1680
1681 /**
1682 * Decode Temporal Noise Shaping data; reference: table 4.48.
1683 *
1684 * @return Returns error status. 0 - OK, !0 - error
1685 */
1686 6927 static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
1687 GetBitContext *gb, const IndividualChannelStream *ics)
1688 {
1689 int w, filt, i, coef_len, coef_res, coef_compress;
1690 6927 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1691
4/4
✓ Branch 0 taken 6268 times.
✓ Branch 1 taken 659 times.
✓ Branch 2 taken 163 times.
✓ Branch 3 taken 6105 times.
6927 const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1692
2/2
✓ Branch 0 taken 11540 times.
✓ Branch 1 taken 6927 times.
18467 for (w = 0; w < ics->num_windows; w++) {
1693
2/2
✓ Branch 1 taken 7918 times.
✓ Branch 2 taken 3622 times.
11540 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1694 7918 coef_res = get_bits1(gb);
1695
1696
2/2
✓ Branch 0 taken 9379 times.
✓ Branch 1 taken 7918 times.
17297 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1697 int tmp2_idx;
1698 9379 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1699
1700
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9379 times.
9379 if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1701 av_log(ac->avctx, AV_LOG_ERROR,
1702 "TNS filter order %d is greater than maximum %d.\n",
1703 tns->order[w][filt], tns_max_order);
1704 tns->order[w][filt] = 0;
1705 return AVERROR_INVALIDDATA;
1706 }
1707
2/2
✓ Branch 0 taken 8859 times.
✓ Branch 1 taken 520 times.
9379 if (tns->order[w][filt]) {
1708 8859 tns->direction[w][filt] = get_bits1(gb);
1709 8859 coef_compress = get_bits1(gb);
1710 8859 coef_len = coef_res + 3 - coef_compress;
1711 8859 tmp2_idx = 2 * coef_compress + coef_res;
1712
1713
2/2
✓ Branch 0 taken 58308 times.
✓ Branch 1 taken 8859 times.
67167 for (i = 0; i < tns->order[w][filt]; i++)
1714 58308 tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1715 }
1716 }
1717 }
1718 }
1719 6927 return 0;
1720 }
1721
1722 /**
1723 * Decode Mid/Side data; reference: table 4.54.
1724 *
1725 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1726 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1727 * [3] reserved for scalable AAC
1728 */
1729 26972 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1730 int ms_present)
1731 {
1732 int idx;
1733 26972 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1734
2/2
✓ Branch 0 taken 12685 times.
✓ Branch 1 taken 14287 times.
26972 if (ms_present == 1) {
1735
2/2
✓ Branch 0 taken 497780 times.
✓ Branch 1 taken 12685 times.
510465 for (idx = 0; idx < max_idx; idx++)
1736 497780 cpe->ms_mask[idx] = get_bits1(gb);
1737
1/2
✓ Branch 0 taken 14287 times.
✗ Branch 1 not taken.
14287 } else if (ms_present == 2) {
1738 14287 memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1739 }
1740 26972 }
1741
1742 /**
1743 * Decode spectral data; reference: table 4.50.
1744 * Dequantize and scale spectral data; reference: 4.6.3.3.
1745 *
1746 * @param coef array of dequantized, scaled spectral data
1747 * @param sf array of scalefactors or intensity stereo positions
1748 * @param pulse_present set if pulses are present
1749 * @param pulse pointer to pulse data struct
1750 * @param band_type array of the used band type
1751 *
1752 * @return Returns error status. 0 - OK, !0 - error
1753 */
1754 93576 static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
1755 GetBitContext *gb, const INTFLOAT sf[120],
1756 int pulse_present, const Pulse *pulse,
1757 const IndividualChannelStream *ics,
1758 enum BandType band_type[120])
1759 {
1760 93576 int i, k, g, idx = 0;
1761 93576 const int c = 1024 / ics->num_windows;
1762 93576 const uint16_t *offsets = ics->swb_offset;
1763 93576 INTFLOAT *coef_base = coef;
1764
1765
2/2
✓ Branch 0 taken 108178 times.
✓ Branch 1 taken 93576 times.
201754 for (g = 0; g < ics->num_windows; g++)
1766 108178 memset(coef + g * 128 + offsets[ics->max_sfb], 0,
1767 108178 sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
1768
1769
2/2
✓ Branch 0 taken 98814 times.
✓ Branch 1 taken 93576 times.
192390 for (g = 0; g < ics->num_window_groups; g++) {
1770 98814 unsigned g_len = ics->group_len[g];
1771
1772
2/2
✓ Branch 0 taken 3464366 times.
✓ Branch 1 taken 98814 times.
3563180 for (i = 0; i < ics->max_sfb; i++, idx++) {
1773 3464366 const unsigned cbt_m1 = band_type[idx] - 1;
1774 3464366 INTFLOAT *cfo = coef + offsets[i];
1775 3464366 int off_len = offsets[i + 1] - offsets[i];
1776 int group;
1777
1778
2/2
✓ Branch 0 taken 599741 times.
✓ Branch 1 taken 2864625 times.
3464366 if (cbt_m1 >= INTENSITY_BT2 - 1) {
1779
2/2
✓ Branch 0 taken 637814 times.
✓ Branch 1 taken 599741 times.
1237555 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1780 637814 memset(cfo, 0, off_len * sizeof(*cfo));
1781 }
1782
2/2
✓ Branch 0 taken 281615 times.
✓ Branch 1 taken 2583010 times.
2864625 } else if (cbt_m1 == NOISE_BT - 1) {
1783
2/2
✓ Branch 0 taken 281642 times.
✓ Branch 1 taken 281615 times.
563257 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1784 INTFLOAT band_energy;
1785 #if USE_FIXED
1786
2/2
✓ Branch 0 taken 2883476 times.
✓ Branch 1 taken 137931 times.
3021407 for (k = 0; k < off_len; k++) {
1787 2883476 ac->random_state = lcg_random(ac->random_state);
1788 2883476 cfo[k] = ac->random_state >> 3;
1789 }
1790
1791 137931 band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
1792 137931 band_energy = fixed_sqrt(band_energy, 31);
1793 137931 noise_scale(cfo, sf[idx], band_energy, off_len);
1794 #else
1795 float scale;
1796
1797
2/2
✓ Branch 0 taken 3060724 times.
✓ Branch 1 taken 143711 times.
3204435 for (k = 0; k < off_len; k++) {
1798 3060724 ac->random_state = lcg_random(ac->random_state);
1799 3060724 cfo[k] = ac->random_state;
1800 }
1801
1802 143711 band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
1803 143711 scale = sf[idx] / sqrtf(band_energy);
1804 143711 ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
1805 #endif /* USE_FIXED */
1806 }
1807 } else {
1808 #if !USE_FIXED
1809 1810423 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1810 #endif /* !USE_FIXED */
1811 2583010 VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1812 2583010 OPEN_READER(re, gb);
1813
1814
5/5
✓ Branch 0 taken 626588 times.
✓ Branch 1 taken 577284 times.
✓ Branch 2 taken 397453 times.
✓ Branch 3 taken 542942 times.
✓ Branch 4 taken 438743 times.
2583010 switch (cbt_m1 >> 1) {
1815 626588 case 0:
1816
2/2
✓ Branch 0 taken 653917 times.
✓ Branch 1 taken 626588 times.
1280505 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1817 653917 INTFLOAT *cf = cfo;
1818 653917 int len = off_len;
1819
1820 do {
1821 int code;
1822 unsigned cb_idx;
1823
1824 3251102 UPDATE_CACHE(re, gb);
1825
2/2
✓ Branch 1 taken 110026 times.
✓ Branch 2 taken 3141076 times.
3251102 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1826 3251102 cb_idx = code;
1827 #if USE_FIXED
1828 978157 cf = DEC_SQUAD(cf, cb_idx);
1829 #else
1830 2272945 cf = VMUL4(cf, vq, cb_idx, sf + idx);
1831 #endif /* USE_FIXED */
1832
2/2
✓ Branch 0 taken 2597185 times.
✓ Branch 1 taken 653917 times.
3251102 } while (len -= 4);
1833 }
1834 626588 break;
1835
1836 577284 case 1:
1837
2/2
✓ Branch 0 taken 589727 times.
✓ Branch 1 taken 577284 times.
1167011 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1838 589727 INTFLOAT *cf = cfo;
1839 589727 int len = off_len;
1840
1841 do {
1842 int code;
1843 unsigned nnz;
1844 unsigned cb_idx;
1845 uint32_t bits;
1846
1847 2626440 UPDATE_CACHE(re, gb);
1848
2/2
✓ Branch 1 taken 93825 times.
✓ Branch 2 taken 2532615 times.
2626440 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1849 2626440 cb_idx = code;
1850 2626440 nnz = cb_idx >> 8 & 15;
1851
2/2
✓ Branch 0 taken 1726581 times.
✓ Branch 1 taken 899859 times.
2626440 bits = nnz ? GET_CACHE(re, gb) : 0;
1852 2626440 LAST_SKIP_BITS(re, gb, nnz);
1853 #if USE_FIXED
1854 816691 cf = DEC_UQUAD(cf, cb_idx, bits);
1855 #else
1856 1809749 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1857 #endif /* USE_FIXED */
1858
2/2
✓ Branch 0 taken 2036713 times.
✓ Branch 1 taken 589727 times.
2626440 } while (len -= 4);
1859 }
1860 577284 break;
1861
1862 397453 case 2:
1863
2/2
✓ Branch 0 taken 410129 times.
✓ Branch 1 taken 397453 times.
807582 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1864 410129 INTFLOAT *cf = cfo;
1865 410129 int len = off_len;
1866
1867 do {
1868 int code;
1869 unsigned cb_idx;
1870
1871 2487082 UPDATE_CACHE(re, gb);
1872
2/2
✓ Branch 1 taken 87117 times.
✓ Branch 2 taken 2399965 times.
2487082 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1873 2487082 cb_idx = code;
1874 #if USE_FIXED
1875 672272 cf = DEC_SPAIR(cf, cb_idx);
1876 #else
1877 1814810 cf = VMUL2(cf, vq, cb_idx, sf + idx);
1878 #endif /* USE_FIXED */
1879
2/2
✓ Branch 0 taken 2076953 times.
✓ Branch 1 taken 410129 times.
2487082 } while (len -= 2);
1880 }
1881 397453 break;
1882
1883 542942 case 3:
1884 case 4:
1885
2/2
✓ Branch 0 taken 561429 times.
✓ Branch 1 taken 542942 times.
1104371 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1886 561429 INTFLOAT *cf = cfo;
1887 561429 int len = off_len;
1888
1889 do {
1890 int code;
1891 unsigned nnz;
1892 unsigned cb_idx;
1893 unsigned sign;
1894
1895 2977742 UPDATE_CACHE(re, gb);
1896
2/2
✓ Branch 1 taken 212019 times.
✓ Branch 2 taken 2765723 times.
2977742 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1897 2977742 cb_idx = code;
1898 2977742 nnz = cb_idx >> 8 & 15;
1899
2/2
✓ Branch 0 taken 2371121 times.
✓ Branch 1 taken 606621 times.
2977742 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1900 2977742 LAST_SKIP_BITS(re, gb, nnz);
1901 #if USE_FIXED
1902 703742 cf = DEC_UPAIR(cf, cb_idx, sign);
1903 #else
1904 2274000 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1905 #endif /* USE_FIXED */
1906
2/2
✓ Branch 0 taken 2416313 times.
✓ Branch 1 taken 561429 times.
2977742 } while (len -= 2);
1907 }
1908 542942 break;
1909
1910 438743 default:
1911
2/2
✓ Branch 0 taken 448600 times.
✓ Branch 1 taken 438743 times.
887343 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1912 #if USE_FIXED
1913 119275 int *icf = cfo;
1914 int v;
1915 #else
1916 329325 float *cf = cfo;
1917 329325 uint32_t *icf = (uint32_t *) cf;
1918 #endif /* USE_FIXED */
1919 448600 int len = off_len;
1920
1921 do {
1922 int code;
1923 unsigned nzt, nnz;
1924 unsigned cb_idx;
1925 uint32_t bits;
1926 int j;
1927
1928 1485852 UPDATE_CACHE(re, gb);
1929
2/2
✓ Branch 1 taken 424415 times.
✓ Branch 2 taken 1061437 times.
1485852 GET_VLC(code, re, gb, vlc_tab, 8, 2);
1930 1485852 cb_idx = code;
1931
1932
2/2
✓ Branch 0 taken 70646 times.
✓ Branch 1 taken 1415206 times.
1485852 if (cb_idx == 0x0000) {
1933 70646 *icf++ = 0;
1934 70646 *icf++ = 0;
1935 70646 continue;
1936 }
1937
1938 1415206 nnz = cb_idx >> 12;
1939 1415206 nzt = cb_idx >> 8;
1940 1415206 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1941 1415206 LAST_SKIP_BITS(re, gb, nnz);
1942
1943
2/2
✓ Branch 0 taken 2830412 times.
✓ Branch 1 taken 1415206 times.
4245618 for (j = 0; j < 2; j++) {
1944
2/2
✓ Branch 0 taken 365035 times.
✓ Branch 1 taken 2465377 times.
2830412 if (nzt & 1<<j) {
1945 uint32_t b;
1946 int n;
1947 /* The total length of escape_sequence must be < 22 bits according
1948 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1949 365035 UPDATE_CACHE(re, gb);
1950 365035 b = GET_CACHE(re, gb);
1951 365035 b = 31 - av_log2(~b);
1952
1953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 365035 times.
365035 if (b > 8) {
1954 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1955 return AVERROR_INVALIDDATA;
1956 }
1957
1958 365035 SKIP_BITS(re, gb, b + 1);
1959 365035 b += 4;
1960 365035 n = (1 << b) + SHOW_UBITS(re, gb, b);
1961 365035 LAST_SKIP_BITS(re, gb, b);
1962 #if USE_FIXED
1963 64951 v = n;
1964
2/2
✓ Branch 0 taken 32336 times.
✓ Branch 1 taken 32615 times.
64951 if (bits & 1U<<31)
1965 32336 v = -v;
1966 64951 *icf++ = v;
1967 #else
1968 300084 *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
1969 #endif /* USE_FIXED */
1970 365035 bits <<= 1;
1971 } else {
1972 #if USE_FIXED
1973 539259 v = cb_idx & 15;
1974
2/2
✓ Branch 0 taken 256298 times.
✓ Branch 1 taken 282961 times.
539259 if (bits & 1U<<31)
1975 256298 v = -v;
1976 539259 *icf++ = v;
1977 #else
1978 1926118 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1979 1926118 *icf++ = (bits & 1U<<31) | v;
1980 #endif /* USE_FIXED */
1981 2465377 bits <<= !!v;
1982 }
1983 2830412 cb_idx >>= 4;
1984 }
1985
2/2
✓ Branch 0 taken 1037252 times.
✓ Branch 1 taken 448600 times.
1485852 } while (len -= 2);
1986 #if !USE_FIXED
1987 329325 ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1988 #endif /* !USE_FIXED */
1989 }
1990 }
1991
1992 2583010 CLOSE_READER(re, gb);
1993 }
1994 }
1995 98814 coef += g_len << 7;
1996 }
1997
1998
2/2
✓ Branch 0 taken 1098 times.
✓ Branch 1 taken 92478 times.
93576 if (pulse_present) {
1999 1098 idx = 0;
2000
2/2
✓ Branch 0 taken 2744 times.
✓ Branch 1 taken 1098 times.
3842 for (i = 0; i < pulse->num_pulse; i++) {
2001 2744 INTFLOAT co = coef_base[ pulse->pos[i] ];
2002
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 2744 times.
6840 while (offsets[idx + 1] <= pulse->pos[i])
2003 4096 idx++;
2004
3/4
✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2668 times.
✓ Branch 3 taken 76 times.
2744 if (band_type[idx] != NOISE_BT && sf[idx]) {
2005 2668 INTFLOAT ico = -pulse->amp[i];
2006 #if USE_FIXED
2007
2/2
✓ Branch 0 taken 730 times.
✓ Branch 1 taken 134 times.
864 if (co) {
2008
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 351 times.
730 ico = co + (co > 0 ? -ico : ico);
2009 }
2010 864 coef_base[ pulse->pos[i] ] = ico;
2011 #else
2012
2/2
✓ Branch 0 taken 1564 times.
✓ Branch 1 taken 240 times.
1804 if (co) {
2013 1564 co /= sf[idx];
2014
2/2
✓ Branch 0 taken 799 times.
✓ Branch 1 taken 765 times.
1564 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
2015 }
2016 1804 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
2017 #endif /* USE_FIXED */
2018 }
2019 }
2020 }
2021 #if USE_FIXED
2022 28710 coef = coef_base;
2023 28710 idx = 0;
2024
2/2
✓ Branch 0 taken 31042 times.
✓ Branch 1 taken 28710 times.
59752 for (g = 0; g < ics->num_window_groups; g++) {
2025 31042 unsigned g_len = ics->group_len[g];
2026
2027
2/2
✓ Branch 0 taken 1040049 times.
✓ Branch 1 taken 31042 times.
1071091 for (i = 0; i < ics->max_sfb; i++, idx++) {
2028 1040049 const unsigned cbt_m1 = band_type[idx] - 1;
2029 1040049 int *cfo = coef + offsets[i];
2030 1040049 int off_len = offsets[i + 1] - offsets[i];
2031 int group;
2032
2033
2/2
✓ Branch 0 taken 772587 times.
✓ Branch 1 taken 267462 times.
1040049 if (cbt_m1 < NOISE_BT - 1) {
2034
2/2
✓ Branch 0 taken 787858 times.
✓ Branch 1 taken 772587 times.
1560445 for (group = 0; group < (int)g_len; group++, cfo+=128) {
2035 787858 ac->vector_pow43(cfo, off_len);
2036 787858 ac->subband_scale(cfo, cfo, sf[idx], 34, off_len, ac->avctx);
2037 }
2038 }
2039 }
2040 31042 coef += g_len << 7;
2041 }
2042 #endif /* USE_FIXED */
2043 93576 return 0;
2044 }
2045
2046 /**
2047 * Apply AAC-Main style frequency domain prediction.
2048 */
2049 7388 static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
2050 {
2051 int sfb, k;
2052
2053
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 7368 times.
7388 if (!sce->ics.predictor_initialized) {
2054 20 reset_all_predictors(sce->predictor_state);
2055 20 sce->ics.predictor_initialized = 1;
2056 }
2057
2058
2/2
✓ Branch 0 taken 7368 times.
✓ Branch 1 taken 20 times.
7388 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2059 7368 for (sfb = 0;
2060
2/2
✓ Branch 0 taken 255198 times.
✓ Branch 1 taken 7368 times.
262566 sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
2061 255198 sfb++) {
2062 255198 for (k = sce->ics.swb_offset[sfb];
2063
2/2
✓ Branch 0 taken 4047936 times.
✓ Branch 1 taken 255198 times.
4303134 k < sce->ics.swb_offset[sfb + 1];
2064 4047936 k++) {
2065 4047936 predict(&sce->predictor_state[k], &sce->coeffs[k],
2066
2/2
✓ Branch 0 taken 892992 times.
✓ Branch 1 taken 3154944 times.
4047936 sce->ics.predictor_present &&
2067
2/2
✓ Branch 0 taken 271896 times.
✓ Branch 1 taken 621096 times.
892992 sce->ics.prediction_used[sfb]);
2068 }
2069 }
2070
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 6850 times.
7368 if (sce->ics.predictor_reset_group)
2071 518 reset_predictor_group(sce->predictor_state,
2072 sce->ics.predictor_reset_group);
2073 } else
2074 20 reset_all_predictors(sce->predictor_state);
2075 7388 }
2076
2077 static void decode_gain_control(SingleChannelElement * sce, GetBitContext * gb)
2078 {
2079 // wd_num, wd_test, aloc_size
2080 static const uint8_t gain_mode[4][3] = {
2081 {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
2082 {2, 1, 2}, // LONG_START_SEQUENCE,
2083 {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
2084 {2, 1, 5}, // LONG_STOP_SEQUENCE
2085 };
2086
2087 const int mode = sce->ics.window_sequence[0];
2088 uint8_t bd, wd, ad;
2089
2090 // FIXME: Store the gain control data on |sce| and do something with it.
2091 uint8_t max_band = get_bits(gb, 2);
2092 for (bd = 0; bd < max_band; bd++) {
2093 for (wd = 0; wd < gain_mode[mode][0]; wd++) {
2094 uint8_t adjust_num = get_bits(gb, 3);
2095 for (ad = 0; ad < adjust_num; ad++) {
2096 skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
2097 ? 4
2098 : gain_mode[mode][2]));
2099 }
2100 }
2101 }
2102 }
2103
2104 /**
2105 * Decode an individual_channel_stream payload; reference: table 4.44.
2106 *
2107 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
2108 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
2109 *
2110 * @return Returns error status. 0 - OK, !0 - error
2111 */
2112 93576 static int decode_ics(AACContext *ac, SingleChannelElement *sce,
2113 GetBitContext *gb, int common_window, int scale_flag)
2114 {
2115 Pulse pulse;
2116 93576 TemporalNoiseShaping *tns = &sce->tns;
2117 93576 IndividualChannelStream *ics = &sce->ics;
2118 93576 INTFLOAT *out = sce->coeffs;
2119 93576 int global_gain, eld_syntax, er_syntax, pulse_present = 0;
2120 int ret;
2121
2122 93576 eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2123 280728 er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
2124
1/2
✓ Branch 0 taken 93576 times.
✗ Branch 1 not taken.
93576 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
2125
3/4
✓ Branch 0 taken 93576 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 89940 times.
✓ Branch 3 taken 3636 times.
277092 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
2126
2/2
✓ Branch 0 taken 26598 times.
✓ Branch 1 taken 63342 times.
89940 ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2127
2128 /* This assignment is to silence a GCC warning about the variable being used
2129 * uninitialized when in fact it always is.
2130 */
2131 93576 pulse.num_pulse = 0;
2132
2133 93576 global_gain = get_bits(gb, 8);
2134
2135
3/4
✓ Branch 0 taken 32656 times.
✓ Branch 1 taken 60920 times.
✓ Branch 2 taken 32656 times.
✗ Branch 3 not taken.
93576 if (!common_window && !scale_flag) {
2136 32656 ret = decode_ics_info(ac, ics, gb);
2137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32656 times.
32656 if (ret < 0)
2138 goto fail;
2139 }
2140
2141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93576 times.
93576 if ((ret = decode_band_types(ac, sce->band_type,
2142 93576 sce->band_type_run_end, gb, ics)) < 0)
2143 goto fail;
2144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93576 times.
93576 if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
2145 93576 sce->band_type, sce->band_type_run_end)) < 0)
2146 goto fail;
2147
2148 93576 pulse_present = 0;
2149
1/2
✓ Branch 0 taken 93576 times.
✗ Branch 1 not taken.
93576 if (!scale_flag) {
2150
4/4
✓ Branch 0 taken 66978 times.
✓ Branch 1 taken 26598 times.
✓ Branch 3 taken 1098 times.
✓ Branch 4 taken 65880 times.
93576 if (!eld_syntax && (pulse_present = get_bits1(gb))) {
2151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2152 av_log(ac->avctx, AV_LOG_ERROR,
2153 "Pulse tool not allowed in eight short sequence.\n");
2154 ret = AVERROR_INVALIDDATA;
2155 goto fail;
2156 }
2157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1098 times.
1098 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
2158 av_log(ac->avctx, AV_LOG_ERROR,
2159 "Pulse data corrupt or invalid.\n");
2160 ret = AVERROR_INVALIDDATA;
2161 goto fail;
2162 }
2163 }
2164 93576 tns->present = get_bits1(gb);
2165
4/4
✓ Branch 0 taken 6927 times.
✓ Branch 1 taken 86649 times.
✓ Branch 2 taken 5663 times.
✓ Branch 3 taken 1264 times.
93576 if (tns->present && !er_syntax) {
2166 5663 ret = decode_tns(ac, tns, gb, ics);
2167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5663 times.
5663 if (ret < 0)
2168 goto fail;
2169 }
2170
3/4
✓ Branch 0 taken 66978 times.
✓ Branch 1 taken 26598 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 66978 times.
93576 if (!eld_syntax && get_bits1(gb)) {
2171 decode_gain_control(sce, gb);
2172 if (!ac->warned_gain_control) {
2173 avpriv_report_missing_feature(ac->avctx, "Gain control");
2174 ac->warned_gain_control = 1;
2175 }
2176 }
2177 // I see no textual basis in the spec for this occurring after SSR gain
2178 // control, but this is what both reference and real implmentations do
2179
4/4
✓ Branch 0 taken 6927 times.
✓ Branch 1 taken 86649 times.
✓ Branch 2 taken 1264 times.
✓ Branch 3 taken 5663 times.
93576 if (tns->present && er_syntax) {
2180 1264 ret = decode_tns(ac, tns, gb, ics);
2181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1264 times.
1264 if (ret < 0)
2182 goto fail;
2183 }
2184 }
2185
2186 93576 ret = decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
2187 93576 &pulse, ics, sce->band_type);
2188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93576 times.
93576 if (ret < 0)
2189 goto fail;
2190
2191
4/4
✓ Branch 0 taken 7388 times.
✓ Branch 1 taken 86188 times.
✓ Branch 2 taken 6094 times.
✓ Branch 3 taken 1294 times.
93576 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
2192 6094 apply_prediction(ac, sce);
2193
2194 93576 return 0;
2195 fail:
2196 tns->present = 0;
2197 return ret;
2198 }
2199
2200 /**
2201 * Mid/Side stereo decoding; reference: 4.6.8.1.3.
2202 */
2203 26972 static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
2204 {
2205 26972 const IndividualChannelStream *ics = &cpe->ch[0].ics;
2206 26972 INTFLOAT *ch0 = cpe->ch[0].coeffs;
2207 26972 INTFLOAT *ch1 = cpe->ch[1].coeffs;
2208 26972 int g, i, group, idx = 0;
2209 26972 const uint16_t *offsets = ics->swb_offset;
2210
2/2
✓ Branch 0 taken 28677 times.
✓ Branch 1 taken 26972 times.
55649 for (g = 0; g < ics->num_window_groups; g++) {
2211
2/2
✓ Branch 0 taken 1012976 times.
✓ Branch 1 taken 28677 times.
1041653 for (i = 0; i < ics->max_sfb; i++, idx++) {
2212
2/2
✓ Branch 0 taken 795390 times.
✓ Branch 1 taken 217586 times.
1012976 if (cpe->ms_mask[idx] &&
2213
1/2
✓ Branch 0 taken 795390 times.
✗ Branch 1 not taken.
795390 cpe->ch[0].band_type[idx] < NOISE_BT &&
2214
2/2
✓ Branch 0 taken 793014 times.
✓ Branch 1 taken 2376 times.
795390 cpe->ch[1].band_type[idx] < NOISE_BT) {
2215 #if USE_FIXED
2216
2/2
✓ Branch 0 taken 253158 times.
✓ Branch 1 taken 247459 times.
500617 for (group = 0; group < ics->group_len[g]; group++) {
2217 253158 ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
2218 253158 ch1 + group * 128 + offsets[i],
2219 253158 offsets[i+1] - offsets[i]);
2220 #else
2221
2/2
✓ Branch 0 taken 570043 times.
✓ Branch 1 taken 545555 times.
1115598 for (group = 0; group < ics->group_len[g]; group++) {
2222 570043 ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
2223 570043 ch1 + group * 128 + offsets[i],
2224 570043 offsets[i+1] - offsets[i]);
2225 #endif /* USE_FIXED */
2226 }
2227 }
2228 }
2229 28677 ch0 += ics->group_len[g] * 128;
2230 28677 ch1 += ics->group_len[g] * 128;
2231 }
2232 26972 }
2233
2234 /**
2235 * intensity stereo decoding; reference: 4.6.8.2.3
2236 *
2237 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
2238 * [1] mask is decoded from bitstream; [2] mask is all 1s;
2239 * [3] reserved for scalable AAC
2240 */
2241 30682 static void apply_intensity_stereo(AACContext *ac,
2242 ChannelElement *cpe, int ms_present)
2243 {
2244 30682 const IndividualChannelStream *ics = &cpe->ch[1].ics;
2245 30682 SingleChannelElement *sce1 = &cpe->ch[1];
2246 30682 INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
2247 30682 const uint16_t *offsets = ics->swb_offset;
2248 30682 int g, group, i, idx = 0;
2249 int c;
2250 INTFLOAT scale;
2251
2/2
✓ Branch 0 taken 32610 times.
✓ Branch 1 taken 30682 times.
63292 for (g = 0; g < ics->num_window_groups; g++) {
2252
2/2
✓ Branch 0 taken 160764 times.
✓ Branch 1 taken 32610 times.
193374 for (i = 0; i < ics->max_sfb;) {
2253
2/2
✓ Branch 0 taken 151292 times.
✓ Branch 1 taken 9472 times.
160764 if (sce1->band_type[idx] == INTENSITY_BT ||
2254
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 151181 times.
160875 sce1->band_type[idx] == INTENSITY_BT2) {
2255 9583 const int bt_run_end = sce1->band_type_run_end[idx];
2256
2/2
✓ Branch 0 taken 66207 times.
✓ Branch 1 taken 9583 times.
75790 for (; i < bt_run_end; i++, idx++) {
2257 66207 c = -1 + 2 * (sce1->band_type[idx] - 14);
2258
2/2
✓ Branch 0 taken 61324 times.
✓ Branch 1 taken 4883 times.
66207 if (ms_present)
2259 61324 c *= 1 - 2 * cpe->ms_mask[idx];
2260 66207 scale = c * sce1->sf[idx];
2261
2/2
✓ Branch 0 taken 66538 times.
✓ Branch 1 taken 66207 times.
132745 for (group = 0; group < ics->group_len[g]; group++)
2262 #if USE_FIXED
2263 17955 ac->subband_scale(coef1 + group * 128 + offsets[i],
2264 17955 coef0 + group * 128 + offsets[i],
2265 scale,
2266 23,
2267 17955 offsets[i + 1] - offsets[i] ,ac->avctx);
2268 #else
2269 48583 ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
2270 48583 coef0 + group * 128 + offsets[i],
2271 scale,
2272 48583 offsets[i + 1] - offsets[i]);
2273 #endif /* USE_FIXED */
2274 }
2275 } else {
2276 151181 int bt_run_end = sce1->band_type_run_end[idx];
2277 151181 idx += bt_run_end - i;
2278 151181 i = bt_run_end;
2279 }
2280 }
2281 32610 coef0 += ics->group_len[g] * 128;
2282 32610 coef1 += ics->group_len[g] * 128;
2283 }
2284 30682 }
2285
2286 /**
2287 * Decode a channel_pair_element; reference: table 4.4.
2288 *
2289 * @return Returns error status. 0 - OK, !0 - error
2290 */
2291 30682 static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
2292 {
2293 30682 int i, ret, common_window, ms_present = 0;
2294 30682 int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2295
2296
4/4
✓ Branch 0 taken 20449 times.
✓ Branch 1 taken 10233 times.
✓ Branch 3 taken 20227 times.
✓ Branch 4 taken 222 times.
30682 common_window = eld_syntax || get_bits1(gb);
2297
2/2
✓ Branch 0 taken 30460 times.
✓ Branch 1 taken 222 times.
30682 if (common_window) {
2298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30460 times.
30460 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
2299 return AVERROR_INVALIDDATA;
2300 30460 i = cpe->ch[1].ics.use_kb_window[0];
2301 30460 cpe->ch[1].ics = cpe->ch[0].ics;
2302 30460 cpe->ch[1].ics.use_kb_window[1] = i;
2303
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 29737 times.
30460 if (cpe->ch[1].ics.predictor_present &&
2304
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 185 times.
723 (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
2305
2/2
✓ Branch 1 taken 404 times.
✓ Branch 2 taken 134 times.
538 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
2306 404 decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
2307 30460 ms_present = get_bits(gb, 2);
2308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30460 times.
30460 if (ms_present == 3) {
2309 av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
2310 return AVERROR_INVALIDDATA;
2311
2/2
✓ Branch 0 taken 26972 times.
✓ Branch 1 taken 3488 times.
30460 } else if (ms_present)
2312 26972 decode_mid_side_stereo(cpe, gb, ms_present);
2313 }
2314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30682 times.
30682 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
2315 return ret;
2316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30682 times.
30682 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
2317 return ret;
2318
2319
2/2
✓ Branch 0 taken 30460 times.
✓ Branch 1 taken 222 times.
30682 if (common_window) {
2320
2/2
✓ Branch 0 taken 26972 times.
✓ Branch 1 taken 3488 times.
30460 if (ms_present)
2321 26972 apply_mid_side_stereo(ac, cpe);
2322
2/2
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 29813 times.
30460 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
2323 647 apply_prediction(ac, &cpe->ch[0]);
2324 647 apply_prediction(ac, &cpe->ch[1]);
2325 }
2326 }
2327
2328 30682 apply_intensity_stereo(ac, cpe, ms_present);
2329 30682 return 0;
2330 }
2331
2332 static const float cce_scale[] = {
2333 1.09050773266525765921, //2^(1/8)
2334 1.18920711500272106672, //2^(1/4)
2335 M_SQRT2,
2336 2,
2337 };
2338
2339 /**
2340 * Decode coupling_channel_element; reference: table 4.8.
2341 *
2342 * @return Returns error status. 0 - OK, !0 - error
2343 */
2344 1174 static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
2345 {
2346 1174 int num_gain = 0;
2347 int c, g, sfb, ret;
2348 int sign;
2349 INTFLOAT scale;
2350 1174 SingleChannelElement *sce = &che->ch[0];
2351 1174 ChannelCoupling *coup = &che->coup;
2352
2353 1174 coup->coupling_point = 2 * get_bits1(gb);
2354 1174 coup->num_coupled = get_bits(gb, 3);
2355
2/2
✓ Branch 0 taken 2741 times.
✓ Branch 1 taken 1174 times.
3915 for (c = 0; c <= coup->num_coupled; c++) {
2356 2741 num_gain++;
2357 2741 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
2358 2741 coup->id_select[c] = get_bits(gb, 4);
2359
2/2
✓ Branch 0 taken 2348 times.
✓ Branch 1 taken 393 times.
2741 if (coup->type[c] == TYPE_CPE) {
2360 2348 coup->ch_select[c] = get_bits(gb, 2);
2361
2/2
✓ Branch 0 taken 1967 times.
✓ Branch 1 taken 381 times.
2348 if (coup->ch_select[c] == 3)
2362 1967 num_gain++;
2363 } else
2364 393 coup->ch_select[c] = 2;
2365 }
2366
4/4
✓ Branch 1 taken 602 times.
✓ Branch 2 taken 572 times.
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 450 times.
1174 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
2367
2368 1174 sign = get_bits(gb, 1);
2369 #if USE_FIXED
2370 130 scale = get_bits(gb, 2);
2371 #else
2372 1044 scale = cce_scale[get_bits(gb, 2)];
2373 #endif
2374
2375
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1174 times.
1174 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
2376 return ret;
2377
2378
2/2
✓ Branch 0 taken 4708 times.
✓ Branch 1 taken 1174 times.
5882 for (c = 0; c < num_gain; c++) {
2379 4708 int idx = 0;
2380 4708 int cge = 1;
2381 4708 int gain = 0;
2382 4708 INTFLOAT gain_cache = FIXR10(1.);
2383
2/2
✓ Branch 0 taken 3534 times.
✓ Branch 1 taken 1174 times.
4708 if (c) {
2384
2/2
✓ Branch 0 taken 1962 times.
✓ Branch 1 taken 1572 times.
3534 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
2385
2/2
✓ Branch 0 taken 1973 times.
✓ Branch 1 taken 1561 times.
3534 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
2386 3534 gain_cache = GET_GAIN(scale, gain);
2387 #if USE_FIXED
2388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if ((abs(gain_cache)-1024) >> 3 > 30)
2389 return AVERROR(ERANGE);
2390 #endif
2391 }
2392
2/2
✓ Branch 0 taken 1965 times.
✓ Branch 1 taken 2743 times.
4708 if (coup->coupling_point == AFTER_IMDCT) {
2393 1965 coup->gain[c][0] = gain_cache;
2394 } else {
2395
2/2
✓ Branch 0 taken 2743 times.
✓ Branch 1 taken 2743 times.
5486 for (g = 0; g < sce->ics.num_window_groups; g++) {
2396
2/2
✓ Branch 0 taken 109720 times.
✓ Branch 1 taken 2743 times.
112463 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
2397
2/2
✓ Branch 0 taken 5196 times.
✓ Branch 1 taken 104524 times.
109720 if (sce->band_type[idx] != ZERO_BT) {
2398
2/2
✓ Branch 0 taken 2879 times.
✓ Branch 1 taken 2317 times.
5196 if (!cge) {
2399 2879 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
2400
2/2
✓ Branch 0 taken 2724 times.
✓ Branch 1 taken 155 times.
2879 if (t) {
2401 2724 int s = 1;
2402 2724 t = gain += t;
2403
2/2
✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 1058 times.
2724 if (sign) {
2404 1666 s -= 2 * (t & 0x1);
2405 1666 t >>= 1;
2406 }
2407 2724 gain_cache = GET_GAIN(scale, t) * s;
2408 #if USE_FIXED
2409 if ((abs(gain_cache)-1024) >> 3 > 30)
2410 return AVERROR(ERANGE);
2411 #endif
2412 }
2413 }
2414 5196 coup->gain[c][idx] = gain_cache;
2415 }
2416 }
2417 }
2418 }
2419 }
2420 1174 return 0;
2421 }
2422
2423 /**
2424 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
2425 *
2426 * @return Returns number of bytes consumed.
2427 */
2428 4 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
2429 GetBitContext *gb)
2430 {
2431 int i;
2432 4 int num_excl_chan = 0;
2433
2434 do {
2435
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (i = 0; i < 7; i++)
2436 28 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
2437
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
2438
2439 4 return num_excl_chan / 7;
2440 }
2441
2442 /**
2443 * Decode dynamic range information; reference: table 4.52.
2444 *
2445 * @return Returns number of bytes consumed.
2446 */
2447 880 static int decode_dynamic_range(DynamicRangeControl *che_drc,
2448 GetBitContext *gb)
2449 {
2450 880 int n = 1;
2451 880 int drc_num_bands = 1;
2452 int i;
2453
2454 /* pce_tag_present? */
2455
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 880 times.
880 if (get_bits1(gb)) {
2456 che_drc->pce_instance_tag = get_bits(gb, 4);
2457 skip_bits(gb, 4); // tag_reserved_bits
2458 n++;
2459 }
2460
2461 /* excluded_chns_present? */
2462
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 876 times.
880 if (get_bits1(gb)) {
2463 4 n += decode_drc_channel_exclusions(che_drc, gb);
2464 }
2465
2466 /* drc_bands_present? */
2467
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 845 times.
880 if (get_bits1(gb)) {
2468 35 che_drc->band_incr = get_bits(gb, 4);
2469 35 che_drc->interpolation_scheme = get_bits(gb, 4);
2470 35 n++;
2471 35 drc_num_bands += che_drc->band_incr;
2472
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 35 times.
140 for (i = 0; i < drc_num_bands; i++) {
2473 105 che_drc->band_top[i] = get_bits(gb, 8);
2474 105 n++;
2475 }
2476 }
2477
2478 /* prog_ref_level_present? */
2479
2/2
✓ Branch 1 taken 878 times.
✓ Branch 2 taken 2 times.
880 if (get_bits1(gb)) {
2480 878 che_drc->prog_ref_level = get_bits(gb, 7);
2481 878 skip_bits1(gb); // prog_ref_level_reserved_bits
2482 878 n++;
2483 }
2484
2485
2/2
✓ Branch 0 taken 950 times.
✓ Branch 1 taken 880 times.
1830 for (i = 0; i < drc_num_bands; i++) {
2486 950 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2487 950 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2488 950 n++;
2489 }
2490
2491 880 return n;
2492 }
2493
2494 8949 static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
2495 uint8_t buf[256];
2496 int i, major, minor;
2497
2498
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 8569 times.
8949 if (len < 13+7*8)
2499 380 goto unknown;
2500
2501 8569 get_bits(gb, 13); len -= 13;
2502
2503
4/4
✓ Branch 0 taken 1136273 times.
✓ Branch 1 taken 441 times.
✓ Branch 2 taken 1128145 times.
✓ Branch 3 taken 8128 times.
1136714 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2504 1128145 buf[i] = get_bits(gb, 8);
2505
2506 8569 buf[i] = 0;
2507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8569 times.
8569 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2508 av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2509
2510
2/2
✓ Branch 0 taken 8550 times.
✓ Branch 1 taken 19 times.
8569 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2511 19 ac->avctx->internal->skip_samples = 1024;
2512 }
2513
2514 8550 unknown:
2515 8949 skip_bits_long(gb, len);
2516
2517 8949 return 0;
2518 }
2519
2520 /**
2521 * Decode extension data (incomplete); reference: table 4.51.
2522 *
2523 * @param cnt length of TYPE_FIL syntactic element in bytes
2524 *
2525 * @return Returns number of bytes consumed
2526 */
2527 23680 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
2528 ChannelElement *che, enum RawDataBlockType elem_type)
2529 {
2530 23680 int crc_flag = 0;
2531 23680 int res = cnt;
2532 23680 int type = get_bits(gb, 4);
2533
2534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23680 times.
23680 if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2535 av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2536
2537
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
✓ Branch 2 taken 880 times.
✓ Branch 3 taken 8949 times.
✓ Branch 4 taken 2950 times.
23680 switch (type) { // extension type
2538 case EXT_SBR_DATA_CRC:
2539 crc_flag++;
2540 10901 case EXT_SBR_DATA:
2541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 if (!che) {
2542 av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2543 return res;
2544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 } else if (ac->oc[1].m4ac.frame_length_short) {
2545 if (!ac->warned_960_sbr)
2546 avpriv_report_missing_feature(ac->avctx,
2547 "SBR with 960 frame length");
2548 ac->warned_960_sbr = 1;
2549 skip_bits_long(gb, 8 * cnt - 4);
2550 return res;
2551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10901 times.
10901 } else if (!ac->oc[1].m4ac.sbr) {
2552 av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2553 skip_bits_long(gb, 8 * cnt - 4);
2554 return res;
2555
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10879 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
10901 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2556 av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2557 skip_bits_long(gb, 8 * cnt - 4);
2558 return res;
2559
5/6
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 10807 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 82 times.
10901 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2560 12 ac->oc[1].m4ac.sbr = 1;
2561 12 ac->oc[1].m4ac.ps = 1;
2562 12 ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
2563 12 output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2564 ac->oc[1].status, 1);
2565 } else {
2566 10889 ac->oc[1].m4ac.sbr = 1;
2567 10889 ac->avctx->profile = FF_PROFILE_AAC_HE;
2568 }
2569 10901 res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2570 10901 break;
2571 880 case EXT_DYNAMIC_RANGE:
2572 880 res = decode_dynamic_range(&ac->che_drc, gb);
2573 880 break;
2574 8949 case EXT_FILL:
2575 8949 decode_fill(ac, gb, 8 * cnt - 4);
2576 8949 break;
2577 2950 case EXT_FILL_DATA:
2578 case EXT_DATA_ELEMENT:
2579 default:
2580 2950 skip_bits_long(gb, 8 * cnt - 4);
2581 2950 break;
2582 };
2583 23680 return res;
2584 }
2585
2586 /**
2587 * Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
2588 *
2589 * @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2590 * @param coef spectral coefficients
2591 */
2592 6933 static void apply_tns(INTFLOAT coef_param[1024], TemporalNoiseShaping *tns,
2593 IndividualChannelStream *ics, int decode)
2594 {
2595 6933 const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2596 int w, filt, m, i;
2597 int bottom, top, order, start, end, size, inc;
2598 INTFLOAT lpc[TNS_MAX_ORDER];
2599 INTFLOAT tmp[TNS_MAX_ORDER+1];
2600 6933 UINTFLOAT *coef = coef_param;
2601
2602
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6927 times.
6933 if(!mmm)
2603 6 return;
2604
2605
2/2
✓ Branch 0 taken 11540 times.
✓ Branch 1 taken 6927 times.
18467 for (w = 0; w < ics->num_windows; w++) {
2606 11540 bottom = ics->num_swb;
2607
2/2
✓ Branch 0 taken 9379 times.
✓ Branch 1 taken 11540 times.
20919 for (filt = 0; filt < tns->n_filt[w]; filt++) {
2608 9379 top = bottom;
2609 9379 bottom = FFMAX(0, top - tns->length[w][filt]);
2610 9379 order = tns->order[w][filt];
2611
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 8859 times.
9379 if (order == 0)
2612 520 continue;
2613
2614 // tns_decode_coef
2615 8859 AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
2616
2617
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8853 times.
8859 start = ics->swb_offset[FFMIN(bottom, mmm)];
2618
2/2
✓ Branch 0 taken 7061 times.
✓ Branch 1 taken 1798 times.
8859 end = ics->swb_offset[FFMIN( top, mmm)];
2619
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8851 times.
8859 if ((size = end - start) <= 0)
2620 8 continue;
2621
2/2
✓ Branch 0 taken 3590 times.
✓ Branch 1 taken 5261 times.
8851 if (tns->direction[w][filt]) {
2622 3590 inc = -1;
2623 3590 start = end - 1;
2624 } else {
2625 5261 inc = 1;
2626 }
2627 8851 start += w * 128;
2628
2629
2/2
✓ Branch 0 taken 8845 times.
✓ Branch 1 taken 6 times.
8851 if (decode) {
2630 // ar filter
2631
2/2
✓ Branch 0 taken 2730044 times.
✓ Branch 1 taken 8845 times.
2738889 for (m = 0; m < size; m++, start += inc)
2632
2/2
✓ Branch 0 taken 21153425 times.
✓ Branch 1 taken 2730044 times.
23883469 for (i = 1; i <= FFMIN(m, order); i++)
2633 21153425 coef[start] -= AAC_MUL26((INTFLOAT)coef[start - i * inc], lpc[i - 1]);
2634 } else {
2635 // ma filter
2636
2/2
✓ Branch 0 taken 3696 times.
✓ Branch 1 taken 6 times.
3702 for (m = 0; m < size; m++, start += inc) {
2637 3696 tmp[0] = coef[start];
2638
2/2
✓ Branch 0 taken 35416 times.
✓ Branch 1 taken 3696 times.
39112 for (i = 1; i <= FFMIN(m, order); i++)
2639 35416 coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
2640
2/2
✓ Branch 0 taken 35728 times.
✓ Branch 1 taken 3696 times.
39424 for (i = order; i > 0; i--)
2641 35728 tmp[i] = tmp[i - 1];
2642 }
2643 }
2644 }
2645 }
2646 }
2647
2648 /**
2649 * Apply windowing and MDCT to obtain the spectral
2650 * coefficient from the predicted sample by LTP.
2651 */
2652 786 static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
2653 INTFLOAT *in, IndividualChannelStream *ics)
2654 {
2655
1/2
✓ Branch 0 taken 786 times.
✗ Branch 1 not taken.
786 const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2656
1/2
✓ Branch 0 taken 786 times.
✗ Branch 1 not taken.
786 const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2657
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 2 times.
786 const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2658
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 2 times.
786 const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2659
2660
2/2
✓ Branch 0 taken 784 times.
✓ Branch 1 taken 2 times.
786 if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2661 784 ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
2662 } else {
2663 2 memset(in, 0, 448 * sizeof(*in));
2664 2 ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
2665 }
2666
1/2
✓ Branch 0 taken 786 times.
✗ Branch 1 not taken.
786 if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2667 786 ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2668 } else {
2669 ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2670 memset(in + 1024 + 576, 0, 448 * sizeof(*in));
2671 }
2672 786 ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2673 786 }
2674
2675 /**
2676 * Apply the long term prediction
2677 */
2678 786 static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
2679 {
2680 786 const LongTermPrediction *ltp = &sce->ics.ltp;
2681 786 const uint16_t *offsets = sce->ics.swb_offset;
2682 int i, sfb;
2683
2684
1/2
✓ Branch 0 taken 786 times.
✗ Branch 1 not taken.
786 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2685 786 INTFLOAT *predTime = sce->ret;
2686 786 INTFLOAT *predFreq = ac->buf_mdct;
2687 786 int16_t num_samples = 2048;
2688
2689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 786 times.
786 if (ltp->lag < 1024)
2690 num_samples = ltp->lag + 1024;
2691
2/2
✓ Branch 0 taken 1609728 times.
✓ Branch 1 taken 786 times.
1610514 for (i = 0; i < num_samples; i++)
2692 1609728 predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
2693 786 memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
2694
2695 786 ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2696
2697
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 780 times.
786 if (sce->tns.present)
2698 6 ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2699
2700
2/2
✓ Branch 0 taken 31440 times.
✓ Branch 1 taken 786 times.
32226 for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2701
2/2
✓ Branch 0 taken 22868 times.
✓ Branch 1 taken 8572 times.
31440 if (ltp->used[sfb])
2702
2/2
✓ Branch 0 taken 347544 times.
✓ Branch 1 taken 22868 times.
370412 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2703 347544 sce->coeffs[i] += (UINTFLOAT)predFreq[i];
2704 }
2705 786 }
2706
2707 /**
2708 * Update the LTP buffer for next frame
2709 */
2710 3568 static void update_ltp(AACContext *ac, SingleChannelElement *sce)
2711 {
2712 3568 IndividualChannelStream *ics = &sce->ics;
2713 3568 INTFLOAT *saved = sce->saved;
2714 3568 INTFLOAT *saved_ltp = sce->coeffs;
2715
2/2
✓ Branch 0 taken 3560 times.
✓ Branch 1 taken 8 times.
3568 const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2716
2/2
✓ Branch 0 taken 3560 times.
✓ Branch 1 taken 8 times.
3568 const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2717 int i;
2718
2719
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3564 times.
3568 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2720 4 memcpy(saved_ltp, saved, 512 * sizeof(*saved_ltp));
2721 4 memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2722 4 ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2723
2724
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
260 for (i = 0; i < 64; i++)
2725 256 saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2726
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3560 times.
3564 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2727 4 memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
2728 4 memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2729 4 ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2730
2731
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
260 for (i = 0; i < 64; i++)
2732 256 saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2733 } else { // LONG_STOP or ONLY_LONG
2734 3560 ac->fdsp->vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2735
2736
2/2
✓ Branch 0 taken 1822720 times.
✓ Branch 1 taken 3560 times.
1826280 for (i = 0; i < 512; i++)
2737 1822720 saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
2738 }
2739
2740 3568 memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2741 3568 memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2742 3568 memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2743 3568 }
2744
2745 /**
2746 * Conduct IMDCT and windowing.
2747 */
2748 62174 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
2749 {
2750 62174 IndividualChannelStream *ics = &sce->ics;
2751 62174 INTFLOAT *in = sce->coeffs;
2752 62174 INTFLOAT *out = sce->ret;
2753 62174 INTFLOAT *saved = sce->saved;
2754
2/2
✓ Branch 0 taken 40464 times.
✓ Branch 1 taken 21710 times.
62174 const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2755
2/2
✓ Branch 0 taken 40317 times.
✓ Branch 1 taken 21857 times.
62174 const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_long_1024) : AAC_RENAME2(sine_1024);
2756
2/2
✓ Branch 0 taken 40317 times.
✓ Branch 1 taken 21857 times.
62174 const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME2(aac_kbd_short_128) : AAC_RENAME2(sine_128);
2757 62174 INTFLOAT *buf = ac->buf_mdct;
2758 62174 INTFLOAT *temp = ac->temp;
2759 int i;
2760
2761 // imdct
2762
2/2
✓ Branch 0 taken 2085 times.
✓ Branch 1 taken 60089 times.
62174 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2763
2/2
✓ Branch 0 taken 16680 times.
✓ Branch 1 taken 2085 times.
18765 for (i = 0; i < 1024; i += 128)
2764 16680 ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2765 } else {
2766 60089 ac->mdct.imdct_half(&ac->mdct, buf, in);
2767 #if USE_FIXED
2768
2/2
✓ Branch 0 taken 16798720 times.
✓ Branch 1 taken 16405 times.
16815125 for (i=0; i<1024; i++)
2769 16798720 buf[i] = (buf[i] + 4LL) >> 3;
2770 #endif /* USE_FIXED */
2771 }
2772
2773 /* window overlapping
2774 * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2775 * and long to short transitions are considered to be short to short
2776 * transitions. This leaves just two cases (long to long and short to short)
2777 * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2778 */
2779
4/4
✓ Branch 0 taken 4240 times.
✓ Branch 1 taken 57934 times.
✓ Branch 2 taken 1076 times.
✓ Branch 3 taken 3164 times.
62174 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2780
4/4
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 57804 times.
✓ Branch 2 taken 1204 times.
✓ Branch 3 taken 2 times.
59010 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2781 59008 ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2782 } else {
2783 3166 memcpy( out, saved, 448 * sizeof(*out));
2784
2785
2/2
✓ Branch 0 taken 2085 times.
✓ Branch 1 taken 1081 times.
3166 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2786 2085 ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2787 2085 ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2788 2085 ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2789 2085 ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2790 2085 ac->fdsp->vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2791 2085 memcpy( out + 448 + 4*128, temp, 64 * sizeof(*out));
2792 } else {
2793 1081 ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2794 1081 memcpy( out + 576, buf + 64, 448 * sizeof(*out));
2795 }
2796 }
2797
2798 // buffer update
2799
2/2
✓ Branch 0 taken 2085 times.
✓ Branch 1 taken 60089 times.
62174 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2800 2085 memcpy( saved, temp + 64, 64 * sizeof(*saved));
2801 2085 ac->fdsp->vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2802 2085 ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2803 2085 ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2804 2085 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2805
2/2
✓ Branch 0 taken 1204 times.
✓ Branch 1 taken 58885 times.
60089 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2806 1204 memcpy( saved, buf + 512, 448 * sizeof(*saved));
2807 1204 memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2808 } else { // LONG_STOP or ONLY_LONG
2809 58885 memcpy( saved, buf + 512, 512 * sizeof(*saved));
2810 }
2811 62174 }
2812
2813 /**
2814 * Conduct IMDCT and windowing.
2815 */
2816 387 static void imdct_and_windowing_960(AACContext *ac, SingleChannelElement *sce)
2817 {
2818 #if !USE_FIXED
2819 387 IndividualChannelStream *ics = &sce->ics;
2820 387 INTFLOAT *in = sce->coeffs;
2821 387 INTFLOAT *out = sce->ret;
2822 387 INTFLOAT *saved = sce->saved;
2823
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 373 times.
387 const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
2824
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 373 times.
387 const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_long_960) : AAC_RENAME(sine_960);
2825
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 373 times.
387 const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(aac_kbd_short_120) : AAC_RENAME(sine_120);
2826 387 INTFLOAT *buf = ac->buf_mdct;
2827 387 INTFLOAT *temp = ac->temp;
2828 int i;
2829
2830 // imdct
2831
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 386 times.
387 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2832
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (i = 0; i < 8; i++)
2833 8 ac->mdct120->imdct_half(ac->mdct120, buf + i * 120, in + i * 128, 1);
2834 } else {
2835 386 ac->mdct960->imdct_half(ac->mdct960, buf, in, 1);
2836 }
2837
2838 /* window overlapping
2839 * NOTE: To simplify the overlapping code, all 'meaningless' short to long
2840 * and long to short transitions are considered to be short to short
2841 * transitions. This leaves just two cases (long to long and short to short)
2842 * with a little special sauce for EIGHT_SHORT_SEQUENCE.
2843 */
2844
2845
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
387 if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2846
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 383 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
385 (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2847 385 ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 480);
2848 } else {
2849 2 memcpy( out, saved, 420 * sizeof(*out));
2850
2851
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2852 1 ac->fdsp->vector_fmul_window(out + 420 + 0*120, saved + 420, buf + 0*120, swindow_prev, 60);
2853 1 ac->fdsp->vector_fmul_window(out + 420 + 1*120, buf + 0*120 + 60, buf + 1*120, swindow, 60);
2854 1 ac->fdsp->vector_fmul_window(out + 420 + 2*120, buf + 1*120 + 60, buf + 2*120, swindow, 60);
2855 1 ac->fdsp->vector_fmul_window(out + 420 + 3*120, buf + 2*120 + 60, buf + 3*120, swindow, 60);
2856 1 ac->fdsp->vector_fmul_window(temp, buf + 3*120 + 60, buf + 4*120, swindow, 60);
2857 1 memcpy( out + 420 + 4*120, temp, 60 * sizeof(*out));
2858 } else {
2859 1 ac->fdsp->vector_fmul_window(out + 420, saved + 420, buf, swindow_prev, 60);
2860 1 memcpy( out + 540, buf + 60, 420 * sizeof(*out));
2861 }
2862 }
2863
2864 // buffer update
2865
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 386 times.
387 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2866 1 memcpy( saved, temp + 60, 60 * sizeof(*saved));
2867 1 ac->fdsp->vector_fmul_window(saved + 60, buf + 4*120 + 60, buf + 5*120, swindow, 60);
2868 1 ac->fdsp->vector_fmul_window(saved + 180, buf + 5*120 + 60, buf + 6*120, swindow, 60);
2869 1 ac->fdsp->vector_fmul_window(saved + 300, buf + 6*120 + 60, buf + 7*120, swindow, 60);
2870 1 memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
2871
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 384 times.
386 } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2872 2 memcpy( saved, buf + 480, 420 * sizeof(*saved));
2873 2 memcpy( saved + 420, buf + 7*120 + 60, 60 * sizeof(*saved));
2874 } else { // LONG_STOP or ONLY_LONG
2875 384 memcpy( saved, buf + 480, 480 * sizeof(*saved));
2876 }
2877 #endif
2878 387 }
2879 3636 static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
2880 {
2881 3636 IndividualChannelStream *ics = &sce->ics;
2882 3636 INTFLOAT *in = sce->coeffs;
2883 3636 INTFLOAT *out = sce->ret;
2884 3636 INTFLOAT *saved = sce->saved;
2885 3636 INTFLOAT *buf = ac->buf_mdct;
2886 #if USE_FIXED
2887 int i;
2888 #endif /* USE_FIXED */
2889
2890 // imdct
2891 3636 ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2892
2893 #if USE_FIXED
2894
2/2
✓ Branch 0 taken 1855488 times.
✓ Branch 1 taken 1812 times.
1857300 for (i = 0; i < 1024; i++)
2895 1855488 buf[i] = (buf[i] + 2) >> 2;
2896 #endif /* USE_FIXED */
2897
2898 // window overlapping
2899
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 3574 times.
3636 if (ics->use_kb_window[1]) {
2900 // AAC LD uses a low overlap sine window instead of a KBD window
2901 62 memcpy(out, saved, 192 * sizeof(*out));
2902 62 ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME2(sine_128), 64);
2903 62 memcpy( out + 320, buf + 64, 192 * sizeof(*out));
2904 } else {
2905 3574 ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME2(sine_512), 256);
2906 }
2907
2908 // buffer update
2909 3636 memcpy(saved, buf + 256, 256 * sizeof(*saved));
2910 3636 }
2911
2912 26598 static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
2913 {
2914 26598 UINTFLOAT *in = sce->coeffs;
2915 26598 INTFLOAT *out = sce->ret;
2916 26598 INTFLOAT *saved = sce->saved;
2917 26598 INTFLOAT *buf = ac->buf_mdct;
2918 int i;
2919
2/2
✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 19480 times.
26598 const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
2920 26598 const int n2 = n >> 1;
2921 26598 const int n4 = n >> 2;
2922
2/2
✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 19480 times.
26598 const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
2923 AAC_RENAME(ff_aac_eld_window_512);
2924
2925 // Inverse transform, mapped to the conventional IMDCT by
2926 // Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
2927 // "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
2928 // International Conference on Audio, Language and Image Processing, ICALIP 2008.
2929 // URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
2930
2/2
✓ Branch 0 taken 3347600 times.
✓ Branch 1 taken 26598 times.
3374198 for (i = 0; i < n2; i+=2) {
2931 INTFLOAT temp;
2932 3347600 temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
2933 3347600 temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
2934 }
2935 #if !USE_FIXED
2936
2/2
✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 9743 times.
16861 if (n == 480)
2937 7118 ac->mdct480->imdct_half(ac->mdct480, buf, in, 1);
2938 else
2939 #endif
2940 19480 ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2941
2942 #if USE_FIXED
2943
2/2
✓ Branch 0 taken 9970688 times.
✓ Branch 1 taken 9737 times.
9980425 for (i = 0; i < 1024; i++)
2944 9970688 buf[i] = (buf[i] + 1) >> 1;
2945 #endif /* USE_FIXED */
2946
2947
2/2
✓ Branch 0 taken 6695200 times.
✓ Branch 1 taken 26598 times.
6721798 for (i = 0; i < n; i+=2) {
2948 6695200 buf[i] = -buf[i];
2949 }
2950 // Like with the regular IMDCT at this point we still have the middle half
2951 // of a transform but with even symmetry on the left and odd symmetry on
2952 // the right
2953
2954 // window overlapping
2955 // The spec says to use samples [0..511] but the reference decoder uses
2956 // samples [128..639].
2957
2/2
✓ Branch 0 taken 3347600 times.
✓ Branch 1 taken 26598 times.
3374198 for (i = n4; i < n2; i ++) {
2958 3347600 out[i - n4] = AAC_MUL31( buf[ n2 - 1 - i] , window[i - n4]) +
2959 3347600 AAC_MUL31( saved[ i + n2] , window[i + n - n4]) +
2960 3347600 AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
2961 3347600 AAC_MUL31(-saved[ 2*n + n2 + i] , window[i + 3*n - n4]);
2962 }
2963
2/2
✓ Branch 0 taken 6695200 times.
✓ Branch 1 taken 26598 times.
6721798 for (i = 0; i < n2; i ++) {
2964 6695200 out[n4 + i] = AAC_MUL31( buf[ i] , window[i + n2 - n4]) +
2965 6695200 AAC_MUL31(-saved[ n - 1 - i] , window[i + n2 + n - n4]) +
2966 6695200 AAC_MUL31(-saved[ n + i] , window[i + n2 + 2*n - n4]) +
2967 6695200 AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
2968 }
2969
2/2
✓ Branch 0 taken 3347600 times.
✓ Branch 1 taken 26598 times.
3374198 for (i = 0; i < n4; i ++) {
2970 3347600 out[n2 + n4 + i] = AAC_MUL31( buf[ i + n2] , window[i + n - n4]) +
2971 3347600 AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
2972 3347600 AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
2973 }
2974
2975 // buffer update
2976 26598 memmove(saved + n, saved, 2 * n * sizeof(*saved));
2977 26598 memcpy( saved, buf, n * sizeof(*saved));
2978 26598 }
2979
2980 /**
2981 * channel coupling transformation interface
2982 *
2983 * @param apply_coupling_method pointer to (in)dependent coupling function
2984 */
2985 175822 static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
2986 enum RawDataBlockType type, int elem_id,
2987 enum CouplingPoint coupling_point,
2988 void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2989 {
2990 int i, c;
2991
2992
2/2
✓ Branch 0 taken 2813152 times.
✓ Branch 1 taken 175822 times.
2988974 for (i = 0; i < MAX_ELEM_ID; i++) {
2993 2813152 ChannelElement *cce = ac->che[TYPE_CCE][i];
2994 2813152 int index = 0;
2995
2996
4/4
✓ Branch 0 taken 11740 times.
✓ Branch 1 taken 2801412 times.
✓ Branch 2 taken 3915 times.
✓ Branch 3 taken 7825 times.
2813152 if (cce && cce->coup.coupling_point == coupling_point) {
2997 3915 ChannelCoupling *coup = &cce->coup;
2998
2999
2/2
✓ Branch 0 taken 9402 times.
✓ Branch 1 taken 3915 times.
13317 for (c = 0; c <= coup->num_coupled; c++) {
3000
4/4
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 4313 times.
✓ Branch 2 taken 2741 times.
✓ Branch 3 taken 2348 times.
9402 if (coup->type[c] == type && coup->id_select[c] == elem_id) {
3001
2/2
✓ Branch 0 taken 2561 times.
✓ Branch 1 taken 180 times.
2741 if (coup->ch_select[c] != 1) {
3002 2561 apply_coupling_method(ac, &cc->ch[0], cce, index);
3003
1/2
✓ Branch 0 taken 2561 times.
✗ Branch 1 not taken.
2561 if (coup->ch_select[c] != 0)
3004 2561 index++;
3005 }
3006
2/2
✓ Branch 0 taken 2147 times.
✓ Branch 1 taken 594 times.
2741 if (coup->ch_select[c] != 2)
3007 2147 apply_coupling_method(ac, &cc->ch[1], cce, index++);
3008 } else
3009
2/2
✓ Branch 0 taken 4720 times.
✓ Branch 1 taken 1941 times.
6661 index += 1 + (coup->ch_select[c] == 3);
3010 }
3011 }
3012 }
3013 175822 }
3014
3015 /**
3016 * Convert spectral data to samples, applying all supported tools as appropriate.
3017 */
3018 49848 static void spectral_to_sample(AACContext *ac, int samples)
3019 {
3020 int i, type;
3021 void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
3022
3/3
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 16365 times.
✓ Branch 2 taken 32877 times.
49848 switch (ac->oc[1].m4ac.object_type) {
3023 606 case AOT_ER_AAC_LD:
3024 606 imdct_and_window = imdct_and_windowing_ld;
3025 606 break;
3026 16365 case AOT_ER_AAC_ELD:
3027 16365 imdct_and_window = imdct_and_windowing_eld;
3028 16365 break;
3029 32877 default:
3030
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 32490 times.
32877 if (ac->oc[1].m4ac.frame_length_short)
3031 387 imdct_and_window = imdct_and_windowing_960;
3032 else
3033 32490 imdct_and_window = ac->imdct_and_windowing;
3034 }
3035
2/2
✓ Branch 0 taken 199392 times.
✓ Branch 1 taken 49848 times.
249240 for (type = 3; type >= 0; type--) {
3036
2/2
✓ Branch 0 taken 3190272 times.
✓ Branch 1 taken 199392 times.
3389664 for (i = 0; i < MAX_ELEM_ID; i++) {
3037 3190272 ChannelElement *che = ac->che[type][i];
3038
4/4
✓ Branch 0 taken 63223 times.
✓ Branch 1 taken 3127049 times.
✓ Branch 2 taken 62894 times.
✓ Branch 3 taken 329 times.
3190272 if (che && che->present) {
3039
2/2
✓ Branch 0 taken 58216 times.
✓ Branch 1 taken 4678 times.
62894 if (type <= TYPE_CPE)
3040 58216 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
3041
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 61110 times.
62894 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
3042
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 1246 times.
1784 if (che->ch[0].ics.predictor_present) {
3043
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 156 times.
538 if (che->ch[0].ics.ltp.present)
3044 382 ac->apply_ltp(ac, &che->ch[0]);
3045
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
538 if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
3046 404 ac->apply_ltp(ac, &che->ch[1]);
3047 }
3048 }
3049
2/2
✓ Branch 0 taken 4633 times.
✓ Branch 1 taken 58261 times.
62894 if (che->ch[0].tns.present)
3050 4633 ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
3051
2/2
✓ Branch 0 taken 2294 times.
✓ Branch 1 taken 60600 times.
62894 if (che->ch[1].tns.present)
3052 2294 ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
3053
2/2
✓ Branch 0 taken 58216 times.
✓ Branch 1 taken 4678 times.
62894 if (type <= TYPE_CPE)
3054 58216 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
3055
4/4
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 61720 times.
✓ Branch 2 taken 393 times.
✓ Branch 3 taken 781 times.
62894 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
3056 62113 imdct_and_window(ac, &che->ch[0]);
3057
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 60329 times.
62113 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
3058 1784 ac->update_ltp(ac, &che->ch[0]);
3059
2/2
✓ Branch 0 taken 30682 times.
✓ Branch 1 taken 31431 times.
62113 if (type == TYPE_CPE) {
3060 30682 imdct_and_window(ac, &che->ch[1]);
3061
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 28898 times.
30682 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
3062 1784 ac->update_ltp(ac, &che->ch[1]);
3063 }
3064
2/2
✓ Branch 0 taken 12411 times.
✓ Branch 1 taken 49702 times.
62113 if (ac->oc[1].m4ac.sbr > 0) {
3065 12411 AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
3066 }
3067 }
3068
2/2
✓ Branch 0 taken 59390 times.
✓ Branch 1 taken 3504 times.
62894 if (type <= TYPE_CCE)
3069 59390 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
3070
3071 #if USE_FIXED
3072 {
3073 int j;
3074 /* preparation for resampler */
3075
2/2
✓ Branch 0 taken 19726848 times.
✓ Branch 1 taken 19299 times.
19746147 for(j = 0; j<samples; j++){
3076 19726848 che->ch[0].ret[j] = (int32_t)av_clip64((int64_t)che->ch[0].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
3077
2/2
✓ Branch 0 taken 9935872 times.
✓ Branch 1 taken 9790976 times.
19726848 if(type == TYPE_CPE)
3078 9935872 che->ch[1].ret[j] = (int32_t)av_clip64((int64_t)che->ch[1].ret[j]*128, INT32_MIN, INT32_MAX-0x8000)+0x8000;
3079 }
3080 }
3081 #endif /* USE_FIXED */
3082 62894 che->present = 0;
3083
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 3127049 times.
3127378 } else if (che) {
3084 329 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
3085 }
3086 }
3087 }
3088 49848 }
3089
3090 4112 static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
3091 {
3092 int size;
3093 AACADTSHeaderInfo hdr_info;
3094 uint8_t layout_map[MAX_ELEM_ID*4][3];
3095 int layout_map_tags, ret;
3096
3097 4112 size = ff_adts_header_parse(gb, &hdr_info);
3098
1/2
✓ Branch 0 taken 4112 times.
✗ Branch 1 not taken.
4112 if (size > 0) {
3099
2/4
✓ Branch 0 taken 4112 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4112 times.
4112 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
3100 // This is 2 for "VLB " audio in NSV files.
3101 // See samples/nsv/vlb_audio.
3102 avpriv_report_missing_feature(ac->avctx,
3103 "More than one AAC RDB per ADTS frame");
3104 ac->warned_num_aac_frames = 1;
3105 }
3106 4112 push_output_configuration(ac);
3107
1/2
✓ Branch 0 taken 4112 times.
✗ Branch 1 not taken.
4112 if (hdr_info.chan_config) {
3108 4112 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
3109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4112 times.
4112 if ((ret = set_default_channel_config(ac, ac->avctx,
3110 layout_map,
3111 &layout_map_tags,
3112 4112 hdr_info.chan_config)) < 0)
3113 return ret;
3114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4112 times.
4112 if ((ret = output_configure(ac, layout_map, layout_map_tags,
3115 4112 FFMAX(ac->oc[1].status,
3116 OC_TRIAL_FRAME), 0)) < 0)
3117 return ret;
3118 } else {
3119 ac->oc[1].m4ac.chan_config = 0;
3120 /**
3121 * dual mono frames in Japanese DTV can have chan_config 0
3122 * WITHOUT specifying PCE.
3123 * thus, set dual mono as default.
3124 */
3125 if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
3126 layout_map_tags = 2;
3127 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
3128 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
3129 layout_map[0][1] = 0;
3130 layout_map[1][1] = 1;
3131 if (output_configure(ac, layout_map, layout_map_tags,
3132 OC_TRIAL_FRAME, 0))
3133 return -7;
3134 }
3135 }
3136 4112 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
3137 4112 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
3138 4112 ac->oc[1].m4ac.object_type = hdr_info.object_type;
3139 4112 ac->oc[1].m4ac.frame_length_short = 0;
3140
2/2
✓ Branch 0 taken 4058 times.
✓ Branch 1 taken 54 times.
4112 if (ac->oc[0].status != OC_LOCKED ||
3141
1/2
✓ Branch 0 taken 4058 times.
✗ Branch 1 not taken.
4058 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
3142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4058 times.
4058 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
3143 54 ac->oc[1].m4ac.sbr = -1;
3144 54 ac->oc[1].m4ac.ps = -1;
3145 }
3146
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4111 times.
4112 if (!hdr_info.crc_absent)
3147 1 skip_bits(gb, 16);
3148 }
3149 4112 return size;
3150 }
3151
3152 16971 static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
3153 int *got_frame_ptr, GetBitContext *gb)
3154 {
3155 16971 AACContext *ac = avctx->priv_data;
3156 16971 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
3157 ChannelElement *che;
3158 int err, i;
3159
2/2
✓ Branch 0 taken 3559 times.
✓ Branch 1 taken 13412 times.
16971 int samples = m4ac->frame_length_short ? 960 : 1024;
3160 16971 int chan_config = m4ac->chan_config;
3161 16971 int aot = m4ac->object_type;
3162
3163
3/4
✓ Branch 0 taken 16365 times.
✓ Branch 1 taken 606 times.
✓ Branch 2 taken 16365 times.
✗ Branch 3 not taken.
16971 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
3164 16971 samples >>= 1;
3165
3166 16971 ac->frame = data;
3167
3168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16971 times.
16971 if ((err = frame_configure_elements(avctx)) < 0)
3169 return err;
3170
3171 // The FF_PROFILE_AAC_* defines are all object_type - 1
3172 // This may lead to an undefined profile being signaled
3173 16971 ac->avctx->profile = aot - 1;
3174
3175 16971 ac->tags_mapped = 0;
3176
3177
3/8
✓ Branch 0 taken 16971 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16971 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 16971 times.
16971 if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
3178 avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
3179 chan_config);
3180 return AVERROR_INVALIDDATA;
3181 }
3182
2/2
✓ Branch 0 taken 18789 times.
✓ Branch 1 taken 16971 times.
35760 for (i = 0; i < tags_per_config[chan_config]; i++) {
3183 18789 const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
3184 18789 const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
3185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18789 times.
18789 if (!(che=get_che(ac, elem_type, elem_id))) {
3186 av_log(ac->avctx, AV_LOG_ERROR,
3187 "channel element %d.%d is not allocated\n",
3188 elem_type, elem_id);
3189 return AVERROR_INVALIDDATA;
3190 }
3191 18789 che->present = 1;
3192
2/2
✓ Branch 0 taken 2424 times.
✓ Branch 1 taken 16365 times.
18789 if (aot != AOT_ER_AAC_ELD)
3193 2424 skip_bits(gb, 4);
3194
3/4
✓ Branch 0 taken 6738 times.
✓ Branch 1 taken 11445 times.
✓ Branch 2 taken 606 times.
✗ Branch 3 not taken.
18789 switch (elem_type) {
3195 6738 case TYPE_SCE:
3196 6738 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3197 6738 break;
3198 11445 case TYPE_CPE:
3199 11445 err = decode_cpe(ac, gb, che);
3200 11445 break;
3201 606 case TYPE_LFE:
3202 606 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3203 606 break;
3204 }
3205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18789 times.
18789 if (err < 0)
3206 return err;
3207 }
3208
3209 16971 spectral_to_sample(ac, samples);
3210
3211
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16971 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16971 if (!ac->frame->data[0] && samples) {
3212 av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
3213 return AVERROR_INVALIDDATA;
3214 }
3215
3216 16971 ac->frame->nb_samples = samples;
3217 16971 ac->frame->sample_rate = avctx->sample_rate;
3218 16971 *got_frame_ptr = 1;
3219
3220 16971 skip_bits_long(gb, get_bits_left(gb));
3221 16971 return 0;
3222 }
3223
3224 32877 static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
3225 int *got_frame_ptr, GetBitContext *gb,
3226 const AVPacket *avpkt)
3227 {
3228 32877 AACContext *ac = avctx->priv_data;
3229 32877 ChannelElement *che = NULL, *che_prev = NULL;
3230 32877 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
3231 int err, elem_id;
3232 32877 int samples = 0, multiplier, audio_found = 0, pce_found = 0;
3233 32877 int is_dmono, sce_count = 0;
3234 int payload_alignment;
3235 32877 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
3236
3237 32877 ac->frame = data;
3238
3239
2/2
✓ Branch 1 taken 4112 times.
✓ Branch 2 taken 28765 times.
32877 if (show_bits(gb, 12) == 0xfff) {
3240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4112 times.
4112 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
3241 av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
3242 goto fail;
3243 }
3244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4112 times.
4112 if (ac->oc[1].m4ac.sampling_index > 12) {
3245 av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
3246 err = AVERROR_INVALIDDATA;
3247 goto fail;
3248 }
3249 }
3250
3251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32877 times.
32877 if ((err = frame_configure_elements(avctx)) < 0)
3252 goto fail;
3253
3254 // The FF_PROFILE_AAC_* defines are all object_type - 1
3255 // This may lead to an undefined profile being signaled
3256 32877 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
3257
3258 32877 payload_alignment = get_bits_count(gb);
3259 32877 ac->tags_mapped = 0;
3260 // parse
3261
2/2
✓ Branch 1 taken 72127 times.
✓ Branch 2 taken 32877 times.
105004 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
3262 72127 elem_id = get_bits(gb, 4);
3263
3264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72127 times.
72127 if (avctx->debug & FF_DEBUG_STARTCODE)
3265 av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
3266
3267
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 72127 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
72127 if (!avctx->channels && elem_type != TYPE_PCE) {
3268 err = AVERROR_INVALIDDATA;
3269 goto fail;
3270 }
3271
3272
2/2
✓ Branch 0 taken 44105 times.
✓ Branch 1 taken 28022 times.
72127 if (elem_type < TYPE_DSE) {
3273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44105 times.
44105 if (che_presence[elem_type][elem_id]) {
3274 int error = che_presence[elem_type][elem_id] > 1;
3275 av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
3276 elem_type, elem_id);
3277 if (error) {
3278 err = AVERROR_INVALIDDATA;
3279 goto fail;
3280 }
3281 }
3282 44105 che_presence[elem_type][elem_id]++;
3283
3284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 44105 times.
44105 if (!(che=get_che(ac, elem_type, elem_id))) {
3285 av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
3286 elem_type, elem_id);
3287 err = AVERROR_INVALIDDATA;
3288 goto fail;
3289 }
3290
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 43718 times.
44105 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
3291 44105 che->present = 1;
3292 }
3293
3294
6/8
✓ Branch 0 taken 20796 times.
✓ Branch 1 taken 19237 times.
✓ Branch 2 taken 1174 times.
✓ Branch 3 taken 2898 times.
✓ Branch 4 taken 2821 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25201 times.
✗ Branch 7 not taken.
72127 switch (elem_type) {
3295
3296 20796 case TYPE_SCE:
3297 20796 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3298 20796 audio_found = 1;
3299 20796 sce_count++;
3300 20796 break;
3301
3302 19237 case TYPE_CPE:
3303 19237 err = decode_cpe(ac, gb, che);
3304 19237 audio_found = 1;
3305 19237 break;
3306
3307 1174 case TYPE_CCE:
3308 1174 err = decode_cce(ac, gb, che);
3309 1174 break;
3310
3311 2898 case TYPE_LFE:
3312 2898 err = decode_ics(ac, &che->ch[0], gb, 0, 0);
3313 2898 audio_found = 1;
3314 2898 break;
3315
3316 2821 case TYPE_DSE:
3317 2821 err = skip_data_stream_element(ac, gb);
3318 2821 break;
3319
3320 case TYPE_PCE: {
3321 uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
3322 int tags;
3323
3324 int pushed = push_output_configuration(ac);
3325