Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libav: Add a low latency encode option #751

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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