]> git.sesse.net Git - ffmpeg/blob - libswresample/resample_template.c
Merge commit 'f9157463dbcd2db8fe9504197c0c04d0d7d04f31'
[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 #    define RENAME(N) N ## _double
30 #    define FILTER_SHIFT 0
31 #    define DELEM  double
32 #    define FELEM  double
33 #    define FELEM2 double
34 #    define FELEML double
35 #    define OUT(d, v) d = v
36
37 #elif    defined(TEMPLATE_RESAMPLE_FLT)     \
38       || defined(TEMPLATE_RESAMPLE_FLT_SSE)
39
40 #    define FILTER_SHIFT 0
41 #    define DELEM  float
42 #    define FELEM  float
43 #    define FELEM2 float
44 #    define FELEML float
45 #    define OUT(d, v) d = v
46
47 #    if defined(TEMPLATE_RESAMPLE_FLT)
48 #        define RENAME(N) N ## _float
49 #    elif defined(TEMPLATE_RESAMPLE_FLT_SSE)
50 #        define COMMON_CORE COMMON_CORE_FLT_SSE
51 #        define LINEAR_CORE LINEAR_CORE_FLT_SSE
52 #        define RENAME(N) N ## _float_sse
53 #    endif
54
55 #elif defined(TEMPLATE_RESAMPLE_S32)
56 #    define RENAME(N) N ## _int32
57 #    define FILTER_SHIFT 30
58 #    define DELEM  int32_t
59 #    define FELEM  int32_t
60 #    define FELEM2 int64_t
61 #    define FELEML int64_t
62 #    define FELEM_MAX INT32_MAX
63 #    define FELEM_MIN INT32_MIN
64 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
65                       d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
66
67 #elif    defined(TEMPLATE_RESAMPLE_S16)      \
68       || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
69       || defined(TEMPLATE_RESAMPLE_S16_SSE2)
70
71 #    define FILTER_SHIFT 15
72 #    define DELEM  int16_t
73 #    define FELEM  int16_t
74 #    define FELEM2 int32_t
75 #    define FELEML int64_t
76 #    define FELEM_MAX INT16_MAX
77 #    define FELEM_MIN INT16_MIN
78 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
79                       d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
80
81 #    if defined(TEMPLATE_RESAMPLE_S16)
82 #        define RENAME(N) N ## _int16
83 #    elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
84 #        define COMMON_CORE COMMON_CORE_INT16_MMX2
85 #        define LINEAR_CORE LINEAR_CORE_INT16_MMX2
86 #        define RENAME(N) N ## _int16_mmx2
87 #    elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
88 #        define COMMON_CORE COMMON_CORE_INT16_SSE2
89 #        define LINEAR_CORE LINEAR_CORE_INT16_SSE2
90 #        define RENAME(N) N ## _int16_sse2
91 #    endif
92
93 #endif
94
95 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
96     int dst_index, i;
97     int index= c->index;
98     int frac= c->frac;
99     int dst_incr_frac= c->dst_incr % c->src_incr;
100     int dst_incr=      c->dst_incr / c->src_incr;
101     int compensation_distance= c->compensation_distance;
102
103     av_assert1(c->filter_shift == FILTER_SHIFT);
104     av_assert1(c->felem_size == sizeof(FELEM));
105
106     if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
107         int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
108         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
109         int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
110
111         dst_size= FFMIN(dst_size, new_size);
112
113         for(dst_index=0; dst_index < dst_size; dst_index++){
114             dst[dst_index] = src[index2>>32];
115             index2 += incr;
116         }
117         index += dst_index * dst_incr;
118         index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
119         frac   = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
120         av_assert2(index >= 0);
121         *consumed= index;
122         index = 0;
123     }else if(compensation_distance == 0 && !c->linear && index >= 0){
124         int sample_index = 0;
125         for(dst_index=0; dst_index < dst_size; dst_index++){
126             FELEM *filter;
127             sample_index += index >> c->phase_shift;
128             index &= c->phase_mask;
129             filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
130
131             if(sample_index + c->filter_length > src_size){
132                 break;
133             }else{
134 #ifdef COMMON_CORE
135                 COMMON_CORE
136 #else
137                 FELEM2 val=0;
138                 for(i=0; i<c->filter_length; i++){
139                     val += src[sample_index + i] * (FELEM2)filter[i];
140                 }
141                 OUT(dst[dst_index], val);
142 #endif
143             }
144
145             frac += dst_incr_frac;
146             index += dst_incr;
147             if(frac >= c->src_incr){
148                 frac -= c->src_incr;
149                 index++;
150             }
151         }
152         *consumed = sample_index;
153     }else{
154         int sample_index = 0;
155         for(dst_index=0; dst_index < dst_size; dst_index++){
156             FELEM *filter;
157             FELEM2 val=0;
158
159             sample_index += index >> c->phase_shift;
160             index &= c->phase_mask;
161             filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
162
163             if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
164                 break;
165             }else if(sample_index < 0){
166                 for(i=0; i<c->filter_length; i++)
167                     val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
168                 OUT(dst[dst_index], val);
169             }else if(c->linear){
170                 FELEM2 v2=0;
171 #ifdef LINEAR_CORE
172                 LINEAR_CORE
173 #else
174                 for(i=0; i<c->filter_length; i++){
175                     val += src[sample_index + i] * (FELEM2)filter[i];
176                     v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
177                 }
178 #endif
179                 val+=(v2-val)*(FELEML)frac / c->src_incr;
180                 OUT(dst[dst_index], val);
181             }else{
182 #ifdef COMMON_CORE
183                 COMMON_CORE
184 #else
185                 for(i=0; i<c->filter_length; i++){
186                     val += src[sample_index + i] * (FELEM2)filter[i];
187                 }
188                 OUT(dst[dst_index], val);
189 #endif
190             }
191
192             frac += dst_incr_frac;
193             index += dst_incr;
194             if(frac >= c->src_incr){
195                 frac -= c->src_incr;
196                 index++;
197             }
198
199             if(dst_index + 1 == compensation_distance){
200                 compensation_distance= 0;
201                 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
202                 dst_incr=      c->ideal_dst_incr / c->src_incr;
203             }
204         }
205         *consumed= FFMAX(sample_index, 0);
206         index += FFMIN(sample_index, 0) << c->phase_shift;
207
208         if(compensation_distance){
209             compensation_distance -= dst_index;
210             av_assert1(compensation_distance > 0);
211         }
212     }
213
214     if(update_ctx){
215         c->frac= frac;
216         c->index= index;
217         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
218         c->compensation_distance= compensation_distance;
219     }
220
221     return dst_index;
222 }
223
224 #undef COMMON_CORE
225 #undef LINEAR_CORE
226 #undef RENAME
227 #undef FILTER_SHIFT
228 #undef DELEM
229 #undef FELEM
230 #undef FELEM2
231 #undef FELEML
232 #undef FELEM_MAX
233 #undef FELEM_MIN
234 #undef OUT