2 * Copyright (c) 2001, 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/mem.h"
26 #include "mpegaudiodsp.h"
27 #include "mpegaudio.h"
28 #include "mpegaudiodata.h"
31 #define RENAME(n) n##_float
33 static inline float round_sample(float *sum)
40 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
41 #define MULS(ra, rb) ((ra)*(rb))
42 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
46 #define RENAME(n) n##_fixed
47 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
49 static inline int round_sample(int64_t *sum)
52 sum1 = (int)((*sum) >> OUT_SHIFT);
53 *sum &= (1<<OUT_SHIFT)-1;
54 return av_clip_int16(sum1);
57 # define MULS(ra, rb) MUL64(ra, rb)
58 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
59 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
62 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
64 #define SUM8(op, sum, w, p) \
66 op(sum, (w)[0 * 64], (p)[0 * 64]); \
67 op(sum, (w)[1 * 64], (p)[1 * 64]); \
68 op(sum, (w)[2 * 64], (p)[2 * 64]); \
69 op(sum, (w)[3 * 64], (p)[3 * 64]); \
70 op(sum, (w)[4 * 64], (p)[4 * 64]); \
71 op(sum, (w)[5 * 64], (p)[5 * 64]); \
72 op(sum, (w)[6 * 64], (p)[6 * 64]); \
73 op(sum, (w)[7 * 64], (p)[7 * 64]); \
76 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
80 op1(sum1, (w1)[0 * 64], tmp);\
81 op2(sum2, (w2)[0 * 64], tmp);\
83 op1(sum1, (w1)[1 * 64], tmp);\
84 op2(sum2, (w2)[1 * 64], tmp);\
86 op1(sum1, (w1)[2 * 64], tmp);\
87 op2(sum2, (w2)[2 * 64], tmp);\
89 op1(sum1, (w1)[3 * 64], tmp);\
90 op2(sum2, (w2)[3 * 64], tmp);\
92 op1(sum1, (w1)[4 * 64], tmp);\
93 op2(sum2, (w2)[4 * 64], tmp);\
95 op1(sum1, (w1)[5 * 64], tmp);\
96 op2(sum2, (w2)[5 * 64], tmp);\
98 op1(sum1, (w1)[6 * 64], tmp);\
99 op2(sum2, (w2)[6 * 64], tmp);\
101 op1(sum1, (w1)[7 * 64], tmp);\
102 op2(sum2, (w2)[7 * 64], tmp);\
105 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
106 int *dither_state, OUT_INT *samples,
109 register const MPA_INT *w, *w2, *p;
118 /* copy to avoid wrap */
119 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
121 samples2 = samples + 31 * incr;
127 SUM8(MACS, sum, w, p);
129 SUM8(MLSS, sum, w + 32, p);
130 *samples = round_sample(&sum);
134 /* we calculate two samples at the same time to avoid one memory
135 access per two sample */
138 p = synth_buf + 16 + j;
139 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
140 p = synth_buf + 48 - j;
141 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
143 *samples = round_sample(&sum);
146 *samples2 = round_sample(&sum);
153 SUM8(MLSS, sum, w + 32, p);
154 *samples = round_sample(&sum);
158 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
160 void RENAME(ff_mpa_synth_filter)(MPADSPContext *s, MPA_INT *synth_buf_ptr,
161 int *synth_buf_offset,
162 MPA_INT *window, int *dither_state,
163 OUT_INT *samples, int incr,
169 offset = *synth_buf_offset;
170 synth_buf = synth_buf_ptr + offset;
172 s->RENAME(dct32)(synth_buf, sb_samples);
173 s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
175 offset = (offset - 32) & 511;
176 *synth_buf_offset = offset;
179 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
183 /* max = 18760, max sum over all 16 coefs : 44736 */
186 v = ff_mpa_enwindow[i];
188 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
197 // Needed for avoiding shuffles in ASM implementations
199 for(j=0; j < 16; j++)
200 window[512+16*i+j] = window[64*i+32-j];
203 for(j=0; j < 16; j++)
204 window[512+128+16*i+j] = window[64*i+48-j];