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

📄 uscamrwb.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: AMRWB speech codec: USC functions.
//
*/

#include "ownamrwb.h"
#include "amrwbapi.h"

#include "amrwbusc.h"

#define  AMRWB_NUM_RATES  9
#define  AMRWB_SAMPLE_FREQUENCY 16000
#define  AMRWB_BITS_PER_SAMPLE 16
#define  AMRWB_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 updateSidCount;
    TXFrameType prevFrameType;
} SIDSyncState;

typedef struct {
    USC_Direction direction;
    Ipp32s trunc;
    Ipp32s bitrate;
    Ipp32s vad;
    Ipp32s reset_flag;
    Ipp32s reset_flag_old;
    Ipp32s bitrate_old;
    Ipp32s usedRate;
    SIDSyncState sid_state;
    Ipp32s reserved0; // for future extension
    Ipp32s reserved1; // for future extension
} AMRWB_Handle_Header;

static Ipp32s ownSidSyncInit(SIDSyncState *st);
static void   ownSidSync(SIDSyncState *st, Ipp32s valRate, TXFrameType *pTXFrameType);
static Ipp32s ownTestPCMFrameHoming(const Ipp16s *pSrc);
static Ipp32s ownTestBitstreamFrameHoming(Ipp16s* pPrmsvec, Ipp32s valRate);

/* global usc vector table */
USCFUN USC_Fxns USC_AMRWB_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 BitsLenTbl[NUM_OF_MODES]={
   132, 177, 253, 285, 317, 365, 397, 461, 477, 35
};

static __ALIGN32 CONST Ipp16s LostFrame[AMRWB_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,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 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_AMRWB[AMRWB_NUM_RATES]={
    {23850},
    {23050},
    {19850},
    {18250},
    {15850},
    {14250},
    {12650},
    {8850},
    {6600}
};

static __ALIGN32 CONST USC_PCMType pcmTypesTbl_AMRWB[1]={
   {AMRWB_SAMPLE_FREQUENCY,AMRWB_BITS_PER_SAMPLE, AMRWB_NCHANNELS}
};

static void ownConvert15(Ipp32s valRate, const Ipp8s *pSerialvec, Ipp16s* pPrmvec)
{
    Ipp16s i, j, tmp;
    Ipp32s valNumPrms = BitsLenTbl[valRate];
    Ipp32s nbit=0;

    j = 0;
    i = 0;

    tmp = (Ipp16s)(valNumPrms - 15);
    while (tmp > j)
    {
        pPrmvec[i] = amrExtractBits(&pSerialvec,&nbit,15);
        j += 15;
        i++;
    }
    tmp = (Ipp16s)(valNumPrms - j);
    pPrmvec[i] = amrExtractBits(&pSerialvec,&nbit,tmp);
    pPrmvec[i] <<= (15 - tmp);
}

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)
{
    AMRWB_Handle_Header *amrwb_header;

    USC_CHECK_PTR(pInfo);

    pInfo->name = "IPP_AMRWB";
    pInfo->params.framesize = AMRWB_Frame*sizeof(Ipp16s);
    if (handle == NULL) {
        pInfo->params.modes.bitrate = 6600;
        pInfo->params.modes.truncate = 1;
        pInfo->params.direction = USC_DECODE;
        pInfo->params.modes.vad = 1;
    } else {
      amrwb_header = (AMRWB_Handle_Header*)handle;
      pInfo->params.modes.bitrate = amrwb_header->bitrate;
      pInfo->params.modes.truncate = amrwb_header->trunc;
      pInfo->params.direction = amrwb_header->direction;
      pInfo->params.modes.vad = amrwb_header->vad;
    }
    pInfo->maxbitsize = BITSTREAM_SIZE;
    pInfo->params.pcmType.sample_frequency = pcmTypesTbl_AMRWB[0].sample_frequency;
    pInfo->params.pcmType.nChannels = pcmTypesTbl_AMRWB[0].nChannels;
    pInfo->params.pcmType.bitPerSample = pcmTypesTbl_AMRWB[0].bitPerSample;
    pInfo->nPcmTypes = 1;
    pInfo->pPcmTypesTbl = pcmTypesTbl_AMRWB;
    pInfo->params.modes.hpf = 0;
    pInfo->params.modes.pf = 0;
    pInfo->params.modes.outMode = USC_OUT_NO_CONTROL;
    pInfo->params.law = 0;
    pInfo->nRates = AMRWB_NUM_RATES;
    pInfo->pRateTbl = (const USC_Rates *)&pTblRates_AMRWB;
    pInfo->params.nModes = sizeof(USC_Modes)/sizeof(Ipp32s);

    return USC_NoError;
}

static Ipp32s CheckRate_AMRWB(Ipp32s rate_bps)
{
    Ipp32s rate;

    switch(rate_bps) {
      case 6600:  rate = 0; break;
      case 8850:  rate = 1; break;
      case 12650: rate = 2; break;
      case 14250: rate = 3; break;
      case 15850: rate = 4; break;
      case 18250: rate = 5; break;
      case 19850: rate = 6; break;
      case 23050: rate = 7; break;
      case 23850: rate = 8; break;
      default: rate = -1; break;
    }
    return rate;
}

static Ipp32s CheckIdxRate_AMRWB(Ipp32s idx_rate)
{
    Ipp32s rate;

    switch(idx_rate) {
      case 0: rate = 6600; break;
      case 1: rate = 8850; break;
      case 2: rate = 12650; break;
      case 3: rate = 14250; break;
      case 4: rate = 15850; break;
      case 5: rate = 18250; break;
      case 6: rate = 19850; break;
      case 7: rate = 23050; break;
      case 8: rate = 23850; break;
      default: rate = -1; break;
    }
    return rate;
}

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);
   USC_BADARG(options->modes.vad > 1, USC_UnsupportedVADType);

   pBanks->pMem = NULL;
   pBanks->align = 32;
   pBanks->memType = USC_OBJECT;
   pBanks->memSpaceType = USC_NORMAL;

   if (options->direction == USC_ENCODE) /* encode only */
   {
      AMRWBEnc_Params enc_params;
      enc_params.codecType = (AMRWBCodec_Type)0;
      enc_params.mode = options->modes.vad;
      apiAMRWBEncoder_Alloc(&enc_params, &nbytes);
   }
   else if (options->direction == USC_DECODE) /* decode only */
   {
      AMRWBDec_Params dec_params;
      dec_params.codecType = (AMRWBCodec_Type)0;
      dec_params.mode = options->modes.vad;
      apiAMRWBDecoder_Alloc(&dec_params, &nbytes);
   } else {
      return USC_NoOperation;
   }
   pBanks->nbytes = nbytes + sizeof(AMRWB_Handle_Header); /* include header in handle */
   return USC_NoError;
}

static USC_Status Init(const USC_Option *options, const USC_MemBank *pBanks, USC_Handle *handle)
{
   AMRWB_Handle_Header *amrwb_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 > 1, USC_UnsupportedVADType);
   USC_BADARG(options->modes.vad < 0, USC_UnsupportedVADType);

   USC_BADARG(options->pcmType.bitPerSample!=AMRWB_BITS_PER_SAMPLE, USC_UnsupportedPCMType);
   USC_BADARG(options->pcmType.sample_frequency!=AMRWB_SAMPLE_FREQUENCY, USC_UnsupportedPCMType);
   USC_BADARG(options->pcmType.nChannels!=AMRWB_NCHANNELS, USC_UnsupportedPCMType);

    *handle = (USC_Handle*)pBanks->pMem;
    amrwb_header = (AMRWB_Handle_Header*)*handle;

    bitrate_idx = CheckRate_AMRWB(options->modes.bitrate);
     if(bitrate_idx < 0) return USC_UnsupportedBitRate;
    amrwb_header->direction = options->direction;
    amrwb_header->trunc = options->modes.truncate;
    amrwb_header->vad = options->modes.vad;
     amrwb_header->bitrate = options->modes.bitrate;
     amrwb_header->bitrate_old = 0;

    if (options->direction == USC_ENCODE) /* encode only */
    {
        AMRWBEncoder_Obj *EncObj = (AMRWBEncoder_Obj *)((Ipp8s*)*handle + sizeof(AMRWB_Handle_Header));
        apiAMRWBEncoder_Init((AMRWBEncoder_Obj*)EncObj, amrwb_header->vad);
    }
    else if (options->direction == USC_DECODE) /* decode only */
    {
        AMRWBDecoder_Obj *DecObj = (AMRWBDecoder_Obj *)((Ipp8s*)*handle + sizeof(AMRWB_Handle_Header));
        apiAMRWBDecoder_Init((AMRWBDecoder_Obj*)DecObj);
          amrwb_header->usedRate = 6600;
        amrwb_header->reset_flag = 0;
        amrwb_header->reset_flag_old = 1;

    } else {
        return USC_NoOperation;
    }
    return USC_NoError;
}

static USC_Status Reinit(const USC_Modes *modes, USC_Handle handle )
{
    AMRWB_Handle_Header *amrwb_header;
    Ipp32s bitrate_idx;

    USC_CHECK_PTR(modes);
    USC_CHECK_HANDLE(handle);

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

    amrwb_header = (AMRWB_Handle_Header*)handle;

    bitrate_idx = CheckRate_AMRWB(modes->bitrate);
    if(bitrate_idx < 0) return USC_UnsupportedBitRate;
    amrwb_header->vad = modes->vad;
    amrwb_header->bitrate = modes->bitrate;
    amrwb_header->bitrate_old = 0;

    if (amrwb_header->direction == USC_ENCODE) /* encode only */
    {
        AMRWBEncoder_Obj *EncObj = (AMRWBEncoder_Obj *)((Ipp8s*)handle + sizeof(AMRWB_Handle_Header));
        amrwb_header->reset_flag = 0;
        ownSidSyncInit(&amrwb_header->sid_state);
        apiAMRWBEncoder_Init((AMRWBEncoder_Obj*)EncObj, modes->vad);
    }
    else if (amrwb_header->direction == USC_DECODE) /* decode only */
    {
        AMRWBDecoder_Obj *DecObj = (AMRWBDecoder_Obj *)((Ipp8s*)handle + sizeof(AMRWB_Handle_Header));
        apiAMRWBDecoder_Init((AMRWBDecoder_Obj*)DecObj);
          amrwb_header->usedRate = 6600;
        amrwb_header->reset_flag = 0;
        amrwb_header->reset_flag_old = 1;
    } else {
        return USC_NoOperation;
    }
    return USC_NoError;
}

static USC_Status Control(const USC_Modes *modes, USC_Handle handle )
{
   AMRWB_Handle_Header *amrwb_header;
   Ipp32s bitrate_idx;

   USC_CHECK_PTR(modes);
   USC_CHECK_HANDLE(handle);

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

   bitrate_idx = CheckRate_AMRWB(modes->bitrate);
   USC_BADARG(bitrate_idx < 0,USC_UnsupportedBitRate);

   amrwb_header = (AMRWB_Handle_Header*)handle;

   amrwb_header->trunc = modes->truncate;
   amrwb_header->vad = modes->vad;
   amrwb_header->bitrate = modes->bitrate;

   if (amrwb_header->direction == USC_ENCODE) { /* encode only */
      AMRWBEncoder_Obj *EncObj;
      EncObj = (AMRWBEncoder_Obj *)((Ipp8s*)handle + sizeof(AMRWB_Handle_Header));
      apiAMRWBEncoder_Mode(EncObj, amrwb_header->vad);
   }

   return USC_NoError;
}

static __ALIGN32 CONST AMRWB_Rate_t usc2amrwb[9]={
    AMRWB_RATE_6600,
    AMRWB_RATE_8850,
    AMRWB_RATE_12650,
    AMRWB_RATE_14250,
    AMRWB_RATE_15850,
    AMRWB_RATE_18250,
    AMRWB_RATE_19850,
    AMRWB_RATE_23050,
    AMRWB_RATE_23850
};

static Ipp32s getBitstreamSize(Ipp32s rate, Ipp32s frametype, Ipp32s *outRate)
{
    Ipp32s nbytes;
    Ipp32s usedRate = rate;

    if (frametype != TX_SPEECH) {
        usedRate = AMRWB_RATE_DTX;
    }

    nbytes = ((BitsLenTbl[usedRate] + 7) / 8);
    *outRate = rate;

    return nbytes;
}

static USC_Status Encode(USC_Handle handle, USC_PCMStream *in, USC_Bitstream *out)
{
    AMRWB_Handle_Header *amrwb_header;
    Ipp32s bitrate_idx;
    AMRWBEncoder_Obj *EncObj;
    Ipp16u work_buf[AMRWB_Frame];
    Ipp32s WrkRate, out_WrkRate;

    USC_CHECK_PTR(in);
    USC_CHECK_PTR(out);
    USC_CHECK_HANDLE(handle);

    USC_BADARG(NULL == in->pBuffer, USC_NoOperation);
    USC_BADARG(NULL == out->pBuffer, USC_NoOperation);

    USC_BADARG(in->nbytes<AMRWB_Frame*sizeof(Ipp16s), USC_NoOperation);

    USC_BADARG(in->pcmType.bitPerSample!=AMRWB_BITS_PER_SAMPLE, USC_UnsupportedPCMType);
    USC_BADARG(in->pcmType.sample_frequency!=AMRWB_SAMPLE_FREQUENCY, USC_UnsupportedPCMType);

⌨️ 快捷键说明

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