diff --git a/libavcodec/cyuv.c b/libavcodec/cyuv.c
index 1bf676a434df1b1cb12ee9515432ab848c49d3ee..267f7c230377f2c742ebc7cdf8187e3763148d77 100644
--- a/libavcodec/cyuv.c
+++ b/libavcodec/cyuv.c
@@ -98,6 +98,9 @@ static int cyuv_decode_frame(AVCodecContext *avctx,
     /* pixel data starts 48 bytes in, after 3x16-byte tables */
     stream_ptr = 48;
 
+    if(s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
     s->frame.reference = 0;
     if(avctx->get_buffer(avctx, &s->frame) < 0) {
         fprintf(stderr, "get_buffer() failed\n");
@@ -159,8 +162,6 @@ static int cyuv_decode_frame(AVCodecContext *avctx,
     *data_size=sizeof(AVFrame);
     *(AVFrame*)data= s->frame;
 
-    avctx->release_buffer(avctx, &s->frame);
-
     return buf_size;
 }
 
diff --git a/libavcodec/dv.c b/libavcodec/dv.c
index 2d89a3cce7f19bd54394a19d3e83e280a2507a64..32d4c3a271dfdd3c856c079e1f75b909b3c55dca 100644
--- a/libavcodec/dv.c
+++ b/libavcodec/dv.c
@@ -580,6 +580,9 @@ static int dvvideo_decode_frame(AVCodecContext *avctx,
     else
         avctx->aspect_ratio = 4.0 / 3.0;
 
+    if(s->picture.data[0])
+        avctx->release_buffer(avctx, &s->picture);
+    
     s->picture.reference= 0;
     if(avctx->get_buffer(avctx, &s->picture) < 0) {
         fprintf(stderr, "get_buffer() failed\n");
@@ -617,8 +620,6 @@ static int dvvideo_decode_frame(AVCodecContext *avctx,
     *data_size = sizeof(AVFrame);
     *(AVFrame*)data= s->picture;
     
-    avctx->release_buffer(avctx, &s->picture);
-    
     return packet_size;
 }
 
diff --git a/libavcodec/huffyuv.c b/libavcodec/huffyuv.c
index a4193fdea451eab3619296e3cc9fd315d271599b..c00f9a2bb13deeee870293c7b3b555294a513456 100644
--- a/libavcodec/huffyuv.c
+++ b/libavcodec/huffyuv.c
@@ -737,6 +737,9 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8
     
     init_get_bits(&s->gb, s->bitstream_buffer, buf_size*8);
 
+    if(p->data[0])
+        avctx->release_buffer(avctx, p);
+
     p->reference= 0;
     if(avctx->get_buffer(avctx, p) < 0){
         fprintf(stderr, "get_buffer() failed\n");
@@ -943,9 +946,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8
     emms_c();
     
     *picture= *p;
-    
-    avctx->release_buffer(avctx, p);
-
     *data_size = sizeof(AVFrame);
     
     return (get_bits_count(&s->gb)+31)/32*4;
diff --git a/libavcodec/indeo3.c b/libavcodec/indeo3.c
index 133b9513cb5f2310ad3eee30c573e486ed9683a3..327b24d0dad91d0c45e603786408a9b82fe4e715 100644
--- a/libavcodec/indeo3.c
+++ b/libavcodec/indeo3.c
@@ -1056,6 +1056,9 @@ static int indeo3_decode_frame(AVCodecContext *avctx,
 
     iv_decode_frame(s, buf, buf_size);
 
+    if(s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+
     s->frame.reference = 0;
     if(avctx->get_buffer(avctx, &s->frame) < 0) {
         fprintf(stderr, "get_buffer() failed\n");
@@ -1089,8 +1092,6 @@ static int indeo3_decode_frame(AVCodecContext *avctx,
     *data_size=sizeof(AVFrame);
     *(AVFrame*)data= s->frame;
 
-    avctx->release_buffer(avctx, &s->frame);
-
     return buf_size;
 }
 
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 0f57bee35683a0a4f364d9d2b01925d195ed8457..ea24c2a4881907d627a9224375bf97db95470a91 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -929,7 +929,7 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
     s->mb_skiped = 0;
 
     assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264);
-        
+
     /* mark&release old frames */
     if (s->pict_type != B_TYPE && s->last_picture_ptr) {
         avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
@@ -945,9 +945,15 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
             }
         }
     }
-    
 alloc:
     if(!s->encoding){
+        /* release non refernce frames */
+        for(i=0; i<MAX_PICTURE_COUNT; i++){
+            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
+                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
+            }
+        }
+
         i= find_unused_picture(s, 0);
     
         pic= (AVFrame*)&s->picture[i];
@@ -1041,12 +1047,14 @@ void MPV_frame_end(MpegEncContext *s)
     assert(i<MAX_PICTURE_COUNT);
 #endif    
 
-    /* release non refernce frames */
-    for(i=0; i<MAX_PICTURE_COUNT; i++){
-        if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
-            s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
+    if(s->encoding){
+        /* release non refernce frames */
+        for(i=0; i<MAX_PICTURE_COUNT; i++){
+            if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
+                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
+            }
+        }
     }
-    
     // clear copies, to avoid confusion
 #if 0
     memset(&s->last_picture, 0, sizeof(Picture));