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

vid_snr.c

Go to the documentation of this file.
00001 /************************************************************************
00002  *
00003  *  vid_snr.c, part of tmn (TMN encoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
00006  *  
00007  *  Contacts: 
00008  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>, or
00009  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00010  *
00011  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00012  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00013  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00014  *  
00015  ************************************************************************/
00016 
00017 /*
00018  * Disclaimer of Warranty
00019  *
00020  * These software programs are available to the user without any
00021  * license fee or royalty on an "as is" basis.  Telenor Research and
00022  * Development disclaims any and all warranties, whether express,
00023  * implied, or statuary, including any implied warranties or
00024  * merchantability or of fitness for a particular purpose.  In no
00025  * event shall the copyright-holder be liable for any incidental,
00026  * punitive, or consequential damages of any kind whatsoever arising
00027  * from the use of these programs.
00028  *
00029  * This disclaimer of warranty extends to the user of these programs
00030  * and user's customers, employees, agents, transferees, successors,
00031  * and assigns.
00032  *
00033  * Telenor Research and Development does not represent or warrant that
00034  * the programs furnished hereunder are free of infringement of any
00035  * third-party patents.
00036  *
00037  * Commercial implementations of H.263, including shareware, are
00038  * subject to royalty fees to patent holders.  Many of these patents
00039  * are general enough such that they are unavoidable regardless of
00040  * implementation design.
00041  * */
00042 
00043 
00044 #include"vid_sim.h"
00045 #include"video_codec.h"
00046 
00047 extern video_codec *VidSt;
00048 
00049 #include<math.h>
00050 
00051 /**********************************************************************
00052  *
00053  *      Name:        SNRcomp
00054  *      Description:        Compares two image files using SNR
00055  *                      No conversion to 422
00056  *      
00057  *      Input:        
00058  *      Returns:       
00059  *      Side effects:
00060  *
00061  *      Date: 930711    Author: Karl.Lillevold@nta.no
00062  *
00063  ***********************************************************************/
00064 
00065 
00066 
00067 void ComputeSNR(PictImage *im1, PictImage *im2, Results *res, int write)
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 }
00145 

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