AVFrame
AVFrame是包含碼流參數(shù)較多的結(jié)構(gòu)體扣囊。本文將會(huì)詳細(xì)分析一下該結(jié)構(gòu)體里主要變量的含義和作用夷家。
首先看一下結(jié)構(gòu)體的定義(位于avcodec.h):
/**
* Audio Video Frame.
* New fields can be added to the end of AVFRAME with minor version
* bumps. Similarly fields that are marked as to be only accessed by
* av_opt_ptr() can be reordered. This allows 2 forks to add fields
* without breaking compatibility with each other.
* Removal, reordering and changes in the remaining cases require
* a major version bump.
* sizeof(AVFrame) must not be used outside libavcodec.
*/
typedef struct AVFrame {
#define AV_NUM_DATA_POINTERS 8
/**圖像數(shù)據(jù)
* pointer to the picture/channel planes.
* This might be different from the first allocated byte
* - encoding: Set by user
* - decoding: set by AVCodecContext.get_buffer()
*/
uint8_t *data[AV_NUM_DATA_POINTERS];
/**
* Size, in bytes, of the data for each picture/channel plane.
*
* For audio, only linesize[0] may be set. For planar audio, each channel
* plane must be the same size.
*
* - encoding: Set by user
* - decoding: set by AVCodecContext.get_buffer()
*/
int linesize[AV_NUM_DATA_POINTERS];
/**
* pointers to the data planes/channels.
*
* For video, this should simply point to data[].
*
* For planar audio, each channel has a separate data pointer, and
* linesize[0] contains the size of each channel buffer.
* For packed audio, there is just one data pointer, and linesize[0]
* contains the total size of the buffer for all channels.
*
* Note: Both data and extended_data will always be set by get_buffer(),
* but for planar audio with more channels that can fit in data,
* extended_data must be used by the decoder in order to access all
* channels.
*
* encoding: unused
* decoding: set by AVCodecContext.get_buffer()
*/
uint8_t **extended_data;
/**寬高
* width and height of the video frame
* - encoding: unused
* - decoding: Read by user.
*/
int width, height;
/**
* number of audio samples (per channel) described by this frame
* - encoding: Set by user
* - decoding: Set by libavcodec
*/
int nb_samples;
/**
* format of the frame, -1 if unknown or unset
* Values correspond to enum AVPixelFormat for video frames,
* enum AVSampleFormat for audio)
* - encoding: unused
* - decoding: Read by user.
*/
int format;
/**是否是關(guān)鍵幀
* 1 -> keyframe, 0-> not
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
int key_frame;
/**幀類(lèi)型(I,B,P)
* Picture type of the frame, see ?_TYPE below.
* - encoding: Set by libavcodec. for coded_picture (and set by user for input).
* - decoding: Set by libavcodec.
*/
enum AVPictureType pict_type;
/**
* pointer to the first allocated byte of the picture. Can be used in get_buffer/release_buffer.
* This isn't used by libavcodec unless the default get/release_buffer() is used.
* - encoding:
* - decoding:
*/
uint8_t *base[AV_NUM_DATA_POINTERS];
/**
* sample aspect ratio for the video frame, 0/1 if unknown/unspecified
* - encoding: unused
* - decoding: Read by user.
*/
AVRational sample_aspect_ratio;
/**
* presentation timestamp in time_base units (time when frame should be shown to user)
* If AV_NOPTS_VALUE then frame_rate = 1/time_base will be assumed.
* - encoding: MUST be set by user.
* - decoding: Set by libavcodec.
*/
int64_t pts;
/**
* reordered pts from the last AVPacket that has been input into the decoder
* - encoding: unused
* - decoding: Read by user.
*/
int64_t pkt_pts;
/**
* dts from the last AVPacket that has been input into the decoder
* - encoding: unused
* - decoding: Read by user.
*/
int64_t pkt_dts;
/**
* picture number in bitstream order
* - encoding: set by
* - decoding: Set by libavcodec.
*/
int coded_picture_number;
/**
* picture number in display order
* - encoding: set by
* - decoding: Set by libavcodec.
*/
int display_picture_number;
/**
* quality (between 1 (good) and FF_LAMBDA_MAX (bad))
* - encoding: Set by libavcodec. for coded_picture (and set by user for input).
* - decoding: Set by libavcodec.
*/
int quality;
/**
* is this picture used as reference
* The values for this are the same as the MpegEncContext.picture_structure
* variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
* Set to 4 for delayed, non-reference frames.
* - encoding: unused
* - decoding: Set by libavcodec. (before get_buffer() call)).
*/
int reference;
/**QP表
* QP table
* - encoding: unused
* - decoding: Set by libavcodec.
*/
int8_t *qscale_table;
/**
* QP store stride
* - encoding: unused
* - decoding: Set by libavcodec.
*/
int qstride;
/**
*
*/
int qscale_type;
/**跳過(guò)宏塊表
* mbskip_table[mb]>=1 if MB didn't change
* stride= mb_width = (width+15)>>4
* - encoding: unused
* - decoding: Set by libavcodec.
*/
uint8_t *mbskip_table;
/**運(yùn)動(dòng)矢量表
* motion vector table
* @code
* example:
* int mv_sample_log2= 4 - motion_subsample_log2;
* int mb_width= (width+15)>>4;
* int mv_stride= (mb_width << mv_sample_log2) + 1;
* motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
* @endcode
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
int16_t (*motion_val[2])[2];
/**宏塊類(lèi)型表
* macroblock type table
* mb_type_base + mb_width + 2
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
uint32_t *mb_type;
/**DCT系數(shù)
* DCT coefficients
* - encoding: unused
* - decoding: Set by libavcodec.
*/
short *dct_coeff;
/**參考幀列表
* motion reference frame index
* the order in which these are stored can depend on the codec.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
int8_t *ref_index[2];
/**
* for some private data of the user
* - encoding: unused
* - decoding: Set by user.
*/
void *opaque;
/**
* error
* - encoding: Set by libavcodec. if flags&CODEC_FLAG_PSNR.
* - decoding: unused
*/
uint64_t error[AV_NUM_DATA_POINTERS];
/**
* type of the buffer (to keep track of who has to deallocate data[*])
* - encoding: Set by the one who allocates it.
* - decoding: Set by the one who allocates it.
* Note: User allocated (direct rendering) & internal buffers cannot coexist currently.
*/
int type;
/**
* When decoding, this signals how much the picture must be delayed.
* extra_delay = repeat_pict / (2*fps)
* - encoding: unused
* - decoding: Set by libavcodec.
*/
int repeat_pict;
/**
* The content of the picture is interlaced.
* - encoding: Set by user.
* - decoding: Set by libavcodec. (default 0)
*/
int interlaced_frame;
/**
* If the content is interlaced, is top field displayed first.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
int top_field_first;
/**
* Tell user application that palette has changed from previous frame.
* - encoding: ??? (no palette-enabled encoder yet)
* - decoding: Set by libavcodec. (default 0).
*/
int palette_has_changed;
/**
* codec suggestion on buffer type if != 0
* - encoding: unused
* - decoding: Set by libavcodec. (before get_buffer() call)).
*/
int buffer_hints;
/**
* Pan scan.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
AVPanScan *pan_scan;
/**
* reordered opaque 64bit (generally an integer or a double precision float
* PTS but can be anything).
* The user sets AVCodecContext.reordered_opaque to represent the input at
* that time,
* the decoder reorders values as needed and sets AVFrame.reordered_opaque
* to exactly one of the values provided by the user through AVCodecContext.reordered_opaque
* @deprecated in favor of pkt_pts
* - encoding: unused
* - decoding: Read by user.
*/
int64_t reordered_opaque;
/**
* hardware accelerator private data (FFmpeg-allocated)
* - encoding: unused
* - decoding: Set by libavcodec
*/
void *hwaccel_picture_private;
/**
* the AVCodecContext which ff_thread_get_buffer() was last called on
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
struct AVCodecContext *owner;
/**
* used by multithreading to store frame-specific info
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
void *thread_opaque;
/**
* log2 of the size of the block which a single vector in motion_val represents:
* (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
* - encoding: unused
* - decoding: Set by libavcodec.
*/
uint8_t motion_subsample_log2;
/**(音頻)采樣率
* Sample rate of the audio data.
*
* - encoding: unused
* - decoding: read by user
*/
int sample_rate;
/**
* Channel layout of the audio data.
*
* - encoding: unused
* - decoding: read by user.
*/
uint64_t channel_layout;
/**
* frame timestamp estimated using various heuristics, in stream time base
* Code outside libavcodec should access this field using:
* av_frame_get_best_effort_timestamp(frame)
* - encoding: unused
* - decoding: set by libavcodec, read by user.
*/
int64_t best_effort_timestamp;
/**
* reordered pos from the last AVPacket that has been input into the decoder
* Code outside libavcodec should access this field using:
* av_frame_get_pkt_pos(frame)
* - encoding: unused
* - decoding: Read by user.
*/
int64_t pkt_pos;
/**
* duration of the corresponding packet, expressed in
* AVStream->time_base units, 0 if unknown.
* Code outside libavcodec should access this field using:
* av_frame_get_pkt_duration(frame)
* - encoding: unused
* - decoding: Read by user.
*/
int64_t pkt_duration;
/**
* metadata.
* Code outside libavcodec should access this field using:
* av_frame_get_metadata(frame)
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
AVDictionary *metadata;
/**
* decode error flags of the frame, set to a combination of
* FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there
* were errors during the decoding.
* Code outside libavcodec should access this field using:
* av_frame_get_decode_error_flags(frame)
* - encoding: unused
* - decoding: set by libavcodec, read by user.
*/
int decode_error_flags;
#define FF_DECODE_ERROR_INVALID_BITSTREAM 1
#define FF_DECODE_ERROR_MISSING_REFERENCE 2
/**
* number of audio channels, only used for audio.
* Code outside libavcodec should access this field using:
* av_frame_get_channels(frame)
* - encoding: unused
* - decoding: Read by user.
*/
int64_t channels;
} AVFrame;
AVFrame結(jié)構(gòu)體一般用于存儲(chǔ)原始數(shù)據(jù)(即非壓縮數(shù)據(jù),例如對(duì)視頻來(lái)說(shuō)是YUV帝美,RGB碍彭,對(duì)音頻來(lái)說(shuō)是PCM),此外還包含了一些相關(guān)的信息悼潭。比如說(shuō)庇忌,解碼的時(shí)候存儲(chǔ)了宏塊類(lèi)型表,QP表女责,運(yùn)動(dòng)矢量表等數(shù)據(jù)漆枚。編碼的時(shí)候也存儲(chǔ)了相關(guān)的數(shù)據(jù)。因此在使用FFMPEG進(jìn)行碼流分析的時(shí)候抵知,AVFrame是一個(gè)很重要的結(jié)構(gòu)體墙基。
下面看幾個(gè)主要變量的作用(在這里考慮解碼的情況):
**uint8_t *data[AV_NUM_DATA_POINTERS]:解碼后原始數(shù)據(jù)(對(duì)視頻來(lái)說(shuō)是YUV,RGB刷喜,對(duì)音頻來(lái)說(shuō)是PCM)
int linesize[AV_NUM_DATA_POINTERS]:data中“一行”數(shù)據(jù)的大小残制。注意:未必等于圖像的寬,一般大于圖像的寬掖疮。
int width, height:視頻幀寬和高(1920x1080,1280x720...)
int nb_samples:音頻的一個(gè)AVFrame中可能包含多個(gè)音頻幀初茶,在此標(biāo)記包含了幾個(gè)
int format:解碼后原始數(shù)據(jù)類(lèi)型(YUV420,YUV422浊闪,RGB24...)
int key_frame:是否是關(guān)鍵幀
enum AVPictureType pict_type:幀類(lèi)型(I,B,P...)
AVRational sample_aspect_ratio:寬高比(16:9恼布,4:3...)
int64_t pts:顯示時(shí)間戳
int coded_picture_number:編碼幀序號(hào)
int display_picture_number:顯示幀序號(hào)
int8_t *qscale_table:QP表
uint8_t mbskip_table:跳過(guò)宏塊表
int16_t (motion_val[2])[2]:運(yùn)動(dòng)矢量表
uint32_t *mb_type:宏塊類(lèi)型表
short dct_coeff:DCT系數(shù)螺戳,這個(gè)沒(méi)有提取過(guò)
int8_t ref_index[2]:運(yùn)動(dòng)估計(jì)參考幀列表(貌似H.264這種比較新的標(biāo)準(zhǔn)才會(huì)涉及到多參考幀)
int interlaced_frame:是否是隔行掃描
uint8_t motion_subsample_log2:一個(gè)宏塊中的運(yùn)動(dòng)矢量采樣個(gè)數(shù),取log的
其他的變量不再一一列舉折汞,源代碼中都有詳細(xì)的說(shuō)明倔幼。在這里重點(diǎn)分析一下幾個(gè)需要一定的理解的變量:
1.data[]
對(duì)于packed格式的數(shù)據(jù)(例如RGB24),會(huì)存到data[0]里面爽待。
對(duì)于planar格式的數(shù)據(jù)(例如YUV420P)损同,則會(huì)分開(kāi)成data[0],data[1]鸟款,data[2]...(YUV420P中data[0]存Y膏燃,data[1]存U,data[2]存V)
具體參見(jiàn):FFMPEG 實(shí)現(xiàn) YUV何什,RGB各種圖像原始數(shù)據(jù)之間的轉(zhuǎn)換(swscale)
2.pict_type
包含以下類(lèi)型:
enum AVPictureType {
AV_PICTURE_TYPE_NONE = 0, ///< Undefined
AV_PICTURE_TYPE_I, ///< Intra
AV_PICTURE_TYPE_P, ///< Predicted
AV_PICTURE_TYPE_B, ///< Bi-dir predicted
AV_PICTURE_TYPE_S, ///< S(GMC)-VOP MPEG4
AV_PICTURE_TYPE_SI, ///< Switching Intra
AV_PICTURE_TYPE_SP, ///< Switching Predicted
AV_PICTURE_TYPE_BI, ///< BI type
};
3.sample_aspect_ratio
寬高比是一個(gè)分?jǐn)?shù)组哩,F(xiàn)FMPEG中用AVRational表達(dá)分?jǐn)?shù):
/**
* rational number numerator/denominator
*/
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
4.qscale_table
QP表指向一塊內(nèi)存,里面存儲(chǔ)的是每個(gè)宏塊的QP值富俄。宏塊的標(biāo)號(hào)是從左往右禁炒,一行一行的來(lái)的。每個(gè)宏塊對(duì)應(yīng)1個(gè)QP霍比。
qscale_table[0]就是第1行第1列宏塊的QP值幕袱;qscale_table[1]就是第1行第2列宏塊的QP值;qscale_table[2]就是第1行第3列宏塊的QP值悠瞬。以此類(lèi)推...
宏塊的個(gè)數(shù)用下式計(jì)算:
注:宏塊大小是16x16的们豌。
每行宏塊數(shù):
int mb_stride = pCodecCtx->width/16+1
宏塊的總數(shù):
int mb_sum = ((pCodecCtx->height+15)>>4)*(pCodecCtx->width/16+1)
5.motion_subsample_log2
1個(gè)運(yùn)動(dòng)矢量所能代表的畫(huà)面大小(用寬或者高表示浅妆,單位是像素)望迎,注意,這里取了log2凌外。
代碼注釋中給出以下數(shù)據(jù):
4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2
即1個(gè)運(yùn)動(dòng)矢量代表16x16的畫(huà)面的時(shí)候辩尊,該值取4;1個(gè)運(yùn)動(dòng)矢量代表8x8的畫(huà)面的時(shí)候康辑,該值取3...以此類(lèi)推
6.motion_val
運(yùn)動(dòng)矢量表存儲(chǔ)了一幀視頻中的所有運(yùn)動(dòng)矢量摄欲。
該值的存儲(chǔ)方式比較特別:
int16_t (*motion_val[2])[2];
注釋中給了一段代碼:
int mv_sample_log2= 4 - motion_subsample_log2;
int mb_width= (width+15)>>4;
int mv_stride= (mb_width << mv_sample_log2) + 1;
motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
該數(shù)據(jù)的結(jié)構(gòu):
1.首先分為兩個(gè)列表L0和L1
2.每個(gè)列表(L0或L1)存儲(chǔ)了一系列的MV(每個(gè)MV對(duì)應(yīng)一個(gè)畫(huà)面,大小由motion_subsample_log2決定)
3.每個(gè)MV分為橫坐標(biāo)和縱坐標(biāo)(x,y)
注意疮薇,在FFMPEG中MV和MB在存儲(chǔ)的結(jié)構(gòu)上是沒(méi)有什么關(guān)聯(lián)的胸墙,第1個(gè)MV是屏幕上左上角畫(huà)面的MV(畫(huà)面的大小取決于motion_subsample_log2),第2個(gè)MV是屏幕上第1行第2列的畫(huà)面的MV按咒,以此類(lèi)推迟隅。因此在一個(gè)宏塊(16x16)的運(yùn)動(dòng)矢量很有可能如下圖所示(line代表一行運(yùn)動(dòng)矢量的個(gè)數(shù)):
//例如8x8劃分的運(yùn)動(dòng)矢量與宏塊的關(guān)系:
//-------------------------
//| | |
//|mv[x] |mv[x+1] |
//-------------------------
//| | |
//|mv[x+line]|mv[x+line+1]|
//-------------------------
7.mb_type
宏塊類(lèi)型表存儲(chǔ)了一幀視頻中的所有宏塊的類(lèi)型。其存儲(chǔ)方式和QP表差不多。只不過(guò)其是uint32類(lèi)型的智袭,而QP表是uint8類(lèi)型的奔缠。每個(gè)宏塊對(duì)應(yīng)一個(gè)宏塊類(lèi)型變量。
宏塊類(lèi)型如下定義所示:
//The following defines may change, don't expect compatibility if you use them.
#define MB_TYPE_INTRA4x4 0x0001
#define MB_TYPE_INTRA16x16 0x0002 //FIXME H.264-specific
#define MB_TYPE_INTRA_PCM 0x0004 //FIXME H.264-specific
#define MB_TYPE_16x16 0x0008
#define MB_TYPE_16x8 0x0010
#define MB_TYPE_8x16 0x0020
#define MB_TYPE_8x8 0x0040
#define MB_TYPE_INTERLACED 0x0080
#define MB_TYPE_DIRECT2 0x0100 //FIXME
#define MB_TYPE_ACPRED 0x0200
#define MB_TYPE_GMC 0x0400
#define MB_TYPE_SKIP 0x0800
#define MB_TYPE_P0L0 0x1000
#define MB_TYPE_P1L0 0x2000
#define MB_TYPE_P0L1 0x4000
#define MB_TYPE_P1L1 0x8000
#define MB_TYPE_L0 (MB_TYPE_P0L0 | MB_TYPE_P1L0)
#define MB_TYPE_L1 (MB_TYPE_P0L1 | MB_TYPE_P1L1)
#define MB_TYPE_L0L1 (MB_TYPE_L0 | MB_TYPE_L1)
#define MB_TYPE_QUANT 0x00010000
#define MB_TYPE_CBP 0x00020000
//Note bits 24-31 are reserved for codec specific use (h264 ref0, mpeg1 0mv, ...)
一個(gè)宏塊如果包含上述定義中的一種或兩種類(lèi)型吼野,則其對(duì)應(yīng)的宏塊變量的對(duì)應(yīng)位會(huì)被置1添坊。
注:一個(gè)宏塊可以包含好幾種類(lèi)型,但是有些類(lèi)型是不能重復(fù)包含的箫锤,比如說(shuō)一個(gè)宏塊不可能既是16x16又是8x8。
8.ref_index
運(yùn)動(dòng)估計(jì)參考幀列表存儲(chǔ)了一幀視頻中所有宏塊的參考幀索引雨女。這個(gè)列表其實(shí)在比較早的壓縮編碼標(biāo)準(zhǔn)中是沒(méi)有什么用的谚攒。只有像H.264這樣的編碼標(biāo)準(zhǔn)才有多參考幀的概念。但是這個(gè)字段目前我還沒(méi)有研究透氛堕。只是知道每個(gè)宏塊包含有4個(gè)該值馏臭,該值反映的是參考幀的索引。