From patchwork Wed Apr 17 14:46:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Samuelson X-Patchwork-Id: 48108 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ce4e:b0:1a9:af23:56c1 with SMTP id id14csp1072242pzb; Wed, 17 Apr 2024 08:10:48 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVd395vT0ponAqF/Qq1L7TCJODOlYWINGO/D58niXaR+bAO4YpDCbptuO1l5srD3OgbOjBfYeKgnfojkwziY7NxfPlwO5MBx0K4yA== X-Google-Smtp-Source: AGHT+IHvpOIfiCFGbUhZYJPprNPQjHlPSQmk+6aSAhuziKk83jPhZAx34I3I5oqVCvwVPPA1SZ03 X-Received: by 2002:a50:a6d2:0:b0:56e:2daf:1ed9 with SMTP id f18-20020a50a6d2000000b0056e2daf1ed9mr12566033edc.23.1713366648564; Wed, 17 Apr 2024 08:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713366648; cv=none; d=google.com; s=arc-20160816; b=Q0JidhVGtCy0jrXGgkTlX5Y50NhA4N+3OMk8pIQ5T7HLwVvdiHKDF3XZUL1/CyWoZZ ZsLFIkpZkvS0niiSD81k1DeheE35TRBmiaWbjUioyet8W30uJr+hyb7kJ6B9A4Jhemn1 M3Z6virLsRVj9OJz4VUBD1B32g1UtS8tzNoxcoSl9U61M0ozYRbdsjuTF26G+2w7akG/ 3CwYjogQZRBF02IPxDe34ikPINawwNuxKuQjKi8KM+wmC1cwIf00ljbH0cPXenav/Hk6 /QngJRO/79ztGP6DRKjl2wd4yqzUjcluNvn0rtXt8Hn/K/YE3NDrtoIdWl9s4IZu20VU E36w== 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=BuV2z4DD/P80EyVIINCKbVL8WDbqHIk25WKBqyBsgdg=; fh=QKFI4dMj83J2pxOiuhiYCo6Z/ly20dID22f33yRIZqE=; b=NuaWxJMylo/M8KtfPH2GVO36y4HC/6i92+a2of2xhtvkssNnO2CiuebCnpHuBioJwJ npiPBRLda0qdwM/bcX3qdJW/CKLp7BsK7uctn8I6MRL2qKtTTwtoJVpbQdx5/QE7jC05 SmXVmhf+Fw7WfcVtWCHNXK2vcSIvprnCweQQTjfncWvAbtjwDktKiAB2LgQgSqrLMilB izgQVvkpr79Yl9shD3sa7QWE3OdKKBspmUBympMYkA8mD3QlFXL+9YmynQeqV3I4jRb/ fAe/sFDjwA+MmPQoqyXfY0yx9D+XSCyLANz3nCyBrOr5A/lzHtRtpYVb1iqN9XgB4NJE UALw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=KF6GmaGm; 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 w5-20020a056402268500b00570238a5c36si3731588edd.420.2024.04.17.08.10.43; Wed, 17 Apr 2024 08:10:48 -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=@gmail.com header.s=20230601 header.b=KF6GmaGm; 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 5EE4E68D2FB; Wed, 17 Apr 2024 18:10:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Nn5/0z/Vq2Ty 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) {