📄 psnr_process.c
字号:
/*
///////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2006-2008 Beijing, pengzhen (pengzhenxp@yahoo.com.cn) //
// //
///////////////////////////////////////////////////////////////////////////////
*/
static FILE* m_fp_in0 = 0 ;
static FILE* m_fp_in1 = 0 ;
static FILE* m_fp_out = 0 ;
/**
** get psnr
*/
static float get_psnr1( unsigned char * i420_old,
unsigned char * i420_new, unsigned int length )
{
unsigned int i , mse0 = 0 ;
double mse ;
i = length ;
while( i -- )
{
int temp = (*i420_old) - (*i420_new) ;
mse0 += temp*temp ;
i420_old ++ ; /* next data */
i420_new ++ ;
}
mse = (double)(mse0) / length ;
if( mse == 0 )
{
return 0 ;
}
else
{
return (float)( 10*log10(mse) ) ;
}
/* return (float)( 10*log10(mse/length) ) ; */
}
/**
** get psnr
*/
static void get_psnr( unsigned char * i420_old,
unsigned char * i420_new, unsigned int length,
float* psnr_y, float* psnr_u, float* psnr_v )
{
static float psnr0 = 0 ;
if( ! psnr0 ) psnr0 = (float)( 10* log10(255.0*255.0) ) ;
*psnr_y = psnr0 - get_psnr1( i420_old, i420_new, length ) ; /* Y */
i420_old += length ;
i420_new += length ; length /=4 ; /* Yuv420 planar */
*psnr_u = psnr0 - get_psnr1( i420_old, i420_new, length ) ; /* U */
i420_old += length ;
i420_new += length ;
*psnr_v = psnr0 - get_psnr1( i420_old, i420_new, length ) ; /* V */
}
/**
** write psnr (average)
*/
static void write_psnr_avg( float psnr_y, float psnr_u, float psnr_v )
{
fprintf( m_fp_out, "\n" );
fprintf( m_fp_out, "|--------------------------------------| \n" ) ;
fprintf( m_fp_out, "| average | \n" ) ;
fprintf( m_fp_out, "|--------------------------------------| \n" ) ;
fprintf( m_fp_out, "| PSNR Y(dB) | PSNR U(dB) | PSNR V(dB) | \n" ) ;
fprintf( m_fp_out, "|------------|------------|------------| \n" ) ;
fprintf( m_fp_out, "| %8.3f |%9.3f |%9.3f |\n", psnr_y, psnr_u, psnr_v ) ;
fprintf( m_fp_out, "|--------------------------------------| \n" ) ;
fprintf( m_fp_out, "\n" );
}
/**
** write psnr
*/
static void write_psnr( void * i420_old, void * i420_new,
unsigned int length, int process_frames, int last )
{
float psnr_y, psnr_u, psnr_v ;
static double sum_psnr_y =0 ;
static double sum_psnr_u =0 ;
static double sum_psnr_v =0 ;
if( ! last )
{
get_psnr( i420_old, i420_new, length, &psnr_y, &psnr_u, &psnr_v ) ;
sum_psnr_y += psnr_y ;
sum_psnr_u += psnr_u ;
sum_psnr_v += psnr_v ;
}
else
{
psnr_y = (float)( sum_psnr_y/process_frames ) ;
psnr_u = (float)( sum_psnr_u/process_frames ) ;
psnr_v = (float)( sum_psnr_v/process_frames ) ;
}
if( process_frames == 1 )
{
write_psnr_avg( psnr_y, psnr_u, psnr_v ) ; /* average */
fprintf( m_fp_out, "|-----------------------------------------------| \n" ) ;
fprintf( m_fp_out, "| frames | PSNR Y(dB) | PSNR U(dB) | PSNR V(dB) | \n" ) ;
//fprintf( m_fp_out, "|--------|------------|------------|------------| \n" ) ;
}
else if( last )
{
fprintf( m_fp_out, "|-----------------------------------------------| \n" ) ;
fprintf( m_fp_out, "\n" );
fseek( m_fp_out, 0, SEEK_SET ) ; /* beginning of file */
write_psnr_avg( psnr_y, psnr_u, psnr_v ) ; /* average */
return ;
}
fprintf( m_fp_out, "|--------|------------|------------|------------| \n" ) ;
fprintf( m_fp_out, "|%4d |%9.3f |%9.3f |%9.3f |\n", process_frames, psnr_y, psnr_u, psnr_v ) ;
}
/**
** psnr main
*/
static void psnr_main(int argc,char **argv)
{
void * i420_old = 0 ;
void * i420_new = 0 ;
unsigned int length = 0 ;
unsigned int length1 = 0 ;
int process_frames = 0 ;
/* parameter */
get_parameter0( argc, argv ) ;
length1 = m_width * m_height ; /* only (Y) */
length = length1 * 3/2 ; /* Yuv420 planar */
if( ! length1 ) { printf("fail : width = %d height = %d \n", m_width, m_height ) ; return ; }
/* file open */
m_fp_in0 = fopen(m_in_file_name0,"rb") ;
if( ! m_fp_in0 ) { printf("can't open file : %s \n", m_in_file_name0 ) ; return ; }
m_fp_in1 = fopen(m_in_file_name1,"rb") ;
if( ! m_fp_in1 ) { printf("can't open file : %s \n", m_in_file_name1 ) ; return ; }
/* */
{
char file_trace_name[100]; sprintf( file_trace_name, "%s.psnr.txt", m_in_file_name1 ) ;
m_fp_out = fopen(file_trace_name,"w") ;
if( ! m_fp_out ) { printf("can't open file : %s \n", file_trace_name ) ; return ; }
}
i420_old = (void *)malloc( length + 32 ) ;
i420_new = (void *)malloc( length + 32 ) ;
if( (! i420_old) || (! i420_new) ) { printf("can't malloc memory \n" ) ; return ; }
/* read file */
while( ( fread( i420_old, 1, length, m_fp_in0 ) == length ) &&
( fread( i420_new, 1, length, m_fp_in1 ) == length ) )
{
process_frames ++ ; /* */
/* write file */
write_psnr( i420_old, i420_new, length1, process_frames, 0x00000000 ) ;
/* report */
fprintf(stderr, " psnr process frames %d \r", process_frames ) ; /* */
}
/* write file */
if( process_frames )
{
write_psnr( i420_old, i420_new, length1, process_frames, 0x00000001 ) ;
}
fprintf(stderr, " \n" ) ; /* */
free( i420_old ) ;
free( i420_new ) ;
/* file close */
fclose(m_fp_in0) ;
fclose(m_fp_in1) ;
fclose(m_fp_out) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -