Commit 17e7ea9a authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

linear resampler: audio filter2

parent ff981aa0
...@@ -39,11 +39,6 @@ ...@@ -39,11 +39,6 @@
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
*****************************************************************************/ *****************************************************************************/
static int Create ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
aout_buffer_t * );
static int OpenFilter ( vlc_object_t * ); static int OpenFilter ( vlc_object_t * );
static void CloseFilter( vlc_object_t * ); static void CloseFilter( vlc_object_t * );
static block_t *Resample( filter_t *, block_t * ); static block_t *Resample( filter_t *, block_t * );
...@@ -67,105 +62,56 @@ vlc_module_begin () ...@@ -67,105 +62,56 @@ vlc_module_begin ()
set_description( N_("Audio filter for linear interpolation resampling") ) set_description( N_("Audio filter for linear interpolation resampling") )
set_category( CAT_AUDIO ) set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_MISC ) set_subcategory( SUBCAT_AUDIO_MISC )
set_capability( "audio filter", 5 )
set_callbacks( Create, Close )
add_submodule ()
set_description( N_("Audio filter for linear interpolation resampling") )
set_capability( "audio filter2", 5 ) set_capability( "audio filter2", 5 )
set_callbacks( OpenFilter, CloseFilter ) set_callbacks( OpenFilter, CloseFilter )
vlc_module_end () vlc_module_end ()
/***************************************************************************** /*****************************************************************************
* Create: allocate linear resampler * Resample: convert a buffer
*****************************************************************************/ *****************************************************************************/
static int Create( vlc_object_t *p_this ) static block_t *Resample( filter_t *p_filter, block_t *p_in_buf )
{ {
aout_filter_t * p_filter = (aout_filter_t *)p_this; if( !p_in_buf || !p_in_buf->i_nb_samples )
struct filter_sys_t * p_sys;
if ( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate
|| p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format
|| p_filter->fmt_in.audio.i_physical_channels
!= p_filter->fmt_out.audio.i_physical_channels
|| p_filter->fmt_in.audio.i_original_channels
!= p_filter->fmt_out.audio.i_original_channels
|| p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
{ {
return VLC_EGENERIC; if( p_in_buf )
} block_Release( p_in_buf );
return NULL;
/* Allocate the memory needed to store the module's structure */
p_sys = malloc( sizeof(filter_sys_t) );
p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
if( p_sys == NULL )
return VLC_ENOMEM;
p_sys->p_prev_sample = malloc(
p_filter->fmt_in.audio.i_channels * sizeof(int32_t) );
if( p_sys->p_prev_sample == NULL )
{
free( p_sys );
return VLC_ENOMEM;
} }
date_Init( &p_sys->end_date, p_filter->fmt_out.audio.i_rate, 1 );
p_filter->pf_do_work = DoWork; filter_sys_t *p_sys = p_filter->p_sys;
unsigned i_nb_channels = p_filter->fmt_in.audio.i_channels;
/* We don't want a new buffer to be created because we're not sure we'll
* actually need to resample anything. */
p_filter->b_in_place = true;
return VLC_SUCCESS;
}
/*****************************************************************************
* Close: free our resources
*****************************************************************************/
static void Close( vlc_object_t * p_this )
{
aout_filter_t * p_filter = (aout_filter_t *)p_this;
filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
free( p_sys->p_prev_sample );
free( p_sys );
}
/*****************************************************************************
* DoWork: convert a buffer
*****************************************************************************/
static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
{
filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
float *p_out = (float *)p_out_buf->p_buffer;
float *p_prev_sample = (float *)p_sys->p_prev_sample; float *p_prev_sample = (float *)p_sys->p_prev_sample;
int i_nb_channels = p_filter->fmt_in.audio.i_channels;
int i_in_nb = p_in_buf->i_nb_samples;
int i_chan, i_in, i_out = 0;
/* Check if we really need to run the resampler */ /* Check if we really need to run the resampler */
if( p_aout->mixer_format.i_rate == p_filter->fmt_in.audio.i_rate ) if( p_filter->fmt_out.audio.i_rate == p_filter->fmt_in.audio.i_rate )
{ {
#if 0 /* FIXME: needs audio filter2 for block_Realloc */
if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) ) if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) )
{ {
p_in_buf = block_Realloc( p_in_buf, sizeof(float) * i_nb_channels, p_in_buf = block_Realloc( p_in_buf, sizeof(float) * i_nb_channels,
p_in_buf->i_buffer ); p_in_buf->i_buffer );
if( !p_in_buf ) if( !p_in_buf )
abort(); return NULL;
memcpy( p_in_buf->p_buffer, p_prev_sample, memcpy( p_in_buf->p_buffer, p_prev_sample,
i_nb_channels * sizeof(float) ); i_nb_channels * sizeof(float) );
} }
#endif return p_in_buf;
p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
return;
} }
float p_in_orig[p_in_buf->i_buffer / 4], *p_in = p_in_orig; unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
p_filter->fmt_out.audio.i_bitspersample / 8;
vlc_memcpy( p_in, p_in_buf->p_buffer, p_in_buf->i_buffer ); size_t i_out_size = i_bytes_per_frame * (1 + (p_in_buf->i_nb_samples *
p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
if( !p_out_buf )
goto out;
float *p_out = (float *)p_out_buf->p_buffer;
unsigned i_in_nb = p_in_buf->i_nb_samples;
unsigned i_out = 0;
const float *p_in = (float *)p_in_buf->p_buffer;
/* Take care of the previous input sample (if any) */ /* Take care of the previous input sample (if any) */
if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY ) if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
...@@ -178,7 +124,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -178,7 +124,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
{ {
while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate ) while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
{ {
for( i_chan = i_nb_channels ; i_chan ; ) for( unsigned i_chan = i_nb_channels ; i_chan ; )
{ {
i_chan--; i_chan--;
p_out[i_chan] = p_prev_sample[i_chan]; p_out[i_chan] = p_prev_sample[i_chan];
...@@ -195,11 +141,11 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -195,11 +141,11 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
} }
/* Take care of the current input samples (minus last one) */ /* Take care of the current input samples (minus last one) */
for( i_in = 0; i_in < i_in_nb - 1; i_in++ ) for( unsigned i_in = 0; i_in < i_in_nb - 1; i_in++ )
{ {
while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate ) while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
{ {
for( i_chan = i_nb_channels ; i_chan ; ) for( unsigned i_chan = i_nb_channels ; i_chan ; )
{ {
i_chan--; i_chan--;
p_out[i_chan] = p_in[i_chan]; p_out[i_chan] = p_in[i_chan];
...@@ -218,7 +164,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -218,7 +164,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
} }
/* Backup the last input sample for next time */ /* Backup the last input sample for next time */
for( i_chan = i_nb_channels ; i_chan ; ) for( unsigned i_chan = i_nb_channels ; i_chan ; )
{ {
i_chan--; i_chan--;
p_prev_sample[i_chan] = p_in[i_chan]; p_prev_sample[i_chan] = p_in[i_chan];
...@@ -238,6 +184,9 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -238,6 +184,9 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
p_out_buf->i_buffer = p_out_buf->i_nb_samples * p_out_buf->i_buffer = p_out_buf->i_nb_samples *
i_nb_channels * sizeof(int32_t); i_nb_channels * sizeof(int32_t);
out:
block_Release( p_in_buf );
return p_out_buf;
} }
/***************************************************************************** /*****************************************************************************
...@@ -268,6 +217,7 @@ static int OpenFilter( vlc_object_t *p_this ) ...@@ -268,6 +217,7 @@ static int OpenFilter( vlc_object_t *p_this )
return VLC_ENOMEM; return VLC_ENOMEM;
} }
date_Init( &p_sys->end_date, p_filter->fmt_in.audio.i_rate, 1 ); date_Init( &p_sys->end_date, p_filter->fmt_in.audio.i_rate, 1 );
p_sys->i_remainder = 0;
p_filter->pf_audio_filter = Resample; p_filter->pf_audio_filter = Resample;
...@@ -294,61 +244,3 @@ static void CloseFilter( vlc_object_t *p_this ) ...@@ -294,61 +244,3 @@ static void CloseFilter( vlc_object_t *p_this )
free( p_filter->p_sys->p_prev_sample ); free( p_filter->p_sys->p_prev_sample );
free( p_filter->p_sys ); free( p_filter->p_sys );
} }
/*****************************************************************************
* Resample
*****************************************************************************/
static block_t *Resample( filter_t *p_filter, block_t *p_block )
{
aout_filter_t aout_filter;
aout_buffer_t in_buf, out_buf;
block_t *p_out;
int i_out_size;
int i_bytes_per_frame;
if( !p_block || !p_block->i_nb_samples )
{
if( p_block )
block_Release( p_block );
return NULL;
}
i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
p_filter->fmt_out.audio.i_bitspersample / 8;
i_out_size = i_bytes_per_frame * ( 1 + (p_block->i_nb_samples *
p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
if( !p_out )
{
msg_Warn( p_filter, "can't get output buffer" );
block_Release( p_block );
return NULL;
}
p_out->i_nb_samples = i_out_size / i_bytes_per_frame;
p_out->i_dts = p_block->i_dts;
p_out->i_pts = p_block->i_pts;
p_out->i_length = p_block->i_length;
aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
aout_filter.fmt_in.audio = p_filter->fmt_in.audio;
aout_filter.fmt_out.audio = p_filter->fmt_out.audio;
in_buf.p_buffer = p_block->p_buffer;
in_buf.i_buffer = p_block->i_buffer;
in_buf.i_nb_samples = p_block->i_nb_samples;
out_buf.p_buffer = p_out->p_buffer;
out_buf.i_buffer = p_out->i_buffer;
out_buf.i_nb_samples = p_out->i_nb_samples;
DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
block_Release( p_block );
p_out->i_buffer = out_buf.i_buffer;
p_out->i_nb_samples = out_buf.i_nb_samples;
return p_out;
}
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