4646 Smith, Julius O. Digital Audio Resampling Home Page
4747 Center for Computer Research in Music and Acoustics (CCRMA),
4848 Stanford University, 2007.
49- Web published at http ://ccrma.stanford.edu/~jos/resample/.
49+ Web published at https ://ccrma.stanford.edu/~jos/resample/.
5050
5151 There is one main difference, though. This resampler uses cubic
5252 interpolation instead of linear interpolation in the above paper. This
6363
6464#ifdef OUTSIDE_SPEEX
6565#include <stdlib.h>
66- static void * speex_alloc (int size ) {return calloc (size ,1 );}
67- static void * speex_realloc (void * ptr , int size ) {return realloc (ptr , size );}
68- static void speex_free (void * ptr ) {free (ptr );}
66+ static void * speex_alloc (int size ) {return calloc (size ,1 );}
67+ static void * speex_realloc (void * ptr , int size ) {return realloc (ptr , size );}
68+ static void speex_free (void * ptr ) {free (ptr );}
69+ #ifndef EXPORT
70+ #define EXPORT
71+ #endif
6972#include "speex_resampler.h"
7073#include "arch.h"
7174#else /* OUTSIDE_SPEEX */
@@ -75,7 +78,6 @@ static void speex_free (void *ptr) {free(ptr);}
7578#include "os_support.h"
7679#endif /* OUTSIDE_SPEEX */
7780
78- #include "stack_alloc.h"
7981#include <math.h>
8082#include <limits.h>
8183
@@ -91,18 +93,18 @@ static void speex_free (void *ptr) {free(ptr);}
9193#endif
9294
9395#ifndef UINT32_MAX
94- #define UINT32_MAX 4294967296U
96+ #define UINT32_MAX 4294967295U
9597#endif
9698
97- #ifdef _USE_SSE
99+ #ifdef USE_SSE
98100#include "resample_sse.h"
99101#endif
100102
101- #ifdef _USE_NEON
103+ #ifdef USE_NEON
102104#include "resample_neon.h"
103105#endif
104106
105- /* Numer of elements to allocate on the stack */
107+ /* Number of elements to allocate on the stack */
106108#ifdef VAR_ARRAYS
107109#define FIXED_STACK_ALLOC 8192
108110#else
@@ -194,16 +196,14 @@ struct FuncDef {
194196 int oversample ;
195197};
196198
197- static const struct FuncDef _KAISER12 = {kaiser12_table , 64 };
198- #define KAISER12 (&_KAISER12)
199- /*static struct FuncDef _KAISER12 = {kaiser12_table, 32};
200- #define KAISER12 (&_KAISER12)*/
201- static const struct FuncDef _KAISER10 = {kaiser10_table , 32 };
202- #define KAISER10 (&_KAISER10)
203- static const struct FuncDef _KAISER8 = {kaiser8_table , 32 };
204- #define KAISER8 (&_KAISER8)
205- static const struct FuncDef _KAISER6 = {kaiser6_table , 32 };
206- #define KAISER6 (&_KAISER6)
199+ static const struct FuncDef kaiser12_funcdef = {kaiser12_table , 64 };
200+ #define KAISER12 (&kaiser12_funcdef)
201+ static const struct FuncDef kaiser10_funcdef = {kaiser10_table , 32 };
202+ #define KAISER10 (&kaiser10_funcdef)
203+ static const struct FuncDef kaiser8_funcdef = {kaiser8_table , 32 };
204+ #define KAISER8 (&kaiser8_funcdef)
205+ static const struct FuncDef kaiser6_funcdef = {kaiser6_table , 32 };
206+ #define KAISER6 (&kaiser6_funcdef)
207207
208208struct QualityMapping {
209209 int base_length ;
@@ -473,7 +473,7 @@ static int resampler_basic_interpolate_single(SpeexResamplerState *st, spx_uint3
473473 }
474474
475475 cubic_coef (frac , interp );
476- sum = MULT16_32_Q15 (interp [0 ],SHR32 ( accum [0 ], 1 )) + MULT16_32_Q15 (interp [1 ],SHR32 ( accum [1 ], 1 )) + MULT16_32_Q15 (interp [2 ],SHR32 ( accum [2 ], 1 )) + MULT16_32_Q15 (interp [3 ],SHR32 ( accum [3 ], 1 ) );
476+ sum = MULT16_32_Q15 (interp [0 ],accum [0 ]) + MULT16_32_Q15 (interp [1 ],accum [1 ]) + MULT16_32_Q15 (interp [2 ],accum [2 ]) + MULT16_32_Q15 (interp [3 ],accum [3 ]);
477477 sum = SATURATE32PSHR (sum , 15 , 32767 );
478478#else
479479 cubic_coef (frac , interp );
@@ -572,6 +572,7 @@ static int resampler_basic_zero(SpeexResamplerState *st, spx_uint32_t channel_in
572572 const int frac_advance = st -> frac_advance ;
573573 const spx_uint32_t den_rate = st -> den_rate ;
574574
575+ (void )in ;
575576 while (!(last_sample >= (spx_int32_t )* in_len || out_sample >= (spx_int32_t )* out_len ))
576577 {
577578 out [out_stride * out_sample ++ ] = 0 ;
@@ -589,16 +590,15 @@ static int resampler_basic_zero(SpeexResamplerState *st, spx_uint32_t channel_in
589590 return out_sample ;
590591}
591592
592- static int _muldiv (spx_uint32_t * result , spx_uint32_t value , spx_uint32_t mul , spx_uint32_t div )
593+ static int multiply_frac (spx_uint32_t * result , spx_uint32_t value , spx_uint32_t num , spx_uint32_t den )
593594{
594- speex_assert (result );
595- spx_uint32_t major = value / div ;
596- spx_uint32_t remainder = value % div ;
595+ spx_uint32_t major = value / den ;
596+ spx_uint32_t remain = value % den ;
597597 /* TODO: Could use 64 bits operation to check for overflow. But only guaranteed in C99+ */
598- if (remainder > UINT32_MAX / mul || major > UINT32_MAX / mul
599- || major * mul > UINT32_MAX - remainder * mul / div )
598+ if (remain > UINT32_MAX / num || major > UINT32_MAX / num
599+ || major * num > UINT32_MAX - remain * num / den )
600600 return RESAMPLER_ERR_OVERFLOW ;
601- * result = remainder * mul / div + major * mul ;
601+ * result = remain * num / den + major * num ;
602602 return RESAMPLER_ERR_SUCCESS ;
603603}
604604
@@ -619,7 +619,7 @@ static int update_filter(SpeexResamplerState *st)
619619 {
620620 /* down-sampling */
621621 st -> cutoff = quality_map [st -> quality ].downsample_bandwidth * st -> den_rate / st -> num_rate ;
622- if (_muldiv (& st -> filt_len ,st -> filt_len ,st -> num_rate ,st -> den_rate ) != RESAMPLER_ERR_SUCCESS )
622+ if (multiply_frac (& st -> filt_len ,st -> filt_len ,st -> num_rate ,st -> den_rate ) != RESAMPLER_ERR_SUCCESS )
623623 goto fail ;
624624 /* Round up to make sure we have a multiple of 8 for SSE */
625625 st -> filt_len = ((st -> filt_len - 1 )& (~0x7 ))+ 8 ;
@@ -638,12 +638,12 @@ static int update_filter(SpeexResamplerState *st)
638638 st -> cutoff = quality_map [st -> quality ].upsample_bandwidth ;
639639 }
640640
641- /* Choose the resampling type that requires the least amount of memory */
642641#ifdef RESAMPLE_FULL_SINC_TABLE
643642 use_direct = 1 ;
644643 if (INT_MAX /sizeof (spx_word16_t )/st -> den_rate < st -> filt_len )
645644 goto fail ;
646645#else
646+ /* Choose the resampling type that requires the least amount of memory */
647647 use_direct = st -> filt_len * st -> den_rate <= st -> filt_len * st -> oversample + 8
648648 && INT_MAX /sizeof (spx_word16_t )/st -> den_rate >= st -> filt_len ;
649649#endif
@@ -733,34 +733,37 @@ static int update_filter(SpeexResamplerState *st)
733733 {
734734 spx_uint32_t j ;
735735 spx_uint32_t olen = old_length ;
736+ spx_uint32_t start = i * st -> mem_alloc_size ;
737+ spx_uint32_t magic_samples = st -> magic_samples [i ];
736738 /*if (st->magic_samples[i])*/
737739 {
738740 /* Try and remove the magic samples as if nothing had happened */
739741
740742 /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */
741- olen = old_length + 2 * st -> magic_samples [ i ] ;
742- for (j = old_length - 1 + st -> magic_samples [ i ] ;j -- ;)
743- st -> mem [i * st -> mem_alloc_size + j + st -> magic_samples [ i ] ] = st -> mem [i * old_alloc_size + j ];
744- for (j = 0 ;j < st -> magic_samples [ i ] ;j ++ )
745- st -> mem [i * st -> mem_alloc_size + j ] = 0 ;
743+ olen = old_length + 2 * magic_samples ;
744+ for (j = old_length - 1 + magic_samples ;j -- ;)
745+ st -> mem [start + j + magic_samples ] = st -> mem [i * old_alloc_size + j ];
746+ for (j = 0 ;j < magic_samples ;j ++ )
747+ st -> mem [start + j ] = 0 ;
746748 st -> magic_samples [i ] = 0 ;
747749 }
748750 if (st -> filt_len > olen )
749751 {
750752 /* If the new filter length is still bigger than the "augmented" length */
751753 /* Copy data going backward */
752754 for (j = 0 ;j < olen - 1 ;j ++ )
753- st -> mem [i * st -> mem_alloc_size + (st -> filt_len - 2 - j )] = st -> mem [i * st -> mem_alloc_size + (olen - 2 - j )];
755+ st -> mem [start + (st -> filt_len - 2 - j )] = st -> mem [start + (olen - 2 - j )];
754756 /* Then put zeros for lack of anything better */
755757 for (;j < st -> filt_len - 1 ;j ++ )
756- st -> mem [i * st -> mem_alloc_size + (st -> filt_len - 2 - j )] = 0 ;
758+ st -> mem [start + (st -> filt_len - 2 - j )] = 0 ;
757759 /* Adjust last_sample */
758760 st -> last_sample [i ] += (st -> filt_len - olen )/2 ;
759761 } else {
760762 /* Put back some of the magic! */
761- st -> magic_samples [i ] = (olen - st -> filt_len )/2 ;
762- for (j = 0 ;j < st -> filt_len - 1 + st -> magic_samples [i ];j ++ )
763- st -> mem [i * st -> mem_alloc_size + j ] = st -> mem [i * st -> mem_alloc_size + j + st -> magic_samples [i ]];
763+ magic_samples = (olen - st -> filt_len )/2 ;
764+ for (j = 0 ;j < st -> filt_len - 1 + magic_samples ;j ++ )
765+ st -> mem [start + j ] = st -> mem [start + j + magic_samples ];
766+ st -> magic_samples [i ] = magic_samples ;
764767 }
765768 }
766769 } else if (st -> filt_len < old_length )
@@ -977,8 +980,7 @@ EXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t cha
977980 const spx_uint32_t xlen = st -> mem_alloc_size - (st -> filt_len - 1 );
978981#ifdef VAR_ARRAYS
979982 const unsigned int ylen = (olen < FIXED_STACK_ALLOC ) ? olen : FIXED_STACK_ALLOC ;
980- VARDECL (spx_word16_t * ystack );
981- ALLOC (ystack , ylen , spx_word16_t );
983+ spx_word16_t ystack [ylen ];
982984#else
983985 const unsigned int ylen = FIXED_STACK_ALLOC ;
984986 spx_word16_t ystack [FIXED_STACK_ALLOC ];
@@ -1093,7 +1095,7 @@ EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_r
10931095 * out_rate = st -> out_rate ;
10941096}
10951097
1096- static inline spx_uint32_t _gcd (spx_uint32_t a , spx_uint32_t b )
1098+ static inline spx_uint32_t compute_gcd (spx_uint32_t a , spx_uint32_t b )
10971099{
10981100 while (b != 0 )
10991101 {
@@ -1123,7 +1125,7 @@ EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t r
11231125 st -> num_rate = ratio_num ;
11241126 st -> den_rate = ratio_den ;
11251127
1126- fact = _gcd (st -> num_rate , st -> den_rate );
1128+ fact = compute_gcd (st -> num_rate , st -> den_rate );
11271129
11281130 st -> num_rate /= fact ;
11291131 st -> den_rate /= fact ;
@@ -1132,7 +1134,7 @@ EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t r
11321134 {
11331135 for (i = 0 ;i < st -> nb_channels ;i ++ )
11341136 {
1135- if (_muldiv (& st -> samp_frac_num [i ],st -> samp_frac_num [i ],st -> den_rate ,old_den ) != RESAMPLER_ERR_SUCCESS )
1137+ if (multiply_frac (& st -> samp_frac_num [i ],st -> samp_frac_num [i ],st -> den_rate ,old_den ) != RESAMPLER_ERR_SUCCESS )
11361138 return RESAMPLER_ERR_OVERFLOW ;
11371139 /* Safety net */
11381140 if (st -> samp_frac_num [i ] >= st -> den_rate )
0 commit comments