From patchwork Fri Jul 5 10:42:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 50365 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp4214941vqv; Fri, 5 Jul 2024 03:42:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXXLRRrLIK++Q+oGXt+Pom/q0NkWi7yEiNoSIVjtXuRmtV/4ixWk0K8ESRSinSEmj+2m4ZWdNDwkC4e9YPhbGFdvAGYoJOu3Hrtaw== X-Google-Smtp-Source: AGHT+IF89iaLtIGOt2HOZIlD3OnShzGoLsGVBp+cQr4BxdQbjNQhzuD5bCN73s6Vl5PPa4KyF9AH X-Received: by 2002:a17:906:4556:b0:a72:8c15:c73a with SMTP id a640c23a62f3a-a77ba72d772mr250941566b.66.1720176171959; Fri, 05 Jul 2024 03:42:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720176171; cv=none; d=google.com; s=arc-20160816; b=nwhkEIJj0ZOLYFnWqVexzysyAiuhl/xzKZPth1dbIZ7axSZlbTMZzCmZ0eiBx7yhQJ xKDgzcbQc3Gub/HFV+bp/Q1W7Bq5oozPhuPwspHy/cSEnT+tw57zYTrtRaHuQNmuW2aG pQpumEHclgPGWwBS0GtC1x+CFYOoZUrpeoRiozZ4nmR8wf1r9YpYB94F4DL7ufH/dMGw tpSGhCJ+KB2L3C5QHjbVNYGtI2RZ1CyhM6KhAcr37nUpgOxTaR8U9BTCDcb+IZ2dlehU 2prJ8qI7xOpw4kuTZWJhI+hb7DbKEC2I0nKL0aP54nNciow04F0Qm8GuQWocchH7v+JB 8NXw== 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 :dkim-signature:delivered-to; bh=TXhq+Tj7B2/RY+qGnwq9a2Pw4XOlcBCGQetfcTkGWKk=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=StEAURh3kZvmDkZVYsZceHkRO9SXuE+gX9KzP1wU6SbhbJ7A18h2kk3IGL/371ga8/ 3KnhMk667qekXCc1KbEXJ5Xjy4ZEkrH1h+bpXELMHLrIQvAH8cDPSxPRgm4769OFGziZ Rr431T+HBqPlNe6eVdc0ip1sVUd050u8az+HXJ1fTAWgSk7Bu6trZmqWB0rloFKEs+9N WUiaItX18XgC6Y1AsJi1GGK9VUJNN3WKTw4HFiDflsEHD4ZbFKNCrmOzaFixFSornODp gGVNeghdtPk6d23eJKQb6hpXSVPMHcZZaVixZYue0394nY1HnRgAoZEEOvzTKmrvR7sT FQoA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@khirnov.net header.s=mail header.b="Q3H/6P2Z"; 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 a640c23a62f3a-a77c5b0cf24si106435466b.367.2024.07.05.03.42.51; Fri, 05 Jul 2024 03:42:51 -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=@khirnov.net header.s=mail header.b="Q3H/6P2Z"; 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 6D82668DAF4; Fri, 5 Jul 2024 13:42:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A29B468DAE0 for ; Fri, 5 Jul 2024 13:42:39 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=Q3H/6P2Z; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 3D2814DE2 for ; Fri, 5 Jul 2024 12:42:38 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id BadrLvs5cInA for ; Fri, 5 Jul 2024 12:42:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1720176156; bh=/o4yPYOZy+jaFtlQF90smdBbCcrjTDcFHPHH2GtDdSs=; h=From:To:Subject:Date:From; b=Q3H/6P2ZePDpZsSLDyqkoS5cPUUPHP7GOgIVYal70ApDhVMkx7Hr7t3cJpiuqB5Gk jMFvu5rNo2S+81dzNRZAX66/WM52OV3+WmNZwTUugF/4X8ff+20SH2d9IhRzD8L2PL VgUDmk5aizF2DNZZKHihqsIBIrBD/ohJiZm6BEb4Rg/PaqGsIo/piBYkenpY1kQCCd ZzQ43gXqEjIzLACf1S5npqRVATq0WloPdrXU0rjDRQy+6MS6ww3mueRr4V/L7fPJpm 8S402fz/zJ1o9xafnegpovfv0cGF6SGma05anLoi1XRwhxWLA22Bb2AUn1Iv8Mm7Ks n0wB0JPvW5GNg== 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 mail1.khirnov.net (Postfix) with ESMTPS id E5D174D9C for ; Fri, 5 Jul 2024 12:42:36 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id B02CB3A02CA for ; Fri, 05 Jul 2024 12:42:36 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Jul 2024 12:42:31 +0200 Message-ID: <20240705104233.6703-1-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] fftools/ffmpeg_dec: improve detection of lavf-guessed durations 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: RFnaJK4H2ZvN Will be useful in following commit. --- fftools/ffmpeg_dec.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index 70de151301..c2bcf784b0 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -260,6 +260,10 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE; const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED; int64_t codec_duration = 0; + // difference between this and last frame's timestamps + const int64_t ts_diff = + (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE) ? + frame->pts - dp->last_frame_pts : -1; // XXX lavf currently makes up frame durations when they are not provided by // the container. As there is no way to reliably distinguish real container @@ -267,8 +271,13 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra // the container has timestamps. Eventually lavf should stop making up // durations, then this should be simplified. + // frame duration is unreliable (typically guessed by lavf) when it is equal + // to 1 and the actual duration of the last frame is more than 2x larger + const int duration_unreliable = frame->duration == 1 && ts_diff > 2 * frame->duration; + // prefer frame duration for containers with timestamps - if (frame->duration > 0 && (!ts_unreliable || fr_forced)) + if (fr_forced || + (frame->duration > 0 && !ts_unreliable && !duration_unreliable)) return frame->duration; if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) { @@ -285,9 +294,8 @@ static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *fra // when timestamps are available, repeat last frame's actual duration // (i.e. pts difference between this and last frame) - if (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE && - frame->pts > dp->last_frame_pts) - return frame->pts - dp->last_frame_pts; + if (ts_diff > 0) + return ts_diff; // try frame/codec duration if (frame->duration > 0)