From ab98c06a19086ee3763722556295fa32ab8b8789 Mon Sep 17 00:00:00 2001
From: Miroslav Slugen <thunder.m@email.cz>
Date: Mon, 21 Nov 2016 11:30:27 +0100
Subject: [PATCH] NVENC: Better surface allocation alghoritm, fix rc_lookahead
limit
---
libavcodec/nvenc.c | 28 +++++++++++++++++++++++-----
1 file changed, 23 insertions(+), 5 deletions(-)
@@ -674,6 +674,27 @@ static void nvenc_override_rate_control(AVCodecContext *avctx)
rc->rateControlMode = ctx->rc;
}
+static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx)
+{
+ NvencContext *ctx = avctx->priv_data;
+ int nb_surfaces = 0;
+
+ if (ctx->rc_lookahead > 0) {
+ nb_surfaces = ctx->rc_lookahead + ((ctx->encode_config.frameIntervalP > 0) ? ctx->encode_config.frameIntervalP : 0) + 1 + 4;
+ if (ctx->nb_surfaces < nb_surfaces) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Defined rc_lookahead require more surfaces, "
+ "increasing used surfaces %d -> %d\n", ctx->nb_surfaces, nb_surfaces);
+ ctx->nb_surfaces = nb_surfaces;
+ }
+ }
+
+ ctx->nb_surfaces = FFMAX(1, FFMIN(MAX_REGISTERED_FRAMES, ctx->nb_surfaces));
+ ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
+
+ return 0;
+}
+
static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
@@ -1030,6 +1051,8 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
ctx->initial_pts[0] = AV_NOPTS_VALUE;
ctx->initial_pts[1] = AV_NOPTS_VALUE;
+ nvenc_recalc_surfaces(avctx);
+
nvenc_setup_rate_control(avctx);
if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
@@ -1156,11 +1179,6 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
{
NvencContext *ctx = avctx->priv_data;
int i, res;
- int num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
- ctx->nb_surfaces = FFMAX((num_mbs >= 8160) ? 32 : 48,
- ctx->nb_surfaces);
- ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
-
ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces));
if (!ctx->surfaces)
--
2.1.4