FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec.c
Date: 2026-08-14 15:11:37
Exec Total Coverage
Lines: 1044 1398 74.7%
Functions: 45 47 95.7%
Branches: 679 1043 65.1%

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