6.4. Decoding Module
Please refer to Multimedia Development Reference Manual for details on decoding. Refer to the SAIL User Development Manual for information about ffmpeg decoding the python interface.
This chapter mainly introduces the following two aspects:
OpenCV decoding
FFmpeg decoding
6.4.1. OpenCV Decoding
OpenCV supports YUVI420/BGR format output. In order to improve performance, yuv format data is set for decoding output in the example.
A simple example is as follows:
1cv::VideoCapture cap;
2 if (!cap.isOpened()) {
3 cap.open(input_url);
4}
5cap.set(cv::CAP_PROP_OUTPUT_YUV, 1.0); //Set output YUVI420 format data, if BGR output is selected comment out this line of code
6cv::Mat *img = new cv::Mat;
7cap.read(*img);
8//do something
9......
10//end
11delete img;
The cap.set interface function sets the output format. cap::read obtains cv::Mat object img. The img data then needs to be preprocessed before reasoning through the image arithmetic acceleration interface (bmcv module).
6.4.2. FFmpeg Decoding
C programming interface initialization configuration:
1// ffmpeg outputs NV12 compressed data by default. Initialize the decoder as follows:
2/*set compressed output*/
3av_dict_set(&opts, "output_format", "101", 0);
4
5if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) {
6fprintf(stderr, "Failed to open %s codec\n",
7 av_get_media_type_string(type));
8return ret;
9}
Python programming interface
1import sophon.sail as sail
2decoder = sail.Decoder(filename)
3img0 = decoder.read(handle) #The default output is yuv i420 format