FFmpeg source code Simple analysis: Av_find_decoder () and Av_find_encoder ()

Source: Internet
Author: User

This document records the two API functions of FFmpeg: Avcodec_find_encoder () and Avcodec_find_decoder (). Avcodec_find_encoder () is used to find the FFmpeg encoder, Avcodec_find_decoder () is used to find the decoder for ffmpeg.
The declaration of Avcodec_find_encoder () is located in Libavcodec\avcodec.h, as shown below.
/** * Find a registered encoder with a matching codec ID. * * @param ID avcodecid of the requested encoder * @return An encoder If one is found, NULL otherwise. */avcodec *avcodec_find_encoder (enum avcodecid ID);
The parameter of the function is the ID of an encoder, which returns the encoder that was found (returns null if no find).
The declaration of Avcodec_find_decoder () is also located in Libavcodec\avcodec.h, as shown below.
/** * 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);

The parameter of the function is the ID of a decoder that returns the found decoder (null is returned without finding).


The most typical example of the Avcodec_find_encoder () function can be consulted:

The simplest video encoder based on FFmpeg (YUV encoded as H.

The most typical example of the Avcodec_find_decoder () function can be consulted:

The simplest ffmpeg+sdl-based video player Ver2 (with SDL2.0)

In fact, the essence of these two functions is to traverse the Avcodec list and get the elements that match the conditions. For the establishment of AVCODEC linked list, refer to the article:

FFmpeg source code Simple analysis: Av_register_all ()


function call Graphs Avcodec_find_encoder () and Avcodec_find_decoder () are shown below.


The source code for Avcodec_find_encoder () Avcodec_find_encoder () is located in Libavcodec\utils.c, as shown below.
Avcodec *avcodec_find_encoder (enum avcodecid ID) {    return Find_encdec (ID, 1);}

From the source code it can be seen that avcodec_find_encoder () called a find_encdec (), note that its second parameter is 0.

Let's take a look at the definition of Find_encdec ().


The source code for FIND_ENCDEC () Find_encdec () is located in Libavcodec\utils.c, as shown below.
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;}

There is a loop in Find_encdec () that iterates through the list of AVCODEC structures, comparing the ID of the input and the ID of each encoder, until it finds an encoder with an equal ID value.
Here are a few things to note:
(1) First_avcodec is a global variable that stores the first element of a avcodec list.
(2) remap_deprecated_codec_id () is used to map some outdated encoder IDs to the new encoder ID.
(3) The second parameter of the function encoder is used to determine whether to find the encoder or decoder. When the value is 1, it is used to find the encoder, which will call Av_codec_is_encoder () to determine whether the AVCODEC is an encoder, and when the value is 0, to find the decoder, the Av_codec_is_decoder () is called at this time Determines whether the AVCODEC is a decoder.

Av_codec_is_encoder () Av_codec_is_encoder () is a function that determines whether a avcodec is an encoder. If it is an encoder, returns a value other than 0, otherwise returns 0.
/** * @return A Non-zero number if codec is an encoder, zero otherwise */int av_codec_is_encoder (const AVCODEC *CODEC);
The Av_codec_is_encoder () source code is simple, as shown below.
int Av_codec_is_encoder (const avcodec *codec) {    return codec && (Codec->encode_sub | | codec->encode2) ;}

As can be seen from the source code, Av_codec_is_encoder () determines whether Avcodec contains the Encode2 () or encode_sub () interface functions.


Av_codec_is_decoder () Av_codec_is_decoder () is a function that determines whether a avcodec is a decoder. If it is a decoder, returns a value other than 0, otherwise returns 0.
/** * @return A Non-zero number if codec is a decoder, zero otherwise */int av_codec_is_decoder (const AVCODEC *CODEC);
The Av_codec_is_decoder () source code is also simple, as shown below.
int Av_codec_is_decoder (const avcodec *codec) {    return codec && Codec->decode;}
As you can see from the source code, Av_codec_is_decoder () determines if the Avcodec contains the decode () interface function.

The source code for Avcodec_find_decoder () Avcodec_find_decoder () is located in Libavcodec\utils.c, as shown below.
Avcodec *avcodec_find_decoder (enum avcodecid ID) {    return Find_encdec (ID, 0);}
It can be seen that Avcodec_find_decoder () also calls Find_encdec (), except that the 2nd parameter is set to 0. Therefore no more detailed analysis.


Lei Huawei
[Email protected]
http://blog.csdn.net/leixiaohua1020

FFmpeg source code Simple analysis: Av_find_decoder () and Av_find_encoder ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.