[Mplayerxp-cvslog] SF.net SVN: mplayerxp:[301] mplayerxp
Brought to you by:
olov
From: <nic...@us...> - 2012-11-07 15:12:00
|
Revision: 301 http://mplayerxp.svn.sourceforge.net/mplayerxp/?rev=301&view=rev Author: nickols_k Date: 2012-11-07 15:11:47 +0000 (Wed, 07 Nov 2012) Log Message: ----------- bound arrays test: use libswresample for resampler Modified Paths: -------------- mplayerxp/Makefile mplayerxp/configure mplayerxp/mplayer.c mplayerxp/postproc/af.c mplayerxp/postproc/af_channels.c mplayerxp/postproc/af_crystality.c mplayerxp/postproc/af_echo3d.c mplayerxp/postproc/af_format.c mplayerxp/postproc/af_resample.c Removed Paths: ------------- mplayerxp/postproc/af_resample.h Modified: mplayerxp/Makefile =================================================================== --- mplayerxp/Makefile 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/Makefile 2012-11-07 15:11:47 UTC (rev 301) @@ -30,6 +30,7 @@ FF_LIBS = ../ffmpeg/libavcodec/libavcodec$(FF_SUFFIX).a \ ../ffmpeg/libswscale/libswscale$(FF_SUFFIX).a \ + ../ffmpeg/libswresample/libswresample$(FF_SUFFIX).a \ ../ffmpeg/libpostproc/libpostproc$(FF_SUFFIX).a \ ../ffmpeg/libavformat/libavformat$(FF_SUFFIX).a \ ../ffmpeg/libavutil/libavutil$(FF_SUFFIX).a Modified: mplayerxp/configure =================================================================== --- mplayerxp/configure 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/configure 2012-11-07 15:11:47 UTC (rev 301) @@ -320,7 +320,7 @@ add_cflags "-I$srcdir/../ffmpeg" print_config HAVE_ mp_config.h mp_config.mak ffmpeg # Configuring external ffmpeg stuff -ffmpeg_args="--enable-static --disable-shared --enable-gpl --enable-pthreads --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-ffprobe --disable-avdevice --disable-avfilter --disable-avresample --disable-swresample" +ffmpeg_args="--enable-static --disable-shared --enable-gpl --enable-pthreads --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-ffprobe --disable-avdevice --disable-avfilter --disable-avresample" if test -n $host ; then _arch=$host_arch x86_32 && _arch="i686" @@ -332,7 +332,7 @@ test "$debug" != "0" && ffmpeg_args="$ffmpeg_args --enable-debug=$debug" enabled $profile && ffmpeg_args="$ffmpeg_args --enable-profile" if test -f "../ffmpeg/libavcodec/libavcodec$ff_libsuffix.a"; then -echocheck "ffmpeg was already configured for $ff_libsuffix" +echocheck "ffmpeg was already configured for $_arch" else echocheck "configuring ffmpeg stuff: --cc=\"$cc\" $ffmpeg_args" cd ../ffmpeg Modified: mplayerxp/mplayer.c =================================================================== --- mplayerxp/mplayer.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/mplayer.c 2012-11-07 15:11:47 UTC (rev 301) @@ -1656,6 +1656,8 @@ mp_conf.malloc_debug=0; for(i=0;i<argc;i++) if(strcmp(argv[i],"-core.malloc-debug")==0) { mp_conf.malloc_debug=1; break; } mp_init_malloc(argv[0],1000,10,mp_conf.malloc_debug?MPA_FLG_BACKTRACE:MPA_FLG_RANDOMIZER); +// mp_init_malloc(argv[0],1000,10,MPA_FLG_BOUNDS_CHECK); +// mp_init_malloc(argv[0],1000,10,MPA_FLG_BEFORE_CHECK); mpxp_init_structs(); priv_t*priv=mp_data->priv; Modified: mplayerxp/postproc/af.c =================================================================== --- mplayerxp/postproc/af.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af.c 2012-11-07 15:11:47 UTC (rev 301) @@ -227,7 +227,7 @@ else s->last=af->prev; - // Uninitialize af and mp_free memory + // Uninitialize af and mp_free memory af->uninit(af); mp_free(af); } @@ -245,15 +245,14 @@ af_data_t in; // Format of the input to current filter int rv=0; // Return value - // Check if this is the first filter - if(!af->prev) - memcpy(&in,&(s->input),sizeof(af_data_t)); - else - memcpy(&in,af->prev->data,sizeof(af_data_t)); + // Check if this is the first filter + if(!af->prev) memcpy(&in,&(s->input),sizeof(af_data_t)); + else memcpy(&in,af->prev->data,sizeof(af_data_t)); + // Reset just in case... in.audio=NULL; in.len=0; - + rv = af->control(af,AF_CONTROL_REINIT,&in); switch(rv){ case CONTROL_OK: @@ -264,58 +263,44 @@ af_instance_t* _new = NULL; // Insert channels filter if((af->prev?af->prev->data->nch:s->input.nch) != in.nch){ - // Create channels filter - if(NULL == (_new = af_prepend(s,af,"channels"))) - return CONTROL_ERROR; - // Set number of output channels - if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_CHANNELS,&in.nch))) - return rv; + if(NULL == (_new = af_prepend(s,af,"channels"))) return CONTROL_ERROR; + if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_CHANNELS,&in.nch))) return rv; // Initialize channels filter if(!_new->prev) memcpy(&in,&(s->input),sizeof(af_data_t)); else memcpy(&in,_new->prev->data,sizeof(af_data_t)); - if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_REINIT,&in))) - return rv; + if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_REINIT,&in))) return rv; } // Insert rate filter if((af->prev?af->prev->data->rate:s->input.rate) != in.rate){ - // Create channels filter - if(NULL == (_new = af_prepend(s,af,"resample"))) - return CONTROL_ERROR; - // Set number of output channels - if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_RESAMPLE_RATE,&in.rate))) - return rv; + if(NULL == (_new = af_prepend(s,af,"resample"))) return CONTROL_ERROR; + if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_RESAMPLE_RATE,&in.rate))) return rv; // Initialize channels filter if(!_new->prev) memcpy(&in,&(s->input),sizeof(af_data_t)); else memcpy(&in,_new->prev->data,sizeof(af_data_t)); if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_REINIT,&in))) { +#if 0 af_instance_t* af2 = af_prepend(s,af,"format"); // Init the new filter if(af2) { if((CONTROL_OK != af2->control(af2,AF_CONTROL_FORMAT_BPS,&(in.bps))) - || (CONTROL_OK != af2->control(af2,AF_CONTROL_FORMAT_FMT,&(in.format)))) - return -1; - if(CONTROL_OK != af_reinit(s,af2)) - return -1; + || (CONTROL_OK != af2->control(af2,AF_CONTROL_FORMAT_FMT,&(in.format)))) return -1; + if(CONTROL_OK != af_reinit(s,af2)) return -1; rv = _new->control(_new,AF_CONTROL_REINIT,&in); } +#endif return rv; } } // Insert format filter - if(((af->prev?af->prev->data->format:s->input.format) != in.format) || + if(((af->prev?af->prev->data->format:s->input.format) != in.format) || ((af->prev?af->prev->data->bps:s->input.bps) != in.bps)){ - // Create format filter - if(NULL == (_new = af_prepend(s,af,"format"))) - return CONTROL_ERROR; - // Set output bits per sample - if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_FORMAT_BPS,&in.bps)) || - CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_FORMAT_FMT,&in.format))) - return rv; + if(NULL == (_new = af_prepend(s,af,"format"))) return CONTROL_ERROR; + if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_FORMAT_BPS,&in.bps)) || + CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_FORMAT_FMT,&in.format))) return rv; // Initialize format filter if(!_new->prev) memcpy(&in,&(s->input),sizeof(af_data_t)); else memcpy(&in,_new->prev->data,sizeof(af_data_t)); - if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_REINIT,&in))) - return rv; + if(CONTROL_OK != (rv = _new->control(_new,AF_CONTROL_REINIT,&in))) return rv; } if(!_new){ // Should _never_ happen MSG_ERR("[libaf] Unable to correct audio format. " Modified: mplayerxp/postproc/af_channels.c =================================================================== --- mplayerxp/postproc/af_channels.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_channels.c 2012-11-07 15:11:47 UTC (rev 301) @@ -249,12 +249,12 @@ static af_data_t* __FASTCALL__ play(struct af_instance_s* af, af_data_t* data,int final) { af_data_t* c = data; // Current working data - af_data_t* l = af->data; // Local data af_channels_t* s = af->setup; int i; if(CONTROL_OK != RESIZE_LOCAL_BUFFER(af,data)) return NULL; + af_data_t* l = af->data; // Local data // Reset unused channels memset(l->audio,0,(c->len*af->mul.n)/af->mul.d); Modified: mplayerxp/postproc/af_crystality.c =================================================================== --- mplayerxp/postproc/af_crystality.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_crystality.c 2012-11-07 15:11:47 UTC (rev 301) @@ -331,7 +331,7 @@ int acount; // counter float lval, rval; // value float sal, sar; // sum - float al, ar; + float al, ar; float a1l, a1r; }; Modified: mplayerxp/postproc/af_echo3d.c =================================================================== --- mplayerxp/postproc/af_echo3d.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_echo3d.c 2012-11-07 15:11:47 UTC (rev 301) @@ -166,7 +166,7 @@ af->data->format = AF_FORMAT_NE | AF_FORMAT_F; af->data->bps = 4; init_echo3d(s,af->data->rate); - + return af_test_output(af,arg); } case AF_CONTROL_COMMAND_LINE:{ @@ -182,7 +182,7 @@ return CONTROL_UNKNOWN; } -// Deallocate memory +// Deallocate memory static void __FASTCALL__ uninit(struct af_instance_s* af) { if(af->data) Modified: mplayerxp/postproc/af_format.c =================================================================== --- mplayerxp/postproc/af_format.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_format.c 2012-11-07 15:11:47 UTC (rev 301) @@ -280,11 +280,11 @@ // Helper functions to check sanity for input arguments // Sanity check for bytes per sample -static int __FASTCALL__ check_bps(int bps) +static int __FASTCALL__ check_bps(const char *pfx,int bps) { if(bps != 4 && bps != 3 && bps != 2 && bps != 1){ - MSG_ERR("[format] The number of bytes per sample" - " must be 1, 2, 3 or 4. Current value is %i \n",bps); + MSG_ERR("[%s-format] The number of bytes per sample" + " must be 1, 2, 3 or 4. Current value is %i \n",pfx,bps); return CONTROL_ERROR; } return CONTROL_OK; @@ -311,15 +311,15 @@ char buf1[256],buf2[256]; switch(cmd){ case AF_CONTROL_REINIT:{ - // Make sure this filter isn't redundant + // Make sure this filter isn't redundant if(af->data->format == ((af_data_t*)arg)->format && af->data->bps == ((af_data_t*)arg)->bps) return CONTROL_DETACH; // Check for errors in configuraton - if((CONTROL_OK != check_bps(((af_data_t*)arg)->bps)) || + if((CONTROL_OK != check_bps("new",((af_data_t*)arg)->bps)) || (CONTROL_OK != check_format(((af_data_t*)arg)->format)) || - (CONTROL_OK != check_bps(af->data->bps)) || + (CONTROL_OK != check_bps("prev",af->data->bps)) || (CONTROL_OK != check_format(af->data->format))) return CONTROL_ERROR; @@ -341,7 +341,7 @@ int format = AF_FORMAT_NE; // Convert string to format format = str2fmt((char *)arg,&bps); - + if((CONTROL_OK != af->control(af,AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET,&bps)) || (CONTROL_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format))) return CONTROL_ERROR; @@ -349,9 +349,9 @@ } case AF_CONTROL_FORMAT_BPS | AF_CONTROL_SET: // Reinit must be called after this function has been called - + // Check for errors in configuraton - if(CONTROL_OK != check_bps(*(int*)arg)) + if(CONTROL_OK != check_bps("arg",*(int*)arg)) return CONTROL_ERROR; af->data->bps = *(int*)arg; @@ -370,7 +370,7 @@ return CONTROL_UNKNOWN; } -// Deallocate memory +// Deallocate memory static void __FASTCALL__ uninit(struct af_instance_s* af) { if(af->data) @@ -382,12 +382,12 @@ // Filter data through filter static af_data_t* __FASTCALL__ play(struct af_instance_s* af, af_data_t* data,int final) { - af_data_t* l = af->data; // Local data af_data_t* c = data; // Current working data int len = c->len/c->bps; // Lenght in samples of current audio block if(CONTROL_OK != RESIZE_LOCAL_BUFFER(af,data)) return NULL; + af_data_t* l = af->data; // Local data // Change to cpu native endian format if((c->format&AF_FORMAT_END_MASK)!=AF_FORMAT_NE) @@ -429,7 +429,7 @@ break; default: // Input must be int - + // Change signed/unsigned if((c->format&AF_FORMAT_SIGN_MASK) != (l->format&AF_FORMAT_SIGN_MASK)){ if((c->format&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) @@ -593,5 +593,5 @@ for(i=0;i<len;i++) ((int32_t*)out)[i]=(int32_t)(INT_MIN+((uint32_t*)in)[i]); break; - } + } } Modified: mplayerxp/postproc/af_resample.c =================================================================== --- mplayerxp/postproc/af_resample.c 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_resample.c 2012-11-07 15:11:47 UTC (rev 301) @@ -11,433 +11,189 @@ /* This audio filter changes the sample rate. */ #include <stdio.h> #include <stdlib.h> +#include <signal.h> #include <unistd.h> #include <inttypes.h> +#include "libavutil/audioconvert.h" +#include "libswresample/swresample.h" + #include "af.h" #include "dsp.h" #include "osdep/mplib.h" #include "pp_msg.h" -/* Below definition selects the length of each poly phase component. - Valid definitions are L8 and L16, where the number denotes the - length of the filter. This definition affects the computational - complexity (see play()), the performance (see filter.h) and the - memory usage. The filterlenght is choosen to 8 if the machine is - slow and to 16 if the machine is fast and has MMX. -*/ +uint64_t layouts[]={ + 0, + AV_CH_LAYOUT_MONO, + AV_CH_LAYOUT_STEREO, + AV_CH_LAYOUT_SURROUND, + AV_CH_LAYOUT_4POINT0, + AV_CH_LAYOUT_5POINT0, + AV_CH_LAYOUT_5POINT1, + AV_CH_LAYOUT_7POINT0, + AV_CH_LAYOUT_7POINT1 +}; -#if __CPU__ < 686 // This machine is slow -#define L8 -#else -#define L16 -#endif - -#include "af_resample.h" - -// Filtering types -#define RSMP_LIN (0<<0) // Linear interpolation -#define RSMP_INT (1<<0) // 16 bit integer -#define RSMP_FLOAT (2<<0) // 32 bit floating point -#define RSMP_MASK (3<<0) - -// Defines for sloppy or exact resampling -#define FREQ_SLOPPY (0<<2) -#define FREQ_EXACT (1<<2) -#define FREQ_MASK (1<<2) - -// Accuracy for linear interpolation -#define STEPACCURACY 32 - -// local data -typedef struct af_resample_s -{ - any_t* w; // Current filter weights - any_t** xq; // Circular buffers - uint32_t xi; // Index for circular buffers - uint32_t wi; // Index for w - uint32_t i; // Number of new samples to put in x queue - uint32_t dn; // Down sampling factor - uint32_t up; // Up sampling factor - uint64_t step; // Step size for linear interpolation - uint64_t pt; // Pointer remainder for linear interpolation - int setup; // Setup parameters cmdline or through postcreate - int ifreq; -} af_resample_t; - -// Euclids algorithm for calculating Greatest Common Divisor GCD(a,b) -// Extended for negative and 0 values. If both are 0 the result is 1. -// The sign of the result will be so that it has the same sign as b. -static inline int gcd(register int a, register int b) -{ - int b_org = b; - while (b != 0) { - a %= b; - if (a == 0) - break; - b %= a; - } - // the result is either in a or b. As the other one is 0 just add them. - a += b; - if (!a) - return 1; - if (a * b_org < 0) - return 1;//-a; - return a; +static uint64_t get_ch_layout(unsigned nch) { + if(nch < sizeof(layouts)/sizeof(uint64_t)) + return layouts[nch]; + return 0; } -/** - * \brief cancel down a fraction f - * \param f fraction to cancel down - * \ingroup af_filter - */ -static void frac_cancel(frac_t *f) { - int _gcd = gcd(f->n, f->d); - f->n /= _gcd; - f->d /= _gcd; -} - -// Fast linear interpolation resample with modest audio quality -static int __FASTCALL__ linint(af_data_t* c,af_data_t* l, af_resample_t* s) -{ - uint32_t len = 0; // Number of input samples - uint32_t nch = l->nch; // Words pre transfer - uint64_t step = s->step; - int16_t* in16 = ((int16_t*)c->audio); - int16_t* out16 = ((int16_t*)l->audio); - int32_t* in32 = ((int32_t*)c->audio); - int32_t* out32 = ((int32_t*)l->audio); - uint64_t end = ((((uint64_t)c->len)/2LL)<<STEPACCURACY); - uint64_t pt = s->pt; - uint16_t tmp; - - switch (nch){ - case 1: - while(pt < end){ - out16[len++]=in16[pt>>STEPACCURACY]; - pt+=step; +static enum AVSampleFormat get_sample_format(unsigned fmt,unsigned nbps) { + switch(nbps) { + case 1: if((fmt&AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) return AV_SAMPLE_FMT_U8; + break; + case 2: if((fmt&AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI && + (fmt&AF_FORMAT_POINT_MASK) == AF_FORMAT_I && + (fmt&AF_FORMAT_END_MASK) == AF_FORMAT_NE) return AV_SAMPLE_FMT_S16; + break; + case 4: if((fmt&AF_FORMAT_POINT_MASK) == AF_FORMAT_I) { + if((fmt&AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI && + (fmt&AF_FORMAT_POINT_MASK) == AF_FORMAT_I && + (fmt&AF_FORMAT_END_MASK) == AF_FORMAT_NE) return AV_SAMPLE_FMT_S32; + } + else return AV_SAMPLE_FMT_FLT; + break; + case 8: if((fmt&AF_FORMAT_POINT_MASK) == AF_FORMAT_F) return AV_SAMPLE_FMT_DBL; + break; } - s->pt=pt & ((1LL<<STEPACCURACY)-1); - break; - case 2: - end/=2; - while(pt < end){ - out32[len++]=in32[pt>>STEPACCURACY]; - pt+=step; - } - len=(len<<1); - s->pt=pt & ((1LL<<STEPACCURACY)-1); - break; - default: - end /=nch; - while(pt < end){ - tmp=nch; - do { - tmp--; - out16[len+tmp]=in16[tmp+(pt>>STEPACCURACY)*nch]; - } while (tmp); - len+=nch; - pt+=step; - } - s->pt=pt & ((1LL<<STEPACCURACY)-1); - } - return len; + return AV_SAMPLE_FMT_NONE; } -/* Determine resampling type and format */ -static int __FASTCALL__ set_types(struct af_instance_s* af, af_data_t* data) -{ - af_resample_t* s = af->setup; - int rv = CONTROL_OK; - float rd = 0; +// local data +typedef struct af_resample_s { + struct SwrContext* ctx; + unsigned irate; + unsigned inch; + unsigned ifmt; + unsigned ibps; +} af_resample_t; - // Make sure this filter isn't redundant - if((af->data->rate == data->rate) || (af->data->rate == 0)) - return CONTROL_DETACH; - - /* If sloppy and small resampling difference (2%) */ - rd = abs((float)af->data->rate - (float)data->rate)/(float)data->rate; - if((((s->setup & FREQ_MASK) == FREQ_SLOPPY) && (rd < 0.02) && - (data->format != (AF_FORMAT_NE | AF_FORMAT_F))) || - ((s->setup & RSMP_MASK) == RSMP_LIN)){ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_LIN; - af->data->format = AF_FORMAT_NE | AF_FORMAT_SI; - af->data->bps = 2; - MSG_V("[resample] Using linear interpolation. \n"); - } - else{ - /* If the input format is float or if float is explicitly selected - use float, otherwise use int */ - if((data->format == (AF_FORMAT_NE | AF_FORMAT_F)) || - ((s->setup & RSMP_MASK) == RSMP_FLOAT)){ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_FLOAT; - af->data->format = AF_FORMAT_NE | AF_FORMAT_F; - af->data->bps = 4; - } - else{ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_INT; - af->data->format = AF_FORMAT_NE | AF_FORMAT_SI; - af->data->bps = 2; - } - MSG_V("[resample] Using %s processing and %s frequency conversion.\n", - ((s->setup & RSMP_MASK) == RSMP_FLOAT)?"floating point":"integer", - ((s->setup & FREQ_MASK) == FREQ_SLOPPY)?"inexact":"exact"); - } - - if(af->data->format != data->format || af->data->bps != data->bps) { - char buff[256]; - MSG_V("[resample] doesn't fork with '%s' input format\n",fmt2str(data->format,data->bps,buff,sizeof(buff))); - rv = CONTROL_FALSE; - } - data->format = af->data->format; - data->bps = af->data->bps; - af->data->nch = data->nch; - return rv; -} - // Initialization and runtime control static ControlCodes __FASTCALL__ control(struct af_instance_s* af, int cmd, any_t* arg) { - af_resample_t* s = (af_resample_t*)af->setup; - switch(cmd){ - case AF_CONTROL_REINIT:{ - af_data_t* n = (af_data_t*)arg; // New configuration - int i,d = 0; - int rv = CONTROL_OK; + af_resample_t* s = (af_resample_t*)af->setup; + switch(cmd){ + case AF_CONTROL_REINIT: { + enum AVSampleFormat avfmt; + uint64_t nch; + af_data_t* n = (af_data_t*)arg; // New configuration + int rv = CONTROL_OK; - // Free space for circular bufers - s->ifreq=n->rate; - if(s->xq){ - for(i=1;i<af->data->nch;i++) - if(s->xq[i]) - mp_free(s->xq[i]); - mp_free(s->xq); - } + if(s->ctx) { swr_free(&s->ctx); s->ctx=NULL; } + // Make sure this filter isn't redundant + if((af->data->rate == n->rate) || (af->data->rate == 0)) { + MSG_V("[af_resample] detach due: %i -> %i Hz\n", + af->data->rate,n->rate); + return CONTROL_DETACH; + } + avfmt=get_sample_format(n->format,n->bps); + nch=get_ch_layout(n->nch); + if(avfmt==AV_SAMPLE_FMT_NONE) rv=CONTROL_ERROR; + if(nch==0) rv=CONTROL_ERROR; + if(rv!=CONTROL_OK) { + char buff[256]; + MSG_V("[af_resample] doesn't work with '%s' x %i\n" + ,fmt2str(n->format,n->bps,buff,sizeof(buff)) + ,n->nch); + } + s->ctx = swr_alloc_set_opts(NULL, + nch, avfmt,af->data->rate, + nch, avfmt,n->rate, + 0, NULL); + if(swr_init(s->ctx)<0) { + MSG_ERR("[af_resample] Cannot init swr_init\n"); + rv=CONTROL_ERROR; + } - if(CONTROL_DETACH == (rv = set_types(af,n))) - return CONTROL_DETACH; + af->data->format = n->format; + af->data->bps = n->bps; + af->data->nch = n->nch; - // If linear interpolation - if((s->setup & RSMP_MASK) == RSMP_LIN){ - s->pt=0LL; - s->step=((uint64_t)n->rate<<STEPACCURACY)/(uint64_t)af->data->rate+1LL; - MSG_DBG2("[resample] Linear interpolation step: 0x%016X.\n", - s->step); - af->mul.n = af->data->rate; - af->mul.d = n->rate; - frac_cancel(&af->mul); - return rv; - } - - // Calculate up and down sampling factors - d=gcd(af->data->rate,n->rate); - - // If sloppy resampling is enabled limit the upsampling factor - if(((s->setup & FREQ_MASK) == FREQ_SLOPPY) && (af->data->rate/d > 5000)){ - int up=af->data->rate/2; - int dn=n->rate/2; - int m=2; - while(af->data->rate/(d*m) > 50){ - d=gcd(up,dn); - up/=2; dn/=2; m*=2; - } - d*=m; - } - - // Create space for circular bufers - s->xq = mp_malloc(n->nch*sizeof(any_t*)); - for(i=0;i<n->nch;i++) - s->xq[i] = mp_malloc(2*L*af->data->bps); - s->xi = 0; - - // Check if the the design needs to be redone - if(s->up != af->data->rate/d || s->dn != n->rate/d){ - float* w; - float* wt; - float fc; - int j; - if(af->data->rate/d && n->rate/d) - { - s->up = af->data->rate/d; - s->dn = n->rate/d; - } - else if(af->data->rate/d) - { - s->up=(af->data->rate/d)*(d/n->rate); - s->dn=1; - } - else - { - s->up=1; - s->dn=n->rate/d*(d/af->data->rate); - } - - // Calculate cuttof frequency for filter - fc = 1/(float)(max(s->up,s->dn)); - // Allocate space for polyphase filter bank and protptype filter - w = mp_malloc(sizeof(float) * s->up *L); - if(NULL != s->w) - mp_free(s->w); - s->w = mp_malloc(L*s->up*af->data->bps); - - // Design prototype filter type using Kaiser window with beta = 10 - if(NULL == w || NULL == s->w || - -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ - MSG_ERR("[resample] Unable to design prototype filter.\n"); - return CONTROL_ERROR; - } - // Copy data from prototype to polyphase filter - wt=w; - for(j=0;j<L;j++){//Columns - for(i=0;i<s->up;i++){//Rows - if((s->setup & RSMP_MASK) == RSMP_INT){ - float t=(float)s->up*32767.0*(*wt); - ((int16_t*)s->w)[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5)); - } - else - ((float*)s->w)[i*L+j] = (float)s->up*(*wt); - wt++; + s->irate=n->rate; + s->inch=n->nch; + s->ifmt=n->format; + s->ibps=n->bps; + // Set multiplier and delay + af->delay = (double)swr_get_delay(s->ctx,1000); + af->mul.n = af->data->rate; + af->mul.d = n->rate; + return rv; } - } - mp_free(w); + case AF_CONTROL_SHOWCONF: + MSG_INFO("[af_resample] New filter designed (%i -> %i Hz)\n", s->irate,af->data->rate); + return CONTROL_OK; + case AF_CONTROL_COMMAND_LINE:{ + int rate=0; + sscanf((char*)arg,"%i", &rate); + return af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &rate); + } + case AF_CONTROL_POST_CREATE: return CONTROL_OK; + case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: { + af->data->rate = ((int*)arg)[0]; + return CONTROL_OK; + } + default: break; } - - // Set multiplier and delay - af->delay = (double)(1000*L/2)/((double)n->rate); - af->mul.n = s->up; - af->mul.d = s->dn; - return rv; - } - case AF_CONTROL_SHOWCONF: - MSG_INFO("[af_resample] New filter designed up: %i down: %i (%i -> %i Hz)\n", s->up, s->dn,s->ifreq,af->data->rate); - return CONTROL_OK; - case AF_CONTROL_COMMAND_LINE:{ - af_resample_t* s = (af_resample_t*)af->setup; - int rate=0; - int type=RSMP_INT; - int sloppy=1; - sscanf((char*)arg,"%i:%i:%i", &rate, &sloppy, &type); - s->setup = (sloppy?FREQ_SLOPPY:FREQ_EXACT) | - (clamp(type,RSMP_LIN,RSMP_FLOAT)); - return af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &rate); - } - case AF_CONTROL_POST_CREATE: - if((((af_cfg_t*)arg)->force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) - ((af_resample_t*)af->setup)->setup = RSMP_FLOAT; - return CONTROL_OK; - case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: - // Reinit must be called after this function has been called - - // Sanity check - if(((int*)arg)[0] < 4000 || ((int*)arg)[0] > 192000){ - MSG_ERR("[resample] The output sample frequency " - "must be between 4kHz and 192kHz. Current value is %i \n", - ((int*)arg)[0]); - return CONTROL_ERROR; - } - - af->data->rate=((int*)arg)[0]; - MSG_V("[resample] Changing sample rate " - "to %iHz\n",af->data->rate); - return CONTROL_OK; - default: break; - } - return CONTROL_UNKNOWN; + return CONTROL_UNKNOWN; } // Deallocate memory static void __FASTCALL__ uninit(struct af_instance_s* af) { - if(af->data) - mp_free(af->data); + af_resample_t* s = (af_resample_t*)af->setup; + if(af->data) { + if(af->data->audio) mp_free(af->data->audio); + mp_free(af->data); + } + if(s->ctx) swr_free(&s->ctx); + s->ctx=NULL; } // Filter data through filter static af_data_t* __FASTCALL__ play(struct af_instance_s* af, af_data_t* data,int final) { - int len = 0; // Length of output data - af_data_t* c = data; // Current working data - af_data_t* l = af->data; // Local data - af_resample_t* s = (af_resample_t*)af->setup; - uint32_t ci = l->nch; // Index for channels - uint32_t nch = l->nch; // Number of channels - uint32_t up = s->up; - uint32_t dn = s->dn; - uint32_t ns = c->len/l->bps; - register int32_t i = 0; - register uint32_t wi = 0; - register uint32_t xi = 0; + int rc; + af_data_t* c = data; // Current working data + af_resample_t* s = (af_resample_t*)af->setup; - if(CONTROL_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; + if (CONTROL_OK != RESIZE_LOCAL_BUFFER(af, data)) return NULL; + af_data_t* l = af->data; // Local data + uint8_t* ain[SWR_CH_MAX]; + const uint8_t* aout[SWR_CH_MAX]; - // Run resampling - switch(s->setup & RSMP_MASK){ - case(RSMP_INT): -# define FORMAT_I 1 - if(s->up>s->dn){ -# define UP -# include "af_resample.h" -# undef UP - } - else{ -# define DN -# include "af_resample.h" -# undef DN - } - break; - case(RSMP_FLOAT): -# undef FORMAT_I -# define FORMAT_F 1 - if(s->up>s->dn){ -# define UP -# include "af_resample.h" -# undef UP - } - else{ -# define DN -# include "af_resample.h" -# undef DN - } - break; - case(RSMP_LIN): - len = linint(c, l, s); - break; - } + aout[0]=l->audio; + ain[0]=c->audio; - // Save values that needs to be kept for next time - s->wi = wi; - s->xi = xi; - // Set output data - c->audio = l->audio; - c->len = len*l->bps; - c->rate = l->rate; + rc=swr_convert(s->ctx,aout,l->len/(l->nch*l->bps),ain,c->len/(c->nch*c->bps)); + if(rc<0) MSG_ERR("%i=swr_convert\n",rc); + else l->len=rc*l->nch*l->bps; - return c; + return l; } // Allocate memory and set function pointers static ControlCodes __FASTCALL__ open(af_instance_t* af){ - af->control=control; - af->uninit=uninit; - af->play=play; - af->mul.n=1; - af->mul.d=1; - af->data=mp_calloc(1,sizeof(af_data_t)); - af->setup=mp_calloc(1,sizeof(af_resample_t)); - if(af->data == NULL || af->setup == NULL) - return CONTROL_ERROR; - ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY; - return CONTROL_OK; + af->control=control; + af->uninit=uninit; + af->play=play; + af->mul.n=1; + af->mul.d=1; + af->data=mp_calloc(1,sizeof(af_data_t)); + af->setup=mp_calloc(1,sizeof(af_resample_t)); + if(af->data == NULL || af->setup == NULL) return CONTROL_ERROR; + return CONTROL_OK; } // Description of this plugin const af_info_t af_info_resample = { - "Sample frequency conversion", - "resample", - "Anders", - "", - AF_FLAGS_REENTRANT, - open + "Sample frequency conversion", + "resample", + "Anders", + "", + AF_FLAGS_REENTRANT, + open }; Deleted: mplayerxp/postproc/af_resample.h =================================================================== --- mplayerxp/postproc/af_resample.h 2012-11-07 15:09:02 UTC (rev 300) +++ mplayerxp/postproc/af_resample.h 2012-11-07 15:11:47 UTC (rev 301) @@ -1,144 +0,0 @@ -/*============================================================================= -// -// This software has been released under the terms of the GNU General Public -// license. See http://www.gnu.org/copyleft/gpl.html for details. -// -// Copyright 2002 Anders Johansson aj...@at... -// -//============================================================================= -*/ - -/* This file contains the resampling engine, the sample format is - controlled by the FORMAT parameter, the filter length by the L - parameter and the resampling type by UP and DN. This file should - only be included by af_resample.c -*/ - -#undef L -#undef SHIFT -#undef FORMAT -#undef FIR -#undef ADDQUE - -/* The lenght Lxx definition selects the length of each poly phase - component. Valid definitions are L8 and L16 where the number - defines the nuber of taps. This definition affects the - computational complexity, the performance and the memory usage. -*/ - -/* The FORMAT_x parameter selects the sample format type currently - float and int16 are supported. Thes two formats are selected by - defining eiter FORMAT_F or FORMAT_I. The advantage of using float - is that the amplitude and therefore the SNR isn't affected by the - filtering, the disadvantage is that it is a lot slower. -*/ - -#if defined(FORMAT_I) -#define SHIFT >>16 -#define FORMAT int16_t -#if defined( ARCH_X86 ) || defined(ARCH_X86_64) -#define FIR(x,w,y) {y[0]=FIR_i16(x,w);} -#endif -#else -#define SHIFT -#define FORMAT float -#if defined( ARCH_X86 ) || defined(ARCH_X86_64) -#define FIR(x,w,y) {y[0]=FIR_f32(x,w);} -#endif -#endif - -// Short filter -#if defined(L8) - -#define L 8 // Filter length -// Unrolled loop to speed up execution -#if !(defined( ARCH_X86 ) || defined(ARCH_X86_64)) -#define FIR(x,w,y) \ - (y[0]) = ( w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3] \ - + w[4]*x[4]+w[5]*x[5]+w[6]*x[6]+w[7]*x[7] ) SHIFT -#endif - - -#else /* L8/L16 */ - -#define L 16 -// Unrolled loop to speed up execution -#if !(defined( ARCH_X86 ) || defined(ARCH_X86_64)) -#define FIR(x,w,y) \ - y[0] = ( w[0] *x[0] +w[1] *x[1] +w[2] *x[2] +w[3] *x[3] \ - + w[4] *x[4] +w[5] *x[5] +w[6] *x[6] +w[7] *x[7] \ - + w[8] *x[8] +w[9] *x[9] +w[10]*x[10]+w[11]*x[11] \ - + w[12]*x[12]+w[13]*x[13]+w[14]*x[14]+w[15]*x[15] ) SHIFT -#endif -#endif /* L8/L16 */ - -// Macro to add data to circular que -#define ADDQUE(xi,xq,in)\ - xq[xi]=xq[(xi)+L]=*(in);\ - xi=((xi)-1)&(L-1); - -#if defined(UP) - - uint32_t inc = s->up/s->dn; - uint32_t level = s->up%s->dn; - register FORMAT* w = s->w; - - // Index current channel - while(ci--){ - // Temporary pointers - register FORMAT* x = s->xq[ci]; - register FORMAT* in = ((FORMAT*)c->audio)+ci; - register FORMAT* out = ((FORMAT*)l->audio)+ci; - FORMAT* end = in+ns; // Block loop end - wi = s->wi; xi = s->xi; - - while(in < end){ - register uint32_t i = inc; - if(wi<level) i++; - - ADDQUE(xi,x,in); - in+=nch; - while(i--){ - // Run the FIR filter - FIR((&x[xi]),(&w[wi*L]),out); - len++; out+=nch; - // Update wi to point at the correct polyphase component - wi=(wi+dn)%up; - } - } - - } -#endif /* UP */ - -#if defined(DN) /* DN */ - uint32_t inc = s->dn/s->up; - uint32_t level = s->dn%s->up; - FORMAT* w = s->w; - - // Index current channel - while(ci--){ - // Temporary pointers - register FORMAT* x = s->xq[ci]; - register FORMAT* in = ((FORMAT*)c->audio)+ci; - register FORMAT* out = ((FORMAT*)l->audio)+ci; - register FORMAT* end = in+ns; // Block loop end - i = s->i; wi = s->wi; xi = s->xi; - - while(in < end){ - - ADDQUE(xi,x,in); - in+=nch; - if((--i)<=0){ - // Run the FIR filter - if(out<l->audio+l->len-L) FIR((&x[xi]),(&w[wi*L]),out); - len++; out+=nch; - // Update wi to point at the correct polyphase component - wi=(wi+dn)%up; - // Insert i number of new samples in queue - i = inc; - if(wi<level) i++; - } - } - } - s->i = i; -#endif /* DN */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |