温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

FFmpeg avformat_open_input函数剖析以及问题

发布时间:2020-03-30 14:46:26 来源:网络 阅读:1553 作者:fengyuzaitu 栏目:软件技术

函数调用逻辑
avformat_open_input
       init_input
            av_probe_input_buffer2
                av_probe_input_format3
                      read_header


简介
avformat_open_input函数初始化AVFormatContext结构体。其中在初始化AVIOContext结构体参数中调用init_input函数,而它会默认调用av_probe_input_buffer2填充AVIOContext结构体参数。一般情况下,可以优先调用av_probe_input_buffer函数,填充AVIOContext结构体参数,然后赋值给AVFormatContext结构体中的AVIOContext字段
代码
AVIOContext pIOContext= avio_alloc_context
AVInputFormat pInputFormat = NULL
av_probe_input_buffer(pIOContext, &pInputFormat, "", NULL, 0, 0)
AVFormatContext* pFormatContext = avformat_alloc_context();
pFormatContext->pb = pIOContext;
if (avformat_open_input(&FormatContext, "", pInputFormat, NULL) < 0)
根据av_probe_input_buffer会调用av_probe_input_buffer2,包裹了一层
简化代码如下
AVFormatContext* pFormatContext = avformat_alloc_context();
if (avformat_open_input(&FormatContext, "", pInputFormat, NULL) < 0)
根据实际的测试,关键的地方是AVIOContext的初始化,该结构体将会保存一个读取网络数据的函数入口,根据函数入口来分析数据流。avformat_open_input函数在目前的
测试结果是耗时1秒多,这个应该是一个优化的方向
av_probe_input_buffer2主要是针对输入输出结构体AVIOContext的初始化,如果知道avformat_open_input的赋值内容,对各种协议的读写格式的探测,就可以优化这一块代码。协议的探测分别有file协议,rtmp协议等等,目前在项目中只需要实现文件协议,而文件协议应该如何进行读写?
调用ffio_fdopen()函数创建AVIOContext()结构体并获取URLContext结构体引用的资源(调用avio_alloc_context()实现)


问题
    avformat_open_input函数探测ES流开销是150毫秒,探测PS流开销是500毫秒。avformat_open_input函数里面已经实现了av_probe_input_buffer函数的调用,去探测AVInputFormat结构体的相关变量。所以在avformat_open_input函数之前,调用av_probe_input_buffer函数之后,就不会去探测AVInputFormat结构体

优化方向
    尝试屏蔽avformat_open_input函数,直接指定码流的输入格式pInputFormat,代码如下:
        pInputFormat = av_find_input_format("h364");
        pFormatCtx->iformat = pInputFormat;
如果这个时候屏蔽掉avformat_open_input,图像是条带状的,按照参考文献的说法,该avformat_open_input
函数就是为了探测AVInputFormat结构体的相关变量

最终优化方案
    没有屏蔽avformat_open_input函数,而是在调用之前指定AVInputFormat的码流输入格式
代码
    AVInputFormat* pInputFormat = NULL;
    AVFormatContext* pFormatContext = NULL;
    pFormatContext = avformat_alloc_context();
    pInputFormat = av_find_input_format("h364");
    if (avformat_open_input(&pFormatContext, "", InputFormat, NULL) < 0)
    {
        av_free(...);
        avformat_free_context(...);
        fprintf(stdout, "open stream failed!\n");
    }
    else
    {
        fprintf(stdout, "open stream success!\n");
    }


笔记
m_pVideoc->io_ctx = avio_alloc_context(m_pAvioBuf, BUF_SIZE, 0, this, ReadStreamData, NULL, NULL);
if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0)
上面的ReadStreamData实际的用途是在下面打开实时流的时候,如果需要读取数据,可以通过ReadStreamData函数获取到帧的消息内容
而不用通过avformat_open_input函数的第二个参数传递url

参考
http://blog.csdn.net/leo2007608/article/details/53421528
http://blog.csdn.net/leixiaohua1020/article/details/39759163
http://blog.csdn.net/leixiaohua1020/article/details/44064715


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI