【FFmpeg】AVCodecContext结构体
参考:
FFMPEG结构体分析:AVCodecContext
示例工程:
【FFmpeg】调用ffmpeg库实现264软编
【FFmpeg】调用ffmpeg库实现264软解
【FFmpeg】调用ffmpeg库进行RTMP推流和拉流
【FFmpeg】调用ffmpeg库进行SDL2解码后渲染
流程分析:
【FFmpeg】编码链路上主要函数的简单分析
【FFmpeg】解码链路上主要函数的简单分析
结构体分析:
【FFmpeg】AVCodec结构体
1. AVCodecContext的定义
AVCodecContext的定义位于libavcodec\avcodec.h中,涉及到的变量非常多,下面做一些简单的记录
/**
* main external API structure.
* New fields can be added to the end with minor version bumps.
* Removal, reordering and changes to existing fields require a major
* version bump.
* You can use AVOptions (av_opt* / av_set/get*()) to access these fields from user
* applications.
* The name string for AVOptions options matches the associated command line
* parameter name and can be found in libavcodec/options_table.h
* The AVOption/command line parameter names differ in some cases from the C
* structure field names for historic reasons or brevity.
* sizeof(AVCodecContext) must not be used outside libav*.
*/
// 主要的外部API结构。新字段可以添加到末尾,并伴有轻微的版本颠簸。删除、重新排序和更改现有字段需要一个主要的版本更新
// 可以使用AVOptions (av_opt* / av_set/get*())从用户应用程序访问这些字段
// AVOptions选项的名称字符串与相关的命令行参数名称匹配,可以在libavcodec/options_table.h中找到
// 由于历史原因或简洁,AVOption/命令行参数名在某些情况下与C结构字段名不同
// sizeof(AVCodecContext)不能在libav之外使用
typedef struct AVCodecContext {
/**
* information on struct for av_log
* - set by avcodec_alloc_context3
*/
// 与av_log信息相关的结构体
const AVClass *av_class;
// log_level的偏移量
int log_level_offset;
// 编解码器类型,例如视频、音频等等
enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */
// 编解码器结构体
const struct AVCodec *codec;
// 编解码器ID,例如AV_CODEC_ID_H264
enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */
/**
* fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
* This is used to work around some encoder bugs.
* A demuxer should set this to what is stored in the field used to identify the codec.
* If there are multiple such fields in a container then the demuxer should choose the one
* which maximizes the information about the used codec.
* If the codec tag field in a container is larger than 32 bits then the demuxer should
* remap the longer ID to 32 bits with a table or other structure. Alternatively a new
* extra_codec_tag + size could be added but for this a clear advantage must be demonstrated
* first.
* - encoding: Set by user, if not then the default based on codec_id will be used.
* - decoding: Set by user, will be converted to uppercase by libavcodec during init.
*/
// fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
// 上面fourcc的含义应该是,通过位运算的方式来描述一些操作符
// 这是用来解决一些编码器的错误
// 解码器应该将其设置为存储在用于标识编解码器的字段中的内容。如果在一个容器中有多个这样的字段,那么demuxer应该选择最大限度地获取有关所使用编解码器的信息的字段。
// 如果容器中的编解码器标签字段大于32位,则demuxer应该使用表或其他结构将较长的ID重新映射为32位。或者,可以添加下一个extra_codec_tag + size,但为此必须首先展示一个明显的优势。
unsigned int codec_tag;
// 私有data,常用于数据转换
void *priv_data;
/**
* Private context used for internal data.
*
* Unlike priv_data, this is not codec-specific. It is used in general
* libavcodec functions.
*/
// 用于内部数据的私有上下文。
// 与priv_data不同,这不是特定于编解码器的。它用于一般的libavcodec函数
struct AVCodecInternal *internal;
/**
* Private data of the user, can be used to carry app specific stuff.
* - encoding: Set by user.
* - decoding: Set by user.
*/
// 用户的私人数据,可以用来携带应用程序特定的东西
void *opaque;
/**
* the average bitrate
* - encoding: Set by user; unused for constant quantizer encoding.
* - decoding: Set by user, may be overwritten by libavcodec
* if this info is available in the stream
*/
// 平均码率
// encoding: 用户设置;常数量化器编码不使用
// decoding: 由用户设置,如果该信息在流中可用,则可能被libavcodec覆盖
int64_t bit_rate;
/**
* AV_CODEC_FLAG_*.
* - encoding: Set by user.
* - decoding: Set by user.
*/
// AV_CODEC_FLAG_表示了编解码器的一些基本属性和功能
// 例如帧率,环路滤波,低延时等
int flags;
/**
* AV_CODEC_FLAG2_*
* - encoding: Set by user.
* - decoding: Set by user.
*/
// AV_CODEC_FLAG_表示编解码器的一些扩展功能
// 例如不输出码流,允许不符合规范的加速技巧等等
int flags2;
/**
* some codecs need / can use extradata like Huffman tables.
* MJPEG: Huffman tables
* rv10: additional flags
* MPEG-4: global headers (they can be in the bitstream or here)
* The allocated memory should be AV_INPUT_BUFFER_PADDING_SIZE bytes larger
* than extradata_size to avoid problems if it is read with the bitstream reader.
* The bytewise contents of extradata must not depend on the architecture or CPU endianness.
* Must be allocated with the av_malloc() family of functions.
* - encoding: Set/allocated/freed by libavcodec.
* - decoding: Set/allocated/freed by user.
*/
// 一些编解码器需要/可以使用额外的数据,如霍夫曼表
// MJPEG: 霍夫曼表
// rv10: 额外的标志
// MPEG-4:全局头部(它们可以在比特流中或这里)
// 分配的内存应该比extradata_size大AV_INPUT_BUFFER_PADDING_SIZE字节,以避免使用位流读取器读取时出现问题
// 额外数据的字节内容不能依赖于架构或CPU端序。必须用av_malloc()函数族分配
// encoding: 由libavcodec设置/分配/释放
// decoding: 由用户设置/分配/释放
uint8_t *extradata;
// 额外数据的大小
int extradata_size;
/**
* This is the fundamental unit of time (in seconds) in terms
* of which frame timestamps are represented. For fixed-fps content,
* timebase should be 1/framerate and timestamp increments should be
* identically 1.
* This often, but not always is the inverse of the frame rate or field rate
* for video. 1/time_base is not the average frame rate if the frame rate is not
* constant.
*
* Like containers, elementary streams also can store timestamps, 1/time_base
* is the unit in which these timestamps are specified.
* As example of such codec time base see ISO/IEC 14496-2:2001(E)
* vop_time_increment_resolution and fixed_vop_rate
* (fixed_vop_rate == 0 implies that it is different from the framerate)
*
* - encoding: MUST be set by user.
* - decoding: unused.
*/
// 这是表示帧时间戳的基本时间单位(以秒为单位)。对于固定帧/秒的内容,时基应该是1/帧,时间戳增量应该相同
// 这通常(但不总是)与视频的帧速率或场速率相反。如果帧率不恒定,则1/time_base不是平均帧率
// 与容器一样,基本流也可以存储时间戳,1/time_base是指定这些时间戳的单位。
// 作为这样的编解码器时间基数的例子,请参阅ISO/IEC 14496-2:2001(E) vop_time_increment_resolution和fixed_vop_rate (fixed_vop_rate == 0意味着它与帧率不同)
// encoding: 必须由用户设置
// decoding: 未使用
AVRational time_base;
/**
* Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
* - encoding: unused.
* - decoding: set by user.
*/
// pkt_dts/pts和AVPacket所在的时间基准。表示Dts /pts
// encoding: 未使用
// decoding: 由用户设置
AVRational pkt_timebase;
/**
* - decoding: For codecs that store a framerate value in the compressed
* bitstream, the decoder may export it here. { 0, 1} when
* unknown.
* - encoding: May be used to signal the framerate of CFR content to an
* encoder.
*/
// decoding: 对于在压缩的比特流中存储帧率值的编解码器,解码器可以在这里导出它。当未知时{0,1}
// encoding: 可以用来向编码器发送CFR内容的帧率信号
AVRational framerate;
#if FF_API_TICKS_PER_FRAME
/**
* For some codecs, the time base is closer to the field rate than the frame rate.
* Most notably, H.264 and MPEG-2 specify time_base as half of frame duration
* if no telecine is used ...
*
* Set to time_base ticks per frame. Default 1, e.g., H.264/MPEG-2 set it to 2.
*
* @deprecated
* - decoding: Use AVCodecDescriptor.props & AV_CODEC_PROP_FIELDS
* - encoding: Set AVCodecContext.framerate instead
*
*/
// 对于某些编解码器,时基比帧速率更接近于场速率。
// 最值得注意的是,如果不使用telecine, H.264和MPEG-2将time_base指定为帧持续时间的一半
attribute_deprecated
int ticks_per_frame;
#endif
/**
* Codec delay.
*
* Encoding: Number of frames delay there will be from the encoder input to
* the decoder output. (we assume the decoder matches the spec)
* Decoding: Number of frames delay in addition to what a standard decoder
* as specified in the spec would produce.
*
* Video:
* Number of frames the decoded output will be delayed relative to the
* encoded input.
*
* Audio:
* For encoding, this field is unused (see initial_padding).
*
* For decoding, this is the number of samples the decoder needs to
* output before the decoder's output is valid. When seeking, you should
* start decoding this many samples prior to your desired seek point.
*
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
// 编码器延时
// Encoding: 从编码器输入到解码器输出的帧延迟数。(我们假设解码器符合规格)
// Decoding: 除了规范中指定的标准解码器将产生的帧延迟之外的帧延迟数
// 视频:解码后的输出相对于编码后的输入延迟的帧数
// 音频:对于编码,这个字段是未使用的(参见initial_padding)
// 对于解码,这是在解码器的输出有效之前解码器需要输出的样本数量。在寻找时,您应该在期望的寻道点之前开始解码这么多样本
// encoding: 由libavcodec决定
// decoding: 由libavcodec决定
int delay;
/* video only */
/**
* picture width / height.
*
* @note Those fields may not match the values of the last
* AVFrame output by avcodec_receive_frame() due frame
* reordering.
*
* - encoding: MUST be set by user.
* - decoding: May be set by the user before opening the decoder if known e.g.
* from the container. Some decoders will require the dimensions
* to be set by the caller. During decoding, the decoder may
* overwrite those values as required while parsing the data.
*/
// 视频仅用
// 帧宽/帧高
// 由于帧重排序,这些字段可能与avcodec_receive_frame()最后一个AVFrame输出的值不匹配
// encoding: 必须由用户设置
// decoding: 可以由用户在打开解码器之前设置,例如从容器中知道
// 一些解码器将要求由调用者设置尺寸。在解码期间,解码器可以在解析数据时根据需要覆盖这些值
int width, height;
/**
* Bitstream width / height, may be different from width/height e.g. when
* the decoded frame is cropped before being output or lowres is enabled.
*
* @note Those field may not match the value of the last
* AVFrame output by avcodec_receive_frame() due frame
* reordering.
*
* - encoding: unused
* - decoding: May be set by the user before opening the decoder if known
* e.g. from the container. During decoding, the decoder may
* overwrite those values as required while parsing the data.
*/
// 比特流宽度/高度,可能不同于宽度/高度,例如,当解码帧在输出之前被裁剪或启用低分辨率时
// 由于帧重新排序,这些字段可能与avcodec_receive_frame()最后一个AVFrame输出的值不匹配
// encoding: 未使用
// decoding: 可以由用户在打开解码器之前设置,例如从容器中知道。在解码期间,解码器可以在解析数据时根据需要覆盖这些值
int coded_width, coded_height;
/**
* sample aspect ratio (0 if unknown)
* That is the width of a pixel divided by the height of the pixel.
* Numerator and denominator must be relatively prime and smaller than 256 for some video standards.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
// 样本宽高比(如果未知则为0)
// 这是像素的宽度除以像素的高度
// 对于某些视频标准,分子和分母必须是相对素数且小于256
// encoding: 由用户设置
// decodig: 由libavcodec设置
AVRational sample_aspect_ratio;
/**
* Pixel format, see AV_PIX_FMT_xxx.
* May be set by the demuxer if known from headers.
* May be overridden by the decoder if it knows better.
*
* @note This field may not match the value of the last
* AVFrame output by avcodec_receive_frame() due frame
* reordering.
*
* - encoding: Set by user.
* - decoding: Set by user if known, overridden by libavcodec while
* parsing the data.
*/
// 像素格式参见AV_PIX_FMT_xxx
// 如果从头文件中知道,可以由demuxer设置
// 可能会被解码器覆盖,如果它知道更好
// 由于帧重排序,该字段可能与avcodec_receive_frame()最后一个AVFrame输出的值不匹配
// encoding: 由用户设置
// decoding: 如果已知,由用户设置,在解析数据时由libavcodec覆盖
enum AVPixelFormat pix_fmt;
/**
* Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
* - encoding: unused.
* - decoding: Set by libavcodec before calling get_format()
*/
// 标称非加速像素格式,参见AV_PIX_FMT_xxx
// encoding: 未使用
// decoding: 在调用get_format()之前由libavcodec设置
enum AVPixelFormat sw_pix_fmt;
/**
* Chromaticity coordinates of the source primaries.
* - encoding: Set by user
* - decoding: Set by libavcodec
*/
// 色度坐标
// encoding: 由用户设置
// decoding: 由libavcodec决定
enum AVColorPrimaries color_primaries;
/**
* Color Transfer Characteristic.
* - encoding: Set by user
* - decoding: Set by libavcodec
*/
// 色彩转移特性
// encoding: 由用户设置
// decoding: 由libavcodec设置
enum AVColorTransferCharacteristic color_trc;
/**
* YUV colorspace type.
* - encoding: Set by user
* - decoding: Set by libavcodec
*/
// YUV 色度空间格式
enum AVColorSpace colorspace;
/**
* MPEG vs JPEG YUV range.
* - encoding: Set by user to override the default output color range value,
* If not specified, libavcodec sets the color range depending on the
* output format.
* - decoding: Set by libavcodec, can be set by the user to propagate the
* color range to components reading from the decoder context.
*/
// MPEG 对 JPEG YUV范围
// encoding: 由用户设置以覆盖默认输出颜色范围值,如果未指定,libavcodec将根据输出格式设置颜色范围
// decoding: 由libavcodec设置,可以由用户设置,将颜色范围传播到从解码器上下文读取的组件
enum AVColorRange color_range;
/**
* This defines the location of chroma samples.
* - encoding: Set by user
* - decoding: Set by libavcodec
*/
// 定义色度样本位置
enum AVChromaLocation chroma_sample_location;
/** Field order
* - encoding: set by libavcodec
* - decoding: Set by user.
*/
// 场顺序
enum AVFieldOrder field_order;
/**
* number of reference frames
* - encoding: Set by user.
* - decoding: Set by lavc.
*/
// 参考帧数量
int refs;
/**
* Size of the frame reordering buffer in the decoder.
* For MPEG-2 it is 1 IPB or 0 low delay IP.
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
// 解码器中帧重排序缓冲区的大小
// 对于MPEG-2,它是1ipb或0低延迟IP
int has_b_frames;
/**
* slice flags
* - encoding: unused
* - decoding: Set by user.
*/
// slice flag的定义如下
int slice_flags;
// draw_horizon_band()按编码顺序调用,而不是显示
#define SLICE_FLAG_CODED_ORDER 0x0001 ///< draw_horiz_band() is called in coded order instead of display
// 允许draw_horizon_band()带字段切片(MPEG-2字段图片)
#define SLICE_FLAG_ALLOW_FIELD 0x0002 ///< allow draw_horiz_band() with field slices (MPEG-2 field pics)
// 允许draw_horizon_band()一次使用一个组件(SVQ1)
#define SLICE_FLAG_ALLOW_PLANE 0x0004 ///< allow draw_horiz_band() with 1 component at a time (SVQ1)
/**
* If non NULL, 'draw_horiz_band' is called by the libavcodec
* decoder to draw a horizontal band. It improves cache usage. Not
* all codecs can do that. You must check the codec capabilities
* beforehand.
* When multithreading is used, it may be called from multiple threads
* at the same time; threads might draw different parts of the same AVFrame,
* or multiple AVFrames, and there is no guarantee that slices will be drawn
* in order.
* The function is also used by hardware acceleration APIs.
* It is called at least once during frame decoding to pass
* the data needed for hardware render.
* In that mode instead of pixel data, AVFrame points to
* a structure specific to the acceleration API. The application
* reads the structure and can change some fields to indicate progress
* or mark state.
* - encoding: unused
* - decoding: Set by user.
* @param height the height of the slice
* @param y the y position of the slice
* @param type 1->top field, 2->bottom field, 3->frame
* @param offset offset into the AVFrame.data from which the slice should be read
*/
// 如果非NULL, ' draw_horizon_band '将被libavcodec解码器调用以绘制水平带。它提高了缓存使用率。并不是所有的编解码器都能做到这一点。您必须事先检查编解码器功能
// 当使用多线程时,它可以同时从多个线程调用;线程可以绘制同一个AVFrame的不同部分,或者多个AVFrame,并且不能保证切片将按顺序绘制
// 该函数也被硬件加速api使用
// 它在帧解码期间至少被调用一次,以传递硬件呈现所需的数据
// 在这种模式下,AVFrame指向一个特定于加速API的结构,而不是像素数据。应用程序读取结构,并可以更改一些字段来指示进度或标记状态
void (*draw_horiz_band)(struct AVCodecContext *s,
const AVFrame *src, int offset[AV_NUM_DATA_POINTERS],
int y, int type, int height);
/**
* Callback to negotiate the pixel format. Decoding only, may be set by the
* caller before avcodec_open2().
*
* Called by some decoders to select the pixel format that will be used for
* the output frames. This is mainly used to set up hardware acceleration,
* then the provided format list contains the corresponding hwaccel pixel
* formats alongside the "software" one. The software pixel format may also
* be retrieved from \ref sw_pix_fmt.
*
* This callback will be called when the coded frame properties (such as
* resolution, pixel format, etc.) change and more than one output format is
* supported for those new properties. If a hardware pixel format is chosen
* and initialization for it fails, the callback may be called again
* immediately.
*
* This callback may be called from different threads if the decoder is
* multi-threaded, but not from more than one thread simultaneously.
*
* @param fmt list of formats which may be used in the current
* configuration, terminated by AV_PIX_FMT_NONE.
* @warning Behavior is undefined if the callback returns a value other
* than one of the formats in fmt or AV_PIX_FMT_NONE.
* @return the chosen format or AV_PIX_FMT_NONE
*/
// 回调协商像素格式。只能解码,可以在avcodec_open2()之前由调用者设置
// 由一些解码器调用,以选择将用于输出帧的像素格式。这主要用于设置硬件加速,然后提供的格式列表中包含相应的hwaccel像素格式以及“软件”格式。软件像素格式也可以从\ref sw_pix_fmt中检索
// 当编码帧属性(如分辨率,像素格式等)发生变化并且这些新属性支持多种输出格式时,将调用此回调。如果选择了硬件像素格式,并且初始化失败,则可以立即再次调用回调
// 如果解码器是多线程的,这个回调可以从不同的线程调用,但不能同时从多个线程调用
// @param: fmt当前配置中可能使用的格式列表,以AV_PIX_FMT_NONE结束
// @warning: 如果回调返回的值不是fmt或AV_PIX_FMT_NONE中的格式之一,则未定义行为
// @return: 选择格式或AV_PIX_FMT_NONE
enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
/**
* maximum number of B-frames between non-B-frames
* Note: The output will be delayed by max_b_frames+1 relative to the input.
* - encoding: Set by user.
* - decoding: unused
*/
// 非b帧之间的最大b帧数
// 相对于输入,输出将延迟max_b_frames+1
int max_b_frames;
/**
* qscale factor between IP and B-frames
* If > 0 then the last P-frame quantizer will be used (q= lastp_q*factor+offset).
* If < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset).
* - encoding: Set by user.
* - decoding: unused
*/
// IP和b帧之间的qscale因子
float b_quant_factor;
/**
* qscale offset between IP and B-frames
* - encoding: Set by user.
* - decoding: unused
*/
// IP帧和b帧之间的qscale偏移
float b_quant_offset;
/**
* qscale factor between P- and I-frames
* If > 0 then the last P-frame quantizer will be used (q = lastp_q * factor + offset).
* If < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset).
* - encoding: Set by user.
* - decoding: unused
*/
// P-帧和i -帧之间的qscale因子
float i_quant_factor;
/**
* qscale offset between P and I-frames
* - encoding: Set by user.
* - decoding: unused
*/
// P帧和i帧之间的qscale偏移
float i_quant_offset;
/**
* luminance masking (0-> disabled)
* - encoding: Set by user.
* - decoding: unused
*/
// 亮度mask,如果为0则禁用
float lumi_masking;
/**
* temporary complexity masking (0-> disabled)
* - encoding: Set by user.
* - decoding: unused
*/
// 时域复杂度mask,如果为0则禁用
float temporal_cplx_masking;
/**
* spatial complexity masking (0-> disabled)
* - encoding: Set by user.
* - decoding: unused
*/
// 空域复杂度mask,为0则禁用
float spatial_cplx_masking;
/**
* p block masking (0-> disabled)
* - encoding: Set by user.
* - decoding: unused
*/
// p块的mask
float p_masking;
/**
* darkness masking (0-> disabled)
* - encoding: Set by user.
* - decoding: unused
*/
// 暗度mask
float dark_masking;
/**
* noise vs. sse weight for the nsse comparison function
* - encoding: Set by user.
* - decoding: unused
*/
// 噪声与sse权重的nsse比较函数
int nsse_weight;
/**
* motion estimation comparison function
* - encoding: Set by user.
* - decoding: unused
*/
// 运动估计比较函数
int me_cmp;
/**
* subpixel motion estimation comparison function
* - encoding: Set by user.
* - decoding: unused
*/
// 亚像素运动估计比较功能
int me_sub_cmp;
/**
* macroblock comparison function (not supported yet)
* - encoding: Set by user.
* - decoding: unused
*/
// Macroblock比较函数(暂不支持)
int mb_cmp;
/**
* interlaced DCT comparison function
* - encoding: Set by user.
* - decoding: unused
*/
// interlaced DCT对比函数,下面定义了计算的公式
int ildct_cmp;
#define FF_CMP_SAD 0
#define FF_CMP_SSE 1
#define FF_CMP_SATD 2
#define FF_CMP_DCT 3
#define FF_CMP_PSNR 4
#define FF_CMP_BIT 5
#define FF_CMP_RD 6
#define FF_CMP_ZERO 7
#define FF_CMP_VSAD 8
#define FF_CMP_VSSE 9
#define FF_CMP_NSSE 10
#define FF_CMP_W53 11
#define FF_CMP_W97 12
#define FF_CMP_DCTMAX 13
#define FF_CMP_DCT264 14
#define FF_CMP_MEDIAN_SAD 15
#define FF_CMP_CHROMA 256
/**
* ME diamond size & shape
* - encoding: Set by user.
* - decoding: unused
*/
// 菱形搜索尺寸和形状
int dia_size;
/**
* amount of previous MV predictors (2a+1 x 2a+1 square)
* - encoding: Set by user.
* - decoding: unused
*/
// amount of previous MV predictors (2a+1 x 2a+1 square)
int last_predictor_count;
/**
* motion estimation prepass comparison function
* - encoding: Set by user.
* - decoding: unused
*/
// 运动估计预传比较函数
int me_pre_cmp;
/**
* ME prepass diamond size & shape
* - encoding: Set by user.
* - decoding: unused
*/
// 菱形预先运动估计的尺寸和形状
int pre_dia_size;
/**
* subpel ME quality
* - encoding: Set by user.
* - decoding: unused
*/
// 亚像素me的质量
int me_subpel_quality;
/**
* maximum motion estimation search range in subpel units
* If 0 then no limit.
*
* - encoding: Set by user.
* - decoding: unused
*/
// 最大运动估计搜索范围在子单元,为0则没有限制
int me_range;
/**
* macroblock decision mode
* - encoding: Set by user.
* - decoding: unused
*/
// 宏块决策模式
int mb_decision;
#define FF_MB_DECISION_SIMPLE 0 ///< uses mb_cmp
#define FF_MB_DECISION_BITS 1 ///< chooses the one which needs the fewest bits
#define FF_MB_DECISION_RD 2 ///< rate distortion
/**
* custom intra quantization matrix
* Must be allocated with the av_malloc() family of functions, and will be freed in
* avcodec_free_context().
* - encoding: Set/allocated by user, freed by libavcodec. Can be NULL.
* - decoding: Set/allocated/freed by libavcodec.
*/
// 自定义帧内量化矩阵
// 必须使用av_malloc()函数族分配,并将在avcodec_free_context()中释放
uint16_t *intra_matrix;
/**
* custom inter quantization matrix
* Must be allocated with the av_malloc() family of functions, and will be freed in
* avcodec_free_context().
* - encoding: Set/allocated by user, freed by libavcodec. Can be NULL.
* - decoding: Set/allocated/freed by libavcodec.
*/
// 自定义帧间量化矩阵
// 必须使用av_malloc()函数族分配,并将在avcodec_free_context()中释放
uint16_t *inter_matrix;
/**
* custom intra quantization matrix
* - encoding: Set by user, can be NULL.
* - decoding: unused.
*/
// 色度自定义帧内量化矩阵
uint16_t *chroma_intra_matrix;
/**
* precision of the intra DC coefficient - 8
* - encoding: Set by user.
* - decoding: Set by libavcodec
*/
// 帧内直流系数- 8的精度
int intra_dc_precision;
/**
* minimum MB Lagrange multiplier
* - encoding: Set by user.
* - decoding: unused
*/
// 最小的mb拉格朗日乘子
int mb_lmin;
/**
* maximum MB Lagrange multiplier
* - encoding: Set by user.
* - decoding: unused
*/
// 最大的mb拉格朗日乘子
int mb_lmax;
/**
* - encoding: Set by user.
* - decoding: unused
*/
// 细化双向宏块中使用的两个运动向量
int bidir_refine;
/**
* minimum GOP size
* - encoding: Set by user.
* - decoding: unused
*/
// 最小的GOP大小
int keyint_min;
/**
* the number of pictures in a group of pictures, or 0 for intra_only
* - encoding: Set by user.
* - decoding: unused
*/
// 图片组中的图片数量,intra_only为0
int gop_size;
/**
* Note: Value depends upon the compare function used for fullpel ME.
* - encoding: Set by user.
* - decoding: unused
*/
// 值取决于用于fullpel ME的比较函数
int mv0_threshold;
/**
* Number of slices.
* Indicates number of picture subdivisions. Used for parallelized
* decoding.
* - encoding: Set by user
* - decoding: unused
*/
// slice的数量
// 显示图片细分的个数。用于并行解码
int slices;
/* audio only */
// 【音频】采样率
int sample_rate; ///< samples per second
/**
* audio sample format
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
// 【音频】采样格式
enum AVSampleFormat sample_fmt; ///< sample format
/**
* Audio channel layout.
* - encoding: must be set by the caller, to one of AVCodec.ch_layouts.
* - decoding: may be set by the caller if known e.g. from the container.
* The decoder can then override during decoding as needed.
*/
// 【音频】声道数
AVChannelLayout ch_layout;
/* The following data should not be initialized. */
/**
* Number of samples per channel in an audio frame.
*
* - encoding: set by libavcodec in avcodec_open2(). Each submitted frame
* except the last must contain exactly frame_size samples per channel.
* May be 0 when the codec has AV_CODEC_CAP_VARIABLE_FRAME_SIZE set, then the
* frame size is not restricted.
* - decoding: may be set by some decoders to indicate constant frame size
*/
// 音频帧中每个通道的采样数
int frame_size;
/**
* number of bytes per packet if constant and known or 0
* Used by some WAV based audio codecs.
*/
// 每个数据包的字节数,如果是常量且已知,则为0
// 用于一些基于WAV的音频编解码器
int block_align;
/**
* Audio cutoff bandwidth (0 means "automatic")
* - encoding: Set by user.
* - decoding: unused
*/
// 【音频】截止带宽
int cutoff;
/**
* Type of service that the audio stream conveys.
* - encoding: Set by user.
* - decoding: Set by libavcodec.
*/
// 【音频】音频流所传递的服务类型
enum AVAudioServiceType audio_service_type;
/**
* desired sample format
* - encoding: Not used.
* - decoding: Set by user.
* Decoder will decode to this format if it can.
*/
// 【音频】期望的示例格式
// 如果可以,解码器将解码为这种格式
enum AVSampleFormat request_sample_fmt;
/**
* Audio only. The number of "priming" samples (padding) inserted by the
* encoder at the beginning of the audio. I.e. this number of leading
* decoded samples must be discarded by the caller to get the original audio
* without leading padding.
*
* - decoding: unused
* - encoding: Set by libavcodec. The timestamps on the output packets are
* adjusted by the encoder so that they always refer to the
* first sample of the data actually contained in the packet,
* including any added padding. E.g. if the timebase is
* 1/samplerate and the timestamp of the first input sample is
* 0, the timestamp of the first output packet will be
* -initial_padding.
*/
// 【音频】编码器在音频开始时插入的“启动”样本(填充)的数量
// 也就是说,这个数量的前导解码样本必须被调用者丢弃,以获得没有前导填充的原始音频
// 由libavcodec设置。编码器调整输出数据包上的时间戳,以便它们始终指向数据包中实际包含的数据的第一个样本,包括任何添加的填充
// 例如,如果timebase为1/samplerate,并且第一个输入样本的时间戳为0,则第一个输出数据包的时间戳将为-initial_padding
int initial_padding;
/**
* Audio only. The amount of padding (in samples) appended by the encoder to
* the end of the audio. I.e. this number of decoded samples must be
* discarded by the caller from the end of the stream to get the original
* audio without any trailing padding.
*
* - decoding: unused
* - encoding: unused
*/
// 【音频】由编码器附加到音频末尾的填充量(在样本中)。也就是说
// 这个解码样本的数量必须被调用者从流的末尾丢弃,以获得没有任何尾部填充的原始音频
int trailing_padding;
/**
* Number of samples to skip after a discontinuity
* - decoding: unused
* - encoding: set by libavcodec
*/
// 【音频】不连续后要跳过的样本数
int seek_preroll;
/**
* This callback is called at the beginning of each frame to get data
* buffer(s) for it. There may be one contiguous buffer for all the data or
* there may be a buffer per each data plane or anything in between. What
* this means is, you may set however many entries in buf[] you feel necessary.
* Each buffer must be reference-counted using the AVBuffer API (see description
* of buf[] below).
*
* The following fields will be set in the frame before this callback is
* called:
* - format
* - width, height (video only)
* - sample_rate, channel_layout, nb_samples (audio only)
* Their values may differ from the corresponding values in
* AVCodecContext. This callback must use the frame values, not the codec
* context values, to calculate the required buffer size.
*
* This callback must fill the following fields in the frame:
* - data[]
* - linesize[]
* - extended_data:
* * if the data is planar audio with more than 8 channels, then this
* callback must allocate and fill extended_data to contain all pointers
* to all data planes. data[] must hold as many pointers as it can.
* extended_data must be allocated with av_malloc() and will be freed in
* av_frame_unref().
* * otherwise extended_data must point to data
* - buf[] must contain one or more pointers to AVBufferRef structures. Each of
* the frame's data and extended_data pointers must be contained in these. That
* is, one AVBufferRef for each allocated chunk of memory, not necessarily one
* AVBufferRef per data[] entry. See: av_buffer_create(), av_buffer_alloc(),
* and av_buffer_ref().
* - extended_buf and nb_extended_buf must be allocated with av_malloc() by
* this callback and filled with the extra buffers if there are more
* buffers than buf[] can hold. extended_buf will be freed in
* av_frame_unref().
*
* If AV_CODEC_CAP_DR1 is not set then get_buffer2() must call
* avcodec_default_get_buffer2() instead of providing buffers allocated by
* some other means.
*
* Each data plane must be aligned to the maximum required by the target
* CPU.
*
* @see avcodec_default_get_buffer2()
*
* Video:
*
* If AV_GET_BUFFER_FLAG_REF is set in flags then the frame may be reused
* (read and/or written to if it is writable) later by libavcodec.
*
* avcodec_align_dimensions2() should be used to find the required width and
* height, as they normally need to be rounded up to the next multiple of 16.
*
* Some decoders do not support linesizes changing between frames.
*
* If frame multithreading is used, this callback may be called from a
* different thread, but not from more than one at once. Does not need to be
* reentrant.
*
* @see avcodec_align_dimensions2()
*
* Audio:
*
* Decoders request a buffer of a particular size by setting
* AVFrame.nb_samples prior to calling get_buffer2(). The decoder may,
* however, utilize only part of the buffer by setting AVFrame.nb_samples
* to a smaller value in the output frame.
*
* As a convenience, av_samples_get_buffer_size() and
* av_samples_fill_arrays() in libavutil may be used by custom get_buffer2()
* functions to find the required data size and to fill data pointers and
* linesize. In AVFrame.linesize, only linesize[0] may be set for audio
* since all planes must be the same size.
*
* @see av_samples_get_buffer_size(), av_samples_fill_arrays()
*
* - encoding: unused
* - decoding: Set by libavcodec, user can override.
*/
/*
1.这个回调在每帧开始时被调用,以获取数据缓冲区。可以为所有数据提供一个连续的缓冲区
也可以为每个数据平面或介于两者之间的任何东西提供一个缓冲区。这意味着,
您可以在buf[]中设置任何您认为必要的条目。每个缓冲区必须使用AVBuffer API进行引用计数(参见下面buf[]的描述)
2.在调用这个回调之前,将在帧中设置以下字段:
(1)格式
(2)宽度,高度(仅限视频)
(3)sample_rate, channel_layout, nb_samples(仅音频)
3.它们的值可能与AVCodecContext中的对应值不同。这个回调必须使用frame中的值,而不是AVCodecContext中的值来计算所需的缓冲区大小
4.这个回调必须在帧中填充以下字段
(1)data[]
(2)linesize[]
(3)extended_data:
如果数据是平面音频,有超过8个通道,那么这个回调必须分配和填充extended_data来包含指向所有数据平面的所有指针
Data[]必须保存尽可能多的指针。Extended_data必须使用av_malloc()分配,并将在av_frame_unref()中释放。
否则extended_data必须指向data
(4)buf[]必须包含一个或多个指向AVBufferRef结构的指针。每个帧的data和extended_data指针都必须包含在这些指针中
也就是说,每个分配的内存块对应一个AVBufferRef,而不一定是每个数据[]项对应一个AVBufferRef。
参见:av_buffer_create(), av_buffer_alloc(), av_buffer_ref()
(5)Extended_buf和nb_extended_buf必须由av_malloc()分配,如果缓冲区超过buf[]所能容纳的,则用额外的缓冲区填充
Extended_buf将在av_frame_unref()中被释放
5.如果AV_CODEC_CAP_DR1未设置,则get_buffer2()必须调用avcodec_default_get_buffer2(),而不是通过其他方式提供分配的缓冲区
6.每个数据平面必须对齐目标CPU所需的最大值
【视频】
1.如果在flags中设置了AV_GET_BUFFER_FLAG_REF,那么该帧可能会被libavcodec重用(如果它是可写的,可以读和/或写)
2.应该使用Avcodec_align_dimensions2()来查找所需的宽度和高度,因为它们通常需要四舍五入到16的下一个倍数
3.一些解码器不支持帧之间的linesize变化
4.如果使用帧多线程,这个回调可以从不同的线程调用,但不能一次从多个线程调用。不需要可重入
【音频】
1.解码器通过设置AVFrame请求一个特定大小的缓冲区。在调用get_buffer2()之前调用Nb_samples
然而,解码器可以通过设置AVFrame仅利用部分缓冲区。Nb_samples在输出帧中设置一个较小的值
2.为了方便起见,libavutil中的av_samples_get_buffer_size()和av_samples_fill_arrays()
可以通过自定义get_buffer2()函数来查找所需的数据大小并填充数据指针和linesize。
在AVFrame,因为所有平面必须是相同的大小,所以只能为音频设置Linesize[0]
*/
int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
/* - encoding parameters */
/**
* number of bits the bitstream is allowed to diverge from the reference.
* the reference can be CBR (for CBR pass1) or VBR (for pass2)
* - encoding: Set by user; unused for constant quantizer encoding.
* - decoding: unused
*/
// 码流大小允许偏移的量级
int bit_rate_tolerance;
/**
* Global quality for codecs which cannot change it per frame.
* This should be proportional to MPEG-1/2/4 qscale.
* - encoding: Set by user.
* - decoding: unused
*/
// 编解码器的全局质量,不能每帧改变它。这应该与MPEG-1/2/4 qscale成比例
int global_quality;
/**
* - encoding: Set by user.
* - decoding: unused
*/
// 设置压缩级别,如果没有显式设置许多其他选项,则选择默认值。有效值为0 ~ 12,默认值为5
int compression_level;
#define FF_COMPRESSION_DEFAULT -1
// 简单和困难场景之间的qscale变化量(0.0-1.0)
float qcompress; ///< amount of qscale change between easy & hard scenes (0.0-1.0)
// 随着时间的推移,qscale平滑量(0.0-1.0)
float qblur; ///< amount of qscale smoothing over time (0.0-1.0)
/**
* minimum quantizer
* - encoding: Set by user.
* - decoding: unused
*/
// 最小的量化参数
int qmin;
/**
* maximum quantizer
* - encoding: Set by user.
* - decoding: unused
*/
// 最大的量化参数
int qmax;
/**
* maximum quantizer difference between frames
* - encoding: Set by user.
* - decoding: unused
*/
// 帧之间的最大量化参数的差异
int max_qdiff;
/**
* decoder bitstream buffer size
* - encoding: Set by user.
* - decoding: May be set by libavcodec.
*/
// 解码码流缓冲区大小
int rc_buffer_size;
/**
* ratecontrol override, see RcOverride
* - encoding: Allocated/set/freed by user.
* - decoding: unused
*/
// 码控的覆盖?感觉是利用码控的参数来做编码过程的修正
int rc_override_count;
RcOverride *rc_override;
/**
* maximum bitrate
* - encoding: Set by user.
* - decoding: Set by user, may be overwritten by libavcodec.
*/
// 最大的码率
int64_t rc_max_rate;
/**
* minimum bitrate
* - encoding: Set by user.
* - decoding: unused
*/
// 最小的码率
int64_t rc_min_rate;
/**
* Ratecontrol attempt to use, at maximum, <value> of what can be used without an underflow.
* - encoding: Set by user.
* - decoding: unused.
*/
// 速率控制尝试最大限度地使用<value>可以在没有底流的情况下使用的值
float rc_max_available_vbv_use;
/**
* Ratecontrol attempt to use, at least, <value> times the amount needed to prevent a vbv overflow.
* - encoding: Set by user.
* - decoding: unused.
*/
// 速率控制尝试最大限度地使用<value>可以在没有底流的情况下使用的值
float rc_min_vbv_overflow_use;
/**
* Number of bits which should be loaded into the rc buffer before decoding starts.
* - encoding: Set by user.
* - decoding: unused
*/
// 解码开始前应该载入rc缓冲区的位数
int rc_initial_buffer_occupancy;
/**
* trellis RD quantization
* - encoding: Set by user.
* - decoding: unused
*/
// trellis的RD量化
int trellis;
/**
* pass1 encoding statistics output buffer
* - encoding: Set by libavcodec.
* - decoding: unused
*/
// Pass1编码统计输出缓冲区
char *stats_out;
/**
* pass2 encoding statistics input buffer
* Concatenated stuff from stats_out of pass1 should be placed here.
* - encoding: Allocated/set/freed by user.
* - decoding: unused
*/
// Pass2编码统计输入缓冲区
// 来自stats_out of pass1的连接内容应该放在这里
char *stats_in;
/**
* Work around bugs in encoders which sometimes cannot be detected automatically.
* - encoding: Set by user
* - decoding: Set by user
*/
// 解决编码器中的错误,有时无法自动检测
int workaround_bugs;
#define FF_BUG_AUTODETECT 1 ///< autodetection
#define FF_BUG_XVID_ILACE 4
#define FF_BUG_UMP4 8
#define FF_BUG_NO_PADDING 16
#define FF_BUG_AMV 32
#define FF_BUG_QPEL_CHROMA 64
#define FF_BUG_STD_QPEL 128
#define FF_BUG_QPEL_CHROMA2 256
#define FF_BUG_DIRECT_BLOCKSIZE 512
#define FF_BUG_EDGE 1024
#define FF_BUG_HPEL_CHROMA 2048
#define FF_BUG_DC_CLIP 4096
#define FF_BUG_MS 8192 ///< Work around various bugs in Microsoft's broken decoders.
#define FF_BUG_TRUNCATED 16384
#define FF_BUG_IEDGE 32768
/**
* strictly follow the standard (MPEG-4, ...).
* - encoding: Set by user.
* - decoding: Set by user.
* Setting this to STRICT or higher means the encoder and decoder will
* generally do stupid things, whereas setting it to unofficial or lower
* will mean the encoder might produce output that is not supported by all
* spec-compliant decoders. Decoders don't differentiate between normal,
* unofficial and experimental (that is, they always try to decode things
* when they can) unless they are explicitly asked to behave stupidly
* (=strictly conform to the specs)
* This may only be set to one of the FF_COMPLIANCE_* values in defs.h.
*/
// 将其设置为STRICT或更高意味着编码器和解码器通常会做一些愚蠢的事情
// 而将其设置为非官方或更低将意味着编码器可能会产生不被所有规范兼容的解码器支持的输出
// 解码器不会区分正常的、非官方的和实验性的(也就是说,他们总是在可能的情况下尝试解码)
// 除非他们被明确要求做出愚蠢的行为
// (=严格符合规格)
// 这只能设置为defs.h中的ff_complance_ *值之一
int strict_std_compliance;
/**
* error concealment flags
* - encoding: unused
* - decoding: Set by user.
*/
// 错误隐藏标志
int error_concealment;
#define FF_EC_GUESS_MVS 1
#define FF_EC_DEBLOCK 2
#define FF_EC_FAVOR_INTER 256
/**
* debug
* - encoding: Set by user.
* - decoding: Set by user.
*/
int debug;
#define FF_DEBUG_PICT_INFO 1
#define FF_DEBUG_RC 2
#define FF_DEBUG_BITSTREAM 4
#define FF_DEBUG_MB_TYPE 8
#define FF_DEBUG_QP 16
#define FF_DEBUG_DCT_COEFF 0x00000040
#define FF_DEBUG_SKIP 0x00000080
#define FF_DEBUG_STARTCODE 0x00000100
#define FF_DEBUG_ER 0x00000400
#define FF_DEBUG_MMCO 0x00000800
#define FF_DEBUG_BUGS 0x00001000
#define FF_DEBUG_BUFFERS 0x00008000
#define FF_DEBUG_THREADS 0x00010000
#define FF_DEBUG_GREEN_MD 0x00800000
#define FF_DEBUG_NOMC 0x01000000
/**
* Error recognition; may misdetect some more or less valid parts as errors.
* This is a bitfield of the AV_EF_* values defined in defs.h.
*
* - encoding: Set by user.
* - decoding: Set by user.
*/
// 错误认识;可能会将一些或多或少有效的部分误检测为错误。这是defs.h中定义的AV_EF_*值的位域
int err_recognition;
/**
* Hardware accelerator in use
* - encoding: unused.
* - decoding: Set by libavcodec
*/
// 硬件加速
const struct AVHWAccel *hwaccel;
/**
* Legacy hardware accelerator context.
*
* For some hardware acceleration methods, the caller may use this field to
* signal hwaccel-specific data to the codec. The struct pointed to by this
* pointer is hwaccel-dependent and defined in the respective header. Please
* refer to the FFmpeg HW accelerator documentation to know how to fill
* this.
*
* In most cases this field is optional - the necessary information may also
* be provided to libavcodec through @ref hw_frames_ctx or @ref
* hw_device_ctx (see avcodec_get_hw_config()). However, in some cases it
* may be the only method of signalling some (optional) information.
*
* The struct and its contents are owned by the caller.
*
* - encoding: May be set by the caller before avcodec_open2(). Must remain
* valid until avcodec_free_context().
* - decoding: May be set by the caller in the get_format() callback.
* Must remain valid until the next get_format() call,
* or avcodec_free_context() (whichever comes first).
*/
// 硬件加速的上下文
/*
1.对于某些硬件加速方法,调用者可以使用此字段向编解码器发送hwaccel特定数据的信号
该指针指向的结构体依赖于hwaccel,并在相应的头文件中定义。请参考FFmpeg HW加速器文档来了解如何填写这个
2.在大多数情况下,这个字段是可选的——必要的信息也可以通过@ref hw_frames_ctx或
@ref hw_device_ctx提供给libavcodec(参见avcodec_get_hw_config())
然而,在某些情况下,它可能是发送某些(可选)信息的唯一方法
3.结构体及其内容归调用方所有
*/
void *hwaccel_context;
/**
* A reference to the AVHWFramesContext describing the input (for encoding)
* or output (decoding) frames. The reference is set by the caller and
* afterwards owned (and freed) by libavcodec - it should never be read by
* the caller after being set.
*
* - decoding: This field should be set by the caller from the get_format()
* callback. The previous reference (if any) will always be
* unreffed by libavcodec before the get_format() call.
*
* If the default get_buffer2() is used with a hwaccel pixel
* format, then this AVHWFramesContext will be used for
* allocating the frame buffers.
*
* - encoding: For hardware encoders configured to use a hwaccel pixel
* format, this field should be set by the caller to a reference
* to the AVHWFramesContext describing input frames.
* AVHWFramesContext.format must be equal to
* AVCodecContext.pix_fmt.
*
* This field should be set before avcodec_open2() is called.
*/
/*
1.对AVHWFramesContext的引用,描述输入(用于编码)或输出(解码)帧。引用由调用者设置,
然后由libavcodec拥有(并释放)-在设置之后,它不应该被调用者读取
decoding:
该字段应该由调用者从get_format()回调中设置。之前的引用(如果有的话)总是在get_format()调用之前被libavcodec取消
encoding:
对于配置为使用hwaccel像素格式的硬件编码器,该字段应该由调用者设置为对描述输入帧的AVHWFramesContext的引用。
AVHWFramesContext。格式必须等于AVCodecContext.pix_fmt。这个字段应该在avcodec_open2()被调用之前设置
*/
AVBufferRef *hw_frames_ctx;
/**
* A reference to the AVHWDeviceContext describing the device which will
* be used by a hardware encoder/decoder. The reference is set by the
* caller and afterwards owned (and freed) by libavcodec.
*
* This should be used if either the codec device does not require
* hardware frames or any that are used are to be allocated internally by
* libavcodec. If the user wishes to supply any of the frames used as
* encoder input or decoder output then hw_frames_ctx should be used
* instead. When hw_frames_ctx is set in get_format() for a decoder, this
* field will be ignored while decoding the associated stream segment, but
* may again be used on a following one after another get_format() call.
*
* For both encoders and decoders this field should be set before
* avcodec_open2() is called and must not be written to thereafter.
*
* Note that some decoders may require this field to be set initially in
* order to support hw_frames_ctx at all - in that case, all frames
* contexts used must be created on the same device.
*/
/*
1.对AVHWDeviceContext的引用,描述硬件编码器/解码器将使用的设备
该引用由调用者设置,然后由libavcodec拥有(并释放)
2.如果编解码器设备不需要硬件帧,或者使用的任何硬件帧都将由libavcodec在内部分配,
则应该使用此选项。如果用户希望提供任何用作编码器输入或解码器输出的帧,
则应使用hw_frames_ctx。当在get_format()中为解码器设置hw_frames_ctx时,
该字段将在解码相关流段时被忽略,但可以在下一次get_format()调用之后再次使用
3.对于编码器和解码器,此字段应在调用avcodec_open2()之前设置,并且此后不得写入
4.注意,为了支持hw_frames_ctx,一些解码器可能需要初始设置这个字段——在这种情况下,
所有使用的帧上下文必须在同一设备上创建
*/
AVBufferRef *hw_device_ctx;
/**
* Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated
* decoding (if active).
* - encoding: unused
* - decoding: Set by user (either before avcodec_open2(), or in the
* AVCodecContext.get_format callback)
*/
// AV_HWACCEL_FLAG_*标志的位集,它影响硬件加速解码(如果激活)
int hwaccel_flags;
/**
* Video decoding only. Sets the number of extra hardware frames which
* the decoder will allocate for use by the caller. This must be set
* before avcodec_open2() is called.
*
* Some hardware decoders require all frames that they will use for
* output to be defined in advance before decoding starts. For such
* decoders, the hardware frame pool must therefore be of a fixed size.
* The extra frames set here are on top of any number that the decoder
* needs internally in order to operate normally (for example, frames
* used as reference pictures).
*/
// 【视频】设置解码器将分配给调用者使用的额外硬件帧的数量。这必须在avcodec_open2()被调用之前设置
// 一些硬件解码器需要在解码开始之前预先定义用于输出的所有帧。对于这样的解码器,硬件帧池必须具有固定的大小。
// 这里设置的额外帧是在解码器内部正常运行所需的任何数量之上的(例如,用作参考图片的帧)
int extra_hw_frames;
/**
* error
* - encoding: Set by libavcodec if flags & AV_CODEC_FLAG_PSNR.
* - decoding: unused
*/
// 错误码
uint64_t error[AV_NUM_DATA_POINTERS];
/**
* DCT algorithm, see FF_DCT_* below
* - encoding: Set by user.
* - decoding: unused
*/
// DCT算法
int dct_algo;
#define FF_DCT_AUTO 0
#define FF_DCT_FASTINT 1
#define FF_DCT_INT 2
#define FF_DCT_MMX 3
#define FF_DCT_ALTIVEC 5
#define FF_DCT_FAAN 6
/**
* IDCT algorithm, see FF_IDCT_* below.
* - encoding: Set by user.
* - decoding: Set by user.
*/
// 反DCT算法
int idct_algo;
#define FF_IDCT_AUTO 0
#define FF_IDCT_INT 1
#define FF_IDCT_SIMPLE 2
#define FF_IDCT_SIMPLEMMX 3
#define FF_IDCT_ARM 7
#define FF_IDCT_ALTIVEC 8
#define FF_IDCT_SIMPLEARM 10
#define FF_IDCT_XVID 14
#define FF_IDCT_SIMPLEARMV5TE 16
#define FF_IDCT_SIMPLEARMV6 17
#define FF_IDCT_FAAN 20
#define FF_IDCT_SIMPLENEON 22
#define FF_IDCT_SIMPLEAUTO 128
/**
* bits per sample/pixel from the demuxer (needed for huffyuv).
* - encoding: Set by libavcodec.
* - decoding: Set by user.
*/
// 来自解耦器的每样本/像素位数(huffyuv需要)
int bits_per_coded_sample;
/**
* Bits per sample/pixel of internal libavcodec pixel/sample format.
* - encoding: set by user.
* - decoding: set by libavcodec.
*/
// 每像素使用比特数
int bits_per_raw_sample;
/**
* thread count
* is used to decide how many independent tasks should be passed to execute()
* - encoding: Set by user.
* - decoding: Set by user.
*/
// 线程数量
int thread_count;
/**
* Which multithreading methods to use.
* Use of FF_THREAD_FRAME will increase decoding delay by one frame per thread,
* so clients which cannot provide future frames should not use it.
*
* - encoding: Set by user, otherwise the default is used.
* - decoding: Set by user, otherwise the default is used.
*/
// 线程类型
int thread_type;
#define FF_THREAD_FRAME 1 ///< Decode more than one frame at once
#define FF_THREAD_SLICE 2 ///< Decode more than one part of a single frame at once
/**
* Which multithreading methods are in use by the codec.
* - encoding: Set by libavcodec.
* - decoding: Set by libavcodec.
*/
// 线程使用的方法
int active_thread_type;
/**
* The codec may call this to execute several independent things.
* It will return only after finishing all tasks.
* The user may replace this with some multithreaded implementation,
* the default implementation will execute the parts serially.
* @param count the number of things to execute
* - encoding: Set by libavcodec, user can override.
* - decoding: Set by libavcodec, user can override.
*/
// 编解码器可以调用它来执行几个独立的事情。它只会在完成所有任务后返回
// 用户可以将其替换为一些多线程实现,默认实现将串行执行各部分
int (*execute)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size);
/**
* The codec may call this to execute several independent things.
* It will return only after finishing all tasks.
* The user may replace this with some multithreaded implementation,
* the default implementation will execute the parts serially.
* @param c context passed also to func
* @param count the number of things to execute
* @param arg2 argument passed unchanged to func
* @param ret return values of executed functions, must have space for "count" values. May be NULL.
* @param func function that will be called count times, with jobnr from 0 to count-1.
* threadnr will be in the range 0 to c->thread_count-1 < MAX_THREADS and so that no
* two instances of func executing at the same time will have the same threadnr.
* @return always 0 currently, but code should handle a future improvement where when any call to func
* returns < 0 no further calls to func may be done and < 0 is returned.
* - encoding: Set by libavcodec, user can override.
* - decoding: Set by libavcodec, user can override.
*/
// 编解码器可以调用它来执行几个独立的事情。它只会在完成所有任务后返回
// 用户可以将其替换为一些多线程实现,默认实现将串行执行各部分
int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count);
/**
* profile
* - encoding: Set by user.
* - decoding: Set by libavcodec.
* See the AV_PROFILE_* defines in defs.h.
*/
// profile级别
int profile;
#if FF_API_FF_PROFILE_LEVEL
/** @deprecated The following defines are deprecated; use AV_PROFILE_*
* in defs.h instead. */
#define FF_PROFILE_UNKNOWN -99
#define FF_PROFILE_RESERVED -100
#define FF_PROFILE_AAC_MAIN 0
#define FF_PROFILE_AAC_LOW 1
#define FF_PROFILE_AAC_SSR 2
#define FF_PROFILE_AAC_LTP 3
#define FF_PROFILE_AAC_HE 4
#define FF_PROFILE_AAC_HE_V2 28
#define FF_PROFILE_AAC_LD 22
#define FF_PROFILE_AAC_ELD 38
#define FF_PROFILE_MPEG2_AAC_LOW 128
#define FF_PROFILE_MPEG2_AAC_HE 131
#define FF_PROFILE_DNXHD 0
#define FF_PROFILE_DNXHR_LB 1
#define FF_PROFILE_DNXHR_SQ 2
#define FF_PROFILE_DNXHR_HQ 3
#define FF_PROFILE_DNXHR_HQX 4
#define FF_PROFILE_DNXHR_444 5
#define FF_PROFILE_DTS 20
#define FF_PROFILE_DTS_ES 30
#define FF_PROFILE_DTS_96_24 40
#define FF_PROFILE_DTS_HD_HRA 50
#define FF_PROFILE_DTS_HD_MA 60
#define FF_PROFILE_DTS_EXPRESS 70
#define FF_PROFILE_DTS_HD_MA_X 61
#define FF_PROFILE_DTS_HD_MA_X_IMAX 62
#define FF_PROFILE_EAC3_DDP_ATMOS 30
#define FF_PROFILE_TRUEHD_ATMOS 30
#define FF_PROFILE_MPEG2_422 0
#define FF_PROFILE_MPEG2_HIGH 1
#define FF_PROFILE_MPEG2_SS 2
#define FF_PROFILE_MPEG2_SNR_SCALABLE 3
#define FF_PROFILE_MPEG2_MAIN 4
#define FF_PROFILE_MPEG2_SIMPLE 5
#define FF_PROFILE_H264_CONSTRAINED (1<<9) // 8+1; constraint_set1_flag
#define FF_PROFILE_H264_INTRA (1<<11) // 8+3; constraint_set3_flag
#define FF_PROFILE_H264_BASELINE 66
#define FF_PROFILE_H264_CONSTRAINED_BASELINE (66|FF_PROFILE_H264_CONSTRAINED)
#define FF_PROFILE_H264_MAIN 77
#define FF_PROFILE_H264_EXTENDED 88
#define FF_PROFILE_H264_HIGH 100
#define FF_PROFILE_H264_HIGH_10 110
#define FF_PROFILE_H264_HIGH_10_INTRA (110|FF_PROFILE_H264_INTRA)
#define FF_PROFILE_H264_MULTIVIEW_HIGH 118
#define FF_PROFILE_H264_HIGH_422 122
#define FF_PROFILE_H264_HIGH_422_INTRA (122|FF_PROFILE_H264_INTRA)
#define FF_PROFILE_H264_STEREO_HIGH 128
#define FF_PROFILE_H264_HIGH_444 144
#define FF_PROFILE_H264_HIGH_444_PREDICTIVE 244
#define FF_PROFILE_H264_HIGH_444_INTRA (244|FF_PROFILE_H264_INTRA)
#define FF_PROFILE_H264_CAVLC_444 44
#define FF_PROFILE_VC1_SIMPLE 0
#define FF_PROFILE_VC1_MAIN 1
#define FF_PROFILE_VC1_COMPLEX 2
#define FF_PROFILE_VC1_ADVANCED 3
#define FF_PROFILE_MPEG4_SIMPLE 0
#define FF_PROFILE_MPEG4_SIMPLE_SCALABLE 1
#define FF_PROFILE_MPEG4_CORE 2
#define FF_PROFILE_MPEG4_MAIN 3
#define FF_PROFILE_MPEG4_N_BIT 4
#define FF_PROFILE_MPEG4_SCALABLE_TEXTURE 5
#define FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION 6
#define FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE 7
#define FF_PROFILE_MPEG4_HYBRID 8
#define FF_PROFILE_MPEG4_ADVANCED_REAL_TIME 9
#define FF_PROFILE_MPEG4_CORE_SCALABLE 10
#define FF_PROFILE_MPEG4_ADVANCED_CODING 11
#define FF_PROFILE_MPEG4_ADVANCED_CORE 12
#define FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE 13
#define FF_PROFILE_MPEG4_SIMPLE_STUDIO 14
#define FF_PROFILE_MPEG4_ADVANCED_SIMPLE 15
#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0 1
#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1 2
#define FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION 32768
#define FF_PROFILE_JPEG2000_DCINEMA_2K 3
#define FF_PROFILE_JPEG2000_DCINEMA_4K 4
#define FF_PROFILE_VP9_0 0
#define FF_PROFILE_VP9_1 1
#define FF_PROFILE_VP9_2 2
#define FF_PROFILE_VP9_3 3
#define FF_PROFILE_HEVC_MAIN 1
#define FF_PROFILE_HEVC_MAIN_10 2
#define FF_PROFILE_HEVC_MAIN_STILL_PICTURE 3
#define FF_PROFILE_HEVC_REXT 4
#define FF_PROFILE_HEVC_SCC 9
#define FF_PROFILE_VVC_MAIN_10 1
#define FF_PROFILE_VVC_MAIN_10_444 33
#define FF_PROFILE_AV1_MAIN 0
#define FF_PROFILE_AV1_HIGH 1
#define FF_PROFILE_AV1_PROFESSIONAL 2
#define FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT 0xc0
#define FF_PROFILE_MJPEG_HUFFMAN_EXTENDED_SEQUENTIAL_DCT 0xc1
#define FF_PROFILE_MJPEG_HUFFMAN_PROGRESSIVE_DCT 0xc2
#define FF_PROFILE_MJPEG_HUFFMAN_LOSSLESS 0xc3
#define FF_PROFILE_MJPEG_JPEG_LS 0xf7
#define FF_PROFILE_SBC_MSBC 1
#define FF_PROFILE_PRORES_PROXY 0
#define FF_PROFILE_PRORES_LT 1
#define FF_PROFILE_PRORES_STANDARD 2
#define FF_PROFILE_PRORES_HQ 3
#define FF_PROFILE_PRORES_4444 4
#define FF_PROFILE_PRORES_XQ 5
#define FF_PROFILE_ARIB_PROFILE_A 0
#define FF_PROFILE_ARIB_PROFILE_C 1
#define FF_PROFILE_KLVA_SYNC 0
#define FF_PROFILE_KLVA_ASYNC 1
#define FF_PROFILE_EVC_BASELINE 0
#define FF_PROFILE_EVC_MAIN 1
#endif
/**
* Encoding level descriptor.
* - encoding: Set by user, corresponds to a specific level defined by the
* codec, usually corresponding to the profile level, if not specified it
* is set to FF_LEVEL_UNKNOWN.
* - decoding: Set by libavcodec.
* See AV_LEVEL_* in defs.h.
*/
// 编码级别描述符
int level;
#if FF_API_FF_PROFILE_LEVEL
/** @deprecated The following define is deprecated; use AV_LEVEL_UNKOWN
* in defs.h instead. */
#define FF_LEVEL_UNKNOWN -99
#endif
/**
* Properties of the stream that gets decoded
* - encoding: unused
* - decoding: set by libavcodec
*/
// 被解码的流的属性
unsigned properties;
#define FF_CODEC_PROPERTY_LOSSLESS 0x00000001
#define FF_CODEC_PROPERTY_CLOSED_CAPTIONS 0x00000002
#define FF_CODEC_PROPERTY_FILM_GRAIN 0x00000004
/**
* Skip loop filtering for selected frames.
* - encoding: unused
* - decoding: Set by user.
*/
// 跳过所选帧的循环过滤
enum AVDiscard skip_loop_filter;
/**
* Skip IDCT/dequantization for selected frames.
* - encoding: unused
* - decoding: Set by user.
*/
// 跳过所选帧的IDCT/去量化
enum AVDiscard skip_idct;
/**
* Skip decoding for selected frames.
* - encoding: unused
* - decoding: Set by user.
*/
// 跳过所选帧的解码
enum AVDiscard skip_frame;
/**
* Skip processing alpha if supported by codec.
* Note that if the format uses pre-multiplied alpha (common with VP6,
* and recommended due to better video quality/compression)
* the image will look as if alpha-blended onto a black background.
* However for formats that do not use pre-multiplied alpha
* there might be serious artefacts (though e.g. libswscale currently
* assumes pre-multiplied alpha anyway).
*
* - decoding: set by user
* - encoding: unused
*/
// 如果编解码器支持,则跳过alpha处理。请注意,如果格式使用预乘alpha
// (在VP6中很常见,并且由于更好的视频质量/压缩而推荐使用),图像将看起来像是在黑色背景上混合了alpha
// 然而,对于不使用预乘alpha的格式,可能会有严重的伪影(尽管例如libswscale目前假设预乘alpha)。
int skip_alpha;
/**
* Number of macroblock rows at the top which are skipped.
* - encoding: unused
* - decoding: Set by user.
*/
// 在顶部被跳过的宏块行数
int skip_top;
/**
* Number of macroblock rows at the bottom which are skipped.
* - encoding: unused
* - decoding: Set by user.
*/
// 在底部被跳过的宏块行数
int skip_bottom;
/**
* low resolution decoding, 1-> 1/2 size, 2->1/4 size
* - encoding: unused
* - decoding: Set by user.
*/
// 低分辨率解码,为1,则1/2尺寸,为2,则1/4尺寸
int lowres;
/**
* AVCodecDescriptor
* - encoding: unused.
* - decoding: set by libavcodec.
*/
// AVCodec描述器
const struct AVCodecDescriptor *codec_descriptor;
/**
* Character encoding of the input subtitles file.
* - decoding: set by user
* - encoding: unused
*/
// 输入字幕文件的字符编码
char *sub_charenc;
/**
* Subtitles character encoding mode. Formats or codecs might be adjusting
* this setting (if they are doing the conversion themselves for instance).
* - decoding: set by libavcodec
* - encoding: unused
*/
// 字幕字符编码模式。格式或编解码器可能会调整此设置(例如,如果它们自己进行转换)
int sub_charenc_mode;
#define FF_SUB_CHARENC_MODE_DO_NOTHING -1 ///< do nothing (demuxer outputs a stream supposed to be already in UTF-8, or the codec is bitmap for instance)
#define FF_SUB_CHARENC_MODE_AUTOMATIC 0 ///< libavcodec will select the mode itself
#define FF_SUB_CHARENC_MODE_PRE_DECODER 1 ///< the AVPacket data needs to be recoded to UTF-8 before being fed to the decoder, requires iconv
#define FF_SUB_CHARENC_MODE_IGNORE 2 ///< neither convert the subtitles, nor check them for valid UTF-8
/**
* Header containing style information for text subtitles.
* For SUBTITLE_ASS subtitle type, it should contain the whole ASS
* [Script Info] and [V4+ Styles] section, plus the [Events] line and
* the Format line following. It shouldn't include any Dialogue line.
* - encoding: Set/allocated/freed by user (before avcodec_open2())
* - decoding: Set/allocated/freed by libavcodec (by avcodec_open2())
*/
// 包含文本字幕样式信息的标题。对于SUBTITLE_ASS subtitle类型,它应该包含整个ASS
// [Script Info]和[V4+ Styles]部分,加上[Events]行和下面的格式行。它不应该包含任何对话线
int subtitle_header_size;
uint8_t *subtitle_header;
/**
* dump format separator.
* can be ", " or "\n " or anything else
* - encoding: Set by user.
* - decoding: Set by user.
*/
// 转储格式分隔符
uint8_t *dump_separator;
/**
* ',' separated list of allowed decoders.
* If NULL then all are allowed
* - encoding: unused
* - decoding: set by user
*/
// 允许的解码器的分隔列表。如果为NULL,则所有都是允许的
char *codec_whitelist;
/**
* Additional data associated with the entire coded stream.
*
* - decoding: may be set by user before calling avcodec_open2().
* - encoding: may be set by libavcodec after avcodec_open2().
*/
// 与整个编码流相关联的附加数据
AVPacketSideData *coded_side_data;
int nb_coded_side_data;
/**
* Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of
* metadata exported in frame, packet, or coded stream side data by
* decoders and encoders.
*
* - decoding: set by user
* - encoding: set by user
*/
// AV_CODEC_EXPORT_DATA_*标志的位集,它影响解码器和编码器在帧、包或编码流端数据中导出的元数据的类型
int export_side_data;
/**
* The number of pixels per image to maximally accept.
*
* - decoding: set by user
* - encoding: set by user
*/
// 最大接受的每个图像的像素数
int64_t max_pixels;
/**
* Video decoding only. Certain video codecs support cropping, meaning that
* only a sub-rectangle of the decoded frame is intended for display. This
* option controls how cropping is handled by libavcodec.
*
* When set to 1 (the default), libavcodec will apply cropping internally.
* I.e. it will modify the output frame width/height fields and offset the
* data pointers (only by as much as possible while preserving alignment, or
* by the full amount if the AV_CODEC_FLAG_UNALIGNED flag is set) so that
* the frames output by the decoder refer only to the cropped area. The
* crop_* fields of the output frames will be zero.
*
* When set to 0, the width/height fields of the output frames will be set
* to the coded dimensions and the crop_* fields will describe the cropping
* rectangle. Applying the cropping is left to the caller.
*
* @warning When hardware acceleration with opaque output frames is used,
* libavcodec is unable to apply cropping from the top/left border.
*
* @note when this option is set to zero, the width/height fields of the
* AVCodecContext and output AVFrames have different meanings. The codec
* context fields store display dimensions (with the coded dimensions in
* coded_width/height), while the frame fields store the coded dimensions
* (with the display dimensions being determined by the crop_* fields).
*/
/*
【视频解码】
1.某些视频编解码器支持裁剪,这意味着只有被解码帧的子矩形用于显示。这个选项控制libavcodec如何处理裁剪
2.当设置为1(默认值)时,libavcodec将在内部应用裁剪。也就是说,它将修改输出帧宽度/高度字段,
并偏移数据指针(仅在保持对齐的情况下尽可能多地偏移,或者如果设置了AV_CODEC_FLAG_UNALIGNED标志,则偏移全部量),
以便解码器输出的帧只指向裁剪过的区域。输出帧的crop_*字段将为零
3.当设置为0时,输出帧的宽度/高度字段将被设置为编码尺寸,并且crop_*字段将描述裁剪矩形。裁剪的应用留给调用者
4.@warning 当使用带有不透明输出帧的硬件加速时,libavcodec无法从上/左边框应用裁剪
5.@note 当此选项设置为零时,AVCodecContext和输出AVFrames的宽度/高度字段具有不同的含义。
编解码器上下文字段存储显示尺寸(编码尺寸在coded_width/height中),而框架字段存储编码尺寸(显示尺寸由crop_*字段决定)
*/
int apply_cropping;
/**
* The percentage of damaged samples to discard a frame.
*
* - decoding: set by user
* - encoding: unused
*/
// 丢弃一帧的损坏样本的百分比
int discard_damaged_percentage;
/**
* The number of samples per frame to maximally accept.
*
* - decoding: set by user
* - encoding: set by user
*/
// 【音频】最大限度地接受每帧的采样数
int64_t max_samples;
/**
* This callback is called at the beginning of each packet to get a data
* buffer for it.
*
* The following field will be set in the packet before this callback is
* called:
* - size
* This callback must use the above value to calculate the required buffer size,
* which must padded by at least AV_INPUT_BUFFER_PADDING_SIZE bytes.
*
* In some specific cases, the encoder may not use the entire buffer allocated by this
* callback. This will be reflected in the size value in the packet once returned by
* avcodec_receive_packet().
*
* This callback must fill the following fields in the packet:
* - data: alignment requirements for AVPacket apply, if any. Some architectures and
* encoders may benefit from having aligned data.
* - buf: must contain a pointer to an AVBufferRef structure. The packet's
* data pointer must be contained in it. See: av_buffer_create(), av_buffer_alloc(),
* and av_buffer_ref().
*
* If AV_CODEC_CAP_DR1 is not set then get_encode_buffer() must call
* avcodec_default_get_encode_buffer() instead of providing a buffer allocated by
* some other means.
*
* The flags field may contain a combination of AV_GET_ENCODE_BUFFER_FLAG_ flags.
* They may be used for example to hint what use the buffer may get after being
* created.
* Implementations of this callback may ignore flags they don't understand.
* If AV_GET_ENCODE_BUFFER_FLAG_REF is set in flags then the packet may be reused
* (read and/or written to if it is writable) later by libavcodec.
*
* This callback must be thread-safe, as when frame threading is used, it may
* be called from multiple threads simultaneously.
*
* @see avcodec_default_get_encode_buffer()
*
* - encoding: Set by libavcodec, user can override.
* - decoding: unused
*/
/*
1.这个回调函数在每个包的开头被调用,为它获得一个数据缓冲区
2.在调用这个回调之前,将在数据包中设置以下字段:
(1)尺寸
3.这个回调必须使用上面的值来计算所需的缓冲区大小,它必须至少填充AV_INPUT_BUFFER_PADDING_SIZE字节
4.在某些特定情况下,编码器可能不会使用此回调分配的整个缓冲区。这将反映在avcodec_receive_packet()返回的数据包的大小值中
5.这个回调必须在数据包中填写以下字段:
(1)data:AVPacket对齐要求适用,如果有的话。一些架构和编码器可能受益于对齐的数据。
(2)buf:必须包含指向AVBufferRef结构体的指针。数据包的数据指针必须包含在其中。
参见:av_buffer_create(), av_buffer_alloc()和av_buffer_ref()
6.如果AV_CODEC_CAP_DR1未设置,则get_encode_buffer()必须调用avcodec_default_get_encode_buffer(),
而不是提供通过其他方式分配的缓冲区
7.flags字段可以包含AV_GET_ENCODE_BUFFER_FLAG_标志的组合。例如,它们可以用来提示缓冲区在创建后可能得到的使用。
这个回调的实现可能会忽略它们不理解的标志。如果AV_GET_ENCODE_BUFFER_FLAG_REF在flags中设置,
那么包可能会被libavcodec重用(如果它是可写的,可以读取和/或写入)
8.这个回调必须是线程安全的,因为当使用帧线程时,它可以同时从多个线程调用
*/
int (*get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags);
/**
* Frame counter, set by libavcodec.
*
* - decoding: total number of frames returned from the decoder so far.
* - encoding: total number of frames passed to the encoder so far.
*
* @note the counter is not incremented if encoding/decoding resulted in
* an error.
*/
// 帧数
int64_t frame_num;
/**
* Decoding only. May be set by the caller before avcodec_open2() to an
* av_malloc()'ed array (or via AVOptions). Owned and freed by the decoder
* afterwards.
*
* Side data attached to decoded frames may come from several sources:
* 1. coded_side_data, which the decoder will for certain types translate
* from packet-type to frame-type and attach to frames;
* 2. side data attached to an AVPacket sent for decoding (same
* considerations as above);
* 3. extracted from the coded bytestream.
* The first two cases are supplied by the caller and typically come from a
* container.
*
* This array configures decoder behaviour in cases when side data of the
* same type is present both in the coded bytestream and in the
* user-supplied side data (items 1. and 2. above). In all cases, at most
* one instance of each side data type will be attached to output frames. By
* default it will be the bytestream side data. Adding an
* AVPacketSideDataType value to this array will flip the preference for
* this type, thus making the decoder prefer user-supplied side data over
* bytestream. In case side data of the same type is present both in
* coded_data and attacked to a packet, the packet instance always has
* priority.
*
* The array may also contain a single -1, in which case the preference is
* switched for all side data types.
*/
/*
【解码】
1.可以由调用者在avcodec_open2()之前设置为av_malloc()'ed数组(或通过AVOptions)。之后由解码器拥有并释放
2.附加到解码帧的侧数据可能来自以下几个来源:
(1)Coded_side_data,对于某些类型,解码器将从包类型转换为帧类型并附加到帧;
(2)附加在AVPacket上用于解码的侧数据(如上所述);
(3)从编码字节流中提取
前两种情况由调用者提供,通常来自容器。
3.当编码字节流和用户提供的侧数据中同时存在相同类型的侧数据时,此数组配置解码器行为(第1项,第2项及以上)。
在所有情况下,每个端数据类型的最多一个实例将附加到输出帧。默认情况下,它将是字节流端数据。
将AVPacketSideDataType值添加到此数组将翻转此类型的首选项,从而使解码器更喜欢用户提供的侧数据而不是字节流。
如果coded_data中同时存在相同类型的侧数据并攻击到数据包,则数据包实例始终具有优先级
4.该数组还可能包含一个-1,在这种情况下,所有侧数据类型的首选项都会切换
*/
int *side_data_prefer_packet;
/**
* Number of entries in side_data_prefer_packet.
*/
// side_data_prefer_packet表项数
unsigned nb_side_data_prefer_packet;
/**
* Array containing static side data, such as HDR10 CLL / MDCV structures.
* Side data entries should be allocated by usage of helpers defined in
* libavutil/frame.h.
*
* - encoding: may be set by user before calling avcodec_open2() for
* encoder configuration. Afterwards owned and freed by the
* encoder.
* - decoding: unused
*/
// 包含静态侧数据的数组,如HDR10 CLL / MDCV结构。侧数据项应该通过使用libavutil/frame.h中定义的helper来分配
AVFrameSideData **decoded_side_data;
int nb_decoded_side_data;
} AVCodecContext;
在AVCodecContext结构体中,变量数非常多,只考虑视频相关的参数,其中比较核心的变量和函数有:
(1)enum AVMediaType codec_type:编解码器的类型,例如视频、音频等等
(2)const struct AVCodec *codec:编解码器
(3)enum AVCodecID codec_id:编解码器ID
(4)unsigned int codec_tag:描述编码过程中的bug
(5)void *priv_data:常用于结构体的强制转换
(6)int64_t bit_rate:码率
(7)uint8_t *extradata:额外的数据
(8)AVRational time_base:时间基
(9)AVRational framerate:帧率
(10)int delay:延时
(11)int width, height:视频帧的长和宽
(12)enum AVPixelFormat pix_fmt:像素格式
(13)int refs:参考帧的数量
(14)int has_b_frames:两个非b帧之间的b帧数量
(15)enum AVPixelFormat (*get_format)(struct AVCodecContext *s, const enum AVPixelFormat * fmt):获取帧的格式
(16)int mb_lmin,int mb_lmax:最小和最大的宏块级的拉格朗日乘子
(17)int keyint_min:最小的gop大小
(18)int gop_size:gop大小
(19)int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags):获取数据缓冲区
(20)int qmin,int qmax:最小和最大的量化参数
(21)int64_t rc_max_rate,int64_t rc_min_rate:最大最小码率
(22)int (*execute)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size):执行函数
(23)int (*execute2)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count):执行函数2
(24)char *codec_whitelist:编解码器的白名单,表示可用的编解码器有哪些
(25)int (*get_encode_buffer)(struct AVCodecContext *s, AVPacket *pkt, int flags):获取编码缓冲区
(26)int64_t frame_num:帧的数量
(27)struct AVCodecInternal *internal:编解码器内部信息
这里一些关键结构体如AVMediaType、AVCodec、AVCodecID、AVRational和AVPixelFormat在【FFmpeg】AVCodec结构体一文中记录。从这里来看,AVCodec这个结构体是被AVCodecContext包含在其中的
1.1 struct AVCodecInternal *internal
AVCodecInternal描述的是编解码器内部的信息,定义在libavcodec\internal.h中
typedef struct AVCodecInternal {
/**
* When using frame-threaded decoding, this field is set for the first
* worker thread (e.g. to decode extradata just once).
*/
// 当使用帧线程解码时,这个字段是为第一个工作线程设置的(例如,只解码一次额外数据)
int is_copy;
/**
* Audio encoders can set this flag during init to indicate that they
* want the small last frame to be padded to a multiple of pad_samples.
*/
// 【音频】音频编码器可以在初始化期间设置此标志,以表明他们希望将最后一帧填充为pad_samples的倍数
int pad_samples;
// 帧池
struct FramePool *pool;
// 线程的上下文
void *thread_ctx;
/**
* This packet is used to hold the packet given to decoders
* implementing the .decode API; it is unused by the generic
* code for decoders implementing the .receive_frame API and
* may be freely used (but not freed) by them with the caveat
* that the packet will be unreferenced generically in
* avcodec_flush_buffers().
*/
// 此数据包用于保存给实现.decode API的解码器的数据包;
// 它不被实现.receive_frame API的解码器的通用代码使用,并且可以被它们自由使用(但不是释放),
// 但需要注意的是,数据包将在avcodec_flush_buffers()中不被通用引用
AVPacket *in_pkt;
// 码流的上下文信息
struct AVBSFContext *bsf;
/**
* Properties (timestamps+side data) extracted from the last packet passed
* for decoding.
*/
// 从经过解码的最后一个数据包中提取的属性(时间戳+侧数据)
AVPacket *last_pkt_props;
/**
* temporary buffer used for encoders to store their bitstream
*/
// 编码器用于存储其位流的临时缓冲区
uint8_t *byte_buffer;
// 缓冲区大小
unsigned int byte_buffer_size;
// 线程级帧编码器
void *frame_thread_encoder;
/**
* The input frame is stored here for encoders implementing the simple
* encode API.
*
* Not allocated in other cases.
*/
AVFrame *in_frame;
/**
* When the AV_CODEC_FLAG_RECON_FRAME flag is used. the encoder should store
* here the reconstructed frame corresponding to the last returned packet.
*
* Not allocated in other cases.
*/
// 当使用AV_CODEC_FLAG_RECON_FRAME标志时。编码器应该在这里存储与最后返回的数据包相对应的重构帧
// 在其他情况下不分配
AVFrame *recon_frame;
/**
* If this is set, then FFCodec->close (if existing) needs to be called
* for the parent AVCodecContext.
*/
// 如果设置了这个,那么FFCodec->close(如果存在)需要为父AVCodecContext调用
int needs_close;
/**
* Number of audio samples to skip at the start of the next decoded frame
*/
// 在下一个解码帧开始时要跳过的音频样本数
int skip_samples;
/**
* hwaccel-specific private data
*/
// 硬件加速特定的私有数据
void *hwaccel_priv_data;
/**
* checks API usage: after codec draining, flush is required to resume operation
*/
// 检查API使用情况:编解码器排水后,需要冲洗才能恢复操作
int draining;
/**
* Temporary buffers for newly received or not yet output packets/frames.
*/
// 临时缓冲区用于新接收或尚未输出的数据包/帧
AVPacket *buffer_pkt;
// 缓冲的帧
AVFrame *buffer_frame;
// 出队完成情况
int draining_done;
#if FF_API_DROPCHANGED
/* used when avctx flag AV_CODEC_FLAG_DROPCHANGED is set */
int changed_frames_dropped;
int initial_format;
int initial_width, initial_height;
int initial_sample_rate;
AVChannelLayout initial_ch_layout;
#endif
#if CONFIG_LCMS2
FFIccContext icc; /* used to read and write embedded ICC profiles */
#endif
/**
* Set when the user has been warned about a failed allocation from
* a fixed frame pool.
*/
int warned_on_failed_allocation_from_fixed_pool;
} AVCodecInternal;
上面的变量之中AVFrame和AVPacket均为关键结构体,在其它文中记录,下面先看看FramePool结构体
1.1.1 struct FramePool *pool
FramePool结构体定义于libavcodec\get_buffer.c中,定义了编解码帧池中帧的信息,包括AVBufferPool,格式format,帧宽width,帧高height等
typedef struct FramePool {
/**
* Pools for each data plane. For audio all the planes have the same size,
* so only pools[0] is used.
*/
// 每个数据平面的池。对于音频,所有的平面都有相同的大小,所以只使用pool [0]
AVBufferPool *pools[4];
/*
* Pool parameters
*/
int format;
int width, height;
int stride_align[AV_NUM_DATA_POINTERS];
int linesize[4];
int planes;
int channels;
int samples;
} FramePool;
由于定义了线程池,需要在AVBufferPool中定义线程锁;此外,还定义了线程池入口和buffer分配的函数,AVBufferRef中的ref表示是一个指向AVBuffer的指针
struct AVBufferPool {
AVMutex mutex;
BufferPoolEntry *pool;
/*
* This is used to track when the pool is to be freed.
* The pointer to the pool itself held by the caller is considered to
* be one reference. Each buffer requested by the caller increases refcount
* by one, returning the buffer to the pool decreases it by one.
* refcount reaches zero when the buffer has been uninited AND all the
* buffers have been released, then it's safe to free the pool and all
* the buffers in it.
*/
atomic_uint refcount;
size_t size;
void *opaque;
AVBufferRef* (*alloc)(size_t size);
AVBufferRef* (*alloc2)(void *opaque, size_t size);
void (*pool_free)(void *opaque);
};
CSDN : https://blog.csdn.net/weixin_42877471
Github : https://github.com/DoFulangChen