• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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     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     /* if upsampling, only need to interpolate, no filter */
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; //first order derivative = -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         /* normalize so that an uniform color remains the same */
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     c->src_incr= out_rate;
00211     c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
00212     c->index= -phase_count*((c->filter_length-1)/2);
00213 
00214     return c;
00215 error:
00216     av_free(c->filter_bank);
00217     av_free(c);
00218     return NULL;
00219 }
00220 
00221 void av_resample_close(AVResampleContext *c){
00222     av_freep(&c->filter_bank);
00223     av_freep(&c);
00224 }
00225 
00226 void av_resample_compensate(AVResampleContext *c, int sample_delta, int compensation_distance){
00227 //    sample_delta += (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->compensation_distance / c->ideal_dst_incr;
00228     c->compensation_distance= compensation_distance;
00229     c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
00230 }
00231 
00232 int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx){
00233     int dst_index, i;
00234     int index= c->index;
00235     int frac= c->frac;
00236     int dst_incr_frac= c->dst_incr % c->src_incr;
00237     int dst_incr=      c->dst_incr / c->src_incr;
00238     int compensation_distance= c->compensation_distance;
00239 
00240   if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
00241         int64_t index2= ((int64_t)index)<<32;
00242         int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
00243         dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
00244 
00245         for(dst_index=0; dst_index < dst_size; dst_index++){
00246             dst[dst_index] = src[index2>>32];
00247             index2 += incr;
00248         }
00249         frac += dst_index * dst_incr_frac;
00250         index += dst_index * dst_incr;
00251         index += frac / c->src_incr;
00252         frac %= c->src_incr;
00253   }else{
00254     for(dst_index=0; dst_index < dst_size; dst_index++){
00255         FELEM *filter= c->filter_bank + c->filter_length*(index & c->phase_mask);
00256         int sample_index= index >> c->phase_shift;
00257         FELEM2 val=0;
00258 
00259         if(sample_index < 0){
00260             for(i=0; i<c->filter_length; i++)
00261                 val += src[FFABS(sample_index + i) % src_size] * filter[i];
00262         }else if(sample_index + c->filter_length > src_size){
00263             break;
00264         }else if(c->linear){
00265             FELEM2 v2=0;
00266             for(i=0; i<c->filter_length; i++){
00267                 val += src[sample_index + i] * (FELEM2)filter[i];
00268                 v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_length];
00269             }
00270             val+=(v2-val)*(FELEML)frac / c->src_incr;
00271         }else{
00272             for(i=0; i<c->filter_length; i++){
00273                 val += src[sample_index + i] * (FELEM2)filter[i];
00274             }
00275         }
00276 
00277 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
00278         dst[dst_index] = av_clip_int16(lrintf(val));
00279 #else
00280         val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
00281         dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
00282 #endif
00283 
00284         frac += dst_incr_frac;
00285         index += dst_incr;
00286         if(frac >= c->src_incr){
00287             frac -= c->src_incr;
00288             index++;
00289         }
00290 
00291         if(dst_index + 1 == compensation_distance){
00292             compensation_distance= 0;
00293             dst_incr_frac= c->ideal_dst_incr % c->src_incr;
00294             dst_incr=      c->ideal_dst_incr / c->src_incr;
00295         }
00296     }
00297   }
00298     *consumed= FFMAX(index, 0) >> c->phase_shift;
00299     if(index>=0) index &= c->phase_mask;
00300 
00301     if(compensation_distance){
00302         compensation_distance -= dst_index;
00303         assert(compensation_distance > 0);
00304     }
00305     if(update_ctx){
00306         c->frac= frac;
00307         c->index= index;
00308         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
00309         c->compensation_distance= compensation_distance;
00310     }
00311 #if 0
00312     if(update_ctx && !c->compensation_distance){
00313 #undef rand
00314         av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
00315 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
00316     }
00317 #endif
00318 
00319     return dst_index;
00320 }
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1