FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacdec_fixed.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 88 205 42.9%
Functions: 8 14 57.1%
Branches: 32 88 36.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013
3 * MIPS Technologies, Inc., California.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * AAC decoder fixed-point implementation
30 *
31 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
32 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
33 *
34 * This file is part of FFmpeg.
35 *
36 * FFmpeg is free software; you can redistribute it and/or
37 * modify it under the terms of the GNU Lesser General Public
38 * License as published by the Free Software Foundation; either
39 * version 2.1 of the License, or (at your option) any later version.
40 *
41 * FFmpeg is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 * Lesser General Public License for more details.
45 *
46 * You should have received a copy of the GNU Lesser General Public
47 * License along with FFmpeg; if not, write to the Free Software
48 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
49 */
50
51 /**
52 * @file
53 * AAC decoder
54 * @author Oded Shimon ( ods15 ods15 dyndns org )
55 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
56 *
57 * Fixed point implementation
58 * @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
59 */
60
61 #define USE_FIXED 1
62 #define TX_TYPE AV_TX_INT32_MDCT
63
64 #include "libavutil/fixed_dsp.h"
65 #include "libavutil/opt.h"
66 #include "avcodec.h"
67 #include "codec_internal.h"
68 #include "get_bits.h"
69 #include "kbdwin.h"
70 #include "sinewin_fixed_tablegen.h"
71
72 #include "aac.h"
73 #include "aacdec.h"
74 #include "aactab.h"
75 #include "aacdectab.h"
76 #include "adts_header.h"
77 #include "cbrt_data.h"
78 #include "sbr.h"
79 #include "aacsbr.h"
80 #include "mpeg4audio.h"
81 #include "profiles.h"
82 #include "libavutil/intfloat.h"
83
84 #include <math.h>
85 #include <string.h>
86
87 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_long_1024))[1024];
88 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_short_128))[128];
89 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_long_960))[960];
90 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_short_120))[120];
91
92 /* @name ltp_coef
93 * Table of the LTP coefficients
94 */
95 static const int ltp_coef_fixed[8] = {
96 Q30(0.570829), Q30(0.696616), Q30(0.813004), Q30(0.911304),
97 Q30(0.984900), Q30(1.067894), Q30(1.194601), Q30(1.369533),
98 };
99
100 /* @name tns_tmp2_map
101 * Tables of the tmp2[] arrays of LPC coefficients used for TNS.
102 * The suffix _M_N[] indicate the values of coef_compress and coef_res
103 * respectively.
104 * @{
105 */
106 static const int tns_tmp2_map_1_3[4] = {
107 Q31(0.00000000), Q31(-0.43388373), Q31(0.64278758), Q31(0.34202015),
108 };
109
110 static const int tns_tmp2_map_0_3[8] = {
111 Q31(0.00000000), Q31(-0.43388373), Q31(-0.78183150), Q31(-0.97492790),
112 Q31(0.98480773), Q31( 0.86602539), Q31( 0.64278758), Q31( 0.34202015),
113 };
114
115 static const int tns_tmp2_map_1_4[8] = {
116 Q31(0.00000000), Q31(-0.20791170), Q31(-0.40673664), Q31(-0.58778524),
117 Q31(0.67369562), Q31( 0.52643216), Q31( 0.36124167), Q31( 0.18374951),
118 };
119
120 static const int tns_tmp2_map_0_4[16] = {
121 Q31( 0.00000000), Q31(-0.20791170), Q31(-0.40673664), Q31(-0.58778524),
122 Q31(-0.74314481), Q31(-0.86602539), Q31(-0.95105654), Q31(-0.99452192),
123 Q31( 0.99573416), Q31( 0.96182561), Q31( 0.89516330), Q31( 0.79801720),
124 Q31( 0.67369562), Q31( 0.52643216), Q31( 0.36124167), Q31( 0.18374951),
125 };
126
127 static const int * const tns_tmp2_map_fixed[4] = {
128 tns_tmp2_map_0_3,
129 tns_tmp2_map_0_4,
130 tns_tmp2_map_1_3,
131 tns_tmp2_map_1_4
132 };
133 // @}
134
135 static av_always_inline void reset_predict_state(PredictorState *ps)
136 {
137 ps->r0.mant = 0;
138 ps->r0.exp = 0;
139 ps->r1.mant = 0;
140 ps->r1.exp = 0;
141 ps->cor0.mant = 0;
142 ps->cor0.exp = 0;
143 ps->cor1.mant = 0;
144 ps->cor1.exp = 0;
145 ps->var0.mant = 0x20000000;
146 ps->var0.exp = 1;
147 ps->var1.mant = 0x20000000;
148 ps->var1.exp = 1;
149 }
150
151 static const int exp2tab[4] = { Q31(1.0000000000/2), Q31(1.1892071150/2), Q31(1.4142135624/2), Q31(1.6817928305/2) }; // 2^0, 2^0.25, 2^0.5, 2^0.75
152
153 690688 static inline int *DEC_SPAIR(int *dst, unsigned idx)
154 {
155 690688 dst[0] = (idx & 15) - 4;
156 690688 dst[1] = (idx >> 4 & 15) - 4;
157
158 690688 return dst + 2;
159 }
160
161 996762 static inline int *DEC_SQUAD(int *dst, unsigned idx)
162 {
163 996762 dst[0] = (idx & 3) - 1;
164 996762 dst[1] = (idx >> 2 & 3) - 1;
165 996762 dst[2] = (idx >> 4 & 3) - 1;
166 996762 dst[3] = (idx >> 6 & 3) - 1;
167
168 996762 return dst + 4;
169 }
170
171 718254 static inline int *DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
172 {
173 718254 dst[0] = (idx & 15) * (1 - (sign & 0xFFFFFFFE));
174 718254 dst[1] = (idx >> 4 & 15) * (1 - ((sign & 1) * 2));
175
176 718254 return dst + 2;
177 }
178
179 826740 static inline int *DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
180 {
181 826740 unsigned nz = idx >> 12;
182
183 826740 dst[0] = (idx & 3) * (1 + (((int)sign >> 31) * 2));
184 826740 sign <<= nz & 1;
185 826740 nz >>= 1;
186 826740 dst[1] = (idx >> 2 & 3) * (1 + (((int)sign >> 31) * 2));
187 826740 sign <<= nz & 1;
188 826740 nz >>= 1;
189 826740 dst[2] = (idx >> 4 & 3) * (1 + (((int)sign >> 31) * 2));
190 826740 sign <<= nz & 1;
191 826740 nz >>= 1;
192 826740 dst[3] = (idx >> 6 & 3) * (1 + (((int)sign >> 31) * 2));
193
194 826740 return dst + 4;
195 }
196
197 798771 static void vector_pow43(int *coefs, int len)
198 {
199 int i, coef;
200
201
2/2
✓ Branch 0 taken 10768416 times.
✓ Branch 1 taken 798771 times.
11567187 for (i=0; i<len; i++) {
202 10768416 coef = coefs[i];
203
2/2
✓ Branch 0 taken 1879416 times.
✓ Branch 1 taken 8889000 times.
10768416 if (coef < 0)
204 1879416 coef = -(int)ff_cbrt_tab_fixed[(-coef) & 8191];
205 else
206 8889000 coef = (int)ff_cbrt_tab_fixed[ coef & 8191];
207 10768416 coefs[i] = coef;
208 }
209 798771 }
210
211 816726 static void subband_scale(int *dst, int *src, int scale, int offset, int len, void *log_context)
212 {
213
2/2
✓ Branch 0 taken 798773 times.
✓ Branch 1 taken 17953 times.
816726 int ssign = scale < 0 ? -1 : 1;
214 816726 int s = FFABS(scale);
215 unsigned int round;
216 816726 int i, out, c = exp2tab[s & 3];
217
218 816726 s = offset - (s >> 2);
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 816726 times.
816726 if (s > 31) {
221 for (i=0; i<len; i++) {
222 dst[i] = 0;
223 }
224
2/2
✓ Branch 0 taken 94860 times.
✓ Branch 1 taken 721866 times.
816726 } else if (s > 0) {
225 94860 round = 1 << (s-1);
226
2/2
✓ Branch 0 taken 1297084 times.
✓ Branch 1 taken 94860 times.
1391944 for (i=0; i<len; i++) {
227 1297084 out = (int)(((int64_t)src[i] * c) >> 32);
228 1297084 dst[i] = ((int)(out+round) >> s) * ssign;
229 }
230
1/2
✓ Branch 0 taken 721866 times.
✗ Branch 1 not taken.
721866 } else if (s > -32) {
231 721866 s = s + 32;
232 721866 round = 1U << (s-1);
233
2/2
✓ Branch 0 taken 10006532 times.
✓ Branch 1 taken 721866 times.
10728398 for (i=0; i<len; i++) {
234 10006532 out = (int)((int64_t)((int64_t)src[i] * c + round) >> s);
235 10006532 dst[i] = out * (unsigned)ssign;
236 }
237 } else {
238 av_log(log_context, AV_LOG_ERROR, "Overflow in subband_scale()\n");
239 }
240 816726 }
241
242 137931 static void noise_scale(int *coefs, int scale, int band_energy, int len)
243 {
244 137931 int s = -scale;
245 unsigned int round;
246 137931 int i, out, c = exp2tab[s & 3];
247 137931 int nlz = 0;
248
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137931 times.
137931 av_assert0(s >= 0);
250
2/2
✓ Branch 0 taken 2009279 times.
✓ Branch 1 taken 137931 times.
2147210 while (band_energy > 0x7fff) {
251 2009279 band_energy >>= 1;
252 2009279 nlz++;
253 }
254 137931 c /= band_energy;
255 137931 s = 21 + nlz - (s >> 2);
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137931 times.
137931 if (s > 31) {
258 for (i=0; i<len; i++) {
259 coefs[i] = 0;
260 }
261
2/2
✓ Branch 0 taken 73177 times.
✓ Branch 1 taken 64754 times.
137931 } else if (s >= 0) {
262
2/2
✓ Branch 0 taken 67551 times.
✓ Branch 1 taken 5626 times.
73177 round = s ? 1 << (s-1) : 0;
263
2/2
✓ Branch 0 taken 2387284 times.
✓ Branch 1 taken 73177 times.
2460461 for (i=0; i<len; i++) {
264 2387284 out = (int)(((int64_t)coefs[i] * c) >> 32);
265 2387284 coefs[i] = -((int)(out+round) >> s);
266 }
267 }
268 else {
269 64754 s = s + 32;
270
1/2
✓ Branch 0 taken 64754 times.
✗ Branch 1 not taken.
64754 if (s > 0) {
271 64754 round = 1 << (s-1);
272
2/2
✓ Branch 0 taken 496192 times.
✓ Branch 1 taken 64754 times.
560946 for (i=0; i<len; i++) {
273 496192 out = (int)((int64_t)((int64_t)coefs[i] * c + round) >> s);
274 496192 coefs[i] = -out;
275 }
276 } else {
277 for (i=0; i<len; i++)
278 coefs[i] = -(int64_t)coefs[i] * c * (1 << -s);
279 }
280 }
281 137931 }
282
283 static av_always_inline SoftFloat flt16_round(SoftFloat pf)
284 {
285 SoftFloat tmp;
286 int s;
287
288 tmp.exp = pf.exp;
289 s = pf.mant >> 31;
290 tmp.mant = (pf.mant ^ s) - s;
291 tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
292 tmp.mant = (tmp.mant ^ s) - s;
293
294 return tmp;
295 }
296
297 static av_always_inline SoftFloat flt16_even(SoftFloat pf)
298 {
299 SoftFloat tmp;
300 int s;
301
302 tmp.exp = pf.exp;
303 s = pf.mant >> 31;
304 tmp.mant = (pf.mant ^ s) - s;
305 tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
306 tmp.mant = (tmp.mant ^ s) - s;
307
308 return tmp;
309 }
310
311 static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
312 {
313 SoftFloat pun;
314 int s;
315
316 pun.exp = pf.exp;
317 s = pf.mant >> 31;
318 pun.mant = (pf.mant ^ s) - s;
319 pun.mant = pun.mant & 0xFFC00000U;
320 pun.mant = (pun.mant ^ s) - s;
321
322 return pun;
323 }
324
325 static av_always_inline void predict(PredictorState *ps, int *coef,
326 int output_enable)
327 {
328 const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
329 const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
330 SoftFloat e0, e1;
331 SoftFloat pv;
332 SoftFloat k1, k2;
333 SoftFloat r0 = ps->r0, r1 = ps->r1;
334 SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
335 SoftFloat var0 = ps->var0, var1 = ps->var1;
336 SoftFloat tmp;
337
338 if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
339 k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
340 }
341 else {
342 k1.mant = 0;
343 k1.exp = 0;
344 }
345
346 if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
347 k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
348 }
349 else {
350 k2.mant = 0;
351 k2.exp = 0;
352 }
353
354 tmp = av_mul_sf(k1, r0);
355 pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
356 if (output_enable) {
357 int shift = 28 - pv.exp;
358
359 if (shift < 31) {
360 if (shift > 0) {
361 *coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
362 } else
363 *coef += (unsigned)pv.mant << -shift;
364 }
365 }
366
367 e0 = av_int2sf(*coef, 2);
368 e1 = av_sub_sf(e0, tmp);
369
370 ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
371 tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
372 tmp.exp--;
373 ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
374 ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
375 tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
376 tmp.exp--;
377 ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
378
379 ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
380 ps->r0 = flt16_trunc(av_mul_sf(a, e0));
381 }
382
383
384 static const int cce_scale_fixed[8] = {
385 Q30(1.0), //2^(0/8)
386 Q30(1.0905077327), //2^(1/8)
387 Q30(1.1892071150), //2^(2/8)
388 Q30(1.2968395547), //2^(3/8)
389 Q30(1.4142135624), //2^(4/8)
390 Q30(1.5422108254), //2^(5/8)
391 Q30(1.6817928305), //2^(6/8)
392 Q30(1.8340080864), //2^(7/8)
393 };
394
395 /**
396 * Apply dependent channel coupling (applied before IMDCT).
397 *
398 * @param index index into coupling gain array
399 */
400 static void apply_dependent_coupling_fixed(AACDecContext *ac,
401 SingleChannelElement *target,
402 ChannelElement *cce, int index)
403 {
404 IndividualChannelStream *ics = &cce->ch[0].ics;
405 const uint16_t *offsets = ics->swb_offset;
406 int *dest = target->coeffs;
407 const int *src = cce->ch[0].coeffs;
408 int g, i, group, k, idx = 0;
409 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
410 av_log(ac->avctx, AV_LOG_ERROR,
411 "Dependent coupling is not supported together with LTP\n");
412 return;
413 }
414 for (g = 0; g < ics->num_window_groups; g++) {
415 for (i = 0; i < ics->max_sfb; i++, idx++) {
416 if (cce->ch[0].band_type[idx] != ZERO_BT) {
417 const int gain = cce->coup.gain[index][idx];
418 int shift, round, c, tmp;
419
420 if (gain < 0) {
421 c = -cce_scale_fixed[-gain & 7];
422 shift = (-gain-1024) >> 3;
423 }
424 else {
425 c = cce_scale_fixed[gain & 7];
426 shift = (gain-1024) >> 3;
427 }
428
429 if (shift < -31) {
430 // Nothing to do
431 } else if (shift < 0) {
432 shift = -shift;
433 round = 1 << (shift - 1);
434
435 for (group = 0; group < ics->group_len[g]; group++) {
436 for (k = offsets[i]; k < offsets[i + 1]; k++) {
437 tmp = (int)(((int64_t)src[group * 128 + k] * c + \
438 (int64_t)0x1000000000) >> 37);
439 dest[group * 128 + k] += (tmp + (int64_t)round) >> shift;
440 }
441 }
442 }
443 else {
444 for (group = 0; group < ics->group_len[g]; group++) {
445 for (k = offsets[i]; k < offsets[i + 1]; k++) {
446 tmp = (int)(((int64_t)src[group * 128 + k] * c + \
447 (int64_t)0x1000000000) >> 37);
448 dest[group * 128 + k] += tmp * (1U << shift);
449 }
450 }
451 }
452 }
453 }
454 dest += ics->group_len[g] * 128;
455 src += ics->group_len[g] * 128;
456 }
457 }
458
459 /**
460 * Apply independent channel coupling (applied after IMDCT).
461 *
462 * @param index index into coupling gain array
463 */
464 650 static void apply_independent_coupling_fixed(AACDecContext *ac,
465 SingleChannelElement *target,
466 ChannelElement *cce, int index)
467 {
468 int i, c, shift, round, tmp;
469 650 const int gain = cce->coup.gain[index][0];
470 650 const int *src = cce->ch[0].ret;
471 650 unsigned int *dest = target->ret;
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
473
474 650 c = cce_scale_fixed[gain & 7];
475 650 shift = (gain-1024) >> 3;
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 if (shift < -31) {
477 return;
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 } else if (shift < 0) {
479 shift = -shift;
480 round = 1 << (shift - 1);
481
482 for (i = 0; i < len; i++) {
483 tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
484 dest[i] += (tmp + round) >> shift;
485 }
486 }
487 else {
488
2/2
✓ Branch 0 taken 665600 times.
✓ Branch 1 taken 650 times.
666250 for (i = 0; i < len; i++) {
489 665600 tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
490 665600 dest[i] += tmp * (1U << shift);
491 }
492 }
493 }
494
495 #include "aacdec_template.c"
496
497 const FFCodec ff_aac_fixed_decoder = {
498 .p.name = "aac_fixed",
499 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
500 .p.type = AVMEDIA_TYPE_AUDIO,
501 .p.id = AV_CODEC_ID_AAC,
502 .priv_data_size = sizeof(AACDecContext),
503 .init = aac_decode_init,
504 .close = aac_decode_close,
505 FF_CODEC_DECODE_CB(aac_decode_frame),
506 .p.sample_fmts = (const enum AVSampleFormat[]) {
507 AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
508 },
509 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
510 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
511 .p.ch_layouts = ff_aac_ch_layout,
512 .p.priv_class = &aac_decoder_class,
513 .p.profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
514 .flush = flush,
515 };
516