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