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 E5A854A00D for ; Wed, 17 Apr 2024 15:10:42 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2024668D2FA; Wed, 17 Apr 2024 18:10:40 +0300 (EEST) Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4CEAC68C7C2 for ; Wed, 17 Apr 2024 18:10:33 +0300 (EEST) Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-78f05fde6dcso23408385a.2 for ; Wed, 17 Apr 2024 08:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713366632; x=1713971432; 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=lYKbYD0JfaeMzuF6hhjLw8DibKBtar/fYbw6fSq+90w=; b=KF6GmaGm/M1s3UJguRUZiyw9qzD9wgJn2/lZJR3jQPhm6Q3IjTpr7vZUglK6gasSXQ LVuueipr1QADEYzEV8vvG0AHTqIwZMyQoTwTjWJOfOO+zVkAFFb2G0fvsnmB+2ymecHg M2ciET5QSKGeYilI5qn2qh9pQEQpGyod3w7tjvhM5k+pahjWkAB+7eQJlxUDkhiHjUxn jQt/v0tV7eCaduRLMgyV95xpR88hEtfJIeTj5FlCNGI+ljLCDsxDS4rRB5mf/QZYioGg 4NVnwZlDMZ/wN3ztXLW93Jm589dKR0VGFmznfCJ3jxVST0OgDiFct7pepQ/ETnGs6rPk pvZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713366632; x=1713971432; 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=lYKbYD0JfaeMzuF6hhjLw8DibKBtar/fYbw6fSq+90w=; b=PdyGOGAMl+C38Apj2S6mLY/c8/tkOucioOnc+VsHkKeMt9gsKK1K/MiHoIIY69t22w VxflKrTGt9IUySqnK5fWjbwwLKAtGTzsX8BHTmYNk6jJ4o4WAeuTQQjiIatYFR0IN5oF 1kOEfgZZTa8nDIu+sOnXSt5cymneBqLXPeWTxfEQsil1ANR4Tvb/R+ugo7+FRqDEsSph DzYW6v5G9pgweaIRB6TJqvt0mPAeBQKgz/HE/bKfeYAefm9acddGITPy77BPfAhOrZ+g wSp3a0Rz6NlGp2zeNEhUdIeOQLeYu2fmLu78UycuKqGoQgi2SFzGkd73yM3XE5YEEzkp CLQg== X-Gm-Message-State: AOJu0YzRSoM2TlLhYnjxU9UynHIRGfG4DcKWIMrGwJqQU2kfU2J5u/pf J+Q6jQ6ANBjsd7UIQ+EMTd1ZS4PFcBK7C0lq8gF18NV5ri22kIu8cHrE9Q== X-Google-Smtp-Source: AGHT+IFOZRHefnS6v8yTpx8gNiVmKPXMEf9JdJv5DLarmTpGjvBq5pidMh4IjsxfL7cNLoksjAKahQ== X-Received: by 2002:a05:620a:4694:b0:78f:17e:e575 with SMTP id bq20-20020a05620a469400b0078f017ee575mr2096294qkb.75.1713366631626; Wed, 17 Apr 2024 08:10:31 -0700 (PDT) Received: from localhost.localdomain ([174.82.139.8]) by smtp.gmail.com with ESMTPSA id v9-20020a05620a0a8900b0078d5e60b52esm8419337qkg.114.2024.04.17.08.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 08:10:31 -0700 (PDT) From: Mark Samuelson To: ffmpeg-devel@ffmpeg.org Date: Wed, 17 Apr 2024 09:46:30 -0500 Message-ID: <20240417151013.362-1-samuelsonm@gmail.com> X-Mailer: git-send-email 2.44.0.windows.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavutil/hwcontext_qsv: Make qsv hardware transfers thread safe 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: Mark Samuelson Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: The QSV hardware context currently uses pthreads to lock initilization, which is not available on windows builds. Instead, use the AVMutex object. Also lock uses of the realigned_upload_frame and realigned_download_frame objects, so multiple threads do not attempt to write to them at the same time. --- Here is a new patch addressing your comments Fixed the nested calls to ff_mutex_lock Fixed the two accidental tabs Fixed the two violations of K&R style Fixed the two incidents of mixing declaration and code libavutil/hwcontext_qsv.c | 93 +++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c index c7c7878644..ed462d440a 100644 --- a/libavutil/hwcontext_qsv.c +++ b/libavutil/hwcontext_qsv.c @@ -23,10 +23,7 @@ #include #include "config.h" - -#if HAVE_PTHREADS -#include -#endif +#include "thread.h" #define COBJMACROS #if CONFIG_VAAPI @@ -98,9 +95,7 @@ typedef struct QSVFramesContext { atomic_int session_download_init; mfxSession session_upload; atomic_int session_upload_init; -#if HAVE_PTHREADS - pthread_mutex_t session_lock; -#endif + AVMutex session_lock; AVBufferRef *child_frames_ref; mfxFrameSurface1 *surfaces_internal; @@ -354,9 +349,7 @@ static void qsv_frames_uninit(AVHWFramesContext *ctx) s->session_upload = NULL; s->session_upload_init = 0; -#if HAVE_PTHREADS - pthread_mutex_destroy(&s->session_lock); -#endif + ff_mutex_destroy(&s->session_lock); av_freep(&s->mem_ids); #if QSV_HAVE_OPAQUE @@ -1302,9 +1295,7 @@ static int qsv_frames_init(AVHWFramesContext *ctx) s->session_download_init = 0; s->session_upload_init = 0; -#if HAVE_PTHREADS - pthread_mutex_init(&s->session_lock, NULL); -#endif + ff_mutex_init(&s->session_lock, NULL); return 0; } @@ -1629,24 +1620,20 @@ static int qsv_internal_session_check_init(AVHWFramesContext *ctx, int upload) if (atomic_load(inited)) return 0; -#if HAVE_PTHREADS - pthread_mutex_lock(&s->session_lock); -#endif + ff_mutex_lock(&s->session_lock); if (!atomic_load(inited)) { ret = qsv_init_internal_session(ctx, session, upload); atomic_store(inited, 1); } -#if HAVE_PTHREADS - pthread_mutex_unlock(&s->session_lock); -#endif + ff_mutex_unlock(&s->session_lock); return ret; } -static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, - const AVFrame *src) +static int qsv_transfer_data_from_internal(AVHWFramesContext *ctx, AVFrame *dst, + const AVFrame *src, int realigned) { QSVFramesContext *s = ctx->hwctx; mfxFrameSurface1 out = {{ 0 }}; @@ -1658,17 +1645,11 @@ static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, /* download to temp frame if the output is not padded as libmfx requires */ AVFrame *tmp_frame = &s->realigned_download_frame; AVFrame *dst_frame; - int realigned = 0; - - ret = qsv_internal_session_check_init(ctx, 0); - if (ret < 0) - return ret; /* According to MSDK spec for mfxframeinfo, "Width must be a multiple of 16. * Height must be a multiple of 16 for progressive frame sequence and a * multiple of 32 otherwise.", so allign all frames to 16 before downloading. */ - if (dst->height & 15 || dst->linesize[0] & 15) { - realigned = 1; + if (realigned) { if (tmp_frame->format != dst->format || tmp_frame->width != FFALIGN(dst->linesize[0], 16) || tmp_frame->height != FFALIGN(dst->height, 16)) { @@ -1728,8 +1709,30 @@ static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, return 0; } -static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, - const AVFrame *src) +static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, + const AVFrame *src) +{ + QSVFramesContext *s = ctx->hwctx; + int realigned = 0; + int ret = 0; + + ret = qsv_internal_session_check_init(ctx, 0); + if (ret < 0) + return ret; + + if (dst->height & 15 || dst->linesize[0] & 15) { + realigned = 1; + ff_mutex_lock(&s->session_lock); + } + ret = qsv_transfer_data_from_internal(ctx, dst, src, realigned); + if (realigned) + ff_mutex_unlock(&s->session_lock); + + return ret; +} + +static int qsv_transfer_data_to_internal(AVHWFramesContext *ctx, AVFrame *dst, + const AVFrame *src, int realigned) { QSVFramesContext *s = ctx->hwctx; mfxFrameSurface1 in = {{ 0 }}; @@ -1742,17 +1745,11 @@ static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, /* make a copy if the input is not padded as libmfx requires */ AVFrame *tmp_frame = &s->realigned_upload_frame; const AVFrame *src_frame; - int realigned = 0; - - ret = qsv_internal_session_check_init(ctx, 1); - if (ret < 0) - return ret; /* According to MSDK spec for mfxframeinfo, "Width must be a multiple of 16. * Height must be a multiple of 16 for progressive frame sequence and a * multiple of 32 otherwise.", so allign all frames to 16 before uploading. */ - if (src->height & 15 || src->linesize[0] & 15) { - realigned = 1; + if (realigned) { if (tmp_frame->format != src->format || tmp_frame->width != FFALIGN(src->width, 16) || tmp_frame->height != FFALIGN(src->height, 16)) { @@ -1821,6 +1818,28 @@ static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, return 0; } +static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, + const AVFrame *src) +{ + QSVFramesContext *s = ctx->hwctx; + int realigned = 0; + int ret = 0; + + ret = qsv_internal_session_check_init(ctx, 1); + if (ret < 0) + return ret; + + if (src->height & 15 || src->linesize[0] & 15) { + realigned = 1; + ff_mutex_lock(&s->session_lock); + } + ret = qsv_transfer_data_to_internal(ctx, dst, src, realigned); + if (realigned) + ff_mutex_unlock(&s->session_lock); + + return ret; +} + static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx, AVHWFramesContext *src_ctx, int flags) { -- 2.44.0.windows.1 _______________________________________________ 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".