Git init
[framework/multimedia/ffmpeg.git] / libavcodec / mpegaudiodsp_template.c
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 #include "mpegaudiodata.h"
29
30 #if CONFIG_FLOAT
31 #define RENAME(n) n##_float
32
33 static inline float round_sample(float *sum)
34 {
35     float sum1=*sum;
36     *sum = 0;
37     return sum1;
38 }
39
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)
43
44 #else
45
46 #define RENAME(n) n##_fixed
47 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
48
49 static inline int round_sample(int64_t *sum)
50 {
51     int sum1;
52     sum1 = (int)((*sum) >> OUT_SHIFT);
53     *sum &= (1<<OUT_SHIFT)-1;
54     return av_clip_int16(sum1);
55 }
56
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)
60 #endif
61
62 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
63
64 #define SUM8(op, sum, w, p)               \
65 {                                         \
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]);    \
74 }
75
76 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
77 {                                               \
78     INTFLOAT tmp;\
79     tmp = p[0 * 64];\
80     op1(sum1, (w1)[0 * 64], tmp);\
81     op2(sum2, (w2)[0 * 64], tmp);\
82     tmp = p[1 * 64];\
83     op1(sum1, (w1)[1 * 64], tmp);\
84     op2(sum2, (w2)[1 * 64], tmp);\
85     tmp = p[2 * 64];\
86     op1(sum1, (w1)[2 * 64], tmp);\
87     op2(sum2, (w2)[2 * 64], tmp);\
88     tmp = p[3 * 64];\
89     op1(sum1, (w1)[3 * 64], tmp);\
90     op2(sum2, (w2)[3 * 64], tmp);\
91     tmp = p[4 * 64];\
92     op1(sum1, (w1)[4 * 64], tmp);\
93     op2(sum2, (w2)[4 * 64], tmp);\
94     tmp = p[5 * 64];\
95     op1(sum1, (w1)[5 * 64], tmp);\
96     op2(sum2, (w2)[5 * 64], tmp);\
97     tmp = p[6 * 64];\
98     op1(sum1, (w1)[6 * 64], tmp);\
99     op2(sum2, (w2)[6 * 64], tmp);\
100     tmp = p[7 * 64];\
101     op1(sum1, (w1)[7 * 64], tmp);\
102     op2(sum2, (w2)[7 * 64], tmp);\
103 }
104
105 void RENAME(ff_mpadsp_apply_window)(MPA_INT *synth_buf, MPA_INT *window,
106                                   int *dither_state, OUT_INT *samples,
107                                   int incr)
108 {
109     register const MPA_INT *w, *w2, *p;
110     int j;
111     OUT_INT *samples2;
112 #if CONFIG_FLOAT
113     float sum, sum2;
114 #else
115     int64_t sum, sum2;
116 #endif
117
118     /* copy to avoid wrap */
119     memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
120
121     samples2 = samples + 31 * incr;
122     w = window;
123     w2 = window + 31;
124
125     sum = *dither_state;
126     p = synth_buf + 16;
127     SUM8(MACS, sum, w, p);
128     p = synth_buf + 48;
129     SUM8(MLSS, sum, w + 32, p);
130     *samples = round_sample(&sum);
131     samples += incr;
132     w++;
133
134     /* we calculate two samples at the same time to avoid one memory
135        access per two sample */
136     for(j=1;j<16;j++) {
137         sum2 = 0;
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);
142
143         *samples = round_sample(&sum);
144         samples += incr;
145         sum += sum2;
146         *samples2 = round_sample(&sum);
147         samples2 -= incr;
148         w++;
149         w2--;
150     }
151
152     p = synth_buf + 32;
153     SUM8(MLSS, sum, w + 32, p);
154     *samples = round_sample(&sum);
155     *dither_state= sum;
156 }
157
158 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
159    32 samples. */
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,
164                                  MPA_INT *sb_samples)
165 {
166     MPA_INT *synth_buf;
167     int offset;
168
169     offset = *synth_buf_offset;
170     synth_buf = synth_buf_ptr + offset;
171
172     s->RENAME(dct32)(synth_buf, sb_samples);
173     s->RENAME(apply_window)(synth_buf, window, dither_state, samples, incr);
174
175     offset = (offset - 32) & 511;
176     *synth_buf_offset = offset;
177 }
178
179 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
180 {
181     int i, j;
182
183     /* max = 18760, max sum over all 16 coefs : 44736 */
184     for(i=0;i<257;i++) {
185         INTFLOAT v;
186         v = ff_mpa_enwindow[i];
187 #if CONFIG_FLOAT
188         v *= 1.0 / (1LL<<(16 + FRAC_BITS));
189 #endif
190         window[i] = v;
191         if ((i & 63) != 0)
192             v = -v;
193         if (i != 0)
194             window[512 - i] = v;
195     }
196
197     // Needed for avoiding shuffles in ASM implementations
198     for(i=0; i < 8; i++)
199         for(j=0; j < 16; j++)
200             window[512+16*i+j] = window[64*i+32-j];
201
202     for(i=0; i < 8; i++)
203         for(j=0; j < 16; j++)
204             window[512+128+16*i+j] = window[64*i+48-j];
205 }