From 3c22436ddf5995dd2be7f54c0301a737e22056c8 Mon Sep 17 00:00:00 2001 From: James Almer Date: Tue, 13 Aug 2019 22:07:08 -0300 Subject: [PATCH] avcodec/libx265: apply some global AVCodecContext settings to the encoder context There's no reason to ignore them if set. Signed-off-by: James Almer --- doc/encoders.texi | 14 ++++++++++++++ libavcodec/libx265.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/doc/encoders.texi b/doc/encoders.texi index a207363650..673eaf6496 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -2415,6 +2415,20 @@ during configuration. You need to explicitly configure the build with @subsection Options @table @option +@item b +Sets target video bitrate. + +@item bf + +@item g +Set the GOP size. + +@item keyint_min +Minimum GOP size. + +@item refs +Number of reference frames each P-frame can use. The range is from @var{1-16}. + @item preset Set the x265 preset. diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c index 963c28fb1d..798ba12efa 100644 --- a/libavcodec/libx265.c +++ b/libavcodec/libx265.c @@ -82,10 +82,26 @@ static av_cold int libx265_encode_close(AVCodecContext *avctx) return 0; } +static av_cold int libx265_param_parse_int(AVCodecContext *avctx, + const char *key, int value) +{ + libx265Context *ctx = avctx->priv_data; + char buf[256]; + + snprintf(buf, sizeof(buf), "%d", value); + if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) { + av_log(avctx, AV_LOG_ERROR, "Invalid value %d for param \"%s\".\n", value, key); + return AVERROR(EINVAL); + } + + return 0; +} + static av_cold int libx265_encode_init(AVCodecContext *avctx) { libx265Context *ctx = avctx->priv_data; AVCPBProperties *cpb_props = NULL; + int ret; ctx->api = x265_api_get(av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth); if (!ctx->api) @@ -241,6 +257,27 @@ static av_cold int libx265_encode_init(AVCodecContext *avctx) if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) ctx->params->bRepeatHeaders = 1; + if (avctx->gop_size >= 0) { + ret = libx265_param_parse_int(avctx, "keyint", avctx->gop_size); + if (ret < 0) + return ret; + } + if (avctx->keyint_min > 0) { + ret = libx265_param_parse_int(avctx, "min-keyint", avctx->keyint_min); + if (ret < 0) + return ret; + } + if (avctx->max_b_frames >= 0) { + ret = libx265_param_parse_int(avctx, "bframes", avctx->max_b_frames); + if (ret < 0) + return ret; + } + if (avctx->refs >= 0) { + ret = libx265_param_parse_int(avctx, "ref", avctx->refs); + if (ret < 0) + return ret; + } + if (ctx->x265_opts) { AVDictionary *dict = NULL; AVDictionaryEntry *en = NULL; @@ -556,6 +593,10 @@ static const AVClass class = { static const AVCodecDefault x265_defaults[] = { { "b", "0" }, + { "bf", "-1" }, + { "g", "-1" }, + { "keyint_min", "-1" }, + { "refs", "-1" }, { NULL }, };