FFMPeg代码分析:AVCodec结构体以及编解码器的查找和加载

书接上回。在调用av_find_stream_info函数分析媒体文件并找到其中的视频流之后,视频流的相关信息被存放在了AVFormatContext结构体实例中。此时AVCodecContext实例所保存的AVCodec仍然为空。该结构体的定义如下:

typedef struct AVCodec {
    
    const char *name;//codec名称,如果是解码HEVC的文件,那就是"hevc"

    const char *long_name;//codec全名,"HEVC(High Efficient Video Coding)"
    enum AVMediaType type;//当前codec针对的媒体类型,在此为AVMEDIA_TYPE_VIDEO
    enum AVCodecID id;//codec_id,检索codec的依据
    /**
     * Codec capabilities.
     * see CODEC_CAP_*
     */
    int capabilities;
    const AVRational *supported_framerates; //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的码率</span>

    const enum AVPixelFormat *pix_fmts;     //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的像素格式</span>

    const int *supported_samplerates;       //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的采样速率(audio)</span>

    const enum AVSampleFormat *sample_fmts; //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的采样格式(audio)</span>

    const uint64_t *channel_layouts;         //<span style="font-family: Arial, Helvetica, sans-serif;">当前codec支持的声道数</span>
#if FF_API_LOWRES
    uint8_t max_lowres;                     ///< maximum value for lowres supported by the decoder, no direct access, use av_codec_get_max_lowres()
#endif
    const AVClass *priv_class;              ///< AVClass for the private context
    const AVProfile *profiles;              ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}

    /*****************************************************************
     * No fields below this line are part of the public API. They
     * may not be used outside of libavcodec and can be changed and
     * removed at will.
     * New public fields should be added right above.
     *****************************************************************
     */
    int priv_data_size;
    struct AVCodec *next;
    /**
     * @name Frame-level threading support functions
     * @{
     */
    /**
     * If defined, called on thread contexts when they are created.
     * If the codec allocates writable tables in init(), re-allocate them here.
     * priv_data will be set to a copy of the original.
     */
    int (*init_thread_copy)(AVCodecContext *);
    /**
     * Copy necessary context variables from a previous thread context to the current one.
     * If not defined, the next thread will start automatically; otherwise, the codec
     * must call ff_thread_finish_setup().
     *
     * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
     */
    int (*update_thread_context)(AVCodecContext *dst, const AVCodecContext *src);
    /** @} */

    /**
     * Private codec-specific defaults.
     */
    const AVCodecDefault *defaults;

    /**
     * Initialize codec static data, called from avcodec_register().
     */
    void (*init_static_data)(struct AVCodec *codec);

    int (*init)(AVCodecContext *);
    int (*encode_sub)(AVCodecContext *, uint8_t *buf, int buf_size,
                      const struct AVSubtitle *sub);
    /**
     * Encode data to an AVPacket.
     *
     * @param      avctx          codec context
     * @param      avpkt          output AVPacket (may contain a user-provided buffer)
     * @param[in]  frame          AVFrame containing the raw data to be encoded
     * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
     *                            non-empty packet was returned in avpkt.
     * @return 0 on success, negative error code on failure
     */
    int (*encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame,
                   int *got_packet_ptr);
    int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt);
    int (*close)(AVCodecContext *);
    /**
     * Flush buffers.
     * Will be called when seeking
     */
    void (*flush)(AVCodecContext *);
} AVCodec;
在先前的demo中,该结构体实例的codec_id为AV_CODEC_ID_HEVC,表明源文件所包含的视频流应采用HEVC解码器进行解码。函数avcodec_find_decoder通过这个codec_id搜索解码器并返回给一个AVCodec实例,实现代码如下:

AVCodec *avcodec_find_decoder(enum AVCodecID id)
{
    return find_encdec(id, 0);
}
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;
}
int av_codec_is_decoder(const AVCodec *codec)
{
    return codec && codec->decode;
}
在find_encdec函数中可以看出,各个编解码器以链表的形式连接。通过遍历链表的方法找到id与输入id相同的AVCodec并返回。HEVC的解码器也由一个结构体ff_hevc_decoder实现,这个结构体的定义如下(hevc.c):

AVCodec ff_hevc_decoder = {
    .name                  = "hevc",
    .long_name             = NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
    .type                  = AVMEDIA_TYPE_VIDEO,
    .id                    = AV_CODEC_ID_HEVC,
    .priv_data_size        = sizeof(HEVCContext),
    .priv_class            = &hevc_decoder_class,
    .init                  = hevc_decode_init,
    .close                 = hevc_decode_free,
    .decode                = hevc_decode_frame,
    .flush                 = hevc_decode_flush,
    .update_thread_context = hevc_update_thread_context,
    .init_thread_copy      = hevc_init_thread_copy,
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
};

你可能感兴趣的:(ffmpeg,流媒体,视频处理技术)