[FFmpeg-cvslog] flvenc: use first packet delay as global delay.

Justin Ruggles git at videolan.org
Thu Nov 3 02:23:14 CET 2011


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Wed Oct 26 12:43:18 2011 -0400| [905de1190796d3e62d6545e6af34f2c413a68a47] | committer: Justin Ruggles

flvenc: use first packet delay as global delay.

This keeps the streams sychronized. The packets must be interleaved per-DTS.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=905de1190796d3e62d6545e6af34f2c413a68a47
---

 libavformat/flvenc.c |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 0d1fa73..80ddcd8 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -57,10 +57,10 @@ typedef struct FLVContext {
     int64_t duration_offset;
     int64_t filesize_offset;
     int64_t duration;
+    int64_t delay;      ///< first dts delay (needed for AVC & Speex)
 } FLVContext;
 
 typedef struct FLVStreamContext {
-    int     delay;      ///< first dts delay for each stream (needed for AVC & Speex)
     int64_t last_ts;    ///< last timestamp for each stream
 } FLVStreamContext;
 
@@ -207,6 +207,8 @@ static int flv_write_header(AVFormatContext *s)
         s->streams[i]->priv_data = sc;
         sc->last_ts = -1;
     }
+    flv->delay = AV_NOPTS_VALUE;
+
     avio_write(pb, "FLV", 3);
     avio_w8(pb,1);
     avio_w8(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
@@ -416,10 +418,15 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return -1;
         }
     }
-    if (!sc->delay && pkt->dts < 0)
-        sc->delay = -pkt->dts;
+    if (flv->delay == AV_NOPTS_VALUE)
+        flv->delay = -pkt->dts;
+    if (pkt->dts < -flv->delay) {
+        av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
+                                  "respect to DTS\n");
+        return AVERROR(EINVAL);
+    }
 
-    ts = pkt->dts + sc->delay; // add delay to force positive dts
+    ts = pkt->dts + flv->delay; // add delay to force positive dts
 
     /* check Speex packet duration */
     if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
@@ -450,7 +457,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     avio_write(pb, data ? data : pkt->data, size);
 
     avio_wb32(pb,size+flags_size+11); // previous tag size
-    flv->duration = FFMAX(flv->duration, pkt->pts + sc->delay + pkt->duration);
+    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
 
     avio_flush(pb);
 



More information about the ffmpeg-cvslog mailing list