Commit 1abc59b7 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

aout: partly rewrite and move filters initialization code

 - simplify "audio-filter" parser and fix a tautology
 - keep a single pointer for the resampler instead of a table
parent 714c9998
......@@ -51,15 +51,6 @@ struct aout_input_t
{
unsigned samplerate; /**< Input sample rate */
/* filters */
filter_t * pp_filters[AOUT_MAX_FILTERS];
unsigned i_nb_filters;
filter_t * p_playback_rate_filter;
/* Resampler + converter to mixer */
filter_t * pp_resamplers[5];
unsigned i_nb_resamplers;
int i_resampling_type;
mtime_t i_resamp_start_date;
int i_resamp_start_drift;
......@@ -69,10 +60,6 @@ struct aout_input_t
/* */
int i_buffer_lost;
/* */
bool b_recycle_vout;
aout_request_vout_t request_vout;
};
typedef struct
......@@ -87,12 +74,20 @@ typedef struct
date_t date;
} sync;
audio_sample_format_t mixer_format;
audio_sample_format_t input_format;
audio_sample_format_t mixer_format;
/* Filters between mixer and output */
filter_t *filters[5];
unsigned nb_filters;
filter_t *rate_filter; /**< The filter adjusting samples count
(either the scaletempo filter or a resampler) */
filter_t *resampler; /**< The resampler */
filter_t *filters[AOUT_MAX_FILTERS]; /**< Configured user filters
(e.g. equalization) and their conversions */
unsigned nb_filters;
unsigned nb_converters;
filter_t *converters[5]; /**< Converters to the output */
aout_request_vout_t request_vout;
bool recycle_vout;
vlc_atomic_t restart;
} aout_owner_t;
......@@ -128,6 +123,10 @@ int aout_FiltersCreatePipeline( vlc_object_t *, filter_t **, unsigned *,
void aout_FiltersDestroyPipeline( filter_t *const *, unsigned );
void aout_FiltersPlay( filter_t *const *, unsigned, block_t ** );
int aout_FiltersNew(audio_output_t *, const audio_sample_format_t *,
const audio_sample_format_t *, const aout_request_vout_t *);
void aout_FiltersDestroy(audio_output_t *);
/* From mixer.c : */
aout_volume_t *aout_volume_New(vlc_object_t *, const audio_replay_gain_t *);
#define aout_volume_New(o, g) aout_volume_New(VLC_OBJECT(o), g)
......
......@@ -138,7 +138,7 @@ static void aout_CheckRestart (audio_output_t *aout)
assert (restart & AOUT_RESTART_INPUT);
const aout_request_vout_t request_vout = owner->input->request_vout;
const aout_request_vout_t request_vout = owner->request_vout;
if (likely(owner->input != NULL))
aout_InputDelete (aout, owner->input);
......
......@@ -28,17 +28,18 @@
# include "config.h"
#endif
#include <string.h>
#include <assert.h>
#include <vlc_common.h>
#include <vlc_dialog.h>
#include <vlc_modules.h>
#include <vlc_aout.h>
#include <vlc_filter.h>
#include <vlc_cpu.h>
#include "aout_internal.h"
#include <vlc_vout.h> /* for vout_Request */
#include <libvlc.h>
#include "aout_internal.h"
/*****************************************************************************
* FindFilter: find an audio filter for a specific transformation
......@@ -211,6 +212,12 @@ void aout_FiltersDestroyPipeline( filter_t *const *filters, unsigned n )
}
}
static inline bool ChangeFiltersString (vlc_object_t *aout, const char *var,
const char *filter, bool add)
{
return aout_ChangeFilterString (aout, aout, var, filter, add);
}
/**
* Filters an audio buffer through a chain of filters.
*/
......@@ -230,3 +237,302 @@ void aout_FiltersPlay( filter_t *const *pp_filters,
}
*pp_block = p_block;
}
/** Callback for visualization selection */
static int VisualizationCallback (vlc_object_t *obj, char const *var,
vlc_value_t oldval, vlc_value_t newval,
void *data)
{
audio_output_t *aout = (audio_output_t *)obj;
const char *mode = newval.psz_string;
if (!*mode)
{
ChangeFiltersString (obj, "audio-visual", "goom", false);
ChangeFiltersString (obj, "audio-visual", "visual", false);
ChangeFiltersString (obj, "audio-visual", "projectm", false);
ChangeFiltersString (obj, "audio-visual", "vsxu", false);
}
else if (!strcmp ("goom", mode))
{
ChangeFiltersString (obj, "audio-visual", "visual", false );
ChangeFiltersString (obj, "audio-visual", "goom", true );
ChangeFiltersString (obj, "audio-visual", "projectm", false );
ChangeFiltersString (obj, "audio-visual", "vsxu", false);
}
else if (!strcmp ("projectm", mode))
{
ChangeFiltersString (obj, "audio-visual", "visual", false);
ChangeFiltersString (obj, "audio-visual", "goom", false);
ChangeFiltersString (obj, "audio-visual", "projectm", true);
ChangeFiltersString (obj, "audio-visual", "vsxu", false);
}
else if (!strcmp ("vsxu", mode))
{
ChangeFiltersString (obj, "audio-visual", "visual", false);
ChangeFiltersString (obj, "audio-visual", "goom", false);
ChangeFiltersString (obj, "audio-visual", "projectm", false);
ChangeFiltersString (obj, "audio-visual", "vsxu", true);
}
else
{
var_Create (obj, "effect-list", VLC_VAR_STRING);
var_SetString (obj, "effect-list", mode);
ChangeFiltersString (obj, "audio-visual", "goom", false);
ChangeFiltersString (obj, "audio-visual", "visual", true);
ChangeFiltersString (obj, "audio-visual", "projectm", false);
}
aout_InputRequestRestart (aout);
(void) var; (void) oldval; (void) data;
return VLC_SUCCESS;
}
static int EqualizerCallback (vlc_object_t *obj, char const *var,
vlc_value_t oldval, vlc_value_t newval,
void *data)
{
audio_output_t *aout = (audio_output_t *)obj;
char *mode = newval.psz_string;
bool ret;
if (!*mode)
ret = ChangeFiltersString (obj, "audio-filter", "equalizer", false);
else
{
var_Create (obj, "equalizer-preset", VLC_VAR_STRING);
var_SetString (obj, "equalizer-preset", mode);
ret = ChangeFiltersString (obj, "audio-filter", "equalizer", true);
}
/* That sucks */
if (ret)
aout_InputRequestRestart (aout);
(void) var; (void) oldval; (void) data;
return VLC_SUCCESS;
}
static vout_thread_t *RequestVout (void *data, vout_thread_t *vout,
video_format_t *fmt, bool recycle)
{
audio_output_t *aout = data;
vout_configuration_t cfg = {
.vout = vout,
.input = NULL,
.change_fmt = true,
.fmt = fmt,
.dpb_size = 1,
};
(void) recycle;
return vout_Request (aout, &cfg);
}
vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
video_format_t *fmt)
{
/* NOTE: This only works from audio output.
* If you want to use visualization filters from another place, you will
* need to add a new pf_aout_request_vout callback or store a pointer
* to aout_request_vout_t inside filter_t (i.e. a level of indirection). */
aout_owner_t *owner = aout_owner ((audio_output_t *)filter->p_parent);
aout_request_vout_t *req = &owner->request_vout;
return req->pf_request_vout (req->p_private, vout, fmt,
owner->recycle_vout);
}
static filter_t *CreateFilter (vlc_object_t *parent, const char *name,
const audio_sample_format_t *restrict infmt,
const audio_sample_format_t *restrict outfmt,
bool visu)
{
filter_t *filter = vlc_custom_create (parent, sizeof (*filter),
"audio filter");
if (unlikely(filter == NULL))
return NULL;
/*filter->p_owner = NOT NEEDED;*/
filter->fmt_in.i_codec = infmt->i_format;
filter->fmt_in.audio = *infmt;
filter->fmt_out.i_codec = outfmt->i_format;
filter->fmt_out.audio = *outfmt;
if (!visu)
{
filter->p_module = module_need (filter, "audio filter", name, true);
if (filter->p_module != NULL)
return filter;
/* If probing failed, formats shall not have been modified. */
assert (AOUT_FMTS_IDENTICAL(&filter->fmt_in.audio, infmt));
assert (AOUT_FMTS_IDENTICAL(&filter->fmt_out.audio, outfmt));
}
filter->p_module = module_need (filter, "visualization2", name, true);
if (filter->p_module != NULL)
return filter;
vlc_object_release (filter);
return NULL;
}
/**
* Sets up the audio filters.
*/
int aout_FiltersNew (audio_output_t *aout,
const audio_sample_format_t *restrict infmt,
const audio_sample_format_t *restrict outfmt,
const aout_request_vout_t *request_vout)
{
aout_owner_t *owner = aout_owner (aout);
/* Prepare format structure */
aout_FormatPrint (aout, "input", infmt);
audio_sample_format_t input_format = *infmt;
audio_sample_format_t output_format = *outfmt;
/* Now add user filters */
owner->nb_filters = 0;
owner->rate_filter = NULL;
owner->resampler = NULL;
if (!AOUT_FMT_LINEAR(outfmt))
return 0;
var_AddCallback (aout, "visual", VisualizationCallback, NULL);
var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
bool scaletempo = var_InheritBool (aout, "audio-time-stretch");
char *filters = var_InheritString (aout, "audio-filter");
char *visual = var_InheritString (aout, "audio-visual");
if (request_vout != NULL)
owner->request_vout = *request_vout;
else
{
owner->request_vout.pf_request_vout = RequestVout;
owner->request_vout.p_private = aout;
}
owner->recycle_vout = (visual != NULL) && *visual;
/* parse user filter lists */
const char *list[AOUT_MAX_FILTERS];
unsigned n = 0;
if (scaletempo)
list[n++] = "scaletempo";
if (filters != NULL)
{
char *p = filters, *name;
while ((name = strsep (&p, " :")) != NULL && n < AOUT_MAX_FILTERS)
list[n++] = name;
}
if (visual != NULL && n < AOUT_MAX_FILTERS)
list[n++] = visual;
for (unsigned i = 0; i < n; i++)
{
const char *name = list[i];
if (owner->nb_filters >= AOUT_MAX_FILTERS)
{
msg_Err (aout, "maximum of %u filters reached", AOUT_MAX_FILTERS);
msg_Err (aout, "cannot add user filter %s (skipped)", name);
break;
}
filter_t *filter = CreateFilter (VLC_OBJECT(aout), name,
&input_format, &output_format,
i == (n - (visual != NULL)));
if (filter == NULL)
{
msg_Err (aout, "cannot add user filter %s (skipped)", name);
continue;
}
/* convert to the filter input format if necessary */
if (aout_FiltersCreatePipeline (VLC_OBJECT(aout), owner->filters,
&owner->nb_filters,
AOUT_MAX_FILTERS - 1,
&input_format, &filter->fmt_in.audio))
{
msg_Err (aout, "cannot add user filter %s (skipped)", name);
module_unneed (filter, filter->p_module);
vlc_object_release (filter);
continue;
}
assert (owner->nb_filters < AOUT_MAX_FILTERS);
owner->filters[owner->nb_filters++] = filter;
input_format = filter->fmt_out.audio;
if (i == 0 && scaletempo) /* scaletempo */
owner->rate_filter = filter;
}
free (visual);
free (filters);
/* convert to the output format (minus resampling) if necessary */
output_format.i_rate = input_format.i_rate;
if (aout_FiltersCreatePipeline (VLC_OBJECT(aout), owner->filters,
&owner->nb_filters, AOUT_MAX_FILTERS,
&input_format, &output_format))
{
msg_Err (aout, "cannot setup filtering pipeline");
goto error;
}
input_format = output_format;
/* insert the resampler */
output_format.i_rate = outfmt->i_rate;
assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
unsigned rate_bak = input_format.i_rate;
if (output_format.i_rate == input_format.i_rate)
/* For historical reasons, a different rate is required to probe
* resampling filters. */
input_format.i_rate++;
owner->resampler = FindFilter (VLC_OBJECT(aout), &input_format,
&output_format);
if (owner->resampler == NULL)
{
msg_Err (aout, "cannot setup a resampler");
goto error;
}
owner->resampler->fmt_in.audio.i_rate = rate_bak;
if (owner->rate_filter == NULL)
owner->rate_filter = owner->resampler;
return 0;
error:
aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
var_DelCallback (aout, "visual", VisualizationCallback, NULL);
return -1;
}
/**
* Destroys the audio filters.
*/
void aout_FiltersDestroy (audio_output_t *aout)
{
aout_owner_t *owner = aout_owner (aout);
if (owner->resampler != NULL)
aout_FiltersDestroyPipeline (&owner->resampler, 1);
aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
var_DelCallback (aout, "visual", VisualizationCallback, NULL);
/* XXX We need to update recycle_vout before calling
* aout_FiltersDestroyPipeline().
* FIXME There may be a race condition if audio-visual is updated between
* aout_FiltersDestroy() and the next aout_FiltersNew().
*/
char *visual = var_InheritString (aout, "audio-visual");
owner->recycle_vout = (visual != NULL) && *visual;
free (visual);
}
......@@ -30,30 +30,15 @@
#endif
#include <vlc_common.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <vlc_input.h>
#include <vlc_vout.h> /* for vout_Request */
#include <vlc_modules.h>
#include <vlc_aout.h>
#include <vlc_filter.h>
#include <libvlc.h>
#include <libvlc.h>
#include "aout_internal.h"
static void inputDrop( aout_input_t *, block_t * );
static void inputResamplingStop( aout_input_t *p_input );
static int VisualizationCallback( vlc_object_t *, char const *,
vlc_value_t, vlc_value_t, void * );
static int EqualizerCallback( vlc_object_t *, char const *,
vlc_value_t, vlc_value_t, void * );
static vout_thread_t *RequestVout( void *,
vout_thread_t *, video_format_t *, bool );
static void inputResamplingStop( audio_output_t *, aout_input_t * );
/*****************************************************************************
* aout_InputNew : allocate a new input and rework the filter pipeline
......@@ -67,232 +52,18 @@ aout_input_t *aout_InputNew (audio_output_t * p_aout,
if (unlikely(p_input == NULL))
return NULL;
aout_FormatPrint( p_aout, "input", infmt );
p_input->samplerate = infmt->i_rate;
p_input->i_nb_resamplers = p_input->i_nb_filters = 0;
/* */
if( p_request_vout )
{
p_input->request_vout = *p_request_vout;
}
else
{
p_input->request_vout.pf_request_vout = RequestVout;
p_input->request_vout.p_private = p_aout;
}
/* Prepare format structure */
audio_sample_format_t chain_input_format = *infmt;
audio_sample_format_t chain_output_format = *outfmt;
chain_output_format.i_rate = infmt->i_rate;
aout_FormatPrepare( &chain_output_format );
/* Now add user filters */
var_AddCallback( p_aout, "visual", VisualizationCallback, p_input );
var_AddCallback( p_aout, "equalizer", EqualizerCallback, p_input );
char *psz_filters = var_GetString( p_aout, "audio-filter" );
char *psz_visual = var_GetString( p_aout, "audio-visual");
char *psz_scaletempo = var_InheritBool( p_aout, "audio-time-stretch" ) ? strdup( "scaletempo" ) : NULL;
p_input->b_recycle_vout = psz_visual && *psz_visual;
/* parse user filter lists */
char *const ppsz_array[] = { psz_scaletempo, psz_filters, psz_visual };
p_input->p_playback_rate_filter = NULL;
for (unsigned i_visual = 0;
i_visual < 3 && AOUT_FMT_LINEAR(&chain_output_format);
i_visual++)
if (aout_FiltersNew (p_aout, infmt, outfmt, p_request_vout))
{
char *psz_next = NULL;
char *psz_parser = ppsz_array[i_visual];
if( psz_parser == NULL || !*psz_parser )
continue;
while( psz_parser && *psz_parser )
{
filter_t * p_filter = NULL;
if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
{
msg_Dbg( p_aout, "max filters reached (%d)", AOUT_MAX_FILTERS );
break;
}
while( *psz_parser == ' ' && *psz_parser == ':' )
{
psz_parser++;
}
if( ( psz_next = strchr( psz_parser , ':' ) ) )
{
*psz_next++ = '\0';
}
if( *psz_parser =='\0' )
{
break;
}
/* Create a VLC object */
p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
"audio filter" );
if( p_filter == NULL )
{
msg_Err( p_aout, "cannot add user filter %s (skipped)",
psz_parser );
psz_parser = psz_next;
continue;
}
p_filter->p_owner = (filter_owner_sys_t *)p_input;
/* request format */
memcpy( &p_filter->fmt_in.audio, &chain_output_format,
sizeof(audio_sample_format_t) );
p_filter->fmt_in.i_codec = chain_output_format.i_format;
memcpy( &p_filter->fmt_out.audio, &chain_output_format,
sizeof(audio_sample_format_t) );
p_filter->fmt_out.i_codec = chain_output_format.i_format;
/* try to find the requested filter */
if( i_visual == 2 ) /* this can only be a visualization module */
{
p_filter->p_module = module_need( p_filter, "visualization2",
psz_parser, true );
}
else /* this can be a audio filter module as well as a visualization module */
{
p_filter->p_module = module_need( p_filter, "audio filter",
psz_parser, true );
if ( p_filter->p_module == NULL )
{
/* if the filter requested a special format, retry */
if ( !( AOUT_FMTS_IDENTICAL( &p_filter->fmt_in.audio,
&chain_input_format )
&& AOUT_FMTS_IDENTICAL( &p_filter->fmt_out.audio,
&chain_output_format ) ) )
{
aout_FormatPrepare( &p_filter->fmt_in.audio );
aout_FormatPrepare( &p_filter->fmt_out.audio );
p_filter->p_module = module_need( p_filter,
"audio filter",
psz_parser, true );
}
/* try visual filters */
else
{
memcpy( &p_filter->fmt_in.audio, &chain_output_format,
sizeof(audio_sample_format_t) );
memcpy( &p_filter->fmt_out.audio, &chain_output_format,
sizeof(audio_sample_format_t) );
p_filter->p_module = module_need( p_filter,
"visualization2",
psz_parser, true );
}
}
}
/* failure */
if ( p_filter->p_module == NULL )
{
msg_Err( p_aout, "cannot add user filter %s (skipped)",
psz_parser );
vlc_object_release( p_filter );
psz_parser = psz_next;
continue;
}
/* complete the filter chain if necessary */
if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
&p_input->i_nb_filters,
AOUT_MAX_FILTERS,
&chain_input_format,
&p_filter->fmt_in.audio ) < 0 )
{
msg_Err( p_aout, "cannot add user filter %s (skipped)",
psz_parser );
module_unneed( p_filter, p_filter->p_module );
vlc_object_release( p_filter );
psz_parser = psz_next;
continue;
}
/* success */
p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
memcpy( &chain_input_format, &p_filter->fmt_out.audio,
sizeof( audio_sample_format_t ) );
if( i_visual == 0 ) /* scaletempo */
p_input->p_playback_rate_filter = p_filter;
/* next filter if any */