📄 chan_map.c
字号:
/*-------------------------------------------------------------------------*/
/* */
/* Mobile Network Simulation */
/* for transmission of G.723.1 bitstreams */
/* */
/*-------------------------------------------------------------------------*/
/* */
/*-------------------------------------------------------------------------*/
/* (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"
#define tstbit( x, n ) (( x ) & ( 1 << ( n )))
#define setbit( x, n ) (( x ) |= ( 1 << ( n )))
#define clrbit( x, n ) (( x ) &= (~( 1 << ( n ))))
/*___________________________________________________________________________
| |
| local Functions |
|___________________________________________________________________________|
*/
static void GetChannelBitrates(char *ConfigName);
static Word16 Channel_Unpack (Word16 ChannelBits[], FILE *fp, Word16 *Tx);
static void ConfigDecode (Word16 chan_bit_r[], Word16 *mode_r, Word16 *err_conf);
static void DecodeChannelBitrate(Word16 CC_mode_r,Word16 G723_mode_r, Word16 *SysChannelBitrate_r);
static FILE *OpenBinfile( char *name, char *mode );
static int Strincmp( const char *s, const char *t, size_t max );
/*___________________________________________________________________________
| |
| Constants & Globals |
|___________________________________________________________________________|
*/
static char outtext[] = " Transmitting Frame: %4d\r";
static Word16 MAX_CHAN_BIT=684;
/* Definition of BCH Code for configuration bits */
/* --------------------------------------------- */
#define BCHLen 13
#define NumBCHWords 8
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}};
/* Channel bitrates (are read from file) */
/* ------------------------------------- */
static Word16 ChannelBitrate_53[2];
static Word16 ChannelBitrate_63[2];
static Word16 ChannelBitrate_SID[2];
/*___________________________________________________________________________
| |
| Subroutines |
|___________________________________________________________________________|
*/
static Word16 Channel_Unpack (Word16 ChannelBits[], FILE *fp, Word16 *Tx)
{
char ChannelBitStream[200];
Word16 i, NumChannelBytes, pw, pb, config[BCHLen], mode_r, err_conf;
Word16 G723_mode, CC_mode, SysChannelBitrate, n;
/* Read configuration Code */
/* ----------------------- */
n = fread((char*)ChannelBitStream,sizeof(char),2,fp);
if (n !=2) return(1);
if ((ChannelBitStream[0] == (char)0xAF)&&(ChannelBitStream[1] == (char)0x16)){
*Tx = 0; /* the frame is marked on the decoder side as not transmitted */
return(0);
}
*Tx=1; /* this is a regular frame which is transmitted */
pw=0;
pb=0;
for (i=0;i<BCHLen;i++){
config[i] = (ChannelBitStream[pw] >> pb) & 0x1;
config[i] = ( config[i] == 0 ) ? 0X007F : 0XFF81;
pb++;
if (pb == 8) {
pb = 0;
pw++;
}
}
/* Decode to get channel bitrate */
/* ----------------------------- */
ConfigDecode (config, &mode_r, &err_conf);
G723_mode = mode_r & 0x3;
CC_mode = mode_r >> 2;
DecodeChannelBitrate(CC_mode,G723_mode,&SysChannelBitrate);
NumChannelBytes = (Word16) ((double)SysChannelBitrate * 30./8000.);
if ((double)NumChannelBytes*8000./30. < (double)SysChannelBitrate)
NumChannelBytes++;
n = fread((char*)&ChannelBitStream[2],sizeof(char),NumChannelBytes-2,fp);
if (n != (NumChannelBytes-2)) return(1);
pw = 0;
pb = 0;
for (i=0;i<8*NumChannelBytes;i++) {
ChannelBits[i] = (ChannelBitStream[pw] >> pb) & 0x1;
ChannelBits[i] = ( ChannelBits[i] == 0 ) ? 0X007F : 0XFF81;
pb++;
if (pb == 8) {
pb = 0;
pw++;
}
}
for (i=8*NumChannelBytes;i<MAX_CHAN_BIT;i++) ChannelBits[i] = 0;
return(0);
}
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 ConfigDecode (Word16 chan_bit_r[], Word16 *mode_r, Word16 *err_conf)
{
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 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;
Word16 end_bsf, i;
Word16 ChannelBits[1000], Tx;
char ChannelBits_r[1000];
if( (argc < 4) || (argc > 5) ) {
fprintf( stderr, "-----------------------------------------------------------\n");
fprintf( stderr, "|Usage: NETWORK ChanBitFile R_ChanBitFile ConfigFile [/f=f]|\n");
fprintf( stderr, "-----------------------------------------------------------\n");
fprintf( stderr, "\n");
fprintf( stderr, " ChanBitFile - channel bitstream input file\n");
fprintf( stderr, " R_ChanBitFile - received soft channel bits\n");
fprintf( stderr, " ConfigFile - configuration files with allowed channel bitrates\n");
fprintf( stderr, " /f= - number of transmitted 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( " | Error Free Channel Mapping |\n" );
printf( " |_____________________________________________|\n\n" );
printf( " Transmitted Channel Bitstream File : %s\n", argv[1] );
printf( " Received Channel Bitstream 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]);
for( framecount = 0; framecount++ < frameno || frameno == 0; ) {
for (i=0;i<MAX_CHAN_BIT;i++) ChannelBits[i]=0;
/* Read and unpack channel bitstream */
/* --------------------------------- */
end_bsf = Channel_Unpack (ChannelBits, bit_infile, &Tx);
if (end_bsf == 1) break;
/* Transmit the encoder channel bits with maximum reliability to the decoder */
/* The MSB contains the channel bit, bits 0-7 the reliability */
/* ------------------------------------------------------------------------- */
if (Tx == 0) {
/* Mark that frame as not transmitted */
for (i=0;i<MAX_CHAN_BIT;i++) ChannelBits_r[i] = 0;
}else{
for (i=0;i<MAX_CHAN_BIT;i++) ChannelBits_r[i] = (char) ChannelBits[i];
}
/* Write synthesized speech signal */
/* ------------------------------- */
fwrite ((char*)ChannelBits_r, sizeof(char), MAX_CHAN_BIT, dec_outfile);
printf( outtext, framecount+1 );
}
printf ("\n done\n");
fclose( bit_infile );
fclose( dec_outfile );
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -