From d8b321f49f09a3a3e3465df86efea5f16ade7e36 Mon Sep 17 00:00:00 2001
From: Steven Liu <lingjiujianke@gmail.com>
Date: Fri, 9 Sep 2016 19:58:05 +0800
Subject: [PATCH] avformat/segment: fix the duration error of use
 output_ts_offset

this commit is used for fix commit 1da00be009aa74400042bf470b9a5ffbd82a1c5e
because the option initial_offset will deprecated

Signed-off-by: Steven Liu <lingjiujianke@gmail.com>
---
 libavformat/segment.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index 33a5cf0..74ffe03 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -101,6 +101,7 @@ typedef struct SegmentContext {
     char *times_str;       ///< segment times specification string
     int64_t *times;        ///< list of segment interval specification
     int nb_times;          ///< number of elments in the times array
+    int64_t init_time;     ///< first segment start time
 
     char *frames_str;      ///< segment frame numbers specification string
     int *frames;           ///< list of frame number specification
@@ -642,6 +643,7 @@ static int seg_init(AVFormatContext *s)
     int ret;
     int i;
 
+    seg->init_time = 0;
     seg->segment_count = 0;
     if (!seg->write_header_trailer)
         seg->individual_header_trailer = 0;
@@ -837,7 +839,11 @@ calc_times:
             }
             seg->last_val = wrapped_val;
         } else {
-            end_pts = seg->time * (seg->segment_count + 1);
+            if (seg->init_time == 0) {
+                seg->init_time = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
+            } else {
+                end_pts = seg->time * (seg->segment_count + 1) + seg->init_time;
+            }
         }
     }
 
@@ -885,6 +891,10 @@ calc_times:
         av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
                seg->avf->filename, pkt->stream_index,
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
+        seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap * seg->segment_idx_wrap_nb;
+        seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
+        seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
+        seg->cur_entry.end_time = seg->cur_entry.start_time;
     }
 
     av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
-- 
2.7.4 (Apple Git-66)

