MatroskaDemuxer.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MatroskaDemuxer.h"
  7. namespace Video::Matroska {
  8. DecoderErrorOr<NonnullOwnPtr<MatroskaDemuxer>> MatroskaDemuxer::from_file(StringView filename)
  9. {
  10. return make<MatroskaDemuxer>(TRY(Reader::from_file(filename)));
  11. }
  12. DecoderErrorOr<NonnullOwnPtr<MatroskaDemuxer>> MatroskaDemuxer::from_data(ReadonlyBytes data)
  13. {
  14. return make<MatroskaDemuxer>(TRY(Reader::from_data(data)));
  15. }
  16. DecoderErrorOr<Vector<Track>> MatroskaDemuxer::get_tracks_for_type(TrackType type)
  17. {
  18. TrackEntry::TrackType matroska_track_type;
  19. switch (type) {
  20. case TrackType::Video:
  21. matroska_track_type = TrackEntry::TrackType::Video;
  22. break;
  23. case TrackType::Audio:
  24. matroska_track_type = TrackEntry::TrackType::Audio;
  25. break;
  26. case TrackType::Subtitles:
  27. matroska_track_type = TrackEntry::TrackType::Subtitle;
  28. break;
  29. }
  30. Vector<Track> tracks;
  31. TRY(m_reader.for_each_track_of_type(matroska_track_type, [&](TrackEntry const& track_entry) -> DecoderErrorOr<IterationDecision> {
  32. VERIFY(track_entry.track_type() == matroska_track_type);
  33. DECODER_TRY_ALLOC(tracks.try_append(Track(type, track_entry.track_number())));
  34. return IterationDecision::Continue;
  35. }));
  36. return tracks;
  37. }
  38. DecoderErrorOr<MatroskaDemuxer::TrackStatus*> MatroskaDemuxer::get_track_status(Track track)
  39. {
  40. if (!m_track_statuses.contains(track)) {
  41. auto iterator = TRY(m_reader.create_sample_iterator(track.identifier()));
  42. DECODER_TRY_ALLOC(m_track_statuses.try_set(track, { iterator }));
  43. }
  44. return &m_track_statuses.get(track).release_value();
  45. }
  46. DecoderErrorOr<Time> MatroskaDemuxer::seek_to_most_recent_keyframe(Track track, Time timestamp)
  47. {
  48. // Removing the track status will cause us to start from the beginning.
  49. if (timestamp.is_zero()) {
  50. m_track_statuses.remove(track);
  51. return timestamp;
  52. }
  53. auto& track_status = *TRY(get_track_status(track));
  54. TRY(m_reader.seek_to_random_access_point(track_status.iterator, timestamp));
  55. return track_status.iterator.last_timestamp();
  56. }
  57. DecoderErrorOr<NonnullOwnPtr<Sample>> MatroskaDemuxer::get_next_sample_for_track(Track track)
  58. {
  59. // FIXME: This makes a copy of the sample, which shouldn't be necessary.
  60. // Matroska should make a RefPtr<ByteBuffer>, probably.
  61. auto& status = *TRY(get_track_status(track));
  62. if (!status.block.has_value() || status.frame_index >= status.block->frame_count()) {
  63. status.block = TRY(status.iterator.next_block());
  64. status.frame_index = 0;
  65. }
  66. auto cicp = TRY(m_reader.track_for_track_number(track.identifier())).video_track()->color_format.to_cicp();
  67. return make<VideoSample>(status.block->frame(status.frame_index++), cicp, status.block->timestamp());
  68. }
  69. DecoderErrorOr<Time> MatroskaDemuxer::duration()
  70. {
  71. auto duration = TRY(m_reader.segment_information()).duration();
  72. return duration.value_or(Time::zero());
  73. }
  74. }