FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aacdec_fixed.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 88 203 43.3%
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 FFT_FLOAT 0
62 #define USE_FIXED 1
63
64 #include "libavutil/fixed_dsp.h"
65 #include "libavutil/opt.h"
66 #include "avcodec.h"
67 #include "internal.h"
68 #include "get_bits.h"
69 #include "fft.h"
70 #include "lpc.h"
71 #include "kbdwin.h"
72 #include "sinewin_fixed_tablegen.h"
73
74 #include "aac.h"
75 #include "aactab.h"
76 #include "aacdectab.h"
77 #include "adts_header.h"
78 #include "cbrt_data.h"
79 #include "sbr.h"
80 #include "aacsbr.h"
81 #include "mpeg4audio.h"
82 #include "profiles.h"
83 #include "libavutil/intfloat.h"
84
85 #include <math.h>
86 #include <string.h>
87
88 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_long_1024))[1024];
89 DECLARE_ALIGNED(32, static int, AAC_RENAME2(aac_kbd_short_128))[128];
90
91 static av_always_inline void reset_predict_state(PredictorState *ps)
92 {
93 ps->r0.mant = 0;
94 ps->r0.exp = 0;
95 ps->r1.mant = 0;
96 ps->r1.exp = 0;
97 ps->cor0.mant = 0;
98 ps->cor0.exp = 0;
99 ps->cor1.mant = 0;
100 ps->cor1.exp = 0;
101 ps->var0.mant = 0x20000000;
102 ps->var0.exp = 1;
103 ps->var1.mant = 0x20000000;
104 ps->var1.exp = 1;
105 }
106
107 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
108
109 672272 static inline int *DEC_SPAIR(int *dst, unsigned idx)
110 {
111 672272 dst[0] = (idx & 15) - 4;
112 672272 dst[1] = (idx >> 4 & 15) - 4;
113
114 672272 return dst + 2;
115 }
116
117 978157 static inline int *DEC_SQUAD(int *dst, unsigned idx)
118 {
119 978157 dst[0] = (idx & 3) - 1;
120 978157 dst[1] = (idx >> 2 & 3) - 1;
121 978157 dst[2] = (idx >> 4 & 3) - 1;
122 978157 dst[3] = (idx >> 6 & 3) - 1;
123
124 978157 return dst + 4;
125 }
126
127 703742 static inline int *DEC_UPAIR(int *dst, unsigned idx, unsigned sign)
128 {
129 703742 dst[0] = (idx & 15) * (1 - (sign & 0xFFFFFFFE));
130 703742 dst[1] = (idx >> 4 & 15) * (1 - ((sign & 1) * 2));
131
132 703742 return dst + 2;
133 }
134
135 816691 static inline int *DEC_UQUAD(int *dst, unsigned idx, unsigned sign)
136 {
137 816691 unsigned nz = idx >> 12;
138
139 816691 dst[0] = (idx & 3) * (1 + (((int)sign >> 31) * 2));
140 816691 sign <<= nz & 1;
141 816691 nz >>= 1;
142 816691 dst[1] = (idx >> 2 & 3) * (1 + (((int)sign >> 31) * 2));
143 816691 sign <<= nz & 1;
144 816691 nz >>= 1;
145 816691 dst[2] = (idx >> 4 & 3) * (1 + (((int)sign >> 31) * 2));
146 816691 sign <<= nz & 1;
147 816691 nz >>= 1;
148 816691 dst[3] = (idx >> 6 & 3) * (1 + (((int)sign >> 31) * 2));
149
150 816691 return dst + 4;
151 }
152
153 787858 static void vector_pow43(int *coefs, int len)
154 {
155 int i, coef;
156
157
2/2
✓ Branch 0 taken 10580336 times.
✓ Branch 1 taken 787858 times.
11368194 for (i=0; i<len; i++) {
158 10580336 coef = coefs[i];
159
2/2
✓ Branch 0 taken 1834713 times.
✓ Branch 1 taken 8745623 times.
10580336 if (coef < 0)
160 1834713 coef = -(int)ff_cbrt_tab_fixed[(-coef) & 8191];
161 else
162 8745623 coef = (int)ff_cbrt_tab_fixed[ coef & 8191];
163 10580336 coefs[i] = coef;
164 }
165 787858 }
166
167 805813 static void subband_scale(int *dst, int *src, int scale, int offset, int len, void *log_context)
168 {
169
2/2
✓ Branch 0 taken 787860 times.
✓ Branch 1 taken 17953 times.
805813 int ssign = scale < 0 ? -1 : 1;
170 805813 int s = FFABS(scale);
171 unsigned int round;
172 805813 int i, out, c = exp2tab[s & 3];
173
174 805813 s = offset - (s >> 2);
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 805813 times.
805813 if (s > 31) {
177 for (i=0; i<len; i++) {
178 dst[i] = 0;
179 }
180
2/2
✓ Branch 0 taken 94338 times.
✓ Branch 1 taken 711475 times.
805813 } else if (s > 0) {
181 94338 round = 1 << (s-1);
182
2/2
✓ Branch 0 taken 1290900 times.
✓ Branch 1 taken 94338 times.
1385238 for (i=0; i<len; i++) {
183 1290900 out = (int)(((int64_t)src[i] * c) >> 32);
184 1290900 dst[i] = ((int)(out+round) >> s) * ssign;
185 }
186
1/2
✓ Branch 0 taken 711475 times.
✗ Branch 1 not taken.
711475 } else if (s > -32) {
187 711475 s = s + 32;
188 711475 round = 1U << (s-1);
189
2/2
✓ Branch 0 taken 9824636 times.
✓ Branch 1 taken 711475 times.
10536111 for (i=0; i<len; i++) {
190 9824636 out = (int)((int64_t)((int64_t)src[i] * c + round) >> s);
191 9824636 dst[i] = out * (unsigned)ssign;
192 }
193 } else {
194 av_log(log_context, AV_LOG_ERROR, "Overflow in subband_scale()\n");
195 }
196 805813 }
197
198 137931 static void noise_scale(int *coefs, int scale, int band_energy, int len)
199 {
200 137931 int s = -scale;
201 unsigned int round;
202 137931 int i, out, c = exp2tab[s & 3];
203 137931 int nlz = 0;
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137931 times.
137931 av_assert0(s >= 0);
206
2/2
✓ Branch 0 taken 2009279 times.
✓ Branch 1 taken 137931 times.
2147210 while (band_energy > 0x7fff) {
207 2009279 band_energy >>= 1;
208 2009279 nlz++;
209 }
210 137931 c /= band_energy;
211 137931 s = 21 + nlz - (s >> 2);
212
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137931 times.
137931 if (s > 31) {
214 for (i=0; i<len; i++) {
215 coefs[i] = 0;
216 }
217
2/2
✓ Branch 0 taken 73177 times.
✓ Branch 1 taken 64754 times.
137931 } else if (s >= 0) {
218
2/2
✓ Branch 0 taken 67551 times.
✓ Branch 1 taken 5626 times.
73177 round = s ? 1 << (s-1) : 0;
219
2/2
✓ Branch 0 taken 2387284 times.
✓ Branch 1 taken 73177 times.
2460461 for (i=0; i<len; i++) {
220 2387284 out = (int)(((int64_t)coefs[i] * c) >> 32);
221 2387284 coefs[i] = -((int)(out+round) >> s);
222 }
223 }
224 else {
225 64754 s = s + 32;
226
1/2
✓ Branch 0 taken 64754 times.
✗ Branch 1 not taken.
64754 if (s > 0) {
227 64754 round = 1 << (s-1);
228
2/2
✓ Branch 0 taken 496192 times.
✓ Branch 1 taken 64754 times.
560946 for (i=0; i<len; i++) {
229 496192 out = (int)((int64_t)((int64_t)coefs[i] * c + round) >> s);
230 496192 coefs[i] = -out;
231 }
232 } else {
233 for (i=0; i<len; i++)
234 coefs[i] = -(int64_t)coefs[i] * c * (1 << -s);
235 }
236 }
237 137931 }
238
239 static av_always_inline SoftFloat flt16_round(SoftFloat pf)
240 {
241 SoftFloat tmp;
242 int s;
243
244 tmp.exp = pf.exp;
245 s = pf.mant >> 31;
246 tmp.mant = (pf.mant ^ s) - s;
247 tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
248 tmp.mant = (tmp.mant ^ s) - s;
249
250 return tmp;
251 }
252
253 static av_always_inline SoftFloat flt16_even(SoftFloat pf)
254 {
255 SoftFloat tmp;
256 int s;
257
258 tmp.exp = pf.exp;
259 s = pf.mant >> 31;
260 tmp.mant = (pf.mant ^ s) - s;
261 tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
262 tmp.mant = (tmp.mant ^ s) - s;
263
264 return tmp;
265 }
266
267 static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
268 {
269 SoftFloat pun;
270 int s;
271
272 pun.exp = pf.exp;
273 s = pf.mant >> 31;
274 pun.mant = (pf.mant ^ s) - s;
275 pun.mant = pun.mant & 0xFFC00000U;
276 pun.mant = (pun.mant ^ s) - s;
277
278 return pun;
279 }
280
281 static av_always_inline void predict(PredictorState *ps, int *coef,
282 int output_enable)
283 {
284 const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
285 const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
286 SoftFloat e0, e1;
287 SoftFloat pv;
288 SoftFloat k1, k2;
289 SoftFloat r0 = ps->r0, r1 = ps->r1;
290 SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
291 SoftFloat var0 = ps->var0, var1 = ps->var1;
292 SoftFloat tmp;
293
294 if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
295 k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
296 }
297 else {
298 k1.mant = 0;
299 k1.exp = 0;
300 }
301
302 if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
303 k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
304 }
305 else {
306 k2.mant = 0;
307 k2.exp = 0;
308 }
309
310 tmp = av_mul_sf(k1, r0);
311 pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
312 if (output_enable) {
313 int shift = 28 - pv.exp;
314
315 if (shift < 31) {
316 if (shift > 0) {
317 *coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
318 } else
319 *coef += (unsigned)pv.mant << -shift;
320 }
321 }
322
323 e0 = av_int2sf(*coef, 2);
324 e1 = av_sub_sf(e0, tmp);
325
326 ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
327 tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
328 tmp.exp--;
329 ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
330 ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
331 tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
332 tmp.exp--;
333 ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
334
335 ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
336 ps->r0 = flt16_trunc(av_mul_sf(a, e0));
337 }
338
339
340 static const int cce_scale_fixed[8] = {
341 Q30(1.0), //2^(0/8)
342 Q30(1.0905077327), //2^(1/8)
343 Q30(1.1892071150), //2^(2/8)
344 Q30(1.2968395547), //2^(3/8)
345 Q30(1.4142135624), //2^(4/8)
346 Q30(1.5422108254), //2^(5/8)
347 Q30(1.6817928305), //2^(6/8)
348 Q30(1.8340080864), //2^(7/8)
349 };
350
351 /**
352 * Apply dependent channel coupling (applied before IMDCT).
353 *
354 * @param index index into coupling gain array
355 */
356 static void apply_dependent_coupling_fixed(AACContext *ac,
357 SingleChannelElement *target,
358 ChannelElement *cce, int index)
359 {
360 IndividualChannelStream *ics = &cce->ch[0].ics;
361 const uint16_t *offsets = ics->swb_offset;
362 int *dest = target->coeffs;
363 const int *src = cce->ch[0].coeffs;
364 int g, i, group, k, idx = 0;
365 if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
366 av_log(ac->avctx, AV_LOG_ERROR,
367 "Dependent coupling is not supported together with LTP\n");
368 return;
369 }
370 for (g = 0; g < ics->num_window_groups; g++) {
371 for (i = 0; i < ics->max_sfb; i++, idx++) {
372 if (cce->ch[0].band_type[idx] != ZERO_BT) {
373 const int gain = cce->coup.gain[index][idx];
374 int shift, round, c, tmp;
375
376 if (gain < 0) {
377 c = -cce_scale_fixed[-gain & 7];
378 shift = (-gain-1024) >> 3;
379 }
380 else {
381 c = cce_scale_fixed[gain & 7];
382 shift = (gain-1024) >> 3;
383 }
384
385 if (shift < -31) {
386 // Nothing to do
387 } else if (shift < 0) {
388 shift = -shift;
389 round = 1 << (shift - 1);
390
391 for (group = 0; group < ics->group_len[g]; group++) {
392 for (k = offsets[i]; k < offsets[i + 1]; k++) {
393 tmp = (int)(((int64_t)src[group * 128 + k] * c + \
394 (int64_t)0x1000000000) >> 37);
395 dest[group * 128 + k] += (tmp + (int64_t)round) >> shift;
396 }
397 }
398 }
399 else {
400 for (group = 0; group < ics->group_len[g]; group++) {
401 for (k = offsets[i]; k < offsets[i + 1]; k++) {
402 tmp = (int)(((int64_t)src[group * 128 + k] * c + \
403 (int64_t)0x1000000000) >> 37);
404 dest[group * 128 + k] += tmp * (1U << shift);
405 }
406 }
407 }
408 }
409 }
410 dest += ics->group_len[g] * 128;
411 src += ics->group_len[g] * 128;
412 }
413 }
414
415 /**
416 * Apply independent channel coupling (applied after IMDCT).
417 *
418 * @param index index into coupling gain array
419 */
420 650 static void apply_independent_coupling_fixed(AACContext *ac,
421 SingleChannelElement *target,
422 ChannelElement *cce, int index)
423 {
424 int i, c, shift, round, tmp;
425 650 const int gain = cce->coup.gain[index][0];
426 650 const int *src = cce->ch[0].ret;
427 650 unsigned int *dest = target->ret;
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
429
430 650 c = cce_scale_fixed[gain & 7];
431 650 shift = (gain-1024) >> 3;
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 if (shift < -31) {
433 return;
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 } else if (shift < 0) {
435 shift = -shift;
436 round = 1 << (shift - 1);
437
438 for (i = 0; i < len; i++) {
439 tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
440 dest[i] += (tmp + round) >> shift;
441 }
442 }
443 else {
444
2/2
✓ Branch 0 taken 665600 times.
✓ Branch 1 taken 650 times.
666250 for (i = 0; i < len; i++) {
445 665600 tmp = (int)(((int64_t)src[i] * c + (int64_t)0x1000000000) >> 37);
446 665600 dest[i] += tmp * (1U << shift);
447 }
448 }
449 }
450
451 #include "aacdec_template.c"
452
453 const AVCodec ff_aac_fixed_decoder = {
454 .name = "aac_fixed",
455 .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
456 .type = AVMEDIA_TYPE_AUDIO,
457 .id = AV_CODEC_ID_AAC,
458 .priv_data_size = sizeof(AACContext),
459 .init = aac_decode_init,
460 .close = aac_decode_close,
461 .decode = aac_decode_frame,
462 .sample_fmts = (const enum AVSampleFormat[]) {
463 AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
464 },
465 .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
466 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
467 .channel_layouts = aac_channel_layout,
468 .profiles = NULL_IF_CONFIG_SMALL(ff_aac_profiles),
469 .flush = flush,
470 };
471