]> git.sesse.net Git - ffmpeg/commitdiff
avformat/mux: implement AVFMT_FLAG_SHORTEST
authorMichael Niedermayer <michael@niedermayer.cc>
Fri, 12 Aug 2016 19:28:08 +0000 (21:28 +0200)
committerMichael Niedermayer <michael@niedermayer.cc>
Sun, 11 Sep 2016 21:17:31 +0000 (23:17 +0200)
This will allow fixing several bugs with the -shortest option

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
doc/formats.texi
libavformat/avformat.h
libavformat/internal.h
libavformat/mux.c
libavformat/options.c
libavformat/options_table.h

index f79ebe28ac55cfe10c966f57e4513dcfca2aadaa..dda81e6f8d1e3a962f0c77dcb6bf37f879be9175 100644 (file)
@@ -61,6 +61,10 @@ Reduce the latency introduced by optional buffering
 Only write platform-, build- and time-independent data.
 This ensures that file and data checksums are reproducible and match between
 platforms. Its primary use is for regression testing.
+@item shortest
+Stop muxing at the end of the shortest stream.
+It may be needed to increase max_interleave_delta to avoid flusing the longer
+streams before EOF.
 @end table
 
 @item seek2any @var{integer} (@emph{input})
index 3ee705198cec9b75dc6dbd352551e99941848f0b..74915a1482295b0fc054ac3a37f01e1d01736d6f 100644 (file)
@@ -1448,6 +1448,7 @@ typedef struct AVFormatContext {
 #define AVFMT_FLAG_PRIV_OPT    0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)
 #define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Don't merge side data but keep it separate.
 #define AVFMT_FLAG_FAST_SEEK   0x80000 ///< Enable fast, but inaccurate seeks for some formats
+#define AVFMT_FLAG_SHORTEST   0x100000 ///< Stop muxing when the shortest stream stops.
 
     /**
      * Maximum size of the data read from input for determining
index f9278c5b9aded5dc59410f1d97587ebd682b23da..49244faf238071f97a5fd8ae6d330938bb443db8 100644 (file)
@@ -125,6 +125,11 @@ struct AVFormatInternal {
      */
     int header_written;
     int write_header_ret;
+
+    /**
+     * Timestamp of the end of the shortest stream.
+     */
+    int64_t shortest_end;
 };
 
 struct AVStreamInternal {
index a427f4659b940406faf26eb62e4792e379e6b7b0..176af59f1388e2277142784c874ad8e82109e8a0 100644 (file)
@@ -1032,6 +1032,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
     int stream_count = 0;
     int noninterleaved_count = 0;
     int i, ret;
+    int eof = flush;
 
     if (pkt) {
         if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
@@ -1084,6 +1085,44 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
         }
     }
 
+    if (s->internal->packet_buffer &&
+        eof &&
+        (s->flags & AVFMT_FLAG_SHORTEST) &&
+        s->internal->shortest_end == AV_NOPTS_VALUE) {
+        AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
+
+        s->internal->shortest_end = av_rescale_q(top_pkt->dts,
+                                       s->streams[top_pkt->stream_index]->time_base,
+                                       AV_TIME_BASE_Q);
+    }
+
+    if (s->internal->shortest_end != AV_NOPTS_VALUE) {
+        while (s->internal->packet_buffer) {
+            AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
+            AVStream *st;
+            int64_t top_dts = av_rescale_q(top_pkt->dts,
+                                        s->streams[top_pkt->stream_index]->time_base,
+                                        AV_TIME_BASE_Q);
+
+            if (s->internal->shortest_end + 1 >= top_dts)
+                break;
+
+            pktl = s->internal->packet_buffer;
+            st   = s->streams[pktl->pkt.stream_index];
+
+            s->internal->packet_buffer = pktl->next;
+            if (!s->internal->packet_buffer)
+                s->internal->packet_buffer_end = NULL;
+
+            if (st->last_in_packet_buffer == pktl)
+                st->last_in_packet_buffer = NULL;
+
+            av_packet_unref(&pktl->pkt);
+            av_freep(&pktl);
+            flush = 0;
+        }
+    }
+
     if (stream_count && flush) {
         AVStream *st;
         pktl = s->internal->packet_buffer;
index 04d9c454d3d5e9f5fab6c5ce2d5279dee16805b9..25a506eef86a6fe1223ccf41f204f207e859a47b 100644 (file)
@@ -143,6 +143,7 @@ AVFormatContext *avformat_alloc_context(void)
     }
     ic->internal->offset = AV_NOPTS_VALUE;
     ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
+    ic->internal->shortest_end = AV_NOPTS_VALUE;
 
     return ic;
 }
index 3b74d1b2fd752fceea6429ea8f0ae9fe69825e2a..699809a84c32d880239a720534b33d579553bbda 100644 (file)
@@ -54,6 +54,7 @@ static const AVOption avformat_options[] = {
 {"nobuffer", "reduce the latency introduced by optional buffering", 0, AV_OPT_TYPE_CONST, {.i64 = AVFMT_FLAG_NOBUFFER }, 0, INT_MAX, D, "fflags"},
 {"seek2any", "allow seeking to non-keyframes on demuxer level when supported", OFFSET(seek2any), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, D},
 {"bitexact", "do not write random/volatile data", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_BITEXACT }, 0, 0, E, "fflags" },
+{"shortest", "stop muxing with the shortest stream", 0, AV_OPT_TYPE_CONST, { .i64 = AVFMT_FLAG_SHORTEST }, 0, 0, E, "fflags" },
 {"analyzeduration", "specify how many microseconds are analyzed to probe the input", OFFSET(max_analyze_duration), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D},
 {"cryptokey", "decryption key", OFFSET(key), AV_OPT_TYPE_BINARY, {.dbl = 0}, 0, 0, D},
 {"indexmem", "max memory used for timestamp index (per stream)", OFFSET(max_index_size), AV_OPT_TYPE_INT, {.i64 = 1<<20 }, 0, INT_MAX, D},