diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index fdb4276260..595bd0e54c 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1345,6 +1345,7 @@ typedef struct AVCodecContext {
      */
     int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
 
+#if FF_API_OLD_ENCDEC
     /**
      * If non-zero, the decoded audio and video frames returned from
      * avcodec_decode_video2() and avcodec_decode_audio4() are reference-counted
@@ -1360,6 +1361,7 @@ typedef struct AVCodecContext {
      */
     attribute_deprecated
     int refcounted_frames;
+#endif
 
     /* - encoding parameters */
     float qcompress;  ///< amount of qscale change between easy & hard scenes (0.0-1.0)
@@ -2982,6 +2984,7 @@ int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos);
  */
 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos);
 
+#if FF_API_OLD_ENCDEC
 /**
  * Decode the audio frame of size avpkt->size from avpkt->data into frame.
  *
@@ -3088,6 +3091,7 @@ attribute_deprecated
 int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
                          int *got_picture_ptr,
                          const AVPacket *avpkt);
+#endif
 
 /**
  * Decode a subtitle message.
@@ -3625,6 +3629,7 @@ void av_parser_close(AVCodecParserContext *s);
  * @{
  */
 
+#if FF_API_OLD_ENCDEC
 /**
  * Encode a frame of audio.
  *
@@ -3707,6 +3712,7 @@ int avcodec_encode_audio2(AVCodecContext *avctx, AVPacket *avpkt,
 attribute_deprecated
 int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt,
                           const AVFrame *frame, int *got_packet_ptr);
+#endif
 
 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
                             const AVSubtitle *sub);
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 9861ead073..8086362eb2 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -221,8 +221,10 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
     if (ret < 0)
         goto finish;
 
+#if FF_API_OLD_ENCDEC
     if (avctx->codec->receive_frame)
         avci->compat_decode_consumed += pkt->size;
+#endif
 
     return 0;
 finish:
@@ -469,7 +471,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
         }
     }
 
+#if FF_API_OLD_ENCDEC
     avci->compat_decode_consumed += ret;
+#endif
 
     if (ret >= pkt->size || ret < 0) {
         av_packet_unref(pkt);
@@ -693,6 +697,8 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
     return 0;
 }
 
+#if FF_API_OLD_ENCDEC
+FF_DISABLE_DEPRECATION_WARNINGS
 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
 {
     int ret;
@@ -834,6 +840,8 @@ int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
 {
     return compat_decode(avctx, frame, got_frame_ptr, avpkt);
 }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
 static void get_subtitle_defaults(AVSubtitle *sub)
 {
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 2e540baf37..29f41c3f92 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -356,6 +356,7 @@ int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *
     return 0;
 }
 
+#if FF_API_OLD_ENCDEC
 static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
                          int *got_packet, const AVFrame *frame)
 {
@@ -456,3 +457,4 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
 
     return ret;
 }
+#endif
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 141f3fb88e..65760368d6 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -131,7 +131,9 @@ typedef struct AVCodecInternal {
      */
     int last_audio_frame;
 
+#if FF_API_OLD_ENCDEC
     AVFrame *to_free;
+#endif
 
     AVBufferRef *pool;
 
@@ -179,6 +181,8 @@ typedef struct AVCodecInternal {
     AVPacket *buffer_pkt;
     AVFrame *buffer_frame;
     int draining_done;
+
+#if FF_API_OLD_ENCDEC
     int compat_decode_warned;
     /* this variable is set by the decoder internals to signal to the old
      * API compat wrappers the amount of data consumed from the last packet */
@@ -188,6 +192,7 @@ typedef struct AVCodecInternal {
     size_t compat_decode_partial_size;
     AVFrame *compat_decode_frame;
     AVPacket *compat_encode_packet;
+#endif
 
     int showed_multi_packet_warning;
 
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index 1b9d39a3a7..53fa065fc1 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -448,7 +448,9 @@ static const AVOption avcodec_options[] = {
 #if FF_API_ASS_TIMING
 {"ass_with_timings", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS}, INT_MIN, INT_MAX, S|D, "sub_text_format"},
 #endif
+#if FF_API_OLD_ENCDEC
 {"refcounted_frames", NULL, OFFSET(refcounted_frames), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|V|D },
+#endif
 #if FF_API_SIDEDATA_ONLY_PKT
 {"side_data_only_packets", NULL, OFFSET(side_data_only_packets), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, A|V|E },
 #endif
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index aeb9c6de72..a1ae9cab18 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -580,18 +580,23 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     }
     avctx->internal = avci;
 
+#if FF_API_OLD_ENCDEC
     avci->to_free = av_frame_alloc();
     avci->compat_decode_frame = av_frame_alloc();
     avci->compat_encode_packet = av_packet_alloc();
+    if (!avci->to_free || !avci->compat_decode_frame || !avci->compat_encode_packet) {
+        ret = AVERROR(ENOMEM);
+        goto free_and_end;
+    }
+#endif
     avci->buffer_frame = av_frame_alloc();
     avci->buffer_pkt = av_packet_alloc();
     avci->es.in_frame = av_frame_alloc();
     avci->ds.in_pkt = av_packet_alloc();
     avci->last_pkt_props = av_packet_alloc();
-    if (!avci->compat_decode_frame || !avci->compat_encode_packet ||
-        !avci->buffer_frame || !avci->buffer_pkt          ||
+    if (!avci->buffer_frame || !avci->buffer_pkt          ||
         !avci->es.in_frame  || !avci->ds.in_pkt           ||
-        !avci->to_free      || !avci->last_pkt_props) {
+        !avci->last_pkt_props) {
         ret = AVERROR(ENOMEM);
         goto free_and_end;
     }
@@ -1065,10 +1070,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
     av_freep(&avctx->priv_data);
     av_freep(&avctx->subtitle_header);
 
+#if FF_API_OLD_ENCDEC
     av_frame_free(&avci->to_free);
     av_frame_free(&avci->compat_decode_frame);
-    av_frame_free(&avci->buffer_frame);
     av_packet_free(&avci->compat_encode_packet);
+#endif
+    av_frame_free(&avci->buffer_frame);
     av_packet_free(&avci->buffer_pkt);
     av_packet_free(&avci->last_pkt_props);
 
@@ -1106,8 +1113,10 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
     avci->draining_done = 0;
     avci->nb_draining_errors = 0;
     av_frame_unref(avci->buffer_frame);
+#if FF_API_OLD_ENCDEC
     av_frame_unref(avci->compat_decode_frame);
     av_packet_unref(avci->compat_encode_packet);
+#endif
     av_packet_unref(avci->buffer_pkt);
 
     av_packet_unref(avci->last_pkt_props);
@@ -1128,8 +1137,12 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
     if (av_codec_is_decoder(avctx->codec))
         av_bsf_flush(avci->bsf);
 
+#if FF_API_OLD_ENCDEC
+FF_DISABLE_DEPRECATION_WARNINGS
     if (!avctx->refcounted_frames)
         av_frame_unref(avci->to_free);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 }
 
 void avsubtitle_free(AVSubtitle *sub)
@@ -1169,10 +1182,12 @@ av_cold int avcodec_close(AVCodecContext *avctx)
             avctx->codec->close(avctx);
         avctx->internal->byte_buffer_size = 0;
         av_freep(&avctx->internal->byte_buffer);
+#if FF_API_OLD_ENCDEC
         av_frame_free(&avctx->internal->to_free);
         av_frame_free(&avctx->internal->compat_decode_frame);
-        av_frame_free(&avctx->internal->buffer_frame);
         av_packet_free(&avctx->internal->compat_encode_packet);
+#endif
+        av_frame_free(&avctx->internal->buffer_frame);
         av_packet_free(&avctx->internal->buffer_pkt);
         av_packet_free(&avctx->internal->last_pkt_props);
         avpriv_packet_list_free(&avctx->internal->pkt_props,
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 50ad2671af..7a37890f9e 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -147,6 +147,9 @@
 #ifndef FF_API_AVPRIV_PUT_BITS
 #define FF_API_AVPRIV_PUT_BITS     (LIBAVCODEC_VERSION_MAJOR < 59)
 #endif
+#ifndef FF_API_OLD_ENCDEC
+#define FF_API_OLD_ENCDEC          (LIBAVCODEC_VERSION_MAJOR < 59)
+#endif
 #ifndef FF_API_THREAD_SAFE_CALLBACKS
 #define FF_API_THREAD_SAFE_CALLBACKS (LIBAVCODEC_VERSION_MAJOR < 60)
 #endif
