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