📄 chan_dec.c
字号:
/* ------------------- */
metric[0] = 0;
for( i = 1; i < 16; ++i )
metric[i] = -5000;
/* --------------------------------------------------- */
/* | Process trellis segments for protected classes | */
/* --------------------------------------------------- */
ptr1 = BCHLen;
cnt12 = -1;
ptr0 = 0;
for( k = 0; k <= last; ++k ) {
for( i = 0; i < nbit_class[k]; ++i ) {
/* Include zero values into punctured channel bit stream */
/* -------------------------------------------------------*/
cnt12 = ++cnt12 % 12;
for( j = 0; j < 3; ++j ) {
if(( punctur[k][j] & mask[cnt12] ) != 0 ) {
chan_bit_np[ptr0][j] = shl( chan_bit_r[ptr1++], 1 );
} else {
chan_bit_np[ptr0][j] = 0;
}
}
/* Update metrics without storage of metric differences: */
/* ----------------------------------------------------- */
metric_update( chan_bit_np[ptr0], metric, &surv_mem[ptr0] );
++ptr0;
}
}
/* Hard decision decoding of information bits: */
/* ------------------------------------------- */
adr = 0;
for( i = ninfo_conv - 1; i >= 0; --i ) {
surv[i] = adr & 1;
if(( surv_mem[i] & mask[adr] ) != 0 ) adr += 16;
adr = shr( adr, 1 );
}
/* --------------------------------------------------------------------*/
/* | Determine soft decision value for each protected information bit |*/
/* --------------------------------------------------------------------*/
/* this algorithm is not included in this version */
/* Determine output from hard decision values: */
/* ------------------------------------------- */
ptr0 = ptr1;
for( i = 0; i < ninfo_conv - 4; ++i ) info_bit_r[i] = surv[i];
for( i = ninfo_conv - 4; i < ninfo_conv + n0 - 4; ++i ) {
if (chan_bit_r[ptr1++] < 0)
info_bit_r[i] = 1;
else
info_bit_r[i] = 0;
}
/* Copy soft decision values of unprotected class: */
/* ----------------------------------------------- */
/* insert here the values from soft decision evaluation */
for( i = 0; i < ninfo_conv +n0- 4; ++i ) info_bit_r_soft[i] = 0x7f;
}else{
/* no convolutional code has been specified */
ptr0 = BCHLen;
for( i = 0; i < nbit_class[4]; ++i ) {
info_bit_r_soft[i] = 0x7f;
if (chan_bit_r[ptr0++] < 0)
info_bit_r[i] = 1;
else
info_bit_r[i] = 0;
}
}
}
static void ChannelDecoder(Word16 chan_bit_r[], char InfoBitStream_r[])
{
static char first=0;
static Word16 LastMode_r={-1};
static Word16 bit_class[5],punctur[5][3];
Word16 info_bit_r[210], Bfi,Efi,Fii,bad_frame, bad_frame_soft[2];
Word16 info_bit_r_soft[210],IndexError[3];
Word16 mode_r, err_conf, i, ModeAccept, G723_mode_r, CC_mode_r;
Word16 SysChannelBitrate_r;
char CInfoBitStream_r[25];
/* Decode configuration bits: */
/* -------------------------- */
ConfigDecode (chan_bit_r, &mode_r, &err_conf);
ModeChangeAccept (mode_r, err_conf, &ModeAccept);
if (ModeAccept == 1) {
G723_mode_r = mode_r & 0x3;
CC_mode_r = mode_r >> 2;
if (mode_r != LastMode_r) {
/* Re-Initialise Channel Codec */
/* --------------------------- */
DecodeChannelBitrate(CC_mode_r,G723_mode_r,&SysChannelBitrate_r);
ChannelCodecInit(G723_mode_r,SysChannelBitrate_r,bit_class,punctur);
LastMode_r = mode_r;
}
/* Viterbi decoder with soft input and output: */
/* ------------------------------------------- */
viterbi(bit_class, punctur, chan_bit_r, info_bit_r, info_bit_r_soft);
/* Consider low residual error probability at the end of the trellis */
/* ----------------------------------------------------------------- */
EndTrellisReorderDec (info_bit_r, info_bit_r_soft, bit_class, punctur, G723_mode_r);
/* Determine badframe flag from CRC code */
/* ------------------------------------- */
CrcDecoder(info_bit_r,&bad_frame, G723_mode_r);
/* Improve badframe decision by soft output evaluation */
/* --------------------------------------------------- */
SoftBFI (bit_class,punctur,info_bit_r_soft, bad_frame_soft, bad_frame, G723_mode_r);
/* Reconstruct bitstream */
/* --------------------- */
bit_deallocation(info_bit_r, CInfoBitStream_r, G723_mode_r);
/* Adapt bitstream to G.723.1 decoder */
/* ---------------------------------- */
speech_decoder_adaptation(CInfoBitStream_r,InfoBitStream_r,IndexError);
}else{
bad_frame = 1;
bad_frame_soft[0] = 1;
bad_frame_soft[1] = 1;
}
/* Set concealment flags */
/* --------------------- */
Bfi = 0;
if ((bad_frame | bad_frame_soft[0]) == 1) Bfi = 1;
Efi = bad_frame_soft[1];
Fii = 0;
if (IndexError[0]+IndexError[1]+IndexError[2] > 0) Fii=1;
/* Add concealment information as last byte to the compressed bitstream */
/* -------------------------------------------------------------------- */
if ((InfoBitStream_r[0] & 0x3) == 0) i=24;
if ((InfoBitStream_r[0] & 0x3) == 1) i=20;
if ((InfoBitStream_r[0] & 0x3) == 2) i= 4;
InfoBitStream_r[i] = 0;
InfoBitStream_r[i] = InfoBitStream_r[i] | Bfi;
InfoBitStream_r[i] = InfoBitStream_r[i] | (Efi << 1);
InfoBitStream_r[i] = InfoBitStream_r[i] | (Fii << 2);
return;
}
static void ModeChangeAccept (Word16 mode_r, Word16 err_conf, Word16 *ModeAccept)
{
static Word16 mode_r_old = {0};
Word16 AcceptThreshold;
AcceptThreshold=1000;
if (mode_r == mode_r_old){
*ModeAccept = 1;
}else{
/* Accept the mode change if the decoding was sufficiently reliable */
/* ---------------------------------------------------------------- */
if (err_conf > AcceptThreshold){
/* Check whether G.723.1 mode is valid */
if ( (mode_r & 0x3) == 3 ) {
*ModeAccept = 0;
}else{
*ModeAccept = 1;
mode_r_old = mode_r;
}
}else{
*ModeAccept = 0;
}
}
return;
}
static void ConfigDecode (Word16 chan_bit_r[], Word16 *mode_r, Word16 *err_conf)
{
/* Definition of BCH Code for configuration bits (see section 2.5.4) */
/* ----------------------------------------------------------------- */
static const Word16 ConfigGenMatrix[NumBCHWords][BCHLen] = {
{ 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127},
{-127,-127,-127, 127,-127,-127, 127, 127,-127, 127,-127, 127, 127},
{ 127,-127,-127,-127, 127,-127,-127, 127, 127,-127, 127,-127, 127},
{-127, 127, 127,-127,-127, 127,-127, 127,-127,-127,-127,-127, 127},
{ 127, 127,-127,-127,-127, 127,-127,-127, 127, 127,-127, 127,-127},
{-127,-127, 127,-127, 127,-127,-127,-127,-127, 127, 127, 127,-127},
{ 127,-127, 127, 127,-127,-127, 127,-127, 127,-127,-127,-127,-127},
{-127, 127,-127, 127, 127, 127, 127,-127,-127,-127, 127,-127,-127}};
Word32 dist_max, dist[NumBCHWords], dist_max2, i, j, tmp;
static Word16 first={0}; /* for error analysis only */
/* Determine distance between received codeword and stored codewords */
for (i=0;i<NumBCHWords;i++){
dist[i] = 0;
for (j=0;j<BCHLen;j++) dist[i] += chan_bit_r[j] * ConfigGenMatrix[i][j];
}
/* Perform maximum likelihood decoding */
dist_max = -2147483647;
for (i=0;i<NumBCHWords;i++){
if (dist[i] > dist_max) {
*mode_r = i;
dist_max = dist[i];
}
}
/* Determine decoding reliability */
dist[*mode_r] = -2147483647;
dist_max2 = -2147483647;
for (i=0;i<NumBCHWords;i++){
if (dist[i] > dist_max2) {
dist_max2 = dist[i];
}
}
tmp = (dist_max-dist_max2) >> 3;
if (tmp > 32767){
*err_conf = 32767;
}else{
*err_conf = tmp;
}
return;
}
static void Init_DecoderState (void)
{
Word16 i;
DecStat.Ecount=0 ;
DecStat.InterGain=0 ;
DecStat.InterIndx=0 ;
DecStat.Rseed=0 ;
DecStat.Park=0 ;
DecStat.Gain=0 ;
for(i=0;i<LpcOrder;i++) DecStat.PrevLsp[i]=0 ;
for(i=0;i<PitchMax;i++) DecStat.PrevExc[i]=0 ;
for(i=0;i<LpcOrder;i++) DecStat.SyntIirDl[i]=0 ;
for(i=0;i<LpcOrder;i++) DecStat.PostFirDl[i]=0 ;
for(i=0;i<LpcOrder;i++) DecStat.PostIirDl[i]=0 ;
return;
}
static void bit_deallocation(Word16 info_bit_r[], char CInfoBitStream_r[],
Word16 G723_mode_r )
{
static const Word16 BitOrder63[NBits6_3] =
{175, 180, 189, 190, 191, 192 , -1 , -1,
98 , 73, 107, 154, 167, 168, 169, 170,
30 , 17 , 16 , 31 , 48 , 55 , 49 , 71,
6 , 4 , 0 , 2 , 11 , 26 , 10 , 14,
5 , 1 , 3 , 12 , 27 , 24 , 60 , 8,
44 , 66 , 62 , 82 , 25 , 61 , 9 , 7,
45 , 67 , 63 , 83 , 78 , 36 , 50 , 40,
46 , 68 , 64 , 84 , 79 , 37 , 51 , 41,
47 , 69 , 65 , 85 , 80 , 38 , 52 , 42,
56 , 99, 159, 185 , 81 , 39 , 53 , 43,
161, 187 , 20, 57 ,100, 160, 186 , 19,
22, 59 ,102, 162, 188 , 21, 58 ,101,
35 , 54 , 70 , 72, 179, 178, 177, 176,
13 , 15 , 23 , 28 , 29 , 32 , 33 , 34,
128, 132, 146, 155, 163, 171, 181, 18,
76 , 86 , 90 , 94 ,103, 108, 112, 116,
129, 133, 147, 156, 164, 172, 182 , 74,
183 , 87 , 91 , 95, 104, 109, 113, 117,
114, 118, 130, 134, 148, 157, 165, 173,
184 , 75 , 77 , 88 , 92 , 96, 105, 110,
115, 119, 131, 135, 149, 158, 166, 174,
136, 124, 120 , 89 , 93 , 97, 106, 111,
151, 141, 137, 125, 121, 144, 150, 140,
127, 123, 145, 152, 142, 138, 126, 122,
-1 , -1 , -1 , -1 , -1, 153, 143, 139};
static const Word16 BitOrder53[NBits5_3] =
{152, 153, 158, 159, 160, 161 , -1 , -1,
69 , 64 , 70 , 91, 145, 140, 147, 146 ,
24 , 15 , 14 , 25 , 46 , 50 , 47, 63,
4 , 6 , 0 , 2 , 11 , 18 , 10, 13 ,
7 , 1 , 3 , 12 , 19 , 16 , 48, 8,
42 , 59 , 55 , 65 , 17 , 49 , 9, 5 ,
43 , 60 , 56 , 66 , 26 , 30 , 34, 38 ,
44 , 61 , 57 , 67 , 27 , 31 , 35, 39 ,
45 , 62 , 58 , 68 , 28 , 32 , 36, 40 ,
51 , 87 ,141, 154 , 29 , 33 , 37, 41 ,
143, 156 , 21, 52 , 88 ,142, 155, 20 ,
23, 54 , 90 ,144, 157 , 22, 53, 89 ,
100, 128 , 96, 104, 151, 150, 149, 148,
132, 112, 116, 136, 108, 120, 124, 92 ,
109, 121, 125 , 93, 101, 129 , 97, 105,
102, 130 , 98, 106, 133, 113, 117, 137 ,
134, 114, 118, 138, 110, 122, 126, 94 ,
111, 123, 127 , 95, 103, 131 , 99, 107,
83 , 79 , 75 , 71, 135, 115, 119, 139 ,
85 , 81 , 77 , 73 , 84 , 80 , 76, 72,
-1 , -1 , -1 , -1 , 86 , 82 , 78, 74};
static const Word16 BitOrderSID[NBitsSID] =
{ 21, 22, 23, 24, 25, 26, -1, -1,
13, 14, 15, 16, 17, 18, 20, 19,
5, 6, 7, 8, 9, 10, 11, 12,
0, 1, 2, 29, 28, 27, 3, 4};
Word16 i, pw, pb, j, info_bit_hd[NInfoBits6_3];
Word16 BitOrder[NBits6_3], EndLoop, crc_window, nbit_all;
if ((G723_mode_r & 3) == 0){
/* Rate 6.3 */
EndLoop = NBits6_3;
for (i=0;i<EndLoop;i++) BitOrder[i] = BitOrder63[i];
crc_window = CrcWindow63;
nbit_all = NInfoBits6_3+CrcLen+4;
}
if ((G723_mode_r & 3) == 1){
/* Rate 5.3 */
EndLoop = NBits5_3;
for (i=0;i<EndLoop;i++) BitOrder[i] = BitOrder53[i];
crc_window = CrcWindow53;
nbit_all = NInfoBits5_3+CrcLen+4;
}
if( (G723_mode_r & 0x3) == 2) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -