ffmpeg结构体以及函数介绍(一)


    本文对在使用ffmpeg进行音视频编解码时使用到的一些函数做一个简单介绍,我当前使用的ffmpeg版本为:0.8.5,因为本人发现在不同的版本中,有些函数名称会有点小改动,所以在此有必要说明下ffmpeg的版本号。
    ffmpeg本人也是刚接触,本文将采用累加的方法逐个介绍我使用到的函数,如有不妥之处,还望谅解!
    头文件引入方法:

extern "C"

{

#include "libavcodec/avcodec.h"

#include "libavformat/avformat.h"

#include "libavutil/avutil.h"

#include "libavutil/mem.h"

#include "libavutil/fifo.h"

#include "libswscale/swscale.h"

};

 

1 avcodec_init()

/**

 * Initialize libavcodec.

 * If called more than once, does nothing.

 *

 * @warning This function must be called before any other libavcodec

 * function.

 *

 * @warning This function is not thread-safe.

 */

void avcodec_init(void);

// 初始化libavcodec,一般最先调用该函数

// 引入头文件: #include "libavcodec/avcodec.h"

// 该函数必须在调用libavcodec里的其它函数前调用,一般在程序启动或模块初始化时调用,如果你调用了多次也无所谓,因为后面的调用不会做任何事情

// 该函数是非线程安全的

 

2 av_register_all()

/**

 * Initialize libavformat and register all the muxers, demuxers and

 * protocols. If you do not call this function, then you can select

 * exactly which formats you want to support.

 *

 * @see av_register_input_format()

 * @see av_register_output_format()

 * @see av_register_protocol()

 */

void av_register_all(void);

// 初始化 libavformat和注册所有的muxers、demuxers和protocols,

// 一般在调用avcodec_init后调用该方法

// 引入头文件:#include "libavformat/avformat.h"

// 当然,你也可以不调用该函数,而通过选择调用特定的方法来提供支持

 

3 avformat_alloc_context()

/**

 * Allocate an AVFormatContext.

 * avformat_free_context() can be used to free the context and everything

 * allocated by the framework within it.

 */

AVFormatContext *avformat_alloc_context(void);

// 分配一个AVFormatContext结构

// 引入头文件:#include "libavformat/avformat.h"

// avformat_free_context()可以用来释放该结构里的所有东西以及该结构本身

// 也是就说使用 avformat_alloc_context()分配的结构,需要使用avformat_free_context()来释放

// 有些版本中函数名可能为: av_alloc_format_context();

 

4 avformat_free_context()

/**

 * Free an AVFormatContext and all its streams.

 * @param s context to free

 */

void avformat_free_context(AVFormatContext *s);

// 释放一个AVFormatContext结构

// 引入头文件:#include "libavformat/avformat.h"

// 使用 avformat_alloc_context()分配的结构,采用该函数进行释放

// 有些版本中函数名猜测可能为: av_free_format_context();

 

5 AVFormatContext 结构

/**

 * Format I/O context.

 * New fields can be added to the end with minor version bumps.

 * Removal, reordering and changes to existing fields require a major

 * version bump.

 * sizeof(AVFormatContext) must not be used outside libav*.

 */

typedef struct AVFormatContext {

    struct AVInputFormat *iformat;

    struct AVOutputFormat *oformat;

    unsigned int nb_streams;

    AVStream **streams;

    char filename[1024]; /**< input or output filename */

.

.

.

} AVFormatContext;

// AVFormatContext在FFMpeg里是一个非常重要的的结构,是其它输入、输出相关信息的一个容器

// 引入头文件:#include "libavformat/avformat.h"

// 以上只列出了其中的部分成员

// 作为输入容器时 struct AVInputFormat *iformat; 不能为空, 其中包含了输入文件的音视频流信息,程序从输入容器从读出音视频包进行解码处理

// 作为输出容器时 struct AVOutputFormat *oformat; 不能为空, 程序把编码好的音视频包写入到输出容器中

// unsigned int nb_streams; 音视频流数量

// AVStream **streams; 音视频流

 

6 av_open_input_file()

/**

 * Open a media file as input. The codecs are not opened. Only the file

 * header (if present) is read.

 *

 * @param ic_ptr The opened media file handle is put here.

 * @param filename filename to open

 * @param fmt If non-NULL, force the file format to use.

 * @param buf_size optional buffer size (zero if default is OK)

 * @param ap Additional parameters needed when opening the file

 *           (NULL if default).

 * @return 0 if OK, AVERROR_xxx otherwise

 *

 * @deprecated use avformat_open_input instead.

 */

attribute_deprecated int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,

                       AVInputFormat *fmt,

                       int buf_size,

                       AVFormatParameters *ap);

// 以输入方式打开一个媒体文件,也即源文件

// 引入头文件:#include "libavformat/avformat.h"

// AVFormatContext **ic_ptr 输入文件容器

// const char *filename 输入文件名,全路径,并且保证文件存在

// AVInputFormat *fmt 输入文件格式,填NULL即可

// int buf_size,缓冲区大小,直接填0即可

// AVFormatParameters *ap, 格式参数,添NULL即可

// 成功返回0,其它失败

// 不赞成使用 avformat_open_input 代替

 

7 av_close_input_file()

/**

 * Close a media file (but not its codecs).

 *

 * @param s media file handle

 */

void av_close_input_file(AVFormatContext *s);

// 关闭输入文件容器

// 引入头文件:#include "libavformat/avformat.h"

// 使用av_open_input_file 打开的文件容器,可以使用该函数关闭

// 使用 av_close_input_file 关闭后,就不再需要使用avformat_free_context 进行释放了

 

8 av_find_stream_info()

/**

 * Read packets of a media file to get stream information. This

 * is useful for file formats with no headers such as MPEG. This

 * function also computes the real framerate in case of MPEG-2 repeat

 * frame mode.

 * The logical file position is not changed by this function;

 * examined packets may be buffered for later processing.

 *

 * @param ic media file handle

 * @return >=0 if OK, AVERROR_xxx on error

 * @todo Let the user decide somehow what information is needed so that

 *       we do not waste time getting stuff the user does not need.

 */

int av_find_stream_info(AVFormatContext *ic);

// 获取媒体文件中的流信息

// 引入头文件:#include "libavformat/avformat.h"

// 也就是把媒体文件中的音视频流等信息读出来,保存在容器中,以便解码时使用

// 返回>=0时成功,否则失败

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值