]> git.sesse.net Git - ffmpeg/commitdiff
nvenc: Delay frame output to increase encoding speed
authorTimo Rothenpieler <timo@rothenpieler.org>
Sat, 25 Jul 2015 21:20:28 +0000 (23:20 +0200)
committerAnton Khirnov <anton@khirnov.net>
Thu, 19 May 2016 12:17:01 +0000 (14:17 +0200)
Signed-off-by: Anton Khirnov <anton@khirnov.net>
libavcodec/nvenc.c
libavcodec/nvenc.h
libavcodec/nvenc_h264.c
libavcodec/nvenc_hevc.c

index 97922453f7be9e20bbef2e62a3237fb7716ab66d..bd704a76adbc6324f555eb5f080a9da37531f64f 100644 (file)
@@ -840,6 +840,8 @@ static int nvenc_setup_surfaces(AVCodecContext *avctx)
 
     ctx->nb_surfaces = FFMAX(4 + avctx->max_b_frames,
                              ctx->nb_surfaces);
+    ctx->async_depth = FFMIN(ctx->async_depth, ctx->nb_surfaces - 1);
+
 
     ctx->frames = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->frames));
     if (!ctx->frames)
@@ -1301,13 +1303,19 @@ FF_ENABLE_DEPRECATION_WARNINGS
 static int output_ready(AVCodecContext *avctx, int flush)
 {
     NVENCContext *ctx = avctx->priv_data;
+    int nb_ready, nb_pending;
 
     /* when B-frames are enabled, we wait for two initial timestamps to
      * calculate the first dts */
     if (!flush && avctx->max_b_frames > 0 &&
         (ctx->initial_pts[0] == AV_NOPTS_VALUE || ctx->initial_pts[1] == AV_NOPTS_VALUE))
         return 0;
-    return av_fifo_size(ctx->ready) > 0;
+
+    nb_ready   = av_fifo_size(ctx->ready)   / sizeof(NVENCFrame*);
+    nb_pending = av_fifo_size(ctx->pending) / sizeof(NVENCFrame*);
+    if (flush)
+        return nb_ready > 0;
+    return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
 }
 
 int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
index 866099789cffbb3e662fc0504c9670caf01a73c1..8cd5991f4c5892b06f4d247649d8c2d7c3f90ad0 100644 (file)
@@ -142,6 +142,7 @@ typedef struct NVENCContext {
     int rc;
     int device;
     int flags;
+    int async_depth;
 } NVENCContext;
 
 int ff_nvenc_encode_init(AVCodecContext *avctx);
index 829ce90cd6b46c7a248f167a6c844d1b7d4c5570..ea91e077c02db051fff0fd57766b20570dc0ceed 100644 (file)
@@ -72,6 +72,8 @@ static const AVOption options[] = {
     { "device",   "Select a specific NVENC device",       OFFSET(device),      AV_OPT_TYPE_INT,    { .i64 = -1 },                   -2, INT_MAX, VE, "device" },
     { "any",      "Pick the first device available",      0,                   AV_OPT_TYPE_CONST,  { .i64 = ANY_DEVICE },           0, 0, VE, "device" },
     { "list",     "List the available devices",           0,                   AV_OPT_TYPE_CONST,  { .i64 = LIST_DEVICES },         0, 0, VE, "device" },
+    { "async_depth", "Delay frame output by the given amount of frames", OFFSET(async_depth), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
+    { "delay",       "Delay frame output by the given amount of frames", OFFSET(async_depth), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
     { NULL }
 };
 
index d49ae62502858d683195c90d48df1a708bae8569..6ff350698c319f83a181cae68cc539982375bbaa 100644 (file)
@@ -68,6 +68,8 @@ static const AVOption options[] = {
     { "device",   "Select a specific NVENC device",       OFFSET(device),      AV_OPT_TYPE_INT,    { .i64 = -1 },                   -2, INT_MAX, VE, "device" },
     { "any",      "Pick the first device available",      0,                   AV_OPT_TYPE_CONST,  { .i64 = ANY_DEVICE },           0, 0, VE, "device" },
     { "list",     "List the available devices",           0,                   AV_OPT_TYPE_CONST,  { .i64 = LIST_DEVICES },         0, 0, VE, "device" },
+    { "async_depth", "Delay frame output by the given amount of frames", OFFSET(async_depth), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
+    { "delay",       "Delay frame output by the given amount of frames", OFFSET(async_depth), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 0, INT_MAX, VE },
     { NULL }
 };