FFmpeg源码分析:avformat_find_stream_info分析码流信息

2024-06-15 10:38

本文主要是介绍FFmpeg源码分析:avformat_find_stream_info分析码流信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

FFmpeg在调用avformat_open_input()之后,可能码流信息不够完整,可以使用avformat_find_stream_info()获取更多的码流信息。比如获取视频帧率、视频宽高,重新计算最大分析时长,打开解码器解码获取codec数据。具体流程如下图所示:

avformat_find_stream_info方法位于libavformat/utils.c,具体代码如下(部分删减):

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
{// 针对flv/mpeg/mpegts格式,max_analyze_duration另外赋值if (!max_analyze_duration) {max_stream_analyze_duration =max_analyze_duration        = 5*AV_TIME_BASE;max_subtitle_analyze_duration = 30*AV_TIME_BASE;if (!strcmp(ic->iformat->name, "flv"))max_stream_analyze_duration = 90*AV_TIME_BASE;if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))max_stream_analyze_duration = 7*AV_TIME_BASE;}for (i = 0; i < ic->nb_streams; i++) {// 查找解码器codec = find_probe_decoder(ic, st, st->codecpar->codec_id);// 确保字幕头信息已正确设置if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE&& codec && !avctx->codec) {if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)av_log(ic, AV_LOG_WARNING, "Failed to open codec in %s\n",__FUNCTION__);}// 尝试打开解码器if (!has_codec_parameters(st, NULL) && st->internal->request_probe <= 0) {if (codec && !avctx->codec)if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)av_log(ic, AV_LOG_WARNING, "Failed to open codec in %s\n",__FUNCTION__);}}......for (;;) {for (i = 0; i < ic->nb_streams; i++) {int fps_analyze_framecount = 20;int count;st = ic->streams[i];if (!has_codec_parameters(st, NULL))break;// 如果时间基是粗糙的,比如mkv采用ms作为单位// 需要分析更多帧数据来获取帧率if (av_q2d(st->time_base) > 0.0005)fps_analyze_framecount *= 2;if (!tb_unreliable(st->internal->avctx))fps_analyze_framecount = 0;if (ic->fps_probe_size >= 0)fps_analyze_framecount = ic->fps_probe_size;if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)fps_analyze_framecount = 0;count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?st->internal->info->codec_info_duration_fields/2 :st->internal->info->duration_count;if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {if (count < fps_analyze_framecount)break;}// 如果存在帧延时,查看前3帧if (st->internal->info->frame_delay_evidence && count < 2 && st->internal->avctx->has_b_frames == 0)break;if (!st->internal->avctx->extradata &&(!st->internal->extract_extradata.inited ||st->internal->extract_extradata.bsf) &&extract_extradata_check(st))break;if (st->first_dts == AV_NOPTS_VALUE &&!(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&st->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))break;}// 读取超过指定最大数据量,但还没获取到足够的解码器信息if (read_size >= probesize) {ret = count;av_log(ic, AV_LOG_DEBUG,"Probe buffer size limit of %"PRId64" bytes reached\n", probesize);for (i = 0; i < ic->nb_streams; i++)if (!ic->streams[i]->r_frame_rate.num &&ic->streams[i]->internal->info->duration_count <= 1 &&ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&strcmp(ic->iformat->name, "image2"))av_log(ic, AV_LOG_WARNING,"Stream #%d: not enough frames to estimate rate; ""consider increasing probesize\n", i);break;}// 读取一帧数据ret = read_frame_internal(ic, pkt1);......if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {// 检测非单调递增的dtsif (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&st->internal->info->fps_last_dts >= pkt->dts) {av_log(ic, AV_LOG_DEBUG,"Non-increasing DTS in stream %d: packet %d with DTS ""%"PRId64", packet %d with DTS %"PRId64"\n",st->index, st->internal->info->fps_last_dts_idx,st->internal->info->fps_last_dts, st->codec_info_nb_frames,pkt->dts);st->internal->info->fps_first_dts =st->internal->info->fps_last_dts  = AV_NOPTS_VALUE;}// 检测不连续的dts:序列的平均帧时长是否超过1000帧存在差异if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&st->internal->info->fps_last_dts_idx > st->internal->info->fps_first_dts_idx &&(pkt->dts - (uint64_t)st->internal->info->fps_last_dts) / 1000 >(st->internal->info->fps_last_dts     - (uint64_t)st->internal->info->fps_first_dts) /(st->internal->info->fps_last_dts_idx - st->internal->info->fps_first_dts_idx)) {st->internal->info->fps_first_dts =st->internal->info->fps_last_dts  = AV_NOPTS_VALUE;}// 更新dtsif (st->internal->info->fps_first_dts == AV_NOPTS_VALUE) {st->internal->info->fps_first_dts     = pkt->dts;st->internal->info->fps_first_dts_idx = st->codec_info_nb_frames;}st->internal->info->fps_last_dts     = pkt->dts;st->internal->info->fps_last_dts_idx = st->codec_info_nb_frames;}// 提取额外数据数组if (!st->internal->avctx->extradata) {ret = extract_extradata(st, pkt);if (ret < 0)goto unref_then_goto_end;}// 如果仍然没有信息,则打开解码器尝试解码一帧// 针对MPEG-4, 需要为QuickTime进行解码try_decode_frame(ic, st, pkt,(options && i < orig_nb_streams) ? &options[i] : NULL);if (ic->flags & AVFMT_FLAG_NOBUFFER)av_packet_unref(pkt1);st->codec_info_nb_frames++;count++;}if (flush_codecs) {for (i = 0; i < ic->nb_streams; i++) {st = ic->streams[i];// 刷新解码器,尝试解码一帧数据if (st->internal->info->found_decoder == 1) {do {err = try_decode_frame(ic, st, empty_pkt,(options && i < orig_nb_streams)? &options[i] : NULL);} while (err > 0 && !has_codec_parameters(st, NULL));}}}......for (i = 0; i < ic->nb_streams; i++) {st = ic->streams[i];avctx = st->internal->avctx;if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);if (avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), tag) == avctx->pix_fmt)avctx->codec_tag= tag;}// 如果demuxer没有设置帧率,则估算平均帧率if (st->internal->info->codec_info_duration_fields &&!st->avg_frame_rate.num &&st->internal->info->codec_info_duration) {int best_fps      = 0;double best_error = 0.01;AVRational codec_frame_rate = avctx->framerate;if (st->internal->info->codec_info_duration        >= INT64_MAX / st->time_base.num / 2||st->internal->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||st->internal->info->codec_info_duration        < 0)continue;av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,st->internal->info->codec_info_duration_fields * (int64_t) st->time_base.den,st->internal->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);// 推测标准帧率for (j = 0; j < MAX_STD_TIMEBASES; j++) {AVRational std_fps = { get_std_framerate(j), 12 * 1001 };double error       = fabs(av_q2d(st->avg_frame_rate) /av_q2d(std_fps) - 1);if (error < best_error) {best_error = error;best_fps   = std_fps.num;}if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {error       = fabs(av_q2d(codec_frame_rate) /av_q2d(std_fps) - 1);if (error < best_error) {best_error = error;best_fps   = std_fps.num;}}}if (best_fps)av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,best_fps, 12 * 1001, INT_MAX);}// 计算帧率的分子与分母if (!st->r_frame_rate.num) {if (    avctx->time_base.den * (int64_t) st->time_base.num<= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) {av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);} else {st->r_frame_rate.num = st->time_base.den;st->r_frame_rate.den = st->time_base.num;}}// 计算宽高比if (st->internal->display_aspect_ratio.num && st->internal->display_aspect_ratio.den) {AVRational hw_ratio = { avctx->height, avctx->width };st->sample_aspect_ratio = av_mul_q(st->internal->display_aspect_ratio,hw_ratio);}}}......
}

1、最大分析时长

max_analyze_duration用于设定探测格式阶段的分析时长,如果最大分析时长没有被赋值,默认值如下:

max_stream_analyze_duration = 5*AV_TIME_BASE;
max_subtitle_analyze_duration = 30*AV_TIME_BASE;
if (!strcmp(ic->iformat->name, "flv"))max_stream_analyze_duration = 90*AV_TIME_BASE;
if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))max_stream_analyze_duration = 7*AV_TIME_BASE;

2、查找解码器

使用find_probe_decoder()来查找解码器,具体代码如下:

static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
{const AVCodec *codec;#if CONFIG_H264_DECODERif (codec_id == AV_CODEC_ID_H264)return avcodec_find_decoder_by_name("h264");
#endifcodec = find_decoder(s, st, codec_id);if (!codec)return NULL;if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {const AVCodec *probe_codec = NULL;void *iter = NULL;while ((probe_codec = av_codec_iterate(&iter))) {if (probe_codec->id == codec->id &&av_codec_is_decoder(probe_codec) &&!(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {return probe_codec;}}}return codec;
}

3、打开解码器

使用avcodec_open2()打开解码器,传递三个参数:AVCodecContext、AVCodec、AVOption。分两种情况:字幕流和缺少codec参数。

3.1 字幕流

如果是字幕流,打开解码器,确保字幕头信息已经正确设置。

3.2 没有codec参数

如果缺少codec参数,也会打开解码器,来获取codec相关参数。判断是否有codec参数方法如下:

static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
{AVCodecContext *avctx = st->internal->avctx;#define FAIL(errmsg) do {                                         \if (errmsg_ptr)                                           \*errmsg_ptr = errmsg;                                 \return 0;                                                 \} while (0)if (   avctx->codec_id == AV_CODEC_ID_NONE&& avctx->codec_type != AVMEDIA_TYPE_DATA)FAIL("unknown codec");switch (avctx->codec_type) {case AVMEDIA_TYPE_AUDIO:if (!avctx->frame_size && determinable_frame_size(avctx))FAIL("unspecified frame size");if (st->internal->info->found_decoder >= 0 &&avctx->sample_fmt == AV_SAMPLE_FMT_NONE)FAIL("unspecified sample format");if (!avctx->sample_rate)FAIL("unspecified sample rate");if (!avctx->channels)FAIL("unspecified number of channels");if (st->internal->info->found_decoder >= 0 && !st->internal->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)FAIL("no decodable DTS frames");break;case AVMEDIA_TYPE_VIDEO:if (!avctx->width)FAIL("unspecified size");if (st->internal->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)FAIL("unspecified pixel format");if (st->codecpar->codec_id == AV_CODEC_ID_RV30 || st->codecpar->codec_id == AV_CODEC_ID_RV40)if (!st->sample_aspect_ratio.num && !st->codecpar->sample_aspect_ratio.num && !st->codec_info_nb_frames)FAIL("no frame in rv30/40 and no sar");break;case AVMEDIA_TYPE_SUBTITLE:if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)FAIL("unspecified size");break;case AVMEDIA_TYPE_DATA:if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;}return 1;
}

检测音频:sample_rate 、sample_format、channels、frame_size;

检测视频:width、pixel_format、sample_aspect_ratio;

检测字幕:如果是PGS图像字幕流,检测width;

4、读取一帧数据

使用read_frame_internal()来读取一帧数据,内部调用ff_read_packet()和parse_packet(),主要过程是读取packet进行组包,最终读取到一个完整帧。

5、检测dts

检测非单调递增的dts,如果dts不是单调递增,那么判定码流有问题。检测不连续的dts,如果序列的平均帧时长超过1000帧存在差异,那么判定dts不连续。最终更新dts。

6、解码一帧

调用try_decode_frame()进行解码,具体代码如下:

static int try_decode_frame(AVFormatContext *s, AVStream *st,const AVPacket *avpkt, AVDictionary **options)
{......if (!avcodec_is_open(avctx) &&st->internal->info->found_decoder <= 0 &&(st->codecpar->codec_id != -st->internal->info->found_decoder || !st->codecpar->codec_id)) {AVDictionary *thread_opt = NULL;codec = find_probe_decoder(s, st, st->codecpar->codec_id);if (!codec) {st->internal->info->found_decoder = -st->codecpar->codec_id;ret                     = -1;goto fail;}av_dict_set(options ? options : &thread_opt, "threads", "1", 0);av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);if (s->codec_whitelist)av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);if (!options)av_dict_free(&thread_opt);if (ret < 0) {st->internal->info->found_decoder = -avctx->codec_id;goto fail;}st->internal->info->found_decoder = 1;} else if (!st->internal->info->found_decoder)st->internal->info->found_decoder = 1;......while ((pkt.size > 0 || (!pkt.data && got_picture)) &&ret >= 0 &&(!has_codec_parameters(st, NULL) || !has_decode_delay_been_guessed(st) ||(!st->codec_info_nb_frames &&(avctx->codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)))) {got_picture = 0;if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||avctx->codec_type == AVMEDIA_TYPE_AUDIO) {ret = avcodec_send_packet(avctx, &pkt);if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)break;if (ret >= 0)pkt.size = 0;ret = avcodec_receive_frame(avctx, frame);if (ret >= 0)got_picture = 1;if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)ret = 0;} else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {ret = avcodec_decode_subtitle2(avctx, &subtitle,&got_picture, &pkt);if (got_picture)avsubtitle_free(&subtitle);if (ret >= 0)pkt.size = 0;}if (ret >= 0) {if (got_picture)st->internal->nb_decoded_frames++;ret       = got_picture;}}......return ret;
}

7、计算帧率

针对mkv格式,时间基单位是ms,需要读取更多帧来计算帧率。针对有帧延时情况,读取前3帧。然后计算帧率的分子与分母。

这篇关于FFmpeg源码分析:avformat_find_stream_info分析码流信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1063223

相关文章

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

Java中最全最基础的IO流概述和简介案例分析

《Java中最全最基础的IO流概述和简介案例分析》JavaIO流用于程序与外部设备的数据交互,分为字节流(InputStream/OutputStream)和字符流(Reader/Writer),处理... 目录IO流简介IO是什么应用场景IO流的分类流的超类类型字节文件流应用简介核心API文件输出流应用文

java 恺撒加密/解密实现原理(附带源码)

《java恺撒加密/解密实现原理(附带源码)》本文介绍Java实现恺撒加密与解密,通过固定位移量对字母进行循环替换,保留大小写及非字母字符,由于其实现简单、易于理解,恺撒加密常被用作学习加密算法的入... 目录Java 恺撒加密/解密实现1. 项目背景与介绍2. 相关知识2.1 恺撒加密算法原理2.2 Ja

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与