(转)ffmpeg 中 av_read_frame_internal分析
作者: chenwei1983 时间: 2012-3-5 04:21 PM
标题: ffmpeg 中 av_read_frame_internal分析 原出处:http://www.chinavideo.org/viewthread.php?action=printable&tid=13846
av_read_frame_internal 在ffmpeg中实现了将format格式的packet,最终转换成一帧帧的es流packet,并解析填充了packet的pts,dts,等信息,为最终解码提供了重要的数据,av_read_frame_internal,调用av_read_packet,每次只读取一个包,然后直到parser完这个包的所有数据,才开始读取下一个包,parser完的数据被保存在parser结构的数据缓冲中,这样即使av_read_packet读取的下一包和前一包的流不一样,由于parser也不一样,所以实现了av_read_frame_internal这个函数调用,可以解析出不同流的es流,而av_read_frame_internal函数除非出错否则必须解析出一帧数据才能返回
static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
AVStream *st;
int len, ret, i;
av_init_packet(pkt);
for(;;) {
//s->cur_st记录现在的stream指针,如果真,就需要接着分析packet数据,否则需要读包
st = s->cur_st;
if (st) {
//当流不需要parser或者parser为空时,即没有注册这种parser,就不要parser 包了,输出原始包数据
if (!st->need_parsing || !st->parser) {
/* no parsing needed: we just output the packet as is */
/* raw data support */
*pkt = s->cur_pkt;
compute_pkt_fields(s, st, NULL, pkt);//计算包的,其它信息
s->cur_st = NULL; //当输出一个帧数据时,需要将现在的流置成NULL,这是因为下一个包并不一定和现在是同一个流,下次调用av_read_frame_internal 时,也能够直接进入读包的那个分支
break;
//当现在的数据长度大于0,并且不是丢掉所有的包条件下,进去parser
} else if (s->cur_len > 0 && st->discard < AVDISCARD_ALL) {
//如果数据足够parser出一帧就会返回,注意ffmpeg 的parser设计得非常好,它并不需要每次从头将所有的流parser一遍,而只要每次parser新的数据就可以了,直到parser出一个完整帧出来
len = av_parser_parse(st->parser, st->codec, &pkt->data, &pkt->size,
s->cur_ptr, s->cur_len,
s->cur_pkt.pts, s->cur_pkt.dts);
s->cur_pkt.pts = AV_NOPTS_VALUE;
s->cur_pkt.dts = AV_NOPTS_VALUE;
/* increment read pointer */
//如果一个原始包里有多个帧,那就需要调用多次av_read_frame_internal
s->cur_ptr += len;
s->cur_len -= len;
/* return packet if any */
//当parser出一帧后,pkt->size大小为真
if (pkt->size) {
got_packet:
//填充pkt的其它信息
pkt->pos = s->cur_pkt.pos; // Isn't quite accurate but close.
pkt->duration = 0;
pkt->stream_index = st->index;
pkt->pts = st->parser->pts;
pkt->dts = st->parser->dts;
//注意这里使用这个destruct函数的含义,因为这里 pkt->data的指针并没有实际分配内存,而是指向了
parser结构中数据buffer,所以这里后面需要处理
pkt->destruct = av_destruct_packet_nofree;
compute_pkt_fields(s, st, st->parser, pkt);
//当需要产生通用的index时,并且是关键帧,那么需要将这一帧添加到index表中
if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & PKT_FLAG_KEY){
ff_reduce_index(s, st->index);
av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
0, 0, AVINDEX_KEYFRAME);
}
break;
}
} else {//s->cur_len == 0 ,当 s->cur_ptr 数据解析完了,则需要重新读取一个原始包来分析
/* free packet */
av_free_packet(&s->cur_pkt);
s->cur_st = NULL;
}
} else {//s->cur_st 为NULL时,读取一个原始包
/* read next packet */
ret = av_read_packet(s, &s->cur_pkt);
if (ret < 0) {
if (ret == AVERROR(EAGAIN))
return ret;
/* return the last frames, if any */
//输出最后的包
for(i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
if (st->parser && st->need_parsing) {
av_parser_parse(st->parser, st->codec,
&pkt->data, &pkt->size,
NULL, 0,
AV_NOPTS_VALUE, AV_NOPTS_VALUE);
if (pkt->size)
goto got_packet;
}
}
/* no more packets: really terminate parsing */
return ret;
}
if(s->cur_pkt.pts != AV_NOPTS_VALUE &&
s->cur_pkt.dts != AV_NOPTS_VALUE &&
s->cur_pkt.pts < s->cur_pkt.dts){//如果pts < dts 出错,返回
av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
s->cur_pkt.stream_index,
s->cur_pkt.pts,
s->cur_pkt.dts,
s->cur_pkt.size);
// av_free_packet(&s->cur_pkt);
// return -1;
}
st = s->streams[s->cur_pkt.stream_index];
if(s->debug & FF_FDEBUG_TS)
av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, flags=%d\n",
s->cur_pkt.stream_index,
s->cur_pkt.pts,
s->cur_pkt.dts,
s->cur_pkt.size,
s->cur_pkt.flags);
s->cur_st = st;//读取一个包后,设置好cur_ptr,cur_len,以便解析
s->cur_ptr = s->cur_pkt.data;//
s->cur_len = s->cur_pkt.size;//
// 当准备好解析数据时,如果parser为空则创建parser
if (st->need_parsing && !st->parser) {
st->parser = av_parser_init(st->codec->codec_id);
if (!st->parser) {
/* no parser available: just output the raw packets */
st->need_parsing = AVSTREAM_PARSE_NONE;
}else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
}
if(st->parser && (s->iformat->flags & AVFMT_GENERIC_INDEX)){
st->parser->next_frame_offset=
st->parser->cur_offset= s->cur_pkt.pos;
}
}
}
}
if(s->debug & FF_FDEBUG_TS)
av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, flags=%d\n",
pkt->stream_index,
pkt->pts,
pkt->dts,
pkt->size,
pkt->flags);
return 0;
}
(转)ffmpeg 中 av_read_frame_internal分析的更多相关文章
- ffmpeg中AVOption的实现分析
[时间:2017-10] [状态:Open] [关键词:ffmpeg,avutil,AVOption] 0 引言 AVOptions提供了一种通用的options机制,可以用于任意特定结构的对象. 本 ...
- ffmpeg中av_log的实现分析
[时间:2017-10] [状态:Open] [关键词:ffmpeg,avutil,av_log, 日志输出] 0 引言 FFmpeg的libavutil中的日志输出的接口整体比较少,但是功能还是不错 ...
- ffmpeg中AVBuffer的实现分析
[时间:2017-10] [状态:Open] [关键词:ffmpeg,avutil,avbuffer, 引用计数] 0 引言 AVBuffer是ffmpeg提供的基于引用计数的智能指针的一个实现版本. ...
- Ffmpeg解析media容器过程/ ffmpeg 源代码简单分析 : av_read_frame()
ffmpeg 源代码简单分析 : av_read_frame() http://blog.csdn.net/leixiaohua1020/article/details/12678577 ffmpeg ...
- 最新版ffmpeg源码分析
最新版ffmpeg源码分析一:框架 (ffmpeg v0.9) 框架 最新版的ffmpeg中发现了一个新的东西:avconv,而且ffmpeg.c与avconv.c一个模样,一研究才发现是libav下 ...
- FFmpeg源代码简单分析:libavdevice的gdigrab
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- FFmpeg源代码简单分析:libavdevice的avdevice_register_all()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- FFmpeg源代码简单分析:configure
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- FFmpeg源代码简单分析:makefile
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
随机推荐
- 【Bootloader】bootloader启动过程分析
Boot Loader启动过程分析 一. Boot Loader的概念和功能 1.嵌入式Linux软件结构与分布在一般情况下嵌入式Linux系统中的软件主要分为以下及部分: (1)引导加载程序: ...
- 腾讯云主机安装登录mysql失败--解决方案[重置root密码并实现远程连接]
登录MySQL时报错:Access denied for user 'root'@'localhost' (using password: YES) 解决步骤: 1.使用ssh工具连接主机,使用mys ...
- java向mysql插入数据乱码
修改jdbc的链接,将原来的 jdbc:mysql://localhost:3306/demo改为 jdbc:mysql://localhost:3306/demo?us ...
- python 发送邮件 带附件
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/26' # __Desc__ = 实现发送带有各种附件类型的邮件 ...
- jQuery之自定义pagination控件
slpagination 效果: slpagination.js (function($) { $.fn.slpagination = function(options, params) { if ( ...
- ajax无刷新方式对form表单进行赋值!
/** * 把json数据填充到from表单中 */ <form id="editForm" action="user.php"> 用户名:< ...
- UltraEdit 常用快捷方式
Ctrl+N :创建一个新文件 Ctrl+O :打开文件 Ctrl+Q :快速打开文件 Ctrl+F4 :关闭文件 Ctrl+S :保存活动文件 F12 :另存为 Ctrl+P :打印当前活动文件 C ...
- 通过github搭建个人博客
今天突发奇想,想用GitHub搭建一个个人博客,就大概学习了一下,特此记录. 其实非常简单,首先要知道,这里是通过GitHub Pages进行搭建的,什么?不知道什么是GitHub Pages?Git ...
- WebRTC 学习资源 电子书 WebRTC权威指南 Learning WebRTC
webRTC源码下载地址:https://pan.baidu.com/s/18CjClvAuz3B9oF33ngbJIw 提取码:wl1e 1.<WebRTC权威指南>第三版 中文版 本书 ...
- etl的表输入时精度问题
SELECT RecipeID, IngredientID as ingre_id, ROUND(Quantity, 5) Quantity, Preparation, RecipeIngredien ...