📄 uscgsmamr.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: GSMAMR speech codec: USC funtions.
//
*/
#include "owngsmamr.h"
#include "gsmamrapi.h"
#include <string.h>
#include "gsmamrusc.h"
#define GSMAMR_NUM_RATES 8
#define GSMAMR_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 {
short sid_update_counter;
TXFrameType prev_ft;
} sid_syncState;
typedef struct {
int direction;
int trunc;
int bitrate;
int vad;
int bitrate_old;
short reset_flag;
short reset_flag_old;
sid_syncState sid_state;
} GSMAMR_Handle_Header;
static int sid_sync_init (sid_syncState *st);
static void sid_sync(sid_syncState *st, int mode, TXFrameType *tx_frame_type);
static int is_pcm_frame_homing (short *input_frame);
static short is_bitstream_frame_homing (unsigned char* bitstream, int mode);
/* global usc vector table */
USCFUN USC_Fxns USC_GSMAMR_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 int bitsLen[N_MODES]={
95, 103, 118, 134, 148, 159, 204, 244, 35
};
static __ALIGN32 CONST short LostFrame[GSMAMR_Frame]=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static __ALIGN32 CONST USC_Rates pTblRates_GSMAMR[GSMAMR_NUM_RATES]={
{12200},
{10200},
{7950},
{7400},
{6700},
{5900},
{5150},
{4750}
};
static int CheckRate_GSMAMR(int rate_bps)
{
int rate;
switch(rate_bps) {
case 4750: rate = 0; break;
case 5150: rate = 1; break;
case 5900: rate = 2; break;
case 6700: rate = 3; break;
case 7400: rate = 4; break;
case 7950: rate = 5; break;
case 10200: rate = 6; break;
case 12200: rate = 7; break;
default: rate = -1; break;
}
return rate;
}
static int CheckIdxRate_GSMAMR(int idx_rate)
{
int rate;
switch(idx_rate) {
case 0: rate = 4750; break;
case 1: rate = 5150; break;
case 2: rate = 5900; break;
case 3: rate = 6700; break;
case 4: rate = 7400; break;
case 5: rate = 7950; break;
case 6: rate = 10200; break;
case 7: rate = 12200; break;
default: rate = -1; break;
}
return rate;
}
static USC_Status GetInfo(USC_Handle handle, USC_CodecInfo *pInfo)
{
GSMAMR_Handle_Header *gsmamr_header;
pInfo->name = "GSMAMR";
pInfo->framesize = GSMAMR_Frame*sizeof(short);
if (handle == NULL) {
pInfo->params.modes.bitrate = 4750;
pInfo->params.modes.truncate = 1;
pInfo->params.direction = 0;
pInfo->params.modes.vad = 2;
} else {
gsmamr_header = (GSMAMR_Handle_Header*)handle;
pInfo->params.modes.bitrate = gsmamr_header->bitrate;
pInfo->params.modes.truncate = gsmamr_header->trunc;
pInfo->params.direction = gsmamr_header->direction;
pInfo->params.modes.vad = gsmamr_header->vad;
}
pInfo->maxbitsize = BITSTREAM_SIZE;
pInfo->pcmType.sample_frequency = 8000;
pInfo->pcmType.bitPerSample = GSMAMR_BITS_PER_SAMPLE;
pInfo->params.modes.hpf = 0;
pInfo->params.modes.pf = 0;
pInfo->params.law = 0;
pInfo->nRates = GSMAMR_NUM_RATES;
pInfo->pRateTbl = (const USC_Rates *)&pTblRates_GSMAMR;
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 */
{
GSMAMREnc_Params enc_params;
enc_params.codecType = (GSMAMRCodec_Type)0;
enc_params.mode = options->modes.vad;
apiGSMAMREncoder_Alloc(&enc_params, &nbytes);
}
else if (options->direction == 1) /* decode only */
{
GSMAMRDec_Params dec_params;
dec_params.codecType = (GSMAMRCodec_Type)0;
dec_params.mode = options->modes.vad;
apiGSMAMRDecoder_Alloc(&dec_params, &nbytes);
} else {
return USC_NoOperation;
}
pBanks->nbytes = nbytes + sizeof(GSMAMR_Handle_Header); /* room for USC header */
return USC_NoError;
}
static USC_Status Init(const USC_Option *options, const USC_MemBank *pBanks, USC_Handle *handle)
{
GSMAMR_Handle_Header *gsmamr_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;
gsmamr_header = (GSMAMR_Handle_Header*)*handle;
bitrate_idx = CheckRate_GSMAMR(options->modes.bitrate);
if(bitrate_idx < 0) return USC_UnsupportedBitRate;
gsmamr_header->direction = options->direction;
gsmamr_header->trunc = options->modes.truncate;
gsmamr_header->vad = options->modes.vad;
gsmamr_header->bitrate = options->modes.bitrate;
gsmamr_header->bitrate_old = 0;
if (options->direction == 0) /* encode only */
{
GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((char*)*handle + sizeof(GSMAMR_Handle_Header));
apiGSMAMREncoder_Init((GSMAMREncoder_Obj*)EncObj, gsmamr_header->vad);
}
else if (options->direction == 1) /* decode only */
{
GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((char*)*handle + sizeof(GSMAMR_Handle_Header));
apiGSMAMRDecoder_Init((GSMAMRDecoder_Obj*)DecObj, gsmamr_header->vad);
gsmamr_header->reset_flag = 0;
gsmamr_header->reset_flag_old = 1;
} else {
return USC_NoOperation;
}
return USC_NoError;
}
static USC_Status Reinit(const USC_Modes *modes, USC_Handle handle )
{
GSMAMR_Handle_Header *gsmamr_header;
int bitrate_idx;
if(modes==NULL) return USC_BadDataPointer;
if(handle==NULL) return USC_InvalidHandler;
gsmamr_header = (GSMAMR_Handle_Header*)handle;
bitrate_idx = CheckRate_GSMAMR(modes->bitrate);
if(bitrate_idx < 0) return USC_UnsupportedBitRate;
gsmamr_header->vad = modes->vad;
gsmamr_header->bitrate = modes->bitrate;
gsmamr_header->bitrate_old = 0;
if (gsmamr_header->direction == 0) /* encode only */
{
GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((char*)handle + sizeof(GSMAMR_Handle_Header));
gsmamr_header->reset_flag = 0;
sid_sync_init (&gsmamr_header->sid_state);
apiGSMAMREncoder_Init((GSMAMREncoder_Obj*)EncObj, modes->vad);
}
else if (gsmamr_header->direction == 1) /* decode only */
{
GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((char*)handle + sizeof(GSMAMR_Handle_Header));
apiGSMAMRDecoder_Init((GSMAMRDecoder_Obj*)DecObj, modes->vad);
gsmamr_header->reset_flag = 0;
gsmamr_header->reset_flag_old = 1;
} else {
return USC_NoOperation;
}
return USC_NoError;
}
static USC_Status Control(const USC_Modes *modes, USC_Handle handle )
{
GSMAMR_Handle_Header *gsmamr_header;
int bitrate_idx;
if(modes==NULL) return USC_BadDataPointer;
if(handle==NULL) return USC_InvalidHandler;
gsmamr_header = (GSMAMR_Handle_Header*)handle;
bitrate_idx = CheckRate_GSMAMR(modes->bitrate);
if(bitrate_idx < 0) return USC_UnsupportedBitRate;
gsmamr_header->vad = modes->vad;
gsmamr_header->bitrate = modes->bitrate;
if (gsmamr_header->direction == 0) /* encode only */
{
GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((char*)handle + sizeof(GSMAMR_Handle_Header));
apiGSMAMREncoder_Mode((GSMAMREncoder_Obj*)EncObj, modes->vad);
}
else if (gsmamr_header->direction == 1) /* decode only */
{
GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((char*)handle + sizeof(GSMAMR_Handle_Header));
apiGSMAMRDecoder_Mode((GSMAMRDecoder_Obj*)DecObj, modes->vad);
} else {
return USC_NoOperation;
}
return USC_NoError;
}
#define IO_MASK (short)0xfff8 /* 13-bit input/output */
static __ALIGN32 CONST GSMAMR_Rate_t usc2amr[8]={
GSMAMR_RATE_4750,
GSMAMR_RATE_5150,
GSMAMR_RATE_5900,
GSMAMR_RATE_6700,
GSMAMR_RATE_7400,
GSMAMR_RATE_7950,
GSMAMR_RATE_10200,
GSMAMR_RATE_12200
};
static int getBitstreamSize(int rate, int frametype, int *outRate)
{
int nbytes;
int usedRate = rate;
if (frametype != TX_SPEECH_GOOD) {
usedRate = GSMAMR_RATE_DTX;
}
nbytes = ((bitsLen[usedRate] + 7) >> 3);
//*outRate = usedRate;
*outRate = rate;
return nbytes;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -