Skip to content

Truly independent web browser

LibMedia+Tests: Ignore Matroska cue positions for undeclared tracks

Zaggy1024af1c65795ba1
diff --git a/Libraries/LibMedia/Containers/Matroska/Reader.cpp b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
index 82b15c15caf..f85a86275df 100644
--- a/Libraries/LibMedia/Containers/Matroska/Reader.cpp
+++ b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
@@ -1070,9 +1070,10 @@ static DecoderErrorOr<CueTrackPosition> parse_cue_track_position(Streamer& strea
return track_position;
}
-static DecoderErrorOr<CuePoint> parse_cue_point(Streamer& streamer, u64 timestamp_scale)
+static DecoderErrorOr<CuePoint> parse_cue_point(Streamer& streamer, u64 timestamp_scale, OrderedHashMap<u64, NonnullRefPtr<TrackEntry>> const& tracks)
{
CuePoint cue_point;
+ bool had_track_positions = false;
TRY(Reader::parse_master_element(streamer, "CuePoint"sv, [&](u64 element_id) -> DecoderErrorOr<ElementIterationDecision> {
switch (element_id) {
@@ -1093,6 +1094,9 @@ static DecoderErrorOr<CuePoint> parse_cue_point(Streamer& streamer, u64 timestam
}
case CUE_TRACK_POSITIONS_ID: {
auto track_position = TRY(parse_cue_track_position(streamer));
+ had_track_positions = true;
+ if (!tracks.contains(track_position.track_number()))
+ break;
DECODER_TRY_ALLOC(cue_point.track_positions().try_set(track_position.track_number(), track_position));
break;
}
@@ -1107,7 +1111,7 @@ static DecoderErrorOr<CuePoint> parse_cue_point(Streamer& streamer, u64 timestam
if (cue_point.timestamp().is_negative())
return DecoderError::corrupted("CuePoint was missing a timestamp"sv);
- if (cue_point.track_positions().is_empty())
+ if (!had_track_positions)
return DecoderError::corrupted("CuePoint was missing track positions"sv);
return cue_point;
@@ -1129,7 +1133,7 @@ DecoderErrorOr<void> Reader::parse_cues(Streamer& streamer)
TRY(Reader::parse_master_element(streamer, "Cues"sv, [&](u64 element_id) -> DecoderErrorOr<ElementIterationDecision> {
switch (element_id) {
case CUE_POINT_ID: {
- auto cue_point = TRY(parse_cue_point(streamer, m_segment_information.timestamp_scale()));
+ auto cue_point = TRY(parse_cue_point(streamer, m_segment_information.timestamp_scale(), m_tracks));
// FIXME: Verify that these are already in order of timestamp. If they are not, return a corrupted error for now,
// but if it turns out that Matroska files with out-of-order cue points are valid, sort them instead.
diff --git a/Tests/LibMedia/TestParseMatroska.cpp b/Tests/LibMedia/TestParseMatroska.cpp
index 09b03db7515..b5190d27436 100644
--- a/Tests/LibMedia/TestParseMatroska.cpp
+++ b/Tests/LibMedia/TestParseMatroska.cpp
@@ -327,6 +327,57 @@ ByteBuffer make_matroska_without_cues(SegmentSize segment_size, ClusterSize clus
return data;
}
+ByteBuffer make_matroska_with_cue_points_for_tracks(Vector<Vector<u64>> const& track_numbers_of_cue_points)
+{
+ ByteBuffer data;
+ auto ebml_header = begin_ebml_master(data, Media::Matroska::EBML_MASTER_ELEMENT_ID);
+ append_ebml_string(data, Media::Matroska::DOCTYPE_ELEMENT_ID, "webm"sv);
+ append_ebml_uint(data, Media::Matroska::DOCTYPE_VERSION_ELEMENT_ID, 4);
+ finish_ebml_master(data, ebml_header);
+
+ auto segment = begin_ebml_master(data, Media::Matroska::SEGMENT_ELEMENT_ID);
+ auto segment_contents_position = data.size();
+
+ auto info = begin_ebml_master(data, Media::Matroska::SEGMENT_INFORMATION_ELEMENT_ID);
+ append_ebml_uint(data, Media::Matroska::TIMESTAMP_SCALE_ID, 1000000);
+ finish_ebml_master(data, info);
+
+ auto tracks = begin_ebml_master(data, Media::Matroska::TRACK_ELEMENT_ID);
+ auto track_entry = begin_ebml_master(data, Media::Matroska::TRACK_ENTRY_ID);
+ append_ebml_uint(data, Media::Matroska::TRACK_NUMBER_ID, 1);
+ append_ebml_uint(data, Media::Matroska::TRACK_UID_ID, 1);
+ append_ebml_uint(data, Media::Matroska::TRACK_TYPE_ID, 1);
+ append_ebml_string(data, Media::Matroska::TRACK_CODEC_ID, "V_VP9"sv);
+ auto video = begin_ebml_master(data, Media::Matroska::TRACK_VIDEO_ID);
+ append_ebml_uint(data, Media::Matroska::PIXEL_WIDTH_ID, 1);
+ append_ebml_uint(data, Media::Matroska::PIXEL_HEIGHT_ID, 1);
+ finish_ebml_master(data, video);
+ finish_ebml_master(data, track_entry);
+ finish_ebml_master(data, tracks);
+
+ auto cluster_position = data.size() - segment_contents_position;
+ auto cluster = begin_ebml_master(data, Media::Matroska::CLUSTER_ELEMENT_ID);
+ append_ebml_uint(data, Media::Matroska::TIMESTAMP_ID, 0);
+ finish_ebml_master(data, cluster);
+
+ auto cues = begin_ebml_master(data, Media::Matroska::CUES_ID);
+ for (size_t i = 0; i < track_numbers_of_cue_points.size(); i++) {
+ auto cue_point = begin_ebml_master(data, Media::Matroska::CUE_POINT_ID);
+ append_ebml_uint(data, Media::Matroska::CUE_TIME_ID, i);
+ for (auto track_number : track_numbers_of_cue_points[i]) {
+ auto cue_track_positions = begin_ebml_master(data, Media::Matroska::CUE_TRACK_POSITIONS_ID);
+ append_ebml_uint(data, Media::Matroska::CUE_TRACK_ID, track_number);
+ append_ebml_uint(data, Media::Matroska::CUE_CLUSTER_POSITION_ID, cluster_position);
+ finish_ebml_master(data, cue_track_positions);
+ }
+ finish_ebml_master(data, cue_point);
+ }
+ finish_ebml_master(data, cues);
+
+ finish_ebml_master(data, segment);
+ return data;
+}
+
ByteBuffer make_track_entry_with_element_of_size(u32 id, size_t payload_size)
{
ByteBuffer data;
@@ -403,6 +454,18 @@ TEST_CASE(reader_limits_linked_seek_head_traversal)
EXPECT_EQ(cue_points->size(), 1u);
}
+TEST_CASE(reader_ignores_cue_positions_for_undeclared_tracks)
+{
+ auto stream = Media::IncrementallyPopulatedStream::create_from_buffer(make_matroska_with_cue_points_for_tracks({ { 1, 2 }, { 3 }, { 1 } }));
+
+ auto reader = MUST(Media::Matroska::Reader::from_stream(stream->create_cursor()));
+ auto cue_points = reader.cue_points_for_track(1);
+ EXPECT(cue_points.has_value());
+ EXPECT_EQ(cue_points->size(), 2u);
+ EXPECT(!reader.cue_points_for_track(2).has_value());
+ EXPECT(!reader.cue_points_for_track(3).has_value());
+}
+
TEST_CASE(streamer_read_element_id)
{
// 1-byte ID: 0xEC (Void)