From patchwork Fri Dec 15 07:00:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 45156 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1225:b0:181:818d:5e7f with SMTP id v37csp5470828pzf; Thu, 14 Dec 2023 23:01:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IEF95WujcCWH8e0D/Ro2TVIy80lohZs8IMXcMPWx2XA0HAhGAxNGZHHKOiRfP8JtC6jp8PP X-Received: by 2002:a17:906:5a6b:b0:a18:a5b1:31a9 with SMTP id my43-20020a1709065a6b00b00a18a5b131a9mr11726242ejc.12.1702623680212; Thu, 14 Dec 2023 23:01:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702623680; cv=none; d=google.com; s=arc-20160816; b=Hhm5AC2ZDVertqt0O4Penqi/ooyfkLMXRgEoSJOkR2uQcFULrurCK5ljxkYre2cRMx B+CzQKa3Hu7Hy9JUJR8M0g+g6QrsNE3oMyaOAVG65xtJ5oLQus/FHjBxyQRWVJUTK8tb Z/y82pwTqY/6tHFUeK5gRSuTJ09SBSc+5VbXkh2vNvu4MSh69nCd7CaTVjdWPjo49OYd Qc9PlVWT4Qiltb+TlcJoYUkBQkwJotQmMP3boArfGAsgFbc5mxQqIARSVkjtbwha3jot yFHQtaF0V4gRBkDnzGGzR8o4/wF3tyHoCoZZPKHXFvtUkrkPxjTIeIzxoTyTi4gMo03Y TmmA== 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=Ca1PNlReuo3m69myhXnmM93sEEYib4pYzIkv9xWsw7M=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=tTfLtJ8Re9acBaS+1RZ3DFC1v6ZVFUUJtK4PN+9EX8ksj8VvVAgobHl5Fxns0yKYM/ jqJJuN8tmmn7T7Y2kCpNlp0Xs4dtGek+jFOBJqOJEs3WuPxN2ypLt1sLua0f3ejo7+6W ZgXQKdTOUFCXVRHviqfYABXkmmndnGE6kU613Ffqk5S/reyy2hOKsOh8W5vIekaNkMDY Ft6fE6dJz7xVx0vGA/rteoXiM3us3ZuGOzJdQPoiYGrJfEaWGfX9ItPM38Ey9t/c8ZYP kunqA+oMVOTW3+JnlcwEpoUm4Ubgo/8BSWMWSLXMZJvdFWc832miT7H8qVAKsCR9kO+D r1Bg== 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 s23-20020a50d497000000b00552616c8145si1526180edi.426.2023.12.14.23.01.19; Thu, 14 Dec 2023 23:01:20 -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 339DA68CD4B; Fri, 15 Dec 2023 09:01:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1C11D68D15C for ; Fri, 15 Dec 2023 09:01:07 +0200 (EET) Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4Ss0Vm2YYbz9t1v for ; Fri, 15 Dec 2023 08:01:04 +0100 (CET) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Fri, 15 Dec 2023 12:30:46 +0530 Message-Id: <20231215070046.771-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] swr/swresample: avoid reapplication of firstpts 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: dIta8AxCyD+k During a resampling operation where 1) user has specified first_pts 2) SWR_FLAG_RESAMPLE is not set initially (directly or otherwise) 3) first_pts has been fulfilled (always using hard compensation) then upon first encountering a delay where a soft compensation is required, swr_set_compensation will lead to another init of swr which will reset outpts to the specified firstpts thus leading to an output frame having its pts = firstpts. When the next input frame is received, swr will see a large delay and inject silence from firstpts to the current frame's pts. This can lead to severe desync and in worst case, loss of audio playback. Parameter firstpts initialized to AV_NOPTS_VALUE in swr_alloc and then checked in swr_init to avoid resetting outpts, thus avoiding reapplication of firstpts. Fixes #4131. --- libswresample/options.c | 1 + libswresample/swresample.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libswresample/options.c b/libswresample/options.c index fb109fdbab..d8cf85c053 100644 --- a/libswresample/options.c +++ b/libswresample/options.c @@ -171,6 +171,7 @@ av_cold struct SwrContext *swr_alloc(void){ if(s){ s->av_class= &av_class; av_opt_set_defaults(s); + s->firstpts = AV_NOPTS_VALUE; } return s; } diff --git a/libswresample/swresample.c b/libswresample/swresample.c index f2a9b40474..1cf83a803f 100644 --- a/libswresample/swresample.c +++ b/libswresample/swresample.c @@ -375,8 +375,9 @@ av_cold int swr_init(struct SwrContext *s){ if (s->firstpts_in_samples != AV_NOPTS_VALUE) { if (!s->async && s->min_compensation >= FLT_MAX/2) s->async = 1; - s->firstpts = - s->outpts = s->firstpts_in_samples * s->out_sample_rate; + if (s->firstpts == AV_NOPTS_VALUE) + s->firstpts = + s->outpts = s->firstpts_in_samples * s->out_sample_rate; } else s->firstpts = AV_NOPTS_VALUE;