]> git.sesse.net Git - ffmpeg/commitdiff
avcodec/libx265: add a qp option and apply the relevant global AVCodecContext setting...
authorJames Almer <jamrial@gmail.com>
Wed, 14 Aug 2019 01:09:34 +0000 (22:09 -0300)
committerJames Almer <jamrial@gmail.com>
Wed, 1 Jan 2020 16:48:02 +0000 (13:48 -0300)
Signed-off-by: James Almer <jamrial@gmail.com>
doc/encoders.texi
libavcodec/libx265.c
libavcodec/version.h

index 673eaf649604b1758e2b764ebc85bdaec6833d09..61e674cf9688ea2934b6feb6a6e2a12b7db552cf 100644 (file)
@@ -2441,6 +2441,28 @@ Set profile restrictions.
 @item crf
 Set the quality for constant quality mode.
 
+@item qp
+Set constant quantization rate control method parameter.
+
+@item qmin
+Minimum quantizer scale.
+
+@item qmax
+Maximum quantizer scale.
+
+@item qdiff
+Maximum difference between quantizer scales.
+
+@item qblur
+Quantizer curve blur
+
+@item qcomp
+Quantizer curve compression factor
+
+@item i_qfactor
+
+@item b_qfactor
+
 @item forced-idr
 Normally, when forcing a I-frame type, the encoder can select any type
 of I-frame. This option forces it to choose an IDR-frame.
index 798ba12efadb199852b7250191049f55c36f2a2d..3c3deb2bdb55c80534eb569bc4e7ff24b16c6aa7 100644 (file)
@@ -42,6 +42,7 @@ typedef struct libx265Context {
     const x265_api *api;
 
     float crf;
+    int   cqp;
     int   forced_idr;
     char *preset;
     char *tune;
@@ -82,6 +83,21 @@ static av_cold int libx265_encode_close(AVCodecContext *avctx)
     return 0;
 }
 
+static av_cold int libx265_param_parse_float(AVCodecContext *avctx,
+                                           const char *key, float value)
+{
+    libx265Context *ctx = avctx->priv_data;
+    char buf[256];
+
+    snprintf(buf, sizeof(buf), "%2.2f", value);
+    if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid value %2.2f for param \"%s\".\n", value, key);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
 static av_cold int libx265_param_parse_int(AVCodecContext *avctx,
                                            const char *key, int value)
 {
@@ -242,6 +258,48 @@ static av_cold int libx265_encode_init(AVCodecContext *avctx)
     } else if (avctx->bit_rate > 0) {
         ctx->params->rc.bitrate         = avctx->bit_rate / 1000;
         ctx->params->rc.rateControlMode = X265_RC_ABR;
+    } else if (ctx->cqp >= 0) {
+        ret = libx265_param_parse_int(avctx, "qp", ctx->cqp);
+        if (ret < 0)
+            return ret;
+    }
+
+#if X265_BUILD >= 89
+    if (avctx->qmin >= 0) {
+        ret = libx265_param_parse_int(avctx, "qpmin", avctx->qmin);
+        if (ret < 0)
+            return ret;
+    }
+    if (avctx->qmax >= 0) {
+        ret = libx265_param_parse_int(avctx, "qpmax", avctx->qmax);
+        if (ret < 0)
+            return ret;
+    }
+#endif
+    if (avctx->max_qdiff >= 0) {
+        ret = libx265_param_parse_int(avctx, "qpstep", avctx->max_qdiff);
+        if (ret < 0)
+            return ret;
+    }
+    if (avctx->qblur >= 0) {
+        ret = libx265_param_parse_float(avctx, "qblur", avctx->qblur);
+        if (ret < 0)
+            return ret;
+    }
+    if (avctx->qcompress >= 0) {
+        ret = libx265_param_parse_float(avctx, "qcomp", avctx->qcompress);
+        if (ret < 0)
+            return ret;
+    }
+    if (avctx->i_quant_factor >= 0) {
+        ret = libx265_param_parse_float(avctx, "ipratio", avctx->i_quant_factor);
+        if (ret < 0)
+            return ret;
+    }
+    if (avctx->b_quant_factor >= 0) {
+        ret = libx265_param_parse_float(avctx, "pbratio", avctx->b_quant_factor);
+        if (ret < 0)
+            return ret;
     }
 
     ctx->params->rc.vbvBufferSize = avctx->rc_buffer_size / 1000;
@@ -576,6 +634,7 @@ static av_cold void libx265_encode_init_csp(AVCodec *codec)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "crf",         "set the x265 crf",                                                            OFFSET(crf),       AV_OPT_TYPE_FLOAT,  { .dbl = -1 }, -1, FLT_MAX, VE },
+    { "qp",          "set the x265 qp",                                                             OFFSET(cqp),       AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX, VE },
     { "forced-idr",  "if forcing keyframes, force them as IDR frames",                              OFFSET(forced_idr),AV_OPT_TYPE_BOOL,   { .i64 =  0 },  0,       1, VE },
     { "preset",      "set the x265 preset",                                                         OFFSET(preset),    AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
     { "tune",        "set the x265 tune parameter",                                                 OFFSET(tune),      AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
@@ -597,6 +656,13 @@ static const AVCodecDefault x265_defaults[] = {
     { "g", "-1" },
     { "keyint_min", "-1" },
     { "refs", "-1" },
+    { "qmin", "-1" },
+    { "qmax", "-1" },
+    { "qdiff", "-1" },
+    { "qblur", "-1" },
+    { "qcomp", "-1" },
+    { "i_qfactor", "-1" },
+    { "b_qfactor", "-1" },
     { NULL },
 };
 
index 94bcbae2d68795a6b7de1c9c125c34c18c4e76b9..4c4027d70966fd6ad788f5c78805ccf1be050eac 100644 (file)
@@ -29,7 +29,7 @@
 
 #define LIBAVCODEC_VERSION_MAJOR  58
 #define LIBAVCODEC_VERSION_MINOR  65
-#define LIBAVCODEC_VERSION_MICRO 100
+#define LIBAVCODEC_VERSION_MICRO 101
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \