diff --git a/libavcodec/rawenc.c b/libavcodec/rawenc.c
index bca0d08b7ad1808bcbde185d08b96ba4ede876bc..d6e800934143f08a1f8ca35ee55e82b2b8ab3c57 100644
--- a/libavcodec/rawenc.c
+++ b/libavcodec/rawenc.c
@@ -35,7 +35,10 @@ static av_cold int raw_encode_init(AVCodecContext *avctx)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
 
-    avctx->coded_frame            = avctx->priv_data;
+    avctx->coded_frame            = av_frame_alloc();
+    if (!avctx->coded_frame)
+        return AVERROR(ENOMEM);
+
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
     avctx->coded_frame->key_frame = 1;
     avctx->bits_per_coded_sample = av_get_bits_per_pixel(desc);
@@ -69,12 +72,18 @@ static int raw_encode(AVCodecContext *avctx, AVPacket *pkt,
     return 0;
 }
 
+static av_cold int raw_encode_close(AVCodecContext *avctx)
+{
+    av_frame_free(&avctx->coded_frame);
+    return 0;
+}
+
 AVCodec ff_rawvideo_encoder = {
     .name           = "rawvideo",
     .long_name      = NULL_IF_CONFIG_SMALL("raw video"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_RAWVIDEO,
-    .priv_data_size = sizeof(AVFrame),
     .init           = raw_encode_init,
+    .close          = raw_encode_close,
     .encode2        = raw_encode,
 };