From dff2e8e11485cd5a66fed980fe14ab20bd7c0aed Mon Sep 17 00:00:00 2001
From: Sumit Agarwal <sumita@nvidia.com>
Date: Tue, 9 May 2017 19:54:54 +0530
Subject: [PATCH] Add NVENC Weighted Prediction support

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

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 00766c2..fc88ab3 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -298,6 +298,12 @@ static int nvenc_check_capabilities(AVCodecContext *avctx)
         return AVERROR(ENOSYS);
     }
 
+    ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION);
+    if (ctx->weighted_pred > 0 && ret <= 0) {
+        av_log (avctx, AV_LOG_VERBOSE, "Weighted Prediction not supported\n");
+        return AVERROR(ENOSYS);
+    }
+
     return 0;
 }
 
@@ -1012,6 +1018,9 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx)
     ctx->init_encode_params.enableEncodeAsync = 0;
     ctx->init_encode_params.enablePTD = 1;
 
+    if (ctx->weighted_pred == 1)
+        ctx->init_encode_params.enableWeightedPrediction = 1;
+
     if (ctx->bluray_compat) {
         ctx->aud = 1;
         avctx->refs = FFMIN(FFMAX(avctx->refs, 0), 6);
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index 763647b..ce63aa0 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -159,6 +159,7 @@ typedef struct NvencContext
     int init_qp_b;
     int init_qp_i;
     int cqp;
+    int weighted_pred;
 } NvencContext;
 
 int ff_nvenc_encode_init(AVCodecContext *avctx);
diff --git a/libavcodec/nvenc_h264.c b/libavcodec/nvenc_h264.c
index 8d44b1f..f7bc028 100644
--- a/libavcodec/nvenc_h264.c
+++ b/libavcodec/nvenc_h264.c
@@ -114,6 +114,8 @@ static const AVOption options[] = {
     { "init_qpI",     "Initial QP value for I frame",       OFFSET(init_qp_i),    AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
     { "qp",           "Constant quantization parameter rate control method",
                                                             OFFSET(cqp),          AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
+    { "weighted_pred","Set 1 to enable weighted prediction",
+                                                            OFFSET(weighted_pred),AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, 1, VE },
     { NULL }
 };
 
diff --git a/libavcodec/nvenc_hevc.c b/libavcodec/nvenc_hevc.c
index 6d6750a..f532eb1 100644
--- a/libavcodec/nvenc_hevc.c
+++ b/libavcodec/nvenc_hevc.c
@@ -111,6 +111,8 @@ static const AVOption options[] = {
     { "init_qpI",     "Initial QP value for I frame",       OFFSET(init_qp_i),    AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
     { "qp",           "Constant quantization parameter rate control method",
                                                             OFFSET(cqp),          AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 51, VE },
+    { "weighted_pred","Set 1 to enable weighted prediction",
+                                                            OFFSET(weighted_pred),AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, 1, VE },
     { NULL }
 };
 
-- 
2.10.1.windows.1

