diff mbox

[FFmpeg-devel,02/11] avformat/dashenc: segmentation at the configured segment duration rate

Message ID 1521782773-25778-1-git-send-email-vdixit@akamai.com
State Superseded
Headers show

Commit Message

Dixit, Vishwanath March 23, 2018, 5:26 a.m. UTC
From: Vishwanath Dixit <vdixit@akamai.com>

When use_template is enabled and use_timeline is disabled, typically
it is required to generate the segments at the configured segment duration
rate on an average. This commit is particularly needed to handle the
segmentation when video frame rates are fractional like 29.97 or 59.94 fps.
---
 doc/muxers.texi       |  4 +++-
 libavformat/dashenc.c | 15 ++++++++++++---
 2 files changed, 15 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 65eec92..7130cd0 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -226,7 +226,9 @@  ffmpeg -re -i <input> -map 0 -map 0 -c:a libfdk_aac -c:v libx264
 
 @table @option
 @item -seg_duration @var{microseconds}
-Set the segment length in microseconds.
+Set the segment length in microseconds. The value is treated as average segment
+duration when use_template is enabled and use_timeline is disabled or as minimum
+segment duration for all the other use cases.
 @item -window_size @var{size}
 Set the maximum number of segments kept in the manifest.
 @item -extra_window_size @var{size}
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index bdd5b56..a489f5e 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -1257,6 +1257,7 @@  static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
     DASHContext *c = s->priv_data;
     AVStream *st = s->streams[pkt->stream_index];
     OutputStream *os = &c->streams[pkt->stream_index];
+    int64_t seg_end_duration, elapsed_duration;
     int ret;
 
     ret = update_stream_extradata(s, os, st->codecpar);
@@ -1284,10 +1285,18 @@  static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (os->first_pts == AV_NOPTS_VALUE)
         os->first_pts = pkt->pts;
 
+    if (c->use_template && !c->use_timeline) {
+        elapsed_duration = pkt->pts - os->first_pts;
+        seg_end_duration = (int64_t) os->segment_index * c->seg_duration;
+    } else {
+        elapsed_duration = pkt->pts - os->start_pts;
+        seg_end_duration = c->seg_duration;
+    }
+
     if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
         pkt->flags & AV_PKT_FLAG_KEY && os->packets_written &&
-        av_compare_ts(pkt->pts - os->start_pts, st->time_base,
-                      c->seg_duration, AV_TIME_BASE_Q) >= 0) {
+        av_compare_ts(elapsed_duration, st->time_base,
+                      seg_end_duration, AV_TIME_BASE_Q) >= 0) {
         int64_t prev_duration = c->last_duration;
 
         c->last_duration = av_rescale_q(pkt->pts - os->start_pts,
@@ -1427,7 +1436,7 @@  static const AVOption options[] = {
     { "adaptation_sets", "Adaptation sets. Syntax: id=0,streams=0,1,2 id=1,streams=3,4 and so on", OFFSET(adaptation_sets), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
     { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
     { "extra_window_size", "number of segments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E },
-    { "seg_duration", "minimum segment duration (in microseconds)", OFFSET(seg_duration), AV_OPT_TYPE_INT, { .i64 = 5000000 }, 0, INT_MAX, E },
+    { "seg_duration", "value in microseconds, average segment duration when use_template is enabled and use_timeline is disabled or minimum segment duration in other cases ", OFFSET(seg_duration), AV_OPT_TYPE_INT, { .i64 = 5000000 }, 0, INT_MAX, E },
     { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
     { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
     { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },