FFmpeg common API

First, the common API

1.1 av_register_all ()

Libavformat initialization and registration of all multiplexers, demultiplexers and protocol processors. If you do not call this function, you can call the following three functions to select a supported format.

  • Registration multiplexer function is av_register_output_format().
  • Functions registered demultiplexer is av_register_input_format().
  • Functions registered protocol handler is ffurl_register_protocol().

Note: The above FFmpeg4.0 version, this function is obsolete.


Memory allocation and deallocation 1.2 (av_malloc (), av_free (), etc.)

av_malloc () and av_free () are simple packaging system functions malloc () and free (), and do some error checking work. Similarly there av_realloc ().


1.3 avcodec_find_encoder() 和 avcodec_find_decoder()

avcodec_find_encoder () is used to find the FFmpeg encoder, avcodec_find_decoder () is used to find FFmpeg decoder declaration are located in libavcodec \ avcodec.h. The prototype is as follows:

// 函数的参数是一个编码器的ID,返回查找到的编码器(没有找到就返回NULL)。
AVCodec *avcodec_find_encoder(enum AVCodecID id);

// 函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。
AVCodec *avcodec_find_decoder(enum AVCodecID id);


1.4 avcodec_open2()

AVCodecContext for initializing a video and audio codec, located declaration libavcodec \ utils.c. The prototype is as follows:

int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)  
  • avctx: requires initialization AVCodecContext.
  • codec: AVCodec input.
  • options: some options. For example, when using libx264 encoding, "preset", "tune" and so on may be provided by this parameter.


1.5 avcodec_close()

For closing the encoder, the statement is located libavcodec \ utils.c. The prototype is as follows:

int avcodec_close(AVCodecContext *avctx)

The function takes a single parameter, the encoder needs to close AVCodecContext.


Second, the decoding API

Here are a few need to use the decoding function declarations are in libavformat \ avformat.h.


2.1 avformat_open_input()

Open flow readhead and information output. The prototype is as follows:

int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options) 
  • ps: treated after the function call is successful AVFormatContext structure.
  • url: URL to open video and audio streams.
  • fmt: enforce the AVFormatContext in AVInputFormat of. Generally this parameter can be set to NULL, this can be automatically detected FFmpeg AVInputFormat.
  • options: additional options that, under normal circumstances can be set to NULL.

If the function succeeds, the return value is greater than or equal to 0.


2.2 avformat_find_stream_info()

Reads the audio and video data to get some relevant information. The prototype is as follows:

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)  


2.3 av_read_frame

Reads the audio stream into frames or a video. For example, when decoding video, a video decoding each frame, you need to call av_read_frame () to obtain a compressed video data before the data is decoded. The prototype is as follows:

int av_read_frame(AVFormatContext *s, AVPacket *pkt)    


2.4 avformat_close_input()

Close the open stream. The prototype is as follows:

void avformat_close_input(AVFormatContext **s)  


Third, coding API

FFmpeg based video encoder audio program, avformat_alloc_output_context2 () function is generally the first function call (in addition to the component registration function av_register_all ()). Also introduced FFmpeg write file used three functions, declarations are in libavformat \ avformat.h:

  • av_write_frame () for writing video data;
  • avformat_write_header () for writing the video file header;
  • av_write_trailer () - used to write the end of the video file.


3.1 avformat_alloc_output_context2()

AVFormatContext initialize a structure for output. The prototype is as follows:

int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat * oformat, const char * format_name, const char * filename)


3.2 avformat_write_header()

Assign a stream of private data and write header stream to a media file output. The prototype is as follows:

int avformat_write_header(AVFormatContext *s, AVDictionary ** options)


3.3 av_write_frame()

For outputting a video and audio data. The prototype is as follows:

int av_write_frame(AVFormatContext *s, AVPacket *pkt)

Parameter s is AVFormatContext, pkt parameter for output to AVPacket for output.


3.4 av_write_trailer ()

An output end of the file. The prototype is as follows:

int av_write_trailer(AVFormatContext *s)

It only needs to specify one parameter, i.e. the output for the AVFormatContext, execution returns to the normal function value is equal to 0.


Fourth, the image processing API

libswscale is one of the main library for processing picture pixel data. Pictures can complete the conversion pixel format, image stretching and so on. Number of commonly used functions libswscale rarely, in general, on 3, are located in a statement libswscale \ swscale.h:

sws_getContext():分配和返回一个SwsContext。

sws_scale():处理图像数据。

sws_freeContext():释放一个SwsContext。

Wherein sws_getContext () may be substituted with sws_getCachedContext ().


4.1 sws_getContext()

Allocation and return a SwsContext. The prototype is as follows:

struct SwsContext* sws_getContext   (   int     srcW,
    int     srcH,
    enum AVPixelFormat  srcFormat,
    int     dstW,
    int     dstH,
    enum AVPixelFormat  dstFormat,
    int     flags,
    SwsFilter *     srcFilter,
    SwsFilter *     dstFilter,
    const double *  param 
)   


4.2 sws_scale()

Processing image data. The prototype is as follows:

int sws_scale(struct SwsContext *c,
                                  const uint8_t * const srcSlice[],
                                  const int srcStride[], int srcSliceY,
                                  int srcSliceH, uint8_t *const dst[],
                                  const int dstStride[]) )


4.3 sws_freeContext()

Release a SwsContext. The prototype is as follows:

void sws_freeContext(struct SwsContext *swsContext)


V. Other API

5.1 log output system (av_log (), etc.)

av_log () is a function of the log output FFmpeg. Generally FFmpeg library source code is not allowed printf () This function, always use all output av_log (). av_log () statement is located libavutil \ log.h, whose prototype is as follows:

void av_log(void* avcl, int level, const char *fmt, ...)

The last parameter of the function is "...."
In the C language, a number of arguments in the case of uncertainty "..." to represent the parameters. E.g. printf () prototype is defined as: int printf (const char*, ...).


reference:

FFmpeg Lei Xiao Hua Great God of source code analysis library functions list of articles:

[GM]

FFmpeg simple source code analysis: av_register_all ()

FFmpeg simple source code analysis: avcodec_register_all ()

FFmpeg simple source code analysis: memory allocation and release (av_malloc (), av_free (), etc.)

FFmpeg source code analysis is simple: initialize and destroy a common structure (AVFormatContext, AVFrame, etc.)

FFmpeg simple source code analysis: avio_open2 ()

FFmpeg simple source code analysis: av_find_decoder () and av_find_encoder ()

FFmpeg simple source code analysis: avcodec_open2 ()

FFmpeg simple source code analysis: avcodec_close ()

【decoding】

Open function illustrated FFMPEG media avformat_open_input

FFmpeg simple source code analysis: avformat_open_input ()

FFmpeg simple source code analysis: avformat_find_stream_info ()

FFmpeg simple source code analysis: av_read_frame ()

FFmpeg simple source code analysis: avcodec_decode_video2 ()

FFmpeg simple source code analysis: avformat_close_input ()

【coding】

FFmpeg simple source code analysis: avformat_alloc_output_context2 ()

FFmpeg simple source code analysis: avformat_write_header ()

FFmpeg simple source code analysis: avcodec_encode_video ()

FFmpeg simple source code analysis: av_write_frame ()

FFmpeg simple source code analysis: av_write_trailer ()

【other】

FFmpeg simple source code analysis: log output system (av_log (), etc.)

FFmpeg source code analysis is simple: a structure member management system -AVClass

FFmpeg source code analysis is simple: a structure member management system -AVOption

FFmpeg simple source code analysis: sws_getContext libswscale of ()

FFmpeg simple source code analysis: sws_scale libswscale of ()

FFmpeg simple source code analysis: avdevice_register_all libavdevice of ()

FFmpeg source code for a simple analysis: libavdevice of gdigrab


Simple Book - FFmpeg API- common data structures and functions


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/12041359.html