Skip to content

Commit fcb87b6

Browse files
committed
lavc: add a hint to increase throughput
If the compression cannot make it, print a hint that would perhaps increase a latence and thus is not enabled by default. As as consequence of that, set NVENC delay to 0 again to be consistent with the other compressions (latency preferred).
1 parent 2f01fbc commit fcb87b6

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/video_compress/libavcodec.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ struct state_video_compress_libav {
285285
#endif
286286

287287
int conv_thread_count = clamp<unsigned int>(thread::hardware_concurrency(), 1, INT_MAX); ///< number of threads used for UG conversions
288+
double mov_avg_comp_duration = 0;
289+
long mov_avg_frames = 0;
288290
};
289291

290292
struct codec_encoders_decoders{
@@ -1353,6 +1355,7 @@ static bool configure_with(struct state_video_compress_libav *s, struct video_de
13531355
s->compressed_desc = desc;
13541356
s->compressed_desc.color_spec = ug_codec;
13551357
s->compressed_desc.tile_count = 1;
1358+
s->mov_avg_frames = s->mov_avg_comp_duration = 0;
13561359

13571360
s->out_codec = s->compressed_desc.color_spec;
13581361

@@ -1390,6 +1393,36 @@ static void *pixfmt_conv_task(void *arg) {
13901393
return NULL;
13911394
}
13921395

1396+
static void check_duration(struct state_video_compress_libav *s, time_ns_t dur_ns)
1397+
{
1398+
constexpr int mov_window = 100;
1399+
if (s->mov_avg_frames >= 10 * mov_window) {
1400+
return;
1401+
}
1402+
double duration = dur_ns / NS_IN_SEC_DBL;
1403+
s->mov_avg_comp_duration = (s->mov_avg_comp_duration * (mov_window - 1) + duration) / mov_window;
1404+
s->mov_avg_frames += 1;
1405+
if (s->mov_avg_frames < 2 * mov_window || s->mov_avg_comp_duration < 1 / s->compressed_desc.fps) {
1406+
return;
1407+
}
1408+
log_msg(LOG_LEVEL_WARNING, MOD_NAME "Average compression time of last %d frames is %f ms but time per frame is only %f ms!\n",
1409+
mov_window, s->mov_avg_comp_duration * 1000, 1000 / s->compressed_desc.fps);
1410+
string hint;
1411+
if (regex_match(s->codec_ctx->codec->name, regex(".*nvenc.*"))) {
1412+
if (s->lavc_opts.find("delay") == s->lavc_opts.end()) {
1413+
hint = "\"delay=<frames>\" option to NVENC compression (2 suggested)";
1414+
}
1415+
} else if ((s->codec_ctx->thread_type & FF_THREAD_SLICE) == 0 && (s->codec_ctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) != 0) {
1416+
hint = "\"threads=<n>FS\" option with small <n> or 0 (nr of logical cores) to compression";
1417+
} else if (s->codec_ctx->thread_count == 1 && (s->codec_ctx->codec->capabilities & AV_CODEC_CAP_OTHER_THREADS) != 0) {
1418+
hint = "\"threads=<n>\" option with small <n> or 0 (nr of logical cores) to compression";
1419+
}
1420+
if (!hint.empty()) {
1421+
LOG(LOG_LEVEL_WARNING) << MOD_NAME "Consider adding " << hint << " to increase throughput at the expense of latency.\n";
1422+
}
1423+
s->mov_avg_frames = LONG_MAX;
1424+
}
1425+
13931426
static shared_ptr<video_frame> libavcodec_compress_tile(struct module *mod, shared_ptr<video_frame> tx)
13941427
{
13951428
struct state_video_compress_libav *s = (struct state_video_compress_libav *) mod->priv_data;
@@ -1590,6 +1623,7 @@ static shared_ptr<video_frame> libavcodec_compress_tile(struct module *mod, shar
15901623
LOG(LOG_LEVEL_DEBUG2) << MOD_NAME << "duration pixfmt change: " << (t1 - t0) / (double) NS_IN_SEC <<
15911624
" s, dump+swscale " << (t2 - t1) / (double) NS_IN_SEC <<
15921625
" s, compression " << (t3 - t2) / (double) NS_IN_SEC << " s\n";
1626+
check_duration(s, t3 - t0);
15931627

15941628
if (out->tiles[0].data_len == 0) { // videotoolbox returns sometimes frames with pkt->size == 0 but got_output == true
15951629
return {};
@@ -1850,7 +1884,7 @@ static void configure_nvenc(AVCodecContext *codec_ctx, struct setparam_param *pa
18501884
check_av_opt_set<const char *>(codec_ctx->priv_data, "rc", DEFAULT_NVENC_RC);
18511885
check_av_opt_set<int>(codec_ctx->priv_data, "spatial_aq", 0);
18521886
check_av_opt_set<int>(codec_ctx->priv_data, "gpu", cuda_devices[0]);
1853-
check_av_opt_set<int>(codec_ctx->priv_data, "delay", 2); // increases throughput 2x at expense of higher latency
1887+
check_av_opt_set<int>(codec_ctx->priv_data, "delay", 0); // 2'd increase throughput 2x at expense of higher latency
18541888
check_av_opt_set<int>(codec_ctx->priv_data, "zerolatency", 1, "zero latency operation (no reordering delay)");
18551889
check_av_opt_set<const char *>(codec_ctx->priv_data, "b_ref_mode", "disabled", 0);
18561890
codec_ctx->rc_max_rate = codec_ctx->bit_rate;

0 commit comments

Comments
 (0)