00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "avcodec.h"
00028 #include "common.h"
00029 #include "dsputil.h"
00030
00031 #if 1
00032 #define FILTER_SHIFT 15
00033
00034 #define FELEM int16_t
00035 #define FELEM2 int32_t
00036 #define FELEM_MAX INT16_MAX
00037 #define FELEM_MIN INT16_MIN
00038 #else
00039 #define FILTER_SHIFT 22
00040
00041 #define FELEM int32_t
00042 #define FELEM2 int64_t
00043 #define FELEM_MAX INT32_MAX
00044 #define FELEM_MIN INT32_MIN
00045 #endif
00046
00047
00048 typedef struct AVResampleContext{
00049 FELEM *filter_bank;
00050 int filter_length;
00051 int ideal_dst_incr;
00052 int dst_incr;
00053 int index;
00054 int frac;
00055 int src_incr;
00056 int compensation_distance;
00057 int phase_shift;
00058 int phase_mask;
00059 int linear;
00060 }AVResampleContext;
00061
00065 double bessel(double x){
00066 double v=1;
00067 double t=1;
00068 int i;
00069
00070 for(i=1; i<50; i++){
00071 t *= i;
00072 v += pow(x*x/4, i)/(t*t);
00073 }
00074 return v;
00075 }
00076
00083 void av_build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){
00084 int ph, i, v;
00085 double x, y, w, tab[tap_count];
00086 const int center= (tap_count-1)/2;
00087
00088
00089 if (factor > 1.0)
00090 factor = 1.0;
00091
00092 for(ph=0;ph<phase_count;ph++) {
00093 double norm = 0;
00094 double e= 0;
00095 for(i=0;i<tap_count;i++) {
00096 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00097 if (x == 0) y = 1.0;
00098 else y = sin(x) / x;
00099 switch(type){
00100 case 0:{
00101 const float d= -0.5;
00102 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00103 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
00104 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
00105 break;}
00106 case 1:
00107 w = 2.0*x / (factor*tap_count) + M_PI;
00108 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
00109 break;
00110 case 2:
00111 w = 2.0*x / (factor*tap_count*M_PI);
00112 y *= bessel(16*sqrt(FFMAX(1-w*w, 0)));
00113 break;
00114 }
00115
00116 tab[i] = y;
00117 norm += y;
00118 }
00119
00120
00121 for(i=0;i<tap_count;i++) {
00122 v = clip(lrintf(tab[i] * scale / norm + e), FELEM_MIN, FELEM_MAX);
00123 filter[ph * tap_count + i] = v;
00124 e += tab[i] * scale / norm - v;
00125 }
00126 }
00127 }
00128
00133 AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
00134 AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
00135 double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
00136 int phase_count= 1<<phase_shift;
00137
00138 c->phase_shift= phase_shift;
00139 c->phase_mask= phase_count-1;
00140 c->linear= linear;
00141
00142 c->filter_length= FFMAX(ceil(filter_size/factor), 1);
00143 c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
00144 av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, 1);
00145 memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
00146 c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
00147
00148 c->src_incr= out_rate;
00149 c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
00150 c->index= -phase_count*((c->filter_length-1)/2);
00151
00152 return c;
00153 }
00154
00155 void av_resample_close(AVResampleContext *c){
00156 av_freep(&c->filter_bank);
00157 av_freep(&c);
00158 }
00159
00172 void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
00173
00174 c->compensation_distance= compensation_distance;
00175 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00176 }
00177
00187 int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
00188 int dst_index, i;
00189 int index= c->index;
00190 int frac= c->frac;
00191 int dst_incr_frac= c->dst_incr % c->src_incr;
00192 int dst_incr= c->dst_incr / c->src_incr;
00193 int compensation_distance= c->compensation_distance;
00194
00195 if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
00196 int64_t index2= ((int64_t)index)<<32;
00197 int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
00198 dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
00199
00200 for(dst_index=0; dst_index < dst_size; dst_index++){
00201 dst[dst_index] = src[index2>>32];
00202 index2 += incr;
00203 }
00204 frac += dst_index * dst_incr_frac;
00205 index += dst_index * dst_incr;
00206 index += frac / c->src_incr;
00207 frac %= c->src_incr;
00208 }else{
00209 for(dst_index=0; dst_index < dst_size; dst_index++){
00210 FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
00211 int sample_index= index >> c->phase_shift;
00212 FELEM2 val=0;
00213
00214 if(sample_index < 0){
00215 for(i=0; i<c->filter_length; i++)
00216 val += src[ABS(sample_index + i) % src_size] * filter[i];
00217 }else if(sample_index + c->filter_length > src_size){
00218 break;
00219 }else if(c->linear){
00220 int64_t v=0;
00221 int sub_phase= (frac<<8) / c->src_incr;
00222 for(i=0; i<c->filter_length; i++){
00223 int64_t coeff= filter[i]*(256 - sub_phase) + filter[i + c->filter_length]*sub_phase;
00224 v += src[sample_index + i] * coeff;
00225 }
00226 val= v>>8;
00227 }else{
00228 for(i=0; i<c->filter_length; i++){
00229 val += src[sample_index + i] * (FELEM2)filter[i];
00230 }
00231 }
00232
00233 val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
00234 dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
00235
00236 frac += dst_incr_frac;
00237 index += dst_incr;
00238 if(frac >= c->src_incr){
00239 frac -= c->src_incr;
00240 index++;
00241 }
00242
00243 if(dst_index + 1 == compensation_distance){
00244 compensation_distance= 0;
00245 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
00246 dst_incr= c->ideal_dst_incr / c->src_incr;
00247 }
00248 }
00249 }
00250 *consumed= FFMAX(index, 0) >> c->phase_shift;
00251 if(index>=0) index &= c->phase_mask;
00252
00253 if(compensation_distance){
00254 compensation_distance -= dst_index;
00255 assert(compensation_distance > 0);
00256 }
00257 if(update_ctx){
00258 c->frac= frac;
00259 c->index= index;
00260 c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
00261 c->compensation_distance= compensation_distance;
00262 }
00263 #if 0
00264 if(update_ctx && !c->compensation_distance){
00265 #undef rand
00266 av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
00267 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
00268 }
00269 #endif
00270
00271 return dst_index;
00272 }