Commit dae5c333 authored by Koen Vos's avatar Koen Vos Committed by Jean-Marc Valin
Browse files

Added an interface to encode/decode from a CELT range coder

parent 894f1cf5
......@@ -31,6 +31,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SKP_Silk_control.h"
#include "SKP_Silk_typedef.h"
#include "SKP_Silk_errors.h"
#include "entenc.h"
#include "entdec.h"
#ifdef __cplusplus
extern "C"
......@@ -84,8 +86,8 @@ SKP_int SKP_Silk_SDK_Encode( /* O: Returns error co
const SKP_SILK_SDK_EncControlStruct *encControl, /* I: Control status */
const SKP_int16 *samplesIn, /* I: Speech sample input vector */
SKP_int nSamplesIn, /* I: Number of samples in input vector */
SKP_uint8 *outData, /* O: Encoded output vector */
SKP_int16 *nBytesOut /* I/O: Number of bytes in outData (input: Max bytes) */
ec_enc *psRangeEnc, /* I/O Compressor data structure */
SKP_int16 *nBytesOut /* I/O: Number of bytes in payload (input: Max bytes) */
);
/****************************************/
......@@ -113,7 +115,7 @@ SKP_int SKP_Silk_SDK_Decode( /* O: Returns error co
void* decState, /* I/O: State */
SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control Structure */
SKP_int lostFlag, /* I: 0: no loss, 1 loss */
const SKP_uint8 *inData, /* I: Encoded input vector */
ec_dec *psRangeDec, /* I/O Compressor data structure */
const SKP_int nBytesIn, /* I: Number of input bytes */
SKP_int16 *samplesOut, /* O: Decoded output speech vector */
SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */
......@@ -134,7 +136,7 @@ void SKP_Silk_SDK_search_for_LBRR(
/* Get table of contents for a packet */
/**************************************/
void SKP_Silk_SDK_get_TOC(
const SKP_uint8 *inData, /* I: Encoded input vector */
ec_dec *psRangeDec, /* I/O Compressor data structure */
const SKP_int16 nBytesIn, /* I: Number of input bytes */
SKP_Silk_TOC_struct *Silk_TOC /* O: Table of contents */
);
......
......@@ -6,9 +6,9 @@
/****************/
SKP_int SKP_Silk_encode_frame_FLP(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
SKP_uint8 *pCode, /* O Payload */
SKP_int16 *pnBytesOut, /* I/O Number of payload bytes; */
SKP_int16 *pnBytesOut, /* I/O Number of payload bytes; */
/* input: max length; output: used */
ec_enc *psRangeEnc, /* I/O compressor data structure */
const SKP_int16 *pIn /* I Input speech frame */
)
{
......@@ -25,8 +25,6 @@ SKP_int SKP_Silk_encode_frame_FLP(
SKP_uint8 LBRRpayload[ MAX_ARITHM_BYTES ];
SKP_int16 nBytesLBRR;
ec_byte_buffer range_enc_celt_buf;
const SKP_uint16 *FrameTermination_CDF;
TIC(ENCODE_FRAME)
......@@ -117,7 +115,7 @@ TOC(PROCESS_GAINS)
/****************************************/
nBytesLBRR = MAX_ARITHM_BYTES;
TIC(LBRR)
SKP_Silk_LBRR_encode_FLP( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw );
//SKP_Silk_LBRR_encode_FLP( psEnc, &sEncCtrl, LBRRpayload, &nBytesLBRR, xfw );
TOC(LBRR)
/*****************************************/
......@@ -148,13 +146,9 @@ TOC(NSQ)
}
/****************************************/
/* Initialize arithmetic coder */
/* Initialize range coder */
/****************************************/
if( psEnc->sCmn.nFramesInPayloadBuf == 0 ) {
ec_byte_writeinit_buffer( &range_enc_celt_buf, psEnc->sCmn.sRC.buffer, MAX_ARITHM_BYTES );
ec_enc_init( &psEnc->sCmn.sRC.range_enc_celt_state, &range_enc_celt_buf );
SKP_Silk_range_enc_init( &psEnc->sCmn.sRC );
psEnc->sCmn.nBytesInPayloadBuf = 0;
}
......@@ -162,8 +156,8 @@ TOC(NSQ)
/* Encode Parameters */
/****************************************/
TIC(ENCODE_PARAMS)
SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &sEncCtrl.sCmn, &psEnc->sCmn.sRC );
FrameTermination_CDF = SKP_Silk_FrameTermination_v4_CDF;
SKP_Silk_encode_parameters( &psEnc->sCmn, &sEncCtrl.sCmn, psRangeEnc );
FrameTermination_CDF = SKP_Silk_FrameTermination_CDF;
TOC(ENCODE_PARAMS)
/****************************************/
......@@ -178,7 +172,7 @@ TOC(ENCODE_PARAMS)
psEnc->sCmn.prevLag = sEncCtrl.sCmn.pitchL[ psEnc->sCmn.nb_subfr - 1 ];
psEnc->sCmn.first_frame_after_reset = 0;
if( psEnc->sCmn.sRC.error ) {
if( 0 ) { //psEnc->sCmn.sRC.error ) {
/* Encoder returned error: Clear payload buffer */
psEnc->sCmn.nFramesInPayloadBuf = 0;
} else {
......@@ -203,21 +197,23 @@ TOC(ENCODE_PARAMS)
}
/* Add the frame termination info to stream */
SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );
ec_encode_bin( psRangeEnc, FrameTermination_CDF[ frame_terminator ],
FrameTermination_CDF[ frame_terminator + 1 ], 16 );
for( i = 0; i < psEnc->sCmn.nFramesInPayloadBuf; i++ ) {
SKP_Silk_encode_pulses( &psEnc->sCmn.sRC, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ],
SKP_Silk_encode_pulses( psRangeEnc, psEnc->sCmn.sigtype[ i ], psEnc->sCmn.QuantOffsetType[ i ],
&psEnc->sCmn.q[ i * psEnc->sCmn.frame_length ], psEnc->sCmn.frame_length );
}
/* Payload length so far */
SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );
nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
/* Check that there is enough space in external output buffer, and move data */
if( *pnBytesOut >= nBytes ) {
SKP_int bits_in_stream, mask;
bits_in_stream = ec_enc_tell( &psEnc->sCmn.sRC.range_enc_celt_state, 0 );
ec_enc_done( &psEnc->sCmn.sRC.range_enc_celt_state );
//SKP_int bits_in_stream, mask;
//bits_in_stream = ec_enc_tell( psRangeEnc, 0 );
ec_enc_done( psRangeEnc );
#if 0
/* Fill up any remaining bits in the last byte with 1s */
if( bits_in_stream & 7 ) {
mask = SKP_RSHIFT( 0xFF, bits_in_stream & 7 );
......@@ -226,7 +222,9 @@ TOC(ENCODE_PARAMS)
}
}
SKP_memcpy( pCode, psEnc->sCmn.sRC.range_enc_celt_state.buf->buf, nBytes * sizeof( SKP_uint8 ) );
#endif
#if 0
if( frame_terminator > SKP_SILK_MORE_FRAMES &&
*pnBytesOut >= nBytes + psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes ) {
/* Get old packet and add to payload. */
......@@ -235,8 +233,9 @@ TOC(ENCODE_PARAMS)
psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes * sizeof( SKP_uint8 ) );
nBytes += psEnc->sCmn.LBRR_buffer[ LBRR_idx ].nBytes;
}
#endif
*pnBytesOut = nBytes;
/* Update FEC buffer */
SKP_memcpy( psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].payload, LBRRpayload,
nBytesLBRR * sizeof( SKP_uint8 ) );
......@@ -245,36 +244,32 @@ TOC(ENCODE_PARAMS)
psEnc->sCmn.LBRR_buffer[ psEnc->sCmn.oldest_LBRR_idx ].usage = sEncCtrl.sCmn.LBRR_usage;
psEnc->sCmn.oldest_LBRR_idx = ( ( psEnc->sCmn.oldest_LBRR_idx + 1 ) & LBRR_IDX_MASK );
/* Reset the number of frames in payload buffer */
psEnc->sCmn.nFramesInPayloadBuf = 0;
} else {
/* Not enough space: Payload will be discarded */
*pnBytesOut = 0;
nBytes = 0;
psEnc->sCmn.nFramesInPayloadBuf = 0;
ret = SKP_SILK_ENC_PAYLOAD_BUF_TOO_SHORT;
}
/* Reset the number of frames in payload buffer */
psEnc->sCmn.nFramesInPayloadBuf = 0;
} else {
/* No payload for you this time */
*pnBytesOut = 0;
/* Encode that more frames follows */
frame_terminator = SKP_SILK_MORE_FRAMES;
SKP_Silk_range_encoder( &psEnc->sCmn.sRC, frame_terminator, FrameTermination_CDF );
ec_encode_bin( psRangeEnc, FrameTermination_CDF[ frame_terminator ],
FrameTermination_CDF[ frame_terminator + 1 ], 16 );
/* Payload length so far */
SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC, &nBytes );
nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc, 0 ) + 7, 3 );
/* Take into account the q signal that isn't in the bitstream yet */
nBytes += SKP_Silk_pulses_to_bytes( &psEnc->sCmn,
&psEnc->sCmn.q[ psEnc->sCmn.nFramesInPayloadBuf * psEnc->sCmn.frame_length ] );
}
/* Check for arithmetic coder errors */
if( psEnc->sCmn.sRC.error ) {
ret = SKP_SILK_ENC_INTERNAL_ERROR;
}
/* simulate number of ms buffered in channel because of exceeding TargetRate */
psEnc->BufferedInChannel_ms += ( 8.0f * 1000.0f * ( nBytes - psEnc->sCmn.nBytesInPayloadBuf ) ) / psEnc->sCmn.TargetRate_bps;
psEnc->BufferedInChannel_ms -= SUB_FRAME_LENGTH_MS * psEnc->sCmn.nb_subfr;
......@@ -314,6 +309,7 @@ TOC(ENCODE_FRAME)
return( ret );
}
#if 0 //tmp
/* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate */
void SKP_Silk_LBRR_encode_FLP(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
......@@ -397,7 +393,7 @@ void SKP_Silk_LBRR_encode_FLP(
/****************************************/
/* Encode Parameters */
/****************************************/
SKP_Silk_encode_parameters_v4( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR );
SKP_Silk_encode_parameters( &psEnc->sCmn, &psEncCtrl->sCmn, &psEnc->sCmn.sRC_LBRR );
/****************************************/
/* Encode Parameters */
......@@ -418,7 +414,8 @@ void SKP_Silk_LBRR_encode_FLP(
frame_terminator = SKP_SILK_LAST_FRAME;
/* Add the frame termination info to stream */
SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );
ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ],
FrameTermination_CDF[ frame_terminator + 1 ], 16 );
/*********************************************/
/* Encode quantization indices of excitation */
......@@ -429,7 +426,7 @@ void SKP_Silk_LBRR_encode_FLP(
}
/* Payload length so far */
SKP_Silk_range_encoder_get_length( &psEnc->sCmn.sRC_LBRR, &nBytes );
nBytes = SKP_RSHIFT( ec_enc_tell( psRangeEnc_LBRR, 0 ) + 7, 3 );
/* Check that there is enough space in external output buffer and move data */
if( *pnBytesOut >= nBytes ) {
......@@ -458,7 +455,8 @@ void SKP_Silk_LBRR_encode_FLP(
/* Encode that more frames follows */
frame_terminator = SKP_SILK_MORE_FRAMES;
SKP_Silk_range_encoder( &psEnc->sCmn.sRC_LBRR, frame_terminator, SKP_Silk_FrameTermination_v4_CDF );
ec_encode_bin( psRangeEnc_LBRR, FrameTermination_CDF[ frame_terminator ],
FrameTermination_CDF[ frame_terminator + 1 ], 16 );
}
/* Restore original Gains */
......@@ -470,3 +468,4 @@ void SKP_Silk_LBRR_encode_FLP(
psEnc->sCmn.typeOffsetPrev = typeOffset;
}
}
#endif
\ No newline at end of file
......@@ -78,9 +78,9 @@ void SKP_Silk_HP_variable_cutoff_FLP(
/* Encoder main function */
SKP_int SKP_Silk_encode_frame_FLP(
SKP_Silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
SKP_uint8 *pCode, /* O Payload */
SKP_int16 *pnBytesOut, /* I/O Number of payload bytes; */
SKP_int16 *pnBytesOut, /* I/O Number of payload bytes; */
/* input: max length; output: used */
ec_enc *psRangeEnc, /* I/O compressor data structure */
const SKP_int16 *pIn /* I Input speech frame */
);
......
......@@ -35,34 +35,32 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* Encodes signs of excitation */
void SKP_Silk_encode_signs(
SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */
const SKP_int8 q[], /* I Pulse signal */
const SKP_int length, /* I Length of input */
const SKP_int sigtype, /* I Signal type */
const SKP_int QuantOffsetType, /* I Quantization offset type */
const SKP_int RateLevelIndex /* I Rate level index */
ec_enc *psRangeEnc, /* I/O Compressor data structure */
const SKP_int8 q[], /* I pulse signal */
const SKP_int length, /* I length of input */
const SKP_int sigtype, /* I Signal type */
const SKP_int QuantOffsetType, /* I Quantization offset type */
const SKP_int RateLevelIndex /* I Rate Level Index */
)
{
SKP_int i;
SKP_int inData;
SKP_uint16 cdf[ 3 ];
SKP_uint16 prob;
i = SKP_SMULBB( N_RATE_LEVELS - 1, SKP_LSHIFT( sigtype, 1 ) + QuantOffsetType ) + RateLevelIndex;
cdf[ 0 ] = 0;
cdf[ 1 ] = SKP_Silk_sign_CDF[ i ];
cdf[ 2 ] = 65535;
prob = 65536 - SKP_Silk_sign_CDF[ i ];
for( i = 0; i < length; i++ ) {
if( q[ i ] != 0 ) {
inData = SKP_enc_map( q[ i ] ); /* - = 0, + = 1 */
SKP_Silk_range_encoder( sRC, inData, cdf );
ec_enc_bit_prob( psRangeEnc, inData, prob );
}
}
}
/* Decodes signs of excitation */
void SKP_Silk_decode_signs(
SKP_Silk_range_coder_state *sRC, /* I/O Range coder state */
ec_dec *psRangeDec, /* I/O Compressor data structure */
SKP_int q[], /* I/O pulse signal */
const SKP_int length, /* I length of output */
const SKP_int sigtype, /* I Signal type */
......@@ -81,7 +79,7 @@ void SKP_Silk_decode_signs(
for( i = 0; i < length; i++ ) {
if( q[ i ] > 0 ) {
SKP_Silk_range_decoder( &data, sRC, cdf, 1 );
SKP_Silk_range_decoder( &data, psRangeDec, cdf, 1 );
/* attach sign */
/* implementation with shift, subtraction, multiplication */
q[ i ] *= SKP_dec_map( data );
......
......@@ -32,10 +32,10 @@ SKP_int SKP_Silk_SDK_InitDecoder(
/* Decode a frame */
SKP_int SKP_Silk_SDK_Decode(
void* decState, /* I/O: State */
SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control structure */
SKP_SILK_SDK_DecControlStruct* decControl, /* I/O: Control Structure */
SKP_int lostFlag, /* I: 0: no loss, 1 loss */
const SKP_uint8 *inData, /* I: Encoded input vector */
const SKP_int nBytesIn, /* I: Number of input Bytes */
ec_dec *psRangeDec, /* I/O Compressor data structure */
const SKP_int nBytesIn, /* I: Number of input bytes */
SKP_int16 *samplesOut, /* O: Decoded output speech vector */
SKP_int16 *nSamplesOut /* I/O: Number of samples (vector/decoded) */
)
......@@ -65,8 +65,7 @@ SKP_int SKP_Silk_SDK_Decode(
prev_fs_kHz = psDec->fs_kHz;
/* Call decoder for one frame */
ret += SKP_Silk_decode_frame( psDec, samplesOut, nSamplesOut, inData, nBytesIn,
lostFlag, &used_bytes );
ret += SKP_Silk_decode_frame( psDec, psRangeDec, samplesOut, nSamplesOut, nBytesIn, lostFlag, &used_bytes );
if( used_bytes ) { /* Only Call if not a packet loss */
if( psDec->nBytesLeft > 0 && psDec->FrameTermination == SKP_SILK_MORE_FRAMES && psDec->nFramesDecoded < 5 ) {
......@@ -132,6 +131,7 @@ SKP_int SKP_Silk_SDK_Decode(
return ret;
}
#if 0
/* Function to find LBRR information in a packet */
void SKP_Silk_SDK_search_for_LBRR(
const SKP_uint8 *inData, /* I: Encoded input vector */
......@@ -155,17 +155,16 @@ void SKP_Silk_SDK_search_for_LBRR(
sDec.nFramesDecoded = 0;
sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */
SKP_memset( sDec.prevNLSF_Q15, 0, MAX_LPC_ORDER * sizeof( SKP_int ) );
SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
/* Decode all parameter indices for the whole packet*/
SKP_Silk_decode_indices_v4( &sDec );
SKP_Silk_decode_indices( &sDec, psRangeDec );
/* Is there usable LBRR in this packet */
*nLBRRBytes = 0;
if( ( sDec.FrameTermination - 1 ) & lost_offset && sDec.FrameTermination > 0 && sDec.nBytesLeft >= 0 ) {
/* The wanted FEC is present in the packet */
for( i = 0; i < sDec.nFramesInPacket; i++ ) {
SKP_Silk_decode_parameters_v4( &sDec, &sDecCtrl, TempQ, 0 );
SKP_Silk_decode_parameters( &sDec, &sDecCtrl, psRangeDec, TempQ, 0 );
if( sDec.nBytesLeft <= 0 || sDec.sRC.error ) {
/* Corrupt stream */
......@@ -184,10 +183,12 @@ void SKP_Silk_SDK_search_for_LBRR(
}
}
}
#endif
#if 0 // todo: clean up, make efficient
/* Getting type of content for a packet */
void SKP_Silk_SDK_get_TOC(
const SKP_uint8 *inData, /* I: Encoded input vector */
ec_dec *psRangeDec, /* I/O Compressor data structure */
const SKP_int16 nBytesIn, /* I: Number of input bytes */
SKP_Silk_TOC_struct *Silk_TOC /* O: Type of content */
)
......@@ -197,10 +198,9 @@ void SKP_Silk_SDK_get_TOC(
sDec.nFramesDecoded = 0;
sDec.fs_kHz = 0; /* Force update parameters LPC_order etc */
SKP_Silk_range_dec_init( &sDec.sRC, inData, ( SKP_int32 )nBytesIn );
/* Decode all parameter indices for the whole packet*/
SKP_Silk_decode_indices_v4( &sDec );
SKP_Silk_decode_indices( &sDec );
if( sDec.nFramesInPacket > SILK_MAX_FRAMES_PER_PACKET || sDec.sRC.error ) {
/* Corrupt packet */
......@@ -222,6 +222,7 @@ void SKP_Silk_SDK_get_TOC(
}
}
}
#endif
/**************************/
/* Get the version number */
......
......@@ -33,19 +33,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/* Decode frame */
/****************/
SKP_int SKP_Silk_decode_frame(
SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */
SKP_int16 pOut[], /* O Pointer to output speech frame */
SKP_int16 *pN, /* O Pointer to size of output frame */
const SKP_uint8 pCode[], /* I Pointer to payload */
const SKP_int nBytes, /* I Payload length */
SKP_int action, /* I Action from Jitter Buffer */
SKP_int *decBytes /* O Used bytes to decode this frame */
SKP_Silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */
ec_dec *psRangeDec, /* I/O Compressor data structure */
SKP_int16 pOut[], /* O Pointer to output speech frame */
SKP_int16 *pN, /* O Pointer to size of output frame */
const SKP_int nBytes, /* I Payload length */
SKP_int action, /* I Action from Jitter Buffer */
SKP_int *decBytes /* O Used bytes to decode this frame */
)
{
SKP_Silk_decoder_control sDecCtrl;
SKP_int L, fs_Khz_old, nb_subfr_old, mv_len, ret = 0;
SKP_int Pulses[ MAX_FRAME_LENGTH ];
ec_byte_buffer range_dec_celt_buf;
TIC(decode_frame)
......@@ -66,41 +65,33 @@ TIC(decode_frame)
fs_Khz_old = psDec->fs_kHz;
nb_subfr_old = psDec->nb_subfr;
if( psDec->nFramesDecoded == 0 ) {
/* Initialize range decoder state */
/* check input */
psDec->sRC.error = 0;
if( nBytes > MAX_ARITHM_BYTES ) {
psDec->sRC.error = RANGE_CODER_DEC_PAYLOAD_TOO_LONG;
}
ec_byte_writeinit_buffer( &range_dec_celt_buf, psDec->sRC.buffer, nBytes );
SKP_memcpy( psDec->sRC.buffer, pCode, nBytes * sizeof( SKP_uint8 ) );
ec_dec_init( &psDec->sRC.range_dec_celt_state, &range_dec_celt_buf );
SKP_Silk_decode_indices_v4( psDec );
SKP_Silk_decode_indices( psDec, psRangeDec );
}
/********************************************/
/* Decode parameters and pulse signal */
/********************************************/
TIC(decode_params)
SKP_Silk_decode_parameters_v4( psDec, &sDecCtrl, Pulses, 1 );
SKP_Silk_decode_parameters( psDec, &sDecCtrl, psRangeDec, Pulses, 1 );
TOC(decode_params)
if( psDec->sRC.error ) {
if( 0 ) { //psDec->sRC.error ) {
psDec->nBytesLeft = 0;
action = 1; /* PLC operation */
SKP_Silk_decoder_set_fs( psDec, fs_Khz_old, nb_subfr_old );
/* Avoid crashing */
*decBytes = psDec->sRC.range_dec_celt_state.buf->storage;
*decBytes = psRangeDec->buf->storage;
/*
if( psDec->sRC.error == RANGE_CODER_DEC_PAYLOAD_TOO_LONG ) {
ret = SKP_SILK_DEC_PAYLOAD_TOO_LARGE;
} else {
ret = SKP_SILK_DEC_PAYLOAD_ERROR;
}
*/
} else {
*decBytes = psDec->sRC.range_dec_celt_state.buf->storage - psDec->nBytesLeft;
*decBytes = psRangeDec->buf->storage - psDec->nBytesLeft;
psDec->nFramesDecoded++;
/* Update lengths. Sampling frequency could have changed */
......
......@@ -28,31 +28,26 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SKP_Silk_main.h"
/* Decode indices from payload */
void SKP_Silk_decode_indices_v4(
SKP_Silk_decoder_state *psDec /* I/O State */
void SKP_Silk_decode_indices(
SKP_Silk_decoder_state *psDec, /* I/O State */
ec_dec *psRangeDec /* I/O Compressor data structure */
)
{
SKP_int i, k, Ix, fs_kHz_dec, nb_subfr, FrameIndex = 0, FrameTermination;
SKP_int sigtype, QuantOffsetType, seed_int, nBytesUsed;
SKP_int decode_absolute_lagIndex, delta_lagIndex, prev_lagIndex = 0;
const SKP_Silk_NLSF_CB_struct *psNLSF_CB = NULL;
SKP_Silk_range_coder_state *psRC = &psDec->sRC;
/************************/
/* Decode sampling rate */
/************************/
/* only done for first frame of packet */
if( psDec->nFramesDecoded == 0 ) {
SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset );
/* check that sampling rate is supported */
if( Ix < 0 || Ix > 3 ) {
psRC->error = RANGE_CODER_ILLEGAL_SAMPLING_RATE;
return;
}
SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_SamplingRates_CDF, SKP_Silk_SamplingRates_offset );
fs_kHz_dec = SKP_Silk_SamplingRates_table[ Ix ];
/* Convert number of subframes to index */
SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_NbSubframes_CDF, SKP_Silk_NbSubframes_offset );
SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_NbSubframes_CDF, SKP_Silk_NbSubframes_offset );
nb_subfr = (Ix + 1) << 1;
SKP_assert( nb_subfr == MAX_NB_SUBFR >> 1 || nb_subfr == MAX_NB_SUBFR );
......@@ -66,17 +61,17 @@ void SKP_Silk_decode_indices_v4(
/*******************/
/* Decode VAD flag */
/*******************/
SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRC, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset );
SKP_Silk_range_decoder( &psDec->vadFlagBuf[ FrameIndex ], psRangeDec, SKP_Silk_vadflag_CDF, SKP_Silk_vadflag_offset );
/*******************************************/
/* Decode signal type and quantizer offset */
/*******************************************/
if( FrameIndex == 0 ) {
/* first frame in packet: independent coding */
SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset );
SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_type_offset_CDF, SKP_Silk_type_offset_CDF_offset );
} else {
/* condidtional coding */
SKP_Silk_range_decoder( &Ix, psRC, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ],
SKP_Silk_range_decoder( &Ix, psRangeDec, SKP_Silk_type_offset_joint_CDF[ psDec->typeOffsetPrev ],
SKP_Silk_type_offset_CDF_offset );
}
sigtype = SKP_RSHIFT( Ix, 1 );
......@@ -89,15 +84,15 @@ void SKP_Silk_decode_indices_v4(
/* first subframe */
if( FrameIndex == 0 ) {
/* first frame in packet: independent coding */
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset );
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRangeDec, SKP_Silk_gain_CDF[ sigtype ], SKP_Silk_gain_CDF_offset );
} else {
/* condidtional coding */
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ 0 ], psRangeDec, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
}
/* remaining subframes */
for( i = 1; i < psDec->nb_subfr; i++ ) {
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRC, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
SKP_Silk_range_decoder( &psDec->GainsIndices[ FrameIndex ][ i ], psRangeDec, SKP_Silk_delta_gain_CDF, SKP_Silk_delta_gain_CDF_offset );
}
/**********************/
......@@ -108,12 +103,14 @@ void SKP_Silk_decode_indices_v4(
psNLSF_CB = psDec->psNLSF_CB[ sigtype ];
/* Arithmetically decode NLSF path */
SKP_Silk_range_decoder_multi( psDec->NLSFIndices[ FrameIndex ], psRC, psNLSF_CB->StartPtr, psNLSF_CB->MiddleIx, psNLSF_CB->nStages );
for( i = 0; i < psNLSF_CB->nStages; i++ ) {
SKP_Silk_range_decoder( &psDec->NLSFIndices[ FrameIndex ][ i ], psRangeDec, psNLSF_CB->StartPtr[ i ], psNLSF_CB->MiddleIx[ i ] );
}
/***********************************/
/* Decode LSF interpolation factor */
/***********************************/
SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRC, SKP_Silk_NLSF_interpolation_factor_CDF,
SKP_Silk_range_decoder( &psDec->NLSFInterpCoef_Q2[ FrameIndex ], psRangeDec, SKP_Silk_NLSF_interpolation_factor_CDF,
SKP_Silk_NLSF_interpolation_factor_offset );
if( sigtype == SIG_TYPE_VOICED ) {
......@@ -124,7 +121,7 @@ void SKP_Silk_decode_indices_v4(
decode_absolute_lagIndex = 1;
if( FrameIndex > 0 && psDec->sigtype[ FrameIndex - 1 ] == SIG_TYPE_VOICED ) {
/* Decode Delta index */
SKP_Silk_range_decoder( &delta_lagIndex,psRC, SKP_Silk_pitch_delta_CDF, SKP_Silk_pitch_delta_CDF_offset );
SKP_Silk_range_decoder( &delta_lagIndex, psRangeDec, SKP_Silk_pitch_delta_CDF, SKP_Silk_pitch_delta_CDF_offset );
if( delta_lagIndex < ( MAX_DELTA_LAG << 1 ) + 1 ) {
delta_lagIndex = delta_lagIndex - MAX_DELTA_LAG;
psDec->lagIndex[ FrameIndex ] = prev_lagIndex + delta_lagIndex;
......@@ -134,13 +131,13 @@ void SKP_Silk_decode_indices_v4(
if( decode_absolute_lagIndex ) {
/* Absolute decoding */
if( psDec->fs_kHz == 8 ) {
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset );
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_NB_CDF, SKP_Silk_pitch_lag_NB_CDF_offset );
} else if( psDec->fs_kHz == 12 ) {
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset );
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_MB_CDF, SKP_Silk_pitch_lag_MB_CDF_offset );
} else if( psDec->fs_kHz == 16 ) {
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset );
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_WB_CDF, SKP_Silk_pitch_lag_WB_CDF_offset );
} else {
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRC, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset );
SKP_Silk_range_decoder( &psDec->lagIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_lag_SWB_CDF, SKP_Silk_pitch_lag_SWB_CDF_offset );
}
}
prev_lagIndex = psDec->lagIndex[ FrameIndex ];
......@@ -148,38 +145,38 @@ void SKP_Silk_decode_indices_v4(
/* Get countour index */
if( psDec->fs_kHz == 8 ) {
/* Less codevectors used in 8 khz mode */
SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset );
SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_contour_NB_CDF, SKP_Silk_pitch_contour_NB_CDF_offset );
} else {
/* Joint for 12, 16, and 24 khz */
SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRC, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset );
SKP_Silk_range_decoder( &psDec->contourIndex[ FrameIndex ], psRangeDec, SKP_Silk_pitch_contour_CDF, SKP_Silk_pitch_contour_CDF_offset );
}
/********************/
/* Decode LTP gains */
/********************/