Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Gyan Doshi <ffmpeg@gyani.pro>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH v2 2/2] ffmpeg: fix implementation of updated input start time
Date: Fri, 21 Oct 2022 22:01:50 +0530
Message-ID: <20221021163150.2196-2-ffmpeg@gyani.pro> (raw)
In-Reply-To: <20221021163150.2196-1-ffmpeg@gyani.pro>

The current adjustment of input start times just adjusts the tsoffset.
And it does so, by resetting the tsoffset to nullify the new start time.
This leads to breakage of -copyts, ignoring of input_ts_offset, breaking
of -isync as well as breaking wrap correction.

Fixed by taking cognizance of these parameters, and by correcting start times
just before sync offsets are applied.
---
 fftools/ffmpeg.c       |  2 +-
 fftools/ffmpeg.h       |  5 ++++-
 fftools/ffmpeg_demux.c |  4 ++--
 fftools/ffmpeg_opt.c   | 33 +++++++++++++++++++++++----------
 4 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0fe582be3b..c564b2649e 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1815,7 +1815,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
         start_time = 0;
         if (copy_ts) {
             start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
-            start_time += start_at_zero ? 0 : f->ctx->start_time;
+            start_time += start_at_zero ? 0 : f->start_time_effective;
         }
         if (ist->pts >= f->recording_time + start_time) {
             close_output_stream(ost);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 5966cac60e..2d97ccaa15 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -447,7 +447,10 @@ typedef struct InputFile {
     AVRational time_base; /* time base of the duration */
     int64_t input_ts_offset;
     int input_sync_ref;
-
+    /**
+     * Effective format start time based on enabled streams.
+     */
+    int64_t start_time_effective;
     int64_t ts_offset;
     /**
      * Extra timestamp offset added by discontinuity handling.
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 6e89f5999a..220fda56da 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -120,7 +120,7 @@ static int seek_to_start(InputFile *ifile)
 static void ts_fixup(InputFile *ifile, AVPacket *pkt, int *repeat_pict)
 {
     InputStream *ist = input_streams[ifile->ist_index + pkt->stream_index];
-    const int64_t start_time = ifile->ctx->start_time;
+    const int64_t start_time = ifile->start_time_effective;
     int64_t duration;
 
     if (debug_ts) {
@@ -367,7 +367,7 @@ int ifile_get_packet(InputFile *f, AVPacket **pkt)
     if (f->readrate || f->rate_emu) {
         int i;
         int64_t file_start = copy_ts * (
-                              (f->ctx->start_time != AV_NOPTS_VALUE ? f->ctx->start_time * !start_at_zero : 0) +
+                              (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
                               (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
                              );
         float scale = f->rate_emu ? 1.0 : f->readrate;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 28b7d4dc27..a6fa9b7801 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -216,13 +216,15 @@ int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id
     return 0;
 }
 
+/* Correct input file start times based on enabled streams */
 static void correct_input_start_times(void)
 {
-    // Correct starttime based on the enabled streams
     for (int i = 0; i < nb_input_files; i++) {
         InputFile       *ifile = input_files[i];
         AVFormatContext    *is = ifile->ctx;
-        int64_t new_start_time = INT64_MAX;
+        int64_t new_start_time = INT64_MAX, diff, abs_start_seek;
+
+        ifile->start_time_effective = is->start_time;
 
         if (is->start_time == AV_NOPTS_VALUE ||
             !(is->iformat->flags & AVFMT_TS_DISCONT))
@@ -234,9 +236,20 @@ static void correct_input_start_times(void)
                 continue;
             new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
         }
-        if (new_start_time > is->start_time) {
-            av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time);
-            ifile->ts_offset = -new_start_time;
+
+        diff = new_start_time - is->start_time;
+        if (diff) {
+            av_log(NULL, AV_LOG_VERBOSE, "Correcting start time of Input #%d by %"PRId64" us.\n", i, diff);
+            ifile->start_time_effective = new_start_time;
+            if (copy_ts && start_at_zero)
+                ifile->ts_offset = -new_start_time;
+            else if (!copy_ts) {
+                abs_start_seek = is->start_time + (ifile->start_time != AV_NOPTS_VALUE) ? ifile->start_time : 0;
+                ifile->ts_offset = abs_start_seek > new_start_time ? -abs_start_seek : -new_start_time;
+            } else if (copy_ts)
+                ifile->ts_offset = 0;
+
+            ifile->ts_offset += ifile->input_ts_offset;
         }
     }
 }
@@ -269,9 +282,9 @@ static int apply_sync_offsets(void)
         if (self->ctx->start_time_realtime != AV_NOPTS_VALUE && ref->ctx->start_time_realtime != AV_NOPTS_VALUE) {
             self_start_time = self->ctx->start_time_realtime;
             ref_start_time  =  ref->ctx->start_time_realtime;
-        } else if (self->ctx->start_time != AV_NOPTS_VALUE && ref->ctx->start_time != AV_NOPTS_VALUE) {
-            self_start_time = self->ctx->start_time;
-            ref_start_time  =  ref->ctx->start_time;
+        } else if (self->start_time_effective != AV_NOPTS_VALUE && ref->start_time_effective != AV_NOPTS_VALUE) {
+            self_start_time = self->start_time_effective;
+            ref_start_time  =  ref->start_time_effective;
         } else {
             start_times_set = 0;
         }
@@ -1918,8 +1931,6 @@ int ffmpeg_parse_options(int argc, char **argv)
         goto fail;
     }
 
-    apply_sync_offsets();
-
     /* create the complex filtergraphs */
     ret = init_complex_filters();
     if (ret < 0) {
@@ -1936,6 +1947,8 @@ int ffmpeg_parse_options(int argc, char **argv)
 
     correct_input_start_times();
 
+    apply_sync_offsets();
+
     check_filter_outputs();
 
 fail:
-- 
2.36.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".

  reply	other threads:[~2022-10-21 16:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21 16:31 [FFmpeg-devel] [PATCH v2 1/2] ffmpeg: shift start time correction to ffmpeg_opt Gyan Doshi
2022-10-21 16:31 ` Gyan Doshi [this message]
2022-10-25 15:58   ` [FFmpeg-devel] [PATCH v2 2/2] ffmpeg: fix implementation of updated input start time Gyan Doshi
2022-10-29 20:04     ` Gyan Doshi
2022-11-01 13:48       ` Gyan Doshi
2022-11-03  9:27         ` Gyan Doshi

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=20221021163150.2196-2-ffmpeg@gyani.pro \
    --to=ffmpeg@gyani.pro \
    --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