📄 g728api.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.728 speech codec: API functions.
//
*/
#include <stdlib.h>
#include <stdio.h>
#include <ipps.h>
#include "g728api.h"
#include "owng728.h"
#include "g728tables.h"
static __ALIGN32 CONST IppSpchBitRate rate2ipp[3] = {
IPP_SPCHBR_16000,
IPP_SPCHBR_12800,
IPP_SPCHBR_9600
};
#define ADD_ALIGN_MEM_BLOCK(align,addr,length) (addr + length + align -1)
G728_CODECFUN( APIG728_Status, apiG728Encoder_GetSize,
(G728Encoder_Obj* encoderObj, unsigned int *pCodecSize))
{
if(NULL == encoderObj)
return APIG728_StsBadArgErr;
if(NULL == pCodecSize)
return APIG728_StsBadArgErr;
if(encoderObj->objPrm.key != ENC_KEY)
return APIG728_StsNotInitialized;
*pCodecSize = encoderObj->objPrm.objSize;
return APIG728_StsNoErr;
}
G728_CODECFUN( APIG728_Status, apiG728Decoder_GetSize,
(G728Decoder_Obj* decoderObj, unsigned int *pCodecSize))
{
if(NULL == decoderObj)
return APIG728_StsBadArgErr;
if(NULL == pCodecSize)
return APIG728_StsBadArgErr;
if(decoderObj->objPrm.key != DEC_KEY)
return APIG728_StsNotInitialized;
*pCodecSize = decoderObj->objPrm.objSize;
return APIG728_StsNoErr;
}
G728_CODECFUN( APIG728_Status, apiG728Encoder_Alloc,(unsigned int* objSize))
{
int rexpMemSize=IPP_MAX_32S;
int rexpwMemSize=IPP_MAX_32S;
int rexplgMemSize=IPP_MAX_32S;
int combSize=IPP_MAX_32S;
int iirMemSize=IPP_MAX_32S;
char* eObj = NULL;
eObj = (char*)IPP_ALIGNED_PTR(eObj+sizeof(G728Encoder_Obj), 16);
ippsIIR16sGetStateSize_G728_16s(&iirMemSize);
eObj = (char*)IPP_ALIGNED_PTR(eObj+iirMemSize, 16);
ippsCombinedFilterGetStateSize_G728_16s(&combSize);
eObj = (char*)IPP_ALIGNED_PTR(eObj+combSize, 16);
ippsWinHybridGetStateSize_G728_16s(LPCLG, NUPDATE, NONRLG, 0, &rexplgMemSize);
eObj = (char*)IPP_ALIGNED_PTR(eObj+rexplgMemSize, 16);
ippsWinHybridGetStateSize_G728_16s(LPCW, NFRSZ, NONRW, 0, &rexpwMemSize);
eObj = (char*)IPP_ALIGNED_PTR(eObj+rexpwMemSize, 16);
ippsWinHybridGetStateSize_G728_16s(LPC, NFRSZ, NONR, IDIM, &rexpMemSize);
eObj = (char*)IPP_ALIGNED_PTR(eObj+rexpMemSize, 16);
*objSize = (Ipp32u)(eObj - (char*)NULL + 16);
return APIG728_StsNoErr;
}
G728_CODECFUN( APIG728_Status, apiG728Encoder_Init,(G728Encoder_Obj* eObj, G728_Rate rate))
{
int rexpMemSize=IPP_MAX_32S;
int rexpwMemSize=IPP_MAX_32S;
int rexplgMemSize=IPP_MAX_32S;
int combSize=IPP_MAX_32S;
int iirMemSize=IPP_MAX_32S;
char* tmpObjPtr = (char*)eObj;
unsigned int objSize;
// if((int)eObj & 0x7){ /* shall be at least 8 bytes aligned */
// return APIG728_StsNotInitialized;
// }
ippsZero_16s((Ipp16s*) eObj,sizeof(G728Encoder_Obj)/2);
eObj->objPrm.key = ENC_KEY;
eObj->objPrm.rate = rate;
eObj->h[0] = 8192;
eObj->vecLGPredictorCoeffs[0] = -16384;
ippsSet_16s(-16384, eObj->vecLGPredictorState, LPCLG);
ippsSet_16s(16, eObj->nlssttmp, 4);
if(rate==G728_Rate_12800) {
eObj->pGq = gq_128;
eObj->pNgq = nngq_128;
eObj->pCodebookGain = cnstCodebookVectorsGain_128;
}
else if(rate==G728_Rate_9600) {
eObj->pGq = gq_96;
eObj->pNgq = nngq_96;
eObj->pCodebookGain = cnstCodebookVectorsGain_96;
}
else if(rate==G728_Rate_16000){
eObj->pGq = gq;
eObj->pNgq = nngq;
eObj->pCodebookGain = cnstCodebookVectorsGain;
}
ippsImpulseResponseEnergy_G728_16s(eObj->h, eObj->y2);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+sizeof(G728Encoder_Obj), 16);
eObj->wgtMem = (IppsIIRState_G728_16s*)tmpObjPtr;
ippsIIR16sGetStateSize_G728_16s(&iirMemSize);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+iirMemSize, 16);
eObj->combMem = (IppsCombinedFilterState_G728_16s*)tmpObjPtr;
ippsCombinedFilterGetStateSize_G728_16s(&combSize);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+combSize, 16);
eObj->rexplgMem = (IppsWinHybridState_G728_16s*)tmpObjPtr;
ippsWinHybridGetStateSize_G728_16s(LPCLG, NUPDATE, NONRLG, 0, &rexplgMemSize);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+rexplgMemSize, 16);
eObj->rexpwMem = (IppsWinHybridState_G728_16s*)tmpObjPtr;
ippsWinHybridGetStateSize_G728_16s(LPCW, NFRSZ, NONRW, 0, &rexpwMemSize);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+rexpwMemSize, 16);
eObj->rexpMem = (IppsWinHybridState_G728_16s*)tmpObjPtr;
ippsWinHybridGetStateSize_G728_16s(LPC, NFRSZ, NONR, IDIM, &rexpMemSize);
tmpObjPtr = (char*)IPP_ALIGNED_PTR(tmpObjPtr+rexpMemSize, 16);
eObj->objPrm.objSize = (Ipp32u)((char*)tmpObjPtr - (char*)eObj);
apiG728Encoder_Alloc(&objSize);
if(objSize < eObj->objPrm.objSize){
return APIG728_StsNotInitialized; /* must not occur */
}
ippsIIR16sInit_G728_16s(eObj->wgtMem);
ippsCombinedFilterInit_G728_16s(eObj->combMem);
ippsWinHybridInit_G728_16s(wnrlg,LPCLG, NUPDATE, NONRLG, 0, 12288, eObj->rexplgMem);
ippsWinHybridInit_G728_16s(wnrw,LPCW, NFRSZ, NONRW, 0, 8192, eObj->rexpwMem);
ippsWinHybridInit_G728_16s(wnr,LPC, NFRSZ, NONR, IDIM, 12288, eObj->rexpMem);
return APIG728_StsNoErr;
}
static void prm2bits(const short* prm, unsigned char* bitstream, G728_Rate rate)
{
if(rate==G728_Rate_12800) {
bitstream[0] = (unsigned char) prm[0];
bitstream[1] = (unsigned char) prm[1];
bitstream[2] = (unsigned char) prm[2];
bitstream[3] = (unsigned char) prm[3];
}
else if(rate==G728_Rate_9600) {
bitstream[0] = (unsigned char) (( prm[0] << 2 ) | ( prm[1] >> 4));
bitstream[1] = (unsigned char) (( (prm[1] & 0xf) << 4 ) | (prm[2] >> 2));
bitstream[2] = (unsigned char) (( (prm[2] & 0x3) << 6 )| prm[3]);
}
else if(rate==G728_Rate_16000){
bitstream[0] = (unsigned char) ( prm[0] >> 2);
bitstream[1] = (unsigned char) (( (prm[0] & 0x3) << 6 ) | (prm[1] >> 4));
bitstream[2] = (unsigned char) (( (prm[1] & 0xf) << 4 ) | (prm[2] >> 6));
bitstream[3] = (unsigned char) (( (prm[2] & 0x3f) << 2 )| (prm[3] >> 8));
bitstream[4] = (unsigned char) (prm[3] & 0xff);
}
}
static Ipp16s EncOncePerFrameProcessing(G728Encoder_Obj* eObj, const Ipp16s *src,
Ipp16s index){
Ipp32s gainLog;
Ipp16s linearGain;
Ipp16s scaleGain;
Ipp16s nlstarget;
Ipp16s codebookIdx;
Ipp32s i, gainIdx, shapeIdx;
Ipp32s aa0;
Ipp16s tmp;
Ipp16s rate = eObj->objPrm.rate;
Ipp16s nlset;
IPP_ALIGNED_ARRAY(16, Ipp16s, sw, (IDIM +3)); /* IDIM + 3 = 16 byte */
IPP_ALIGNED_ARRAY(16, Ipp16s, target, (IDIM +3));
IPP_ALIGNED_ARRAY(16, Ipp16s, pn, (IDIM +3));
IPP_ALIGNED_ARRAY(16, Ipp16s, tempZIR, (IDIM +3));
IPP_ALIGNED_ARRAY(16, Ipp16s, et, (IDIM +3));
/* Get backward-adapted gain */
/* gstate[1:9] shifted down 1 position */
LoggainLinearPrediction(eObj->vecLGPredictorCoeffs, eObj->vecLGPredictorState, &gainLog);
if(gainLog > 14336) gainLog = 14336;
if(gainLog < -16384) gainLog = -16384;
InverseLogarithmCalc(gainLog, &linearGain, &scaleGain);
/* Synthesis filter with zero input combined with perceptual weighting filter */
ippsCombinedFilterZeroInput_G728_16s(eObj->vecSyntFltrCoeffs, eObj->vecWghtFltrCoeffs, tempZIR, eObj->combMem);
/* Perceptual weighting filter */
ippsIIR16s_G728_16s((Ipp16s*)eObj->vecWghtFltrCoeffs, src, sw, IDIM, eObj->wgtMem);
/* VQ target vector computation */
VQ_target_vector_calc(sw, tempZIR, target);
/* VQ target vector normalization */
nlstarget = 2;
VQ_target_vec_norm(linearGain, scaleGain, target, &nlstarget);
/* Time-reversed convolution */
Time_reversed_conv(eObj->h, target, nlstarget, pn);
/* Excitation codebook search */
ippsCodebookSearch_G728_16s(pn, eObj->y2, &shapeIdx, &gainIdx, &codebookIdx, rate2ipp[rate]);
/* Scale selected excitation codevector */
aa0 = eObj->pGq[gainIdx] * linearGain;
aa0 = ShiftL_32s(aa0, eObj->pNgq[gainIdx]);
tmp = Cnvrt_NR_32s16s(aa0);
nlset = scaleGain + eObj->pNgq[gainIdx] + shape_all_nls[shapeIdx] - 8;
Excitation_VQ_and_gain_scaling(tmp, &shape_all_norm[shapeIdx * IDIM], et);
/* Memory update */
ippsCombinedFilterZeroState_G728_16s(eObj->vecSyntFltrCoeffs, eObj->vecWghtFltrCoeffs,
et, nlset, eObj->st, &eObj->nlsst, eObj->combMem);
/* Update log-gain and gain predictor memory */
eObj->vecLGPredictorState[0] = LoggainAdderLimiter(gainLog, (Ipp16s)gainIdx, (Ipp16s)shapeIdx,
eObj->pCodebookGain);
i = (index - 1) *IDIM;
ippsCopy_16s(eObj->st,&eObj->sttmp[i],5);
eObj->nlssttmp[index-1] = eObj->nlsst;
/* stmp - cyclic buffer */
/* index stmp */
/* 1 0, 0, src[ 0: 4], 0, */
/* 2 0, 0, src[ 0: 4],src[ 5: 9], */
/* 3 src[10:14], 0, src[ 0: 4],src[ 5: 9], */
/* 4 src[10:14],src[15:19], src[ 0: 4],src[ 5: 9], */
/* 1 src[10:14],src[15:19], src[20:24],src[ 5: 9], */
/* 2 src[10:14],src[15:19], src[20:24],src[25:29], .... */
i = (index + 1) & 0x3;
i *= IDIM;
ippsCopy_16s(src,&eObj->stmp[i],5);
return codebookIdx;
}
G728_CODECFUN( APIG728_Status, apiG728Encode,
(G728Encoder_Obj* eObj, short *src, unsigned char *dst))
{
Ipp16s gtmp[4];
Ipp16s foo;
Ipp16s codebookIdxs[4];/* four codebook indexes */
int index;
/* four vectors of 5 samples length (20 short integer)*/
eObj->icount = eObj->icount & 3;
eObj->icount++;
index = 1;
codebookIdxs[index-1] = EncOncePerFrameProcessing(eObj, src, (Ipp16s)index);
gtmp[0] = eObj->vecLGPredictorState[3];
gtmp[1] = eObj->vecLGPredictorState[2];
gtmp[2] = eObj->vecLGPredictorState[1];
gtmp[3] = eObj->vecLGPredictorState[0];
/* Block 43 */
eObj->illcondg=0;
if(ippsWinHybrid_G728_16s(0, gtmp, eObj->r, eObj->rexplgMem ) != ippStsNoErr){
eObj->illcondg=1;
};
ippsZero_16s(eObj->tmpLGPredictorCoeffs, LPCLG);
LevinsonDurbin(eObj->r, 0, LPCLG, eObj->tmpLGPredictorCoeffs, &foo, &foo,
&eObj->scaleLGPredictorCoeffs, &eObj->illcondp, &eObj->illcondg);
src += 5;
eObj->icount = eObj->icount & 3;
eObj->icount++;
index = 2;
if(eObj->illcondg==0)
BandwidthExpansionModul(cnstGainPreditorBroadenVector, eObj->tmpLGPredictorCoeffs,
eObj->scaleLGPredictorCoeffs, eObj->vecLGPredictorCoeffs, LPCLG);
codebookIdxs[index-1] = EncOncePerFrameProcessing(eObj, src, (Ipp16s)index);
/* Block 36 */
eObj->illcondw=0;
if(ippsWinHybrid_G728_16s(0, eObj->stmp, eObj->r, eObj->rexpwMem ) != ippStsNoErr){
eObj->illcondw=1;
};
ippsZero_16s(eObj->tmpWghtFltrCoeffs, LPCW);
LevinsonDurbin(eObj->r, 0, LPCW, eObj->tmpWghtFltrCoeffs, &foo, &foo,
&eObj->scaleWghtFltrCoeffs, &eObj->illcondp, &eObj->illcondw);
src += 5;
eObj->icount = eObj->icount & 3;
eObj->icount++;
index = 3;
if(eObj->illcond==0)
BandwidthExpansionModul(cnstSynthesisFilterBroadenVector, eObj->tmpSyntFltrCoeffs,
eObj->scaleSyntFltrCoeffs, eObj->vecSyntFltrCoeffs, LPC);
if(eObj->illcondw==0)
WeightingFilterCoeffsCalc(eObj->tmpWghtFltrCoeffs, eObj->scaleWghtFltrCoeffs, eObj->vecWghtFltrCoeffs);
Impulse_response_vec_calc(eObj->vecSyntFltrCoeffs, eObj->vecWghtFltrCoeffs, eObj->h);
ippsImpulseResponseEnergy_G728_16s(eObj->h, eObj->y2);
codebookIdxs[index-1] = EncOncePerFrameProcessing(eObj,src,(Ipp16s)index);
src += 5;
eObj->icount = eObj->icount & 3;
eObj->icount++;
index = 4;
codebookIdxs[index-1] = EncOncePerFrameProcessing(eObj,src,(Ipp16s)index);
eObj->illcond = 0;
if(ippsWinHybridBlock_G728_16s(0, eObj->sttmp, eObj->nlssttmp,
eObj->rtmp, eObj->rexpMem ) != ippStsNoErr){
eObj->illcond = 1;
};
ippsZero_16s(eObj->tmpSyntFltrCoeffs, LPC);
LevinsonDurbin(eObj->rtmp, 0, LPC, eObj->tmpSyntFltrCoeffs, &foo, &foo,
&eObj->scaleSyntFltrCoeffs, &eObj->illcondp, &eObj->illcond);
/* pack indexes into bitstream */
prm2bits(codebookIdxs, dst, eObj->objPrm.rate);
/* End once-per-frame processing */
return APIG728_StsNoErr;
}
G728_CODECFUN( APIG728_Status, apiG728Decoder_Alloc,(unsigned int* objSize))
{
int rexpMemSize=IPP_MAX_32S;
int rexplgMemSize=IPP_MAX_32S;
int iirMemSize=IPP_MAX_32S;
int stpMemSize=IPP_MAX_32S;
int syntMemSize=IPP_MAX_32S;
int postFltAdaptMemSize = IPP_MAX_32S;
char* dObj = NULL;
dObj = (char*)IPP_ALIGNED_PTR(dObj+sizeof(G728Decoder_Obj), 16);
ippsSynthesisFilterGetStateSize_G728_16s(&syntMemSize);
dObj = (char*)IPP_ALIGNED_PTR(dObj+syntMemSize, 16);
ippsPostFilterGetStateSize_G728_16s(&stpMemSize);
ippsIIR16sGetStateSize_G728_16s(&iirMemSize);
dObj = (char*)IPP_ALIGNED_PTR(dObj+IPP_MAX(stpMemSize,iirMemSize), 16);
ippsWinHybridGetStateSize_G728_16s(LPCLG, NUPDATE, NONRLG, 0, &rexplgMemSize);
dObj = (char*)IPP_ALIGNED_PTR(dObj+rexplgMemSize, 16);
ippsWinHybridGetStateSize_G728_16s(LPC, NFRSZ, NONR, IDIM, &rexpMemSize);
dObj = (char*)IPP_ALIGNED_PTR(dObj+rexpMemSize, 16);
ippsPostFilterAdapterGetStateSize_G728(&postFltAdaptMemSize);
dObj = (char*)IPP_ALIGNED_PTR(dObj+postFltAdaptMemSize, 16);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -