nut coded pts is unsigned, so we need to shift all timestamps to avoid any
negative pts
---
 libavformat/nut.h    |    1 +
 libavformat/nutenc.c |   11 +++++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libavformat/nut.h b/libavformat/nut.h
index 419b123..2b781dc 100644
--- a/libavformat/nut.h
+++ b/libavformat/nut.h
@@ -101,6 +101,7 @@ typedef struct {
     int header_count;
     AVRational *time_base;
     struct AVTreeNode *syncpoints;
+    int64_t global_delay;
 } NUTContext;
 
 extern const AVCodecTag ff_nut_subtitle_tags[];
diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index ae1c2f1..b597ea9 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -702,8 +702,15 @@ static int nut_write_packet(AVFormatContext *s, AVPacket 
*pkt){
     int store_sp=0;
     int ret;
 
-    if(pkt->pts < 0)
-        return -1;
+    /* Shift timestamps to avoid negative pts, which are not allowed in nut.
+       We use the smallest dts as a reference point because pts can be
+       out-of-order and we need to apply the global delay to all packets. */
+    if (pkt->dts < 0 && !nut->global_delay)
+        nut->global_delay = -pkt->dts;
+    if (nut->global_delay) {
+        pkt->pts += nut->global_delay;
+        pkt->dts += nut->global_delay;
+    }
 
     if(1LL<<(20+3*nut->header_count) <= avio_tell(bc))
         write_headers(s, bc);
-- 
1.7.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to