From patchwork Wed Oct 18 20:36:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 44295 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2a09:b0:15d:8365:d4b8 with SMTP id e9csp139705pzh; Wed, 18 Oct 2023 13:36:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEM3n+OlZeyf6F8UDffKqKMlYQI685sgUOH2M2XoltKr05Ca3dhCDyR9lqE+PaIGOY4TaPq X-Received: by 2002:a17:906:eece:b0:9c7:4dab:20ea with SMTP id wu14-20020a170906eece00b009c74dab20eamr232747ejb.67.1697661381824; Wed, 18 Oct 2023 13:36:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697661381; cv=none; d=google.com; s=arc-20160816; b=0SR+D4l3q1rqa/DWAiEJo1tv0ThwhCQl2sWtdyn9veBEfFFIq8krglpPuCg4yczSgP Ewj47PowtyjkSAbbzOU1y3aYXFon+5wT/ygUmGipJ71ltuJ7NauPMiLP8Xbj1ho1/1GQ fJClrBMLlwVQ+lxfoYQMOTFybFHw/qD8RWSek5bfEQjuHe/EnUHR5mIdJROh2G+OUsNc J+PvEdxHX3dDHXoz9s5QS5hRhfaaUWGlQMCZlTo8GjE+FP6JBXKiTCNsSGeiV/yergVH k4cgWFy6h0eMlL08N35i65ZtTVyAUow2wlfaf1BIBY6CWagAfqIX2leIEHH/z3lDiAR8 Jgiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:in-reply-to:from:references:to:content-language :user-agent:mime-version:date:message-id:dkim-signature:delivered-to; bh=qXlKdBbJnkelwXN5d8jWf7wxZQsXEvhVUb6OCrcCJzM=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=jDDqJ9kEIatVvbHStTQY/ZYxaWX/aZ642i/ruTldlDYEFohR4nteXe4/qgoxK2ZjR+ UW/zmn6ZQAN+PHY3aKF5+RhcYkp778qrY9jC2/vYTuECg3cxdWP9SAHHAHowsk8dUV1o I9yAQK/ZGYjaVxtS9nT3nGHXgA+OI8Szm5cR01ncKTIkEhmzLlg9E1MDZAYc7RFa1Vg9 tvpwsJYeXq63Nk2sBtcaW3hmU7u/ePgzo8LoyVQvd+spllz+0mZf25qEhm2Xo5ik29ZP T7Sw/st2inyngWi1bm7WR08iMe/0uWo7/QVxsTFXcv51xU712qidFyGuBaJQQ8YsrB+o QdhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20230601.gappssmtp.com header.s=20230601 header.b=zvau46CZ; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t14-20020a170906a10e00b009b9ee6a33e2si1320580ejy.307.2023.10.18.13.36.21; Wed, 18 Oct 2023 13:36:21 -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; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20230601.gappssmtp.com header.s=20230601 header.b=zvau46CZ; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA7D968B349; Wed, 18 Oct 2023 23:36:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0CF0D68C92E for ; Wed, 18 Oct 2023 23:36:12 +0300 (EEST) Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-32d8c2c6dfdso6577513f8f.1 for ; Wed, 18 Oct 2023 13:36:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20230601.gappssmtp.com; s=20230601; t=1697661371; x=1698266171; darn=ffmpeg.org; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=eJiWSKLjqnRpD4yQ3jypQAm8worCyVUECRqM5XWi7P8=; b=zvau46CZIaBdGzqSPk84I4X3/dCt1v0xGI7NJ2AD3qaeQqHeGWWwAUorszyUjXG5EX 5l1ZYFCPQI2xy5GEcrlzw1+HkFLYtTRXALZoGyESBQFhsmUQszxYpP/BefsvOkaidDbQ M66gxGx+OUCTXfI+paAtnyH1qt4Tvm6u0cHT15d6oiaJCnSeG8ziuxHk7Sozxsw6ooWO NBT9jWmvD2vFwpZHIPu0RmtfbBqObvOv30W6uHSxExCTSuqdhqZQmYhx8Gc+IxbBjroe 8/Mp42np9thQNiBVhcIux3jLpjnvbUqjtdHPEOz+IGIU0AqLFJoZZ4vvG7qjr+rqe3U+ zhlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661371; x=1698266171; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eJiWSKLjqnRpD4yQ3jypQAm8worCyVUECRqM5XWi7P8=; b=n+yo9EesEMy2hjVwQHmQsoKrlFWlIensqVR6cDgcm6K+sCK7J0+D3QbABy0B3OHn2E b/OmPxGbhio2VkwES4MCqUV5eWorwBQKMY/DOIZfQrsGN4oid7P2ltQqkWc7Ppn1hwpS DUid9CR1PeopiGB9wRQ6XPbJZW/zMbqjUrprqljT2JlKvxQf2hL4l9fJANWa7X5f0mHO kXXF0fQX/qU/nrZ92SqD2NMK16/D5UZB/F3WzJeOyrVPXIvFHQtTsX7fI1ldPuQe628E 8DNDx90MTxrSpjdd99DiS4661rsNCme4xEp0HxDxFvVWXM3qJCMzbLjcC6pDfKTDATFg 1M4A== X-Gm-Message-State: AOJu0Yw9B9WdfRelLFLqiclBmTHK8bLfrSgxu5OLNWi3a3l0eO2FeXMR AgueIucggJ5pAgJ/JM05Lfu/FLllJN5H9ayC/RE= X-Received: by 2002:a5d:5191:0:b0:32d:a7c8:eaf7 with SMTP id k17-20020a5d5191000000b0032da7c8eaf7mr99619wrv.20.1697661371263; Wed, 18 Oct 2023 13:36:11 -0700 (PDT) Received: from [192.168.0.15] (cpc92320-cmbg19-2-0-cust383.5-4.cable.virginm.net. [82.13.65.128]) by smtp.gmail.com with ESMTPSA id i11-20020a5d630b000000b00327bf4f2f14sm2857144wru.88.2023.10.18.13.36.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Oct 2023 13:36:11 -0700 (PDT) Message-ID: Date: Wed, 18 Oct 2023 21:36:23 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <2315cfb6-7fed-4429-aebd-d958471898f9@jkqxz.net> From: Mark Thompson In-Reply-To: Subject: [FFmpeg-devel] [PATCH] amfenc: Use a blocking call instead of sleeping and polling 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: QusUywQXebhw --- On 17/10/2023 18:11, Evgeny Pavlov wrote: > The reason for using av_usleep() here is that AMF API doesn’t provide an > API for explicit wait. There are two modes to get output from encoder: > > 1. Polling with some sleep to avoid CPU thrashing – currently used in FFmpeg > > 2. Set timeout parameter on AMF encoder and QueryOutput call will block > till output is available or the timeout happens. > > #2 is the preferable way but it is designed more to be used with a separate > polling thread. With a single-thread approach in FFmpeg, the use of timeout > can block input submission making things slower. This is even more > pronounced when B-frames are enabled and several inputs are needed to produce > the first output. This approach seems like it should work here? Run non-blocking until the queue is full, then switch to blocking when you need to wait for some output. I tried the patch enclosing (H.264 only, different proprties needed for other codecs), but it doesn't seem to work - the test assert always hits immediately and timing shows that QueryOutput didn't block even though the timeout should be set? I'm probably doing something incorrect, maybe you would know how to fix it. > The condition of this sleep is in special events (primarily when amf input > queue is full), not the core loop part. During the experiments the cpu > increasing is about 2-4% or so, not a burst. What cases are you experimenting with? The most problematic case I can think of is multiple encodes running simultaneously sharing the same instance so that each one has to wait for others to complete and therefore all queues fill up. The busy wait will end up being the only place where it can block (since everything else runs asynchronously), so you will peg one CPU at close to 100% per encode running. Thanks, - Mark libavcodec/amfenc.c | 22 +++++++++++++++++++--- libavcodec/amfenc.h | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 061859f85c..db7ddbb083 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -713,13 +713,22 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) } } - + block_and_wait = 0; do { - block_and_wait = 0; // poll data if (!avpkt->data && !avpkt->buf) { + int64_t timeout = block_and_wait ? 100 : 0; + if (timeout != ctx->output_query_timeout) { + av_log(avctx, AV_LOG_INFO, "Set output query timeout to %"PRId64"\n", timeout); + AMF_ASSIGN_PROPERTY_INT64(res, ctx->encoder, AMF_VIDEO_ENCODER_QUERY_TIMEOUT, timeout); + AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Failed to set output query timeout\n"); + ctx->output_query_timeout = timeout; + } + res_query = ctx->encoder->pVtbl->QueryOutput(ctx->encoder, &data); if (data) { + av_log(avctx, AV_LOG_INFO, "QueryOutput returned with data\n"); + // copy data to packet AMFBuffer *buffer; AMFGuid guid = IID_AMFBuffer(); @@ -740,7 +749,13 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) data->pVtbl->Release(data); AMF_RETURN_IF_FALSE(ctx, ret >= 0, ret, "amf_copy_buffer() failed with error %d\n", ret); + } else { + av_log(avctx, AV_LOG_INFO, "QueryOutput returned with nothing (%d)\n", res_query); + // For testing, shouldn't hit this unless machine is otherwise very loaded. + av_assert0(!block_and_wait); } + + block_and_wait = 0; } res_resubmit = AMF_OK; if (ctx->delayed_surface != NULL) { // try to resubmit frame @@ -769,8 +784,9 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) if (query_output_data_flag == 0) { if (res_resubmit == AMF_INPUT_FULL || ctx->delayed_drain || (ctx->eof && res_query != AMF_EOF) || (ctx->hwsurfaces_in_queue >= ctx->hwsurfaces_in_queue_max)) { + av_log(avctx, AV_LOG_INFO, "Need to wait for output\n"); block_and_wait = 1; - av_usleep(1000); + //av_usleep(1000); } } } while (block_and_wait); diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h index 2dbd378ef8..64c77115b6 100644 --- a/libavcodec/amfenc.h +++ b/libavcodec/amfenc.h @@ -72,6 +72,7 @@ typedef struct AmfContext { int delayed_drain; AMFSurface *delayed_surface; AVFrame *delayed_frame; + int64_t output_query_timeout; // shift dts back by max_b_frames in timing AVFifo *timestamp_list;