Skip to content

Commit

Permalink
libav: Add a low latency encode option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
naushir committed Dec 4, 2024
1 parent 6318ac7 commit 6287bff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions core/video_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ struct VideoOptions : public Options
("av-sync", value<std::string>(&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<bool>(&low_latency)->default_value(false)->implicit_value(true),
"Enables the libav/libx264 low latncy presets for video encoding.")
#endif
;
// clang-format on
Expand Down Expand Up @@ -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
{
Expand Down
22 changes: 17 additions & 5 deletions encoder/libav_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6287bff

Please sign in to comment.