Commit 5576aaac authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

aout: revector filters chain (part 1)

This tries to make the aout filters reusable outside of the aout
(refs #8447).
parent 683f96fa
...@@ -44,6 +44,7 @@ typedef struct ...@@ -44,6 +44,7 @@ typedef struct
void *p_private; void *p_private;
} aout_request_vout_t; } aout_request_vout_t;
typedef struct aout_filters aout_filters_t;
typedef struct aout_volume aout_volume_t; typedef struct aout_volume aout_volume_t;
typedef struct aout_dev aout_dev_t; typedef struct aout_dev aout_dev_t;
...@@ -51,6 +52,7 @@ typedef struct ...@@ -51,6 +52,7 @@ typedef struct
{ {
vlc_mutex_t lock; vlc_mutex_t lock;
module_t *module; /**< Output plugin (or NULL if inactive) */ module_t *module; /**< Output plugin (or NULL if inactive) */
aout_filters_t *filters;
aout_volume_t *volume; aout_volume_t *volume;
struct struct
...@@ -79,14 +81,6 @@ typedef struct ...@@ -79,14 +81,6 @@ typedef struct
audio_sample_format_t input_format; audio_sample_format_t input_format;
audio_sample_format_t mixer_format; audio_sample_format_t mixer_format;
filter_t *rate_filter; /**< The filter adjusting samples count
(either the scaletempo filter or a resampler) */
filter_t *resampler; /**< The resampler */
int resampling; /**< Current resampling (Hz) */
unsigned nb_filters;
filter_t *filters[AOUT_MAX_FILTERS]; /**< Configured user filters
(e.g. equalization) and their conversions */
aout_request_vout_t request_vout; aout_request_vout_t request_vout;
bool recycle_vout; bool recycle_vout;
...@@ -110,11 +104,13 @@ static inline aout_owner_t *aout_owner (audio_output_t *aout) ...@@ -110,11 +104,13 @@ static inline aout_owner_t *aout_owner (audio_output_t *aout)
*****************************************************************************/ *****************************************************************************/
/* From filters.c : */ /* From filters.c : */
int aout_FiltersNew(audio_output_t *, const audio_sample_format_t *, aout_filters_t *aout_FiltersNew(audio_output_t *,
const audio_sample_format_t *, const aout_request_vout_t *); const audio_sample_format_t *,
void aout_FiltersDelete(audio_output_t *); const audio_sample_format_t *,
bool aout_FiltersAdjustResampling(audio_output_t *, int); const aout_request_vout_t *);
block_t *aout_FiltersPlay(audio_output_t *, block_t *, int rate); void aout_FiltersDelete(audio_output_t *, aout_filters_t *);
bool aout_FiltersAdjustResampling(aout_filters_t *, int);
block_t *aout_FiltersPlay(aout_filters_t *, block_t *, int rate);
/* From mixer.c : */ /* From mixer.c : */
aout_volume_t *aout_volume_New(vlc_object_t *, const audio_replay_gain_t *); aout_volume_t *aout_volume_New(vlc_object_t *, const audio_replay_gain_t *);
......
...@@ -84,8 +84,9 @@ int aout_DecNew( audio_output_t *p_aout, ...@@ -84,8 +84,9 @@ int aout_DecNew( audio_output_t *p_aout,
aout_volume_SetFormat (owner->volume, owner->mixer_format.i_format); aout_volume_SetFormat (owner->volume, owner->mixer_format.i_format);
/* Create the audio filtering "input" pipeline */ /* Create the audio filtering "input" pipeline */
if (aout_FiltersNew (p_aout, p_format, &owner->mixer_format, owner->filters = aout_FiltersNew (p_aout, p_format, &owner->mixer_format,
p_request_vout)) p_request_vout);
if (owner->filters == NULL)
{ {
aout_OutputDelete (p_aout); aout_OutputDelete (p_aout);
error: error:
...@@ -113,7 +114,7 @@ void aout_DecDelete (audio_output_t *aout) ...@@ -113,7 +114,7 @@ void aout_DecDelete (audio_output_t *aout)
aout_OutputLock (aout); aout_OutputLock (aout);
if (owner->mixer_format.i_format) if (owner->mixer_format.i_format)
{ {
aout_FiltersDelete (aout); aout_FiltersDelete (aout, owner->filters);
aout_OutputDelete (aout); aout_OutputDelete (aout);
} }
aout_volume_Delete (owner->volume); aout_volume_Delete (owner->volume);
...@@ -131,7 +132,7 @@ static int aout_CheckReady (audio_output_t *aout) ...@@ -131,7 +132,7 @@ static int aout_CheckReady (audio_output_t *aout)
const aout_request_vout_t request_vout = owner->request_vout; const aout_request_vout_t request_vout = owner->request_vout;
if (owner->mixer_format.i_format) if (owner->mixer_format.i_format)
aout_FiltersDelete (aout); aout_FiltersDelete (aout, owner->filters);
if (restart & AOUT_RESTART_OUTPUT) if (restart & AOUT_RESTART_OUTPUT)
{ /* Reinitializes the output */ { /* Reinitializes the output */
...@@ -149,12 +150,16 @@ static int aout_CheckReady (audio_output_t *aout) ...@@ -149,12 +150,16 @@ static int aout_CheckReady (audio_output_t *aout)
owner->sync.end = VLC_TS_INVALID; owner->sync.end = VLC_TS_INVALID;
owner->sync.resamp_type = AOUT_RESAMPLING_NONE; owner->sync.resamp_type = AOUT_RESAMPLING_NONE;
if (owner->mixer_format.i_format if (owner->mixer_format.i_format)
&& aout_FiltersNew (aout, &owner->input_format, &owner->mixer_format,
&request_vout))
{ {
aout_OutputDelete (aout); owner->filters = aout_FiltersNew (aout, &owner->input_format,
owner->mixer_format.i_format = 0; &owner->mixer_format,
&request_vout);
if (owner->filters == NULL)
{
aout_OutputDelete (aout);
owner->mixer_format.i_format = 0;
}
} }
} }
return (owner->mixer_format.i_format) ? 0 : -1; return (owner->mixer_format.i_format) ? 0 : -1;
...@@ -208,7 +213,7 @@ static void aout_StopResampling (audio_output_t *aout) ...@@ -208,7 +213,7 @@ static void aout_StopResampling (audio_output_t *aout)
aout_owner_t *owner = aout_owner (aout); aout_owner_t *owner = aout_owner (aout);
owner->sync.resamp_type = AOUT_RESAMPLING_NONE; owner->sync.resamp_type = AOUT_RESAMPLING_NONE;
aout_FiltersAdjustResampling (aout, 0); aout_FiltersAdjustResampling (owner->filters, 0);
} }
static void aout_DecSilence (audio_output_t *aout, mtime_t length, mtime_t pts) static void aout_DecSilence (audio_output_t *aout, mtime_t length, mtime_t pts)
...@@ -342,7 +347,7 @@ static void aout_DecSynchronize (audio_output_t *aout, mtime_t dec_pts, ...@@ -342,7 +347,7 @@ static void aout_DecSynchronize (audio_output_t *aout, mtime_t dec_pts,
* value, then it is time to switch back the resampling direction. */ * value, then it is time to switch back the resampling direction. */
adj *= -1; adj *= -1;
if (!aout_FiltersAdjustResampling (aout, adj)) if (!aout_FiltersAdjustResampling (owner->filters, adj))
{ /* Everything is back to normal: stop resampling. */ { /* Everything is back to normal: stop resampling. */
owner->sync.resamp_type = AOUT_RESAMPLING_NONE; owner->sync.resamp_type = AOUT_RESAMPLING_NONE;
msg_Dbg (aout, "resampling stopped (drift: %"PRId64" us)", drift); msg_Dbg (aout, "resampling stopped (drift: %"PRId64" us)", drift);
...@@ -384,7 +389,7 @@ int aout_DecPlay (audio_output_t *aout, block_t *block, int input_rate) ...@@ -384,7 +389,7 @@ int aout_DecPlay (audio_output_t *aout, block_t *block, int input_rate)
if (block->i_flags & BLOCK_FLAG_DISCONTINUITY) if (block->i_flags & BLOCK_FLAG_DISCONTINUITY)
owner->sync.discontinuity = true; owner->sync.discontinuity = true;
block = aout_FiltersPlay (aout, block, input_rate); block = aout_FiltersPlay (owner->filters, block, input_rate);
if (block == NULL) if (block == NULL)
goto lost; goto lost;
......
...@@ -272,6 +272,18 @@ static block_t *aout_FiltersPipelinePlay(filter_t *const *filters, ...@@ -272,6 +272,18 @@ static block_t *aout_FiltersPipelinePlay(filter_t *const *filters,
return block; return block;
} }
struct aout_filters
{
filter_t *rate_filter; /**< The filter adjusting samples count
(either the scaletempo filter or a resampler) */
filter_t *resampler; /**< The resampler */
int resampling; /**< Current resampling (Hz) */
unsigned count; /**< Number of filters */
filter_t *tab[AOUT_MAX_FILTERS]; /**< Configured user filters
(e.g. equalization) and their conversions */
};
/** Callback for visualization selection */ /** Callback for visualization selection */
static int VisualizationCallback (vlc_object_t *obj, const char *var, static int VisualizationCallback (vlc_object_t *obj, const char *var,
vlc_value_t oldval, vlc_value_t newval, vlc_value_t oldval, vlc_value_t newval,
...@@ -332,12 +344,12 @@ vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout, ...@@ -332,12 +344,12 @@ vout_thread_t *aout_filter_RequestVout (filter_t *filter, vout_thread_t *vout,
} }
static int AppendFilter(vlc_object_t *obj, const char *type, const char *name, static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
filter_t **filters, unsigned *count, aout_filters_t *restrict filters,
audio_sample_format_t *restrict infmt, audio_sample_format_t *restrict infmt,
const audio_sample_format_t *restrict outfmt) const audio_sample_format_t *restrict outfmt)
{ {
const unsigned max = AOUT_MAX_FILTERS; const unsigned max = sizeof (filters->tab) / sizeof (filters->tab[0]);
if (*count >= max) if (filters->count >= max)
{ {
msg_Err (obj, "maximum of %u filters reached", max); msg_Err (obj, "maximum of %u filters reached", max);
return -1; return -1;
...@@ -351,8 +363,8 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name, ...@@ -351,8 +363,8 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
} }
/* convert to the filter input format if necessary */ /* convert to the filter input format if necessary */
if (aout_FiltersPipelineCreate (filter, filters, count, max - 1, infmt, if (aout_FiltersPipelineCreate (filter, filters->tab, &filters->count,
&filter->fmt_in.audio)) max - 1, infmt, &filter->fmt_in.audio))
{ {
msg_Err (filter, "cannot add user %s \"%s\" (skipped)", type, name); msg_Err (filter, "cannot add user %s \"%s\" (skipped)", type, name);
module_unneed (filter, filter->p_module); module_unneed (filter, filter->p_module);
...@@ -360,8 +372,9 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name, ...@@ -360,8 +372,9 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
return -1; return -1;
} }
assert (*count < max); assert (filters->count < max);
filters[(*count)++] = filter; filters->tab[filters->count] = filter;
filters->count++;
*infmt = filter->fmt_out.audio; *infmt = filter->fmt_out.audio;
return 0; return 0;
} }
...@@ -370,11 +383,20 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name, ...@@ -370,11 +383,20 @@ static int AppendFilter(vlc_object_t *obj, const char *type, const char *name,
/** /**
* Sets up the audio filters. * Sets up the audio filters.
*/ */
int aout_FiltersNew (audio_output_t *aout, aout_filters_t *aout_FiltersNew (audio_output_t *aout,
const audio_sample_format_t *restrict infmt, const audio_sample_format_t *restrict infmt,
const audio_sample_format_t *restrict outfmt, const audio_sample_format_t *restrict outfmt,
const aout_request_vout_t *request_vout) const aout_request_vout_t *request_vout)
{ {
aout_filters_t *filters = malloc (sizeof (*filters));
if (unlikely(filters == NULL))
return NULL;
filters->rate_filter = NULL;
filters->resampler = NULL;
filters->resampling = 0;
filters->count = 0;
aout_owner_t *owner = aout_owner (aout); aout_owner_t *owner = aout_owner (aout);
/* Prepare format structure */ /* Prepare format structure */
...@@ -383,10 +405,6 @@ int aout_FiltersNew (audio_output_t *aout, ...@@ -383,10 +405,6 @@ int aout_FiltersNew (audio_output_t *aout,
audio_sample_format_t output_format = *outfmt; audio_sample_format_t output_format = *outfmt;
/* Now add user filters */ /* Now add user filters */
owner->nb_filters = 0;
owner->rate_filter = NULL;
owner->resampler = NULL;
var_AddCallback (aout, "visual", VisualizationCallback, NULL); var_AddCallback (aout, "visual", VisualizationCallback, NULL);
var_AddCallback (aout, "equalizer", EqualizerCallback, NULL); var_AddCallback (aout, "equalizer", EqualizerCallback, NULL);
...@@ -395,37 +413,35 @@ int aout_FiltersNew (audio_output_t *aout, ...@@ -395,37 +413,35 @@ int aout_FiltersNew (audio_output_t *aout,
if (!AOUT_FMTS_IDENTICAL(infmt, outfmt)) if (!AOUT_FMTS_IDENTICAL(infmt, outfmt))
{ {
aout_FormatsPrint (aout, "pass-through:", infmt, outfmt); aout_FormatsPrint (aout, "pass-through:", infmt, outfmt);
owner->filters[0] = FindConverter(VLC_OBJECT(aout), infmt, outfmt); filters->tab[0] = FindConverter(VLC_OBJECT(aout), infmt, outfmt);
if (owner->filters[0] == NULL) if (filters->tab[0] == NULL)
{ {
msg_Err (aout, "cannot setup pass-through"); msg_Err (aout, "cannot setup pass-through");
goto error; goto error;
} }
owner->nb_filters++; filters->count++;
} }
return 0; return filters;
} }
/* parse user filter lists */ /* parse user filter lists */
if (var_InheritBool (aout, "audio-time-stretch")) if (var_InheritBool (aout, "audio-time-stretch"))
{ {
if (AppendFilter(VLC_OBJECT(aout), "audio filter", "scaletempo", if (AppendFilter(VLC_OBJECT(aout), "audio filter", "scaletempo",
owner->filters, &owner->nb_filters, filters, &input_format, &output_format) == 0)
&input_format, &output_format) == 0) filters->rate_filter = filters->tab[filters->count - 1];
owner->rate_filter = owner->filters[owner->nb_filters - 1];
} }
char *filters = var_InheritString (aout, "audio-filter"); char *str = var_InheritString (aout, "audio-filter");
if (filters != NULL) if (str != NULL)
{ {
char *p = filters, *name; char *p = str, *name;
while ((name = strsep (&p, " :")) != NULL) while ((name = strsep (&p, " :")) != NULL)
{ {
AppendFilter(VLC_OBJECT(aout), "audio filter", name, AppendFilter(VLC_OBJECT(aout), "audio filter", name, filters,
owner->filters, &owner->nb_filters,
&input_format, &output_format); &input_format, &output_format);
} }
free (filters); free (str);
} }
char *visual = var_InheritString (aout, "audio-visual"); char *visual = var_InheritString (aout, "audio-visual");
...@@ -433,17 +449,15 @@ int aout_FiltersNew (audio_output_t *aout, ...@@ -433,17 +449,15 @@ int aout_FiltersNew (audio_output_t *aout,
owner->recycle_vout = visual != NULL; owner->recycle_vout = visual != NULL;
if (visual != NULL && strcasecmp (visual, "none")) if (visual != NULL && strcasecmp (visual, "none"))
{ {
AppendFilter(VLC_OBJECT(aout), "visualization", visual, AppendFilter(VLC_OBJECT(aout), "visualization", visual, filters,
owner->filters, &owner->nb_filters,
&input_format, &output_format); &input_format, &output_format);
} }
free (visual); free (visual);
/* convert to the output format (minus resampling) if necessary */ /* convert to the output format (minus resampling) if necessary */
output_format.i_rate = input_format.i_rate; output_format.i_rate = input_format.i_rate;
if (aout_FiltersPipelineCreate (aout, owner->filters, &owner->nb_filters, if (aout_FiltersPipelineCreate (aout, filters->tab, &filters->count,
AOUT_MAX_FILTERS, AOUT_MAX_FILTERS, &input_format, &output_format))
&input_format, &output_format))
{ {
msg_Err (aout, "cannot setup filtering pipeline"); msg_Err (aout, "cannot setup filtering pipeline");
goto error; goto error;
...@@ -453,36 +467,36 @@ int aout_FiltersNew (audio_output_t *aout, ...@@ -453,36 +467,36 @@ int aout_FiltersNew (audio_output_t *aout,
/* insert the resampler */ /* insert the resampler */
output_format.i_rate = outfmt->i_rate; output_format.i_rate = outfmt->i_rate;
assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt)); assert (AOUT_FMTS_IDENTICAL(&output_format, outfmt));
owner->resampler = FindResampler (VLC_OBJECT(aout), &input_format, filters->resampler = FindResampler (VLC_OBJECT(aout), &input_format,
&output_format); &output_format);
if (owner->resampler == NULL && input_format.i_rate != outfmt->i_rate) if (filters->resampler == NULL && input_format.i_rate != outfmt->i_rate)
{ {
msg_Err (aout, "cannot setup a resampler"); msg_Err (aout, "cannot setup a resampler");
goto error; goto error;
} }
if (owner->rate_filter == NULL) if (filters->rate_filter == NULL)
owner->rate_filter = owner->resampler; filters->rate_filter = filters->resampler;
owner->resampling = 0;
return 0; return filters;
error: error:
aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters); aout_FiltersPipelineDestroy (filters->tab, filters->count);
var_DelCallback (aout, "equalizer", EqualizerCallback, NULL); var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
var_DelCallback (aout, "visual", VisualizationCallback, NULL); var_DelCallback (aout, "visual", VisualizationCallback, NULL);
return -1; free (filters);
return NULL;
} }
/** /**
* Destroys the audio filters. * Destroys the audio filters.
*/ */
void aout_FiltersDelete (audio_output_t *aout) void aout_FiltersDelete (audio_output_t *aout, aout_filters_t *filters)
{ {
aout_owner_t *owner = aout_owner (aout); aout_owner_t *owner = aout_owner (aout);
if (owner->resampler != NULL) if (filters->resampler != NULL)
aout_FiltersPipelineDestroy (&owner->resampler, 1); aout_FiltersPipelineDestroy (&filters->resampler, 1);
aout_FiltersPipelineDestroy (owner->filters, owner->nb_filters); aout_FiltersPipelineDestroy (filters->tab, filters->count);
var_DelCallback (aout, "equalizer", EqualizerCallback, NULL); var_DelCallback (aout, "equalizer", EqualizerCallback, NULL);
var_DelCallback (aout, "visual", VisualizationCallback, NULL); var_DelCallback (aout, "visual", VisualizationCallback, NULL);
...@@ -494,30 +508,29 @@ void aout_FiltersDelete (audio_output_t *aout) ...@@ -494,30 +508,29 @@ void aout_FiltersDelete (audio_output_t *aout)
char *visual = var_InheritString (aout, "audio-visual"); char *visual = var_InheritString (aout, "audio-visual");
owner->recycle_vout = (visual != NULL) && *visual; owner->recycle_vout = (visual != NULL) && *visual;
free (visual); free (visual);
free (filters);
} }
bool aout_FiltersAdjustResampling (audio_output_t *aout, int adjust) bool aout_FiltersAdjustResampling (aout_filters_t *filters, int adjust)
{ {
aout_owner_t *owner = aout_owner (aout); if (filters->resampler == NULL)
if (owner->resampler == NULL)
return false; return false;
if (adjust) if (adjust)
owner->resampling += adjust; filters->resampling += adjust;
else else
owner->resampling = 0; filters->resampling = 0;
return owner->resampling != 0; return filters->resampling != 0;
} }
block_t *aout_FiltersPlay (audio_output_t *aout, block_t *block, int rate) block_t *aout_FiltersPlay (aout_filters_t *filters, block_t *block, int rate)
{ {
aout_owner_t *owner = aout_owner (aout);
int nominal_rate = 0; int nominal_rate = 0;
if (rate != INPUT_RATE_DEFAULT) if (rate != INPUT_RATE_DEFAULT)
{ {
filter_t *rate_filter = owner->rate_filter; filter_t *rate_filter = filters->rate_filter;
if (rate_filter == NULL) if (rate_filter == NULL)
goto drop; /* Without linear, non-nominal rate is impossible. */ goto drop; /* Without linear, non-nominal rate is impossible. */
...@@ -528,20 +541,19 @@ block_t *aout_FiltersPlay (audio_output_t *aout, block_t *block, int rate) ...@@ -528,20 +541,19 @@ block_t *aout_FiltersPlay (audio_output_t *aout, block_t *block, int rate)
(nominal_rate * INPUT_RATE_DEFAULT) / rate; (nominal_rate * INPUT_RATE_DEFAULT) / rate;
} }
block = aout_FiltersPipelinePlay (owner->filters, owner->nb_filters, block = aout_FiltersPipelinePlay (filters->tab, filters->count, block);
block); if (filters->resampler != NULL)
if (owner->resampler != NULL)
{ /* NOTE: the resampler needs to run even if resampling is 0. { /* NOTE: the resampler needs to run even if resampling is 0.
* The decoder and output rates can still be different. */ * The decoder and output rates can still be different. */
owner->resampler->fmt_in.audio.i_rate += owner->resampling; filters->resampler->fmt_in.audio.i_rate += filters->resampling;
block = aout_FiltersPipelinePlay (&owner->resampler, 1, block); block = aout_FiltersPipelinePlay (&filters->resampler, 1, block);
owner->resampler->fmt_in.audio.i_rate -= owner->resampling; filters->resampler->fmt_in.audio.i_rate -= filters->resampling;
} }
if (nominal_rate != 0) if (nominal_rate != 0)
{ /* Restore input rate */ { /* Restore input rate */
assert (owner->rate_filter != NULL); assert (filters->rate_filter != NULL);
owner->rate_filter->fmt_in.audio.i_rate = nominal_rate; filters->rate_filter->fmt_in.audio.i_rate = nominal_rate;
} }
return block; return block;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment