FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec.c
Date: 2026-11-01 12:52:33
Exec Total Coverage
Lines: 1090 1416 77.0%
Functions: 46 48 95.8%
Branches: 718 1067 67.3%

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