From cb0b0d99a4ab04905f59ec1a9fcbf90635105d11 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 3 Nov 2012 14:06:53 +0100 Subject: ad_lavc: use fmt-conversion to map sample formats --- audio/decode/ad_lavc.c | 24 +++++-------------- audio/fmt-conversion.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ audio/fmt-conversion.h | 25 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 audio/fmt-conversion.c create mode 100644 audio/fmt-conversion.h (limited to 'audio') diff --git a/audio/decode/ad_lavc.c b/audio/decode/ad_lavc.c index 382d4eacc5..b7dec6bc71 100644 --- a/audio/decode/ad_lavc.c +++ b/audio/decode/ad_lavc.c @@ -35,6 +35,7 @@ #include "ad_internal.h" #include "audio/reorder_ch.h" +#include "audio/fmt-conversion.h" #include "compat/mpbswap.h" #include "compat/libav.h" @@ -144,17 +145,8 @@ static int preinit(sh_audio_t *sh) static int setup_format(sh_audio_t *sh_audio, const AVCodecContext *lavc_context) { - int sample_format = sh_audio->sample_format; - switch (av_get_packed_sample_fmt(lavc_context->sample_fmt)) { - case AV_SAMPLE_FMT_U8: sample_format = AF_FORMAT_U8; break; - case AV_SAMPLE_FMT_S16: sample_format = AF_FORMAT_S16_NE; break; - case AV_SAMPLE_FMT_S32: sample_format = AF_FORMAT_S32_NE; break; - case AV_SAMPLE_FMT_FLT: sample_format = AF_FORMAT_FLOAT_NE; break; - default: - mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n"); - sample_format = AF_FORMAT_UNKNOWN; - } - + int sample_format = + af_from_avformat(av_get_packed_sample_fmt(lavc_context->sample_fmt)); bool broken_srate = false; int samplerate = lavc_context->sample_rate; int container_samplerate = sh_audio->container_out_samplerate; @@ -279,13 +271,9 @@ static int init(sh_audio_t *sh_audio, const char *decoder) if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec) sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec; - switch (av_get_packed_sample_fmt(lavc_context->sample_fmt)) { - case AV_SAMPLE_FMT_U8: - case AV_SAMPLE_FMT_S16: - case AV_SAMPLE_FMT_S32: - case AV_SAMPLE_FMT_FLT: - break; - default: + int af_sample_fmt = + af_from_avformat(av_get_packed_sample_fmt(lavc_context->sample_fmt)); + if (af_sample_fmt == AF_FORMAT_UNKNOWN) { uninit(sh_audio); return 0; } diff --git a/audio/fmt-conversion.c b/audio/fmt-conversion.c new file mode 100644 index 0000000000..58943d3b4b --- /dev/null +++ b/audio/fmt-conversion.c @@ -0,0 +1,64 @@ +/* + * 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 "core/mp_msg.h" +#include +#include +#include "format.h" +#include "fmt-conversion.h" + +static const struct { + enum AVSampleFormat sample_fmt; + int fmt; +} audio_conversion_map[] = { + {AV_SAMPLE_FMT_U8, AF_FORMAT_U8}, + {AV_SAMPLE_FMT_S16, AF_FORMAT_S16_NE}, + {AV_SAMPLE_FMT_S32, AF_FORMAT_S32_NE}, + {AV_SAMPLE_FMT_FLT, AF_FORMAT_FLOAT_NE}, + + {AV_SAMPLE_FMT_NONE, 0}, +}; + +enum AVSampleFormat af_to_avformat(int fmt) +{ + int i; + enum AVSampleFormat sample_fmt; + for (i = 0; audio_conversion_map[i].fmt; i++) + if (audio_conversion_map[i].fmt == fmt) + break; + sample_fmt = audio_conversion_map[i].sample_fmt; + if (sample_fmt == AF_FORMAT_UNKNOWN) + mp_msg(MSGT_GLOBAL, MSGL_V, "Unsupported sample format: %s\n", + af_fmt2str_short(fmt)); + return sample_fmt; +} + +int af_from_avformat(enum AVSampleFormat sample_fmt) +{ + int i; + for (i = 0; audio_conversion_map[i].fmt; i++) + if (audio_conversion_map[i].sample_fmt == sample_fmt) + break; + int fmt = audio_conversion_map[i].fmt; + if (!fmt) { + const char *fmtname = av_get_sample_fmt_name(sample_fmt); + mp_msg(MSGT_GLOBAL, MSGL_ERR, "Unsupported AVSampleFormat %s (%d)\n", + fmtname ? fmtname : "INVALID", sample_fmt); + } + return fmt; +} diff --git a/audio/fmt-conversion.h b/audio/fmt-conversion.h new file mode 100644 index 0000000000..7f2739f86f --- /dev/null +++ b/audio/fmt-conversion.h @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#ifndef MPLAYER_SAMPLE_FMT_CONVERSION_H +#define MPLAYER_SAMPLE_FMT_CONVERSION_H + +enum AVSampleFormat af_to_avformat(int fmt); +int af_from_avformat(enum AVSampleFormat sample_fmt); + +#endif /* MPLAYER_SAMPLE_FMT_CONVERSION_H */ -- cgit v1.2.3 From 8bf759e888249e57ea8e786a368e0068a24838dc Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 21 Mar 2013 00:49:16 +0100 Subject: af: uncrustify --- audio/filter/af.c | 959 ++++++++++++++++++++++++++++-------------------------- audio/filter/af.h | 3 +- 2 files changed, 496 insertions(+), 466 deletions(-) (limited to 'audio') diff --git a/audio/filter/af.c b/audio/filter/af.c index ad43e5fca7..e240c83a40 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -47,200 +47,206 @@ extern struct af_info af_info_karaoke; extern struct af_info af_info_scaletempo; extern struct af_info af_info_bs2b; -static struct af_info* filter_list[]={ - &af_info_dummy, - &af_info_delay, - &af_info_channels, - &af_info_format, - &af_info_volume, - &af_info_equalizer, - &af_info_pan, - &af_info_surround, - &af_info_sub, +static struct af_info* filter_list[] = { + &af_info_dummy, + &af_info_delay, + &af_info_channels, + &af_info_format, + &af_info_volume, + &af_info_equalizer, + &af_info_pan, + &af_info_surround, + &af_info_sub, #ifdef HAVE_SYS_MMAN_H - &af_info_export, + &af_info_export, #endif - &af_info_drc, - &af_info_extrastereo, - &af_info_lavcac3enc, - &af_info_lavrresample, - &af_info_sweep, - &af_info_hrtf, + &af_info_drc, + &af_info_extrastereo, + &af_info_lavcac3enc, + &af_info_lavrresample, + &af_info_sweep, + &af_info_hrtf, #ifdef CONFIG_LADSPA - &af_info_ladspa, + &af_info_ladspa, #endif - &af_info_center, - &af_info_sinesuppress, - &af_info_karaoke, - &af_info_scaletempo, + &af_info_center, + &af_info_sinesuppress, + &af_info_karaoke, + &af_info_scaletempo, #ifdef CONFIG_LIBBS2B - &af_info_bs2b, + &af_info_bs2b, #endif - NULL + NULL }; // CPU speed -int* af_cpu_speed = NULL; +int *af_cpu_speed = NULL; /* Find a filter in the static list of filters using it's name. This function is used internally */ -static struct af_info* af_find(char*name) +static struct af_info *af_find(char *name) { - int i=0; - while(filter_list[i]){ - if(!strcmp(filter_list[i]->name,name)) - return filter_list[i]; - i++; - } - mp_msg(MSGT_AFILTER, MSGL_ERR, "Couldn't find audio filter '%s'\n",name); - return NULL; + int i = 0; + while (filter_list[i]) { + if (!strcmp(filter_list[i]->name, name)) + return filter_list[i]; + i++; + } + mp_msg(MSGT_AFILTER, MSGL_ERR, "Couldn't find audio filter '%s'\n", name); + return NULL; } /* Find filter in the dynamic filter list using it's name This function is used for finding already initialized filters */ -struct af_instance* af_get(struct af_stream* s, char* name) +struct af_instance *af_get(struct af_stream *s, char *name) { - struct af_instance* af=s->first; - // Find the filter - while(af != NULL){ - if(!strcmp(af->info->name,name)) - return af; - af=af->next; - } - return NULL; + struct af_instance *af = s->first; + // Find the filter + while (af != NULL) { + if (!strcmp(af->info->name, name)) + return af; + af = af->next; + } + return NULL; } -/*/ Function for creating a new filter of type name. The name may - contain the commandline parameters for the filter */ -static struct af_instance* af_create(struct af_stream* s, const char* name_with_cmd) +/* Function for creating a new filter of type name.The name may +contain the commandline parameters for the filter */ +static struct af_instance *af_create(struct af_stream *s, + const char *name_with_cmd) { - char* name = strdup(name_with_cmd); - char* cmdline = name; - - // Allocate space for the new filter and reset all pointers - struct af_instance* new=malloc(sizeof(struct af_instance)); - if (!name || !new) { - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Could not allocate memory\n"); - goto err_out; - } - memset(new,0,sizeof(struct af_instance)); - - // Check for commandline parameters - char *skip = strstr(cmdline, "="); - if (skip) { - *skip = '\0'; // for name - cmdline = skip + 1; - } else { - cmdline = NULL; - } - - // Find filter from name - if(NULL == (new->info=af_find(name))) - goto err_out; - - /* Make sure that the filter is not already in the list if it is - non-reentrant */ - if(new->info->flags & AF_FLAGS_NOT_REENTRANT){ - if(af_get(s,name)){ - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] There can only be one instance of" - " the filter '%s' in each stream\n",name); - goto err_out; + char *name = strdup(name_with_cmd); + char *cmdline = name; + + // Allocate space for the new filter and reset all pointers + struct af_instance *new = malloc(sizeof(struct af_instance)); + if (!name || !new) { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Could not allocate memory\n"); + goto err_out; } - } + memset(new, 0, sizeof(struct af_instance)); - mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Adding filter %s \n",name); + // Check for commandline parameters + char *skip = strstr(cmdline, "="); + if (skip) { + *skip = '\0'; // for name + cmdline = skip + 1; + } else { + cmdline = NULL; + } - // Initialize the new filter - if(AF_OK == new->info->open(new) && - AF_ERROR < new->control(new,AF_CONTROL_POST_CREATE,&s->cfg)){ - if(cmdline){ - if(AF_ERROR>=new->control(new,AF_CONTROL_COMMAND_LINE,cmdline)) + // Find filter from name + if (NULL == (new->info = af_find(name))) goto err_out; + + /* Make sure that the filter is not already in the list if it is + non-reentrant */ + if (new->info->flags & AF_FLAGS_NOT_REENTRANT) { + if (af_get(s, name)) { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] There can only be one " + "instance of the filter '%s' in each stream\n", name); + goto err_out; + } + } + + mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Adding filter %s \n", name); + + // Initialize the new filter + if (AF_OK == new->info->open(new) && + AF_ERROR < new->control(new, AF_CONTROL_POST_CREATE, &s->cfg)) + { + if (cmdline) { + if (AF_ERROR >= new->control(new, AF_CONTROL_COMMAND_LINE, cmdline)) + goto err_out; + } + free(name); + return new; } - free(name); - return new; - } err_out: - free(new); - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Couldn't create or open audio filter '%s'\n", - name); - free(name); - return NULL; + free(new); + mp_msg(MSGT_AFILTER, MSGL_ERR, + "[libaf] Couldn't create or open audio filter '%s'\n", name); + free(name); + return NULL; } /* Create and insert a new filter of type name before the filter in the argument. This function can be called during runtime, the return value is the new filter */ -static struct af_instance* af_prepend(struct af_stream* s, struct af_instance* af, const char* name) +static struct af_instance *af_prepend(struct af_stream *s, + struct af_instance *af, + const char *name) { - // Create the new filter and make sure it is OK - struct af_instance* new=af_create(s,name); - if(!new) - return NULL; - // Update pointers - new->next=af; - if(af){ - new->prev=af->prev; - af->prev=new; - } - else - s->last=new; - if(new->prev) - new->prev->next=new; - else - s->first=new; - return new; + // Create the new filter and make sure it is OK + struct af_instance *new = af_create(s, name); + if (!new) + return NULL; + // Update pointers + new->next = af; + if (af) { + new->prev = af->prev; + af->prev = new; + } else + s->last = new; + if (new->prev) + new->prev->next = new; + else + s->first = new; + return new; } /* Create and insert a new filter of type name after the filter in the argument. This function can be called during runtime, the return value is the new filter */ -static struct af_instance* af_append(struct af_stream* s, struct af_instance* af, const char* name) +static struct af_instance *af_append(struct af_stream *s, + struct af_instance *af, + const char *name) { - // Create the new filter and make sure it is OK - struct af_instance* new=af_create(s,name); - if(!new) - return NULL; - // Update pointers - new->prev=af; - if(af){ - new->next=af->next; - af->next=new; - } - else - s->first=new; - if(new->next) - new->next->prev=new; - else - s->last=new; - return new; + // Create the new filter and make sure it is OK + struct af_instance *new = af_create(s, name); + if (!new) + return NULL; + // Update pointers + new->prev = af; + if (af) { + new->next = af->next; + af->next = new; + } else + s->first = new; + if (new->next) + new->next->prev = new; + else + s->last = new; + return new; } // Uninit and remove the filter "af" -void af_remove(struct af_stream* s, struct af_instance* af) +void af_remove(struct af_stream *s, struct af_instance *af) { - if(!af) return; - - // Print friendly message - mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Removing filter %s \n",af->info->name); - - // Notify filter before changing anything - af->control(af,AF_CONTROL_PRE_DESTROY,0); - - // Detach pointers - if(af->prev) - af->prev->next=af->next; - else - s->first=af->next; - if(af->next) - af->next->prev=af->prev; - else - s->last=af->prev; - - // Uninitialize af and free memory - af->uninit(af); - free(af); + if (!af) + return; + + // Print friendly message + mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Removing filter %s \n", + af->info->name); + + // Notify filter before changing anything + af->control(af, AF_CONTROL_PRE_DESTROY, 0); + + // Detach pointers + if (af->prev) + af->prev->next = af->next; + else + s->first = af->next; + if (af->next) + af->next->prev = af->prev; + else + s->last = af->prev; + + // Uninitialize af and free memory + af->uninit(af); + free(af); } static void print_fmt(struct mp_audio *d) @@ -248,12 +254,11 @@ static void print_fmt(struct mp_audio *d) if (d) { mp_msg(MSGT_AFILTER, MSGL_V, "%dHz/%dch/%s", d->rate, d->nch, af_fmt2str_short(d->format)); - } else { + } else mp_msg(MSGT_AFILTER, MSGL_V, "(?)"); - } } -static void af_print_filter_chain(struct af_stream* s) +static void af_print_filter_chain(struct af_stream *s) { mp_msg(MSGT_AFILTER, MSGL_V, "Audio filter chain:\n"); @@ -280,181 +285,192 @@ static void af_print_filter_chain(struct af_stream* s) // state (for example, format filters that were tentatively inserted stay // inserted). // In that case, you should always rebuild the filter chain, or abort. -int af_reinit(struct af_stream* s, struct af_instance* af) +int af_reinit(struct af_stream *s, struct af_instance *af) { - do{ - struct mp_audio in; // Format of the input to current filter - int rv=0; // Return value - - // Check if there are any filters left in the list - if(NULL == af){ - if(!(af=af_append(s,s->first,"dummy"))) - return AF_UNKNOWN; - else - return AF_ERROR; - } - - // Check if this is the first filter - if(!af->prev) - memcpy(&in,&(s->input),sizeof(struct mp_audio)); - else - memcpy(&in,af->prev->data,sizeof(struct mp_audio)); - // Reset just in case... - in.audio=NULL; - in.len=0; - - rv = af->control(af,AF_CONTROL_REINIT,&in); - switch(rv){ - case AF_OK: - af = af->next; - break; - case AF_FALSE:{ // Configuration filter is needed - // Do auto insertion only if force is not specified - if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ - struct af_instance* 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 AF_ERROR; - // Set number of output channels - if(AF_OK != (rv = new->control(new,AF_CONTROL_CHANNELS,&in.nch))) - return rv; - // Initialize channels filter - if(!new->prev) - memcpy(&in,&(s->input),sizeof(struct mp_audio)); - else - memcpy(&in,new->prev->data,sizeof(struct mp_audio)); - if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) - return rv; - } - // Insert format filter - if((af->prev?af->prev->data->format:s->input.format) != in.format){ - // Create format filter - if(NULL == (new = af_prepend(s,af,"format"))) - return AF_ERROR; - // Set output bits per sample - in.format |= af_bits2fmt(in.bps*8); - if(AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT_FMT,&in.format))) - return rv; - // Initialize format filter - if(!new->prev) - memcpy(&in,&(s->input),sizeof(struct mp_audio)); - else - memcpy(&in,new->prev->data,sizeof(struct mp_audio)); - if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) - return rv; - } - if(!new){ // Should _never_ happen - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Unable to correct audio format. " - "This error should never occur, please send a bug report.\n"); - return AF_ERROR; - } - af=new->next; - } - else { - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Automatic filter insertion disabled " - "but formats do not match. Giving up.\n"); - return AF_ERROR; - } - break; - } - case AF_DETACH:{ // Filter is redundant and wants to be unloaded - // Do auto remove only if force is not specified - if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ - struct af_instance* aft=af->prev; - af_remove(s,af); - if(aft) - af=aft->next; - else - af=s->first; // Restart configuration - } - break; - } - default: - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Reinitialization did not work, audio" - " filter '%s' returned error code %i\n",af->info->name,rv); - return AF_ERROR; - } - }while(af); + do { + struct mp_audio in; // Format of the input to current filter + int rv = 0; // Return value + + // Check if there are any filters left in the list + if (NULL == af) { + if (!(af = af_append(s, s->first, "dummy"))) + return AF_UNKNOWN; + else + return AF_ERROR; + } + + // Check if this is the first filter + if (!af->prev) + memcpy(&in, &(s->input), sizeof(struct mp_audio)); + else + memcpy(&in, af->prev->data, sizeof(struct mp_audio)); + // Reset just in case... + in.audio = NULL; + in.len = 0; + + rv = af->control(af, AF_CONTROL_REINIT, &in); + switch (rv) { + case AF_OK: + af = af->next; + break; + case AF_FALSE: { // Configuration filter is needed + // Do auto insertion only if force is not specified + if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) { + struct af_instance *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 AF_ERROR; + // Set number of output channels + if (AF_OK != + (rv = new->control(new, AF_CONTROL_CHANNELS, &in.nch))) + return rv; + // Initialize channels filter + if (!new->prev) + memcpy(&in, &(s->input), sizeof(struct mp_audio)); + else + memcpy(&in, new->prev->data, sizeof(struct mp_audio)); + if (AF_OK != (rv = new->control(new, AF_CONTROL_REINIT, &in))) + return rv; + } + // Insert format filter + if ((af->prev ? af->prev->data->format : s->input.format) != + in.format) + { + // Create format filter + if (NULL == (new = af_prepend(s, af, "format"))) + return AF_ERROR; + // Set output bits per sample + in.format |= af_bits2fmt(in.bps * 8); + if (AF_OK != + (rv = new->control(new, AF_CONTROL_FORMAT_FMT, &in.format))) + return rv; + // Initialize format filter + if (!new->prev) + memcpy(&in, &(s->input), sizeof(struct mp_audio)); + else + memcpy(&in, new->prev->data, sizeof(struct mp_audio)); + if (AF_OK != (rv = new->control(new, AF_CONTROL_REINIT, &in))) + return rv; + } + if (!new) { // Should _never_ happen + mp_msg( + MSGT_AFILTER, MSGL_ERR, + "[libaf] Unable to correct audio format. " + "This error should never occur, please send a bug report.\n"); + return AF_ERROR; + } + af = new->next; + } else { + mp_msg( + MSGT_AFILTER, MSGL_ERR, + "[libaf] Automatic filter insertion disabled " + "but formats do not match. Giving up.\n"); + return AF_ERROR; + } + break; + } + case AF_DETACH: { // Filter is redundant and wants to be unloaded + // Do auto remove only if force is not specified + if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) { + struct af_instance *aft = af->prev; + af_remove(s, af); + if (aft) + af = aft->next; + else + af = s->first; // Restart configuration + } + break; + } + default: + mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Reinitialization did not " + "work, audio filter '%s' returned error code %i\n", + af->info->name, rv); + return AF_ERROR; + } + } while (af); + + af_print_filter_chain(s); - af_print_filter_chain(s); - - return AF_OK; + return AF_OK; } // Uninit and remove all filters -void af_uninit(struct af_stream* s) +void af_uninit(struct af_stream *s) { - while(s->first) - af_remove(s,s->first); + while (s->first) + af_remove(s, s->first); } /** * Extend the filter chain so we get the required output format at the end. * \return AF_ERROR on error, AF_OK if successful. */ -static int fixup_output_format(struct af_stream* s) +static int fixup_output_format(struct af_stream *s) { - struct af_instance* af = NULL; + struct af_instance *af = NULL; // Check number of output channels fix if not OK // If needed always inserted last -> easy to screw up other filters - if(s->output.nch && s->last->data->nch!=s->output.nch){ - if(!strcmp(s->last->info->name,"format")) - af = af_prepend(s,s->last,"channels"); - else - af = af_append(s,s->last,"channels"); - // Init the new filter - if(!af || (AF_OK != af->control(af,AF_CONTROL_CHANNELS,&(s->output.nch)))) - return AF_ERROR; - if(AF_OK != af_reinit(s,af)) - return AF_ERROR; + if (s->output.nch && s->last->data->nch != s->output.nch) { + if (!strcmp(s->last->info->name, "format")) + af = af_prepend(s, s->last, "channels"); + else + af = af_append(s, s->last, "channels"); + // Init the new filter + if (!af || + (AF_OK != af->control(af, AF_CONTROL_CHANNELS, &(s->output.nch)))) + return AF_ERROR; + if (AF_OK != af_reinit(s, af)) + return AF_ERROR; } // Check output format fix if not OK - if(s->output.format != AF_FORMAT_UNKNOWN && - s->last->data->format != s->output.format){ - if(strcmp(s->last->info->name,"format")) - af = af_append(s,s->last,"format"); - else - af = s->last; - // Init the new filter - s->output.format |= af_bits2fmt(s->output.bps*8); - if(!af || (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT,&(s->output.format)))) - return AF_ERROR; - if(AF_OK != af_reinit(s,af)) - return AF_ERROR; + if (s->output.format != AF_FORMAT_UNKNOWN && + s->last->data->format != s->output.format) { + if (strcmp(s->last->info->name, "format")) + af = af_append(s, s->last, "format"); + else + af = s->last; + // Init the new filter + s->output.format |= af_bits2fmt(s->output.bps * 8); + if (!af || + (AF_OK != af->control(af, AF_CONTROL_FORMAT_FMT, &(s->output.format)))) + return AF_ERROR; + if (AF_OK != af_reinit(s, af)) + return AF_ERROR; } // Re init again just in case - if(AF_OK != af_reinit(s,s->first)) - return AF_ERROR; + if (AF_OK != af_reinit(s, s->first)) + return AF_ERROR; if (s->output.format == AF_FORMAT_UNKNOWN) - s->output.format = s->last->data->format; - if (!s->output.nch) s->output.nch = s->last->data->nch; - if (!s->output.rate) s->output.rate = s->last->data->rate; - if((s->last->data->format != s->output.format) || - (s->last->data->nch != s->output.nch) || - (s->last->data->rate != s->output.rate)) { - return AF_ERROR; - } + s->output.format = s->last->data->format; + if (!s->output.nch) + s->output.nch = s->last->data->nch; + if (!s->output.rate) + s->output.rate = s->last->data->rate; + if ((s->last->data->format != s->output.format) || + (s->last->data->nch != s->output.nch) || + (s->last->data->rate != s->output.rate)) + return AF_ERROR; return AF_OK; } /** * Automatic downmix to stereo in case the codec does not implement it. */ -static void af_downmix(struct af_stream* s) +static void af_downmix(struct af_stream *s) { - static const char * const downmix_strs[AF_NCH + 1] = { + static const char *const downmix_strs[AF_NCH + 1] = { /* FL FR RL RR FC LF AL AR */ [3] = "pan=2:" "0.6:0:" "0:0.6:" "0.4:0.4", [4] = "pan=2:" "0.6:0:" "0:0.6:" "0.4:0:" "0:0.4", [5] = "pan=2:" "0.5:0:" "0:0.5:" "0.2:0:" "0:0.2:" "0.3:0.3", [6] = "pan=2:" "0.4:0:" "0:0.4:" "0.2:0:" "0:0.2:" "0.3:0.3:" "0.1:0.1", - [7] = "pan=2:" "0.4:0:" "0:0.4:" "0.2:0:" "0:0.2:" "0.3:0.3:" "0.1:0:" "0:0.1", - [8] = "pan=2:" "0.4:0:" "0:0.4:" "0.15:0:" "0:0.15:" "0.25:0.25:" "0.1:0.1:" "0.1:0:" "0:0.1", + [7] = "pan=2:" "0.4:0:" "0:0.4:" "0.2:0:" "0:0.2:" "0.3:0.3:" "0.1:0:" "0:0.1", + [8] = "pan=2:" "0.4:0:" "0:0.4:" "0.15:0:" "0:0.15:" "0.25:0.25:" "0.1:0.1:" "0.1:0:" "0:0.1", }; const char *af_pan_str = downmix_strs[s->input.nch]; @@ -471,221 +487,236 @@ static void af_downmix(struct af_stream* s) If one of the prefered output parameters is 0 the one that needs no conversion is used (i.e. the output format in the last filter). The return value is 0 if success and -1 if failure */ -int af_init(struct af_stream* s) +int af_init(struct af_stream *s) { struct MPOpts *opts = s->opts; - int i=0; - - // Sanity check - if(!s) return -1; - - // Precaution in case caller is misbehaving - s->input.audio = s->output.audio = NULL; - s->input.len = s->output.len = 0; - - // Figure out how fast the machine is - if(AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force)) - s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE; - - // Check if this is the first call - if(!s->first){ - // Append a downmix pan filter at the beginning of the chain if needed - if (s->input.nch != opts->audio_output_channels - && opts->audio_output_channels == 2) - af_downmix(s); - // Add all filters in the list (if there are any) - if (s->cfg.list) { - while(s->cfg.list[i]){ - if(!af_append(s,s->last,s->cfg.list[i++])) - return -1; - } + int i = 0; + + // Sanity check + if (!s) + return -1; + + // Precaution in case caller is misbehaving + s->input.audio = s->output.audio = NULL; + s->input.len = s->output.len = 0; + + // Figure out how fast the machine is + if (AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force)) + s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE; + + // Check if this is the first call + if (!s->first) { + // Append a downmix pan filter at the beginning of the chain if needed + if (s->input.nch != opts->audio_output_channels + && opts->audio_output_channels == 2) + af_downmix(s); + // Add all filters in the list (if there are any) + if (s->cfg.list) { + while (s->cfg.list[i]) { + if (!af_append(s, s->last, s->cfg.list[i++])) + return -1; + } + } } - } - - // If we do not have any filters otherwise - // add dummy to make automatic format conversion work - if (!s->first && !af_append(s, s->first, "dummy")) - return -1; - - // Init filters - if(AF_OK != af_reinit(s,s->first)) - return -1; - - // make sure the chain is not empty and valid (e.g. because of AF_DETACH) - if (!s->first) - if (!af_append(s,s->first,"dummy") || AF_OK != af_reinit(s,s->first)) - return -1; - - // Check output format - if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ - struct af_instance* af = NULL; // New filter - // Check output frequency if not OK fix with resample - if(s->output.rate && s->last->data->rate!=s->output.rate){ - // try to find a filter that can change samplrate - af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, - &(s->output.rate)); - if (!af) { - 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); - else - af = af_prepend(s,s->first,resampler); - } - else{ - if(!strcmp(s->last->info->name,"format")) - af = af_prepend(s,s->last,resampler); - else - af = af_append(s,s->last,resampler); - } - // Init the new filter - if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, - &(s->output.rate)))) - return -1; - } - if(AF_OK != af_reinit(s,af)) - return -1; + + // If we do not have any filters otherwise + // add dummy to make automatic format conversion work + if (!s->first && !af_append(s, s->first, "dummy")) + return -1; + + // Init filters + if (AF_OK != af_reinit(s, s->first)) + return -1; + + // make sure the chain is not empty and valid (e.g. because of AF_DETACH) + if (!s->first) { + if (!af_append(s, s->first, "dummy") || AF_OK != af_reinit(s, s->first)) + return -1; } - if (AF_OK != fixup_output_format(s)) { - // Something is stuffed audio out will not work - mp_msg(MSGT_AFILTER, MSGL_ERR, "[libaf] Unable to setup filter system can not" - " meet sound-card demands, please send a bug report. \n"); - af_uninit(s); - return -1; + + // Check output format + if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) { + struct af_instance *af = NULL; // New filter + // Check output frequency if not OK fix with resample + if (s->output.rate && s->last->data->rate != s->output.rate) { + // try to find a filter that can change samplrate + af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, + &(s->output.rate)); + if (!af) { + 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); + else + af = af_prepend(s, s->first, resampler); + } else { + if (!strcmp(s->last->info->name, "format")) + af = af_prepend(s, s->last, resampler); + else + af = af_append(s, s->last, resampler); + } + // Init the new filter + if (!af) + return -1; + if (af->control(af, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, + &(s->output.rate)) != AF_OK) + return -1; + } + if (AF_OK != af_reinit(s, af)) + return -1; + } + if (AF_OK != fixup_output_format(s)) { + // Something is stuffed audio out will not work + mp_msg( + MSGT_AFILTER, MSGL_ERR, + "[libaf] Unable to setup filter system can not" + " meet sound-card demands, please send a bug report. \n"); + af_uninit(s); + return -1; + } } - } - return 0; + return 0; } /* Add filter during execution. This function adds the filter "name" to the stream s. The filter will be inserted somewhere nice in the list of filters. The return value is a pointer to the new filter, If the filter couldn't be added the return value is NULL. */ -struct af_instance* af_add(struct af_stream* s, char* name){ - struct af_instance* new; - // Sanity check - if(!s || !s->first || !name) - return NULL; - // Insert the filter somewhere nice - if(!strcmp(s->first->info->name,"format")) - new = af_append(s, s->first, name); - else - new = af_prepend(s, s->first, name); - if(!new) - return NULL; - - // Reinitalize the filter list - if(AF_OK != af_reinit(s, s->first) || - AF_OK != fixup_output_format(s)){ - while (s->first) - af_remove(s, s->first); - af_init(s); - return NULL; - } - return new; +struct af_instance *af_add(struct af_stream *s, char *name) +{ + struct af_instance *new; + // Sanity check + if (!s || !s->first || !name) + return NULL; + // Insert the filter somewhere nice + if (!strcmp(s->first->info->name, "format")) + new = af_append(s, s->first, name); + else + new = af_prepend(s, s->first, name); + if (!new) + return NULL; + + // Reinitalize the filter list + if (AF_OK != af_reinit(s, s->first) || + AF_OK != fixup_output_format(s)) { + while (s->first) + af_remove(s, s->first); + af_init(s); + return NULL; + } + return new; } // Filter data chunk through the filters in the list -struct mp_audio* af_play(struct af_stream* s, struct mp_audio* data) +struct mp_audio *af_play(struct af_stream *s, struct mp_audio *data) { - struct af_instance* af=s->first; - // Iterate through all filters - do{ - if (data->len <= 0) break; - data=af->play(af,data); - af=af->next; - }while(af && data); - return data; + struct af_instance *af = s->first; + // Iterate through all filters + do { + if (data->len <= 0) + break; + data = af->play(af, data); + af = af->next; + } while (af && data); + return data; } /* Calculate the minimum output buffer size for given input data d * when using the RESIZE_LOCAL_BUFFER macro. The +t+1 part ensures the * value is >= len*mul rounded upwards to whole samples even if the * double 'mul' is inexact. */ -int af_lencalc(double mul, struct mp_audio* d) +int af_lencalc(double mul, struct mp_audio *d) { - int t = d->bps * d->nch; - return d->len * mul + t + 1; + int t = d->bps * d->nch; + return d->len * mul + t + 1; } // Calculate average ratio of filter output size to input size -double af_calc_filter_multiplier(struct af_stream* s) +double af_calc_filter_multiplier(struct af_stream *s) { - struct af_instance* af=s->first; - double mul = 1; - // Iterate through all filters and calculate total multiplication factor - do{ - mul *= af->mul; - af=af->next; - }while(af); - - return mul; + struct af_instance *af = s->first; + double mul = 1; + // Iterate through all filters and calculate total multiplication factor + do { + mul *= af->mul; + af = af->next; + } while (af); + + return mul; } /* Calculate the total delay [bytes output] caused by the filters */ -double af_calc_delay(struct af_stream* s) +double af_calc_delay(struct af_stream *s) { - struct af_instance* af=s->first; - register double delay = 0.0; - // Iterate through all filters - while(af){ - delay += af->delay; - delay *= af->mul; - af=af->next; - } - return delay; + struct af_instance *af = s->first; + register double delay = 0.0; + // Iterate through all filters + while (af) { + delay += af->delay; + delay *= af->mul; + af = af->next; + } + return delay; } /* Helper function called by the macro with the same name this function should not be called directly */ -int af_resize_local_buffer(struct af_instance* af, struct mp_audio* data) +int af_resize_local_buffer(struct af_instance *af, struct mp_audio *data) { - // Calculate new length - register int len = af_lencalc(af->mul,data); - mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, " - "old len = %i, new len = %i\n",af->info->name,af->data->len,len); - // If there is a buffer free it - free(af->data->audio); - // Create new buffer and check that it is OK - af->data->audio = malloc(len); - if(!af->data->audio){ - mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n"); - return AF_ERROR; - } - af->data->len=len; - return AF_OK; + // Calculate new length + register int len = af_lencalc(af->mul, data); + mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, " + "old len = %i, new len = %i\n", af->info->name, af->data->len, len); + // If there is a buffer free it + free(af->data->audio); + // Create new buffer and check that it is OK + af->data->audio = malloc(len); + if (!af->data->audio) { + mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n"); + return AF_ERROR; + } + af->data->len = len; + return AF_OK; } // documentation in af.h -struct af_instance *af_control_any_rev (struct af_stream* s, int cmd, void* arg) { - int res = AF_UNKNOWN; - struct af_instance* filt = s->last; - while (filt) { - res = filt->control(filt, cmd, arg); - if (res == AF_OK) - return filt; - filt = filt->prev; - } - return NULL; +struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg) +{ + int res = AF_UNKNOWN; + struct af_instance *filt = s->last; + while (filt) { + res = filt->control(filt, cmd, arg); + if (res == AF_OK) + return filt; + filt = filt->prev; + } + return NULL; } -void af_help (void) { - int i = 0; - mp_msg(MSGT_AFILTER, MSGL_INFO, "Available audio filters:\n"); - while (filter_list[i]) { - if (filter_list[i]->comment && filter_list[i]->comment[0]) - mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s (%s)\n", filter_list[i]->name, filter_list[i]->info, filter_list[i]->comment); - else - mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s\n", filter_list[i]->name, filter_list[i]->info); - i++; - } +void af_help(void) +{ + int i = 0; + mp_msg(MSGT_AFILTER, MSGL_INFO, "Available audio filters:\n"); + while (filter_list[i]) { + if (filter_list[i]->comment && filter_list[i]->comment[0]) { + mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s (%s)\n", + filter_list[i]->name, filter_list[i]->info, + filter_list[i]->comment); + } else { + mp_msg(MSGT_AFILTER, MSGL_INFO, " %-15s: %s\n", + filter_list[i]->name, + filter_list[i]->info); + } + i++; + } } void af_fix_parameters(struct mp_audio *data) { if (data->nch < 0 || data->nch > AF_NCH) { - mp_msg(MSGT_AFILTER, MSGL_ERR, "Invalid number of channels %i, assuming 2.\n", data->nch); - data->nch = 2; + mp_msg(MSGT_AFILTER, MSGL_ERR, + "Invalid number of channels %i, assuming 2.\n", data->nch); + data->nch = 2; } - data->bps = af_fmt2bits(data->format)/8; + data->bps = af_fmt2bits(data->format) / 8; } diff --git a/audio/filter/af.h b/audio/filter/af.h index 31abe1edee..71892aa144 100644 --- a/audio/filter/af.h +++ b/audio/filter/af.h @@ -321,8 +321,7 @@ void af_fix_parameters(struct mp_audio *data); * \ingroup af_filter */ #define RESIZE_LOCAL_BUFFER(a, d) \ - ((a->data->len < \ - af_lencalc(a->mul, d)) ? af_resize_local_buffer(a, d) : AF_OK) + ((a->data->len < af_lencalc(a->mul, d)) ? af_resize_local_buffer(a, d) : AF_OK) /* Some other useful macro definitions*/ #ifndef min -- cgit v1.2.3 From fc24ab9298ff155ad94171c1b8f16f4da422376c Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 21 Mar 2013 00:58:05 +0100 Subject: audio/filter: replace pointless memcpys with assignments The change in af_scaletempo actually fixes a memory leak. af->data contained a pointer to an allocated buffer, which was overwritten during format negotiation. Set the format explicitly instead. --- audio/filter/af.c | 16 +++------------- audio/filter/af_dummy.c | 4 ++-- audio/filter/af_scaletempo.c | 5 ++++- audio/filter/af_tools.c | 2 +- 4 files changed, 10 insertions(+), 17 deletions(-) (limited to 'audio') diff --git a/audio/filter/af.c b/audio/filter/af.c index e240c83a40..9b00bf0fa5 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -288,7 +288,6 @@ static void af_print_filter_chain(struct af_stream *s) int af_reinit(struct af_stream *s, struct af_instance *af) { do { - struct mp_audio in; // Format of the input to current filter int rv = 0; // Return value // Check if there are any filters left in the list @@ -300,10 +299,7 @@ int af_reinit(struct af_stream *s, struct af_instance *af) } // Check if this is the first filter - if (!af->prev) - memcpy(&in, &(s->input), sizeof(struct mp_audio)); - else - memcpy(&in, af->prev->data, sizeof(struct mp_audio)); + struct mp_audio in = af->prev ? *(af->prev->data) : s->input; // Reset just in case... in.audio = NULL; in.len = 0; @@ -327,10 +323,7 @@ int af_reinit(struct af_stream *s, struct af_instance *af) (rv = new->control(new, AF_CONTROL_CHANNELS, &in.nch))) return rv; // Initialize channels filter - if (!new->prev) - memcpy(&in, &(s->input), sizeof(struct mp_audio)); - else - memcpy(&in, new->prev->data, sizeof(struct mp_audio)); + in = new->prev ? (*new->prev->data) : s->input; if (AF_OK != (rv = new->control(new, AF_CONTROL_REINIT, &in))) return rv; } @@ -347,10 +340,7 @@ int af_reinit(struct af_stream *s, struct af_instance *af) (rv = new->control(new, AF_CONTROL_FORMAT_FMT, &in.format))) return rv; // Initialize format filter - if (!new->prev) - memcpy(&in, &(s->input), sizeof(struct mp_audio)); - else - memcpy(&in, new->prev->data, sizeof(struct mp_audio)); + in = new->prev ? (*new->prev->data) : s->input; if (AF_OK != (rv = new->control(new, AF_CONTROL_REINIT, &in))) return rv; } diff --git a/audio/filter/af_dummy.c b/audio/filter/af_dummy.c index 29a5b3d4b8..5a54cdd80c 100644 --- a/audio/filter/af_dummy.c +++ b/audio/filter/af_dummy.c @@ -29,8 +29,8 @@ static int control(struct af_instance* af, int cmd, void* arg) { switch(cmd){ - case AF_CONTROL_REINIT: - memcpy(af->data,(struct mp_audio*)arg,sizeof(struct mp_audio)); + case AF_CONTROL_REINIT: ; + *af->data = *(struct mp_audio*)arg; mp_msg(MSGT_AFILTER, MSGL_V, "[dummy] Was reinitialized: %iHz/%ich/%s\n", af->data->rate,af->data->nch,af_fmt2str_short(af->data->format)); return AF_OK; diff --git a/audio/filter/af_scaletempo.c b/audio/filter/af_scaletempo.c index cf326fedfb..657fd7f712 100644 --- a/audio/filter/af_scaletempo.c +++ b/audio/filter/af_scaletempo.c @@ -305,7 +305,10 @@ static int control(struct af_instance* af, int cmd, void* arg) if (s->scale == 1.0) { if (s->speed_tempo && s->speed_pitch) return AF_DETACH; - memcpy(af->data, data, sizeof(struct mp_audio)); + af->data->format = data->format; + af->data->nch = data->nch; + af->data->rate = data->rate; + af->data->bps = data->bps; af->delay = 0; af->mul = 1; return af_test_output(af, data); diff --git a/audio/filter/af_tools.c b/audio/filter/af_tools.c index 0d5dc6c573..22534cda8d 100644 --- a/audio/filter/af_tools.c +++ b/audio/filter/af_tools.c @@ -91,7 +91,7 @@ int af_test_output(struct af_instance* af, struct mp_audio* out) (af->data->bps != out->bps) || (af->data->rate != out->rate) || (af->data->nch != out->nch)){ - memcpy(out,af->data,sizeof(struct mp_audio)); + *out = *af->data; return AF_FALSE; } return AF_OK; -- cgit v1.2.3 From 0a136ece5aa6c3004cc6e7b778f889fb6aa82633 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 23 Mar 2013 13:01:44 +0100 Subject: af_lavrresample: allow other ffmpeg sample formats for input/output The format was locked to s16. Extend it to accept all other ffmpeg sample formats, and even allow different in- and output formats. The generic filter code will still insert af_format on format mismatches, though. --- audio/filter/af_lavrresample.c | 65 +++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 17 deletions(-) (limited to 'audio') diff --git a/audio/filter/af_lavrresample.c b/audio/filter/af_lavrresample.c index 5b26a0dce6..aed60f7078 100644 --- a/audio/filter/af_lavrresample.c +++ b/audio/filter/af_lavrresample.c @@ -50,6 +50,7 @@ #include "core/mp_msg.h" #include "core/subopt-helper.h" #include "audio/filter/af.h" +#include "audio/fmt-conversion.h" struct af_resample_opts { int filter_size; @@ -59,6 +60,9 @@ struct af_resample_opts { int out_rate; int in_rate; + int out_format; + int in_format; + int channels; }; struct af_resample { @@ -90,6 +94,9 @@ static bool needs_lavrctx_reconfigure(struct af_resample *s, { return s->ctx.out_rate != out->rate || s->ctx.in_rate != in->rate || + s->ctx.in_format != in->format || + s->ctx.out_format != out->format || + s->ctx.channels != out->nch || s->ctx.filter_size != s->opts.filter_size || s->ctx.phase_shift != s->opts.phase_shift || s->ctx.linear != s->opts.linear || @@ -108,12 +115,30 @@ static int control(struct af_instance *af, int cmd, void *arg) switch (cmd) { case AF_CONTROL_REINIT: { - if ((out->rate == in->rate) || (out->rate == 0)) + struct mp_audio orig_in = *in; + + if (((out->rate == in->rate) || (out->rate == 0)) && + (out->format == in->format) && + (out->bps == in->bps)) return AF_DETACH; + if (out->rate == 0) + out->rate = in->rate; + + enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); + if (in_samplefmt == AV_SAMPLE_FMT_NONE) { + in->format = AF_FORMAT_FLOAT_NE; + in_samplefmt = af_to_avformat(in->format); + } + enum AVSampleFormat out_samplefmt = af_to_avformat(out->format); + if (out_samplefmt == AV_SAMPLE_FMT_NONE) { + out->format = in->format; + out_samplefmt = in_samplefmt; + } + out->nch = FFMIN(in->nch, AF_NCH); - out->format = AF_FORMAT_S16_NE; - out->bps = 2; + out->bps = af_fmt2bits(out->format) / 8; + in->bps = af_fmt2bits(in->format) / 8; af->mul = (double) out->rate / in->rate; af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1); @@ -123,6 +148,9 @@ static int control(struct af_instance *af, int cmd, void *arg) s->ctx.out_rate = out->rate; s->ctx.in_rate = in->rate; + s->ctx.out_format = out->format; + s->ctx.in_format = in->format; + s->ctx.channels = out->nch; s->ctx.filter_size = s->opts.filter_size; s->ctx.phase_shift = s->opts.phase_shift; s->ctx.linear = s->opts.linear; @@ -136,8 +164,8 @@ static int control(struct af_instance *af, int cmd, void *arg) 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("in_sample_fmt", in_samplefmt); + ctx_opt_set_int("out_sample_fmt", out_samplefmt); ctx_opt_set_int("filter_size", s->ctx.filter_size); ctx_opt_set_int("phase_shift", s->ctx.phase_shift); @@ -152,13 +180,18 @@ static int control(struct af_instance *af, int cmd, void *arg) } } - 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; + return ((in->format == orig_in.format) && + (in->bps == orig_in.bps) && + (in->nch == orig_in.nch)) + ? AF_OK : AF_FALSE; + } + case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET: { + if (af_to_avformat(*(int*)arg) == AV_SAMPLE_FMT_NONE) + return AF_FALSE; + + af->data->format = *(int*)arg; + af->data->bps = af_fmt2bits(af->data->format)/8; + return AF_OK; } case AF_CONTROL_COMMAND_LINE: { s->opts.cutoff = 0.0; @@ -227,10 +260,8 @@ static struct mp_audio *play(struct af_instance *af, struct mp_audio *data) (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; + out->len = out->bps * out_samples * out->nch; + *data = *out; return data; } @@ -244,7 +275,7 @@ static int af_open(struct af_instance *af) af->mul = 1; af->data = talloc_zero(s, struct mp_audio); - af->data->rate = 44100; + af->data->rate = 0; int default_filter_size = 16; s->opts = (struct af_resample_opts) { -- cgit v1.2.3 From 5a958921a738f2cd928f8339872b74a3c299ff0e Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 23 Mar 2013 13:02:59 +0100 Subject: af: remove automatically inserted filters on full reinit Make sure automatically inserted filters are removed on full reinit (they are re-added later if they are really needed). Automatically inserted filters were never explicitly removed, instead, it was expected that redundant conversion filters detach themselves. This didn't work if there were several chained format conversion filters, e.g. s16le->floatle->s16le, which could result from repeated filter insertion and removal. (format filters detach only if input format and output format are the same.) Further, the dummy filter (which exists only because af.c can't handle an empty filter chain for some reason) could introduce bad conversions due to how the format negotiation works. Change the code so that the dummy filter never takes part on format negotiation. (It would be better to fix format negotiation, but that would be much more complicated and would involving fixing all filters.) Simplify af_reinit() and remove the start audio filter parameter. This means format negotiation and filter initialization is run more often, but should be harmless. --- audio/filter/af.c | 69 ++++++++++++++++++++++++++++++++----------------------- audio/filter/af.h | 6 +++-- 2 files changed, 44 insertions(+), 31 deletions(-) (limited to 'audio') diff --git a/audio/filter/af.c b/audio/filter/af.c index 9b00bf0fa5..b7bfbd3145 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "af.h" @@ -249,6 +250,17 @@ void af_remove(struct af_stream *s, struct af_instance *af) free(af); } +static void remove_auto_inserted_filters(struct af_stream *s, bool dummy_only) +{ +repeat: + for (struct af_instance *af = s->first; af; af = af->next) { + if ((af->auto_inserted && !dummy_only) || af->info == &af_info_dummy) { + af_remove(s, af); + goto repeat; + } + } +} + static void print_fmt(struct mp_audio *d) { if (d) { @@ -285,18 +297,15 @@ static void af_print_filter_chain(struct af_stream *s) // state (for example, format filters that were tentatively inserted stay // inserted). // In that case, you should always rebuild the filter chain, or abort. -int af_reinit(struct af_stream *s, struct af_instance *af) +// Also, note that for complete reinit, fixup_output_format() must be called +// after this function. +int af_reinit(struct af_stream *s) { - do { - int rv = 0; // Return value + remove_auto_inserted_filters(s, true); - // Check if there are any filters left in the list - if (NULL == af) { - if (!(af = af_append(s, s->first, "dummy"))) - return AF_UNKNOWN; - else - return AF_ERROR; - } + struct af_instance *af = s->first; + while (af) { + int rv = 0; // Return value // Check if this is the first filter struct mp_audio in = af->prev ? *(af->prev->data) : s->input; @@ -334,6 +343,7 @@ int af_reinit(struct af_stream *s, struct af_instance *af) // Create format filter if (NULL == (new = af_prepend(s, af, "format"))) return AF_ERROR; + new->auto_inserted = true; // Set output bits per sample in.format |= af_bits2fmt(in.bps * 8); if (AF_OK != @@ -379,7 +389,15 @@ int af_reinit(struct af_stream *s, struct af_instance *af) af->info->name, rv); return AF_ERROR; } - } while (af); + } + + // At least one filter must exist in the chain. + if (!s->last) { + af = af_append(s, NULL, "dummy"); + if (!af) + return AF_ERROR; + af->control(af, AF_CONTROL_REINIT, &s->input); + } af_print_filter_chain(s); @@ -411,28 +429,29 @@ static int fixup_output_format(struct af_stream *s) if (!af || (AF_OK != af->control(af, AF_CONTROL_CHANNELS, &(s->output.nch)))) return AF_ERROR; - if (AF_OK != af_reinit(s, af)) + if (AF_OK != af_reinit(s)) return AF_ERROR; } // Check output format fix if not OK if (s->output.format != AF_FORMAT_UNKNOWN && s->last->data->format != s->output.format) { - if (strcmp(s->last->info->name, "format")) + if (strcmp(s->last->info->name, "format")) { af = af_append(s, s->last, "format"); - else + af->auto_inserted = true; + } else af = s->last; // Init the new filter s->output.format |= af_bits2fmt(s->output.bps * 8); if (!af || (AF_OK != af->control(af, AF_CONTROL_FORMAT_FMT, &(s->output.format)))) return AF_ERROR; - if (AF_OK != af_reinit(s, af)) + if (AF_OK != af_reinit(s)) return AF_ERROR; } // Re init again just in case - if (AF_OK != af_reinit(s, s->first)) + if (AF_OK != af_reinit(s)) return AF_ERROR; if (s->output.format == AF_FORMAT_UNKNOWN) @@ -509,21 +528,12 @@ int af_init(struct af_stream *s) } } - // If we do not have any filters otherwise - // add dummy to make automatic format conversion work - if (!s->first && !af_append(s, s->first, "dummy")) - return -1; + remove_auto_inserted_filters(s, false); // Init filters - if (AF_OK != af_reinit(s, s->first)) + if (AF_OK != af_reinit(s)) return -1; - // make sure the chain is not empty and valid (e.g. because of AF_DETACH) - if (!s->first) { - if (!af_append(s, s->first, "dummy") || AF_OK != af_reinit(s, s->first)) - return -1; - } - // Check output format if ((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE) { struct af_instance *af = NULL; // New filter @@ -548,11 +558,12 @@ int af_init(struct af_stream *s) // Init the new filter if (!af) return -1; + af->auto_inserted = true; if (af->control(af, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &(s->output.rate)) != AF_OK) return -1; } - if (AF_OK != af_reinit(s, af)) + if (AF_OK != af_reinit(s)) return -1; } if (AF_OK != fixup_output_format(s)) { @@ -587,7 +598,7 @@ struct af_instance *af_add(struct af_stream *s, char *name) return NULL; // Reinitalize the filter list - if (AF_OK != af_reinit(s, s->first) || + if (AF_OK != af_reinit(s) || AF_OK != fixup_output_format(s)) { while (s->first) af_remove(s, s->first); diff --git a/audio/filter/af.h b/audio/filter/af.h index 71892aa144..e4a329abea 100644 --- a/audio/filter/af.h +++ b/audio/filter/af.h @@ -20,6 +20,7 @@ #define MPLAYER_AF_H #include +#include #include "config.h" @@ -75,6 +76,7 @@ struct af_instance { * corresponding output */ double mul; /* length multiplier: how much does this instance change the length of the buffer. */ + bool auto_inserted; // inserted by af.c, such as conversion filters }; // Initialization flags @@ -161,10 +163,10 @@ void af_uninit(struct af_stream *s); /** * \brief Reinit the filter list from the given filter on downwards - * \param Filter instance to begin the reinit from + * See af.c. * \return AF_OK on success or AF_ERROR on failure */ -int af_reinit(struct af_stream *s, struct af_instance *af); +int af_reinit(struct af_stream *s); /** * \brief This function adds the filter "name" to the stream s. -- cgit v1.2.3 From c866583e1e31e6f648f2346fb9c5394d8d080587 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 23 Mar 2013 13:05:32 +0100 Subject: af: use af_lavrresample for format conversions, if possible Refactor to remove the duplicated format filter insertion code. Allow other format converting filters to be inserted on format mismatches. af_info.test_conversion checks whether conversion between two formats would work with the given filter; do this to avoid having to insert multiple conversion filters at once and such things. (Although this isn't ideal: what if we want to avoid af_format for some conversions? What if we want to split af_format in endian-swapping filters etc.?) Prefer af_lavrresample for conversions that it supports natively, otherwise let af_format handle the full conversion. --- audio/filter/af.c | 133 ++++++++++++++++++++++++++++------------- audio/filter/af.h | 1 + audio/filter/af_format.c | 13 +++- audio/filter/af_lavrresample.c | 9 ++- 4 files changed, 111 insertions(+), 45 deletions(-) (limited to 'audio') diff --git a/audio/filter/af.c b/audio/filter/af.c index b7bfbd3145..2ca8d9a289 100644 --- a/audio/filter/af.c +++ b/audio/filter/af.c @@ -52,7 +52,6 @@ static struct af_info* filter_list[] = { &af_info_dummy, &af_info_delay, &af_info_channels, - &af_info_format, &af_info_volume, &af_info_equalizer, &af_info_pan, @@ -77,6 +76,8 @@ static struct af_info* filter_list[] = { #ifdef CONFIG_LIBBS2B &af_info_bs2b, #endif + // Must come last, because it's the fallback format conversion filter + &af_info_format, NULL }; @@ -292,6 +293,74 @@ static void af_print_filter_chain(struct af_stream *s) mp_msg(MSGT_AFILTER, MSGL_V, "\n"); } +static const char *af_find_conversion_filter(int srcfmt, int dstfmt) +{ + for (int n = 0; filter_list[n]; n++) { + struct af_info *af = filter_list[n]; + if (af->test_conversion && af->test_conversion(srcfmt, dstfmt)) + return af->name; + } + return NULL; +} + +static bool af_is_conversion_filter(struct af_instance *af) +{ + return af && af->info->test_conversion != NULL; +} + +// in is what af can take as input - insert a conversion filter if the actual +// input format doesn't match what af expects. +// If af is NULL, in is the output format of the stream. +// Returns: +// AF_OK: must call af_reinit() or equivalent, format matches +// AF_FALSE: nothing was changed, format matches +// else: error +static int af_fix_format_conversion(struct af_stream *s, + struct af_instance **p_af, + struct mp_audio in) +{ + int rv; + struct af_instance *af = p_af ? *p_af : NULL; + struct mp_audio actual; + if (af) { + actual = af->prev ? *af->prev->data : s->input; + } else { + actual = *s->last->data; + } + if (actual.format == in.format) + return AF_FALSE; + const char *filter = af_find_conversion_filter(actual.