00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdint.h>
00022
00023 #include "libavutil/mem.h"
00024 #include "dct32.h"
00025 #include "mathops.h"
00026 #include "mpegaudiodsp.h"
00027 #include "mpegaudio.h"
00028
00029 #if CONFIG_FLOAT
00030 #define RENAME(n) n##_float
00031
00032 static inline float round_sample(float *sum)
00033 {
00034 float sum1=*sum;
00035 *sum = 0;
00036 return sum1;
00037 }
00038
00039 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
00040 #define MULS(ra, rb) ((ra)*(rb))
00041 #define MULH3(x, y, s) ((s)*(y)*(x))
00042 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
00043 #define MULLx(x, y, s) ((y)*(x))
00044 #define FIXHR(x) ((float)(x))
00045 #define FIXR(x) ((float)(x))
00046 #define SHR(a,b) ((a)*(1.0f/(1<<(b))))
00047
00048 #else
00049
00050 #define RENAME(n) n##_fixed
00051 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
00052
00053 static inline int round_sample(int64_t *sum)
00054 {
00055 int sum1;
00056 sum1 = (int)((*sum) >> OUT_SHIFT);
00057 *sum &= (1<<OUT_SHIFT)-1;
00058 return av_clip_int16(sum1);
00059 }
00060
00061 # define MULS(ra, rb) MUL64(ra, rb)
00062 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
00063 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
00064 # define MULH3(x, y, s) MULH((s)*(x), y)
00065 # define MULLx(x, y, s) MULL(x,y,s)
00066 # define SHR(a,b) ((a)>>(b))
00067 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
00068 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
00069 #endif
00070
00075 DECLARE_ALIGNED(16, INTFLOAT, RENAME(ff_mdct_win))[8][MDCT_BUF_SIZE];
00076
00077 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
00078
00079 #define SUM8(op, sum, w, p) \
00080 { \
00081 op(sum, (w)[0 * 64], (p)[0 * 64]); \
00082 op(sum, (w)[1 * 64], (p)[1 * 64]); \
00083 op(sum, (w)[2 * 64], (p)[2 * 64]); \
00084 op(sum, (w)[3 * 64], (p)[3 * 64]); \
00085 op(sum, (w)[4 * 64], (p)[4 * 64]); \
00086 op(sum, (w)[5 * 64], (p)[5 * 64]); \
00087 op(sum, (w)[6 * 64], (p)[6 * 64]); \
00088 op(sum, (w)[7 * 64], (p)[7 * 64]); \
00089 }
00090
00091 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
00092 { \
00093 INTFLOAT tmp;\
00094 tmp = p[0 * 64];\
00095 op1(sum1, (w1)[0 * 64], tmp);\
00096 op2(sum2, (w2)[0 * 64], tmp);\
00097 tmp = p[1 * 64];\
00098 op1(sum1, (w1)[1 * 64], tmp);\
00099 op2(sum2, (w2)[1 * 64], tmp);\
00100 tmp = p[2 * 64];\
00101 op1(sum1, (w1)[2 * 64], tmp);\
00102 op2(sum2, (w2)[2 * 64], tmp);\
00103 tmp = p[3 * 64];\
00104 op1(sum1, (w1)[3 * 64], tmp);\
00105 op2(sum2, (w2)[3 * 64], tmp);\
00106 tmp = p[4 * 64];\
00107 op1(sum1, (w1)[4 * 64], tmp);\
00108 op2(sum2, (w2)[4 * 64], tmp);\
00109 tmp = p[5 * 64];\
00110 op1(sum1, (w1)[5 * 64], tmp);\
00111 op2(sum2, (w2)[5 * 64], tmp);\
00112 tmp = p[6 * 64];\
00113 op1(sum1, (w1)[6 * 64], tmp);\
00114 op2(sum2, (w2)[6 * 64], tmp);\
00115 tmp = p[7 * 64];\
00116 op1(sum1, (w1)[7 * 64], tmp);\
00117 op2(sum2, (w2)[7 * 64], tmp);\
00118 }
00119
00120 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
00121 int *dither_state, OUT_INT *samples,
00122 int incr)
00123 {
00124 register const MPA_INT *w, *w2, *p;
00125 int j;
00126 OUT_INT *samples2;
00127 #if CONFIG_FLOAT
00128 float sum, sum2;
00129 #else
00130 int64_t sum, sum2;
00131 #endif
00132
00133
00134 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
00135
00136 samples2 = samples + 31 * incr;
00137 w = window;
00138 w2 = window + 31;
00139
00140 sum = *dither_state;
00141 p = synth_buf + 16;
00142 SUM8(MACS, sum, w, p);
00143 p = synth_buf + 48;
00144 SUM8(MLSS, sum, w + 32, p);
00145 *samples = round_sample(&sum);
00146 samples += incr;
00147 w++;
00148
00149
00150
00151 for(j=1;j<16;j++) {
00152 sum2 = 0;
00153 p = synth_buf + 16 + j;
00154 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
00155 p = synth_buf + 48 - j;
00156 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
00157
00158 *samples = round_sample(&sum);
00159 samples += incr;
00160 sum += sum2;
00161 *samples2 = round_sample(&sum);
00162 samples2 -= incr;
00163 w++;
00164 w2--;
00165 }
00166
00167 p = synth_buf + 32;
00168 SUM8(MLSS, sum, w + 32, p);
00169 *samples = round_sample(&sum);
00170 *dither_state= sum;
00171 }
00172
00173
00174
00175 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
00176 int *synth_buf_offset,
00177 MPA_INT *window, int *dither_state,
00178 OUT_INT *samples, int incr,
00179 MPA_INT *sb_samples)
00180 {
00181 MPA_INT *synth_buf;
00182 int offset;
00183
00184 offset = *synth_buf_offset;
00185 synth_buf = synth_buf_ptr + offset;
00186
00187 s->RENAME(dct32)(synth_buf, sb_samples);
00188 s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
00189
00190 offset = (offset - 32) & 511;
00191 *synth_buf_offset = offset;
00192 }
00193
00194 av_cold void RENAME(ff_mpa_synth_init)(MPA_INT *window)
00195 {
00196 int i, j;
00197
00198
00199 for(i=0;i<257;i++) {
00200 INTFLOAT v;
00201 v = ff_mpa_enwindow[i];
00202 #if CONFIG_FLOAT
00203 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
00204 #endif
00205 window[i] = v;
00206 if ((i & 63) != 0)
00207 v = -v;
00208 if (i != 0)
00209 window[512 - i] = v;
00210 }
00211
00212
00213
00214 for(i=0; i < 8; i++)
00215 for(j=0; j < 16; j++)
00216 window[512+16*i+j] = window[64*i+32-j];
00217
00218 for(i=0; i < 8; i++)
00219 for(j=0; j < 16; j++)
00220 window[512+128+16*i+j] = window[64*i+48-j];
00221 }
00222
00223 void RENAME(ff_init_mpadsp_tabs)(void)
00224 {
00225 int i, j;
00226
00227 for (i = 0; i < 36; i++) {
00228 for (j = 0; j < 4; j++) {
00229 double d;
00230
00231 if (j == 2 && i % 3 != 1)
00232 continue;
00233
00234 d = sin(M_PI * (i + 0.5) / 36.0);
00235 if (j == 1) {
00236 if (i >= 30) d = 0;
00237 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
00238 else if (i >= 18) d = 1;
00239 } else if (j == 3) {
00240 if (i < 6) d = 0;
00241 else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
00242 else if (i < 18) d = 1;
00243 }
00244
00245 d *= 0.5 * IMDCT_SCALAR / cos(M_PI * (2 * i + 19) / 72);
00246
00247 if (j == 2)
00248 RENAME(ff_mdct_win)[j][i/3] = FIXHR((d / (1<<5)));
00249 else {
00250 int idx = i < 18 ? i : i + (MDCT_BUF_SIZE/2 - 18);
00251 RENAME(ff_mdct_win)[j][idx] = FIXHR((d / (1<<5)));
00252 }
00253 }
00254 }
00255
00256
00257
00258 for (j = 0; j < 4; j++) {
00259 for (i = 0; i < MDCT_BUF_SIZE; i += 2) {
00260 RENAME(ff_mdct_win)[j + 4][i ] = RENAME(ff_mdct_win)[j][i ];
00261 RENAME(ff_mdct_win)[j + 4][i + 1] = -RENAME(ff_mdct_win)[j][i + 1];
00262 }
00263 }
00264 }
00265
00266 #define C1 FIXHR(0.98480775301220805936/2)
00267 #define C2 FIXHR(0.93969262078590838405/2)
00268 #define C3 FIXHR(0.86602540378443864676/2)
00269 #define C4 FIXHR(0.76604444311897803520/2)
00270 #define C5 FIXHR(0.64278760968653932632/2)
00271 #define C6 FIXHR(0.5/2)
00272 #define C7 FIXHR(0.34202014332566873304/2)
00273 #define C8 FIXHR(0.17364817766693034885/2)
00274
00275
00276 static const INTFLOAT icos36[9] = {
00277 FIXR(0.50190991877167369479),
00278 FIXR(0.51763809020504152469),
00279 FIXR(0.55168895948124587824),
00280 FIXR(0.61038729438072803416),
00281 FIXR(0.70710678118654752439),
00282 FIXR(0.87172339781054900991),
00283 FIXR(1.18310079157624925896),
00284 FIXR(1.93185165257813657349),
00285 FIXR(5.73685662283492756461),
00286 };
00287
00288
00289 static const INTFLOAT icos36h[9] = {
00290 FIXHR(0.50190991877167369479/2),
00291 FIXHR(0.51763809020504152469/2),
00292 FIXHR(0.55168895948124587824/2),
00293 FIXHR(0.61038729438072803416/2),
00294 FIXHR(0.70710678118654752439/2),
00295 FIXHR(0.87172339781054900991/2),
00296 FIXHR(1.18310079157624925896/4),
00297 FIXHR(1.93185165257813657349/4),
00298
00299 };
00300
00301
00302 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
00303 {
00304 int i, j;
00305 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
00306 INTFLOAT tmp[18], *tmp1, *in1;
00307
00308 for (i = 17; i >= 1; i--)
00309 in[i] += in[i-1];
00310 for (i = 17; i >= 3; i -= 2)
00311 in[i] += in[i-2];
00312
00313 for (j = 0; j < 2; j++) {
00314 tmp1 = tmp + j;
00315 in1 = in + j;
00316
00317 t2 = in1[2*4] + in1[2*8] - in1[2*2];
00318
00319 t3 = in1[2*0] + SHR(in1[2*6],1);
00320 t1 = in1[2*0] - in1[2*6];
00321 tmp1[ 6] = t1 - SHR(t2,1);
00322 tmp1[16] = t1 + t2;
00323
00324 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
00325 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
00326 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
00327
00328 tmp1[10] = t3 - t0 - t2;
00329 tmp1[ 2] = t3 + t0 + t1;
00330 tmp1[14] = t3 + t2 - t1;
00331
00332 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
00333 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
00334 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
00335 t0 = MULH3(in1[2*3], C3, 2);
00336
00337 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
00338
00339 tmp1[ 0] = t2 + t3 + t0;
00340 tmp1[12] = t2 + t1 - t0;
00341 tmp1[ 8] = t3 - t1 - t0;
00342 }
00343
00344 i = 0;
00345 for (j = 0; j < 4; j++) {
00346 t0 = tmp[i];
00347 t1 = tmp[i + 2];
00348 s0 = t1 + t0;
00349 s2 = t1 - t0;
00350
00351 t2 = tmp[i + 1];
00352 t3 = tmp[i + 3];
00353 s1 = MULH3(t3 + t2, icos36h[ j], 2);
00354 s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
00355
00356 t0 = s0 + s1;
00357 t1 = s0 - s1;
00358 out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[4*(9 + j)];
00359 out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[4*(8 - j)];
00360 buf[4 * ( 9 + j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
00361 buf[4 * ( 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
00362
00363 t0 = s2 + s3;
00364 t1 = s2 - s3;
00365 out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
00366 out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[4*( j)];
00367 buf[4 * ( 9 + 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
00368 buf[4 * ( j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + j], 1);
00369 i += 4;
00370 }
00371
00372 s0 = tmp[16];
00373 s1 = MULH3(tmp[17], icos36h[4], 2);
00374 t0 = s0 + s1;
00375 t1 = s0 - s1;
00376 out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[4*(9 + 4)];
00377 out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[4*(8 - 4)];
00378 buf[4 * ( 9 + 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
00379 buf[4 * ( 8 - 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
00380 }
00381
00382 void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
00383 int count, int switch_point, int block_type)
00384 {
00385 int j;
00386 for (j=0 ; j < count; j++) {
00387
00388
00389
00390 int win_idx = (switch_point && j < 2) ? 0 : block_type;
00391 INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
00392
00393 imdct36(out, buf, in, win);
00394
00395 in += 18;
00396 buf += ((j&3) != 3 ? 1 : (72-3));
00397 out++;
00398 }
00399 }
00400