From patchwork Mon Jul 15 10:59:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 13949 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 EC59044A0BA for ; Mon, 15 Jul 2019 13:59:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8C3468AAAB; Mon, 15 Jul 2019 13:59:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C874468A4E8 for ; Mon, 15 Jul 2019 13:59:51 +0300 (EEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Jul 2019 03:59:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,493,1557212400"; d="scan'208";a="172198309" Received: from media_lj_kbl.sh.intel.com ([10.239.13.115]) by orsmga006.jf.intel.com with ESMTP; 15 Jul 2019 03:59:48 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Mon, 15 Jul 2019 18:59:36 +0800 Message-Id: <20190715105936.4860-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 maxframesize 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" Add support for max frame size: - max_frame_size (bytes) to indicate the max allowed size for frame. If the frame size exceeds the limitation, encoder will to control the frame size by adjusting QP value. - MFS_NUM_PASSES to indicate number of passes for QP adjust. - MFS_DELTA_QP to indicate adjust qp value per pass. To simplify the usage, default QP adjust is set to delta_qp[4] = {1, 1, 1, 1}. Use new_qp for encoder if frame size exceeds the limitation: new_qp = base_qp + delta_qp[0] + delta_qp[1] + ... ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -f rawvideo \ -v verbose -s:v 352x288 -i ./input.yuv -vf format=nv12,hwupload \ -c:v h264_vaapi -profile:v main -g 30 -bf 3 -max_frame_size 40000 \ -vframes 100 -y ./max_frame_size.h264 Max frame size was enabled since VA-API version (1, 3, 0), but query is available since (1, 5, 0). It will be passed as a parameter in picParam and should be set for each frame. Signed-off-by: Linjie Fu --- Since the Attribute query for maxframesize is merged in media driver, it's more robust now for vaapi to make use of this feature. https://github.com/intel/media-driver/pull/656 libavcodec/vaapi_encode.c | 68 ++++++++++++++++++++++++++++++++++++++- libavcodec/vaapi_encode.h | 19 +++++++++-- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index dd2a24de04..fdc1cec178 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -258,7 +258,16 @@ static int vaapi_encode_issue(AVCodecContext *avctx, if (err < 0) goto fail; } - +#if VA_CHECK_VERSION(1, 5, 0) + if (ctx->max_frame_size) { + err = vaapi_encode_make_misc_param_buffer(avctx, pic, + VAEncMiscParameterTypeMultiPassFrameSize, + &ctx->mfs_params, + sizeof(ctx->mfs_params)); + if (err < 0) + goto fail; + } +#endif if (pic->type == PICTURE_TYPE_IDR) { if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE && ctx->codec->write_sequence_header) { @@ -1671,6 +1680,54 @@ rc_mode_found: return 0; } +static av_cold int vaapi_encode_init_max_frame_size(AVCodecContext *avctx) +{ +#if VA_CHECK_VERSION(1, 5, 0) + VAAPIEncodeContext *ctx = avctx->priv_data; + VAConfigAttrib attr = { VAConfigAttribMaxFrameSize }; + VAStatus vas; + int i; + + vas = vaGetConfigAttributes(ctx->hwctx->display, + ctx->va_profile, + ctx->va_entrypoint, + &attr, 1); + if (vas != VA_STATUS_SUCCESS) { + ctx->max_frame_size = 0; + av_log(avctx, AV_LOG_ERROR, "Failed to query max frame size " + "config attribute: %d (%s).\n", vas, vaErrorStr(vas)); + return AVERROR_EXTERNAL; + } + + if (attr.value == VA_ATTRIB_NOT_SUPPORTED) { + ctx->max_frame_size = 0; + av_log(avctx, AV_LOG_WARNING, "Max frame size attribute " + "is not supported.\n"); + } else { + ctx->delta_qp = av_mallocz_array(MFS_NUM_PASSES, sizeof(uint8_t)); + if (!ctx->delta_qp) { + return AVERROR(ENOMEM); + } + for (i = 0; i delta_qp[i] = MFS_DELTA_QP; + + ctx->mfs_params = (VAEncMiscParameterBufferMultiPassFrameSize){ + .max_frame_size = ctx->max_frame_size, + .num_passes = MFS_NUM_PASSES, + .delta_qp = ctx->delta_qp, + }; + + av_log(avctx, AV_LOG_VERBOSE, "Max Frame Size: %d bytes.\n ", + ctx->max_frame_size); + } +#else + av_log(avctx, AV_LOG_WARNING, "Max Frame Size is " + "not supported with this VA version.\n"); +#endif + + return 0; +} + static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; @@ -2138,6 +2195,12 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) goto fail; } + if (ctx->max_frame_size) { + err = vaapi_encode_init_max_frame_size(avctx); + if (err < 0) + goto fail; + } + vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile, ctx->va_entrypoint, ctx->config_attributes, ctx->nb_config_attributes, @@ -2262,6 +2325,9 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) ctx->va_config = VA_INVALID_ID; } + if (ctx->delta_qp) + av_freep(&ctx->delta_qp); + av_freep(&ctx->codec_sequence_params); av_freep(&ctx->codec_picture_params); diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index eeec06036b..05b35608c2 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -51,6 +51,11 @@ enum { PICTURE_TYPE_B = 3, }; +enum { + MFS_NUM_PASSES = 4, + MFS_DELTA_QP = 1, +}; + typedef struct VAAPIEncodeSlice { int index; int row_start; @@ -176,6 +181,9 @@ typedef struct VAAPIEncodeContext { // Desired B frame reference depth. int desired_b_depth; + // Max Frame Size + int max_frame_size; + // Explicitly set RC mode (otherwise attempt to pick from // available modes). int explicit_rc_mode; @@ -256,7 +264,9 @@ typedef struct VAAPIEncodeContext { #if VA_CHECK_VERSION(0, 36, 0) VAEncMiscParameterBufferQualityLevel quality_params; #endif - +#if VA_CHECK_VERSION(1, 3, 0) + VAEncMiscParameterBufferMultiPassFrameSize mfs_params; +#endif // Per-sequence parameter structure (VAEncSequenceParameterBuffer*). void *codec_sequence_params; @@ -303,6 +313,7 @@ typedef struct VAAPIEncodeContext { int idr_counter; int gop_counter; int end_of_stream; + uint8_t *delta_qp; // The encoder does not support cropping information, so warn about // it the first time we encounter any nonzero crop fields. @@ -418,7 +429,11 @@ 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 }, \ + { "max_frame_size", \ + "Maximum frame size (in bytes)",\ + OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \ + { .i64 = 0 }, 0, INT_MAX, FLAGS } #define VAAPI_ENCODE_RC_MODE(name, desc) \ { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \