Commit b54992769c for ffmpeg
commit b54992769c95f8caadc84a8efdbc3ac0d1762511
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Tue Nov 17 08:47:52 2026 -0600
avformat/codecstring: get the HEVC profile, tier and level from hevc.c
hevc.c already reads profile_tier_level() from both hvcC records and
Annex B parameter sets to write hvcC. Expose that, and drop the copy in
codecstring.c, whose Annex B scan missed an SPS behind a 3-byte start
code. hvcC records are now recognized as libavcodec does.
hevc.o is now always built, as codecstring.o is, and so are the Golomb
tables it needs.
diff --git a/configure b/configure
index 89d897089e..5f1bcebf5b 100755
--- a/configure
+++ b/configure
@@ -4363,6 +4363,7 @@ avdevice_suggest="libm stdatomic"
avfilter_deps="avutil"
avfilter_suggest="libm stdatomic zlib"
avformat_deps="avcodec avutil"
+avformat_select="golomb"
avformat_suggest="libm network zlib stdatomic"
avutil_suggest="amf clock_gettime ffnvcodec gcrypt libm zlib libdrm libmfx opencl openssl user32 vaapi vulkan videotoolbox corefoundation corevideo coremedia bcrypt stdatomic"
swresample_deps="avutil"
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 698e45adba..2c50cdc94e 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -18,6 +18,7 @@ OBJS = allformats.o \
dump.o \
dv.o \
format.o \
+ hevc.o \
id3v1.o \
id3v2/reader.o \
isom_tags.o \
@@ -48,7 +49,7 @@ include $(SRC_PATH)/libavformat/vorbis/comment/Makefile
OBJS-$(CONFIG_CBS_APV_LAVF) += cbs_apv.o cbs.o
OBJS-$(CONFIG_CBS_AV1_LAVF) += cbs_av1.o cbs.o
OBJS-$(CONFIG_ISO_MEDIA) += isom.o
-OBJS-$(CONFIG_ISO_WRITER) += avc.o hevc.o vvc.o
+OBJS-$(CONFIG_ISO_WRITER) += avc.o vvc.o
OBJS-$(CONFIG_IAMFDEC) += iamf_reader.o iamf_parse.o iamf.o
OBJS-$(CONFIG_IAMFENC) += iamf_writer.o iamf.o
OBJS-$(CONFIG_NETWORK) += network.o
@@ -747,9 +748,9 @@ OBJS-$(CONFIG_LIBZMQ_PROTOCOL) += libzmq.o
# Objects duplicated from other libraries for shared builds
SHLIBOBJS += log2_tab.o to_upper4.o \
- h2645_parse.o lcevctab.o
+ golomb_tab.o h2645_parse.o \
+ lcevctab.o
SHLIBOBJS-$(CONFIG_ISO_MEDIA) += mpegaudiotabs.o
-SHLIBOBJS-$(CONFIG_ISO_WRITER) += golomb_tab.o
SHLIBOBJS-$(CONFIG_FLV_MUXER) += mpeg4audio_sample_rates.o
SHLIBOBJS-$(CONFIG_HLS_DEMUXER) += ac3_channel_layout_tab.o
SHLIBOBJS-$(CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER) += jpegxl_parse.o
@@ -761,11 +762,10 @@ SHLIBOBJS-$(CONFIG_MATROSKA_MUXER) += opus_frame_duration_tab.o
SHLIBOBJS-$(CONFIG_MOV_DEMUXER) += ac3_channel_layout_tab.o
SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpegaudiotabs.o
SHLIBOBJS-$(CONFIG_MP3_MUXER) += mpegaudiotabs.o
-SHLIBOBJS-$(CONFIG_MXF_MUXER) += golomb_tab.o \
- rangecoder_dec.o
+SHLIBOBJS-$(CONFIG_MXF_MUXER) += rangecoder_dec.o
SHLIBOBJS-$(CONFIG_NUT_MUXER) += mpegaudiotabs.o
SHLIBOBJS-$(CONFIG_RTPDEC) += jpegtables.o
-SHLIBOBJS-$(CONFIG_RTP_MUXER) += golomb_tab.o jpegtables.o \
+SHLIBOBJS-$(CONFIG_RTP_MUXER) += jpegtables.o \
mpeg4audio_sample_rates.o
SHLIBOBJS-$(CONFIG_SPDIF_MUXER) += dca_sample_rate_tab.o
SHLIBOBJS-$(CONFIG_WEBM_MUXER) += opus_frame_duration_tab.o
diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index ef9db1259c..6c0f594bc9 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -22,15 +22,14 @@
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
-#include "libavutil/mem.h"
#include "libavutil/rational.h"
#include "av1.h"
#include "avc.h"
#include "avformat.h"
+#include "hevc.h"
#include "internal.h"
#include "lcevc.h"
-#include "nal.h"
#include "vpcc.h"
static const struct codec_string {
@@ -61,50 +60,57 @@ 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_space, int *profile,
- uint32_t *profile_compatibility,
- char *tier, int *level,
- char *constraints,
- size_t constraints_size)
+/** Byte i of the 48-bit general_constraint_indicator_flags, 0 first. */
+static unsigned hevc_constraint_byte(const HVCCProfileTierLevel *ptl, int i)
+{
+ return (ptl->constraint_indicator_flags >> (40 - 8 * i)) & 0xff;
+}
+
+/* 3GPP TS 26.244 and ISO/IEC 14496-15 */
+static int set_hevc_codec_str(const AVCodecParameters *par, AVBPrint *out)
{
- uint32_t profile_compatibility_flags;
+ HVCCProfileTierLevel ptl;
+ uint32_t compatibility;
int constraint_bytes = 6;
+ int ret;
+
+ if (par->codec_tag != MKTAG('h','v','c','1') &&
+ par->codec_tag != MKTAG('h','e','v','1'))
+ return AVERROR(EINVAL);
+ if (!par->extradata)
+ return AVERROR(EINVAL);
+
+ ret = ff_hevc_get_profile_tier_level(&ptl, par->extradata,
+ par->extradata_size);
+ if (ret < 0)
+ return ret;
+ /* No parameter set, or a zeroed hvcC record as some muxers write */
+ if (!ptl.profile_idc || !ptl.level_idc)
+ return AVERROR(EINVAL);
+
+ /* general_profile_compatibility_flags in reverse bit order */
+ compatibility = ptl.profile_compatibility_flags;
+ compatibility = ((compatibility & 0x55555555U) << 1) | ((compatibility >> 1) & 0x55555555U);
+ compatibility = ((compatibility & 0x33333333U) << 2) | ((compatibility >> 2) & 0x33333333U);
+ compatibility = ((compatibility & 0x0F0F0F0FU) << 4) | ((compatibility >> 4) & 0x0F0F0F0FU);
+ compatibility = ((compatibility & 0x00FF00FFU) << 8) | ((compatibility >> 8) & 0x00FF00FFU);
+ compatibility = (compatibility << 16) | (compatibility >> 16);
+
+ av_bprintf(out, "%s.", av_fourcc2str(par->codec_tag));
+ /* general_profile_space 1 to 3 is a letter A to C before the profile */
+ if (ptl.profile_space)
+ av_bprint_chars(out, 'A' + ptl.profile_space - 1, 1);
+ av_bprintf(out, "%d.%"PRIx32".%c%d", ptl.profile_idc, compatibility,
+ ptl.tier_flag ? 'H' : 'L', ptl.level_idc);
- /* 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,
- * 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.
- */
- while (constraint_bytes > 1 && !ptl[4 + constraint_bytes])
+ /* The six constraint bytes, trailing zero bytes omitted */
+ while (constraint_bytes > 1 &&
+ !hevc_constraint_byte(&ptl, constraint_bytes - 1))
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];
+ av_bprintf(out, ".%02x", hevc_constraint_byte(&ptl, i));
+
+ return 0;
}
int ff_make_codec_str(void *logctx, const AVCodecParameters *par,
@@ -147,87 +153,7 @@ int ff_make_codec_str(void *logctx, const AVCodecParameters *par,
return AVERROR(EINVAL);
}
} 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;
- int level = AV_LEVEL_UNKNOWN;
- char constraints[19] = "";
-
- if (par->profile != AV_PROFILE_UNKNOWN)
- profile = par->profile;
- 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. Any extradata not
- * starting with a start code is taken as a record, which covers
- * configurationVersion 0 from older muxers; this is looser than
- * libavcodec, which wants the full 23-byte record. Only the 13 bytes
- * up to general_level_idc are read. */
- 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_space, &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 */
- if (!(data[0] | data[1] | data[2]) && data[3] == 1 && ((data[4] & 0x7E) == 0x42)) {
- uint8_t *rbsp_buf;
- int remain_size = 0;
- int rbsp_size = 0;
- /* skip start code + nalu header */
- data += 6;
- /* process by reference General NAL unit syntax */
- remain_size = par->extradata_size - (data - par->extradata);
- rbsp_buf = ff_nal_unit_extract_rbsp(data, remain_size, &rbsp_size, 0);
- if (!rbsp_buf)
- return AVERROR(EINVAL);
- if (rbsp_size < 13) {
- av_freep(&rbsp_buf);
- break;
- }
- /* skip sps_video_parameter_set_id u(4),
- * sps_max_sub_layers_minus1 u(3),
- * and sps_temporal_id_nesting_flag u(1)
- */
- parse_hevc_profile_tier_level(rbsp_buf + 1, &profile_space,
- &profile,
- &profile_compatibility, &tier,
- &level, constraints,
- sizeof(constraints));
- av_freep(&rbsp_buf);
- break;
- }
- data++;
- }
- 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 &&
- level != AV_LEVEL_UNKNOWN &&
- constraints[0] != '\0') {
- /* 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);
+ return set_hevc_codec_str(par, out);
} else if (par->codec_id == AV_CODEC_ID_LCEVC) {
LCEVCDecoderConfigurationRecord lvcc;
int err;
diff --git a/libavformat/hevc.c b/libavformat/hevc.c
index 787e6cce16..890cbee2c0 100644
--- a/libavformat/hevc.c
+++ b/libavformat/hevc.c
@@ -86,15 +86,6 @@ typedef struct HEVCDecoderConfigurationRecord {
uint8_t alpha_layer_nuh_id;
} HEVCDecoderConfigurationRecord;
-typedef struct HVCCProfileTierLevel {
- uint8_t profile_space;
- uint8_t tier_flag;
- uint8_t profile_idc;
- uint32_t profile_compatibility_flags;
- uint64_t constraint_indicator_flags;
- uint8_t level_idc;
-} HVCCProfileTierLevel;
-
static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
HVCCProfileTierLevel *ptl)
{
@@ -1417,6 +1408,29 @@ static int write_configuration_record(void *logctx, AVIOContext *pb, const uint8
return ret;
}
+int ff_hevc_get_profile_tier_level(HVCCProfileTierLevel *ptl,
+ const uint8_t *data, int size)
+{
+ HEVCDecoderConfigurationRecord hvcc;
+ uint8_t *nal_buf = NULL;
+ int flags = 0;
+ int ret;
+
+ hvcc_init(&hvcc);
+ ret = parse_configuration_record(&hvcc, data, size, &flags, &nal_buf);
+ if (ret >= 0) {
+ ptl->profile_space = hvcc.general_profile_space;
+ ptl->tier_flag = hvcc.general_tier_flag;
+ ptl->profile_idc = hvcc.general_profile_idc;
+ ptl->profile_compatibility_flags = hvcc.general_profile_compatibility_flags;
+ ptl->constraint_indicator_flags = hvcc.general_constraint_indicator_flags;
+ ptl->level_idc = hvcc.general_level_idc;
+ }
+ hvcc_close(&hvcc);
+ av_free(nal_buf);
+ return ret;
+}
+
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
int size, int ps_array_completeness, void *logctx)
{
diff --git a/libavformat/hevc.h b/libavformat/hevc.h
index 83e149f702..290c6d75e6 100644
--- a/libavformat/hevc.h
+++ b/libavformat/hevc.h
@@ -29,6 +29,27 @@
#include <stdint.h>
#include "avio.h"
+/** The general profile_tier_level() fields, as an hvcC record carries them. */
+typedef struct HVCCProfileTierLevel {
+ uint8_t profile_space;
+ uint8_t tier_flag;
+ uint8_t profile_idc;
+ uint32_t profile_compatibility_flags;
+ uint64_t constraint_indicator_flags; ///< 48 bits
+ uint8_t level_idc;
+} HVCCProfileTierLevel;
+
+/**
+ * Get the general profile, tier and level an hvcC record written from this
+ * extradata would carry.
+ *
+ * @param data extradata, either an hvcC record or Annex B
+ * @return 0 on success, a negative AVERROR code on failure. profile_idc
+ * and level_idc are 0 when the extradata names none.
+ */
+int ff_hevc_get_profile_tier_level(HVCCProfileTierLevel *ptl,
+ const uint8_t *data, int size);
+
/**
* Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
*
diff --git a/libavformat/tests/codecstring.c b/libavformat/tests/codecstring.c
index bba34a429e..b45e0d678c 100644
--- a/libavformat/tests/codecstring.c
+++ b/libavformat/tests/codecstring.c
@@ -45,6 +45,13 @@ static const uint8_t hevc_annexb[] = {
0x00, 0x03, 0x00, 0x20, 0x00, 0x00, 0x03, 0x03, 0x21, 0x00,
};
+static const uint8_t hevc_annexb_3byte_start_code[] = {
+ 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,
@@ -113,6 +120,9 @@ static const TestCase cases[] = {
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-3byte-start-code", AV_CODEC_ID_HEVC,
+ MKTAG('h', 'v', 'c', '1'), AV_PROFILE_UNKNOWN,
+ hevc_annexb_3byte_start_code, sizeof(hevc_annexb_3byte_start_code) },
{ "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'),
diff --git a/tests/ref/fate/codecstring b/tests/ref/fate/codecstring
index 7e7a886c93..169a367f18 100644
--- a/tests/ref/fate/codecstring
+++ b/tests/ref/fate/codecstring
@@ -3,6 +3,7 @@ h264-annexb-avc3: avc3.64000b
h264-avcc: avc1.64000b
hevc-annexb-hvc1: hvc1.1.6.L30.90
hevc-annexb-hev1: hev1.1.6.L30.90
+hevc-annexb-3byte-start-code: hvc1.1.6.L30.90
hevc-annexb-untagged: error
hevc-hvcc-hvc1: hvc1.1.6.L30.90
hevc-hvcc-hev1: hev1.1.6.L30.90