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

📄 uscg711.c

📁 G.711,G.723.1,G.726,G.729,GSM CODEC C/C++ code
💻 C
📖 第 1 页 / 共 2 页
字号:
      G711Decoder_Obj *DecObj = (G711Decoder_Obj *)((char*)*handle + sizeof(G711_Handle_Header));
      apiG711Decoder_Init((G711Decoder_Obj*)DecObj,
            (G711Codec_Type)G711_MULAW_CODEC);
      apiG711Decoder_Mode(DecObj, g711_header->pf);
   } else {
      return USC_NoOperation;
   }
   return USC_NoError;
}
static USC_Status ReinitA(const USC_Modes *modes, USC_Handle handle )
{
   G711_Handle_Header *g711_header;

   if(modes==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   g711_header = (G711_Handle_Header*)handle;
   g711_header->law = G711_ALAW_CODEC;
   g711_header->vad = modes->vad;
   g711_header->pf = modes->pf;

   if (g711_header->direction == 0) { /* encode only */
      G711Encoder_Obj *EncObj = (G711Encoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Encoder_Init((G711Encoder_Obj*)EncObj, (G711Codec_Type)g711_header->law, (G711Encode_Mode)modes->vad);
   } else if (g711_header->direction == 1) { /* decode only */
      G711Decoder_Obj *DecObj = (G711Decoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Decoder_Init((G711Decoder_Obj*)DecObj, (G711Codec_Type)g711_header->law);
      apiG711Decoder_Mode(DecObj, modes->pf);
   } else {
      return USC_NoOperation;
   }
   return USC_NoError;
}
static USC_Status ReinitU(const USC_Modes *modes, USC_Handle handle )
{
   G711_Handle_Header *g711_header;

   if(modes==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   g711_header = (G711_Handle_Header*)handle;
   g711_header->law = G711_MULAW_CODEC;
   g711_header->vad = modes->vad;
   g711_header->pf = modes->pf;

   if (g711_header->direction == 0) { /* encode only */
      G711Encoder_Obj *EncObj = (G711Encoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Encoder_Init((G711Encoder_Obj*)EncObj, (G711Codec_Type)g711_header->law, (G711Encode_Mode)modes->vad);
   } else if (g711_header->direction == 1) { /* decode only */
      G711Decoder_Obj *DecObj = (G711Decoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Decoder_Init((G711Decoder_Obj*)DecObj, (G711Codec_Type)g711_header->law);
      apiG711Decoder_Mode(DecObj, modes->pf);
   } else {
      return USC_NoOperation;
   }
   return USC_NoError;
}
static USC_Status ControlA(const USC_Modes *modes, USC_Handle handle )
{
   G711_Handle_Header *g711_header;

   if(modes==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   g711_header = (G711_Handle_Header*)handle;

   g711_header->law = G711_ALAW_CODEC;
   g711_header->vad = modes->vad;
   g711_header->pf = modes->pf;

   if(g711_header->direction == 0) {
      G711Encoder_Obj *EncObj = (G711Encoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Encoder_Mode(EncObj, (G711Encode_Mode)g711_header->vad);
   } else if(g711_header->direction == 1) {
      G711Decoder_Obj *DecObj = (G711Decoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Decoder_Mode(DecObj, g711_header->pf);
   } else
      return USC_NoOperation;

   return USC_NoError;
}
static USC_Status ControlU(const USC_Modes *modes, USC_Handle handle )
{
   G711_Handle_Header *g711_header;

   if(modes==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   g711_header = (G711_Handle_Header*)handle;

   g711_header->law = G711_MULAW_CODEC;
   g711_header->vad = modes->vad;
   g711_header->pf = modes->pf;

   if(g711_header->direction == 0) {
      G711Encoder_Obj *EncObj = (G711Encoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Encoder_Mode(EncObj, (G711Encode_Mode)g711_header->vad);
   } else if(g711_header->direction == 1) {
      G711Decoder_Obj *DecObj = (G711Decoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));
      apiG711Decoder_Mode(DecObj, g711_header->pf);
   } else
      return USC_NoOperation;

   return USC_NoError;
}

static int GetBitstreamSize_G711(int frametype)
{
   if(frametype==G711_Voice_Frame) return BITSTREAM_SIZE;
   else return 2;
}
static USC_Status Encode(USC_Handle handle, USC_PCMStream *in, USC_Bitstream *out)
{
   G711_Handle_Header *g711_header;
   G711Encoder_Obj *EncObj;

   if(in==NULL) return USC_BadDataPointer;
   if(out==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   if(in->pcmType.bitPerSample!=G711_BITS_PER_SAMPLE) return USC_UnsupportedPCMType;
   if(in->pcmType.sample_frequency!=G711_SAMPLE_FREQUENCE) return USC_UnsupportedPCMType;
   if(in->bitrate != pTblRates_G711[0].bitrate) return USC_UnsupportedBitRate;

   if(in->nbytes < G711_SPEECH_FRAME*sizeof(short)) {
      in->nbytes = 0;
      return USC_NoOperation;
   }

   g711_header = (G711_Handle_Header*)handle;
   EncObj = (G711Encoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));

   if(apiG711Encode(EncObj,(const short*)in->pBuffer,out->pBuffer,&out->frametype) != APIG711_StsNoErr){
      return USC_NoOperation;
   }
   out->bitrate = in->bitrate;
   out->nbytes = GetBitstreamSize_G711(out->frametype);
   in->nbytes = G711_SPEECH_FRAME*sizeof(short);
   return USC_NoError;
}
static __ALIGN32 CONST unsigned char LostFrame_G711[BITSTREAM_SIZE] = {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0,
   0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0,
   0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 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 USC_Status Decode(USC_Handle handle, USC_Bitstream *in, USC_PCMStream *out)
{
   G711_Handle_Header *g711_header;
   G711Decoder_Obj *DecObj;

   if(out==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   g711_header = (G711_Handle_Header*)handle;
   DecObj = (G711Decoder_Obj *)((char*)handle + sizeof(G711_Handle_Header));

   if(in==NULL) {
      if(apiG711Decode(DecObj,LostFrame_G711,G711_Bad_Frame,
                                    (unsigned short*)out->pBuffer) != APIG711_StsNoErr){
         return USC_NoOperation;
      }
      out->bitrate = pTblRates_G711[0].bitrate;
   } else {
      if(apiG711Decode(DecObj,(const unsigned char*)in->pBuffer,in->frametype,
                                    (unsigned short*)out->pBuffer) != APIG711_StsNoErr){
         return USC_NoOperation;
      }
      out->bitrate = in->bitrate;
      in->nbytes = GetBitstreamSize_G711(in->frametype);
   }
   out->pcmType.bitPerSample = G711_BITS_PER_SAMPLE;
   out->pcmType.sample_frequency = G711_SAMPLE_FREQUENCE;
   out->nbytes = G711_SPEECH_FRAME*sizeof(short);
   return USC_NoError;
}

static USC_Status CalsOutStreamSize(const USC_Option *options, int nbytesSrc, int *nbytesDst)
{
   int nBlocks, nSamples;

   if(options->direction==0) { /*Encode: src - PCMStream, dst - bitstream*/
      if(options->modes.vad>1) return USC_UnsupportedVADType;

      nSamples = nbytesSrc / (G711_BITS_PER_SAMPLE >> 3);
      nBlocks = nSamples / G711_SPEECH_FRAME;

      if (0 == nBlocks) return USC_NoOperation;

      if (0 != nSamples % G711_SPEECH_FRAME) {
         nBlocks++;
      }

      *nbytesDst = nBlocks * GetBitstreamSize_G711(G711_Voice_Frame);
   } else if(options->direction==1) {/*Decode: src - bitstream, dst - PCMStream*/
      if(options->modes.vad==0) { /*VAD off*/
         nBlocks = nbytesSrc / GetBitstreamSize_G711(G711_Voice_Frame);
      } else if(options->modes.vad==1) { /*VAD on*/
         nBlocks = nbytesSrc / GetBitstreamSize_G711(G711_SID_Frame);
      } else return USC_UnsupportedVADType;

      if (0 == nBlocks) return USC_NoOperation;

      nSamples = nBlocks * G711_SPEECH_FRAME;
      *nbytesDst = nSamples * (G711_BITS_PER_SAMPLE >> 3);
   } else if(options->direction==2) {/* Both: src - PCMStream, dst - PCMStream*/
      nSamples = nbytesSrc / (G711_BITS_PER_SAMPLE >> 3);
      nBlocks = nSamples / G711_SPEECH_FRAME;

      if (0 == nBlocks) return USC_NoOperation;

      if (0 != nSamples % G711_SPEECH_FRAME) {
         nBlocks++;
      }
      *nbytesDst = nBlocks * G711_SPEECH_FRAME * (G711_BITS_PER_SAMPLE >> 3);
   } else return USC_NoOperation;

   return USC_NoError;
}
static USC_Status GetOutStreamSize(const USC_Option *options, int bitrate, int nbytesSrc, int *nbytesDst)
{
   if(options==NULL) return USC_BadDataPointer;
   if(nbytesDst==NULL) return USC_BadDataPointer;
   if(nbytesSrc <= 0) return USC_NoOperation;

    if(bitrate != pTblRates_G711[0].bitrate) return USC_UnsupportedBitRate;

   return CalsOutStreamSize(options, nbytesSrc, nbytesDst);
}

USC_Status SetFrameSize(const USC_Option *options, USC_Handle handle, int frameSize)
{
   if(options==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;

   return USC_NoError;
}

⌨️ 快捷键说明

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