diff mbox

[FFmpeg-devel,2/3] avcodec/libx265: add a qp option and apply the relevant global AVCodecContext settings to the encoder context

Message ID 20191228235514.5335-2-jamrial@gmail.com
State Superseded
Headers show

Commit Message

James Almer Dec. 28, 2019, 11:55 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 doc/encoders.texi    | 22 ++++++++++++++++++++++
 libavcodec/libx265.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

Comments

Michael Niedermayer Dec. 29, 2019, 11:53 a.m. UTC | #1
On Sat, Dec 28, 2019 at 08:55:13PM -0300, James Almer wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  doc/encoders.texi    | 22 ++++++++++++++++++++++
>  libavcodec/libx265.c | 28 ++++++++++++++++++++++++++++
>  2 files changed, 50 insertions(+)

breaks build here, i susupect missing libx265 version check ...

CC	libavcodec/libx265.o
libavcodec/libx265.c: In function ‘libx265_encode_init’:
libavcodec/libx265.c:237:24: error: ‘struct <anonymous>’ has no member named ‘qpMin’
         ctx->params->rc.qpMin = avctx->qmin;
                        ^
libavcodec/libx265.c:239:24: error: ‘struct <anonymous>’ has no member named ‘qpMax’
         ctx->params->rc.qpMax = avctx->qmax;
                        ^
libavcodec/libx265.c:241:20: error: request for member ‘rc’ in something not a structure or union
         ctx->params.rc.qpStep = avctx->max_qdiff;
                    ^
make: *** [libavcodec/libx265.o] Error 1



[...]
James Almer Dec. 29, 2019, 1:17 p.m. UTC | #2
On 12/29/2019 8:53 AM, Michael Niedermayer wrote:
> On Sat, Dec 28, 2019 at 08:55:13PM -0300, James Almer wrote:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>  doc/encoders.texi    | 22 ++++++++++++++++++++++
>>  libavcodec/libx265.c | 28 ++++++++++++++++++++++++++++
>>  2 files changed, 50 insertions(+)
> 
> breaks build here, i susupect missing libx265 version check ...
> 
> CC	libavcodec/libx265.o
> libavcodec/libx265.c: In function ‘libx265_encode_init’:
> libavcodec/libx265.c:237:24: error: ‘struct <anonymous>’ has no member named ‘qpMin’
>          ctx->params->rc.qpMin = avctx->qmin;
>                         ^
> libavcodec/libx265.c:239:24: error: ‘struct <anonymous>’ has no member named ‘qpMax’
>          ctx->params->rc.qpMax = avctx->qmax;
>                         ^
> libavcodec/libx265.c:241:20: error: request for member ‘rc’ in something not a structure or union
>          ctx->params.rc.qpStep = avctx->max_qdiff;
>                     ^

Is this a distro provided libx265? The commit that added these fields is
three and a half years old by now.

I'm inclined to bump the required library version from 68 to 89, to
reduce clutter from pre-processor checks. But i want to hear Derek's
opinion first since he's the maintainer.
Michael Niedermayer Dec. 29, 2019, 2:25 p.m. UTC | #3
On Sun, Dec 29, 2019 at 10:17:48AM -0300, James Almer wrote:
> On 12/29/2019 8:53 AM, Michael Niedermayer wrote:
> > On Sat, Dec 28, 2019 at 08:55:13PM -0300, James Almer wrote:
> >> Signed-off-by: James Almer <jamrial@gmail.com>
> >> ---
> >>  doc/encoders.texi    | 22 ++++++++++++++++++++++
> >>  libavcodec/libx265.c | 28 ++++++++++++++++++++++++++++
> >>  2 files changed, 50 insertions(+)
> > 
> > breaks build here, i susupect missing libx265 version check ...
> > 
> > CC	libavcodec/libx265.o
> > libavcodec/libx265.c: In function ‘libx265_encode_init’:
> > libavcodec/libx265.c:237:24: error: ‘struct <anonymous>’ has no member named ‘qpMin’
> >          ctx->params->rc.qpMin = avctx->qmin;
> >                         ^
> > libavcodec/libx265.c:239:24: error: ‘struct <anonymous>’ has no member named ‘qpMax’
> >          ctx->params->rc.qpMax = avctx->qmax;
> >                         ^
> > libavcodec/libx265.c:241:20: error: request for member ‘rc’ in something not a structure or union
> >          ctx->params.rc.qpStep = avctx->max_qdiff;
> >                     ^
> 
> Is this a distro provided libx265? The commit that added these fields is
> three and a half years old by now.

no, it just wasnt updated since a long time.

Thanks


[...]
diff mbox

Patch

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 673eaf6496..61e674cf96 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -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.
diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index 508fade98b..581732d169 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -42,6 +42,7 @@  typedef struct libx265Context {
     const x265_api *api;
 
     float crf;
+    int   cqp;
     int   forced_idr;
     char *preset;
     char *tune;
@@ -223,11 +224,30 @@  static av_cold int libx265_encode_init(AVCodecContext *avctx)
             av_log(avctx, AV_LOG_ERROR, "Invalid crf: %2.2f.\n", ctx->crf);
             return AVERROR(EINVAL);
         }
+        ctx->params->rc.rateControlMode = X265_RC_CRF;
     } 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) {
+        ctx->params->rc.qp              = ctx->cqp;
+        ctx->params->rc.rateControlMode = X265_RC_CQP;
     }
 
+    if (avctx->qmin >= 0)
+        ctx->params->rc.qpMin = avctx->qmin;
+    if (avctx->qmax >= 0)
+        ctx->params->rc.qpMax = avctx->qmax;
+    if (avctx->max_qdiff >= 0)
+        ctx->params.rc.qpStep = avctx->max_qdiff;
+    if (avctx->qblur >= 0)
+        ctx->params->rc.qblur = avctx->qblur;
+    if (avctx->qcompress >= 0)
+        ctx->params->rc.qCompress = avctx->qcompress;
+    if (avctx->i_quant_factor > 0)
+        ctx->params->rc.ipFactor = 1 / fabs(avctx->i_quant_factor);
+    if (avctx->b_quant_factor > 0)
+        ctx->params->rc.pbFactor = avctx->b_quant_factor;
+
     ctx->params->rc.vbvBufferSize = avctx->rc_buffer_size / 1000;
     ctx->params->rc.vbvMaxBitrate = avctx->rc_max_rate    / 1000;
 
@@ -548,6 +568,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 },
@@ -569,6 +590,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 },
 };