From patchwork Sat Apr 13 12:57:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Samuelson X-Patchwork-Id: 48041 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:670b:b0:1a9:af23:56c1 with SMTP id wh11csp826874pzb; Sat, 13 Apr 2024 05:58:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVbvuLPYyYlgtxv3snBh+Nljw29UWEmh2RCOw4JmGZVKaF599tBQ9neAueJ/pn/i7NKxFyNFwjMpdKhAmH1Sjhwj++/vFO/cVmpqA== X-Google-Smtp-Source: AGHT+IFG59TvIZhssqC4GbjjwRnI263jYA3KMeI/VUa3lV26WukGNEBK6+ktsZBE+EVCPpDGRV8n X-Received: by 2002:a17:907:928e:b0:a51:b84d:34c5 with SMTP id bw14-20020a170907928e00b00a51b84d34c5mr3422561ejc.75.1713013132446; Sat, 13 Apr 2024 05:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713013132; cv=none; d=google.com; s=arc-20160816; b=ujQHebbp1UV7z1UfMl5WbWEgOyuoSWhVZQqe1e+ohtAMdvW/Fo/pWG6Ufnn/hl9SC/ 8jNrxvtD9pf5rpBbn78/Rgdh2nGGC7orIKveVEUmwZ0H7pn/lR6eDY3AQt9kslb2IVAI pqZyFN2ofMaX72lavCzfoHjjRboVslKBVhqAGq31J/MNKV8a5iBQf/EvpyKAnVJZdB9K VZeJvgqRdDfnP3ZDg6VnwQBNWEpg8Zh/CzYt12JRCt5SkvMt8ZPaAJ+7G8zKOLQVrjTK SqCHdK55C226G2a1qdCRVSoV1mwQXf6aYBWi4LI9ytmV++4aPdZGhaJ5ZJlaWRMW4NfX IfNA== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=Qo1MxDRdlYji/VrSZnTDUajX0RST3c7COah/mfbrrT4=; fh=QKFI4dMj83J2pxOiuhiYCo6Z/ly20dID22f33yRIZqE=; b=YBZmkNbiRUtUAFz0uFVo7Z8YyWNlA04O7jWxsafmYRysXNKwsOJf3cfSms7RzCrtGA 7Q/rgm0mIG7VaYOzDYBisGlL66iiSfccoDDygdijTCANGjpnZpuvf9jtxuL/+HQ/a7i0 DIgEWYgxstnhSueg4iVVkZW/1ibwu1UUlrpJTn1Z6vXJauImhQNF5n07FtwZevrSyxwY itK/teBGagt5XQLKLgSN8CKPm58Y44oD1v9sBBf9FTuYOs3+VKTAnqkNwexdsh3frxIN GiJPtXlR9NBPCUgdktOu+PIu9yoMXYcvGcMltSYM3U+puCugSGy9R75wiTlExbXfsrwF xA0w==; 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=gAbV437N; 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 n24-20020a170906b31800b00a51d60792efsi2646037ejz.572.2024.04.13.05.58.51; Sat, 13 Apr 2024 05:58:52 -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=gAbV437N; 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 31C8468D306; Sat, 13 Apr 2024 15:58:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B8F7468D083 for ; Sat, 13 Apr 2024 15:58:41 +0300 (EEST) Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3c707580d4eso12278b6e.1 for ; Sat, 13 Apr 2024 05:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713013119; x=1713617919; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7wSLnPNV9maqTaOLVHsFk4926Nk7MPNjQaPsbpep13o=; b=gAbV437NR0key3RMQ0yOQ+l6zvZMEVMnsvYozAd75A3hl7mrhjvocMp9DyfpgMqUax M3Zvs2UOgpOJQ/O22trfsi5tQ8V6wSLkK0rbI2kR2iulbj3PROHuMT0WQg0rnnlq5VhZ xTytxl5iEwA51b8nNxMr3cllPipaEQRpMdn/xJJ6vC1WsCKv3OomwP5Js0TGwKMBAygx 1I3iD/fes5PrlHgDUgGy/7/v2+BcvuApt48YunxjRlebqopUMwLsyDBHpWbzI+vSx8Ef qcPPZNOFr6jlkpjuvAe3JtyNFL8beC8DcpvvQTZoM0n8ufyjaYAv2rnSFcigNgZlpXTo W5GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713013119; x=1713617919; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7wSLnPNV9maqTaOLVHsFk4926Nk7MPNjQaPsbpep13o=; b=WhXjJDgKYIxOLlQZQAdUMfHI8tdXeKXDS3+lrWJuHDO9jK06OtEgmHRfdtCB+5ab4S ya1Ahu/ZiYkDirAxNOWMzxD5dUYhuxWmgL+kDfHDlh7Q4BbW66FdiqThhflvGmpOZlbR bPVoF/0BJxW1XSUQtMN7CeHtM0jxiGdokKT3nib9wmt6OVocgTZs+VPeBMXGFLw8WNWg ucfxRGMwwhGjb+LEOq4JvkW7CUmmu61CKaoTZLdMS2mtPeupVPl7qrK9ZW+OMBdB0za/ 04qEJirDQqfrWy0xKea9fXEg+sHtodTRXgV/qED6p/8/GTYBpO1cy2bMR9BgV6D53Sk7 0rNg== X-Gm-Message-State: AOJu0YyQEkhHulcVixR+52OlCTOCfDGSK6EdA7ijwWENggZvHQwd1EFQ LJ0CV/Gdq0jMKRFUiQd6nLQLWCVglHRy+LUuWLD8AyzkxIwTshBaQieICg== X-Received: by 2002:a05:6808:641:b0:3c6:11cd:1da7 with SMTP id z1-20020a056808064100b003c611cd1da7mr5313599oih.51.1713013118847; Sat, 13 Apr 2024 05:58:38 -0700 (PDT) Received: from localhost.localdomain (174-082-139-008.res.spectrum.com. [174.82.139.8]) by smtp.gmail.com with ESMTPSA id f26-20020ac8465a000000b00434a352e239sm3486844qto.43.2024.04.13.05.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Apr 2024 05:58:38 -0700 (PDT) From: Mark Samuelson To: ffmpeg-devel@ffmpeg.org Date: Sat, 13 Apr 2024 07:57:58 -0500 Message-ID: <20240413125759.1427-1-samuelsonm@gmail.com> X-Mailer: git-send-email 2.44.0.windows.1 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: sy9WhQOS7YLs 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. --- libavutil/hwcontext_qsv.c | 75 ++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c index c7c7878644..92bab134e4 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,7 +1645,6 @@ 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) @@ -1667,8 +1653,7 @@ static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, /* 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 +1713,25 @@ 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; + if (dst->height & 15 || dst->linesize[0] & 15) + { + realigned = 1; + ff_mutex_lock(&s->session_lock); + } + int 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,7 +1744,6 @@ 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) @@ -1751,8 +1752,7 @@ static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, /* 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 +1821,23 @@ 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; + if (src->height & 15 || src->linesize[0] & 15) + { + realigned = 1; + ff_mutex_lock(&s->session_lock); + } + int 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) {