shithub: libsamplerate

Download patch

ref: d68834852fe69ace6213e266b4b4d7c779a65867
parent: b200f95bd88d8dd170f401c3b6b41bae8a925008
author: evpobr <evpobr@gmail.com>
date: Fri Nov 13 06:54:17 EST 2020

Refactor src_clone ()

--- a/src/common.h
+++ b/src/common.h
@@ -108,7 +108,7 @@
 	void			(*reset) (SRC_STATE *state) ;
 
 	/* State clone. */
-	SRC_ERROR		(*copy) (SRC_STATE *from, SRC_STATE *to) ;
+	SRC_STATE		*(*copy) (SRC_STATE *state) ;
 
 	/* State private_data close. */
 	void			(*close) (SRC_STATE *state) ;
--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -28,27 +28,19 @@
 SRC_STATE*
 src_clone (SRC_STATE* orig, int *error)
 {
-	SRC_STATE *state ;
-	int copy_error ;
-
+	if (!orig)
+	{
+		if (error)
+			*error = SRC_ERR_BAD_STATE ;
+		return NULL ;
+	}
 	if (error)
 		*error = SRC_ERR_NO_ERROR ;
 
-	if ((state = (SRC_STATE *) calloc (1, sizeof (SRC_STATE))) ==  NULL)
-	{	if (error)
+	SRC_STATE *state = orig->vt->copy (orig) ;
+	if (!state)
+		if (error)
 			*error = SRC_ERR_MALLOC_FAILED ;
-		return NULL ;
-		} ;
-
-	SRC_STATE *orig_state = orig ;
-	memcpy (state, orig_state, sizeof (SRC_STATE)) ;
-
-	if ((copy_error = orig_state->vt->copy (orig_state, state)) != SRC_ERR_NO_ERROR)
-	{	if (error)
-			*error = copy_error ;
-		free (state) ;
-		state = NULL ;
-		} ;
 
 	return state ;
 }
--- a/src/src_linear.c
+++ b/src/src_linear.c
@@ -17,7 +17,7 @@
 
 static SRC_ERROR linear_vari_process (SRC_STATE *state, SRC_DATA *data) ;
 static void linear_reset (SRC_STATE *state) ;
-static SRC_ERROR linear_copy (SRC_STATE *from, SRC_STATE *to) ;
+static SRC_STATE *linear_copy (SRC_STATE *state) ;
 static void linear_close (SRC_STATE *state) ;
 
 /*========================================================================================
@@ -240,29 +240,40 @@
 	return ;
 } /* linear_reset */
 
-SRC_ERROR
-linear_copy (SRC_STATE *from, SRC_STATE *to)
+SRC_STATE *
+linear_copy (SRC_STATE *state)
 {
-	if (from->private_data == NULL)
-		return SRC_ERR_NO_PRIVATE ;
+	assert (state != NULL) ;
 
-	LINEAR_DATA *to_priv = NULL ;
-	LINEAR_DATA* from_priv = (LINEAR_DATA*) from->private_data ;
-	if ((to_priv = (LINEAR_DATA*) calloc (1, sizeof (LINEAR_DATA))) == NULL)
-		return SRC_ERR_MALLOC_FAILED ;
+	if (state->private_data == NULL)
+		return NULL ;
 
+	SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
+	if (!state)
+		return NULL ;
+	memcpy (to, state, sizeof (SRC_STATE)) ;
+
+	LINEAR_DATA* from_priv = (LINEAR_DATA*) state->private_data ;
+	LINEAR_DATA *to_priv = (LINEAR_DATA *) calloc (1, sizeof (LINEAR_DATA)) ;
+	if (!to_priv)
+	{
+		free (to) ;
+		return NULL ;
+	}
+
 	memcpy (to_priv, from_priv, sizeof (LINEAR_DATA)) ;
-	to_priv->last_value = (float *) malloc (sizeof (float) * from->channels) ;
+	to_priv->last_value = (float *) malloc (sizeof (float) * state->channels) ;
 	if (!to_priv->last_value)
 	{
+		free (to) ;
 		free (to_priv) ;
-		return SRC_ERR_MALLOC_FAILED ;
+		return NULL ;
 	}
-	memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * from->channels) ;
+	memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * state->channels) ;
 
 	to->private_data = to_priv ;
 
-	return SRC_ERR_NO_ERROR ;
+	return to ;
 } /* linear_copy */
 
 static void
--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -73,7 +73,7 @@
 static SRC_ERROR prepare_data (SINC_FILTER *filter, int channels, SRC_DATA *data, int half_filter_chan_len) WARN_UNUSED ;
 
 static void sinc_reset (SRC_STATE *state) ;
-static SRC_ERROR sinc_copy (SRC_STATE *from, SRC_STATE *to) ;
+static SRC_STATE *sinc_copy (SRC_STATE *state) ;
 static void sinc_close (SRC_STATE *state) ;
 
 static SRC_STATE_VT sinc_multichan_state_vt =
@@ -314,29 +314,40 @@
 	memset (filter->buffer + filter->b_len, 0xAA, state->channels * sizeof (filter->buffer [0])) ;
 } /* sinc_reset */
 
-static SRC_ERROR
-sinc_copy (SRC_STATE *from, SRC_STATE *to)
+static SRC_STATE *
+sinc_copy (SRC_STATE *state)
 {
-	if (from->private_data == NULL)
+	assert (state != NULL) ;
+
+	if (state->private_data == NULL)
 		return SRC_ERR_NO_PRIVATE ;
 
-	SINC_FILTER *to_filter = NULL ;
-	SINC_FILTER* from_filter = (SINC_FILTER*) from->private_data ;
+	SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
+	if (!state)
+		return NULL ;
+	memcpy (to, state, sizeof (SRC_STATE)) ;
 
-	if ((to_filter = (SINC_FILTER *) calloc (1, sizeof (SINC_FILTER))) == NULL)
-		return SRC_ERR_MALLOC_FAILED ;
+
+	SINC_FILTER* from_filter = (SINC_FILTER*) state->private_data ;
+	SINC_FILTER *to_filter = (SINC_FILTER *) calloc (1, sizeof (SINC_FILTER)) ;
+	if (!to_filter)
+	{
+		free (to) ;
+		return NULL ;
+	}
 	memcpy (to_filter, from_filter, sizeof (SINC_FILTER)) ;
-	to_filter->buffer = (float *) malloc (sizeof (float) * (from_filter->b_len + from->channels)) ;
+	to_filter->buffer = (float *) malloc (sizeof (float) * (from_filter->b_len + state->channels)) ;
 	if (!to_filter->buffer)
 	{
+		free (to) ;
 		free (to_filter) ;
-		return SRC_ERR_MALLOC_FAILED ;
+		return NULL ;
 	}
-	memcpy (to_filter->buffer, from_filter->buffer, sizeof (float) * (from_filter->b_len + from->channels)) ;
+	memcpy (to_filter->buffer, from_filter->buffer, sizeof (float) * (from_filter->b_len + state->channels)) ;
 
 	to->private_data = to_filter ;
 
-	return SRC_ERR_NO_ERROR ;
+	return to ;
 } /* sinc_copy */
 
 /*========================================================================================
--- a/src/src_zoh.c
+++ b/src/src_zoh.c
@@ -17,7 +17,7 @@
 
 static SRC_ERROR zoh_vari_process (SRC_STATE *state, SRC_DATA *data) ;
 static void zoh_reset (SRC_STATE *state) ;
-static SRC_ERROR zoh_copy (SRC_STATE *from, SRC_STATE *to) ;
+static SRC_STATE *zoh_copy (SRC_STATE *state) ;
 static void zoh_close (SRC_STATE *state) ;
 
 /*========================================================================================
@@ -229,29 +229,40 @@
 	return ;
 } /* zoh_reset */
 
-static SRC_ERROR
-zoh_copy (SRC_STATE *from, SRC_STATE *to)
+static SRC_STATE *
+zoh_copy (SRC_STATE *state)
 {
-	if (from->private_data == NULL)
-		return SRC_ERR_NO_PRIVATE ;
+	assert (state != NULL) ;
 
-	ZOH_DATA *to_priv = NULL ;
-	ZOH_DATA* from_priv = (ZOH_DATA*) from->private_data ;
+	if (state->private_data == NULL)
+		return NULL ;
 
-	if ((to_priv = (ZOH_DATA *) calloc (1, sizeof (ZOH_DATA))) == NULL)
-		return SRC_ERR_MALLOC_FAILED ;
+	SRC_STATE *to = (SRC_STATE *) calloc (1, sizeof (SRC_STATE)) ;
+	if (!state)
+		return NULL ;
+	memcpy (to, state, sizeof (SRC_STATE)) ;
 
+	ZOH_DATA* from_priv = (ZOH_DATA*) state->private_data ;
+	ZOH_DATA *to_priv = (ZOH_DATA *) calloc (1, sizeof (ZOH_DATA)) ;
+	if (!to_priv)
+	{
+		free (to) ;
+		return NULL ;
+	}
+
 	memcpy (to_priv, from_priv, sizeof (ZOH_DATA)) ;
-	to_priv->last_value = (float *) malloc (sizeof (float) * from->channels) ;
+	to_priv->last_value = (float *) malloc (sizeof (float) * state->channels) ;
 	if (!to_priv->last_value)
-	{	free (to_priv) ;
-		return SRC_ERR_MALLOC_FAILED ;
-		} ;
-	memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * from->channels) ;
+	{
+		free (to) ;
+		free (to_priv) ;
+		return NULL ;
+	}
+	memcpy (to_priv->last_value, from_priv->last_value, sizeof (float) * state->channels) ;
 
 	to->private_data = to_priv ;
 
-	return SRC_ERR_NO_ERROR ;
+	return to ;
 } /* zoh_copy */
 
 static void