ffmpeg的avcodec_encode_video2延遲


正文


/**
 * Encode a frame of video.
 *
 * Takes input raw video data from frame and writes the next output packet, if
 * available, to avpkt. The output packet does not necessarily contain data for
 * the most recent frame, as encoders can delay and reorder input frames
 * internally as needed.
 *
 * @param avctx     codec context
 * @param avpkt     output AVPacket.
 *                  The user can supply an output buffer by setting
 *                  avpkt->data and avpkt->size prior to calling the
 *                  function, but if the size of the user-provided data is not
 *                  large enough, encoding will fail. All other AVPacket fields
 *                  will be reset by the encoder using av_init_packet(). If
 *                  avpkt->data is NULL, the encoder will allocate it.
 *                  The encoder will set avpkt->size to the size of the
 *                  output packet. The returned data (if any) belongs to the
 *                  caller, he is responsible for freeing it.
 *
 *                  If this function fails or produces no output, avpkt will be
 *                  freed using av_packet_unref().
 * @param[in] frame AVFrame containing the raw video data to be encoded.
 *                  May be NULL when flushing an encoder that has the
 *                  AV_CODEC_CAP_DELAY capability set.
 * @param[out] got_packet_ptr This field is set to 1 by libavcodec if the
 *                            output packet is non-empty, and to 0 if it is
 *                            empty. If the function returns an error, the
 *                            packet can be assumed to be invalid, and the
 *                            value of got_packet_ptr is undefined and should
 *                            not be used.
 * @return          0 on success, negative error code on failure
 *
 * @deprecated use avcodec_send_frame()/avcodec_receive_packet() instead
 */
attribute_deprecated
int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt,
                          const AVFrame *frame, int *got_packet_ptr);

avcodec_encode_video2是用來進行視頻幀編碼的一個函數,原型如上, 其中got_packet_ptr表示編碼之后的數據,但是這個數據因為編碼器優化的原因有可能還沒出現是NULL,要解決這個問題對編碼器的context做如下設置:

  av_opt_set(codec_ctx->priv_data, "preset", "superfast", 0); 
  av_opt_set(codec_ctx->priv_data, "tune", "zerolatency", 0);

取x264為例,其中presettune的值都對應着x264的一組參數,preset控制編碼速度,而tune更偏向於畫面質量和延遲,詳見,這些在x264 --fullhelp中都有很好的說明
對於suprefast,其對應的編碼選項為:

--no-mbtree      //mbtree,一種碼率優化方法,編碼后續的幀發現對前面幀的引用增加,進行評估后是否要減少前面幀的量化步長提升參考幀的質量,就能減少殘差獲得整體碼率以及質量的提升
--me dia         //運動估計搜索方法,菱形搜索方法,是最快的方法
--no-mixed-refs  //幀間編碼塊會使用16x16宏塊以及8x8塊的混合參考編碼,禁用后只使用宏塊作為參考
--partitions i8x8,i4x4 //預測分塊方式使用幀內8x8和4x4,其它的還有幀間編碼p8x8,以及雙向幀間編碼b8x8
--rc-lookahead 0 //不是dpb中的list[0]的長度,是碼率優化時候,前向搜索mb優化時的搜索數
--ref 1          //(DPB: Decoded Picture Buffer)的大小。數值范圍0至16。這里設置為1
--subme 1        //子像素運動估計的匹配算法,0是ful pixel匹配,1是sad,sad比較快
--trellis 0      //不使用網格編碼量化
--weightp 1      // 使x264能夠使用明確加權預測(explicit weighted prediction)來改進P幀的壓縮。模式越高越慢。0代表不啟用

對於zerolatency

--bframes 0      //連續B幀數為0
--force-cfr      //強制固定碼率
--no-mbtree      //不適用mbtree,同上
--sync-lookahead 0 //設置用於線程預測的幀緩存大小。最大值是250, 還搞不懂它和dpb的關系
--sliced-threads //對slice多線程編碼
--rc-lookahead 0 //同上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM