---
 libavutil/frame.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/frame.h |   36 ++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)

diff --git a/libavutil/frame.c b/libavutil/frame.c
index 4583750..ab12f4d 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -262,6 +262,12 @@ void av_frame_unref(AVFrame *frame)
 {
     int i;
 
+    for (i = 0; i < frame->nb_side_data; i++) {
+        av_freep(&frame->side_data[i]->data);
+        av_freep(&frame->side_data[i]);
+    }
+    av_freep(&frame->side_data);
+
     for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
         av_buffer_unref(&frame->buf[i]);
     for (i = 0; i < frame->nb_extended_buf; i++)
@@ -342,6 +348,8 @@ int av_frame_make_writable(AVFrame *frame)
 
 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
 {
+    int i;
+
     dst->key_frame           = src->key_frame;
     dst->pict_type           = src->pict_type;
     dst->sample_aspect_ratio = src->sample_aspect_ratio;
@@ -353,6 +361,21 @@ int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
     dst->sample_rate         = src->sample_rate;
     dst->opaque              = src->opaque;
 
+    for (i = 0; i < src->nb_side_data; i++) {
+        const AVFrameSideData *sd_src = src->side_data[i];
+        AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
+                                                         sd_src->size);
+        if (!sd_dst) {
+            for (i = 0; i < dst->nb_side_data; i++) {
+                av_freep(&dst->side_data[i]->data);
+                av_freep(&dst->side_data[i]);
+            }
+            av_freep(&dst->side_data);
+            return AVERROR(ENOMEM);
+        }
+        memcpy(sd_dst->data, sd_src->data, sd_src->size);
+    }
+
     return 0;
 }
 
@@ -385,3 +408,48 @@ AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int 
plane)
     }
     return NULL;
 }
+
+AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type,
+                                        int size)
+{
+    AVFrameSideData *ret, **tmp;
+
+    if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
+        return NULL;
+
+    tmp = av_realloc(frame->side_data,
+                     (frame->nb_side_data + 1) * sizeof(*frame->side_data));
+    if (!tmp)
+        return NULL;
+    frame->side_data = tmp;
+
+    ret = av_mallocz(sizeof(*ret));
+    if (!ret)
+        return NULL;
+
+    ret->data = av_malloc(size);
+    if (!ret->data) {
+        av_freep(&ret);
+        return NULL;
+    }
+
+    ret->size = size;
+    ret->type = type;
+
+    frame->side_data[frame->nb_side_data++] = ret;
+
+    return ret;
+}
+
+AVFrameSideData *av_frame_get_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type)
+{
+    int i;
+
+    for (i = 0; i < frame->nb_side_data; i++) {
+        if (frame->side_data[i]->type == type)
+            return frame->side_data[i];
+    }
+    return NULL;
+}
diff --git a/libavutil/frame.h b/libavutil/frame.h
index e468cc2..d9de70e 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -29,6 +29,18 @@
 #include "rational.h"
 #include "samplefmt.h"
 
+enum AVFrameSideDataType {
+    /**
+     * The data is the AVPanScan struct defined in libavcodec.
+     */
+    AV_FRAME_DATA_PANSCAN,
+};
+
+typedef struct AVFrameSideData {
+    enum AVFrameSideDataType type;
+    uint8_t *data;
+    int      size;
+} AVFrameSideData;
 
 /**
  * This structure describes decoded (raw) audio or video data.
@@ -304,6 +316,9 @@ typedef struct AVFrame {
      * Number of elements in extended_buf.
      */
     int        nb_extended_buf;
+
+    AVFrameSideData **side_data;
+    int            nb_side_data;
 } AVFrame;
 
 /**
@@ -418,6 +433,7 @@ int av_frame_make_writable(AVFrame *frame);
  * Metadata for the purpose of this function are those fields that do not 
affect
  * the data layout in the buffers.  E.g. pts, sample rate (for audio) or sample
  * aspect ratio (for video), but not width/height or channel layout.
+ * Side data is also copied.
  */
 int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
 
@@ -431,4 +447,24 @@ int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
  */
 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane);
 
+/**
+ * Add a new side data to a frame.
+ *
+ * @param frame a frame to which the side data should be added
+ * @param type type of the added side data
+ * @param size size of the side data
+ *
+ * @return newly added side data on success, NULL on error
+ */
+AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type,
+                                        int size);
+
+/**
+ * @return a pointer to the side data of a given type on success, NULL if there
+ * is no side data with such type in this frame.
+ */
+AVFrameSideData *av_frame_get_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type);
+
 #endif /* AVUTIL_FRAME_H */
-- 
1.7.10.4

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to