[libav-devel] [PATCH 3/4] movenc: Allow setting start_dts/start_cts before writing actual packets

Martin Storsjö martin at martin.st
Tue Nov 10 22:43:04 CET 2015


By writing a zero-sized packet, the caller can communicate the
start_dts/start_cts for the stream without actually writing
the first packet.

This allows doing random-access writing of fragments when the
start dts of the stream isn't zero, so that the edit list in the moov
is written based on timestamps from the nominal start time signaled
via the zero-sized packet, while the first proper packet written
corresponds to a later fragment.
---
After the last round, I've learnt that we already support
zero-sized side-data-only packets, so this could be considered a
case of that (except that pts/dts isn't side data but actual main
data in AVPacket), so perhaps it isn't too brittle after all?

This approach feels more analoguous to normal muxing than passing
this info via a completely different mechanism.
---
 libavformat/movenc.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ac452b1..ddb0541 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3585,8 +3585,18 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
         int64_t frag_duration = 0;
         int size = pkt->size;
 
-        if (!pkt->size)
+        if (!pkt->size) {
+            if (trk->start_dts == AV_NOPTS_VALUE) {
+                trk->start_dts = pkt->dts;
+                if (pkt->pts != AV_NOPTS_VALUE)
+                    trk->start_cts = pkt->pts - pkt->dts;
+                else
+                    trk->start_cts = 0;
+                trk->frag_discont = 1; // We don't set end_pts here
+            }
+
             return 0;             /* Discard 0 sized packets */
+        }
 
         if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
             int i;
-- 
2.4.9 (Apple Git-60)



More information about the libav-devel mailing list