diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 3b3835950c74117a1edc182e41f087b0f847b9a3..244b9ed4c2c30391259053463f976248353e16d0 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -892,6 +892,8 @@ static void mpegts_push_data(MpegTSFilter *filter,
             /* PES packing parsing */
         case MPEGTS_PESHEADER_FILL:
             len = pes->pes_header_size - pes->data_index;
+            if (len < 0)
+                return
             if (len > buf_size)
                 len = buf_size;
             memcpy(pes->header + pes->data_index, p, len);