From d4bdd0473d6f43132257c9fb3848d829755167a3 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 5 Nov 2012 17:02:04 +0100 Subject: Rename directories, move files (step 1 of 2) (does not compile) Tis drops the silly lib prefixes, and attempts to organize the tree in a more logical way. Make the top-level directory less cluttered as well. Renames the following directories: libaf -> audio/filter libao2 -> audio/out libvo -> video/out libmpdemux -> demux Split libmpcodecs: vf* -> video/filter vd*, dec_video.* -> video/decode mp_image*, img_format*, ... -> video/ ad*, dec_audio.* -> audio/decode libaf/format.* is moved to audio/ - this is similar to how mp_image.* is located in video/. Move most top-level .c/.h files to core. (talloc.c/.h is left on top- level, because it's external.) Park some of the more annoying files in compat/. Some of these are relicts from the time mplayer used ffmpeg internals. sub/ is not split, because it's too much of a mess (subtitle code is mixed with OSD display and rendering). Maybe the organization of core is not ideal: it mixes playback core (like mplayer.c) and utility helpers (like bstr.c/h). Should the need arise, the playback core will be moved somewhere else, while core contains all helper and common code. --- audio/filter/af.c | 700 +++++++++++++++++++++++++++ audio/filter/af.h | 349 ++++++++++++++ audio/filter/af_bs2b.c | 274 +++++++++++ audio/filter/af_center.c | 129 +++++ audio/filter/af_channels.c | 306 ++++++++++++ audio/filter/af_delay.c | 200 ++++++++ audio/filter/af_dummy.c | 76 +++ audio/filter/af_equalizer.c | 248 ++++++++++ audio/filter/af_export.c | 273 +++++++++++ audio/filter/af_extrastereo.c | 157 +++++++ audio/filter/af_format.c | 519 ++++++++++++++++++++ audio/filter/af_format_alaw.h | 324 +++++++++++++ audio/filter/af_format_ulaw.h | 837 +++++++++++++++++++++++++++++++++ audio/filter/af_hrtf.c | 670 ++++++++++++++++++++++++++ audio/filter/af_hrtf.h | 511 ++++++++++++++++++++ audio/filter/af_karaoke.c | 98 ++++ audio/filter/af_ladspa.c | 915 ++++++++++++++++++++++++++++++++++++ audio/filter/af_lavcac3enc.c | 332 +++++++++++++ audio/filter/af_lavcresample.c | 213 +++++++++ audio/filter/af_pan.c | 210 +++++++++ audio/filter/af_resample.c | 394 ++++++++++++++++ audio/filter/af_resample_template.c | 171 +++++++ audio/filter/af_scaletempo.c | 581 +++++++++++++++++++++++ audio/filter/af_sinesuppress.c | 184 ++++++++ audio/filter/af_sub.c | 188 ++++++++ audio/filter/af_surround.c | 273 +++++++++++ audio/filter/af_sweep.c | 103 ++++ audio/filter/af_tools.c | 110 +++++ audio/filter/af_volnorm.c | 353 ++++++++++++++ audio/filter/af_volume.c | 226 +++++++++ audio/filter/control.h | 257 ++++++++++ audio/filter/dsp.h | 32 ++ audio/filter/equalizer.h | 48 ++ audio/filter/filter.c | 360 ++++++++++++++ audio/filter/filter.h | 75 +++ audio/filter/window.c | 213 +++++++++ audio/filter/window.h | 43 ++ 37 files changed, 10952 insertions(+) create mode 100644 audio/filter/af.c create mode 100644 audio/filter/af.h create mode 100644 audio/filter/af_bs2b.c create mode 100644 audio/filter/af_center.c create mode 100644 audio/filter/af_channels.c create mode 100644 audio/filter/af_delay.c create mode 100644 audio/filter/af_dummy.c create mode 100644 audio/filter/af_equalizer.c create mode 100644 audio/filter/af_export.c create mode 100644 audio/filter/af_extrastereo.c create mode 100644 audio/filter/af_format.c create mode 100644 audio/filter/af_format_alaw.h create mode 100644 audio/filter/af_format_ulaw.h create mode 100644 audio/filter/af_hrtf.c create mode 100644 audio/filter/af_hrtf.h create mode 100644 audio/filter/af_karaoke.c create mode 100644 audio/filter/af_ladspa.c create mode 100644 audio/filter/af_lavcac3enc.c create mode 100644 audio/filter/af_lavcresample.c create mode 100644 audio/filter/af_pan.c create mode 100644 audio/filter/af_resample.c create mode 100644 audio/filter/af_resample_template.c create mode 100644 audio/filter/af_scaletempo.c create mode 100644 audio/filter/af_sinesuppress.c create mode 100644 audio/filter/af_sub.c create mode 100644 audio/filter/af_surround.c create mode 100644 audio/filter/af_sweep.c create mode 100644 audio/filter/af_tools.c create mode 100644 audio/filter/af_volnorm.c create mode 100644 audio/filter/af_volume.c create mode 100644 audio/filter/control.h create mode 100644 audio/filter/dsp.h create mode 100644 audio/filter/equalizer.h create mode 100644 audio/filter/filter.c create mode 100644 audio/filter/filter.h create mode 100644 audio/filter/window.c create mode 100644 audio/filter/window.h (limited to 'audio/filter') diff --git a/audio/filter/af.c b/audio/filter/af.c new file mode 100644 index 0000000000..1f3e446821 --- /dev/null +++ b/audio/filter/af.c @@ -0,0 +1,700 @@ +/* + * 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 "config.h" +#include +#include +#include +#include "osdep/strsep.h" + +#include "af.h" + +// Static list of filters +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; +extern struct af_info af_info_surround; +extern struct af_info af_info_sub; +extern struct af_info af_info_export; +extern struct af_info af_info_volnorm; +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_sweep; +extern struct af_info af_info_hrtf; +extern struct af_info af_info_ladspa; +extern struct af_info af_info_center; +extern struct af_info af_info_sinesuppress; +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_resample, + &af_info_volume, + &af_info_equalizer, + &af_info_pan, + &af_info_surround, + &af_info_sub, +#ifdef HAVE_SYS_MMAN_H + &af_info_export, +#endif + &af_info_volnorm, + &af_info_extrastereo, + &af_info_lavcac3enc, + &af_info_lavcresample, + &af_info_sweep, + &af_info_hrtf, +#ifdef CONFIG_LADSPA + &af_info_ladspa, +#endif + &af_info_center, + &af_info_sinesuppress, + &af_info_karaoke, + &af_info_scaletempo, +#ifdef CONFIG_LIBBS2B + &af_info_bs2b, +#endif + NULL +}; + +// CPU speed +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) +{ + 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=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) +{ + 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 + strsep(&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; + } + +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; +} + +/* 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) +{ + // 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) +{ + // 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) +{ + 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) +{ + if (d) { + mp_msg(MSGT_AFILTER, MSGL_V, "%dHz/%dch/%s", d->rate, d->nch, + af_fmt2str_short(d->format)); + } else { + mp_msg(MSGT_AFILTER, MSGL_V, "(?)"); + } +} + +static void af_print_filter_chain(struct af_stream* s) +{ + mp_msg(MSGT_AFILTER, MSGL_V, "Audio filter chain:\n"); + + mp_msg(MSGT_AFILTER, MSGL_V, " [in] "); + print_fmt(&s->input); + mp_msg(MSGT_AFILTER, MSGL_V, "\n"); + + struct af_instance *af = s->first; + while (af) { + mp_msg(MSGT_AFILTER, MSGL_V, " [%s] ", af->info->name); + print_fmt(af->data); + mp_msg(MSGT_AFILTER, MSGL_V, "\n"); + + af = af->next; + } + + mp_msg(MSGT_AFILTER, MSGL_V, " [out] "); + print_fmt(&s->output); + mp_msg(MSGT_AFILTER, MSGL_V, "\n"); +} + +// Warning: +// A failed af_reinit() leaves the audio chain behind in a useless, broken +// 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) +{ + 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); + + return AF_OK; +} + +// Uninit and remove all filters +void af_uninit(struct af_stream* s) +{ + 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) +{ + 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; + } + + // 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; + } + + // Re init again just in case + 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; + } + return AF_OK; +} + +/** + * Automatic downmix to stereo in case the codec does not implement it. + */ +static void af_downmix(struct af_stream* s) +{ + 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", + }; + const char *af_pan_str = downmix_strs[s->input.nch]; + + if (af_pan_str) + af_append(s, s->first, af_pan_str); +} + +/* Initialize the stream "s". This function creates a new filter list + if necessary according to the values set in input and output. Input + and output should contain the format of the current movie and the + formate of the preferred output respectively. The function is + reentrant i.e. if called with an already initialized stream the + stream will be reinitialized. + 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) +{ + 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; + } + } + } + + // 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 = "resample"; + if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW) + resampler = "lavcresample"; + 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; + // 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; + } + 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; +} + +/* 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; +} + +// Filter data chunk through the filters in the list +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; +} + +/* 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 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) +{ + 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) +{ + 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) +{ + // 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; +} + +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; + } + data->bps = af_fmt2bits(data->format)/8; +} diff --git a/audio/filter/af.h b/audio/filter/af.h new file mode 100644 index 0000000000..edce49a978 --- /dev/null +++ b/audio/filter/af.h @@ -0,0 +1,349 @@ +/* + * 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_AF_H +#define MPLAYER_AF_H + +#include + +#include "config.h" + +#include "options.h" +#include "libaf/format.h" +#include "control.h" +#include "cpudetect.h" +#include "mp_msg.h" + +struct af_instance; + +// Number of channels +#ifndef AF_NCH +#define AF_NCH 8 +#endif + +// Audio data chunk +struct mp_audio { + void *audio; // data buffer + int len; // buffer length + int rate; // sample rate + int nch; // number of channels + int format; // format + int bps; // bytes per sample +}; + + +// Flags used for defining the behavior of an audio filter +#define AF_FLAGS_REENTRANT 0x00000000 +#define AF_FLAGS_NOT_REENTRANT 0x00000001 + +/* Audio filter information not specific for current instance, but for + a specific filter */ +struct af_info { + const char *info; + const char *name; + const char *author; + const char *comment; + const int flags; + int (*open)(struct af_instance *vf); +}; + +// Linked list of audio filters +struct af_instance { + struct af_info *info; + int (*control)(struct af_instance *af, int cmd, void *arg); + void (*uninit)(struct af_instance *af); + struct mp_audio * (*play)(struct af_instance *af, struct mp_audio *data); + void *setup; // setup data for this specific instance and filter + struct mp_audio *data; // configuration for outgoing data stream + struct af_instance *next; + struct af_instance *prev; + double delay; /* Delay caused by the filter, in units of bytes read without + * corresponding output */ + double mul; /* length multiplier: how much does this instance change + the length of the buffer. */ +}; + +// Initialization flags +extern int *af_cpu_speed; + +#define AF_INIT_AUTO 0x00000000 +#define AF_INIT_SLOW 0x00000001 +#define AF_INIT_FAST 0x00000002 +#define AF_INIT_FORCE 0x00000003 +#define AF_INIT_TYPE_MASK 0x00000003 + +#define AF_INIT_INT 0x00000000 +#define AF_INIT_FLOAT 0x00000004 +#define AF_INIT_FORMAT_MASK 0x00000004 + +// Default init type +#ifndef AF_INIT_TYPE +#define AF_INIT_TYPE (af_cpu_speed ? *af_cpu_speed : AF_INIT_SLOW) +#endif + +// Configuration switches +struct af_cfg { + int force; // Initialization type + char **list; /* list of names of filters that are added to filter + list during first initialization of stream */ +}; + +// Current audio stream +struct af_stream { + // The first and last filter in the list + struct af_instance *first; + struct af_instance *last; + // Storage for input and output data formats + struct mp_audio input; + struct mp_audio output; + // Configuration for this stream + struct af_cfg cfg; + struct MPOpts *opts; +}; + +/********************************************* + // Return values + */ + +#define AF_DETACH 2 +#define AF_OK 1 +#define AF_TRUE 1 +#define AF_FALSE 0 +#define AF_UNKNOWN -1 +#define AF_ERROR -2 +#define AF_FATAL -3 + + + +/********************************************* + // Export functions + */ + +/** + * \defgroup af_chain Audio filter chain functions + * \{ + * \param s filter chain + */ + +/** + * \brief Initialize the stream "s". + * \return 0 on success, -1 on failure + * + * This function creates a new filter list if necessary, according + * to the values set in input and output. Input and output should contain + * the format of the current movie and the format of the preferred output + * respectively. + * Filters to convert to the preferred output format are inserted + * automatically, except when they are set to 0. + * The function is reentrant i.e. if called with an already initialized + * stream the stream will be reinitialized. + */ +int af_init(struct af_stream *s); + +/** + * \brief Uninit and remove all filters from audio filter chain + */ +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 + * \return AF_OK on success or AF_ERROR on failure + */ +int af_reinit(struct af_stream *s, struct af_instance *af); + +/** + * \brief This function adds the filter "name" to the stream s. + * \param name name of filter to add + * \return pointer to the new filter, NULL if insert failed + * + * The filter will be inserted somewhere nice in the + * list of filters (i.e. at the beginning unless the + * first filter is the format filter (why??). + */ +struct af_instance *af_add(struct af_stream *s, char *name); + +/** + * \brief Uninit and remove the filter "af" + * \param af filter to remove + */ +void af_remove(struct af_stream *s, struct af_instance *af); + +/** + * \brief find filter in chain by name + * \param name name of the filter to find + * \return first filter with right name or NULL if not found + * + * This function is used for finding already initialized filters + */ +struct af_instance *af_get(struct af_stream *s, char *name); + +/** + * \brief filter data chunk through the filters in the list + * \param data data to play + * \return resulting data + * \ingroup af_chain + */ +struct mp_audio *af_play(struct af_stream *s, struct mp_audio *data); + +/** + * \brief send control to all filters, starting with the last until + * one accepts the command with AF_OK. + * \param cmd filter control command + * \param arg argument for filter command + * \return the accepting filter or NULL if none was found + */ +struct af_instance *af_control_any_rev(struct af_stream *s, int cmd, void *arg); + +/** + * \brief calculate average ratio of filter output lenth to input length + * \return the ratio + */ +double af_calc_filter_multiplier(struct af_stream *s); + +/** + * \brief Calculate the total delay caused by the filters + * \return delay in bytes of "missing" output + */ +double af_calc_delay(struct af_stream *s); + +/** \} */ // end of af_chain group + +// Helper functions and macros used inside the audio filters + +/** + * \defgroup af_filter Audio filter helper functions + * \{ + */ + +/* Helper function called by the macro with the same name only to be + called from inside filters */ +int af_resize_local_buffer(struct af_instance *af, struct mp_audio *data); + +/* Helper function used to calculate the exact buffer length needed + when buffers are resized. The returned length is >= than what is + needed */ +int af_lencalc(double mul, struct mp_audio *data); + +/** + * \brief convert dB to gain value + * \param n number of values to convert + * \param in [in] values in dB, <= -200 will become 0 gain + * \param out [out] gain values + * \param k input values are divided by this + * \param mi minimum dB value, input will be clamped to this + * \param ma maximum dB value, input will be clamped to this + * \return AF_ERROR on error, AF_OK otherwise + */ +int af_from_dB(int n, float *in, float *out, float k, float mi, float ma); + +/** + * \brief convert gain value to dB + * \param n number of values to convert + * \param in [in] gain values, 0 wil become -200 dB + * \param out [out] values in dB + * \param k output values will be multiplied by this + * \return AF_ERROR on error, AF_OK otherwise + */ +int af_to_dB(int n, float *in, float *out, float k); + +/** + * \brief convert milliseconds to sample time + * \param n number of values to convert + * \param in [in] values in milliseconds + * \param out [out] sample time values + * \param rate sample rate + * \param mi minimum ms value, input will be clamped to this + * \param ma maximum ms value, input will be clamped to this + * \return AF_ERROR on error, AF_OK otherwise + */ +int af_from_ms(int n, float *in, int *out, int rate, float mi, float ma); + +/** + * \brief convert sample time to milliseconds + * \param n number of values to convert + * \param in [in] sample time values + * \param out [out] values in milliseconds + * \param rate sample rate + * \return AF_ERROR on error, AF_OK otherwise + */ +int af_to_ms(int n, int *in, float *out, int rate); + +/** + * \brief test if output format matches + * \param af audio filter + * \param out needed format, will be overwritten by available + * format if they do not match + * \return AF_FALSE if formats do not match, AF_OK if they match + * + * compares the format, bps, rate and nch values of af->data with out + */ +int af_test_output(struct af_instance *af, struct mp_audio *out); + +/** + * \brief soft clipping function using sin() + * \param a input value + * \return clipped value + */ +float af_softclip(float a); + +/** \} */ // end of af_filter group, but more functions of this group below + +/** Print a list of all available audio filters */ +void af_help(void); + +/** + * \brief fill the missing parameters in the struct mp_audio structure + * \param data structure to fill + * \ingroup af_filter + * + * Currently only sets bps based on format + */ +void af_fix_parameters(struct mp_audio *data); + +/** Memory reallocation macro: if a local buffer is used (i.e. if the + filter doesn't operate on the incoming buffer this macro must be + called to ensure the buffer is big enough. + * \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) + +/* Some other useful macro definitions*/ +#ifndef min +#define min(a, b)(((a) > (b)) ? (b) : (a)) +#endif + +#ifndef max +#define max(a, b)(((a) > (b)) ? (a) : (b)) +#endif + +#ifndef clamp +#define clamp(a, min, max) (((a) > (max)) ? (max) : (((a) < (min)) ? (min) : (a))) +#endif + +#ifndef sign +#define sign(a) (((a) > 0) ? (1) : (-1)) +#endif + +#ifndef lrnd +#define lrnd(a, b) ((b)((a) >= 0.0 ? (a) + 0.5 : (a) - 0.5)) +#endif + +#endif /* MPLAYER_AF_H */ diff --git a/audio/filter/af_bs2b.c b/audio/filter/af_bs2b.c new file mode 100644 index 0000000000..ccbf3794c5 --- /dev/null +++ b/audio/filter/af_bs2b.c @@ -0,0 +1,274 @@ +/* + * The Bauer stereophonic-to-binaural DSP using bs2b library: + * http://bs2b.sourceforge.net/ + * + * Copyright (c) 2009 Andrew Savchenko + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include "af.h" +#include "subopt-helper.h" + +/// Internal specific data of the filter +struct af_bs2b { + int fcut; ///< cut frequency in Hz + int feed; ///< feed level for low frequencies in 0.1*dB + char *profile; ///< profile (available crossfeed presets) + t_bs2bdp filter; ///< instance of a library filter +}; + +#define PLAY(name, type) \ +static struct mp_audio *play_##name(struct af_instance *af, struct mp_audio *data) \ +{ \ + /* filter is called for all pairs of samples available in the buffer */ \ + bs2b_cross_feed_##name(((struct af_bs2b*)(af->setup))->filter, \ + (type*)(data->audio), data->len/data->bps/2); \ +\ + return data; \ +} + +PLAY(f, float) +PLAY(fbe, float) +PLAY(fle, float) +PLAY(s32be, int32_t) +PLAY(u32be, uint32_t) +PLAY(s32le, int32_t) +PLAY(u32le, uint32_t) +PLAY(s24be, bs2b_int24_t) +PLAY(u24be, bs2b_uint24_t) +PLAY(s24le, bs2b_int24_t) +PLAY(u24le, bs2b_uint24_t) +PLAY(s16be, int16_t) +PLAY(u16be, uint16_t) +PLAY(s16le, int16_t) +PLAY(u16le, uint16_t) +PLAY(s8, int8_t) +PLAY(u8, uint8_t) + +/// Sanity check for fcut value +static int test_fcut(void *par) +{ + const int val = *(int*)par; + if (val >= BS2B_MINFCUT && val <= BS2B_MAXFCUT) + return 1; + + mp_msg(MSGT_AFILTER, MSGL_ERR, + "[bs2b] Cut frequency must be in range [%d..%d], but current value is %d.\n", + BS2B_MINFCUT, BS2B_MAXFCUT, val); + return 0; +} + +/// Sanity check for feed value +static int test_feed(void *par) +{ + const int val = *(int*)par; + if (val >= BS2B_MINFEED && val <= BS2B_MAXFEED) + return 1; + + mp_msg(MSGT_AFILTER, MSGL_ERR, + "[bs2b] Feed level must be in range [%d..%d], but current value is %d.\n", + BS2B_MINFEED, BS2B_MAXFEED, val); + return 0; +} + +/// Initialization and runtime control +static int control(struct af_instance *af, int cmd, void *arg) +{ + struct af_bs2b *s = af->setup; + + switch (cmd) { + case AF_CONTROL_REINIT: { + int format; + char buf[256]; + // Sanity check + if (!arg) return AF_ERROR; + + format = ((struct mp_audio*)arg)->format; + af->data->rate = ((struct mp_audio*)arg)->rate; + af->data->nch = 2; // bs2b is useful only for 2ch audio + af->data->bps = ((struct mp_audio*)arg)->bps; + af->data->format = format; + + /* check for formats supported by libbs2b + and assign corresponding handlers */ + switch (format) { + case AF_FORMAT_FLOAT_BE: + af->play = play_fbe; + break; + case AF_FORMAT_FLOAT_LE: + af->play = play_fle; + break; + case AF_FORMAT_S32_BE: + af->play = play_s32be; + break; + case AF_FORMAT_U32_BE: + af->play = play_u32be; + break; + case AF_FORMAT_S32_LE: + af->play = play_s32le; + break; + case AF_FORMAT_U32_LE: + af->play = play_u32le; + break; + case AF_FORMAT_S24_BE: + af->play = play_s24be; + break; + case AF_FORMAT_U24_BE: + af->play = play_u24be; + break; + case AF_FORMAT_S24_LE: + af->play = play_s24le; + break; + case AF_FORMAT_U24_LE: + af->play = play_u24le; + break; + case AF_FORMAT_S16_BE: + af->play = play_s16be; + break; + case AF_FORMAT_U16_BE: + af->play = play_u16be; + break; + case AF_FORMAT_S16_LE: + af->play = play_s16le; + break; + case AF_FORMAT_U16_LE: + af->play = play_u16le; + break; + case AF_FORMAT_S8: + af->play = play_s8; + break; + case AF_FORMAT_U8: + af->play = play_u8; + break; + default: + af->play = play_f; + af->data->format = AF_FORMAT_FLOAT_NE; + af->data->bps = 4; + break; + } + + // bs2b have srate limits, try to resample if needed + if (af->data->rate > BS2B_MAXSRATE || af->data->rate < BS2B_MINSRATE) { + af->data->rate = BS2B_DEFAULT_SRATE; + mp_msg(MSGT_AFILTER, MSGL_WARN, + "[bs2b] Requested sample rate %d Hz is out of bounds [%d..%d] Hz.\n" + "[bs2b] Trying to resample to %d Hz.\n", + af->data->rate, BS2B_MINSRATE, BS2B_MAXSRATE, BS2B_DEFAULT_SRATE); + } + bs2b_set_srate(s->filter, (long)af->data->rate); + mp_msg(MSGT_AFILTER, MSGL_V, "[bs2b] using format %s\n", + af_fmt2str(af->data->format,buf,256)); + + return af_test_output(af,(struct mp_audio*)arg); + } + case AF_CONTROL_COMMAND_LINE: { + const opt_t subopts[] = { + {"fcut", OPT_ARG_INT, &s->fcut, test_fcut}, + {"feed", OPT_ARG_INT, &s->feed, test_feed}, + {"profile", OPT_ARG_MSTRZ, &s->profile, NULL}, + {NULL} + }; + if (subopt_parse(arg, subopts) != 0) { + mp_msg(MSGT_AFILTER, MSGL_ERR, "[bs2b] Invalid option specified.\n"); + free(s->profile); + return AF_ERROR; + } + // parse profile if specified + if (s->profile) { + if (!strcmp(s->profile, "default")) + bs2b_set_level(s->filter, BS2B_DEFAULT_CLEVEL); + else if (!strcmp(s->profile, "cmoy")) + bs2b_set_level(s->filter, BS2B_CMOY_CLEVEL); + else if (!strcmp(s->profile, "jmeier")) + bs2b_set_level(s->filter, BS2B_JMEIER_CLEVEL); + else { + mp_msg(MSGT_AFILTER, MSGL_ERR, + "[bs2b] Invalid profile specified: %s.\n" + "[bs2b] Available profiles are: default, cmoy, jmeier.\n", + s->profile); + free(s->profile); + return AF_ERROR; + } + } + // set fcut and feed only if specified, otherwise defaults will be used + if (s->fcut) + bs2b_set_level_fcut(s->filter, s->fcut); + if (s->feed) + bs2b_set_level_feed(s->filter, s->feed); + + mp_msg(MSGT_AFILTER, MSGL_V, + "[bs2b] using cut frequency %d, LF feed level %d\n", + bs2b_get_level_fcut(s->filter), bs2b_get_level_feed(s->filter)); + free(s->profile); + return AF_OK; + } + } + return AF_UNKNOWN; +} + +/// Deallocate memory and close library +static void uninit(struct af_instance *af) +{ + struct af_bs2b *s = af->setup; + free(af->data); + if (s && s->filter) + bs2b_close(s->filter); + free(s); +} + +/// Allocate memory, set function pointers and init library +static int af_open(struct af_instance *af) +{ + struct af_bs2b *s; + af->control = control; + af->uninit = uninit; + af->mul = 1; + if (!(af->data = calloc(1, sizeof(struct mp_audio)))) + return AF_ERROR; + if (!(af->setup = s = calloc(1, sizeof(struct af_bs2b)))) { + free(af->data); + return AF_ERROR; + } + + // NULL means failed initialization + if (!(s->filter = bs2b_open())) { + free(af->data); + free(af->setup); + return AF_ERROR; + } + // Set zero defaults indicating no option was specified. + s->profile = NULL; + s->fcut = 0; + s->feed = 0; + return AF_OK; +} + +/// Description of this filter +struct af_info af_info_bs2b = { + "Bauer stereophonic-to-binaural audio filter", + "bs2b", + "Andrew Savchenko", + "", + AF_FLAGS_REENTRANT, + af_open +}; diff --git a/audio/filter/af_center.c b/audio/filter/af_center.c new file mode 100644 index 0000000000..aa9aae8514 --- /dev/null +++ b/audio/filter/af_center.c @@ -0,0 +1,129 @@ +/* + * This filter adds a center channel to the audio stream by + * averaging the left and right channel. + * There are two runtime controls one for setting which channel + * to insert the center-audio into called AF_CONTROL_SUB_CH. + * + * FIXME: implement a high-pass filter for better results. + * + * copyright (c) 2005 Alex Beregszaszi + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include "af.h" + +// Data for specific instances of this filter +typedef struct af_center_s +{ + int ch; // Channel number which to insert the filtered data +}af_center_t; + +// Initialization and runtime control +static int control(struct af_instance* af, int cmd, void* arg) +{ + af_center_t* s = af->setup; + + switch(cmd){ + case AF_CONTROL_REINIT:{ + // Sanity check + if(!arg) return AF_ERROR; + + af->data->rate = ((struct mp_audio*)arg)->rate; + af->data->nch = max(s->ch+1,((struct mp_audio*)arg)->nch); + af->data->format = AF_FORMAT_FLOAT_NE; + af->data->bps = 4; + + return af_test_output(af,(struct mp_audio*)arg); + } + case AF_CONTROL_COMMAND_LINE:{ + int ch=1; + sscanf(arg,"%i", &ch); + return control(af,AF_CONTROL_CENTER_CH | AF_CONTROL_SET, &ch); + } + case AF_CONTROL_CENTER_CH | AF_CONTROL_SET: // Requires reinit + // Sanity check + if((*(int*)arg >= AF_NCH) || (*(int*)arg < 0)){ + mp_msg(MSGT_AFILTER, MSGL_ERR, "[sub] Center channel number must be between " + " 0 and %i current value is %i\n", AF_NCH-1, *(int*)arg); + return AF_ERROR; + } + s->ch = *(int*)arg; + return AF_OK; + case AF_CONTROL_CENTER_CH | AF_CONTROL_GET: + *(int*)arg = s->ch; + return AF_OK; + } + return AF_UNKNOWN; +} + +// Deallocate memory +static void uninit(struct af_instance* af) +{ + free(af->data); + free(af->setup); +} + +// Filter data through filter +static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) +{ + struct mp_audio* c = data; // Current working data + af_center_t* s = af->setup; // Setup for this instance + float* a = c->audio; // Audio data + int len = c->len/4; // Number of samples in current audio block + int nch = c->nch; // Number of channels + int ch = s->ch; // Channel in which to insert the center audio + register int i; + + // Run filter + for(i=0;icontrol=control; + af->uninit=uninit; + af->play=play; + af->mul=1; + af->data=calloc(1,sizeof(struct mp_audio)); + af->setup=s=calloc(1,sizeof(af_center_t)); + if(af->data == NULL || af->setup == NULL) + return AF_ERROR; + // Set default values + s->ch = 1; // Channel nr 2 + return AF_OK; +} + +// Description of this filter +struct af_info af_info_center = { + "Audio filter for adding a center channel", + "center", + "Alex Beregszaszi", + "", + AF_FLAGS_NOT_REENTRANT, + af_open +}; diff --git a/audio/filter/af_channels.c b/audio/filter/af_channels.c new file mode 100644 index 0000000000..8f676d8cfd --- /dev/null +++ b/audio/filter/af_channels.c @@ -0,0 +1,306 @@ +/* + * Audio filter that adds and removes channels, according to the + * command line parameter channels. It is stupid and can only add + * silence or copy channels, not mix or filter. + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include "af.h" + +#define FR 0 +#define TO 1 + +typedef struct af_channels_s{ + int route[AF_NCH][2]; + int nr; + int router; +}af_channels_t; + +// Local function for copying data +static void copy(void* in, void* out, int ins, int inos,int outs, int outos, int len, int bps) +{ + switch(bps){ + case 1:{ + int8_t* tin = (int8_t*)in; + int8_t* tout = (int8_t*)out; + tin += inos; + tout += outos; + len = len/ins; + while(len--){ + *tout=*tin; + tin +=ins; + tout+=outs; + } + break; + } + case 2:{ + int16_t* tin = (int16_t*)in; + int16_t* tout = (int16_t*)out; + tin += inos; + tout += outos; + len = len/(2*ins); + while(len--){ + *tout=*tin; + tin +=ins; + tout+=outs; + } + break; + } + case 3:{ + int8_t* tin = (int8_t*)in; + int8_t* tout = (int8_t*)out; + tin += 3 * inos; + tout += 3 * outos; + len = len / ( 3 * ins); + while (len--) { + tout[0] = tin[0]; + tout[1] = tin[1]; + tout[2] = tin[2]; + tin += 3 * ins; + tout += 3 * outs; + } + break; + } + case 4:{ + int32_t* tin = (int32_t*)in; + int32_t* tout = (int32_t*)out; + tin += inos; + tout += outos; + len = len/(4*ins); + while(len--){ + *tout=*tin; + tin +=ins; + tout+=outs; + } + break; + } + case 8:{ + int64_t* tin = (int64_t*)in; + int64_t* tout = (int64_t*)out; + tin += inos; + tout += outos; + len = len/(8*ins); + while(len--){ + *tout=*tin; + tin +=ins; + tout+=outs; + } + break; + } + default: + mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Unsupported number of bytes/sample: %i" + " please report this error on the MPlayer mailing list. \n",bps); + } +} + +// Make sure the routes are sane +static int check_routes(af_channels_t* s, int nin, int nout) +{ + int i; + if((s->nr < 1) || (s->nr > AF_NCH)){ + mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of routing pairs must be" + " between 1 and %i. Current value is %i\n",AF_NCH,s->nr); + return AF_ERROR; + } + + for(i=0;inr;i++){ + if((s->route[i][FR] >= nin) || (s->route[i][TO] >= nout)){ + mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Invalid routing in pair nr. %i.\n", i); + return AF_ERROR; + } + } + return AF_OK; +} + +// Initialization and runtime control +static int control(struct af_instance* af, int cmd, void* arg) +{ + af_channels_t* s = af->setup; + switch(cmd){ + case AF_CONTROL_REINIT: + + // Set default channel assignment + if(!s->router){ + int i; + // Make sure this filter isn't redundant + if(af->data->nch == ((struct mp_audio*)arg)->nch) + return AF_DETACH; + + // If mono: fake stereo + if(((struct mp_audio*)arg)->nch == 1){ + s->nr = min(af->data->nch,2); + for(i=0;inr;i++){ + s->route[i][FR] = 0; + s->route[i][TO] = i; + } + } + else{ + s->nr = min(af->data->nch, ((struct mp_audio*)arg)->nch); + for(i=0;inr;i++){ + s->route[i][FR] = i; + s->route[i][TO] = i; + } + } + } + + af->data->rate = ((struct mp_audio*)arg)->rate; + af->data->format = ((struct mp_audio*)arg)->format; + af->data->bps = ((struct mp_audio*)arg)->bps; + af->mul = (double)af->data->nch / ((struct mp_audio*)arg)->nch; + return check_routes(s,((struct mp_audio*)arg)->nch,af->data->nch); + case AF_CONTROL_COMMAND_LINE:{ + int nch = 0; + int n = 0; + // Check number of channels and number of routing pairs + sscanf(arg, "%i:%i%n", &nch, &s->nr, &n); + + // If router scan commandline for routing pairs + if(s->nr){ + char* cp = &((char*)arg)[n]; + int ch = 0; + // Sanity check + if((s->nr < 1) || (s->nr > AF_NCH)){ + mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of routing pairs must be" + " between 1 and %i. Current value is %i\n",AF_NCH,s->nr); + } + s->router = 1; + // Scan for pairs on commandline + while((*cp == ':') && (ch < s->nr)){ + sscanf(cp, ":%i:%i%n" ,&s->route[ch][FR], &s->route[ch][TO], &n); + mp_msg(MSGT_AFILTER, MSGL_V, "[channels] Routing from channel %i to" + " channel %i\n",s->route[ch][FR],s->route[ch][TO]); + cp = &cp[n]; + ch++; + } + } + + if(AF_OK != af->control(af,AF_CONTROL_CHANNELS | AF_CONTROL_SET ,&nch)) + return AF_ERROR; + return AF_OK; + } + case AF_CONTROL_CHANNELS | AF_CONTROL_SET: + // Reinit must be called after this function has been called + + // Sanity check + if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ + mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] The number of output channels must be" + " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); + return AF_ERROR; + } + + af->data->nch=((int*)arg)[0]; + if(!s->router) + mp_msg(MSGT_AFILTER, MSGL_V, "[channels] Changing number of channels" + " to %i\n",af->data->nch); + return AF_OK; + case AF_CONTROL_CHANNELS | AF_CONTROL_GET: + *(int*)arg = af->data->nch; + return AF_OK; + case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_SET:{ + int ch = ((af_control_ext_t*)arg)->ch; + int* route = ((af_control_ext_t*)arg)->arg; + s->route[ch][FR] = route[FR]; + s->route[ch][TO] = route[TO]; + return AF_OK; + } + case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_GET:{ + int ch = ((af_control_ext_t*)arg)->ch; + int* route = ((af_control_ext_t*)arg)->arg; + route[FR] = s->route[ch][FR]; + route[TO] = s->route[ch][TO]; + return AF_OK; + } + case AF_CONTROL_CHANNELS_NR | AF_CONTROL_SET: + s->nr = *(int*)arg; + return AF_OK; + case AF_CONTROL_CHANNELS_NR | AF_CONTROL_GET: + *(int*)arg = s->nr; + return AF_OK; + case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_SET: + s->router = *(int*)arg; + return AF_OK; + case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_GET: + *(int*)arg = s->router; + return AF_OK; + } + return AF_UNKNOWN; +} + +// Deallocate memory +static void uninit(struct af_instance* af) +{ + free(af->setup); + if (af->data) + free(af->data->audio); + free(af->data); +} + +// Filter data through filter +static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) +{ + struct mp_audio* c = data; // Current working data + struct mp_audio* l = af->data; // Local data + af_channels_t* s = af->setup; + int i; + + if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) + return NULL; + + // Reset unused channels + memset(l->audio,0,c->len / c->nch * l->nch); + + if(AF_OK == check_routes(s,c->nch,l->nch)) + for(i=0;inr;i++) + copy(c->audio,l->audio,c->nch,s->route[i][FR], + l->nch,s->route[i][TO],c->len,c->bps); + + // Set output data + c->audio = l->audio; + c->len = c->len / c->nch * l->nch; + c->nch = l->nch; + + return c; +} + +// Allocate memory and set function pointers +static int af_open(struct af_instance* af){ + af->control=control; + af->uninit=uninit; + af->play=play; + af->mul=1; + af->data=calloc(1,sizeof(struct mp_audio)); + af->setup=calloc(1,sizeof(af_channels_t)); + if((af->data == NULL) || (af->setup == NULL)) + return AF_ERROR; + return AF_OK; +} + +// Description of this filter +struct af_info af_info_channels = { + "Insert or remove channels", + "channels", + "Anders", + "", + AF_FLAGS_REENTRANT, + af_open +}; diff --git a/audio/filter/af_delay.c b/audio/filter/af_delay.c new file mode 100644 index 0000000000..ce8d71980b --- /dev/null +++ b/audio/filter/af_delay.c @@ -0,0 +1,200 @@ +/* + * This audio filter delays the output signal for the different + * channels and can be used for simple position panning. + * An extension for this filter would be a reverb. + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +#include "af.h" + +#define L 65536 + +#define UPDATEQI(qi) qi=(qi+1)&(L-1) + +// Data for specific instances of this filter +typedef struct af_delay_s +{ + void* q[AF_NCH]; // Circular queues used for delaying audio signal + int wi[AF_NCH]; // Write index + int ri; // Read index + float d[AF_NCH]; // Delay [ms] +}af_delay_t; + +// Initialization and runtime control +static int control(struct af_instance* af, int cmd, void* arg) +{ + af_delay_t* s = af->setup; + switch(cmd){ + case AF_CONTROL_REINIT:{ + int i; + + // Free prevous delay queues + for(i=0;idata->nch;i++) + free(s->q[i]); + + af->data->rate = ((struct mp_audio*)arg)->rate; + af->data->nch = ((struct mp_audio*)arg)->nch; + af->data->format = ((struct mp_audio*)arg)->format; + af->data->bps = ((struct mp_audio*)arg)->bps; + + // Allocate new delay queues + for(i=0;idata->nch;i++){ + s->q[i] = calloc(L,af->data->bps); + if(NULL == s->q[i]) + mp_msg(MSGT_AFILTER, MSGL_FATAL, "[delay] Out of memory\n"); + } + + return control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET,s->d); + } + case AF_CONTROL_COMMAND_LINE:{ + int n = 1; + int i = 0; + char* cl = arg; + while(n && i < AF_NCH ){ + sscanf(cl,"%f:%n",&s->d[i],&n); + if(n==0 || cl[n-1] == '\0') + break; + cl=&cl[n]; + i++; + } + return AF_OK; + } + case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{ + int i; + if(AF_OK != af_from_ms(AF_NCH, arg, s->wi, af->data->rate, 0.0, 1000.0)) + return AF_ERROR; + s->ri = 0; + for(i=0;id[i],0.0,1000.0)); + mp_msg(MSGT_AFILTER, MSGL_DBG3, "[delay] Channel %i delayed by %i samples\n", + i,s->wi[i]); + } + return AF_OK; + } + case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET:{ + int i; + for(i=0;iri > s->wi[i]) + s->wi[i] = L - (s->ri - s->wi[i]); + else + s->wi[i] = s->wi[i] - s->ri; + } + return af_to_ms(AF_NCH, s->wi, arg, af->data->rate); + } + } + return AF_UNKNOWN; +} + +// Deallocate memory +static void uninit(struct af_instance* af) +{ + int i; + + free(af->data); + for(i=0;isetup))->q[i]); + free(af->setup); +} + +// Filter data through filter +static struct mp_audio* play(struct af_instance* af, struct mp_audio* data) +{ + struct mp_audio* c = data; // Current working data + af_delay_t* s = af->setup; // Setup for this instance + int nch = c->nch; // Number of channels + int len = c->len/c->bps; // Number of sample in data chunk + int ri = 0; + int ch,i; + for(ch=0;chbps){ + case 1:{ + int8_t* a = c->audio; + int8_t* q = s->q[ch]; + int wi = s->wi[ch]; + ri = s->ri; + for(i=ch;iwi[ch] = wi; + break; + } + case 2:{ + int16_t* a = c->audio; + int16_t* q = s->q[ch]; + int wi = s->wi[ch]; + ri = s->ri; + for(i=ch;iwi[ch] = wi; + break; + } + case 4:{ + int32_t* a = c->audio; + int32_t* q = s->q[ch]; + int wi = s->wi[ch]; + ri = s->ri; + for(i=ch;iwi[ch] = wi; + break; + } + } + } + s->ri = ri; + return c; +} + +// Allocate memory and set function pointers +static int af_open(struct af_instance* af){ + af->control=control; + af->uninit=uninit; + af->play=play; + af->mul=1; + af->data=calloc(1,sizeof(struct mp_audio)); + af->setup=calloc(1,sizeof(af_delay_t)); + if(af->data == NULL || af->setup == NULL) + return AF_ERROR; + return AF_OK; +} + +// Description of this filter +struct af_info af_info_delay = { + "Delay audio filter", + "delay", + "Anders", + "", + AF_FLAGS_REENTRANT, + af_open +}; diff --git a/audio/filter/af_dummy.c b/audio/filter/af_dummy.c new file mode 100644 index 0000000000..29a5b3d4b8 --- /dev/null +++ b/audio/filter/af_dummy.c @@ -0,0 +1,76 @@ +/* + * The name speaks for itself. This filter is a dummy and will + * not blow up regardless of what you do with it. + * + * 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 Licen