From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id CA69640D90 for ; Tue, 8 Feb 2022 03:06:16 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CEB6568B16A; Tue, 8 Feb 2022 05:06:02 +0200 (EET) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F281D68AE13 for ; Tue, 8 Feb 2022 05:05:55 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644289561; x=1675825561; h=from:to:subject:date:message-id:in-reply-to:references: mime-version:content-transfer-encoding; bh=5hSoa3nl8Elgb09nf3PNqO/X0BcGgr5aNWUF7RkJ+Cw=; b=LGrt9Crgk65yGLuj36Bv4dH5rKY1tVuSOhrql34tJ0cTDROAcMwfFQ2A zW3CO+IlBRhRxk/pioAu7Jou5njACj87NGRVub6cLAXnaJ15pwsTUwtrK WyoczVkPXgXAKB7J7u8x/nu++MEWyfYKCWwT1hgMVU9GvpWLPx191OGpa 2FhtZ3tEBHobfatEjtbIcDBD+sl1A/+OTvxqiD5ebQWp/Xv2+Zl69cVbi Xd6xHAVGmeMczMya3uLbPxBy9aOLAHjtKL0PmClfgNVgHjx6dALtnhN0A MWnTjhs2zZQYJddUQnn1IX+y4/OWdFg6bkEz/Kkw+AQJvwRTTQQ+RZwkt g==; X-IronPort-AV: E=McAfee;i="6200,9189,10251"; a="228829621" X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="228829621" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 19:05:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="628742459" Received: from wenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.110]) by fmsmga002.fm.intel.com with ESMTP; 07 Feb 2022 19:05:52 -0800 From: Wenbin Chen To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 11:05:48 +0800 Message-Id: <20220208030549.340748-2-wenbin.chen@intel.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220208030549.340748-1-wenbin.chen@intel.com> References: <20220208030549.340748-1-wenbin.chen@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH V3 2/3] libavcodec/vaapi_encode: Change the way to call async to increase performance X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Fix: #7706. After commit 5fdcf85bbffe7451c2, vaapi encoder's performance decrease. The reason is that vaRenderPicture() and vaSyncBuffer() are called at the same time (vaRenderPicture() always followed by a vaSyncBuffer()). When we encode stream with B frames, we need buffer to reorder frames, so we can send serveral frames to HW at once to increase performance. Now I changed them to be called in a asynchronous way, which will make better use of hardware. 1080p transcoding increases about 17% fps on my environment. This change fits vaSyncBuffer(), so if driver does not support vaSyncBuffer, it will keep previous operation. Signed-off-by: Wenbin Chen --- libavcodec/vaapi_encode.c | 64 ++++++++++++++++++++++++++++++++------- libavcodec/vaapi_encode.h | 5 +++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index b87b58a42b..15ddbbaa4a 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -984,8 +984,10 @@ static int vaapi_encode_pick_next(AVCodecContext *avctx, if (!pic && ctx->end_of_stream) { --b_counter; pic = ctx->pic_end; - if (pic->encode_issued) + if (pic->encode_complete) return AVERROR_EOF; + else if (pic->encode_issued) + return AVERROR(EAGAIN); } if (!pic) { @@ -1210,18 +1212,44 @@ int ff_vaapi_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt) return AVERROR(EAGAIN); } - pic = NULL; - err = vaapi_encode_pick_next(avctx, &pic); - if (err < 0) - return err; - av_assert0(pic); +#if VA_CHECK_VERSION(1, 9, 0) + if (ctx->has_sync_buffer_func) { + while (av_fifo_can_read(ctx->encode_fifo) <= MAX_PICTURE_REFERENCES) { + pic = NULL; + err = vaapi_encode_pick_next(avctx, &pic); + if (err < 0) + break; + + av_assert0(pic); + pic->encode_order = ctx->encode_order + + av_fifo_can_read(ctx->encode_fifo); + err = vaapi_encode_issue(avctx, pic); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err); + return err; + } + av_fifo_write(ctx->encode_fifo, &pic, 1); + } + if (!av_fifo_can_read(ctx->encode_fifo)) + return err; + av_fifo_read(ctx->encode_fifo, &pic, 1); + ctx->encode_order = pic->encode_order + 1; + } else +#endif + { + pic = NULL; + err = vaapi_encode_pick_next(avctx, &pic); + if (err < 0) + return err; + av_assert0(pic); - pic->encode_order = ctx->encode_order++; + pic->encode_order = ctx->encode_order++; - err = vaapi_encode_issue(avctx, pic); - if (err < 0) { - av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err); - return err; + err = vaapi_encode_issue(avctx, pic); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err); + return err; + } } err = vaapi_encode_output(avctx, pic, pkt); @@ -2555,6 +2583,19 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) } } +#if VA_CHECK_VERSION(1, 9, 0) + //check vaSyncBuffer function + vas = vaSyncBuffer(ctx->hwctx->display, 0, 0); + if (vas != VA_STATUS_ERROR_UNIMPLEMENTED) { + ctx->has_sync_buffer_func = 1; + ctx->encode_fifo = av_fifo_alloc2(MAX_PICTURE_REFERENCES + 1, + sizeof(VAAPIEncodePicture *), + 0); + if (!ctx->encode_fifo) + return AVERROR(ENOMEM); + } +#endif + return 0; fail: @@ -2592,6 +2633,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) av_freep(&ctx->codec_sequence_params); av_freep(&ctx->codec_picture_params); + av_fifo_freep2(&ctx->encode_fifo); av_buffer_unref(&ctx->recon_frames_ref); av_buffer_unref(&ctx->input_frames_ref); diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index b41604a883..d33a486cb8 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -29,6 +29,7 @@ #include "libavutil/hwcontext.h" #include "libavutil/hwcontext_vaapi.h" +#include "libavutil/fifo.h" #include "avcodec.h" #include "hwconfig.h" @@ -345,6 +346,10 @@ typedef struct VAAPIEncodeContext { int roi_warned; AVFrame *frame; + //Store buffered pic + AVFifo *encode_fifo; + //Whether the driver support vaSyncBuffer + int has_sync_buffer_func; } VAAPIEncodeContext; enum { -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".