]> git.sesse.net Git - ffmpeg/blob - libswresample/resample_template.c
Rewrite main resampling loop (common and linear).
[ffmpeg] / libswresample / resample_template.c
1 /*
2  * audio resampling
3  * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * audio resampling
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #if    defined(TEMPLATE_RESAMPLE_DBL)     \
29     || defined(TEMPLATE_RESAMPLE_DBL_SSE2)
30
31 #    define FILTER_SHIFT 0
32 #    define DELEM  double
33 #    define FELEM  double
34 #    define FELEM2 double
35 #    define FELEML double
36 #    define OUT(d, v) d = v
37
38 #    if defined(TEMPLATE_RESAMPLE_DBL)
39 #        define RENAME(N) N ## _double
40 #    elif defined(TEMPLATE_RESAMPLE_DBL_SSE2)
41 #        define COMMON_CORE COMMON_CORE_DBL_SSE2
42 #        define LINEAR_CORE LINEAR_CORE_DBL_SSE2
43 #        define RENAME(N) N ## _double_sse2
44 #    endif
45
46 #elif    defined(TEMPLATE_RESAMPLE_FLT)     \
47       || defined(TEMPLATE_RESAMPLE_FLT_SSE) \
48       || defined(TEMPLATE_RESAMPLE_FLT_AVX)
49
50 #    define FILTER_SHIFT 0
51 #    define DELEM  float
52 #    define FELEM  float
53 #    define FELEM2 float
54 #    define FELEML float
55 #    define OUT(d, v) d = v
56
57 #    if defined(TEMPLATE_RESAMPLE_FLT)
58 #        define RENAME(N) N ## _float
59 #    elif defined(TEMPLATE_RESAMPLE_FLT_SSE)
60 #        define COMMON_CORE COMMON_CORE_FLT_SSE
61 #        define LINEAR_CORE LINEAR_CORE_FLT_SSE
62 #        define RENAME(N) N ## _float_sse
63 #    elif defined(TEMPLATE_RESAMPLE_FLT_AVX)
64 #        define COMMON_CORE COMMON_CORE_FLT_AVX
65 #        define LINEAR_CORE LINEAR_CORE_FLT_AVX
66 #        define RENAME(N) N ## _float_avx
67 #    endif
68
69 #elif defined(TEMPLATE_RESAMPLE_S32)
70 #    define RENAME(N) N ## _int32
71 #    define FILTER_SHIFT 30
72 #    define DELEM  int32_t
73 #    define FELEM  int32_t
74 #    define FELEM2 int64_t
75 #    define FELEML int64_t
76 #    define FELEM_MAX INT32_MAX
77 #    define FELEM_MIN INT32_MIN
78 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
79                       d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
80
81 #elif    defined(TEMPLATE_RESAMPLE_S16)      \
82       || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
83       || defined(TEMPLATE_RESAMPLE_S16_SSE2)
84
85 #    define FILTER_SHIFT 15
86 #    define DELEM  int16_t
87 #    define FELEM  int16_t
88 #    define FELEM2 int32_t
89 #    define FELEML int64_t
90 #    define FELEM_MAX INT16_MAX
91 #    define FELEM_MIN INT16_MIN
92 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
93                       d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
94
95 #    if defined(TEMPLATE_RESAMPLE_S16)
96 #        define RENAME(N) N ## _int16
97 #    elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
98 #        define COMMON_CORE COMMON_CORE_INT16_MMX2
99 #        define LINEAR_CORE LINEAR_CORE_INT16_MMX2
100 #        define RENAME(N) N ## _int16_mmx2
101 #    elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
102 #        define COMMON_CORE COMMON_CORE_INT16_SSE2
103 #        define LINEAR_CORE LINEAR_CORE_INT16_SSE2
104 #        define RENAME(N) N ## _int16_sse2
105 #    endif
106
107 #endif
108
109 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
110     int dst_index, i;
111     int index= c->index;
112     int frac= c->frac;
113     int dst_incr_frac= c->dst_incr % c->src_incr;
114     int dst_incr=      c->dst_incr / c->src_incr;
115     int compensation_distance= c->compensation_distance;
116
117     av_assert1(c->filter_shift == FILTER_SHIFT);
118     av_assert1(c->felem_size == sizeof(FELEM));
119
120     if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
121         int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
122         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
123         int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
124
125         dst_size= FFMIN(dst_size, new_size);
126
127         for(dst_index=0; dst_index < dst_size; dst_index++){
128             dst[dst_index] = src[index2>>32];
129             index2 += incr;
130         }
131         index += dst_index * dst_incr;
132         index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
133         frac   = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
134         av_assert2(index >= 0);
135         *consumed= index;
136         index = 0;
137     } else if (compensation_distance == 0 && index >= 0) {
138         int64_t end_index = (1 + src_size - c->filter_length) << c->phase_shift;
139         int64_t delta_frac = (end_index - index) * c->src_incr - c->frac;
140         int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
141         int n = FFMIN(dst_size, delta_n);
142         int sample_index;
143
144         if (!c->linear) {
145             sample_index = index >> c->phase_shift;
146             index &= c->phase_mask;
147             for (dst_index = 0; dst_index < n; dst_index++) {
148                 FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
149
150 #ifdef COMMON_CORE
151                 COMMON_CORE
152 #else
153                 FELEM2 val=0;
154                 for (i = 0; i < c->filter_length; i++) {
155                     val += src[sample_index + i] * (FELEM2)filter[i];
156                 }
157                 OUT(dst[dst_index], val);
158 #endif
159
160                 frac += dst_incr_frac;
161                 index += dst_incr;
162                 if (frac >= c->src_incr) {
163                     frac -= c->src_incr;
164                     index++;
165                 }
166                 sample_index += index >> c->phase_shift;
167                 index &= c->phase_mask;
168             }
169         } else {
170             sample_index = index >> c->phase_shift;
171             index &= c->phase_mask;
172             for (dst_index = 0; dst_index < n; dst_index++) {
173                 FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
174                 FELEM2 val=0, v2 = 0;
175
176 #ifdef LINEAR_CORE
177                 LINEAR_CORE
178 #else
179                 for (i = 0; i < c->filter_length; i++) {
180                     val += src[sample_index + i] * (FELEM2)filter[i];
181                     v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
182                 }
183 #endif
184                 val += (v2 - val) * (FELEML) frac / c->src_incr;
185                 OUT(dst[dst_index], val);
186
187                 frac += dst_incr_frac;
188                 index += dst_incr;
189                 if (frac >= c->src_incr) {
190                     frac -= c->src_incr;
191                     index++;
192                 }
193                 sample_index += index >> c->phase_shift;
194                 index &= c->phase_mask;
195             }
196         }
197
198         *consumed = sample_index;
199     } else {
200         int sample_index = 0;
201         for(dst_index=0; dst_index < dst_size; dst_index++){
202             FELEM *filter;
203             FELEM2 val=0;
204
205             sample_index += index >> c->phase_shift;
206             index &= c->phase_mask;
207             filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
208
209             if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
210                 break;
211             }else if(sample_index < 0){
212                 for(i=0; i<c->filter_length; i++)
213                     val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
214                 OUT(dst[dst_index], val);
215             }else if(c->linear){
216                 FELEM2 v2=0;
217 #ifdef LINEAR_CORE
218                 LINEAR_CORE
219 #else
220                 for(i=0; i<c->filter_length; i++){
221                     val += src[sample_index + i] * (FELEM2)filter[i];
222                     v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
223                 }
224 #endif
225                 val+=(v2-val)*(FELEML)frac / c->src_incr;
226                 OUT(dst[dst_index], val);
227             }else{
228 #ifdef COMMON_CORE
229                 COMMON_CORE
230 #else
231                 for(i=0; i<c->filter_length; i++){
232                     val += src[sample_index + i] * (FELEM2)filter[i];
233                 }
234                 OUT(dst[dst_index], val);
235 #endif
236             }
237
238             frac += dst_incr_frac;
239             index += dst_incr;
240             if(frac >= c->src_incr){
241                 frac -= c->src_incr;
242                 index++;
243             }
244
245             if(dst_index + 1 == compensation_distance){
246                 compensation_distance= 0;
247                 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
248                 dst_incr=      c->ideal_dst_incr / c->src_incr;
249             }
250         }
251         *consumed= FFMAX(sample_index, 0);
252         index += FFMIN(sample_index, 0) << c->phase_shift;
253
254         if(compensation_distance){
255             compensation_distance -= dst_index;
256             av_assert1(compensation_distance > 0);
257         }
258     }
259
260     if(update_ctx){
261         c->frac= frac;
262         c->index= index;
263         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
264         c->compensation_distance= compensation_distance;
265     }
266
267     return dst_index;
268 }
269
270 #undef COMMON_CORE
271 #undef LINEAR_CORE
272 #undef RENAME
273 #undef FILTER_SHIFT
274 #undef DELEM
275 #undef FELEM
276 #undef FELEM2
277 #undef FELEML
278 #undef FELEM_MAX
279 #undef FELEM_MIN
280 #undef OUT