FFmpeg源代码简单分析:av_find_decoder()和av_find_encoder()

=====================================================

FFmpeg的库函数源代码分析文章列表:

【架构图】

FFmpeg源代码结构图 - 解码

FFmpeg源代码结构图 - 编码

【通用】

FFmpeg 源代码简单分析:av_register_all()

FFmpeg 源代码简单分析:avcodec_register_all()

FFmpeg 源代码简单分析:内存的分配和释放(av_malloc()av_free()等)

FFmpeg 源代码简单分析:常见结构体的初始化和销毁(AVFormatContextAVFrame等)

FFmpeg 源代码简单分析:avio_open2()

FFmpeg 源代码简单分析:av_find_decoder()av_find_encoder()

FFmpeg 源代码简单分析:avcodec_open2()

FFmpeg 源代码简单分析:avcodec_close()

【解码】

图解FFMPEG打开媒体的函数avformat_open_input

FFmpeg 源代码简单分析:avformat_open_input()

FFmpeg 源代码简单分析:avformat_find_stream_info()

FFmpeg 源代码简单分析:av_read_frame()

FFmpeg 源代码简单分析:avcodec_decode_video2()

FFmpeg 源代码简单分析:avformat_close_input()

【编码】

FFmpeg 源代码简单分析:avformat_alloc_output_context2()

FFmpeg 源代码简单分析:avformat_write_header()

FFmpeg 源代码简单分析:avcodec_encode_video()

FFmpeg 源代码简单分析:av_write_frame()

FFmpeg 源代码简单分析:av_write_trailer()

【其它】

FFmpeg源代码简单分析:日志输出系统(av_log()等)

FFmpeg源代码简单分析:结构体成员管理系统-AVClass

FFmpeg源代码简单分析:结构体成员管理系统-AVOption

FFmpeg源代码简单分析:libswscalesws_getContext()

FFmpeg源代码简单分析:libswscalesws_scale()

FFmpeg源代码简单分析:libavdeviceavdevice_register_all()

FFmpeg源代码简单分析:libavdevicegdigrab

【脚本】

FFmpeg源代码简单分析:makefile

FFmpeg源代码简单分析:configure

【H.264】

FFmpegH.264解码器源代码简单分析:概述

=====================================================


本文记录FFmpeg的两个API函数:avcodec_find_encoder()和avcodec_find_decoder()。avcodec_find_encoder()用于查找FFmpeg的编码器,avcodec_find_decoder()用于查找FFmpeg的解码器。

avcodec_find_encoder()的声明位于libavcodec\avcodec.h,如下所示。
/**
 * 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);
函数的参数是一个编码器的ID,返回查找到的编码器(没有找到就返回NULL)。
avcodec_find_decoder()的声明也位于libavcodec\avcodec.h,如下所示。
/**
 * 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);

函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。


avcodec_find_encoder()函数最典型的例子可以参考:

最简单的基于FFMPEG的视频编码器(YUV编码为H.264)

avcodec_find_decoder()函数最典型的例子可以参考:

最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)

其实这两个函数的实质就是遍历AVCodec链表并且获得符合条件的元素。有关AVCodec链表的建立可以参考文章:

ffmpeg 源代码简单分析 : av_register_all()


函数调用关系图

avcodec_find_encoder()和avcodec_find_decoder()的函数调用关系图如下所示。


avcodec_find_encoder()

avcodec_find_encoder()的源代码位于libavcodec\utils.c,如下所示。
AVCodec *avcodec_find_encoder(enum AVCodecID id)
{
    return find_encdec(id, 1);
}

从源代码可以看出avcodec_find_encoder()调用了一个find_encdec(),注意它的第二个参数是0。

下面我们看一下find_encdec()的定义。


find_encdec()

find_encdec()的源代码位于libavcodec\utils.c,如下所示。
static AVCodec *first_avcodec;

static AVCodec *find_encdec(enum AVCodecID id, int encoder)
{
    AVCodec *p, *experimental = NULL;
    p = first_avcodec;
    id= remap_deprecated_codec_id(id);
    while (p) {
        if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
            p->id == id) {
            if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
                experimental = p;
            } else
                return p;
        }
        p = p->next;
    }
    return experimental;
}

find_encdec()中有一个循环,该循环会遍历AVCodec结构的链表,逐一比较输入的ID和每一个编码器的ID,直到找到ID取值相等的编码器。
在这里有几点需要注意:
(1)first_avcodec是一个全局变量,存储AVCodec链表的第一个元素。
(2)remap_deprecated_codec_id()用于将一些过时的编码器ID映射到新的编码器ID。
(3)函数的第二个参数encoder用于确定查找编码器还是解码器。当该值为1的时候,用于查找编码器,此时会调用av_codec_is_encoder()判断AVCodec是否为编码器;当该值为0的时候,用于查找解码器,此时会调用av_codec_is_decoder()判断AVCodec是否为解码器。

av_codec_is_encoder()

av_codec_is_encoder()是一个判断AVCodec是否为编码器的函数。如果是编码器,返回非0值,否则返回0。
/**
 * @return a non-zero number if codec is an encoder, zero otherwise
 */
int av_codec_is_encoder(const AVCodec *codec);
av_codec_is_encoder()源代码很简单,如下所示。
int av_codec_is_encoder(const AVCodec *codec)
{
    return codec && (codec->encode_sub || codec->encode2);
}

从源代码可以看出,av_codec_is_encoder()判断了一下AVCodec是否包含了encode2()或者encode_sub()接口函数。


av_codec_is_decoder()

av_codec_is_decoder()是一个判断AVCodec是否为解码器的函数。如果是解码器,返回非0值,否则返回0。
/**
 * @return a non-zero number if codec is a decoder, zero otherwise
 */
int av_codec_is_decoder(const AVCodec *codec);
av_codec_is_decoder()源代码也很简单,如下所示。
int av_codec_is_decoder(const AVCodec *codec)
{
    return codec && codec->decode;
}
从源代码可以看出,av_codec_is_decoder()判断了一下AVCodec是否包含了decode()接口函数。

avcodec_find_decoder()

avcodec_find_decoder()的源代码位于libavcodec\utils.c,如下所示。
AVCodec *avcodec_find_decoder(enum AVCodecID id)
{
    return find_encdec(id, 0);
}
可以看出avcodec_find_decoder()同样调用了find_encdec(),只是第2个参数设置为0。因此不再详细分析。


雷霄骅
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020

FFmpeg 是一款非常流行的开源多媒体框架,其主要功能是对音频视频进行解码、编码、转换等操作。在实际使用中,我们经常会遇到 FFmpeg 内存耗用过多的问题,本文将简单分析 FFmpeg 源代码中的内存分配释放。 FFmpeg 中的内存分配释放主要使用了以下几个函数: 1. av_malloc(size_t size):分配 size 字节的内存,返回指向该内存区域的指针。 2. av_realloc(void *ptr, size_t size):重新分配 ptr 指向的内存,使其大小为 size 字节,返回指向该内存区域的指针。 3. av_free(void *ptr):释放 ptr 指向的内存。 在 FFmpeg 源代码中,这些函数被广泛应用于各种场景,例如解码、编码、转换等操作。下面以 AVPacket 结构体为例,简单分析 FFmpeg 中内存分配释放的过程。 在 FFmpeg 中,AVPacket 结构体用于存储压缩的音频或视频数据。当我们读取一帧音视频数据时,FFmpeg 会先将该数据存储到 AVPacket 结构体中,然后再进行解码等操作。因此,AVPacket 结构体的内存分配释放非常重要。 在 FFmpeg 中,AVPacket 结构体的内存分配释放主要通过以下两个函数实现: 1. av_packet_alloc():分配一个 AVPacket 结构体,返回指向该结构体的指针。 2. av_packet_free(AVPacket **pkt):释放指向 AVPacket 结构体的指针 pkt,并将其置为 NULL。 在具体实现中,av_packet_alloc() 函数使用了 av_malloc() 函数分配内存,而 av_packet_free() 函数使用了 av_free() 函数释放内存。具体代码如下: ```c AVPacket *av_packet_alloc(void) { AVPacket *pkt = av_malloc(sizeof(AVPacket)); if (!pkt) return NULL; av_packet_unref(pkt); return pkt; } void av_packet_free(AVPacket **pkt) { if (!pkt || !*pkt) return; av_packet_unref(*pkt); av_freep(pkt); } ``` 在 av_packet_alloc() 函数中,首先使用 av_malloc() 函数分配了一个 AVPacket 结构体的大小,然后将其传递给 av_packet_unref() 函数。av_packet_unref() 函数会将 AVPacket 结构体中的数据清空,以防止出现未知错误。最后,函数返回该结构体的指针。 在 av_packet_free() 函数中,首先判断传入的指针是否为空,如果为空则直接返回。否则,先调用 av_packet_unref() 函数将 AVPacket 结构体中的数据清空,然后使用 av_freep() 函数释放该结构体的内存,并将指针置为 NULL。 总的来说,FFmpeg 中的内存分配释放是非常重要的一部分,它直接影响着程序的性能稳定性。因此,在使用 FFmpeg 进行开发时,需要注意内存的使用释放,避免出现内存泄漏等问题。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值