ffmpeg 从mp4上提取H264的nalu
转自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;
- }
ffmpeg 从mp4上提取H264的nalu的更多相关文章
- (转)ffmpeg 从mp4上提取H264的nalu
出自:http://blog.csdn.net/gavinr/article/details/7183499 1.获取数据ffmpeg读取mp4中的H264数据,并不能直接得到NALU,文件中也 ...
- 嵌入式 H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
一.MP4格式基本概念 MP4格式对应标准MPEG-4标准(ISO/IEC14496) 二.MP4封装格式核心概念 1 MP4封装格式对应标准为 ISO/IEC 14496-12(信息技术 视听对象 ...
- [转]【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
[流媒體]H264—MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph Apr 1st 2012 Email:skyseraph00@163.com 一.MP4格式基本 ...
- 多媒体开发之---H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
一.MP4格式基本概念 MP4格式对应标准MPEG-4标准(ISO/IEC14496) 二.MP4封装格式核心概念 1 MP4封装格式对应标准为 ISO/IEC 14496-12(信息技术 视听对象 ...
- js对flv提取h264、aac音视频流
FLV提取里面的h264视频流 FLV和MP4支持的编码 流媒体和媒体文件的区别 流媒体是指将一连串的多媒体资料压缩后,经过互联网分段发送资料,在互联网上即时传输影音以供观赏的一种技术与过程,此技术使 ...
- ffmpeg architecture(上)
ffmpeg architecture(上) 目录 介绍 视频-您看到的是什么! 音频-您在听什么! 编解码器-缩小数据 容器-音频和视频的舒适场所 FFmpeg-命令行 FFmpeg命令行工具101 ...
- ffmpeg 转换 mp4 成 flv
参考资料: https://addpipe.com/blog/flv-to-mp4/ ffmpeg -i demo.mp4 -c:v libx264 -crf 19 demo.flv 或者 ffmpe ...
- [转载]用 FFMPEG 合并 MP4 视频
因为 ffmpeg 是支持切分 mp4 视频的,所以我就理所当然的以为 ffmpeg 是支持视频合并.直到今天同事找我问方法,才发现一直以为的方法是错误的, mp4 不支持直接 concate(丢人了 ...
- ps流提取H264并解码播放
因为需要从海康ps流中提取H264数据并进行解码播放,才有了这篇文章.因为是视频编解码领域的纯入门新手,个别理解或者方法有误,需要自行判断,不过相关方法已经测试通过,对于 像我这样的新手还是有一定的借 ...
随机推荐
- javaweb之jsp的九个隐含对象与基本语法
1.在页面上可以不用声明直接使用的对象称为jsp页面的隐含对象.使用<% %>编写的java代码在_jspService方法中,如下: public void _jspService(fi ...
- thrift简介
thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...
- golang 应用的部署相关技术
nohup命令 在 linux 下面部署,我们可以利用 nohup 命令,把应用部署在后端,如下所示: nohup ./yourapp & 这样你的应用就跑在了 Linux 系统的守护进程 n ...
- php基础部分(1)
PHP 输出文本的基础指令:echo 和 print.echo和print的区别echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)echo 输出一 ...
- .NET开源工作流RoadFlow-流程运行-调试
我们在设计好流程后,往往要先经过测试,把整个流程走遍,但是一个流程要多人审批才能走完,这样换别人账号来测试流程很麻烦. 所以我们设计了一种调试模式,即可以在流程运行时看到相关信息(错误时会显示错误信息 ...
- Spring Boot Async异步执行
异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...
- python SQLAchemy常用语法
SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果. ...
- [poj 2479] Maximum sum -- 转载
转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410 ...
- webstorm中使用git
webstorm中使用git将代码放入tfs两种方式: 直接在tfs上建立仓库,复制仓库地址,然后在本地打开webstorm,然后git克隆这个仓库 使用git命令将本地项目上传到tfs git re ...
- centos 开机执行的命令
centos开机执行的命令-------待验证,因为有可能涉及到root问题,没想明白怎么输入密码 1.增加rc.local可执行权限 chmod +x /etc/rc.d/rc.local 2.在里 ...