optimize bessel function instead of trusting gcc to do trivial optimizations (as...
[libav.git] / libavcodec / resample2.c
CommitLineData
aaaf1635
MN
1/*
2 * audio resampling
3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
aaaf1635
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
aaaf1635 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
aaaf1635
MN
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
aaaf1635
MN
20 *
21 */
115329f1 22
aaaf1635
MN
23/**
24 * @file resample2.c
25 * audio resampling
26 * @author Michael Niedermayer <michaelni@gmx.at>
27 */
28
29#include "avcodec.h"
30#include "common.h"
1ac31760 31#include "dsputil.h"
aaaf1635 32
c252b26d 33#ifndef CONFIG_RESAMPLE_HP
aaaf1635
MN
34#define FILTER_SHIFT 15
35
51a88020
MN
36#define FELEM int16_t
37#define FELEM2 int32_t
38#define FELEM_MAX INT16_MAX
39#define FELEM_MIN INT16_MIN
14f887ef 40#define WINDOW_TYPE 9
51a88020 41#else
ca6940f8 42#define FILTER_SHIFT 30
51a88020
MN
43
44#define FELEM int32_t
45#define FELEM2 int64_t
46#define FELEM_MAX INT32_MAX
47#define FELEM_MIN INT32_MIN
14f887ef 48#define WINDOW_TYPE 12
51a88020
MN
49#endif
50
51
aaaf1635 52typedef struct AVResampleContext{
51a88020 53 FELEM *filter_bank;
aaaf1635
MN
54 int filter_length;
55 int ideal_dst_incr;
56 int dst_incr;
57 int index;
58 int frac;
59 int src_incr;
60 int compensation_distance;
ed861c6b
MN
61 int phase_shift;
62 int phase_mask;
63 int linear;
aaaf1635
MN
64}AVResampleContext;
65
66/**
67 * 0th order modified bessel function of the first kind.
68 */
7b49ce2e 69static double bessel(double x){
aaaf1635
MN
70 double v=1;
71 double t=1;
72 int i;
115329f1 73
50df67d3 74 x= x*x/4;
aaaf1635 75 for(i=1; i<50; i++){
50df67d3
MN
76 t *= x/(i*i);
77 v += t;
aaaf1635
MN
78 }
79 return v;
80}
81
82/**
83 * builds a polyphase filterbank.
84 * @param factor resampling factor
85 * @param scale wanted sum of coefficients for each filter
20cf58c3 86 * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2..16->kaiser windowed sinc beta=2..16
aaaf1635 87 */
51a88020 88void av_build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){
aaaf1635
MN
89 int ph, i, v;
90 double x, y, w, tab[tap_count];
91 const int center= (tap_count-1)/2;
92
93 /* if upsampling, only need to interpolate, no filter */
94 if (factor > 1.0)
95 factor = 1.0;
96
97 for(ph=0;ph<phase_count;ph++) {
98 double norm = 0;
aaaf1635
MN
99 for(i=0;i<tap_count;i++) {
100 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
101 if (x == 0) y = 1.0;
102 else y = sin(x) / x;
103 switch(type){
104 case 0:{
105 const float d= -0.5; //first order derivative = -0.5
106 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
107 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
108 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
109 break;}
110 case 1:
111 w = 2.0*x / (factor*tap_count) + M_PI;
112 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
113 break;
20cf58c3 114 default:
aaaf1635 115 w = 2.0*x / (factor*tap_count*M_PI);
20cf58c3 116 y *= bessel(type*sqrt(FFMAX(1-w*w, 0)));
aaaf1635
MN
117 break;
118 }
119
120 tab[i] = y;
121 norm += y;
122 }
123
124 /* normalize so that an uniform color remains the same */
125 for(i=0;i<tap_count;i++) {
66a148a1 126 v = av_clip(lrintf(tab[i] * scale / norm), FELEM_MIN, FELEM_MAX);
aaaf1635 127 filter[ph * tap_count + i] = v;
aaaf1635
MN
128 }
129 }
ae232dd7
MN
130#if 0
131 {
132#define LEN 1024
133 int j,k;
134 double sine[LEN + tap_count];
135 double filtered[LEN];
136 double maxff=-2, minff=2, maxsf=-2, minsf=2;
137 for(i=0; i<LEN; i++){
138 double ss=0, sf=0, ff=0;
139 for(j=0; j<LEN+tap_count; j++)
140 sine[j]= cos(i*j*M_PI/LEN);
141 for(j=0; j<LEN; j++){
142 double sum=0;
143 ph=0;
144 for(k=0; k<tap_count; k++)
145 sum += filter[ph * tap_count + k] * sine[k+j];
146 filtered[j]= sum / (1<<FILTER_SHIFT);
147 ss+= sine[j + center] * sine[j + center];
148 ff+= filtered[j] * filtered[j];
149 sf+= sine[j + center] * filtered[j];
150 }
151 ss= sqrt(2*ss/LEN);
152 ff= sqrt(2*ff/LEN);
153 sf= 2*sf/LEN;
154 maxff= FFMAX(maxff, ff);
155 minff= FFMIN(minff, ff);
156 maxsf= FFMAX(maxsf, sf);
157 minsf= FFMIN(minsf, sf);
158 if(i%11==0){
159 av_log(NULL, AV_LOG_ERROR, "i:%4d ss:%f ff:%f-%f sf:%f-%f\n", i, ss, maxff, minff, maxsf, minsf);
160 minff=minsf= 2;
161 maxff=maxsf= -2;
162 }
163 }
164 }
165#endif
aaaf1635
MN
166}
167
168/**
169 * initalizes a audio resampler.
170 * note, if either rate is not a integer then simply scale both rates up so they are
171 */
6e225de2 172AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
aaaf1635 173 AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
6e225de2 174 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
ed861c6b 175 int phase_count= 1<<phase_shift;
115329f1 176
ed861c6b
MN
177 c->phase_shift= phase_shift;
178 c->phase_mask= phase_count-1;
179 c->linear= linear;
aaaf1635 180
f0ff20a1 181 c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);
ed861c6b 182 c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
14f887ef 183 av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE);
ed861c6b
MN
184 memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
185 c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
aaaf1635
MN
186
187 c->src_incr= out_rate;
ed861c6b
MN
188 c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
189 c->index= -phase_count*((c->filter_length-1)/2);
aaaf1635
MN
190
191 return c;
192}
193
194void av_resample_close(AVResampleContext *c){
195 av_freep(&c->filter_bank);
196 av_freep(&c);
197}
198
788d7a8c
MN
199/**
200 * Compensates samplerate/timestamp drift. The compensation is done by changing
201 * the resampler parameters, so no audible clicks or similar distortions ocur
202 * @param compensation_distance distance in output samples over which the compensation should be performed
203 * @param sample_delta number of output samples which should be output less
204 *
205 * example: av_resample_compensate(c, 10, 500)
206 * here instead of 510 samples only 500 samples would be output
207 *
115329f1 208 * note, due to rounding the actual compensation might be slightly different,
788d7a8c
MN
209 * especially if the compensation_distance is large and the in_rate used during init is small
210 */
aaaf1635 211void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
08f7073a 212// sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
aaaf1635 213 c->compensation_distance= compensation_distance;
08f7073a 214 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
aaaf1635
MN
215}
216
217/**
218 * resamples.
219 * @param src an array of unconsumed samples
220 * @param consumed the number of samples of src which have been consumed are returned here
221 * @param src_size the number of unconsumed samples available
222 * @param dst_size the amount of space in samples available in dst
223 * @param update_ctx if this is 0 then the context wont be modified, that way several channels can be resampled with the same context
224 * @return the number of samples written in dst or -1 if an error occured
225 */
226int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
227 int dst_index, i;
228 int index= c->index;
229 int frac= c->frac;
230 int dst_incr_frac= c->dst_incr % c->src_incr;
231 int dst_incr= c->dst_incr / c->src_incr;
80e85288 232 int compensation_distance= c->compensation_distance;
53f0090d 233
6cb5dcb3 234 if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
53f0090d
MN
235 int64_t index2= ((int64_t)index)<<32;
236 int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
237 dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
115329f1 238
6cb5dcb3 239 for(dst_index=0; dst_index < dst_size; dst_index++){
53f0090d
MN
240 dst[dst_index] = src[index2>>32];
241 index2 += incr;
6cb5dcb3 242 }
53f0090d
MN
243 frac += dst_index * dst_incr_frac;
244 index += dst_index * dst_incr;
245 index += frac / c->src_incr;
246 frac %= c->src_incr;
6cb5dcb3 247 }else{
aaaf1635 248 for(dst_index=0; dst_index < dst_size; dst_index++){
ed861c6b
MN
249 FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
250 int sample_index= index >> c->phase_shift;
51a88020 251 FELEM2 val=0;
115329f1 252
aaaf1635
MN
253 if(sample_index < 0){
254 for(i=0; i<c->filter_length; i++)
c26abfa5 255 val += src[FFABS(sample_index + i) % src_size] * filter[i];
aaaf1635
MN
256 }else if(sample_index + c->filter_length > src_size){
257 break;
ed861c6b 258 }else if(c->linear){
aaaf1635 259 int64_t v=0;
f25ba8b3 260 int sub_phase= (frac<<8) / c->src_incr;
aaaf1635 261 for(i=0; i<c->filter_length; i++){
f25ba8b3 262 int64_t coeff= filter[i]*(256 - sub_phase) + filter[i + c->filter_length]*sub_phase;
aaaf1635
MN
263 v += src[sample_index + i] * coeff;
264 }
f25ba8b3 265 val= v>>8;
ed861c6b 266 }else{
aaaf1635 267 for(i=0; i<c->filter_length; i++){
51a88020 268 val += src[sample_index + i] * (FELEM2)filter[i];
aaaf1635 269 }
aaaf1635
MN
270 }
271
272 val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
273 dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
274
275 frac += dst_incr_frac;
276 index += dst_incr;
277 if(frac >= c->src_incr){
278 frac -= c->src_incr;
279 index++;
280 }
80e85288
MN
281
282 if(dst_index + 1 == compensation_distance){
283 compensation_distance= 0;
284 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
285 dst_incr= c->ideal_dst_incr / c->src_incr;
286 }
aaaf1635 287 }
6cb5dcb3 288 }
ed861c6b 289 *consumed= FFMAX(index, 0) >> c->phase_shift;
4e255822 290 if(index>=0) index &= c->phase_mask;
b9d2085b 291
80e85288
MN
292 if(compensation_distance){
293 compensation_distance -= dst_index;
294 assert(compensation_distance > 0);
295 }
aaaf1635 296 if(update_ctx){
aaaf1635 297 c->frac= frac;
b9d2085b 298 c->index= index;
80e85288
MN
299 c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
300 c->compensation_distance= compensation_distance;
aaaf1635 301 }
115329f1 302#if 0
08f7073a
MN
303 if(update_ctx && !c->compensation_distance){
304#undef rand
305 av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
306av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
307 }
308#endif
115329f1 309
aaaf1635
MN
310 return dst_index;
311}