diff options
author | Wim Taymans <wtaymans@redhat.com> | 2016-02-03 17:01:35 +0300 |
---|---|---|
committer | Tristan Matthews <tmatth@videolan.org> | 2016-03-06 20:33:46 +0300 |
commit | d08a14462744654f7097f17943d8e2ddc0dfe93e (patch) | |
tree | 05df298766e5e8295b1d6d9a038e903eb4e36a11 | |
parent | cc4110de6fa597c6de0186985c19868fd06d6319 (diff) |
resample: Improve GCD calculation
Use Euclids algorithm to calculate the greatest common divisor to
simplify the resample ratio fraction instead of the slow iterative
method.
Signed-off-by: Tristan Matthews <tmatth@videolan.org>
-rw-r--r-- | libspeexdsp/resample.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/libspeexdsp/resample.c b/libspeexdsp/resample.c index 4e47d67..e69b309 100644 --- a/libspeexdsp/resample.c +++ b/libspeexdsp/resample.c @@ -1068,6 +1068,18 @@ EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_r *out_rate = st->out_rate; } +static inline spx_uint32_t _gcd(spx_uint32_t a, spx_uint32_t b) +{ + while (b != 0) + { + spx_uint32_t temp = a; + + a = b; + b = temp % b; + } + return a; +} + EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate) { spx_uint32_t fact; @@ -1081,15 +1093,11 @@ EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t r st->out_rate = out_rate; st->num_rate = ratio_num; st->den_rate = ratio_den; - /* FIXME: This is terribly inefficient, but who cares (at least for now)? */ - for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++) - { - while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) - { - st->num_rate /= fact; - st->den_rate /= fact; - } - } + + fact = _gcd (st->num_rate, st->den_rate); + + st->num_rate /= fact; + st->den_rate /= fact; if (old_den > 0) { |