Main Page | Class List | File List | Class Members | File Members

vid_snr.c File Reference

#include "vid_sim.h"
#include "video_codec.h"
#include <math.h>

Go to the source code of this file.

Functions

void ComputeSNR (PictImage *im1, PictImage *im2, Results *res, int write)

Variables

video_codecVidSt


Function Documentation

void ComputeSNR PictImage im1,
PictImage im2,
Results res,
int  write
 

Definition at line 67 of file vid_snr.c.

References pict_image::Cb, pict_image::Cr, video_codec::lines, pict_image::lum, video_codec::pels, PictImage, Results, results::SNR_Cb, results::SNR_Cr, results::SNR_l, and VidSt.

Referenced by code_video().

00068 {
00069   FILE *out = NULL;
00070   int n;
00071   register int m;
00072   int quad, quad_Cr, quad_Cb, diff;
00073   PictImage *diff_image = NULL;
00074   /* Diff. image written to diff_filename */
00075   char *diff_filename=DEF_DIFFILENAME;
00076 
00077   if (write) {
00078     out = fopen(diff_filename,"ab");
00079     diff_image = (PictImage *)malloc(sizeof(PictImage));
00080     diff_image->lum = (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines));
00081     diff_image->Cr =  (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines)/4);
00082     diff_image->Cb =  (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines)/4);    
00083   }
00084 
00085   quad = 0;
00086   quad_Cr = quad_Cb = 0;
00087   /* Luminance */
00088   quad = 0;
00089   for (n = 0; n < (VidSt->lines); n++)
00090     for (m = 0; m < (VidSt->pels); m++) {
00091       diff = *(im1->lum + m + n*(VidSt->pels)) - *(im2->lum + m + n*(VidSt->pels));
00092       if (write)
00093         *(diff_image->lum + m + n*(VidSt->pels)) = 10*diff + 128;
00094       quad += diff * diff;
00095     }
00096 
00097   res->SNR_l = (float)quad/(float)((VidSt->pels)*(VidSt->lines));
00098   if (res->SNR_l) {
00099     res->SNR_l = (float)(255*255) / res->SNR_l;
00100     res->SNR_l = 10 * (float)log10(res->SNR_l);
00101   }
00102   else res->SNR_l = (float)99.99;
00103 
00104   /* Chrominance */
00105   for (n = 0; n < (VidSt->lines)/2; n++)
00106     for (m = 0; m < (VidSt->pels)/2; m++) {
00107       quad_Cr += (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2)) *
00108         (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2));
00109       quad_Cb += (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2)) *
00110         (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2));
00111       if (write) {
00112         *(diff_image->Cr + m + n*(VidSt->pels)/2) = 
00113           (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2))*10+128;
00114         *(diff_image->Cb + m + n*(VidSt->pels)/2) = 
00115           (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2))*10+128;
00116       }
00117     }
00118 
00119   res->SNR_Cr = (float)quad_Cr/(float)((VidSt->pels)*(VidSt->lines)/4);
00120   if (res->SNR_Cr) {
00121     res->SNR_Cr = (float)(255*255) / res->SNR_Cr;
00122     res->SNR_Cr = 10 * (float)log10(res->SNR_Cr);
00123   }
00124   else res->SNR_Cr = (float)99.99;
00125 
00126   res->SNR_Cb = (float)quad_Cb/(float)((VidSt->pels)*(VidSt->lines)/4);
00127   if (res->SNR_Cb) {
00128     res->SNR_Cb = (float)(255*255) / res->SNR_Cb;
00129     res->SNR_Cb = 10 * (float)log10(res->SNR_Cb);
00130   }
00131   else res->SNR_Cb = (float)99.99;
00132 
00133   if (write) {
00134     fwrite(diff_image->lum, sizeof(char), (VidSt->pels)*(VidSt->lines), out);
00135     fwrite(diff_image->Cr,  sizeof(char), (VidSt->pels)*(VidSt->lines)/4, out);
00136     fwrite(diff_image->Cb,  sizeof(char), (VidSt->pels)*(VidSt->lines)/4, out);
00137     free(diff_image->lum);
00138     free(diff_image->Cr);
00139     free(diff_image->Cb);
00140     free(diff_image);
00141     fclose(out);
00142   }
00143   return;
00144 }


Variable Documentation

video_codec* VidSt
 

Definition at line 16 of file vid_wrapper.c.


Generated on Sun Jul 16 16:27:48 2006 by  doxygen 1.3.9.1