📄 loss_generator.cpp
字号:
/*
==========================================================================================
ITU-T Telecommunications Standardization Sector Document: VCEG-M77
Study Group 16 Question 6 Filename: loss_generator.cc
Video Coding Experts Group (VCEG) Generated: 27 June, 2001
----------------------------------------
Thirteenth meeting: Austin, Texas, 2-4 April, 2001
Intention:
~~~~~~~~~~~~
Simple offline software simulator for RTP/IP over 3GPP/3GPP2 bearers
Source:
~~~~~~~
Thomas Stockhammer, Guenther Liebl Tel: +49 89 28923473
Institute for Communications Engineering Fax: +49 89 28923490
Munich University of Technology Email: {stockhammer,liebl}@ei.tum.de
80290 Munich, Germany
==========================================================================================
*/
#include "loss_generator.h"
// constructor
LOSS_GENERATOR::LOSS_GENERATOR()
{
// basic initialization
ModuleName = "LOSS-GENERATOR";
pattern_length = 0;
pattern_start_position = -1;
pattern_read_position = -1;
total_bits_counter = 0;
total_erroneous_bits_counter = 0;
bit_error_rate = 0.;
total_frames_counter = 0;
total_erroneous_frames_counter = 0;
frame_error_rate = 0.;
}
// destructor
LOSS_GENERATOR::~LOSS_GENERATOR()
{
}
// initialize the loss generator from the given parameter file
void LOSS_GENERATOR::initialize(FILE *ParameterFile_ptr, FILE *CommonLogFile_ptr)
{
LogFile_ptr = CommonLogFile_ptr;
fprintf(LogFile_ptr,"%s: start initialization process\n",ModuleName);
// get the bit error pattern location from the parameter file
fscanf(ParameterFile_ptr,"- file containing the bit error pattern: %s\n",ErrorPatternFile);
if ( (ErrorPatternFile_ptr=fopen(ErrorPatternFile,"rb")) == NULL )
{
fprintf(stderr,"!!error in module %s: couldn't open file %s \n",ModuleName,ErrorPatternFile);
exit(-1);
}
else
{
fprintf(LogFile_ptr,"%s: reading bit error pattern from binary file %s\n",ModuleName,ErrorPatternFile);
}
// determine length of the error pattern file and read it into the memory buffer
if ( fseek(ErrorPatternFile_ptr,0L,SEEK_END) )
{
fprintf(stderr,"!!error in module %s: can't fseek to end of file %s\n",ModuleName,ErrorPatternFile);
exit(-1);
}
if ( (pattern_length = ftell(ErrorPatternFile_ptr)) == -1 )
{
fprintf(stderr,"!!error in module %s: can't get length of file %s\n",ModuleName,ErrorPatternFile);
exit(-1);
}
else
{
fprintf(LogFile_ptr,"%s: bit error pattern is of length %d bytes\n",ModuleName,pattern_length);
}
fseek(ErrorPatternFile_ptr,0L,SEEK_SET);
if ( (bit_error_pattern = (byte *)malloc(pattern_length)) == NULL)
{
fprintf(stderr,"!!error in module %s: cannot allocate enough memory to store bit error pattern\n",ModuleName);
exit(-1);
}
if ( fread(bit_error_pattern,pattern_length,1,ErrorPatternFile_ptr) != 1 )
{
fprintf(stderr,"!!error in module %s: could not read error pattern from file %s\n",ModuleName,ErrorPatternFile);
exit(-1);
}
// close error pattern file after successful reading
fclose(ErrorPatternFile_ptr);
// get the starting position in the bit error pattern from the parameter file
fscanf(ParameterFile_ptr,"- start position in the pattern (in bytes): %d\n",&pattern_start_position);
if ( (pattern_start_position < 0) || (pattern_start_position >= pattern_length) )
{
fprintf(stderr,"!!error in module %s: illegal value %d for pattern start position\n",ModuleName,pattern_start_position);
exit(-1);
}
else
{
fprintf(LogFile_ptr,"%s: starting at byte %d in the error pattern\n",ModuleName,pattern_start_position);
pattern_read_position = pattern_start_position;
}
fprintf(LogFile_ptr,"%s: end initialization process\n",ModuleName);
}
// determine, if frame of length <frame_size> bits contains bit errors after transmission
Boolean LOSS_GENERATOR::bit_error_indication(int32 frame_size)
{
int32 i,j;
int32 erroneous_bits_counter;
Boolean error_indicator;
byte error_byte;
erroneous_bits_counter = 0;
error_indicator = FALSE;
// check, if frame_size is a multiple of eight bits, i.e. byte-aligned
if ( (frame_size % 8) != 0 )
{
fprintf(stderr,"!!error in module %s: frame size %d is not a multiple of eight bits\n",ModuleName,frame_size);
exit(-1);
}
// check for bit errors in each byte of the current frame
for (i=0;i<(frame_size/8);i++)
{
error_byte = bit_error_pattern[pattern_read_position++];
// wrap around stored bit error pattern at the end
if ( pattern_read_position == pattern_length )
{
pattern_read_position = 0;
}
erroneous_bits_counter = 0;
if ( error_byte )
{
for (j=0;j<8;j++)
{
if ( (error_byte>>j) & 1 )
{
erroneous_bits_counter++;
}
}
total_erroneous_bits_counter += erroneous_bits_counter;
error_indicator = TRUE;
}
total_bits_counter += 8;
}
total_frames_counter++;
if ( error_indicator )
{
total_erroneous_frames_counter++;
}
return error_indicator;
}
// write transmission statistics at the end of the simulation
void LOSS_GENERATOR::write_statistics(FILE *StatisticsFile_ptr)
{
//calculate statistics first
bit_error_rate = (double)total_erroneous_bits_counter/(double)total_bits_counter;
frame_error_rate = (double)total_erroneous_frames_counter/(double)total_frames_counter;
// write statistics to file
fprintf(StatisticsFile_ptr,"----------------------------------------------------------------------------\n");
fprintf(StatisticsFile_ptr,"%s: Statistics:\n\n",ModuleName);
fprintf(StatisticsFile_ptr,"total number of bits transmitted: %10d\n",total_bits_counter);
fprintf(StatisticsFile_ptr,"number of erroneous bits: %10d\n",total_erroneous_bits_counter);
fprintf(StatisticsFile_ptr,"bit error rate: %e\n\n",bit_error_rate);
fprintf(StatisticsFile_ptr,"total number of frames transmitted: %10d\n",total_frames_counter);
fprintf(StatisticsFile_ptr,"number of erroneous frames: %10d\n",total_erroneous_frames_counter);
fprintf(StatisticsFile_ptr,"frame error rate: %e\n",frame_error_rate);
//fprintf(StatisticsFile_ptr,"----------------------------------------------------------------------------\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -