[PATCH] resample: Fix input indexing bug from interleaved functions

<[email protected]> Wed, 2 May 2012 17:28:08 +0300
Newsgroups gmane.comp.audio.compression.speex.devel
Message-ID <[email protected]>
From: Jyri Sarha <[email protected]>

This bug happens quite often when resampling from a low to a high
sample-rate with big enough factor. Also the resampling call has to
be limited by the output buffer size and some unused samples needs be
left in the input buffer.

Sometimes when up-sampling with a big factor the resampling function
wants to peek one more sample from the input buffer to produce one
more output sample, but the inputs sample is not consumed because it
is also needed to produce the next output sample. When resampling the
next channel this peeked sample is not any more available because
*in_len parameter value was updated when resampling the first channel.

Signed-off-by: Jyri Sarha <[email protected]>
---
 libspeex/resample.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/libspeex/resample.c b/libspeex/resample.c
index 7957c61..d59508d 100644
--- a/libspeex/resample.c
+++ b/libspeex/resample.c
@@ -970,13 +970,15 @@ EXPORT int speex_resampler_process_interleaved_float(SpeexResamplerState *st, co
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
-   spx_uint32_t bak_len = *out_len;
+   spx_uint32_t bak_out_len = *out_len;
+   spx_uint32_t bak_in_len = *in_len;
    istride_save = st->in_stride;
    ostride_save = st->out_stride;
    st->in_stride = st->out_stride = st->nb_channels;
    for (i=0;i<st->nb_channels;i++)
    {
-      *out_len = bak_len;
+      *out_len = bak_out_len;
+      *in_len = bak_in_len;
       if (in != NULL)
          speex_resampler_process_float(st, i, in+i, in_len, out+i, out_len);
       else
@@ -991,13 +993,15 @@ EXPORT int speex_resampler_process_interleaved_int(SpeexResamplerState *st, cons
 {
    spx_uint32_t i;
    int istride_save, ostride_save;
-   spx_uint32_t bak_len = *out_len;
+   spx_uint32_t bak_out_len = *out_len;
+   spx_uint32_t bak_in_len = *in_len;
    istride_save = st->in_stride;
    ostride_save = st->out_stride;
    st->in_stride = st->out_stride = st->nb_channels;
    for (i=0;i<st->nb_channels;i++)
    {
-      *out_len = bak_len;
+      *out_len = bak_out_len;
+      *in_len = bak_in_len;
       if (in != NULL)
          speex_resampler_process_int(st, i, in+i, in_len, out+i, out_len);
       else
-- 
1.7.4.1