[GPU encoding and decoding] GPU hard coding

1. Hardcoding in OpenCV

In OpenCV2.4.6, the use of GPU to write video has been implemented. The encoding process is completed by cv::gpu::VideoWriter_GPU . The sample program is as follows.

  1. int main(int argc, constchar* argv[])  
  2.     if (argc != 2) 
  3.     { 
  4.         std::cerr << "Usage : video_writer <input video file>" << std::endl; 
  5.         return -1; 
  6.     } 
  7.  
  8.     constdouble FPS = 25.0;  
  9.     cv::VideoCapture reader(argv[1]); 
  10.  
  11.     if (!reader.isOpened()) 
  12.     { 
  13.         std::cerr << "Can't open input video file" << std::endl; 
  14.         return -1; 
  15.     } 
  16.  
  17.     cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice()); 
  18.     cv::gpu::VideoWriter_GPU d_writer; 
  19.  
  20.     cv::Mat frame; 
  21.     cv::gpu::GpuMat d_frame; 
  22.  
  23.     for (int i = 1;; ++i) 
  24.     { 
  25.         std::cout << "Read " << i << " frame" << std::endl; 
  26.         reader >> frame; 
  27.         if (frame.empty()) 
  28.         { 
  29.             std::cout << "Stop" << std::endl; 
  30.             break
  31.         } 
  32.         cv::resize(frame,frame,cv::Size(704,576)); 
  33.         if (!d_writer.isOpened()) 
  34.         { 
  35.             std::cout << "Open GPU Writer" << std::endl; 
  36.  
  37.             d_writer.open("output_gpu.avi", frame.size(), FPS); 
  38.         } 
  39.         d_frame.upload(frame); 
  40.         std::cout << "Write " << i << " frame" << std::endl; 
  41.         d_writer.write(d_frame); 
  42.     } 
  43.     return 0; 

Deeply read the implementation of the cv::gpu::VideoWriter_GPU class, and found that its underlying encoding implementation is based on the NVCUVENC library, and the video file package is FFmpeg.

2. NVCUVENC library

The NVCUVENC library is a video encoding library provided by NVIDIA, which can implement H.264 GPU encoding, receive raw YUV frames data, and encode to generate NAL packets.

The basic steps for video hardcoding using the NVCUVENC library are as follows:

1. Check whether NVCUVENC is supported

2. Create a new encoder and set the encoding type

3. Set the encoding parameters and register the callback function

4. Create encoder related resources

5. Loop encoding each frame of data

6. Delete the encoder and release resources

The pseudo-code schematic diagram is shown below, in which the allocation of buffers before encoding and the processing of data after encoding are all performed by callback functions.

Callback function description:

AcquireBitstream() allocates a coded bitstream buffer

ReleaseBitstream() process and save coded bitstream

OnBeginFrame() initialization work before encoding

OnEndFrame() encoded statistical work

The calling sequence of the callback function:

(dwdxdy)
The article is reproduced from: Rosso Lab  []
Source of this article: Blog Park Author: dwdxdy  original text

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326971971&siteId=291194637