Commit df1bb63985 for ffmpeg
commit df1bb639853d577296972ed41ccf2eb6c398f262
Author: luoye-cpu <3412829159@qq.com>
Date: Mon Nov 16 10:09:14 2026 +0800
avutil/hdr_dynamic_metadata: tolerate zero padding past the payload
Some phone-recorded HDR10+ HEVC streams fail to decode entirely.
av_dynamic_hdr_plus_from_t35() returns AVERROR(EINVAL) for a payload
longer than AV_HDR_PLUS_MAX_PAYLOAD_SIZE (907 bytes, the byte-aligned
upper bound of the largest field combination the parser and serializer
represent, not a fixed length mandated by CTA-861). The HEVC SEI path
only skips AVERROR_INVALIDDATA NAL units (unless AV_EF_EXPLODE is set),
so the EINVAL aborts the packet and every picture after it.
A Xiaomi REDMI K90 Pro Max recording starts every picture with a
1024-byte T.35 HDR10+ message; after the 6-byte header this function
receives 1018 bytes whose fields occupy 339 bits and whose remainder is
all zero.
Accept an oversized payload only when every byte beyond the limit is
zero, then parse the first 907 bytes; a non-zero excess byte returns
AVERROR_INVALIDDATA. The retained bytes still go through the existing
field validation, and the clamp precedes memcpy(), so the stack buffer
stays bounded as before. The doxygen documents this contract.
The libavutil unit test is updated to the new contract and now compares
the fields parsed from a normal-length payload against the same payload
with a zero excess, checks a non-zero byte at both the first and the
last excess position, and keeps malformed-prefix rejection at both
lengths.
Signed-off-by: luoye-cpu <3412829159@qq.com>
diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c
index d52470da6d..e45bd4398a 100644
--- a/libavutil/hdr_dynamic_metadata.c
+++ b/libavutil/hdr_dynamic_metadata.c
@@ -61,13 +61,20 @@ int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
{
uint8_t padded_buf[AV_HDR_PLUS_MAX_PAYLOAD_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
GetBitContext gbc, *gb = &gbc;
+ size_t i;
int ret;
if (!s)
return AVERROR(EINVAL);
- if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE)
- return AVERROR(EINVAL);
+ if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE) {
+ /* Tolerate an all-zero excess beyond the maximum payload size. */
+ for (i = AV_HDR_PLUS_MAX_PAYLOAD_SIZE; i < size; i++) {
+ if (data[i])
+ return AVERROR_INVALIDDATA;
+ }
+ size = AV_HDR_PLUS_MAX_PAYLOAD_SIZE;
+ }
memcpy(padded_buf, data, size);
// Zero-initialize the buffer padding to avoid overreads into uninitialized data.
diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h
index 5881c35220..f1427f0b76 100644
--- a/libavutil/hdr_dynamic_metadata.h
+++ b/libavutil/hdr_dynamic_metadata.h
@@ -352,11 +352,17 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame);
* @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.
+ * @return >= 0 on success, otherwise an AVERROR. A payload longer than
+ * AV_HDR_PLUS_MAX_PAYLOAD_SIZE is parsed only when every byte
+ * beyond that limit is zero; unparsed bytes are not validated.
*/
int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
size_t size);
+/** Maximum HDR10+ payload size in bytes the parser and serializer support,
+ * derived from the largest field combination (7249 bits), not a fixed
+ * length mandated by CTA-861, which defines no padding element for this
+ * syntax. */
#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE 907
/**
diff --git a/libavutil/tests/hdr_dynamic_metadata.c b/libavutil/tests/hdr_dynamic_metadata.c
index dc0501333d..4b7ff0765d 100644
--- a/libavutil/tests/hdr_dynamic_metadata.c
+++ b/libavutil/tests/hdr_dynamic_metadata.c
@@ -583,15 +583,58 @@ static void test_hdr_plus(void)
ret == AVERROR(EINVAL) ? "yes" : "no");
{
- /* Oversized input is rejected before parsing. */
- size_t big = AV_HDR_PLUS_MAX_PAYLOAD_SIZE + 1;
- uint8_t *oversized = av_mallocz(big);
- if (oversized) {
+ /* A zero excess beyond the limit is ignored and must not change the
+ * parsed fields; a non-zero excess byte is rejected. */
+ size_t big = AV_HDR_PLUS_MAX_PAYLOAD_SIZE + 2;
+ uint8_t *normal = NULL, *oversized = av_mallocz(big);
+ size_t nsize = 0;
+
+ fill_hdr_plus_conforming(hdr);
+ ret = av_dynamic_hdr_plus_to_t35(hdr, &normal, &nsize);
+ printf("oversized fixture: to_t35 ret=%d size=%zu\n", ret, nsize);
+
+ if (oversized && normal && ret >= 0) {
+ memcpy(oversized, normal, nsize);
+
+ memset(hdr_rt, 0, sizeof(*hdr_rt));
+ ret = av_dynamic_hdr_plus_from_t35(hdr_rt, normal, nsize);
+ printf("from_t35 normal length: ret=%d\n", ret);
+ if (ret >= 0)
+ print_hdr_plus("normal", hdr_rt);
+
+ memset(hdr_rt, 0, sizeof(*hdr_rt));
ret = av_dynamic_hdr_plus_from_t35(hdr_rt, oversized, big);
- printf("from_t35 oversized: ret==AVERROR(EINVAL)=%s\n",
- ret == AVERROR(EINVAL) ? "yes" : "no");
- av_free(oversized);
+ printf("from_t35 zero-padded: ret=%d\n", ret);
+ if (ret >= 0)
+ print_hdr_plus("zero-padded", hdr_rt);
+
+ /* First byte beyond the limit: rejected. */
+ oversized[AV_HDR_PLUS_MAX_PAYLOAD_SIZE] = 1;
+ ret = av_dynamic_hdr_plus_from_t35(hdr_rt, oversized, big);
+ printf("from_t35 non-zero at limit: ret==AVERROR_INVALIDDATA=%s\n",
+ ret == AVERROR_INVALIDDATA ? "yes" : "no");
+ oversized[AV_HDR_PLUS_MAX_PAYLOAD_SIZE] = 0;
+
+ /* Last byte of the input: rejected. */
+ oversized[big - 1] = 1;
+ ret = av_dynamic_hdr_plus_from_t35(hdr_rt, oversized, big);
+ printf("from_t35 non-zero at end: ret==AVERROR_INVALIDDATA=%s\n",
+ ret == AVERROR_INVALIDDATA ? "yes" : "no");
+ oversized[big - 1] = 0;
+
+ /* A malformed prefix is still rejected at normal length... */
+ normal[1] &= 0x3F; /* num_windows = 0 */
+ ret = av_dynamic_hdr_plus_from_t35(hdr_rt, normal, nsize);
+ printf("from_t35 malformed normal: ret==AVERROR_INVALIDDATA=%s\n",
+ ret == AVERROR_INVALIDDATA ? "yes" : "no");
+ /* ...and with a zero excess: padding does not excuse it. */
+ oversized[1] &= 0x3F;
+ ret = av_dynamic_hdr_plus_from_t35(hdr_rt, oversized, big);
+ printf("from_t35 malformed zero-padded: ret==AVERROR_INVALIDDATA=%s\n",
+ ret == AVERROR_INVALIDDATA ? "yes" : "no");
}
+ av_free(normal);
+ av_free(oversized);
}
av_freep(&hdr);
diff --git a/tests/ref/fate/hdr_dynamic_metadata b/tests/ref/fate/hdr_dynamic_metadata
index 3ec298fc17..5d759ffef4 100644
--- a/tests/ref/fate/hdr_dynamic_metadata
+++ b/tests/ref/fate/hdr_dynamic_metadata
@@ -71,7 +71,33 @@ to_t35 rows>25 with flag clear: ret==0=yes
to_t35 mastering rows>25 with flag clear: ret==0=yes
to_t35 anchors>15 with flag clear: ret==0=yes
from_t35 NULL s: ret==AVERROR(EINVAL)=yes
-from_t35 oversized: ret==AVERROR(EINVAL)=yes
+oversized fixture: to_t35 ret=0 size=58
+from_t35 normal length: ret=0
+normal:
+ application_version=1 num_windows=1 targeted_system_display_maximum_luminance=400/1
+ targeted_system_display_actual_peak_luminance_flag=0
+ mastering_display_actual_peak_luminance_flag=0
+ window 0:
+ maxscl: 50000/100000 60000/100000 99000/100000 average_maxrgb=40000/100000
+ distribution_maxrgb: n=9 1%=1000/100000 5%=0/100000 10%=255/100000 25%=25000/100000 50%=50000/100000 75%=75000/100000 90%=90000/100000 95%=95000/100000 99%=99000/100000
+ fraction_bright_pixels=250/1000
+ tone_mapping_flag=1 knee=512/4095,1024/4095 anchors=9: 100/1023 200/1023 300/1023 400/1023 500/1023 600/1023 700/1023 800/1023 900/1023
+ color_saturation_mapping_flag=0
+from_t35 zero-padded: ret=0
+zero-padded:
+ application_version=1 num_windows=1 targeted_system_display_maximum_luminance=400/1
+ targeted_system_display_actual_peak_luminance_flag=0
+ mastering_display_actual_peak_luminance_flag=0
+ window 0:
+ maxscl: 50000/100000 60000/100000 99000/100000 average_maxrgb=40000/100000
+ distribution_maxrgb: n=9 1%=1000/100000 5%=0/100000 10%=255/100000 25%=25000/100000 50%=50000/100000 75%=75000/100000 90%=90000/100000 95%=95000/100000 99%=99000/100000
+ fraction_bright_pixels=250/1000
+ tone_mapping_flag=1 knee=512/4095,1024/4095 anchors=9: 100/1023 200/1023 300/1023 400/1023 500/1023 600/1023 700/1023 800/1023 900/1023
+ color_saturation_mapping_flag=0
+from_t35 non-zero at limit: ret==AVERROR_INVALIDDATA=yes
+from_t35 non-zero at end: ret==AVERROR_INVALIDDATA=yes
+from_t35 malformed normal: ret==AVERROR_INVALIDDATA=yes
+from_t35 malformed zero-padded: ret==AVERROR_INVALIDDATA=yes
Testing OOM path
alloc OOM: OK