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