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 
00045 
00046 
00047 #include "vid_sim.h"
00048 #include "video_codec.h"
00049 
00050 extern video_codec *VidSt;
00051 
00052 FILE *streamfile;
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 int NextTwoPB(PictImage *next2, PictImage *next1, PictImage *prev, 
00076               int bskip, int pskip, int seek_dist)
00077 {
00078   int adv_is_on = 0, mof_is_on = 0, lv_is_on = 0;
00079   int psad1, psad2, bsad, psad;
00080   MotionVector *MV[6][MBR+1][MBC+2];
00081   MotionVector *mvp, *mvbf, *mvbb;
00082   int x,y;
00083   int i,j,k,tmp;
00084 
00085   
00086   if ((VidSt->advanced)) {
00087     (VidSt->advanced) = OFF;
00088     adv_is_on = ON;
00089   }
00090   if ((VidSt->mv_outside_frame)) {
00091     (VidSt->mv_outside_frame) = OFF;
00092     mof_is_on = ON;
00093   }
00094   if ((VidSt->long_vectors)) {
00095     (VidSt->long_vectors) = OFF;
00096     lv_is_on = ON;
00097   }
00098 
00099   for (j = 1; j <= ((VidSt->lines)>>4); j++) 
00100     for (i = 1; i <= ((VidSt->pels)>>4); i++) 
00101       for (k = 0; k < 3; k++) {
00102         MV[k][j][i] = (MotionVector *)calloc(1,sizeof(MotionVector));
00103         
00104 
00105       }
00106 
00107   mvbf = (MotionVector *)malloc(sizeof(MotionVector));
00108   mvbb = (MotionVector *)malloc(sizeof(MotionVector));
00109 
00110   psad = 0;
00111   psad1 = 0;
00112   psad2 = 0;
00113   bsad = 0;
00114 
00115   
00116   for ( j = 1; j < (VidSt->lines)/MB_SIZE - 1; j++) {
00117     for ( i = 1; i < (VidSt->pels)/MB_SIZE - 1 ; i++) {
00118       x = i*MB_SIZE;
00119       y = j*MB_SIZE;
00120 
00121       
00122       
00123       
00124 
00125       
00126       MotionEstimation(next2->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
00127       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00128         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00129       
00130       memcpy(MV[2][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
00131 
00132       
00133       MotionEstimation(next1->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
00134       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00135         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00136       memcpy(MV[1][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
00137 
00138       
00139       MotionEstimation(next2->lum,next1->lum,x,y,0,0,seek_dist,MV,&tmp);
00140       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00141         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00142 
00143       
00144       mvp = MV[2][j+1][i+1];
00145       mvbf->x =   bskip * mvp->x / (bskip + pskip);
00146       mvbb->x = - pskip * mvp->x / (bskip + pskip);
00147       mvbf->y =   bskip * mvp->y / (bskip + pskip);
00148       mvbb->y = - pskip * mvp->y / (bskip + pskip);
00149 
00150       psad1 += MV[0][j+1][i+1]->min_error;
00151       psad2 += MV[1][j+1][i+1]->min_error;
00152       psad +=  mvp->min_error;
00153 
00154       
00155       bsad += SAD_MB_Bidir(next1->lum + x + y*(VidSt->pels),
00156            next2->lum + x + mvbb->x + (y + mvbb->y)*(VidSt->pels),
00157            prev->lum + x + mvbf->x + (y + mvbf->y)*(VidSt->pels),
00158            (VidSt->pels), INT_MAX);
00159     }
00160   }
00161 
00162   for (j = 1; j <= ((VidSt->lines)>>4); j++) 
00163     for (i = 1; i <= ((VidSt->pels)>>4); i++) 
00164       for (k = 0; k < 3; k++) 
00165         free(MV[k][j][i]);
00166   free(mvbf);
00167   free(mvbb);
00168 
00169   
00170   (VidSt->advanced) = adv_is_on;
00171   (VidSt->mv_outside_frame) = mof_is_on;
00172   (VidSt->long_vectors) = lv_is_on;
00173 
00174   
00175   if (bsad < (psad1+psad2)/2)
00176     fprintf(stdout,"Chose PB - bsad %d, psad %d\n", 
00177             bsad, (psad1+psad2)/2);
00178   else
00179     fprintf(stdout,"Chose PP  - bsad %d, psad %d\n", 
00180             bsad, (psad1+psad2)/2);
00181 
00182   if (bsad < (psad1 + psad2)/2)
00183     return 1;
00184   else 
00185     return 0;
00186 }
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 void PrintResult(Bits *bits,int num_units, int num)
00205 {
00206   fprintf(stdout,"# intra   : %d\n", bits->no_intra/num_units);
00207   fprintf(stdout,"# inter   : %d\n", bits->no_inter/num_units);
00208   fprintf(stdout,"# inter4v : %d\n", bits->no_inter4v/num_units);
00209   fprintf(stdout,"--------------\n");
00210   fprintf(stdout,"Coeff_Y: %d\n", bits->Y/num);
00211   fprintf(stdout,"Coeff_C: %d\n", bits->C/num);
00212   fprintf(stdout,"Vectors: %d\n", bits->vec/num);
00213   fprintf(stdout,"CBPY   : %d\n", bits->CBPY/num);
00214   fprintf(stdout,"MCBPC  : %d\n", bits->CBPCM/num);
00215   fprintf(stdout,"MODB   : %d\n", bits->MODB/num);
00216   fprintf(stdout,"CBPB   : %d\n", bits->CBPB/num);
00217   fprintf(stdout,"COD    : %d\n", bits->COD/num);
00218   fprintf(stdout,"DQUANT : %d\n", bits->DQUANT/num);
00219   fprintf(stdout,"header : %d\n", bits->header/num);
00220   fprintf(stdout,"==============\n");
00221   fprintf(stdout,"Total  : %d\n", bits->total/num);
00222   fprintf(stdout,"\n");
00223   return;
00224 }
00225 
00226 void PrintSNR(Results *res, int num)
00227 {
00228   FILE *fp = fopen("snr.dat","a");
00229   assert(fp!=NULL);
00230   fprintf(fp,"%.2f %.2f %.2f\n",res->SNR_l/num,res->SNR_Cb/num,res->SNR_Cr/num);
00231   fclose(fp);
00232   fprintf(stdout,"SNR_Y  : %.2f\n", res->SNR_l/num);
00233   fprintf(stdout,"SNR_Cb : %.2f\n", res->SNR_Cb/num);
00234   fprintf(stdout,"SNR_Cr : %.2f\n", res->SNR_Cr/num);
00235   fprintf(stdout,"--------------\n");
00236 
00237   return;
00238 }
00239