diff options
author | wm4 <wm4@nowhere> | 2013-04-30 00:52:32 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-04-30 00:52:32 +0200 |
commit | ea7b920184a865a3343001318fc4a32dcfc7b4f5 (patch) | |
tree | fbefd8539b77ee7473ad73740cc8ffa97bc4ffbe /audio/filter | |
parent | 3dcc83a70609d392c8ecd917dd5c16995424e9c4 (diff) | |
parent | d98e61ea438db66323734ad1b6bea66411a3c97b (diff) | |
download | mpv-ea7b920184a865a3343001318fc4a32dcfc7b4f5.tar.bz2 mpv-ea7b920184a865a3343001318fc4a32dcfc7b4f5.tar.xz |
Merge branch 'master' into low_quality_intel_crap
Conflicts:
video/out/gl_video_shaders.glsl
video/out/vo_opengl.c
Diffstat (limited to 'audio/filter')
-rw-r--r-- | audio/filter/af.c | 20 | ||||
-rw-r--r-- | audio/filter/af_lavcac3enc.c | 116 | ||||
-rw-r--r-- | audio/filter/af_lavcresample.c | 213 | ||||
-rw-r--r-- | audio/filter/af_lavrresample.c | 277 | ||||
-rw-r--r-- | audio/filter/af_resample.c | 394 | ||||
-rw-r--r-- | audio/filter/af_resample_template.c | 171 |
6 files changed, 351 insertions, 840 deletions
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_lavcac3enc.c b/audio/filter/af_lavcac3enc.c index 2b7a4ffb4c..94d59a11ca 100644 --- a/audio/filter/af_lavcac3enc.c +++ b/audio/filter/af_lavcac3enc.c @@ -27,7 +27,9 @@ #include <assert.h> #include <libavcodec/avcodec.h> +#include <libavutil/audioconvert.h> #include <libavutil/intreadwrite.h> +#include <libavutil/common.h> #include <libavutil/mem.h> #include "config.h" @@ -47,6 +49,7 @@ const uint16_t ac3_bitrate_tab[19] = { typedef struct af_ac3enc_s { struct AVCodec *lavc_acodec; struct AVCodecContext *lavc_actx; + AVPacket pkt; bool planarize; int add_iec61937_header; int bit_rate; @@ -105,6 +108,8 @@ static int control(struct af_instance *af, int cmd, void *arg) // Put sample parameters s->lavc_actx->channels = af->data->nch; + s->lavc_actx->channel_layout = + av_get_default_channel_layout(af->data->nch); s->lavc_actx->sample_rate = af->data->rate; s->lavc_actx->bit_rate = bit_rate; @@ -155,12 +160,13 @@ static int control(struct af_instance *af, int cmd, void *arg) // Deallocate memory static void uninit(struct af_instance* af) { + af_ac3enc_t *s = af->setup; + if (af->data) free(af->data->audio); free(af->data); - if (af->setup) { - af_ac3enc_t *s = af->setup; - af->setup = NULL; + if (s) { + av_free_packet(&s->pkt); if(s->lavc_actx) { avcodec_close(s->lavc_actx); av_free(s->lavc_actx); @@ -176,8 +182,8 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) af_ac3enc_t *s = af->setup; struct mp_audio *c = data; // Current working data struct mp_audio *l; - int len, left, outsize = 0, destsize; - char *buf, *src, *dest; + int left, outsize = 0; + char *buf, *src; int max_output_len; int frame_num = (data->len + s->pending_len) / s->expect_len; int samplesize = af_fmt2bits(s->in_sampleformat) / 8; @@ -207,6 +213,8 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) while (left > 0) { + int ret; + if (left + s->pending_len < s->expect_len) { memcpy(s->pending_data + s->pending_len, src, left); src += left; @@ -215,8 +223,7 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) break; } - dest = s->add_iec61937_header ? buf + 8 : buf; - destsize = (char *)l->audio + l->len - buf; + char *src2 = src; if (s->pending_len) { int needs = s->expect_len - s->pending_len; @@ -225,58 +232,69 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) src += needs; left -= needs; } + src2= s->pending_data; + } - if (c->nch >= 5) - reorder_channel_nch(s->pending_data, - AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, - AF_CHANNEL_LAYOUT_LAVC_DEFAULT, - c->nch, - s->expect_len / samplesize, samplesize); - - void *data = (void *) s->pending_data; - if (s->planarize) { - void *data2 = malloc(s->expect_len); - reorder_to_planar(data2, data, samplesize, - c->nch, s->expect_len / samplesize / c->nch); - data = data2; - } + if (c->nch >= 5) { + reorder_channel_nch(src2, + AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, + AF_CHANNEL_LAYOUT_LAVC_DEFAULT, + c->nch, + s->expect_len / samplesize, + samplesize); + } - len = avcodec_encode_audio(s->lavc_actx, dest, destsize, data); + void *data = (void *) src2; + if (s->planarize) { + void *data2 = malloc(s->expect_len); + reorder_to_planar(data2, data, samplesize, + c->nch, s->expect_len / samplesize / c->nch); + data = data2; + } - if (s->planarize) - free(data); + AVFrame *frame = avcodec_alloc_frame(); + if (!frame) { + mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n"); + return NULL; + } + frame->nb_samples = AC3_FRAME_SIZE; + frame->format = s->lavc_actx->sample_fmt; + frame->channel_layout = s->lavc_actx->channel_layout; + + ret = avcodec_fill_audio_frame(frame, c->nch, s->lavc_actx->sample_fmt, + (const uint8_t*)data, s->expect_len, 0); + if (ret < 0) { + mp_msg(MSGT_AFILTER, MSGL_FATAL, "[lavac3enc] Frame setup failed.\n"); + return NULL; + } - s->pending_len = 0; + int ok; + ret = avcodec_encode_audio2(s->lavc_actx, &s->pkt, frame, &ok); + if (ret < 0 || !ok) { + mp_msg(MSGT_AFILTER, MSGL_FATAL, "[lavac3enc] Encode failed.\n"); + return NULL; } - else { - if (c->nch >= 5) - reorder_channel_nch(src, - AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, - AF_CHANNEL_LAYOUT_LAVC_DEFAULT, - c->nch, - s->expect_len / samplesize, samplesize); - - void *data = (void *) src; - if (s->planarize) { - void *data2 = malloc(s->expect_len); - reorder_to_planar(data2, data, samplesize, - c->nch, s->expect_len / samplesize / c->nch); - data = data2; - } - len = avcodec_encode_audio(s->lavc_actx, dest, destsize, data); + if (s->planarize) + free(data); - if (s->planarize) - free(data); + avcodec_free_frame(&frame); + if (s->pending_len) { + s->pending_len = 0; + } else { src += s->expect_len; left -= s->expect_len; } + mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n", - len, s->pending_len); + s->pkt.size, s->pending_len); + int len = s->pkt.size; + int header_len = 0; if (s->add_iec61937_header) { - int bsmod = dest[5] & 0x7; + assert(s->pkt.size > 5); + int bsmod = s->pkt.data[5] & 0x7; AV_WB16(buf, 0xF872); // iec 61937 syncword 1 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2 @@ -285,9 +303,15 @@ static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) AV_WB16(buf + 6, len << 3); // number of bits in payload memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len); + header_len = 8; len = AC3_FRAME_SIZE * 2 * 2; } + assert(buf + len <= (char *)af->data->audio + af->data->len); + assert(s->pkt.size <= len - header_len); + + memcpy(buf + header_len, s->pkt.data, s->pkt.size); + outsize += len; buf += len; } @@ -360,6 +384,8 @@ static int af_open(struct af_instance* af){ mp_msg(MSGT_AFILTER, MSGL_WARN, "[af_lavcac3enc]: need to planarize audio data\n"); + av_init_packet(&s->pkt); + return AF_OK; } 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 <michaelni@gmx.at> - * - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <inttypes.h> - -#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; i<chans; i++){ - s->in[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; j<in_len; j++){ - s->in[0][j + s->index]= *(in++); - s->in[1][j + s->index]= *(in++); - } - }else{ - for(j=0; j<in_len; j++){ - for(i=0; i<chans; i++){ - s->in[i][j + s->index]= *(in++); - } - } - } - in_len += s->index; - - for(i=0; i<chans; i++){ - ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); - } - out_len= ret; - - s->index= in_len - consumed; - for(i=0; i<chans; i++){ - memmove(s->in[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; j<out_len; j++){ - *(out++)= tmp[0][j]; - *(out++)= tmp[1][j]; - } - }else{ - for(j=0; j<out_len; j++){ - for(i=0; i<chans; i++){ - *(out++)= tmp[i][j]; - } - } - } - - data->audio = 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 <michaelni@gmx.at> + * Copyright (c) 2013 Stefano Pigozzi <stefano.pigozzi@gmail.com> + * + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> +#include <libavutil/opt.h> +#include <libavutil/audioconvert.h> +#include <libavutil/common.h> +#include <libavutil/samplefmt.h> +#include <libavutil/mathematics.h> + +#include "talloc.h" +#include "config.h" + +#if defined(CONFIG_LIBAVRESAMPLE) +#include <libavresample/avresample.h> +#elif defined(CONFIG_LIBSWRESAMPLE) +#include <libswresample/swresample.h> +#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 <stdio.h> -#include <stdlib.h> -#include <inttypes.h> - -#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)<<STEPACCURACY); - uint64_t pt = s->pt; - uint16_t tmp; - - switch (nch){ - case 1: - while(pt < end){ - out16[len++]=in16[pt>>STEPACCURACY]; - pt+=step; - } - 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; -} - -/* Determine resampling type and format */ -static int set_types(struct af_instance* af, struct mp_audio* data) -{ - af_resample_t* s = af->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)? |