FFmpeg coverage


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