shithub: libsamplerate

Download patch

ref: 1bb5e8adc3cd6686b492a369b83cb9eb440b42ef
parent: 2da4fd035cc44db3cd75543eeec100cdb5447081
author: Erik de Castro Lopo <erikd@mega-nerd.com>
date: Sat Dec 13 10:40:54 EST 2008

src/src_sinc.c : Rejig converter so filter coefficients are calculated once per frame and special case channel counts of 1, 2 and 4.

--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,10 @@
     * tests/multichan_throughput_test.c
     Do throughput test on all three SINC based converters.
 
+    * src/src_sinc.c
+    Rejig converter so filter coefficients are calculated once per frame and
+    special case channel counts of 1, 2 and 4.
+
 2008-12-12  Erik de Castro Lopo  <erikd AT mega-nerd DOT com>
 
     *  src/samplerate.c examples/audio_out.c tests/*.c
--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -65,11 +65,15 @@
 	coeff_t const	*coeffs ;
 
 	int		b_current, b_end, b_real_end, b_len ;
-	float	buffer [1] ;
+
+	/* C99 struct flexible array. */
+	float	buffer [] ;
 } SINC_FILTER ;
 
 static int sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
-static int sinc_1chan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_quad_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_stereo_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
+static int sinc_mono_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
 
 static void prepare_data (SINC_FILTER *filter, SRC_DATA *data, int half_filter_chan_len) ;
 
@@ -164,10 +168,20 @@
 	temp_filter.channels = psrc->channels ;
 
 	if (psrc->channels == 1)
-	{	psrc->const_process = sinc_1chan_vari_process ;
-		psrc->vari_process = sinc_1chan_vari_process ;
+	{	psrc->const_process = sinc_mono_vari_process ;
+		psrc->vari_process = sinc_mono_vari_process ;
 		}
 	else
+	if (psrc->channels == 2)
+	{	psrc->const_process = sinc_stereo_vari_process ;
+		psrc->vari_process = sinc_stereo_vari_process ;
+		}
+	else
+	if (psrc->channels == 4)
+	{	psrc->const_process = sinc_quad_vari_process ;
+		psrc->vari_process = sinc_quad_vari_process ;
+		}
+	else
 	{	psrc->const_process = sinc_multichan_vari_process ;
 		psrc->vari_process = sinc_multichan_vari_process ;
 		} ;
@@ -300,9 +314,102 @@
 	return (left + right) ;
 } /* calc_output_single */
 
-static inline double
-calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int ch)
-{	double		fraction, left, right, icoeff ;
+static int
+sinc_mono_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+{	SINC_FILTER *filter ;
+	double		input_index, src_ratio, count, float_increment, terminate, rem ;
+	increment_t	increment, start_filter_index ;
+	int			half_filter_chan_len, samples_in_hand ;
+
+	if (psrc->private_data == NULL)
+		return SRC_ERR_NO_PRIVATE ;
+
+	filter = (SINC_FILTER*) psrc->private_data ;
+
+	/* If there is not a problem, this will be optimised out. */
+	if (sizeof (filter->buffer [0]) != sizeof (data->data_in [0]))
+		return SRC_ERR_SIZE_INCOMPATIBILITY ;
+
+	filter->in_count = data->input_frames * filter->channels ;
+	filter->out_count = data->output_frames * filter->channels ;
+	filter->in_used = filter->out_gen = 0 ;
+
+	src_ratio = psrc->last_ratio ;
+
+	/* Check the sample rate ratio wrt the buffer len. */
+	count = (filter->coeff_half_len + 2.0) / filter->index_inc ;
+	if (MIN (psrc->last_ratio, data->src_ratio) < 1.0)
+		count /= MIN (psrc->last_ratio, data->src_ratio) ;
+
+	/* Maximum coefficientson either side of center point. */
+	half_filter_chan_len = filter->channels * (lrint (count) + 1) ;
+
+	input_index = psrc->last_position ;
+	float_increment = filter->index_inc ;
+
+	rem = fmod_one (input_index) ;
+	filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+	input_index = rem ;
+
+	terminate = 1.0 / src_ratio + 1e-20 ;
+
+	/* Main processing loop. */
+	while (filter->out_gen < filter->out_count)
+	{
+		/* Need to reload buffer? */
+		samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+
+		if (samples_in_hand <= half_filter_chan_len)
+		{	prepare_data (filter, data, half_filter_chan_len) ;
+
+			samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+			if (samples_in_hand <= half_filter_chan_len)
+				break ;
+			} ;
+
+		/* This is the termination condition. */
+		if (filter->b_real_end >= 0)
+		{	if (filter->b_current + input_index + terminate >= filter->b_real_end)
+				break ;
+			} ;
+
+		if (filter->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > 1e-10)
+			src_ratio = psrc->last_ratio + filter->out_gen * (data->src_ratio - psrc->last_ratio) / filter->out_count ;
+
+		float_increment = filter->index_inc * 1.0 ;
+		if (src_ratio < 1.0)
+			float_increment = filter->index_inc * src_ratio ;
+
+		increment = double_to_fp (float_increment) ;
+
+		start_filter_index = double_to_fp (input_index * float_increment) ;
+
+		data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
+										calc_output_single (filter, increment, start_filter_index)) ;
+		filter->out_gen ++ ;
+
+		/* Figure out the next index. */
+		input_index += 1.0 / src_ratio ;
+		rem = fmod_one (input_index) ;
+
+		filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+		input_index = rem ;
+		} ;
+
+	psrc->last_position = input_index ;
+
+	/* Save current ratio rather then target ratio. */
+	psrc->last_ratio = src_ratio ;
+
+	data->input_frames_used = filter->in_used / filter->channels ;
+	data->output_frames_gen = filter->out_gen / filter->channels ;
+
+	return SRC_ERR_NO_ERROR ;
+} /* sinc_mono_vari_process */
+
+static inline void
+calc_output_stereo (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, double scale, float * output)
+{	double		fraction, left [2], right [2], icoeff ;
 	increment_t	filter_index, max_filter_index ;
 	int			data_index, coeff_count, indx ;
 
@@ -313,9 +420,9 @@
 	filter_index = start_filter_index ;
 	coeff_count = (max_filter_index - filter_index) / increment ;
 	filter_index = filter_index + coeff_count * increment ;
-	data_index = filter->b_current - filter->channels * coeff_count + ch ;
+	data_index = filter->b_current - filter->channels * coeff_count ;
 
-	left = 0.0 ;
+	left [0] = left [1] = 0.0 ;
 	do
 	{	fraction = fp_to_double (filter_index) ;
 		indx = fp_to_int (filter_index) ;
@@ -322,10 +429,11 @@
 
 		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
 
-		left += icoeff * filter->buffer [data_index] ;
+		left [0] += icoeff * filter->buffer [data_index] ;
+		left [1] += icoeff * filter->buffer [data_index + 1] ;
 
 		filter_index -= increment ;
-		data_index = data_index + filter->channels ;
+		data_index = data_index + 2 ;
 		}
 	while (filter_index >= MAKE_INCREMENT_T (0)) ;
 
@@ -333,9 +441,9 @@
 	filter_index = increment - start_filter_index ;
 	coeff_count = (max_filter_index - filter_index) / increment ;
 	filter_index = filter_index + coeff_count * increment ;
-	data_index = filter->b_current + filter->channels * (1 + coeff_count) + ch ;
+	data_index = filter->b_current + filter->channels * (1 + coeff_count) ;
 
-	right = 0.0 ;
+	right [0] = right [1] = 0.0 ;
 	do
 	{	fraction = fp_to_double (filter_index) ;
 		indx = fp_to_int (filter_index) ;
@@ -342,7 +450,8 @@
 
 		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
 
-		right += icoeff * filter->buffer [data_index] ;
+		right [0] += icoeff * filter->buffer [data_index] ;
+		right [1] += icoeff * filter->buffer [data_index + 1] ;
 
 		filter_index -= increment ;
 		data_index = data_index - filter->channels ;
@@ -349,11 +458,12 @@
 		}
 	while (filter_index > MAKE_INCREMENT_T (0)) ;
 
-	return (left + right) ;
-} /* calc_output_multi */
+	output [0] = scale * (left [0] + right [0]) ;
+	output [1] = scale * (left [1] + right [1]) ;
+} /* calc_output_stereo */
 
 static int
-sinc_1chan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+sinc_stereo_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
 {	SINC_FILTER *filter ;
 	double		input_index, src_ratio, count, float_increment, terminate, rem ;
 	increment_t	increment, start_filter_index ;
@@ -422,9 +532,8 @@
 
 		start_filter_index = double_to_fp (input_index * float_increment) ;
 
-		data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
-										calc_output_single (filter, increment, start_filter_index)) ;
-		filter->out_gen ++ ;
+		calc_output_stereo (filter, increment, start_filter_index, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += 2 ;
 
 		/* Figure out the next index. */
 		input_index += 1.0 / src_ratio ;
@@ -443,14 +552,75 @@
 	data->output_frames_gen = filter->out_gen / filter->channels ;
 
 	return SRC_ERR_NO_ERROR ;
-} /* sinc_1chan_vari_process */
+} /* sinc_stereo_vari_process */
 
+static inline void
+calc_output_quad (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, double scale, float * output)
+{	double		fraction, left [4], right [4], icoeff ;
+	increment_t	filter_index, max_filter_index ;
+	int			data_index, coeff_count, indx ;
+
+	/* Convert input parameters into fixed point. */
+	max_filter_index = int_to_fp (filter->coeff_half_len) ;
+
+	/* First apply the left half of the filter. */
+	filter_index = start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current - filter->channels * coeff_count ;
+
+	left [0] = left [1] = left [2] = left [3] = 0.0 ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		left [0] += icoeff * filter->buffer [data_index] ;
+		left [1] += icoeff * filter->buffer [data_index + 1] ;
+		left [2] += icoeff * filter->buffer [data_index + 2] ;
+		left [3] += icoeff * filter->buffer [data_index + 3] ;
+
+		filter_index -= increment ;
+		data_index = data_index + 4 ;
+		}
+	while (filter_index >= MAKE_INCREMENT_T (0)) ;
+
+	/* Now apply the right half of the filter. */
+	filter_index = increment - start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current + filter->channels * (1 + coeff_count) ;
+
+	right [0] = right [1] = right [2] = right [3] = 0.0 ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		right [0] += icoeff * filter->buffer [data_index] ;
+		right [1] += icoeff * filter->buffer [data_index + 1] ;
+		right [2] += icoeff * filter->buffer [data_index + 2] ;
+		right [3] += icoeff * filter->buffer [data_index + 3] ;
+
+		filter_index -= increment ;
+		data_index = data_index - filter->channels ;
+		}
+	while (filter_index > MAKE_INCREMENT_T (0)) ;
+
+	output [0] = scale * (left [0] + right [0]) ;
+	output [1] = scale * (left [1] + right [1]) ;
+	output [2] = scale * (left [2] + right [2]) ;
+	output [3] = scale * (left [3] + right [3]) ;
+} /* calc_output_quad */
+
 static int
-sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+sinc_quad_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
 {	SINC_FILTER *filter ;
 	double		input_index, src_ratio, count, float_increment, terminate, rem ;
 	increment_t	increment, start_filter_index ;
-	int			half_filter_chan_len, samples_in_hand, ch ;
+	int			half_filter_chan_len, samples_in_hand ;
 
 	if (psrc->private_data == NULL)
 		return SRC_ERR_NO_PRIVATE ;
@@ -515,11 +685,161 @@
 
 		start_filter_index = double_to_fp (input_index * float_increment) ;
 
-		for (ch = 0 ; ch < filter->channels ; ch++)
-		{	data->data_out [filter->out_gen] = (float) ((float_increment / filter->index_inc) *
-											calc_output_multi (filter, increment, start_filter_index, ch)) ;
-			filter->out_gen ++ ;
+		calc_output_quad (filter, increment, start_filter_index, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += 4 ;
+
+		/* Figure out the next index. */
+		input_index += 1.0 / src_ratio ;
+		rem = fmod_one (input_index) ;
+
+		filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+		input_index = rem ;
+		} ;
+
+	psrc->last_position = input_index ;
+
+	/* Save current ratio rather then target ratio. */
+	psrc->last_ratio = src_ratio ;
+
+	data->input_frames_used = filter->in_used / filter->channels ;
+	data->output_frames_gen = filter->out_gen / filter->channels ;
+
+	return SRC_ERR_NO_ERROR ;
+} /* sinc_quad_vari_process */
+
+static inline void
+calc_output_multi (SINC_FILTER *filter, increment_t increment, increment_t start_filter_index, int channels, double scale, float * output)
+{	double		fraction, left [channels], right [channels], icoeff ;
+	increment_t	filter_index, max_filter_index ;
+	int			data_index, coeff_count, indx, ch ;
+
+	/* Convert input parameters into fixed point. */
+	max_filter_index = int_to_fp (filter->coeff_half_len) ;
+
+	/* First apply the left half of the filter. */
+	filter_index = start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current - channels * coeff_count ;
+
+	memset (left, 0, sizeof (left)) ;
+
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		ch = channels ;
+		while (-- ch >= 0)
+			left [ch] += icoeff * filter->buffer [data_index + ch] ;
+
+		filter_index -= increment ;
+		data_index = data_index + channels ;
+		}
+	while (filter_index >= MAKE_INCREMENT_T (0)) ;
+
+	/* Now apply the right half of the filter. */
+	filter_index = increment - start_filter_index ;
+	coeff_count = (max_filter_index - filter_index) / increment ;
+	filter_index = filter_index + coeff_count * increment ;
+	data_index = filter->b_current + channels * (1 + coeff_count) ;
+
+	memset (right, 0, sizeof (right)) ;
+	do
+	{	fraction = fp_to_double (filter_index) ;
+		indx = fp_to_int (filter_index) ;
+
+		icoeff = filter->coeffs [indx] + fraction * (filter->coeffs [indx + 1] - filter->coeffs [indx]) ;
+
+		ch = channels ;
+		while (-- ch >= 0)
+			right [ch] += icoeff * filter->buffer [data_index + ch] ;
+
+		filter_index -= increment ;
+		data_index = data_index - channels ;
+		}
+	while (filter_index > MAKE_INCREMENT_T (0)) ;
+
+	ch = channels ;
+	while (-- ch >= 0)
+		output [ch] = scale * (left [ch] + right [ch]) ;
+
+	return ;
+} /* calc_output_multi */
+
+static int
+sinc_multichan_vari_process (SRC_PRIVATE *psrc, SRC_DATA *data)
+{	SINC_FILTER *filter ;
+	double		input_index, src_ratio, count, float_increment, terminate, rem ;
+	increment_t	increment, start_filter_index ;
+	int			half_filter_chan_len, samples_in_hand ;
+
+	if (psrc->private_data == NULL)
+		return SRC_ERR_NO_PRIVATE ;
+
+	filter = (SINC_FILTER*) psrc->private_data ;
+
+	/* If there is not a problem, this will be optimised out. */
+	if (sizeof (filter->buffer [0]) != sizeof (data->data_in [0]))
+		return SRC_ERR_SIZE_INCOMPATIBILITY ;
+
+	filter->in_count = data->input_frames * filter->channels ;
+	filter->out_count = data->output_frames * filter->channels ;
+	filter->in_used = filter->out_gen = 0 ;
+
+	src_ratio = psrc->last_ratio ;
+
+	/* Check the sample rate ratio wrt the buffer len. */
+	count = (filter->coeff_half_len + 2.0) / filter->index_inc ;
+	if (MIN (psrc->last_ratio, data->src_ratio) < 1.0)
+		count /= MIN (psrc->last_ratio, data->src_ratio) ;
+
+	/* Maximum coefficientson either side of center point. */
+	half_filter_chan_len = filter->channels * (lrint (count) + 1) ;
+
+	input_index = psrc->last_position ;
+	float_increment = filter->index_inc ;
+
+	rem = fmod_one (input_index) ;
+	filter->b_current = (filter->b_current + filter->channels * lrint (input_index - rem)) % filter->b_len ;
+	input_index = rem ;
+
+	terminate = 1.0 / src_ratio + 1e-20 ;
+
+	/* Main processing loop. */
+	while (filter->out_gen < filter->out_count)
+	{
+		/* Need to reload buffer? */
+		samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+
+		if (samples_in_hand <= half_filter_chan_len)
+		{	prepare_data (filter, data, half_filter_chan_len) ;
+
+			samples_in_hand = (filter->b_end - filter->b_current + filter->b_len) % filter->b_len ;
+			if (samples_in_hand <= half_filter_chan_len)
+				break ;
 			} ;
+
+		/* This is the termination condition. */
+		if (filter->b_real_end >= 0)
+		{	if (filter->b_current + input_index + terminate >= filter->b_real_end)
+				break ;
+			} ;
+
+		if (filter->out_count > 0 && fabs (psrc->last_ratio - data->src_ratio) > 1e-10)
+			src_ratio = psrc->last_ratio + filter->out_gen * (data->src_ratio - psrc->last_ratio) / filter->out_count ;
+
+		float_increment = filter->index_inc * 1.0 ;
+		if (src_ratio < 1.0)
+			float_increment = filter->index_inc * src_ratio ;
+
+		increment = double_to_fp (float_increment) ;
+
+		start_filter_index = double_to_fp (input_index * float_increment) ;
+
+		calc_output_multi (filter, increment, start_filter_index, filter->channels, float_increment / filter->index_inc, data->data_out + filter->out_gen) ;
+		filter->out_gen += psrc->channels ;
 
 		/* Figure out the next index. */
 		input_index += 1.0 / src_ratio ;