]> git.sesse.net Git - ffmpeg/blob - libavcodec/sbrdsp_fixed.c
avcodec/aacdec_fixed: Fix various integer overflows
[ffmpeg] / libavcodec / sbrdsp_fixed.c
1 /*
2  * AAC Spectral Band Replication decoding functions
3  * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
4  * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * Note: Rounding-to-nearest used unless otherwise stated
23  *
24  */
25
26 #define USE_FIXED 1
27
28 #include "aac.h"
29 #include "config.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/intfloat.h"
32 #include "sbrdsp.h"
33
34 static SoftFloat sbr_sum_square_c(int (*x)[2], int n)
35 {
36     SoftFloat ret;
37     uint64_t accu = 0, round;
38     int i, nz;
39     unsigned u;
40
41     for (i = 0; i < n; i += 2) {
42         // Larger values are inavlid and could cause overflows of accu.
43         av_assert2(FFABS(x[i + 0][0]) >> 29 == 0);
44         accu += (int64_t)x[i + 0][0] * x[i + 0][0];
45         av_assert2(FFABS(x[i + 0][1]) >> 29 == 0);
46         accu += (int64_t)x[i + 0][1] * x[i + 0][1];
47         av_assert2(FFABS(x[i + 1][0]) >> 29 == 0);
48         accu += (int64_t)x[i + 1][0] * x[i + 1][0];
49         av_assert2(FFABS(x[i + 1][1]) >> 29 == 0);
50         accu += (int64_t)x[i + 1][1] * x[i + 1][1];
51     }
52
53     u = accu >> 32;
54     if (u == 0) {
55         nz = 1;
56     } else {
57         nz = -1;
58         while (u < 0x80000000U) {
59             u <<= 1;
60             nz++;
61         }
62         nz = 32 - nz;
63     }
64
65     round = 1ULL << (nz-1);
66     u = ((accu + round) >> nz);
67     u >>= 1;
68     ret = av_int2sf(u, 15 - nz);
69
70     return ret;
71 }
72
73 static void sbr_neg_odd_64_c(int *x)
74 {
75     int i;
76     for (i = 1; i < 64; i += 2)
77         x[i] = -x[i];
78 }
79
80 static void sbr_qmf_pre_shuffle_c(int *z)
81 {
82     int k;
83     z[64] = z[0];
84     z[65] = z[1];
85     for (k = 1; k < 32; k++) {
86         z[64+2*k  ] = -z[64 - k];
87         z[64+2*k+1] =  z[ k + 1];
88     }
89 }
90
91 static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
92 {
93     int k;
94     for (k = 0; k < 32; k++) {
95         W[k][0] = -z[63-k];
96         W[k][1] = z[k];
97     }
98 }
99
100 static void sbr_qmf_deint_neg_c(int *v, const int *src)
101 {
102     int i;
103     for (i = 0; i < 32; i++) {
104         v[     i] = ( src[63 - 2*i    ] + 0x10) >> 5;
105         v[63 - i] = (-src[63 - 2*i - 1] + 0x10) >> 5;
106     }
107 }
108
109 static av_always_inline SoftFloat autocorr_calc(int64_t accu)
110 {
111         int nz, mant, expo;
112         unsigned round;
113         int i = (int)(accu >> 32);
114         if (i == 0) {
115             nz = 1;
116         } else {
117             nz = 0;
118             while (FFABS(i) < 0x40000000) {
119                 i <<= 1;
120                 nz++;
121             }
122             nz = 32-nz;
123         }
124
125         round = 1U << (nz-1);
126         mant = (int)((accu + round) >> nz);
127         mant = (mant + 0x40)>>7;
128         mant <<= 6;
129         expo = nz + 15;
130         return av_int2sf(mant, 30 - expo);
131 }
132
133 static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
134 {
135     int i;
136     int64_t real_sum, imag_sum;
137     int64_t accu_re = 0, accu_im = 0;
138
139     if (lag) {
140         for (i = 1; i < 38; i++) {
141             accu_re += (int64_t)x[i][0] * x[i+lag][0];
142             accu_re += (int64_t)x[i][1] * x[i+lag][1];
143             accu_im += (int64_t)x[i][0] * x[i+lag][1];
144             accu_im -= (int64_t)x[i][1] * x[i+lag][0];
145         }
146
147         real_sum = accu_re;
148         imag_sum = accu_im;
149
150         accu_re += (int64_t)x[ 0][0] * x[lag][0];
151         accu_re += (int64_t)x[ 0][1] * x[lag][1];
152         accu_im += (int64_t)x[ 0][0] * x[lag][1];
153         accu_im -= (int64_t)x[ 0][1] * x[lag][0];
154
155         phi[2-lag][1][0] = autocorr_calc(accu_re);
156         phi[2-lag][1][1] = autocorr_calc(accu_im);
157
158         if (lag == 1) {
159             accu_re = real_sum;
160             accu_im = imag_sum;
161             accu_re += (int64_t)x[38][0] * x[39][0];
162             accu_re += (int64_t)x[38][1] * x[39][1];
163             accu_im += (int64_t)x[38][0] * x[39][1];
164             accu_im -= (int64_t)x[38][1] * x[39][0];
165
166             phi[0][0][0] = autocorr_calc(accu_re);
167             phi[0][0][1] = autocorr_calc(accu_im);
168         }
169     } else {
170         for (i = 1; i < 38; i++) {
171             accu_re += (int64_t)x[i][0] * x[i][0];
172             accu_re += (int64_t)x[i][1] * x[i][1];
173         }
174         real_sum = accu_re;
175         accu_re += (int64_t)x[ 0][0] * x[ 0][0];
176         accu_re += (int64_t)x[ 0][1] * x[ 0][1];
177
178         phi[2][1][0] = autocorr_calc(accu_re);
179
180         accu_re = real_sum;
181         accu_re += (int64_t)x[38][0] * x[38][0];
182         accu_re += (int64_t)x[38][1] * x[38][1];
183
184         phi[1][0][0] = autocorr_calc(accu_re);
185     }
186 }
187
188 static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
189 {
190     autocorrelate(x, phi, 0);
191     autocorrelate(x, phi, 1);
192     autocorrelate(x, phi, 2);
193 }
194
195 static void sbr_hf_gen_c(int (*X_high)[2], const int (*X_low)[2],
196                        const int alpha0[2], const int alpha1[2],
197                        int bw, int start, int end)
198 {
199     int alpha[4];
200     int i;
201     int64_t accu;
202
203     accu = (int64_t)alpha0[0] * bw;
204     alpha[2] = (int)((accu + 0x40000000) >> 31);
205     accu = (int64_t)alpha0[1] * bw;
206     alpha[3] = (int)((accu + 0x40000000) >> 31);
207     accu = (int64_t)bw * bw;
208     bw = (int)((accu + 0x40000000) >> 31);
209     accu = (int64_t)alpha1[0] * bw;
210     alpha[0] = (int)((accu + 0x40000000) >> 31);
211     accu = (int64_t)alpha1[1] * bw;
212     alpha[1] = (int)((accu + 0x40000000) >> 31);
213
214     for (i = start; i < end; i++) {
215         accu  = (int64_t)X_low[i][0] * 0x20000000;
216         accu += (int64_t)X_low[i - 2][0] * alpha[0];
217         accu -= (int64_t)X_low[i - 2][1] * alpha[1];
218         accu += (int64_t)X_low[i - 1][0] * alpha[2];
219         accu -= (int64_t)X_low[i - 1][1] * alpha[3];
220         X_high[i][0] = (int)((accu + 0x10000000) >> 29);
221
222         accu  = (int64_t)X_low[i][1] * 0x20000000;
223         accu += (int64_t)X_low[i - 2][1] * alpha[0];
224         accu += (int64_t)X_low[i - 2][0] * alpha[1];
225         accu += (int64_t)X_low[i - 1][1] * alpha[2];
226         accu += (int64_t)X_low[i - 1][0] * alpha[3];
227         X_high[i][1] = (int)((accu + 0x10000000) >> 29);
228     }
229 }
230
231 static void sbr_hf_g_filt_c(int (*Y)[2], const int (*X_high)[40][2],
232                           const SoftFloat *g_filt, int m_max, intptr_t ixh)
233 {
234     int m, r;
235     int64_t accu;
236
237     for (m = 0; m < m_max; m++) {
238         r = 1 << (22-g_filt[m].exp);
239         accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
240         Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
241
242         accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
243         Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
244     }
245 }
246
247 static av_always_inline void sbr_hf_apply_noise(int (*Y)[2],
248                                                 const SoftFloat *s_m,
249                                                 const SoftFloat *q_filt,
250                                                 int noise,
251                                                 int phi_sign0,
252                                                 int phi_sign1,
253                                                 int m_max)
254 {
255     int m;
256
257     for (m = 0; m < m_max; m++) {
258         int y0 = Y[m][0];
259         int y1 = Y[m][1];
260         noise = (noise + 1) & 0x1ff;
261         if (s_m[m].mant) {
262             int shift, round;
263
264             shift = 22 - s_m[m].exp;
265             if (shift < 30) {
266                 round = 1 << (shift-1);
267                 y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
268                 y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
269             }
270         } else {
271             int shift, round, tmp;
272             int64_t accu;
273
274             shift = 22 - q_filt[m].exp;
275             if (shift < 30) {
276                 round = 1 << (shift-1);
277
278                 accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
279                 tmp = (int)((accu + 0x40000000) >> 31);
280                 y0 += (tmp + round) >> shift;
281
282                 accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
283                 tmp = (int)((accu + 0x40000000) >> 31);
284                 y1 += (tmp + round) >> shift;
285             }
286         }
287         Y[m][0] = y0;
288         Y[m][1] = y1;
289         phi_sign1 = -phi_sign1;
290     }
291 }
292
293 #include "sbrdsp_template.c"