PR #26122 opened by Romain Beauxis (toots)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26122
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26122.patch

`ff_make_codec_str()` / `av_mime_codec_str()` builds the RFC 6381 `CODECS` 
value that the HLS and DASH muxers write into manifests. For H.264 and HEVC it 
got several common cases wrong, and players then rejected the variant or picked 
the wrong decoder setup. In the HEVC cases that returned no string, hlsenc left 
`CODECS` out altogether.

- H.264 streams tagged `avc3` were announced as `avc1`.
- HEVC streams tagged `hev1` got no string at all, because only `hvc1` was 
accepted. hlsenc also warned about any HEVC tag other than `hvc1`, so a user 
passing `-tag:v hev1` was told to switch to `hvc1`.
- HEVC extradata copied from mp4 or Matroska is an 
`HEVCDecoderConfigurationRecord` (hvcC), not Annex-B. The SPS scan found 
nothing there, so stream-copied HEVC got no string. The hvcC record carries the 
profile_tier_level fields right after `configurationVersion`, so they are now 
read from it directly. The record is detected the same way as in libavcodec, 
which also covers records with `configurationVersion` 0 written by older 
muxers. A zeroed record (no profile or no level) is rejected rather than turned 
into `hvc1.0.0.L0.00`.
- `general_profile_space` was dropped. ISO/IEC 14496-15 requires an `A`/`B`/`C` 
prefix on the profile when it is non-zero.
- Only the first constraint byte and the high nibble of the second were 
written. All six `general_constraint_indicator_flags` bytes are now written, 
dot separated, with trailing zero bytes omitted. Note that a second byte such 
as `0x10` now prints as `.10` instead of `.1`.

The profile_tier_level parsing is moved to a function first (no functional 
change), so the Annex-B SPS path and the hvcC path share it.

Tests: a new FATE test, `fate-codecstring` (`libavformat/tests/codecstring.c`), 
calls `av_mime_codec_str()` on H.264 and HEVC extradata in both Annex-B and 
ISO/IEC 14496-15 form, with each sample entry tag. Later commits add cases for 
hvcC with `configurationVersion` 0, a zeroed hvcC, a non-zero profile space and 
multi-byte constraint flags. I ran each new case against the code before its 
fix to confirm it fails there. `fate-codecstring` passes at every commit of the 
series.


>From 603bcb40f1ac1686b4462299748a298fab5b4121 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:40:51 -0600
Subject: [PATCH 1/7] avformat/tests: add a codec string test

Covers av_mime_codec_str() for H.264 and HEVC extradata in Annex-B and
ISO/IEC 14496-15 form, with the sample entry names of both codecs.
---
 libavformat/Makefile            |   3 +-
 libavformat/tests/codecstring.c | 120 ++++++++++++++++++++++++++++++++
 tests/fate/libavformat.mak      |   4 ++
 tests/ref/fate/codecstring      |   9 +++
 4 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/tests/codecstring.c
 create mode 100644 tests/ref/fate/codecstring

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 891d7f82d9a..e0e240c8026 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -777,7 +777,8 @@ SKIPHEADERS-$(CONFIG_IMF_DEMUXER)        += imf.h
 SKIPHEADERS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpdh.h
 SKIPHEADERS-$(CONFIG_NETWORK)            += ip.h network.h rtsp.h
 
-TESTPROGS = demux_probe                                                 \
+TESTPROGS = codecstring                                                 \
+            demux_probe                                                 \
             id3v2                                                       \
             mkdir                                                       \
             rename                                                      \
diff --git a/libavformat/tests/codecstring.c b/libavformat/tests/codecstring.c
new file mode 100644
index 00000000000..7b30a865b7d
--- /dev/null
+++ b/libavformat/tests/codecstring.c
@@ -0,0 +1,120 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavutil/bprint.h"
+#include "libavutil/macros.h"
+#include "libavutil/mem.h"
+#include "libavcodec/codec_par.h"
+#include "libavcodec/defs.h"
+#include "libavformat/avformat.h"
+
+/* Extradata of a 160x120 encoding: libx264 High profile, libx265 Main
+ * profile. */
+static const uint8_t h264_annexb[] = {
+    0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x0b, 0xac, 0xd9, 0x42, 0x84,
+    0x7e, 0x5c, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0c,
+    0x83, 0xc5, 0x0a, 0x65, 0x80,
+};
+
+static const uint8_t h264_avcc[] = {
+    0x01, 0x64, 0x00, 0x0b, 0xff, 0xe1,
+};
+
+static const uint8_t hevc_annexb[] = {
+    0x00, 0x00, 0x00, 0x01, 0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03,
+    0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x1e, 0xa0, 0x14,
+    0x20, 0x79, 0x65, 0x95, 0x9a, 0x49, 0x32, 0xbc, 0x05, 0xa0, 0x20, 0x00,
+    0x00, 0x03, 0x00, 0x20, 0x00, 0x00, 0x03, 0x03, 0x21, 0x00,
+};
+
+static const uint8_t hevc_hvcc[] = {
+    0x01, 0x01, 0x60, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
+typedef struct TestCase {
+    const char *name;
+    enum AVCodecID codec_id;
+    uint32_t codec_tag;
+    int profile;
+    const uint8_t *extradata;
+    int extradata_size;
+} TestCase;
+
+static const TestCase cases[] = {
+    { "h264-annexb",          AV_CODEC_ID_H264, 0,
+      AV_PROFILE_UNKNOWN, h264_annexb, sizeof(h264_annexb) },
+    { "h264-annexb-avc3",     AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '3'),
+      AV_PROFILE_UNKNOWN, h264_annexb, sizeof(h264_annexb) },
+    { "h264-avcc",            AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, h264_avcc,   sizeof(h264_avcc)   },
+    { "hevc-annexb-hvc1",     AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, hevc_annexb, sizeof(hevc_annexb) },
+    { "hevc-annexb-hev1",     AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1'),
+      AV_PROFILE_UNKNOWN, hevc_annexb, sizeof(hevc_annexb) },
+    { "hevc-annexb-untagged", AV_CODEC_ID_HEVC, 0,
+      AV_PROFILE_UNKNOWN, hevc_annexb, sizeof(hevc_annexb) },
+    { "hevc-hvcc-hvc1",       AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, hevc_hvcc,   sizeof(hevc_hvcc)   },
+    { "hevc-hvcc-hev1",       AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1'),
+      AV_PROFILE_UNKNOWN, hevc_hvcc,   sizeof(hevc_hvcc)   },
+    { "aac-lc",               AV_CODEC_ID_AAC,  0,
+      AV_PROFILE_AAC_LOW, NULL,        0                   },
+};
+
+static int run_case(const TestCase *test)
+{
+    AVCodecParameters *par = avcodec_parameters_alloc();
+    AVBPrint out;
+    int ret;
+
+    if (!par)
+        return AVERROR(ENOMEM);
+    par->codec_id  = test->codec_id;
+    par->codec_tag = test->codec_tag;
+    par->profile   = test->profile;
+    if (test->extradata_size) {
+        par->extradata = av_mallocz(test->extradata_size +
+                                    AV_INPUT_BUFFER_PADDING_SIZE);
+        if (!par->extradata) {
+            avcodec_parameters_free(&par);
+            return AVERROR(ENOMEM);
+        }
+        memcpy(par->extradata, test->extradata, test->extradata_size);
+        par->extradata_size = test->extradata_size;
+    }
+
+    av_bprint_init(&out, 0, AV_BPRINT_SIZE_UNLIMITED);
+    ret = av_mime_codec_str(par, (AVRational){ 1, 0 }, &out);
+    printf("%s: %s\n", test->name, ret < 0 ? "error" : out.str);
+    av_bprint_finalize(&out, NULL);
+    avcodec_parameters_free(&par);
+
+    return ret == AVERROR(ENOMEM) ? ret : 0;
+}
+
+int main(void)
+{
+    for (size_t i = 0; i < FF_ARRAY_ELEMS(cases); i++)
+        if (run_case(&cases[i]) < 0)
+            return 1;
+    return 0;
+}
diff --git a/tests/fate/libavformat.mak b/tests/fate/libavformat.mak
index 70d5439437f..25e318768ad 100644
--- a/tests/fate/libavformat.mak
+++ b/tests/fate/libavformat.mak
@@ -1,3 +1,7 @@
+FATE_LIBAVFORMAT += fate-codecstring
+fate-codecstring: libavformat/tests/codecstring$(EXESUF)
+fate-codecstring: CMD = run libavformat/tests/codecstring$(EXESUF)
+
 FATE_LIBAVFORMAT += fate-demux-probe
 fate-demux-probe: libavformat/tests/demux_probe$(EXESUF)
 fate-demux-probe: CMD = run libavformat/tests/demux_probe$(EXESUF)
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
new file mode 100644
index 00000000000..472f24f38d2
--- /dev/null
+++ b/tests/ref/fate/codecstring
@@ -0,0 +1,9 @@
+h264-annexb: avc1.64000b
+h264-annexb-avc3: avc1.64000b
+h264-avcc: avc1.64000b
+hevc-annexb-hvc1: hvc1.1.6.L30.90
+hevc-annexb-hev1: error
+hevc-annexb-untagged: error
+hevc-hvcc-hvc1: error
+hevc-hvcc-hev1: error
+aac-lc: mp4a.40.2
-- 
2.54.0


>From 13726603cb66bd8df7ca8487e0065609e1722528 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:41:09 -0600
Subject: [PATCH 2/7] avformat/codecstring: name H.264 avc3 streams avc3

The profile and level part is the same for both sample entries: only
the name differs. A stream tagged avc3, such as one muxed with
-tag:v avc3, was announced as avc1.
---
 libavformat/codecstring.c  | 7 ++++++-
 tests/ref/fate/codecstring | 2 +-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 6ecec13ed5e..70609d9a239 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -91,7 +91,12 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
                 p = &data[1];
             else
                 return AVERROR(EINVAL);
-            av_bprintf(out, "avc1.%02x%02x%02x", p[0], p[1], p[2]);
+            /* avc3 differs from avc1 by where the parameter sets are
+             * stored; the profile and level part is the same
+             * (ISO/IEC 14496-15) */
+            av_bprintf(out, "%s.%02x%02x%02x",
+                       par->codec_tag == MKTAG('a','v','c','3') ? "avc3" : 
"avc1",
+                       p[0], p[1], p[2]);
         } else {
             return AVERROR(EINVAL);
         }
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index 472f24f38d2..70f570e579b 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -1,5 +1,5 @@
 h264-annexb: avc1.64000b
-h264-annexb-avc3: avc1.64000b
+h264-annexb-avc3: avc3.64000b
 h264-avcc: avc1.64000b
 hevc-annexb-hvc1: hvc1.1.6.L30.90
 hevc-annexb-hev1: error
-- 
2.54.0


>From 4ec81082bf088a6e889acbdc386af37a670f0f56 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:41:24 -0600
Subject: [PATCH 3/7] avformat/codecstring: accept the hev1 sample entry for
 HEVC

ISO/IEC 14496-15 gives hev1 the same codecs parameter as hvc1, under its
own name. The string was already built from the codec tag, but only for
hvc1: a stream tagged hev1 got no string.
---
 libavformat/codecstring.c  | 3 ++-
 libavformat/hlsenc.c       | 5 +++--
 tests/ref/fate/codecstring | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 70609d9a239..9d2fca8432d 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -169,7 +169,8 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
             }
             data++;
         }
-        if (par->codec_tag == MKTAG('h','v','c','1') &&
+        if ((par->codec_tag == MKTAG('h','v','c','1') ||
+             par->codec_tag == MKTAG('h','e','v','1')) &&
             profile != AV_PROFILE_UNKNOWN &&
             profile_compatibility != AV_PROFILE_UNKNOWN &&
             tier != 0 &&
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index e35af34d230..6b0de93a6db 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2359,8 +2359,9 @@ static int hls_write_header(AVFormatContext *s)
             }
             avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, 
inner_st->time_base.num, inner_st->time_base.den);
             if (outer_st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
-                outer_st->codecpar->codec_tag != MKTAG('h','v','c','1')) {
-                av_log(s, AV_LOG_WARNING, "Stream HEVC is not hvc1, you should 
use tag:v hvc1 to set it.\n");
+                outer_st->codecpar->codec_tag != MKTAG('h','v','c','1') &&
+                outer_st->codecpar->codec_tag != MKTAG('h','e','v','1')) {
+                av_log(s, AV_LOG_WARNING, "Stream HEVC is not hvc1 or hev1, 
you should use tag:v hvc1 to set it.\n");
             }
             write_codec_attr(outer_st, vs);
 
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index 70f570e579b..c3f03847b52 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -2,7 +2,7 @@ h264-annexb: avc1.64000b
 h264-annexb-avc3: avc3.64000b
 h264-avcc: avc1.64000b
 hevc-annexb-hvc1: hvc1.1.6.L30.90
-hevc-annexb-hev1: error
+hevc-annexb-hev1: hev1.1.6.L30.90
 hevc-annexb-untagged: error
 hevc-hvcc-hvc1: error
 hevc-hvcc-hev1: error
-- 
2.54.0


>From 6a6cf265ba0cb6971d554070a2a27093eae495bc Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:42:03 -0600
Subject: [PATCH 4/7] avformat/codecstring: move HEVC profile_tier_level
 parsing to a function

No functional change. The hvcC record holds the same fields with the same
layout, so they can be read from it too.
---
 libavformat/codecstring.c | 74 ++++++++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 28 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 9d2fca8432d..a7a845dfb47 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -61,6 +61,48 @@ static void set_vp9_codec_str(void *logctx, const 
AVCodecParameters *par,
     }
 }
 
+/**
+ * Read the general profile, tier, level and constraint fields of an HEVC
+ * profile_tier_level(). ptl points to the byte holding general_profile_space,
+ * and 12 bytes are read.
+ */
+static void parse_hevc_profile_tier_level(const uint8_t *ptl, int *profile,
+                                          uint32_t *profile_compatibility,
+                                          char *tier, int *level,
+                                          char *constraints,
+                                          size_t constraints_size)
+{
+    uint32_t profile_compatibility_flags;
+    uint8_t high_nibble;
+
+    /* TIER represents the general_tier_flag, with 'L' indicating the flag is 
0,
+     * and 'H' indicating the flag is 1
+     */
+    *tier = (ptl[0] & 0x20) == 0 ? 'L' : 'H';
+    *profile = ptl[0] & 0x1f;
+    /* PROFILE_COMPATIBILITY is general_profile_compatibility_flags, but in 
reverse bit order,
+     * in a hexadecimal representation (leading zeroes may be omitted).
+     */
+    profile_compatibility_flags = AV_RB32(ptl + 1);
+    /* revise these bits to get the profile compatibility value */
+    profile_compatibility_flags = ((profile_compatibility_flags & 0x55555555U) 
<< 1) | ((profile_compatibility_flags >> 1) & 0x55555555U);
+    profile_compatibility_flags = ((profile_compatibility_flags & 0x33333333U) 
<< 2) | ((profile_compatibility_flags >> 2) & 0x33333333U);
+    profile_compatibility_flags = ((profile_compatibility_flags & 0x0F0F0F0FU) 
<< 4) | ((profile_compatibility_flags >> 4) & 0x0F0F0F0FU);
+    profile_compatibility_flags = ((profile_compatibility_flags & 0x00FF00FFU) 
<< 8) | ((profile_compatibility_flags >> 8) & 0x00FF00FFU);
+    *profile_compatibility = (profile_compatibility_flags << 16) | 
(profile_compatibility_flags >> 16);
+    /* skip 8 + 32
+     * CONSTRAINTS is a hexadecimal representation of the 
general_constraint_indicator_flags.
+     * each byte is separated by a '.', and trailing zero bytes may be omitted.
+     * drop the trailing zero bytes refer to ISO/IEC14496-15.
+     */
+    high_nibble = ptl[6] >> 4;
+    snprintf(constraints, constraints_size,
+             high_nibble ? "%02x.%x" : "%02x",
+             ptl[5], high_nibble);
+    /* skip 8 + 32 + 4 + 43 + 1 bit */
+    *level = ptl[11];
+}
+
 int ff_make_codec_str(void *logctx, const AVCodecParameters *par,
                       const AVRational *frame_rate, struct AVBPrint *out)
 {
@@ -121,8 +163,6 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
                 uint8_t *rbsp_buf;
                 int remain_size = 0;
                 int rbsp_size = 0;
-                uint32_t profile_compatibility_flags = 0;
-                uint8_t high_nibble = 0;
                 /* skip start code + nalu header */
                 data += 6;
                 /* process by reference General NAL unit syntax */
@@ -137,33 +177,11 @@ int ff_make_codec_str(void *logctx, const 
AVCodecParameters *par,
                 /* skip sps_video_parameter_set_id   u(4),
                  *      sps_max_sub_layers_minus1    u(3),
                  *  and sps_temporal_id_nesting_flag u(1)
-                 *
-                 * TIER represents the general_tier_flag, with 'L' indicating 
the flag is 0,
-                 * and 'H' indicating the flag is 1
                  */
-                tier = (rbsp_buf[1] & 0x20) == 0 ? 'L' : 'H';
-                profile = rbsp_buf[1] & 0x1f;
-                /* PROFILE_COMPATIBILITY is 
general_profile_compatibility_flags, but in reverse bit order,
-                 * in a hexadecimal representation (leading zeroes may be 
omitted).
-                 */
-                profile_compatibility_flags = AV_RB32(rbsp_buf + 2);
-                /* revise these bits to get the profile compatibility value */
-                profile_compatibility_flags = ((profile_compatibility_flags & 
0x55555555U) << 1) | ((profile_compatibility_flags >> 1) & 0x55555555U);
-                profile_compatibility_flags = ((profile_compatibility_flags & 
0x33333333U) << 2) | ((profile_compatibility_flags >> 2) & 0x33333333U);
-                profile_compatibility_flags = ((profile_compatibility_flags & 
0x0F0F0F0FU) << 4) | ((profile_compatibility_flags >> 4) & 0x0F0F0F0FU);
-                profile_compatibility_flags = ((profile_compatibility_flags & 
0x00FF00FFU) << 8) | ((profile_compatibility_flags >> 8) & 0x00FF00FFU);
-                profile_compatibility = (profile_compatibility_flags << 16) | 
(profile_compatibility_flags >> 16);
-                /* skip 8 + 8 + 32
-                 * CONSTRAINTS is a hexadecimal representation of the 
general_constraint_indicator_flags.
-                 * each byte is separated by a '.', and trailing zero bytes 
may be omitted.
-                 * drop the trailing zero bytes refer to ISO/IEC14496-15.
-                 */
-                high_nibble = rbsp_buf[7] >> 4;
-                snprintf(constraints, sizeof(constraints),
-                         high_nibble ? "%02x.%x" : "%02x",
-                         rbsp_buf[6], high_nibble);
-                /* skip 8 + 8 + 32 + 4 + 43 + 1 bit */
-                level = rbsp_buf[12];
+                parse_hevc_profile_tier_level(rbsp_buf + 1, &profile,
+                                              &profile_compatibility, &tier,
+                                              &level, constraints,
+                                              sizeof(constraints));
                 av_freep(&rbsp_buf);
                 break;
             }
-- 
2.54.0


>From a53c63bf907035e1f0ef84c51191f8f01ed79124 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:42:30 -0600
Subject: [PATCH 5/7] avformat/codecstring: read the HEVC codec string from an
 hvcC record

Extradata from mp4 or Matroska is an HEVCDecoderConfigurationRecord, not
Annex-B, so no SPS was found and HEVC copied from those containers got
no string. The record holds the profile_tier_level fields right after
configurationVersion.
---
 libavformat/codecstring.c       | 17 +++++++++++++++++
 libavformat/tests/codecstring.c | 16 +++++++++++++++-
 tests/ref/fate/codecstring      |  6 ++++--
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index a7a845dfb47..42c58de1be0 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -156,6 +156,23 @@ int ff_make_codec_str(void *logctx, const 
AVCodecParameters *par,
         if (par->level != AV_LEVEL_UNKNOWN)
             level = par->level;
 
+        /* HEVCDecoderConfigurationRecord (ISO/IEC 14496-15), as in mp4 and
+         * Matroska: its profile_tier_level fields follow configurationVersion.
+         * Its NAL units have no start code to scan for. Detected as in
+         * libavcodec, which also covers records with configurationVersion 0
+         * from older muxers. */
+        if (data && par->extradata_size >= 13 &&
+            (data[0] || data[1] || data[2] > 1)) {
+            /* Some muxers write a zeroed record: no profile, no level. */
+            if (!(data[1] & 0x1f) || !data[12])
+                return AVERROR(EINVAL);
+            parse_hevc_profile_tier_level(data + 1, &profile,
+                                          &profile_compatibility, &tier,
+                                          &level, constraints,
+                                          sizeof(constraints));
+            data = NULL;
+        }
+
         /* check the boundary of data which from current position is small 
than extradata_size */
         while (data && (data - par->extradata + 19) < par->extradata_size) {
             /* get HEVC SPS NAL and seek to profile_tier_level */
diff --git a/libavformat/tests/codecstring.c b/libavformat/tests/codecstring.c
index 7b30a865b7d..8b193a5d2bc 100644
--- a/libavformat/tests/codecstring.c
+++ b/libavformat/tests/codecstring.c
@@ -50,6 +50,16 @@ static const uint8_t hevc_hvcc[] = {
     0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
 };
 
+static const uint8_t hevc_hvcc_version0[] = {
+    0x00, 0x01, 0x60, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
+static const uint8_t hevc_hvcc_zeroed[] = {
+    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
 typedef struct TestCase {
     const char *name;
     enum AVCodecID codec_id;
@@ -76,7 +86,11 @@ static const TestCase cases[] = {
       AV_PROFILE_UNKNOWN, hevc_hvcc,   sizeof(hevc_hvcc)   },
     { "hevc-hvcc-hev1",       AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1'),
       AV_PROFILE_UNKNOWN, hevc_hvcc,   sizeof(hevc_hvcc)   },
-    { "aac-lc",               AV_CODEC_ID_AAC,  0,
+    { "hevc-hvcc-version0",   AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, hevc_hvcc_version0, sizeof(hevc_hvcc_version0) },
+    { "hevc-hvcc-zeroed",     AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_HEVC_MAIN, hevc_hvcc_zeroed, sizeof(hevc_hvcc_zeroed) },
+    { "aac-lc",              AV_CODEC_ID_AAC,  0,
       AV_PROFILE_AAC_LOW, NULL,        0                   },
 };
 
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index c3f03847b52..0e0e6dce59b 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -4,6 +4,8 @@ h264-avcc: avc1.64000b
 hevc-annexb-hvc1: hvc1.1.6.L30.90
 hevc-annexb-hev1: hev1.1.6.L30.90
 hevc-annexb-untagged: error
-hevc-hvcc-hvc1: error
-hevc-hvcc-hev1: error
+hevc-hvcc-hvc1: hvc1.1.6.L30.90
+hevc-hvcc-hev1: hev1.1.6.L30.90
+hevc-hvcc-version0: hvc1.1.6.L30.90
+hevc-hvcc-zeroed: error
 aac-lc: mp4a.40.2
-- 
2.54.0


>From 25f1279335d58a6c7f2bdedce277bedc49334c65 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:51:15 -0600
Subject: [PATCH 6/7] avformat/codecstring: write the HEVC
 general_profile_space

ISO/IEC 14496-15 prefixes the profile with A, B or C when
general_profile_space is 1, 2 or 3. It was dropped, which named a
different profile.
---
 libavformat/codecstring.c       | 18 +++++++++++++-----
 libavformat/tests/codecstring.c |  8 ++++++++
 tests/ref/fate/codecstring      |  1 +
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 42c58de1be0..776e4324ce5 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -66,7 +66,8 @@ static void set_vp9_codec_str(void *logctx, const 
AVCodecParameters *par,
  * profile_tier_level(). ptl points to the byte holding general_profile_space,
  * and 12 bytes are read.
  */
-static void parse_hevc_profile_tier_level(const uint8_t *ptl, int *profile,
+static void parse_hevc_profile_tier_level(const uint8_t *ptl,
+                                          int *profile_space, int *profile,
                                           uint32_t *profile_compatibility,
                                           char *tier, int *level,
                                           char *constraints,
@@ -78,6 +79,7 @@ static void parse_hevc_profile_tier_level(const uint8_t *ptl, 
int *profile,
     /* TIER represents the general_tier_flag, with 'L' indicating the flag is 
0,
      * and 'H' indicating the flag is 1
      */
+    *profile_space = ptl[0] >> 6;
     *tier = (ptl[0] & 0x20) == 0 ? 'L' : 'H';
     *profile = ptl[0] & 0x1f;
     /* PROFILE_COMPATIBILITY is general_profile_compatibility_flags, but in 
reverse bit order,
@@ -145,6 +147,7 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
     } else if (par->codec_id == AV_CODEC_ID_HEVC) {
         // 3GPP TS 26.244
         uint8_t *data = par->extradata;
+        int profile_space = 0;
         int profile = AV_PROFILE_UNKNOWN;
         uint32_t profile_compatibility = AV_PROFILE_UNKNOWN;
         char tier = 0;
@@ -166,7 +169,7 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
             /* Some muxers write a zeroed record: no profile, no level. */
             if (!(data[1] & 0x1f) || !data[12])
                 return AVERROR(EINVAL);
-            parse_hevc_profile_tier_level(data + 1, &profile,
+            parse_hevc_profile_tier_level(data + 1, &profile_space, &profile,
                                           &profile_compatibility, &tier,
                                           &level, constraints,
                                           sizeof(constraints));
@@ -195,7 +198,8 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
                  *      sps_max_sub_layers_minus1    u(3),
                  *  and sps_temporal_id_nesting_flag u(1)
                  */
-                parse_hevc_profile_tier_level(rbsp_buf + 1, &profile,
+                parse_hevc_profile_tier_level(rbsp_buf + 1, &profile_space,
+                                              &profile,
                                               &profile_compatibility, &tier,
                                               &level, constraints,
                                               sizeof(constraints));
@@ -211,8 +215,12 @@ int ff_make_codec_str(void *logctx, const 
AVCodecParameters *par,
             tier != 0 &&
             level != AV_LEVEL_UNKNOWN &&
             constraints[0] != '\0') {
-            av_bprintf(out, "%s.%d.%x.%c%d.%s",
-                       av_fourcc2str(par->codec_tag), profile,
+            /* general_profile_space 1 to 3 is a letter A to C before
+             * the profile (ISO/IEC 14496-15) */
+            av_bprintf(out, "%s.", av_fourcc2str(par->codec_tag));
+            if (profile_space)
+                av_bprint_chars(out, 'A' + profile_space - 1, 1);
+            av_bprintf(out, "%d.%x.%c%d.%s", profile,
                        profile_compatibility, tier, level, constraints);
         } else
             return AVERROR(EINVAL);
diff --git a/libavformat/tests/codecstring.c b/libavformat/tests/codecstring.c
index 8b193a5d2bc..58fc4221188 100644
--- a/libavformat/tests/codecstring.c
+++ b/libavformat/tests/codecstring.c
@@ -60,6 +60,11 @@ static const uint8_t hevc_hvcc_zeroed[] = {
     0x00, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
 };
 
+static const uint8_t hevc_hvcc_profile_space[] = {
+    0x01, 0x41, 0x60, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
 typedef struct TestCase {
     const char *name;
     enum AVCodecID codec_id;
@@ -90,6 +95,9 @@ static const TestCase cases[] = {
       AV_PROFILE_UNKNOWN, hevc_hvcc_version0, sizeof(hevc_hvcc_version0) },
     { "hevc-hvcc-zeroed",     AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
       AV_PROFILE_HEVC_MAIN, hevc_hvcc_zeroed, sizeof(hevc_hvcc_zeroed) },
+    { "hevc-hvcc-profile-space", AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, hevc_hvcc_profile_space,
+      sizeof(hevc_hvcc_profile_space) },
     { "aac-lc",              AV_CODEC_ID_AAC,  0,
       AV_PROFILE_AAC_LOW, NULL,        0                   },
 };
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index 0e0e6dce59b..70536e02631 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -8,4 +8,5 @@ hevc-hvcc-hvc1: hvc1.1.6.L30.90
 hevc-hvcc-hev1: hev1.1.6.L30.90
 hevc-hvcc-version0: hvc1.1.6.L30.90
 hevc-hvcc-zeroed: error
+hevc-hvcc-profile-space: hvc1.A1.6.L30.90
 aac-lc: mp4a.40.2
-- 
2.54.0


>From d8506971fde2e2d80396cc64b7fe93b518a5bfc2 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Mon, 16 Nov 2026 20:51:53 -0600
Subject: [PATCH 7/7] avformat/codecstring: write every HEVC constraint byte

The codec string carries the six general_constraint_indicator_flags
bytes, dot separated, with trailing zero bytes omitted
(ISO/IEC 14496-15). Only the first byte and the high nibble of the
second were written, so later non-zero bytes were lost.
---
 libavformat/codecstring.c       | 14 ++++++++------
 libavformat/tests/codecstring.c | 16 ++++++++++++++++
 tests/ref/fate/codecstring      |  2 ++
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 776e4324ce5..571499acc00 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -74,7 +74,7 @@ static void parse_hevc_profile_tier_level(const uint8_t *ptl,
                                           size_t constraints_size)
 {
     uint32_t profile_compatibility_flags;
-    uint8_t high_nibble;
+    int constraint_bytes = 6;
 
     /* TIER represents the general_tier_flag, with 'L' indicating the flag is 
0,
      * and 'H' indicating the flag is 1
@@ -97,10 +97,12 @@ static void parse_hevc_profile_tier_level(const uint8_t 
*ptl,
      * each byte is separated by a '.', and trailing zero bytes may be omitted.
      * drop the trailing zero bytes refer to ISO/IEC14496-15.
      */
-    high_nibble = ptl[6] >> 4;
-    snprintf(constraints, constraints_size,
-             high_nibble ? "%02x.%x" : "%02x",
-             ptl[5], high_nibble);
+    while (constraint_bytes > 1 && !ptl[4 + constraint_bytes])
+        constraint_bytes--;
+    for (int i = 0; i < constraint_bytes; i++)
+        snprintf(constraints + 3 * i, constraints_size - 3 * i,
+                 "%02x.", ptl[5 + i]);
+    constraints[3 * constraint_bytes - 1] = '\0';
     /* skip 8 + 32 + 4 + 43 + 1 bit */
     *level = ptl[11];
 }
@@ -152,7 +154,7 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters 
*par,
         uint32_t profile_compatibility = AV_PROFILE_UNKNOWN;
         char tier = 0;
         int level = AV_LEVEL_UNKNOWN;
-        char constraints[8] = "";
+        char constraints[19] = "";
 
         if (par->profile != AV_PROFILE_UNKNOWN)
             profile = par->profile;
diff --git a/libavformat/tests/codecstring.c b/libavformat/tests/codecstring.c
index 58fc4221188..bd257dd4d8a 100644
--- a/libavformat/tests/codecstring.c
+++ b/libavformat/tests/codecstring.c
@@ -65,6 +65,16 @@ static const uint8_t hevc_hvcc_profile_space[] = {
     0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
 };
 
+static const uint8_t hevc_hvcc_constraints[] = {
+    0x01, 0x01, 0x60, 0x00, 0x00, 0x00, 0x90, 0x00, 0x10, 0x00, 0x00, 0x00,
+    0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
+static const uint8_t hevc_hvcc_constraints_full[] = {
+    0x01, 0x01, 0x60, 0x00, 0x00, 0x00, 0x90, 0x10, 0x20, 0x30, 0x40, 0x50,
+    0x1e, 0xf0, 0x00, 0xfc, 0xfd, 0xf8, 0xf8, 0x00, 0x00, 0x0f, 0x00,
+};
+
 typedef struct TestCase {
     const char *name;
     enum AVCodecID codec_id;
@@ -98,6 +108,12 @@ static const TestCase cases[] = {
     { "hevc-hvcc-profile-space", AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
       AV_PROFILE_UNKNOWN, hevc_hvcc_profile_space,
       sizeof(hevc_hvcc_profile_space) },
+    { "hevc-hvcc-constraints", AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1'),
+      AV_PROFILE_UNKNOWN, hevc_hvcc_constraints,
+      sizeof(hevc_hvcc_constraints) },
+    { "hevc-hvcc-constraints-full", AV_CODEC_ID_HEVC,
+      MKTAG('h', 'v', 'c', '1'), AV_PROFILE_UNKNOWN,
+      hevc_hvcc_constraints_full, sizeof(hevc_hvcc_constraints_full) },
     { "aac-lc",              AV_CODEC_ID_AAC,  0,
       AV_PROFILE_AAC_LOW, NULL,        0                   },
 };
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index 70536e02631..7ef22bd9d71 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -9,4 +9,6 @@ hevc-hvcc-hev1: hev1.1.6.L30.90
 hevc-hvcc-version0: hvc1.1.6.L30.90
 hevc-hvcc-zeroed: error
 hevc-hvcc-profile-space: hvc1.A1.6.L30.90
+hevc-hvcc-constraints: hvc1.1.6.L30.90.00.10
+hevc-hvcc-constraints-full: hvc1.1.6.L30.90.10.20.30.40.50
 aac-lc: mp4a.40.2
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to