/****************************************************************** iLBC Speech Coder ANSI-C Source Code iLBC_encode.c Copyright (c) 2001, Global IP Sound AB. All rights reserved. ******************************************************************/ #include #include #include "iLBC_define.h" #include "LPCencode.h" #include "FrameClassify.h" #include "StateSearchW.h" #include "StateConstructW.h" #include "helpfun.h" #include "constants.h" #include "packing.h" #include "iLBC_encode.h" #include "iCBSearch.h" #include "iCBConstruct.h" #include "hpInput.h" #include "anaFilter.h" #include "syntFilter.h" /*----------------------------------------------------------------* * Initiation of encoder instance. *---------------------------------------------------------------*/ short initEncode( /* (o) Number of bytes encoded */ iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) Encoder instance */ ){ memset((*iLBCenc_inst).anaMem, 0, LPC_FILTERORDER*sizeof(float)); memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl, LPC_FILTERORDER*sizeof(float)); memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl, LPC_FILTERORDER*sizeof(float)); memset((*iLBCenc_inst).lpc_buffer, 0, LPC_LOOKBACK*sizeof(float)); memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float)); return (NO_OF_BYTES); } /*----------------------------------------------------------------* * main encoder function *---------------------------------------------------------------*/ void iLBC_encode( unsigned char *bytes, /* (o) encoded data bits iLBC */ float *block, /* (o) speech vector to encode */ iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder state */ ){ float data[BLOCKL]; float residual[BLOCKL], reverseResidual[BLOCKL]; int start, idxForMax, idxVec[STATE_LEN]; float reverseDecresidual[BLOCKL], mem[CB_MEML]; int n, k, meml_gotten, Nfor, Nback, i, pos; int gain_index[CB_NSTAGES*NASUB], extra_gain_index[CB_NSTAGES]; int cb_index[CB_NSTAGES*NASUB],extra_cb_index[CB_NSTAGES]; int lsf_i[LSF_NSPLIT*LPC_N]; unsigned char *pbytes; int diff, start_pos, state_first; float en1, en2; int index, ulp, firstpart; int subcount, subframe; float weightState[LPC_FILTERORDER]; float syntdenum[NSUB*(LPC_FILTERORDER+1)]; float weightdenum[NSUB*(LPC_FILTERORDER+1)]; float decresidual[BLOCKL]; /* high pass filtering of input signal if such is not done prior to calling this function */ /*hpInput(block, BLOCKL, data, (*iLBCenc_inst).hpimem);*/ /* otherwise simply copy */ memcpy(data,block,BLOCKL*sizeof(float)); /* LPC of hp filtered input data */ LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst); /* inverse filter to get residual */ for (n=0; n en2) { state_first = 1; start_pos = (start-1)*SUBL; } else { state_first = 0; start_pos = (start-1)*SUBL + diff; } /* scalar quantization of state */ StateSearchW(&residual[start_pos], &syntdenum[(start-1)*(LPC_FILTERORDER+1)], &weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax, idxVec, STATE_SHORT_LEN, state_first); StateConstructW(idxForMax, idxVec, &syntdenum[(start-1)*(LPC_FILTERORDER+1)], &decresidual[start_pos], STATE_SHORT_LEN); /* predictive quantization in state */ if (state_first) { /* put adaptive part in the end */ /* setup memory */ memset(mem, 0, (CB_MEML-STATE_SHORT_LEN)*sizeof(float)); memcpy(mem+CB_MEML-STATE_SHORT_LEN, decresidual+start_pos, STATE_SHORT_LEN*sizeof(float)); memset(weightState, 0, LPC_FILTERORDER*sizeof(float)); /* encode subframes */ iCBSearch(extra_cb_index, extra_gain_index, &residual[start_pos+STATE_SHORT_LEN], mem+CB_MEML-stMemLTbl, stMemLTbl, diff, CB_NSTAGES, &weightdenum[start*(LPC_FILTERORDER+1)], weightState, 0); /* construct decoded vector */ iCBConstruct(&decresidual[start_pos+STATE_SHORT_LEN], extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl, diff, CB_NSTAGES); } else { /* put adaptive part in the beginning */ /* create reversed vectors for prediction */ for(k=0; k 0 ){ /* setup memory */ memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float)); memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL, STATE_LEN*sizeof(float)); memset(weightState, 0, LPC_FILTERORDER*sizeof(float)); /* loop over subframes to encode */ for (subframe=0; subframe 0 ){ /* create reverse order vectors */ for( n=0; n CB_MEML ) { meml_gotten=CB_MEML; } for( k=0; k