#include #include #include #include static void calcSNR(unsigned char *org, unsigned char *new, int w, int h, double *pv, double *pe , int c, int type); FILE *out_name_Y, *out_name_U, *out_name_V; main(){ int Max_Frame, i,j,k, p = 0; FILE *fd; FILE *par; char org_Y[128], org_U[128], org_V[128]; char new_Y[128], new_U[128], new_V[128]; char error_Y[128], error_U[128], error_V[128]; //darshan int chrom_hize,chrom_vsize; int width, height, chrom_width, chrom_height, size; int horizontal, vertical, horizontal1, vertical1; unsigned char *org[3]; unsigned char *new[3]; unsigned char *error[3]; //darshan int counter =0 ; double e, v; int c, type; out_name_Y = fopen("SNR_Y.out", "w"); out_name_U = fopen("SNR_U.out", "w"); out_name_V = fopen("SNR_V.out", "w"); if((par = fopen("SNR.par", "r")) == NULL) printf("Warnning !!! FILE SNR.par could not open\n"); else{ fscanf(par, "%d %d %d", &width, &height, &Max_Frame); } fclose(par); // printf(" W: %d \tH: %d\t MAX:%d\n", width, height, Max_Frame); Max_Frame = Max_Frame + 1; printf("Maxframe=%d\n", Max_Frame); //darshan horizontal = width; vertical = height; chrom_width = width/2; chrom_height = height/2; horizontal1 = chrom_width; vertical1 = chrom_height; fprintf(out_name_Y,"Width: %d\tHeight: %d\tTotal Frame: %d\n", width, height, Max_Frame); fprintf(out_name_U,"Width: %d\tHeight: %d\tTotal Frame: %d\n", chrom_width, chrom_height, Max_Frame); fprintf(out_name_V,"Width: %d\tHeight: %d\tTotal Frame: %d\n", chrom_width, chrom_height, Max_Frame); //printf(" width = %d height = %d \n", width, height); //Dynamic memory allocation for(j=0; j< 3; j++){ size = (j==0) ? width*height : chrom_width*chrom_height; //printf("size = %d\n", size); org[j] = (unsigned char *)malloc(size); new[j] = (unsigned char *)malloc(size); error[j] = (unsigned char *)malloc(size); } for(k = 0; k< Max_Frame; k++){ fprintf(out_name_Y,"\n\n\nFrame number %d\n",k); fprintf(out_name_U,"\n\n\nFrame number %d\n",k); fprintf(out_name_V,"\n\n\nFrame number %d\n",k); printf("SNR:Frame number %d \n", k); /************* Original Y **************/ sprintf(org_Y, "org%d.Y", k); //printf("org:Y = %s\t", org_Y); if((fd = fopen(org_Y,"rb")) == NULL){ printf(" Couldn't open %s\n", org_Y); } else{ for(i = 0; i < vertical; i++) fread(org[0]+i*width, 1, horizontal, fd); } fclose(fd); /************* Original U **************/ sprintf(org_U, "org%d.U", k); //printf("org:U = %s\t", org_U); if((fd = fopen(org_U,"rb")) == NULL){ printf(" Couldn't open %s\n", org_U); } else{ for(i = 0; i < vertical1; i++) fread(org[1]+i*chrom_width, 1, horizontal1, fd); } fclose(fd); /************* Original V **************/ sprintf(org_V, "org%d.V", k); //printf("org:V = %s\n", org_V); if((fd = fopen(org_V,"rb")) == NULL){ printf(" Couldn't open %s\n", org_V); } else{ for(i = 0; i < vertical1; i++) fread(org[2]+i*chrom_width, 1, horizontal1, fd); } fclose(fd); //Reading the YUV for the reconstructed video buffer /************* NEW Y **************/ sprintf(new_Y, "new%d.Y", k); //printf("new:Y = %s\t", new_Y); if((fd = fopen(new_Y,"rb")) == NULL){ printf(" Couldn't open %s\n", new_Y); } else{ for(i = 0; i < vertical; i++) fread(new[0]+i*width, 1, horizontal, fd); } fclose(fd); /************* NEW U **************/ sprintf(new_U, "new%d.U", k); //printf("new:U = %s\t", new_U); if((fd = fopen(new_U,"rb")) == NULL){ printf(" Couldn't open %s\n", new_U); } else{ for(i = 0; i < vertical1; i++) fread(new[1]+i*chrom_width, 1, horizontal1, fd); } fclose(fd); /************* NEW V **************/ sprintf(new_V, "new%d.V", k); //printf("new:V = %s\n", new_V); if((fd = fopen(new_V,"rb")) == NULL){ printf(" Couldn't open %s\n", new_V); } else{ for(i = 0; i < vertical1; i++) fread(new[2]+i*chrom_width, 1, horizontal1, fd); } fclose(fd); //darshan //generating the error buffers for(i = 0; i < vertical; i++) for(j = 0; j < horizontal; j++) { //printf("org0[i]=%d new0[i] =%d\n",*(org[0]+i*width),*(new[0]+i*width)); /* *(error[0]+i*width) = abs((*(org[0]+i*width) - *(new[0]+i*width))); if((*(error[0]+i*width)) != 0 ) { *(error[0]+i*width) =153; printf("hit for error\n"); } else { *(error[0]+i*width) = 153; printf("no error\n"); }*/ *(error[0]+i*width+j) = 0.3*(*(org[0]+i*width+j) - *(new[0]+i*width+j)); // *(error[0]+i*width+j) = 0; // *(error[0]+i*width) =(*(new[0]+i*width)- *(org[0]+i*width)); // *(error[1]+i*width) = 255; //printf(" org[i] =%d\n",*(org[0]+i*width)); //printf(" error0[i] =%d\n",*(error[0]+i*width)); } printf("completed generating errors for Y\n"); for(i = 0; i < vertical1; i++) for(j = 0; j < horizontal1; j++) { *(error[1]+i*chrom_width+j) = 0; *(error[2]+i*chrom_width+j) = 0; // *(error[1]+i*chrom_width+j) = 0.59*(*(org[1]+i*chrom_width+j) - *(new[1]+i*chrom_width+j)); // *(error[2]+i*chrom_width+j) = 0.11*(*(org[2]+i*chrom_width+j) - *(new[2]+i*chrom_width)); // printf("Notification of errors for U&V\n"); // *(error[1]+i*chrom_width) = 24*((*(org[1]+i*chrom_width) - *(new[1]+i*chrom_width))); // *(error[2]+i*chrom_width) = 24*((*(org[2]+i*chrom_width) - *(new[2]+i*chrom_width))); // *(error[2]+i*chrom_width) = 128; // printf(" error0[i] =%d\n",*(error[2]+i*chrom_width)); } printf("completed generating errors for U&V\n"); //Writing the YUV for the error video buffer /************* error Y ************/ sprintf(error_Y, "error%d.Y", k); //printf("new:Y = %s\t", new_Y); unlink(new_Y); if((fd = fopen(error_Y,"wb")) == NULL){ printf(" Couldn't open %s\n", error_Y); } else { for(i = 0; i < vertical; i++){ fwrite(org[0]+i*width, 1, horizontal, fd); // printf("subtracted error[0] buffer value=%d\n",*(error[0]+i*width)); // printf("original org[0] buffer value=%d\n",*(error[0]+i*width)); // getchar(); } // printf("saving darshan Y file\n"); } fclose(fd); /************* error U **************/ sprintf(error_U, "error%d.U", k); //printf("new:U = %s\t", new_U); unlink(new_U); if((fd = fopen(error_U,"wb")) == NULL){ printf(" Couldn't open %s\n", error_U); } else{ for(i = 0; i < vertical1; i++) fwrite(error[1]+i*chrom_width, 1, horizontal1, fd); // printf("saving darshan U file\n"); } fclose(fd); /************* error V **************/ sprintf(error_V, "error%d.V", k); //printf("new:V = %s\n", new_V); unlink(new_V); if((fd = fopen(error_V,"wb")) == NULL){ printf(" Couldn't open %s\n", error_V); } else{ for(i = 0; i < vertical1; i++) fwrite(error[2]+i*chrom_width, 1, horizontal1, fd); // printf("saving darshan V file\n"); } fclose(fd); //Generating the SNR files below /* for(i=0; i<3; i++){ if(i == 0){ fprintf(out_name_Y,"SNR_Y \n"); width = horizontal; height = vertical; c = 1; type = 0; } else if(i == 1){ fprintf(out_name_U,"SNR_U \n"); width = chrom_width; height = chrom_height; c = 2; type = 1; } else if(i == 2){ fprintf(out_name_V,"SNR_V \n"); width = chrom_width; height = chrom_height; c = 2; type = 2; } calcSNR(org[i], new[i], width, height, &v, &e, c, type); printf("%3.3g ", 10.0 * log10(255.0*255.0/(e))); } */ //End of snr file generation } //Freeing the buffers used for original and new YUVs for(j=0; j< 3; j++){ free(org[j]); free(new[j]); free(error[j]); } // printf("\nWidth = %d\tHeight = %d\tTotal Frame=%d\n", horizontal, vertical, Max_Frame); fclose(out_name_Y); fclose(out_name_U); fclose(out_name_V); } static void calcSNR(org, new, w, h, pv, pe, c, type) unsigned char *org; unsigned char *new; int w, h, c; double *pv, *pe; { int i , j, k = 0; double v1,s1,s2,e2; int mb_count, row, col; mb_count = (w * h)/(256/(c*c)); while(k < mb_count){ row = (int)(k/(w/(16/c))); col = k%(w/(16/c)); s1 = s2 = e2 = 0.0; for(j=0; j<16; ){ j=j+c; for(i=0; i<16;){ v1 = org[(w/c)*(row*16/c + (int)(j/c)) + (col*16)/c + (int)(i/c)]; s1+= v1; s2+= v1*v1; v1-= new[(w/c)*(row*16/c + (int)(j/c)) + (col*16)/c + (int)(i/c)]; e2+= v1*v1; i=i+c; } } s1 /= w * h; s2 /= w * h; e2 /= w * h; if(e2 == 0.0) e2 = 0.00001; *pv = s2 - s1 * s1; *pe = e2; if(type == 0) fprintf(out_name_Y,"%3.3g ", 10.0 * log10(255.0*255.0/(*pe))); if(type == 1) fprintf(out_name_U,"%3.3g ", 10.0 * log10(255.0*255.0/(*pe))); if(type == 2) fprintf(out_name_V,"%3.3g ", 10.0 * log10(255.0*255.0/(*pe))); if(((k>1)&&((k+1) % (w/(16/c))) == 0)&&(type == 0))fprintf(out_name_Y,"\n"); if(((k>1)&&((k+1) % (w/(16/c))) == 0)&&(type == 1))fprintf(out_name_U,"\n"); if(((k>1)&&((k+1) % (w/(16/c))) == 0)&&(type == 2))fprintf(out_name_V,"\n"); k++; } }