FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec.c
Date: 2026-10-15 22:25:26
Exec Total Coverage
Lines: 1074 1400 76.7%
Functions: 45 47 95.7%
Branches: 695 1045 66.5%

Line Branch Exec Source
1 /*
2 * Common parts of the AAC decoders
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 /* We use several quantization functions here (Q31, Q30),
33 * for which we need this to be defined for them to work as expected. */
34 #define USE_FIXED 1
35
36 #include "config_components.h"
37
38 #include <limits.h>
39 #include <stddef.h>
40
41 #include "aacdec.h"
42 #include "aacdec_tab.h"
43 #include "aacdec_usac.h"
44
45 #include "libavcodec/aac.h"
46 #include "libavcodec/aac_defines.h"
47 #include "libavcodec/aacsbr.h"
48 #include "libavcodec/aactab.h"
49 #include "libavcodec/adts_header.h"
50
51 #include "libavcodec/avcodec.h"
52 #include "libavcodec/internal.h"
53 #include "libavcodec/codec_internal.h"
54 #include "libavcodec/decode.h"
55 #include "libavcodec/profiles.h"
56
57 #include "libavutil/attributes.h"
58 #include "libavutil/error.h"
59 #include "libavutil/log.h"
60 #include "libavutil/macros.h"
61 #include "libavutil/mem.h"
62 #include "libavutil/opt.h"
63 #include "libavutil/tx.h"
64 #include "libavutil/version.h"
65 #include "libavutil/refstruct.h"
66
67 /*
68 * supported tools
69 *
70 * Support? Name
71 * N (code in SoC repo) gain control
72 * Y block switching
73 * Y window shapes - standard
74 * N window shapes - Low Delay
75 * Y filterbank - standard
76 * N (code in SoC repo) filterbank - Scalable Sample Rate
77 * Y Temporal Noise Shaping
78 * Y Long Term Prediction
79 * Y intensity stereo
80 * Y channel coupling
81 * Y frequency domain prediction
82 * Y Perceptual Noise Substitution
83 * Y Mid/Side stereo
84 * N Scalable Inverse AAC Quantization
85 * N Frequency Selective Switch
86 * N upsampling filter
87 * Y quantization & coding - AAC
88 * N quantization & coding - TwinVQ
89 * N quantization & coding - BSAC
90 * N AAC Error Resilience tools
91 * N Error Resilience payload syntax
92 * N Error Protection tool
93 * N CELP
94 * N Silence Compression
95 * N HVXC
96 * N HVXC 4kbits/s VR
97 * N Structured Audio tools
98 * N Structured Audio Sample Bank Format
99 * N MIDI
100 * N Harmonic and Individual Lines plus Noise
101 * N Text-To-Speech Interface
102 * Y Spectral Band Replication
103 * Y (not in this code) Layer-1
104 * Y (not in this code) Layer-2
105 * Y (not in this code) Layer-3
106 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
107 * Y Parametric Stereo
108 * N Direct Stream Transfer
109 * Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
110 *
111 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
112 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
113 Parametric Stereo.
114 */
115
116 #define overread_err "Input buffer exhausted before END element found\n"
117
118 633 static int count_channels(uint8_t (*layout)[3], int tags)
119 {
120 633 int i, sum = 0;
121
2/2
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 633 times.
1864 for (i = 0; i < tags; i++) {
122 1231 int syn_ele = layout[i][0];
123 1231 int pos = layout[i][2];
124
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 511 times.
2462 sum += (1 + (syn_ele == TYPE_CPE)) *
125
3/4
✓ Branch 0 taken 1231 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1223 times.
✓ Branch 3 taken 8 times.
1231 (pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
126 }
127 633 return sum;
128 }
129
130 /**
131 * Check for the channel element in the current channel position configuration.
132 * If it exists, make sure the appropriate element is allocated and map the
133 * channel order to match the internal FFmpeg channel layout.
134 *
135 * @param che_pos current channel position configuration
136 * @param type channel element type
137 * @param id channel element id
138 * @param channels count of the number of channels in the configuration
139 *
140 * @return Returns error status. 0 - OK, !0 - error
141 */
142 11090 static av_cold int che_configure(AACDecContext *ac,
143 enum ChannelPosition che_pos,
144 int type, int id, int *channels)
145 {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11090 times.
11090 if (*channels >= MAX_CHANNELS)
147 ✗ return AVERROR_INVALIDDATA;
148
1/2
✓ Branch 0 taken 11090 times.
✗ Branch 1 not taken.
11090 if (che_pos) {
149
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 10494 times.
11090 if (!ac->che[type][id]) {
150 596 int ret = ac->proc.sbr_ctx_alloc_init(ac, &ac->che[type][id], type);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
596 if (ret < 0)
152 ✗ return ret;
153 }
154
2/2
✓ Branch 0 taken 11082 times.
✓ Branch 1 taken 8 times.
11090 if (type != TYPE_CCE) {
155
7/8
✓ Branch 0 taken 3074 times.
✓ Branch 1 taken 8008 times.
✓ Branch 2 taken 3040 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 368 times.
✓ Branch 5 taken 2672 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 11082 times.
11082 if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
156 ✗ av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
157 ✗ return AVERROR_INVALIDDATA;
158 }
159 11082 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
160
4/4
✓ Branch 0 taken 3074 times.
✓ Branch 1 taken 8008 times.
✓ Branch 2 taken 3040 times.
✓ Branch 3 taken 34 times.
11082 if (type == TYPE_CPE ||
161
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 2672 times.
3040 (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
162 8376 ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
163 }
164 }
165 } else {
166 ✗ if (ac->che[type][id]) {
167 ✗ for (int i = 0; i < FF_ARRAY_ELEMS(ac->tag_che_map); i++) {
168 ✗ for (int j = 0; j < MAX_ELEM_ID; j++) {
169 ✗ if (ac->tag_che_map[i][j] == ac->che[type][id])
170 ✗ ac->tag_che_map[i][j] = NULL;
171 }
172 }
173 ✗ ac->proc.sbr_ctx_close(ac->che[type][id]);
174 }
175 ✗ av_freep(&ac->che[type][id]);
176 ✗ memset(ac->output_element, 0, sizeof(ac->output_element));
177 }
178 11090 return 0;
179 }
180
181 59981 static int frame_configure_elements(AVCodecContext *avctx)
182 {
183 59981 AACDecContext *ac = avctx->priv_data;
184 int type, id, ch, ret;
185
186 /* set channel pointers to internal buffers by default */
187
2/2
✓ Branch 0 taken 239924 times.
✓ Branch 1 taken 59981 times.
299905 for (type = 0; type < 4; type++) {
188
2/2
✓ Branch 0 taken 15355136 times.
✓ Branch 1 taken 239924 times.
15595060 for (id = 0; id < MAX_ELEM_ID; id++) {
189 15355136 ChannelElement *che = ac->che[type][id];
190
2/2
✓ Branch 0 taken 74307 times.
✓ Branch 1 taken 15280829 times.
15355136 if (che) {
191 74307 che->ch[0].output = che->ch[0].ret_buf;
192 74307 che->ch[1].output = che->ch[1].ret_buf;
193 }
194 }
195 }
196
197 /* get output buffer */
198 59981 av_frame_unref(ac->frame);
199
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 59970 times.
59981 if (!avctx->ch_layout.nb_channels)
200 11 return 1;
201
202 59970 ac->frame->nb_samples = 2048;
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 59970 times.
59970 if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
204 ✗ return ret;
205
206 /* map output channel pointers to AVFrame data */
207
2/2
✓ Branch 0 taken 112933 times.
✓ Branch 1 taken 59970 times.
172903 for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
208
1/2
✓ Branch 0 taken 112933 times.
✗ Branch 1 not taken.
112933 if (ac->output_element[ch])
209 112933 ac->output_element[ch]->output = (void *)ac->frame->extended_data[ch];
210 }
211
212 59970 return 0;
213 }
214
215 struct elem_to_channel {
216 uint64_t av_position;
217 uint8_t syn_ele;
218 uint8_t elem_id;
219 uint8_t aac_position;
220 };
221
222 8214 static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
223 uint8_t (*layout_map)[3], int offset, uint64_t left,
224 uint64_t right, int pos, uint64_t *layout)
225 {
226
2/2
✓ Branch 0 taken 7992 times.
✓ Branch 1 taken 222 times.
8214 if (layout_map[offset][0] == TYPE_CPE) {
227 7992 e2c_vec[offset] = (struct elem_to_channel) {
228 7992 .av_position = left | right,
229 .syn_ele = TYPE_CPE,
230 7992 .elem_id = layout_map[offset][1],
231 .aac_position = pos
232 };
233
1/2
✓ Branch 0 taken 7992 times.
✗ Branch 1 not taken.
7992 if (e2c_vec[offset].av_position != UINT64_MAX)
234 7992 *layout |= e2c_vec[offset].av_position;
235
236 7992 return 1;
237 } else {
238 222 e2c_vec[offset] = (struct elem_to_channel) {
239 .av_position = left,
240 .syn_ele = TYPE_SCE,
241 222 .elem_id = layout_map[offset][1],
242 .aac_position = pos
243 };
244 222 e2c_vec[offset + 1] = (struct elem_to_channel) {
245 .av_position = right,
246 .syn_ele = TYPE_SCE,
247 222 .elem_id = layout_map[offset + 1][1],
248 .aac_position = pos
249 };
250
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 if (left != UINT64_MAX)
251 222 *layout |= left;
252
253
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 if (right != UINT64_MAX)
254 222 *layout |= right;
255
256 222 return 2;
257 }
258 }
259
260 42960 static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
261 int current)
262 {
263 42960 int num_pos_channels = 0;
264 42960 int first_cpe = 0;
265 42960 int sce_parity = 0;
266 int i;
267
2/2
✓ Branch 0 taken 11391 times.
✓ Branch 1 taken 42667 times.
54058 for (i = current; i < tags; i++) {
268
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 11098 times.
11391 if (layout_map[i][2] != pos)
269 293 break;
270
2/2
✓ Branch 0 taken 8016 times.
✓ Branch 1 taken 3082 times.
11098 if (layout_map[i][0] == TYPE_CPE) {
271
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 7975 times.
8016 if (sce_parity) {
272
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
273 41 sce_parity = 0;
274 } else {
275 ✗ return -1;
276 }
277 }
278 8016 num_pos_channels += 2;
279 8016 first_cpe = 1;
280 } else {
281 3082 num_pos_channels++;
282 3082 sce_parity ^= (pos != AAC_CHANNEL_LFE);
283 }
284 }
285
4/4
✓ Branch 0 taken 2563 times.
✓ Branch 1 taken 40397 times.
✓ Branch 2 taken 2560 times.
✓ Branch 3 taken 3 times.
42960 if (sce_parity &&
286
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2536 times.
2560 (pos == AAC_CHANNEL_FRONT && first_cpe))
287 24 return -1;
288
289 42936 return num_pos_channels;
290 }
291
292 42960 static int assign_channels(struct elem_to_channel e2c_vec[MAX_ELEM_ID], uint8_t (*layout_map)[3],
293 uint64_t *layout, int tags, int layer, int pos, int *current)
294 {
295 42960 int i = *current, j = 0;
296 42960 int nb_channels = count_paired_channels(layout_map, tags, pos, i);
297
298
3/4
✓ Branch 0 taken 42936 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42936 times.
42960 if (nb_channels < 0 || nb_channels > 5)
299 24 return 0;
300
301
2/2
✓ Branch 0 taken 10740 times.
✓ Branch 1 taken 32196 times.
42936 if (pos == AAC_CHANNEL_LFE) {
302
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10740 times.
10774 while (nb_channels) {
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE)
304 ✗ return -1;
305 34 e2c_vec[i] = (struct elem_to_channel) {
306 34 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][j],
307 34 .syn_ele = layout_map[i][0],
308 34 .elem_id = layout_map[i][1],
309 .aac_position = pos
310 };
311 34 *layout |= e2c_vec[i].av_position;
312 34 i++;
313 34 j++;
314 34 nb_channels--;
315 }
316 10740 *current = i;
317
318 10740 return 0;
319 }
320
321
2/2
✓ Branch 0 taken 2580 times.
✓ Branch 1 taken 32193 times.
34773 while (nb_channels & 1) {
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2580 times.
2580 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_NONE)
323 ✗ return -1;
324
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2577 times.
2580 if (ff_aac_channel_map[layer][pos - 1][0] == AV_CHAN_UNUSED)
325 3 break;
326 2577 e2c_vec[i] = (struct elem_to_channel) {
327 2577 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][0],
328 2577 .syn_ele = layout_map[i][0],
329 2577 .elem_id = layout_map[i][1],
330 .aac_position = pos
331 };
332 2577 *layout |= e2c_vec[i].av_position;
333 2577 i++;
334 2577 nb_channels--;
335 }
336
337
4/4
✓ Branch 0 taken 21456 times.
✓ Branch 1 taken 10740 times.
✓ Branch 2 taken 21438 times.
✓ Branch 3 taken 18 times.
32196 j = (pos != AAC_CHANNEL_SIDE) && nb_channels <= 3 ? 3 : 1;
338
2/2
✓ Branch 0 taken 8214 times.
✓ Branch 1 taken 32196 times.
40410 while (nb_channels >= 2) {
339
1/2
✓ Branch 0 taken 8214 times.
✗ Branch 1 not taken.
8214 if (ff_aac_channel_map[layer][pos - 1][j] == AV_CHAN_NONE ||
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8214 times.
8214 ff_aac_channel_map[layer][pos - 1][j+1] == AV_CHAN_NONE)
341 ✗ return -1;
342 16428 i += assign_pair(e2c_vec, layout_map, i,
343 8214 1ULL << ff_aac_channel_map[layer][pos - 1][j],
344 8214 1ULL << ff_aac_channel_map[layer][pos - 1][j+1],
345 pos, layout);
346 8214 j += 2;
347 8214 nb_channels -= 2;
348 }
349
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 32196 times.
32199 while (nb_channels & 1) {
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ff_aac_channel_map[layer][pos - 1][5] == AV_CHAN_NONE)
351 ✗ return -1;
352 3 e2c_vec[i] = (struct elem_to_channel) {
353 3 .av_position = 1ULL << ff_aac_channel_map[layer][pos - 1][5],
354 3 .syn_ele = layout_map[i][0],
355 3 .elem_id = layout_map[i][1],
356 .aac_position = pos
357 };
358 3 *layout |= e2c_vec[i].av_position;
359 3 i++;
360 3 nb_channels--;
361 }
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32196 times.
32196 if (nb_channels)
363 ✗ return -1;
364
365 32196 *current = i;
366
367 32196 return 0;
368 }
369
370 10696 static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
371 {
372 int i, n, total_non_cc_elements;
373 10696 struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
374 10696 uint64_t layout = 0;
375
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10696 times.
10696 if (FF_ARRAY_ELEMS(e2c_vec) < tags)
377 ✗ return 0;
378
379
4/4
✓ Branch 0 taken 21420 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 10740 times.
✓ Branch 3 taken 10680 times.
21436 for (n = 0, i = 0; n < 3 && i < tags; n++) {
380 10740 int ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_FRONT, &i);
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740 times.
10740 if (ret < 0)
382 ✗ return 0;
383 10740 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_SIDE, &i);
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740 times.
10740 if (ret < 0)
385 ✗ return 0;
386 10740 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_BACK, &i);
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740 times.
10740 if (ret < 0)
388 ✗ return 0;
389 10740 ret = assign_channels(e2c_vec, layout_map, &layout, tags, n, AAC_CHANNEL_LFE, &i);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10740 times.
10740 if (ret < 0)
391 ✗ return 0;
392 }
393
394 10696 total_non_cc_elements = n = i;
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10696 times.
10696 if (layout == AV_CH_LAYOUT_22POINT2) {
397 // For 22.2 reorder the result as needed
398 ✗ FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[0]); // FL & FR first (final), FC third
399 ✗ FFSWAP(struct elem_to_channel, e2c_vec[2], e2c_vec[1]); // FC second (final), FLc & FRc third
400 ✗ FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[2]); // LFE1 third (final), FLc & FRc seventh
401 ✗ FFSWAP(struct elem_to_channel, e2c_vec[4], e2c_vec[3]); // BL & BR fourth (final), SiL & SiR fifth
402 ✗ FFSWAP(struct elem_to_channel, e2c_vec[6], e2c_vec[4]); // FLc & FRc fifth (final), SiL & SiR seventh
403 ✗ FFSWAP(struct elem_to_channel, e2c_vec[7], e2c_vec[6]); // LFE2 seventh (final), SiL & SiR eight (final)
404 ✗ FFSWAP(struct elem_to_channel, e2c_vec[9], e2c_vec[8]); // TpFL & TpFR ninth (final), TFC tenth (final)
405 ✗ FFSWAP(struct elem_to_channel, e2c_vec[11], e2c_vec[10]); // TC eleventh (final), TpSiL & TpSiR twelfth
406 ✗ FFSWAP(struct elem_to_channel, e2c_vec[12], e2c_vec[11]); // TpBL & TpBR twelfth (final), TpSiL & TpSiR thirteenth (final)
407 } else {
408 // For everything else, utilize the AV channel position define as a
409 // stable sort.
410 do {
411 10754 int next_n = 0;
412
2/2
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 10754 times.
11233 for (i = 1; i < n; i++)
413
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 370 times.
479 if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
414 109 FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
415 109 next_n = i;
416 }
417 10754 n = next_n;
418
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 10696 times.
10754 } while (n > 0);
419
420 }
421
422
2/2
✓ Branch 0 taken 11050 times.
✓ Branch 1 taken 10696 times.
21746 for (i = 0; i < total_non_cc_elements; i++) {
423 11050 layout_map[i][0] = e2c_vec[i].syn_ele;
424 11050 layout_map[i][1] = e2c_vec[i].elem_id;
425 11050 layout_map[i][2] = e2c_vec[i].aac_position;
426 }
427
428 10696 return layout;
429 }
430
431 1450 static void copy_oc(OutputConfiguration *dst, OutputConfiguration *src)
432 {
433 int i;
434
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1450 times.
1450 for (i = 0; i < src->usac.nb_elems; i++) {
436 ✗ AACUsacElemConfig *src_e = &src->usac.elems[i];
437 ✗ AACUsacElemConfig *dst_e = &dst->usac.elems[i];
438 /* dst_e->ext.pl_buf is guaranteed to be set to src_e->ext.pl_buf
439 * upon this function's return */
440 ✗ av_refstruct_replace(&dst_e->ext.pl_buf, src_e->ext.pl_buf);
441 }
442
443 /* Unref all additional buffers to close leaks */
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1450 times.
1450 for (; i < dst->usac.nb_elems; i++)
445 ✗ av_refstruct_unref(&dst->usac.elems[i].ext.pl_buf);
446
447 /* Set all other properties */
448 1450 *dst = *src;
449 1450 }
450
451 /**
452 * Save current output configuration if and only if it has been locked.
453 */
454 1450 static int push_output_configuration(AACDecContext *ac)
455 {
456 1450 int pushed = 0;
457
458
3/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1358 times.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
1450 if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
459 1450 copy_oc(&ac->oc[0], &ac->oc[1]);
460 1450 pushed = 1;
461 }
462 1450 ac->oc[1].status = OC_NONE;
463 1450 return pushed;
464 }
465
466 /**
467 * Restore the previous output configuration if and only if the current
468 * configuration is unlocked.
469 */
470 ✗ static void pop_output_configuration(AACDecContext *ac)
471 {
472 ✗ if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
473 ✗ copy_oc(&ac->oc[1], &ac->oc[0]);
474
475 ✗ ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
476 ac->oc[1].status, 0);
477 }
478 ✗ }
479
480 /**
481 * Configure output channel order based on the current program
482 * configuration element.
483 *
484 * @return Returns error status. 0 - OK, !0 - error
485 */
486 10696 int ff_aac_output_configure(AACDecContext *ac,
487 uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
488 enum OCStatus oc_type, int get_new_frame)
489 {
490 10696 AVCodecContext *avctx = ac->avctx;
491 10696 int i, channels = 0, ret;
492 10696 uint64_t layout = 0;
493 10696 uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
494 10696 uint8_t type_counts[TYPE_END] = { 0 };
495
496
3/4
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 10455 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 241 times.
10696 if (get_new_frame && !ac->frame)
497 ✗ return AVERROR_INVALIDDATA;
498
499
2/2
✓ Branch 0 taken 1789 times.
✓ Branch 1 taken 8907 times.
10696 if (ac->oc[1].layout_map != layout_map) {
500 1789 memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
501 1789 ac->oc[1].layout_map_tags = tags;
502 }
503
2/2
✓ Branch 0 taken 11090 times.
✓ Branch 1 taken 10696 times.
21786 for (i = 0; i < tags; i++) {
504 11090 int type = layout_map[i][0];
505 11090 int id = layout_map[i][1];
506 11090 id_map[type][id] = type_counts[type]++;
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11090 times.
11090 if (id_map[type][id] >= MAX_ELEM_ID) {
508 ✗ avpriv_request_sample(ac->avctx, "Too large remapped id");
509 ✗ return AVERROR_PATCHWELCOME;
510 }
511 }
512 // Try to sniff a reasonable channel order, otherwise output the
513 // channels in the order the PCE declared them.
514
1/2
✓ Branch 0 taken 10696 times.
✗ Branch 1 not taken.
10696 if (ac->output_channel_order == CHANNEL_ORDER_DEFAULT)
515 10696 layout = sniff_channel_order(layout_map, tags);
516
2/2
✓ Branch 0 taken 11090 times.
✓ Branch 1 taken 10696 times.
21786 for (i = 0; i < tags; i++) {
517 11090 int type = layout_map[i][0];
518 11090 int id = layout_map[i][1];
519 11090 int iid = id_map[type][id];
520 11090 int position = layout_map[i][2];
521 // Allocate or free elements depending on if they are in the
522 // current program configuration.
523 11090 ret = che_configure(ac, position, type, iid, &channels);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11090 times.
11090 if (ret < 0)
525 ✗ return ret;
526 11090 ac->tag_che_map[type][id] = ac->che[type][iid];
527 }
528
3/4
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 10328 times.
✓ Branch 2 taken 368 times.
✗ Branch 3 not taken.
10696 if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
529
1/2
✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
368 if (layout == AV_CH_FRONT_CENTER) {
530 368 layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
531 } else {
532 ✗ layout = 0;
533 }
534 }
535
536 10696 av_channel_layout_uninit(&ac->oc[1].ch_layout);
537
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 8 times.
10696 if (layout)
538 10688 av_channel_layout_from_mask(&ac->oc[1].ch_layout, layout);
539 else {
540 8 ac->oc[1].ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
541 8 ac->oc[1].ch_layout.nb_channels = channels;
542 }
543
544 10696 av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout);
545 10696 ac->oc[1].status = oc_type;
546
547
2/2
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 10455 times.
10696 if (get_new_frame) {
548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 241 times.
241 if ((ret = frame_configure_elements(ac->avctx)) < 0)
549 ✗ return ret;
550 }
551
552 10696 return 0;
553 }
554
555 3 static av_cold void flush(AVCodecContext *avctx)
556 {
557 3 AACDecContext *ac= avctx->priv_data;
558 int type, i, j;
559
560
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (type = 3; type >= 0; type--) {
561
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 12 times.
780 for (i = 0; i < MAX_ELEM_ID; i++) {
562 768 ChannelElement *che = ac->che[type][i];
563
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 765 times.
768 if (che) {
564
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (j = 0; j <= 1; j++) {
565 6 memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
566 }
567 }
568 }
569 }
570
571 #if CONFIG_AAC_DECODER
572 3 ff_aac_usac_reset_state(ac, &ac->oc[1]);
573 #endif
574 3 }
575
576 /**
577 * Set up channel positions based on a default channel configuration
578 * as specified in table 1.17.
579 *
580 * @return Returns error status. 0 - OK, !0 - error
581 */
582 1831 int ff_aac_set_default_channel_config(AACDecContext *ac, AVCodecContext *avctx,
583 uint8_t (*layout_map)[3],
584 int *tags,
585 int channel_config)
586 {
587
3/8
✓ Branch 0 taken 1831 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1831 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1831 times.
1831 if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
588 channel_config > 14) {
589 ✗ av_log(avctx, AV_LOG_ERROR,
590 "invalid default channel configuration (%d)\n",
591 channel_config);
592 ✗ return AVERROR_INVALIDDATA;
593 }
594 1831 *tags = ff_tags_per_config[channel_config];
595 1831 memcpy(layout_map, ff_aac_channel_layout_map[channel_config - 1],
596 1831 *tags * sizeof(*layout_map));
597
598 /*
599 * AAC specification has 7.1(wide) as a default layout for 8-channel streams.
600 * However, at least Nero AAC encoder encodes 7.1 streams using the default
601 * channel config 7, mapping the side channels of the original audio stream
602 * to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
603 * decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
604 * the incorrect streams as if they were correct (and as the encoder intended).
605 *
606 * As actual intended 7.1(wide) streams are very rare, default to assuming a
607 * 7.1 layout was intended.
608 */
609
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1829 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
1831 if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
610 2 layout_map[2][2] = AAC_CHANNEL_BACK;
611
612
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (!ac || !ac->warned_71_wide++) {
613 2 av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
614 " instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
615 " according to the specification instead.\n", FF_COMPLIANCE_STRICT);
616 }
617 }
618
619 1831 return 0;
620 }
621
622 72897 ChannelElement *ff_aac_get_che(AACDecContext *ac, int type, int elem_id)
623 {
624 /* For PCE based channel configurations map the channels solely based
625 * on tags. */
626
2/2
✓ Branch 0 taken 24233 times.
✓ Branch 1 taken 48664 times.
72897 if (!ac->oc[1].m4ac.chan_config) {
627 24233 return ac->tag_che_map[type][elem_id];
628 }
629 // Allow single CPE stereo files to be signalled with mono configuration.
630
4/4
✓ Branch 0 taken 46204 times.
✓ Branch 1 taken 2460 times.
✓ Branch 2 taken 28326 times.
✓ Branch 3 taken 17878 times.
48664 if (!ac->tags_mapped && type == TYPE_CPE &&
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28326 times.
28326 ac->oc[1].m4ac.chan_config == 1) {
632 uint8_t layout_map[MAX_ELEM_ID*4][3];
633 int layout_map_tags;
634 ✗ push_output_configuration(ac);
635
636 ✗ av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
637
638 ✗ if (ff_aac_set_default_channel_config(ac, ac->avctx, layout_map,
639 &layout_map_tags, 2) < 0)
640 ✗ return NULL;
641 ✗ if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
642 OC_TRIAL_FRAME, 1) < 0)
643 ✗ return NULL;
644
645 ✗ ac->oc[1].m4ac.chan_config = 2;
646 ✗ ac->oc[1].m4ac.ps = 0;
647 }
648 // And vice-versa
649
4/4
✓ Branch 0 taken 46204 times.
✓ Branch 1 taken 2460 times.
✓ Branch 2 taken 17878 times.
✓ Branch 3 taken 28326 times.
48664 if (!ac->tags_mapped && type == TYPE_SCE &&
650
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 17660 times.
17878 ac->oc[1].m4ac.chan_config == 2) {
651 uint8_t layout_map[MAX_ELEM_ID * 4][3];
652 int layout_map_tags;
653 218 push_output_configuration(ac);
654
655 218 av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
656
657 218 layout_map_tags = 2;
658 218 layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
659 218 layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
660 218 layout_map[0][1] = 0;
661 218 layout_map[1][1] = 1;
662
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
663 OC_TRIAL_FRAME, 1) < 0)
664 ✗ return NULL;
665
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 218 times.
218 if (ac->oc[1].m4ac.sbr)
667 ✗ ac->oc[1].m4ac.ps = -1;
668 }
669 /* For indexed channel configurations map the channels solely based
670 * on position. */
671
3/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3280 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 28544 times.
✓ Branch 8 taken 16840 times.
✗ Branch 9 not taken.
48664 switch (ac->oc[1].m4ac.chan_config) {
672 ✗ case 14:
673 ✗ if (ac->tags_mapped > 2 && ((type == TYPE_CPE && elem_id < 3) ||
674 ✗ (type == TYPE_LFE && elem_id < 1))) {
675 ✗ ac->tags_mapped++;
676 ✗ return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
677 }
678 av_fallthrough;
679 case 13:
680 ✗ if (ac->tags_mapped > 3 && ((type == TYPE_CPE && elem_id < 8) ||
681 ✗ (type == TYPE_SCE && elem_id < 6) ||
682 ✗ (type == TYPE_LFE && elem_id < 2))) {
683 ✗ ac->tags_mapped++;
684 ✗ return ac->tag_che_map[type][elem_id] = ac->che[type][elem_id];
685 }
686 av_fallthrough;
687 case 12:
688 case 7:
689 ✗ if (ac->tags_mapped == 3 && type == TYPE_CPE) {
690 ✗ ac->tags_mapped++;
691 ✗ return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
692 }
693 av_fallthrough;
694 case 11:
695 ✗ if (ac->tags_mapped == 3 && type == TYPE_SCE) {
696 ✗ ac->tags_mapped++;
697 ✗ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
698 }
699 av_fallthrough;
700 case 6:
701 /* Some streams incorrectly code 5.1 audio as
702 * SCE[0] CPE[0] CPE[1] SCE[1]
703 * instead of
704 * SCE[0] CPE[0] CPE[1] LFE[0].
705 * If we seem to have encountered such a stream, transfer
706 * the LFE[0] element to the SCE[1]'s mapping */
707
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 == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
708
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)) {
709 ✗ av_log(ac->avctx, AV_LOG_WARNING,
710 "This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
711 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
712 ✗ ac->warned_remapping_once++;
713 }
714 820 ac->tags_mapped++;
715 820 return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
716 }
717 av_fallthrough;
718 case 5:
719
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) {
720 820 ac->tags_mapped++;
721 820 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
722 }
723 av_fallthrough;
724 case 4:
725 /* Some streams incorrectly code 4.0 audio as
726 * SCE[0] CPE[0] LFE[0]
727 * instead of
728 * SCE[0] CPE[0] SCE[1].
729 * If we seem to have encountered such a stream, transfer
730 * the SCE[1] element to the LFE[0]'s mapping */
731
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 == ff_tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
732 ✗ if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
733 ✗ av_log(ac->avctx, AV_LOG_WARNING,
734 "This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
735 type == TYPE_SCE ? "SCE" : "LFE", elem_id);
736 ✗ ac->warned_remapping_once++;
737 }
738 ✗ ac->tags_mapped++;
739 ✗ return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
740 }
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if (ac->tags_mapped == 2 &&
742 ✗ ac->oc[1].m4ac.chan_config == 4 &&
743 type == TYPE_SCE) {
744 ✗ ac->tags_mapped++;
745 ✗ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
746 }
747 av_fallthrough;
748 case 3:
749 case 2:
750
4/4
✓ Branch 0 taken 29364 times.
✓ Branch 1 taken 820 times.
✓ Branch 2 taken 29146 times.
✓ Branch 3 taken 218 times.
30184 if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
751 type == TYPE_CPE) {
752 29146 ac->tags_mapped++;
753 29146 return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
754
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1038 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1038 } else if (ac->tags_mapped == 1 && ac->oc[1].m4ac.chan_config == 2 &&
755 type == TYPE_SCE) {
756 ✗ ac->tags_mapped++;
757 ✗ return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
758 }
759 av_fallthrough;
760 case 1:
761
2/4
✓ Branch 0 taken 17878 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17878 times.
✗ Branch 3 not taken.
17878 if (!ac->tags_mapped && type == TYPE_SCE) {
762 17878 ac->tags_mapped++;
763 17878 return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
764 }
765 av_fallthrough;
766 default:
767 ✗ return NULL;
768 }
769 }
770
771 /**
772 * Decode an array of 4 bit element IDs, optionally interleaved with a
773 * stereo/mono switching bit.
774 *
775 * @param type speaker type/position for these channels
776 */
777 325 static void decode_channel_map(uint8_t layout_map[][3],
778 enum ChannelPosition type,
779 GetBitContext *gb, int n)
780 {
781
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 325 times.
537 while (n--) {
782 enum RawDataBlockType syn_ele;
783
3/4
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
212 switch (type) {
784 179 case AAC_CHANNEL_FRONT:
785 case AAC_CHANNEL_BACK:
786 case AAC_CHANNEL_SIDE:
787 179 syn_ele = get_bits1(gb);
788 179 break;
789 8 case AAC_CHANNEL_CC:
790 8 skip_bits1(gb);
791 8 syn_ele = TYPE_CCE;
792 8 break;
793 25 case AAC_CHANNEL_LFE:
794 25 syn_ele = TYPE_LFE;
795 25 break;
796 ✗ default:
797 // AAC_CHANNEL_OFF has no channel map
798 ✗ av_assert0(0);
799 }
800 212 layout_map[0][0] = syn_ele;
801 212 layout_map[0][1] = get_bits(gb, 4);
802 212 layout_map[0][2] = type;
803 212 layout_map++;
804 }
805 325 }
806
807 65 static inline void relative_align_get_bits(GetBitContext *gb,
808 int reference_position) {
809 65 int n = (reference_position - get_bits_count(gb) & 7);
810
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6 times.
65 if (n)
811 59 skip_bits(gb, n);
812 65 }
813
814 /**
815 * Decode program configuration element; reference: table 4.2.
816 *
817 * @return Returns error status. 0 - OK, !0 - error
818 */
819 65 static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
820 uint8_t (*layout_map)[3],
821 GetBitContext *gb, int byte_align_ref)
822 {
823 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
824 int sampling_index;
825 int comment_len;
826 int tags;
827
828 65 skip_bits(gb, 2); // object_type
829
830 65 sampling_index = get_bits(gb, 4);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (m4ac->sampling_index != sampling_index)
832 ✗ av_log(avctx, AV_LOG_WARNING,
833 "Sample rate index (%d) in program config element does not "
834 "match the sample rate index (%d) configured by the container.\n", sampling_index, m4ac->sampling_index);
835
836 65 num_front = get_bits(gb, 4);
837 65 num_side = get_bits(gb, 4);
838 65 num_back = get_bits(gb, 4);
839 65 num_lfe = get_bits(gb, 2);
840 65 num_assoc_data = get_bits(gb, 3);
841 65 num_cc = get_bits(gb, 4);
842
843
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (get_bits1(gb))
844 ✗ skip_bits(gb, 4); // mono_mixdown_tag
845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (get_bits1(gb))
846 ✗ skip_bits(gb, 4); // stereo_mixdown_tag
847
848
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (get_bits1(gb))
849 ✗ skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
850
851
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (get_bits_left(gb) < 5 * (num_front + num_side + num_back + num_cc) + 4 *(num_lfe + num_assoc_data + num_cc)) {
852 ✗ av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
853 ✗ return -1;
854 }
855 65 decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
856 65 tags = num_front;
857 65 decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
858 65 tags += num_side;
859 65 decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
860 65 tags += num_back;
861 65 decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
862 65 tags += num_lfe;
863
864 65 skip_bits_long(gb, 4 * num_assoc_data);
865
866 65 decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
867 65 tags += num_cc;
868
869 65 relative_align_get_bits(gb, byte_align_ref);
870
871 /* comment field, first byte is length */
872 65 comment_len = get_bits(gb, 8) * 8;
873
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (get_bits_left(gb) < comment_len) {
874 ✗ av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
875 ✗ return AVERROR_INVALIDDATA;
876 }
877
878 // Height extension
879 65 int height_ext = 0;
880
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 39 times.
65 if (comment_len >= 16 + (num_front * 2 + num_side * 2 + num_back * 2))
881 26 height_ext = show_bits(gb, 8) == 0xAC;
882
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 53 times.
65 if (height_ext) {
883 uint8_t height_map[4 /* ChannelPosition */][16 /* Channel */]; // 0 == base, 1 == top, 2 == bottom.
884 uint8_t tag[6 /* ChannelPosition */ ][16 /* Channel */][3];
885 12 int i, invalid = 0, height_tags = 0;
886
887 12 skip_bits(gb, 8);
888
889 // Read height extension bits to height_map, which define which layer each element belongs to.
890 // Also make a copy of layout_map that will then be used to rearrange it.
891
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 12 times.
52 for (i = 0; i < num_front; i++) {
892 40 int height = get_bits(gb, 2);
893 40 invalid |= height > 2;
894 40 height_map [AAC_CHANNEL_FRONT][i] = height;
895 40 memcpy(&tag[AAC_CHANNEL_FRONT][i], layout_map + height_tags, sizeof(*layout_map));
896 40 height_tags++;
897 }
898
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 for (i = 0; i < num_side; i++) {
899 3 int height = get_bits(gb, 2);
900 3 invalid |= height > 2;
901 3 height_map [AAC_CHANNEL_SIDE][i] = height;
902 3 memcpy(&tag[AAC_CHANNEL_SIDE][i], layout_map + height_tags, sizeof(*layout_map));
903 3 height_tags++;
904 }
905
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12 times.
37 for (i = 0; i < num_back; i++) {
906 25 int height = get_bits(gb, 2);
907 25 invalid |= height > 2;
908 25 height_map [AAC_CHANNEL_BACK][i] = height;
909 25 memcpy(&tag[AAC_CHANNEL_BACK][i], layout_map + height_tags, sizeof(*layout_map));
910 25 height_tags++;
911 }
912
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 12 times.
25 for (i = 0; i < num_lfe; i++) {
913 13 memcpy(&tag[AAC_CHANNEL_LFE][i], layout_map + height_tags, sizeof(*layout_map));
914 13 height_tags++;
915 }
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 for (i = 0; i < num_cc; i++) {
917 ✗ memcpy(&tag[AAC_CHANNEL_CC][i], layout_map + height_tags, sizeof(*layout_map));
918 ✗ height_tags++;
919 }
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 av_assert0(height_tags == tags);
921
922
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!invalid) {
923 12 height_tags = 0;
924 // For each height layer, check that an element belongs to it and copy it back to layout_map.
925 // We need to take into account LFE and CC elements that may be present.
926
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
48 for (i = 0; i < 3; i++) {
927
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 36 times.
156 for (int j = 0; j < num_front; j++) {
928
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 80 times.
120 if (height_map[AAC_CHANNEL_FRONT][j] == i) {
929 40 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_FRONT][j], sizeof(*layout_map));
930 40 height_tags++;
931 }
932 }
933
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 36 times.
45 for (int j = 0; j < num_side; j++) {
934
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (height_map[AAC_CHANNEL_SIDE][j] == i) {
935 3 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_SIDE][j], sizeof(*layout_map));
936 3 height_tags++;
937 }
938 }
939
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 36 times.
111 for (int j = 0; j < num_back; j++) {
940
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 50 times.
75 if (height_map[AAC_CHANNEL_BACK][j] == i) {
941 25 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_BACK][j], sizeof(*layout_map));
942 25 height_tags++;
943 }
944 }
945
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
36 if (i == 0) { // Base height, copy LFE and CC elements before moving to Top and Bottom
946
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 12 times.
25 for (int j = 0; j < num_lfe; j++) {
947 13 memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_LFE][j], sizeof(*layout_map));
948 13 height_tags++;
949 }
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 for (int j = 0; j < num_cc; j++) {
951 ✗ memcpy(layout_map + height_tags, &tag[AAC_CHANNEL_CC][j], sizeof(*layout_map));
952 ✗ height_tags++;
953 }
954 }
955 }
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 av_assert0(height_tags == tags);
957 }
958
959 12 comment_len -= 8 + (num_front * 2 + num_side * 2 + num_back * 2);
960 }
961
962 65 skip_bits_long(gb, comment_len);
963 65 return tags;
964 }
965
966 /**
967 * Decode GA "General Audio" specific configuration; reference: table 4.1.
968 *
969 * @param ac pointer to AACDecContext, may be null
970 * @param avctx pointer to AVCCodecContext, used for logging
971 *
972 * @return Returns error status. 0 - OK, !0 - error
973 */
974 633 static int decode_ga_specific_config(AACDecContext *ac, AVCodecContext *avctx,
975 GetBitContext *gb,
976 int get_bit_alignment,
977 MPEG4AudioConfig *m4ac,
978 int channel_config)
979 {
980 int extension_flag, ret, ep_config, res_flags;
981 uint8_t layout_map[MAX_ELEM_ID*4][3];
982 633 int tags = 0;
983
984 633 m4ac->frame_length_short = get_bits1(gb);
985
986
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 633 times.
633 if (get_bits1(gb)) // dependsOnCoreCoder
987 ✗ skip_bits(gb, 14); // coreCoderDelay
988 633 extension_flag = get_bits1(gb);
989
990
1/2
✓ Branch 0 taken 633 times.
✗ Branch 1 not taken.
633 if (m4ac->object_type == AOT_AAC_SCALABLE ||
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633 times.
633 m4ac->object_type == AOT_ER_AAC_SCALABLE)
992 ✗ skip_bits(gb, 3); // layerNr
993
994
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 579 times.
633 if (channel_config == 0) {
995 54 skip_bits(gb, 4); // element_instance_tag
996 54 tags = decode_pce(avctx, m4ac, layout_map, gb, get_bit_alignment);
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (tags < 0)
998 ✗ return tags;
999 } else {
1000
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 579 times.
579 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1001 &tags, channel_config)))
1002 ✗ return ret;
1003 }
1004
1005
2/2
✓ Branch 1 taken 530 times.
✓ Branch 2 taken 103 times.
633 if (count_channels(layout_map, tags) > 1) {
1006 530 m4ac->ps = 0;
1007
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
103 } else if (m4ac->sbr == 1 && m4ac->ps == -1)
1008 18 m4ac->ps = 1;
1009
1010
3/4
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 325 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 308 times.
633 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1011 ✗ return ret;
1012
1013
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 629 times.
633 if (extension_flag) {
1014
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 switch (m4ac->object_type) {
1015 ✗ case AOT_ER_BSAC:
1016 ✗ skip_bits(gb, 5); // numOfSubFrame
1017 ✗ skip_bits(gb, 11); // layer_length
1018 ✗ break;
1019 4 case AOT_ER_AAC_LC:
1020 case AOT_ER_AAC_LTP:
1021 case AOT_ER_AAC_SCALABLE:
1022 case AOT_ER_AAC_LD:
1023 4 res_flags = get_bits(gb, 3);
1024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (res_flags) {
1025 ✗ avpriv_report_missing_feature(avctx,
1026 "AAC data resilience (flags %x)",
1027 res_flags);
1028 ✗ return AVERROR_PATCHWELCOME;
1029 }
1030 4 break;
1031 }
1032 4 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
1033 }
1034
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 629 times.
633 switch (m4ac->object_type) {
1035 4 case AOT_ER_AAC_LC:
1036 case AOT_ER_AAC_LTP:
1037 case AOT_ER_AAC_SCALABLE:
1038 case AOT_ER_AAC_LD:
1039 4 ep_config = get_bits(gb, 2);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ep_config) {
1041 ✗ avpriv_report_missing_feature(avctx,
1042 "epConfig %d", ep_config);
1043 ✗ return AVERROR_PATCHWELCOME;
1044 }
1045 }
1046 633 return 0;
1047 }
1048
1049 10 static int decode_eld_specific_config(AACDecContext *ac, AVCodecContext *avctx,
1050 GetBitContext *gb,
1051 MPEG4AudioConfig *m4ac,
1052 int channel_config)
1053 {
1054 int ret, ep_config, res_flags;
1055 uint8_t layout_map[MAX_ELEM_ID*4][3];
1056 10 int tags = 0;
1057 10 const int ELDEXT_TERM = 0;
1058
1059 10 m4ac->ps = 0;
1060 10 m4ac->sbr = 0;
1061 10 m4ac->frame_length_short = get_bits1(gb);
1062
1063 10 res_flags = get_bits(gb, 3);
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (res_flags) {
1065 ✗ avpriv_report_missing_feature(avctx,
1066 "AAC data resilience (flags %x)",
1067 res_flags);
1068 ✗ return AVERROR_PATCHWELCOME;
1069 }
1070
1071
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (get_bits1(gb)) { // ldSbrPresentFlag
1072 ✗ avpriv_report_missing_feature(avctx,
1073 "Low Delay SBR");
1074 ✗ return AVERROR_PATCHWELCOME;
1075 }
1076
1077
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 while (get_bits(gb, 4) != ELDEXT_TERM) {
1078 ✗ int len = get_bits(gb, 4);
1079 ✗ if (len == 15)
1080 ✗ len += get_bits(gb, 8);
1081 ✗ if (len == 15 + 255)
1082 ✗ len += get_bits(gb, 16);
1083 ✗ if (get_bits_left(gb) < len * 8 + 4) {
1084 ✗ av_log(avctx, AV_LOG_ERROR, overread_err);
1085 ✗ return AVERROR_INVALIDDATA;
1086 }
1087 ✗ skip_bits_long(gb, 8 * len);
1088 }
1089
1090
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1091 &tags, channel_config)))
1092 ✗ return ret;
1093
1094
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
10 if (ac && (ret = ff_aac_output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
1095 ✗ return ret;
1096
1097 10 ep_config = get_bits(gb, 2);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ep_config) {
1099 ✗ avpriv_report_missing_feature(avctx,
1100 "epConfig %d", ep_config);
1101 ✗ return AVERROR_PATCHWELCOME;
1102 }
1103 10 return 0;
1104 }
1105
1106 /**
1107 * Decode audio specific configuration; reference: table 1.13.
1108 *
1109 * @param ac pointer to AACDecContext, may be null
1110 * @param avctx pointer to AVCCodecContext, used for logging
1111 * @param m4ac pointer to MPEG4AudioConfig, used for parsing
1112 * @param gb buffer holding an audio specific config
1113 * @param get_bit_alignment relative alignment for byte align operations
1114 * @param sync_extension look for an appended sync extension
1115 *
1116 * @return Returns error status or number of consumed bits. <0 - error
1117 */
1118 665 static int decode_audio_specific_config_gb(AACDecContext *ac,
1119 AVCodecContext *avctx,
1120 OutputConfiguration *oc,
1121 GetBitContext *gb,
1122 int get_bit_alignment,
1123 int sync_extension)
1124 {
1125 int i, ret;
1126 665 GetBitContext gbc = *gb;
1127 665 MPEG4AudioConfig *m4ac = &oc->m4ac;
1128 665 MPEG4AudioConfig m4ac_bak = *m4ac;
1129
1130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 665 times.
665 if ((i = ff_mpeg4audio_get_config_gb(m4ac, &gbc, sync_extension, avctx)) < 0) {
1131 ✗ *m4ac = m4ac_bak;
1132 ✗ return AVERROR_INVALIDDATA;
1133 }
1134
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
665 if (m4ac->sampling_index > 12) {
1136 ✗ av_log(avctx, AV_LOG_ERROR,
1137 "invalid sampling rate index %d\n",
1138 m4ac->sampling_index);
1139 ✗ *m4ac = m4ac_bak;
1140 ✗ return AVERROR_INVALIDDATA;
1141 }
1142
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 661 times.
665 if (m4ac->object_type == AOT_ER_AAC_LD &&
1143
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)) {
1144 ✗ av_log(avctx, AV_LOG_ERROR,
1145 "invalid low delay sampling rate index %d\n",
1146 m4ac->sampling_index);
1147 ✗ *m4ac = m4ac_bak;
1148 ✗ return AVERROR_INVALIDDATA;
1149 }
1150
1151 665 skip_bits_long(gb, i);
1152
1153
3/4
✓ Branch 0 taken 633 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
665 switch (m4ac->object_type) {
1154 633 case AOT_AAC_MAIN:
1155 case AOT_AAC_LC:
1156 case AOT_AAC_SSR:
1157 case AOT_AAC_LTP:
1158 case AOT_ER_AAC_LC:
1159 case AOT_ER_AAC_LD:
1160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 633 times.
633 if ((ret = decode_ga_specific_config(ac, avctx, gb, get_bit_alignment,
1161 &oc->m4ac, m4ac->chan_config)) < 0)
1162 ✗ return ret;
1163 633 break;
1164 10 case AOT_ER_AAC_ELD:
1165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = decode_eld_specific_config(ac, avctx, gb,
1166 &oc->m4ac, m4ac->chan_config)) < 0)
1167 ✗ return ret;
1168 10 break;
1169 #if CONFIG_AAC_DECODER
1170 22 case AOT_USAC:
1171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 if ((ret = ff_aac_usac_config_decode(ac, avctx, gb,
1172 oc, m4ac->chan_config)) < 0)
1173 ✗ return ret;
1174 22 break;
1175 #endif
1176 ✗ default:
1177 ✗ avpriv_report_missing_feature(avctx,
1178 "Audio object type %s%d",
1179 ✗ m4ac->sbr == 1 ? "SBR+" : "",
1180 m4ac->object_type);
1181 ✗ return AVERROR(ENOSYS);
1182 }
1183
1184 ff_dlog(avctx,
1185 "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1186 m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1187 m4ac->sample_rate, m4ac->sbr,
1188 m4ac->ps);
1189
1190 665 return get_bits_count(gb);
1191 }
1192
1193 340 static int decode_audio_specific_config(AACDecContext *ac,
1194 AVCodecContext *avctx,
1195 OutputConfiguration *oc,
1196 const uint8_t *data, int64_t bit_size,
1197 int sync_extension)
1198 {
1199 int i, ret;
1200 GetBitContext gb;
1201
1202
2/4
✓ Branch 0 taken 340 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 340 times.
340 if (bit_size < 0 || bit_size > INT_MAX) {
1203 ✗ av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
1204 ✗ return AVERROR_INVALIDDATA;
1205 }
1206
1207 ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
1208
2/2
✓ Branch 0 taken 2178 times.
✓ Branch 1 taken 340 times.
2518 for (i = 0; i < bit_size >> 3; i++)
1209 ff_dlog(avctx, "%02x ", data[i]);
1210 ff_dlog(avctx, "\n");
1211
1212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 340 times.
340 if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
1213 ✗ return ret;
1214
1215 340 return decode_audio_specific_config_gb(ac, avctx, oc, &gb, 0,
1216 sync_extension);
1217 }
1218
1219 421 static av_cold int decode_close(AVCodecContext *avctx)
1220 {
1221 421 AACDecContext *ac = avctx->priv_data;
1222
1223
2/2
✓ Branch 0 taken 842 times.
✓ Branch 1 taken 421 times.
1263 for (int i = 0; i < 2; i++) {
1224 842 OutputConfiguration *oc = &ac->oc[i];
1225 842 AACUSACConfig *usac = &oc->usac;
1226
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 842 times.
886 for (int j = 0; j < usac->nb_elems; j++) {
1227 44 AACUsacElemConfig *ec = &usac->elems[j];
1228 44 av_refstruct_unref(&ec->ext.pl_buf);
1229 }
1230
1231 842 av_channel_layout_uninit(&ac->oc[i].ch_layout);
1232 }
1233
1234
2/2
✓ Branch 0 taken 1684 times.
✓ Branch 1 taken 421 times.
2105 for (int type = 0; type < FF_ARRAY_ELEMS(ac->che); type++) {
1235
2/2
✓ Branch 0 taken 107776 times.
✓ Branch 1 taken 1684 times.
109460 for (int i = 0; i < MAX_ELEM_ID; i++) {
1236
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 107180 times.
107776 if (ac->che[type][i]) {
1237 596 ac->proc.sbr_ctx_close(ac->che[type][i]);
1238 596 av_freep(&ac->che[type][i]);
1239 }
1240 }
1241 }
1242
1243 421 av_tx_uninit(&ac->mdct96);
1244 421 av_tx_uninit(&ac->mdct120);
1245 421 av_tx_uninit(&ac->mdct128);
1246 421 av_tx_uninit(&ac->mdct480);
1247 421 av_tx_uninit(&ac->mdct512);
1248 421 av_tx_uninit(&ac->mdct768);
1249 421 av_tx_uninit(&ac->mdct960);
1250 421 av_tx_uninit(&ac->mdct1024);
1251 421 av_tx_uninit(&ac->mdct_ltp);
1252
1253 // Compiler will optimize this branch away.
1254
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 400 times.
421 if (ac->is_fixed)
1255 21 av_freep(&ac->RENAME_FIXED(fdsp));
1256 else
1257 400 av_freep(&ac->fdsp);
1258
1259 421 return 0;
1260 }
1261
1262 421 static av_cold int init_dsp(AVCodecContext *avctx)
1263 {
1264 421 AACDecContext *ac = avctx->priv_data;
1265 421 int is_fixed = ac->is_fixed, ret;
1266 float scale_fixed, scale_float;
1267
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 400 times.
421 const float *const scalep = is_fixed ? &scale_fixed : &scale_float;
1268
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 400 times.
421 enum AVTXType tx_type = is_fixed ? AV_TX_INT32_MDCT : AV_TX_FLOAT_MDCT;
1269
1270 #define MDCT_INIT(s, fn, len, sval) \
1271 scale_fixed = (sval) * 128.0f; \
1272 scale_float = (sval) / 32768.0f; \
1273 ret = av_tx_init(&s, &fn, tx_type, 1, len, scalep, 0); \
1274 if (ret < 0) \
1275 return ret
1276
1277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct96, ac->mdct96_fn, 96, 1.0/96);
1278
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct120, ac->mdct120_fn, 120, 1.0/120);
1279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct128, ac->mdct128_fn, 128, 1.0/128);
1280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct480, ac->mdct480_fn, 480, 1.0/480);
1281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct512, ac->mdct512_fn, 512, 1.0/512);
1282
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct768, ac->mdct768_fn, 768, 1.0/768);
1283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct960, ac->mdct960_fn, 960, 1.0/960);
1284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
421 MDCT_INIT(ac->mdct1024, ac->mdct1024_fn, 1024, 1.0/1024);
1285 #undef MDCT_INIT
1286
1287 /* LTP forward MDCT */
1288 421 scale_fixed = -1.0;
1289 421 scale_float = -32786.0*2 + 36;
1290 421 ret = av_tx_init(&ac->mdct_ltp, &ac->mdct_ltp_fn, tx_type, 0, 1024, scalep, 0);
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (ret < 0)
1292 ✗ return ret;
1293
1294 421 return 0;
1295 }
1296
1297 421 av_cold int ff_aac_decode_init(AVCodecContext *avctx)
1298 {
1299 421 AACDecContext *ac = avctx->priv_data;
1300 int ret;
1301
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (avctx->sample_rate > 96000)
1303 ✗ return AVERROR_INVALIDDATA;
1304
1305 421 ff_aacdec_common_init_once();
1306
1307 421 ac->avctx = avctx;
1308 421 ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1309
1310
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 92 times.
421 if (avctx->extradata_size > 0) {
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
1312 329 avctx->extradata,
1313 329 avctx->extradata_size * 8LL,
1314 1)) < 0)
1315 ✗ return ret;
1316 } else {
1317 int sr, i;
1318 uint8_t layout_map[MAX_ELEM_ID*4][3];
1319 int layout_map_tags;
1320
1321 92 sr = ff_aac_sample_rate_idx(avctx->sample_rate);
1322 92 ac->oc[1].m4ac.sampling_index = sr;
1323 92 ac->oc[1].m4ac.channels = avctx->ch_layout.nb_channels;
1324 92 ac->oc[1].m4ac.sbr = -1;
1325 92 ac->oc[1].m4ac.ps = -1;
1326
1327
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 8 times.
257 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1328
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 165 times.
249 if (ff_mpeg4audio_channels[i] == avctx->ch_layout.nb_channels)
1329 84 break;
1330
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 84 times.
92 if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
1331 8 i = 0;
1332 }
1333 92 ac->oc[1].m4ac.chan_config = i;
1334
1335
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 72 times.
92 if (ac->oc[1].m4ac.chan_config) {
1336 20 ret = ff_aac_set_default_channel_config(ac, avctx, layout_map,
1337 &layout_map_tags,
1338 ac->oc[1].m4ac.chan_config);
1339
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!ret)
1340 20 ff_aac_output_configure(ac, layout_map, layout_map_tags,
1341 OC_GLOBAL_HDR, 0);
1342 ✗ else if (avctx->err_recognition & AV_EF_EXPLODE)
1343 ✗ return AVERROR_INVALIDDATA;
1344 }
1345 }
1346
1347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 421 times.
421 if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
1348 ✗ av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1349 ✗ return AVERROR_INVALIDDATA;
1350 }
1351
1352 421 ac->random_state = 0x1f2e3d4c;
1353
1354 421 return init_dsp(avctx);
1355 }
1356
1357 /**
1358 * Skip data_stream_element; reference: table 4.10.
1359 */
1360 2826 static int skip_data_stream_element(AACDecContext *ac, GetBitContext *gb)
1361 {
1362 2826 int byte_align = get_bits1(gb);
1363 2826 int count = get_bits(gb, 8);
1364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2826 times.
2826 if (count == 255)
1365 ✗ count += get_bits(gb, 8);
1366
2/2
✓ Branch 0 taken 2456 times.
✓ Branch 1 taken 370 times.
2826 if (byte_align)
1367 2456 align_get_bits(gb);
1368
1369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2826 times.
2826 if (get_bits_left(gb) < 8 * count) {
1370 ✗ av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1371 ✗ return AVERROR_INVALIDDATA;
1372 }
1373 2826 skip_bits_long(gb, 8 * count);
1374 2826 return 0;
1375 }
1376
1377 1273 static int decode_prediction(AACDecContext *ac, IndividualChannelStream *ics,
1378 GetBitContext *gb)
1379 {
1380 int sfb;
1381
2/2
✓ Branch 1 taken 212 times.
✓ Branch 2 taken 1061 times.
1273 if (get_bits1(gb)) {
1382 212 ics->predictor_reset_group = get_bits(gb, 5);
1383
1/2
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
212 if (ics->predictor_reset_group == 0 ||
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 212 times.
212 ics->predictor_reset_group > 30) {
1385 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1386 "Invalid Predictor Reset Group.\n");
1387 ✗ return AVERROR_INVALIDDATA;
1388 }
1389 }
1390
2/2
✓ Branch 0 taken 40255 times.
✓ Branch 1 taken 1273 times.
41528 for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1391 40255 ics->prediction_used[sfb] = get_bits1(gb);
1392 }
1393 1273 return 0;
1394 }
1395
1396 /**
1397 * Decode Long Term Prediction data; reference: table 4.xx.
1398 */
1399 786 static void decode_ltp(AACDecContext *ac, LongTermPrediction *ltp,
1400 GetBitContext *gb, uint8_t max_sfb)
1401 {
1402 int sfb;
1403
1404 786 ltp->lag = get_bits(gb, 11);
1405
2/2
✓ Branch 0 taken 393 times.
✓ Branch 1 taken 393 times.
786 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1406 393 ltp->coef_fixed = Q30(ff_ltp_coef[get_bits(gb, 3)]);
1407 else if (CONFIG_AAC_DECODER)
1408 393 ltp->coef = ff_ltp_coef[get_bits(gb, 3)];
1409
1410
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++)
1411 31440 ltp->used[sfb] = get_bits1(gb);
1412 786 }
1413
1414 /**
1415 * Decode Individual Channel Stream info; reference: table 4.6.
1416 */
1417 63980 static int decode_ics_info(AACDecContext *ac, IndividualChannelStream *ics,
1418 GetBitContext *gb)
1419 {
1420 63980 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1421 63980 const int aot = m4ac->object_type;
1422 63980 const int sampling_index = m4ac->sampling_index;
1423 63980 int ret_fail = AVERROR_INVALIDDATA;
1424
1425
2/2
✓ Branch 0 taken 47615 times.
✓ Branch 1 taken 16365 times.
63980 if (aot != AOT_ER_AAC_ELD) {
1426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47615 times.
47615 if (get_bits1(gb)) {
1427 ✗ av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1428 ✗ if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
1429 ✗ return AVERROR_INVALIDDATA;
1430 }
1431 47615 ics->window_sequence[1] = ics->window_sequence[0];
1432 47615 ics->window_sequence[0] = get_bits(gb, 2);
1433
2/2
✓ Branch 0 taken 2424 times.
✓ Branch 1 taken 45191 times.
47615 if (aot == AOT_ER_AAC_LD &&
1434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2424 times.
2424 ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1435 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1436 "AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1437 ✗ "window sequence %d found.\n", ics->window_sequence[0]);
1438 ✗ ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
1439 ✗ return AVERROR_INVALIDDATA;
1440 }
1441 47615 ics->use_kb_window[1] = ics->use_kb_window[0];
1442 47615 ics->use_kb_window[0] = get_bits1(gb);
1443 }
1444 63980 ics->prev_num_window_groups = FFMAX(ics->num_window_groups, 1);
1445 63980 ics->num_window_groups = 1;
1446 63980 ics->group_len[0] = 1;
1447
2/2
✓ Branch 0 taken 1436 times.
✓ Branch 1 taken 62544 times.
63980 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1448 int i;
1449 1436 ics->max_sfb = get_bits(gb, 4);
1450
2/2
✓ Branch 0 taken 10052 times.
✓ Branch 1 taken 1436 times.
11488 for (i = 0; i < 7; i++) {
1451
2/2
✓ Branch 1 taken 6609 times.
✓ Branch 2 taken 3443 times.
10052 if (get_bits1(gb)) {
1452 6609 ics->group_len[ics->num_window_groups - 1]++;
1453 } else {
1454 3443 ics->num_window_groups++;
1455 3443 ics->group_len[ics->num_window_groups - 1] = 1;
1456 }
1457 }
1458 1436 ics->num_windows = 8;
1459
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1435 times.
1436 if (m4ac->frame_length_short) {
1460 1 ics->swb_offset = ff_swb_offset_120[sampling_index];
1461 1 ics->num_swb = ff_aac_num_swb_120[sampling_index];
1462 } else {
1463 1435 ics->swb_offset = ff_swb_offset_128[sampling_index];
1464 1435 ics->num_swb = ff_aac_num_swb_128[sampling_index];
1465 }
1466 1436 ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1467 1436 ics->predictor_present = 0;
1468 } else {
1469 62544 ics->max_sfb = get_bits(gb, 6);
1470 62544 ics->num_windows = 1;
1471
4/4
✓ Branch 0 taken 60120 times.
✓ Branch 1 taken 2424 times.
✓ Branch 2 taken 16365 times.
✓ Branch 3 taken 43755 times.
62544 if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1472
2/2
✓ Branch 0 taken 3559 times.
✓ Branch 1 taken 15230 times.
18789 if (m4ac->frame_length_short) {
1473 3559 ics->swb_offset = ff_swb_offset_480[sampling_index];
1474 3559 ics->num_swb = ff_aac_num_swb_480[sampling_index];
1475 3559 ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1476 } else {
1477 15230 ics->swb_offset = ff_swb_offset_512[sampling_index];
1478 15230 ics->num_swb = ff_aac_num_swb_512[sampling_index];
1479 15230 ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1480 }
1481
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) {
1482 ✗ ret_fail = AVERROR_BUG;
1483 ✗ goto fail;
1484 }
1485 } else {
1486
2/2
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 43369 times.
43755 if (m4ac->frame_length_short) {
1487 386 ics->num_swb = ff_aac_num_swb_960[sampling_index];
1488 386 ics->swb_offset = ff_swb_offset_960[sampling_index];
1489 } else {
1490 43369 ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1491 43369 ics->swb_offset = ff_swb_offset_1024[sampling_index];
1492 }
1493 43755 ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1494 }
1495
2/2
✓ Branch 0 taken 46179 times.
✓ Branch 1 taken 16365 times.
62544 if (aot != AOT_ER_AAC_ELD) {
1496 46179 ics->predictor_present = get_bits1(gb);
1497 46179 ics->predictor_reset_group = 0;
1498 }
1499
2/2
✓ Branch 0 taken 1811 times.
✓ Branch 1 taken 60733 times.
62544 if (ics->predictor_present) {
1500
2/2
✓ Branch 0 taken 1273 times.
✓ Branch 1 taken 538 times.
1811 if (aot == AOT_AAC_MAIN) {
1501
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1273 times.
1273 if (decode_prediction(ac, ics, gb)) {
1502 ✗ goto fail;
1503 }
1504
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 ||
1505 aot == AOT_ER_AAC_LC) {
1506 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1507 "Prediction is not allowed in AAC-LC.\n");
1508 ✗ goto fail;
1509 } else {
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538 times.
538 if (aot == AOT_ER_AAC_LD) {
1511 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1512 "LTP in ER AAC LD not yet implemented.\n");
1513 ✗ ret_fail = AVERROR_PATCHWELCOME;
1514 ✗ goto fail;
1515 }
1516
2/2
✓ Branch 1 taken 382 times.
✓ Branch 2 taken 156 times.
538 if ((ics->ltp.present = get_bits(gb, 1)))
1517 382 decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
1518 }
1519 }
1520 }
1521
1522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63980 times.
63980 if (ics->max_sfb > ics->num_swb) {
1523 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1524 "Number of scalefactor bands in group (%d) "
1525 "exceeds limit (%d).\n",
1526 ✗ ics->max_sfb, ics->num_swb);
1527 ✗ goto fail;
1528 }
1529
1530 63980 return 0;
1531 ✗ fail:
1532 ✗ ics->max_sfb = 0;
1533 ✗ return ret_fail;
1534 }
1535
1536 /**
1537 * Decode band types (section_data payload); reference: table 4.46.
1538 *
1539 * @param band_type array of the used band type
1540 * @param band_type_run_end array of the last scalefactor band of a band type run
1541 *
1542 * @return Returns error status. 0 - OK, !0 - error
1543 */
1544 95410 static int decode_band_types(AACDecContext *ac, SingleChannelElement *sce,
1545 GetBitContext *gb)
1546 {
1547 95410 IndividualChannelStream *ics = &sce->ics;
1548
2/2
✓ Branch 0 taken 2382 times.
✓ Branch 1 taken 93028 times.
95410 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1549
1550
2/2
✓ Branch 0 taken 100923 times.
✓ Branch 1 taken 95410 times.
196333 for (int g = 0; g < ics->num_window_groups; g++) {
1551 100923 int k = 0;
1552
2/2
✓ Branch 0 taken 471998 times.
✓ Branch 1 taken 100923 times.
572921 while (k < ics->max_sfb) {
1553 471998 uint8_t sect_end = k;
1554 int sect_len_incr;
1555 471998 int sect_band_type = get_bits(gb, 4);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471998 times.
471998 if (sect_band_type == 12) {
1557 ✗ av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1558 ✗ return AVERROR_INVALIDDATA;
1559 }
1560 do {
1561 491860 sect_len_incr = get_bits(gb, bits);
1562 491860 sect_end += sect_len_incr;
1563
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 491860 times.
491860 if (get_bits_left(gb) < 0) {
1564 ✗ av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1565 ✗ return AVERROR_INVALIDDATA;
1566 }
1567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 491860 times.
491860 if (sect_end > ics->max_sfb) {
1568 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1569 "Number of bands (%d) exceeds limit (%d).\n",
1570 ✗ sect_end, ics->max_sfb);
1571 ✗ return AVERROR_INVALIDDATA;
1572 }
1573
2/2
✓ Branch 0 taken 19862 times.
✓ Branch 1 taken 471998 times.
491860 } while (sect_len_incr == (1 << bits) - 1);
1574
2/2
✓ Branch 0 taken 3529559 times.
✓ Branch 1 taken 471998 times.
4001557 for (; k < sect_end; k++)
1575 3529559 sce->band_type[g*ics->max_sfb + k] = sect_band_type;
1576 }
1577 }
1578 95410 return 0;
1579 }
1580
1581 /**
1582 * Decode scalefactors; reference: table 4.47.
1583 *
1584 * @param global_gain first scalefactor value as scalefactors are differentially coded
1585 * @param band_type array of the used band type
1586 * @param band_type_run_end array of the last scalefactor band of a band type run
1587 * @param sf array of scalefactors or intensity stereo positions
1588 *
1589 * @return Returns error status. 0 - OK, !0 - error
1590 */
1591 95410 static int decode_scalefactors(AACDecContext *ac, SingleChannelElement *sce,
1592 GetBitContext *gb, unsigned int global_gain)
1593 {
1594 95410 IndividualChannelStream *ics = &sce->ics;
1595 95410 int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1596 int clipped_offset;
1597 95410 int noise_flag = 1;
1598
1599
2/2
✓ Branch 0 taken 100923 times.
✓ Branch 1 taken 95410 times.
196333 for (int g = 0; g < ics->num_window_groups; g++) {
1600
2/2
✓ Branch 0 taken 3529559 times.
✓ Branch 1 taken 100923 times.
3630482 for (int sfb = 0; sfb < ics->max_sfb; sfb++) {
1601
4/4
✓ Branch 0 taken 542827 times.
✓ Branch 1 taken 75214 times.
✓ Branch 2 taken 281173 times.
✓ Branch 3 taken 2630345 times.
3529559 switch (sce->band_type[g*ics->max_sfb + sfb]) {
1602 542827 case ZERO_BT:
1603 542827 sce->sfo[g*ics->max_sfb + sfb] = 0;
1604 542827 break;
1605 75214 case INTENSITY_BT: /* fallthrough */
1606 case INTENSITY_BT2:
1607 75214 offset[2] += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
1608 75214 clipped_offset = av_clip(offset[2], -155, 100);
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75214 times.
75214 if (offset[2] != clipped_offset) {
1610 ✗ avpriv_request_sample(ac->avctx,
1611 "If you heard an audible artifact, there may be a bug in the decoder. "
1612 "Clipped intensity stereo position (%d -> %d)",
1613 offset[2], clipped_offset);
1614 }
1615 75214 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset - 100;
1616 75214 break;
1617 281173 case NOISE_BT:
1618
2/2
✓ Branch 0 taken 6794 times.
✓ Branch 1 taken 274379 times.
281173 if (noise_flag-- > 0)
1619 6794 offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1620 else
1621 274379 offset[1] += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
1622 281173 clipped_offset = av_clip(offset[1], -100, 155);
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281173 times.
281173 if (offset[1] != clipped_offset) {
1624 ✗ avpriv_request_sample(ac->avctx,
1625 "If you heard an audible artifact, there may be a bug in the decoder. "
1626 "Clipped noise gain (%d -> %d)",
1627 offset[1], clipped_offset);
1628 }
1629 281173 sce->sfo[g*ics->max_sfb + sfb] = clipped_offset;
1630 281173 break;
1631 2630345 default:
1632 2630345 offset[0] += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO;
1633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2630345 times.
2630345 if (offset[0] > 255U) {
1634 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1635 "Scalefactor (%d) out of range.\n", offset[0]);
1636 ✗ return AVERROR_INVALIDDATA;
1637 }
1638 2630345 sce->sfo[g*ics->max_sfb + sfb] = offset[0] - 100;
1639 2630345 break;
1640 }
1641 }
1642 }
1643
1644 95410 return 0;
1645 }
1646
1647 /**
1648 * Decode pulse data; reference: table 4.7.
1649 */
1650 1098 static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1651 const uint16_t *swb_offset, int num_swb)
1652 {
1653 int i, pulse_swb;
1654 1098 pulse->num_pulse = get_bits(gb, 2) + 1;
1655 1098 pulse_swb = get_bits(gb, 6);
1656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (pulse_swb >= num_swb)
1657 ✗ return -1;
1658 1098 pulse->pos[0] = swb_offset[pulse_swb];
1659 1098 pulse->pos[0] += get_bits(gb, 5);
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (pulse->pos[0] >= swb_offset[num_swb])
1661 ✗ return -1;
1662 1098 pulse->amp[0] = get_bits(gb, 4);
1663
2/2
✓ Branch 0 taken 1646 times.
✓ Branch 1 taken 1098 times.
2744 for (i = 1; i < pulse->num_pulse; i++) {
1664 1646 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1646 times.
1646 if (pulse->pos[i] >= swb_offset[num_swb])
1666 ✗ return -1;
1667 1646 pulse->amp[i] = get_bits(gb, 4);
1668 }
1669 1098 return 0;
1670 }
1671
1672 /**
1673 * Decode Temporal Noise Shaping data; reference: table 4.48.
1674 *
1675 * @return Returns error status. 0 - OK, !0 - error
1676 */
1677 8609 int ff_aac_decode_tns(AACDecContext *ac, TemporalNoiseShaping *tns,
1678 GetBitContext *gb, const IndividualChannelStream *ics)
1679 {
1680 8609 int tns_max_order = INT32_MAX;
1681 8609 const int is_usac = ac->oc[1].m4ac.object_type == AOT_USAC;
1682 int w, filt, i, coef_len, coef_res, coef_compress;
1683 8609 const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1684
1685 /* USAC doesn't seem to have a limit */
1686
2/2
✓ Branch 0 taken 7113 times.
✓ Branch 1 taken 1496 times.
8609 if (!is_usac)
1687
4/4
✓ Branch 0 taken 6436 times.
✓ Branch 1 taken 677 times.
✓ Branch 2 taken 163 times.
✓ Branch 3 taken 6273 times.
7113 tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1688
1689
2/2
✓ Branch 0 taken 13355 times.
✓ Branch 1 taken 8609 times.
21964 for (w = 0; w < ics->num_windows; w++) {
1690
2/2
✓ Branch 1 taken 9695 times.
✓ Branch 2 taken 3660 times.
13355 if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1691 9695 coef_res = get_bits1(gb);
1692
1693
2/2
✓ Branch 0 taken 12703 times.
✓ Branch 1 taken 9695 times.
22398 for (filt = 0; filt < tns->n_filt[w]; filt++) {
1694 int tmp2_idx;
1695 12703 tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1696
1697
2/2
✓ Branch 0 taken 2941 times.
✓ Branch 1 taken 9762 times.
12703 if (is_usac)
1698 2941 tns->order[w][filt] = get_bits(gb, 4 - is8);
1699 else
1700 9762 tns->order[w][filt] = get_bits(gb, 5 - (2 * is8));
1701
1702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12703 times.
12703 if (tns->order[w][filt] > tns_max_order) {
1703 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1704 "TNS filter order %d is greater than maximum %d.\n",
1705 tns->order[w][filt], tns_max_order);
1706 ✗ tns->order[w][filt] = 0;
1707 ✗ return AVERROR_INVALIDDATA;
1708 }
1709
2/2
✓ Branch 0 taken 12112 times.
✓ Branch 1 taken 591 times.
12703 if (tns->order[w][filt]) {
1710 12112 tns->direction[w][filt] = get_bits1(gb);
1711 12112 coef_compress = get_bits1(gb);
1712 12112 coef_len = coef_res + 3 - coef_compress;
1713 12112 tmp2_idx = 2 * coef_compress + coef_res;
1714
1715
2/2
✓ Branch 0 taken 76727 times.
✓ Branch 1 taken 12112 times.
88839 for (i = 0; i < tns->order[w][filt]; i++) {
1716
2/2
✓ Branch 0 taken 14113 times.
✓ Branch 1 taken 62614 times.
76727 if (CONFIG_AAC_FIXED_DECODER && ac->is_fixed)
1717 14113 tns->coef_fixed[w][filt][i] = Q31(ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)]);
1718 else if (CONFIG_AAC_DECODER)
1719 62614 tns->coef[w][filt][i] = ff_tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1720 }
1721 }
1722 }
1723 }
1724 }
1725 8609 return 0;
1726 }
1727
1728 /**
1729 * Decode Mid/Side data; reference: table 4.54.
1730 *
1731 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1732 * [1] mask is decoded from bitstream; [2] mask is all 1s;
1733 * [3] reserved for scalable AAC
1734 */
1735 28049 static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1736 int ms_present)
1737 {
1738 int idx;
1739 28049 int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1740 28049 cpe->max_sfb_ste = cpe->ch[0].ics.max_sfb;
1741
2/2
✓ Branch 0 taken 13592 times.
✓ Branch 1 taken 14457 times.
28049 if (ms_present == 1) {
1742
2/2
✓ Branch 0 taken 537766 times.
✓ Branch 1 taken 13592 times.
551358 for (idx = 0; idx < max_idx; idx++)
1743 537766 cpe->ms_mask[idx] = get_bits1(gb);
1744
1/2
✓ Branch 0 taken 14457 times.
✗ Branch 1 not taken.
14457 } else if (ms_present == 2) {
1745 14457 memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1746 }
1747 28049 }
1748
1749 ✗ static void decode_gain_control(SingleChannelElement * sce, GetBitContext * gb)
1750 {
1751 // wd_num, wd_test, aloc_size
1752 static const uint8_t gain_mode[4][3] = {
1753 {1, 0, 5}, // ONLY_LONG_SEQUENCE = 0,
1754 {2, 1, 2}, // LONG_START_SEQUENCE,
1755 {8, 0, 2}, // EIGHT_SHORT_SEQUENCE,
1756 {2, 1, 5}, // LONG_STOP_SEQUENCE
1757 };
1758
1759 ✗ const int mode = sce->ics.window_sequence[0];
1760 uint8_t bd, wd, ad;
1761
1762 // FIXME: Store the gain control data on |sce| and do something with it.
1763 ✗ uint8_t max_band = get_bits(gb, 2);
1764 ✗ for (bd = 0; bd < max_band; bd++) {
1765 ✗ for (wd = 0; wd < gain_mode[mode][0]; wd++) {
1766 ✗ uint8_t adjust_num = get_bits(gb, 3);
1767 ✗ for (ad = 0; ad < adjust_num; ad++) {
1768 ✗ skip_bits(gb, 4 + ((wd == 0 && gain_mode[mode][1])
1769 ? 4
1770 ✗ : gain_mode[mode][2]));
1771 }
1772 }
1773 }
1774 ✗ }
1775
1776 /**
1777 * Decode an individual_channel_stream payload; reference: table 4.44.
1778 *
1779 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1780 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1781 *
1782 * @return Returns error status. 0 - OK, !0 - error
1783 */
1784 95410 int ff_aac_decode_ics(AACDecContext *ac, SingleChannelElement *sce,
1785 GetBitContext *gb, int common_window, int scale_flag)
1786 {
1787 Pulse pulse;
1788 95410 TemporalNoiseShaping *tns = &sce->tns;
1789 95410 IndividualChannelStream *ics = &sce->ics;
1790 95410 int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1791 int ret;
1792
1793 95410 eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1794 286230 er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1795
1/2
✓ Branch 0 taken 95410 times.
✗ Branch 1 not taken.
95410 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1796
3/4
✓ Branch 0 taken 95410 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 91774 times.
✓ Branch 3 taken 3636 times.
282594 ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1797
2/2
✓ Branch 0 taken 26598 times.
✓ Branch 1 taken 65176 times.
91774 ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1798
1799 /* This assignment is to silence a GCC warning about the variable being used
1800 * uninitialized when in fact it always is.
1801 */
1802 95410 pulse.num_pulse = 0;
1803
1804 95410 global_gain = get_bits(gb, 8);
1805
1806
3/4
✓ Branch 0 taken 32550 times.
✓ Branch 1 taken 62860 times.
✓ Branch 2 taken 32550 times.
✗ Branch 3 not taken.
95410 if (!common_window && !scale_flag) {
1807 32550 ret = decode_ics_info(ac, ics, gb);
1808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32550 times.
32550 if (ret < 0)
1809 ✗ goto fail;
1810 }
1811
1812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95410 times.
95410 if ((ret = decode_band_types(ac, sce, gb)) < 0)
1813 ✗ goto fail;
1814
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95410 times.
95410 if ((ret = decode_scalefactors(ac, sce, gb, global_gain)) < 0)
1815 ✗ goto fail;
1816
1817 95410 ac->dsp.dequant_scalefactors(sce);
1818
1819 95410 pulse_present = 0;
1820
1/2
✓ Branch 0 taken 95410 times.
✗ Branch 1 not taken.
95410 if (!scale_flag) {
1821
4/4
✓ Branch 0 taken 68812 times.
✓ Branch 1 taken 26598 times.
✓ Branch 3 taken 1098 times.
✓ Branch 4 taken 67714 times.
95410 if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
1098 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1823 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1824 "Pulse tool not allowed in eight short sequence.\n");
1825 ✗ ret = AVERROR_INVALIDDATA;
1826 ✗ goto fail;
1827 }
1828
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1098 times.
1098 if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1829 ✗ av_log(ac->avctx, AV_LOG_ERROR,
1830 "Pulse data corrupt or invalid.\n");
1831 ✗ ret = AVERROR_INVALIDDATA;
1832 ✗ goto fail;
1833 }
1834 }
1835 95410 tns->present = get_bits1(gb);
1836
4/4
✓ Branch 0 taken 7113 times.
✓ Branch 1 taken 88297 times.
✓ Branch 2 taken 5849 times.
✓ Branch 3 taken 1264 times.
95410 if (tns->present && !er_syntax) {
1837 5849 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5849 times.
5849 if (ret < 0)
1839 ✗ goto fail;
1840 }
1841
3/4
✓ Branch 0 taken 68812 times.
✓ Branch 1 taken 26598 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68812 times.
95410 if (!eld_syntax && get_bits1(gb)) {
1842 ✗ decode_gain_control(sce, gb);
1843 ✗ if (!ac->warned_gain_control) {
1844 ✗ avpriv_report_missing_feature(ac->avctx, "Gain control");
1845 ✗ ac->warned_gain_control = 1;
1846 }
1847 }
1848 // I see no textual basis in the spec for this occurring after SSR gain
1849 // control, but this is what both reference and real implementations do
1850
4/4
✓ Branch 0 taken 7113 times.
✓ Branch 1 taken 88297 times.
✓ Branch 2 taken 1264 times.
✓ Branch 3 taken 5849 times.
95410 if (tns->present && er_syntax) {
1851 1264 ret = ff_aac_decode_tns(ac, tns, gb, ics);
1852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1264 times.
1264 if (ret < 0)
1853 ✗ goto fail;
1854 }
1855 }
1856
1857
2/2
✓ Branch 0 taken 1098 times.
✓ Branch 1 taken 94312 times.
95410 ret = ac->proc.decode_spectrum_and_dequant(ac, gb,
1858 pulse_present ? &pulse : NULL,
1859 sce);
1860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95410 times.
95410 if (ret < 0)
1861 ✗ goto fail;
1862
1863
4/4
✓ Branch 0 taken 6564 times.
✓ Branch 1 taken 88846 times.
✓ Branch 2 taken 6040 times.
✓ Branch 3 taken 524 times.
95410 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1864 6040 ac->dsp.apply_prediction(ac, sce);
1865
1866 95410 return 0;
1867 ✗ fail:
1868 ✗ memset(sce->sfo, 0, sizeof(sce->sfo));
1869 ✗ tns->present = 0;
1870 ✗ return ret;
1871 }
1872
1873 /**
1874 * Decode a channel_pair_element; reference: table 4.4.
1875 *
1876 * @return Returns error status. 0 - OK, !0 - error
1877 */
1878 31430 static int decode_cpe(AACDecContext *ac, GetBitContext *gb, ChannelElement *cpe)
1879 {
1880 31430 int i, ret, common_window, ms_present = 0;
1881 31430 int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1882
1883
3/4
✓ Branch 0 taken 21197 times.
✓ Branch 1 taken 10233 times.
✓ Branch 3 taken 21197 times.
✗ Branch 4 not taken.
31430 common_window = eld_syntax || get_bits1(gb);
1884
1/2
✓ Branch 0 taken 31430 times.
✗ Branch 1 not taken.
31430 if (common_window) {
1885
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31430 times.
31430 if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
1886 ✗ return AVERROR_INVALIDDATA;
1887 31430 i = cpe->ch[1].ics.use_kb_window[0];
1888 31430 cpe->ch[1].ics = cpe->ch[0].ics;
1889 31430 cpe->ch[1].ics.use_kb_window[1] = i;
1890
2/2
✓ Branch 0 taken 593 times.
✓ Branch 1 taken 30837 times.
31430 if (cpe->ch[1].ics.predictor_present &&
1891
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 55 times.
593 (ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
1892
2/2
✓ Branch 1 taken 404 times.
✓ Branch 2 taken 134 times.
538 if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1893 404 decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1894 31430 ms_present = get_bits(gb, 2);
1895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31430 times.
31430 if (ms_present == 3) {
1896 ✗ av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
1897 ✗ return AVERROR_INVALIDDATA;
1898
2/2
✓ Branch 0 taken 28049 times.
✓ Branch 1 taken 3381 times.
31430 } else if (ms_present)
1899 28049 decode_mid_side_stereo(cpe, gb, ms_present);
1900 }
1901
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31430 times.
31430 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
1902 ✗ return ret;
1903
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31430 times.
31430 if ((ret = ff_aac_decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
1904 ✗ return ret;
1905
1906
1/2
✓ Branch 0 taken 31430 times.
✗ Branch 1 not taken.
31430 if (common_window) {
1907
2/2
✓ Branch 0 taken 28049 times.
✓ Branch 1 taken 3381 times.
31430 if (ms_present)
1908 28049 ac->dsp.apply_mid_side_stereo(ac, cpe);
1909
2/2
✓ Branch 0 taken 262 times.
✓ Branch 1 taken 31168 times.
31430 if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
1910 262 ac->dsp.apply_prediction(ac, &cpe->ch[0]);
1911 262 ac->dsp.apply_prediction(ac, &cpe->ch[1]);
1912 }
1913 }
1914
1915 31430 ac->dsp.apply_intensity_stereo(ac, cpe, ms_present);
1916 31430 return 0;
1917 }
1918
1919 /**
1920 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1921 *
1922 * @return Returns number of bytes consumed.
1923 */
1924 4 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1925 GetBitContext *gb)
1926 {
1927 int i;
1928 4 int num_excl_chan = 0;
1929
1930 do {
1931
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 for (i = 0; i < 7; i++)
1932 28 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
1933
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));
1934
1935 4 return num_excl_chan / 7;
1936 }
1937
1938 /**
1939 * Decode dynamic range information; reference: table 4.52.
1940 *
1941 * @return Returns number of bytes consumed.
1942 */
1943 883 static int decode_dynamic_range(DynamicRangeControl *che_drc,
1944 GetBitContext *gb)
1945 {
1946 883 int n = 1;
1947 883 int drc_num_bands = 1;
1948 int i;
1949
1950 /* pce_tag_present? */
1951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 883 times.
883 if (get_bits1(gb)) {
1952 ✗ che_drc->pce_instance_tag = get_bits(gb, 4);
1953 ✗ skip_bits(gb, 4); // tag_reserved_bits
1954 ✗ n++;
1955 }
1956
1957 /* excluded_chns_present? */
1958
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 879 times.
883 if (get_bits1(gb)) {
1959 4 n += decode_drc_channel_exclusions(che_drc, gb);
1960 }
1961
1962 /* drc_bands_present? */
1963
2/2
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 845 times.
883 if (get_bits1(gb)) {
1964 38 che_drc->band_incr = get_bits(gb, 4);
1965 38 che_drc->interpolation_scheme = get_bits(gb, 4);
1966 38 n++;
1967 38 drc_num_bands += che_drc->band_incr;
1968
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 38 times.
146 for (i = 0; i < drc_num_bands; i++) {
1969 108 che_drc->band_top[i] = get_bits(gb, 8);
1970 108 n++;
1971 }
1972 }
1973
1974 /* prog_ref_level_present? */
1975
2/2
✓ Branch 1 taken 881 times.
✓ Branch 2 taken 2 times.
883 if (get_bits1(gb)) {
1976 881 che_drc->prog_ref_level = get_bits(gb, 7);
1977 881 skip_bits1(gb); // prog_ref_level_reserved_bits
1978 881 n++;
1979 }
1980
1981
2/2
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 883 times.
1836 for (i = 0; i < drc_num_bands; i++) {
1982 953 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
1983 953 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
1984 953 n++;
1985 }
1986
1987 883 return n;
1988 }
1989
1990 8988 static int decode_fill(AACDecContext *ac, GetBitContext *gb, int len) {
1991 uint8_t buf[256];
1992 int i, major, minor;
1993
1994
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 8608 times.
8988 if (len < 13+7*8)
1995 380 goto unknown;
1996
1997 8608 get_bits(gb, 13); len -= 13;
1998
1999
4/4
✓ Branch 0 taken 1140984 times.
✓ Branch 1 taken 456 times.
✓ Branch 2 taken 1132832 times.
✓ Branch 3 taken 8152 times.
1141440 for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2000 1132832 buf[i] = get_bits(gb, 8);
2001
2002 8608 buf[i] = 0;
2003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8608 times.
8608 if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2004 ✗ av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2005
2006
2/2
✓ Branch 0 taken 8589 times.
✓ Branch 1 taken 19 times.
8608 if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2007 19 ac->avctx->internal->skip_samples = 1024;
2008 }
2009
2010 8589 unknown:
2011 8988 skip_bits_long(gb, len);
2012
2013 8988 return 0;
2014 }
2015
2016 /**
2017 * Decode extension data (incomplete); reference: table 4.51.
2018 *
2019 * @param cnt length of TYPE_FIL syntactic element in bytes
2020 *
2021 * @return Returns number of bytes consumed
2022 */
2023 23919 static int decode_extension_payload(AACDecContext *ac, GetBitContext *gb, int cnt,
2024 ChannelElement *che, enum RawDataBlockType elem_type)
2025 {
2026 23919 int crc_flag = 0;
2027 23919 int res = cnt;
2028 23919 int type = get_bits(gb, 4);
2029
2030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23919 times.
23919 if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2031 ✗ av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2032
2033
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 10996 times.
✓ Branch 2 taken 883 times.
✓ Branch 3 taken 8988 times.
✓ Branch 4 taken 3052 times.
23919 switch (type) { // extension type
2034 ✗ case EXT_SBR_DATA_CRC:
2035 ✗ crc_flag++;
2036 av_fallthrough;
2037 10996 case EXT_SBR_DATA:
2038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10996 times.
10996 if (!che) {
2039 ✗ av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2040 ✗ return res;
2041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10996 times.
10996 } else if (!ac->oc[1].m4ac.sbr) {
2042 ✗ av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2043 ✗ skip_bits_long(gb, 8 * cnt - 4);
2044 ✗ return res;
2045
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10974 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
10996 } else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2046 ✗ av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2047 ✗ skip_bits_long(gb, 8 * cnt - 4);
2048 ✗ return res;
2049
3/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 10902 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
10996 } else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED &&
2050
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 82 times.
94 ac->avctx->ch_layout.nb_channels == 1) {
2051 12 ac->oc[1].m4ac.sbr = 1;
2052 12 ac->oc[1].m4ac.ps = 1;
2053 12 ac->avctx->profile = AV_PROFILE_AAC_HE_V2;
2054 12 ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2055 ac->oc[1].status, 1);
2056 } else {
2057 10984 ac->oc[1].m4ac.sbr = 1;
2058 10984 ac->avctx->profile = AV_PROFILE_AAC_HE;
2059 }
2060
2061 10996 ac->proc.sbr_decode_extension(ac, che, gb, crc_flag, cnt, elem_type,
2062 ac->oc[1].m4ac.frame_length_short);
2063
2064
4/4
✓ Branch 0 taken 2047 times.
✓ Branch 1 taken 8949 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 2025 times.
10996 if (ac->oc[1].m4ac.ps == 1 && !ac->warned_he_aac_mono) {
2065 22 av_log(ac->avctx, AV_LOG_VERBOSE, "Treating HE-AAC mono as stereo.\n");
2066 22 ac->warned_he_aac_mono = 1;
2067 }
2068 10996 break;
2069 883 case EXT_DYNAMIC_RANGE:
2070 883 res = decode_dynamic_range(&ac->che_drc, gb);
2071 883 break;
2072 8988 case EXT_FILL:
2073 8988 decode_fill(ac, gb, 8 * cnt - 4);
2074 8988 break;
2075 3052 case EXT_FILL_DATA:
2076 case EXT_DATA_ELEMENT:
2077 default:
2078 3052 skip_bits_long(gb, 8 * cnt - 4);
2079 3052 break;
2080 };
2081 23919 return res;
2082 }
2083
2084 /**
2085 * channel coupling transformation interface
2086 *
2087 * @param apply_coupling_method pointer to (in)dependent coupling function
2088 */
2089 179041 static void apply_channel_coupling(AACDecContext *ac, ChannelElement *cc,
2090 enum RawDataBlockType type, int elem_id,
2091 enum CouplingPoint coupling_point,
2092 void (*apply_coupling_method)(AACDecContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2093 {
2094 int i, c;
2095
2096
2/2
✓ Branch 0 taken 11458624 times.
✓ Branch 1 taken 179041 times.
11637665 for (i = 0; i < MAX_ELEM_ID; i++) {
2097 11458624 ChannelElement *cce = ac->che[TYPE_CCE][i];
2098 11458624 int index = 0;
2099
2100
4/4
✓ Branch 0 taken 11740 times.
✓ Branch 1 taken 11446884 times.
✓ Branch 2 taken 3915 times.
✓ Branch 3 taken 7825 times.
11458624 if (cce && cce->coup.coupling_point == coupling_point) {
2101 3915 ChannelCoupling *coup = &cce->coup;
2102
2103
2/2
✓ Branch 0 taken 9402 times.
✓ Branch 1 taken 3915 times.
13317 for (c = 0; c <= coup->num_coupled; c++) {
2104
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) {
2105
2/2
✓ Branch 0 taken 2561 times.
✓ Branch 1 taken 180 times.
2741 if (coup->ch_select[c] != 1) {
2106 2561 apply_coupling_method(ac, &cc->ch[0], cce, index);
2107
1/2
✓ Branch 0 taken 2561 times.
✗ Branch 1 not taken.
2561 if (coup->ch_select[c] != 0)
2108 2561 index++;
2109 }
2110
2/2
✓ Branch 0 taken 2147 times.
✓ Branch 1 taken 594 times.
2741 if (coup->ch_select[c] != 2)
2111 2147 apply_coupling_method(ac, &cc->ch[1], cce, index++);
2112 } else
2113
2/2
✓ Branch 0 taken 4720 times.
✓ Branch 1 taken 1941 times.
6661 index += 1 + (coup->ch_select[c] == 3);
2114 }
2115 }
2116 }
2117 179041 }
2118
2119 /**
2120 * Convert spectral data to samples, applying all supported tools as appropriate.
2121 */
2122 50845 static void spectral_to_sample(AACDecContext *ac, int samples)
2123 {
2124 int i, type;
2125 void (*imdct_and_window)(AACDecContext *ac, SingleChannelElement *sce);
2126
3/3
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 16365 times.
✓ Branch 2 taken 33874 times.
50845 switch (ac->oc[1].m4ac.object_type) {
2127 606 case AOT_ER_AAC_LD:
2128 606 imdct_and_window = ac->dsp.imdct_and_windowing_ld;
2129 606 break;
2130 16365 case AOT_ER_AAC_ELD:
2131 16365 imdct_and_window = ac->dsp.imdct_and_windowing_eld;
2132 16365 break;
2133 33874 default:
2134
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 33487 times.
33874 if (ac->oc[1].m4ac.frame_length_short)
2135 387 imdct_and_window = ac->dsp.imdct_and_windowing_960;
2136 else
2137 33487 imdct_and_window = ac->dsp.imdct_and_windowing;
2138 }
2139
2/2
✓ Branch 0 taken 203380 times.
✓ Branch 1 taken 50845 times.
254225 for (type = 3; type >= 0; type--) {
2140
2/2
✓ Branch 0 taken 13016320 times.
✓ Branch 1 taken 203380 times.
13219700 for (i = 0; i < MAX_ELEM_ID; i++) {
2141 13016320 ChannelElement *che = ac->che[type][i];
2142
4/4
✓ Branch 0 taken 64745 times.
✓ Branch 1 taken 12951575 times.
✓ Branch 2 taken 63980 times.
✓ Branch 3 taken 765 times.
13016320 if (che && che->present) {
2143
2/2
✓ Branch 0 taken 59289 times.
✓ Branch 1 taken 4691 times.
63980 if (type <= TYPE_CPE)
2144 59289 apply_channel_coupling(ac, che, type, i, BEFORE_TNS, ac->dsp.apply_dependent_coupling);
2145
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 62196 times.
63980 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2146
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 1246 times.
1784 if (che->ch[0].ics.predictor_present) {
2147
2/2
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 156 times.
538 if (che->ch[0].ics.ltp.present)
2148 382 ac->dsp.apply_ltp(ac, &che->ch[0]);
2149
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)
2150 404 ac->dsp.apply_ltp(ac, &che->ch[1]);
2151 }
2152 }
2153
2/2
✓ Branch 0 taken 4753 times.
✓ Branch 1 taken 59227 times.
63980 if (che->ch[0].tns.present)
2154 4753 ac->dsp.apply_tns(che->ch[0].coeffs,
2155 &che->ch[0].tns, &che->ch[0].ics, 1);
2156
2/2
✓ Branch 0 taken 2360 times.
✓ Branch 1 taken 61620 times.
63980 if (che->ch[1].tns.present)
2157 2360 ac->dsp.apply_tns(che->ch[1].coeffs,
2158 &che->ch[1].tns, &che->ch[1].ics, 1);
2159
2/2
✓ Branch 0 taken 59289 times.
✓ Branch 1 taken 4691 times.
63980 if (type <= TYPE_CPE)
2160 59289 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, ac->dsp.apply_dependent_coupling);
2161
4/4
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 62806 times.
✓ Branch 2 taken 393 times.
✓ Branch 3 taken 781 times.
63980 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2162 63199 imdct_and_window(ac, &che->ch[0]);
2163
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 61415 times.
63199 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2164 1784 ac->dsp.update_ltp(ac, &che->ch[0]);
2165
2/2
✓ Branch 0 taken 31430 times.
✓ Branch 1 taken 31769 times.
63199 if (type == TYPE_CPE) {
2166 31430 imdct_and_window(ac, &che->ch[1]);
2167
2/2
✓ Branch 0 taken 1784 times.
✓ Branch 1 taken 29646 times.
31430 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2168 1784 ac->dsp.update_ltp(ac, &che->ch[1]);
2169 }
2170
2/2
✓ Branch 0 taken 12506 times.
✓ Branch 1 taken 50693 times.
63199 if (ac->oc[1].m4ac.sbr > 0) {
2171 12506 ac->proc.sbr_apply(ac, che, type,
2172 ac->oc[1].m4ac.frame_length_short,
2173 12506 che->ch[0].output,
2174 12506 che->ch[1].output);
2175 }
2176 }
2177
2/2
✓ Branch 0 taken 60463 times.
✓ Branch 1 taken 3517 times.
63980 if (type <= TYPE_CCE)
2178 60463 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, ac->dsp.apply_independent_coupling);
2179 63980 ac->dsp.clip_output(ac, che, type, samples);
2180 63980 che->present = 0;
2181
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 12951575 times.
12952340 } else if (che) {
2182 765 av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2183 765 memset(che->ch[0].output, 0, samples * sizeof(*che->ch[0].output));
2184
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 218 times.
765 if (type == TYPE_CPE)
2185 547 memset(che->ch[1].output, 0, samples * sizeof(*che->ch[1].output));
2186 }
2187 }
2188 }
2189 50845 }
2190
2191 1211 static int parse_adts_frame_header(AACDecContext *ac, GetBitContext *gb)
2192 {
2193 int size;
2194 AACADTSHeaderInfo hdr_info;
2195 uint8_t layout_map[MAX_ELEM_ID*4][3];
2196 int layout_map_tags, ret;
2197
2198 1211 size = ff_adts_header_parse(gb, &hdr_info);
2199
1/2
✓ Branch 0 taken 1211 times.
✗ Branch 1 not taken.
1211 if (size > 0) {
2200
2/4
✓ Branch 0 taken 1211 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1211 times.
1211 if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2201 // This is 2 for "VLB " audio in NSV files.
2202 // See samples/nsv/vlb_audio.
2203 ✗ avpriv_report_missing_feature(ac->avctx,
2204 "More than one AAC RDB per ADTS frame");
2205 ✗ ac->warned_num_aac_frames = 1;
2206 }
2207 1211 push_output_configuration(ac);
2208
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 11 times.
1211 if (hdr_info.chan_config) {
2209 1200 ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1200 times.
1200 if ((ret = ff_aac_set_default_channel_config(ac, ac->avctx,
2211 layout_map,
2212 &layout_map_tags,
2213 1200 hdr_info.chan_config)) < 0)
2214 ✗ return ret;
2215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1200 times.
1200 if ((ret = ff_aac_output_configure(ac, layout_map, layout_map_tags,
2216 1200 FFMAX(ac->oc[1].status,
2217 OC_TRIAL_FRAME), 0)) < 0)
2218 ✗ return ret;
2219 } else {
2220 11 ac->oc[1].m4ac.chan_config = 0;
2221 /**
2222 * dual mono frames in Japanese DTV can have chan_config 0
2223 * WITHOUT specifying PCE.
2224 * thus, set dual mono as default.
2225 */
2226
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11 if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2227 ✗ layout_map_tags = 2;
2228 ✗ layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2229 ✗ layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2230 ✗ layout_map[0][1] = 0;
2231 ✗ layout_map[1][1] = 1;
2232 ✗ if (ff_aac_output_configure(ac, layout_map, layout_map_tags,
2233 OC_TRIAL_FRAME, 0))
2234 ✗ return -7;
2235 }
2236 }
2237 1211 ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2238 1211 ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2239 1211 ac->oc[1].m4ac.object_type = hdr_info.object_type;
2240 1211 ac->oc[1].m4ac.frame_length_short = 0;
2241
2/2
✓ Branch 0 taken 1141 times.
✓ Branch 1 taken 70 times.
1211 if (ac->oc[0].status != OC_LOCKED ||
2242
1/2
✓ Branch 0 taken 1141 times.
✗ Branch 1 not taken.
1141 ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1141 times.
1141 ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2244 70 ac->oc[1].m4ac.sbr = -1;
2245 70 ac->oc[1].m4ac.ps = -1;
2246 }
2247
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1209 times.
1211 if (!hdr_info.crc_absent)
2248 2 skip_bits(gb, 16);
2249 }
2250 1211 return size;
2251 }
2252
2253 16971 static int aac_decode_er_frame(AVCodecContext *avctx, AVFrame *frame,
2254 int *got_frame_ptr, GetBitContext *gb)
2255 {
2256 16971 AACDecContext *ac = avctx->priv_data;
2257 16971 const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2258 ChannelElement *che;
2259 int err, i;
2260
2/2
✓ Branch 0 taken 3559 times.
✓ Branch 1 taken 13412 times.
16971 int samples = m4ac->frame_length_short ? 960 : 1024;
2261 16971 int chan_config = m4ac->chan_config;
2262 16971 int aot = m4ac->object_type;
2263
2264
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)
2265 16971 samples >>= 1;
2266
2267 16971 ac->frame = frame;
2268
2269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16971 times.
16971 if ((err = frame_configure_elements(avctx)) < 0)
2270 ✗ return err;
2271
2272 // The AV_PROFILE_AAC_* defines are all object_type - 1
2273 // This may lead to an undefined profile being signaled
2274 16971 ac->avctx->profile = aot - 1;
2275
2276 16971 ac->tags_mapped = 0;
2277
2278
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) {
2279 ✗ avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2280 chan_config);
2281 ✗ return AVERROR_INVALIDDATA;
2282 }
2283
2/2
✓ Branch 0 taken 18789 times.
✓ Branch 1 taken 16971 times.
35760 for (i = 0; i < ff_tags_per_config[chan_config]; i++) {
2284 18789 const int elem_type = ff_aac_channel_layout_map[chan_config-1][i][0];
2285 18789 const int elem_id = ff_aac_channel_layout_map[chan_config-1][i][1];
2286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18789 times.
18789 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2287 ✗ av_log(ac->avctx, AV_LOG_ERROR,
2288 "channel element %d.%d is not allocated\n",
2289 elem_type, elem_id);
2290 ✗ return AVERROR_INVALIDDATA;
2291 }
2292 18789 che->present = 1;
2293
2/2
✓ Branch 0 taken 2424 times.
✓ Branch 1 taken 16365 times.
18789 if (aot != AOT_ER_AAC_ELD)
2294 2424 skip_bits(gb, 4);
2295
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) {
2296 6738 case TYPE_SCE:
2297 6738 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2298 6738 break;
2299 11445 case TYPE_CPE:
2300 11445 err = decode_cpe(ac, gb, che);
2301 11445 break;
2302 606 case TYPE_LFE:
2303 606 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2304 606 break;
2305 }
2306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18789 times.
18789 if (err < 0)
2307 ✗ return err;
2308 }
2309
2310 16971 spectral_to_sample(ac, samples);
2311
2312
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) {
2313 ✗ av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2314 ✗ return AVERROR_INVALIDDATA;
2315 }
2316
2317 16971 ac->frame->nb_samples = samples;
2318 16971 ac->frame->sample_rate = avctx->sample_rate;
2319 16971 ac->frame->flags |= AV_FRAME_FLAG_KEY;
2320 16971 *got_frame_ptr = 1;
2321
2322 16971 skip_bits_long(gb, get_bits_left(gb));
2323 16971 return 0;
2324 }
2325
2326 33874 static int decode_frame_ga(AVCodecContext *avctx, AACDecContext *ac,
2327 GetBitContext *gb, int *got_frame_ptr)
2328 {
2329 int err;
2330 int is_dmono;
2331 int elem_id;
2332 33874 enum RawDataBlockType elem_type, che_prev_type = TYPE_END;
2333 33874 uint8_t che_presence[4][MAX_ELEM_ID] = {{0}};
2334 33874 ChannelElement *che = NULL, *che_prev = NULL;
2335 33874 int samples = 0, multiplier, audio_found = 0, pce_found = 0, sce_count = 0;
2336 33874 AVFrame *frame = ac->frame;
2337
2338 33874 int payload_alignment = get_bits_count(gb);
2339 // parse
2340
2/2
✓ Branch 1 taken 73492 times.
✓ Branch 2 taken 33874 times.
107366 while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2341 73492 elem_id = get_bits(gb, 4);
2342
2343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73492 times.
73492 if (avctx->debug & FF_DEBUG_STARTCODE)
2344 ✗ av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2345
2346
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 73481 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
73492 if (!avctx->ch_layout.nb_channels && elem_type != TYPE_PCE)
2347 ✗ return AVERROR_INVALIDDATA;
2348
2349
2/2
✓ Branch 0 taken 45191 times.
✓ Branch 1 taken 28301 times.
73492 if (elem_type < TYPE_DSE) {
2350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45191 times.
45191 if (che_presence[elem_type][elem_id]) {
2351 ✗ int error = che_presence[elem_type][elem_id] > 1;
2352 ✗ av_log(ac->avctx, error ? AV_LOG_ERROR : AV_LOG_DEBUG, "channel element %d.%d duplicate\n",
2353 elem_type, elem_id);
2354 ✗ if (error)
2355 ✗ return AVERROR_INVALIDDATA;
2356 }
2357 45191 che_presence[elem_type][elem_id]++;
2358
2359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45191 times.
45191 if (!(che=ff_aac_get_che(ac, elem_type, elem_id))) {
2360 ✗ av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2361 elem_type, elem_id);
2362 ✗ return AVERROR_INVALIDDATA;
2363 }
2364
2/2
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 44804 times.
45191 samples = ac->oc[1].m4ac.frame_length_short ? 960 : 1024;
2365 45191 che->present = 1;
2366 }
2367
2368
7/8
✓ Branch 0 taken 21121 times.
✓ Branch 1 taken 19985 times.
✓ Branch 2 taken 1174 times.
✓ Branch 3 taken 2911 times.
✓ Branch 4 taken 2826 times.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 25464 times.
✗ Branch 7 not taken.
73492 switch (elem_type) {
2369
2370 21121 case TYPE_SCE:
2371 21121 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2372 21121 audio_found = 1;
2373 21121 sce_count++;
2374 21121 break;
2375
2376 19985 case TYPE_CPE:
2377 19985 err = decode_cpe(ac, gb, che);
2378 19985 audio_found = 1;
2379 19985 break;
2380
2381 1174 case TYPE_CCE:
2382 1174 err = ac->proc.decode_cce(ac, gb, che);
2383 1174 break;
2384
2385 2911 case TYPE_LFE:
2386 2911 err = ff_aac_decode_ics(ac, &che->ch[0], gb, 0, 0);
2387 2911 audio_found = 1;
2388 2911 break;
2389
2390 2826 case TYPE_DSE:
2391 2826 err = skip_data_stream_element(ac, gb);
2392 2826 break;
2393
2394 11 case TYPE_PCE: {
2395 11 uint8_t layout_map[MAX_ELEM_ID*4][3] = {{0}};
2396 int tags;
2397
2398 11 int pushed = push_output_configuration(ac);
2399
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11 if (pce_found && !pushed)
2400 ✗ return AVERROR_INVALIDDATA;
2401
2402 11 tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb,
2403 payload_alignment);
2404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (tags < 0) {
2405 ✗ err = tags;
2406 11 break;
2407 }
2408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (pce_found) {
2409 ✗ av_log(avctx, AV_LOG_ERROR,
2410 "Not evaluating a further program_config_element as this construct is dubious at best.\n");
2411 ✗ pop_output_configuration(ac);
2412 } else {
2413 11 err = ff_aac_output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
2414
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!err)
2415 11 ac->oc[1].m4ac.chan_config = 0;
2416 11 pce_found = 1;
2417 }
2418 11 break;
2419 }
2420
2421 25464 case TYPE_FIL:
2422
2/2
✓ Branch 0 taken 16623 times.
✓ Branch 1 taken 8841 times.
25464 if (elem_id == 15)
2423 16623 elem_id += get_bits(gb, 8) - 1;
2424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25464 times.
25464 if (get_bits_left(gb) < 8 * elem_id) {
2425 ✗ av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
2426 ✗ return AVERROR_INVALIDDATA;
2427 }
2428 25464 err = 0;
2429
2/2
✓ Branch 0 taken 23919 times.
✓ Branch 1 taken 25464 times.
49383 while (elem_id > 0) {
2430 23919 int ret = decode_extension_payload(ac, gb, elem_id, che_prev, che_prev_type);
2431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23919 times.
23919 if (ret < 0) {
2432 ✗ err = ret;
2433 ✗ break;
2434 }
2435 23919 elem_id -= ret;
2436 }
2437 25464 break;
2438
2439 ✗ default:
2440 ✗ err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
2441 ✗ break;
2442 }
2443
2444
2/2
✓ Branch 0 taken 45191 times.
✓ Branch 1 taken 28301 times.
73492 if (elem_type < TYPE_DSE) {
2445 45191 che_prev = che;
2446 45191 che_prev_type = elem_type;
2447 }
2448
2449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73492 times.
73492 if (err)
2450 ✗ return err;
2451
2452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73492 times.
73492 if (get_bits_left(gb) < 3) {
2453 ✗ av_log(avctx, AV_LOG_ERROR, overread_err);
2454 ✗ return AVERROR_INVALIDDATA;
2455 }
2456 }
2457
2458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33874 times.
33874 if (!avctx->ch_layout.nb_channels)
2459 ✗ return 0;
2460
2461
4/4
✓ Branch 0 taken 7976 times.
✓ Branch 1 taken 25898 times.
✓ Branch 2 taken 5678 times.
✓ Branch 3 taken 2298 times.
33874 multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
2462 33874 samples <<= multiplier;
2463
2464 33874 spectral_to_sample(ac, samples);
2465
2466
2/4
✓ Branch 0 taken 33874 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33874 times.
✗ Branch 3 not taken.
33874 if (ac->oc[1].status && audio_found) {
2467 33874 avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
2468 33874 avctx->frame_size = samples;
2469 33874 ac->oc[1].status = OC_LOCKED;
2470 }
2471
2472
2/4
✓ Branch 0 taken 33874 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33874 times.
33874 if (samples && avctx->sample_rate <= 0) {
2473 ✗ av_log(avctx, AV_LOG_ERROR,
2474 "Cannot output a frame without a valid sample rate\n");
2475 ✗ return AVERROR_INVALIDDATA;
2476 }
2477
2478
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33874 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33874 if (!ac->frame->data[0] && samples) {
2479 ✗ av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2480 ✗ return AVERROR_INVALIDDATA;
2481 }
2482
2483
1/2
✓ Branch 0 taken 33874 times.
✗ Branch 1 not taken.
33874 if (samples) {
2484 33874 ac->frame->nb_samples = samples;
2485 33874 ac->frame->sample_rate = avctx->sample_rate;
2486 33874 ac->frame->flags |= AV_FRAME_FLAG_KEY;
2487 33874 *got_frame_ptr = 1;
2488 } else {
2489 ✗ av_frame_unref(ac->frame);
2490 ✗ *got_frame_ptr = 0;
2491 }
2492
2493 /* for dual-mono audio (SCE + SCE) */
2494
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 33874 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
33874 is_dmono = ac->dmono_mode && sce_count == 2 &&
2495 ✗ !av_channel_layout_compare(&ac->oc[1].ch_layout,
2496 ✗ &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO);
2497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33874 times.
33874 if (is_dmono) {
2498 ✗ if (ac->dmono_mode == 1)
2499 ✗ frame->data[1] = frame->data[0];
2500 ✗ else if (ac->dmono_mode == 2)
2501 ✗ frame->data[0] = frame->data[1];
2502 }
2503
2504 33874 return 0;
2505 }
2506
2507 42769 static int aac_decode_frame_int(AVCodecContext *avctx, AVFrame *frame,
2508 int *got_frame_ptr, GetBitContext *gb,
2509 const AVPacket *avpkt)
2510 {
2511 int err;
2512 42769 AACDecContext *ac = avctx->priv_data;
2513
2514 42769 ac->frame = frame;
2515 42769 *got_frame_ptr = 0;
2516
2517 // USAC can't be packed into ADTS due to field size limitations.
2518
3/4
✓ Branch 1 taken 1211 times.
✓ Branch 2 taken 41558 times.
✓ Branch 3 taken 1211 times.
✗ Branch 4 not taken.
42769 if (show_bits(gb, 12) == 0xfff && ac->oc[1].m4ac.object_type != AOT_USAC) {
2519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1211 times.
1211 if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2520 ✗ av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2521 ✗ goto fail;
2522 }
2523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1211 times.
1211 if (ac->oc[1].m4ac.sampling_index > 12) {
2524 ✗ av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2525 ✗ err = AVERROR_INVALIDDATA;
2526 ✗ goto fail;
2527 }
2528 }
2529
2530
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42769 times.
42769 if ((err = frame_configure_elements(avctx)) < 0)
2531 ✗ goto fail;
2532
2533 // The AV_PROFILE_AAC_* defines are all object_type - 1
2534 // This may lead to an undefined profile being signaled
2535 42769 ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2536
2537 42769 ac->tags_mapped = 0;
2538
2539
2/2
✓ Branch 0 taken 8895 times.
✓ Branch 1 taken 33874 times.
42769 if (ac->oc[1].m4ac.object_type == AOT_USAC) {
2540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8895 times.
8895 if (ac->is_fixed) {
2541 ✗ avpriv_report_missing_feature(ac->avctx,
2542 "AAC USAC fixed-point decoding");
2543 ✗ return AVERROR_PATCHWELCOME;
2544 }
2545 #if CONFIG_AAC_DECODER
2546 8895 err = ff_aac_usac_decode_frame(avctx, ac, gb, got_frame_ptr);
2547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8895 times.
8895 if (err < 0)
2548 ✗ goto fail;
2549 #endif
2550 } else {
2551 33874 err = decode_frame_ga(avctx, ac, gb, got_frame_ptr);
2552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33874 times.
33874 if (err < 0)
2553 ✗ goto fail;
2554 }
2555
2556 42769 return err;
2557
2558 ✗ fail:
2559 ✗ pop_output_configuration(ac);
2560 ✗ return err;
2561 }
2562
2563 59222 static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2564 int *got_frame_ptr, AVPacket *avpkt)
2565 {
2566 59222 AACDecContext *ac = avctx->priv_data;
2567 59222 const uint8_t *buf = avpkt->data;
2568 59222 int buf_size = avpkt->size;
2569 GetBitContext gb;
2570 int buf_consumed;
2571 int buf_offset;
2572 int err;
2573 size_t new_extradata_size;
2574 59222 const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
2575 AV_PKT_DATA_NEW_EXTRADATA,
2576 &new_extradata_size);
2577 size_t jp_dualmono_size;
2578 59222 const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
2579 AV_PKT_DATA_JP_DUALMONO,
2580 &jp_dualmono_size);
2581
2582
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 59221 times.
59222 if (new_extradata) {
2583 /* discard previous configuration */
2584 1 ac->oc[1].status = OC_NONE;
2585 1 err = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1],
2586 new_extradata,
2587 1 new_extradata_size * 8LL, 1);
2588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (err < 0) {
2589 ✗ return err;
2590 }
2591 }
2592
2593 59222 ac->dmono_mode = 0;
2594
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
59222 if (jp_dualmono && jp_dualmono_size > 0)
2595 ✗ ac->dmono_mode = 1 + *jp_dualmono;
2596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
59222 if (ac->force_dmono_mode >= 0)
2597 ✗ ac->dmono_mode = ac->force_dmono_mode;
2598
2599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
59222 if (INT_MAX / 8 <= buf_size)
2600 ✗ return AVERROR_INVALIDDATA;
2601
2602
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 59222 times.
59222 if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
2603 ✗ return err;
2604
2605
2/2
✓ Branch 0 taken 16971 times.
✓ Branch 1 taken 42251 times.
59222 switch (ac->oc[1].m4ac.object_type) {
2606 16971 case AOT_ER_AAC_LC:
2607 case AOT_ER_AAC_LTP:
2608 case AOT_ER_AAC_LD:
2609 case AOT_ER_AAC_ELD:
2610 16971 err = aac_decode_er_frame(avctx, frame, got_frame_ptr, &gb);
2611 16971 break;
2612 42251 default:
2613 42251 err = aac_decode_frame_int(avctx, frame, got_frame_ptr, &gb, avpkt);
2614 }
2615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
59222 if (err < 0)
2616 ✗ return err;
2617
2618 59222 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
59222 for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
2620 ✗ if (buf[buf_offset])
2621 ✗ break;
2622
2623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59222 times.
59222 return buf_size > buf_offset ? buf_consumed : buf_size;
2624 }
2625
2626 #if CONFIG_AAC_LATM_DECODER
2627 #include "aacdec_latm.h"
2628 #endif
2629
2630 #define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2631 #define OFF(field) offsetof(AACDecContext, field)
2632 static const AVOption options[] = {
2633 /**
2634 * AVOptions for Japanese DTV specific extensions (ADTS only)
2635 */
2636 {"dual_mono_mode", "Select the channel to decode for dual mono",
2637 OFF(force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
2638 AACDEC_FLAGS, .unit = "dual_mono_mode"},
2639
2640 {"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2641 {"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2642 {"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2643 {"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, .unit = "dual_mono_mode"},
2644
2645 { "channel_order", "Order in which the channels are to be exported",
2646 OFF(output_channel_order), AV_OPT_TYPE_INT,
2647 { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, AACDEC_FLAGS, .unit = "channel_order" },
2648 { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST,
2649 { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2650 { "coded", "order in which the channels are coded in the bitstream",
2651 0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = AACDEC_FLAGS, .unit = "channel_order" },
2652
2653 { "target_level", "Target output loudness in dBFS for xHE-AAC normalization (0 = disabled)",
2654 OFF(target_level), AV_OPT_TYPE_INT, { .i64 = 0 }, -70, 0, AACDEC_FLAGS },
2655
2656 {NULL},
2657 };
2658
2659 static const AVClass decoder_class = {
2660 .class_name = "AAC decoder",
2661 .item_name = av_default_item_name,
2662 .option = options,
2663 .version = LIBAVUTIL_VERSION_INT,
2664 };
2665
2666 #if CONFIG_AAC_DECODER
2667 const FFCodec ff_aac_decoder = {
2668 .p.name = "aac",
2669 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2670 .p.type = AVMEDIA_TYPE_AUDIO,
2671 .p.id = AV_CODEC_ID_AAC,
2672 .p.priv_class = &decoder_class,
2673 .priv_data_size = sizeof(AACDecContext),
2674 .init = ff_aac_decode_init_float,
2675 .close = decode_close,
2676 FF_CODEC_DECODE_CB(aac_decode_frame),
2677 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP),
2678 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2679 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2680 CODEC_CH_LAYOUTS_ARRAY(ff_aac_ch_layout),
2681 .flush = flush,
2682 .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
2683 };
2684 #endif
2685
2686 #if CONFIG_AAC_FIXED_DECODER
2687 const FFCodec ff_aac_fixed_decoder = {
2688 .p.name = "aac_fixed",
2689 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
2690 .p.type = AVMEDIA_TYPE_AUDIO,
2691 .p.id = AV_CODEC_ID_AAC,
2692 .p.priv_class = &decoder_class,
2693 .priv_data_size = sizeof(AACDecContext),
2694 .init = ff_aac_decode_init_fixed,
2695 .close = decode_close,
2696 FF_CODEC_DECODE_CB(aac_decode_frame),
2697 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S32P),
2698 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
2699 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2700 CODEC_CH_LAYOUTS_ARRAY(ff_aac_ch_layout),
2701 .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
2702 .flush = flush,
2703 };
2704 #endif
2705