diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 7e1ef99234..e600ac3a1e 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -342,6 +342,36 @@ FF_ENABLE_DEPRECATION_WARNINGS
         avci->needs_close = 1;
     }
 
+#if FF_API_CODED_SIDE_DATA
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (av_codec_is_encoder(avctx->codec)) {
+        const AVPacketSideData *sd =
+            av_packet_side_data_set_get(&avctx->packet_side_data, AV_PKT_DATA_CPB_PROPERTIES);
+        if (sd) {
+            uint8_t *data;
+            AVPacketSideData *tmp = av_realloc_array(avctx->coded_side_data,
+                                                     avctx->nb_coded_side_data + 1U,
+                                                     sizeof(*tmp));
+            if (!tmp) {
+                ret = AVERROR(ENOMEM);
+                goto free_and_end;
+            }
+            avctx->coded_side_data = tmp;
+
+            data = av_memdup(sd->data, sd->size);
+            if (!data) {
+                ret = AVERROR(ENOMEM);
+                goto free_and_end;
+            }
+
+            avctx->coded_side_data[avctx->nb_coded_side_data].data   = data;
+            avctx->coded_side_data[avctx->nb_coded_side_data].size   = sd->size;
+            avctx->coded_side_data[avctx->nb_coded_side_data++].type = AV_PKT_DATA_CPB_PROPERTIES;
+        }
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
     ret=0;
 
     if (av_codec_is_decoder(avctx->codec)) {
@@ -477,10 +507,14 @@ av_cold int avcodec_close(AVCodecContext *avctx)
 
     av_packet_side_data_set_free(&avctx->packet_side_data);
 
+#if FF_API_CODED_SIDE_DATA
+FF_DISABLE_DEPRECATION_WARNINGS
     for (i = 0; i < avctx->nb_coded_side_data; i++)
         av_freep(&avctx->coded_side_data[i].data);
     av_freep(&avctx->coded_side_data);
     avctx->nb_coded_side_data = 0;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
     av_buffer_unref(&avctx->hw_frames_ctx);
     av_buffer_unref(&avctx->hw_device_ctx);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index dda8a2412b..946a3977d7 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1890,14 +1890,20 @@ typedef struct AVCodecContext {
 #define FF_CODEC_PROPERTY_CLOSED_CAPTIONS 0x00000002
 #define FF_CODEC_PROPERTY_FILM_GRAIN      0x00000004
 
+#if FF_API_CODED_SIDE_DATA
     /**
      * Additional data associated with the entire coded stream.
      *
      * - decoding: unused
      * - encoding: may be set by libavcodec after avcodec_open2().
+     * @deprecated use the context's @ref AVCodecContext.packet_side_data
+     *             "packet_side_data" field.
      */
+    attribute_deprecated
     AVPacketSideData *coded_side_data;
+    attribute_deprecated
     int            nb_coded_side_data;
+#endif
 
     /**
      * A reference to the AVHWFramesContext describing the input (for encoding)
@@ -2105,7 +2111,7 @@ typedef struct AVCodecContext {
      * Additional data associated with the entire stream.
      *
      * - decoding: set by user
-     * - encoding: unused
+     * - encoding: may be set by libavcodec after avcodec_open2().
      */
     AVPacketSideDataSet packet_side_data;
 } AVCodecContext;
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index bd4131db62..90e573890f 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1020,31 +1020,23 @@ AVCPBProperties *av_cpb_properties_alloc(size_t *size)
 
 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
 {
-    AVPacketSideData *tmp;
+    AVPacketSideData *sd;
     AVCPBProperties  *props;
     size_t size;
-    int i;
 
-    for (i = 0; i < avctx->nb_coded_side_data; i++)
-        if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
-            return (AVCPBProperties *)avctx->coded_side_data[i].data;
+    av_assert0(av_codec_is_encoder(avctx->codec));
+
+    sd = av_packet_side_data_set_get(&avctx->packet_side_data, AV_PKT_DATA_CPB_PROPERTIES);
+    if (sd)
+        return (AVCPBProperties *)sd->data;
 
     props = av_cpb_properties_alloc(&size);
     if (!props)
         return NULL;
 
-    tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
-    if (!tmp) {
+    if (!av_packet_side_data_set_add(&avctx->packet_side_data, AV_PKT_DATA_CPB_PROPERTIES,
+                                     (uint8_t *)props, size, 0))
         av_freep(&props);
-        return NULL;
-    }
-
-    avctx->coded_side_data = tmp;
-    avctx->nb_coded_side_data++;
-
-    avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
-    avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
-    avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
 
     return props;
 }
diff --git a/libavcodec/version_major.h b/libavcodec/version_major.h
index cfd0b5023e..440ee857da 100644
--- a/libavcodec/version_major.h
+++ b/libavcodec/version_major.h
@@ -49,6 +49,7 @@
 #define FF_API_SUBFRAMES           (LIBAVCODEC_VERSION_MAJOR < 61)
 #define FF_API_TICKS_PER_FRAME     (LIBAVCODEC_VERSION_MAJOR < 61)
 #define FF_API_DROPCHANGED         (LIBAVCODEC_VERSION_MAJOR < 61)
+#define FF_API_CODED_SIDE_DATA     (LIBAVCODEC_VERSION_MAJOR < 61)
 
 #define FF_API_AVFFT               (LIBAVCODEC_VERSION_MAJOR < 62)
 
