yading@11: /* yading@11: * audio resampling yading@11: * Copyright (c) 2004-2012 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * audio resampling yading@11: * @author Michael Niedermayer yading@11: */ yading@11: yading@11: #include "libavutil/log.h" yading@11: #include "libavutil/avassert.h" yading@11: #include "swresample_internal.h" yading@11: yading@11: yading@11: typedef struct ResampleContext { yading@11: const AVClass *av_class; yading@11: uint8_t *filter_bank; yading@11: int filter_length; yading@11: int filter_alloc; yading@11: int ideal_dst_incr; yading@11: int dst_incr; yading@11: int index; yading@11: int frac; yading@11: int src_incr; yading@11: int compensation_distance; yading@11: int phase_shift; yading@11: int phase_mask; yading@11: int linear; yading@11: enum SwrFilterType filter_type; yading@11: int kaiser_beta; yading@11: double factor; yading@11: enum AVSampleFormat format; yading@11: int felem_size; yading@11: int filter_shift; yading@11: } ResampleContext; yading@11: yading@11: /** yading@11: * 0th order modified bessel function of the first kind. yading@11: */ yading@11: static double bessel(double x){ yading@11: double v=1; yading@11: double lastv=0; yading@11: double t=1; yading@11: int i; yading@11: static const double inv[100]={ yading@11: 1.0/( 1* 1), 1.0/( 2* 2), 1.0/( 3* 3), 1.0/( 4* 4), 1.0/( 5* 5), 1.0/( 6* 6), 1.0/( 7* 7), 1.0/( 8* 8), 1.0/( 9* 9), 1.0/(10*10), yading@11: 1.0/(11*11), 1.0/(12*12), 1.0/(13*13), 1.0/(14*14), 1.0/(15*15), 1.0/(16*16), 1.0/(17*17), 1.0/(18*18), 1.0/(19*19), 1.0/(20*20), yading@11: 1.0/(21*21), 1.0/(22*22), 1.0/(23*23), 1.0/(24*24), 1.0/(25*25), 1.0/(26*26), 1.0/(27*27), 1.0/(28*28), 1.0/(29*29), 1.0/(30*30), yading@11: 1.0/(31*31), 1.0/(32*32), 1.0/(33*33), 1.0/(34*34), 1.0/(35*35), 1.0/(36*36), 1.0/(37*37), 1.0/(38*38), 1.0/(39*39), 1.0/(40*40), yading@11: 1.0/(41*41), 1.0/(42*42), 1.0/(43*43), 1.0/(44*44), 1.0/(45*45), 1.0/(46*46), 1.0/(47*47), 1.0/(48*48), 1.0/(49*49), 1.0/(50*50), yading@11: 1.0/(51*51), 1.0/(52*52), 1.0/(53*53), 1.0/(54*54), 1.0/(55*55), 1.0/(56*56), 1.0/(57*57), 1.0/(58*58), 1.0/(59*59), 1.0/(60*60), yading@11: 1.0/(61*61), 1.0/(62*62), 1.0/(63*63), 1.0/(64*64), 1.0/(65*65), 1.0/(66*66), 1.0/(67*67), 1.0/(68*68), 1.0/(69*69), 1.0/(70*70), yading@11: 1.0/(71*71), 1.0/(72*72), 1.0/(73*73), 1.0/(74*74), 1.0/(75*75), 1.0/(76*76), 1.0/(77*77), 1.0/(78*78), 1.0/(79*79), 1.0/(80*80), yading@11: 1.0/(81*81), 1.0/(82*82), 1.0/(83*83), 1.0/(84*84), 1.0/(85*85), 1.0/(86*86), 1.0/(87*87), 1.0/(88*88), 1.0/(89*89), 1.0/(90*90), yading@11: 1.0/(91*91), 1.0/(92*92), 1.0/(93*93), 1.0/(94*94), 1.0/(95*95), 1.0/(96*96), 1.0/(97*97), 1.0/(98*98), 1.0/(99*99), 1.0/(10000) yading@11: }; yading@11: yading@11: x= x*x/4; yading@11: for(i=0; v != lastv; i++){ yading@11: lastv=v; yading@11: t *= x*inv[i]; yading@11: v += t; yading@11: av_assert2(i<99); yading@11: } yading@11: return v; yading@11: } yading@11: yading@11: /** yading@11: * builds a polyphase filterbank. yading@11: * @param factor resampling factor yading@11: * @param scale wanted sum of coefficients for each filter yading@11: * @param filter_type filter type yading@11: * @param kaiser_beta kaiser window beta yading@11: * @return 0 on success, negative on error yading@11: */ yading@11: static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale, yading@11: int filter_type, int kaiser_beta){ yading@11: int ph, i; yading@11: double x, y, w; yading@11: double *tab = av_malloc(tap_count * sizeof(*tab)); yading@11: const int center= (tap_count-1)/2; yading@11: yading@11: if (!tab) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: /* if upsampling, only need to interpolate, no filter */ yading@11: if (factor > 1.0) yading@11: factor = 1.0; yading@11: yading@11: for(ph=0;phformat){ yading@11: case AV_SAMPLE_FMT_S16P: yading@11: for(i=0;iphase_shift != phase_shift || c->linear!=linear || c->factor != factor yading@11: || c->filter_length != FFMAX((int)ceil(filter_size/factor), 1) || c->format != format yading@11: || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) { yading@11: c = av_mallocz(sizeof(*c)); yading@11: if (!c) yading@11: return NULL; yading@11: yading@11: c->format= format; yading@11: yading@11: c->felem_size= av_get_bytes_per_sample(c->format); yading@11: yading@11: switch(c->format){ yading@11: case AV_SAMPLE_FMT_S16P: yading@11: c->filter_shift = 15; yading@11: break; yading@11: case AV_SAMPLE_FMT_S32P: yading@11: c->filter_shift = 30; yading@11: break; yading@11: case AV_SAMPLE_FMT_FLTP: yading@11: case AV_SAMPLE_FMT_DBLP: yading@11: c->filter_shift = 0; yading@11: break; yading@11: default: yading@11: av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n"); yading@11: av_assert0(0); yading@11: } yading@11: yading@11: c->phase_shift = phase_shift; yading@11: c->phase_mask = phase_count - 1; yading@11: c->linear = linear; yading@11: c->factor = factor; yading@11: c->filter_length = FFMAX((int)ceil(filter_size/factor), 1); yading@11: c->filter_alloc = FFALIGN(c->filter_length, 8); yading@11: c->filter_bank = av_calloc(c->filter_alloc, (phase_count+1)*c->felem_size); yading@11: c->filter_type = filter_type; yading@11: c->kaiser_beta = kaiser_beta; yading@11: if (!c->filter_bank) yading@11: goto error; yading@11: if (build_filter(c, (void*)c->filter_bank, factor, c->filter_length, c->filter_alloc, phase_count, 1<filter_shift, filter_type, kaiser_beta)) yading@11: goto error; yading@11: memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size); yading@11: memcpy(c->filter_bank + (c->filter_alloc*phase_count )*c->felem_size, c->filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size); yading@11: } yading@11: yading@11: c->compensation_distance= 0; yading@11: if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2)) yading@11: goto error; yading@11: c->ideal_dst_incr= c->dst_incr; yading@11: yading@11: c->index= -phase_count*((c->filter_length-1)/2); yading@11: c->frac= 0; yading@11: yading@11: return c; yading@11: error: yading@11: av_free(c->filter_bank); yading@11: av_free(c); yading@11: return NULL; yading@11: } yading@11: yading@11: static void resample_free(ResampleContext **c){ yading@11: if(!*c) yading@11: return; yading@11: av_freep(&(*c)->filter_bank); yading@11: av_freep(c); yading@11: } yading@11: yading@11: static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance){ yading@11: c->compensation_distance= compensation_distance; yading@11: if (compensation_distance) yading@11: c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance; yading@11: else yading@11: c->dst_incr = c->ideal_dst_incr; yading@11: return 0; yading@11: } yading@11: yading@11: #define TEMPLATE_RESAMPLE_S16 yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_S16 yading@11: yading@11: #define TEMPLATE_RESAMPLE_S32 yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_S32 yading@11: yading@11: #define TEMPLATE_RESAMPLE_FLT yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_FLT yading@11: yading@11: #define TEMPLATE_RESAMPLE_DBL yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_DBL yading@11: yading@11: // XXX FIXME the whole C loop should be written in asm so this x86 specific code here isnt needed yading@11: #if HAVE_MMXEXT_INLINE yading@11: yading@11: #include "x86/resample_mmx.h" yading@11: yading@11: #define TEMPLATE_RESAMPLE_S16_MMX2 yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_S16_MMX2 yading@11: yading@11: #if HAVE_SSSE3_INLINE yading@11: #define TEMPLATE_RESAMPLE_S16_SSSE3 yading@11: #include "resample_template.c" yading@11: #undef TEMPLATE_RESAMPLE_S16_SSSE3 yading@11: #endif yading@11: yading@11: #endif // HAVE_MMXEXT_INLINE yading@11: yading@11: static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){ yading@11: int i, ret= -1; yading@11: int av_unused mm_flags = av_get_cpu_flags(); yading@11: int need_emms= 0; yading@11: yading@11: for(i=0; ich_count; i++){ yading@11: #if HAVE_MMXEXT_INLINE yading@11: #if HAVE_SSSE3_INLINE yading@11: if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_SSSE3)) ret= swri_resample_int16_ssse3(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: else yading@11: #endif yading@11: if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){ yading@11: ret= swri_resample_int16_mmx2 (c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: need_emms= 1; yading@11: } else yading@11: #endif yading@11: if(c->format == AV_SAMPLE_FMT_S16P) ret= swri_resample_int16(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: else if(c->format == AV_SAMPLE_FMT_S32P) ret= swri_resample_int32(c, (int32_t*)dst->ch[i], (const int32_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: else if(c->format == AV_SAMPLE_FMT_FLTP) ret= swri_resample_float(c, (float *)dst->ch[i], (const float *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: else if(c->format == AV_SAMPLE_FMT_DBLP) ret= swri_resample_double(c,(double *)dst->ch[i], (const double *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count); yading@11: } yading@11: if(need_emms) yading@11: emms_c(); yading@11: return ret; yading@11: } yading@11: yading@11: static int64_t get_delay(struct SwrContext *s, int64_t base){ yading@11: ResampleContext *c = s->resample; yading@11: int64_t num = s->in_buffer_count - (c->filter_length-1)/2; yading@11: num <<= c->phase_shift; yading@11: num -= c->index; yading@11: num *= c->src_incr; yading@11: num -= c->frac; yading@11: return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr << c->phase_shift); yading@11: } yading@11: yading@11: static int resample_flush(struct SwrContext *s) { yading@11: AudioData *a= &s->in_buffer; yading@11: int i, j, ret; yading@11: if((ret = swri_realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0) yading@11: return ret; yading@11: av_assert0(a->planar); yading@11: for(i=0; ich_count; i++){ yading@11: for(j=0; jin_buffer_count; j++){ yading@11: memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps, yading@11: a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps); yading@11: } yading@11: } yading@11: s->in_buffer_count += (s->in_buffer_count+1)/2; yading@11: return 0; yading@11: } yading@11: yading@11: struct Resampler const swri_resampler={ yading@11: resample_init, yading@11: resample_free, yading@11: multiple_resample, yading@11: resample_flush, yading@11: set_compensation, yading@11: get_delay, yading@11: };