From patchwork Fri Jan 11 12:09:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhong Li X-Patchwork-Id: 11707 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 C772744D920 for ; Fri, 11 Jan 2019 14:10:14 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D93EA68A806; Fri, 11 Jan 2019 14:10:02 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CEB6068A28A for ; Fri, 11 Jan 2019 14:09:55 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Jan 2019 04:10:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,465,1539673200"; d="scan'208";a="126873294" Received: from media_ffmpeg_skl_e3.sh.intel.com ([10.239.159.38]) by orsmga001.jf.intel.com with ESMTP; 11 Jan 2019 04:10:05 -0800 From: Zhong Li To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Jan 2019 20:09:38 +0800 Message-Id: <1547208578-26642-1-git-send-email-zhong.li@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH] lavc/qsvenc: set BRCParamMultiplier to aviod BRC overflow 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: Zhong Li MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fix ticket #7663 Signed-off-by: Zhong Li --- libavcodec/qsvenc.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index e3b5a72..0e8229e 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -158,8 +158,8 @@ static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, #endif ) { av_log(avctx, AV_LOG_VERBOSE, - "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"\n", - info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps); + "BufferSizeInKB: %"PRIu16"; InitialDelayInKB: %"PRIu16"; TargetKbps: %"PRIu16"; MaxKbps: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n", + info->BufferSizeInKB, info->InitialDelayInKB, info->TargetKbps, info->MaxKbps, info->BRCParamMultiplier); } else if (info->RateControlMethod == MFX_RATECONTROL_CQP) { av_log(avctx, AV_LOG_VERBOSE, "QPI: %"PRIu16"; QPP: %"PRIu16"; QPB: %"PRIu16"\n", info->QPI, info->QPP, info->QPB); @@ -167,8 +167,8 @@ static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, #if QSV_HAVE_AVBR else if (info->RateControlMethod == MFX_RATECONTROL_AVBR) { av_log(avctx, AV_LOG_VERBOSE, - "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"\n", - info->TargetKbps, info->Accuracy, info->Convergence); + "TargetKbps: %"PRIu16"; Accuracy: %"PRIu16"; Convergence: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n", + info->TargetKbps, info->Accuracy, info->Convergence, info->BRCParamMultiplier); } #endif #if QSV_HAVE_LA @@ -178,8 +178,8 @@ static void dump_video_param(AVCodecContext *avctx, QSVEncContext *q, #endif ) { av_log(avctx, AV_LOG_VERBOSE, - "TargetKbps: %"PRIu16"; LookAheadDepth: %"PRIu16"\n", - info->TargetKbps, co2->LookAheadDepth); + "TargetKbps: %"PRIu16"; LookAheadDepth: %"PRIu16"; BRCParamMultiplier: %"PRIu16"\n", + info->TargetKbps, co2->LookAheadDepth, info->BRCParamMultiplier); } #endif #if QSV_HAVE_ICQ @@ -451,6 +451,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) avctx->sw_pix_fmt : avctx->pix_fmt; const AVPixFmtDescriptor *desc; float quant; + int target_bitrate_kbps, max_bitrate_kbps, brc_param_multiplier; int ret; mfxVersion ver; @@ -552,16 +553,22 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) if (ret < 0) return ret; + //libmfx BRC parameters are 16 bits thus maybe overflow, then BRCParamMultiplier is needed + target_bitrate_kbps = avctx->bit_rate / 1000; + max_bitrate_kbps = avctx->rc_max_rate / 1000; + brc_param_multiplier = (FFMAX(target_bitrate_kbps, max_bitrate_kbps) + 0x10000) / 0x10000; + switch (q->param.mfx.RateControlMethod) { case MFX_RATECONTROL_CBR: case MFX_RATECONTROL_VBR: #if QSV_HAVE_VCM case MFX_RATECONTROL_VCM: #endif - q->param.mfx.BufferSizeInKB = avctx->rc_buffer_size / 8000; - q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000; - q->param.mfx.TargetKbps = avctx->bit_rate / 1000; - q->param.mfx.MaxKbps = avctx->rc_max_rate / 1000; + q->param.mfx.BufferSizeInKB = avctx->rc_buffer_size / 8000 / brc_param_multiplier; + q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000 / brc_param_multiplier; + q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier; + q->param.mfx.MaxKbps = max_bitrate_kbps / brc_param_multiplier; + q->param.mfx.BRCParamMultiplier = brc_param_multiplier; break; case MFX_RATECONTROL_CQP: quant = avctx->global_quality / FF_QP2LAMBDA; @@ -573,15 +580,17 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) break; #if QSV_HAVE_AVBR case MFX_RATECONTROL_AVBR: - q->param.mfx.TargetKbps = avctx->bit_rate / 1000; + q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier; q->param.mfx.Convergence = q->avbr_convergence; q->param.mfx.Accuracy = q->avbr_accuracy; + q->param.mfx.BRCParamMultiplier = brc_param_multiplier; break; #endif #if QSV_HAVE_LA case MFX_RATECONTROL_LA: - q->param.mfx.TargetKbps = avctx->bit_rate / 1000; + q->param.mfx.TargetKbps = target_bitrate_kbps / brc_param_multiplier; q->extco2.LookAheadDepth = q->look_ahead_depth; + q->param.mfx.BRCParamMultiplier = brc_param_multiplier; break; #if QSV_HAVE_ICQ case MFX_RATECONTROL_LA_ICQ: @@ -726,7 +735,7 @@ static int qsv_retrieve_enc_jpeg_params(AVCodecContext *avctx, QSVEncContext *q) return ff_qsv_print_error(avctx, ret, "Error calling GetVideoParam"); - q->packet_size = q->param.mfx.BufferSizeInKB * 1000; + q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000; // for qsv mjpeg the return value maybe 0 so alloc the buffer if (q->packet_size == 0) @@ -779,7 +788,7 @@ static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q) return ff_qsv_print_error(avctx, ret, "Error calling GetVideoParam"); - q->packet_size = q->param.mfx.BufferSizeInKB * 1000; + q->packet_size = q->param.mfx.BufferSizeInKB * q->param.mfx.BRCParamMultiplier * 1000; if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) { av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");