📄 uscg726.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.726 speech codec: USC functions.
//
*/
#include <string.h>
#include <usc.h>
#include <ipps.h>
#include <ippsc.h>
#include "scratchmem.h"
#define BITSTREAM_SIZE 5
#define G726_NUM_RATES 4
#define G726_FRAME_SIZE 8
#define G726_BITS_PER_SAMPLE 16
static USC_Status GetInfo(USC_Handle handle, USC_CodecInfo *pInfo);
static USC_Status NumAlloc(const USC_Option *options, int *nbanks);
static USC_Status MemAlloc(const USC_Option *options, USC_MemBank *pBanks);
static USC_Status Init(const USC_Option *options, const USC_MemBank *pBanks, USC_Handle *handle);
static USC_Status Reinit(const USC_Modes *modes, USC_Handle handle );
static USC_Status Control(const USC_Modes *modes, USC_Handle handle );
static USC_Status Encode(USC_Handle handle, USC_PCMStream *in, USC_Bitstream *out);
static USC_Status Decode(USC_Handle handle, USC_Bitstream *in, USC_PCMStream *out);
static USC_Status GetOutStreamSize(const USC_Option *options, int bitrate, int nbytesSrc, int *nbytesDst);
static USC_Status SetFrameSize(const USC_Option *options, USC_Handle handle, int frameSize);
typedef struct {
int direction;
int bitrate;
int law;
int frameSize; // for future extension
} G726_Handle_Header;
/* global usc vector table */
USCFUN USC_Fxns USC_G726_Fxns=
{
{
USC_Codec,
GetInfo,
NumAlloc,
MemAlloc,
Init,
Reinit,
Control
},
Encode,
Decode,
GetOutStreamSize,
SetFrameSize
};
static USC_Option params; /* what is supported */
static USC_PCMType pcmType; /* codec audio source */
static __ALIGN32 CONST short LostFrame[8]=
{ 0, 0, 0, 0, 0, 0, 0, 0 };
static __ALIGN32 CONST USC_Rates pTblRates_G726[G726_NUM_RATES]={
{40000},
{32000},
{24000},
{16000}
};
static int CheckRate_G726(int rate_bps)
{
int rate;
switch(rate_bps) {
case 16000: rate = 0; break;
case 24000: rate = 1; break;
case 32000: rate = 2; break;
case 40000: rate = 3; break;
default: rate = -1; break;
}
return rate;
}
static USC_Status GetInfo(USC_Handle handle, USC_CodecInfo *pInfo)
{
G726_Handle_Header *g726_header;
pInfo->name = "G726";
if (handle == NULL) {
pInfo->params.modes.bitrate = 16000;
pInfo->params.direction = 0;
pInfo->params.law = 3;
pInfo->framesize = G726_FRAME_SIZE*sizeof(short);
pInfo->maxbitsize = BITSTREAM_SIZE;
} else {
g726_header = (G726_Handle_Header*)handle;
pInfo->params.modes.bitrate = g726_header->bitrate;
pInfo->params.direction = g726_header->direction;
pInfo->params.law = g726_header->law;
pInfo->framesize = g726_header->frameSize;
pInfo->maxbitsize = (g726_header->frameSize / (G726_FRAME_SIZE*sizeof(short))) * BITSTREAM_SIZE;
}
pInfo->params.modes.vad = 0;
pInfo->params.modes.truncate = 0;
pInfo->pcmType.sample_frequency = 8000;
pInfo->pcmType.bitPerSample = G726_BITS_PER_SAMPLE;
pInfo->params.modes.hpf = 0;
pInfo->params.modes.pf = 0;
pInfo->nRates = G726_NUM_RATES;
pInfo->pRateTbl = (const USC_Rates *)&pTblRates_G726;
return USC_NoError;
}
static USC_Status NumAlloc(const USC_Option *options, int *nbanks)
{
if(options==NULL) return USC_BadDataPointer;
if(nbanks==NULL) return USC_BadDataPointer;
*nbanks = 1;
return USC_NoError;
}
static USC_Status MemAlloc(const USC_Option *options, USC_MemBank *pBanks)
{
unsigned int nbytes;
if(options==NULL) return USC_BadDataPointer;
if(pBanks==NULL) return USC_BadDataPointer;
pBanks->pMem = NULL;
if (options->direction == 0) /* encode only */
{
ippsEncodeGetStateSize_G726_16s8u(&nbytes);
}
else if (options->direction == 1) /* decode only */
{
ippsDecodeGetStateSize_G726_8u16s(&nbytes);
} else {
return USC_NoOperation;
}
pBanks->nbytes = nbytes + sizeof(G726_Handle_Header); /* room for USC header */
return USC_NoError;
}
static IppPCMLaw Usc2IppLaw(int uscLaw)
{
if(uscLaw==0) return IPP_PCM_LINEAR;
else if(uscLaw==1) return IPP_PCM_ALAW;
else return IPP_PCM_MULAW;
}
static __ALIGN32 CONST IppSpchBitRate usc2g726Rate[4]={
IPP_SPCHBR_16000,
IPP_SPCHBR_24000,
IPP_SPCHBR_32000,
IPP_SPCHBR_40000
};
static USC_Status Init(const USC_Option *options, const USC_MemBank *pBanks, USC_Handle *handle)
{
G726_Handle_Header *g726_header;
int bitrate_idx;
if(options==NULL) return USC_BadDataPointer;
if(pBanks==NULL) return USC_BadDataPointer;
if(pBanks->pMem==NULL) return USC_NotInitialized;
if(pBanks->nbytes<=0) return USC_NotInitialized;
if(handle==NULL) return USC_InvalidHandler;
*handle = (USC_Handle*)pBanks->pMem;
g726_header = (G726_Handle_Header*)*handle;
bitrate_idx = CheckRate_G726(options->modes.bitrate);
if(bitrate_idx < 0) return USC_UnsupportedBitRate;
g726_header->direction = options->direction;
g726_header->bitrate = options->modes.bitrate;
g726_header->law = options->law;
g726_header->frameSize = G726_FRAME_SIZE*sizeof(short);
if (options->direction == 0) /* encode only */
{
IppsEncoderState_G726_16s *EncObj = (IppsEncoderState_G726_16s *)((char*)*handle + sizeof(G726_Handle_Header));
ippsEncodeInit_G726_16s8u(EncObj, usc2g726Rate[bitrate_idx]);
}
else if (options->direction == 1) /* decode only */
{
IppsDecoderState_G726_16s *DecObj = (IppsDecoderState_G726_16s *)((char*)*handle + sizeof(G726_Handle_Header));
ippsDecodeInit_G726_8u16s(DecObj, usc2g726Rate[bitrate_idx],Usc2IppLaw(options->law));
} else {
return USC_NoOperation;
}
return USC_NoError;
}
static USC_Status Reinit(const USC_Modes *modes, USC_Handle handle )
{
G726_Handle_Header *g726_header;
int bitrate_idx;
if(modes==NULL) return USC_BadDataPointer;
if(handle==NULL) return USC_InvalidHandler;
g726_header = (G726_Handle_Header*)handle;
bitrate_idx = CheckRate_G726(modes->bitrate);
if(bitrate_idx < 0) return USC_UnsupportedBitRate;
g726_header->bitrate = modes->bitrate;
g726_header->frameSize = G726_FRAME_SIZE*sizeof(short);
if (g726_header->direction == 0) /* encode only */
{
IppsEncoderState_G726_16s *EncObj = (IppsEncoderState_G726_16s *)((char*)handle + sizeof(G726_Handle_Header));
ippsEncodeInit_G726_16s8u(EncObj, usc2g726Rate[bitrate_idx]);///////////////////////////
}
else if (g726_header->direction == 1) /* decode only */
{
IppsDecoderState_G726_16s *DecObj = (IppsDecoderState_G726_16s *)((char*)handle + sizeof(G726_Handle_Header));
ippsDecodeInit_G726_8u16s(DecObj, usc2g726Rate[bitrate_idx],Usc2IppLaw(g726_header->law));////////////////
} else {
return USC_NoOperation;
}
return USC_NoError;
}
static USC_Status Control(const USC_Modes *modes, USC_Handle handle )
{
if(modes==NULL) return USC_BadDataPointer;
if(handle==NULL) return USC_InvalidHandler;
return USC_NoError;
}
int OutFrameSize(int bitrate_bps)
{
switch(bitrate_bps) {
case 16000: return 2;
case 24000: return 3;
case 32000: return 4;
case 40000: return 5;
default: return 0;
}
}
__INLINE int PackCodeword(const char* inCode, char* outCode, int bitrate, int *outLen)
{
*outLen = OutFrameSize(bitrate);
if(*outLen==0) return 0;
if(*outLen == 2) {
outCode[0] = (inCode[3] << 6) | ((inCode[2] & 0x3) << 4) | ((inCode[1] & 0x3) << 2) | (inCode[0] & 0x3);
outCode[1] = (inCode[7] << 6) | ((inCode[6] & 0x3) << 4) | ((inCode[5] & 0x3) << 2) | (inCode[4] & 0x3);
} else if(*outLen == 3) {
outCode[0] = (inCode[0] & 0x7) | ((inCode[1] & 0x7)<<3) | ((inCode[2] & 0x3)<<6);
outCode[1] = ((inCode[2] & 0x4)>>2) | ((inCode[3] & 0x7)<<1) | ((inCode[4] & 0x7)<<4) | ((inCode[5] & 0x1)<<7);
outCode[2] = ((inCode[5] & 0x7)>>1) | ((inCode[6] & 0x7)<<2) | ((inCode[7] & 0x7)<<5);
} else if(*outLen == 4) {
outCode[0] = (inCode[1] << 4) | (inCode[0] & 0xf);
outCode[1] = (inCode[3] << 4) | (inCode[2] & 0xf);
outCode[2] = (inCode[5] << 4) | (inCode[4] & 0xf);
outCode[3] = (inCode[7] << 4) | (inCode[6] & 0xf);
} else if(*outLen == 5) {
outCode[0] = (inCode[0] & 0x1f) | ((inCode[1] & 0x7)<<5);
outCode[1] = ((inCode[1] & 0x18)>>3) | ((inCode[2] & 0x1f)<<2) | ((inCode[3] & 0x1)<<7);
outCode[2] = ((inCode[3] & 0x1e)>>1) | ((inCode[4] & 0xf)<<4);
outCode[3] = ((inCode[4] & 0x10)>>4) | ((inCode[5] & 0x1f)<<1) | ((inCode[6] & 0x3)<<6);
outCode[4] = ((inCode[6] & 0x1c)>>2) | ((inCode[7] & 0x1f)<<3);
}
return 1;
}
__INLINE int PackCodeword_tst(const char* inCode, char* outCode, int bitrate, int inLen, int *outLen)
{
int i, frmLen, packedLen = 0;
frmLen = OutFrameSize(bitrate);
if(frmLen==0) return 0;
if(frmLen == 2) {
for(i=0;i<inLen;i+=8) {
outCode[0] = (inCode[3] << 6) | ((inCode[2] & 0x3) << 4) | ((inCode[1] & 0x3) << 2) | (inCode[0] & 0x3);
outCode[1] = (inCode[7] << 6) | ((inCode[6] & 0x3) << 4) | ((inCode[5] & 0x3) << 2) | (inCode[4] & 0x3);
outCode += 2;
inCode += 8;
packedLen += 2;
}
} else if(frmLen == 3) {
for(i=0;i<inLen;i+=8) {
outCode[0] = (inCode[0] & 0x7) | ((inCode[1] & 0x7)<<3) | ((inCode[2] & 0x3)<<6);
outCode[1] = ((inCode[2] & 0x4)>>2) | ((inCode[3] & 0x7)<<1) | ((inCode[4] & 0x7)<<4) | ((inCode[5] & 0x1)<<7);
outCode[2] = ((inCode[5] & 0x7)>>1) | ((inCode[6] & 0x7)<<2) | ((inCode[7] & 0x7)<<5);
outCode += 3;
inCode += 8;
packedLen += 3;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -