00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00054
00055
00056
00057
00058
00059
00060
00061
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
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
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
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