• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/resample2.c

Go to the documentation of this file.
00001 /*
00002  * audio resampling
00003  * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     FELEM *filter_bank;
00061     int filter_length;
00062     int ideal_dst_incr;
00063     int dst_incr;
00064     int index;
00065     int frac;
00066     int src_incr;
00067     int compensation_distance;
00068     int phase_shift;
00069     int phase_mask;
00070     int linear;
00071 }AVResampleContext;
00072 
00076 static double bessel(double x){
00077     double v=1;
00078     double t=1;
00079     int i;
00080 
00081     x= x*x/4;
00082     for(i=1; i<50; i++){
00083         t *= x/(i*i);
00084         v += t;
00085     }
00086     return v;
00087 }
00088 
00095 void av_build_filter(FELEM *filter, double factor, int tap_count, int phase_count, int scale, int type){
00096     int ph, i;
00097     double x, y, w, tab[tap_count];
00098     const int center= (tap_count-1)/2;
00099 
00100     /* if upsampling, only need to interpolate, no filter */
00101     if (factor > 1.0)
00102         factor = 1.0;
00103 
00104     for(ph=0;ph<phase_count;ph++) {
00105         double norm = 0;
00106         for(i=0;i<tap_count;i++) {
00107             x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00108             if (x == 0) y = 1.0;
00109             else        y = sin(x) / x;
00110             switch(type){
00111             case 0:{
00112                 const float d= -0.5; //first order derivative = -0.5
00113                 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00114                 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*(            -x*x + x*x*x);
00115                 else      y=                       d*(-4 + 8*x - 5*x*x + x*x*x);
00116                 break;}
00117             case 1:
00118                 w = 2.0*x / (factor*tap_count) + M_PI;
00119                 y *= 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*w) - 0.0106411 * cos(3*w);
00120                 break;
00121             default:
00122                 w = 2.0*x / (factor*tap_count*M_PI);
00123                 y *= bessel(type*sqrt(FFMAX(1-w*w, 0)));
00124                 break;
00125             }
00126 
00127             tab[i] = y;
00128             norm += y;
00129         }
00130 
00131         /* normalize so that an uniform color remains the same */
00132         for(i=0;i<tap_count;i++) {
00133 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
00134             filter[ph * tap_count + i] = tab[i] / norm;
00135 #else
00136             filter[ph * tap_count + i] = av_clip(lrintf(tab[i] * scale / norm), FELEM_MIN, FELEM_MAX);
00137 #endif
00138         }
00139     }
00140 #if 0
00141     {
00142 #define LEN 1024
00143         int j,k;
00144         double sine[LEN + tap_count];
00145         double filtered[LEN];
00146         double maxff=-2, minff=2, maxsf=-2, minsf=2;
00147         for(i=0; i<LEN; i++){
00148             double ss=0, sf=0, ff=0;
00149             for(j=0; j<LEN+tap_count; j++)
00150                 sine[j]= cos(i*j*M_PI/LEN);
00151             for(j=0; j<LEN; j++){
00152                 double sum=0;
00153                 ph=0;
00154                 for(k=0; k<tap_count; k++)
00155                     sum += filter[ph * tap_count + k] * sine[k+j];
00156                 filtered[j]= sum / (1<<FILTER_SHIFT);
00157                 ss+= sine[j + center] * sine[j + center];
00158                 ff+= filtered[j] * filtered[j];
00159                 sf+= sine[j + center] * filtered[j];
00160             }
00161             ss= sqrt(2*ss/LEN);
00162             ff= sqrt(2*ff/LEN);
00163             sf= 2*sf/LEN;
00164             maxff= FFMAX(maxff, ff);
00165             minff= FFMIN(minff, ff);
00166             maxsf= FFMAX(maxsf, sf);
00167             minsf= FFMIN(minsf, sf);
00168             if(i%11==0){
00169                 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);
00170                 minff=minsf= 2;
00171                 maxff=maxsf= -2;
00172             }
00173         }
00174     }
00175 #endif
00176 }
00177 
00178 AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
00179     AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
00180     double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
00181     int phase_count= 1<<phase_shift;
00182 
00183     c->phase_shift= phase_shift;
00184     c->phase_mask= phase_count-1;
00185     c->linear= linear;
00186 
00187     c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);
00188     c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
00189     av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE);
00190     memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
00191     c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
00192 
00193     c->src_incr= out_rate;
00194     c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
00195     c->index= -phase_count*((c->filter_length-1)/2);
00196 
00197     return c;
00198 }
00199 
00200 void av_resample_close(AVResampleContext *c){
00201     av_freep(&c->filter_bank);
00202     av_freep(&c);
00203 }
00204 
00205 void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
00206 //    sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
00207     c->compensation_distance= compensation_distance;
00208     c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00209 }
00210 
00211 int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
00212     int dst_index, i;
00213     int index= c->index;
00214     int frac= c->frac;
00215     int dst_incr_frac= c->dst_incr % c->src_incr;
00216     int dst_incr=      c->dst_incr / c->src_incr;
00217     int compensation_distance= c->compensation_distance;
00218 
00219   if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
00220         int64_t index2= ((int64_t)index)<<32;
00221         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
00222         dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
00223 
00224         for(dst_index=0; dst_index < dst_size; dst_index++){
00225             dst[dst_index] = src[index2>>32];
00226             index2 += incr;
00227         }
00228         frac += dst_index * dst_incr_frac;
00229         index += dst_index * dst_incr;
00230         index += frac / c->src_incr;
00231         frac %= c->src_incr;
00232   }else{
00233     for(dst_index=0; dst_index < dst_size; dst_index++){
00234         FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
00235         int sample_index= index >> c->phase_shift;
00236         FELEM2 val=0;
00237 
00238         if(sample_index < 0){
00239             for(i=0; i<c->filter_length; i++)
00240                 val += src[FFABS(sample_index + i) % src_size] * filter[i];
00241         }else if(sample_index + c->filter_length > src_size){
00242             break;
00243         }else if(c->linear){
00244             FELEM2 v2=0;
00245             for(i=0; i<c->filter_length; i++){
00246                 val += src[sample_index + i] * (FELEM2)filter[i];
00247                 v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_length];
00248             }
00249             val+=(v2-val)*(FELEML)frac / c->src_incr;
00250         }else{
00251             for(i=0; i<c->filter_length; i++){
00252                 val += src[sample_index + i] * (FELEM2)filter[i];
00253             }
00254         }
00255 
00256 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
00257         dst[dst_index] = av_clip_int16(lrintf(val));
00258 #else
00259         val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
00260         dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
00261 #endif
00262 
00263         frac += dst_incr_frac;
00264         index += dst_incr;
00265         if(frac >= c->src_incr){
00266             frac -= c->src_incr;
00267             index++;
00268         }
00269 
00270         if(dst_index + 1 == compensation_distance){
00271             compensation_distance= 0;
00272             dst_incr_frac= c->ideal_dst_incr % c->src_incr;
00273             dst_incr=      c->ideal_dst_incr / c->src_incr;
00274         }
00275     }
00276   }
00277     *consumed= FFMAX(index, 0) >> c->phase_shift;
00278     if(index>=0) index &= c->phase_mask;
00279 
00280     if(compensation_distance){
00281         compensation_distance -= dst_index;
00282         assert(compensation_distance > 0);
00283     }
00284     if(update_ctx){
00285         c->frac= frac;
00286         c->index= index;
00287         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
00288         c->compensation_distance= compensation_distance;
00289     }
00290 #if 0
00291     if(update_ctx && !c->compensation_distance){
00292 #undef rand
00293         av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
00294 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
00295     }
00296 #endif
00297 
00298     return dst_index;
00299 }

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1