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

📄 uscgsmamr.c

📁 audio-video-codecs.rar语音编解码器
💻 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) 2005-2007 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 ippEULA.rtf or ippEULA.txt 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 "gsmamrusc.h"

#define GSMAMR_NUM_RATES          8
#define GSMAMR_BITS_PER_SAMPLE   16
#define GSMAMR_SAMPLE_FREQ     8000
#define GSMAMR_NCHANNELS          1


static USC_Status GetInfoSize(Ipp32s *pSize);
static USC_Status GetInfo(USC_Handle handle, USC_CodecInfo *pInfo);
static USC_Status NumAlloc(const USC_Option *options, Ipp32s *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, Ipp32s bitrate, Ipp32s nbytesSrc, Ipp32s *nbytesDst);
static USC_Status SetFrameSize(const USC_Option *options, USC_Handle handle, Ipp32s frameSize);

typedef struct {
    Ipp16s sid_update_counter;
    TXFrameType prev_ft;
} sid_syncState;

typedef struct {
    USC_Direction direction;
    Ipp32s trunc;
    Ipp32s bitrate;
    Ipp32s vad;
    Ipp32s bitrate_old;
    Ipp16s reset_flag;
    Ipp16s reset_flag_old;
    sid_syncState sid_state;
} GSMAMR_Handle_Header;

static Ipp32s sid_sync_init (sid_syncState *st);
static void sid_sync(sid_syncState *st, GSMAMR_Rate_t mode, TXFrameType *tx_frame_type);
static Ipp32s is_pcm_frame_homing (Ipp16s *input_frame);
static Ipp16s is_bitstream_frame_homing (Ipp8u* bitstream, Ipp32s mode);

/* global usc vector table */
USCFUN USC_Fxns USC_GSMAMR_Fxns=
{
    {
        USC_Codec,
        (GetInfoSize_func) GetInfoSize,
        (GetInfo_func)     GetInfo,
        (NumAlloc_func)    NumAlloc,
        (MemAlloc_func)    MemAlloc,
        (Init_func)        Init,
        (Reinit_func)      Reinit,
        (Control_func)     Control
    },
    Encode,
    Decode,
    GetOutStreamSize,
    SetFrameSize

};


static __ALIGN32 CONST Ipp32s bitsLen[N_MODES]={
   95, 103, 118, 134, 148, 159, 204, 244, 35
};

static __ALIGN32 CONST Ipp16s 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 __ALIGN32 CONST USC_PCMType pcmTypesTbl_GSMAMR[1]={
   {GSMAMR_SAMPLE_FREQ,GSMAMR_BITS_PER_SAMPLE,GSMAMR_NCHANNELS}
};

static Ipp32s CheckRate_GSMAMR(Ipp32s rate_bps)
{
    Ipp32s 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 Ipp32s CheckIdxRate_GSMAMR(Ipp32s idx_rate)
{
    Ipp32s 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 GetInfoSize(Ipp32s *pSize)
{
   USC_CHECK_PTR(pSize);
   *pSize = sizeof(USC_CodecInfo);
   return USC_NoError;
}

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

    USC_CHECK_PTR(pInfo);

    pInfo->name = "IPP_GSMAMR";
    pInfo->params.framesize = GSMAMR_Frame*sizeof(Ipp16s);
    if (handle == NULL) {
      pInfo->params.modes.bitrate = 12200;
      pInfo->params.modes.truncate = 1;
      pInfo->params.direction = USC_DECODE;
      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->params.pcmType.sample_frequency = pcmTypesTbl_GSMAMR[0].sample_frequency;
    pInfo->params.pcmType.nChannels = pcmTypesTbl_GSMAMR[0].nChannels;
    pInfo->params.pcmType.bitPerSample = pcmTypesTbl_GSMAMR[0].bitPerSample;
    pInfo->nPcmTypes = 1;
    pInfo->pPcmTypesTbl = pcmTypesTbl_GSMAMR;
    pInfo->params.modes.hpf = 0;
    pInfo->params.modes.pf = 0;
    pInfo->params.modes.outMode = USC_OUT_NO_CONTROL;
    pInfo->params.law = 0;
    pInfo->nRates = GSMAMR_NUM_RATES;
    pInfo->pRateTbl = (const USC_Rates *)&pTblRates_GSMAMR;
    pInfo->params.nModes = sizeof(USC_Modes)/sizeof(Ipp32s);

    return USC_NoError;
}

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

static USC_Status MemAlloc(const USC_Option *options, USC_MemBank *pBanks)
{
    Ipp32u nbytes;

   USC_CHECK_PTR(options);
   USC_CHECK_PTR(pBanks);

    pBanks->pMem = NULL;
    pBanks->align = 32;
    pBanks->memType = USC_OBJECT;
    pBanks->memSpaceType = USC_NORMAL;
   if (options->direction == USC_ENCODE) /* encode only */
    {
        GSMAMREnc_Params enc_params;
        enc_params.codecType = GSMAMR_CODEC;
        enc_params.mode = options->modes.vad;
        apiGSMAMREncoder_Alloc(&enc_params, &nbytes);
    }
    else if (options->direction == USC_DECODE) /* decode only */
    {
        GSMAMRDec_Params dec_params;
        dec_params.codecType = GSMAMR_CODEC;
        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;
    Ipp32s bitrate_idx;

    USC_CHECK_PTR(options);
    USC_CHECK_PTR(pBanks);
    USC_CHECK_PTR(pBanks->pMem);
    USC_BADARG(pBanks->nbytes<=0, USC_NotInitialized);
    USC_CHECK_HANDLE(handle);
    USC_BADARG(options->modes.vad > 2, USC_UnsupportedVADType);
    USC_BADARG(options->modes.vad < 0, USC_UnsupportedVADType);

    USC_BADARG(options->pcmType.bitPerSample!=GSMAMR_BITS_PER_SAMPLE, USC_UnsupportedPCMType);
    USC_BADARG(options->pcmType.sample_frequency!=GSMAMR_SAMPLE_FREQ, USC_UnsupportedPCMType);
    USC_BADARG(options->pcmType.nChannels!=GSMAMR_NCHANNELS, USC_UnsupportedPCMType);

    *handle = (USC_Handle*)pBanks->pMem;
    gsmamr_header = (GSMAMR_Handle_Header*)*handle;

    bitrate_idx = CheckRate_GSMAMR(options->modes.bitrate);
    USC_BADARG(bitrate_idx < 0, 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 == USC_ENCODE) /* encode only */
    {
        GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((Ipp8s*)*handle + sizeof(GSMAMR_Handle_Header));
        apiGSMAMREncoder_Init((GSMAMREncoder_Obj*)EncObj, gsmamr_header->vad);
    }
    else if (options->direction == USC_DECODE) /* decode only */
    {
        GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((Ipp8s*)*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;
    Ipp32s bitrate_idx;

    USC_CHECK_PTR(modes);
    USC_CHECK_HANDLE(handle);

    USC_BADARG(modes->vad > 2, USC_UnsupportedVADType);
    USC_BADARG(modes->vad < 0, USC_UnsupportedVADType);

    gsmamr_header = (GSMAMR_Handle_Header*)handle;

    bitrate_idx = CheckRate_GSMAMR(modes->bitrate);
    USC_BADARG(bitrate_idx < 0, USC_UnsupportedBitRate);
    gsmamr_header->vad = modes->vad;
    gsmamr_header->bitrate = modes->bitrate;
    gsmamr_header->bitrate_old = 0;

    if (gsmamr_header->direction == USC_ENCODE) /* encode only */
    {
        GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((Ipp8s*)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 == USC_DECODE) /* decode only */
    {
        GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((Ipp8s*)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;
   Ipp32s bitrate_idx;

   USC_CHECK_PTR(modes);
   USC_CHECK_HANDLE(handle);

   USC_BADARG(modes->vad > 2, USC_UnsupportedVADType);
   USC_BADARG(modes->vad < 0, USC_UnsupportedVADType);

   gsmamr_header = (GSMAMR_Handle_Header*)handle;

   bitrate_idx = CheckRate_GSMAMR(modes->bitrate);
   USC_BADARG(bitrate_idx < 0, USC_UnsupportedBitRate);
   gsmamr_header->vad = modes->vad;
   gsmamr_header->bitrate = modes->bitrate;
   gsmamr_header->trunc = modes->truncate;

   if (gsmamr_header->direction == USC_ENCODE) /* encode only */
   {
      GSMAMREncoder_Obj *EncObj = (GSMAMREncoder_Obj *)((Ipp8s*)handle + sizeof(GSMAMR_Handle_Header));
      apiGSMAMREncoder_Mode((GSMAMREncoder_Obj*)EncObj, modes->vad);
   }
   else if (gsmamr_header->direction == USC_DECODE) /* decode only */
   {
      GSMAMRDecoder_Obj *DecObj = (GSMAMRDecoder_Obj *)((Ipp8s*)handle + sizeof(GSMAMR_Handle_Header));
      apiGSMAMRDecoder_Mode((GSMAMRDecoder_Obj*)DecObj, modes->vad);
   } else {
      return USC_NoOperation;
   }
    return USC_NoError;
}

#define IO_MASK (Ipp16u)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 Ipp32s getBitstreamSize(Ipp32s rate, Ipp32s frametype, Ipp32s *outRate)
{
   Ipp32s nbytes;
   Ipp32s usedRate = rate;

   if (frametype != TX_SPEECH_GOOD) {
        usedRate = GSMAMR_RATE_DTX;
   }
    nbytes = ((bitsLen[usedRate] + 7) >> 3);
   //*outRate = usedRate;

⌨️ 快捷键说明

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