FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec.c
Date: 2026-05-03 13:33:45
Exec Total Coverage
Lines: 980 1340 73.1%
Functions: 45 47 95.7%
Branches: 633 999 63.4%

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