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