From patchwork Mon Mar 8 02:43:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wenbin Chen X-Patchwork-Id: 26243 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 13BB044B9CF for ; Mon, 8 Mar 2021 04:45:26 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DFE22689246; Mon, 8 Mar 2021 04:45:25 +0200 (EET) 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 D3621680ADB for ; Mon, 8 Mar 2021 04:45:18 +0200 (EET) IronPort-SDR: uVR1lyYih/iB6D01Iqxei40eE+9Qn2ddh9KdHblFwloX2rV4tMxn8ysL9cQc5HzNhvxWzNfdCK KctVt0gJebWQ== X-IronPort-AV: E=McAfee;i="6000,8403,9916"; a="167220728" X-IronPort-AV: E=Sophos;i="5.81,231,1610438400"; d="scan'208";a="167220728" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Mar 2021 18:45:16 -0800 IronPort-SDR: OrIsJ/apfiz50R7QUMq7YnALlRCiThTOWIqB+6QzJu38Yq3JrYQNJc9Mkp4tsDCZPuBFde51Aa sHM+Bz7vUR4A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,231,1610438400"; d="scan'208";a="601976937" Received: from unknown (HELO chenwenbin-Z390-AORUS-ULTRA.sh.intel.com) ([10.239.35.3]) by fmsmga005.fm.intel.com with ESMTP; 07 Mar 2021 18:45:15 -0800 From: wenbin.chen@intel.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Mar 2021 10:43:29 +0800 Message-Id: <20210308024329.11396-1-wenbin.chen@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavcodec/vaapi_encode: Change libva call to async way 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: "Chen,Wenbin" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: "Chen,Wenbin" Fix: #7706. After commit 5fdcf85bbffe7451c2, vaapi encoder's performance drop 20~30%. One reason is that vaRenderPicture() and vaSyncSurface() are called at the same time (vaRenderPicture() always followed by a vaSyncSurface()). Now I changed them to be called in a asynchronous way, which will make better use of hardware. Another reason of performance drop is that in old version, ffmpeg-vaapi use CQP as default while the ffmpeg-vaapi of lastest version does not, so the same command line will have a even bigger performance gap. To test this patch you'd better specify the bitrate (-b:v XXX). Signed-off-by: Wenbin CHEN --- libavcodec/vaapi_encode.c | 42 +++++++++++++++++++++++++++++---------- libavcodec/vaapi_encode.h | 3 +++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 518e5b2c00..851dea3fc2 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -950,8 +950,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) { @@ -1176,20 +1178,34 @@ 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 (av_fifo_size(ctx->encode_fifo) == 0) { + while (!err) { + pic = NULL; + err = vaapi_encode_pick_next(avctx, &pic); + if (err == AVERROR(EAGAIN)) + break; + else 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; + } + + av_fifo_generic_write(ctx->encode_fifo, &pic, sizeof(pic), NULL); + } } + if (av_fifo_size(ctx->encode_fifo) == 0) + return err; + + av_fifo_generic_read(ctx->encode_fifo, &pic, sizeof(pic), NULL); + ctx->encode_order = pic->encode_order+1; + err = vaapi_encode_output(avctx, pic, pkt); if (err < 0) { av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err); @@ -2519,6 +2535,10 @@ av_cold int ff_vaapi_encode_init(AVCodecContext *avctx) } } + ctx->encode_fifo = av_fifo_alloc((MAX_PICTURE_REFERENCES+1)* sizeof(VAAPIEncodePicture *)); + if (!ctx->encode_fifo) + return AVERROR(ENOMEM); + return 0; fail: diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index b41604a883..89fe8de466 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,8 @@ typedef struct VAAPIEncodeContext { int roi_warned; AVFrame *frame; + + AVFifoBuffer *encode_fifo; } VAAPIEncodeContext; enum {