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