<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#330000">
<tt>Hi,<br>
<br>
I have run into some unexpected behavior when applying </tt><tt>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.<br>
<br>
I have made my version of this function that can move data from
speex_bits buffer to chars buffer in parts:<br>
(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.<br>
</tt><tt><br>
/** Like speex_bits_write_whole_bytes, but only removes the written
bytes from the stream */<br>
int speex_bits_write_whole_nbytes(SpeexBits *bits, char *chars, int
max_nbytes)<br>
{<br>
int max_nchars = max_nbytes/BYTES_PER_CHAR;<br>
int i,j;<br>
if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))<br>
max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);<br>
for (i=0; i<max_nchars; i++)<br>
chars[i] = bits->chars[i]; // copy the
required bytes<br>
for (i=0, j=max_nchars; j<bits->charPtr; i++, j++)<br>
bits->chars[i] = (bits->chars[j]); // then move
bits home<br>
bits->chars[max_nchars]= bits->bitPtr ?
bits->chars[bits->charPtr] : 0;<br>
bits->charPtr -= max_nchars;<br>
bits->nbBits -= ((bits->charPtr)<<LOG2_BITS_PER_CHAR);<br>
return max_nchars*BYTES_PER_CHAR;<br>
}<br>
<br>
As a reminder, Here is the original one from libspeex/bits.c:<br>
<br>
int speex_bits_write_whole_bytes(SpeexBits *bits, char *chars, int
max_nbytes)<br>
{<br>
int max_nchars = max_nbytes/BYTES_PER_CHAR;<br>
int i;<br>
if (max_nchars > ((bits->nbBits)>>LOG2_BITS_PER_CHAR))<br>
max_nchars = ((bits->nbBits)>>LOG2_BITS_PER_CHAR);<br>
for (i=0;i<max_nchars;i++)<br>
chars[i]=HTOLS(bits->chars[i]);<br>
<br>
if (bits->bitPtr>0)<br>
bits->chars[0]=bits->chars[max_nchars];<br>
else<br>
bits->chars[0]=0;<br>
bits->charPtr=0;<br>
bits->nbBits &= (BITS_PER_CHAR-1);<br>
return max_nchars*BYTES_PER_CHAR;<br>
}<br>
<br>
Regards:<br>
Tom<br>
<br>
</tt>
</body>
</html>