2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cstdint>
|
2014-08-25 22:51:19 +00:00
|
|
|
#include <map>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2013-09-24 01:35:40 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2024-05-11 00:42:34 +00:00
|
|
|
#include <absl/flags/declare.h>
|
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/macros/classes.h>
|
|
|
|
#include <packager/media/base/decryptor_source.h>
|
|
|
|
#include <packager/media/base/media_parser.h>
|
|
|
|
#include <packager/media/base/offset_byte_queue.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2024-05-11 00:42:34 +00:00
|
|
|
ABSL_DECLARE_FLAG(bool, use_dovi_supplemental_codecs);
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
class BoxReader;
|
2013-09-24 04:17:12 +00:00
|
|
|
class TrackRunIterator;
|
2014-03-21 17:26:49 +00:00
|
|
|
struct Movie;
|
|
|
|
struct ProtectionSystemSpecificHeader;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
class MP4MediaParser : public MediaParser {
|
2013-09-24 01:35:40 +00:00
|
|
|
public:
|
2013-09-24 04:17:12 +00:00
|
|
|
MP4MediaParser();
|
2015-07-22 23:40:45 +00:00
|
|
|
~MP4MediaParser() override;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @name MediaParser implementation overrides.
|
|
|
|
/// @{
|
2015-07-22 23:40:45 +00:00
|
|
|
void Init(const InitCB& init_cb,
|
2020-07-01 22:43:44 +00:00
|
|
|
const NewMediaSampleCB& new_media_sample_cb,
|
|
|
|
const NewTextSampleCB& new_text_sample_cb,
|
2015-07-22 23:40:45 +00:00
|
|
|
KeySource* decryption_key_source) override;
|
2023-12-01 17:32:19 +00:00
|
|
|
[[nodiscard]] bool Flush() override;
|
|
|
|
[[nodiscard]] bool Parse(const uint8_t* buf, int size) override;
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-05-21 00:38:09 +00:00
|
|
|
/// Handles ISO-BMFF containers which have the 'moov' box trailing the
|
|
|
|
/// movie data ('mdat'). It does this by doing a sparse parse of the file
|
|
|
|
/// to locate the 'moov' box, and parsing its contents if it is found to be
|
|
|
|
/// located after the 'mdat' box(es).
|
|
|
|
/// @param file_path is the path to the media file to be parsed.
|
|
|
|
/// @return true if successful, false otherwise.
|
|
|
|
bool LoadMoov(const std::string& file_path);
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
private:
|
|
|
|
enum State {
|
|
|
|
kWaitingForInit,
|
|
|
|
kParsingBoxes,
|
|
|
|
kEmittingSamples,
|
|
|
|
kError
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ParseBox(bool* err);
|
|
|
|
bool ParseMoov(mp4::BoxReader* reader);
|
|
|
|
bool ParseMoof(mp4::BoxReader* reader);
|
|
|
|
|
2014-08-25 22:51:19 +00:00
|
|
|
bool FetchKeysIfNecessary(
|
2013-09-24 01:35:40 +00:00
|
|
|
const std::vector<ProtectionSystemSpecificHeader>& headers);
|
|
|
|
|
2015-12-21 18:34:21 +00:00
|
|
|
// To retain proper framing, each 'mdat' box must be read; to limit memory
|
|
|
|
// usage, the box's data needs to be discarded incrementally as frames are
|
2013-09-24 01:35:40 +00:00
|
|
|
// extracted from the stream. This function discards data from the stream up
|
|
|
|
// to |offset|, updating the |mdat_tail_| value so that framing can be
|
|
|
|
// retained after all 'mdat' information has been read.
|
|
|
|
// Returns 'true' on success, 'false' if there was an error.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadAndDiscardMDATsUntil(const int64_t offset);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
void ChangeState(State new_state);
|
|
|
|
|
|
|
|
bool EmitConfigs();
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
bool EnqueueSample(bool* err);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
State state_;
|
|
|
|
InitCB init_cb_;
|
2020-07-01 22:43:44 +00:00
|
|
|
NewMediaSampleCB new_sample_cb_;
|
2014-08-25 22:51:19 +00:00
|
|
|
KeySource* decryption_key_source_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<DecryptorSource> decryptor_source_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
OffsetByteQueue queue_;
|
|
|
|
|
|
|
|
// These two parameters are only valid in the |kEmittingSegments| state.
|
|
|
|
//
|
|
|
|
// |moof_head_| is the offset of the start of the most recently parsed moof
|
|
|
|
// block. All byte offsets in sample information are relative to this offset,
|
|
|
|
// as mandated by the Media Source spec.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t moof_head_;
|
2013-09-24 01:35:40 +00:00
|
|
|
// |mdat_tail_| is the stream offset of the end of the current 'mdat' box.
|
|
|
|
// Valid iff it is greater than the head of the queue.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t mdat_tail_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Movie> moov_;
|
|
|
|
std::unique_ptr<TrackRunIterator> runs_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(MP4MediaParser);
|
2013-09-24 01:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_MP4_MP4_MEDIA_PARSER_H_
|