FFmpeg avformat_open_input函数优化

简介
    avformat_open_input主要是探测码流的格式,例如是H264或者FLV格式的码流

问题
    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("h264");
        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("h264");
    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




     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1573766,如需转载请自行联系原作者




上一篇:QLExpress的基本语法


下一篇:设置域用户登录主目录