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