00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00028 #include "avcodec.h"
00029 #include "dsputil.h"
00030 
00031 #ifndef CONFIG_RESAMPLE_HP
00032 #define FILTER_SHIFT 15
00033 
00034 #define FELEM int16_t
00035 #define FELEM2 int32_t
00036 #define FELEML int64_t
00037 #define FELEM_MAX INT16_MAX
00038 #define FELEM_MIN INT16_MIN
00039 #define WINDOW_TYPE 9
00040 #elif !defined(CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE)
00041 #define FILTER_SHIFT 30
00042 
00043 #define FELEM int32_t
00044 #define FELEM2 int64_t
00045 #define FELEML int64_t
00046 #define FELEM_MAX INT32_MAX
00047 #define FELEM_MIN INT32_MIN
00048 #define WINDOW_TYPE 12
00049 #else
00050 #define FILTER_SHIFT 0
00051 
00052 #define FELEM double
00053 #define FELEM2 double
00054 #define FELEML double
00055 #define WINDOW_TYPE 24
00056 #endif
00057 
00058 
00059 typedef struct AVResampleContext{
00060     FELEM *filter_bank;
00061     int filter_length;
00062     int ideal_dst_incr;
00063     int dst_incr;
00064     int index;
00065     int frac;
00066     int src_incr;
00067     int compensation_distance;
00068     int phase_shift;
00069     int phase_mask;
00070     int linear;
00071 }AVResampleContext;
00072 
00076 static double bessel(double x){
00077     double v=1;
00078     double t=1;
00079     int i;
00080 
00081     x= x*x/4;
00082     for(i=1; i<50; i++){
00083         t *= x/(i*i);
00084         v += t;
00085     }
00086     return v;
00087 }
00088 
00095 void av_build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){
00096     int ph, i;
00097     double x, y, w, tab[tap_count];
00098     const int center= (tap_count-1)/2;
00099 
00100     
00101     if (factor > 1.0)
00102         factor = 1.0;
00103 
00104     for(ph=0;ph<phase_count;ph++) {
00105         double norm = 0;
00106         for(i=0;i<tap_count;i++) {
00107             x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00108             if (x == 0) y = 1.0;
00109             else        y = sin(x) / x;
00110             switch(type){
00111             case 0:{
00112                 const float d= -0.5; 
00113                 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00114                 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*(            -x*x + x*x*x);
00115                 else      y=                       d*(-4 + 8*x - 5*x*x + x*x*x);
00116                 break;}
00117             case 1:
00118                 w = 2.0*x / (factor*tap_count) + M_PI;
00119                 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
00120                 break;
00121             default:
00122                 w = 2.0*x / (factor*tap_count*M_PI);
00123                 y *= bessel(type*sqrt(FFMAX(1-w*w, 0)));
00124                 break;
00125             }
00126 
00127             tab[i] = y;
00128             norm += y;
00129         }
00130 
00131         
00132         for(i=0;i<tap_count;i++) {
00133 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
00134             filter[ph * tap_count + i] = tab[i] / norm;
00135 #else
00136             filter[ph * tap_count + i] = av_clip(lrintf(tab[i] * scale / norm), FELEM_MIN, FELEM_MAX);
00137 #endif
00138         }
00139     }
00140 #if 0
00141     {
00142 #define LEN 1024
00143         int j,k;
00144         double sine[LEN + tap_count];
00145         double filtered[LEN];
00146         double maxff=-2, minff=2, maxsf=-2, minsf=2;
00147         for(i=0; i<LEN; i++){
00148             double ss=0, sf=0, ff=0;
00149             for(j=0; j<LEN+tap_count; j++)
00150                 sine[j]= cos(i*j*M_PI/LEN);
00151             for(j=0; j<LEN; j++){
00152                 double sum=0;
00153                 ph=0;
00154                 for(k=0; k<tap_count; k++)
00155                     sum += filter[ph * tap_count + k] * sine[k+j];
00156                 filtered[j]= sum / (1<<FILTER_SHIFT);
00157                 ss+= sine[j + center] * sine[j + center];
00158                 ff+= filtered[j] * filtered[j];
00159                 sf+= sine[j + center] * filtered[j];
00160             }
00161             ss= sqrt(2*ss/LEN);
00162             ff= sqrt(2*ff/LEN);
00163             sf= 2*sf/LEN;
00164             maxff= FFMAX(maxff, ff);
00165             minff= FFMIN(minff, ff);
00166             maxsf= FFMAX(maxsf, sf);
00167             minsf= FFMIN(minsf, sf);
00168             if(i%11==0){
00169                 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);
00170                 minff=minsf= 2;
00171                 maxff=maxsf= -2;
00172             }
00173         }
00174     }
00175 #endif
00176 }
00177 
00178 AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
00179     AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
00180     double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
00181     int phase_count= 1<<phase_shift;
00182 
00183     c->phase_shift= phase_shift;
00184     c->phase_mask= phase_count-1;
00185     c->linear= linear;
00186 
00187     c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);
00188     c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
00189     av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE);
00190     memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
00191     c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
00192 
00193     if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
00194         return NULL;
00195     c->ideal_dst_incr= c->dst_incr;
00196 
00197     c->index= -phase_count*((c->filter_length-1)/2);
00198 
00199     return c;
00200 }
00201 
00202 void av_resample_close(AVResampleContext *c){
00203     av_freep(&c->filter_bank);
00204     av_freep(&c);
00205 }
00206 
00207 void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
00208 
00209     c->compensation_distance= compensation_distance;
00210     c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00211 }
00212 
00213 int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
00214     int dst_index, i;
00215     int index= c->index;
00216     int frac= c->frac;
00217     int dst_incr_frac= c->dst_incr % c->src_incr;
00218     int dst_incr=      c->dst_incr / c->src_incr;
00219     int compensation_distance= c->compensation_distance;
00220 
00221   if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
00222         int64_t index2= ((int64_t)index)<<32;
00223         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
00224         dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
00225 
00226         for(dst_index=0; dst_index < dst_size; dst_index++){
00227             dst[dst_index] = src[index2>>32];
00228             index2 += incr;
00229         }
00230         index += dst_index * dst_incr;
00231         index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
00232         frac   = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
00233   }else{
00234     for(dst_index=0; dst_index < dst_size; dst_index++){
00235         FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
00236         int sample_index= index >> c->phase_shift;
00237         FELEM2 val=0;
00238 
00239         if(sample_index < 0){
00240             for(i=0; i<c->filter_length; i++)
00241                 val += src[FFABS(sample_index + i) % src_size] * filter[i];
00242         }else if(sample_index + c->filter_length > src_size){
00243             break;
00244         }else if(c->linear){
00245             FELEM2 v2=0;
00246             for(i=0; i<c->filter_length; i++){
00247                 val += src[sample_index + i] * (FELEM2)filter[i];
00248                 v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_length];
00249             }
00250             val+=(v2-val)*(FELEML)frac / c->src_incr;
00251         }else{
00252             for(i=0; i<c->filter_length; i++){
00253                 val += src[sample_index + i] * (FELEM2)filter[i];
00254             }
00255         }
00256 
00257 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
00258         dst[dst_index] = av_clip_int16(lrintf(val));
00259 #else
00260         val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
00261         dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
00262 #endif
00263 
00264         frac += dst_incr_frac;
00265         index += dst_incr;
00266         if(frac >= c->src_incr){
00267             frac -= c->src_incr;
00268             index++;
00269         }
00270 
00271         if(dst_index + 1 == compensation_distance){
00272             compensation_distance= 0;
00273             dst_incr_frac= c->ideal_dst_incr % c->src_incr;
00274             dst_incr=      c->ideal_dst_incr / c->src_incr;
00275         }
00276     }
00277   }
00278     *consumed= FFMAX(index, 0) >> c->phase_shift;
00279     if(index>=0) index &= c->phase_mask;
00280 
00281     if(compensation_distance){
00282         compensation_distance -= dst_index;
00283         assert(compensation_distance > 0);
00284     }
00285     if(update_ctx){
00286         c->frac= frac;
00287         c->index= index;
00288         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
00289         c->compensation_distance= compensation_distance;
00290     }
00291 #if 0
00292     if(update_ctx && !c->compensation_distance){
00293 #undef rand
00294         av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
00295 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
00296     }
00297 #endif
00298 
00299     return dst_index;
00300 }