[FFmpeg-devel] [PATCH 02/12] avcodec/codec_par: add side data to AVCodecParameters

James Almer jamrial at gmail.com
Mon Aug 28 15:34:54 EEST 2023


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/codec_par.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/codec_par.h |  6 ++++++
 2 files changed, 49 insertions(+)

diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c
index a38a475dc7..a932323759 100644
--- a/libavcodec/codec_par.c
+++ b/libavcodec/codec_par.c
@@ -27,11 +27,13 @@
 #include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_par.h"
+#include "packet.h"
 
 static void codec_parameters_reset(AVCodecParameters *par)
 {
     av_freep(&par->extradata);
     av_channel_layout_uninit(&par->ch_layout);
+    av_packet_side_data_set_free(&par->side_data);
 
     memset(par, 0, sizeof(*par));
 
@@ -72,6 +74,37 @@ void avcodec_parameters_free(AVCodecParameters **ppar)
     av_freep(ppar);
 }
 
+static int codec_parameters_copy_side_data(AVPacketSideDataSet *dst,
+                                           const AVPacketSideDataSet *src)
+{
+    if (!src->nb_sd)
+        return 0;
+
+    dst->nb_sd = 0;
+    dst->sd = av_calloc(src->nb_sd, sizeof(*dst->sd));
+    if (!dst->sd)
+        return AVERROR(ENOMEM);
+
+    for (int i = 0; i < src->nb_sd; i++) {
+        AVPacketSideData *sd = av_mallocz(sizeof(*sd));
+
+        if (!sd)
+            return AVERROR(ENOMEM);
+
+        sd->data = av_memdup(src->sd[i]->data, src->sd[i]->size);
+        if (!sd->data) {
+            return AVERROR(ENOMEM);
+            av_free(sd);
+        }
+
+        sd->type = src->sd[i]->type;
+        sd->size = src->sd[i]->size;
+        dst->sd[dst->nb_sd++] = sd;
+    }
+
+    return 0;
+}
+
 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
 {
     int ret;
@@ -89,6 +122,9 @@ int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src
         memcpy(dst->extradata, src->extradata, src->extradata_size);
         dst->extradata_size = src->extradata_size;
     }
+    ret = codec_parameters_copy_side_data(&dst->side_data, &src->side_data);
+    if (ret < 0)
+        return ret;
 
     ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
     if (ret < 0)
@@ -177,6 +213,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
         memcpy(par->extradata, codec->extradata, codec->extradata_size);
         par->extradata_size = codec->extradata_size;
     }
+    ret = codec_parameters_copy_side_data(&par->side_data, &codec->packet_side_data);
+    if (ret < 0)
+        return ret;
 
     return 0;
 }
@@ -261,6 +300,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
         memcpy(codec->extradata, par->extradata, par->extradata_size);
         codec->extradata_size = par->extradata_size;
     }
+    av_packet_side_data_set_free(&codec->packet_side_data);
+    ret = codec_parameters_copy_side_data(&codec->packet_side_data, &par->side_data);
+    if (ret < 0)
+        return ret;
 
     return 0;
 }
diff --git a/libavcodec/codec_par.h b/libavcodec/codec_par.h
index add90fdb1e..169e595b7c 100644
--- a/libavcodec/codec_par.h
+++ b/libavcodec/codec_par.h
@@ -29,6 +29,7 @@
 #include "libavutil/pixfmt.h"
 
 #include "codec_id.h"
+#include "packet.h"
 
 /**
  * @addtogroup lavc_core
@@ -223,6 +224,11 @@ typedef struct AVCodecParameters {
      * when no higher-level timing information is available.
      */
     AVRational framerate;
+
+    /**
+     * Additional data associated with the entire stream.
+     */
+    AVPacketSideDataSet side_data;
 } AVCodecParameters;
 
 /**
-- 
2.41.0



More information about the ffmpeg-devel mailing list