MatroskaDemuxer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MatroskaDemuxer.h"
  7. #include "AK/Debug.h"
  8. namespace Video::Matroska {
  9. DecoderErrorOr<NonnullOwnPtr<MatroskaDemuxer>> MatroskaDemuxer::from_file(StringView filename)
  10. {
  11. return make<MatroskaDemuxer>(TRY(Reader::from_file(filename)));
  12. }
  13. DecoderErrorOr<NonnullOwnPtr<MatroskaDemuxer>> MatroskaDemuxer::from_data(ReadonlyBytes data)
  14. {
  15. return make<MatroskaDemuxer>(TRY(Reader::from_data(data)));
  16. }
  17. DecoderErrorOr<Vector<Track>> MatroskaDemuxer::get_tracks_for_type(TrackType type)
  18. {
  19. TrackEntry::TrackType matroska_track_type;
  20. switch (type) {
  21. case TrackType::Video:
  22. matroska_track_type = TrackEntry::TrackType::Video;
  23. break;
  24. case TrackType::Audio:
  25. matroska_track_type = TrackEntry::TrackType::Audio;
  26. break;
  27. case TrackType::Subtitles:
  28. matroska_track_type = TrackEntry::TrackType::Subtitle;
  29. break;
  30. }
  31. Vector<Track> tracks;
  32. TRY(m_reader.for_each_track_of_type(matroska_track_type, [&](TrackEntry const& track_entry) -> DecoderErrorOr<IterationDecision> {
  33. VERIFY(track_entry.track_type() == matroska_track_type);
  34. Track track(type, track_entry.track_number());
  35. switch (type) {
  36. case TrackType::Video:
  37. if (auto video_track = track_entry.video_track(); video_track.has_value())
  38. track.set_video_data({ TRY(duration()), video_track->pixel_width, video_track->pixel_height });
  39. break;
  40. default:
  41. break;
  42. }
  43. DECODER_TRY_ALLOC(tracks.try_append(track));
  44. return IterationDecision::Continue;
  45. }));
  46. return tracks;
  47. }
  48. DecoderErrorOr<MatroskaDemuxer::TrackStatus*> MatroskaDemuxer::get_track_status(Track track)
  49. {
  50. if (!m_track_statuses.contains(track)) {
  51. auto iterator = TRY(m_reader.create_sample_iterator(track.identifier()));
  52. DECODER_TRY_ALLOC(m_track_statuses.try_set(track, { iterator }));
  53. }
  54. return &m_track_statuses.get(track).release_value();
  55. }
  56. DecoderErrorOr<Optional<Time>> MatroskaDemuxer::seek_to_most_recent_keyframe(Track track, Time timestamp, Optional<Time> earliest_available_sample)
  57. {
  58. // Removing the track status will cause us to start from the beginning.
  59. if (timestamp.is_zero()) {
  60. m_track_statuses.remove(track);
  61. return timestamp;
  62. }
  63. auto& track_status = *TRY(get_track_status(track));
  64. auto seeked_iterator = TRY(m_reader.seek_to_random_access_point(track_status.iterator, timestamp));
  65. VERIFY(seeked_iterator.last_timestamp().has_value());
  66. auto last_sample = earliest_available_sample;
  67. if (!last_sample.has_value()) {
  68. last_sample = track_status.iterator.last_timestamp();
  69. }
  70. if (last_sample.has_value()) {
  71. bool skip_seek = seeked_iterator.last_timestamp().value() <= last_sample.value() && last_sample.value() <= timestamp;
  72. dbgln_if(MATROSKA_DEBUG, "The last available sample at {}ms is {}closer to target timestamp {}ms than the keyframe at {}ms, {}", last_sample->to_milliseconds(), skip_seek ? ""sv : "not "sv, timestamp.to_milliseconds(), seeked_iterator.last_timestamp()->to_milliseconds(), skip_seek ? "skipping seek"sv : "seeking"sv);
  73. if (skip_seek) {
  74. return OptionalNone();
  75. }
  76. }
  77. track_status.iterator = move(seeked_iterator);
  78. return track_status.iterator.last_timestamp();
  79. }
  80. DecoderErrorOr<NonnullOwnPtr<Sample>> MatroskaDemuxer::get_next_sample_for_track(Track track)
  81. {
  82. // FIXME: This makes a copy of the sample, which shouldn't be necessary.
  83. // Matroska should make a RefPtr<ByteBuffer>, probably.
  84. auto& status = *TRY(get_track_status(track));
  85. if (!status.block.has_value() || status.frame_index >= status.block->frame_count()) {
  86. status.block = TRY(status.iterator.next_block());
  87. status.frame_index = 0;
  88. }
  89. auto cicp = TRY(m_reader.track_for_track_number(track.identifier())).video_track()->color_format.to_cicp();
  90. return make<VideoSample>(status.block->frame(status.frame_index++), cicp, status.block->timestamp());
  91. }
  92. DecoderErrorOr<Time> MatroskaDemuxer::duration()
  93. {
  94. auto duration = TRY(m_reader.segment_information()).duration();
  95. return duration.value_or(Time::zero());
  96. }
  97. }