From patchwork Tue Feb 8 18:26:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 34190 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1045293iov; Tue, 8 Feb 2022 10:27:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJyBnlMOMW3gYfS4HsncCuHP9g/Jf0+R5XE3a9WkdJ+rlkdmsEHwLcPk2BUchtzzXRWFWlbA X-Received: by 2002:a17:907:7e9d:: with SMTP id qb29mr4815915ejc.571.1644344821800; Tue, 08 Feb 2022 10:27:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644344821; cv=none; d=google.com; s=arc-20160816; b=xzCEVSEpsJGnV2T8OvQ5mlAdEE9dnlegGsALv4oqr+a0d3j80d9okKAiDJRU5uYMef hgmBvAk3f+GxRoUguE7wOGDIe69Y21J+R4RxARE7igNpF6U8Pmuxfl1EOazVlq5x6CYY x2hJMrnhrnKIpUthIPCuXUpVz9hkxmh/KN6fhs76BAU8ZXSXJELvJ07k7qj2Qb4v4SAT HMM7N7A34oWrlKFN18Opgc6nqQBzIw4+RlWZW9fZ7O3LoGkAmf8q/jd09xN+42qfqlC7 p1LgHjUky22mOkpfWnd5kalrIK/bu8bCBNsqfyK2sKx3jG3HD8t0JWAK0JY9P+kytRnd 6CFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=vji7Ks0Lft35m/ZG3X+98oS/bwLRT9FsqyIIZ/NUd04=; b=YlwwQL5d1hnhyB7obvM93q6T6NY1mhyWpXWsxtT87yt89+3xK3fl6A1L10p9fG7FS1 1wN2rcNi5oiQPOqZ1vNH81vMtY/97ab/lyQ+GZuiv6Btm0XcAqSfooTHByuKAu7vXkxo eX+r7okiFDANc5EQc5xAI9iEO+rAyGppzm2iLb2CYtshQZX8PZ9D/kbN49BVkd8VXj3f hvF+zf6txWcv3cdqBBSrkFSN2C6t74V+t37jI7J7gcaYheAmR+dc2/qu4KZUTfcJvZTt OK7CdNQ9y5um75CYqezJYYcpekq0KYK+TCYNKCPMQtQcHYdpUpx5ldLlth7FBguCQkWg cACQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d27si5514943ejb.90.2022.02.08.10.27.01; Tue, 08 Feb 2022 10:27:01 -0800 (PST) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D87FA68B1E6; Tue, 8 Feb 2022 20:26:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1FE3168B183 for ; Tue, 8 Feb 2022 20:26:53 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B4B4E24017C for ; Tue, 8 Feb 2022 19:26:52 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id eAVE3skoD1jW for ; Tue, 8 Feb 2022 19:26:52 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 67AC3240175 for ; Tue, 8 Feb 2022 19:26:52 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 659B63A06CD; Tue, 8 Feb 2022 19:26:52 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 19:26:38 +0100 Message-Id: <20220208182639.25696-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] lavfi/qsvpp: fix after 85c938fa28 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GcdQWO6EjVUQ --- libavfilter/qsvvpp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavfilter/qsvvpp.c b/libavfilter/qsvvpp.c index 35769dfd60..954f882637 100644 --- a/libavfilter/qsvvpp.c +++ b/libavfilter/qsvvpp.c @@ -796,7 +796,7 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr AVFilterLink *outlink = ctx->outputs[0]; QSVAsyncFrame aframe; mfxSyncPoint sync; - QSVFrame *in_frame, *out_frame, *tmp; + QSVFrame *in_frame, *out_frame; int ret, filter_ret; while (s->eof && av_fifo_read(s->async_fifo, &aframe, 1) >= 0) { @@ -857,15 +857,15 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr ret = MFXVideoCORE_SyncOperation(s->session, aframe.sync, 1000); } while (ret == MFX_WRN_IN_EXECUTION); - filter_ret = s->filter_frame(outlink, tmp->frame); + filter_ret = s->filter_frame(outlink, aframe.frame->frame); if (filter_ret < 0) { - av_frame_free(&tmp->frame); + av_frame_free(&aframe.frame->frame); return filter_ret; } - tmp->queued--; + aframe.frame->queued--; s->got_frame = 1; - tmp->frame = NULL; + aframe.frame->frame = NULL; } } while(ret == MFX_ERR_MORE_SURFACE); From patchwork Tue Feb 8 18:26:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 34191 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1045426iov; Tue, 8 Feb 2022 10:27:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJy7BbldudyJgC0zhvlwCsYNJRyn8yJSi4DsobQ0EHYHD5J+Gvm1X17l+swsS2uChswvMd/L X-Received: by 2002:a17:906:7482:: with SMTP id e2mr2499824ejl.84.1644344831240; Tue, 08 Feb 2022 10:27:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644344831; cv=none; d=google.com; s=arc-20160816; b=iVb4w6bmStZQRYWJFAfq8hMBxso7TOROOVo/RnarFhpGjUuhxjFoF+nnsp9loxtSX5 7gAK+9oHkr3BVQZ4QJTJezr2dPc1y+PoaZPU9iTiiEI84EPixdZ4MVyg/kXsKYU+N8KA e+jlgl4egvOV5LFJne20QqpOuNVIg4NXhXBf7YOF/2tlsDVEAkeUM/GAjfVvXL3QKYxa IGF6fyRpmCEEuW2PHrwUTH6kFSq9fVfUFLjShVZYHZzmj2Pn+ZafO6AKuV4O77fFbHBK bBCItMQV+9hP2kQBU//lnqTpKEVn4nBM/+U0vagB03ntJVekR8YRKAUkQGd/Osi5T6oh Bycw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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:delivered-to; bh=mQdmrTgymo8Wgwn9DCgzsiA5BbOyZWv1T2zztpN36T0=; b=jcTbbcDmeiZiglB+wb+1a9dS3Qqm+N7S/GNPX4yn4wpOYXt/BwGJVbs9JaVyZWTjcH n/hcQgVWZMeKzAGTPmjBnsh5fxcsGwZQuMryhGlN8YWfegyGui8MXb5EiKsOkxYxZLPF LZ67iojsHsyjZrJA+mmuCwJqAmFzyd57bwiL5k8kLHfGBN8Ou4JkHVZmgijAbCpDCfEM QKZmzWn4rRZdzmUw4VJIIj7f9G0s4LSQGKYOGehgNdI6N9qRavCLaCkdw9s6F7WMVZ3V XcFbgMYlFsWVNHoAvyQWeumtzve4mqmgPAk4zT9SXDdiqu1R7l54/VRXQBlus7/aB+0Z OwLw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id jg25si10992201ejc.831.2022.02.08.10.27.10; Tue, 08 Feb 2022 10:27:11 -0800 (PST) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0ADF868B1F8; Tue, 8 Feb 2022 20:27:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6C86968B195 for ; Tue, 8 Feb 2022 20:26:53 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 0AFF5240175 for ; Tue, 8 Feb 2022 19:26:53 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id H2AUKh_ftamy for ; Tue, 8 Feb 2022 19:26:52 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 6EB21240179 for ; Tue, 8 Feb 2022 19:26:52 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 69F5B3A06EC; Tue, 8 Feb 2022 19:26:52 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 19:26:39 +0100 Message-Id: <20220208182639.25696-2-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220208182639.25696-1-anton@khirnov.net> References: <20220208182639.25696-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavu/hwcontext_qsv: fix a potential infinite loop 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: C0sDbohHghfA Current code will loop forever if MFXVideoVPP_Init() fails. Also, simplify the code. --- libavutil/hwcontext_qsv.c | 80 +++++++++++++++------------------------ 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c index d3d8f42c99..95f8071abe 100644 --- a/libavutil/hwcontext_qsv.c +++ b/libavutil/hwcontext_qsv.c @@ -16,6 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include @@ -71,12 +72,11 @@ typedef struct QSVDeviceContext { typedef struct QSVFramesContext { mfxSession session_download; - int session_download_init; + atomic_int session_download_init; mfxSession session_upload; - int session_upload_init; + atomic_int session_upload_init; #if HAVE_PTHREADS pthread_mutex_t session_lock; - pthread_cond_t session_cond; #endif AVBufferRef *child_frames_ref; @@ -297,7 +297,6 @@ static void qsv_frames_uninit(AVHWFramesContext *ctx) #if HAVE_PTHREADS pthread_mutex_destroy(&s->session_lock); - pthread_cond_destroy(&s->session_cond); #endif av_freep(&s->mem_ids); @@ -744,7 +743,6 @@ static int qsv_frames_init(AVHWFramesContext *ctx) #if HAVE_PTHREADS pthread_mutex_init(&s->session_lock, NULL); - pthread_cond_init(&s->session_cond, NULL); #endif return 0; @@ -1024,6 +1022,32 @@ static int map_frame_to_surface(const AVFrame *frame, mfxFrameSurface1 *surface) return 0; } +static int qsv_internal_session_check_init(AVHWFramesContext *ctx, int upload) +{ + QSVFramesContext *s = ctx->internal->priv; + atomic_int *inited = upload ? &s->session_upload_init : &s->session_download_init; + mfxSession *session = upload ? &s->session_upload : &s->session_download; + int ret = 0; + + if (atomic_load(inited)) + return 0; + +#if HAVE_PTHREADS + pthread_mutex_lock(&s->session_lock); +#endif + + 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 + + return ret; +} + static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src) { @@ -1035,28 +1059,7 @@ static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, mfxStatus err; int ret = 0; - while (!s->session_download_init && !s->session_download && !ret) { -#if HAVE_PTHREADS - if (pthread_mutex_trylock(&s->session_lock) == 0) { -#endif - if (!s->session_download_init) { - ret = qsv_init_internal_session(ctx, &s->session_download, 0); - if (s->session_download) - s->session_download_init = 1; - } -#if HAVE_PTHREADS - pthread_mutex_unlock(&s->session_lock); - pthread_cond_signal(&s->session_cond); - } else { - pthread_mutex_lock(&s->session_lock); - while (!s->session_download_init && !s->session_download) { - pthread_cond_wait(&s->session_cond, &s->session_lock); - } - pthread_mutex_unlock(&s->session_lock); - } -#endif - } - + ret = qsv_internal_session_check_init(ctx, 0); if (ret < 0) return ret; @@ -1109,28 +1112,7 @@ static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src_frame; int realigned = 0; - - while (!s->session_upload_init && !s->session_upload && !ret) { -#if HAVE_PTHREADS - if (pthread_mutex_trylock(&s->session_lock) == 0) { -#endif - if (!s->session_upload_init) { - ret = qsv_init_internal_session(ctx, &s->session_upload, 1); - if (s->session_upload) - s->session_upload_init = 1; - } -#if HAVE_PTHREADS - pthread_mutex_unlock(&s->session_lock); - pthread_cond_signal(&s->session_cond); - } else { - pthread_mutex_lock(&s->session_lock); - while (!s->session_upload_init && !s->session_upload) { - pthread_cond_wait(&s->session_cond, &s->session_lock); - } - pthread_mutex_unlock(&s->session_lock); - } -#endif - } + ret = qsv_internal_session_check_init(ctx, 1); if (ret < 0) return ret;