diff options
Diffstat (limited to 'audio')
-rw-r--r-- | audio/decode/ad.h | 2 | ||||
-rw-r--r-- | audio/decode/ad_internal.h | 2 | ||||
-rw-r--r-- | audio/decode/ad_lavc.c | 4 | ||||
-rw-r--r-- | audio/decode/ad_mpg123.c | 2 | ||||
-rw-r--r-- | audio/decode/ad_spdif.c | 30 | ||||
-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 | ||||
-rw-r--r-- | audio/mixer.c | 4 | ||||
-rw-r--r-- | audio/mixer.h | 2 | ||||
-rw-r--r-- | audio/out/ao_dsound.c | 1 | ||||
-rw-r--r-- | audio/out/ao_jack.c | 7 | ||||
-rw-r--r-- | audio/out/ao_lavc.c | 32 |
16 files changed, 405 insertions, 872 deletions
diff --git a/audio/decode/ad.h b/audio/decode/ad.h index 3bc3e39267..0bbc11ed9b 100644 --- a/audio/decode/ad.h +++ b/audio/decode/ad.h @@ -34,7 +34,7 @@ typedef struct ad_functions int (*preinit)(sh_audio_t *sh); int (*init)(sh_audio_t *sh, const char *decoder); void (*uninit)(sh_audio_t *sh); - int (*control)(sh_audio_t *sh,int cmd,void* arg, ...); + int (*control)(sh_audio_t *sh, int cmd, void *arg); int (*decode_audio)(sh_audio_t *sh, unsigned char *buffer, int minlen, int maxlen); } ad_functions_t; diff --git a/audio/decode/ad_internal.h b/audio/decode/ad_internal.h index 7eca629fca..61bf306a5e 100644 --- a/audio/decode/ad_internal.h +++ b/audio/decode/ad_internal.h @@ -32,7 +32,7 @@ static void add_decoders(struct mp_decoder_list *list); static int init(sh_audio_t *sh, const char *decoder); static int preinit(sh_audio_t *sh); static void uninit(sh_audio_t *sh); -static int control(sh_audio_t *sh,int cmd,void* arg, ...); +static int control(sh_audio_t *sh, int cmd, void *arg); static int decode_audio(sh_audio_t *sh,unsigned char *buffer,int minlen,int maxlen); #define LIBAD_EXTERN(x) const ad_functions_t mpcodecs_ad_##x = {\ diff --git a/audio/decode/ad_lavc.c b/audio/decode/ad_lavc.c index 1f59280275..248df307d7 100644 --- a/audio/decode/ad_lavc.c +++ b/audio/decode/ad_lavc.c @@ -160,7 +160,7 @@ static int setup_format(sh_audio_t *sh_audio, int container_samplerate = sh_audio->container_out_samplerate; if (!container_samplerate && sh_audio->wf) container_samplerate = sh_audio->wf->nSamplesPerSec; - if (lavc_context->codec_id == CODEC_ID_AAC + if (lavc_context->codec_id == AV_CODEC_ID_AAC && samplerate == 2 * container_samplerate) broken_srate = true; else if (container_samplerate) @@ -310,7 +310,7 @@ static void uninit(sh_audio_t *sh) sh->context = NULL; } -static int control(sh_audio_t *sh, int cmd, void *arg, ...) +static int control(sh_audio_t *sh, int cmd, void *arg) { struct priv *ctx = sh->context; switch (cmd) { diff --git a/audio/decode/ad_mpg123.c b/audio/decode/ad_mpg123.c index 999dc2fbba..c17edc9197 100644 --- a/audio/decode/ad_mpg123.c +++ b/audio/decode/ad_mpg123.c @@ -455,7 +455,7 @@ static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen, return bytes; } -static int control(sh_audio_t *sh, int cmd, void *arg, ...) +static int control(sh_audio_t *sh, int cmd, void *arg) { switch (cmd) { case ADCTRL_RESYNC_STREAM: diff --git a/audio/decode/ad_spdif.c b/audio/decode/ad_spdif.c index 128b1bd8a9..2101721430 100644 --- a/audio/decode/ad_spdif.c +++ b/audio/decode/ad_spdif.c @@ -73,13 +73,13 @@ static int preinit(sh_audio_t *sh) } static int codecs[] = { - CODEC_ID_AAC, - CODEC_ID_AC3, - CODEC_ID_DTS, - CODEC_ID_EAC3, - CODEC_ID_MP3, - CODEC_ID_TRUEHD, - CODEC_ID_NONE + AV_CODEC_ID_AAC, + AV_CODEC_ID_AC3, + AV_CODEC_ID_DTS, + AV_CODEC_ID_EAC3, + AV_CODEC_ID_MP3, + AV_CODEC_ID_TRUEHD, + AV_CODEC_ID_NONE }; static int init(sh_audio_t *sh, const char *decoder) @@ -149,21 +149,21 @@ static int init(sh_audio_t *sh, const char *decoder) sh->ds->buffer_pos -= in_size; switch (lavf_ctx->streams[0]->codec->codec_id) { - case CODEC_ID_AAC: + case AV_CODEC_ID_AAC: spdif_ctx->iec61937_packet_size = 16384; sh->sample_format = AF_FORMAT_IEC61937_LE; sh->samplerate = srate; sh->channels = 2; sh->i_bps = bps; break; - case CODEC_ID_AC3: + case AV_CODEC_ID_AC3: spdif_ctx->iec61937_packet_size = 6144; sh->sample_format = AF_FORMAT_AC3_LE; sh->samplerate = srate; sh->channels = 2; sh->i_bps = bps; break; - case CODEC_ID_DTS: + case AV_CODEC_ID_DTS: if(sh->opts->dtshd) { opt = av_opt_find(&lavf_ctx->oformat->priv_class, "dtshd_rate", NULL, 0, 0); @@ -185,21 +185,21 @@ static int init(sh_audio_t *sh, const char *decoder) sh->i_bps = bps; } break; - case CODEC_ID_EAC3: + case AV_CODEC_ID_EAC3: spdif_ctx->iec61937_packet_size = 24576; sh->sample_format = AF_FORMAT_IEC61937_LE; sh->samplerate = 192000; sh->channels = 2; sh->i_bps = bps; break; - case CODEC_ID_MP3: + case AV_CODEC_ID_MP3: spdif_ctx->iec61937_packet_size = 4608; sh->sample_format = AF_FORMAT_MPEG2; sh->samplerate = srate; sh->channels = 2; sh->i_bps = bps; break; - case CODEC_ID_TRUEHD: + case AV_CODEC_ID_TRUEHD: spdif_ctx->iec61937_packet_size = 61440; sh->sample_format = AF_FORMAT_IEC61937_LE; sh->samplerate = 192000; @@ -270,7 +270,7 @@ static int decode_audio(sh_audio_t *sh, unsigned char *buf, return spdif_ctx->out_buffer_len; } -static int control(sh_audio_t *sh, int cmd, void* arg, ...) +static int control(sh_audio_t *sh, int cmd, void *arg) { unsigned char *start; double pts; @@ -307,7 +307,7 @@ static void uninit(sh_audio_t *sh) static void add_decoders(struct mp_decoder_list *list) { - for (int n = 0; codecs[n] != CODEC_ID_NONE; n++) { + for (int n = 0; codecs[n] != AV_CODEC_ID_NONE; n++) { const char *format = mp_codec_from_av_codec_id(codecs[n]); if (format) { mp_add_decoder(list, "spdif", format, format, 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; |