From 048ceef655bce41bc6e215b5e05cec0fad4d1428 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 9 Mar 2013 09:30:26 +0100 Subject: af_lavrresample: add new resampling filter to replace the old ones Remove `af_resample` and `af_lavcresample`. The former is a mess while the latter uses an API that was long deprecated in libavcodec and is now removed. `af_lavrresample` rougly has the same features and structure of `af_lavcresample`. libswresample fallback by wm4. --- audio/filter/af.c | 20 +- audio/filter/af_lavcresample.c | 213 ------------------- audio/filter/af_lavrresample.c | 277 +++++++++++++++++++++++++ audio/filter/af_resample.c | 394 ------------------------------------ audio/filter/af_resample_template.c | 171 ---------------- 5 files changed, 280 insertions(+), 795 deletions(-) delete mode 100644 audio/filter/af_lavcresample.c create mode 100644 audio/filter/af_lavrresample.c delete mode 100644 audio/filter/af_resample.c delete mode 100644 audio/filter/af_resample_template.c (limited to 'audio/filter') diff --git a/audio/filter/af.c b/audio/filter/af.c index 71f4c67b51..ad43e5fca7 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -28,7 +28,6 @@ extern struct af_info af_info_dummy; extern struct af_info af_info_delay; extern struct af_info af_info_channels; extern struct af_info af_info_format; -extern struct af_info af_info_resample; extern struct af_info af_info_volume; extern struct af_info af_info_equalizer; extern struct af_info af_info_pan; @@ -38,7 +37,7 @@ extern struct af_info af_info_export; extern struct af_info af_info_drc; extern struct af_info af_info_extrastereo; extern struct af_info af_info_lavcac3enc; -extern struct af_info af_info_lavcresample; +extern struct af_info af_info_lavrresample; extern struct af_info af_info_sweep; extern struct af_info af_info_hrtf; extern struct af_info af_info_ladspa; @@ -53,7 +52,6 @@ static struct af_info* filter_list[]={ &af_info_delay, &af_info_channels, &af_info_format, - &af_info_resample, &af_info_volume, &af_info_equalizer, &af_info_pan, @@ -65,7 +63,7 @@ static struct af_info* filter_list[]={ &af_info_drc, &af_info_extrastereo, &af_info_lavcac3enc, - &af_info_lavcresample, + &af_info_lavrresample, &af_info_sweep, &af_info_hrtf, #ifdef CONFIG_LADSPA @@ -527,9 +525,7 @@ int af_init(struct af_stream* s) af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &(s->output.rate)); if (!af) { - char *resampler = "resample"; - if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW) - resampler = "lavcresample"; + char *resampler = "lavrresample"; if((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW){ if(!strcmp(s->first->info->name,"format")) af = af_append(s,s->first,resampler); @@ -546,16 +542,6 @@ int af_init(struct af_stream* s) if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &(s->output.rate)))) return -1; - // Use lin int if the user wants fast - if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_FAST) { - char args[32]; - sprintf(args, "%d", s->output.rate); - if (strcmp(resampler, "lavcresample") == 0) - strcat(args, ":1"); - else - strcat(args, ":0:0"); - af->control(af, AF_CONTROL_COMMAND_LINE, args); - } } if(AF_OK != af_reinit(s,af)) return -1; diff --git a/audio/filter/af_lavcresample.c b/audio/filter/af_lavcresample.c deleted file mode 100644 index ce777fed31..0000000000 --- a/audio/filter/af_lavcresample.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2004 Michael Niedermayer - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include -#include - -#include "config.h" -#include "af.h" -#include "libavcodec/avcodec.h" -#include "libavutil/rational.h" - -// Data for specific instances of this filter -typedef struct af_resample_s{ - struct AVResampleContext *avrctx; - int16_t *in[AF_NCH]; - int in_alloc; - int index; - - int filter_length; - int linear; - int phase_shift; - double cutoff; - - int ctx_out_rate; - int ctx_in_rate; - int ctx_filter_size; - int ctx_phase_shift; - int ctx_linear; - double ctx_cutoff; -}af_resample_t; - - -// Initialization and runtime control -static int control(struct af_instance* af, int cmd, void* arg) -{ - af_resample_t* s = (af_resample_t*)af->setup; - struct mp_audio *data= (struct mp_audio*)arg; - int out_rate, test_output_res; // helpers for checking input format - - switch(cmd){ - case AF_CONTROL_REINIT: - if((af->data->rate == data->rate) || (af->data->rate == 0)) - return AF_DETACH; - - af->data->nch = data->nch; - if (af->data->nch > AF_NCH) af->data->nch = AF_NCH; - af->data->format = AF_FORMAT_S16_NE; - af->data->bps = 2; - af->mul = (double)af->data->rate / data->rate; - af->delay = af->data->nch * s->filter_length / min(af->mul, 1); // *bps*.5 - - if (s->ctx_out_rate != af->data->rate || s->ctx_in_rate != data->rate || s->ctx_filter_size != s->filter_length || - s->ctx_phase_shift != s->phase_shift || s->ctx_linear != s->linear || s->ctx_cutoff != s->cutoff) { - if(s->avrctx) av_resample_close(s->avrctx); - s->avrctx= av_resample_init(af->data->rate, /*in_rate*/data->rate, s->filter_length, s->phase_shift, s->linear, s->cutoff); - s->ctx_out_rate = af->data->rate; - s->ctx_in_rate = data->rate; - s->ctx_filter_size = s->filter_length; - s->ctx_phase_shift = s->phase_shift; - s->ctx_linear = s->linear; - s->ctx_cutoff = s->cutoff; - } - - // hack to make af_test_output ignore the samplerate change - out_rate = af->data->rate; - af->data->rate = data->rate; - test_output_res = af_test_output(af, (struct mp_audio*)arg); - af->data->rate = out_rate; - return test_output_res; - case AF_CONTROL_COMMAND_LINE:{ - s->cutoff= 0.0; - sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); - if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); - return AF_OK; - } - case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: - af->data->rate = *(int*)arg; - return AF_OK; - } - return AF_UNKNOWN; -} - -// Deallocate memory -static void uninit(struct af_instance* af) -{ - if(af->data) - free(af->data->audio); - free(af->data); - if(af->setup){ - int i; - af_resample_t *s = af->setup; - if(s->avrctx) av_resample_close(s->avrctx); - for (i=0; i < AF_NCH; i++) - free(s->in[i]); - free(s); - } -} - -// Filter data through filter -static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) -{ - af_resample_t *s = af->setup; - int i, j, consumed, ret = 0; - int16_t *in = (int16_t*)data->audio; - int16_t *out; - int chans = data->nch; - int in_len = data->len/(2*chans); - int out_len = in_len * af->mul + 10; - int16_t tmp[AF_NCH][out_len]; - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - out= (int16_t*)af->data->audio; - - out_len= min(out_len, af->data->len/(2*chans)); - - if(s->in_alloc < in_len + s->index){ - s->in_alloc= in_len + s->index; - for(i=0; iin[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); - } - } - - if(chans==1){ - memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t)); - }else if(chans==2){ - for(j=0; jin[0][j + s->index]= *(in++); - s->in[1][j + s->index]= *(in++); - } - }else{ - for(j=0; jin[i][j + s->index]= *(in++); - } - } - } - in_len += s->index; - - for(i=0; iavrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); - } - out_len= ret; - - s->index= in_len - consumed; - for(i=0; iin[i], s->in[i] + consumed, s->index*sizeof(int16_t)); - } - - if(chans==1){ - memcpy(out, tmp[0], out_len*sizeof(int16_t)); - }else if(chans==2){ - for(j=0; jaudio = af->data->audio; - data->len = out_len*chans*2; - data->rate = af->data->rate; - return data; -} - -static int af_open(struct af_instance* af){ - af_resample_t *s = calloc(1,sizeof(af_resample_t)); - af->control=control; - af->uninit=uninit; - af->play=play; - af->mul=1; - af->data=calloc(1,sizeof(struct mp_audio)); - s->filter_length= 16; - s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); - s->phase_shift= 10; -// s->setup = RSMP_INT | FREQ_SLOPPY; - af->setup=s; - return AF_OK; -} - -struct af_info af_info_lavcresample = { - "Sample frequency conversion using libavcodec", - "lavcresample", - "Michael Niedermayer", - "", - AF_FLAGS_REENTRANT, - af_open -}; diff --git a/audio/filter/af_lavrresample.c b/audio/filter/af_lavrresample.c new file mode 100644 index 0000000000..5b26a0dce6 --- /dev/null +++ b/audio/filter/af_lavrresample.c @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2004 Michael Niedermayer + * Copyright (c) 2013 Stefano Pigozzi + * + * This file is part of mpv. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "talloc.h" +#include "config.h" + +#if defined(CONFIG_LIBAVRESAMPLE) +#include +#elif defined(CONFIG_LIBSWRESAMPLE) +#include +#define AVAudioResampleContext SwrContext +#define avresample_alloc_context swr_alloc +#define avresample_open swr_init +#define avresample_close(x) do { } while(0) +#define avresample_available(x) 0 +#define avresample_convert(ctx, out, out_planesize, out_samples, in, in_planesize, in_samples) \ + swr_convert(ctx, out, out_samples, (const uint8_t**)(in), in_samples) +#else +#error "config.h broken" +#endif + +#include "core/mp_msg.h" +#include "core/subopt-helper.h" +#include "audio/filter/af.h" + +struct af_resample_opts { + int filter_size; + int phase_shift; + int linear; + double cutoff; + + int out_rate; + int in_rate; +}; + +struct af_resample { + struct AVAudioResampleContext *avrctx; + struct af_resample_opts ctx; // opts in the context + struct af_resample_opts opts; // opts requested by the user +}; + +#ifdef CONFIG_LIBAVRESAMPLE +static int get_delay(struct af_resample *s) +{ + return avresample_get_delay(s->avrctx); +} +#else +static int get_delay(struct af_resample *s) +{ + return swr_get_delay(s->avrctx, s->ctx.in_rate); +} +#endif + +static double af_resample_default_cutoff(int filter_size) +{ + return FFMAX(1.0 - 6.5 / (filter_size + 8), 0.80); +} + +static bool needs_lavrctx_reconfigure(struct af_resample *s, + struct mp_audio *in, + struct mp_audio *out) +{ + return s->ctx.out_rate != out->rate || + s->ctx.in_rate != in->rate || + s->ctx.filter_size != s->opts.filter_size || + s->ctx.phase_shift != s->opts.phase_shift || + s->ctx.linear != s->opts.linear || + s->ctx.cutoff != s->opts.cutoff; + +} + +#define ctx_opt_set_int(a,b) av_opt_set_int(s->avrctx, (a), (b), 0) +#define ctx_opt_set_dbl(a,b) av_opt_set_double(s->avrctx, (a), (b), 0) + +static int control(struct af_instance *af, int cmd, void *arg) +{ + struct af_resample *s = (struct af_resample *) af->setup; + struct mp_audio *in = (struct mp_audio *) arg; + struct mp_audio *out = (struct mp_audio *) af->data; + + switch (cmd) { + case AF_CONTROL_REINIT: { + if ((out->rate == in->rate) || (out->rate == 0)) + return AF_DETACH; + + out->nch = FFMIN(in->nch, AF_NCH); + out->format = AF_FORMAT_S16_NE; + out->bps = 2; + af->mul = (double) out->rate / in->rate; + af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1); + + if (needs_lavrctx_reconfigure(s, in, out)) { + if (s->avrctx) + avresample_close(s->avrctx); + + s->ctx.out_rate = out->rate; + s->ctx.in_rate = in->rate; + s->ctx.filter_size = s->opts.filter_size; + s->ctx.phase_shift = s->opts.phase_shift; + s->ctx.linear = s->opts.linear; + s->ctx.cutoff = s->opts.cutoff; + + int ch_layout = av_get_default_channel_layout(out->nch); + + ctx_opt_set_int("in_channel_layout", ch_layout); + ctx_opt_set_int("out_channel_layout", ch_layout); + + ctx_opt_set_int("in_sample_rate", s->ctx.in_rate); + ctx_opt_set_int("out_sample_rate", s->ctx.out_rate); + + ctx_opt_set_int("in_sample_fmt", AV_SAMPLE_FMT_S16); + ctx_opt_set_int("out_sample_fmt", AV_SAMPLE_FMT_S16); + + ctx_opt_set_int("filter_size", s->ctx.filter_size); + ctx_opt_set_int("phase_shift", s->ctx.phase_shift); + ctx_opt_set_int("linear_interp", s->ctx.linear); + + ctx_opt_set_dbl("cutoff", s->ctx.cutoff); + + if (avresample_open(s->avrctx) < 0) { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Cannot open " + "Libavresample Context. \n"); + return AF_ERROR; + } + } + + int out_rate, test_output_res; + // hack to make af_test_output ignore the samplerate change + out_rate = out->rate; + out->rate = in->rate; + test_output_res = af_test_output(af, in); + out->rate = out_rate; + return test_output_res; + } + case AF_CONTROL_COMMAND_LINE: { + s->opts.cutoff = 0.0; + + const opt_t subopts[] = { + {"srate", OPT_ARG_INT, &out->rate, NULL}, + {"filter_size", OPT_ARG_INT, &s->opts.filter_size, NULL}, + {"phase_shift", OPT_ARG_INT, &s->opts.phase_shift, NULL}, + {"linear", OPT_ARG_BOOL, &s->opts.linear, NULL}, + {"cutoff", OPT_ARG_FLOAT, &s->opts.cutoff, NULL}, + {0} + }; + + if (subopt_parse(arg, subopts) != 0) { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Invalid option " + "specified.\n"); + return AF_ERROR; + } + + if (s->opts.cutoff <= 0.0) + s->opts.cutoff = af_resample_default_cutoff(s->opts.filter_size); + return AF_OK; + } + case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: + out->rate = *(int *)arg; + return AF_OK; + } + return AF_UNKNOWN; +} + +#undef ctx_opt_set_int +#undef ctx_opt_set_dbl + +static void uninit(struct af_instance *af) +{ + if (af->setup) { + struct af_resample *s = af->setup; + if (s->avrctx) + avresample_close(s->avrctx); + talloc_free(af->setup); + } +} + +static struct mp_audio *play(struct af_instance *af, struct mp_audio *data) +{ + struct af_resample *s = af->setup; + struct mp_audio *in = data; + struct mp_audio *out = af->data; + + + int in_size = data->len; + int in_samples = in_size / (data->bps * data->nch); + int out_samples = avresample_available(s->avrctx) + + av_rescale_rnd(get_delay(s) + in_samples, + s->ctx.out_rate, s->ctx.in_rate, AV_ROUND_UP); + int out_size = out->bps * out_samples * out->nch; + + if (talloc_get_size(out->audio) < out_size) + out->audio = talloc_realloc_size(out, out->audio, out_size); + + af->delay = out->bps * av_rescale_rnd(get_delay(s), + s->ctx.out_rate, s->ctx.in_rate, + AV_ROUND_UP); + + out_samples = avresample_convert(s->avrctx, + (uint8_t **) &out->audio, out_size, out_samples, + (uint8_t **) &in->audio, in_size, in_samples); + + out_size = out->bps * out_samples * out->nch; + in->audio = out->audio; + in->len = out_size; + in->rate = s->ctx.out_rate; + return data; +} + +static int af_open(struct af_instance *af) +{ + struct af_resample *s = talloc_zero(NULL, struct af_resample); + + af->control = control; + af->uninit = uninit; + af->play = play; + af->mul = 1; + af->data = talloc_zero(s, struct mp_audio); + + af->data->rate = 44100; + + int default_filter_size = 16; + s->opts = (struct af_resample_opts) { + .linear = 0, + .filter_size = default_filter_size, + .cutoff = af_resample_default_cutoff(default_filter_size), + .phase_shift = 10, + }; + + s->avrctx = avresample_alloc_context(); + af->setup = s; + + if (s->avrctx) { + return AF_OK; + } else { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Cannot initialize " + "Libavresample Context. \n"); + uninit(af); + return AF_ERROR; + } +} + +struct af_info af_info_lavrresample = { + "Sample frequency conversion using libavresample", + "lavrresample", + "Stefano Pigozzi (based on Michael Niedermayer's lavcresample)", + "", + AF_FLAGS_REENTRANT, + af_open +}; diff --git a/audio/filter/af_resample.c b/audio/filter/af_resample.c deleted file mode 100644 index 1f0b7cc942..0000000000 --- a/audio/filter/af_resample.c +++ /dev/null @@ -1,394 +0,0 @@ -/* - * This audio filter changes the sample rate. - * - * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include -#include -#include - -#include "libavutil/common.h" -#include "libavutil/mathematics.h" -#include "af.h" -#include "dsp.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 filter length is chosen to 8 if the machine is - slow and to 16 if the machine is fast and has MMX. -*/ - -#if !HAVE_MMX // This machine is slow -#define L8 -#else -#define L16 -#endif - -#include "af_resample_template.c" - -// 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 -{ - void* w; // Current filter weights - void** 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 -} af_resample_t; - -// Fast linear interpolation resample with modest audio quality -static int linint(struct mp_audio* c,struct mp_audio* 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)<pt; - uint16_t tmp; - - switch (nch){ - case 1: - while(pt < end){ - out16[len++]=in16[pt>>STEPACCURACY]; - pt+=step; - } - s->pt=pt & ((1LL<>STEPACCURACY]; - pt+=step; - } - len=(len<<1); - s->pt=pt & ((1LL<>STEPACCURACY)*nch]; - } while (tmp); - len+=nch; - pt+=step; - } - s->pt=pt & ((1LL<setup; - int rv = AF_OK; - float rd = 0; - - // Make sure this filter isn't redundant - if((af->data->rate == data->rate) || (af->data->rate == 0)) - return AF_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_FLOAT_NE))) || - ((s->setup & RSMP_MASK) == RSMP_LIN)){ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_LIN; - af->data->format = AF_FORMAT_S16_NE; - af->data->bps = 2; - mp_msg(MSGT_AFILTER, MSGL_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_FLOAT_NE)) || - ((s->setup & RSMP_MASK) == RSMP_FLOAT)){ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_FLOAT; - af->data->format = AF_FORMAT_FLOAT_NE; - af->data->bps = 4; - } - else{ - s->setup = (s->setup & ~RSMP_MASK) | RSMP_INT; - af->data->format = AF_FORMAT_S16_NE; - af->data->bps = 2; - } - mp_msg(MSGT_AFILTER, MSGL_V, "[resample] Using %s processing and %s frequecy" - " 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) - rv = AF_FALSE; - data->format = af->data->format; - data->bps = af->data->bps; - af->data->nch = data->nch; - return rv; -} - -// Initialization and runtime control -static int control(struct af_instance* af, int cmd, void* arg) -{ - switch(cmd){ - case AF_CONTROL_REINIT:{ - af_resample_t* s = af->setup; - struct mp_audio* n = arg; // New configuration - int i,d = 0; - int rv = AF_OK; - - // Free space for circular buffers - if(s->xq){ - free(s->xq[0]); - free(s->xq); - s->xq = NULL; - } - - if(AF_DETACH == (rv = set_types(af,n))) - return AF_DETACH; - - // If linear interpolation - if((s->setup & RSMP_MASK) == RSMP_LIN){ - s->pt=0LL; - s->step=((uint64_t)n->rate<data->rate+1LL; - mp_msg(MSGT_AFILTER, MSGL_DBG2, "[resample] Linear interpolation step: 0x%016"PRIX64".\n", - s->step); - af->mul = (double)af->data->rate / n->rate; - return rv; - } - - // Calculate up and down sampling factors - d=av_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) > 5000){ - d=av_gcd(up,dn); - up/=2; dn/=2; m*=2; - } - d*=m; - } - - // Create space for circular buffers - s->xq = malloc(n->nch*sizeof(void*)); - s->xq[0] = calloc(n->nch, 2*L*af->data->bps); - for(i=1;inch;i++) - s->xq[i] = (uint8_t *)s->xq[i-1] + 2*L*af->data->bps; - s->xi = 0; - - // Check if 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; - s->up = af->data->rate/d; - s->dn = n->rate/d; - s->wi = 0; - s->i = 0; - - // Calculate cutoff frequency for filter - fc = 1/(float)(max(s->up,s->dn)); - // Allocate space for polyphase filter bank and prototype filter - w = malloc(sizeof(float) * s->up *L); - free(s->w); - s->w = malloc(L*s->up*af->data->bps); - - // Design prototype filter type using Kaiser window with beta = 10 - if(NULL == w || NULL == s->w || - -1 == af_filter_design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ - mp_msg(MSGT_AFILTER, MSGL_ERR, "[resample] Unable to design prototype filter.\n"); - return AF_ERROR; - } - // Copy data from prototype to polyphase filter - wt=w; - for(j=0;jup;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++; - } - } - free(w); - mp_msg(MSGT_AFILTER, MSGL_V, "[resample] New filter designed up: %i " - "down: %i\n", s->up, s->dn); - } - - // Set multiplier and delay - af->delay = 0; // not set correctly, but shouldn't be too large anyway - af->mul = (double)s->up / s->dn; - return rv; - } - case AF_CONTROL_COMMAND_LINE:{ - af_resample_t* s = 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((((struct af_cfg*)arg)->force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) - ((af_resample_t*)af->setup)->setup = RSMP_FLOAT; - return AF_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] < 8000 || ((int*)arg)[0] > 192000){ - mp_msg(MSGT_AFILTER, MSGL_ERR, "[resample] The output sample frequency " - "must be between 8kHz and 192kHz. Current value is %i \n", - ((int*)arg)[0]); - return AF_ERROR; - } - - af->data->rate=((int*)arg)[0]; - mp_msg(MSGT_AFILTER, MSGL_V, "[resample] Changing sample rate " - "to %iHz\n",af->data->rate); - return AF_OK; - } - return AF_UNKNOWN; -} - -// Deallocate memory -static void uninit(struct af_instance* af) -{ - af_resample_t *s = af->setup; - if (s) { - if (s->xq) free(s->xq[0]); - free(s->xq); - free(s->w); - free(s); - } - if(af->data) - free(af->data->audio); - free(af->data); -} - -// Filter data through filter -static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) -{ - int len = 0; // Length of output data - struct mp_audio* c = data; // Current working data - struct mp_audio* l = af->data; // Local data - af_resample_t* s = af->setup; - - if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) - return NULL; - - // Run resampling - switch(s->setup & RSMP_MASK){ - case(RSMP_INT): -# define FORMAT_I 1 - if(s->up>s->dn){ -# define UP -# include "af_resample_template.c" -# undef UP - } - else{ -# define DN -# include "af_resample_template.c" -# undef DN - } - break; - case(RSMP_FLOAT): -# undef FORMAT_I -# define FORMAT_F 1 - if(s->up>s->dn){ -# define UP -# include "af_resample_template.c" -# undef UP - } - else{ -# define DN -# include "af_resample_template.c" -# undef DN - } - break; - case(RSMP_LIN): - len = linint(c, l, s); - break; - } - - // Set output data - c->audio = l->audio; - c->len = len*l->bps; - c->rate = l->rate; - - return c; -} - -// Allocate memory and set function pointers -static int af_open(struct af_instance* af){ - af->control=control; - af->uninit=uninit; - af->play=play; - af->mul=1; - af->data=calloc(1,sizeof(struct mp_audio)); - af->setup=calloc(1,sizeof(af_resample_t)); - if(af->data == NULL || af->setup == NULL) - return AF_ERROR; - ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY; - return AF_OK; -} - -// Description of this plugin -struct af_info af_info_resample = { - "Sample frequency conversion", - "resample", - "Anders", - "", - AF_FLAGS_REENTRANT, - af_open -}; diff --git a/audio/filter/af_resample_template.c b/audio/filter/af_resample_template.c deleted file mode 100644 index 4d4c5922ca..0000000000 --- a/audio/filter/af_resample_template.c +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au - * - * This file is part of MPlayer. - * - * MPlayer is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * MPlayer is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with MPlayer; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* 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 length 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 -#else -#define SHIFT -#define FORMAT float -#endif - -// Short filter -#if defined(L8) - -#define L 8 // Filter length -// Unrolled loop to speed up execution -#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 - - - -#else /* L8/L16 */ - -#define L 16 -// Unrolled loop to speed up execution -#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 /* 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 ci = l->nch; // Index for channels - uint32_t nch = l->nch; // Number of channels - uint32_t inc = s->up/s->dn; - uint32_t level = s->up%s->dn; - uint32_t up = s->up; - uint32_t dn = s->dn; - uint32_t ns = c->len/l->bps; - register FORMAT* w = s->w; - - register uint32_t wi = 0; - register uint32_t xi = 0; - - // 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(wiwi = wi; - s->xi = xi; -#endif /* UP */ - -#if defined(DN) /* DN */ - uint32_t ci = l->nch; // Index for channels - uint32_t nch = l->nch; // Number of channels - uint32_t inc = s->dn/s->up; - uint32_t level = s->dn%s->up; - uint32_t up = s->up; - uint32_t dn = s->dn; - uint32_t ns = c->len/l->bps; - FORMAT* w = s->w; - - register int32_t i = 0; - register uint32_t wi = 0; - register uint32_t xi = 0; - - // 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 - 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(wiwi = wi; - s->xi = xi; - s->i = i; -#endif /* DN */ -- cgit v1.2.3