Visual media communication job record (1) Simple yuv420 video player

Job Requirements
Program to realize YUV420 format video (uncompressed) player
Input: YUV420 format video
Key technology: video source format, video display format, format conversion
yuv420 format video download
URL link:
http://trace.eas.asu.edu/ yuv/index.html
http://media.xiph.org/Implementation
scheme
This scheme is based on opencv3.4.4 and developed with c++;
1. First open the yuv420 file, and obtain the ratio of the total data volume to one frame data volume The total number of frames of the video;
2. Then cyclically read the data of each frame into the buffer, and convert the data of one frame in the buffer into a Mat data type by writing the function of converting yuv to rgb, and display it; 3
. Each frame detects the button event at the same time, and performs corresponding operations on the video, and then reads the next frame in a loop, stores, converts, and displays.
Realize the key point
1. The conversion from yuv420 format to RGB format;
in fact, opencv has provided the format conversion function from yuv420 to rgb, but the teacher let us write it ourselves in order to let us better understand the principle; in fact, the principle is
still It's easy to understand, so I won't go into details here. There are many related explanations on the Internet, just search for it,
mainly these three formulas:

  R = Y + 1.402* (V - 128); //R
  G = Y - 0.34413 * (U - 128) - 0.71414 * (V - 128); //G
  B = Y + 1.772 * (U - 128); //B

insert image description here

Post the conversion code. Writing it yourself is really difficult for our major, but it is quite easy to understand. It is recommended that you better understand it, and don’t let the teacher ask you when you are answering;

//公式计算
void YUV420_2_RGB( unsigned char* pYUV, unsigned char* pRGB , int width, int height)  
{  
	//找到Y、U、V在内存中的首地址
    unsigned char* pY = pYUV;   
    unsigned char* pU = pYUV+height*width;  
    unsigned char* pV = pU+(height*width/4);  
    
    unsigned char* pBGR = NULL;  
    unsigned char R = 0;  
    unsigned char G = 0;  
    unsigned char B = 0;  
    unsigned char Y = 0;  
    unsigned char U = 0;  
    unsigned char V = 0;  
    double temp = 0;  
    for ( int i = 0; i < height; i++ )  
    {  
        for ( int j = 0; j < width; j++ )  
        {  
            //找到相应的RGB首地址
			pBGR = pRGB+ i*width*3+j*3;  
  
			//取Y、U、V的数据值
            Y = *(pY+i*width+j);  
            U = *pU;  
            V = *pV;  
  
          //yuv转rgb公式
            //yuv转rgb公式
            temp = Y + ((1.773) * (U-128));  
            B = temp<0?0:(temp>255?255:(unsigned char)temp);
         
            temp = (Y - (0.344) * (U-128) - (0.714) * (V-128) )  ;  
            G= temp<0?0:(temp>255?255:(unsigned char)temp);
 
            temp =(Y + (1.403)*(V-128))  ;  
            R =temp<0?0:(temp>255?255:(unsigned char)temp);
           
			//将转化后的rgb保存在rgb内存中,注意放入的顺序b是最低位
            *pBGR     = B;              
            *(pBGR+1) = G;          
            *(pBGR+2) = R;  
           
            if (  j % 2 != 0)  
            {  
                *pU++;  
				*pV++; 
            }  
           
        }  
      if(i % 2 == 0)
	  {
			pU = pU -  width/2 ;
			pV = pV -  width/2 ;
	  }
    }  
}  

2. Supported playback frames
This can be tested with the function that comes with opencv. If you use opencv for the first time, you may not know it;

 time1 = static_cast<double>(cv::getTickCount());
 time2 = static_cast<double>(cv::getTickCount());
 frame_num = frame_count/((time2 - time1)/cv::getTickFrequency());

Pay attention to the problem
Let me talk about some problems encountered by me and my classmates:
(1) Opencv recommends installing 3.xx, don’t be too new (I know this all the time, but they didn’t ask me), it can avoid some problems;
( 2) If the code found on the Internet does not work after half an hour, it should be a problem with your environment;
(3) I recommend first-hand vscode, which is much easier to use than vs, small and convenient, and by the way, the rainbow fart on vscode Plugin, take off right away! ! !

That's about it, I haven't thought of anything else for the time being,,, related content, online information is still very rich.
By the way, I wish you all a happy homework! ! !

추천

출처blog.csdn.net/weixin_45774698/article/details/123965078