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