AVFormatContext结构体初始化函数 avformat_alloc_context

ffmpeg-4.4 

AVFormatContext结构体初始化函数 


AVFormatContext *avformat_alloc_context(void)
{
    AVFormatContext *ic;
    AVFormatInternal *internal;
//分配内存
    ic = av_malloc(sizeof(AVFormatContext)); 
    if (!ic) return ic;
//为internal分配内存并置零
    internal = av_mallocz(sizeof(*internal));
    if (!internal) {
        av_free(ic);
        return NULL;
    }
//调用avpacket_alloc函数对internal中的pkt和parse_pkt进行初始化
//parse_pkt是一个临时/短期的packet,使用后清空
/*
* /**
     * The generic code uses this as a temporary packet
     * to parse packets; it may also be used for other means
     * for short periods that are guaranteed not to overlap
     * with calls to av_read_frame() (or ff_read_packet())
     * or with each other.
     * It may be used by demuxers as a replacement for
     * stack packets (unless they call one of the aforementioned
     * functions with their own AVFormatContext).
     * Every user has to ensure that this packet is blank
     * after using it.
*/
    internal->pkt = av_packet_alloc();
    internal->parse_pkt = av_packet_alloc();

    if (!internal->pkt || !internal->parse_pkt) {
        av_packet_free(&internal->pkt);
        av_packet_free(&internal->parse_pkt);
        av_free(internal);
        av_free(ic);
        return NULL;
    }
//设置默认值
    avformat_get_context_defaults(ic);
    ic->internal = internal;
    ic->internal->offset = AV_NOPTS_VALUE;
    ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
    ic->internal->shortest_end = AV_NOPTS_VALUE;

    return ic;
}

 

 
posted @ 2021-04-13 17:02  Keep_Silent  阅读(34)  评论(0编辑  收藏  举报