* [FFmpeg-devel] [PR] libavutil: Add functions for SMPTE-2094-50 HDR metadata (PR #21788)
@ 2026-02-18 22:59 vigneshvg via ffmpeg-devel
0 siblings, 0 replies; only message in thread
From: vigneshvg via ffmpeg-devel @ 2026-02-18 22:59 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: vigneshvg
PR #21788 opened by vigneshvg
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21788
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21788.patch
SMPTE-2094-50 is an upcoming standard that is close to being
finalized.
Define a side data type for carrying this metadata. And add
functions for parsing and writing it. This is very similar to
the handling of HDR10+ metadata.
The spec is available here: https://github.com/SMPTE/st2094-50
Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com>
>From 89c5d3d923dbd2b714e27a975d3bcc351aed664f Mon Sep 17 00:00:00 2001
From: Vignesh Venkat <vigneshv@google.com>
Date: Thu, 12 Feb 2026 14:23:36 -0800
Subject: [PATCH] libavutil: Add functions for SMPTE-2094-50 HDR metadata
SMPTE-2094-50 is an upcoming standard that is close to being
finalized.
Define a side data type for carrying this metadata. And add
functions for parsing and writing it. This is very similar to
the handling of HDR10+ metadata.
The spec is available here: https://github.com/SMPTE/st2094-50
Signed-off-by: Vignesh Venkatasubramanian <vigneshv@google.com>
---
libavutil/frame.h | 7 +
libavutil/hdr_dynamic_metadata.c | 310 +++++++++++++++++++++++++++++++
libavutil/hdr_dynamic_metadata.h | 104 +++++++++++
libavutil/side_data.c | 1 +
4 files changed, 422 insertions(+)
diff --git a/libavutil/frame.h b/libavutil/frame.h
index 088b24b717..b8ea379954 100644
--- a/libavutil/frame.h
+++ b/libavutil/frame.h
@@ -260,6 +260,13 @@ enum AVFrameSideDataType {
* EXIF metadata, starting with either 49 49 2a 00, or 4d 4d 00 2a.
*/
AV_FRAME_DATA_EXIF,
+
+ /**
+ * HDR dynamic metadata associated with a video frame. The payload is
+ * an AVDynamicHDRSmpte2094App5 type and contains information for color
+ * volume transform as specified in the SMPTE 2094-50 standard.
+ */
+ AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5,
};
enum AVActiveFormatDescription {
diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c
index 7033f060c0..5b8f3ea87d 100644
--- a/libavutil/hdr_dynamic_metadata.c
+++ b/libavutil/hdr_dynamic_metadata.c
@@ -395,3 +395,313 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t
*size = size_bytes;
return 0;
}
+
+AVDynamicHDRSmpte2094App5 *av_dynamic_hdr_smpte2094_app5_alloc(size_t *size)
+{
+ AVDynamicHDRSmpte2094App5 *smpte2094_app5 = av_mallocz(sizeof(AVDynamicHDRSmpte2094App5));
+ if (!smpte2094_app5)
+ return NULL;
+
+ if (size)
+ *size = sizeof(*smpte2094_app5);
+
+ return smpte2094_app5;
+}
+
+AVDynamicHDRSmpte2094App5 *av_dynamic_hdr_smpte2094_app5_create_side_data(AVFrame *frame)
+{
+ AVFrameSideData *side_data = av_frame_new_side_data(frame,
+ AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5,
+ sizeof(AVDynamicHDRSmpte2094App5));
+ if (!side_data)
+ return NULL;
+
+ memset(side_data->data, 0, sizeof(AVDynamicHDRSmpte2094App5));
+
+ return (AVDynamicHDRSmpte2094App5 *)side_data->data;
+}
+
+#define GET_BITS_OR_FAIL(var, n) \
+ do { \
+ if (get_bits_left(gb) < n) { \
+ ret = AVERROR_INVALIDDATA; \
+ goto end; \
+ } \
+ var = get_bits(gb, n); \
+ } while (0)
+
+int av_dynamic_hdr_smpte2094_app5_from_t35(AVDynamicHDRSmpte2094App5 *s, const uint8_t *data,
+ size_t size)
+{
+ GetBitContext gbc, *gb = &gbc;
+ int ret, reserved_zero;
+ size_t padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
+
+ if (!s)
+ return AVERROR(ENOMEM);
+
+ uint8_t *padded_data = av_mallocz(padded_size);
+ memcpy(padded_data, data, size);
+ ret = init_get_bits8(gb, padded_data, size);
+ if (ret < 0)
+ goto end;
+
+ // Table C.1
+ GET_BITS_OR_FAIL(s->application_version, 3);
+ GET_BITS_OR_FAIL(s->minimum_application_version, 3);
+ GET_BITS_OR_FAIL(reserved_zero, 2);
+ if (reserved_zero) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+
+ // Table C.2
+ GET_BITS_OR_FAIL(s->has_custom_hdr_reference_white_flag, 1);
+ GET_BITS_OR_FAIL(s->has_adaptive_tone_map_flag, 1);
+ GET_BITS_OR_FAIL(reserved_zero, 6);
+ if (reserved_zero) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+ if (s->has_custom_hdr_reference_white_flag)
+ GET_BITS_OR_FAIL(s->hdr_reference_white, 16);
+ if (!s->has_adaptive_tone_map_flag) {
+ ret = 0;
+ goto end;
+ }
+
+ // Table C.3
+ GET_BITS_OR_FAIL(s->baseline_hdr_headroom, 16);
+ GET_BITS_OR_FAIL(s->use_reference_white_tone_mapping_flag, 1);
+ if (s->use_reference_white_tone_mapping_flag) {
+ GET_BITS_OR_FAIL(reserved_zero, 7);
+ ret = reserved_zero ? AVERROR_INVALIDDATA : 0;
+ goto end;
+ }
+ GET_BITS_OR_FAIL(s->num_alternate_images, 3);
+ if (s->num_alternate_images > 4) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+ GET_BITS_OR_FAIL(s->gain_application_space_chromaticities_flag, 2);
+ GET_BITS_OR_FAIL(s->has_common_component_mix_params_flag, 1);
+ GET_BITS_OR_FAIL(s->has_common_curve_params_flag, 1);
+ if (s->gain_application_space_chromaticities_flag == 3)
+ for (int r = 0; r < 8; r++)
+ GET_BITS_OR_FAIL(s->gain_application_space_chromaticities[r], 16);
+
+ for (int a = 0; a < s->num_alternate_images; a++) {
+ GET_BITS_OR_FAIL(s->alternate_hdr_headrooms[a], 16);
+
+ // Table C.4
+ if (!a || !s->has_common_component_mix_params_flag) {
+ GET_BITS_OR_FAIL(s->component_mixing_type[a], 2);
+ if (s->component_mixing_type[a] != 3) {
+ GET_BITS_OR_FAIL(reserved_zero, 6);
+ if (reserved_zero) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+ } else {
+ for (int k = 0; k < 6; k++)
+ GET_BITS_OR_FAIL(s->has_component_mixing_coefficient_flag[a][k], 1);
+ for (int k = 0; k < 6; k++) {
+ if (s->has_component_mixing_coefficient_flag[a][k]) {
+ GET_BITS_OR_FAIL(s->component_mixing_coefficient[a][k], 16);
+ } else {
+ s->component_mixing_coefficient[a][k] = 0;
+ }
+ }
+ }
+ } else {
+ s->component_mixing_type[a] = s->component_mixing_type[0];
+ if (s->component_mixing_type[a] == 3) {
+ for (int k = 0; k < 6; k++) {
+ s->has_component_mixing_coefficient_flag[a][k] =
+ s->has_component_mixing_coefficient_flag[0][k];
+ s->component_mixing_coefficient[a][k] = s->component_mixing_coefficient[0][k];
+ }
+ }
+ }
+
+ // Table C.5
+ if (!a || !s->has_common_curve_params_flag) {
+ GET_BITS_OR_FAIL(s->gain_curve_num_control_points_minus_1[a], 5);
+ if (s->gain_curve_num_control_points_minus_1[a] > 31) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+ GET_BITS_OR_FAIL(s->gain_curve_use_pchip_slope_flag[a], 1);
+ GET_BITS_OR_FAIL(reserved_zero, 2);
+ if (reserved_zero) {
+ ret = AVERROR_INVALIDDATA;
+ goto end;
+ }
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
+ GET_BITS_OR_FAIL(s->gain_curve_control_points_x[a][c], 16);
+ }
+ } else {
+ s->gain_curve_num_control_points_minus_1[a] =
+ s->gain_curve_num_control_points_minus_1[0];
+ s->gain_curve_use_pchip_slope_flag[a] = s->gain_curve_use_pchip_slope_flag[0];
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
+ s->gain_curve_control_points_x[a][c] = s->gain_curve_control_points_x[0][c];
+ }
+ }
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
+ GET_BITS_OR_FAIL(s->gain_curve_control_points_y[a][c], 16);
+ if (!s->gain_curve_use_pchip_slope_flag[a]) {
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
+ GET_BITS_OR_FAIL(s->gain_curve_control_points_theta[a][c], 16);
+ }
+ }
+ ret = 0;
+end:
+ av_free(padded_data);
+ return ret;
+}
+
+int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5 *s, uint8_t **data,
+ size_t *size)
+{
+ uint8_t *buf;
+ size_t size_bytes, size_bits;
+ PutBitContext pbc, *pb = &pbc;
+
+ if (!s)
+ return AVERROR(EINVAL);
+ if ((!data || *data) && !size)
+ return AVERROR(EINVAL);
+
+ if (s->application_version >= 8 || s->minimum_application_version >= 3)
+ return AVERROR_INVALIDDATA;
+ size_bits = 0;
+ size_bits += 3 + 3 + 2;
+ size_bits += 1 + 1 + 6;
+ if (s->has_custom_hdr_reference_white_flag)
+ size_bits += 16;
+ if (s->has_adaptive_tone_map_flag) {
+ size_bits += 16 + 1;
+ if (s->use_reference_white_tone_mapping_flag) {
+ size_bits += 7;
+ } else {
+ size_bits += 3 + 2 + 1 + 1;
+ if (s->gain_application_space_chromaticities_flag == 3)
+ size_bits += 16 * 8;
+ if (s->num_alternate_images > 4)
+ return AVERROR_INVALIDDATA;
+ for (int a = 0; a < s->num_alternate_images; a++) {
+ size_bits += 16;
+ if (!a || !s->has_common_component_mix_params_flag) {
+ size_bits += 2;
+ if (s->component_mixing_type[a] != 3) {
+ size_bits += 6;
+ } else {
+ size_bits += 6;
+ for (int k = 0; k < 6; k++)
+ if (s->has_component_mixing_coefficient_flag[a][k])
+ size_bits += 16;
+ }
+ }
+ if (!a || !s->has_common_curve_params_flag) {
+ size_bits += 5 + 1 + 2;
+ if (s->gain_curve_num_control_points_minus_1[a] > 31)
+ return AVERROR_INVALIDDATA;
+ size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
+ }
+ size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
+ if (!s->gain_curve_use_pchip_slope_flag[a])
+ size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
+ }
+ }
+ }
+ if (size_bits % 8)
+ return AVERROR_INVALIDDATA;
+ size_bytes = size_bits >> 3;
+
+ 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);
+
+ // Table C.1
+ put_bits(pb, 3, s->application_version);
+ put_bits(pb, 3, s->minimum_application_version);
+ put_bits(pb, 2, 0); // reserved_zero
+
+ // Table C.2
+ put_bits(pb, 1, s->has_custom_hdr_reference_white_flag);
+ put_bits(pb, 1, s->has_adaptive_tone_map_flag);
+ put_bits(pb, 6, 0); // reserved_zero
+
+ if (s->has_custom_hdr_reference_white_flag)
+ put_bits(pb, 16, s->hdr_reference_white);
+
+ if (s->has_adaptive_tone_map_flag) {
+ // Table C.3
+ put_bits(pb, 16, s->baseline_hdr_headroom);
+ put_bits(pb, 1, s->use_reference_white_tone_mapping_flag);
+ if (s->use_reference_white_tone_mapping_flag) {
+ put_bits(pb, 7, 0); // reserved_zero
+ } else {
+ put_bits(pb, 3, s->num_alternate_images);
+ put_bits(pb, 2, s->gain_application_space_chromaticities_flag);
+ put_bits(pb, 1, s->has_common_component_mix_params_flag);
+ put_bits(pb, 1, s->has_common_curve_params_flag);
+
+ if (s->gain_application_space_chromaticities_flag == 3)
+ for (int r = 0; r < 8; r++)
+ put_bits(pb, 16, s->gain_application_space_chromaticities[r]);
+
+ for (int a = 0; a < s->num_alternate_images; a++) {
+ put_bits(pb, 16, s->alternate_hdr_headrooms[a]);
+
+ // Table C.4
+ if (!a || !s->has_common_component_mix_params_flag) {
+ put_bits(pb, 2, s->component_mixing_type[a]);
+ if (s->component_mixing_type[a] != 3) {
+ put_bits(pb, 6, 0); // reserved_zero
+ } else {
+ for (int k = 0; k < 6; k++)
+ put_bits(pb, 1, s->has_component_mixing_coefficient_flag[a][k]);
+ for (int k = 0; k < 6; k++)
+ if (s->has_component_mixing_coefficient_flag[a][k])
+ put_bits(pb, 16, s->component_mixing_coefficient[a][k]);
+ }
+ }
+
+ // Table C.5
+ if (!a || !s->has_common_curve_params_flag) {
+ put_bits(pb, 5, s->gain_curve_num_control_points_minus_1[a]);
+ put_bits(pb, 1, s->gain_curve_use_pchip_slope_flag[a]);
+ put_bits(pb, 2, 0); // reserved_zero
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
+ put_bits(pb, 16, s->gain_curve_control_points_x[a][c]);
+ }
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
+ put_bits(pb, 16, s->gain_curve_control_points_y[a][c]);
+ if (!s->gain_curve_use_pchip_slope_flag[a]) {
+ for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
+ put_bits(pb, 16, s->gain_curve_control_points_theta[a][c]);
+ }
+ }
+ }
+ }
+
+ flush_put_bits(pb);
+
+ *data = buf;
+ if (size)
+ *size = size_bytes;
+ return 0;
+}
diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h
index 5100ed6f41..a051ac7471 100644
--- a/libavutil/hdr_dynamic_metadata.h
+++ b/libavutil/hdr_dynamic_metadata.h
@@ -373,4 +373,108 @@ int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
*/
int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size);
+/**
+ * This struct represents dynamic metadata for color volume transform as
+ * specified in the SMPTE 2094-50 standard.
+ *
+ * To be used as payload of a AVFrameSideData or AVPacketSideData with the
+ * appropriate type.
+ *
+ * @note The struct should be allocated with
+ * av_dynamic_smpte2094_app5_alloc() and its size is not a part of
+ * the public ABI.
+ */
+typedef struct AVDynamicHDRSmpte2094App5 {
+ /**
+ * Section C.2.1. smpte_st_2094_50_application_info()
+ */
+ uint8_t application_version;
+ uint8_t minimum_application_version;
+
+ /**
+ * Section C.2.2 smpte_st_2094_50_color_volume_transform()
+ */
+ uint8_t has_custom_hdr_reference_white_flag;
+ uint8_t has_adaptive_tone_map_flag;
+ uint16_t hdr_reference_white;
+
+ /**
+ * Section C.2.3 smpte_st_2094_50_adaptive_tone_map()
+ */
+ uint16_t baseline_hdr_headroom;
+ uint8_t use_reference_white_tone_mapping_flag;
+ uint8_t num_alternate_images;
+ uint8_t gain_application_space_chromaticities_flag;
+ uint8_t has_common_component_mix_params_flag;
+ uint8_t has_common_curve_params_flag;
+ uint16_t gain_application_space_chromaticities[8];
+ uint16_t alternate_hdr_headrooms[4];
+
+ /**
+ * Section C.2.4 smpte_st_2094_50_component_mixing()
+ */
+ uint8_t component_mixing_type[4];
+ uint8_t has_component_mixing_coefficient_flag[4][6];
+ uint16_t component_mixing_coefficient[4][6];
+
+ /**
+ * Section C.2.5 smpte_st_2094_50_gain_curve()
+ */
+ uint8_t gain_curve_num_control_points_minus_1[4];
+ uint8_t gain_curve_use_pchip_slope_flag[4];
+ uint16_t gain_curve_control_points_x[4][32];
+ uint16_t gain_curve_control_points_y[4][32];
+ uint16_t gain_curve_control_points_theta[4][32];
+} AVDynamicHDRSmpte2094App5;
+
+/**
+ * Allocate an AVDynamicHDRSmpte2094App5 structure and set its fields to
+ * default values. The resulting struct can be freed using av_freep().
+ *
+ * @return An AVDynamicHDRSmpte2094App5 filled with default values or NULL
+ * on failure.
+ */
+AVDynamicHDRSmpte2094App5* av_dynamic_hdr_smpte2094_app5_alloc(size_t* size);
+
+/**
+ * Allocate a complete AVDynamicHDRSmpte2094App5 and add it to the frame.
+ *
+ * @param frame The frame which side data is added to.
+ *
+ * @return The AVDynamicHDRSmpte2094App5 structure to be filled by caller or
+ * NULL on failure.
+ */
+AVDynamicHDRSmpte2094App5* av_dynamic_hdr_smpte2094_app5_create_side_data(AVFrame* frame);
+
+/**
+ * Parse the user data formatted as ITU-T T.35 message to AVDynamicHDRSmpte2094App5.
+ *
+ * @param s A pointer containing the decoded AVDynamicHDRSmpte2094App5 structure.
+ * @param data The byte array containing the raw ITU-T T.35 data.
+ * @param size Size of the data array in bytes.
+ *
+ * @return >= 0 on success. Otherwise, returns the appropriate AVERROR.
+ */
+int av_dynamic_hdr_smpte2094_app5_from_t35(AVDynamicHDRSmpte2094App5* s, const uint8_t* data,
+ size_t size);
+
+/**
+ * Serialize dynamic SMPTE-2094-50 metadata to a ITU-T T.35 message. Excluding
+ * the country_code, provider_code and provider_oriented_code.
+ *
+ * @param s A pointer containing the AVDynamicHDRSmpte2094App5 data.
+ * @param[in,out] data 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[in,out] size 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.
+ */
+int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5* s, uint8_t** data,
+ size_t* size);
+
#endif /* AVUTIL_HDR_DYNAMIC_METADATA_H */
diff --git a/libavutil/side_data.c b/libavutil/side_data.c
index bbbeb70ecd..7bf5f7e96e 100644
--- a/libavutil/side_data.c
+++ b/libavutil/side_data.c
@@ -36,6 +36,7 @@ static const AVSideDataDescriptor sd_props[] = {
[AV_FRAME_DATA_S12M_TIMECODE] = { "SMPTE 12-1 timecode" },
[AV_FRAME_DATA_DYNAMIC_HDR_PLUS] = { "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
[AV_FRAME_DATA_DYNAMIC_HDR_VIVID] = { "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
+ [AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5] = { "HDR Dynamic Metadata SMPTE2094-50", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
[AV_FRAME_DATA_REGIONS_OF_INTEREST] = { "Regions Of Interest", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
[AV_FRAME_DATA_VIDEO_ENC_PARAMS] = { "Video encoding parameters" },
[AV_FRAME_DATA_FILM_GRAIN_PARAMS] = { "Film grain parameters" },
--
2.52.0
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-02-18 22:59 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-18 22:59 [FFmpeg-devel] [PR] libavutil: Add functions for SMPTE-2094-50 HDR metadata (PR #21788) vigneshvg via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git