ffmpeg 函数简单分析 : avcodec_decode_video2()

来源:互联网 发布:复杂网络可视化软件 编辑:程序博客网 时间:2024/06/05 02:01

ffmpeg中的avcodec_decode_video2()的作用是解码一帧视频数据。输入一个压缩编码的结构体AVPacket,输出一个解码后的结构体AVFrame。该函数的声明位于libavcodec\avcodec.h,如下所示。

[cpp] view plain copy
  1. /** 
  2.  * Decode the video frame of size avpkt->size from avpkt->data into picture. 
  3.  * Some decoders may support multiple frames in a single AVPacket, such 
  4.  * decoders would then just decode the first frame. 
  5.  * 
  6.  * @warning The input buffer must be FF_INPUT_BUFFER_PADDING_SIZE larger than 
  7.  * the actual read bytes because some optimized bitstream readers read 32 or 64 
  8.  * bits at once and could read over the end. 
  9.  * 
  10.  * @warning The end of the input buffer buf should be set to 0 to ensure that 
  11.  * no overreading happens for damaged MPEG streams. 
  12.  * 
  13.  * @note Codecs which have the CODEC_CAP_DELAY capability set have a delay 
  14.  * between input and output, these need to be fed with avpkt->data=NULL, 
  15.  * avpkt->size=0 at the end to return the remaining frames. 
  16.  * 
  17.  * @param avctx the codec context 
  18.  * @param[out] picture The AVFrame in which the decoded video frame will be stored. 
  19.  *             Use av_frame_alloc() to get an AVFrame. The codec will 
  20.  *             allocate memory for the actual bitmap by calling the 
  21.  *             AVCodecContext.get_buffer2() callback. 
  22.  *             When AVCodecContext.refcounted_frames is set to 1, the frame is 
  23.  *             reference counted and the returned reference belongs to the 
  24.  *             caller. The caller must release the frame using av_frame_unref() 
  25.  *             when the frame is no longer needed. The caller may safely write 
  26.  *             to the frame if av_frame_is_writable() returns 1. 
  27.  *             When AVCodecContext.refcounted_frames is set to 0, the returned 
  28.  *             reference belongs to the decoder and is valid only until the 
  29.  *             next call to this function or until closing or flushing the 
  30.  *             decoder. The caller may not write to it. 
  31.  * 
  32.  * @param[in] avpkt The input AVPacket containing the input buffer. 
  33.  *            You can create such packet with av_init_packet() and by then setting 
  34.  *            data and size, some decoders might in addition need other fields like 
  35.  *            flags&AV_PKT_FLAG_KEY. All decoders are designed to use the least 
  36.  *            fields possible. 
  37.  * @param[in,out] got_picture_ptr Zero if no frame could be decompressed, otherwise, it is nonzero. 
  38.  * @return On error a negative value is returned, otherwise the number of bytes 
  39.  * used or zero if no frame could be decompressed. 
  40.  */  
  41. int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,  
  42.                          int *got_picture_ptr,  
  43.                          const AVPacket *avpkt);  
  44. avcodec_decode_video2()主要做了以下几个方面的工作:

    (1)对输入的字段进行了一系列的检查工作:例如宽高是否正确,输入是否为视频等等。

    (2)通过ret = avctx->codec->decode(avctx, picture, got_picture_ptr,&tmp)这句代码,调用了相应AVCodec的decode()函数,完成了解码操作。

    (3)对得到的AVFrame的一些字段进行了赋值,例如宽高、像素格式等等。

    其中第二部是关键的一步,它调用了AVCodec的decode()方法完成了解码。AVCodec的decode()方法是一个函数指针,指向了具体解码器的解码函数。

原创粉丝点击