diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 0d1fa7312ad0e3fc1c099cb09e1d4edc22dbbf10..80ddcd8989bb6b6d93bf56e21925e31c943b6eeb 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);