00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include "libavutil/common.h"
00023 #include "libavutil/libm.h"
00024 #include "libavutil/log.h"
00025 #include "internal.h"
00026 #include "audio_data.h"
00027 
00028 struct ResampleContext {
00029     AVAudioResampleContext *avr;
00030     AudioData *buffer;
00031     uint8_t *filter_bank;
00032     int filter_length;
00033     int ideal_dst_incr;
00034     int dst_incr;
00035     int index;
00036     int frac;
00037     int src_incr;
00038     int compensation_distance;
00039     int phase_shift;
00040     int phase_mask;
00041     int linear;
00042     enum AVResampleFilterType filter_type;
00043     int kaiser_beta;
00044     double factor;
00045     void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
00046     void (*resample_one)(struct ResampleContext *c, int no_filter, void *dst0,
00047                          int dst_index, const void *src0, int src_size,
00048                          int index, int frac);
00049 };
00050 
00051 
00052 
00053 #define CONFIG_RESAMPLE_DBL
00054 #include "resample_template.c"
00055 #undef CONFIG_RESAMPLE_DBL
00056 
00057 
00058 #define CONFIG_RESAMPLE_FLT
00059 #include "resample_template.c"
00060 #undef CONFIG_RESAMPLE_FLT
00061 
00062 
00063 #define CONFIG_RESAMPLE_S32
00064 #include "resample_template.c"
00065 #undef CONFIG_RESAMPLE_S32
00066 
00067 
00068 #include "resample_template.c"
00069 
00070 
00071 
00072 static double bessel(double x)
00073 {
00074     double v     = 1;
00075     double lastv = 0;
00076     double t     = 1;
00077     int i;
00078 
00079     x = x * x / 4;
00080     for (i = 1; v != lastv; i++) {
00081         lastv = v;
00082         t    *= x / (i * i);
00083         v    += t;
00084     }
00085     return v;
00086 }
00087 
00088 
00089 static int build_filter(ResampleContext *c)
00090 {
00091     int ph, i;
00092     double x, y, w, factor;
00093     double *tab;
00094     int tap_count    = c->filter_length;
00095     int phase_count  = 1 << c->phase_shift;
00096     const int center = (tap_count - 1) / 2;
00097 
00098     tab = av_malloc(tap_count * sizeof(*tab));
00099     if (!tab)
00100         return AVERROR(ENOMEM);
00101 
00102     
00103     factor = FFMIN(c->factor, 1.0);
00104 
00105     for (ph = 0; ph < phase_count; ph++) {
00106         double norm = 0;
00107         for (i = 0; i < tap_count; i++) {
00108             x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
00109             if (x == 0) y = 1.0;
00110             else        y = sin(x) / x;
00111             switch (c->filter_type) {
00112             case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
00113                 const float d = -0.5; 
00114                 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
00115                 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * (                -x*x + x*x*x);
00116                 else         y =                           d * (-4 + 8 * x - 5 * x*x + x*x*x);
00117                 break;
00118             }
00119             case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
00120                 w  = 2.0 * x / (factor * tap_count) + M_PI;
00121                 y *= 0.3635819 - 0.4891775 * cos(    w) +
00122                                  0.1365995 * cos(2 * w) -
00123                                  0.0106411 * cos(3 * w);
00124                 break;
00125             case AV_RESAMPLE_FILTER_TYPE_KAISER:
00126                 w  = 2.0 * x / (factor * tap_count * M_PI);
00127                 y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
00128                 break;
00129             }
00130 
00131             tab[i] = y;
00132             norm  += y;
00133         }
00134         
00135         for (i = 0; i < tap_count; i++)
00136             tab[i] = tab[i] / norm;
00137 
00138         c->set_filter(c->filter_bank, tab, ph, tap_count);
00139     }
00140 
00141     av_free(tab);
00142     return 0;
00143 }
00144 
00145 ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
00146 {
00147     ResampleContext *c;
00148     int out_rate    = avr->out_sample_rate;
00149     int in_rate     = avr->in_sample_rate;
00150     double factor   = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
00151     int phase_count = 1 << avr->phase_shift;
00152     int felem_size;
00153 
00154     if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
00155         avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
00156         avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
00157         avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
00158         av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
00159                "resampling: %s\n",
00160                av_get_sample_fmt_name(avr->internal_sample_fmt));
00161         return NULL;
00162     }
00163     c = av_mallocz(sizeof(*c));
00164     if (!c)
00165         return NULL;
00166 
00167     c->avr           = avr;
00168     c->phase_shift   = avr->phase_shift;
00169     c->phase_mask    = phase_count - 1;
00170     c->linear        = avr->linear_interp;
00171     c->factor        = factor;
00172     c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
00173     c->filter_type   = avr->filter_type;
00174     c->kaiser_beta   = avr->kaiser_beta;
00175 
00176     switch (avr->internal_sample_fmt) {
00177     case AV_SAMPLE_FMT_DBLP:
00178         c->resample_one  = resample_one_dbl;
00179         c->set_filter    = set_filter_dbl;
00180         break;
00181     case AV_SAMPLE_FMT_FLTP:
00182         c->resample_one  = resample_one_flt;
00183         c->set_filter    = set_filter_flt;
00184         break;
00185     case AV_SAMPLE_FMT_S32P:
00186         c->resample_one  = resample_one_s32;
00187         c->set_filter    = set_filter_s32;
00188         break;
00189     case AV_SAMPLE_FMT_S16P:
00190         c->resample_one  = resample_one_s16;
00191         c->set_filter    = set_filter_s16;
00192         break;
00193     }
00194 
00195     felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
00196     c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
00197     if (!c->filter_bank)
00198         goto error;
00199 
00200     if (build_filter(c) < 0)
00201         goto error;
00202 
00203     memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
00204            c->filter_bank, (c->filter_length - 1) * felem_size);
00205     memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
00206            &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
00207 
00208     c->compensation_distance = 0;
00209     if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
00210                    in_rate * (int64_t)phase_count, INT32_MAX / 2))
00211         goto error;
00212     c->ideal_dst_incr = c->dst_incr;
00213 
00214     c->index = -phase_count * ((c->filter_length - 1) / 2);
00215     c->frac  = 0;
00216 
00217     
00218     c->buffer = ff_audio_data_alloc(avr->resample_channels, 0,
00219                                     avr->internal_sample_fmt,
00220                                     "resample buffer");
00221     if (!c->buffer)
00222         goto error;
00223 
00224     av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
00225            av_get_sample_fmt_name(avr->internal_sample_fmt),
00226            avr->in_sample_rate, avr->out_sample_rate);
00227 
00228     return c;
00229 
00230 error:
00231     ff_audio_data_free(&c->buffer);
00232     av_free(c->filter_bank);
00233     av_free(c);
00234     return NULL;
00235 }
00236 
00237 void ff_audio_resample_free(ResampleContext **c)
00238 {
00239     if (!*c)
00240         return;
00241     ff_audio_data_free(&(*c)->buffer);
00242     av_free((*c)->filter_bank);
00243     av_freep(c);
00244 }
00245 
00246 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
00247                                 int compensation_distance)
00248 {
00249     ResampleContext *c;
00250     AudioData *fifo_buf = NULL;
00251     int ret = 0;
00252 
00253     if (compensation_distance < 0)
00254         return AVERROR(EINVAL);
00255     if (!compensation_distance && sample_delta)
00256         return AVERROR(EINVAL);
00257 
00258     
00259 
00260     if (!avr->resample_needed) {
00261         int fifo_samples;
00262         double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 };
00263 
00264         
00265         fifo_samples = av_audio_fifo_size(avr->out_fifo);
00266         if (fifo_samples > 0) {
00267             fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples,
00268                                            avr->out_sample_fmt, NULL);
00269             if (!fifo_buf)
00270                 return AVERROR(EINVAL);
00271             ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf,
00272                                                fifo_samples);
00273             if (ret < 0)
00274                 goto reinit_fail;
00275         }
00276         
00277         ret = avresample_get_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
00278         if (ret < 0)
00279             goto reinit_fail;
00280 
00281         
00282         avresample_close(avr);
00283 
00284         avr->force_resampling = 1;
00285 
00286         
00287         ret = avresample_set_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
00288         if (ret < 0)
00289             goto reinit_fail;
00290 
00291         
00292         ret = avresample_open(avr);
00293         if (ret < 0)
00294             goto reinit_fail;
00295 
00296         
00297         if (fifo_samples > 0) {
00298             ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0,
00299                                             fifo_samples);
00300             if (ret < 0)
00301                 goto reinit_fail;
00302             ff_audio_data_free(&fifo_buf);
00303         }
00304     }
00305     c = avr->resample;
00306     c->compensation_distance = compensation_distance;
00307     if (compensation_distance) {
00308         c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
00309                       (int64_t)sample_delta / compensation_distance;
00310     } else {
00311         c->dst_incr = c->ideal_dst_incr;
00312     }
00313     return 0;
00314 
00315 reinit_fail:
00316     ff_audio_data_free(&fifo_buf);
00317     return ret;
00318 }
00319 
00320 static int resample(ResampleContext *c, void *dst, const void *src,
00321                     int *consumed, int src_size, int dst_size, int update_ctx)
00322 {
00323     int dst_index;
00324     int index         = c->index;
00325     int frac          = c->frac;
00326     int dst_incr_frac = c->dst_incr % c->src_incr;
00327     int dst_incr      = c->dst_incr / c->src_incr;
00328     int compensation_distance = c->compensation_distance;
00329 
00330     if (!dst != !src)
00331         return AVERROR(EINVAL);
00332 
00333     if (compensation_distance == 0 && c->filter_length == 1 &&
00334         c->phase_shift == 0) {
00335         int64_t index2 = ((int64_t)index) << 32;
00336         int64_t incr   = (1LL << 32) * c->dst_incr / c->src_incr;
00337         dst_size       = FFMIN(dst_size,
00338                                (src_size-1-index) * (int64_t)c->src_incr /
00339                                c->dst_incr);
00340 
00341         if (dst) {
00342             for(dst_index = 0; dst_index < dst_size; dst_index++) {
00343                 c->resample_one(c, 1, dst, dst_index, src, 0, index2 >> 32, 0);
00344                 index2 += incr;
00345             }
00346         } else {
00347             dst_index = dst_size;
00348         }
00349         index += dst_index * dst_incr;
00350         index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
00351         frac   = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
00352     } else {
00353         for (dst_index = 0; dst_index < dst_size; dst_index++) {
00354             int sample_index = index >> c->phase_shift;
00355 
00356             if (sample_index + c->filter_length > src_size ||
00357                 -sample_index >= src_size)
00358                 break;
00359 
00360             if (dst)
00361                 c->resample_one(c, 0, dst, dst_index, src, src_size, index, frac);
00362 
00363             frac  += dst_incr_frac;
00364             index += dst_incr;
00365             if (frac >= c->src_incr) {
00366                 frac -= c->src_incr;
00367                 index++;
00368             }
00369             if (dst_index + 1 == compensation_distance) {
00370                 compensation_distance = 0;
00371                 dst_incr_frac = c->ideal_dst_incr % c->src_incr;
00372                 dst_incr      = c->ideal_dst_incr / c->src_incr;
00373             }
00374         }
00375     }
00376     if (consumed)
00377         *consumed = FFMAX(index, 0) >> c->phase_shift;
00378 
00379     if (update_ctx) {
00380         if (index >= 0)
00381             index &= c->phase_mask;
00382 
00383         if (compensation_distance) {
00384             compensation_distance -= dst_index;
00385             if (compensation_distance <= 0)
00386                 return AVERROR_BUG;
00387         }
00388         c->frac     = frac;
00389         c->index    = index;
00390         c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
00391         c->compensation_distance = compensation_distance;
00392     }
00393 
00394     return dst_index;
00395 }
00396 
00397 int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src,
00398                       int *consumed)
00399 {
00400     int ch, in_samples, in_leftover, out_samples = 0;
00401     int ret = AVERROR(EINVAL);
00402 
00403     in_samples  = src ? src->nb_samples : 0;
00404     in_leftover = c->buffer->nb_samples;
00405 
00406     
00407     if (src) {
00408         ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
00409         if (ret < 0)
00410             return ret;
00411     } else if (!in_leftover) {
00412         
00413         return 0;
00414     } else {
00415         
00416     }
00417 
00418     
00419     
00420     if (!dst->read_only && dst->allow_realloc) {
00421         out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
00422                                INT_MAX, 0);
00423         ret = ff_audio_data_realloc(dst, out_samples);
00424         if (ret < 0) {
00425             av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
00426             return ret;
00427         }
00428     }
00429 
00430     
00431     for (ch = 0; ch < c->buffer->channels; ch++) {
00432         out_samples = resample(c, (void *)dst->data[ch],
00433                                (const void *)c->buffer->data[ch], consumed,
00434                                c->buffer->nb_samples, dst->allocated_samples,
00435                                ch + 1 == c->buffer->channels);
00436     }
00437     if (out_samples < 0) {
00438         av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
00439         return out_samples;
00440     }
00441 
00442     
00443     ff_audio_data_drain(c->buffer, *consumed);
00444 
00445     av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n",
00446             in_samples, in_leftover, out_samples, c->buffer->nb_samples);
00447 
00448     dst->nb_samples = out_samples;
00449     return 0;
00450 }
00451 
00452 int avresample_get_delay(AVAudioResampleContext *avr)
00453 {
00454     if (!avr->resample_needed || !avr->resample)
00455         return 0;
00456 
00457     return avr->resample->buffer->nb_samples;
00458 }