From patchwork Mon Nov 13 14:37:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeny Pavlov X-Patchwork-Id: 44648 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:92a5:b0:181:818d:5e7f with SMTP id q37csp1350777pzg; Mon, 13 Nov 2023 06:42:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOlXbeQ70CLCDxMAeEyMXaA8rornF4tlsHdbdm8c6BMwnGeb4pMKUvn/Xy9eFD+wR3XxlC X-Received: by 2002:a17:906:cb93:b0:9b2:babd:cd51 with SMTP id mf19-20020a170906cb9300b009b2babdcd51mr4379134ejb.5.1699886518442; Mon, 13 Nov 2023 06:41:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699886518; cv=none; d=google.com; s=arc-20160816; b=Z5bf+wifBUtMn2EDnNKW8WUEBCtkbmeUGtJ41MOFvhR+Egek6NBNHftkjcHIBjDMx4 vHqdoWbM7wtA3CmhR+2JoCGe2gvRBHlaP3lG4qANEtJ0eRgVH5qhyUuwQPMeYalQKs8Q pj7RKanoNAEPmXssJTrU2LS9TcSdWJmts1j7FZJfd5RXsun1rFXje9QGi2NGlTs/n5aO YoMTLAKmPHpxx7HdRmdiBMuke2CJJY+hbCmK3ryNO6R1G4MwHUDKv/TBQ44IJnuLBRMU 04XuZWS6+iw2xSG8HCFO1XHi07P20nxuGtuIs6xfWwsq8ec7fT18esYsN64lIUn3CeqM tu/A== 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:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=ZOGbIBLV3cfSaMrHIczV38zDnU20j8N9E0XsBoqw42c=; fh=l8BBgSaHK1qPkRTUMT3LasR23D+FgYCLMIPW5YpagSs=; b=v8WQkM+OTksJjQFX+Z5BoT8xpaJwzfwLaMqg+X8+BRZ0fE+69zybBGQI4VDBQGpjce 2jlBFEQPJSH30um4MBNUpUylyD422e0qBLAMkXqsvtZ26jIM3JXNzxpLqFwDkOLukFVc J77zxSTXiHaRfK8DKbmbhMoXXBDP23ILWEPAvpsuASuZbzsQXfVHsGeTdcemavPzasPA wt3cGgaBpd4sjVeYzh4+XVqx4OB4A4VPG9qfDeqPResvyJwW5BRV6twmn0kiCB9HX64e AnqE7YqdFQdD3ZSoimSzKRwhxZArsS9LN+HvtfYCErKXb0MiXl2NxO22g5pkWm+ClWID e4KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=cGq0FT4P; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gx6-20020a170906f1c600b009e64daebe80si2776437ejb.376.2023.11.13.06.41.45; Mon, 13 Nov 2023 06:41:58 -0800 (PST) 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=@gmail.com header.s=20230601 header.b=cGq0FT4P; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2DBBC68CCE7; Mon, 13 Nov 2023 16:41:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 799D068CC41 for ; Mon, 13 Nov 2023 16:41:35 +0200 (EET) Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-9d0b4dfd60dso684862966b.1 for ; Mon, 13 Nov 2023 06:41:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699886494; x=1700491294; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=V/6ZC4095v69QvQIgz7IrCFOm8mLXrKO4dQIrt9ZcVE=; b=cGq0FT4PV762weEdLkuXQUqF08ACCRgXgxt9ckrFP/Cq6pY8lMJV7gOn9E2CtvJ3A3 3u/LTHX0dLuZVwKwAcv75GWeKIgFj8s+3VKicNp7NBGdqrcV1APzk0I0ipw/L6Uh/g+9 LLE3ZzHSh1mF9VwU/xIG6KpCTl2c90n5b/ndxDBOHxVqrANh9MA47SIqjbNGaCOAMgNg JpRY+V4zcm6rBmNsSMvl0IP3e12AJFQIVhQ7xkroJ9BHORKPd0LNA1P5KigyBU8utROz kTSUWbbs8hrQ4BwK4jI3tOrV9ccKe0ql26j9O8LoUVEBvTaWZsAJ8C1w9O8gL83VFopH T8zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699886494; x=1700491294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V/6ZC4095v69QvQIgz7IrCFOm8mLXrKO4dQIrt9ZcVE=; b=uXPVvnRb+zWasQx6gPr5RX+vELHTnmS55TYH1iyFy8LJBX7jaXoj7B76/LIHgBBqiY GsHV5+yzZVvqJtIhcyVOv9YQ6lhmduWBxsoF6WG954mvoLUKF5PlmITfHhN31uAG2QiC h52SwAslAuv30HPzlQQ+lbYGfn+aD+zMgM2lRQ6RPhY90+vNRJc2GSlMEskqAaT2j2XU 2Z7kPQ+h8twYdrtR9tntEwzPrnXRg09N+a3wjgq8D7W0O0bIx2rcp40Ts9C/hnFT7YI0 stNA1Bkm/Yz+h1lg+X7DFitpRJM3WKQg58reY5PHPla27LQHM37QSYbitT/a69BpwxTH 4dZg== X-Gm-Message-State: AOJu0YzD3feutl/gzYC50FIOoEXDa2MDs9dtkw+z4371Wl2E1d78374t s2oqSTpLSEwAj/RRNBiRsFW4qDPsJ94= X-Received: by 2002:a17:906:1395:b0:9be:ab38:a362 with SMTP id f21-20020a170906139500b009beab38a362mr4888586ejc.46.1699886494134; Mon, 13 Nov 2023 06:41:34 -0800 (PST) Received: from MSDN-EVPAVLOV.amd.com (178-223-50-56.dynamic.isp.telekom.rs. [178.223.50.56]) by smtp.gmail.com with ESMTPSA id a26-20020a17090640da00b009e651700b92sm4110985ejk.124.2023.11.13.06.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 06:41:33 -0800 (PST) From: Evgeny Pavlov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Nov 2023 15:37:02 +0100 Message-ID: <20231113143722.1959-1-lucenticus@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231016091402.7972-1-lucenticus@gmail.com> References: <20231016091402.7972-1-lucenticus@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avcodec/amfenc: increase precision of Sleep() on Windows 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: Evgeny Pavlov Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UIncAjWVXx5F This commit increase precision of Sleep() function on Windows. This fix reduces the sleep time on Windows to improve AMF encoding performance on low resolution input videos. Fix for issue #10622 v2: use timeBeginPeriod/timeEndPeriod for increasing precision of Sleep() Signed-off-by: Evgeny Pavlov --- libavcodec/amfenc.c | 31 +++++++++++++++++++++++++++++++ libavcodec/amfenc.h | 3 +++ 2 files changed, 34 insertions(+) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 061859f85c..55e24856e8 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -42,7 +42,12 @@ #endif #ifdef _WIN32 +#include #include "compat/w32dlfcn.h" + +typedef MMRESULT (*timeapi_fun)(UINT uPeriod); +#define WINMM_DLL "winmm.dll" + #else #include #endif @@ -113,6 +118,9 @@ static int amf_load_library(AVCodecContext *avctx) AMFInit_Fn init_fun; AMFQueryVersion_Fn version_fun; AMF_RESULT res; +#ifdef _WIN32 + timeapi_fun time_begin_fun; +#endif ctx->delayed_frame = av_frame_alloc(); if (!ctx->delayed_frame) { @@ -145,6 +153,16 @@ static int amf_load_library(AVCodecContext *avctx) AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "GetTrace() failed with error %d\n", res); res = ctx->factory->pVtbl->GetDebug(ctx->factory, &ctx->debug); AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "GetDebug() failed with error %d\n", res); + +#ifdef _WIN32 + // Increase precision of Sleep() function on Windows platform + ctx->winmm_lib = dlopen(WINMM_DLL, RTLD_NOW | RTLD_LOCAL); + AMF_RETURN_IF_FALSE(ctx, ctx->winmm_lib != NULL, 0, "DLL %s failed to open\n", WINMM_DLL); + time_begin_fun = (timeapi_fun)dlsym(ctx->winmm_lib, "timeBeginPeriod"); + AMF_RETURN_IF_FALSE(ctx, time_begin_fun != NULL, 0, "DLL %s failed to find function %s\n", WINMM_DLL, "timeBeginPeriod"); + time_begin_fun(1); +#endif //_WIN32 + return 0; } @@ -375,6 +393,9 @@ static int amf_init_encoder(AVCodecContext *avctx) int av_cold ff_amf_encode_close(AVCodecContext *avctx) { AmfContext *ctx = avctx->priv_data; +#ifdef _WIN32 + timeapi_fun time_end_fun; +#endif //_WIN32 if (ctx->delayed_surface) { ctx->delayed_surface->pVtbl->Release(ctx->delayed_surface); @@ -410,6 +431,16 @@ int av_cold ff_amf_encode_close(AVCodecContext *avctx) av_frame_free(&ctx->delayed_frame); av_fifo_freep2(&ctx->timestamp_list); +#ifdef _WIN32 + if (ctx->winmm_lib) { + time_end_fun = (timeapi_fun)dlsym(ctx->winmm_lib, "timeEndPeriod"); + AMF_RETURN_IF_FALSE(ctx, time_end_fun != NULL, 0, "DLL %s failed to find function %s\n", WINMM_DLL, "timeEndPeriod"); + time_end_fun(1); + dlclose(ctx->winmm_lib); + ctx->winmm_lib = NULL; + } +#endif //_WIN32 + return 0; } diff --git a/libavcodec/amfenc.h b/libavcodec/amfenc.h index 2dbd378ef8..35bcf1dfe3 100644 --- a/libavcodec/amfenc.h +++ b/libavcodec/amfenc.h @@ -50,6 +50,9 @@ typedef struct AmfContext { AVClass *avclass; // access to AMF runtime amf_handle library; ///< handle to DLL library +#ifdef _WIN32 + amf_handle winmm_lib; ///< handle to winmm DLL library +#endif //_WIN32 AMFFactory *factory; ///< pointer to AMF factory AMFDebug *debug; ///< pointer to AMF debug interface AMFTrace *trace; ///< pointer to AMF trace interface