分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
本文对在使用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"
};
/**
* 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;