2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP4_BOX_READER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP4_BOX_READER_H_
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
#include <map>
|
2016-11-29 23:24:09 +00:00
|
|
|
#include <memory>
|
2013-09-24 01:35:40 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-08-31 23:59:46 +00:00
|
|
|
#include <glog/logging.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/macros.h>
|
|
|
|
#include <packager/media/base/buffer_reader.h>
|
|
|
|
#include <packager/media/base/fourccs.h>
|
|
|
|
#include <packager/media/base/rcheck.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-03-21 17:26:49 +00:00
|
|
|
struct Box;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Class for reading MP4 boxes.
|
2013-09-24 04:17:12 +00:00
|
|
|
class BoxReader : public BufferReader {
|
2013-09-24 01:35:40 +00:00
|
|
|
public:
|
|
|
|
~BoxReader();
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Create a BoxReader from a buffer.
|
|
|
|
/// @param buf is retained but not owned, and must outlive the BoxReader
|
|
|
|
/// instance.
|
|
|
|
/// @param buf_size indicates the size of the input buffer.
|
|
|
|
/// @param[out] err is set to true if there was a stream-level error when
|
|
|
|
/// reading the box.
|
2014-03-14 18:42:11 +00:00
|
|
|
/// @return New box reader if successful, NULL otherwise. Note that this
|
|
|
|
/// function may return NULL if an intact, complete box is not
|
|
|
|
/// available in the buffer. For MDAT box only, a BoxReader object is
|
|
|
|
/// returned as long as the box header is available.
|
2016-05-27 21:38:14 +00:00
|
|
|
static BoxReader* ReadBox(const uint8_t* buf,
|
|
|
|
const size_t buf_size,
|
|
|
|
bool* err);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read the box header from the current buffer.
|
|
|
|
/// @param buf is not retained.
|
|
|
|
/// @param buf_size indicates the size of the input buffer.
|
|
|
|
/// @param[out] type is filled with the fourcc of the box on success.
|
|
|
|
/// @param[out] box_size is filled with the size of the box on success.
|
|
|
|
/// @param[out] err is set to true if there was a stream-level error when
|
|
|
|
/// reading the box.
|
|
|
|
/// @return true if there is enough data to read the header and the header is
|
|
|
|
/// sane, which does not imply that the entire box is in the buffer.
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] static bool StartBox(const uint8_t* buf,
|
|
|
|
const size_t buf_size,
|
|
|
|
FourCC* type,
|
|
|
|
uint64_t* box_size,
|
|
|
|
bool* err);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Scan through all boxes within the current box, starting at the current
|
|
|
|
/// buffer position. Must be called before any of the @b *Child functions
|
|
|
|
/// work.
|
|
|
|
/// @return true on success, false otherwise.
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool ScanChildren();
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return true if child with type @a child.BoxType() exists.
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool ChildExist(Box* child);
|
2013-10-08 17:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read exactly one child box from the set of children. The type of the
|
|
|
|
/// child will be determined by the BoxType() of @a child.
|
|
|
|
/// @return true on success, false otherwise.
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool ReadChild(Box* child);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read one child if available.
|
|
|
|
/// @return false on error, true on successful read or on child absent.
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool TryReadChild(Box* child);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read at least one child.
|
|
|
|
/// @return false on error or no child of type <T> present.
|
2013-11-22 21:24:25 +00:00
|
|
|
template <typename T>
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool ReadChildren(std::vector<T>* children);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read any number of children.
|
|
|
|
/// @return false on error.
|
2013-11-22 21:24:25 +00:00
|
|
|
template <typename T>
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool TryReadChildren(std::vector<T>* children);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read all children. It expects all children to be of type T.
|
|
|
|
/// Note that this method is mutually exclusive with ScanChildren().
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-22 21:24:25 +00:00
|
|
|
template <typename T>
|
2023-08-31 23:59:46 +00:00
|
|
|
[[nodiscard]] bool ReadAllChildren(std::vector<T>* children);
|
2013-11-22 21:24:25 +00:00
|
|
|
|
|
|
|
bool ReadFourCC(FourCC* fourcc) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t val;
|
2013-11-22 21:24:25 +00:00
|
|
|
if (!Read4(&val))
|
|
|
|
return false;
|
|
|
|
*fourcc = static_cast<FourCC>(val);
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
FourCC type() const { return type_; }
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
BoxReader(const uint8_t* buf, size_t size);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Must be called immediately after init. If the return is false, this
|
|
|
|
// indicates that the box header and its contents were not available in the
|
|
|
|
// stream or were nonsensical, and that the box must not be used further. In
|
|
|
|
// this case, if |*err| is false, the problem was simply a lack of data, and
|
|
|
|
// should only be an error condition if some higher-level component knows that
|
|
|
|
// no more data is coming (i.e. EOS or end of containing box). If |*err| is
|
|
|
|
// true, the error is unrecoverable and the stream should be aborted.
|
|
|
|
bool ReadHeader(bool* err);
|
|
|
|
|
|
|
|
FourCC type_;
|
|
|
|
|
2016-11-29 23:24:09 +00:00
|
|
|
typedef std::multimap<FourCC, std::unique_ptr<BoxReader>> ChildMap;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// The set of child box FourCCs and their corresponding buffer readers. Only
|
|
|
|
// valid if scanned_ is true.
|
|
|
|
ChildMap children_;
|
|
|
|
bool scanned_;
|
2013-11-22 21:24:25 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BoxReader);
|
2013-09-24 01:35:40 +00:00
|
|
|
};
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
// Template definitions.
|
|
|
|
template <typename T>
|
|
|
|
bool BoxReader::ReadChildren(std::vector<T>* children) {
|
|
|
|
RCHECK(TryReadChildren(children) && !children->empty());
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
template <typename T>
|
|
|
|
bool BoxReader::TryReadChildren(std::vector<T>* children) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK(scanned_);
|
|
|
|
DCHECK(children->empty());
|
|
|
|
|
|
|
|
children->resize(1);
|
|
|
|
FourCC child_type = (*children)[0].BoxType();
|
|
|
|
|
|
|
|
ChildMap::iterator start_itr = children_.lower_bound(child_type);
|
|
|
|
ChildMap::iterator end_itr = children_.upper_bound(child_type);
|
|
|
|
children->resize(std::distance(start_itr, end_itr));
|
|
|
|
typename std::vector<T>::iterator child_itr = children->begin();
|
|
|
|
for (ChildMap::iterator itr = start_itr; itr != end_itr; ++itr) {
|
2016-11-29 23:24:09 +00:00
|
|
|
RCHECK(child_itr->Parse(itr->second.get()));
|
2013-09-24 01:35:40 +00:00
|
|
|
++child_itr;
|
|
|
|
}
|
|
|
|
children_.erase(start_itr, end_itr);
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
DVLOG(2) << "Found " << children->size() << " " << FourCCToString(child_type)
|
|
|
|
<< " boxes.";
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
template <typename T>
|
2013-09-24 01:35:40 +00:00
|
|
|
bool BoxReader::ReadAllChildren(std::vector<T>* children) {
|
|
|
|
DCHECK(!scanned_);
|
|
|
|
scanned_ = true;
|
|
|
|
|
|
|
|
while (pos() < size()) {
|
2013-11-22 21:24:25 +00:00
|
|
|
BoxReader child_reader(&data()[pos()], size() - pos());
|
|
|
|
bool err;
|
|
|
|
if (!child_reader.ReadHeader(&err))
|
|
|
|
return false;
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
T child;
|
|
|
|
RCHECK(child.Parse(&child_reader));
|
|
|
|
children->push_back(child);
|
2013-11-22 21:24:25 +00:00
|
|
|
RCHECK(SkipBytes(child_reader.size()));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
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_BOX_READER_H_
|