Browse code

lavf/segment: add option to write empty filler segments as needed

Rodger Combs authored on 2016/03/07 11:48:57
Showing 2 changed files
... ...
@@ -1238,6 +1238,11 @@ muxers/codecs. It is set to @code{0} by default.
1238 1238
 @item initial_offset @var{offset}
1239 1239
 Specify timestamp offset to apply to the output packet timestamps. The
1240 1240
 argument must be a time duration specification, and defaults to 0.
1241
+
1242
+@item write_empty_segments @var{1|0}
1243
+If enabled, write an empty segment if there are no packets during the period a
1244
+segment would usually span. Otherwise, the segment will be filled with the next
1245
+packet written. Defaults to @code{0}.
1241 1246
 @end table
1242 1247
 
1243 1248
 @subsection Examples
... ...
@@ -118,6 +118,7 @@ typedef struct SegmentContext {
118 118
     char *reference_stream_specifier; ///< reference stream specifier
119 119
     int   reference_stream_index;
120 120
     int   break_non_keyframes;
121
+    int   write_empty;
121 122
 
122 123
     int use_rename;
123 124
     char temp_list_filename[1024];
... ...
@@ -810,6 +811,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
810 810
     if (!seg->avf)
811 811
         return AVERROR(EINVAL);
812 812
 
813
+calc_times:
813 814
     if (seg->times) {
814 815
         end_pts = seg->segment_count < seg->nb_times ?
815 816
             seg->times[seg->segment_count] : INT64_MAX;
... ...
@@ -841,7 +843,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
841 841
 
842 842
     if (pkt->stream_index == seg->reference_stream_index &&
843 843
         (pkt->flags & AV_PKT_FLAG_KEY || seg->break_non_keyframes) &&
844
-        seg->segment_frame_count > 0 &&
844
+        (seg->segment_frame_count > 0 || seg->write_empty) &&
845 845
         (seg->cut_pending || seg->frame_count >= start_frame ||
846 846
          (pkt->pts != AV_NOPTS_VALUE &&
847 847
           av_compare_ts(pkt->pts, st->time_base,
... ...
@@ -861,6 +863,9 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
861 861
         seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
862 862
         seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
863 863
         seg->cur_entry.end_time = seg->cur_entry.start_time;
864
+
865
+        if (seg->times || (!seg->frames && !seg->use_clocktime) && seg->write_empty)
866
+            goto calc_times;
864 867
     }
865 868
 
866 869
     if (pkt->stream_index == seg->reference_stream_index) {
... ...
@@ -1010,6 +1015,7 @@ static const AVOption options[] = {
1010 1010
     { "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
1011 1011
     { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
1012 1012
     { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
1013
+    { "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
1013 1014
     { NULL },
1014 1015
 };
1015 1016