成都网站建设设计

将想法与焦点和您一起共享

FFmpegAVFormatContext变量的申请以及释放剖析

当前是用的是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

创新互联是专业的苍梧网站建设公司,苍梧接单;提供网站设计、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行苍梧网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!


申请过程
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]);


当前文章:FFmpegAVFormatContext变量的申请以及释放剖析
文章URL:http://chengdu.cdxwcx.cn/article/gpscde.html