00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/log.h"
00029 #include "libavutil/avassert.h"
00030 #include "swresample_internal.h"
00031
00032 #define WINDOW_TYPE 9
00033
00034
00035
00036 typedef struct ResampleContext {
00037 const AVClass *av_class;
00038 uint8_t *filter_bank;
00039 int filter_length;
00040 int ideal_dst_incr;
00041 int dst_incr;
00042 int index;
00043 int frac;
00044 int src_incr;
00045 int compensation_distance;
00046 int phase_shift;
00047 int phase_mask;
00048 int linear;
00049 double factor;
00050 enum AVSampleFormat format;
00051 int felem_size;
00052 int filter_shift;
00053 } ResampleContext;
00054
00058 static double bessel(double x){
00059 double v=1;
00060 double lastv=0;
00061 double t=1;
00062 int i;
00063 static const double inv[100]={
00064 1.0/( 1* 1), 1.0/( 2* 2), 1.0/( 3* 3), 1.0/( 4* 4), 1.0/( 5* 5), 1.0/( 6* 6), 1.0/( 7* 7), 1.0/( 8* 8), 1.0/( 9* 9), 1.0/(10*10),
00065 1.0/(11*11), 1.0/(12*12), 1.0/(13*13), 1.0/(14*14), 1.0/(15*15), 1.0/(16*16), 1.0/(17*17), 1.0/(18*18), 1.0/(19*19), 1.0/(20*20),
00066 1.0/(21*21), 1.0/(22*22), 1.0/(23*23), 1.0/(24*24), 1.0/(25*25), 1.0/(26*26), 1.0/(27*27), 1.0/(28*28), 1.0/(29*29), 1.0/(30*30),
00067 1.0/(31*31), 1.0/(32*32), 1.0/(33*33), 1.0/(34*34), 1.0/(35*35), 1.0/(36*36), 1.0/(37*37), 1.0/(38*38), 1.0/(39*39), 1.0/(40*40),
00068 1.0/(41*41), 1.0/(42*42), 1.0/(43*43), 1.0/(44*44), 1.0/(45*45), 1.0/(46*46), 1.0/(47*47), 1.0/(48*48), 1.0/(49*49), 1.0/(50*50),
00069 1.0/(51*51), 1.0/(52*52), 1.0/(53*53), 1.0/(54*54), 1.0/(55*55), 1.0/(56*56), 1.0/(57*57), 1.0/(58*58), 1.0/(59*59), 1.0/(60*60),
00070 1.0/(61*61), 1.0/(62*62), 1.0/(63*63), 1.0/(64*64), 1.0/(65*65), 1.0/(66*66), 1.0/(67*67), 1.0/(68*68), 1.0/(69*69), 1.0/(70*70),
00071 1.0/(71*71), 1.0/(72*72), 1.0/(73*73), 1.0/(74*74), 1.0/(75*75), 1.0/(76*76), 1.0/(77*77), 1.0/(78*78), 1.0/(79*79), 1.0/(80*80),
00072 1.0/(81*81), 1.0/(82*82), 1.0/(83*83), 1.0/(84*84), 1.0/(85*85), 1.0/(86*86), 1.0/(87*87), 1.0/(88*88), 1.0/(89*89), 1.0/(90*90),
00073 1.0/(91*91), 1.0/(92*92), 1.0/(93*93), 1.0/(94*94), 1.0/(95*95), 1.0/(96*96), 1.0/(97*97), 1.0/(98*98), 1.0/(99*99), 1.0/(10000)
00074 };
00075
00076 x= x*x/4;
00077 for(i=0; v != lastv; i++){
00078 lastv=v;
00079 t *= x*inv[i];
00080 v += t;
00081 }
00082 return v;
00083 }
00084
00092 static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int phase_count, int scale, int type){
00093 int ph, i;
00094 double x, y, w;
00095 double *tab = av_malloc(tap_count * sizeof(*tab));
00096 const int center= (tap_count-1)/2;
00097
00098 if (!tab)
00099 return AVERROR(ENOMEM);
00100
00101
00102 if (factor > 1.0)
00103 factor = 1.0;
00104
00105 for(ph=0;ph<phase_count;ph++) {
00106 double norm = 0;
00107 for(i=0;i<tap_count;i++) {
00108 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00109 if (x == 0) y = 1.0;
00110 else y = sin(x) / x;
00111 switch(type){
00112 case 0:{
00113 const float d= -0.5;
00114 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00115 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
00116 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
00117 break;}
00118 case 1:
00119 w = 2.0*x / (factor*tap_count) + M_PI;
00120 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
00121 break;
00122 default:
00123 w = 2.0*x / (factor*tap_count*M_PI);
00124 y *= bessel(type*sqrt(FFMAX(1-w*w, 0)));
00125 break;
00126 }
00127
00128 tab[i] = y;
00129 norm += y;
00130 }
00131
00132
00133 switch(c->format){
00134 case AV_SAMPLE_FMT_S16P:
00135 for(i=0;i<tap_count;i++)
00136 ((int16_t*)filter)[ph * tap_count + i] = av_clip(lrintf(tab[i] * scale / norm), INT16_MIN, INT16_MAX);
00137 break;
00138 case AV_SAMPLE_FMT_S32P:
00139 for(i=0;i<tap_count;i++)
00140 ((int32_t*)filter)[ph * tap_count + i] = av_clip(lrintf(tab[i] * scale / norm), INT32_MIN, INT32_MAX);
00141 break;
00142 case AV_SAMPLE_FMT_FLTP:
00143 for(i=0;i<tap_count;i++)
00144 ((float*)filter)[ph * tap_count + i] = tab[i] * scale / norm;
00145 break;
00146 case AV_SAMPLE_FMT_DBLP:
00147 for(i=0;i<tap_count;i++)
00148 ((double*)filter)[ph * tap_count + i] = tab[i] * scale / norm;
00149 break;
00150 }
00151 }
00152 #if 0
00153 {
00154 #define LEN 1024
00155 int j,k;
00156 double sine[LEN + tap_count];
00157 double filtered[LEN];
00158 double maxff=-2, minff=2, maxsf=-2, minsf=2;
00159 for(i=0; i<LEN; i++){
00160 double ss=0, sf=0, ff=0;
00161 for(j=0; j<LEN+tap_count; j++)
00162 sine[j]= cos(i*j*M_PI/LEN);
00163 for(j=0; j<LEN; j++){
00164 double sum=0;
00165 ph=0;
00166 for(k=0; k<tap_count; k++)
00167 sum += filter[ph * tap_count + k] * sine[k+j];
00168 filtered[j]= sum / (1<<FILTER_SHIFT);
00169 ss+= sine[j + center] * sine[j + center];
00170 ff+= filtered[j] * filtered[j];
00171 sf+= sine[j + center] * filtered[j];
00172 }
00173 ss= sqrt(2*ss/LEN);
00174 ff= sqrt(2*ff/LEN);
00175 sf= 2*sf/LEN;
00176 maxff= FFMAX(maxff, ff);
00177 minff= FFMIN(minff, ff);
00178 maxsf= FFMAX(maxsf, sf);
00179 minsf= FFMIN(minsf, sf);
00180 if(i%11==0){
00181 av_log(NULL, AV_LOG_ERROR, "i:%4d ss:%f ff:%13.6e-%13.6e sf:%13.6e-%13.6e\n", i, ss, maxff, minff, maxsf, minsf);
00182 minff=minsf= 2;
00183 maxff=maxsf= -2;
00184 }
00185 }
00186 }
00187 #endif
00188
00189 av_free(tab);
00190 return 0;
00191 }
00192
00193 ResampleContext *swri_resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff, enum AVSampleFormat format){
00194 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
00195 int phase_count= 1<<phase_shift;
00196
00197 if (!c || c->phase_shift != phase_shift || c->linear!=linear || c->factor != factor
00198 || c->filter_length != FFMAX((int)ceil(filter_size/factor), 1) || c->format != format) {
00199 c = av_mallocz(sizeof(*c));
00200 if (!c)
00201 return NULL;
00202
00203 c->format= format;
00204
00205 c->felem_size= av_get_bytes_per_sample(c->format);
00206
00207 switch(c->format){
00208 case AV_SAMPLE_FMT_S16P:
00209 c->filter_shift = 15;
00210 break;
00211 case AV_SAMPLE_FMT_S32P:
00212 c->filter_shift = 30;
00213 break;
00214 case AV_SAMPLE_FMT_FLTP:
00215 case AV_SAMPLE_FMT_DBLP:
00216 c->filter_shift = 0;
00217 break;
00218 default:
00219 av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
00220 return NULL;
00221 }
00222
00223 c->phase_shift = phase_shift;
00224 c->phase_mask = phase_count - 1;
00225 c->linear = linear;
00226 c->factor = factor;
00227 c->filter_length = FFMAX((int)ceil(filter_size/factor), 1);
00228 c->filter_bank = av_mallocz(c->filter_length*(phase_count+1)*c->felem_size);
00229 if (!c->filter_bank)
00230 goto error;
00231 if (build_filter(c, (void*)c->filter_bank, factor, c->filter_length, phase_count, 1<<c->filter_shift, WINDOW_TYPE))
00232 goto error;
00233 memcpy(c->filter_bank + (c->filter_length*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_length-1)*c->felem_size);
00234 memcpy(c->filter_bank + (c->filter_length*phase_count )*c->felem_size, c->filter_bank + (c->filter_length - 1)*c->felem_size, c->felem_size);
00235 }
00236
00237 c->compensation_distance= 0;
00238 if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
00239 goto error;
00240 c->ideal_dst_incr= c->dst_incr;
00241
00242 c->index= -phase_count*((c->filter_length-1)/2);
00243 c->frac= 0;
00244
00245 return c;
00246 error:
00247 av_free(c->filter_bank);
00248 av_free(c);
00249 return NULL;
00250 }
00251
00252 void swri_resample_free(ResampleContext **c){
00253 if(!*c)
00254 return;
00255 av_freep(&(*c)->filter_bank);
00256 av_freep(c);
00257 }
00258
00259 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
00260 ResampleContext *c;
00261 int ret;
00262
00263 if (!s || compensation_distance < 0)
00264 return AVERROR(EINVAL);
00265 if (!compensation_distance && sample_delta)
00266 return AVERROR(EINVAL);
00267 if (!s->resample) {
00268 s->flags |= SWR_FLAG_RESAMPLE;
00269 ret = swr_init(s);
00270 if (ret < 0)
00271 return ret;
00272 }
00273 c= s->resample;
00274 c->compensation_distance= compensation_distance;
00275 if (compensation_distance)
00276 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00277 else
00278 c->dst_incr = c->ideal_dst_incr;
00279 return 0;
00280 }
00281
00282 #define RENAME(N) N ## _int16
00283 #define FILTER_SHIFT 15
00284 #define DELEM int16_t
00285 #define FELEM int16_t
00286 #define FELEM2 int32_t
00287 #define FELEML int64_t
00288 #define FELEM_MAX INT16_MAX
00289 #define FELEM_MIN INT16_MIN
00290 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00291 d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
00292 #include "resample_template.c"
00293
00294 #undef RENAME
00295 #undef FELEM
00296 #undef FELEM2
00297 #undef DELEM
00298 #undef FELEML
00299 #undef OUT
00300 #undef FELEM_MIN
00301 #undef FELEM_MAX
00302 #undef FILTER_SHIFT
00303
00304
00305 #define RENAME(N) N ## _int32
00306 #define FILTER_SHIFT 30
00307 #define DELEM int32_t
00308 #define FELEM int32_t
00309 #define FELEM2 int64_t
00310 #define FELEML int64_t
00311 #define FELEM_MAX INT32_MAX
00312 #define FELEM_MIN INT32_MIN
00313 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00314 d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
00315 #include "resample_template.c"
00316
00317 #undef RENAME
00318 #undef FELEM
00319 #undef FELEM2
00320 #undef DELEM
00321 #undef FELEML
00322 #undef OUT
00323 #undef FELEM_MIN
00324 #undef FELEM_MAX
00325 #undef FILTER_SHIFT
00326
00327
00328 #define RENAME(N) N ## _float
00329 #define FILTER_SHIFT 0
00330 #define DELEM float
00331 #define FELEM float
00332 #define FELEM2 float
00333 #define FELEML float
00334 #define OUT(d, v) d = v
00335 #include "resample_template.c"
00336
00337 #undef RENAME
00338 #undef FELEM
00339 #undef FELEM2
00340 #undef DELEM
00341 #undef FELEML
00342 #undef OUT
00343 #undef FELEM_MIN
00344 #undef FELEM_MAX
00345 #undef FILTER_SHIFT
00346
00347
00348 #define RENAME(N) N ## _double
00349 #define FILTER_SHIFT 0
00350 #define DELEM double
00351 #define FELEM double
00352 #define FELEM2 double
00353 #define FELEML double
00354 #define OUT(d, v) d = v
00355 #include "resample_template.c"
00356
00357
00358 int swri_multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
00359 int i, ret= -1;
00360
00361 for(i=0; i<dst->ch_count; i++){
00362 if(c->format == AV_SAMPLE_FMT_S16P) ret= swri_resample_int16(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00363 if(c->format == AV_SAMPLE_FMT_S32P) ret= swri_resample_int32(c, (int32_t*)dst->ch[i], (const int32_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00364 if(c->format == AV_SAMPLE_FMT_FLTP) ret= swri_resample_float(c, (float *)dst->ch[i], (const float *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00365 if(c->format == AV_SAMPLE_FMT_DBLP) ret= swri_resample_double(c,(double *)dst->ch[i], (const double *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00366 }
00367
00368 return ret;
00369 }
00370
00371 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
00372 ResampleContext *c = s->resample;
00373 if(c){
00374 int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
00375 num <<= c->phase_shift;
00376 num -= c->index;
00377 num *= c->src_incr;
00378 num -= c->frac;
00379
00380 return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
00381 }else{
00382 return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
00383 }
00384 }