diff --git a/libavcodec/rasc.c b/libavcodec/rasc.c
index eb021681c6685e8503de2595ac09ec189b847efb..e8e0740ddd5f3fa9888a94914aadc4b806d45e73 100644
--- a/libavcodec/rasc.c
+++ b/libavcodec/rasc.c
@@ -95,10 +95,10 @@ static int init_frames(AVCodecContext *avctx)
     int ret;
 
     av_frame_unref(s->frame1);
+    av_frame_unref(s->frame2);
     if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0)
         return ret;
 
-    av_frame_unref(s->frame2);
     if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0)
         return ret;