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
2 2
3API changes, most recent first:3API changes, most recent first:
4 4
52023-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
52023-03-xx - xxxxxxxxxx - lavfi 9.5.100 - avfilter.h102023-03-xx - xxxxxxxxxx - lavfi 9.5.100 - avfilter.h
6 Add AVFILTER_FLAG_HWDEVICE.11 Add AVFILTER_FLAG_HWDEVICE.
7 12
libavutil/hdr_dynamic_metadata.c+20−8
@@ -18,14 +18,13 @@ · 17 unmodified lines
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */19 */
20 20
21#include "avassert.h"
21#include "hdr_dynamic_metadata.h"22#include "hdr_dynamic_metadata.h"
22#include "mem.h"23#include "mem.h"
23#include "libavcodec/defs.h"24#include "libavcodec/defs.h"
24#include "libavcodec/get_bits.h"25#include "libavcodec/get_bits.h"
25#include "libavcodec/put_bits.h"26#include "libavcodec/put_bits.h"
26 27
27#define T35_PAYLOAD_MAX_SIZE 907
28
29static const int64_t luminance_den = 1;28static const int64_t luminance_den = 1;
30static const int32_t peak_luminance_den = 15;29static const int32_t peak_luminance_den = 15;
31static const int64_t rgb_den = 100000;30static const int64_t rgb_den = 100000;
@@ -62,14 +61,14 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame) · 30 unmodified lines
62int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,61int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
63 size_t size)62 size_t size)
64{63{
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];
66 GetBitContext gbc, *gb = &gbc;65 GetBitContext gbc, *gb = &gbc;
67 int ret;66 int ret;
68 67
69 if (!s)68 if (!s)
70 return AVERROR(ENOMEM);69 return AVERROR(ENOMEM);
71 70
72 if (size > T35_PAYLOAD_MAX_SIZE)71 if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE)
73 return AVERROR(EINVAL);72 return AVERROR(EINVAL);
74 73
75 memcpy(padded_buf, data, size);74 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
243 size_t size_bits, size_bytes;242 size_t size_bits, size_bytes;
244 PutBitContext pbc, *pb = &pbc;243 PutBitContext pbc, *pb = &pbc;
245 244
246 if (!s || !data)245 if (!s)
247 return AVERROR(EINVAL);246 return AVERROR(EINVAL);
247 if ((!data || *data) && !size)
248 return AVERROR(EINVAL);
248 249
249 /**250 /**
250 * Buffer size per CTA-861-H p.253-254:251 * 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
296 297
297 size_bytes = (size_bits + 7) / 8;298 size_bytes = (size_bits + 7) / 8;
298 299
299 buf = av_mallocz(size_bytes);300 av_assert0(size_bytes <= AV_HDR_PLUS_MAX_PAYLOAD_SIZE);
300 if (!buf)301
301 return AVERROR(ENOMEM);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 }
302 314
303 init_put_bits(pb, buf, size_bytes);315 init_put_bits(pb, buf, size_bytes);
304 316
libavutil/hdr_dynamic_metadata.h+11−3
@@ -353,13 +353,21 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); · 352 unmodified lines
353int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,353int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
354 size_t size);354 size_t size);
355 355
356#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE 907
357
356/**358/**
357 * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer,359 * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer,
358 * excluding the first 48 bytes of the header, and beginning with the application mode.360 * excluding the first 48 bytes of the header, and beginning with the application mode.
359 * @param s A pointer containing the decoded AVDynamicHDRPlus structure.361 * @param s A pointer containing the decoded AVDynamicHDRPlus structure.
360 * @param data A pointer to a byte buffer to be allocated and filled362 * @param data[in,out] A pointer to pointer to a byte buffer to be filled with the
361 * with the serialized metadata.363 * serialized metadata.
362 * @param size A pointer to a size to be set to the returned buffer's size (optional).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.
363 *371 *
364 * @return >= 0 on success. Otherwise, returns the appropriate AVERROR.372 * @return >= 0 on success. Otherwise, returns the appropriate AVERROR.
365 */373 */
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
79 */79 */
80 80
81#define LIBAVUTIL_VERSION_MAJOR 5881#define LIBAVUTIL_VERSION_MAJOR 58
82#define LIBAVUTIL_VERSION_MINOR 582#define LIBAVUTIL_VERSION_MINOR 6
83#define LIBAVUTIL_VERSION_MICRO 10083#define LIBAVUTIL_VERSION_MICRO 100
84 84
85#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \85#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