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 8/9] fftools/ffmpeg: use a separate variable for discontinuity offset
Date: Wed, 10 Aug 2022 18:25:44 +0200
Message-ID: <20220810162545.21382-8-anton@khirnov.net> (raw)
In-Reply-To: <20220810162545.21382-1-anton@khirnov.net>

This will allow to move normal offset handling to demuxer thread, since
discontinuities currently have to be processed in the main thread, as
the code uses some decoder-produced values.
---
 fftools/ffmpeg.c | 39 ++++++++++++++++++++++++++++-----------
 fftools/ffmpeg.h |  4 ++++
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index dd45cb4516..8d85af1c97 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3693,8 +3693,8 @@ static void decode_flush(InputFile *ifile)
     }
 }
 
-static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
-                                     AVPacket *pkt)
+static void ts_discontinuity_detect(InputFile *ifile, InputStream *ist,
+                                    AVPacket *pkt)
 {
     const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
     int disable_discontinuity_correction = copy_ts;
@@ -3715,13 +3715,13 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
         if (fmt_is_discont) {
             if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
                 pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) {
-                ifile->ts_offset -= delta;
+                ifile->ts_offset_discont -= delta;
                 av_log(NULL, AV_LOG_DEBUG,
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
                        av_get_media_type_string(ist->st->codecpar->codec_type),
-                       delta, ifile->ts_offset);
+                       delta, ifile->ts_offset_discont);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
                     pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
@@ -3744,10 +3744,10 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
                fmt_is_discont && ifile->last_ts != AV_NOPTS_VALUE) {
         int64_t delta = pkt_dts - ifile->last_ts;
         if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
-            ifile->ts_offset -= delta;
+            ifile->ts_offset_discont -= delta;
             av_log(NULL, AV_LOG_DEBUG,
                    "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
-                   delta, ifile->ts_offset);
+                   delta, ifile->ts_offset_discont);
             pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
             if (pkt->pts != AV_NOPTS_VALUE)
                 pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
@@ -3757,6 +3757,26 @@ static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
     ifile->last_ts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
 }
 
+static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
+                                     AVPacket *pkt)
+{
+    int64_t offset = av_rescale_q(ifile->ts_offset_discont, AV_TIME_BASE_Q,
+                                  ist->st->time_base);
+
+    // apply previously-detected timestamp-discontinuity offset
+    // (to all streams, not just audio/video)
+    if (pkt->dts != AV_NOPTS_VALUE)
+        pkt->dts += offset;
+    if (pkt->pts != AV_NOPTS_VALUE)
+        pkt->pts += offset;
+
+    // detect timestamp discontinuities for audio/video
+    if ((ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
+         ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
+        pkt->dts != AV_NOPTS_VALUE)
+        ts_discontinuity_detect(ifile, ist, pkt);
+}
+
 /*
  * Return
  * - 0 -- one packet was read and processed
@@ -3897,11 +3917,8 @@ static int process_input(int file_index)
     if (pkt->dts != AV_NOPTS_VALUE)
         pkt->dts += duration;
 
-    // detect and correct timestamp discontinuities for audio/video
-    if ((ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
-         ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) &&
-        pkt->dts != AV_NOPTS_VALUE)
-        ts_discontinuity_process(ifile, ist, pkt);
+    // detect and try to correct for timestamp discontinuities
+    ts_discontinuity_process(ifile, ist, pkt);
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 8b2e73d642..6991ba7632 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -427,6 +427,10 @@ typedef struct InputFile {
     int input_sync_ref;
 
     int64_t ts_offset;
+    /**
+     * Extra timestamp offset added by discontinuity handling.
+     */
+    int64_t ts_offset_discont;
     int64_t last_ts;
     int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
     int64_t recording_time;
-- 
2.34.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".

  parent reply	other threads:[~2022-08-10 16:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-10 16:25 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: mark all encode sync queues as done before flushing encoders Anton Khirnov
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg_mux: avoid leaking pkt on errors Anton Khirnov
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: move stream-dependent starttime correction to transcode_init() Anton Khirnov
2022-08-10 22:33   ` Michael Niedermayer
2022-08-11  7:45     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
2022-08-12 15:43       ` Michael Niedermayer
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 4/9] fftools/ffmpeg: pre-compute the streamcopy start pts before transcoding starts Anton Khirnov
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg: move timestamp discontinuity correction out of process_input() Anton Khirnov
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: move inter-stream ts discontinuity handling to ts_discontinuity_process() Anton Khirnov
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 7/9] fftools/ffmpeg: simplify conditions in ts_discontinuity_process Anton Khirnov
2022-08-10 16:25 ` Anton Khirnov [this message]
2022-08-10 16:25 ` [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move packet timestamp processing to demuxer thread Anton Khirnov

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=20220810162545.21382-8-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