从头用脚分析FFmpeg源码 --- avcodec_find_decoder | avcodec_find_encoder

文章详细解释了FFmpeg库中avcodec_find_decoder和avcodec_find_encoder函数的作用,它们分别用于根据AVCodecID查找对应的解码器和编码器。这两个函数内部都调用了find_codec通用函数,通过av_codec_iterate遍历所有编解码器,依据av_codec_is_decoder或av_codec_is_encoder判断函数来确定找到的是解码器还是编码器。
摘要由CSDN通过智能技术生成

avcodec_find_decoder作用

/**
 * Find a registered decoder with a matching codec ID.
 *
 * @param id AVCodecID of the requested decoder
 * @return A decoder if one was found, NULL otherwise.
 */
AVCodec *avcodec_find_decoder(enum AVCodecID id);

就很简单,就是根据输入的AVCodecID类型,找到对应的解码器

avcodec_find_encoder作用

/**
 * Find a registered encoder with a matching codec ID.
 *
 * @param id AVCodecID of the requested encoder
 * @return An encoder if one was found, NULL otherwise.
 */
AVCodec *avcodec_find_encoder(enum AVCodecID id);

根据输入的AVCodecID类型,找到对应的编码器

avcodec_find_decoder | avcodec_find_encoder源码分析

AVCodec *avcodec_find_encoder(enum AVCodecID id)
{
    return find_codec(id, av_codec_is_encoder);
}

AVCodec *avcodec_find_decoder(enum AVCodecID id)
{
    return find_codec(id, av_codec_is_decoder);
}

实际调用的是find_codec函数,输入AVCodecID和一个判断函数,avcodec_find_encoder中输入函数指针作用是编解码器是否为编码器,avcodec_find_decoder中则是判断是否为解码器。

find_codec

static AVCodec *find_codec(enum AVCodecID id, int (*x)(const AVCodec *))
{
    const AVCodec *p, *experimental = NULL;
    void *i = 0;
	// 4.4版本里面这个函数没有任何作用
    id = remap_deprecated_codec_id(id);
	// 遍历存储codec的数组,依次找到
    while ((p = av_codec_iterate(&i))) {
    	// 找到的编解码器是否符合
        if (!x(p))
            continue;
        // id相等
        if (p->id == id) {
            if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
                experimental = p;
            } else
                return (AVCodec*)p;
        }
    }

    return (AVCodec*)experimental;
}

av_codec_iterate

const AVCodec *av_codec_iterate(void **opaque)
{
    uintptr_t i = (uintptr_t)*opaque;
    //codec_list是存放所有codec的地址,包括编解码器
    const AVCodec *c = codec_list[i];
	
	// 不管多少线程调用,只执行一次,部分codec的初始化操作
    ff_thread_once(&av_codec_static_init, av_codec_init_static);
	//遍历
    if (c)
        *opaque = (void*)(i + 1);

    return c;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值