温馨提示×

温馨提示×

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

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

FFmpeg AVFormatContext变量的申请以及释放剖析

发布时间:2020-06-06 10:46:17 来源:网络 阅读:944 作者:fengyuzaitu 栏目:软件技术

当前是用的是3.4版本的FFmpeg
av_register_all
    avformat_open_input
        avformat_find_stream_info
            avcodec_find_decoder
                avcodec_open2
av_read_frame
    avcodec_send_packet
        avcodec_receive_frame


申请过程
1)调用avformat_alloc_context创建一个AVFormatContext变量的实例pAVFormatContext
AVFormatContext* pAVFormatContext = avformat_alloc_context();


2)调用avio_alloc_context创建一个AVIOContext变量的实例pAVIOContext
AVIOContext* pAVIOContext = avio_alloc_context(mallocBuffer,  mallocBufferSize, 0, this, ReadStreamData, NULL, NULL);
该函数中ReadStreamData用于读取读取的网络或者文件中的视频或者音频流的函数,mallocBuffer用于保存读取到的数据用于分析,mallocBufferSize是分配的缓存长度,一旦mallocBufferSize申请的缓存长度小于返回读取的数据长度会导致拷贝到缓存中的数据越界,导致程序崩溃


3)如果已经知道数据的格式为h364,调用av_find_input_format创建一个AVInputFormat变量的实例pAVInputFormat
AVInputFormat* pAVInputFormat = av_find_input_format("h364");
pAVFormatContext->iformat = pAVInputFormat;
if (avformat_open_input(&pAVFormatContext, "", pAVInputFormat, NULL) < 0)


4)开始探测码流格式
avformat_find_stream_info(pAVFormatContext, NULL);



释放过程
avformat_close_input(pAVFormatContext);

分析该函数分为三部分
第一部分
关闭输入:
    if (s->iformat)
        if (s->iformat->read_close)
            s->iformat->read_close(s);
对于播放rtsp://admin:admin888@192.168.28.130:554/h364/ch2/main/av_stream,主要是发送TearDown指令给摄像机


第二部分
avio_close(pb)


第三部分
avformat_free_context(s)
该函数的核心就是释放申请创建的视频和音频的流  
 for (i = s->nb_streams - 1; i >= 0; i--)
        ff_free_stream(s, s->streams[i]);


向AI问一下细节

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

AI