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

band-limited resampler: switch to audio filter2

parent dbd392ea
...@@ -48,10 +48,6 @@ ...@@ -48,10 +48,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 * );
/* audio filter2 */ /* audio filter2 */
static int OpenFilter ( vlc_object_t * ); static int OpenFilter ( vlc_object_t * );
...@@ -81,10 +77,9 @@ struct filter_sys_t ...@@ -81,10 +77,9 @@ struct filter_sys_t
int i_old_wing; int i_old_wing;
unsigned int i_remainder; /* remainder of previous sample */ unsigned int i_remainder; /* remainder of previous sample */
bool b_first;
date_t end_date; date_t end_date;
bool b_filter2;
}; };
/***************************************************************************** /*****************************************************************************
...@@ -94,112 +89,29 @@ vlc_module_begin () ...@@ -94,112 +89,29 @@ vlc_module_begin ()
set_category( CAT_AUDIO ) set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_MISC ) set_subcategory( SUBCAT_AUDIO_MISC )
set_description( N_("Audio filter for band-limited interpolation resampling") ) set_description( N_("Audio filter for band-limited interpolation resampling") )
set_capability( "audio filter", 20 )
set_callbacks( Create, Close )
add_submodule ()
set_description( N_("Audio filter for band-limited interpolation resampling") )
set_capability( "audio filter2", 20 ) set_capability( "audio filter2", 20 )
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;
double d_factor;
int i_filter_wing;
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;
#if !defined( __APPLE__ )
if( !config_GetInt( p_this, "hq-resampling" ) )
{
return VLC_EGENERIC;
}
#endif
/* Allocate the memory needed to store the module's structure */
p_sys = malloc( sizeof(filter_sys_t) );
if( p_sys == NULL )
return VLC_ENOMEM;
p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
/* Calculate worst case for the length of the filter wing */
d_factor = (double)p_filter->fmt_out.audio.i_rate
/ p_filter->fmt_in.audio.i_rate / AOUT_MAX_INPUT_RATE;
i_filter_wing = ((SMALL_FILTER_NMULT + 1)/2.0)
* __MAX(1.0, 1.0/d_factor) + 10;
p_sys->i_buf_size = aout_FormatNbChannels( &p_filter->fmt_in.audio ) *
sizeof(int32_t) * 2 * i_filter_wing;
/* Allocate enough memory to buffer previous samples */
p_sys->p_buf = malloc( p_sys->i_buf_size );
if( p_sys->p_buf == NULL )
{
free( p_sys );
return VLC_ENOMEM;
} }
p_sys->i_old_wing = 0; filter_sys_t *p_sys = p_filter->p_sys;
p_sys->b_filter2 = false; /* It seams to be a good valuefor this module */ unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate;
p_filter->pf_do_work = DoWork;
/* 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_buf );
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;
int i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio ); int i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
int i_in_nb = p_in_buf->i_nb_samples;
int i_in, i_out = 0;
unsigned int i_out_rate;
double d_factor, d_scale_factor, d_old_scale_factor;
int i_filter_wing;
if( p_sys->b_filter2 )
i_out_rate = p_filter->fmt_out.audio.i_rate;
else
i_out_rate = p_aout->mixer_format.i_rate;
/* Check if we really need to run the resampler */ /* Check if we really need to run the resampler */
if( i_out_rate == p_filter->fmt_in.audio.i_rate ) if( i_out_rate == p_filter->fmt_in.audio.i_rate )
{ {
#if 0 /* FIXME: needs audio filter2 to use block_Realloc */
if( /*p_filter->b_continuity && /--* What difference does it make ? :) */ if( /*p_filter->b_continuity && /--* What difference does it make ? :) */
p_sys->i_old_wing ) p_sys->i_old_wing )
{ {
...@@ -208,30 +120,35 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -208,30 +120,35 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame, p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame,
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_sys->p_buf + memcpy( p_in_buf->p_buffer, p_sys->p_buf +
i_nb_channels * p_sys->i_old_wing, i_nb_channels * p_sys->i_old_wing,
p_sys->i_old_wing * p_sys->i_old_wing *
p_filter->fmt_in.audio.i_bytes_per_frame ); p_filter->fmt_in.audio.i_bytes_per_frame );
p_out_buf->i_nb_samples = p_in_buf->i_nb_samples + p_in_buf->i_nb_samples += p_sys->i_old_wing;
p_sys->i_old_wing;
p_out_buf->i_pts = date_Get( &p_sys->end_date ); p_in_buf->i_pts = date_Get( &p_sys->end_date );
p_out_buf->i_length = p_in_buf->i_length =
date_Increment( &p_sys->end_date, date_Increment( &p_sys->end_date,
p_out_buf->i_nb_samples ) - p_out_buf->i_pts; p_in_buf->i_nb_samples ) - p_in_buf->i_pts;
p_out_buf->i_buffer = p_out_buf->i_nb_samples *
p_filter->fmt_in.audio.i_bytes_per_frame;
} }
#endif p_in_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
p_sys->i_old_wing = 0; p_sys->i_old_wing = 0;
return; return p_in_buf;
} }
if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY ) unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
p_filter->fmt_out.audio.i_bitspersample / 8;
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) )
+ p_filter->p_sys->i_buf_size;
block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
if( !p_out_buf )
return NULL;
float *p_out = (float *)p_out_buf->p_buffer;
if( (p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) || p_sys->b_first )
{ {
/* Continuity in sound samples has been broken, we'd better reset /* Continuity in sound samples has been broken, we'd better reset
* everything. */ * everything. */
...@@ -241,8 +158,14 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -241,8 +158,14 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
date_Set( &p_sys->end_date, p_in_buf->i_pts ); date_Set( &p_sys->end_date, p_in_buf->i_pts );
p_sys->d_old_factor = 1; p_sys->d_old_factor = 1;
p_sys->i_old_wing = 0; p_sys->i_old_wing = 0;
p_sys->b_first = false;
} }
int i_in_nb = p_in_buf->i_nb_samples;
int i_in, i_out = 0;
double d_factor, d_scale_factor, d_old_scale_factor;
int i_filter_wing;
#if 0 #if 0
msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i", msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i",
p_sys->i_old_rate, p_sys->d_old_factor, p_sys->i_old_rate, p_sys->d_old_factor,
...@@ -262,9 +185,11 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -262,9 +185,11 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
p_sys->i_old_wing * 2 * p_sys->i_old_wing * 2 *
p_filter->fmt_in.audio.i_bytes_per_frame ); p_filter->fmt_in.audio.i_bytes_per_frame );
} }
/* XXX: why i_nb_channels instead of i_bytes_per_frame??? */
vlc_memcpy( p_in + p_sys->i_old_wing * 2 * i_nb_channels, vlc_memcpy( p_in + p_sys->i_old_wing * 2 * i_nb_channels,
p_in_buf->p_buffer, p_in_buf->p_buffer,
p_in_buf->i_nb_samples * p_filter->fmt_in.audio.i_bytes_per_frame ); p_in_buf->i_nb_samples * p_filter->fmt_in.audio.i_bytes_per_frame );
block_Release( p_in_buf );
/* Make sure the output buffer is reset */ /* Make sure the output buffer is reset */
memset( p_out, 0, p_out_buf->i_buffer ); memset( p_out, 0, p_out_buf->i_buffer );
...@@ -457,7 +382,7 @@ static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, ...@@ -457,7 +382,7 @@ 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);
return p_out_buf;
} }
/***************************************************************************** /*****************************************************************************
...@@ -505,7 +430,7 @@ static int OpenFilter( vlc_object_t *p_this ) ...@@ -505,7 +430,7 @@ static int OpenFilter( vlc_object_t *p_this )
} }
p_sys->i_old_wing = 0; p_sys->i_old_wing = 0;
p_sys->b_filter2 = true; p_sys->b_first = true;
p_filter->pf_audio_filter = Resample; p_filter->pf_audio_filter = Resample;
msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i", msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
...@@ -532,70 +457,6 @@ static void CloseFilter( vlc_object_t *p_this ) ...@@ -532,70 +457,6 @@ static void CloseFilter( vlc_object_t *p_this )
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_filter->p_sys->i_buf_size;
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_in.audio.i_bytes_per_frame = p_filter->fmt_in.audio.i_channels *
p_filter->fmt_in.audio.i_bitspersample / 8;
aout_filter.fmt_out.audio = p_filter->fmt_out.audio;
aout_filter.fmt_out.audio.i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
p_filter->fmt_out.audio.i_bitspersample / 8;
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;
}
void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in, void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
float *p_out, uint32_t ui_remainder, float *p_out, uint32_t ui_remainder,
uint32_t ui_output_rate, int16_t Inc, int i_nb_channels ) uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
......
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