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>
6 * This file is part of FFmpeg.
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.
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.
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
22 * Note: Rounding-to-nearest used unless otherwise stated
30 #include "libavutil/attributes.h"
31 #include "libavutil/intfloat.h"
34 static SoftFloat sbr_sum_square_c(int (*x)[2], int n)
37 uint64_t accu = 0, round;
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];
58 while (u < 0x80000000U) {
65 round = 1ULL << (nz-1);
66 u = ((accu + round) >> nz);
68 ret = av_int2sf(u, 15 - nz);
73 static void sbr_neg_odd_64_c(int *x)
76 for (i = 1; i < 64; i += 2)
80 static void sbr_qmf_pre_shuffle_c(int *z)
85 for (k = 1; k < 32; k++) {
86 z[64+2*k ] = -z[64 - k];
87 z[64+2*k+1] = z[ k + 1];
91 static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
94 for (k = 0; k < 32; k++) {
100 static void sbr_qmf_deint_neg_c(int *v, const int *src)
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;
109 static av_always_inline SoftFloat autocorr_calc(int64_t accu)
113 int i = (int)(accu >> 32);
118 while (FFABS(i) < 0x40000000) {
125 round = 1U << (nz-1);
126 mant = (int)((accu + round) >> nz);
127 mant = (mant + 0x40)>>7;
130 return av_int2sf(mant, 30 - expo);
133 static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
136 int64_t real_sum, imag_sum;
137 int64_t accu_re = 0, accu_im = 0;
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];
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];
155 phi[2-lag][1][0] = autocorr_calc(accu_re);
156 phi[2-lag][1][1] = autocorr_calc(accu_im);
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];
166 phi[0][0][0] = autocorr_calc(accu_re);
167 phi[0][0][1] = autocorr_calc(accu_im);
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];
175 accu_re += (int64_t)x[ 0][0] * x[ 0][0];
176 accu_re += (int64_t)x[ 0][1] * x[ 0][1];
178 phi[2][1][0] = autocorr_calc(accu_re);
181 accu_re += (int64_t)x[38][0] * x[38][0];
182 accu_re += (int64_t)x[38][1] * x[38][1];
184 phi[1][0][0] = autocorr_calc(accu_re);
188 static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
190 autocorrelate(x, phi, 0);
191 autocorrelate(x, phi, 1);
192 autocorrelate(x, phi, 2);
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)
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);
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);
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);
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)
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));
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));
247 static av_always_inline void sbr_hf_apply_noise(int (*Y)[2],
248 const SoftFloat *s_m,
249 const SoftFloat *q_filt,
257 for (m = 0; m < m_max; m++) {
260 noise = (noise + 1) & 0x1ff;
264 shift = 22 - s_m[m].exp;
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;
271 int shift, round, tmp;
274 shift = 22 - q_filt[m].exp;
276 round = 1 << (shift-1);
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;
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;
289 phi_sign1 = -phi_sign1;
293 #include "sbrdsp_template.c"