轉自http://blog.csdn.net/gavinr/article/details/7183499
1.獲取數據
ffmpeg讀取mp4中的H264數據,並不能直接得到NALU,文件中也沒有儲存0x00000001的分隔符。下面這張圖為packet.data中的數據
從圖中可以發現,packet中的數據起始處沒有分隔符(0x00000001), 也不是0x65、0x67、0x68、0x41等字節,所以可以肯定這不是標准的nalu。
其實,前4個字0x000032ce表示的是nalu的長度,從第5個字節開始才是nalu的數據。所以直接將前4個字節替換為0x00000001即可得到標准的nalu數據。
2.獲取pps及sps
pps及sps不能從packet獲得 ,而是保存在AVCodecContext的extradata數據域中。如下:

如何從extradata中解析出sps及pps呢?ffmpeg中提供了一個流過濾器"h264_mp4toannexb"完成這項工作,關鍵代碼如下
- //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;
- }
- //
- //從extradata中分析出SPS、PPS
- //
- /* 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; //跳過前4個字節
- static const uint8_t nalu_header[4] = {0, 0, 0, 1};
- /* retrieve length coded size */
- ctx->length_size = (*extradata++ & 0x3) + 1; //用於指示表示編碼數據長度所需字節數
- if (ctx->length_size == 3)
- return AVERROR(EINVAL);
- /* retrieve sps and pps unit(s) */
- unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
- if (!unit_nb) {
- goto pps;
- } else {
- sps_seen = 1;
- }
- while (unit_nb--) {
- void *tmp;
- unit_size = AV_RB16(extradata);
- total_size += unit_size+4;
- if (total_size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE ||
- extradata+2+unit_size > avctx->extradata+avctx->extradata_size) {
- av_free(out);
- return AVERROR(EINVAL);
- }
- tmp = av_realloc(out, total_size + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!tmp) {
- av_free(out);
- return AVERROR(ENOMEM);
- }
- out = tmp;
- memcpy(out+total_size-unit_size-4, nalu_header, 4);
- memcpy(out+total_size-unit_size, extradata+2, unit_size);
- extradata += 2+unit_size;
- pps:
- if (!unit_nb && !sps_done++) {
- unit_nb = *extradata++; /* number of pps unit(s) */
- if (unit_nb)
- pps_seen = 1;
- }
- }
- if(out)
- memset(out + total_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
- if (!sps_seen)
- av_log(avctx, AV_LOG_WARNING, "Warning: SPS NALU missing or invalid. The resulting stream may not play.\n");
- if (!pps_seen)
- av_log(avctx, AV_LOG_WARNING, "Warning: PPS NALU missing or invalid. The resulting stream may not play.\n");
- av_free(avctx->extradata);
- avctx->extradata = out;
- avctx->extradata_size = total_size;
- ctx->first_idr = 1;
- ctx->extradata_parsed = 1;
- }
- *poutbuf_size = 0;
- *poutbuf = NULL;
- do {
- if (buf + ctx->length_size > buf_end)
- goto fail; //buf為NULL時,以下代碼將不再執行
- //
- //用於保存數據長度的字節數,是在分析原extradata計算出來的
- //
- if (ctx->length_size == 1) {
- nal_size = buf[0];
- } else if (ctx->length_size == 2) {
- nal_size = AV_RB16(buf);
- } else
- nal_size = AV_RB32(buf);
- buf += ctx->length_size;
- unit_type = *buf & 0x1f;
- if (buf + nal_size > buf_end || nal_size < 0)
- goto fail;
- /* prepend only to the first type 5 NAL unit of an IDR picture */
- if (ctx->first_idr && unit_type == 5) {
- //
- //copy IDR 幀時,需要將sps及pps一同拷貝
- //
- if (alloc_and_copy(poutbuf, poutbuf_size,
- avctx->extradata, avctx->extradata_size,
- buf, nal_size) < 0)
- goto fail;
- ctx->first_idr = 0;
- } else {
- //
- //非IDR幀,沒有sps及pps
- if (alloc_and_copy(poutbuf, poutbuf_size,
- NULL, 0,
- buf, nal_size) < 0)
- goto fail;
- if (!ctx->first_idr && unit_type == 1)
- ctx->first_idr = 1;
- }
- buf += nal_size;
- cumul_size += nal_size + ctx->length_size;
- } while (cumul_size < buf_size);
- return 1;
- fail:
- av_freep(poutbuf);
- *poutbuf_size = 0;
- return AVERROR(EINVAL);
- }
一般情況下,extradata中包含一個sps、一個pps 的nalu, 從上面的代碼中容易看出extradata的數據格式。分析后的sps及pps依然儲存在extradata域中,並添加了起始符。從代碼中還可以看出,上面的函數會將sps、pps及packet中的數據,都copy到poutbuf指示的內存中,如果不需要copy到指定內存,直接給buf參數傳入空值即可。
3.使用ffmpeg的流過濾器獲取sps及pps
流過濾器"h264_mp4toannexb", 在av_register_all()函數中會被注冊。用法示例如下:
- int ParseH264ExtraDataInMp4(int stream_id)
- {
- uint8_t *dummy = NULL;
- int dummy_size;
- AVBitStreamFilterContext* bsfc = av_bitstream_filter_init("h264_mp4toannexb");
- if(bsfc == NULL)
- {
- return -1;
- }
- av_bitstream_filter_filter(
- bsfc, format_ctx_->streams[stream_id]->codec, NULL, &dummy, &dummy_size, NULL, 0, 0);
- av_bitstream_filter_close(bsfc);
- return 0;
- }