⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uscgsmfr.c

📁 这是在PCA下的基于IPP库示例代码例子,在网上下了IPP的库之后,设置相关参数就可以编译该代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*/////////////////////////////////////////////////////////////////////////////
//
//                  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) 2004-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: GSM FR 06.10: USC functions.
//
*/

#include "owngsmfr.h"
#include "gsmfrapi.h"
#include <string.h>
#include <usc.h>

#define INIT_SEED          11111
#define GSMFR_NUM_RATES        1
#define GSMFR_EXT_FRAME_LEN  160
#define GSMFR_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;
    GSMFR_Params_t params;
    short seed;
    int fReset;
    int fReset_old;
    int lostFrames;
    int validFrames;
    unsigned char validFrame[GSMFR_PACKED_FRAME_LEN];
    int reserved; // for future extension
} GSMFR_Handle_Header;

static int  ownTestPCMFrameHoming(const short *pSrc);
static int  ownTestBitstreamFrameHoming(char* pPrmsvec, int valRate);

/* global usc vector table */
USCFUN USC_Fxns USC_GSMFR_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 USC_Rates pTblRates_GSMFR[GSMFR_NUM_RATES]={
    {13000}
};

static USC_Status GetInfo(USC_Handle handle, USC_CodecInfo *pInfo)
{

   GSMFR_Handle_Header *gsmfr_header;

   pInfo->name = "GSMFR";
   pInfo->framesize = GSMFR_EXT_FRAME_LEN*sizeof(short);
   if (handle == NULL) {
      pInfo->params.direction = 0;
      pInfo->params.modes.vad = 2;
    } else {
      gsmfr_header = (GSMFR_Handle_Header*)handle;
      pInfo->params.direction = gsmfr_header->direction;
      switch(gsmfr_header->params.vadDirection) {
        case GSMFR_VAD_OFF:  pInfo->params.modes.vad = 0; break;
        case GSMFR_DOWNLINK: pInfo->params.modes.vad = 1; break;
        case GSMFR_UPLINK:   pInfo->params.modes.vad = 2; break;
        default:             pInfo->params.modes.vad = 0; break;
      }
    }
   pInfo->params.modes.bitrate = 13000;
   pInfo->params.modes.truncate = 0;
   pInfo->pcmType.sample_frequency = 8000;
   pInfo->pcmType.bitPerSample = GSMFR_BITS_PER_SAMPLE;
   pInfo->maxbitsize = GSMFR_PACKED_FRAME_LEN;
   pInfo->params.modes.hpf = 0;
   pInfo->params.modes.pf = 0;
   pInfo->params.law = 0;
    pInfo->nRates = GSMFR_NUM_RATES;
    pInfo->pRateTbl = (const USC_Rates *)&pTblRates_GSMFR;

   return USC_NoError;
}

static USC_Status NumAlloc(const USC_Option *options, int *nbanks)
{
    *nbanks = 1;
    return USC_NoError;
}

static USC_Status MemAlloc(const USC_Option *options, USC_MemBank *pBanks)
{
    unsigned int nbytes;
    pBanks->pMem = NULL;
    if (options->direction == 0) /* encode only */
    {
        apiGSMFREncoder_Alloc(&nbytes);
    }
    else if (options->direction == 1) /* decode only */
    {
        apiGSMFRDecoder_Alloc(&nbytes);
    } else {
        return USC_NoOperation;
    }
    pBanks->nbytes = nbytes + sizeof(GSMFR_Handle_Header); /* include GSMFR_Handle_Header */
    return USC_NoError;
}

static USC_Status Init(const USC_Option *options, const USC_MemBank *pBanks, USC_Handle *handle)
{
    GSMFR_Handle_Header *gsmfr_header;
    *handle = (USC_Handle*)pBanks->pMem;
    gsmfr_header = (GSMFR_Handle_Header*)*handle;
    gsmfr_header->direction = options->direction;

    switch(options->modes.vad) {
      case 1: gsmfr_header->params.vadDirection = GSMFR_DOWNLINK; break;
      case 2: gsmfr_header->params.vadDirection = GSMFR_UPLINK; break;
      default: gsmfr_header->params.vadDirection = GSMFR_VAD_OFF; break;
    }
     if(gsmfr_header->params.vadDirection>0)
        gsmfr_header->params.vadMode=GSMFR_VAD_ON;
     else
        gsmfr_header->params.vadMode=GSMFR_VAD_OFF;
     gsmfr_header->params.ltp_f=LTP_OFF;//options->modes.truncate;
     gsmfr_header->params.direction=(GSMFR_DIRECTION_t)options->direction;
     gsmfr_header->fReset=0;
     gsmfr_header->fReset_old = 1;
     gsmfr_header->seed = INIT_SEED;

    if (options->direction == 0) /* encode only */
    {
        GSMFREncoder_Obj *EncObj = (GSMFREncoder_Obj *)((char*)*handle + sizeof(GSMFR_Handle_Header));
        apiGSMFREncoder_Init(EncObj, &gsmfr_header->params);
    }
    else if (options->direction == 1) /* decode only */
    {
        GSMFRDecoder_Obj *DecObj = (GSMFRDecoder_Obj *)((char*)*handle + sizeof(GSMFR_Handle_Header));
        apiGSMFRDecoder_Init(DecObj, &gsmfr_header->params);

    } else {
        return USC_NoOperation;
    }
    return USC_NoError;
}

static USC_Status Reinit(const USC_Modes *modes, USC_Handle handle )
{
    GSMFR_Handle_Header *gsmfr_header;
    gsmfr_header = (GSMFR_Handle_Header*)handle;

    if (gsmfr_header->direction == 0) /* encode only */
    {
        GSMFREncoder_Obj *EncObj = (GSMFREncoder_Obj *)((char*)handle + sizeof(GSMFR_Handle_Header));
        apiGSMFREncoder_Init(EncObj, &gsmfr_header->params);
    }
    else if (gsmfr_header->direction == 1) /* decode only */
    {
        GSMFRDecoder_Obj *DecObj = (GSMFRDecoder_Obj *)((char*)handle + sizeof(GSMFR_Handle_Header));
        gsmfr_header->fReset=0;
        gsmfr_header->fReset_old = 1;
        gsmfr_header->seed = INIT_SEED;

        apiGSMFRDecoder_Init(DecObj, &gsmfr_header->params);
    } else {
        return USC_NoOperation;
    }
    return USC_NoError;
}

static USC_Status Control(const USC_Modes *modes, USC_Handle handle )
{
   GSMFR_Handle_Header *gsmfr_header;
   gsmfr_header = (GSMFR_Handle_Header*)handle;
   switch(modes->vad) {
      case 1:  gsmfr_header->params.vadDirection = GSMFR_DOWNLINK; break;
      case 2:  gsmfr_header->params.vadDirection = GSMFR_UPLINK; break;
      default: gsmfr_header->params.vadDirection = GSMFR_VAD_OFF; break;
   }
   if(gsmfr_header->params.vadDirection>0)
      gsmfr_header->params.vadMode=GSMFR_VAD_ON;
   else
      gsmfr_header->params.vadMode=GSMFR_VAD_OFF;

   if (gsmfr_header->direction == 0) /* encode only */
   {
      GSMFREncoder_Obj *EncObj = (GSMFREncoder_Obj *)((char*)handle + sizeof(GSMFR_Handle_Header));
      apiGSMFREncoder_Init(EncObj, &gsmfr_header->params);
   }
   return USC_NoError;
}

#define EHF_MASK 0x0008

static int is_pcm_frame_homing (const USC_PCMStream *in) {
    int i, k=0;
    short *pSrc = (short *)in->pBuffer;
    for(i = 0; i < (int)(in->nbytes/sizeof(short)); i++) {
        k = pSrc[i] ^ EHF_MASK;
        if(k)
            break;
    }
    return !k;
}

static USC_Status Encode(USC_Handle handle, USC_PCMStream *in, USC_Bitstream *out)
{
    GSMFR_Handle_Header *gsmfr_header;
    GSMFREncoder_Obj *EncObj;
    int vad, hangover,reset_flag;
    gsmfr_header = (GSMFR_Handle_Header*)handle;

    if(handle == NULL) return USC_InvalidHandler;

    EncObj = (GSMFREncoder_Obj *)((char*)handle + sizeof(GSMFR_Handle_Header));

    /* check for homing frame */
    reset_flag = is_pcm_frame_homing(in);

    if(apiGSMFREncode(EncObj,(const short *)in->pBuffer,(unsigned char *)out->pBuffer,&vad,&hangover ) != APIGSMFR_StsNoErr){
        return USC_NoOperation;
    }
    if(reset_flag != 0) {
        apiGSMFREncoder_Init(EncObj, &gsmfr_header->params);
    }
    in->nbytes = GSMFR_EXT_FRAME_LEN*sizeof(short);
    out->bitrate=in->bitrate;
    out->nbytes=GSMFR_PACKED_FRAME_LEN;
    if(gsmfr_header->params.vadMode == GSMFR_VAD_ON ){
        if(vad){/* voice activity detected */
            out->frametype=1;/* speech frame */
        }else{
            if(hangover) {
                out->frametype=2;/* silence frame as speech frame, SP=1 */
            }else{
                out->frametype=0;/* SID frame, SP=0   */
            }
        }
    }else{
        out->frametype=-1;
    }

    return USC_NoError;
}

static void B2R(const unsigned char *pSrc, short *out_buff_cur)
{
   frame *pf = (frame*)out_buff_cur;
   pf->LARc[0]  = (short)((pSrc[0] & 0xF) << 2);
   pf->LARc[0] |= (pSrc[1] >> 6) & 0x3;
   pf->LARc[1]  = (short)(pSrc[1] & 0x3F);
   pf->LARc[2]  =(short)( (pSrc[2] >> 3) & 0x1F);
   pf->LARc[3]  =(short)( (pSrc[2] & 0x7) << 2);
   pf->LARc[3] |=(short)( (pSrc[3] >> 6) & 0x3);
   pf->LARc[4]  =(short)( (pSrc[3] >> 2) & 0xF);
   pf->LARc[5]  =(short)( (pSrc[3] & 0x3) << 2);
   pf->LARc[5] |=(short)( (pSrc[4] >> 6) & 0x3);
   pf->LARc[6]  =(short)( (pSrc[4] >> 3) & 0x7);
   pf->LARc[7]  =(short)( pSrc[4] & 0x7);
   pf->Ncr0     =(short)( (pSrc[5] >> 1) & 0x7F);
   pf->bcr0     =(short)( (pSrc[5] & 0x1) << 1);
   pf->bcr0    |=(short)( (pSrc[6] >> 7) & 0x1);
   pf->Mcr0     =(short)( (pSrc[6] >> 5) & 0x3);
   pf->xmaxc0   =(short)( (pSrc[6] & 0x1F) << 1);
   pf->xmaxc0  |=(short)( (pSrc[7] >> 7) & 0x1);
   pf->xmcr0[0]  =(short)( (pSrc[7] >> 4) & 0x7);
   pf->xmcr0[1]  =(short)( (pSrc[7] >> 1) & 0x7);
   pf->xmcr0[2]  =(short)( (pSrc[7] & 0x1) << 2);
   pf->xmcr0[2] |=(short)( (pSrc[8] >> 6) & 0x3);
   pf->xmcr0[3]  =(short)( (pSrc[8] >> 3) & 0x7);
   pf->xmcr0[4]  =(short)( pSrc[8] & 0x7);
   pf->xmcr0[5]  =(short)( (pSrc[9] >> 5) & 0x7);
   pf->xmcr0[6]  =(short)( (pSrc[9] >> 2) & 0x7);
   pf->xmcr0[7]  =(short)( (pSrc[9] & 0x3) << 1);
   pf->xmcr0[7] |=(short)( (pSrc[10] >> 7) & 0x1);
   pf->xmcr0[8]  =(short)( (pSrc[10] >> 4) & 0x7);
   pf->xmcr0[9]  =(short)( (pSrc[10] >> 1) & 0x7);
   pf->xmcr0[10] =(short)( (pSrc[10] & 0x1) << 2);
   pf->xmcr0[10]|=(short)( (pSrc[11] >> 6) & 0x3);
   pf->xmcr0[11] =(short)( (pSrc[11] >> 3) & 0x7);
   pf->xmcr0[12] =(short)( pSrc[11] & 0x7);
   pf->Ncr1      =(short)( (pSrc[12] >> 1) & 0x7F);
   pf->bcr1      =(short)( (pSrc[12] & 0x1) << 1);
   pf->bcr1     |=(short)( (pSrc[13] >> 7) & 0x1);
   pf->Mcr1      =(short)( (pSrc[13] >> 5) & 0x3);
   pf->xmaxc1    =(short)( (pSrc[13] & 0x1F) << 1);
   pf->xmaxc1   |=(short)( (pSrc[14] >> 7) & 0x1);
   pf->xmcr1[0]  =(short)( (pSrc[14] >> 4) & 0x7);
   pf->xmcr1[1]  =(short)( (pSrc[14] >> 1) & 0x7);
   pf->xmcr1[2]  =(short)( (pSrc[14] & 0x1) << 2);
   pf->xmcr1[2] |=(short)( (pSrc[15] >> 6) & 0x3);
   pf->xmcr1[3]  =(short)( (pSrc[15] >> 3) & 0x7);
   pf->xmcr1[4]  =(short)( pSrc[15] & 0x7);
   pf->xmcr1[5]  =(short)( (pSrc[16] >> 5) & 0x7);
   pf->xmcr1[6]  =(short)( (pSrc[16] >> 2) & 0x7);
   pf->xmcr1[7]  =(short)( (pSrc[16] & 0x3) << 1);
   pf->xmcr1[7] |=(short)( (pSrc[17] >> 7) & 0x1);
   pf->xmcr1[8]  =(short)( (pSrc[17] >> 4) & 0x7);
   pf->xmcr1[9]  =(short)( (pSrc[17] >> 1) & 0x7);
   pf->xmcr1[10] =(short)( (pSrc[17] & 0x1) << 2);
   pf->xmcr1[10]|=(short)( (pSrc[18] >> 6) & 0x3);
   pf->xmcr1[11] =(short)( (pSrc[18] >> 3) & 0x7);
   pf->xmcr1[12] =(short)( pSrc[18] & 0x7);
   pf->Ncr2      =(short)( (pSrc[19] >> 1) & 0x7F);
   pf->bcr2      =(short)( (pSrc[19] & 0x1) << 1);
   pf->bcr2     |=(short)( (pSrc[20] >> 7) & 0x1);
   pf->Mcr2      =(short)( (pSrc[20] >> 5) & 0x3);
   pf->xmaxc2    =(short)( (pSrc[20] & 0x1F) << 1);
   pf->xmaxc2   |=(short)( (pSrc[21] >> 7) & 0x1);
   pf->xmcr2[0]  =(short)( (pSrc[21] >> 4) & 0x7);
   pf->xmcr2[1]  =(short)( (pSrc[21] >> 1) & 0x7);
   pf->xmcr2[2]  =(short)( (pSrc[21] & 0x1) << 2);
   pf->xmcr2[2] |=(short)( (pSrc[22] >> 6) & 0x3);
   pf->xmcr2[3]  =(short)( (pSrc[22] >> 3) & 0x7);
   pf->xmcr2[4]  =(short)( pSrc[22] & 0x7);
   pf->xmcr2[5]  =(short)( (pSrc[23] >> 5) & 0x7);
   pf->xmcr2[6]  =(short)( (pSrc[23] >> 2) & 0x7);
   pf->xmcr2[7]  =(short)( (pSrc[23] & 0x3) << 1);
   pf->xmcr2[7] |=(short)( (pSrc[24] >> 7) & 0x1);
   pf->xmcr2[8]  =(short)( (pSrc[24] >> 4) & 0x7);
   pf->xmcr2[9]  =(short)( (pSrc[24] >> 1) & 0x7);
   pf->xmcr2[10] =(short)( (pSrc[24] & 0x1) << 2);
   pf->xmcr2[10] |=(short)( (pSrc[25] >> 6) & 0x3);
   pf->xmcr2[11]  =(short)( (pSrc[25] >> 3) & 0x7);
   pf->xmcr2[12]  =(short)( pSrc[25] & 0x7);
   pf->Ncr3       =(short)( (pSrc[26] >> 1) & 0x7F);
   pf->bcr3       =(short)( (pSrc[26] & 0x1) << 1);
   pf->bcr3      |=(short)( (pSrc[27] >> 7) & 0x1);
   pf->Mcr3       =(short)( (pSrc[27] >> 5) & 0x3);
   pf->xmaxc3     =(short)( (pSrc[27] & 0x1F) << 1);
   pf->xmaxc3    |=(short)( (pSrc[28] >> 7) & 0x1);
   pf->xmcr3[0]   =(short)( (pSrc[28] >> 4) & 0x7);
   pf->xmcr3[1]   =(short)( (pSrc[28] >> 1) & 0x7);
   pf->xmcr3[2]   =(short)( (pSrc[28] & 0x1) << 2);
   pf->xmcr3[2]  |=(short)( (pSrc[29] >> 6) & 0x3);
   pf->xmcr3[3]   =(short)( (pSrc[29] >> 3) & 0x7);
   pf->xmcr3[4]   =(short)( pSrc[29] & 0x7);
   pf->xmcr3[5]   =(short)( (pSrc[30] >> 5) & 0x7);
   pf->xmcr3[6]   =(short)( (pSrc[30] >> 2) & 0x7);
   pf->xmcr3[7]   =(short)( (pSrc[30] & 0x3) << 1);
   pf->xmcr3[7]  |=(short)( (pSrc[31] >> 7) & 0x1);
   pf->xmcr3[8]   =(short)( (pSrc[31] >> 4) & 0x7);
   pf->xmcr3[9]   =(short)( (pSrc[31] >> 1) & 0x7);
   pf->xmcr3[10]  =(short)( (pSrc[31] & 0x1) << 2);
   pf->xmcr3[10] |=(short)( (pSrc[32] >> 6) & 0x3);
   pf->xmcr3[11]  =(short)( (pSrc[32] >> 3) & 0x7);
   pf->xmcr3[12]  =(short)( pSrc[32] & 0x7);

   return;
}
__INLINE short Rand_16s(short *seed) /* G.729 */
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -