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
00033 typedef struct ResampleContext {
00034 const AVClass *av_class;
00035 uint8_t *filter_bank;
00036 int filter_length;
00037 int filter_alloc;
00038 int ideal_dst_incr;
00039 int dst_incr;
00040 int index;
00041 int frac;
00042 int src_incr;
00043 int compensation_distance;
00044 int phase_shift;
00045 int phase_mask;
00046 int linear;
00047 enum SwrFilterType filter_type;
00048 int kaiser_beta;
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 av_assert2(i<99);
00082 }
00083 return v;
00084 }
00085
00094 static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale,
00095 int filter_type, int kaiser_beta){
00096 int ph, i;
00097 double x, y, w;
00098 double *tab = av_malloc(tap_count * sizeof(*tab));
00099 const int center= (tap_count-1)/2;
00100
00101 if (!tab)
00102 return AVERROR(ENOMEM);
00103
00104
00105 if (factor > 1.0)
00106 factor = 1.0;
00107
00108 for(ph=0;ph<phase_count;ph++) {
00109 double norm = 0;
00110 for(i=0;i<tap_count;i++) {
00111 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00112 if (x == 0) y = 1.0;
00113 else y = sin(x) / x;
00114 switch(filter_type){
00115 case SWR_FILTER_TYPE_CUBIC:{
00116 const float d= -0.5;
00117 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00118 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
00119 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
00120 break;}
00121 case SWR_FILTER_TYPE_BLACKMAN_NUTTALL:
00122 w = 2.0*x / (factor*tap_count) + M_PI;
00123 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
00124 break;
00125 case SWR_FILTER_TYPE_KAISER:
00126 w = 2.0*x / (factor*tap_count*M_PI);
00127 y *= bessel(kaiser_beta*sqrt(FFMAX(1-w*w, 0)));
00128 break;
00129 default:
00130 av_assert0(0);
00131 }
00132
00133 tab[i] = y;
00134 norm += y;
00135 }
00136
00137
00138 switch(c->format){
00139 case AV_SAMPLE_FMT_S16P:
00140 for(i=0;i<tap_count;i++)
00141 ((int16_t*)filter)[ph * alloc + i] = av_clip(lrintf(tab[i] * scale / norm), INT16_MIN, INT16_MAX);
00142 break;
00143 case AV_SAMPLE_FMT_S32P:
00144 for(i=0;i<tap_count;i++)
00145 ((int32_t*)filter)[ph * alloc + i] = av_clip(lrintf(tab[i] * scale / norm), INT32_MIN, INT32_MAX);
00146 break;
00147 case AV_SAMPLE_FMT_FLTP:
00148 for(i=0;i<tap_count;i++)
00149 ((float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
00150 break;
00151 case AV_SAMPLE_FMT_DBLP:
00152 for(i=0;i<tap_count;i++)
00153 ((double*)filter)[ph * alloc + i] = tab[i] * scale / norm;
00154 break;
00155 }
00156 }
00157 #if 0
00158 {
00159 #define LEN 1024
00160 int j,k;
00161 double sine[LEN + tap_count];
00162 double filtered[LEN];
00163 double maxff=-2, minff=2, maxsf=-2, minsf=2;
00164 for(i=0; i<LEN; i++){
00165 double ss=0, sf=0, ff=0;
00166 for(j=0; j<LEN+tap_count; j++)
00167 sine[j]= cos(i*j*M_PI/LEN);
00168 for(j=0; j<LEN; j++){
00169 double sum=0;
00170 ph=0;
00171 for(k=0; k<tap_count; k++)
00172 sum += filter[ph * tap_count + k] * sine[k+j];
00173 filtered[j]= sum / (1<<FILTER_SHIFT);
00174 ss+= sine[j + center] * sine[j + center];
00175 ff+= filtered[j] * filtered[j];
00176 sf+= sine[j + center] * filtered[j];
00177 }
00178 ss= sqrt(2*ss/LEN);
00179 ff= sqrt(2*ff/LEN);
00180 sf= 2*sf/LEN;
00181 maxff= FFMAX(maxff, ff);
00182 minff= FFMIN(minff, ff);
00183 maxsf= FFMAX(maxsf, sf);
00184 minsf= FFMIN(minsf, sf);
00185 if(i%11==0){
00186 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);
00187 minff=minsf= 2;
00188 maxff=maxsf= -2;
00189 }
00190 }
00191 }
00192 #endif
00193
00194 av_free(tab);
00195 return 0;
00196 }
00197
00198 ResampleContext *swri_resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
00199 double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta){
00200 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
00201 int phase_count= 1<<phase_shift;
00202
00203 if (!c || c->phase_shift != phase_shift || c->linear!=linear || c->factor != factor
00204 || c->filter_length != FFMAX((int)ceil(filter_size/factor), 1) || c->format != format
00205 || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) {
00206 c = av_mallocz(sizeof(*c));
00207 if (!c)
00208 return NULL;
00209
00210 c->format= format;
00211
00212 c->felem_size= av_get_bytes_per_sample(c->format);
00213
00214 switch(c->format){
00215 case AV_SAMPLE_FMT_S16P:
00216 c->filter_shift = 15;
00217 break;
00218 case AV_SAMPLE_FMT_S32P:
00219 c->filter_shift = 30;
00220 break;
00221 case AV_SAMPLE_FMT_FLTP:
00222 case AV_SAMPLE_FMT_DBLP:
00223 c->filter_shift = 0;
00224 break;
00225 default:
00226 av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
00227 return NULL;
00228 }
00229
00230 c->phase_shift = phase_shift;
00231 c->phase_mask = phase_count - 1;
00232 c->linear = linear;
00233 c->factor = factor;
00234 c->filter_length = FFMAX((int)ceil(filter_size/factor), 1);
00235 c->filter_alloc = FFALIGN(c->filter_length, 8);
00236 c->filter_bank = av_mallocz(c->filter_alloc*(phase_count+1)*c->felem_size);
00237 c->filter_type = filter_type;
00238 c->kaiser_beta = kaiser_beta;
00239 if (!c->filter_bank)
00240 goto error;
00241 if (build_filter(c, (void*)c->filter_bank, factor, c->filter_length, c->filter_alloc, phase_count, 1<<c->filter_shift, filter_type, kaiser_beta))
00242 goto error;
00243 memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
00244 memcpy(c->filter_bank + (c->filter_alloc*phase_count )*c->felem_size, c->filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size);
00245 }
00246
00247 c->compensation_distance= 0;
00248 if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
00249 goto error;
00250 c->ideal_dst_incr= c->dst_incr;
00251
00252 c->index= -phase_count*((c->filter_length-1)/2);
00253 c->frac= 0;
00254
00255 return c;
00256 error:
00257 av_free(c->filter_bank);
00258 av_free(c);
00259 return NULL;
00260 }
00261
00262 void swri_resample_free(ResampleContext **c){
00263 if(!*c)
00264 return;
00265 av_freep(&(*c)->filter_bank);
00266 av_freep(c);
00267 }
00268
00269 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
00270 ResampleContext *c;
00271 int ret;
00272
00273 if (!s || compensation_distance < 0)
00274 return AVERROR(EINVAL);
00275 if (!compensation_distance && sample_delta)
00276 return AVERROR(EINVAL);
00277 if (!s->resample) {
00278 s->flags |= SWR_FLAG_RESAMPLE;
00279 ret = swr_init(s);
00280 if (ret < 0)
00281 return ret;
00282 }
00283 c= s->resample;
00284 c->compensation_distance= compensation_distance;
00285 if (compensation_distance)
00286 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00287 else
00288 c->dst_incr = c->ideal_dst_incr;
00289 return 0;
00290 }
00291
00292 #define RENAME(N) N ## _int16
00293 #define FILTER_SHIFT 15
00294 #define DELEM int16_t
00295 #define FELEM int16_t
00296 #define FELEM2 int32_t
00297 #define FELEML int64_t
00298 #define FELEM_MAX INT16_MAX
00299 #define FELEM_MIN INT16_MIN
00300 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00301 d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
00302 #include "resample_template.c"
00303
00304 #undef RENAME
00305 #undef FELEM
00306 #undef FELEM2
00307 #undef DELEM
00308 #undef FELEML
00309 #undef OUT
00310 #undef FELEM_MIN
00311 #undef FELEM_MAX
00312 #undef FILTER_SHIFT
00313
00314
00315 #define RENAME(N) N ## _int32
00316 #define FILTER_SHIFT 30
00317 #define DELEM int32_t
00318 #define FELEM int32_t
00319 #define FELEM2 int64_t
00320 #define FELEML int64_t
00321 #define FELEM_MAX INT32_MAX
00322 #define FELEM_MIN INT32_MIN
00323 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00324 d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
00325 #include "resample_template.c"
00326
00327 #undef RENAME
00328 #undef FELEM
00329 #undef FELEM2
00330 #undef DELEM
00331 #undef FELEML
00332 #undef OUT
00333 #undef FELEM_MIN
00334 #undef FELEM_MAX
00335 #undef FILTER_SHIFT
00336
00337
00338 #define RENAME(N) N ## _float
00339 #define FILTER_SHIFT 0
00340 #define DELEM float
00341 #define FELEM float
00342 #define FELEM2 float
00343 #define FELEML float
00344 #define OUT(d, v) d = v
00345 #include "resample_template.c"
00346
00347 #undef RENAME
00348 #undef FELEM
00349 #undef FELEM2
00350 #undef DELEM
00351 #undef FELEML
00352 #undef OUT
00353 #undef FELEM_MIN
00354 #undef FELEM_MAX
00355 #undef FILTER_SHIFT
00356
00357
00358 #define RENAME(N) N ## _double
00359 #define FILTER_SHIFT 0
00360 #define DELEM double
00361 #define FELEM double
00362 #define FELEM2 double
00363 #define FELEML double
00364 #define OUT(d, v) d = v
00365 #include "resample_template.c"
00366
00367 #undef RENAME
00368 #undef FELEM
00369 #undef FELEM2
00370 #undef DELEM
00371 #undef FELEML
00372 #undef OUT
00373 #undef FELEM_MIN
00374 #undef FELEM_MAX
00375 #undef FILTER_SHIFT
00376
00377
00378 #if HAVE_MMXEXT_INLINE
00379 #include "x86/resample_mmx.h"
00380 #define COMMON_CORE COMMON_CORE_INT16_MMX2
00381 #define RENAME(N) N ## _int16_mmx2
00382 #define FILTER_SHIFT 15
00383 #define DELEM int16_t
00384 #define FELEM int16_t
00385 #define FELEM2 int32_t
00386 #define FELEML int64_t
00387 #define FELEM_MAX INT16_MAX
00388 #define FELEM_MIN INT16_MIN
00389 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00390 d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
00391 #include "resample_template.c"
00392
00393 #undef COMMON_CORE
00394 #undef RENAME
00395 #undef FELEM
00396 #undef FELEM2
00397 #undef DELEM
00398 #undef FELEML
00399 #undef OUT
00400 #undef FELEM_MIN
00401 #undef FELEM_MAX
00402 #undef FILTER_SHIFT
00403
00404 #if HAVE_SSSE3_INLINE
00405 #define COMMON_CORE COMMON_CORE_INT16_SSSE3
00406 #define RENAME(N) N ## _int16_ssse3
00407 #define FILTER_SHIFT 15
00408 #define DELEM int16_t
00409 #define FELEM int16_t
00410 #define FELEM2 int32_t
00411 #define FELEML int64_t
00412 #define FELEM_MAX INT16_MAX
00413 #define FELEM_MIN INT16_MIN
00414 #define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
00415 d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
00416 #include "resample_template.c"
00417 #endif
00418 #endif // HAVE_MMXEXT_INLINE
00419
00420 int swri_multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
00421 int i, ret= -1;
00422 int av_unused mm_flags = av_get_cpu_flags();
00423 int need_emms= 0;
00424
00425 for(i=0; i<dst->ch_count; i++){
00426 #if HAVE_MMXEXT_INLINE
00427 #if HAVE_SSSE3_INLINE
00428 if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_SSSE3)) ret= swri_resample_int16_ssse3(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00429 else
00430 #endif
00431 if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){
00432 ret= swri_resample_int16_mmx2 (c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
00433 need_emms= 1;
00434 } else
00435 #endif
00436 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);
00437 else 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);
00438 else 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);
00439 else 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);
00440 }
00441 if(need_emms)
00442 emms_c();
00443 return ret;
00444 }
00445
00446 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
00447 ResampleContext *c = s->resample;
00448 if(c){
00449 int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
00450 num <<= c->phase_shift;
00451 num -= c->index;
00452 num *= c->src_incr;
00453 num -= c->frac;
00454
00455 return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift);
00456 }else{
00457 return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
00458 }
00459 }