ffmpeg从MP4中提取h264的NALU

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”完成这项工作,关键代码如下
[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;  
    }  

    //  
    //从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()函数中会被注册。用法示例如下:

[cpp] view plain copy
print?

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);  

[cpp] view plain copy
print?

    av_bitstream_filter_close(bsfc);  


    return 0;  
}  
在Android使用FFmpeg提取MP4H264视频流,可以使用FFmpeg的C库,以下是基本的步骤: 1. 在 Android 项目添加 FFmpeg C库文件和头文件。 2. 使用 FFmpeg 的 API 打开 MP4 文件并解析出 H264 视频流的信息,例如视频流的宽度、高度、码率、帧率等。 3. 使用 FFmpeg 的 API 打开视频流并解码每一帧视频,将解码后的视频帧保存到文件或渲染到 Android 的 SurfaceView 上。 4. 关闭 FFmpeg 解码器和输入文件。 下面是一个简单的示例代码,可以帮助你开始使用 FFmpeg 解码 H264 视频流: ``` #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> // FFmpeg 头文件 #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libswscale/swscale.h" int main(int argc, char* argv[]) { // 初始化 FFmpeg 库 av_register_all(); // 打开 MP4 文件 AVFormatContext *pFormatCtx = NULL; if (avformat_open_input(&pFormatCtx, "input.mp4", NULL, NULL) != 0) { printf("Error: could not open input file.\n"); return -1; } // 查找视频流信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Error: could not find stream information.\n"); return -1; } // 查找视频流索引 int videoStream = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { printf("Error: could not find video stream.\n"); return -1; } // 获取视频流解码器 AVCodecParameters *pCodecPar = pFormatCtx->streams[videoStream]->codecpar; AVCodec *pCodec = avcodec_find_decoder(pCodecPar->codec_id); if (pCodec == NULL) { printf("Error: unsupported codec.\n"); return -1; } // 打开视频流解码器 AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec); if (avcodec_parameters_to_context(pCodecCtx, pCodecPar) < 0) { printf("Error: could not copy codec parameters to decoder context.\n"); return -1; } if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error: could not open codec.\n"); return -1; } // 分配视频帧缓冲区 AVFrame *pFrame = av_frame_alloc(); AVFrame *pFrameRGB = av_frame_alloc(); if (pFrameRGB == NULL || pFrame == NULL) { printf("Error: could not allocate frame.\n"); return -1; } // 计算视频帧大小 int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); uint8_t *buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); // 初始化视频帧缓冲区 av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); // 创建视频转换器 struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); // 读取视频帧并解码 AVPacket packet; while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { avcodec_send_packet(pCodecCtx, &packet); while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { // 将视频帧转换为 RGB 格式 sws_scale(sws_ctx, (const uint8_t * const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); // 处理 RGB 格式的视频帧,例如保存到文件或渲染到 SurfaceView 上 // ... // 释放视频帧资源 av_frame_unref(pFrame); } } av_packet_unref(&packet); } // 释放资源 av_free(buffer); av_frame_free(&pFrameRGB); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avcodec_free_context(&pCodecCtx); avformat_close_input(&pFormatCtx); return 0; } ``` 这只是一个简单的示例代码,你需要根据自己的需求进行修改和扩展。同时,需要注意的是,FFmpeg 的使用需要遵循相关的协议和法律规定,不要用于非法用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值