当前位置:网站首页>FFmpeg extract H264 nalu from the mp4
FFmpeg extract H264 nalu from the mp4
2022-08-10 17:48:00 【Audio and video development old man】
1.获取数据
ffmpeg读取mp4中的H264数据,并不能直接得到NALU,There is also no saving in the file0x00000001的分隔符.下面这张图为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呢?ffmpegA stream filter is provided in "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时,The following code will no longer execute
//
//The number of bytes used to hold the data length,is analyzing theextradata计算出来的
//
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, It's easy to see from the code aboveextradata的数据格式.分析后的sps及ppsstill storedextradata域中,and added a starter.从代码中还可以看出,The above function will dosps、pps及packet中的数据,都copy到poutbuf指示的内存中,如果不需要copy到指定内存,直接给bufThe parameter can be passed a null value.
本文福利, C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)↓↓↓↓↓↓见下面↓↓文章底部点击领取↓↓
3.使用ffmpegThe stream filter getssps及pps
流过滤器"h264_mp4toannexb", 在av_register_all()function will be registered.用法示例如下:
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;
}
本文福利, C++音视频学习资料包、技术视频,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)↓↓↓↓↓↓见下面↓↓文章底部点击领取↓↓
边栏推荐
- 产品-Axure9英文版,A页面内a1状态跳转B页面的b2状态,(条件跳转状态)
- Colocate Join :ClickHouse的一种高性能分布式join查询模型
- leetcode:1013. 将数组分成和相等的三个部分
- 【图像去雾】基于颜色衰减先验的图像去雾附matlab代码
- docker中安装mysql
- Oracle Install [email protected] 7.6
- Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso「建议收藏」
- 挑战用五行代码轻松集成登录系统,实现单点登录
- 自适应模糊神经网络与bp神经网络的区别
- 程序员兼职踩到的坑,本人亲身经历
猜你喜欢
随机推荐
640. 求解方程
Making Pre-trained Language Models Better Few-Shot Learners
leet面试150
网易云信亮相LiveVideoStackCon2022,解构基于WebRTC的开源低延时播放器实践
Word里表格跨页时自动断开,表格后留有空白部分,未布满整页,如何操作让表格上下页均匀布满?
promise笔记(四)
Return the next higher prime number
分类常用的神经网络模型,深度神经网络主要模型
TradingView_学习笔记
leetcode:340.至多包含K个不同字符的最长子串
机器人控制器编程实践指导书旧版-实践七 无线通信(网络)
Your local docbook2man was found to work with SGML rather than XML
产品-Axure9英文版,A页面内a1状态跳转B页面的b2状态,(条件跳转状态)
函数柯里化(curry)
Quicker+沙拉查词使用
awk if else if else
skywalking vulnerability learning
Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso「建议收藏」
如何构建一个自己的代理ip池
股票量化交易策略:多因子筛选练习