From patchwork Thu Dec 20 20:40:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 11504 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 62A8844CDFA for ; Thu, 20 Dec 2018 22:48:13 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8DA2468AE7B; Thu, 20 Dec 2018 22:48:13 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F1B3468AE72 for ; Thu, 20 Dec 2018 22:48:10 +0200 (EET) Received: by mail-wr1-f68.google.com with SMTP id t6so3126965wrr.12 for ; Thu, 20 Dec 2018 12:48:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=bFyTUxTKQKaA8r9dInuh8YdxbzklkAfseOpcQ0EeyFE=; b=NELF0cKBaZee1p93KY2TmVQ9bQ/gh9GPLpnLDpmkHYJKRQW+NHT0aEoVGwndzEVXQk ZFK/SeLpzE8gDAzJFUWqSMqBq5NY6/Rs3CYTKSnfB1R8EGVK5WgCzWHHlRRhNfyfCsr5 3KiXZuIGEllNpjEfzS2TvFsDQO0urlw0p52QnPyXxM9GhCt3jpe+POjTU+zVbMj3WRQJ wJUnIK/jI5yjN7Mgc72/3n9htZkj1xDiFhRMl6q/BsvT2KqxvTie1Eb3lhVNm9EfHeic BjSV1DyfZ1RtYEpC9joaGy9juxMxcVFXIPE2zrQspXVHXEkYsr2LveMVWMSOHpZPtwZ1 tpTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bFyTUxTKQKaA8r9dInuh8YdxbzklkAfseOpcQ0EeyFE=; b=hMm9H7skowyfUtHtREvICIEDcM18sQ0Hxp9TVy7coano45W2vDlrlDTiCgYbMG2nGQ aOkO62M0x3sYTv3beIZjMTv9a3sYI/aKcqRE9NBrJ1WW5VUxeQgaRcr6oGpdS7HDlqr0 WN/B8kg3Hz+XrNZ0Jmwsn7dX9gr3B1BFN/nFK2C1Vcj54Xey0q9rJxJGizCHvxgv8AN7 hVbk9Bmkpl/8wuQym6WamrtVqBwWZssbMByYAqtMclqvXE/aIRvFSEinn/YoLf1WPwip 7CUeQdM40R2MZZ2JpZ6yT40K0QBgJ9yU3Smbm+9o7uDK94JBLN4q2/2vLKSfP5uGzMsq TMOQ== X-Gm-Message-State: AA+aEWZCdohHhOGyPt5DVUlwdvsqhxBmdgNw9ngyMNT2hsnLzAZXTuy7 xfvvGDIg69++UPfkDjOPwo3tZo8FATU= X-Google-Smtp-Source: AFSGD/VbmhWFCLziEoa/kOZXPGSAWyf9GQI6pvDs+bnu9/abVpAeIPHMpwCNJHyQeo4gkmN67YO4Og== X-Received: by 2002:adf:ec83:: with SMTP id z3mr23846753wrn.264.1545338420650; Thu, 20 Dec 2018 12:40:20 -0800 (PST) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id x10sm11956884wrn.29.2018.12.20.12.40.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Dec 2018 12:40:19 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Thu, 20 Dec 2018 20:40:01 +0000 Message-Id: <20181220204008.32766-7-sw@jkqxz.net> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181220204008.32766-1-sw@jkqxz.net> References: <20181220204008.32766-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/14] vaapi_encode: Support more RC modes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Allow setting the mode explicitly, and try to make a sensible choice given the available parameters if not. --- From patch: // * If the user has set a mode explicitly with the rc_mode option, // use it and fail if it is not available. // * If an explicit QP option has been set, use CQP. // * If the codec is CQ-only, use CQP. // * If the QSCALE avcodec option is set, use CQP. // * If bitrate and quality are both set, try QVBR. // * If quality is set, try ICQ, then CQP. // * If bitrate and maxrate are set and have the same value, try CBR. // * If a bitrate is set, try AVBR, then VBR, then CBR. // * If no bitrate is set, try ICQ, then CQP. ICQ seems to be failing on the current iHD driver with "internal encoding error" - I don't think it needs any more parameters, so I'm inclined to blame the driver for this at the moment. AVBR and QVBR do work. libavcodec/vaapi_encode.c | 345 +++++++++++++++++++++++++------------- libavcodec/vaapi_encode.h | 64 +++++++ 2 files changed, 296 insertions(+), 113 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index b4e9fadaee..282faf041f 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -1283,17 +1283,42 @@ fail: return err; } +static const VAAPIEncodeRCMode vaapi_encode_rc_modes[] = { + // Bitrate Quuality + // | Maxrate | HRD/VBV + { 0 }, // | | | | + { "CQP", 1, VA_RC_CQP, 0, 0, 1, 0 }, + { "CBR", 1, VA_RC_CBR, 1, 0, 0, 1 }, + { "VBR", 1, VA_RC_VBR, 1, 1, 0, 1 }, +#if VA_CHECK_VERSION(1, 1, 0) + { "ICQ", 1, VA_RC_ICQ, 0, 0, 1, 0 }, +#else + { "ICQ", 0 }, +#endif +#if VA_CHECK_VERSION(1, 3, 0) + { "QVBR", 1, VA_RC_QVBR, 1, 1, 1, 1 }, + { "AVBR", 0, VA_RC_AVBR, 1, 0, 0, 0 }, +#else + { "QVBR", 0 }, + { "AVBR", 0 }, +#endif +}; + static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; + uint32_t supported_va_rc_modes; + const VAAPIEncodeRCMode *rc_mode; int64_t rc_bits_per_second; int rc_target_percentage; int rc_window_size; + int rc_quality; int64_t hrd_buffer_size; int64_t hrd_initial_buffer_fullness; int fr_num, fr_den; VAConfigAttrib rc_attr = { VAConfigAttribRateControl }; VAStatus vas; + char supported_rc_modes_string[64]; vas = vaGetConfigAttributes(ctx->hwctx->display, ctx->va_profile, ctx->va_entrypoint, @@ -1303,119 +1328,187 @@ static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx) "config attribute: %d (%s).\n", vas, vaErrorStr(vas)); return AVERROR_EXTERNAL; } - if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) { av_log(avctx, AV_LOG_VERBOSE, "Driver does not report any " - "supported rate control modes: assuming constant-quality.\n"); - ctx->va_rc_mode = VA_RC_CQP; - return 0; - } - if (ctx->codec->flags & FLAG_CONSTANT_QUALITY_ONLY || - avctx->flags & AV_CODEC_FLAG_QSCALE || - avctx->bit_rate <= 0) { - if (rc_attr.value & VA_RC_CQP) { - av_log(avctx, AV_LOG_VERBOSE, "Using constant-quality mode.\n"); - ctx->va_rc_mode = VA_RC_CQP; - if (avctx->bit_rate > 0 || avctx->rc_max_rate > 0) { - av_log(avctx, AV_LOG_WARNING, "Bitrate target parameters " - "ignored in constant-quality mode.\n"); + "supported rate control modes: assuming CQP only.\n"); + supported_va_rc_modes = VA_RC_CQP; + strcpy(supported_rc_modes_string, "unknown"); + } else { + char *str = supported_rc_modes_string; + size_t len = sizeof(supported_rc_modes_string); + int i, first = 1, res; + + supported_va_rc_modes = rc_attr.value; + + first = 1; + for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rc_modes); i++) { + rc_mode = &vaapi_encode_rc_modes[i]; + if (supported_va_rc_modes & rc_mode->va_mode) { + res = snprintf(str, len, "%s%s", + first ? "" : ", ", rc_mode->name); + first = 0; + if (res < 0) { + *str = 0; + break; + } + len -= res; + str += res; + if (len == 0) + break; } - return 0; - } else { - av_log(avctx, AV_LOG_ERROR, "Driver does not support " - "constant-quality mode (%#x).\n", rc_attr.value); - return AVERROR(EINVAL); } - } - if (!(rc_attr.value & (VA_RC_CBR | VA_RC_VBR))) { - av_log(avctx, AV_LOG_ERROR, "Driver does not support any " - "bitrate-targetted rate control modes.\n"); - return AVERROR(EINVAL); - } - - if (avctx->rc_buffer_size) - hrd_buffer_size = avctx->rc_buffer_size; - else if (avctx->rc_max_rate > 0) - hrd_buffer_size = avctx->rc_max_rate; - else - hrd_buffer_size = avctx->bit_rate; - if (avctx->rc_initial_buffer_occupancy) { - if (avctx->rc_initial_buffer_occupancy > hrd_buffer_size) { - av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: " - "must have initial buffer size (%d) < " - "buffer size (%"PRId64").\n", - avctx->rc_initial_buffer_occupancy, hrd_buffer_size); - return AVERROR(EINVAL); + av_log(avctx, AV_LOG_DEBUG, "Driver supports RC modes %s.\n", + supported_rc_modes_string); + } + + // Rate control mode selection: + // * If the user has set a mode explicitly with the rc_mode option, + // use it and fail if it is not available. + // * If an explicit QP option has been set, use CQP. + // * If the codec is CQ-only, use CQP. + // * If the QSCALE avcodec option is set, use CQP. + // * If bitrate and quality are both set, try QVBR. + // * If quality is set, try ICQ, then CQP. + // * If bitrate and maxrate are set and have the same value, try CBR. + // * If a bitrate is set, try AVBR, then VBR, then CBR. + // * If no bitrate is set, try ICQ, then CQP. + +#define TRY_RC_MODE(mode, fail) do { \ + rc_mode = &vaapi_encode_rc_modes[mode]; \ + if (!(rc_mode->va_mode & supported_va_rc_modes)) { \ + if (fail) { \ + av_log(avctx, AV_LOG_ERROR, "Driver does not support %s " \ + "RC mode (supported modes: %s).\n", rc_mode->name, \ + supported_rc_modes_string); \ + return AVERROR(EINVAL); \ + } \ + av_log(avctx, AV_LOG_DEBUG, "Driver does not support %s " \ + "RC mode.\n", rc_mode->name); \ + rc_mode = NULL; \ + } else { \ + goto rc_mode_found; \ + } \ + } while (0) + + if (ctx->explicit_rc_mode) + TRY_RC_MODE(ctx->explicit_rc_mode, 1); + + if (ctx->explicit_qp) + TRY_RC_MODE(RC_MODE_CQP, 1); + + if (ctx->codec->flags & FLAG_CONSTANT_QUALITY_ONLY) + TRY_RC_MODE(RC_MODE_CQP, 1); + + if (avctx->flags & AV_CODEC_FLAG_QSCALE) + TRY_RC_MODE(RC_MODE_CQP, 1); + + if (avctx->bit_rate > 0 && avctx->global_quality > 0) + TRY_RC_MODE(RC_MODE_QVBR, 0); + + if (avctx->global_quality > 0) { + TRY_RC_MODE(RC_MODE_ICQ, 0); + TRY_RC_MODE(RC_MODE_CQP, 0); + } + + if (avctx->bit_rate > 0 && avctx->rc_max_rate == avctx->bit_rate) + TRY_RC_MODE(RC_MODE_CBR, 0); + + if (avctx->bit_rate > 0) { + TRY_RC_MODE(RC_MODE_AVBR, 0); + TRY_RC_MODE(RC_MODE_VBR, 0); + TRY_RC_MODE(RC_MODE_CBR, 0); + } else { + TRY_RC_MODE(RC_MODE_ICQ, 0); + TRY_RC_MODE(RC_MODE_CQP, 0); + } + + av_log(avctx, AV_LOG_ERROR, "Driver does not support any " + "RC mode compatible with selected options " + "(supported modes: %s).\n", supported_rc_modes_string); + return AVERROR(EINVAL); + +rc_mode_found: + if (rc_mode->bitrate) { + if (rc_mode->maxrate) { + if (avctx->rc_max_rate > 0) { + if (avctx->rc_max_rate < avctx->bit_rate) { + av_log(avctx, AV_LOG_ERROR, "Invalid bitrate settings: " + "bitrate (%"PRId64") must not be greater than " + "maxrate (%"PRId64").\n", avctx->bit_rate, + avctx->rc_max_rate); + return AVERROR(EINVAL); + } + rc_bits_per_second = avctx->rc_max_rate; + rc_target_percentage = (avctx->bit_rate * 100) / + avctx->rc_max_rate; + } else { + // We only have a target bitrate, but this mode requires + // that a maximum rate be supplied as well. Since the + // user does not want this to be a constraint, arbitrarily + // pick a maximum rate of double the target rate. + rc_bits_per_second = 2 * avctx->bit_rate; + rc_target_percentage = 50; + } + } else { + if (avctx->rc_max_rate > avctx->bit_rate) { + av_log(avctx, AV_LOG_WARNING, "Max bitrate is ignored " + "in %s RC mode.\n", rc_mode->name); + } + rc_bits_per_second = avctx->bit_rate; + rc_target_percentage = 100; } - hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy; } else { - hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4; - } - - if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) { - av_log(avctx, AV_LOG_ERROR, "Invalid bitrate settings: must have " - "bitrate (%"PRId64") <= maxrate (%"PRId64").\n", - avctx->bit_rate, avctx->rc_max_rate); - return AVERROR(EINVAL); + rc_bits_per_second = 0; + rc_target_percentage = 100; } - if (avctx->rc_max_rate > avctx->bit_rate) { - if (!(rc_attr.value & VA_RC_VBR)) { - av_log(avctx, AV_LOG_WARNING, "Driver does not support " - "VBR mode (%#x), using CBR mode instead.\n", - rc_attr.value); - ctx->va_rc_mode = VA_RC_CBR; - - rc_bits_per_second = avctx->bit_rate; - rc_target_percentage = 100; + if (rc_mode->quality) { + if (ctx->explicit_qp) { + rc_quality = ctx->explicit_qp; + } else if (avctx->global_quality > 0) { + rc_quality = avctx->global_quality; } else { - ctx->va_rc_mode = VA_RC_VBR; - - rc_bits_per_second = avctx->rc_max_rate; - rc_target_percentage = (avctx->bit_rate * 100) / - avctx->rc_max_rate; + rc_quality = ctx->codec->default_quality; + av_log(avctx, AV_LOG_WARNING, "No quality level set; " + "using default (%d).\n", rc_quality); } + } else { + rc_quality = 0; + } - } else if (avctx->rc_max_rate == avctx->bit_rate) { - if (!(rc_attr.value & VA_RC_CBR)) { - av_log(avctx, AV_LOG_WARNING, "Driver does not support " - "CBR mode (%#x), using VBR mode instead.\n", - rc_attr.value); - ctx->va_rc_mode = VA_RC_VBR; + if (rc_mode->hrd) { + if (avctx->rc_buffer_size) + hrd_buffer_size = avctx->rc_buffer_size; + else if (avctx->rc_max_rate > 0) + hrd_buffer_size = avctx->rc_max_rate; + else + hrd_buffer_size = avctx->bit_rate; + if (avctx->rc_initial_buffer_occupancy) { + if (avctx->rc_initial_buffer_occupancy > hrd_buffer_size) { + av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: " + "must have initial buffer size (%d) <= " + "buffer size (%"PRId64").\n", + avctx->rc_initial_buffer_occupancy, hrd_buffer_size); + return AVERROR(EINVAL); + } + hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy; } else { - ctx->va_rc_mode = VA_RC_CBR; + hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4; } - rc_bits_per_second = avctx->bit_rate; - rc_target_percentage = 100; - + rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second; } else { - if (rc_attr.value & VA_RC_VBR) { - ctx->va_rc_mode = VA_RC_VBR; - - // We only have a target bitrate, but VAAPI requires that a - // maximum rate be supplied as well. Since the user has - // offered no particular constraint, arbitrarily pick a - // maximum rate of double the target rate. - rc_bits_per_second = 2 * avctx->bit_rate; - rc_target_percentage = 50; - } else { - ctx->va_rc_mode = VA_RC_CBR; - - rc_bits_per_second = avctx->bit_rate; - rc_target_percentage = 100; + if (avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) { + av_log(avctx, AV_LOG_WARNING, "Buffering settings are ignored " + "in %s RC mode.\n", rc_mode->name); } - } - rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second; + hrd_buffer_size = 0; + hrd_initial_buffer_fullness = 0; - av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s, %d%% of %"PRId64" bps " - "over %d ms.\n", ctx->va_rc_mode == VA_RC_VBR ? "VBR" : "CBR", - rc_target_percentage, rc_bits_per_second, rc_window_size); - av_log(avctx, AV_LOG_VERBOSE, "RC buffer: %"PRId64" bits, " - "initial fullness %"PRId64" bits.\n", - hrd_buffer_size, hrd_initial_buffer_fullness); + rc_window_size = 1000; + } if (rc_bits_per_second > UINT32_MAX || hrd_buffer_size > UINT32_MAX || @@ -1425,36 +1518,59 @@ static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx) return AVERROR(EINVAL); } + ctx->rc_mode = rc_mode; + ctx->rc_quality = rc_quality; + ctx->va_rc_mode = rc_mode->va_mode; ctx->va_bit_rate = rc_bits_per_second; + av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s.\n", rc_mode->name); ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) { .type = VAConfigAttribRateControl, .value = ctx->va_rc_mode, }; - ctx->rc_params.misc.type = VAEncMiscParameterTypeRateControl; - ctx->rc_params.rc = (VAEncMiscParameterRateControl) { - .bits_per_second = rc_bits_per_second, - .target_percentage = rc_target_percentage, - .window_size = rc_window_size, - .initial_qp = 0, - .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0), - .basic_unit_size = 0, + if (rc_mode->quality) + av_log(avctx, AV_LOG_VERBOSE, "RC quality: %d.\n", rc_quality); + + if (rc_mode->bitrate) { + av_log(avctx, AV_LOG_VERBOSE, "RC target: %d%% of " + "%"PRId64" bps over %d ms.\n", rc_target_percentage, + rc_bits_per_second, rc_window_size); + + ctx->rc_params.misc.type = VAEncMiscParameterTypeRateControl; + ctx->rc_params.rc = (VAEncMiscParameterRateControl) { + .bits_per_second = rc_bits_per_second, + .target_percentage = rc_target_percentage, + .window_size = rc_window_size, + .initial_qp = 0, + .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0), + .basic_unit_size = 0, #if VA_CHECK_VERSION(1, 1, 0) - .max_qp = (avctx->qmax > 0 ? avctx->qmax : 0), + .ICQ_quality_factor = rc_quality, + .max_qp = (avctx->qmax > 0 ? avctx->qmax : 0), #endif - }; - vaapi_encode_add_global_param(avctx, &ctx->rc_params.misc, - sizeof(ctx->rc_params)); +#if VA_CHECK_VERSION(1, 3, 0) + .quality_factor = rc_quality, +#endif + }; + vaapi_encode_add_global_param(avctx, &ctx->rc_params.misc, + sizeof(ctx->rc_params)); + } - ctx->hrd_params.misc.type = VAEncMiscParameterTypeHRD; - ctx->hrd_params.hrd = (VAEncMiscParameterHRD) { - .initial_buffer_fullness = hrd_initial_buffer_fullness, - .buffer_size = hrd_buffer_size, - }; - vaapi_encode_add_global_param(avctx, &ctx->hrd_params.misc, - sizeof(ctx->hrd_params)); + if (rc_mode->hrd) { + av_log(avctx, AV_LOG_VERBOSE, "RC buffer: %"PRId64" bits, " + "initial fullness %"PRId64" bits.\n", + hrd_buffer_size, hrd_initial_buffer_fullness); + + ctx->hrd_params.misc.type = VAEncMiscParameterTypeHRD; + ctx->hrd_params.hrd = (VAEncMiscParameterHRD) { + .initial_buffer_fullness = hrd_initial_buffer_fullness, + .buffer_size = hrd_buffer_size, + }; + vaapi_encode_add_global_param(avctx, &ctx->hrd_params.misc, + sizeof(ctx->hrd_params)); + } if (avctx->framerate.num > 0 && avctx->framerate.den > 0) av_reduce(&fr_num, &fr_den, @@ -1463,6 +1579,9 @@ static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx) av_reduce(&fr_num, &fr_den, avctx->time_base.den, avctx->time_base.num, 65535); + av_log(avctx, AV_LOG_VERBOSE, "RC framerate: %d/%d (%.2f fps).\n", + fr_num, fr_den, (double)fr_num / fr_den); + ctx->fr_params.misc.type = VAEncMiscParameterTypeFrameRate; ctx->fr_params.fr.framerate = (unsigned int)fr_den << 16 | fr_num; diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index a4206865ea..123a46efbb 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -49,6 +49,7 @@ enum { PICTURE_TYPE_I = 1, PICTURE_TYPE_P = 2, PICTURE_TYPE_B = 3, + NB_PICTURE_TYPES = 4, }; typedef struct VAAPIEncodeSlice { @@ -129,6 +130,34 @@ typedef struct VAAPIEncodeProfile { VAProfile va_profile; } VAAPIEncodeProfile; +enum { + RC_MODE_AUTO, + RC_MODE_CQP, + RC_MODE_CBR, + RC_MODE_VBR, + RC_MODE_ICQ, + RC_MODE_QVBR, + RC_MODE_AVBR, + RC_MODE_MAX = RC_MODE_AVBR, +}; + +typedef struct VAAPIEncodeRCMode { + // Name. + const char *name; + // Supported in the compile-time VAAPI version. + int supported; + // VA mode value (VA_RC_*). + uint32_t va_mode; + // Uses bitrate parameters. + int bitrate; + // Supports maxrate distinct from bitrate. + int maxrate; + // Uses quality value. + int quality; + // Supports HRD/VBV parameters. + int hrd; +} VAAPIEncodeRCMode; + typedef struct VAAPIEncodeContext { const AVClass *class; @@ -146,6 +175,14 @@ typedef struct VAAPIEncodeContext { // Desired B frame reference depth. int desired_b_depth; + // Explicitly set RC mode (otherwise attempt to pick from + // available modes). + int explicit_rc_mode; + + // Explicitly-set QP, for use with the "qp" options. + // (Forces CQP mode when set, overriding everything else.) + int explicit_qp; + // Desired packed headers. unsigned int desired_packed_headers; @@ -165,6 +202,12 @@ typedef struct VAAPIEncodeContext { // Chosen encoding profile details. const VAAPIEncodeProfile *profile; + // Chosen rate control mode details. + const VAAPIEncodeRCMode *rc_mode; + // RC quality level - meaning depends on codec and RC mode. + // In CQP mode this sets the fixed quantiser value. + int rc_quality; + // Encoding profile (VAProfile*). VAProfile va_profile; // Encoding entrypoint (VAEntryoint*). @@ -296,6 +339,10 @@ typedef struct VAAPIEncodeType { // Codec feature flags. int flags; + // Default quality for this codec - used as quantiser or RC quality + // factor depending on RC mode. + int default_quality; + // Perform any extra codec-specific configuration after the // codec context is initialised (set up the private data and // add any necessary global parameters). @@ -382,5 +429,22 @@ int ff_vaapi_encode_close(AVCodecContext *avctx); OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \ { .i64 = 1 }, 1, INT_MAX, FLAGS } +#define VAAPI_ENCODE_RC_MODE(name, desc) \ + { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \ + 0, 0, FLAGS, "rc_mode" } +#define VAAPI_ENCODE_RC_OPTIONS \ + { "rc_mode",\ + "Set rate control mode", \ + OFFSET(common.explicit_rc_mode), AV_OPT_TYPE_INT, \ + { .i64 = RC_MODE_AUTO }, RC_MODE_AUTO, RC_MODE_MAX, FLAGS, "rc_mode" }, \ + { "auto", "Choose mode automatically based on other parameters", \ + 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_AUTO }, 0, 0, FLAGS, "rc_mode" }, \ + VAAPI_ENCODE_RC_MODE(CQP, "Constant-quality"), \ + VAAPI_ENCODE_RC_MODE(CBR, "Constant-bitrate"), \ + VAAPI_ENCODE_RC_MODE(VBR, "Variable-bitrate"), \ + VAAPI_ENCODE_RC_MODE(ICQ, "Intelligent constant-quality"), \ + VAAPI_ENCODE_RC_MODE(QVBR, "Quality-defined variable-bitrate"), \ + VAAPI_ENCODE_RC_MODE(AVBR, "Average variable-bitrate") + #endif /* AVCODEC_VAAPI_ENCODE_H */