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

📄 uscg726.c

📁 C__ code For Audio Coding
💻 C
📖 第 1 页 / 共 2 页
字号:
   } else if(frmLen == 4) {
      for(i=0;i<inLen;i+=8) {
         outCode[0] = (inCode[1] << 4) | (inCode[0] & 0xf);
         outCode[1] = (inCode[3] << 4) | (inCode[2] & 0xf);
         outCode[2] = (inCode[5] << 4) | (inCode[4] & 0xf);
         outCode[3] = (inCode[7] << 4) | (inCode[6] & 0xf);
         outCode += 4;
         inCode += 8;
         packedLen += 4;
      }
   } else if(frmLen == 5) {
      for(i=0;i<inLen;i+=8) {
         outCode[0] = (inCode[0] & 0x1f) | ((inCode[1] & 0x7)<<5);
         outCode[1] = ((inCode[1] & 0x18)>>3) | ((inCode[2] & 0x1f)<<2) | ((inCode[3] & 0x1)<<7);
         outCode[2] = ((inCode[3] & 0x1e)>>1) | ((inCode[4] & 0xf)<<4);
         outCode[3] = ((inCode[4] & 0x10)>>4) | ((inCode[5] & 0x1f)<<1) | ((inCode[6] & 0x3)<<6);
         outCode[4] = ((inCode[6] & 0x1c)>>2) | ((inCode[7] & 0x1f)<<3);
         outCode += 5;
         inCode += 8;
         packedLen += 5;
      }
   }

   *outLen = packedLen;
   return 1;
}

static USC_Status Encode(USC_Handle handle, USC_PCMStream *in, USC_Bitstream *out)
{
    G726_Handle_Header *g726_header;
    int bitrate_idx, inputLen, outLen = 0, foo = 0;
    IPP_ALIGNED_ARRAY(16, unsigned short, work_buf, 10*G726_FRAME_SIZE);/*10 ms*/
    IPP_ALIGNED_ARRAY(16, unsigned char, outBuffer, 10*G726_FRAME_SIZE);/*10 ms*/
    IppsEncoderState_G726_16s *EncObj;
    short *pPCMPtr;
    char  *pBitstrPtr;

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

    g726_header = (G726_Handle_Header*)handle;
    EncObj = (IppsEncoderState_G726_16s *)((char*)handle + sizeof(G726_Handle_Header));

    inputLen = in->nbytes;
    if(inputLen <= 0) return USC_NoOperation;

    if(in->nbytes > g726_header->frameSize)
      inputLen = g726_header->frameSize;
    else
      inputLen = in->nbytes;

    if(g726_header->bitrate != in->bitrate) {
      bitrate_idx = CheckRate_G726(in->bitrate);
      if(bitrate_idx < 0) return USC_UnsupportedBitRate;
      ippsEncodeInit_G726_16s8u(EncObj, usc2g726Rate[bitrate_idx]);
      g726_header->bitrate = in->bitrate;
    }

    inputLen = in->nbytes;
    pPCMPtr = (short*)in->pBuffer;
    pBitstrPtr = out->pBuffer;
    while(inputLen >= (10*G726_FRAME_SIZE*sizeof(short))) {
       ippsRShiftC_16s(pPCMPtr, 2, work_buf, 10*G726_FRAME_SIZE);
       if(ippsEncode_G726_16s8u(EncObj,work_buf,(unsigned char*)outBuffer,10*G726_FRAME_SIZE) != ippStsNoErr){
         return USC_NoOperation;
       }
       PackCodeword_tst((const char*)outBuffer, pBitstrPtr, in->bitrate, 10*G726_FRAME_SIZE, &foo);

       outLen += foo;
       pBitstrPtr += foo;
       inputLen -= (10*G726_FRAME_SIZE*sizeof(short));
       pPCMPtr += (10*G726_FRAME_SIZE);
    }
    while(inputLen > 0) {
       ippsRShiftC_16s(pPCMPtr, 2, work_buf, G726_FRAME_SIZE);
       if(ippsEncode_G726_16s8u(EncObj,work_buf,(unsigned char*)outBuffer,G726_FRAME_SIZE) != ippStsNoErr){
         return USC_NoOperation;
       }
       PackCodeword_tst((const char*)outBuffer, pBitstrPtr, in->bitrate, G726_FRAME_SIZE, &foo);
       outLen += foo;
       pBitstrPtr += foo;
       inputLen -= (G726_FRAME_SIZE*sizeof(short));
       pPCMPtr += (G726_FRAME_SIZE);
    }
    out->frametype = 0;
    out->bitrate = in->bitrate;
    out->nbytes = outLen;

    return USC_NoError;
}

static int UnpackCodeword(const char* inCode, char* outCode, int bitrate, int bitstrLen)
{

   int len, i;
   len = OutFrameSize(bitrate);
   if(len==0) return 0;

   switch(bitrate) {
      case 16000: {
         for(i = 0;i < bitstrLen;i+=len) {
            outCode[3] = ((inCode[0] >> 6) & 0x3);
            outCode[2] = ((inCode[0] >> 4) & 0x3);
            outCode[1] = ((inCode[0] >> 2) & 0x3);
            outCode[0] = (inCode[0] & 0x3);
            outCode[7] = ((inCode[1] >> 6) & 0x3);
            outCode[6] = ((inCode[1] >> 4) & 0x3);
            outCode[5] = ((inCode[1] >> 2) & 0x3);
            outCode[4] = (inCode[1] & 0x3);
            inCode += len;
            outCode += 8;
         }
         break;
      }
      case 24000: {
         for(i = 0;i < bitstrLen;i+=len) {
            outCode[0] = (inCode[0] & 0x7);
            outCode[1] = (inCode[0] >> 3) & 0x7;
            outCode[2] = ((inCode[0] >> 6) & 0x3) | ((inCode[1] & 0x1))<<2;
            outCode[3] = (inCode[1] >> 1) & 0x7;
            outCode[4] = (inCode[1] >> 4) & 0x7;
            outCode[5] = ((inCode[1] >> 7) & 0x1) | (inCode[2] & 0x3)<<1;
            outCode[6] = (inCode[2] >> 2) & 0x7;
            outCode[7] = (inCode[2] >> 5) & 0x7;
            inCode += len;
            outCode += 8;
         }
         break;
      }
      case 32000: {
         for(i = 0;i < bitstrLen;i+=len) {
            outCode[1] = ((inCode[0] >> 4) & 0xf);
            outCode[0] = (inCode[0] & 0xf);
            outCode[3] = ((inCode[1] >> 4) & 0xf);
            outCode[2] = (inCode[1] & 0xf);
            outCode[5] = ((inCode[2] >> 4) & 0xf);
            outCode[4] = (inCode[2] & 0xf);
            outCode[7] = ((inCode[3] >> 4) & 0xf);
            outCode[6] = (inCode[3] & 0xf);
            inCode += len;
            outCode += 8;
         }
         break;
      }
      case 40000: {
         for(i = 0;i < bitstrLen;i+=len) {
            outCode[0] = (inCode[0] & 0x1f);
            outCode[1] = ((inCode[0] >> 5) & 0x7) | ((inCode[1] & 0x3)<<3);
            outCode[2] = (inCode[1] >> 2) & 0x1f;
            outCode[3] = ((inCode[1] >> 7) & 0x1) | ((inCode[2] & 0xf)<<1);
            outCode[4] = ((inCode[2] >> 4) & 0xf) | ((inCode[3] & 0x1)<<4);
            outCode[5] = (inCode[3] >> 1) & 0x1f;
            outCode[6] = ((inCode[3] >> 6) & 0x3) | ((inCode[4] & 0x7)<<2);
            outCode[7] = (inCode[4]  >>3) & 0x1f;
            inCode += len;
            outCode += 8;
         }
         break;
      }
   }

   return 1;
}

static USC_Status Decode(USC_Handle handle, USC_Bitstream *in, USC_PCMStream *out)
{
   G726_Handle_Header *g726_header;
   int bitrate_idx, inputLen, frmLen;
   IPP_ALIGNED_ARRAY(16, unsigned char, inBuffer, 10*G726_FRAME_SIZE);
   IppsDecoderState_G726_16s *DecObj;
   char *pBitstrPtr;
   short *pPCMPtr;

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

   g726_header = (G726_Handle_Header*)handle;
   DecObj = (IppsDecoderState_G726_16s *)((char*)handle + sizeof(G726_Handle_Header));

   if(in == NULL) {
      int i;
     /* Lost frame */
      pPCMPtr = (short *)out->pBuffer;
      for(i=0; i <  g726_header->frameSize; i += G726_FRAME_SIZE*sizeof(short)) {
         if(ippsDecode_G726_8u16s(DecObj, (const unsigned char*)LostFrame, pPCMPtr, G726_FRAME_SIZE)!=ippStsNoErr){
            return USC_NoOperation;
         }
         pPCMPtr += G726_FRAME_SIZE;
      }
     out->bitrate = g726_header->bitrate;
     out->nbytes  = g726_header->frameSize;
   } else {

     if(g726_header->bitrate != in->bitrate) {
       bitrate_idx = CheckRate_G726(in->bitrate);
       if(bitrate_idx < 0) return USC_UnsupportedBitRate;
       ippsDecodeInit_G726_8u16s(DecObj, usc2g726Rate[bitrate_idx], Usc2IppLaw(g726_header->law));
       g726_header->bitrate = in->bitrate;
     }

     frmLen = OutFrameSize(in->bitrate);

     inputLen = in->nbytes;
     in->nbytes = (inputLen / frmLen) * frmLen;

     if(((in->nbytes/frmLen)*(G726_FRAME_SIZE*sizeof(short))) > g726_header->frameSize)
         in->nbytes = (g726_header->frameSize / (G726_FRAME_SIZE*sizeof(short)))*frmLen;

     inputLen = (in->nbytes / frmLen) * G726_FRAME_SIZE;/*Convert to the unpucked bitstream len*/

     pBitstrPtr = in->pBuffer;
     pPCMPtr = (short *)out->pBuffer;
     out->nbytes = 0;

     while(inputLen >= (10*G726_FRAME_SIZE)) {/*10 ms loop*/
        UnpackCodeword(pBitstrPtr, inBuffer, in->bitrate, 10*frmLen);

        if(ippsDecode_G726_8u16s(DecObj, (const unsigned char*)inBuffer, pPCMPtr, 10*G726_FRAME_SIZE)!=ippStsNoErr){
            return USC_NoOperation;
        }
        pBitstrPtr += (10*frmLen);
        pPCMPtr += (10*G726_FRAME_SIZE);
        out->nbytes += (10*G726_FRAME_SIZE*sizeof(short));
        inputLen -= (10*G726_FRAME_SIZE);
     }

     while(inputLen >= G726_FRAME_SIZE) {/*1 ms loop*/
        UnpackCodeword(pBitstrPtr, inBuffer, in->bitrate, frmLen);

        if(ippsDecode_G726_8u16s(DecObj, (const unsigned char*)inBuffer, pPCMPtr, G726_FRAME_SIZE)!=ippStsNoErr){
            return USC_NoOperation;
        }
        pBitstrPtr += frmLen;
        pPCMPtr += G726_FRAME_SIZE;
        out->nbytes += (G726_FRAME_SIZE*sizeof(short));
        inputLen -= (G726_FRAME_SIZE);
     }

     out->bitrate = in->bitrate;
   }

   return USC_NoError;
}

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

   if(options->modes.vad>0) return USC_UnsupportedVADType;

   if(options->direction==0) { /*Encode: src - PCMStream, dst - bitstream*/
      nSamples = nbytesSrc / (G726_BITS_PER_SAMPLE >> 3);
      nBlocks = nSamples / G726_FRAME_SIZE;

      if (0 == nBlocks) return USC_NoOperation;

      if (0 != nSamples % G726_FRAME_SIZE) {
         /* Add another block to hold the last compressed fragment*/
         nBlocks++;
      }

      *nbytesDst = nBlocks * OutFrameSize(bitrate_bps);
   } else if(options->direction==1) {/*Decode: src - bitstream, dst - PCMStream*/
      nBlocks = nbytesSrc / OutFrameSize(bitrate_bps);

      if (0 == nBlocks) return USC_NoOperation;

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

      if (0 == nBlocks) return USC_NoOperation;

      if (0 != nSamples % G726_FRAME_SIZE) {
         /* Add another block to hold the last compressed fragment*/
         nBlocks++;
      }
      *nbytesDst = nBlocks * G726_FRAME_SIZE * (G726_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)
{
   int bitrate_idx;

   if(options==NULL) return USC_BadDataPointer;
   if(nbytesDst==NULL) return USC_BadDataPointer;
   if(nbytesSrc <= 0) return USC_NoOperation;

   bitrate_idx = CheckRate_G726(bitrate);
    if(bitrate_idx < 0) return USC_UnsupportedBitRate;

   return CalsOutStreamSize(options, bitrate, nbytesSrc, nbytesDst);
}

USC_Status SetFrameSize(const USC_Option *options, USC_Handle handle, int frameSize)
{
   G726_Handle_Header *g726_header;

   if(options==NULL) return USC_BadDataPointer;
   if(handle==NULL) return USC_InvalidHandler;
   if(frameSize <= G726_FRAME_SIZE*sizeof(short)) return USC_NoOperation;

   g726_header = (G726_Handle_Header*)handle;

   g726_header->frameSize = (frameSize / (G726_FRAME_SIZE*sizeof(short))) * (G726_FRAME_SIZE*sizeof(short));

   return USC_NoError;
}

⌨️ 快捷键说明

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