[Speex-dev] Multiple Frames per Packet

David Barrett dbarrett
Thu Jul 29 13:28:21 PDT 2004


Hi, I'm using Speex and I want to pack multiple frames into a single
packet.  The manual (section 4.5) says to call "speex_encode()"
multiple times before calling "speex_bits_write()", and then call
"speex_decode()" until it returns -1.  However, when I try that
"speex_decode()" never returns -1, and it enters an infinite loop.

I'm using 1.0.4.

Any suggestions?  Surely this isn't a new question.  (Incidentally,
the Xiph.org archives of this mailing list appear to be offline.
Where are they now, or when will they come back online?)

I've included some clips from my code below:

-david

---------------

// encode
void GSpeexEncoder::encode( const short* from, int fromSize,
vector<char>& to )
{
GASSERTPUB( );
GASSERTPRE( from && fromSize );
// First convert to floating-point data
static vector<float> fromFloat;
fromFloat.resize( fromSize );
const short* src = from;
float* dst = &fromFloat[0];
int c = fromSize;
while( c-- ) { *dst++ = (float)*src++; }

// Now encode as many times as many frames as are stored
speex_bits_reset( &_bits );
GASSERT( !( fromSize % _frameSize ) );
for( int frame = 0; frame < fromSize / _frameSize; ++frame )
// Encode the frame
speex_encode( _enc_state, &fromFloat[ frame *
_frameSize ], &_bits);

// And copy to the output buffer
to.resize( speex_bits_nbytes( &_bits ) );
GASSERTRET( to.size( ) == speex_bits_write( &_bits, &to[0],
to.size( ) ) );
}



// decode
void GSpeexDecoder::decode( const char* from, int fromSize,
vector<short>& to )
{
GASSERTPUB( );
GASSERTPRE( from && fromSize );
// Read the input
speex_bits_read_from( &_bits, (char*)from, fromSize );

// Decode
static vector<float> toFloat;
int frame = 0;
do
{
// Allocate more data, if needed
if( (frame+1)*_frameSize > (int)toFloat.size( ) )
// Grow as _frameSize * 2^n
if( !toFloat.size( ) ) toFloat.resize(
_frameSize );
else                   toFloat.resize(
toFloat.size( ) * 2 );
}
while( !speex_decode( _dec_state, &_bits, &toFloat[ frame++ *
_frameSize ] ) );

// Finally, convert to back to short data
to.resize( frame * _frameSize );
const float* src = &toFloat[0];
short* dst = &to[0];
int c = to.size( );
while( c-- ) { *dst++ = (short)*src++; }
}



More information about the Speex-dev mailing list