FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 976 1336 73.1%
Functions: 44 46 95.7%
Branches: 632 995 63.5%

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