FFMPeg代码分析:AVFrame结构体及其相关的函数

转自:http://blog.csdn.net/shaqoneal/article/details/16959671

AVFrame结构体保存的是解码后和原始的音视频信息。AVFrame通过函数av_frame_alloc()初始化,该函数仅仅分配AVFrame实例本身,而没有分配其内部的缓存。AVFrame实例由av_frame_free()释放;AVFrame实例通常分配一次,重复使用,如分配一个AVFrame实例来保留解码器中输出的视频帧(此时应在恰当的时候使用av_frame_unref()清理参考帧并将AVFrame归零)。该类所描述的数据通常由AVBuffer的API来保存一个引用计数,并保存于AVFrame.buf /AVFrame.extended_buf,在至少存在一个参考的时候(如AVFrame.buf[0] != NULL),则该对象被标记为“被引用”。在此情况下,AVFrame所包含的每一组数据必须包含于AVFrame的缓存中。

AVFrame的定义如下:

  1. typedef struct AVFrame {  
  2. #define AV_NUM_DATA_POINTERS 8  
  3.     /** 
  4.      * pointer to the picture/channel planes. 
  5.      * This might be different from the first allocated byte 
  6.      * 
  7.      * Some decoders access areas outside 0,0 - width,height, please 
  8.      * see avcodec_align_dimensions2(). Some filters and swscale can read 
  9.      * up to 16 bytes beyond the planes, if these filters are to be used, 
  10.      * then 16 extra bytes must be allocated. 
  11.      */  
  12.     uint8_t *data[AV_NUM_DATA_POINTERS];//指向实际音视频数据的指针  
  13.   
  14.     /** 
  15.      * For video, size in bytes of each picture line. 
  16.      * For audio, size in bytes of each plane. 
  17.      * 
  18.      * For audio, only linesize[0] may be set. For planar audio, each channel 
  19.      * plane must be the same size. 
  20.      * 
  21.      * For video the linesizes should be multiplies of the CPUs alignment 
  22.      * preference, this is 16 or 32 for modern desktop CPUs. 
  23.      * Some code requires such alignment other code can be slower without 
  24.      * correct alignment, for yet other it makes no difference. 
  25.      * 
  26.      * @note The linesize may be larger than the size of usable data -- there 
  27.      * may be extra padding present for performance reasons. 
  28.      */  
  29.     int linesize[AV_NUM_DATA_POINTERS];  
  30.   
  31.     /** 
  32.      * pointers to the data planes/channels. 
  33.      * 
  34.      * For video, this should simply point to data[]. 
  35.      * 
  36.      * For planar audio, each channel has a separate data pointer, and 
  37.      * linesize[0] contains the size of each channel buffer. 
  38.      * For packed audio, there is just one data pointer, and linesize[0] 
  39.      * contains the total size of the buffer for all channels. 
  40.      * 
  41.      * Note: Both data and extended_data should always be set in a valid frame, 
  42.      * but for planar audio with more channels that can fit in data, 
  43.      * extended_data must be used in order to access all channels. 
  44.      */  
  45.     uint8_t **extended_data;  
  46.   
  47.     /** 
  48.      * width and height of the video frame 
  49.      */  
  50.     int width, height;//视频的分辨率  
  51.   
  52.     /** 
  53.      * number of audio samples (per channel) described by this frame 
  54.      */  
  55.     int nb_samples;//音频采样频率  
  56.   
  57.     /** 
  58.      * format of the frame, -1 if unknown or unset 
  59.      * Values correspond to enum AVPixelFormat for video frames, 
  60.      * enum AVSampleFormat for audio) 
  61.      */  
  62.     int format;//解码后的视频帧的格式,为<span style="font-family: Arial, Helvetica, sans-serif;"&gt;AVPixelFormat 枚举类型(<span style="font-family: Arial; line-height: 26px;"><span style="font-size:10px;">YUV420,YUV422,RGB24等</span></span>)</span>  
  63.   
  64.   
  65.     /** 
  66.      * 1 -> keyframe, 0-> not 
  67.      */  
  68.     int key_frame;//是否关键帧,1是0否  
  69.   
  70.     /** 
  71.      * Picture type of the frame. 
  72.      */  
  73.     enum AVPictureType pict_type;//帧类型,IPB  
  74.   
  75. #if FF_API_AVFRAME_LAVC  
  76.     attribute_deprecated  
  77.     uint8_t *base[AV_NUM_DATA_POINTERS];  
  78. #endif  
  79.   
  80.     /** 
  81.      * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified. 
  82.      */  
  83.     AVRational sample_aspect_ratio;//视频的宽高比  
  84.   
  85.     /** 
  86.      * Presentation timestamp in time_base units (time when frame should be shown to user). 
  87.      */  
  88.     int64_t pts;//当前帧的显示时间戳  
  89.   
  90.     /** 
  91.      * PTS copied from the AVPacket that was decoded to produce this frame. 
  92.      */  
  93.     int64_t pkt_pts;  
  94.   
  95.     /** 
  96.      * DTS copied from the AVPacket that triggered returning this frame. (if frame threading isnt used) 
  97.      * This is also the Presentation time of this AVFrame calculated from 
  98.      * only AVPacket.dts values without pts values. 
  99.      */  
  100.     int64_t pkt_dts;  
  101.   
  102.     /** 
  103.      * picture number in bitstream order 
  104.      */  
  105.     int coded_picture_number;//当前帧以解码顺序的编号  
  106.     /** 
  107.      * picture number in display order 
  108.      */  
  109.     int display_picture_number;//当前帧以输出顺序的编号  
  110.   
  111.     /** 
  112.      * quality (between 1 (good) and FF_LAMBDA_MAX (bad)) 
  113.      */  
  114.     int quality;  
  115.   
  116. #if FF_API_AVFRAME_LAVC  
  117.     attribute_deprecated  
  118.     int reference;  
  119.   
  120.     /** 
  121.      * QP table 
  122.      */  
  123.     attribute_deprecated  
  124.     int8_t *qscale_table;//量化参数表  
  125.     /** 
  126.      * QP store stride 
  127.      */  
  128.     attribute_deprecated  
  129.     int qstride;  
  130.   
  131.     attribute_deprecated  
  132.     int qscale_type;  
  133.   
  134.     /** 
  135.      * mbskip_table[mb]>=1 if MB didn't change 
  136.      * stride= mb_width = (width+15)>>4 
  137.      */  
  138.     attribute_deprecated  
  139.     uint8_t *mbskip_table;  
  140.   
  141.     /** 
  142.      * motion vector table 
  143.      * @code 
  144.      * example: 
  145.      * int mv_sample_log2= 4 - motion_subsample_log2; 
  146.      * int mb_width= (width+15)>>4; 
  147.      * int mv_stride= (mb_width << mv_sample_log2) + 1; 
  148.      * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y]; 
  149.      * @endcode 
  150.      */  
  151.     attribute_deprecated  
  152.     int16_t (*motion_val[2])[2];//运动矢量表  
  153.   
  154.     /** 
  155.      * macroblock type table 
  156.      * mb_type_base + mb_width + 2 
  157.      */  
  158.     attribute_deprecated  
  159.     uint32_t *mb_type;//宏块类型  
  160.   
  161.     /** 
  162.      * DCT coefficients 
  163.      */  
  164.     attribute_deprecated  
  165.     short *dct_coeff;//变换系数  
  166.   
  167.     /** 
  168.      * motion reference frame index 
  169.      * the order in which these are stored can depend on the codec. 
  170.      */  
  171.     attribute_deprecated  
  172.     int8_t *ref_index[2];//参考帧索引  
  173. #endif  
  174.   
  175.     /** 
  176.      * for some private data of the user 
  177.      */  
  178.     void *opaque;  
  179.   
  180.     /** 
  181.      * error 
  182.      */  
  183.     uint64_t error[AV_NUM_DATA_POINTERS];  
  184.   
  185. #if FF_API_AVFRAME_LAVC  
  186.     attribute_deprecated  
  187.     int type;  
  188. #endif  
  189.   
  190.     /** 
  191.      * When decoding, this signals how much the picture must be delayed. 
  192.      * extra_delay = repeat_pict / (2*fps) 
  193.      */  
  194.     int repeat_pict;//表示有多少帧被延迟显示,延迟即重复。  
  195.   
  196.     /** 
  197.      * The content of the picture is interlaced. 
  198.      */  
  199.     int interlaced_frame;//隔行视频标识  
  200.   
  201.     /** 
  202.      * If the content is interlaced, is top field displayed first. 
  203.      */  
  204.     int top_field_first;  
  205.   
  206.     /** 
  207.      * Tell user application that palette has changed from previous frame. 
  208.      */  
  209.     int palette_has_changed;  
  210.   
  211. #if FF_API_AVFRAME_LAVC  
  212.     attribute_deprecated  
  213.     int buffer_hints;  
  214.   
  215.     /** 
  216.      * Pan scan. 
  217.      */  
  218.     attribute_deprecated  
  219.     struct AVPanScan *pan_scan;  
  220. #endif  
  221.   
  222.     /** 
  223.      * reordered opaque 64bit (generally an integer or a double precision float 
  224.      * PTS but can be anything). 
  225.      * The user sets AVCodecContext.reordered_opaque to represent the input at 
  226.      * that time, 
  227.      * the decoder reorders values as needed and sets AVFrame.reordered_opaque 
  228.      * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque 
  229.      * @deprecated in favor of pkt_pts 
  230.      */  
  231.     int64_t reordered_opaque;  
  232.   
  233. #if FF_API_AVFRAME_LAVC  
  234.     /** 
  235.      * @deprecated this field is unused 
  236.      */  
  237.     attribute_deprecated void *hwaccel_picture_private;  
  238.   
  239.     attribute_deprecated  
  240.     struct AVCodecContext *owner;  
  241.     attribute_deprecated  
  242.     void *thread_opaque;  
  243.   
  244.     /** 
  245.      * log2 of the size of the block which a single vector in motion_val represents: 
  246.      * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2) 
  247.      */  
  248.     attribute_deprecated  
  249.     uint8_t motion_subsample_log2;  
  250. #endif  
  251.   
  252.     /** 
  253.      * Sample rate of the audio data. 
  254.      */  
  255.     int sample_rate;  
  256.   
  257.     /** 
  258.      * Channel layout of the audio data. 
  259.      */  
  260.     uint64_t channel_layout;  
  261.   
  262.     /** 
  263.      * AVBuffer references backing the data for this frame. If all elements of 
  264.      * this array are NULL, then this frame is not reference counted. 
  265.      * 
  266.      * There may be at most one AVBuffer per data plane, so for video this array 
  267.      * always contains all the references. For planar audio with more than 
  268.      * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in 
  269.      * this array. Then the extra AVBufferRef pointers are stored in the 
  270.      * extended_buf array. 
  271.      */  
  272.     AVBufferRef *buf[AV_NUM_DATA_POINTERS];  
  273.   
  274.     /** 
  275.      * For planar audio which requires more than AV_NUM_DATA_POINTERS 
  276.      * AVBufferRef pointers, this array will hold all the references which 
  277.      * cannot fit into AVFrame.buf. 
  278.      * 
  279.      * Note that this is different from AVFrame.extended_data, which always 
  280.      * contains all the pointers. This array only contains the extra pointers, 
  281.      * which cannot fit into AVFrame.buf. 
  282.      * 
  283.      * This array is always allocated using av_malloc() by whoever constructs 
  284.      * the frame. It is freed in av_frame_unref(). 
  285.      */  
  286.     AVBufferRef **extended_buf;  
  287.     /** 
  288.      * Number of elements in extended_buf. 
  289.      */  
  290.     int        nb_extended_buf;  
  291.   
  292.     AVFrameSideData **side_data;  
  293.     int            nb_side_data;  
  294.   
  295.     /** 
  296.      * frame timestamp estimated using various heuristics, in stream time base 
  297.      * Code outside libavcodec should access this field using: 
  298.      * av_frame_get_best_effort_timestamp(frame) 
  299.      * - encoding: unused 
  300.      * - decoding: set by libavcodec, read by user. 
  301.      */  
  302.     int64_t best_effort_timestamp;  
  303.   
  304.     /** 
  305.      * reordered pos from the last AVPacket that has been input into the decoder 
  306.      * Code outside libavcodec should access this field using: 
  307.      * av_frame_get_pkt_pos(frame) 
  308.      * - encoding: unused 
  309.      * - decoding: Read by user. 
  310.      */  
  311.     int64_t pkt_pos;  
  312.   
  313.     /** 
  314.      * duration of the corresponding packet, expressed in 
  315.      * AVStream->time_base units, 0 if unknown. 
  316.      * Code outside libavcodec should access this field using: 
  317.      * av_frame_get_pkt_duration(frame) 
  318.      * - encoding: unused 
  319.      * - decoding: Read by user. 
  320.      */  
  321.     int64_t pkt_duration;  
  322.   
  323.     /** 
  324.      * metadata. 
  325.      * Code outside libavcodec should access this field using: 
  326.      * av_frame_get_metadata(frame) 
  327.      * - encoding: Set by user. 
  328.      * - decoding: Set by libavcodec. 
  329.      */  
  330.     AVDictionary *metadata;  
  331.   
  332.     /** 
  333.      * decode error flags of the frame, set to a combination of 
  334.      * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there 
  335.      * were errors during the decoding. 
  336.      * Code outside libavcodec should access this field using: 
  337.      * av_frame_get_decode_error_flags(frame) 
  338.      * - encoding: unused 
  339.      * - decoding: set by libavcodec, read by user. 
  340.      */  
  341.     int decode_error_flags;  
  342. #define FF_DECODE_ERROR_INVALID_BITSTREAM   1  
  343. #define FF_DECODE_ERROR_MISSING_REFERENCE   2  
  344.   
  345.     /** 
  346.      * number of audio channels, only used for audio. 
  347.      * Code outside libavcodec should access this field using: 
  348.      * av_frame_get_channels(frame) 
  349.      * - encoding: unused 
  350.      * - decoding: Read by user. 
  351.      */  
  352.     int channels;  
  353.   
  354.     /** 
  355.      * size of the corresponding packet containing the compressed 
  356.      * frame. It must be accessed using av_frame_get_pkt_size() and 
  357.      * av_frame_set_pkt_size(). 
  358.      * It is set to a negative value if unknown. 
  359.      * - encoding: unused 
  360.      * - decoding: set by libavcodec, read by user. 
  361.      */  
  362.     int pkt_size;  
  363.   
  364.     /** 
  365.      * YUV colorspace type. 
  366.      * It must be accessed using av_frame_get_colorspace() and 
  367.      * av_frame_set_colorspace(). 
  368.      * - encoding: Set by user 
  369.      * - decoding: Set by libavcodec 
  370.      */  
  371.     enum AVColorSpace colorspace;  
  372.   
  373.     /** 
  374.      * MPEG vs JPEG YUV range. 
  375.      * It must be accessed using av_frame_get_color_range() and 
  376.      * av_frame_set_color_range(). 
  377.      * - encoding: Set by user 
  378.      * - decoding: Set by libavcodec 
  379.      */  
  380.     enum AVColorRange color_range;  
  381.   
  382.   
  383.     /** 
  384.      * Not to be accessed directly from outside libavutil 
  385.      */  
  386.     AVBufferRef *qp_table_buf;  
  387. } AVFrame;  

通过对其内部的成员变量的研究可以看出,除了基本的储存像素的缓存之外,其成员大多还是与编解码相关的一些参数,如分辨率、帧类型、量化参数表、参考帧列表等。所以从大体上看,AVFrame就是一个容器类,同AVPacket不同的是所容纳的是原始的像素数据,相同点是都有很多编解码相关的参数作为补充数据。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值