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 C837647E06 for ; Mon, 27 Nov 2023 13:42:01 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5105968CEEA; Mon, 27 Nov 2023 15:41:58 +0200 (EET) Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 37A9568CA44 for ; Mon, 27 Nov 2023 15:41:51 +0200 (EET) Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-332ff20ec45so625985f8f.1 for ; Mon, 27 Nov 2023 05:41:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20230601.gappssmtp.com; s=20230601; t=1701092510; x=1701697310; 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=syn3R4BqvuE+z/ezIw6Vpd2cRKk0VNWRQqKq3kBMS14=; b=Kb4V+JUwZzmGeqhL0Q9Gb2z2ZireP8GlDI7ai1LXqxJPeQBAyjbDDRVWXL9U2gtcoT SWSSPQK9vAfR14YRDGictesskyNKVeFfgspPOmsROv0mSg9er7AcgMJT5KZ6pEy5rt1z sW+A2SVvApjilNX4EoumOvMBTGH4WBUNpvivfP9AP1oYttlJCzHzEk+jczTJS4ztnRx5 BpOw+S0xfOC52Kwub/NbCOgXFcTjBzhVcxMlVmjuwXqONOVYZQ8j+NXD2ITiTPqowVdI EudDD7xoivezv/2qjtccoxQJJMUTfTsO0utieuUA/P7dbUQOKT98IW6GGvPAJ/+fVtHU qaZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701092510; x=1701697310; 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=syn3R4BqvuE+z/ezIw6Vpd2cRKk0VNWRQqKq3kBMS14=; b=fcpf94iRsgt291nmwoRgw+FqXAjpkWrFtL5gcZaBnlUEO8QW4qQTxz30OI/fxH/YUM vWwg5yWk06UHuVKbxkVmFQR7+Dv+l6IowYBZ4LPeMnTL6D2mdA9H+dgagcpH7dZy3lN6 34TEiYtHSf75kS5EcyIPGT/Ubroe/2l3gSDXKipaLRbzi4aLaPFV55IULK7P9JaPgilP UJNx7zSs7wHOwNgC+ai0WceoR4uakT/62MEDWFp98aGpHdWpbkegb7r8LINt9S5ZSg1D pL7VjvrOpOqrqGVvOwga5tIAotm5qua9kF7m/oASQ1Fkaf+gn1ifG4Sbz4K3IbylS0dv FJ7g== X-Gm-Message-State: AOJu0YzGZb101dpRbsUXjpgivTh2DAVbvtGqMBh284hay705Rd6a9eaN lZmCF7TIBNCw1MQUJ15EXOqq0M8mF6uJ8ZnBq1o= X-Google-Smtp-Source: AGHT+IElMjhMtZPw3/+Df4Jr+TaKQaIAC8pVOLpdUqMB6g8beFMfOElwHcaoqB2p86o5ByldMt2AxA== X-Received: by 2002:a5d:4571:0:b0:332:eae9:1db1 with SMTP id a17-20020a5d4571000000b00332eae91db1mr9588473wrc.4.1701092510497; Mon, 27 Nov 2023 05:41:50 -0800 (PST) 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 j18-20020adfea52000000b00332f1900476sm6936177wrn.81.2023.11.27.05.41.50 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Nov 2023 05:41:50 -0800 (PST) Message-ID: <9fd98754-299f-4844-9296-3e38c40d4d4a@jkqxz.net> Date: Mon, 27 Nov 2023 13:42:09 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <20231016091402.7972-1-lucenticus@gmail.com> <20231113143722.1959-1-lucenticus@gmail.com> From: Mark Thompson In-Reply-To: <20231113143722.1959-1-lucenticus@gmail.com> Subject: Re: [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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 13/11/2023 14:37, Evgeny Pavlov wrote: > 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 Is it reasonable to set this global state from a library without the parent program knowing? We'd really prefer not to affect the global state unexpectedly. It's also unclear to me what the effect of this tradeoff on power is, given that the whole reason why this happens is that Windows is trying to keep the CPU asleep for as long as possible to save power. Thanks, - Mark _______________________________________________ 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".