⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 estimation.cpp

📁 这是编解码中运动估计的代码(已经调试通过)对于视频处理初学者很有帮助
💻 CPP
字号:
// estimation.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdio.h"

int main()
{
    int row,col;
	int total_no,sad;
	long int sad_min;
	char * src_path;
	char * out_path;
    int i,j,k,m,p,q,k1,m1,p1,q1;
//	i=17;
//	j=21;

	int width = 352,mb_width=width /16;					
    int height = 288,mb_height= height/16;	                    
//	int a[16][16],b[16][16],c[16][16];
	int sad_MATRIX[336][272];     	
	
	total_no= 300;				                              //total frame number
    sad=0;

    src_path = "G:\\videos\\foreman_cif.yuv";
    out_path = "C:\\my_work\\sousuo\\estimation_cif_test.yuv";
	
	int temp = 0;
    int size;
	FILE *in_file, *out_file;
    unsigned char * buf1[3];
	unsigned char * buf2[3];
	unsigned char * buf3[3];
    int frame_no;
    
	size=height * width + (height * width >> 1);

    unsigned char srcYBuf1[352*288];   
    unsigned char srcUBuf1[352/2 * 288/2];
    unsigned char srcVBuf1[352/2 * 288/2];
	
	unsigned char srcYBuf2[352*288];   
    unsigned char srcUBuf2[352/2 * 288/2];
    unsigned char srcVBuf2[352/2 * 288/2];

	unsigned char srcYBuf3[352*288];   
//  unsigned char srcUBuf3[width/2 * height/2];
//  unsigned char srcVBuf3[width/2 * height/2];
	
    buf1[0] = srcYBuf1;
    buf1[1]	= srcUBuf1;                       
    buf1[2]	= srcVBuf1;   

	buf2[0] = srcYBuf2;
    buf2[1]	= srcUBuf2;                       
    buf2[2]	= srcVBuf2; 

	buf3[0] = srcYBuf3;
//  buf3[1]	= srcUBuf3;                       
//  buf3[2]	= srcVBuf3;

    
	in_file = fopen(src_path, "rb");
   
    if (!in_file)
    {
        printf("cannot open input file.");
        return 0;
    }

    out_file = fopen(out_path, "wb");
    if (!out_file)
    {
        printf("cannot write file.\n");
        return 0;
    }
   
    frame_no =0;
    fread(buf1[0],   width,      height,    in_file);
    fread(buf1[1], (width>>1), (height>>1), in_file);
    fread(buf1[2], (width>>1), (height>>1), in_file);

    fwrite(buf1[0],  width,  height,    out_file);
    fwrite(buf1[1], (width>>1), (height>>1), out_file);
    fwrite(buf1[2], (width>>1), (height>>1), out_file);

	printf("Do  Number  %5d \n ", frame_no);

	for (frame_no =1;  frame_no <total_no; frame_no++)
	{		
        
		fread(buf2[0],   width,      height,    in_file);
	    fread(buf2[1], (width>>1), (height>>1), in_file);
	    fread(buf2[2], (width>>1), (height>>1), in_file);
	   
	
	 for(i =0 ; i <mb_height; i ++) 
		 {  
		    for(j =0 ; j<mb_width ; j ++) 
			{
			   for(p=0;p<mb_height;p++)                               //参考帧中参加比较的宏块数
		        {
                  for(q=0;q<mb_width;q++)
		           {
                 for(k=0;k<16;k++)                               //所有参考宏块内容和当前宏块内容的差,求取每个参考宏块对应的sad
			        {
                       for(m=0;m<16;m++) 
				       {
					     //  temp=*(buf2[0]+16*i*width+16*j+m+k*width)-*(buf1[0]+16*p*width+16*q+m+k*width);	
					    temp=buf2[0][16*i*width+16*j+m+k*width]-buf1[0][16*p*width+16*q+m+k*width];	
						 sad=sad+abs(temp);                          
						}		  			          
                     }	
					sad_MATRIX[p][q]=sad;				  
    				sad=0;
				   }//for(q=0;q<width-16;q++)                  
				}//for(p=0;p<height-16;p++)        
				   sad_min=16*16*255; 
                  for(p1=0;p1<mb_height;p1++)      //求取sad_min,并找出sad_min对应的参考宏块的位置
				  {
                    for(q1=0;q1<mb_width;q1++)
					{
					  if(sad_MATRIX[p1][q1]<sad_min)                            
			           {
					     sad_min=sad_MATRIX[p1][q1];
                         row=p1;
				         col=q1;						   
                       }

					 }
                  
				  }						          //最佳宏块已经选出,位置在buf1[0]中的(row,col)为首地址的宏块中

	     		     for(k1=0;k1<16;k1++)          //把最优宏块的数据写向 buf3[0]宏块。                
			             {
                           for(m1=0;m1<16;m1++) 
				            {
						    //(buf3[0]+16*i*width+16*j+m1+k1*width)=*(buf1[0]+16*row*width+16*col+m1+k1*width);
						     buf3[0][16*i*width+16*j+m1+k1*width]=buf1[0][16*row*width+16*col+m1+k1*width];
						    }		  			    
                         }  			
			}	//for(j = 0 ; j < mb_width ; j ++)   		 		 		 
		   
		 }   //for(i = 0 ; i < mb_height ; i ++)

         

		   fwrite(buf3[0],  width,  height,    out_file);
	       fwrite(buf2[1], (width>>1), (height>>1), out_file);
	       fwrite(buf2[2], (width>>1), (height>>1), out_file);

		   printf("Do  Number  %5d \n ", frame_no);

           memcpy(buf1[0],buf2[0],width*height);
           memcpy(buf1[1],buf2[1],width*height);
		   memcpy(buf1[2],buf2[2],width*height);
	
	}
           fclose(in_file);
	       fclose(out_file);

	       return 0;	
    
}



/*           memcpy(buf1[0],buf2[0],width*height);
             memcpy(buf1[1],buf2[1],width*height);
			 memcpy(buf1[2],buf2[2],width*height);
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -