This paper will take X264 encoder as an example to explain how FFmpeg calls a third-party encoder to encode.
all encoders and decoders are registered in the Avcodec_register_all () function. the H264 decoder and X264 encoder for the video can be found:
Register_decoder (H264, h264);
Register_encoder (LIBX264, libx264);
They are all registered by a macro:
#define Register_decoder (x, x) \
{ \
extern avcodec ff_# #x # #_decoder; \
if (config_# #X # #_DECODER) \
Avcodec_register (&ff_# #x # #_decoder); \
}
#define Register_encoder (x, x) \
{ \
extern avcodec ff_# #x # #_encoder; \
if (config_# #X # #_ENCODER) \
Avcodec_register (&ff_# #x # #_encoder); \
}
the process of registering occurs in the Avcodec_register (Avcodec *codec) function, in effect adding Libx264_encoder, H264_decoder specific codecs to the global list Last_avcodec, Input parameter avcodec is a struct that can be understood as the base class of a codec, which contains not only attributes such as name, ID, but also the following function pointers, which are implemented for each specific codec extension class.
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.
* would be called when seeking
*/
void (*flush) (Avcodeccontext *);
continue to trace libx264, also known as X264 's static encoding library, which was introduced as a h. A encoder at FFmpeg compile time. The following code is in the libx264.c
Avcodec Ff_libx264_encoder = {
. Name = "Libx264",
. Long_name = Null_if_config_small ("libx264 h.264/avc/mpeg-4 avc/mpeg-4 Part 10"),
. Type = Avmedia_type_video,
. id = av_codec_id_h264,
. priv_data_size = sizeof (X264context),
. init = X264_init,
. Encode2 = X264_frame,
. Close = X264_close,
. capabilities = Codec_cap_delay | Codec_cap_auto_threads,
. Priv_class = &x264_class,
. defaults = X264_defaults,
. Init_static_data = X264_init_static,
};
The properties and methods from Avcodec are assigned here. which
. init = X264_init,
. Encode2 = X264_frame,
. Close = X264_close,
The function pointer is pointed to a specific function, and these three functions will be implemented using the API provided in the libx264 Static library, which is the main interface function of X264.
The x264context shown above encapsulates the context management data required by X264,
typedef struct X264CONTEXT {
Avclass *class;
x264_param_t params;
x264_t *enc;
x264_picture_t pic;
uint8_t *sei;
int sei_size;
Char *preset;
Char *tune;
Char *profile;
Char *level;
int fastfirstpass;
Char *WPREDP;
Char *x264opts;
float CRF;
float Crf_max;
int CQP;
int Aq_mode;
float aq_strength;
Char *psy_rd;
int psy;
int rc_lookahead;
int WEIGHTP;
int WEIGHTB;
int Ssim;
int Intra_refresh;
int Bluray_compat;
int B_bias;
int b_pyramid;
int mixed_refs;
int dct8x8;
int fast_pskip;
int AUD;
int mbtree;
Char *deblock;
float Cplxblur;
Char *partitions;
int direct_pred;
int slice_max_size;
Char *stats;
int nal_hrd;
Char *x264_params;
} X264context;
It belongs to the void *priv_data variable of the struct Avcodeccontext, which defines the context properties for each codec's private, Avcodeccontext is similar to the context base class.you can use class diagrams to represent approximate combinations of codecs.
The codec open operation is done in Transcode_init (), Init_input_stream (), Avcodec_open2 (), to initialize the specific codec. For example X264_init ()
the specific encoding operation is in Transcode (), Transcode_step (), Reap_filters (), do_video_out () or Do_audio_out (), Avcodec _encode_video2 () or Avcodec_encode_audio2 (). A specific codec is then called through a function pointer. For example , X264_frame ().
Finally, the codec is turned off by Avcodec_close (). For example , X264_close ().
FFmpeg Source Analysis Five: FFmpeg call x264 encoder Process Analysis