FFmpeg coverage


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