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

📄 chan_enc.c

📁 语音压缩编码和解码的标准,其中包含部分源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*-------------------------------------------------------------------------*/ 
/*                                                                         */
/*                   G.723.1 Channel Encoder Simulation V3.1               */
/*                                                                         */
/*-------------------------------------------------------------------------*/
/*                                                                         */
/*-------------------------------------------------------------------------*/
/* (C) Copyright 1995-1996, Bosch Telecom. All rights reserved.            */
/* Contact: Joerg-Martin Mueller, Bosch Telecom, Email: jmm@bk.bosch.de    */
/*-------------------------------------------------------------------------*/


/*___________________________________________________________________________
 |                                                                           |
 | 	Include-Files                                                        |
 |___________________________________________________________________________|
*/
#include "defines.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* G.723.1 Includes */
#include "typedef.h"
#include "basop.h"
#include "cst_lbc.h"
#include "tab_lbc.h"
#include "lbccodec.h"
#include "coder.h"
#include "decod.h"
#include "exc_lbc.h"
#include "util_lbc.h"

#define   tstbit( x, n )    (( x ) &    ( 1 << ( n )))


/*___________________________________________________________________________
 |                                                                           |
 |  	local Functions                                                      |
 |___________________________________________________________________________|
*/
static void  GetChannelBitrates(char *ConfigName);
static void Channel_Pack(Word16 chan_bit[], char ChannelBitStream[], 
                         Word16 SysChannelBitrate, Word16 *NumChannelBytes);
static Word16 GetSpeechFrame (FILE *bit_infile, char InfoBitStream[]);
static void CrcEncoder(Word16 info_bit[], char mode);
static void ChannelEncoder( char InfoBitStream[], char ChannelBitStream[],
                            Word16 ChannelBitrate,  Word16 *NumChannelBytes);
static void bit_allocation( char InfoBitStream[], Word16 info_bit[] );
static void conv_encoder( Word16 bit_class[], Word16 punctur[5][3], 
         Word16 info_bit[], Word16 chan_bit[], char mode);
static void  EndTrellisReorderEnc (Word16 info_bit[], Word16 bit_class[], 
                                   Word16 punctur[5][3], Word16 mode);
static void ChannelCodecInit(Word16 SpeechCodecMode,Word16 ChannelBitrate,
                             Word16 bit_class[],Word16 punctur[5][3]);
static Word16 GetRate (Word16 rbits, Word16 weight, Word16 bit_class, Word16 inv_bit_class, Word16 rnd);
static bitrate_test( Word16 nbit_class[], Word16 punctur[5][3], Word16 last );
static void bitrate_adapt_high (Word16 bit_class[],Word16 punctur[5][3],
                                Word16 cbits, Word16 r[], Word16 last);
static void bitrate_adapt_low (Word16 bit_class[],Word16 punctur[5][3],
                                Word16 cbits, Word16 r[], Word16 last);
static void channel_encoder_adaptation(char InfoBitStream[],char CInfoBitStream[]);
static void Line_Pack_Channel( LINEDEF *Line, char *Vout, Word16 FrType);
static void LSP_Reorder_encoder (LINEDEF *Line);
static void SelectChannelBitrate(Word16 ChannelBitrate,Word16 *SysChannelBitrate,
                                 Word16 *CC_mode, Word16 G723_mode);
static int Strincmp( const char *s, const char *t, size_t max );
static FILE *OpenBinfile( char *name, char *mode );

/*___________________________________________________________________________
 |                                                                           |
 |  Globals                                                                  |
 |___________________________________________________________________________|
*/
/* G.723.1 declarations */
/* -------------------- */
enum  Crate    WrkRate;
Flag  UseHp = True;
Flag  UsePf = True;
Flag  UseVx = True;
Word32  count_clip;

/* Definition of CRC Check Code for error detection (see section 2.4) */
/* ------------------------------------------------------------------ */
#define   CrcLen         5  /* number of parity check bits       */
#define   CrcGen        18  /* generator polynomial              */
#define   CrcWindow53   34  /* number of bits of class 1 for crc */
#define   CrcWindow63   44  /* number of bits of class 1 for crc */
#define   CrcWindowSID  30  /* number of bits of class 1 for crc */

/* Definition of low rate configuration */
/* ------------------------------------ */
#define   NBits5_3       168  /* Info bits with configuration and UB's */
#define   NInfoBits5_3   162  /* Info bits without configuration and UB's */

/* Definition of high rate configuration */
/* ------------------------------------- */
#define   NBits6_3       200  /* Info bits with configuration and UB's */
#define   NInfoBits6_3   193  /* Info bits without configuration and UB's */

/* Definition of SID configuration */
/* ------------------------------- */
#define   NBitsSID        32  /* Info bits with configuration and UB's */
#define   NInfoBitsSID    30  /* Info bits without configuration and UB's */

/* Definition of BCH Code for configuration bits (see section 2.5.4) */
/* ----------------------------------------------------------------- */
#define   BCHLen  13
#define   NumBCHWords  8

/* Channel bitrates (are read from file) */
/* ------------------------------------- */
static Word16   ChannelBitrate_53[2];
static Word16   ChannelBitrate_63[2];
static Word16   ChannelBitrate_SID[2];

/* This flag is only true when exercising a special test sequence */
/* -------------------------------------------------------------- */
Flag TESTSEQUENCE = False;

/*___________________________________________________________________________
 |                                                                           |
 |  	Subroutines                                                          |
 |___________________________________________________________________________|
*/

/*___________________________________________________________________________
 |                                                                           |
 | Generation of channel encoder output format (see section 2.5.4)           |
 |___________________________________________________________________________|
*/
static void Channel_Pack(Word16 chan_bit[], char ChannelBitStream[], 
                         Word16 SysChannelBitrate, Word16 *NumChannelBytes)
{
   Word16 i, pw, pb;
   Word32 L_tmp;

   /* Determine number of bytes to be packed */
   /* -------------------------------------- */

   /* Multiply channel bitrate by 30./8000. */
   /* ------------------------------------- */
   L_tmp = L_mult(SysChannelBitrate,31457);
   *NumChannelBytes = (Word16) L_shr(L_tmp,24);

   /* Multiply channel bitrate by 8000./30. */
   /* ------------------------------------- */
   L_tmp = L_mult(*NumChannelBytes,17067);
   L_tmp = L_shr(L_tmp,7);
   if ((Word16) L_tmp < SysChannelBitrate) *NumChannelBytes = *NumChannelBytes + 1;

   /* Pack channel bits */
   /* ----------------- */
   for (i=0;i<(*NumChannelBytes);i++)  ChannelBitStream[i] = 0;
   pw = 0;
   pb = 0;
   for (i=0;i<*NumChannelBytes*8;i++) {
      ChannelBitStream[pw] = ChannelBitStream[pw] | (chan_bit[i] << pb);
      pb++;
      if (pb == 8) {
         pb = 0;
         pw++;
      }
   }
   return;
}

/*___________________________________________________________________________
 |                                                                           |
 | Quantization of specified channel bitrate to allowed channel bitrate      |
 |               (section 2.5.4)                                             |
 |___________________________________________________________________________|
*/
static void SelectChannelBitrate(Word16 ChannelBitrate,Word16 *SysChannelBitrate,
                                 Word16 *CC_mode, Word16 G723_mode)
{
   Word16   CC_Bitrate[2],i;
 
   /* Set allowed channel bitrates depending on G.723 mode */
   /* ---------------------------------------------------- */
   if (G723_mode == 0) {
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_63[i];
   }
   if (G723_mode == 1) {
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_53[i];
   }
   if (G723_mode == 2) {
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_SID[i];
   }

   /* Quantize requested channel bitrate to next lower allowed channel bitrate */
   /* ------------------------------------------------------------------------ */
   *CC_mode = (-1);
   for (i=1;i>=0;i--) {
      if (ChannelBitrate >= CC_Bitrate[i]){
         *CC_mode = i;
         *SysChannelBitrate = CC_Bitrate[i];
         break;
      }
   }

   if (*CC_mode == (-1)){
      printf (" Requested channel bitrate is too low \n");
      exit(1);
   }      

   return;
}

/*_________________________________________________________________________________
 |                                                                                 |
 | Reorder the last protected bits to consider lower BER at the end of the trellis |
 |               (section 2.5.2)                                                   |
 |_________________________________________________________________________________|
*/
static void  EndTrellisReorderEnc (Word16 info_bit[], Word16 bit_class[], 
                                   Word16 punctur[5][3], Word16 mode)
{
   Word16 last,i,ptr_end,tmp,limit;

    if( (mode & 0x3) == 0) limit=CrcWindow63+CrcLen+20;
    if( (mode & 0x3) == 1) limit=CrcWindow53+CrcLen+20;
    if( (mode & 0x3) == 2) limit=CrcWindowSID+CrcLen+20;


   /* Check which class is the last protected one */
   /* ------------------------------------------- */
   last=0;
   for (i=1;i<5;i++){
      if (punctur[i][0] != 0) last=i;
   }

   /* Determine number of protected bits */
   /* ---------------------------------- */
   ptr_end = 0;
   for (i=0;i<=last;i++) ptr_end = ptr_end + bit_class[i];
   ptr_end = ptr_end - 4;  /* remove tail bits */

   /* Do reordering */
   /* ------------- */
   if (ptr_end >= limit) {
      ptr_end--;
      for (i=0;i<10;i++){
         tmp                    = info_bit[ptr_end-i];
         info_bit[ptr_end-i]    = info_bit[ptr_end-19+i];
         info_bit[ptr_end-19+i] = tmp;
      }
   }
   return;
}



/*_________________________________________________________________________________
 |                                                                                 |
 | Determine the parameters of the channel encoder given the channel bitrate       |
 |               (section 2.5.1)                                                   |
 |_________________________________________________________________________________|
*/
static void ChannelCodecInit(Word16 SpeechCodecMode,Word16 ChannelBitrate,
                             Word16 bit_class[],Word16 punctur[5][3])

{
/* Definition of puncturing matrix for convolutional code (see Table C4) */
/* --------------------------------------------------------------------- */
   static const Word16 PuncturTable[25][3] = {{0x000,0x000,0x000},
                                            {0xd6f,0x690,0x000},
                                            {0xd7f,0x690,0x000},
                                            {0xd7f,0x691,0x000},
                                            {0xd7f,0x695,0x000},
                                            {0xdff,0x695,0x000},
                                            {0xfff,0x695,0x000},
                                            {0xfff,0x69d,0x000},
                                            {0xfff,0x6dd,0x000},
                                            {0xfff,0x6df,0x000},
                                            {0xfff,0x7df,0x000},
                                            {0xfff,0x7ff,0x000},
                                            {0xfff,0xfff,0x000},
                                            {0xfff,0xfff,0x001},
                                            {0xfff,0xfff,0x009},
                                            {0xfff,0xfff,0x109},
                                            {0xfff,0xfff,0x309},
                                            {0xfff,0xfff,0x329},
                                            {0xfff,0xfff,0x729},
                                            {0xfff,0xfff,0x72d},
                                            {0xfff,0xfff,0x72f},
                                            {0xfff,0xfff,0x7af},
                                            {0xfff,0xfff,0x7bf},
                                            {0xfff,0xfff,0x7ff},
                                            {0xfff,0xfff,0xfff}};

/* Definition of low rate configuration (see Table C5a)         */
/* Note that the weight is in 16 bit representation and negated */
/* ------------------------------------------------------------ */
   static const Word16   BitClass53[5]    = { 34+CrcLen, 40, 40, 40+4, 8 };
   static const Word16   Weight53[5]      = { -7864, -10158, -7864, -6882, 0}; 

   static const Word16   InvBitClass53[4] = {26887, 26214, 26214, 26214}; /* 2^20/BitClass53[i]) */
   static const Word16   inv_b0p4_53      = 24386;                        /* 2^20/(BitClass53[0]+4)*/

/* Definition of high rate configuration (see Table C5a)        */
/* Note that the weight is in 16 bit representation and negated */
/* ------------------------------------------------------------ */
   static const Word16   BitClass63[5]    = { 44+CrcLen, 44, 46, 47+4, 12 };
   static const Word16   Weight63[5]      = { -8520, -9502, -7864, -6882, 0};

   static const Word16   InvBitClass63[4] = {21400, 23831, 22795, 22310};
   static const Word16   inv_b0p4_63      = 19785;

/* Definition of SID configuration (see Table C5b)              */
/* Note that the weight is in 16 bit representation and negated */
/* ------------------------------------------------------------ */
   static const Word16   BitClassSID[5] = { 30+CrcLen+4, 0, 0, 0, 0 };
   static const Word16   WeightSID[5]   = { -32768, 0, 0, 0, 0};

   static const Word16   inv_b0p4_SID   = 26887;

   Word16   i,j,rbits,r[5],abits,last,cbits,limit,NInfoBits;
   Word16   weight[5],inv_b0p4,inv_bit_class[4];
   Word32   L_tmp;

⌨️ 快捷键说明

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