[FFmpeg-cvslog] avcodec/nvenc: add intra refresh support

Limin Wang git at videolan.org
Mon Sep 6 15:47:57 EEST 2021


ffmpeg | branch: master | Limin Wang <lance.lmwang at gmail.com> | Mon Sep  6 10:01:58 2021 +0800| [e6bd5171ac47c6858889b483ddd77f8c49e1fba0] | committer: Timo Rothenpieler

avcodec/nvenc: add intra refresh support

Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
Signed-off-by: Timo Rothenpieler <timo at rothenpieler.org>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e6bd5171ac47c6858889b483ddd77f8c49e1fba0
---

 libavcodec/nvenc.c      | 35 +++++++++++++++++++++++++++++++----
 libavcodec/nvenc.h      |  1 +
 libavcodec/nvenc_h264.c |  2 ++
 libavcodec/nvenc_hevc.c |  2 ++
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 163d442b55..41963e4d21 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -529,6 +529,12 @@ static int nvenc_check_capabilities(AVCodecContext *avctx)
     }
 #endif
 
+    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_INTRA_REFRESH);
+    if(ctx->intra_refresh && ret <= 0) {
+        av_log(avctx, AV_LOG_WARNING, "Intra refresh not supported by the device\n");
+        return AVERROR(ENOSYS);
+    }
+
     ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
 
     return 0;
@@ -1076,6 +1082,12 @@ static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
     h264->sliceMode = 3;
     h264->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
 
+    if (ctx->intra_refresh) {
+        h264->enableIntraRefresh = 1;
+        h264->intraRefreshPeriod = avctx->gop_size;
+        h264->intraRefreshCnt = avctx->gop_size - 1;
+    }
+
     h264->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
     h264->repeatSPSPPS  = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
     h264->outputAUD     = ctx->aud;
@@ -1084,8 +1096,11 @@ static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx)
         /* 0 means "let the hardware decide" */
         h264->maxNumRefFrames = ctx->dpb_size;
     }
-    if (avctx->gop_size >= 0) {
-        h264->idrPeriod = cc->gopLength;
+
+    if (ctx->intra_refresh) {
+        h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
+    } else if (avctx->gop_size >= 0) {
+        h264->idrPeriod = avctx->gop_size;
     }
 
     if (IS_CBR(cc->rcParams.rateControlMode)) {
@@ -1173,6 +1188,12 @@ static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
     hevc->sliceMode = 3;
     hevc->sliceModeData = avctx->slices > 0 ? avctx->slices : 1;
 
+    if (ctx->intra_refresh) {
+        hevc->enableIntraRefresh = 1;
+        hevc->intraRefreshPeriod = avctx->gop_size;
+        hevc->intraRefreshCnt = avctx->gop_size - 1;
+    }
+
     hevc->disableSPSPPS = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
     hevc->repeatSPSPPS  = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
     hevc->outputAUD     = ctx->aud;
@@ -1181,8 +1202,11 @@ static av_cold int nvenc_setup_hevc_config(AVCodecContext *avctx)
         /* 0 means "let the hardware decide" */
         hevc->maxNumRefFramesInDPB = ctx->dpb_size;
     }
-    if (avctx->gop_size >= 0) {
-        hevc->idrPeriod = cc->gopLength;
+
+    if (ctx->intra_refresh) {
+        hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
+    } else if (avctx->gop_size >= 0) {
+        hevc->idrPeriod = avctx->gop_size;
     }
 
     if (IS_CBR(cc->rcParams.rateControlMode)) {
@@ -1367,6 +1391,9 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
         ctx->encode_config.gopLength = 1;
     }
 
+    if (ctx->intra_refresh)
+        ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
+
     nvenc_recalc_surfaces(avctx);
 
     nvenc_setup_rate_control(avctx);
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index 7c284fbbd8..e7cc0e6ba0 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -230,6 +230,7 @@ typedef struct NvencContext
     int multipass;
     int ldkfs;
     int extra_sei;
+    int intra_refresh;
 } NvencContext;
 
 int ff_nvenc_encode_init(AVCodecContext *avctx);
diff --git a/libavcodec/nvenc_h264.c b/libavcodec/nvenc_h264.c
index fffdedcf6e..f00755530e 100644
--- a/libavcodec/nvenc_h264.c
+++ b/libavcodec/nvenc_h264.c
@@ -188,6 +188,8 @@ static const AVOption options[] = {
 #endif
     { "extra_sei",    "Pass on extra SEI data (e.g. a53 cc) to be included in the bitstream",
                                                             OFFSET(extra_sei),    AV_OPT_TYPE_BOOL,  { .i64 = 1 }, 0, 1, VE },
+    { "intra-refresh","Use Periodic Intra Refresh instead of IDR frames",
+                                                            OFFSET(intra_refresh),AV_OPT_TYPE_BOOL,  { .i64 = 0 }, 0, 1, VE },
     { NULL }
 };
 
diff --git a/libavcodec/nvenc_hevc.c b/libavcodec/nvenc_hevc.c
index 4edc1797a0..efddd990e4 100644
--- a/libavcodec/nvenc_hevc.c
+++ b/libavcodec/nvenc_hevc.c
@@ -169,6 +169,8 @@ static const AVOption options[] = {
 #endif
     { "extra_sei",    "Pass on extra SEI data (e.g. a53 cc) to be included in the bitstream",
                                                             OFFSET(extra_sei),    AV_OPT_TYPE_BOOL,  { .i64 = 1 }, 0, 1, VE },
+    { "intra-refresh","Use Periodic Intra Refresh instead of IDR frames",
+                                                            OFFSET(intra_refresh),AV_OPT_TYPE_BOOL,  { .i64 = 0 }, 0, 1, VE },
     { NULL }
 };
 



More information about the ffmpeg-cvslog mailing list