FFmpeg 常用API

一、通用API

1.1 av_register_all()

初始化 libavformat 和注册所有的复用器、解复用器和协议处理器。如果不调用这个函数,可以调用下面的三个函数来选择支持的格式。

  • 注册复用器的函数是av_register_output_format()
  • 注册解复用器的函数是av_register_input_format()
  • 注册协议处理器的函数是ffurl_register_protocol()

注:FFmpeg4.0 以上的版本,这个函数已经被废弃。

1.2 内存的分配和释放(av_malloc()、av_free()等)

av_malloc() 和 av_free() 都是简单的封装了系统函数 malloc() 和free(),并做了一些错误检查工作。同理的还有 av_realloc()。

1.3 avcodec_find_encoder() 和 avcodec_find_decoder()

avcodec_find_encoder() 用于查找 FFmpeg 的编码器,avcodec_find_decoder() 用于查找 FFmpeg 的解码器,声明都位于 libavcodec\avcodec.h。其原型如下:

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

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

1.4 avcodec_open2()

用于初始化一个视音频编解码器的 AVCodecContext,声明位于 libavcodec\utils.c。其原型如下:

int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)  
  • avctx:需要初始化的 AVCodecContext。
  • codec:输入的AVCodec。
  • options:一些选项。例如使用libx264编码的时候,“preset”,“tune”等都可以通过该参数设置。

1.5 avcodec_close()

用于关闭编码器,声明位于 libavcodec\utils.c。其原型如下:

int avcodec_close(AVCodecContext *avctx)

该函数只有一个参数,就是需要关闭的编码器的 AVCodecContext。

二、解码API

下面介绍解码需要用到的几个函数,声明都位于 libavformat\avformat.h。

2.1 avformat_open_input()

打开输出的流和读取头信息。其原型如下:

int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options) 
  • ps:函数调用成功之后处理过的 AVFormatContext 结构体。
  • url:打开的视音频流的 URL。
  • fmt:强制指定 AVFormatContext 中 AVInputFormat 的。这个参数一般情况下可以设置为 NULL,这样 FFmpeg 可以自动检测 AVInputFormat。
  • options:附加的一些选项,一般情况下可以设置为 NULL。

函数执行成功的话,其返回值大于等于 0。

2.2 avformat_find_stream_info()

读取音视频数据来获取一些相关的信息。其原型如下:

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

2.3 av_read_frame

读取码流中的音频若干帧或者视频一帧。例如,解码视频的时候,每解码一个视频帧,需要先调用 av_read_frame() 获得一帧视频的压缩数据,然后才能对该数据进行解码。其原型如下:

int av_read_frame(AVFormatContext *s, AVPacket *pkt)    

2.4 avformat_close_input()

关闭打开的流。其原型如下:

void avformat_close_input(AVFormatContext **s)  

三、编码API

在基于 FFmpeg 的音视频编码器程序中,avformat_alloc_output_context2() 函数通常是第一个调用的函数(除了组件注册函数 av_register_all())。另外介绍 FFmpeg 的写文件用到的 3 个函数,声明都位于 libavformat\avformat.h:

  • av_write_frame() 用于写视频数据;
  • avformat_write_header() 用于写视频文件头;
  • av_write_trailer() -用于写视频文件尾。

3.1 avformat_alloc_output_context2()

初始化一个用于输出的 AVFormatContext 结构体。其原型如下:

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

3.2 avformat_write_header()

分配一个 stream 的私有数据而且写 stream 的 header 到一个输出的媒体文件。其原型如下:

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

3.3 av_write_frame()

用于输出一帧视音频数据。其原型如下:

int av_write_frame(AVFormatContext *s, AVPacket *pkt)

参数 s 为用于输出的 AVFormatContext,参数 pkt 为等待输出的 AVPacket。

3.4 av_write_trailer()

用于输出文件尾。其原型如下:

int av_write_trailer(AVFormatContext *s)

它只需要指定一个参数,即用于输出的 AVFormatContext,函数正常执行后返回值等于 0。

四、图像处理API

libswscale 是一个主要用于处理图片像素数据的类库。可以完成图片像素格式的转换,图片的拉伸等工作。libswscale 常用的函数数量很少,一般情况下就3个,声明都位于 libswscale\swscale.h:

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

sws_scale():处理图像数据。

sws_freeContext():释放一个SwsContext。

其中 sws_getContext() 也可以用 sws_getCachedContext() 取代。

4.1 sws_getContext()

分配和返回一个 SwsContext。其原型如下:

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()

处理图像数据。其原型如下:

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()

释放一个 SwsContext。其原型如下:

void sws_freeContext(struct SwsContext *swsContext)

五、其它API

5.1 日志输出系统(av_log()等)

av_log() 是 FFmpeg 中输出日志的函数。一般情况下 FFmpeg 类库的源代码中是不允许使用 printf() 这种的函数的,所有的输出一律使用 av_log()。av_log() 的声明位于 libavutil\log.h,其原型如下:

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

函数最后一个参数是 “…”。
在 C 语言中,在函数参数数量不确定的情况下使用 “…” 来代表参数。例如 printf() 的原型定义为:int printf (const char*, ...)

参考:

雷霄骅大神的FFmpeg的库函数源代码分析文章列表:

【通用】

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

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

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

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

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源代码简单分析:libswscale的sws_getContext()

FFmpeg源代码简单分析:libswscale的sws_scale()

FFmpeg源代码简单分析:libavdevice的avdevice_register_all()

FFmpeg源代码简单分析:libavdevice的gdigrab

简书 - FFmpeg API-常用数据结构和函数

原文链接: https://www.cnblogs.com/linuxAndMcu/p/12041359.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    FFmpeg 常用API

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/307681

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午5:28
下一篇 2023年2月16日 上午5:30

相关推荐