http://hi.baidu.com/absolutely1st/item/a9f7ce969db02f43f0421573
1. AVFormatContext
AVFormatContext在FFmpeg裡是一個最大的容器,是輸入,輸出資訊的容器。它包含有兩個重要的
成員變數:struct AVInputFormat *iformat (資料輸入格式)和 struct AVOutputFormat *oformat(資料輸出
格式)。兩者不可共存。
AVFormatContext和 XXXContext(eg. AVIContext FLVContext etc.)之間存在著類似多態的關係。即:
AVFormatContext記錄著運行時大家共有的資訊,而各個XXXContext記錄著自己檔案格式的資訊。
AVInputFormat->priv_data_size記錄相應XXXContext的大小,AVFormatContext的void *priv_data記錄
XXXContext指標。
AVFormatContext, AVInputFormat 和 XXXContext 這3者一起完成資料輸入模組。可以這樣認為:
AVFormatContext是個類容器,AVInputFormat 是這個類的操作函數集合,XXXContext則代表該類的
private資料對象。
2.AVInputFormat 和 AVOutputFormat
不錯,這兩個結構體相對於AVFormatContext 就是它的操作函數集合!AVInputFormat和AVOutputFormat
對應的功能就是demuxer 和 muxer . (demuxer 和 muxer 作用就是 “脫衣服” 和 “穿衣服”,具體見
PART ONE).
DEMUXER <------> AVInputFormat 這表示輸入模組的操作集合,就是解去輸入檔案的媒體格式,簡稱
“脫衣服”
開放的介面有:
int(*read_probe)(AVProbeData *);
int(*read_header)(struct AVFormatContext *, AVFormatParameters *ap);
int(*read_packet)(struct AVFormatContext *, AVPacket *pkt);
int(*read_close)(struct AVFormatContext *);
int(*read_seek)(struct AVFormatContext *,int stream_index,int64_t timestamp, int flags);
MUXER <-------> AVOutputFormat 這表示輸出模組的操作集合,就是去封裝輸出檔案的媒體格式,簡稱
“穿衣服”
開放的介面有:
int(*write_header)(struct AVFormatContext *);
int(*write_packet)(struct AVFormatContext *,AVPacket *pkt);
int(*write_trailer)(struct AVFormatContext *);
3.AVStream
經過上面的檔案格式解析後,讀出來的資料儲存在哪裡呢?答案是AVStream.
一個AVStream表示一個流對象,如音頻流,視頻流,nb_streams記錄流對象個數。
AVStream也是一個容器[可以把想成一個Context],它的void *priv_data指向具體的Stream類型對象。
如AVIStream.在AVStream中包含成員 AVCodecContext *actx,記錄具體的編解碼容器。
繼續往下看。
4.AVCodec 和 AVCodecContext
AVCodec對應的是encoder和 decoder 這裡才是真正開始解碼啦(參加PART ONE的圖)。
編碼和解碼開發的介面有:
int(*init)(AVCodecContext *);
int(*encode)(AVCodecContext *,uint8_t *buf, int buf_size,void *data);
int(*close)(AVCodecContext *);
int(*decode)(AVCodecContext *,void *outdata,int *outdata_size,uint8_t *buf,int buf_size);
這裡突然冒出了一個新的結構體 AVCodecContext,其實它和AVCodec的關係類似於:
AVFormatContext 和 AVInputFormat .就是說:
AVCodecContext 是解碼模組的容器類,AVCodec 是操作函數的集合(以上開發的介面可以在AVCodec
結構體中找到) ! AVCodecContext中儲存了AVCodec的指標以及和codec相關的資料(如video的
width,height ,audio的sample rate等)。
5.AVFrame AVPicture 和 AVPacket
這3個東西不怎麼好理解。我們可以這樣想:
一個檔案被DEMUXER----> 解出一路Video Stream ---->該Video Stream 被封裝成若干個AVPacket
---> 送入Decoder ----> Decoder拿到n個AVPacket 組成一個 AVFrame ---->解碼出來。如此往複。
那AVPicture是啥呢?
其實AVPicture 可以想成是一幀映像,是由AVFrame抽象出的概念。AVPicture是AVFrame開始的一部分,
是AVFrame的子集。
AVPicture 這東西 和 YUV 相關,這個我們下次補充一節,專門講這東西。