Hi,
I have posted a problem a few weeks ago concerning speex bits. Noone has
replied, possibly because I happened to send a HTML mail. So, here
again, hopefully is plain text:
I have run into some unexpected behavior when applying
speex_bits_write_whole_bytes multiple times on a speex_bits buffer
(moving data in parts): After first use, it messed up data in the
struct. It seems it has only been tested for writing the whole "bits"
buffer to "chars" buffer.
I have made my version of this function that can move data from
speex_bits buffer to chars buffer in parts:
(I have left out HTOLS out of my version, thus not tested, sorry for
that.) As You see, buffer data are moved from speex_bits to chars, and
charPtr and nbBits are set accordingly.
/** Like speex_bits_write_whole_bytes, but only removes the written
bytes from the stream */
int speex_bits_write_whole_nbytes(SpeexBits *bits, char *chars, int
max_nbytes)
{
int max_nchars = max_nbytes/BYTES_PER_CHAR;
int i,j;
if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))
max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);
for (i=0; i<max_nchars; i++)
chars[i] = bits->chars[i]; // copy the required bytes
for (i=0, j=max_nchars; j<bits->charPtr; i++, j++)
bits->chars[i] = (bits->chars[j]); // then move bits home
bits->chars[max_nchars]= bits->bitPtr ?
bits->chars[bits->charPtr] : 0;
bits->charPtr -= max_nchars;
bits->nbBits -= ((bits->charPtr)<<LOG2_BITS_PER_CHAR);
return max_nchars*BYTES_PER_CHAR;
}
As a reminder, Here is the original one from libspeex/bits.c:
int speex_bits_write_whole_bytes(SpeexBits *bits, char *chars, int
max_nbytes)
{
int max_nchars = max_nbytes/BYTES_PER_CHAR;
int i;
if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))
max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);
for (i=0;i<max_nchars;i++)
chars[i]=HTOLS(bits->chars[i]);
if (bits->bitPtr>0)
bits->chars[0]=bits->chars[max_nchars];
else
bits->chars[0]=0;
bits->charPtr=0;
bits->nbBits &= (BITS_PER_CHAR-1);
return max_nchars*BYTES_PER_CHAR;
}
Regards:
Tom