From 8a293cf6332922cccda2e68faeeb760682f66bc2 Mon Sep 17 00:00:00 2001
From: Rodger Combs <rodger.combs@gmail.com>
Date: Wed, 15 Oct 2014 05:00:02 -0500
Subject: [PATCH] Make copyts a tri-state, with `no` normalizing input
 timestamps.

This doesn't change the default or `-copyts` behavior, but allows
`-nocopyts` to make e.g. `-ss 50` have timestamps starting at 50 seconds,
even if the input file's timestamps start at a value other than zero.
---
 ffmpeg.c     | 6 +++---
 ffmpeg_opt.c | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 17fa8b6..fff9d49 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -985,7 +985,7 @@ static void do_video_out(AVFormatContext *s,
             && input_files[ist->file_index]->input_ts_offset == 0) {
             format_video_sync = VSYNC_VSCFR;
         }
-        if (format_video_sync == VSYNC_CFR && copy_ts) {
+        if (format_video_sync == VSYNC_CFR && copy_ts == 1) {
             format_video_sync = VSYNC_VSCFR;
         }
     }
@@ -3632,7 +3632,7 @@ static int process_input(int file_index)
 
     if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
          ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
-        pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts
+        pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && copy_ts != 1
         && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) {
         int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
         int64_t delta   = pkt_dts - ifile->last_ts;
@@ -3651,7 +3651,7 @@ static int process_input(int file_index)
     if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
          ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
          pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
-        !copy_ts) {
+         copy_ts != 1) {
         int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
         int64_t delta   = pkt_dts - ist->next_dts;
         if (is->iformat->flags & AVFMT_TS_DISCONT) {
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 20fc63c..59649b6 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -94,7 +94,7 @@ int do_benchmark      = 0;
 int do_benchmark_all  = 0;
 int do_hex_dump       = 0;
 int do_pkt_dump       = 0;
-int copy_ts           = 0;
+int copy_ts           = -1;
 int copy_tb           = -1;
 int debug_ts          = 0;
 int exit_on_error     = 0;
@@ -926,7 +926,7 @@ static int open_input_file(OptionsContext *o, const char *filename)
     f->start_time = o->start_time;
     f->recording_time = o->recording_time;
     f->input_ts_offset = o->input_ts_offset;
-    f->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
+    f->ts_offset  = o->input_ts_offset - (copy_ts == 1 ? 0 : (copy_ts ? timestamp : ((ic->start_time == AV_NOPTS_VALUE) ? 0 : ic->start_time)));
     f->nb_streams = ic->nb_streams;
     f->rate_emu   = o->rate_emu;
     f->accurate_seek = o->accurate_seek;
-- 
1.9.1

