计算PSNR的源代码。

#include "math.h"
#include <io.h>
#include <stdio.h>
#include <string.h>
int psnr_calculate(const char *raw,const char * compressed, char *filewrite, const int width_arg,const int height_arg);
int DelInStr(char *p, char *toks);

int main()
{
 char file1[30] = "foreman_cif.yuv";
 char file2[30] = "forman_cif11111.yuv";
 char file3[30] = "";
 DelInStr(file1, "1");
 strcat(file3,file1);
 strcat(file3,".txt");
 psnr_calculate(file1,file2,file3,352,288);

 return 0;

}

//char * raw 原始码流

//char * compressed 目标码流

// char * filewrite 用来记录PSNR的文件名,

// width_arg, height_arg输入的帧的宽和高


int
psnr_calculate(const char *raw,  const char * compressed,   const char * filewrite,  const int width_arg, const int height_arg)
{
 typedef struct
 {
  int height;
  int width;
  unsigned char *plane[4];

 } frame;


 int size1,size2;
 int height;
 int width;

 char rawfilename[512];//文件名缓存区
 char compressedfilename[512];
 char filewriteBuf[512];
 unsigned char bufferraw[1024000];//
 unsigned char buffercompressed[1024000];//
 FILE * rawsequence=NULL;
 FILE * compressedsequence=NULL;
 FILE * recordedpsnr=NULL;

 double sum=0;
 double mse=0;
 double psny=0;
 double psnu=0;
 double psnv=0;
 double psnmean=0;
 double psnrm=0;
 

 int left=0;//
 int needed=0;//
 int framecount=0;//帧的数目

 

 frame frame1,frame2;

 frame1.height=height_arg;
 frame1.width=width_arg;
 frame1.plane[0]=bufferraw;
 frame1.plane[1]=bufferraw+width_arg*height_arg;
 frame1.plane[2]=bufferraw+width_arg*height_arg*5/4;
 frame1.plane[3]=0;

 frame2.height=height_arg;
 frame2.width=width_arg;
 frame2.plane[0]=buffercompressed;
 frame2.plane[1]=buffercompressed+width_arg*height_arg;
 frame2.plane[2]=buffercompressed+width_arg*height_arg*5/4;
 frame2.plane[3]=0;

 

 height=height_arg;
 width=width_arg;


 strcpy(rawfilename,raw);
 rawsequence=fopen(rawfilename,"rb");
 if (NULL==rawsequence)
 {
  return 0;
 }

 strcpy(compressedfilename,compressed);
 compressedsequence=fopen(compressedfilename,"rb");
 if (NULL==compressedsequence)
 {
  return 0;
 }

 strcpy(filewriteBuf,filewrite);
 recordedpsnr=fopen(filewriteBuf,"wb");

 if (NULL==recordedpsnr)
 {
  return 0;
 }


 //---------判断文件输入是否成功;
 
 
 printf("%s%s%s",rawfilename,compressedfilename,filewriteBuf);

 left=0;

 needed=102400;
 //fprintf(recordedpsnr,"PSNR = /r/n");
 while(1)
 {    
  int i, j;
  //计算y分量的psnr
  sum=0; 
  size1=fread(bufferraw,sizeof( char ),width*height,rawsequence);
  size2=fread(buffercompressed,sizeof( char ),width*height,compressedsequence);
  //如果文件读到结束那么就计算PSNR完毕了,每帧计算一次,
  if ((0==size1) || (0==size2) || (size1!=size2))
  {
   return 0;
  }
  else
  {
            printf("PSNR is %d",framecount);
  }

  for(i = 0;i <height ;i++)
  {
   for(j = 0;j <width ;j++)
   {
    sum += ((bufferraw[i*width +j]  -buffercompressed[i*width + j])*(bufferraw[i * width +j ]-buffercompressed[i *width +j]));
    
   }
  }


   mse = sum/(width*height);
   psny = 10*log10((pow(2,8)-1)*(pow(2,8)-1)/mse);

   fprintf(recordedpsnr," %f /r/n",psny);


   ++framecount;

   if (size1<0)
   {
    break;
   }

   //读出来抛弃掉uv的值
   size1=fread(bufferraw,sizeof(char),width*height*1/2,rawsequence);
   size2=fread(buffercompressed,sizeof(char),width*height*1/2,compressedsequence);


 }

 fprintf(recordedpsnr,"PSNRENEMEAN= %f /t ",psnrm/framecount);

 fclose(rawsequence);
 fclose(compressedsequence); 
 fprintf(filewrite,"frame = %d",framecount);
 fclose(filewrite);//打开个文件后一定要关掉
  return 0;
}

你可能感兴趣的:(计算PSNR的源代码。)