📄 owng723.c
字号:
/*/////////////////////////////////////////////////////////////////////////////
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2005 Intel Corporation. All Rights Reserved.
//
// Intel(R) Integrated Performance Primitives
// USC - Unified Speech Codec interface library
//
// By downloading and installing USC codec, you hereby agree that the
// accompanying Materials are being provided to you under the terms and
// conditions of the End User License Agreement for the Intel(R) Integrated
// Performance Primitives product previously accepted by you. Please refer
// to the file ipplic.htm located in the root directory of your Intel(R) IPP
// product installation for more information.
//
// A speech coding standards promoted by ITU, ETSI, 3GPP and other
// organizations. Implementations of these standards, or the standard enabled
// platforms may require licenses from various entities, including
// Intel Corporation.
//
//
// Purpose: G.723.1 speech codec: internal functions and tables.
//
*/
#include <stdlib.h>
#include <stdio.h>
#include "owng723.h"
typedef enum {
G723_Rate63Frame = 0,
G723_Rate53Frame,
G723_SIDFrame,
G723_UntransmittedFrame
}G723_Info_Bit;
static int ExtractBits( const char **pBitStrm, int *currBitstrmOffset, int numParamBits )
{
int i ;
int unpackedParam = 0 ;
for ( i = 0 ; i < numParamBits ; i ++ ){
int lTemp;
lTemp = ((*pBitStrm)[(i + *currBitstrmOffset)>>3] >> ((i + *currBitstrmOffset) & 0x7)) & 1;
unpackedParam += lTemp << i ;
}
*pBitStrm += (numParamBits + *currBitstrmOffset)>>3;
*currBitstrmOffset = (numParamBits + *currBitstrmOffset) & 0x7;
return unpackedParam ;
}
void GetParamFromBitstream( const char *pSrcBitStream, ParamStream_G723 *Params)
{
int i;
int lTmp ;
short InfoBit;
short Bound_AcGn;
const char **pBitStream = &pSrcBitStream;
int nBit = 0;
/* Extract the frame type and rate info */
InfoBit = ExtractBits(pBitStream, &nBit, 2);
if ( InfoBit == G723_UntransmittedFrame ) {
Params->FrameType = G723_UntransmittedFrm; Params->lLSPIdx = 0;
return;
}
if ( InfoBit == G723_SIDFrame ) { /* SID frame*/
Params->lLSPIdx = ExtractBits(pBitStream, &nBit, 24);/* LspId */
Params->sAmpIndex[0] = ExtractBits(pBitStream, &nBit, 6); /* Gain */
Params->FrameType = G723_SIDFrm; return;
}
if ( InfoBit == G723_Rate53Frame ) {
Params->FrameType = G723_ActiveFrm;
Params->currRate = G723_Rate53;
Params->lLSPIdx = ExtractBits(pBitStream, &nBit, 24);/* LspId */
Params->PitchLag[0] = ExtractBits(pBitStream, &nBit, 7);
Params->AdCdbkLag[1] = ExtractBits(pBitStream, &nBit, 2);
Params->PitchLag[1] = ExtractBits(pBitStream, &nBit, 7);
Params->AdCdbkLag[3] = ExtractBits(pBitStream, &nBit, 2);
Params->AdCdbkLag[0] = Params->AdCdbkLag[2] = 1;
Params->AdCdbkGain[0] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[1] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[2] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[3] = ExtractBits(pBitStream, &nBit, 12);
Params->sGrid[0] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[1] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[2] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[3] = ExtractBits(pBitStream, &nBit, 1);
Params->sPosition[0] = ExtractBits(pBitStream, &nBit, 12);
Params->sPosition[1] = ExtractBits(pBitStream, &nBit, 12);
Params->sPosition[2] = ExtractBits(pBitStream, &nBit, 12);
Params->sPosition[3] = ExtractBits(pBitStream, &nBit, 12);
Params->sAmplitude[0] = ExtractBits(pBitStream, &nBit, 4);
Params->sAmplitude[1] = ExtractBits(pBitStream, &nBit, 4);
Params->sAmplitude[2] = ExtractBits(pBitStream, &nBit, 4);
Params->sAmplitude[3] = ExtractBits(pBitStream, &nBit, 4);
/* Frame erasure concealment and parameters scaling*/
if( Params->PitchLag[0] <= 123) {
Params->PitchLag[0] += G723_MIN_PITCH ;
} else {
Params->isBadFrame = 1; /* transmission error */
return;
}
if( Params->PitchLag[1] <= 123) {
Params->PitchLag[1] += G723_MIN_PITCH ;
} else {
Params->isBadFrame = 1; /* transmission error */
return;
}
for ( i = 0 ; i < 4 ; i ++ ) {
Params->sTrainDirac[i] = 0 ;
Bound_AcGn = AdCdbkSizeLowRate;
lTmp = Params->AdCdbkGain[i];
Params->AdCdbkGain[i] = (short)(lTmp/N_GAINS) ;
if(Params->AdCdbkGain[i] < Bound_AcGn ) {
Params->sAmpIndex[i] = (short)(lTmp % N_GAINS) ;
} else {
Params->isBadFrame = 1; /* transmission error */
return ;
}
}
return;
}
if (InfoBit == G723_Rate63Frame) {
Params->FrameType = G723_ActiveFrm;
Params->currRate = G723_Rate63;
Params->lLSPIdx = ExtractBits(pBitStream, &nBit, 24);/* LspId */
Params->PitchLag[0] = ExtractBits(pBitStream, &nBit, 7);
Params->AdCdbkLag[1] = ExtractBits(pBitStream, &nBit, 2);
Params->PitchLag[1] = ExtractBits(pBitStream, &nBit, 7);
Params->AdCdbkLag[3] = ExtractBits(pBitStream, &nBit, 2);
Params->AdCdbkLag[0] = Params->AdCdbkLag[2] = 1;
Params->AdCdbkGain[0] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[1] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[2] = ExtractBits(pBitStream, &nBit, 12);
Params->AdCdbkGain[3] = ExtractBits(pBitStream, &nBit, 12);
Params->sGrid[0] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[1] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[2] = ExtractBits(pBitStream, &nBit, 1);
Params->sGrid[3] = ExtractBits(pBitStream, &nBit, 1);
ExtractBits(pBitStream, &nBit, 1);/* Skip the reserved bit for align pack*/
lTmp = ExtractBits(pBitStream, &nBit, 13); /*Exract the position,s MSB*/
/* lTmp = 810*x0 + 90*x1 + 9*x2 + x3*/
Params->sPosition[0] = lTmp / 810;
lTmp -= Params->sPosition[0]*810; /* lTmp = 90*x1 + 9*x2 + x3*/
Params->sPosition[1] = lTmp / 90;
lTmp -= Params->sPosition[1]*90; /* lTmp = 9*x2 + x3*/
Params->sPosition[2] = lTmp / 9;
Params->sPosition[3] = lTmp - (Params->sPosition[2]*9);
/* Extract all the pulse positions */
Params->sPosition[0] = ( Params->sPosition[0] << 16 ) + ExtractBits(pBitStream, &nBit, 16) ;
Params->sPosition[1] = ( Params->sPosition[1] << 14 ) + ExtractBits(pBitStream, &nBit, 14) ;
Params->sPosition[2] = ( Params->sPosition[2] << 16 ) + ExtractBits(pBitStream, &nBit, 16) ;
Params->sPosition[3] = ( Params->sPosition[3] << 14 ) + ExtractBits(pBitStream, &nBit, 14) ;
/* Extract pulse amplitudes */
Params->sAmplitude[0] = ExtractBits(pBitStream, &nBit, 6);
Params->sAmplitude[1] = ExtractBits(pBitStream, &nBit, 5);
Params->sAmplitude[2] = ExtractBits(pBitStream, &nBit, 6);
Params->sAmplitude[3] = ExtractBits(pBitStream, &nBit, 5);
/* Frame erasure concealment and parameters scaling*/
if( Params->PitchLag[0] <= 123) {
Params->PitchLag[0] += G723_MIN_PITCH ;
} else {
Params->isBadFrame = 1; /* transmission error */
return;
}
if( Params->PitchLag[1] <= 123) {
Params->PitchLag[1] += G723_MIN_PITCH ;
} else {
Params->isBadFrame = 1; /* transmission error */
return;
}
for ( i = 0 ; i < 4 ; i ++ ) {
lTmp = Params->AdCdbkGain[i];
Params->sTrainDirac[i] = 0 ;
Bound_AcGn = AdCdbkSizeLowRate ;
if (Params->PitchLag[i>>1] < (G723_SBFR_LEN-2)) {
Params->sTrainDirac[i] = (short)(lTmp >> 11) ;
lTmp &= 0x7ff ;
Bound_AcGn = AdCdbkSizeHighRate ;
}
Params->AdCdbkGain[i] = (short)(lTmp/N_GAINS) ;
if(Params->AdCdbkGain[i] < Bound_AcGn ) {
Params->sAmpIndex[i] = (short)(lTmp % N_GAINS) ;
} else {
Params->isBadFrame = 1; /* transmission error */
return ;
}
}
return;
}
return;
}
static short* Parm2Bits( int param, short *pBitStrm, int ParamBitNum )
{
int i;
for ( i = 0 ; i < ParamBitNum ; i ++ ) {
*pBitStrm ++ = (short) (param & 0x1);
param >>= 1;
}
return pBitStrm;
}
void SetParam2Bitstream(G723Encoder_Obj* encoderObj, ParamStream_G723 *Params, char *pDstBitStream)
{
int i;
short *pBitStream;
int MSBs;
LOCAL_ARRAY(short, Bits,192,encoderObj);
pBitStream = Bits;
switch (Params->FrameType) {
case 0 : /* untransmitted */
pDstBitStream[0] = 0;
pBitStream = Parm2Bits( G723_UntransmittedFrame, pBitStream, 2 );
for ( i = 0 ; i < 2 ; i ++ )
pDstBitStream[i>>3] ^= Bits[i] << (i & 0x7);
LOCAL_ARRAY_FREE(short, Bits,192,encoderObj);
return;
case 2 : /* SID */
ippsZero_8u(pDstBitStream, 4);
pBitStream = Parm2Bits( G723_SIDFrame, pBitStream, 2 );
pBitStream = Parm2Bits( Params->lLSPIdx, pBitStream, 24 ); /* 24 bit lLSPIdx */
pBitStream = Parm2Bits( Params->sAmpIndex[0], pBitStream, 6 );/* Do Sid frame gain */
for ( i = 0 ; i < 32 ; i ++ )
pDstBitStream[i>>3] ^= Bits[i] << (i & 0x7) ;
LOCAL_ARRAY_FREE(short, Bits,192,encoderObj);
return;
default :
if ( Params->currRate == G723_Rate63 ){
ippsZero_8u(pDstBitStream, 24);
pBitStream = Parm2Bits( G723_Rate63Frame, pBitStream, 2 );
pBitStream = Parm2Bits( Params->lLSPIdx, pBitStream, 24 ) ; /* 24 bit LspId */
pBitStream = Parm2Bits( Params->PitchLag[0] - G723_MIN_PITCH, pBitStream, 7 ) ; /* Adaptive codebook lags */
pBitStream = Parm2Bits( Params->AdCdbkLag[1], pBitStream, 2 ) ;
pBitStream = Parm2Bits( Params->PitchLag[1] - G723_MIN_PITCH, pBitStream, 7 ) ;
pBitStream = Parm2Bits( Params->AdCdbkLag[3], pBitStream, 2 ) ;
/* Write gains indexes of 12 bit */
for ( i = 0 ; i < 4 ; i ++ ) {
pBitStream = Parm2Bits( Params->AdCdbkGain[i]*N_GAINS + Params->sAmpIndex[i] + (Params->sTrainDirac[i] << 11), pBitStream, 12 ) ;
}
for ( i = 0 ; i < 4 ; i ++ )
*pBitStream ++ = Params->sGrid[i] ; /* Write all the Grid indices */
*pBitStream ++ = 0 ; /* reserved bit */
/* Write 13 bit combined position index, pack 4 MSB from each pulse position. */
MSBs = (Params->sPosition[0] >> 16) * 810 + ( Params->sPosition[1] >> 14)*90 +
(Params->sPosition[2] >> 16) * 9 + ( Params->sPosition[3] >> 14 );
pBitStream = Parm2Bits(MSBs, pBitStream, 13);
/* Write all the pulse positions */
pBitStream = Parm2Bits(Params->sPosition[0] & 0xffff, pBitStream, 16);
pBitStream = Parm2Bits(Params->sPosition[1] & 0x3fff, pBitStream, 14);
pBitStream = Parm2Bits(Params->sPosition[2] & 0xffff, pBitStream, 16);
pBitStream = Parm2Bits(Params->sPosition[3] & 0x3fff, pBitStream, 14);
/* Write pulse amplitudes */
pBitStream = Parm2Bits(Params->sAmplitude[0], pBitStream, 6);
pBitStream = Parm2Bits(Params->sAmplitude[1] , pBitStream, 5);
pBitStream = Parm2Bits(Params->sAmplitude[2], pBitStream, 6);
pBitStream = Parm2Bits(Params->sAmplitude[3], pBitStream, 5);
for ( i = 0 ; i < 192 ; i ++ )
pDstBitStream[i>>3] ^= Bits[i] << (i & 0x7);
}else{
ippsZero_8u(pDstBitStream, 20);
pBitStream = Parm2Bits( G723_Rate53Frame, pBitStream, 2 );
pBitStream = Parm2Bits( Params->lLSPIdx, pBitStream, 24 ) ; /* 24 bit lLSPIdx */
pBitStream = Parm2Bits( Params->PitchLag[0] - G723_MIN_PITCH, pBitStream, 7 ) ; /* Adaptive codebook lags */
pBitStream = Parm2Bits( Params->AdCdbkLag[1], pBitStream, 2 ) ;
pBitStream = Parm2Bits( Params->PitchLag[1] - G723_MIN_PITCH, pBitStream, 7 ) ;
pBitStream = Parm2Bits( Params->AdCdbkLag[3], pBitStream, 2 ) ;
/* Write gains indexes of 12 bit */
for ( i = 0 ; i < 4 ; i ++ )
pBitStream = Parm2Bits( Params->AdCdbkGain[i]*N_GAINS + Params->sAmpIndex[i], pBitStream, 12 );
for ( i = 0 ; i < 4 ; i ++ )
*pBitStream ++ = Params->sGrid[i] ; /* Write all the Grid indices */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -