From patchwork Mon Jun 17 13:47:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 13583 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 D10784471B3 for ; Mon, 17 Jun 2019 08:47:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA4AA689B2C; Mon, 17 Jun 2019 08:47:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 957C0680912 for ; Mon, 17 Jun 2019 08:47:45 +0300 (EEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Jun 2019 22:47:43 -0700 X-ExtLoop1: 1 Received: from media_lj_kbl.sh.intel.com ([10.239.13.115]) by fmsmga005.fm.intel.com with ESMTP; 16 Jun 2019 22:47:42 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Mon, 17 Jun 2019 21:47:28 +0800 Message-Id: <20190617134728.6036-1-linjie.fu@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v3, 1/2] lavc/vaapi_encode: add support for AVC Trellis 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 Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Trellis quantization is an algorithm that can improve data compression in DCT-based encoding methods. It reduces the size of some DCT coefficients while recovering others to take their place. Trellis quantization effectively finds the optimal quantization for each block to maximize the PSNR relative to bitrate. Add support for VAAPI AVC Trellis Quantization with limitation: - VA-API version >= (1, 5, 0) Use option "-trellis off/I/P/B" to disable or enable Trellis quantization for I/P/B frames. Signed-off-by: Linjie Fu --- libavcodec/vaapi_encode.c | 47 +++++++++++++++++++++++++++++++++++++++ libavcodec/vaapi_encode.h | 19 +++++++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index dd2a24de04..394c824069 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -1671,6 +1671,47 @@ rc_mode_found: return 0; } +static av_cold int vaapi_encode_init_quantization(AVCodecContext *avctx) +{ +#if VA_CHECK_VERSION(1, 5, 0) + VAAPIEncodeContext *ctx = avctx->priv_data; + VAStatus vas; + VAConfigAttrib attr = { VAConfigAttribEncQuantization }; + + vas = vaGetConfigAttributes(ctx->hwctx->display, + ctx->va_profile, + ctx->va_entrypoint, + &attr, 1); + if (vas != VA_STATUS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to query quantization " + "config attribute: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR_EXTERNAL; + } + + if (attr.value == VA_ATTRIB_NOT_SUPPORTED || + attr.value == VA_ENC_QUANTIZATION_NONE) { + av_log(avctx, AV_LOG_WARNING, "Special Quantization attribute is not " + "supported: will use default quantization.\n"); + } else if (attr.value == VA_ENC_QUANTIZATION_TRELLIS_SUPPORTED){ + av_log(avctx, AV_LOG_VERBOSE, "Quantization Trellis supported.\n"); + + ctx->quantization_params = (VAEncMiscParameterQuantization) { + .quantization_flags.value = ctx->trellis, + }; + + vaapi_encode_add_global_param(avctx, + VAEncMiscParameterTypeQuantization, + &ctx->quantization_params, + sizeof(ctx->quantization_params)); + } +#else + av_log(avctx, AV_LOG_WARNING, "The encode quantization option (Trellis) is " + "not supported with this VAAPI version.\n"); +#endif + + return 0; +} + static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; @@ -2132,6 +2173,12 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) if (err < 0) goto fail; + if (ctx->trellis) { + err = vaapi_encode_init_quantization(avctx); + if (err < 0) + goto fail; + } + if (avctx->compression_level >= 0) { err = vaapi_encode_init_quality(avctx); if (err < 0) diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index eeec06036b..9b7f29a2d2 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -37,7 +37,7 @@ struct VAAPIEncodePicture; enum { MAX_CONFIG_ATTRIBUTES = 4, - MAX_GLOBAL_PARAMS = 4, + MAX_GLOBAL_PARAMS = 5, MAX_DPB_SIZE = 16, MAX_PICTURE_REFERENCES = 2, MAX_REORDER_DELAY = 16, @@ -176,6 +176,9 @@ typedef struct VAAPIEncodeContext { // Desired B frame reference depth. int desired_b_depth; + // Quantization mode + int trellis; + // Explicitly set RC mode (otherwise attempt to pick from // available modes). int explicit_rc_mode; @@ -256,7 +259,9 @@ typedef struct VAAPIEncodeContext { #if VA_CHECK_VERSION(0, 36, 0) VAEncMiscParameterBufferQualityLevel quality_params; #endif - +#if VA_CHECK_VERSION(1, 5, 0) + VAEncMiscParameterQuantization quantization_params; +#endif // Per-sequence parameter structure (VAEncSequenceParameterBuffer*). void *codec_sequence_params; @@ -418,7 +423,15 @@ int ff_vaapi_encode_close(AVCodecContext *avctx); { "b_depth", \ "Maximum B-frame reference depth", \ OFFSET(common.desired_b_depth), AV_OPT_TYPE_INT, \ - { .i64 = 1 }, 1, INT_MAX, FLAGS } + { .i64 = 1 }, 1, INT_MAX, FLAGS }, \ + { "trellis", \ + "Trellis Quantization", \ + OFFSET(common.trellis), AV_OPT_TYPE_FLAGS, \ + { .i64 = 0}, 0, INT_MAX, FLAGS, "trellis"}, \ + { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "trellis"}, \ + { "I", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, FLAGS, "trellis"}, \ + { "P", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4 }, INT_MIN, INT_MAX, FLAGS, "trellis"}, \ + { "B", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 8 }, INT_MIN, INT_MAX, FLAGS, "trellis"} #define VAAPI_ENCODE_RC_MODE(name, desc) \ { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \