diff mbox

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

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

Commit Message

Dixit, Vishwanath April 4, 2018, 8:36 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       |  5 ++++-
 libavformat/dashenc.c | 13 +++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index a5358e3..9e8b8a4 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -228,7 +228,10 @@  ffmpeg -re -i <input> -map 0 -map 0 -c:a libfdk_aac -c:v libx264
 @item -min_seg_duration @var{microseconds}
 Set the segment length in microseconds (will be deprecated, use @var{seg_duration} instead).
 @item -seg_duration @var{duration}
-Set the segment length in seconds (fractional value can be set).
+Set the segment length in seconds (fractional value can be set). The value is
+treated as average segment duration when @var{use_template} is enabled and
+@var{use_timeline} is disabled and 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 8ef8627..54ccf30 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -1263,6 +1263,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);
@@ -1290,10 +1291,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,