From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 683B14CC19 for ; Fri, 8 Aug 2025 14:53:03 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 469CB68C683; Fri, 8 Aug 2025 17:52:59 +0300 (EEST) Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id CF57E68BB4E for ; Fri, 8 Aug 2025 17:52:52 +0300 (EEST) Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-af93381a1d2so386319566b.3 for ; Fri, 08 Aug 2025 07:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754664772; x=1755269572; 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=Mmc8ThLnQUGZ1g49E2z1vrBZe/Bv6/EfBGtt8f18E/Q=; b=VShexXDi7zOxbwjreu3Gvl1wlx2Ws6HAgBFy0z7ZEbLdwmf9RKkl7ZMgp5IrCodSfL 2BRPpuNRNmwX2LdPAXR8MstqsaPmGbmnNgp7UrGFa43k31T1GOOj59w+B+JpwtXDo0mf eRpoWqPvzzBvAjB4pyaifyis9O1irsFdacFmSjVwLwn/3tNmAIOZTtucX20Jw5uYEl5g HmRttW5NscdqBkjYYInco5lheeVtS8oBPUPl+78wB1UW4vkRggmC731bUzISZCOgMnwH 78HffSZjPFyRt1WKLvAYrJeJ09qimghjhEQPCpZCjjrZAnbrQ6Xl8pFaCruxJMczHEsg jbLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754664772; x=1755269572; 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=Mmc8ThLnQUGZ1g49E2z1vrBZe/Bv6/EfBGtt8f18E/Q=; b=LiLI8kJ0M75e979cdPW97Rai2BhB202p8ZNws9sH7VHpPS2QYWHl/yX2s+wNTIF5Vn jduXVpY1tKstiJxxYNRRJyVRp3DdFGtvJF1n+L/NjNZuJ99062Qi9UepT02NR+caO4mU KYS7Y2usaE2j+pXptZwPq9cOwsSnooojAKgwgpbk0KT0EP0dzZD+z6bxVeYb47TX5c9p fcwPS0bgSjvvd9+edc8XfMml3rlMlbGUBj85ljOhxu1zPRnByULxt/ZPXlKTB7Ibdute ZqzYXaVRj9pVZLD3Gb47kagHC+Pl34dALYuSdNZ/d+Jm5kaEFurr0VRsPpC8rF/tIimo 3wnA== X-Gm-Message-State: AOJu0YzFxSfD8wN0yle/ieCQpYOUGrGy5ZiEucp+XqQ6KkQJfB8REX9z ZRRJxsgpjfWbeXiezM3Dh9GUC6JYuDKfk30NVCz8xVqjR4Cv8239bdYt5YMKwdE//G4= X-Gm-Gg: ASbGncviFsi4xI5cpNx/aH0SHrcC2+w2NrTzBG/Yh9n+/s6XsL54xjqnr2/nT/F7Fmr WDqAVL1WymqYX8+Is9dErAVegWYHTkUL22qn8DOjvZFmaEqaDLc53BT10Qq7s7Tz/OisECx1Lca k5lEkLnzUzNs6R36sjJaWGuf3V33LBLsuovcz/mz8Kq71w54QR6cGKDWl5HQm8fjOLUI6jIfL+i g+diDjYB5tuF7Cz4osRxKggZwsxnzthjNBv7Xzht8B0/dmTXE3QLIGhe0WBNwmdJDjjI3ZRnjOm wy2asgfyIlPkzVAOKTEahhqs9iEbjBoFCf4UkA8zzqR2Ue7wzsA0CQp1v/WGN/kHoWIDU4nlUP4 wY1I1vUIZcEJiJWrTWEj9LJOqdapLpFZI35Tvl5a/w3xdhCurBxjVkDD2sp4= X-Google-Smtp-Source: AGHT+IHEYHK2JSqGs+L+ISFD6FDjXxaF29th24itxbQ5/ny2Wy5NOI+3rHBaxymQv/QHX3FGbNRo3Q== X-Received: by 2002:a17:907:2d94:b0:ae3:a812:a780 with SMTP id a640c23a62f3a-af9c652bdd1mr278640666b.61.1754664771435; Fri, 08 Aug 2025 07:52:51 -0700 (PDT) Received: from localhost.localdomain ([87.116.182.236]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-af91a1e82b0sm1514927966b.87.2025.08.08.07.52.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Aug 2025 07:52:50 -0700 (PDT) From: Dmitrii Ovchinnikov To: ffmpeg-devel@ffmpeg.org Date: Fri, 8 Aug 2025 16:52:34 +0200 Message-ID: <20250808145234.270-1-ovchinnikov.dmitrii@gmail.com> X-Mailer: git-send-email 2.47.1.windows.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavutil/hwcontext_amf: Simplified blocking before frame submission 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: Dmitrii Ovchinnikov 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: Instead of blocking the entire context, which can cause issues in more complex pipelines, now only frame sending is blocked via AVMutex --- libavcodec/amfenc.c | 93 +++------------------------------------ libavutil/hwcontext_amf.c | 6 ++- libavutil/hwcontext_amf.h | 2 + 3 files changed, 11 insertions(+), 90 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index 962bf6b006..3aa4c06760 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -367,89 +367,6 @@ static AMF_RESULT amf_set_property_buffer(AMFSurface *object, const wchar_t *nam return res; } -static AMF_RESULT amf_lock_context(AVCodecContext *avctx) -{ - AMFEncoderContext *ctx = avctx->priv_data; - AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data; - AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx; - AMF_RESULT res; - - switch(amf_device_ctx->memory_type) { - case AMF_MEMORY_DX11: - res = amf_device_ctx->context->pVtbl->LockDX11(amf_device_ctx->context); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX11() failed with error %d\n", res); - break; - case AMF_MEMORY_DX12: - { - AMFContext2 *context2 = NULL; - AMFGuid guid = IID_AMFContext2(); - res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res); - res = context2->pVtbl->LockDX12(context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX12() failed with error %d\n", res); - context2->pVtbl->Release(context2); - } - break; - case AMF_MEMORY_DX9: - res = amf_device_ctx->context->pVtbl->LockDX9(amf_device_ctx->context); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX9() failed with error %d\n", res); - - case AMF_MEMORY_VULKAN: - { - AMFContext2 *context2 = NULL; - AMFGuid guid = IID_AMFContext2(); - res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res); - res = context2->pVtbl->LockVulkan(context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockVulkan() failed with error %d\n", res); - context2->pVtbl->Release(context2); - } - break; - } - return AMF_OK; -} -static AMF_RESULT amf_unlock_context(AVCodecContext *avctx) -{ - AMFEncoderContext *ctx = avctx->priv_data; - AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data; - AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx; - AMF_RESULT res; - - switch(amf_device_ctx->memory_type) { - case AMF_MEMORY_DX11: - res = amf_device_ctx->context->pVtbl->UnlockDX11(amf_device_ctx->context); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX11() failed with error %d\n", res); - break; - case AMF_MEMORY_DX12: - { - AMFContext2 *context2 = NULL; - AMFGuid guid = IID_AMFContext2(); - res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res); - res = context2->pVtbl->UnlockDX12(context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX12() failed with error %d\n", res); - context2->pVtbl->Release(context2); - } - break; - case AMF_MEMORY_DX9: - res = amf_device_ctx->context->pVtbl->UnlockDX9(amf_device_ctx->context); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockDX9() failed with error %d\n", res); - - case AMF_MEMORY_VULKAN: - { - AMFContext2 *context2 = NULL; - AMFGuid guid = IID_AMFContext2(); - res = amf_device_ctx->context->pVtbl->QueryInterface(amf_device_ctx->context, &guid, (void**)&context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "QueryInterface for AMFContext2 failed with error %d\n", res); - res = context2->pVtbl->UnlockVulkan(context2); - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "LockVulkan() failed with error %d\n", res); - context2->pVtbl->Release(context2); - } - break; - } - return AMF_OK; -} - static AMF_RESULT amf_store_attached_frame_ref(AMFEncoderContext *ctx, const AVFrame *frame, AMFSurface *surface) { AMF_RESULT res = AMF_FAIL; @@ -629,14 +546,14 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface static int amf_submit_frame_locked(AVCodecContext *avctx, AVFrame *frame, AMFSurface **surface_resubmit) { int ret; - int locked = amf_lock_context(avctx); - if(locked != AMF_OK) - av_log(avctx, AV_LOG_WARNING, "amf_lock_context() failed with %d - should not happen\n", locked); + AMFEncoderContext *ctx = avctx->priv_data; + AVHWDeviceContext *hw_device_ctx = (AVHWDeviceContext*)ctx->device_ctx_ref->data; + AVAMFDeviceContext *amf_device_ctx = (AVAMFDeviceContext *)hw_device_ctx->hwctx; + ff_mutex_lock(&amf_device_ctx->mutex); ret = amf_submit_frame(avctx, frame, surface_resubmit); + ff_mutex_unlock(&amf_device_ctx->mutex); - if(locked == AMF_OK) - amf_unlock_context(avctx); return ret; } diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c index a020b0e2e1..ac6b007523 100644 --- a/libavutil/hwcontext_amf.c +++ b/libavutil/hwcontext_amf.c @@ -137,7 +137,7 @@ enum AVPixelFormat av_amf_to_av_format(enum AMF_SURFACE_FORMAT fmt) return format_map[i].av_format; } } - return AMF_SURFACE_UNKNOWN; + return AV_PIX_FMT_NONE; } static const enum AVPixelFormat supported_formats[] = { @@ -378,6 +378,7 @@ static void amf_device_uninit(AVHWDeviceContext *device_ctx) } amf_ctx->version = 0; + ff_mutex_destroy(&amf_ctx->mutex); } static int amf_device_init(AVHWDeviceContext *ctx) @@ -414,7 +415,8 @@ static int amf_device_init(AVHWDeviceContext *ctx) } } #endif - return 0; + ff_mutex_init(&amf_ctx->mutex, NULL); + return 0; } static int amf_load_library(AVAMFDeviceContext* amf_ctx, void* avcl) diff --git a/libavutil/hwcontext_amf.h b/libavutil/hwcontext_amf.h index 8823190697..5b726e3b9e 100644 --- a/libavutil/hwcontext_amf.h +++ b/libavutil/hwcontext_amf.h @@ -26,6 +26,7 @@ #include #include #include +#include "thread.h" /** * This struct is allocated as AVHWDeviceContext.hwctx @@ -38,6 +39,7 @@ typedef struct AVAMFDeviceContext { int64_t version; ///< version of AMF runtime AMFContext *context; AMF_MEMORY_TYPE memory_type; + AVMutex mutex; } AVAMFDeviceContext; enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt); -- 2.47.1.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".