History

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).

James Almer committed 3 years ago

Commit 61b27b11 parent 1c2a1e04 files · +37 −12Browse files

Unified · Split

4 files
  1. doc/APIchanges +5 −0
  2. libavutil/hdr_dynamic_metadata.c +20 −8
  3. libavutil/hdr_dynamic_metadata.h +11 −3
  4. libavutil/version.h +1 −1
doc/APIchanges+5−0
@@ -2,6 +2,11 @@ The last version increases of all libraries were on 2023-02-09 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2023-04-04 - xxxxxxxxxx - lavu 58.6.100 - hdr_dynamic_metadata.h
6+ Add AV_HDR_PLUS_MAX_PAYLOAD_SIZE.
7+ av_dynamic_hdr_plus_create_side_data() now accepts a user provided
8+ buffer.
9+
510 2023-03-xx - xxxxxxxxxx - lavfi 9.5.100 - avfilter.h
611 Add AVFILTER_FLAG_HWDEVICE.
712
libavutil/hdr_dynamic_metadata.c+20−8
@@ -18,14 +18,13 @@ · 17 unmodified lines
1818 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919 */
2020
21+#include "avassert.h"
2122 #include "hdr_dynamic_metadata.h"
2223 #include "mem.h"
2324 #include "libavcodec/defs.h"
2425 #include "libavcodec/get_bits.h"
2526 #include "libavcodec/put_bits.h"
2627
27−#define T35_PAYLOAD_MAX_SIZE 907
28−
2928 static const int64_t luminance_den = 1;
3029 static const int32_t peak_luminance_den = 15;
3130 static const int64_t rgb_den = 100000;
@@ -62,14 +61,14 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame) · 30 unmodified lines
6261 int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
6362 size_t size)
6463 {
65− uint8_t padded_buf[T35_PAYLOAD_MAX_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
64+ uint8_t padded_buf[AV_HDR_PLUS_MAX_PAYLOAD_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
6665 GetBitContext gbc, *gb = &gbc;
6766 int ret;
6867
6968 if (!s)
7069 return AVERROR(ENOMEM);
7170
72− if (size > T35_PAYLOAD_MAX_SIZE)
71+ if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE)
7372 return AVERROR(EINVAL);
7473
7574 memcpy(padded_buf, data, size);
@@ -243,8 +242,10 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t · 167 unmodified lines
243242 size_t size_bits, size_bytes;
244243 PutBitContext pbc, *pb = &pbc;
245244
246− if (!s || !data)
245+ if (!s)
247246 return AVERROR(EINVAL);
247+ if ((!data || *data) && !size)
248+ return AVERROR(EINVAL);
248249
249250 /**
250251 * 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 · 45 unmodified lines
296297
297298 size_bytes = (size_bits + 7) / 8;
298299
299− buf = av_mallocz(size_bytes);
300− if (!buf)
301− return AVERROR(ENOMEM);
300+ av_assert0(size_bytes <= AV_HDR_PLUS_MAX_PAYLOAD_SIZE);
301+
302+ if (!data) {
303+ *size = size_bytes;
304+ return 0;
305+ } else if (*data) {
306+ if (*size < size_bytes)
307+ return AVERROR_BUFFER_TOO_SMALL;
308+ buf = *data;
309+ } else {
310+ buf = av_malloc(size_bytes);
311+ if (!buf)
312+ return AVERROR(ENOMEM);
313+ }
302314
303315 init_put_bits(pb, buf, size_bytes);
304316
libavutil/hdr_dynamic_metadata.h+11−3
@@ -353,13 +353,21 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); · 352 unmodified lines
353353 int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
354354 size_t size);
355355
356+#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE 907
357+
356358 /**
357359 * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer,
358360 * excluding the first 48 bytes of the header, and beginning with the application mode.
359361 * @param s A pointer containing the decoded AVDynamicHDRPlus structure.
360− * @param data A pointer to a byte buffer to be allocated and filled
361− * with the serialized metadata.
362− * @param size A pointer to a size to be set to the returned buffer's size (optional).
362+ * @param data[in,out] A pointer to pointer to a byte buffer to be filled with the
363+ * serialized metadata.
364+ * If *data is NULL, a buffer be will be allocated and a pointer to
365+ * it stored in its place. The caller assumes ownership of the buffer.
366+ * May be NULL, in which case the function will only store the
367+ * required buffer size in *size.
368+ * @param size[in,out] A pointer to a size to be set to the returned buffer's size.
369+ * If *data is not NULL, *size must contain the size of the input
370+ * buffer. May be NULL only if *data is NULL.
363371 *
364372 * @return >= 0 on success. Otherwise, returns the appropriate AVERROR.
365373 */
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
7979 */
8080
8181 #define LIBAVUTIL_VERSION_MAJOR 58
82−#define LIBAVUTIL_VERSION_MINOR 5
82+#define LIBAVUTIL_VERSION_MINOR 6
8383 #define LIBAVUTIL_VERSION_MICRO 100
8484
8585 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t