diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 325fe2033f3d845e487ea52d0ed83ab09e2aefc2..93b0431bdf4f6685d6fdcf9f757053943658b6a2 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -860,9 +860,12 @@ void MPV_common_end(MpegEncContext *s)
     s->last_picture_ptr=
     s->next_picture_ptr=
     s->current_picture_ptr= NULL;
+    s->linesize= s->uvlinesize= 0;
 
     for(i=0; i<3; i++)
         av_freep(&s->visualization_buffer[i]);
+
+    avcodec_default_free_buffers(s->avctx);
 }
 
 #ifdef CONFIG_ENCODERS
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 5e43d4452dc4f2aef1e862971f4f6cbe717032eb..a34a65db712b0b2a9ee7a93207a2c32e6b4471ac 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -31,7 +31,7 @@
 #include <stdarg.h>
 #include <limits.h>
 
-static void avcodec_default_free_buffers(AVCodecContext *s);
+void avcodec_default_free_buffers(AVCodecContext *s);
 
 void *av_mallocz(unsigned int size)
 {
@@ -760,7 +760,7 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
         avctx->codec->flush(avctx);
 }
 
-static void avcodec_default_free_buffers(AVCodecContext *s){
+void avcodec_default_free_buffers(AVCodecContext *s){
     int i, j;
 
     if(s->internal_buffer==NULL) return;