FFmpeg3最新的解碼接口avcodec_send_packet和avcodec_receive_frame分析

ffmpeg3版本的解碼接口作了很多調整,以前的視頻解碼接口avcodec_decode_video2和avcodec_decode_audio4音頻解碼被設置爲deprecated,對這兩個接口作了合併,使用統一的接口。而且將音視頻解碼步驟分爲了兩步,第一步avcodec_send_packet,第二步avcodec_receive_frame,經過接口名字咱們就能夠知道第一步是發送編碼數據包,第二步是接收解碼後數據。新版本是否只是作了接口的變化,還有有哪些咱們須要注意的事項,咱們來分析一下。html

      首先咱們先看一下這兩個接口。git

avcodec_send_packet

  • 接口源碼
    app

/**
 * Supply raw packet data as input to a decoder.
 *
 * Internally, this call will copy relevant AVCodecContext fields, which can
 * influence decoding per-packet, and apply them when the packet is actually
 * decoded. (For example AVCodecContext.skip_frame, which might direct the
 * decoder to drop the frame contained by the packet sent with this function.)
 *
 * @warning The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE
 *          larger than the actual read bytes because some optimized bitstream
 *          readers read 32 or 64 bits at once and could read over the end.
 *
 * @warning Do not mix this API with the legacy API (like avcodec_decode_video2())
 *          on the same AVCodecContext. It will return unexpected results now
 *          or in future libavcodec versions.
 *
 * @note The AVCodecContext MUST have been opened with @ref avcodec_open2()
 *       before packets may be fed to the decoder.
 *
 * @param avctx codec context
 * @param[in] avpkt The input AVPacket. Usually, this will be a single video
 *                  frame, or several complete audio frames.
 *                  Ownership of the packet remains with the caller, and the
 *                  decoder will not write to the packet. The decoder may create
 *                  a reference to the packet data (or copy it if the packet is
 *                  not reference-counted).
 *                  Unlike with older APIs, the packet is always fully consumed,
 *                  and if it contains multiple frames (e.g. some audio codecs),
 *                  will require you to call avcodec_receive_frame() multiple
 *                  times afterwards before you can send a new packet.
 *                  It can be NULL (or an AVPacket with data set to NULL and
 *                  size set to 0); in this case, it is considered a flush
 *                  packet, which signals the end of the stream. Sending the
 *                  first flush packet will return success. Subsequent ones are
 *                  unnecessary and will return AVERROR_EOF. If the decoder
 *                  still has frames buffered, it will return them after sending
 *                  a flush packet.
 *
 * @return 0 on success, otherwise negative error code:
 *      AVERROR(EAGAIN):   input is not accepted right now - the packet must be
 *                         resent after trying to read output
 *      AVERROR_EOF:       the decoder has been flushed, and no new packets can
 *                         be sent to it (also returned if more than 1 flush
 *                         packet is sent)
 *      AVERROR(EINVAL):   codec not opened, it is an encoder, or requires flush
 *      AVERROR(ENOMEM):   failed to add packet to internal queue, or similar
 *      other errors: legitimate decoding errors
ide

 */函數

int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);ui

  • 參數分析
    this

AVCodecContext *avctx:第一個參數與舊的接口一致,是視頻解碼的上下文,包含×××。編碼

const AVPacket *avpkt: 編碼的音視頻幀數據spa

  • 爲何要傳遞空的avpktcode

這裏有一個說明是能夠傳遞NULL,什麼狀況下須要傳遞NULL,你平時看一些視頻播放器,播放常常會少最後幾幀,不少狀況就是由於沒有處理好緩衝幀的問題,ffmpeg內部會緩衝幾幀,要想取出來就須要傳遞空的AVPacket進去。

avcodec_receive_frame

  • 接口源碼

/**
 * Return decoded output data from a decoder.
 *
 * @param avctx codec context
 * @param frame This will be set to a reference-counted video or audio
 *              frame (depending on the decoder type) allocated by the
 *              decoder. Note that the function will always call
 *              av_frame_unref(frame) before doing anything else.
 *
 * @return
 *      0:                 success, a frame was returned
 *      AVERROR(EAGAIN):   output is not available right now - user must try
 *                         to send new input
 *      AVERROR_EOF:       the decoder has been fully flushed, and there will be
 *                         no more output frames
 *      AVERROR(EINVAL):   codec not opened, or it is an encoder
 *      other negative values: legitimate decoding errors

 */

int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);

  • 參數分析

AVCodecContext *avctx:第一個參數視頻解碼的上下文,與上面接口一致。

AVFrame *frame:解碼後的視頻幀數據。

  • 空間申請和釋放問題

     解碼後圖像空間由函數內部申請,你所作的只須要分配 AVFrame 對象空間,若是你每次調用avcodec_receive_frame傳遞同一個對象,接口內部會判斷空間是否已經分配,若是沒有分配會在函數內部分配。

avcodec_send_packet和avcodec_receive_frame調用關係並不必定是一對一的,好比一些音頻數據一個AVPacket中包含了1秒鐘的音頻,調用一次avcodec_send_packet以後,可能須要調用25次 avcodec_receive_frame才能獲取所有的解碼音頻數據,因此要作以下處理:

int re = avcodec_send_packet(codec, pkt);

if (re != 0)

{

    return;

}

while( avcodec_receive_frame(codec, frame) == 0)

{

    //讀取到一幀音頻或者視頻

    //處理解碼後音視頻 frame

}


更多的資料也能夠關注我51CTO上的視頻課程

夏老師的課堂 http://edu.51cto.com/lecturer/12016059.html

手把手教您開發視頻播放器

http://edu.51cto.com/course/course_id-8059.html


wKiom1hhpxiBk1CeAAHvubpXcZo754.png-wh_50

相關文章
相關標籤/搜索