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