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

📄 chan_enc.c

📁 语音压缩编码和解码的标准,其中包含部分源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
       cnt  = 0;
       for( k = 0; k <= last; ++k ) {
           for( i = 0; i < nbit_class[k]; ++i ) {
               /* Determine the output of the encoder. The generator */
               /* polynomial is (23,35,27) octal (section 2.5)       */
               /* -------------------------------------------------- */
               temp   = info_bit[ptr1] + info_bit[ptr1-4];
               out[0] = temp + info_bit[ptr1-1];
               out[1] = temp + info_bit[ptr1-2] + info_bit[ptr1-3];
               out[2] = temp + info_bit[ptr1-1] + info_bit[ptr1-2];
               for( j = 0; j < 3; ++j ) {
                   if(( punctur[k][j] & mask[cnt] ) != 0 ) {
                       chan_bit[ptr0++] = out[j] & 1;
                   }
               }
               ++ptr1;
               cnt = ++cnt % 12;
           }
       }

       /* Copy not protected bits: */
       /* ------------------------ */
       for( i = 0; i < n0; ++i ) {
            chan_bit[ptr0++] = tmp[i];
       }


    }else {

       /* no convolutional code has been specified */
       /* ---------------------------------------- */
       ptr0 = BCHLen;
       for( i = 0; i < nbit_class[4]; ++i ) {
            chan_bit[ptr0++] = info_bit[i];
       }
    }

    /* Set eventually unused bits to zero */
    /* ---------------------------------- */
    for (i=ptr0;i<700;i++) chan_bit[i]=0;

    return;
}

/*__________________________________________________________________________
 |                                                                          |
 |        Read one G.723.1 frame from file                                  |       
 |                                                                          |
 |__________________________________________________________________________|
*/
static Word16 GetSpeechFrame (FILE *bit_infile, char InfoBitStream[])
{
   Word16 nread, n, FrType;

   /* Read one byte from bitstream file to find out G.723.1 mode */
   /* ---------------------------------------------------------- */
   n = fread((char*)InfoBitStream,sizeof(char),1,bit_infile);
   if (n != 1) return(1);

   FrType = (Word16) InfoBitStream[0] & 0x3;

   if (FrType == 0) nread = 23;
   if (FrType == 1) nread = 19;
   if (FrType == 2) nread =  3;
   if (FrType == 3) return(0);

   /* Read the remaining bytes from bitstream file */
   /* -------------------------------------------- */
   n = fread((char*)&InfoBitStream[1],sizeof(char),nread,bit_infile);
   if (n != nread) return(1);

  return(0);
}

/*__________________________________________________________________________
 |                                                                          |
 |        Read allowed channel bitrates from file                           |       
 |                                                                          |
 |__________________________________________________________________________|
*/
static void  GetChannelBitrates(char *ConfigName)
{
    FILE   *fp;
    Word32   flag, i, tmp;
    char   InputLine[100];

    if((fp = fopen(ConfigName,"r"))==NULL){
       fprintf(stderr,"Can't open file '%s'\n",ConfigName);
       exit(1);
    }
    flag=0;
    while(flag == 0 ) {
      fgets( InputLine, 100, fp);
      if( InputLine[0] != '#') flag=1;
    }
    sscanf(InputLine, "%d", &tmp);
    ChannelBitrate_53[0] = (Word16) tmp;
    for (i=1;i<2;i++) {
      fgets( InputLine, 100, fp);
      sscanf(InputLine, "%d", &tmp);
      ChannelBitrate_53[i] = (Word16) tmp;
    }

    flag=0;
    while(flag == 0 ) {
      fgets( InputLine, 100, fp);
      if( InputLine[0] != '#') flag=1;
    }
    sscanf(InputLine, "%d", &tmp);
    ChannelBitrate_63[0] = (Word16) tmp;
    for (i=1;i<2;i++) {
      fgets( InputLine, 100, fp);
      sscanf(InputLine, "%d", &tmp);
      ChannelBitrate_63[i] = (Word16) tmp;
    }

    flag=0;
    while(flag == 0 ) {
      fgets( InputLine, 100, fp);
      if( InputLine[0] != '#') flag=1;
    }
    sscanf(InputLine, "%d", &tmp);
    ChannelBitrate_SID[0] = (Word16) tmp;
    for (i=1;i<2;i++) {
      fgets( InputLine, 100, fp);
      sscanf(InputLine, "%d", &tmp);
      ChannelBitrate_SID[i] = (Word16) tmp;
    }

    fclose(fp);
    return;
}


static int Strincmp( const char *s, const char *t, size_t max )
{
    for( ; max > 1; ++s, ++t, --max ) {
        if( toupper( *s ) != toupper( *t ))
            break;
        if( *s == '\0' )
            return( 0 );
    }
    return( toupper( *s ) - toupper( *t ));
}


static FILE *OpenBinfile( char *name, char *mode )
{
    FILE *fp;

    if( toupper( *mode ) == 'W' ) { /* Write access */
        if(( fp = fopen( name, OPEN_WB )) == NULL ) { 
            printf( "Can't open output file '%s'\n", name );
            exit( 1 );
        }
    } else { /* Read access */
        if(( fp = fopen( name, OPEN_RB )) == NULL ) { 
            printf( "Can't open file '%s'\n", name );
            exit( 1 );
        } 
    }
    return( fp );
}


/*___________________________________________________________________________
 |                                                                           |
 |     Main - Program                                                        |
 |                                                                           |
 |___________________________________________________________________________|
*/
int main( int argc, char *argv[] )
{
#ifdef VAX
    Word32  VaxWriteCnt=0;
#endif
    FILE    *bit_infile, *chan_outfile, *chanfile;
    Word32  framecount, frameno = 0;
    Word16  ChannelBitrate,NumChannelBytes,ChanMode,end_bsf,end_cbf;
    char    InfoBitStream[24], ChannelBitStream[200];
    char    outtext[] = " Encoding Frame: %4d\r";

    if( (argc < 5) || (argc > 6) ) {
        fprintf( stderr, "------------------------------------------------------------------------------\n");
        fprintf( stderr, "|Usage: CHAN_ENC InfoBitFile ChanBitFile ConfigFile ChanBitRate[-File] [/f=f]| \n");
        fprintf( stderr, "------------------------------------------------------------------------------\n");
        fprintf( stderr, "\n");
        fprintf( stderr, "   InfoBitFile - encoded G.723.1 bitstream file\n");
        fprintf( stderr, "   ChanBitFile - channel bitstream file\n");
        fprintf( stderr, "   ConfigFile  - configuration file with allowed channel bitrates\n");
        fprintf( stderr, "   ChanBitRate - either: desired channel bit-rate in bps (e.g. 11000)\n");
        fprintf( stderr, "                     or: file name with channel bit-rates\n");
        fprintf( stderr, "   /f=         - number of frames to be encoded (optional) \n");
        return( 1 );
    }

    bit_infile  = OpenBinfile( argv[1], "r");
    chan_outfile = OpenBinfile( argv[2], "w");


    if (isdigit(argv[4][0])){
       /* A fixed channel bitrate has been specified on the command line */
       /* -------------------------------------------------------------- */
       ChanMode = 0;
       ChannelBitrate = (Word16) atoi(argv[4]);
    }else{
       /* A file with individual channel bitrates for each frame */ 
       /* has been specified on the command line                 */
       /* ------------------------------------------------------ */
       ChanMode = 1;
       chanfile = OpenBinfile( argv[4], "r");
    }


    frameno = 0;
    if(argc==6 && !Strincmp( argv[5], "/f=", 3 )){
        frameno = atoi( &argv[5][3] );
        if( frameno < 0 ) {
             printf( "frame > 0\n" );
             return( 1 );
        }
    }
    if(argc==6 && !Strincmp( argv[5], "-TestSequence", 13 )){
       TESTSEQUENCE = True;
    }


    printf( "  _____________________________________________\n" );
    printf( " |                                             |\n" );
    printf( " |       G.723.1 Channel Encoder Simulation    |\n" );
    printf( " |               Version 3.1                   |\n" );
    printf( " |_____________________________________________|\n\n" );

    printf( "    Info Bitstream Input File          : %s\n", argv[1] );
    printf( "    Transmitted Channel Bitstream File : %s\n", argv[2] );
    printf( "    Channel Bitrate Configuration File : %s\n", argv[3] );
    if (ChanMode == 0)
       printf( "    Channel Bitrate                    : %d\n", ChannelBitrate );
    if (ChanMode == 1)
       printf( "    Channel Bitrate File               : %s\n", argv[4] );
    if( frameno == 0 )
       printf( "    Simulated Frames                   : whole file\n" );
    else
       printf( "    Simulated Frames                   : %d\n", frameno );
    if (TESTSEQUENCE == True) {
       printf( "\n" );
       printf( "    Caution:  You are in testing mode !!\n");
       printf( "    ------------------------------------\n");
    }
    printf( "\n" );


    /* Read file with allowed channel bitrates */
    /* --------------------------------------- */
    GetChannelBitrates(argv[3]);


    for( framecount = 0; framecount++ < frameno || frameno == 0; ) {

       /* Read one frame from bitstream file */
       /* ---------------------------------- */
       end_bsf = GetSpeechFrame (bit_infile, InfoBitStream);
       if (end_bsf == 1) break;


       if (((Word16) InfoBitStream[0] & 0x3) < 3) {

          /* ------------------------------- */
          /* The frame has to be transmitted */
          /* ------------------------------- */

          /* Read channel bitrate for actual frame */
          /* ------------------------------------- */
          if (ChanMode == 1) {
             end_cbf = fread((char*)&ChannelBitrate,sizeof(Word16),1,chanfile);
             if (end_cbf != 1) break;
          }

          printf( outtext, framecount+1 );

          /* Channel encoder */
          /* --------------- */
          ChannelEncoder( InfoBitStream, ChannelBitStream, ChannelBitrate, &NumChannelBytes);

       }else{

          /* ----------------------------------------------------------------------- */
          /* The frame is not transmitted. It is signalled to generate comfort noise */
          /* ----------------------------------------------------------------------- */

	  NumChannelBytes     = 2;
	  ChannelBitStream[0] = 0xAF; /* this is a unused BCH word */
	  ChannelBitStream[1] = 0x16; /* this is a unused BCH word */
      }  

#ifdef VAX
       VaxWriteCnt += NumChannelBytes;
#endif
       /* Write packed channel bitstream */
       /* ------------------------------ */
       fwrite ((char*)ChannelBitStream, sizeof(char),(size_t)NumChannelBytes,chan_outfile);

    }
    printf("\n done \n");
    fclose( bit_infile );

#ifdef VAX
    if ((VaxWriteCnt % 2) == 1) {
       /* Adapt to 16 bit boundary */
       /* ------------------------ */
       ChannelBitStream[0]=0;
       fwrite ((char*)ChannelBitStream, sizeof(char),1,chan_outfile);
    }
#endif
    fclose( chan_outfile );
    if (ChanMode == 1) fclose( chanfile );

    return( 0 );
}

⌨️ 快捷键说明

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