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