Video decoding using ffmpge (from h264 video stream to image)

Source: Internet
Author: User

In the two articles written above, this article is still about ffmpge operations. The previous article is about video compression, and this article is about video decoding on the contrary. Let's talk about the code.

Similarly, in the previous article, this article first designs a video decoding class, which is defined as follows:

Class ffmpeg_decoder {public: avcodecparsercontext * avparsercontext; avpacket avpkt; // data packet structure avframe * m_prgbframe; // frame object avframe * m_pyuvframe; // frame object avcodec * pcodech264; // decoder avcodeccontext * C; // decoder Data Structure object uint8_t * yuv_buff; // YUV image data zone uint8_t * rgb_buff; // swscontext * scxt In the RGB image data zone; // image format conversion object uint8_t * filebuf; // read the File Cache uint8_t * outbuf; // decoded video data cache int ndatalen; // The length of the RGB image data area iplimage * IMG; // opencv Image Display object uint8_t * pbuf; // used to store the int noutsize of the frame data; // used to record the int haveread of the frame data length; // used to record the read Buf length int decodelen; // decoder return length int piclen; // decoder returns image length int piccount; // output image count public: void ffmpeg_decoder_init (); // initialize void ffmpeg_decoder_show (avframe * pframe, int width, int height); // display the image void ffmpeg_decoder_close (); // close };

Define the three functions declared in the class

Void ffmpeg_decoder: ffmpeg_decoder_init () {avcodec_register_all (); // register the codecs av_init_packet (& avpkt); // initialize the package structure m_prgbframe = new avframe [1]; // values m_pyuvframe = avcodec_alloc_frame () for RGB frame data; filebuf = new uint8_t [1024*1024]; // initialize the File Cache data zone pbuf = new uint8_t [200*1024]; // initialize the frame data zone yuv_buff = new uint8_t [200*1024]; // initialize the YUV image data zone rgb_buff = new uint8_t [1024*1024]; // initialize the RGB image frame data area pcodech264 = avcodec_find_decoder (Codec_id_h264); // find the h264 decoder if (! Pcodech264) {fprintf (stderr, "h264 codec not found \ n"); exit (1) ;}avparsercontext = av_parser_init (codec_id_h264); If (! Pcodech264) return; C = avcodec_alloc_context3 (pcodech264); // The function is used to allocate an avcodeccontext and set the default value. If a failure occurs, null is returned and av_free () is available () release if (pcodech264-> capabilities & codec_cap_truncated) C-> flags | = packages;/* we do not send complete frames */If (avcodec_open2 (C, pcodech264, null) <0) return; ndatalen = 0 ;}
Void ffmpeg_decoder: ffmpeg_decoder_show (avframe * pframe, int width, int height) {cvsize limit; Height = height; width = width; IMG = cvcreateimage (rectangle_img_size, limit, 3 ); uchar * imgdata = (uchar *) (IMG-> imagedata); // Image Data Pointer for (INT y = 0; y 
Void ffmpeg_decoder: ffmpeg_decoder_close () {Delete [] filebuf; Delete [] pbuf; Delete [] yuv_buff; Delete [] rgb_buff; av_free (m_pyuvframe ); // release the frame resource avcodec_close (c); // disable the decoder av_free (c );}
This is followed by the main function section, because the decoding usually reads data streams from files or gets data cache from the network, I write the decoded code in the main function for convenience and operability.

Void main () {ffmpeg_decoder ffmpegobj; ffmpegobj. ffmpeg_decoder_init (); // initialize the decoder file * pF = NULL; fopen_s (& PF, "mydata. h264 "," rb "); While (true) {ffmpegobj. ndatalen = fread (ffmpegobj. filebuf, 1, 1024*10, Pf); // read the file data if (ffmpegobj. ndatalen <= 0) {fclose (PF); break;} else {ffmpegobj. haveread = 0; while (ffmpegobj. ndatalen> 0) {int nlength = av_parser_parse2 (ffmpegobj. avparsercontext, ffmpegobj. c, & ffmpegobj. YUV _ Buff, & ffmpegobj. noutsize, ffmpegobj. filebuf + ffmpegobj. haveread, ffmpegobj. ndatalen, 0, 0, 0); // search for the frame header ffmpegobj. ndatalen-= nlength; // the pointer shift flag ffmpegobj. haveread + = nlength; If (ffmpegobj. noutsize <= 0) {continue;} ffmpegobj. avpkt. size = ffmpegobj. noutsize; // put the frame data into the package ffmpegobj. avpkt. data = ffmpegobj. yuv_buff; while (ffmpegobj. avpkt. size> 0) {ffmpegobj. decodelen = avcodec_decode_video2 (ffmpegobj. c, FFM Pegobj. m_pyuvframe, & ffmpegobj. piclen, & ffmpegobj. avpkt); // decode if (ffmpegobj. decodelen <0) {break;} If (ffmpegobj. piclen) {ffmpegobj. scxt = sws_getcontext (ffmpegobj. c-> width, ffmpegobj. c-> height, ffmpegobj. c-> pix_fmt, ffmpegobj. c-> width, ffmpegobj. c-> height, pix_fmt_bgr24, sws_point, null); // initialize the format conversion function if (ffmpegobj. scxt! = NULL) {avpicture_fill (avpicture *) ffmpegobj. m_prgbframe, (uint8_t *) ffmpegobj. rgb_buff, pix_fmt_rgb24, ffmpegobj. c-> width, ffmpegobj. c-> height); // fill rgb_buff to m_prgbframeif (avpicture_alloc (avpicture *) ffmpegobj. m_prgbframe, pix_fmt_rgb24, ffmpegobj. c-> width, ffmpegobj. c-> height)> = 0) {sws_scale (ffmpegobj. scxt, ffmpegobj. m_pyuvframe-> data, ffmpegobj. m_pyuvframe-> linesize, 0, ffmpegobj. c-> height, ffmpegobj. m_prgbframe-> data, ffmpegobj. m_prgbframe-> linesize); ffmpegobj. ffmpeg_decoder_show (ffmpegobj. m_prgbframe, ffmpegobj. c-> width, ffmpegobj. c-> height); // decoded image display} sws_freecontext (ffmpegobj. scxt); // release the format converter resource avpicture_free (avpicture *) ffmpegobj. m_prgbframe); // releases the frame resource av_free_packet (& ffmpegobj. avpkt); // release the read Frame Memory} ffmpegobj. avpkt. size-= ffmpegobj. decodelen; ffmpegobj. avpkt. data + = ffmpegobj. decodelen ;}}} ffmpegobj. ffmpeg_decoder_close (); // close the decoder}
OK. The blog that uses FFMPEG for video decoding has come to an end. The following is the entire project: Click to open the link. Same as the previous blog project code, if you want to run the project, you need to configure opencv on your own.









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.