/**
* 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为例,其中preset
和tune
的值都对应着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 //同上
ffmpeg的avcodec_encode_video2延迟
原文:https://www.cnblogs.com/ishen/p/14783257.html