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"

// 实现在: \ffmpeg\libavcodec\utils.c

// 该函数必须在调用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"

// 实现在:\ffmpeg\libavformat\allformats.c

// 其中会调用avcodec_register_all()注册多种音视频格式的编解码器,并注册各种文件的编解复用器

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

 

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"

// 实现在:\ffmpeg\libavformat\options.c

// 其中负责申请一个AVFormatContext结构的内存,并进行简单初始化

// 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"

// 实现在:\ffmpeg\libavformat\utils.c

// 使用 avformat_alloc_context()分配的结构,采用该函数进行释放,除释放AVFormatContext结构本身内存之外,AVFormatContext中指针所指向的内存也会一并释放

// 有些版本中函数名猜测可能为: 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;

    AVIOContext *pb;

    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; 不能为空, 程序把编码好的音视频包写入到输出容器中

// AVIOContext *pb: I/O上下文,通过对该变量赋值可以改变输入源或输出目的

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

// AVStream **streams; 音视频流

 

6 AVIOContext 结构

/**

 * Bytestream IO 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(AVIOContext) must not be used outside libav*.

 *

 * @note None of the function pointers in AVIOContext should be called

 *       directly, they should only be set by the client application

 *       when implementing custom I/O. Normally these are set to the

 *       function pointers specified in avio_alloc_context()

 */

typedef struct {

    unsigned char *buffer;  /**< Start of the buffer. */

    int buffer_size;        /**< Maximum buffer size */

    unsigned char *buf_ptr; /**< Current position in the buffer */

    unsigned char *buf_end; /**< End of the data, may be less than

                                 buffer+buffer_size if the read function returned

                                 less data than requested, e.g. for streams where

                                 no more data has been received yet. */

    void *opaque;           /**< A private pointer, passed to the read/write/seek/...

                                 functions. */

    int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);

    int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);

    int64_t (*seek)(void *opaque, int64_t offset, int whence);

    int64_t pos;            /**< position in the file of the current buffer */

    int must_flush;         /**< true if the next seek should flush */

    int eof_reached;        /**< true if eof reached */

    int write_flag;         /**< true if open for writing */

#if FF_API_OLD_AVIO

    attribute_deprecated int is_streamed;

#endif

    int max_packet_size;

    unsigned long checksum;

    unsigned char *checksum_ptr;

    unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);

    int error;              /**< contains the error code or 0 if no error happened */

    /**

     * Pause or resume playback for network streaming protocols - e.g. MMS.

     */

    int (*read_pause)(void *opaque, int pause);

    /**

     * Seek to a given timestamp in stream with the specified stream_index.

     * Needed for some network streaming protocols which don't support seeking

     * to byte position.

     */

    int64_t (*read_seek)(void *opaque, int stream_index,

                         int64_t timestamp, int flags);

    /**

     * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.

     */

    int seekable;

} AVIOContext;

// 字节流 I/O 上下文

// 在结构的尾部增加变量可以减少版本冲突

// 移除、排序和修改已经存在的变量将会导致较大的版本冲突

// sizeof(AVIOContext)在libav*.外部不可使用

// AVIOContext里的函数指针不能直接调用,通常使用avio_alloc_context()函数来设置其中的函数指针

// unsigned char *buffer: 缓存的起始指针

// int buffer_size: 缓存的最大值

// void *opaque: 在回调函数中使用的指针

// int (*read_packet)(void *opaque, uint8_t *buf, int buf_size): 读文件回调方法

// int (*write_packet)(void *opaque, uint8_t *buf, int buf_size): 写文件回调方法

// int64_t (*seek)(void *opaque, int64_t offset, int whence): seek文件回调方法

 

7 avio_alloc_context()

/**

 * Allocate and initialize an AVIOContext for buffered I/O. It must be later

 * freed with av_free().

 *

 * @param buffer Memory block for input/output operations via AVIOContext.

 *        The buffer must be allocated with av_malloc() and friends.

 * @param buffer_size The buffer size is very important for performance.

 *        For protocols with fixed blocksize it should be set to this blocksize.

 *        For others a typical size is a cache page, e.g. 4kb.

 * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.

 * @param opaque An opaque pointer to user-specific data.

 * @param read_packet  A function for refilling the buffer, may be NULL.

 * @param write_packet A function for writing the buffer contents, may be NULL.

 * @param seek A function for seeking to specified byte position, may be NULL.

 *

 * @return Allocated AVIOContext or NULL on failure.

 */

AVIOContext *avio_alloc_context(

                  unsigned char *buffer,

                  int buffer_size,

                  int write_flag,

                  void *opaque,

                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),

                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),

                  int64_t (*seek)(void *opaque, int64_t offset, int whence));

// 为I/0缓存申请并初始化一个AVIOContext结构,结束使用时必须使用av_free()进行释放

// unsigned char *buffer: 输入/输出缓存内存块,必须是使用av_malloc()分配的

// int buffer_size: 缓存大小是非常重要的

// int write_flag: 如果缓存为可写则设置为1,否则设置为0

// void *opaque: 指针,用于回调时使用

// int (*read_packet): 读包函数指针

// int (*write_packet): 写包函数指针

// int64_t (*seek): seek文件函数指针

8 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);

// 以输入方式打开一个媒体文件,也即源文件,codecs并没有打开,只读取了文件的头信息.

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

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

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

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

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

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

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

// 不赞成使用 avformat_open_input 代替

 

9 av_close_input_file()

/**

 * @deprecated use avformat_close_input()

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

 *

 * @param s media file handle

 */

void av_close_input_file(AVFormatContext *s);

// 关闭使用avformat_close_input()打开的输入文件容器,但并不关系它的codecs

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

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

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

 

10 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);

// 通过读取媒体文件的中的包来获取媒体文件中的流信息,对于没有头信息的文件如(mpeg)是非常有用的,

// 该函数通常重算类似mpeg-2帧模式的真实帧率,该函数并未改变逻辑文件的position.

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值