diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 058acd526321f240c217a66aae1ad090ad51ecc4..05cfd98cbe33f79df579a16c3e5312d34c9d25ac 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -808,6 +808,8 @@ resync:
                                     pkt->data, pkt->size);
             pkt->destruct = dstr;
             pkt->flags |= AV_PKT_FLAG_KEY;
+            if (size < 0)
+                av_free_packet(pkt);
         } else {
             /* XXX: How to handle B-frames in AVI? */
             pkt->dts = ast->frame_offset;