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

📄 chan_dec.c

📁 语音压缩编码和解码的标准,其中包含部分源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*-------------------------------------------------------------------------*/ 
/*                                                                         */
/*                   G.723.1 Channel Decoder 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   setbit( x, n )    (( x ) |=   ( 1 << ( n )))
#define   clrbit( x, n )    (( x ) &= (~( 1 << ( n ))))


/*___________________________________________________________________________
 |                                                                           |
 |  	local Functions                                                      |
 |___________________________________________________________________________|
*/
static void  GetChannelBitrates(char* ConfigName);
static void CrcDecoder(Word16 info_bit_r[], Word16 *bad_frame, Word16 G723_mode_r);
static void bit_deallocation(Word16 info_bit_r[],  char InfoBitStream_r[], 
                             Word16 G723_mode_r );
static void  EndTrellisReorderDec (Word16 info_bit[], Word16 info_bit_soft[], Word16 bit_class[], 
                                   Word16 punctur[5][3], Word16 mode);
static void ChannelDecoder(Word16 chan_bit_r[], char InfoBitStream_r[]);
static void viterbi( Word16 nbit_class[], Word16 punctur[5][3], 
    Word16 chan_bit_r[], Word16 info_bit_r[], Word16 info_bit_r_soft[]);
static void hamming_distance( Word16 chan_bit_np[], Word16 metric[], Word16 temp[] );
static void metric_update( Word16 chan_bit_np[], Word16 metric[], Word16 *surv_mem );
static void hamming_distance( Word16 chan_bit_np[], Word16 metric[], Word16 temp[] );
static void metric_update( Word16 chan_bit_np[], Word16 metric[], Word16 *surv_mem );
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 SoftBFI (Word16 bit_class[], Word16 punctur[5][3], 
           Word16 info_bit_r_soft[], Word16 bad_frame_soft[], Word16 bad_frame,
Word16 G723_mode_r);
static void ConfigDecode (Word16 chan_bit_r[], Word16 *mode_r, Word16 *err_conf);
static void speech_decoder_adaptation(char CInfoBitStream[],char InfoBitStream[],
                                       Word16 IndexError[]);
static LINEDEF  Line_Unpk_Channel( char *Vinp, Word16 IndexError[] );
static void LSP_Reorder_decoder (LINEDEF *Line);
static void ModeChangeAccept (Word16 mode_r, Word16 err_conf, Word16 *ModeAccept);
static void DecodeChannelBitrate(Word16 CC_mode_r,Word16 G723_mode_r, Word16 *SysChannelBitrate_r);
static int Strincmp( const char *s, const char *t, size_t max );
static FILE *OpenBinfile( char *name, char *mode );
static void  Init_DecoderState (void);

/*___________________________________________________________________________
 |                                                                           |
 |  	Constants & Globals                                                  |
 |___________________________________________________________________________|
*/
static char   outtext[]                = " Decoding Frame: %4d\r";
enum  Wmode    WrkMode = Both ;
enum  Crate    WrkRate;
Flag     UseHp = True;
Flag     UsePf;
Flag     UseVx;
Word32   count_clip;
Word16   MAX_CHAN_BIT = 684;



/* Definition of CRC Check Code for error detection (see section 2.4) */
/* ------------------------------------------------------------------ */
#define   CrcLen  5
#define   CrcGen  18
static const Word16   CrcWindow53   = 34; /* number of bits of class 1 for crc */
static const Word16   CrcWindow63   = 44; /* number of bits of class 1 for crc */
static const Word16   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];

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


static void DecodeChannelBitrate(Word16 CC_mode_r,Word16 G723_mode_r, 
                                 Word16 *SysChannelBitrate_r)
{
   Word16   CC_Bitrate[2],i;

   if (G723_mode_r == 0){
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_63[i];
   }
   if (G723_mode_r == 1){
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_53[i];
   }
   if (G723_mode_r == 2){
      for (i=0;i<2;i++) CC_Bitrate[i] = ChannelBitrate_SID[i];
   }

   *SysChannelBitrate_r = CC_Bitrate[CC_mode_r];

   return;
}


static void  EndTrellisReorderDec (Word16 info_bit[], Word16 info_bit_soft[], 
                         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 */

   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;

         tmp                         = info_bit_soft[ptr_end-i];
         info_bit_soft[ptr_end-i]    = info_bit_soft[ptr_end-19+i];
         info_bit_soft[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;

   /* Determine the number of available channel bits per frame (multiply by 0.03) */
   /* ----------------------------------------------------------------------------*/
   L_tmp = L_mult(ChannelBitrate,31458);
   cbits = (Word16) L_shr(L_tmp,21);

   if ((SpeechCodecMode & 0x3) == 0){
      for (i=0;i<5;i++) bit_class[i] = BitClass63[i];
      for (i=0;i<5;i++) weight[i] = Weight63[i];
      inv_b0p4 = inv_b0p4_63;
      for (i=0;i<4;i++) inv_bit_class[i] = InvBitClass63[i];
      NInfoBits = NInfoBits6_3;
   }
   if ((SpeechCodecMode & 0x3) == 1){
      for (i=0;i<5;i++) bit_class[i] = BitClass53[i];
      for (i=0;i<5;i++) weight[i] = Weight53[i];
      inv_b0p4 = inv_b0p4_53;
      for (i=0;i<4;i++) inv_bit_class[i] = InvBitClass53[i];
      NInfoBits = NInfoBits5_3;
   }
   if ((SpeechCodecMode & 0x3) == 2){
      for (i=0;i<5;i++) bit_class[i] = BitClassSID[i];
      for (i=0;i<5;i++) weight[i] = WeightSID[i];
      inv_b0p4 = inv_b0p4_SID;
      NInfoBits = NInfoBitsSID;
   }

   /* Determine the number of available redundancy bits per frame */
   /* ----------------------------------------------------------- */
   rbits = cbits - (NInfoBits+CrcLen+BCHLen);

   /* Check whether enough bits are available */
   /* --------------------------------------- */
   if (rbits < 0) {
      printf ("\n\nNot enough channel bitrate specified\n");
      printf ("The minimum channel bitrate is %.2f kbit/s \n",
            ((float)(BCHLen+CrcLen+NInfoBits)*1000./30.));
      exit(1);
   }

   if (rbits == 0) {
      /* No convolutional code specified */
      /* ------------------------------- */
      if (SpeechCodecMode < 2) {
         bit_class[3] = bit_class[3]-4;
      }else{
         bit_class[0] = bit_class[0]-4;
      }
      for (i=0;i<4;i++) {
         bit_class[4] = bit_class[4] + bit_class[i];
         bit_class[i] = 0;
      }
      for (i=0;i<5;i++){
         r[i]=12;
         for (j=0;j<3;j++) punctur[i][j] = PuncturTable[r[i]-12][j];
      }
   }else{

      /* Check whether too much bits have been specified */
      /* ----------------------------------------------- */
      limit = 2*(bit_class[0]+bit_class[1]+bit_class[2]+
                 bit_class[3]+bit_class[4]) + 4;
      if (rbits >= limit) {
	 if (rbits > limit) {
	    printf ("\n\nSpecified channel bitrate in bitrate configuration file too high\n");
	    printf ("The channel bitrate is limited to %.2f kbit/s \n",
                  (float)(3*(bit_class[0]+bit_class[1]+bit_class[2]+
                 bit_class[3]+bit_class[4])+BCHLen)*1000./30.);
         }
         for (i=0;i<4;i++)   r[i]=36;
         r[4]=12;
         for (i=0;i<5;i++){
            for (j=0;j<3;j++) punctur[i][j] = PuncturTable[r[i]-12][j];
         }
         bit_class[3] = bit_class[3] + bit_class[4];
         bit_class[4] = 0;
      }else{
         /* Initialise rates and puncturing tables */
         /* -------------------------------------- */
         for (i=0;i<5;i++)   r[i]=12;
         for (i=0;i<5;i++){
            for (j=0;j<3;j++) punctur[i][j] = PuncturTable[r[i]-12][j];
         }

         /* Determine rate for the most sensitive bit class */
         /* ----------------------------------------------- */
         rbits = rbits - 4; /* tail bits have to be transmitted */
         if (rbits < 0) rbits=0;

⌨️ 快捷键说明

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