Decode the H264 data stream into yuv420p with FFmpeg

Source: Internet
Author: User

On the internet for a long time this aspect of the content, found that the online code is too old, the use of the old to even the latest version of the FFmpeg are not included, so for me this beginner is too pit pull. But after many times to find FFmpeg's header files and combined with the content of the Internet, finally successful can decode pull. Now put it out.

The first is to initialize some of the parameters

[CPP]View Plaincopy
  1. The following initializes the H264 decoding library
  2. Avcodec_init ();
  3. Av_register_all ();
  4. Avframe *pframe_ = NULL;
  5. Avcodeccontext *codec_ = Avcodec_alloc_context ();
  6. /* Find the Video encoder */
  7. Avcodec *videocodec = Avcodec_find_decoder (codec_id_h264);
  8. if (!VIDEOCODEC)
  9. {
  10. cout << "codec not found!" << Endl;
  11. return-1;
  12. }
  13. Initialization parameters, the following parameters should be determined by the specific business
  14. Codec_->time_base.num = 1;
  15. Codec_->frame_number = 1; //One video frame per package
  16. Codec_->codec_type = Avmedia_type_video;
  17. codec_->bit_rate = 0;
  18. Codec_->time_base.den = 30; //Frame rate
  19. Codec_->width = 1280; //Video width
  20. Codec_->height = 720; //Video High
  21. if (Avcodec_open (codec_, Videocodec) >= 0)
  22. Pframe_ = Avcodec_alloc_frame (); //Allocate video frame
  23. Else
  24. return-1;


The following is the specific decoding code

[CPP]View Plaincopy
  1. Avpacket packet = {0};
  2. int framefinished = dwbufsize; //This is a random number, no effect
  3. Packet.data = pbuffer; //Fill in a pointer to the full H264 data frame here
  4. Packet.size = dwbufsize; //This fills in the size of the H264 data frame
  5. Let's start with the real decoding.
  6. Avcodec_decode_video2 (Codec_, Pframe_, &framefinished, &packet);
  7. if (framefinished)//Successful decoding
  8. {
  9. int picsize = codec_->height * codec_->width;
  10. int newSize = picsize * 1.5;
  11. //Request Memory
  12. unsigned char *buf = new unsigned char[newsize];
  13. int height = p->codec->height;
  14. int width = p->codec->width;
  15. //Write Data
  16. int a=0,i;
  17. For (i=0; i
  18. {
  19. memcpy (buf+a,pframe_->data[0] + i * pframe_->linesize[0], width);
  20. A+=width;
  21. }
  22. For (i=0; i
  23. {
  24. memcpy (buf+a,pframe_->data[1] + i * pframe_->linesize[1], WIDTH/2);
  25. A+=WIDTH/2;
  26. }
  27. For (i=0; i
  28. {
  29. memcpy (buf+a,pframe_->data[2] + i * pframe_->linesize[2], WIDTH/2);
  30. A+=WIDTH/2;
  31. }
  32. //===============  
  33. //Come here, buf inside is already the data of yuv420p, can do any processing to it pull!
  34. //===============  
  35. Delete [] buf;
  36. }



But I found that this decoding is CPU intensive, my Core2 E7400 2.8G processor, decoding 1920x1080 resolution 30 frames per second of video, CPU utilization can be used to nearly 50%.

PS: The original Avcodec_decode_video2 This function will modify the parameters inside the codec_, that is, if the original filling rate is 1280x720, run Avcodec_decode_video2 after codec_ It becomes the resolution of the actual video.

Decode the H264 data stream into yuv420p with FFmpeg

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.