Browse code

lavf/segment: add initial_offset option

Should address trac ticket #2224.

Stefano Sabatini authored on 2013/07/05 21:28:38
Showing 3 changed files
... ...
@@ -741,6 +741,10 @@ Reset timestamps at the begin of each segment, so that each segment
741 741
 will start with near-zero timestamps. It is meant to ease the playback
742 742
 of the generated segments. May not work with some combinations of
743 743
 muxers/codecs. It is set to @code{0} by default.
744
+
745
+@item initial_offset @var{offset}
746
+Specify timestamp offset to apply to the output packet timestamps. The
747
+argument must be a time duration specification, and defaults to 0.
744 748
 @end table
745 749
 
746 750
 @subsection Examples
... ...
@@ -43,6 +43,7 @@ typedef struct SegmentListEntry {
43 43
     int index;
44 44
     double start_time, end_time;
45 45
     int64_t start_pts;
46
+    int64_t offset_pts;
46 47
     char filename[1024];
47 48
     struct SegmentListEntry *next;
48 49
 } SegmentListEntry;
... ...
@@ -91,6 +92,7 @@ typedef struct {
91 91
     int  write_header_trailer; /**< Set by a private option. */
92 92
 
93 93
     int reset_timestamps;  ///< reset timestamps at the begin of each segment
94
+    int64_t initial_offset;    ///< initial timestamps offset, expressed in microseconds
94 95
     char *reference_stream_specifier; ///< reference stream specifier
95 96
     int   reference_stream_index;
96 97
 
... ...
@@ -644,7 +646,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
644 644
     SegmentContext *seg = s->priv_data;
645 645
     AVFormatContext *oc = seg->avf;
646 646
     AVStream *st = s->streams[pkt->stream_index];
647
-    int64_t end_pts = INT64_MAX;
647
+    int64_t end_pts = INT64_MAX, offset;
648 648
     int start_frame = INT_MAX;
649 649
     int ret;
650 650
 
... ...
@@ -694,7 +696,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
694 694
         seg->is_first_pkt = 0;
695 695
     }
696 696
 
697
-    if (seg->reset_timestamps) {
697
+    /* todo: reindent */
698 698
         av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
699 699
                pkt->stream_index,
700 700
                av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
... ...
@@ -702,15 +704,16 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
702 702
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
703 703
 
704 704
         /* compute new timestamps */
705
+        offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
706
+                              AV_TIME_BASE_Q, st->time_base);
705 707
         if (pkt->pts != AV_NOPTS_VALUE)
706
-            pkt->pts -= av_rescale_q(seg->cur_entry.start_pts, AV_TIME_BASE_Q, st->time_base);
708
+            pkt->pts += offset;
707 709
         if (pkt->dts != AV_NOPTS_VALUE)
708
-            pkt->dts -= av_rescale_q(seg->cur_entry.start_pts, AV_TIME_BASE_Q, st->time_base);
710
+            pkt->dts += offset;
709 711
 
710 712
         av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
711 713
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
712 714
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
713
-    }
714 715
 
715 716
     ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
716 717
 
... ...
@@ -793,6 +796,7 @@ static const AVOption options[] = {
793 793
     { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
794 794
     { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
795 795
     { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
796
+    { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
796 797
     { NULL },
797 798
 };
798 799
 
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 55
33 33
 #define LIBAVFORMAT_VERSION_MINOR 12
34
-#define LIBAVFORMAT_VERSION_MICRO 100
34
+#define LIBAVFORMAT_VERSION_MICRO 101
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \