From 00a22735f1f38c140fd45ecc776bdfede11c6e59 Mon Sep 17 00:00:00 2001 From: Naushir Patuck Date: Wed, 27 Nov 2024 10:30:21 +0000 Subject: [PATCH] libav: Add a low latency encode option Add a new command line argument (--low-latency) to the video options allowing users to switch on encoder tuning parameters for low latency video encode. Note that this mode will turn down the encoder efficiency by disabling more advanced features, e.g. B-frames. Signed-off-by: Naushir Patuck --- core/video_options.hpp | 3 +++ encoder/libav_encoder.cpp | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/video_options.hpp b/core/video_options.hpp index 918596ab..3bcb8f4b 100644 --- a/core/video_options.hpp +++ b/core/video_options.hpp @@ -158,6 +158,8 @@ struct VideoOptions : public Options ("av-sync", value(&av_sync_)->default_value("0us"), "Add a time offset (in microseconds if no units provided) to the audio stream, relative to the video stream. " "The offset value can be either positive or negative.") + ("low-latency", value(&low_latency)->default_value(false)->implicit_value(true), + "Enables the libav/libx264 low latncy presets for video encoding.") #endif ; // clang-format on @@ -191,6 +193,7 @@ struct VideoOptions : public Options uint32_t segment; size_t circular; uint32_t frames; + bool low_latency; virtual bool Parse(int argc, char *argv[]) override { diff --git a/encoder/libav_encoder.cpp b/encoder/libav_encoder.cpp index 05aa9969..b0ac979c 100644 --- a/encoder/libav_encoder.cpp +++ b/encoder/libav_encoder.cpp @@ -80,16 +80,28 @@ void encoderOptionsH264M2M(VideoOptions const *options, AVCodecContext *codec) void encoderOptionsLibx264(VideoOptions const *options, AVCodecContext *codec) { - codec->max_b_frames = 1; codec->me_range = 16; codec->me_cmp = 1; // No chroma ME codec->me_subpel_quality = 0; codec->thread_count = 0; - codec->thread_type = FF_THREAD_FRAME; - codec->slices = 1; - av_opt_set(codec->priv_data, "preset", "superfast", 0); - av_opt_set(codec->priv_data, "partitions", "i8x8,i4x4", 0); + if (options->low_latency) + { + codec->thread_type = FF_THREAD_SLICE; + codec->slices = 4; + codec->refs = 1; + av_opt_set(codec->priv_data, "preset", "ultrafast", 0); + av_opt_set(codec->priv_data, "tune", "zerolatency", 0); + } + else + { + codec->thread_type = FF_THREAD_FRAME; + codec->slices = 1; + codec->max_b_frames = 1; + av_opt_set(codec->priv_data, "preset", "superfast", 0); + av_opt_set(codec->priv_data, "partitions", "i8x8,i4x4", 0); + } + av_opt_set(codec->priv_data, "weightp", "none", 0); av_opt_set(codec->priv_data, "weightb", "0", 0); av_opt_set(codec->priv_data, "motion-est", "dia", 0);