Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 1/3] fftools/ffmpeg_dec: improve detection of lavf-guessed durations
Date: Fri,  5 Jul 2024 12:42:31 +0200
Message-ID: <20240705104233.6703-1-anton@khirnov.net> (raw)

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)
-- 
2.43.0

_______________________________________________
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".

             reply	other threads:[~2024-07-05 10:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-05 10:42 Anton Khirnov [this message]
2024-07-05 10:42 ` [FFmpeg-devel] [PATCH 2/3] fftools/ffmpeg_mux_init: default to input timebase for streamcopy Anton Khirnov
2024-07-05 10:42 ` [FFmpeg-devel] [PATCH 3/3] lavf: deprecate avformat_transfer_internal_stream_timing_info() Anton Khirnov
2024-07-05 16:01   ` James Almer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240705104233.6703-1-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git