📄 fsme.cpp
字号:
#include "FSME.h"
#include <stdio.h> // to make use of I/O of the OS
//#include <stdlib.h>
#include <time.h>
#include "WindowDisplay.h" // include the header of the library "CWindowDisplay"
bool ReadYUV420(unsigned char*pYUV420,char*FileName,int nWidth,int nHeight)
{
FILE *fp;
fp= fopen(FileName, "rb"); // define a file pointer fp
// open the file in diskette with binary read-only mode
// use the file pointer to point to the file
if(!fp){
printf("Unable to open image file:%s\n",FileName);
return false;
}
if(!pYUV420){
printf("No memory allocation\n");
return false;
}
fread(pYUV420,1,nWidth*nHeight*3/2,fp);
fclose(fp);
return true;
}
bool FSME(char*Ref_Frame,char*Cur_Frame,char*Pred_Frame,char*Residual_Frame,char*Reconstruct_Frame,int width,int height)
{
int cost=0,cost1=700000;
long start=0,stop=0;
unsigned char size=16;
unsigned char i=0,j=0,m=0,n=0,l=0,k=0;
unsigned char *pRef = (unsigned char*) malloc (width*height*3/2);
unsigned char *pCur = (unsigned char*) malloc (width*height*3/2);
unsigned char *pPred = (unsigned char*) malloc (width*height*3/2);
unsigned char *pResidual= (unsigned char*) malloc (width*height*3/2);
unsigned char *pReconstruct= (unsigned char*) malloc (width*height*3/2);
ReadYUV420(pRef,Ref_Frame,width,height);
ReadYUV420(pCur,Cur_Frame,width,height);
unsigned char m1=0,n1=0;
FILE *fp;
fp= fopen("mv.txt", "wt");
fprintf(fp,"the motion vector of every block of current frame\n\n");
//Luminar Y estamation
fprintf(fp,"luminar Y prediction\n\n");
fprintf(fp,"(mvx_Y,mvy_Y)=\n\n");
time(&start); //set start time
for(j=0;j<height/size;j++)
{
for(i=0;i<width/size;i++)
{
cost1=700000;
for(n=0;n<height-size+1;n++)
for(m=0;m<width-size+1;m++)
{
cost=0;
for(l=0;l<size;l++)
for(k=0;k<size;k++)
{
cost+=abs(*(pCur+(j*size+l)*width+i*size+k)-*(pRef+(n+l)*width+m+k));
if (cost>cost1) {goto next;}
}
next: if(cost<cost1)
{
cost1=cost;
m1=m;
n1=n;
}
}
fprintf(fp,"(%d,%d) ",m1-i*size,n1-j*size);
for(l=0;l<size;l++)
for(k=0;k<size;k++)
{
*(pPred+(j*size+l)*width+i*size+k)=*(pRef+(n1+l)*width+m1+k);
*(pResidual+(j*size+l)*width+i*size+k)=*(pCur+(j*size+l)*width+i*size+k)-*(pPred+(j*size+l)*width+i*size+k);
}
}
fprintf(fp,"\n\n");
}
//chrominar U estamation
fprintf(fp,"\n\n");
fprintf(fp,"chrominar U prediction\n\n");
fprintf(fp,"(mvx_U,mvy_U)=\n\n");
for(j=0;j<height/size;j++)
{
for(i=0;i<width/size;i++)
{ cost1=700000;
for(n=0;n<height/2-size/2+1;n++)
for(m=0;m<width/2-size/2+1;m++)
{
cost=0;
for(l=0;l<size/2;l++)
for(k=0;k<size/2;k++)
{
cost+=abs(*(pCur+height*width+(j*size/2+l)*width/2+i*size/2+k)-*(pRef+height*width+(n+l)*width/2+m+k));
if (cost>cost1) {goto next1;}
}
next1: if(cost<cost1)
{
cost1=cost;
m1=m;
n1=n;
}
}
fprintf(fp,"(%d,%d) ",m1-i*size/2,n1-j*size/2);
for(l=0;l<size/2;l++)
for(k=0;k<size/2;k++)
{
*(pPred+height*width+(j*size/2+l)*width/2+i*size/2+k)=*(pRef+height*width+(n1+l)*width/2+m1+k);
*(pResidual+height*width+(j*size/2+l)*width/2+i*size/2+k)=*(pCur+height*width+(j*size/2+l)*width/2+i*size/2+k)-*(pPred+height*width+(j*size/2+l)*width/2+i*size/2+k);
}
}
fprintf(fp,"\n\n");
}
//chrominar V estamation
fprintf(fp,"\n\n");
fprintf(fp,"chrominar V prediction\n\n");
fprintf(fp,"(mvx_V,mvy_V)=\n\n");
for(j=0;j<height/size;j++)
{
for(i=0;i<width/size;i++)
{ cost1=700000;
for(n=0;n<height/2-size/2+1;n++)
for(m=0;m<width/2-size/2+1;m++)
{
cost=0;
for(l=0;l<size/2;l++)
for(k=0;k<size/2;k++)
{
cost+=abs(*(pCur+height*width*5/4+(j*size/2+l)*width/2+i*size/2+k)-*(pRef+height*width*5/4+(n+l)*width/2+m+k));
if (cost>cost1) {goto next2;}
}
next2: if(cost<cost1)
{
cost1=cost;
m1=m;
n1=n;
}
}
fprintf(fp,"(%d,%d) ",m1-i*size/2,n1-j*size/2);
for(l=0;l<size/2;l++)
for(k=0;k<size/2;k++)
{
*(pPred+height*width*5/4+(j*size/2+l)*width/2+i*size/2+k)=*(pRef+height*width*5/4+(n1+l)*width/2+m1+k);
*(pResidual+height*width*5/4+(j*size/2+l)*width/2+i*size/2+k)=*(pCur+height*width*5/4+(j*size/2+l)*width/2+i*size/2+k)-*(pPred+height*width*5/4+(j*size/2+l)*width/2+i*size/2+k);
}
}
fprintf(fp,"\n\n");
}
time(&stop); //set motion estimation and compensation stop time
printf("time=%lds\n",stop-start); // print motion estimation and compensation time
fclose(fp);
for(int jj=0;jj<height*width*3/2;jj++)
{
*(pReconstruct+jj)=*(pResidual+jj)+*(pPred+jj);
}
printf("Press Enter to display\n");
getchar();
//display the Ref_Frame,Cur_Frame,and Pred_Frame.
CWindowDisplay wd1,wd2,wd3,wd4,wd5;
wd1.SetWindowPos(0, 0);
wd1.SetWindowTitle(Ref_Frame);
wd1.LoadYUV420(pRef,width,height);
wd2.SetWindowPos(200, 0);
wd2.SetWindowTitle(Cur_Frame);
wd2.LoadYUV420(pCur,width,height);
wd3.SetWindowPos(400, 0);
wd3.SetWindowTitle(Pred_Frame);
wd3.LoadYUV420(pPred,width,height);
wd4.SetWindowPos(600, 0);
wd4.SetWindowTitle(Residual_Frame);
wd4.LoadYUV420(pResidual,width,height);
wd5.SetWindowPos(800, 0);
wd5.SetWindowTitle(Reconstruct_Frame);
wd5.LoadYUV420(pReconstruct,width,height);
free(pRef); // release the memory
free(pCur);
free(pPred);
free(pResidual);
printf("Press Enter to leave\n");
getchar();
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -