[FFmpeg-cvslog] avutil/hdr_dynamic_metadata: allow av_dynamic_hdr_plus_to_t35() to accept an existing buffer

James Almer git at videolan.org
Thu Apr 6 01:09:37 EEST 2023


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Sat Apr  1 12:12:11 2023 -0300| [61b27b15fc924d7fa35baa61cfbc91568945f5db] | committer: James Almer

avutil/hdr_dynamic_metadata: allow av_dynamic_hdr_plus_to_t35() to accept an existing buffer

The function now accepts an existing buffer to avoid unnecessary allocations,
as well as only reporting the needed amount of bytes if you pass a NULL pointer
as input for data.
For this, both parameters become input and output, as well as making data
optional. This is backwards compatible, and as such not breaking any existing
use of the function in external code (if there's any).

Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=61b27b15fc924d7fa35baa61cfbc91568945f5db
---

 doc/APIchanges                   |  5 +++++
 libavutil/hdr_dynamic_metadata.c | 28 ++++++++++++++++++++--------
 libavutil/hdr_dynamic_metadata.h | 14 +++++++++++---
 libavutil/version.h              |  2 +-
 4 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 29ddaa31bd..44ba3ad634 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,11 @@ The last version increases of all libraries were on 2023-02-09
 
 API changes, most recent first:
 
+2023-04-04 - xxxxxxxxxx - lavu 58.6.100 - hdr_dynamic_metadata.h
+  Add AV_HDR_PLUS_MAX_PAYLOAD_SIZE.
+  av_dynamic_hdr_plus_create_side_data() now accepts a user provided
+  buffer.
+
 2023-03-xx - xxxxxxxxxx - lavfi 9.5.100 - avfilter.h
   Add AVFILTER_FLAG_HWDEVICE.
 
diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c
index d458788c32..7033f060c0 100644
--- a/libavutil/hdr_dynamic_metadata.c
+++ b/libavutil/hdr_dynamic_metadata.c
@@ -18,14 +18,13 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "avassert.h"
 #include "hdr_dynamic_metadata.h"
 #include "mem.h"
 #include "libavcodec/defs.h"
 #include "libavcodec/get_bits.h"
 #include "libavcodec/put_bits.h"
 
-#define T35_PAYLOAD_MAX_SIZE 907
-
 static const int64_t luminance_den = 1;
 static const int32_t peak_luminance_den = 15;
 static const int64_t rgb_den = 100000;
@@ -62,14 +61,14 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame)
 int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
                                  size_t size)
 {
-    uint8_t padded_buf[T35_PAYLOAD_MAX_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
+    uint8_t padded_buf[AV_HDR_PLUS_MAX_PAYLOAD_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
     GetBitContext gbc, *gb = &gbc;
     int ret;
 
     if (!s)
         return AVERROR(ENOMEM);
 
-    if (size > T35_PAYLOAD_MAX_SIZE)
+    if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE)
         return AVERROR(EINVAL);
 
     memcpy(padded_buf, data, size);
@@ -243,8 +242,10 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t
     size_t size_bits, size_bytes;
     PutBitContext pbc, *pb = &pbc;
 
-    if (!s || !data)
+    if (!s)
         return AVERROR(EINVAL);
+    if ((!data || *data) && !size)
+       return AVERROR(EINVAL);
 
     /**
      * Buffer size per CTA-861-H p.253-254:
@@ -296,9 +297,20 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t
 
     size_bytes = (size_bits + 7) / 8;
 
-    buf = av_mallocz(size_bytes);
-    if (!buf)
-        return AVERROR(ENOMEM);
+    av_assert0(size_bytes <= AV_HDR_PLUS_MAX_PAYLOAD_SIZE);
+
+    if (!data) {
+        *size = size_bytes;
+        return 0;
+    } else if (*data) {
+        if (*size < size_bytes)
+            return AVERROR_BUFFER_TOO_SMALL;
+        buf = *data;
+    } else {
+        buf = av_malloc(size_bytes);
+        if (!buf)
+            return AVERROR(ENOMEM);
+    }
 
     init_put_bits(pb, buf, size_bytes);
 
diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h
index 771bb8f468..09e9d8bbcc 100644
--- a/libavutil/hdr_dynamic_metadata.h
+++ b/libavutil/hdr_dynamic_metadata.h
@@ -353,13 +353,21 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame);
 int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
                                  size_t size);
 
+#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE 907
+
 /**
  * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer,
  * excluding the first 48 bytes of the header, and beginning with the application mode.
  * @param s A pointer containing the decoded AVDynamicHDRPlus structure.
- * @param data A pointer to a byte buffer to be allocated and filled
- *             with the serialized metadata.
- * @param size A pointer to a size to be set to the returned buffer's size (optional).
+ * @param data[in,out] A pointer to pointer to a byte buffer to be filled with the
+ *                     serialized metadata.
+ *                     If *data is NULL, a buffer be will be allocated and a pointer to
+ *                     it stored in its place. The caller assumes ownership of the buffer.
+ *                     May be NULL, in which case the function will only store the
+ *                     required buffer size in *size.
+ * @param size[in,out] A pointer to a size to be set to the returned buffer's size.
+ *                     If *data is not NULL, *size must contain the size of the input
+ *                     buffer. May be NULL only if *data is NULL.
  *
  * @return >= 0 on success. Otherwise, returns the appropriate AVERROR.
  */
diff --git a/libavutil/version.h b/libavutil/version.h
index a232381ba5..40f92af055 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  58
-#define LIBAVUTIL_VERSION_MINOR   5
+#define LIBAVUTIL_VERSION_MINOR   6
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list