Skip to content

Truly independent web browser

LibMediaCodecs+Tests: Only enable baseline H.264 in software on macOS

Zaggy1024b981db81f4e2
diff --git a/Libraries/LibMediaCodecs/Codecs/H264.cpp b/Libraries/LibMediaCodecs/Codecs/H264.cpp
index 3286125c12e..b0e6e0ba107 100644
--- a/Libraries/LibMediaCodecs/Codecs/H264.cpp
+++ b/Libraries/LibMediaCodecs/Codecs/H264.cpp
@@ -259,11 +259,14 @@ Optional<H264::SequenceParameterSet> H264::parse_sequence_parameter_set(Readonly
if (reader.has_overrun() || seq_parameter_set_id > 31)
return {};
+ // ITU-T H.264 (08/2024), 7.4.2.1.1: absent chroma_format_idc is inferred as 1 (4:2:0), and absent bit depths as 8.
+ u32 chroma_format_idc = 1;
+ u32 bit_depth_luma_minus8 = 0;
if (profile_has_chroma_format_fields(profile_idc)) {
- auto chroma_format_idc = reader.read_unsigned_exp_golomb();
+ chroma_format_idc = reader.read_unsigned_exp_golomb();
if (chroma_format_idc == 3)
- reader.skip_bits(1); // separate_colour_plane_flag
- reader.read_unsigned_exp_golomb(); // bit_depth_luma_minus8
+ reader.skip_bits(1); // separate_colour_plane_flag
+ bit_depth_luma_minus8 = reader.read_unsigned_exp_golomb();
reader.read_unsigned_exp_golomb(); // bit_depth_chroma_minus8
reader.skip_bits(1); // qpprime_y_zero_transform_bypass_flag
if (reader.read_bit()) { // seq_scaling_matrix_present_flag
@@ -310,11 +313,18 @@ Optional<H264::SequenceParameterSet> H264::parse_sequence_parameter_set(Readonly
if (reader.read_bit()) // vui_parameters_present_flag
stated_count = parse_stated_max_reorder_frame_count(reader);
- if (reader.has_overrun())
+ // ITU-T H.264 (08/2024), 7.4.2.1.1: chroma_format_idc is in 0 to 3, bit_depth_luma_minus8 in 0 to 6.
+ if (reader.has_overrun() || chroma_format_idc > 3 || bit_depth_luma_minus8 > 6)
return {};
// ITU-T H.264 (08/2024), E.2.1: Infer absent max_num_reorder_frames as zero for these
// profiles with constraint_set3_flag set, and as MaxDpbFrames otherwise.
- SequenceParameterSet parameter_set { .seq_parameter_set_id = static_cast<u8>(seq_parameter_set_id), .max_num_reorder_frames = 0 };
+ SequenceParameterSet parameter_set {
+ .seq_parameter_set_id = static_cast<u8>(seq_parameter_set_id),
+ .max_num_reorder_frames = 0,
+ .profile_idc = profile_idc,
+ .chroma_format_idc = static_cast<u8>(chroma_format_idc),
+ .bit_depth_luma = static_cast<u8>(bit_depth_luma_minus8 + 8),
+ };
if (stated_count.has_value()) {
parameter_set.max_num_reorder_frames = static_cast<u8>(min(*stated_count, static_cast<u32>(MAXIMUM_REORDER_FRAME_COUNT)));
} else if (!(constraint_set3_flag && first_is_one_of(profile_idc, 44, 86, 100, 110, 122, 244))) {
diff --git a/Libraries/LibMediaCodecs/Codecs/H264.h b/Libraries/LibMediaCodecs/Codecs/H264.h
index 1e73649bdb9..ee9ddb75169 100644
--- a/Libraries/LibMediaCodecs/Codecs/H264.h
+++ b/Libraries/LibMediaCodecs/Codecs/H264.h
@@ -65,6 +65,9 @@ public:
struct SequenceParameterSet {
u8 seq_parameter_set_id;
u8 max_num_reorder_frames;
+ u8 profile_idc { 0 };
+ u8 chroma_format_idc { 1 };
+ u8 bit_depth_luma { 8 };
};
struct PictureParameterSet {
diff --git a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
index 063c2af41c7..1b21f8c86e3 100644
--- a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
+++ b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
@@ -167,6 +167,9 @@ struct DecoderFormat {
RetainedRef<CFMutableDictionaryRef> destination_attributes;
CodingIndependentCodePoints cicp;
u8 reorder_frame_count { 0 };
+ // Set where VideoToolbox has no software decoder for the format, or has one that accepts the format but then fails
+ // every frame of it.
+ bool requires_hardware_decoder { false };
};
// The parameter sets of H.264 and H.265 describe the stream's colours in their video usability information, which the
@@ -213,15 +216,15 @@ RetainedRef<CFMutableDictionaryRef> destination_attributes_for_bit_depth(u8 bit_
return attributes;
}
-bool hardware_decoding_is_required(CodecID codec_id)
+// The software H.264 decoder accepts a session for any profile, but fails every frame of a stream outside 8-bit 4:2:0 at
+// High profile or below. Nothing short of decoding a frame reveals this.
+bool software_decoder_can_decode(Codecs::H264::SequenceParameterSet const& sequence_parameter_set)
{
- switch (codec_id) {
- case CodecID::H264:
- case CodecID::H265:
- return false;
- default:
- return true;
- }
+ constexpr u8 high_profile_idc = 100;
+ constexpr u8 chroma_format_420 = 1;
+ return sequence_parameter_set.profile_idc <= high_profile_idc
+ && sequence_parameter_set.chroma_format_idc == chroma_format_420
+ && sequence_parameter_set.bit_depth_luma == 8;
}
bool is_running_in_virtual_machine()
@@ -240,7 +243,7 @@ DecoderErrorOr<VTDecompressionSessionRef> create_decompression_session(CodecID c
{
// A virtual machine's hardware decoding goes through paravirtualization, which leaks a connection per process.
auto hardware_is_usable = !is_running_in_virtual_machine();
- if (hardware_decoding_is_required(codec_id) && !hardware_is_usable)
+ if (format.requires_hardware_decoder && !hardware_is_usable)
return DecoderError::with_description(DecoderErrorCategory::NotImplemented, "VideoToolbox has no decoder for this format"sv);
auto dimensions = CMVideoFormatDescriptionGetDimensions(format.description.ref());
@@ -250,7 +253,7 @@ DecoderErrorOr<VTDecompressionSessionRef> create_decompression_session(CodecID c
VTRegisterSupplementalVideoDecoderIfAvailable(codec_type_from_codec_id(codec_id));
RetainedRef<CFMutableDictionaryRef> specification { CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) };
- if (hardware_decoding_is_required(codec_id))
+ if (format.requires_hardware_decoder)
CFDictionarySetValue(specification.ref(), kVTVideoDecoderSpecification_RequireHardwareAcceleratedVideoDecoder, kCFBooleanTrue);
else
CFDictionarySetValue(specification.ref(), kVTVideoDecoderSpecification_EnableHardwareAcceleratedVideoDecoder, hardware_is_usable ? kCFBooleanTrue : kCFBooleanFalse);
@@ -359,11 +362,13 @@ struct H264State final : FormatState {
Vector<u8 const*, 288> set_pointers;
Vector<size_t, 288> set_sizes;
u8 reorder_frame_count = 0;
+ auto requires_hardware_decoder = false;
for (auto const& set : parameter_sets.sequence_parameter_sets()) {
set_pointers.append(set.nal_unit.data());
set_sizes.append(set.nal_unit.size());
// Bound every SPS a slice could select, without parsing slice headers to track activation.
reorder_frame_count = max(reorder_frame_count, set.parameters.max_num_reorder_frames);
+ requires_hardware_decoder |= !software_decoder_can_decode(set.parameters);
}
if (set_pointers.is_empty())
return {};
@@ -382,7 +387,7 @@ struct H264State final : FormatState {
if (CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault, set_pointers.size(), set_pointers.data(), set_sizes.data(), nal_unit_length_size, &description) != noErr)
return {};
- return DecoderFormat { RetainedRef<CMVideoFormatDescriptionRef> { description }, destination_attributes_for_bit_depth(8), cicp_of_format_description(description), reorder_frame_count };
+ return DecoderFormat { RetainedRef<CMVideoFormatDescriptionRef> { description }, destination_attributes_for_bit_depth(8), cicp_of_format_description(description), reorder_frame_count, requires_hardware_decoder };
}
};
@@ -556,7 +561,12 @@ struct VP9State final : FormatState {
if (CMVideoFormatDescriptionCreate(kCFAllocatorDefault, kCMVideoCodecType_VP9, header->size.width(), header->size.height(), extensions.ref(), &description) != noErr)
return {};
- return DecoderFormat { RetainedRef<CMVideoFormatDescriptionRef> { description }, destination_attributes_for_bit_depth(header->bit_depth), header->color_parameters.cicp };
+ return DecoderFormat {
+ .description = RetainedRef<CMVideoFormatDescriptionRef> { description },
+ .destination_attributes = destination_attributes_for_bit_depth(header->bit_depth),
+ .cicp = header->color_parameters.cicp,
+ .requires_hardware_decoder = true,
+ };
}
};
@@ -600,7 +610,12 @@ struct AV1State final : FormatState {
if (CMVideoFormatDescriptionCreate(kCFAllocatorDefault, kCMVideoCodecType_AV1, size.width(), size.height(), extensions.ref(), &description) != noErr)
return {};
- return DecoderFormat { RetainedRef<CMVideoFormatDescriptionRef> { description }, destination_attributes_for_bit_depth(parameters.bit_depth), parameters.optional_fields.cicp };
+ return DecoderFormat {
+ .description = RetainedRef<CMVideoFormatDescriptionRef> { description },
+ .destination_attributes = destination_attributes_for_bit_depth(parameters.bit_depth),
+ .cicp = parameters.optional_fields.cicp,
+ .requires_hardware_decoder = true,
+ };
}
};
@@ -689,7 +704,7 @@ static Optional<DecoderCapabilities> probe_decoder_capabilities(ParsedCodec cons
return {};
auto* session = session_or_error.release_value();
- auto uses_hardware = hardware_decoding_is_required(codec.codec_id()) || session_uses_hardware_decoder(session);
+ auto uses_hardware = format->requires_hardware_decoder || session_uses_hardware_decoder(session);
VTDecompressionSessionInvalidate(session);
CFRelease(session);
return DecoderCapabilities { .smooth = true, .power_efficient = uses_hardware };
diff --git a/Tests/LibMedia/TestCodecConfigurations.cpp b/Tests/LibMedia/TestCodecConfigurations.cpp
index 1c294b21c1f..1c1b94daf3b 100644
--- a/Tests/LibMedia/TestCodecConfigurations.cpp
+++ b/Tests/LibMedia/TestCodecConfigurations.cpp
@@ -1144,6 +1144,45 @@ TEST_CASE(h264_sequence_parameter_set_returns_its_id_and_inferred_reorder_depth)
EXPECT(!Media::Codecs::H264::parse_sequence_parameter_set(nal_unit).has_value());
}
+TEST_CASE(h264_sequence_parameter_set_reads_its_profile_chroma_format_and_bit_depth)
+{
+ auto expect_format = [](ReadonlyBytes nal_unit, u8 profile_idc, u8 chroma_format_idc, u8 bit_depth_luma) {
+ auto parameter_set = Media::Codecs::H264::parse_sequence_parameter_set(nal_unit);
+ EXPECT(parameter_set.has_value());
+ if (!parameter_set.has_value())
+ return;
+ EXPECT_EQ(parameter_set->profile_idc, profile_idc);
+ EXPECT_EQ(parameter_set->chroma_format_idc, chroma_format_idc);
+ EXPECT_EQ(parameter_set->bit_depth_luma, bit_depth_luma);
+ };
+
+ // Baseline profile states no chroma format or bit depth, so they are inferred as 4:2:0 and 8-bit.
+ Array<u8, 10> baseline { 0x67, 0x42, 0x00, 0x1e, 0x04, 0x17, 0x03, 0x60, 0xf6, 0x40 };
+ expect_format(baseline, 66, 1, 8);
+
+ // x264's sequence parameter sets for High, High 10, High 4:2:2 and High 4:4:4 Predictive streams.
+ Array<u8, 27> high {
+ 0x67, 0x64, 0x00, 0x0d, 0xac, 0xd9, 0x41, 0x41, 0xfb, 0x01, 0x6a, 0x04, 0x04, 0x0a, 0x80, 0x00,
+ 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x19, 0x07, 0x8a, 0x14, 0xcb
+ };
+ expect_format(high, 100, 1, 8);
+ Array<u8, 29> high_10 {
+ 0x67, 0x6e, 0x00, 0x0d, 0xa6, 0xcd, 0x94, 0x14, 0x1f, 0xb0, 0x16, 0xa0, 0x40, 0x40, 0xa8, 0x00,
+ 0x00, 0x03, 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x90, 0x78, 0xa1, 0x4c, 0xb0
+ };
+ expect_format(high_10, 110, 1, 10);
+ Array<u8, 27> high_422 {
+ 0x67, 0x7a, 0x00, 0x0d, 0xbc, 0xd9, 0x41, 0x41, 0xfb, 0x01, 0x6a, 0x04, 0x04, 0x0a, 0x80, 0x00,
+ 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x19, 0x07, 0x8a, 0x14, 0xcb
+ };
+ expect_format(high_422, 122, 2, 8);
+ Array<u8, 29> high_444 {
+ 0x67, 0xf4, 0x00, 0x0d, 0x91, 0x9b, 0x28, 0x28, 0x3f, 0x60, 0x2d, 0x40, 0x80, 0x81, 0x50, 0x00,
+ 0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, 0x03, 0x20, 0xf1, 0x42, 0x99, 0x60
+ };
+ expect_format(high_444, 244, 3, 8);
+}
+
TEST_CASE(h264_picture_parameter_set_returns_its_id_and_sequence_reference)
{
// PPS ID 255 referring to SPS ID 31, the upper bounds from H.264 (08/2024), 7.4.2.2.