mp4_stream_parser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
  5. #define MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "media/base/media_export.h"
  14. #include "media/base/stream_parser.h"
  15. #include "media/formats/common/offset_byte_queue.h"
  16. #include "media/formats/mp4/parse_result.h"
  17. #include "media/formats/mp4/track_run_iterator.h"
  18. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  19. #include "media/formats/mp4/aac.h"
  20. #endif
  21. namespace media {
  22. namespace mp4 {
  23. struct Movie;
  24. struct MovieHeader;
  25. struct TrackHeader;
  26. class BoxReader;
  27. class MEDIA_EXPORT MP4StreamParser : public StreamParser {
  28. public:
  29. MP4StreamParser(const std::set<int>& audio_object_types,
  30. bool has_sbr,
  31. bool has_flac);
  32. MP4StreamParser(const MP4StreamParser&) = delete;
  33. MP4StreamParser& operator=(const MP4StreamParser&) = delete;
  34. ~MP4StreamParser() override;
  35. void Init(InitCB init_cb,
  36. const NewConfigCB& config_cb,
  37. const NewBuffersCB& new_buffers_cb,
  38. bool ignore_text_tracks,
  39. const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
  40. const NewMediaSegmentCB& new_segment_cb,
  41. const EndMediaSegmentCB& end_of_segment_cb,
  42. MediaLog* media_log) override;
  43. void Flush() override;
  44. bool GetGenerateTimestampsFlag() const override;
  45. bool Parse(const uint8_t* buf, int size) override;
  46. // Calculates the rotation value from the track header display matricies.
  47. VideoTransformation CalculateRotation(const TrackHeader& track,
  48. const MovieHeader& movie);
  49. private:
  50. enum State {
  51. kWaitingForInit,
  52. kParsingBoxes,
  53. kWaitingForSampleData,
  54. kEmittingSamples,
  55. kError
  56. };
  57. ParseResult ParseBox();
  58. bool ParseMoov(mp4::BoxReader* reader);
  59. bool ParseMoof(mp4::BoxReader* reader);
  60. void OnEncryptedMediaInitData(
  61. const std::vector<ProtectionSystemSpecificHeader>& headers);
  62. // To retain proper framing, each 'mdat' atom must be read; to limit memory
  63. // usage, the atom's data needs to be discarded incrementally as frames are
  64. // extracted from the stream. This function discards data from the stream up
  65. // to |max_clear_offset|, updating the |mdat_tail_| value so that framing can
  66. // be retained after all 'mdat' information has been read. |max_clear_offset|
  67. // is the upper bound on what can be removed from |queue_|. Anything below
  68. // this offset is no longer needed by the parser.
  69. // Returns 'true' on success, 'false' if there was an error.
  70. bool ReadAndDiscardMDATsUntil(int64_t max_clear_offset);
  71. void ChangeState(State new_state);
  72. bool EmitConfigs();
  73. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  74. bool PrepareAACBuffer(const AAC& aac_config,
  75. std::vector<uint8_t>* frame_buf,
  76. std::vector<SubsampleEntry>* subsamples) const;
  77. #endif
  78. ParseResult EnqueueSample(BufferQueueMap* buffers);
  79. bool SendAndFlushSamples(BufferQueueMap* buffers);
  80. void Reset();
  81. // Checks to see if we have enough data in |queue_| to transition to
  82. // kEmittingSamples and start enqueuing samples.
  83. bool HaveEnoughDataToEnqueueSamples();
  84. // Sets |highest_end_offset_| based on the data in |moov_|
  85. // and |moof|. Returns true if |highest_end_offset_| was successfully
  86. // computed.
  87. bool ComputeHighestEndOffset(const MovieFragment& moof);
  88. State state_;
  89. InitCB init_cb_;
  90. NewConfigCB config_cb_;
  91. NewBuffersCB new_buffers_cb_;
  92. EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
  93. NewMediaSegmentCB new_segment_cb_;
  94. EndMediaSegmentCB end_of_segment_cb_;
  95. raw_ptr<MediaLog> media_log_;
  96. OffsetByteQueue queue_;
  97. // These two parameters are only valid in the |kEmittingSegments| state.
  98. //
  99. // |moof_head_| is the offset of the start of the most recently parsed moof
  100. // block. All byte offsets in sample information are relative to this offset,
  101. // as mandated by the Media Source spec.
  102. int64_t moof_head_;
  103. // |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
  104. // Valid iff it is greater than the head of the queue.
  105. int64_t mdat_tail_;
  106. // The highest end offset in the current moof. This offset is
  107. // relative to |moof_head_|. This value is used to make sure we have collected
  108. // enough bytes to parse all samples and aux_info in the current moof.
  109. int64_t highest_end_offset_;
  110. std::unique_ptr<mp4::Movie> moov_;
  111. std::unique_ptr<mp4::TrackRunIterator> runs_;
  112. bool has_audio_;
  113. bool has_video_;
  114. std::set<uint32_t> audio_track_ids_;
  115. std::set<uint32_t> video_track_ids_;
  116. // The object types allowed for audio tracks. For FLAC indication, use
  117. // |has_flac_|;
  118. const std::set<int> audio_object_types_;
  119. const bool has_sbr_;
  120. const bool has_flac_;
  121. // Tracks the number of MEDIA_LOGS for skipping empty trun samples.
  122. int num_empty_samples_skipped_;
  123. // Tracks the number of MEDIA_LOGS for invalid bitstream conversion.
  124. int num_invalid_conversions_;
  125. // Tracks the number of MEDIA_LOGS for video keyframe MP4<->frame mismatch.
  126. int num_video_keyframe_mismatches_;
  127. };
  128. } // namespace mp4
  129. } // namespace media
  130. #endif // MEDIA_FORMATS_MP4_MP4_STREAM_PARSER_H_