From patchwork Wed Oct 27 08:57:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wenbin Chen X-Patchwork-Id: 31234 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2084:0:0:0:0 with SMTP id a4csp2351516ioa; Wed, 27 Oct 2021 02:01:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9NOKlX9g2I13JgwW+Ukuyy8pO41GETknpMBX7v83QhIDvI1Yb+GC96EgqZGmHJUoPB0O1 X-Received: by 2002:a05:6402:35c5:: with SMTP id z5mr43545680edc.388.1635325289837; Wed, 27 Oct 2021 02:01:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635325289; cv=none; d=google.com; s=arc-20160816; b=pC/bdYNS+pJqwESVtUmCG3TP/7taKSOJQPbbZmEEpSX5/zFbbbm0B6WyJqyZm27l4X BE7I4/Q88gxzpt5f1wyq2tJ3eae34dFcYTMEgQrrJCbL7dhbIP7g0uKpE5u3qwX3KZTP QZZiJRm0KF58vd+tVEXaOHA2LdOBLW6LJ9OceeCyxQcHX7cw/iJcSSrH/h3lU4sl5J9G hxHG6QgvWG2BeCE8s1opeVOfjHDsI+Z0HY/Y0R8A0+eiwPkkUntiJqHujEVazIfZMtRA 3qjy5vhKRXRVnMMYwgh9WBWc42W1QHqJmY3keDIrtMJI7aWe8B09q88KBHgZt/IAe36M hIzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=14m0IYpqJPAvDAqpdopXP3RsVeCp1AXw08LfJDiXh+U=; b=RbIKJibxIFVmdrj5IDSVwId+Hsv/HShhppF4jw5WKskF3obotbY4+R2X36qnP4c2mo A7308oaTY5Te7FJrTNKnBMQ3x8wyntvESfKKacAV4cMY3QR5D5s0JwlVb5BAXdd8312D ri99aDMct67XKFmdzGM+zlhSU6LQCd3gJIfZ/hku47RzqAdUbyJKfiqHp14tEY31c0qU C+R2OMf07WOlEx/LQ77iML6UQN6hA3jUeAFVjTO+CXk4GA37o5iSXbX8axVp1RvTjko3 GmxuSH0SzrwtsQ24pojrQlQpn0ze2lTvVLcjdwvWUwdNBLiN56S0JxR4xMlR9mXv5yrv Xtiw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t19si3411203edd.74.2021.10.27.02.01.05; Wed, 27 Oct 2021 02:01:29 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 839F168A47F; Wed, 27 Oct 2021 12:01:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 711DB680C28 for ; Wed, 27 Oct 2021 12:00:54 +0300 (EEST) X-IronPort-AV: E=McAfee;i="6200,9189,10149"; a="228866459" X-IronPort-AV: E=Sophos;i="5.87,186,1631602800"; d="scan'208";a="228866459" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2021 02:00:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.87,186,1631602800"; d="scan'208";a="486577189" Received: from chenwenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.110]) by orsmga007.jf.intel.com with ESMTP; 27 Oct 2021 02:00:51 -0700 From: Wenbin Chen To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Oct 2021 16:57:03 +0800 Message-Id: <20211027085705.4114165-1-wenbin.chen@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/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 Cc: Wenbin Chen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ZpJiA2E0Wl5b Fix: #7706. After commit 5fdcf85bbffe7451c2, vaapi encoder's performance decrease. The reason is that vaRenderPicture() and vaSyncSurface() are called at the same time (vaRenderPicture() always followed by a vaSyncSurface()). 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. Signed-off-by: Wenbin Chen --- libavcodec/vaapi_encode.c | 41 ++++++++++++++++++++++++++++----------- libavcodec/vaapi_encode.h | 3 +++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index ec054ae701..5927849233 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -951,8 +951,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) { @@ -1177,20 +1179,31 @@ 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); + while (av_fifo_size(ctx->encode_fifo) <= MAX_PICTURE_REFERENCES * sizeof(VAAPIEncodePicture *)) { + pic = NULL; + err = vaapi_encode_pick_next(avctx, &pic); + if (err < 0) + break; + av_assert0(pic); - pic->encode_order = ctx->encode_order++; + pic->encode_order = ctx->encode_order + + (av_fifo_size(ctx->encode_fifo) / sizeof(VAAPIEncodePicture *)); - 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)) + 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); @@ -2520,6 +2533,11 @@ 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: @@ -2552,6 +2570,7 @@ av_cold int ff_vaapi_encode_close(AVCodecContext *avctx) av_freep(&ctx->codec_sequence_params); av_freep(&ctx->codec_picture_params); + av_fifo_freep(&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..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 {