[FFmpeg-devel] [PATCH v1 1/4] lavu: add sub frame side data

Fei Wang fei.w.wang at intel.com
Fri Apr 29 10:59:38 EEST 2022


Sub frame side data allows attach another AVFrame as side data into
the target AVFrame.

Signed-off-by: Fei Wang <fei.w.wang at intel.com>
---
 libavutil/Makefile             |  2 ++
 libavutil/frame.c              |  5 ++-
 libavutil/frame.h              |  5 +++
 libavutil/sub_frame_metadata.c | 61 ++++++++++++++++++++++++++++++++++
 libavutil/sub_frame_metadata.h | 35 +++++++++++++++++++
 libavutil/version.h            |  4 +--
 6 files changed, 109 insertions(+), 3 deletions(-)
 create mode 100644 libavutil/sub_frame_metadata.c
 create mode 100644 libavutil/sub_frame_metadata.h

diff --git a/libavutil/Makefile b/libavutil/Makefile
index 81df3b0640..09333a0b48 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -88,6 +88,7 @@ HEADERS = adler32.h                                                     \
           tea.h                                                         \
           tx.h                                                          \
           film_grain_params.h                                           \
+          sub_frame_metadata.h                                          \
 
 ARCH_HEADERS = bswap.h                                                  \
                intmath.h                                                \
@@ -176,6 +177,7 @@ OBJS = adler32.o                                                        \
        tx_int32.o                                                       \
        video_enc_params.o                                               \
        film_grain_params.o                                              \
+       sub_frame_metadata.o                                             \
 
 
 OBJS-$(CONFIG_CUDA)                     += hwcontext_cuda.o
diff --git a/libavutil/frame.c b/libavutil/frame.c
index fbb869fffa..b21a15e652 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -315,7 +315,9 @@ static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
         if (   sd_src->type == AV_FRAME_DATA_PANSCAN
             && (src->width != dst->width || src->height != dst->height))
             continue;
-        if (force_copy) {
+        /* Don't copy sub frame side data, otherwise sub frame's pointers in
+         * dst may be invalid. */
+        if (force_copy && sd_src->type != AV_FRAME_DATA_SUB_FRAME) {
             sd_dst = av_frame_new_side_data(dst, sd_src->type,
                                             sd_src->size);
             if (!sd_dst) {
@@ -815,6 +817,7 @@ const char *av_frame_side_data_name(enum AVFrameSideDataType type)
     case AV_FRAME_DATA_DETECTION_BBOXES:            return "Bounding boxes for object detection and classification";
     case AV_FRAME_DATA_DOVI_RPU_BUFFER:             return "Dolby Vision RPU Data";
     case AV_FRAME_DATA_DOVI_METADATA:               return "Dolby Vision Metadata";
+    case AV_FRAME_DATA_SUB_FRAME:                   return "Sub frame Metadata";
     }
     return NULL;
 }
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 33fac2054c..9ae1286100 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -209,6 +209,11 @@ enum AVFrameSideDataType {
      * volume transform - CUVA 005.1-2021.
      */
     AV_FRAME_DATA_DYNAMIC_HDR_VIVID,
+
+    /**
+     * Sub frame of a target frame, as described by AVFrame.
+     */
+    AV_FRAME_DATA_SUB_FRAME,
 };
 
 enum AVActiveFormatDescription {
diff --git a/libavutil/sub_frame_metadata.c b/libavutil/sub_frame_metadata.c
new file mode 100644
index 0000000000..82ea32383f
--- /dev/null
+++ b/libavutil/sub_frame_metadata.c
@@ -0,0 +1,61 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "sub_frame_metadata.h"
+
+static void sub_frame_free(void *opaque, uint8_t *data)
+{
+    AVFrame *frame = (AVFrame*)data;
+
+    av_frame_free(&frame);
+}
+
+static AVFrame *sub_frame_alloc(size_t *out_size)
+{
+    AVFrame *sub_frame = av_frame_alloc();
+    if (!sub_frame)
+        return NULL;
+
+    *out_size = sizeof(*sub_frame);
+
+    return sub_frame;
+}
+
+AVFrame *av_sub_frame_create_side_data(AVFrame *frame)
+{
+    AVBufferRef *buf;
+    AVFrame *sub_frame;
+    size_t size;
+
+    sub_frame = sub_frame_alloc(&size);
+    if (!sub_frame)
+        return NULL;
+
+    buf = av_buffer_create((uint8_t *)sub_frame, size, &sub_frame_free, NULL, 0);
+    if (!buf) {
+        av_frame_free(&sub_frame);
+        return NULL;
+    }
+
+    if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_SUB_FRAME, buf)) {
+        av_buffer_unref(&buf);
+        return NULL;
+    }
+
+    return sub_frame;
+}
diff --git a/libavutil/sub_frame_metadata.h b/libavutil/sub_frame_metadata.h
new file mode 100644
index 0000000000..621fb31e42
--- /dev/null
+++ b/libavutil/sub_frame_metadata.h
@@ -0,0 +1,35 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_SUB_FRAME_METADATA_H
+#define AVUTIL_SUB_FRAME_METADATA_H
+
+#include "frame.h"
+
+/**
+ * Allocate a AVFrame structure and add it to the input frame as
+ * the side data. The allocated AVFrame will be freed automatically
+ * once the buf of created side data reference count decrease to zero.
+ *
+ * @param frame The frame which side data is added to.
+ *
+ * @return The AVFrame structure to be filled by caller.
+ */
+AVFrame *av_sub_frame_create_side_data(AVFrame *frame);
+
+#endif /* AVUTIL_SUB_FRAME_METADATA_H */
diff --git a/libavutil/version.h b/libavutil/version.h
index 6735c20090..dd7d20a9fa 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,8 +79,8 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  57
-#define LIBAVUTIL_VERSION_MINOR  24
-#define LIBAVUTIL_VERSION_MICRO 101
+#define LIBAVUTIL_VERSION_MINOR  25
+#define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
-- 
2.25.1



More information about the ffmpeg-devel mailing list