📄 chan_dec.c
字号:
/* Decode the grids */
for ( i = 0 ; i < SubFrames ; i ++ )
Line.Sfs[i].Grid = *Bsp ++ ;
if ( FrType == 0 ) {
/* Decode 13 bit combined position index */
Temp = Ser2Par( &Bsp, 13 ) ;
Line.Sfs[0].Ppos = ( Temp/90 ) / 9 ;
Line.Sfs[1].Ppos = ( Temp/90 ) % 9 ;
Line.Sfs[2].Ppos = ( Temp%90 ) / 9 ;
Line.Sfs[3].Ppos = ( Temp%90 ) % 9 ;
/* Decode all the pulse positions */
Line.Sfs[0].Ppos = ( Line.Sfs[0].Ppos << 16 ) + Ser2Par( &Bsp, 16 ) ;
Line.Sfs[1].Ppos = ( Line.Sfs[1].Ppos << 14 ) + Ser2Par( &Bsp, 14 ) ;
Line.Sfs[2].Ppos = ( Line.Sfs[2].Ppos << 16 ) + Ser2Par( &Bsp, 16 ) ;
Line.Sfs[3].Ppos = ( Line.Sfs[3].Ppos << 14 ) + Ser2Par( &Bsp, 14 ) ;
/* Decode pulse amplitudes */
Line.Sfs[0].Pamp = (Word16)Ser2Par( &Bsp, 6 ) ;
Line.Sfs[1].Pamp = (Word16)Ser2Par( &Bsp, 5 ) ;
Line.Sfs[2].Pamp = (Word16)Ser2Par( &Bsp, 6 ) ;
Line.Sfs[3].Pamp = (Word16)Ser2Par( &Bsp, 5 ) ;
}
else {
/* Decode the positions */
for ( i = 0 ; i < SubFrames ; i ++ )
Line.Sfs[i].Ppos = Ser2Par( &Bsp, 12 ) ;
/* Decode the amplitudes */
for ( i = 0 ; i < SubFrames ; i ++ )
Line.Sfs[i].Pamp = (Word16)Ser2Par( &Bsp, 4 ) ;
}
return Line ;
}
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)
{
#define FrmHist 10
static Word16 soft_history[20] = {127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127};
static Word16 mean_class_last[5] = {127,127,127,127,127};
Word16 crc_window,last,bclass[5];
Word16 i,j,mean_hist,mean_class[5],ptr,cnt;
double tmp;
if ((G723_mode_r & 3) == 0){
crc_window = CrcWindow63;
}
if ((G723_mode_r & 3) == 1){
crc_window = CrcWindow53;
}
if( (G723_mode_r & 0x3) == 2) {
crc_window = CrcWindowSID;
}
bad_frame_soft[0]=0;
bad_frame_soft[1]=0;
if (punctur[0][0] != 0) {
/* Check which class is the last protected one */
last=0;
for (i=1;i<5;i++){
if (punctur[i][0] != 0) last=i;
}
for (i=0;i<=last-1;i++) bclass[i]=bit_class[i];
bclass[last] = bit_class[last]-4;
/* Determine mean soft value of last "FrmHist" frames */
tmp = 0.;
for (i=20-FrmHist;i<20;i++) tmp += (double) soft_history[i];
mean_hist = (Word16) (tmp/(double)FrmHist);
/* Determine mean soft value of actual frame and store */
for (i=1;i<20;i++) soft_history[i-1] = soft_history[i];
tmp = 0.;
for (i=0;i<crc_window;i++) tmp += (double) info_bit_r_soft[i];
soft_history[19] = (Word16) (tmp/(double) crc_window)+0.5;
/* Set soft badframe flag if actual soft value indicates unreliable decoding */
if ((double)soft_history[19] < 0.17*(double)mean_hist) bad_frame_soft[0] = 1;
if (soft_history[19] < 7) bad_frame_soft[0] = 1;
/* Determine mean soft value for all bit classes */
if (bclass[0]>crc_window+CrcLen){
tmp = 0.;
for (i=crc_window;i<bclass[0];i++) tmp += (double) info_bit_r_soft[i];
mean_class[0] = tmp/(double)(bclass[0]-crc_window);
}else{
mean_class[0]=127;
}
ptr=bclass[0];
for (i=1;i<=last;i++){
if (bclass[i] > 0) {
tmp = 0.;
for (j=0;j<bclass[i];j++) tmp += (double) info_bit_r_soft[ptr++];
mean_class[i] = tmp/(double)(bclass[i]);
}else{
mean_class[i]=127;
}
}
/* Set EFI flag if classes others than 0 are too unraliable */
cnt=0;
tmp = 0.35 * (double)soft_history[19];
for (i=0;i<=last;i++) if (mean_class[i] < (Word16) tmp) cnt++;
if (cnt >= 3) bad_frame_soft[1]=1;
cnt=0;
for (i=0;i<=last;i++) if (mean_class[i] < 20) cnt++;
if (cnt >= 2) bad_frame_soft[1]=1;
cnt=0;
for (i=0;i<=last;i++) if (mean_class[i] < 15) cnt++;
if (cnt >= 1) bad_frame_soft[1]=1;
cnt=0;
for (i=1;i<=last;i++){
tmp = 0.35 * (double)mean_class_last[i];
if (mean_class[i] < (Word16) tmp) cnt++;
}
if (cnt >= 3) bad_frame_soft[1]=1;
for (i=0;i<=last;i++) mean_class_last[i]=mean_class[i];
if ((bad_frame==0)&&(bad_frame_soft[0]==1)&&(bad_frame_soft[1]==0)){
bad_frame_soft[0]=0;
bad_frame_soft[1]=1;
}
}
return;
}
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[] )
{
FILE *bit_infile, *dec_outfile;
Word32 framecount, frameno = 0, n;
char InfoBitStream_r[25];
char tmp[1000];
Word16 ChannelBits_r[1000];
Word16 SynFrame[240], Bfi, cnt;
if( sizeof( char ) != 1 || sizeof( Word16 ) != 2 || sizeof( Word32 ) != 4 ) {
fprintf( stderr, "Wrong Datatypes : Edit Typedef.h (char, Word16, Word32)!\n" );
return( 1 );
}
if( (argc < 4) || (argc > 5) ) {
fprintf( stderr, "-----------------------------------------------------------\n");
fprintf( stderr, "|Usage: CHAN_DEC R_ChanBitFile SynthFile ConfigFile [/f=f]|\n");
fprintf( stderr, "-----------------------------------------------------------\n");
fprintf( stderr, "\n");
fprintf( stderr, " R_ChanBitFile - channel bitstream input file\n");
fprintf( stderr, " SynthFile - speech decoder output file\n");
fprintf( stderr, " ConfigFile - configuration file with allowed channel bitrates\n");
fprintf( stderr, " /f= - number of decoded frames (optional) \n");
return( 1 );
}
bit_infile = OpenBinfile( argv[1], "r");
dec_outfile = OpenBinfile( argv[2], "w");
frameno = 0;
if(argc==5 && !Strincmp( argv[4], "/f=", 3 )){
frameno = atoi( &argv[4][3] );
if( frameno < 0 ) {
printf( "frame > 0\n" );
return( 1 );
}
}
printf( " _____________________________________________\n" );
printf( " | |\n" );
printf( " | G.723.1 Channel Decoder Simulation |\n" );
printf( " | Version 3.1 |\n" );
printf( " |_____________________________________________|\n\n" );
printf( " Received Channel Bitstream File : %s\n", argv[1] );
printf( " Synthesized Speech Signal File : %s\n", argv[2] );
printf( " Channel Bitrate Configuration File : %s\n", argv[3] );
if( frameno == 0 )
printf( " Simulated Frames : whole file\n" );
else
printf( " Simulated Frames : %d\n", frameno );
printf( "\n" );
/* Read file with channel bitrates */
/* ------------------------------- */
GetChannelBitrates(argv[3]);
/* Initialise G.723.1 Decoder */
/* -------------------------- */
Init_DecoderState ();
Init_Decod() ;
UsePf = True ;
UseVx = True ;
for( framecount = 0; framecount++ < frameno || frameno == 0; ) {
printf( outtext, framecount);
/* Read received soft bits */
/* ----------------------- */
n = fread((char*)tmp,sizeof(char),MAX_CHAN_BIT,bit_infile);
if (n != MAX_CHAN_BIT) break;
cnt=0; /* counter to detect not transmitted frames */
for (n=0;n<MAX_CHAN_BIT;n++) {
ChannelBits_r[n]=(Word16) tmp[n];
if (ChannelBits_r[n] == 0) cnt++;
}
if (cnt < MAX_CHAN_BIT) {
/* Channel decoder */
/* --------------- */
ChannelDecoder( ChannelBits_r, InfoBitStream_r);
/* G.723.1 Decoder */
/* --------------- */
if ((InfoBitStream_r[0] & 0x3) == 0) Bfi = InfoBitStream_r[24] & 0x1;
if ((InfoBitStream_r[0] & 0x3) == 1) Bfi = InfoBitStream_r[20] & 0x1;
if ((InfoBitStream_r[0] & 0x3) == 2) Bfi = InfoBitStream_r[4] & 0x1;
}else{
/* This frame was not transmitted and is inserted in the decoder */
/* ------------------------------------------------------------- */
InfoBitStream_r[0] = 0x3;
Bfi = 0;
}
Decod(SynFrame, InfoBitStream_r, Bfi);
/* Write synthesized speech signal */
/* ------------------------------- */
fwrite ((char*)SynFrame, sizeof(Word16), 240, dec_outfile);
}
printf ("\n done\n");
fclose( bit_infile );
fclose( dec_outfile );
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -