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 | | &NBSP;AVCTX->EXTRADATA_SIZE&NBSP;<&NBSP;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;&NBsp; unit_nb = *extradata++ & 0x1f; /* Number of sps unit (s) */ if (!UNIT_NB) { goto pps; } else { sps_seen = 1; } while (unit_nb--) {