FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aac/aacdec_proc_template.c
Date: 2024-05-04 02:01:39
Exec Total Coverage
Lines: 212 218 97.2%
Functions: 5 5 100.0%
Branches: 127 133 95.5%

Line Branch Exec Source
1 /*
2 * AAC decoder
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 /**
33 * linear congruential pseudorandom number generator
34 *
35 * @param previous_val pointer to the current state of the generator
36 *
37 * @return Returns a 32-bit pseudorandom integer
38 */
39 5944200 static av_always_inline int lcg_random(unsigned previous_val)
40 {
41 5944200 union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
42 5944200 return v.s;
43 }
44
45 /**
46 * Decode spectral data; reference: table 4.50.
47 * Dequantize and scale spectral data; reference: 4.6.3.3.
48 *
49 * @param coef array of dequantized, scaled spectral data
50 * @param sf array of scalefactors or intensity stereo positions
51 * @param pulse_present set if pulses are present
52 * @param pulse pointer to pulse data struct
53 * @param band_type array of the used band type
54 *
55 * @return Returns error status. 0 - OK, !0 - error
56 */
57 93936 static int AAC_RENAME(decode_spectrum_and_dequant)(AACDecContext *ac,
58 GetBitContext *gb,
59 const Pulse *pulse,
60 SingleChannelElement *sce)
61 {
62 93936 int i, k, g, idx = 0;
63 93936 INTFLOAT *coef = sce->AAC_RENAME(coeffs);
64 93936 IndividualChannelStream *ics = &sce->ics;
65 93936 const int c = 1024 / ics->num_windows;
66 93936 const uint16_t *offsets = ics->swb_offset;
67 93936 const INTFLOAT *sf = sce->AAC_RENAME(sf);
68 93936 const enum BandType *band_type = sce->band_type;
69 93936 INTFLOAT *coef_base = coef;
70
71
2/2
✓ Branch 0 taken 108559 times.
✓ Branch 1 taken 93936 times.
202495 for (g = 0; g < ics->num_windows; g++)
72 108559 memset(coef + g * 128 + offsets[ics->max_sfb], 0,
73 108559 sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
74
75
2/2
✓ Branch 0 taken 99174 times.
✓ Branch 1 taken 93936 times.
193110 for (g = 0; g < ics->num_window_groups; g++) {
76 99174 unsigned g_len = ics->group_len[g];
77
78
2/2
✓ Branch 0 taken 3478656 times.
✓ Branch 1 taken 99174 times.
3577830 for (i = 0; i < ics->max_sfb; i++, idx++) {
79 3478656 const unsigned cbt_m1 = band_type[idx] - 1;
80 3478656 INTFLOAT *cfo = coef + offsets[i];
81 3478656 int off_len = offsets[i + 1] - offsets[i];
82 int group;
83
84
2/2
✓ Branch 0 taken 603212 times.
✓ Branch 1 taken 2875444 times.
3478656 if (cbt_m1 >= INTENSITY_BT2 - 1) {
85
2/2
✓ Branch 0 taken 641285 times.
✓ Branch 1 taken 603212 times.
1244497 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
86 641285 memset(cfo, 0, off_len * sizeof(*cfo));
87 }
88
2/2
✓ Branch 0 taken 281615 times.
✓ Branch 1 taken 2593829 times.
2875444 } else if (cbt_m1 == NOISE_BT - 1) {
89
2/2
✓ Branch 0 taken 281642 times.
✓ Branch 1 taken 281615 times.
563257 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
90 INTFLOAT band_energy;
91 #if USE_FIXED
92
2/2
✓ Branch 0 taken 2883476 times.
✓ Branch 1 taken 137931 times.
3021407 for (k = 0; k < off_len; k++) {
93 2883476 ac->random_state = lcg_random(ac->random_state);
94 2883476 cfo[k] = ac->random_state >> 3;
95 }
96
97 137931 band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
98 137931 band_energy = fixed_sqrt(band_energy, 31);
99 137931 noise_scale(cfo, sf[idx], band_energy, off_len);
100 #else
101 float scale;
102
103
2/2
✓ Branch 0 taken 3060724 times.
✓ Branch 1 taken 143711 times.
3204435 for (k = 0; k < off_len; k++) {
104 3060724 ac->random_state = lcg_random(ac->random_state);
105 3060724 cfo[k] = ac->random_state;
106 }
107
108 143711 band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
109 143711 scale = sf[idx] / sqrtf(band_energy);
110 143711 ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
111 #endif /* USE_FIXED */
112 }
113 } else {
114 #if !USE_FIXED
115 1810602 const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
116 #endif /* !USE_FIXED */
117 2593829 const VLCElem *vlc_tab = ff_vlc_spectral[cbt_m1];
118 2593829 OPEN_READER(re, gb);
119
120
5/5
✓ Branch 0 taken 629003 times.
✓ Branch 1 taken 579115 times.
✓ Branch 2 taken 400327 times.
✓ Branch 3 taken 545654 times.
✓ Branch 4 taken 439730 times.
2593829 switch (cbt_m1 >> 1) {
121 629003 case 0:
122
2/2
✓ Branch 0 taken 656367 times.
✓ Branch 1 taken 629003 times.
1285370 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
123 656367 INTFLOAT *cf = cfo;
124 656367 int len = off_len;
125
126 do {
127 int code;
128 unsigned cb_idx;
129
130 3269786 UPDATE_CACHE(re, gb);
131
2/2
✓ Branch 1 taken 111229 times.
✓ Branch 2 taken 3158557 times.
3269786 GET_VLC(code, re, gb, vlc_tab, 8, 2);
132 3269786 cb_idx = code;
133 #if USE_FIXED
134 996762 cf = DEC_SQUAD(cf, cb_idx);
135 #else
136 2273024 cf = VMUL4(cf, vq, cb_idx, sf + idx);
137 #endif /* USE_FIXED */
138
2/2
✓ Branch 0 taken 2613419 times.
✓ Branch 1 taken 656367 times.
3269786 } while (len -= 4);
139 }
140 629003 break;
141
142 579115 case 1:
143
2/2
✓ Branch 0 taken 591572 times.
✓ Branch 1 taken 579115 times.
1170687 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
144 591572 INTFLOAT *cf = cfo;
145 591572 int len = off_len;
146
147 do {
148 int code;
149 unsigned nnz;
150 unsigned cb_idx;
151 uint32_t bits;
152
153 2636574 UPDATE_CACHE(re, gb);
154
2/2
✓ Branch 1 taken 94277 times.
✓ Branch 2 taken 2542297 times.
2636574 GET_VLC(code, re, gb, vlc_tab, 8, 2);
155 2636574 cb_idx = code;
156 2636574 nnz = cb_idx >> 8 & 15;
157
2/2
✓ Branch 0 taken 1734646 times.
✓ Branch 1 taken 901928 times.
2636574 bits = nnz ? GET_CACHE(re, gb) : 0;
158 2636574 LAST_SKIP_BITS(re, gb, nnz);
159 #if USE_FIXED
160 826740 cf = DEC_UQUAD(cf, cb_idx, bits);
161 #else
162 1809834 cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
163 #endif /* USE_FIXED */
164
2/2
✓ Branch 0 taken 2045002 times.
✓ Branch 1 taken 591572 times.
2636574 } while (len -= 4);
165 }
166 579115 break;
167
168 400327 case 2:
169
2/2
✓ Branch 0 taken 413052 times.
✓ Branch 1 taken 400327 times.
813379 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
170 413052 INTFLOAT *cf = cfo;
171 413052 int len = off_len;
172
173 do {
174 int code;
175 unsigned cb_idx;
176
177 2505804 UPDATE_CACHE(re, gb);
178
2/2
✓ Branch 1 taken 87742 times.
✓ Branch 2 taken 2418062 times.
2505804 GET_VLC(code, re, gb, vlc_tab, 8, 2);
179 2505804 cb_idx = code;
180 #if USE_FIXED
181 690688 cf = DEC_SPAIR(cf, cb_idx);
182 #else
183 1815116 cf = VMUL2(cf, vq, cb_idx, sf + idx);
184 #endif /* USE_FIXED */
185
2/2
✓ Branch 0 taken 2092752 times.
✓ Branch 1 taken 413052 times.
2505804 } while (len -= 2);
186 }
187 400327 break;
188
189 545654 case 3:
190 case 4:
191
2/2
✓ Branch 0 taken 564253 times.
✓ Branch 1 taken 545654 times.
1109907 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
192 564253 INTFLOAT *cf = cfo;
193 564253 int len = off_len;
194
195 do {
196 int code;
197 unsigned nnz;
198 unsigned cb_idx;
199 unsigned sign;
200
201 2992674 UPDATE_CACHE(re, gb);
202
2/2
✓ Branch 1 taken 212568 times.
✓ Branch 2 taken 2780106 times.
2992674 GET_VLC(code, re, gb, vlc_tab, 8, 2);
203 2992674 cb_idx = code;
204 2992674 nnz = cb_idx >> 8 & 15;
205
2/2
✓ Branch 0 taken 2385006 times.
✓ Branch 1 taken 607668 times.
2992674 sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
206 2992674 LAST_SKIP_BITS(re, gb, nnz);
207 #if USE_FIXED
208 718254 cf = DEC_UPAIR(cf, cb_idx, sign);
209 #else
210 2274420 cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
211 #endif /* USE_FIXED */
212
2/2
✓ Branch 0 taken 2428421 times.
✓ Branch 1 taken 564253 times.
2992674 } while (len -= 2);
213 }
214 545654 break;
215
216 439730 default:
217
2/2
✓ Branch 0 taken 449650 times.
✓ Branch 1 taken 439730 times.
889380 for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
218 #if USE_FIXED
219 120281 int *icf = cfo;
220 int v;
221 #else
222 329369 float *cf = cfo;
223 329369 uint32_t *icf = (uint32_t *) cf;
224 #endif /* USE_FIXED */
225 449650 int len = off_len;
226
227 do {
228 int code;
229 unsigned nzt, nnz;
230 unsigned cb_idx;
231 uint32_t bits;
232 int j;
233
234 1489808 UPDATE_CACHE(re, gb);
235
2/2
✓ Branch 1 taken 425578 times.
✓ Branch 2 taken 1064230 times.
1489808 GET_VLC(code, re, gb, vlc_tab, 8, 2);
236 1489808 cb_idx = code;
237
238
2/2
✓ Branch 0 taken 70729 times.
✓ Branch 1 taken 1419079 times.
1489808 if (cb_idx == 0x0000) {
239 70729 *icf++ = 0;
240 70729 *icf++ = 0;
241 70729 continue;
242 }
243
244 1419079 nnz = cb_idx >> 12;
245 1419079 nzt = cb_idx >> 8;
246 1419079 bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
247 1419079 LAST_SKIP_BITS(re, gb, nnz);
248
249
2/2
✓ Branch 0 taken 2838158 times.
✓ Branch 1 taken 1419079 times.
4257237 for (j = 0; j < 2; j++) {
250
2/2
✓ Branch 0 taken 367260 times.
✓ Branch 1 taken 2470898 times.
2838158 if (nzt & 1<<j) {
251 uint32_t b;
252 int n;
253 /* The total length of escape_sequence must be < 22 bits according
254 to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
255 367260 UPDATE_CACHE(re, gb);
256 367260 b = GET_CACHE(re, gb);
257 367260 b = 31 - av_log2(~b);
258
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 367260 times.
367260 if (b > 8) {
260 av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
261 return AVERROR_INVALIDDATA;
262 }
263
264 367260 SKIP_BITS(re, gb, b + 1);
265 367260 b += 4;
266 367260 n = (1 << b) + SHOW_UBITS(re, gb, b);
267 367260 LAST_SKIP_BITS(re, gb, b);
268 #if USE_FIXED
269 67153 v = n;
270
2/2
✓ Branch 0 taken 33454 times.
✓ Branch 1 taken 33699 times.
67153 if (bits & 1U<<31)
271 33454 v = -v;
272 67153 *icf++ = v;
273 #else
274 300107 *icf++ = ff_cbrt_tab[n] | (bits & 1U<<31);
275 #endif /* USE_FIXED */
276 367260 bits <<= 1;
277 } else {
278 #if USE_FIXED
279 544503 v = cb_idx & 15;
280
2/2
✓ Branch 0 taken 258827 times.
✓ Branch 1 taken 285676 times.
544503 if (bits & 1U<<31)
281 258827 v = -v;
282 544503 *icf++ = v;
283 #else
284 1926395 unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
285 1926395 *icf++ = (bits & 1U<<31) | v;
286 #endif /* USE_FIXED */
287 2470898 bits <<= !!v;
288 }
289 2838158 cb_idx >>= 4;
290 }
291
2/2
✓ Branch 0 taken 1040158 times.
✓ Branch 1 taken 449650 times.
1489808 } while (len -= 2);
292 #if !USE_FIXED
293 329369 ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
294 #endif /* !USE_FIXED */
295 }
296 }
297
298 2593829 CLOSE_READER(re, gb);
299 }
300 }
301 99174 coef += g_len << 7;
302 }
303
304
2/2
✓ Branch 0 taken 1098 times.
✓ Branch 1 taken 92838 times.
93936 if (pulse) {
305 1098 idx = 0;
306
2/2
✓ Branch 0 taken 2744 times.
✓ Branch 1 taken 1098 times.
3842 for (i = 0; i < pulse->num_pulse; i++) {
307 2744 INTFLOAT co = coef_base[ pulse->pos[i] ];
308
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 2744 times.
6840 while (offsets[idx + 1] <= pulse->pos[i])
309 4096 idx++;
310
3/4
✓ Branch 0 taken 2744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2668 times.
✓ Branch 3 taken 76 times.
2744 if (band_type[idx] != NOISE_BT && sf[idx]) {
311 2668 INTFLOAT ico = -pulse->amp[i];
312 #if USE_FIXED
313
2/2
✓ Branch 0 taken 730 times.
✓ Branch 1 taken 134 times.
864 if (co) {
314
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 351 times.
730 ico = co + (co > 0 ? -ico : ico);
315 }
316 864 coef_base[ pulse->pos[i] ] = ico;
317 #else
318
2/2
✓ Branch 0 taken 1564 times.
✓ Branch 1 taken 240 times.
1804 if (co) {
319 1564 co /= sf[idx];
320
2/2
✓ Branch 0 taken 799 times.
✓ Branch 1 taken 765 times.
1564 ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
321 }
322 1804 coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
323 #endif /* USE_FIXED */
324 }
325 }
326 }
327 #if USE_FIXED
328 29050 coef = coef_base;
329 29050 idx = 0;
330
2/2
✓ Branch 0 taken 31382 times.
✓ Branch 1 taken 29050 times.
60432 for (g = 0; g < ics->num_window_groups; g++) {
331 31382 unsigned g_len = ics->group_len[g];
332
333
2/2
✓ Branch 0 taken 1053985 times.
✓ Branch 1 taken 31382 times.
1085367 for (i = 0; i < ics->max_sfb; i++, idx++) {
334 1053985 const unsigned cbt_m1 = band_type[idx] - 1;
335 1053985 int *cfo = coef + offsets[i];
336 1053985 int off_len = offsets[i + 1] - offsets[i];
337 int group;
338
339
2/2
✓ Branch 0 taken 783227 times.
✓ Branch 1 taken 270758 times.
1053985 if (cbt_m1 < NOISE_BT - 1) {
340
2/2
✓ Branch 0 taken 798771 times.
✓ Branch 1 taken 783227 times.
1581998 for (group = 0; group < (int)g_len; group++, cfo+=128) {
341 798771 vector_pow43(cfo, off_len);
342 798771 subband_scale(cfo, cfo, sf[idx], 34, off_len, ac->avctx);
343 }
344 }
345 }
346 31382 coef += g_len << 7;
347 }
348 #endif /* USE_FIXED */
349 93936 return 0;
350 }
351
352 /**
353 * Decode coupling_channel_element; reference: table 4.8.
354 *
355 * @return Returns error status. 0 - OK, !0 - error
356 */
357 1174 static int AAC_RENAME(decode_cce)(AACDecContext *ac, GetBitContext *gb, ChannelElement *che)
358 {
359 1174 int num_gain = 0;
360 int c, g, sfb, ret;
361 int sign;
362 INTFLOAT scale;
363 1174 SingleChannelElement *sce = &che->ch[0];
364 1174 ChannelCoupling *coup = &che->coup;
365
366 1174 coup->coupling_point = 2 * get_bits1(gb);
367 1174 coup->num_coupled = get_bits(gb, 3);
368
2/2
✓ Branch 0 taken 2741 times.
✓ Branch 1 taken 1174 times.
3915 for (c = 0; c <= coup->num_coupled; c++) {
369 2741 num_gain++;
370 2741 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
371 2741 coup->id_select[c] = get_bits(gb, 4);
372
2/2
✓ Branch 0 taken 2348 times.
✓ Branch 1 taken 393 times.
2741 if (coup->type[c] == TYPE_CPE) {
373 2348 coup->ch_select[c] = get_bits(gb, 2);
374
2/2
✓ Branch 0 taken 1967 times.
✓ Branch 1 taken 381 times.
2348 if (coup->ch_select[c] == 3)
375 1967 num_gain++;
376 } else
377 393 coup->ch_select[c] = 2;
378 }
379
4/4
✓ Branch 1 taken 602 times.
✓ Branch 2 taken 572 times.
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 450 times.
1174 coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
380
381 1174 sign = get_bits(gb, 1);
382 #if USE_FIXED
383 130 scale = get_bits(gb, 2);
384 #else
385 1044 scale = cce_scale[get_bits(gb, 2)];
386 #endif
387
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1174 times.
1174 if ((ret = ff_aac_decode_ics(ac, sce, gb, 0, 0)))
389 return ret;
390
391
2/2
✓ Branch 0 taken 4708 times.
✓ Branch 1 taken 1174 times.
5882 for (c = 0; c < num_gain; c++) {
392 4708 int idx = 0;
393 4708 int cge = 1;
394 4708 int gain = 0;
395 4708 INTFLOAT gain_cache = FIXR10(1.);
396
2/2
✓ Branch 0 taken 3534 times.
✓ Branch 1 taken 1174 times.
4708 if (c) {
397
2/2
✓ Branch 0 taken 1962 times.
✓ Branch 1 taken 1572 times.
3534 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
398
2/2
✓ Branch 0 taken 1973 times.
✓ Branch 1 taken 1561 times.
3534 gain = cge ? get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - 60: 0;
399 3534 gain_cache = GET_GAIN(scale, gain);
400 #if USE_FIXED
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if ((abs(gain_cache)-1024) >> 3 > 30)
402 return AVERROR(ERANGE);
403 #endif
404 }
405
2/2
✓ Branch 0 taken 1965 times.
✓ Branch 1 taken 2743 times.
4708 if (coup->coupling_point == AFTER_IMDCT) {
406 1965 coup->gain[c][0] = gain_cache;
407 } else {
408
2/2
✓ Branch 0 taken 2743 times.
✓ Branch 1 taken 2743 times.
5486 for (g = 0; g < sce->ics.num_window_groups; g++) {
409
2/2
✓ Branch 0 taken 109720 times.
✓ Branch 1 taken 2743 times.
112463 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
410
2/2
✓ Branch 0 taken 5196 times.
✓ Branch 1 taken 104524 times.
109720 if (sce->band_type[idx] != ZERO_BT) {
411
2/2
✓ Branch 0 taken 2879 times.
✓ Branch 1 taken 2317 times.
5196 if (!cge) {
412 2879 int t = get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - 60;
413
2/2
✓ Branch 0 taken 2724 times.
✓ Branch 1 taken 155 times.
2879 if (t) {
414 2724 int s = 1;
415 2724 t = gain += t;
416
2/2
✓ Branch 0 taken 1666 times.
✓ Branch 1 taken 1058 times.
2724 if (sign) {
417 1666 s -= 2 * (t & 0x1);
418 1666 t >>= 1;
419 }
420 2724 gain_cache = GET_GAIN(scale, t) * s;
421 #if USE_FIXED
422 if ((abs(gain_cache)-1024) >> 3 > 30)
423 return AVERROR(ERANGE);
424 #endif
425 }
426 }
427 5196 coup->gain[c][idx] = gain_cache;
428 }
429 }
430 }
431 }
432 }
433 1174 return 0;
434 }
435
436 const AACDecProc AAC_RENAME(aac_proc) = {
437 .decode_spectrum_and_dequant = AAC_RENAME(decode_spectrum_and_dequant),
438 .decode_cce = AAC_RENAME(decode_cce),
439 };
440