FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpegaudiodsp_template.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdint.h>
22 
23 #include "libavutil/mem.h"
24 #include "dct32.h"
25 #include "mathops.h"
26 #include "mpegaudiodsp.h"
27 #include "mpegaudio.h"
28 
29 #if CONFIG_FLOAT
30 #define RENAME(n) n##_float
31 
32 static inline float round_sample(float *sum)
33 {
34  float sum1=*sum;
35  *sum = 0;
36  return sum1;
37 }
38 
39 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
40 #define MULS(ra, rb) ((ra)*(rb))
41 #define MULH3(x, y, s) ((s)*(y)*(x))
42 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
43 #define MULLx(x, y, s) ((y)*(x))
44 #define FIXHR(x) ((float)(x))
45 #define FIXR(x) ((float)(x))
46 #define SHR(a,b) ((a)*(1.0f/(1<<(b))))
47 
48 #else
49 
50 #define RENAME(n) n##_fixed
51 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
52 
53 static inline int round_sample(int64_t *sum)
54 {
55  int sum1;
56  sum1 = (int)((*sum) >> OUT_SHIFT);
57  *sum &= (1<<OUT_SHIFT)-1;
58  return av_clip_int16(sum1);
59 }
60 
61 # define MULS(ra, rb) MUL64(ra, rb)
62 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
63 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
64 # define MULH3(x, y, s) MULH((s)*(x), y)
65 # define MULLx(x, y, s) MULL(x,y,s)
66 # define SHR(a,b) ((a)>>(b))
67 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
68 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
69 #endif
70 
71 /** Window for MDCT. Actually only the elements in [0,17] and
72  [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
73  is just to preserve alignment for SIMD implementations.
74 */
76 
77 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
78 
79 #define SUM8(op, sum, w, p) \
80 { \
81  op(sum, (w)[0 * 64], (p)[0 * 64]); \
82  op(sum, (w)[1 * 64], (p)[1 * 64]); \
83  op(sum, (w)[2 * 64], (p)[2 * 64]); \
84  op(sum, (w)[3 * 64], (p)[3 * 64]); \
85  op(sum, (w)[4 * 64], (p)[4 * 64]); \
86  op(sum, (w)[5 * 64], (p)[5 * 64]); \
87  op(sum, (w)[6 * 64], (p)[6 * 64]); \
88  op(sum, (w)[7 * 64], (p)[7 * 64]); \
89 }
90 
91 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
92 { \
93  INTFLOAT tmp;\
94  tmp = p[0 * 64];\
95  op1(sum1, (w1)[0 * 64], tmp);\
96  op2(sum2, (w2)[0 * 64], tmp);\
97  tmp = p[1 * 64];\
98  op1(sum1, (w1)[1 * 64], tmp);\
99  op2(sum2, (w2)[1 * 64], tmp);\
100  tmp = p[2 * 64];\
101  op1(sum1, (w1)[2 * 64], tmp);\
102  op2(sum2, (w2)[2 * 64], tmp);\
103  tmp = p[3 * 64];\
104  op1(sum1, (w1)[3 * 64], tmp);\
105  op2(sum2, (w2)[3 * 64], tmp);\
106  tmp = p[4 * 64];\
107  op1(sum1, (w1)[4 * 64], tmp);\
108  op2(sum2, (w2)[4 * 64], tmp);\
109  tmp = p[5 * 64];\
110  op1(sum1, (w1)[5 * 64], tmp);\
111  op2(sum2, (w2)[5 * 64], tmp);\
112  tmp = p[6 * 64];\
113  op1(sum1, (w1)[6 * 64], tmp);\
114  op2(sum2, (w2)[6 * 64], tmp);\
115  tmp = p[7 * 64];\
116  op1(sum1, (w1)[7 * 64], tmp);\
117  op2(sum2, (w2)[7 * 64], tmp);\
118 }
119 
120 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
121  int *dither_state, OUT_INT *samples,
122  int incr)
123 {
124  register const MPA_INT *w, *w2, *p;
125  int j;
126  OUT_INT *samples2;
127 #if CONFIG_FLOAT
128  float sum, sum2;
129 #else
130  int64_t sum, sum2;
131 #endif
132 
133  /* copy to avoid wrap */
134  memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
135 
136  samples2 = samples + 31 * incr;
137  w = window;
138  w2 = window + 31;
139 
140  sum = *dither_state;
141  p = synth_buf + 16;
142  SUM8(MACS, sum, w, p);
143  p = synth_buf + 48;
144  SUM8(MLSS, sum, w + 32, p);
145  *samples = round_sample(&sum);
146  samples += incr;
147  w++;
148 
149  /* we calculate two samples at the same time to avoid one memory
150  access per two sample */
151  for(j=1;j<16;j++) {
152  sum2 = 0;
153  p = synth_buf + 16 + j;
154  SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
155  p = synth_buf + 48 - j;
156  SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
157 
158  *samples = round_sample(&sum);
159  samples += incr;
160  sum += sum2;
161  *samples2 = round_sample(&sum);
162  samples2 -= incr;
163  w++;
164  w2--;
165  }
166 
167  p = synth_buf + 32;
168  SUM8(MLSS, sum, w + 32, p);
169  *samples = round_sample(&sum);
170  *dither_state= sum;
171 }
172 
173 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
174  32 samples. */
175 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
176  int *synth_buf_offset,
177  MPA_INT *window, int *dither_state,
178  OUT_INT *samples, int incr,
179  MPA_INT *sb_samples)
180 {
181  MPA_INT *synth_buf;
182  int offset;
183 
184  offset = *synth_buf_offset;
185  synth_buf = synth_buf_ptr + offset;
186 
187  s->RENAME(dct32)(synth_buf, sb_samples);
188  s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
189 
190  offset = (offset - 32) & 511;
191  *synth_buf_offset = offset;
192 }
193 
194 av_cold void RENAME(ff_mpa_synth_init)(MPA_INT *window)
195 {
196  int i, j;
197 
198  /* max = 18760, max sum over all 16 coefs : 44736 */
199  for(i=0;i<257;i++) {
200  INTFLOAT v;
201  v = ff_mpa_enwindow[i];
202 #if CONFIG_FLOAT
203  v *= 1.0 / (1LL<<(16 + FRAC_BITS));
204 #endif
205  window[i] = v;
206  if ((i & 63) != 0)
207  v = -v;
208  if (i != 0)
209  window[512 - i] = v;
210  }
211 
212 
213  // Needed for avoiding shuffles in ASM implementations
214  for(i=0; i < 8; i++)
215  for(j=0; j < 16; j++)
216  window[512+16*i+j] = window[64*i+32-j];
217 
218  for(i=0; i < 8; i++)
219  for(j=0; j < 16; j++)
220  window[512+128+16*i+j] = window[64*i+48-j];
221 }
222 
223 void RENAME(ff_init_mpadsp_tabs)(void)
224 {
225  int i, j;
226  /* compute mdct windows */
227  for (i = 0; i < 36; i++) {
228  for (j = 0; j < 4; j++) {
229  double d;
230 
231  if (j == 2 && i % 3 != 1)
232  continue;
233 
234  d = sin(M_PI * (i + 0.5) / 36.0);
235  if (j == 1) {
236  if (i >= 30) d = 0;
237  else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
238  else if (i >= 18) d = 1;
239  } else if (j == 3) {
240  if (i < 6) d = 0;
241  else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
242  else if (i < 18) d = 1;
243  }
244  //merge last stage of imdct into the window coefficients
245  d *= 0.5 * IMDCT_SCALAR / cos(M_PI * (2 * i + 19) / 72);
246 
247  if (j == 2)
248  RENAME(ff_mdct_win)[j][i/3] = FIXHR((d / (1<<5)));
249  else {
250  int idx = i < 18 ? i : i + (MDCT_BUF_SIZE/2 - 18);
251  RENAME(ff_mdct_win)[j][idx] = FIXHR((d / (1<<5)));
252  }
253  }
254  }
255 
256  /* NOTE: we do frequency inversion adter the MDCT by changing
257  the sign of the right window coefs */
258  for (j = 0; j < 4; j++) {
259  for (i = 0; i < MDCT_BUF_SIZE; i += 2) {
260  RENAME(ff_mdct_win)[j + 4][i ] = RENAME(ff_mdct_win)[j][i ];
261  RENAME(ff_mdct_win)[j + 4][i + 1] = -RENAME(ff_mdct_win)[j][i + 1];
262  }
263  }
264 }
265 /* cos(pi*i/18) */
266 #define C1 FIXHR(0.98480775301220805936/2)
267 #define C2 FIXHR(0.93969262078590838405/2)
268 #define C3 FIXHR(0.86602540378443864676/2)
269 #define C4 FIXHR(0.76604444311897803520/2)
270 #define C5 FIXHR(0.64278760968653932632/2)
271 #define C6 FIXHR(0.5/2)
272 #define C7 FIXHR(0.34202014332566873304/2)
273 #define C8 FIXHR(0.17364817766693034885/2)
274 
275 /* 0.5 / cos(pi*(2*i+1)/36) */
276 static const INTFLOAT icos36[9] = {
277  FIXR(0.50190991877167369479),
278  FIXR(0.51763809020504152469), //0
279  FIXR(0.55168895948124587824),
280  FIXR(0.61038729438072803416),
281  FIXR(0.70710678118654752439), //1
282  FIXR(0.87172339781054900991),
283  FIXR(1.18310079157624925896),
284  FIXR(1.93185165257813657349), //2
285  FIXR(5.73685662283492756461),
286 };
287 
288 /* 0.5 / cos(pi*(2*i+1)/36) */
289 static const INTFLOAT icos36h[9] = {
290  FIXHR(0.50190991877167369479/2),
291  FIXHR(0.51763809020504152469/2), //0
292  FIXHR(0.55168895948124587824/2),
293  FIXHR(0.61038729438072803416/2),
294  FIXHR(0.70710678118654752439/2), //1
295  FIXHR(0.87172339781054900991/2),
296  FIXHR(1.18310079157624925896/4),
297  FIXHR(1.93185165257813657349/4), //2
298 // FIXHR(5.73685662283492756461),
299 };
300 
301 /* using Lee like decomposition followed by hand coded 9 points DCT */
302 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
303 {
304  int i, j;
305  INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
306  INTFLOAT tmp[18], *tmp1, *in1;
307 
308  for (i = 17; i >= 1; i--)
309  in[i] += in[i-1];
310  for (i = 17; i >= 3; i -= 2)
311  in[i] += in[i-2];
312 
313  for (j = 0; j < 2; j++) {
314  tmp1 = tmp + j;
315  in1 = in + j;
316 
317  t2 = in1[2*4] + in1[2*8] - in1[2*2];
318 
319  t3 = in1[2*0] + SHR(in1[2*6],1);
320  t1 = in1[2*0] - in1[2*6];
321  tmp1[ 6] = t1 - SHR(t2,1);
322  tmp1[16] = t1 + t2;
323 
324  t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
325  t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
326  t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
327 
328  tmp1[10] = t3 - t0 - t2;
329  tmp1[ 2] = t3 + t0 + t1;
330  tmp1[14] = t3 + t2 - t1;
331 
332  tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
333  t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
334  t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
335  t0 = MULH3(in1[2*3], C3, 2);
336 
337  t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
338 
339  tmp1[ 0] = t2 + t3 + t0;
340  tmp1[12] = t2 + t1 - t0;
341  tmp1[ 8] = t3 - t1 - t0;
342  }
343 
344  i = 0;
345  for (j = 0; j < 4; j++) {
346  t0 = tmp[i];
347  t1 = tmp[i + 2];
348  s0 = t1 + t0;
349  s2 = t1 - t0;
350 
351  t2 = tmp[i + 1];
352  t3 = tmp[i + 3];
353  s1 = MULH3(t3 + t2, icos36h[ j], 2);
354  s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
355 
356  t0 = s0 + s1;
357  t1 = s0 - s1;
358  out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[4*(9 + j)];
359  out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[4*(8 - j)];
360  buf[4 * ( 9 + j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
361  buf[4 * ( 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
362 
363  t0 = s2 + s3;
364  t1 = s2 - s3;
365  out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
366  out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[4*( j)];
367  buf[4 * ( 9 + 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
368  buf[4 * ( j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + j], 1);
369  i += 4;
370  }
371 
372  s0 = tmp[16];
373  s1 = MULH3(tmp[17], icos36h[4], 2);
374  t0 = s0 + s1;
375  t1 = s0 - s1;
376  out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[4*(9 + 4)];
377  out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[4*(8 - 4)];
378  buf[4 * ( 9 + 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
379  buf[4 * ( 8 - 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
380 }
381 
382 void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
383  int count, int switch_point, int block_type)
384 {
385  int j;
386  for (j=0 ; j < count; j++) {
387  /* apply window & overlap with previous buffer */
388 
389  /* select window */
390  int win_idx = (switch_point && j < 2) ? 0 : block_type;
391  INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
392 
393  imdct36(out, buf, in, win);
394 
395  in += 18;
396  buf += ((j&3) != 3 ? 1 : (72-3));
397  out++;
398  }
399 }
400