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 BB9DD4959E for ; Sat, 13 Apr 2024 12:58:50 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D5F5968D2F2; Sat, 13 Apr 2024 15:58:47 +0300 (EEST) 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-Google-Smtp-Source: AGHT+IEq3SwEejQ8Ap2qcSEBQayhKZASeQoPkm6LCIOqwFkw1wkDgdVhQAWdCotPvVNarlm2/iOZYg== 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 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. --- 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) { -- 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".