FFmpeg extract H264 Nalu from mp4

Source: Internet
Author: User

http://blog.csdn.net/gavinr/article/details/7183499

1. Get data ffmpeg read the H264 data in MP4, and do not directly get Nalu, the file does not store 0x00000001 separator. The following figure is the data in Packet.data


As you can see from the diagram, there is no delimiter (0x00000001) at the beginning of the data in packet, nor is it 0x65, 0x67, 0x68, 0x41, and so on, so it is certain that this is not a standard Nalu.

In fact, the first 4 characters 0x000032ce represents the length of the Nalu, starting from the 5th byte is the Nalu data. Therefore, the standard NALU data can be obtained by replacing the first 4 bytes with 0x00000001 directly.


2. Obtaining PPS and SPS

PPS and SPS are not available from packet, but are stored in the Avcodeccontext extradata data domain. As follows:




How to resolve the SPS and PPS from the Extradata. FFmpeg provides a flow filter "H264_MP4TOANNEXB" to complete this work, the key code is as follows
[CPP]  View plain copy print? h264_mp4toannexb_bsf.c   Static int h264_mp4toannexb_filter (AVBitStreamFilterContext  *bsfc,                                        AVCodecContext *avctx, const char *args,                                        uint8_t  ** poutbuf, int *poutbuf_size,                                        const uint8_t *buf, int      buf_size,                                        int keyframe)  {       h264bsfcontext *ctx  = bsfc->priv_data;       uint8_t unit_type;        int32_t nal_size;       uint32_t cumul_size =  0;       const uint8_t *buf_end = buf + buf_ size;             /* nothing to filter */        if  (!avctx->extradata | |  AVCTX->EXTRADATA_SIZE < 6)  {            *poutbuf =  (uint8_t*)  buf;           *poutbuf_size = buf_size;           return 0;       }               //       // Analysis of SPS, pps       //       /*  from Extradata retrieve sps and pps nal units from extradata */        if  (!ctx->extradata_parsed)  {            uint16_t unit_size;           uint64_t  total_size = 0;           uint8_t *out =  NULL, unit_nb, sps_done = 0, sps_seen = 0, pps_seen =  0;           const uint8_t *extradata = avctx->extradata+4;   //Skip the first 4 bytes            static const uint8_ t nalu_header[4] = {0, 0, 0, 1};                  /* retrieve length coded size */           ctx->length_size =  (*extradata++ &  0x3)  + 1;    //to indicate the number of bytes required to represent the encoded data length             if  (ctx->length_size == 3)                 return averror (EINVAL);                  /* retrieve sps and pps unit (s)  */     &NBsp;      unit_nb = *extradata++ & 0x1f; /*  Number of sps unit (s)  */           if   (!UNIT_NB)  {                goto pps;           } else {                sps_seen = 1;            }                  while  (unit_nb--)  {  

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.