Truly independent web browser
LibMedia+LibMediaCodecs: Skip non-reference frames before the decoder
Zaggy1024dfb2eb72bbf8
diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp b/Libraries/LibMedia/Producers/DecodedVideoProducer.cppindex bb7b8b1fe9d..83cf5ac462b 100644--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp@@ -182,6 +182,9 @@ void DecodedVideoProducer::ThreadData::replace_decoder_once_drained(CodedFrame cDecoderErrorOr<void> DecodedVideoProducer::ThreadData::receive_into_decoder(CodedFrame const& frame, DecodeIntent intent){+ if (m_non_reference_frame_detector.frame_is_non_reference(frame) && intent == DecodeIntent::Reference)+ return {};+auto receive_result = m_decoder->receive_coded_data(frame, intent);if (receive_result.is_error() && receive_result.error().category() == DecoderErrorCategory::NotImplemented) {diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.h b/Libraries/LibMedia/Producers/DecodedVideoProducer.hindex 8843ada2032..3b6ec10edf6 100644--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.h+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.h@@ -27,6 +27,7 @@#include <LibMedia/Track.h>#include <LibMediaCodecs/CodedFrame.h>#include <LibMediaCodecs/DecoderError.h>+#include <LibMediaCodecs/NonReferenceFrameDetector.h>#include <LibMediaCodecs/VideoDecoder.h>#include <LibMediaCodecs/VideoDecoderRegistry.h>@@ -149,6 +150,7 @@ private:Optional<CodedFrame> m_frame_awaiting_decoder_replacement;DecodeIntent m_intent_awaiting_decoder_replacement { DecodeIntent::Output };OwnPtr<VideoDecoder> m_decoder;+ NonReferenceFrameDetector m_non_reference_frame_detector;bool m_decoder_needs_keyframe_next_seek { false };bool m_decoder_needs_codec_configuration_next_seek { true };diff --git a/Libraries/LibMedia/VideoToolbox/VideoToolboxVideoDecoder.mm b/Libraries/LibMedia/VideoToolbox/VideoToolboxVideoDecoder.mmindex 2ce91e2f822..477f5149b09 100644--- a/Libraries/LibMedia/VideoToolbox/VideoToolboxVideoDecoder.mm+++ b/Libraries/LibMedia/VideoToolbox/VideoToolboxVideoDecoder.mm@@ -284,8 +284,6 @@ struct FormatState {// Set while the tracked format may differ from the one the current session was built for.bool dirty { true };- // Whether every coded slice of the last frame belongs to a picture nothing will reference.- bool access_unit_is_non_reference { false };// Whether the last frame's picture is a random access point, which the pictures following it are associated with.bool access_unit_is_random_access_point { false };// Whether that random access point's leading pictures may reference pictures that precede it.@@ -318,20 +316,11 @@ struct H264State final : FormatState {if (auto configuration = coded_frame.new_codec_configuration(); configuration.has_value())TRY(apply_configuration(*configuration));- auto saw_coded_slice = false;- auto every_coded_slice_is_non_reference = true;Codecs::NALUnitIterator iterator { coded_frame.data(), nal_unit_length_size };- for (auto nal_unit = iterator.next(); nal_unit.has_value(); nal_unit = iterator.next()) {+ for (auto nal_unit = iterator.next(); nal_unit.has_value(); nal_unit = iterator.next())TRY(apply_nal_unit(*nal_unit));- if (!Codecs::H264::is_coded_slice((*nal_unit)[0]))- continue;- saw_coded_slice = true;- if (Codecs::H264::nal_ref_idc((*nal_unit)[0]) != 0)- every_coded_slice_is_non_reference = false;- }if (iterator.has_error())return DecoderError::corrupted("Invalid H.264 NAL unit length"sv);- access_unit_is_non_reference = saw_coded_slice && every_coded_slice_is_non_reference;return {};}@@ -411,49 +400,30 @@ struct H265State final : FormatState {if (auto configuration = coded_frame.new_codec_configuration(); configuration.has_value())TRY(apply_configuration(*configuration));- auto saw_coded_slice = false;- auto every_coded_slice_is_sub_layer_non_reference = true;auto saw_random_access_point = false;auto saw_open_gop_start = false;auto saw_open_gop_leading_picture = false;- u8 slice_temporal_id = 0;Codecs::NALUnitIterator iterator { coded_frame.data(), nal_unit_length_size };for (auto nal_unit = iterator.next(); nal_unit.has_value(); nal_unit = iterator.next()) {TRY(apply_nal_unit(*nal_unit));auto header = Codecs::H265::parse_nal_unit_header(*nal_unit);if (!header.has_value() || !Codecs::H265::is_coded_slice(*header))continue;- saw_coded_slice = true;- slice_temporal_id = max(slice_temporal_id, header->temporal_id);if (Codecs::H265::is_random_access_point(*header))saw_random_access_point = true;if (Codecs::H265::has_random_access_skipped_leading_pictures(*header))saw_open_gop_start = true;if (Codecs::H265::is_random_access_skipped_leading(*header))saw_open_gop_leading_picture = true;- if (!Codecs::H265::is_sub_layer_non_reference(*header))- every_coded_slice_is_sub_layer_non_reference = false;}if (iterator.has_error())return DecoderError::corrupted("Invalid H.265 NAL unit length"sv);- // ITU-T H.265 (07/2024), 7.4.2.2: an SLNR picture is only discardable for pictures at its own TemporalId.- access_unit_is_non_reference = saw_coded_slice && every_coded_slice_is_sub_layer_non_reference- && slice_temporal_id == highest_temporal_id();access_unit_is_random_access_point = saw_random_access_point;access_unit_begins_open_gop = saw_open_gop_start;access_unit_is_open_gop_leading_picture = saw_open_gop_leading_picture;return {};}- // Bound every SPS a slice could select, without parsing slice headers to track activation.- u8 highest_temporal_id() const- {- u8 highest = 0;- for (auto const& set : parameter_sets.sequence_parameter_sets())- highest = max(highest, set.parameters.sps_max_sub_layers_minus1);- return highest;- }-DecoderErrorOr<void> apply_configuration(ReadonlyBytes configuration){if (configuration.is_empty())@@ -882,11 +852,6 @@ DecoderErrorOr<void> VideoToolboxVideoDecoder::receive_coded_data(CodedFrame conif (m_discarding_open_gop_leading_pictures && m_format_state->access_unit_is_open_gop_leading_picture)return {};- if (intent == DecodeIntent::Reference) {- if (m_format_state->access_unit_is_non_reference)- return {};- }-auto data = coded_frame.data();auto block_was_released = false;diff --git a/Libraries/LibMediaCodecs/CMakeLists.txt b/Libraries/LibMediaCodecs/CMakeLists.txtindex af7980024d9..f0a16edba6d 100644--- a/Libraries/LibMediaCodecs/CMakeLists.txt+++ b/Libraries/LibMediaCodecs/CMakeLists.txt@@ -12,6 +12,7 @@ set(SOURCESDecoderError.cppFFmpeg/FFmpegFunctions.cppFFmpeg/FFmpegHelpers.cpp+ NonReferenceFrameDetector.cppVideoDecoderRegistry.cppVideoFrame.cppVideoFrameHandle.cppdiff --git a/Libraries/LibMediaCodecs/Codecs/H264.cpp b/Libraries/LibMediaCodecs/Codecs/H264.cppindex 409b8079182..f06d025dc8d 100644--- a/Libraries/LibMediaCodecs/Codecs/H264.cpp+++ b/Libraries/LibMediaCodecs/Codecs/H264.cpp@@ -52,6 +52,8 @@ static constexpr size_t NAL_UNIT_HEADER_SIZE = 1;static constexpr u8 NAL_UNIT_TYPE_MASK = 0x1F;static constexpr u8 SEQUENCE_PARAMETER_SET_NAL_UNIT_TYPE = 7;static constexpr u8 PICTURE_PARAMETER_SET_NAL_UNIT_TYPE = 8;+static constexpr u8 SEQUENCE_PARAMETER_SET_EXTENSION_NAL_UNIT_TYPE = 13;+static constexpr u8 SUBSET_SEQUENCE_PARAMETER_SET_NAL_UNIT_TYPE = 15;static constexpr u8 NAL_REF_IDC_MASK = 0x60;static constexpr u8 NAL_REF_IDC_SHIFT = 5;static constexpr u8 FIRST_CODED_SLICE_NAL_UNIT_TYPE = 1;@@ -210,6 +212,16 @@ u8 H264::nal_ref_idc(u8 nal_unit_header)return (nal_unit_header & NAL_REF_IDC_MASK) >> NAL_REF_IDC_SHIFT;}+// ITU-T H.264 (08/2024), Table 7-1.+bool H264::is_parameter_set(u8 nal_unit_header)+{+ auto nal_unit_type = nal_unit_header & NAL_UNIT_TYPE_MASK;+ return nal_unit_type == SEQUENCE_PARAMETER_SET_NAL_UNIT_TYPE+ || nal_unit_type == PICTURE_PARAMETER_SET_NAL_UNIT_TYPE+ || nal_unit_type == SEQUENCE_PARAMETER_SET_EXTENSION_NAL_UNIT_TYPE+ || nal_unit_type == SUBSET_SEQUENCE_PARAMETER_SET_NAL_UNIT_TYPE;+}+// ITU-T H.264 (08/2024), 7.3.2.1.1: seq_parameter_set_data().Optional<H264::SequenceParameterSet> H264::parse_sequence_parameter_set(ReadonlyBytes nal_unit){diff --git a/Libraries/LibMediaCodecs/Codecs/H264.h b/Libraries/LibMediaCodecs/Codecs/H264.hindex e5c4eb18661..c7bbdd53844 100644--- a/Libraries/LibMediaCodecs/Codecs/H264.h+++ b/Libraries/LibMediaCodecs/Codecs/H264.h@@ -108,6 +108,7 @@ public:// Read the NAL unit header byte's fields (H.264, 7.4.1 and Table 7-1).static MEDIACODECS_API bool is_coded_slice(u8 nal_unit_header);static MEDIACODECS_API u8 nal_ref_idc(u8 nal_unit_header);+ static MEDIACODECS_API bool is_parameter_set(u8 nal_unit_header);// Parse the fields needed for parameter-set tracking and output reordering from a complete NAL unit,// including its header. These do not validate the remaining syntax of the parameter set.diff --git a/Libraries/LibMediaCodecs/Codecs/H265.cpp b/Libraries/LibMediaCodecs/Codecs/H265.cppindex 5aced8c368d..5c094842b45 100644--- a/Libraries/LibMediaCodecs/Codecs/H265.cpp+++ b/Libraries/LibMediaCodecs/Codecs/H265.cpp@@ -148,6 +148,12 @@ bool H265::is_sub_layer_non_reference(NALUnitHeader const& header)return header.nal_unit_type < FIRST_IRAP_NAL_UNIT_TYPE && header.nal_unit_type % 2 == 0;}+// ITU-T H.265 (07/2024), Table 7-1.+bool H265::is_parameter_set(NALUnitHeader const& header)+{+ return header.nal_unit_type >= VIDEO_PARAMETER_SET_NAL_UNIT_TYPE && header.nal_unit_type <= PICTURE_PARAMETER_SET_NAL_UNIT_TYPE;+}+// ITU-T H.265 (07/2024), 3.73: every VCL NAL unit of an IRAP picture is in the range BLA_W_LP to RSV_IRAP_VCL23.bool H265::is_random_access_point(NALUnitHeader const& header){diff --git a/Libraries/LibMediaCodecs/Codecs/H265.h b/Libraries/LibMediaCodecs/Codecs/H265.hindex 1fa4a2d2c3c..ad3a03b4fe9 100644--- a/Libraries/LibMediaCodecs/Codecs/H265.h+++ b/Libraries/LibMediaCodecs/Codecs/H265.h@@ -131,6 +131,7 @@ public:static MEDIACODECS_API Optional<NALUnitHeader> parse_nal_unit_header(ReadonlyBytes nal_unit);static MEDIACODECS_API bool is_coded_slice(NALUnitHeader const&);static MEDIACODECS_API bool is_sub_layer_non_reference(NALUnitHeader const&);+ static MEDIACODECS_API bool is_parameter_set(NALUnitHeader const&);static MEDIACODECS_API bool is_random_access_point(NALUnitHeader const&);static MEDIACODECS_API bool is_random_access_skipped_leading(NALUnitHeader const&);static MEDIACODECS_API bool has_random_access_skipped_leading_pictures(NALUnitHeader const&);diff --git a/Libraries/LibMediaCodecs/NonReferenceFrameDetector.cpp b/Libraries/LibMediaCodecs/NonReferenceFrameDetector.cppnew file mode 100644index 00000000000..bee0d0ea669--- /dev/null+++ b/Libraries/LibMediaCodecs/NonReferenceFrameDetector.cpp@@ -0,0 +1,123 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibMediaCodecs/Codecs/H264.h>+#include <LibMediaCodecs/Codecs/H265.h>+#include <LibMediaCodecs/Codecs/NALUnit.h>+#include <LibMediaCodecs/CodedFrame.h>+#include <LibMediaCodecs/NonReferenceFrameDetector.h>++namespace Media {++bool NonReferenceFrameDetector::frame_is_non_reference(CodedFrame const& frame)+{+ if (auto configuration = frame.new_codec_configuration(); configuration.has_value()) {+ m_codec_id = frame.codec_id();+ apply_codec_configuration(*configuration);+ } else if (frame.codec_id() != m_codec_id) {+ m_codec_id = frame.codec_id();+ m_nal_unit_length_size = {};+ }++ switch (m_codec_id) {+ case CodecID::H264:+ return h264_frame_is_non_reference(frame.data());+ case CodecID::H265:+ return h265_frame_is_non_reference(frame.data());+ default:+ return false;+ }+}++void NonReferenceFrameDetector::apply_codec_configuration(ReadonlyBytes configuration)+{+ m_nal_unit_length_size = {};+ m_highest_temporal_id = 0;++ switch (m_codec_id) {+ case CodecID::H264: {+ auto sets = Codecs::H264::parse_parameter_sets_from_configuration_record(configuration);+ if (sets.has_value())+ m_nal_unit_length_size = sets->nal_unit_length_size;+ break;+ }+ case CodecID::H265: {+ auto sets = Codecs::H265::parse_parameter_sets_from_configuration_record(configuration);+ if (!sets.has_value())+ break;+ for (auto nal_unit : sets->sequence) {+ auto sequence_set = Codecs::H265::parse_sequence_parameter_set(nal_unit);+ if (!sequence_set.has_value())+ return;+ m_highest_temporal_id = max(m_highest_temporal_id, sequence_set->sps_max_sub_layers_minus1);+ }+ m_nal_unit_length_size = sets->nal_unit_length_size;+ break;+ }+ default:+ break;+ }+}++bool NonReferenceFrameDetector::h264_frame_is_non_reference(ReadonlyBytes data) const+{+ if (!m_nal_unit_length_size.has_value())+ return false;++ // A parameter set must reach the decoder, whichever picture it travels with.+ auto saw_coded_slice = false;+ Codecs::NALUnitIterator iterator { data, m_nal_unit_length_size.value() };+ for (auto nal_unit = iterator.next(); nal_unit.has_value(); nal_unit = iterator.next()) {+ auto header = (*nal_unit)[0];+ if (Codecs::H264::is_parameter_set(header))+ return false;+ if (!Codecs::H264::is_coded_slice(header))+ continue;+ if (Codecs::H264::nal_ref_idc(header) != 0)+ return false;+ saw_coded_slice = true;+ }+ return saw_coded_slice && !iterator.has_error();+}++bool NonReferenceFrameDetector::h265_frame_is_non_reference(ReadonlyBytes data)+{+ if (!m_nal_unit_length_size.has_value())+ return false;++ // A parameter set must reach the decoder, whichever picture it travels with. Its SPS still has to be read, so+ // that the frames after it are judged by the sub-layers it allows.+ auto saw_parameter_set = false;+ auto saw_coded_slice = false;+ auto every_coded_slice_is_sub_layer_non_reference = true;+ u8 slice_temporal_id = 0;+ Codecs::NALUnitIterator iterator { data, m_nal_unit_length_size.value() };+ for (auto nal_unit = iterator.next(); nal_unit.has_value(); nal_unit = iterator.next()) {+ auto header = Codecs::H265::parse_nal_unit_header(*nal_unit);+ if (!header.has_value())+ continue;+ if (Codecs::H265::is_parameter_set(*header)) {+ saw_parameter_set = true;+ if (header->nuh_layer_id != 0)+ continue;+ if (auto sequence_set = Codecs::H265::parse_sequence_parameter_set(*nal_unit); sequence_set.has_value())+ m_highest_temporal_id = max(m_highest_temporal_id, sequence_set->sps_max_sub_layers_minus1);+ continue;+ }+ if (!Codecs::H265::is_coded_slice(*header))+ continue;+ saw_coded_slice = true;+ slice_temporal_id = max(slice_temporal_id, header->temporal_id);+ if (!Codecs::H265::is_sub_layer_non_reference(*header))+ every_coded_slice_is_sub_layer_non_reference = false;+ }+ if (iterator.has_error() || saw_parameter_set || !saw_coded_slice)+ return false;+ // ITU-T H.265 (07/2024), 7.4.2.2: an SLNR picture is only discardable for pictures at its own TemporalId.+ return every_coded_slice_is_sub_layer_non_reference && slice_temporal_id == m_highest_temporal_id;+}++}diff --git a/Libraries/LibMediaCodecs/NonReferenceFrameDetector.h b/Libraries/LibMediaCodecs/NonReferenceFrameDetector.hnew file mode 100644index 00000000000..ab4bc88b283--- /dev/null+++ b/Libraries/LibMediaCodecs/NonReferenceFrameDetector.h@@ -0,0 +1,36 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Optional.h>+#include <AK/Span.h>+#include <AK/Types.h>+#include <LibMediaCodecs/CodecID.h>+#include <LibMediaCodecs/Export.h>+#include <LibMediaCodecs/Forward.h>++namespace Media {++// Recognizes the coded frames that no other frame is predicted from, which only need decoding to be shown. Frames+// carry the stream parameters this depends on, so every frame of a track must pass through it in decode order.+class MEDIACODECS_API NonReferenceFrameDetector {+public:+ bool frame_is_non_reference(CodedFrame const&);++private:+ void apply_codec_configuration(ReadonlyBytes);+ bool h264_frame_is_non_reference(ReadonlyBytes) const;+ bool h265_frame_is_non_reference(ReadonlyBytes);++ CodecID m_codec_id { CodecID::Unknown };+ // Empty while the frames cannot be split into NAL units, so that none of them is reported.+ Optional<u8> m_nal_unit_length_size;+ // Taken over every H.265 SPS since the last configuration, since overestimating it only leaves frames unreported.+ u8 m_highest_temporal_id { 0 };+};++}diff --git a/Tests/LibMedia/CMakeLists.txt b/Tests/LibMedia/CMakeLists.txtindex b2306ee32b7..edf78ce8b9b 100644--- a/Tests/LibMedia/CMakeLists.txt+++ b/Tests/LibMedia/CMakeLists.txt@@ -26,6 +26,7 @@ set(TEST_SOURCESTestMP3FrameHeader.cppTestMP3Reader.cppTestMP3StreamInfo.cpp+ TestNonReferenceFrameDetector.cppTestMediaSupport.cppTestMediaTime.cppTestOggDemuxer.cppdiff --git a/Tests/LibMedia/TestNonReferenceFrameDetector.cpp b/Tests/LibMedia/TestNonReferenceFrameDetector.cppnew file mode 100644index 00000000000..7d3eb60baaa--- /dev/null+++ b/Tests/LibMedia/TestNonReferenceFrameDetector.cpp@@ -0,0 +1,130 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/FixedArray.h>+#include <AK/Vector.h>+#include <LibMediaCodecs/Codecs/H264.h>+#include <LibMediaCodecs/Codecs/H265.h>+#include <LibMediaCodecs/CodedFrame.h>+#include <LibMediaCodecs/NonReferenceFrameDetector.h>+#include <LibTest/TestCase.h>++using H264 = Media::Codecs::H264;+using H265 = Media::Codecs::H265;++static FixedArray<u8> length_prefixed_nal_units(Vector<ReadonlyBytes> const& nal_units)+{+ Vector<u8> data;+ for (auto nal_unit : nal_units) {+ auto size = static_cast<u32>(nal_unit.size());+ data.append(static_cast<u8>(size >> 24));+ data.append(static_cast<u8>(size >> 16));+ data.append(static_cast<u8>(size >> 8));+ data.append(static_cast<u8>(size));+ data.append(nal_unit.data(), nal_unit.size());+ }+ return MUST(FixedArray<u8>::create(data.span()));+}++static Media::CodedFrame coded_frame(Media::CodecID codec_id, Vector<ReadonlyBytes> const& nal_units, Optional<ReadonlyBytes> configuration = {})+{+ Optional<FixedArray<u8>> configuration_storage;+ if (configuration.has_value())+ configuration_storage = MUST(FixedArray<u8>::create(*configuration));+ return Media::CodedFrame(codec_id, {}, {}, {}, Media::FrameFlags::None, length_prefixed_nal_units(nal_units), move(configuration_storage));+}++// The slice data after each header is irrelevant, since only the headers are read.+static constexpr Array<u8, 2> H264_REFERENCE_SLICE { 0x41, 0x9a };+static constexpr Array<u8, 2> H264_NON_REFERENCE_SLICE { 0x01, 0x9e };+static constexpr Array<u8, 2> H264_IDR_SLICE { 0x65, 0x88 };+static constexpr Array<u8, 2> H264_SUPPLEMENTAL_ENHANCEMENT_INFORMATION { 0x06, 0x05 };++static ReadonlyBytes h264_configuration() { return H264::representative_configuration_record_for_profile(H264::Profile::High); }++TEST_CASE(h264_reports_frames_whose_slices_are_all_non_reference)+{+ Media::NonReferenceFrameDetector detector;+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_IDR_SLICE }, h264_configuration())));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_REFERENCE_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_SUPPLEMENTAL_ENHANCEMENT_INFORMATION, H264_NON_REFERENCE_SLICE, H264_NON_REFERENCE_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE, H264_REFERENCE_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_SUPPLEMENTAL_ENHANCEMENT_INFORMATION })));+}++TEST_CASE(h264_passes_non_reference_frames_carrying_parameter_sets)+{+ Media::NonReferenceFrameDetector detector;+ auto sets = H264::parse_parameter_sets_from_configuration_record(h264_configuration());+ VERIFY(sets.has_value());+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_IDR_SLICE }, h264_configuration())));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { sets->sequence[0], H264_NON_REFERENCE_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { sets->picture[0], H264_NON_REFERENCE_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE })));+}++TEST_CASE(h264_reports_nothing_without_a_usable_configuration)+{+ Media::NonReferenceFrameDetector detector;+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE })));++ Array<u8, 1> truncated_configuration { 0x01 };+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE }, truncated_configuration.span())));+}++TEST_CASE(h264_reports_nothing_for_frames_that_cannot_be_split)+{+ Media::NonReferenceFrameDetector detector;+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_IDR_SLICE }, h264_configuration())));++ auto frame_data = length_prefixed_nal_units({ H264_NON_REFERENCE_SLICE });+ auto overrunning_data = MUST(FixedArray<u8>::create(frame_data.span()));+ overrunning_data[3] = 0xff;+ EXPECT(!detector.frame_is_non_reference(Media::CodedFrame(Media::CodecID::H264, {}, {}, {}, Media::FrameFlags::None, move(overrunning_data))));+}++TEST_CASE(a_codec_change_without_a_configuration_stops_reports)+{+ Media::NonReferenceFrameDetector detector;+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_IDR_SLICE }, h264_configuration())));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::VP9, { H264_NON_REFERENCE_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H264, { H264_NON_REFERENCE_SLICE })));+}++// Two-byte headers: the type in bits 1 to 6 of the first byte, and TemporalId + 1 in the low bits of the second.+static constexpr Array<u8, 3> H265_IDR_SLICE { 0x26, 0x01, 0xaf };+static constexpr Array<u8, 3> H265_TRAIL_R_SLICE { 0x02, 0x01, 0xd0 };+static constexpr Array<u8, 3> H265_TRAIL_N_SLICE { 0x00, 0x01, 0xd0 };+static constexpr Array<u8, 3> H265_TRAIL_N_SLICE_AT_TEMPORAL_ID_1 { 0x00, 0x02, 0xd0 };+static constexpr Array<u8, 3> H265_RASL_N_SLICE { 0x10, 0x01, 0xd0 };++static ReadonlyBytes h265_configuration() { return H265::representative_configuration_record_for_profile(H265::Profile::Main); }++TEST_CASE(h265_reports_sub_layer_non_reference_frames_at_the_highest_temporal_id)+{+ Media::NonReferenceFrameDetector detector;+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_IDR_SLICE }, h265_configuration())));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_TRAIL_R_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_TRAIL_N_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_RASL_N_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_TRAIL_N_SLICE, H265_TRAIL_R_SLICE })));+ // The configuration's SPS allows a single sub-layer, so a picture above it says nothing about what references it.+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_TRAIL_N_SLICE_AT_TEMPORAL_ID_1 })));+}++TEST_CASE(h265_passes_non_reference_frames_carrying_parameter_sets)+{+ Media::NonReferenceFrameDetector detector;+ auto sets = H265::parse_parameter_sets_from_configuration_record(h265_configuration());+ VERIFY(sets.has_value());+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_IDR_SLICE }, h265_configuration())));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { sets->video[0], H265_TRAIL_N_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { sets->sequence[0], H265_TRAIL_N_SLICE })));+ EXPECT(!detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { sets->picture[0], H265_TRAIL_N_SLICE })));+ EXPECT(detector.frame_is_non_reference(coded_frame(Media::CodecID::H265, { H265_TRAIL_N_SLICE })));+}