FFmpeg
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/attributes.h"
24 #include "libavutil/mem_internal.h"
25 #include "libavutil/thread.h"
26 
27 #include "dct32.h"
28 #include "mathops.h"
29 #include "mpegaudiodsp.h"
30 #include "mpegaudio.h"
31 
32 #if USE_FLOATS
33 #define RENAME(n) n##_float
34 
35 static inline float round_sample(float *sum)
36 {
37  float sum1=*sum;
38  *sum = 0;
39  return sum1;
40 }
41 
42 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
43 #define MULS(ra, rb) ((ra)*(rb))
44 #define MULH3(x, y, s) ((s)*(y)*(x))
45 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
46 #define MULLx(x, y, s) ((y)*(x))
47 #define FIXHR(x) ((float)(x))
48 #define FIXR(x) ((float)(x))
49 #define SHR(a,b) ((a)*(1.0f/(1<<(b))))
50 
51 #else
52 
53 #define RENAME(n) n##_fixed
54 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
55 
56 static inline int round_sample(int64_t *sum)
57 {
58  int sum1;
59  sum1 = (int)((*sum) >> OUT_SHIFT);
60  *sum &= (1<<OUT_SHIFT)-1;
61  return av_clip_int16(sum1);
62 }
63 
64 # define MULS(ra, rb) MUL64(ra, rb)
65 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
66 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
67 # define MULH3(x, y, s) MULH((s)*(x), y)
68 # define MULLx(x, y, s) MULL((int)(x),(y),s)
69 # define SHR(a,b) (((int)(a))>>(b))
70 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
71 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
72 #endif
73 
74 /** Window for MDCT. Actually only the elements in [0,17] and
75  [MDCT_BUF_SIZE/2, MDCT_BUF_SIZE/2 + 17] are actually used. The rest
76  is just to preserve alignment for SIMD implementations.
77 */
79 
80 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
81 
82 #define SUM8(op, sum, w, p) \
83 { \
84  op(sum, (w)[0 * 64], (p)[0 * 64]); \
85  op(sum, (w)[1 * 64], (p)[1 * 64]); \
86  op(sum, (w)[2 * 64], (p)[2 * 64]); \
87  op(sum, (w)[3 * 64], (p)[3 * 64]); \
88  op(sum, (w)[4 * 64], (p)[4 * 64]); \
89  op(sum, (w)[5 * 64], (p)[5 * 64]); \
90  op(sum, (w)[6 * 64], (p)[6 * 64]); \
91  op(sum, (w)[7 * 64], (p)[7 * 64]); \
92 }
93 
94 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
95 { \
96  INTFLOAT tmp;\
97  tmp = p[0 * 64];\
98  op1(sum1, (w1)[0 * 64], tmp);\
99  op2(sum2, (w2)[0 * 64], tmp);\
100  tmp = p[1 * 64];\
101  op1(sum1, (w1)[1 * 64], tmp);\
102  op2(sum2, (w2)[1 * 64], tmp);\
103  tmp = p[2 * 64];\
104  op1(sum1, (w1)[2 * 64], tmp);\
105  op2(sum2, (w2)[2 * 64], tmp);\
106  tmp = p[3 * 64];\
107  op1(sum1, (w1)[3 * 64], tmp);\
108  op2(sum2, (w2)[3 * 64], tmp);\
109  tmp = p[4 * 64];\
110  op1(sum1, (w1)[4 * 64], tmp);\
111  op2(sum2, (w2)[4 * 64], tmp);\
112  tmp = p[5 * 64];\
113  op1(sum1, (w1)[5 * 64], tmp);\
114  op2(sum2, (w2)[5 * 64], tmp);\
115  tmp = p[6 * 64];\
116  op1(sum1, (w1)[6 * 64], tmp);\
117  op2(sum2, (w2)[6 * 64], tmp);\
118  tmp = p[7 * 64];\
119  op1(sum1, (w1)[7 * 64], tmp);\
120  op2(sum2, (w2)[7 * 64], tmp);\
121 }
122 
123 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
124  int *dither_state, OUT_INT *samples,
125  ptrdiff_t incr)
126 {
127  register const MPA_INT *w, *w2, *p;
128  int j;
129  OUT_INT *samples2;
130 #if USE_FLOATS
131  float sum, sum2;
132 #else
133  int64_t sum, sum2;
134 #endif
135 
136  /* copy to avoid wrap */
137  memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
138 
139  samples2 = samples + 31 * incr;
140  w = window;
141  w2 = window + 31;
142 
143  sum = *dither_state;
144  p = synth_buf + 16;
145  SUM8(MACS, sum, w, p);
146  p = synth_buf + 48;
147  SUM8(MLSS, sum, w + 32, p);
148  *samples = round_sample(&sum);
149  samples += incr;
150  w++;
151 
152  /* we calculate two samples at the same time to avoid one memory
153  access per two sample */
154  for(j=1;j<16;j++) {
155  sum2 = 0;
156  p = synth_buf + 16 + j;
157  SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
158  p = synth_buf + 48 - j;
159  SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
160 
161  *samples = round_sample(&sum);
162  samples += incr;
163  sum += sum2;
164  *samples2 = round_sample(&sum);
165  samples2 -= incr;
166  w++;
167  w2--;
168  }
169 
170  p = synth_buf + 32;
171  SUM8(MLSS, sum, w + 32, p);
172  *samples = round_sample(&sum);
173  *dither_state= sum;
174 }
175 
176 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
177  32 samples. */
178 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
179  int *synth_buf_offset,
180  MPA_INT *window, int *dither_state,
181  OUT_INT *samples, ptrdiff_t incr,
182  MPA_INT *sb_samples)
183 {
184  MPA_INT *synth_buf;
185  int offset;
186 
187  offset = *synth_buf_offset;
188  synth_buf = synth_buf_ptr + offset;
189 
190  s->RENAME(dct32)(synth_buf, sb_samples);
191  s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
192 
193  offset = (offset - 32) & 511;
194  *synth_buf_offset = offset;
195 }
196 
198 {
199  int i, j;
200 
201  /* max = 18760, max sum over all 16 coefs : 44736 */
202  for(i=0;i<257;i++) {
203  INTFLOAT v;
204  v = ff_mpa_enwindow[i];
205 #if USE_FLOATS
206  v *= 1.0 / (1LL<<(16 + FRAC_BITS));
207 #endif
208  window[i] = v;
209  if ((i & 63) != 0)
210  v = -v;
211  if (i != 0)
212  window[512 - i] = v;
213  }
214 
215 
216  // Needed for avoiding shuffles in ASM implementations
217  for(i=0; i < 8; i++)
218  for(j=0; j < 16; j++)
219  window[512+16*i+j] = window[64*i+32-j];
220 
221  for(i=0; i < 8; i++)
222  for(j=0; j < 16; j++)
223  window[512+128+16*i+j] = window[64*i+48-j];
224 }
225 
227 {
228  mpa_synth_init(RENAME(ff_mpa_synth_window));
229 }
230 
231 av_cold void RENAME(ff_mpa_synth_init)(void)
232 {
233  static AVOnce init_static_once = AV_ONCE_INIT;
234  ff_thread_once(&init_static_once, mpa_synth_window_init);
235 }
236 
237 /* cos(pi*i/18) */
238 #define C1 FIXHR(0.98480775301220805936/2)
239 #define C2 FIXHR(0.93969262078590838405/2)
240 #define C3 FIXHR(0.86602540378443864676/2)
241 #define C4 FIXHR(0.76604444311897803520/2)
242 #define C5 FIXHR(0.64278760968653932632/2)
243 #define C6 FIXHR(0.5/2)
244 #define C7 FIXHR(0.34202014332566873304/2)
245 #define C8 FIXHR(0.17364817766693034885/2)
246 
247 /* 0.5 / cos(pi*(2*i+1)/36) */
248 static const INTFLOAT icos36[9] = {
249  FIXR(0.50190991877167369479),
250  FIXR(0.51763809020504152469), //0
251  FIXR(0.55168895948124587824),
252  FIXR(0.61038729438072803416),
253  FIXR(0.70710678118654752439), //1
254  FIXR(0.87172339781054900991),
255  FIXR(1.18310079157624925896),
256  FIXR(1.93185165257813657349), //2
257  FIXR(5.73685662283492756461),
258 };
259 
260 /* 0.5 / cos(pi*(2*i+1)/36) */
261 static const INTFLOAT icos36h[9] = {
262  FIXHR(0.50190991877167369479/2),
263  FIXHR(0.51763809020504152469/2), //0
264  FIXHR(0.55168895948124587824/2),
265  FIXHR(0.61038729438072803416/2),
266  FIXHR(0.70710678118654752439/2), //1
267  FIXHR(0.87172339781054900991/2),
268  FIXHR(1.18310079157624925896/4),
269  FIXHR(1.93185165257813657349/4), //2
270 // FIXHR(5.73685662283492756461),
271 };
272 
273 /* using Lee like decomposition followed by hand coded 9 points DCT */
274 static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
275 {
276  int i, j;
277  SUINTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
278  SUINTFLOAT tmp[18], *tmp1, *in1;
279 
280  for (i = 17; i >= 1; i--)
281  in[i] += in[i-1];
282  for (i = 17; i >= 3; i -= 2)
283  in[i] += in[i-2];
284 
285  for (j = 0; j < 2; j++) {
286  tmp1 = tmp + j;
287  in1 = in + j;
288 
289  t2 = in1[2*4] + in1[2*8] - in1[2*2];
290 
291  t3 = in1[2*0] + SHR(in1[2*6],1);
292  t1 = in1[2*0] - in1[2*6];
293  tmp1[ 6] = t1 - SHR(t2,1);
294  tmp1[16] = t1 + t2;
295 
296  t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
297  t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
298  t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
299 
300  tmp1[10] = t3 - t0 - t2;
301  tmp1[ 2] = t3 + t0 + t1;
302  tmp1[14] = t3 + t2 - t1;
303 
304  tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
305  t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
306  t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
307  t0 = MULH3(in1[2*3], C3, 2);
308 
309  t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
310 
311  tmp1[ 0] = t2 + t3 + t0;
312  tmp1[12] = t2 + t1 - t0;
313  tmp1[ 8] = t3 - t1 - t0;
314  }
315 
316  i = 0;
317  for (j = 0; j < 4; j++) {
318  t0 = tmp[i];
319  t1 = tmp[i + 2];
320  s0 = t1 + t0;
321  s2 = t1 - t0;
322 
323  t2 = tmp[i + 1];
324  t3 = tmp[i + 3];
325  s1 = MULH3(t3 + t2, icos36h[ j], 2);
326  s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
327 
328  t0 = s0 + s1;
329  t1 = s0 - s1;
330  out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[4*(9 + j)];
331  out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[4*(8 - j)];
332  buf[4 * ( 9 + j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + j], 1);
333  buf[4 * ( 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - j], 1);
334 
335  t0 = s2 + s3;
336  t1 = s2 - s3;
337  out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[4*(9 + 8 - j)];
338  out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[4*( j)];
339  buf[4 * ( 9 + 8 - j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 8 - j], 1);
340  buf[4 * ( j )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + j], 1);
341  i += 4;
342  }
343 
344  s0 = tmp[16];
345  s1 = MULH3(tmp[17], icos36h[4], 2);
346  t0 = s0 + s1;
347  t1 = s0 - s1;
348  out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[4*(9 + 4)];
349  out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[4*(8 - 4)];
350  buf[4 * ( 9 + 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 9 + 4], 1);
351  buf[4 * ( 8 - 4 )] = MULH3(t0, win[MDCT_BUF_SIZE/2 + 8 - 4], 1);
352 }
353 
354 void RENAME(ff_imdct36_blocks)(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in,
355  int count, int switch_point, int block_type)
356 {
357  int j;
358  for (j=0 ; j < count; j++) {
359  /* apply window & overlap with previous buffer */
360 
361  /* select window */
362  int win_idx = (switch_point && j < 2) ? 0 : block_type;
363  INTFLOAT *win = RENAME(ff_mdct_win)[win_idx + (4 & -(j & 1))];
364 
365  imdct36(out, buf, in, win);
366 
367  in += 18;
368  buf += ((j&3) != 3 ? 1 : (72-3));
369  out++;
370  }
371 }
372 
SHR
#define SHR(a, b)
Definition: mpegaudiodsp_template.c:69
C7
#define C7
Definition: mpegaudiodsp_template.c:244
C2
#define C2
Definition: mpegaudiodsp_template.c:239
mem_internal.h
out
FILE * out
Definition: movenc.c:54
icos36h
static const INTFLOAT icos36h[9]
Definition: mpegaudiodsp_template.c:261
thread.h
mpa_synth_init
static av_cold void mpa_synth_init(MPA_INT *window)
Definition: mpegaudiodsp_template.c:197
MULH3
#define MULH3(x, y, s)
Definition: mpegaudiodsp_template.c:67
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
w
uint8_t w
Definition: llviddspenc.c:38
MPADSPContext
Definition: mpegaudiodsp.h:27
t0
#define t0
Definition: regdef.h:28
C8
#define C8
Definition: mpegaudiodsp_template.c:245
t1
#define t1
Definition: regdef.h:29
C5
#define C5
Definition: mpegaudiodsp_template.c:242
win
static float win(SuperEqualizerContext *s, float n, int N)
Definition: af_superequalizer.c:119
window
static SDL_Window * window
Definition: ffplay.c:365
s3
#define s3
Definition: regdef.h:40
SUM8P2
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p)
Definition: mpegaudiodsp_template.c:94
C1
#define C1
Definition: mpegaudiodsp_template.c:238
MACS
#define MACS(rt, ra, rb)
Definition: mpegaudiodsp_template.c:65
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:184
SUM8
#define SUM8(op, sum, w, p)
Definition: mpegaudiodsp_template.c:82
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
dct32
void dct32(INTFLOAT *out, const INTFLOAT *tab_arg)
Definition: dct32_template.c:126
s1
#define s1
Definition: regdef.h:38
C4
#define C4
Definition: mpegaudiodsp_template.c:241
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:182
av_clip_int16
#define av_clip_int16
Definition: common.h:110
mpa_synth_window_init
static av_cold void mpa_synth_window_init(void)
Definition: mpegaudiodsp_template.c:226
mathops.h
dct32.h
SBLIMIT
#define SBLIMIT
Definition: mpegaudio.h:44
AVOnce
#define AVOnce
Definition: thread.h:181
ff_mpa_enwindow
const int32_t ff_mpa_enwindow[257]
Definition: mpegaudiodsp_data.c:22
C3
#define C3
Definition: mpegaudiodsp_template.c:240
OUT_SHIFT
#define OUT_SHIFT
Definition: mpegaudiodsp_template.c:54
s2
#define s2
Definition: regdef.h:39
FIXHR
#define FIXHR(a)
Definition: mpegaudiodsp_template.c:71
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:87
MLSS
#define MLSS(rt, ra, rb)
Definition: mpegaudiodsp_template.c:66
OUT_INT
int16_t OUT_INT
Definition: mpegaudio.h:76
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
imdct36
static void imdct36(INTFLOAT *out, INTFLOAT *buf, SUINTFLOAT *in, INTFLOAT *win)
Definition: mpegaudiodsp_template.c:274
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
t3
#define t3
Definition: regdef.h:31
apply_window
static void(*const apply_window[4])(AVFloatDSPContext *fdsp, SingleChannelElement *sce, const float *audio)
Definition: aacenc.c:465
mpegaudio.h
FIXR
#define FIXR(a)
Definition: mpegaudiodsp_template.c:70
t2
#define t2
Definition: regdef.h:30
FRAC_BITS
#define FRAC_BITS
Definition: g729postfilter.c:36
RENAME
#define RENAME(n)
Definition: mpegaudiodsp_template.c:53
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
round_sample
static int round_sample(int64_t *sum)
Definition: mpegaudiodsp_template.c:56
icos36
static const INTFLOAT icos36[9]
Definition: mpegaudiodsp_template.c:248
mpegaudiodsp.h
MPA_INT
int32_t MPA_INT
Definition: mpegaudio.h:75
MDCT_BUF_SIZE
#define MDCT_BUF_SIZE
For SSE implementation, MDCT_BUF_SIZE/2 should be 128-bit aligned.
Definition: mpegaudiodsp.h:87
s0
#define s0
Definition: regdef.h:37
SUINTFLOAT
#define SUINTFLOAT
Definition: dct32_template.c:45
int
int
Definition: ffmpeg_filter.c:156
INTFLOAT
float INTFLOAT
Definition: aac_defines.h:82
MULLx
#define MULLx(x, y, s)
Definition: mpegaudiodsp_template.c:68