Chromium Code Reviews| Index: media/mp4/box_reader.h |
| diff --git a/media/mp4/box_reader.h b/media/mp4/box_reader.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..deb0ad6289ba84ae3062cfebf56b7d57ec771fc6 |
| --- /dev/null |
| +++ b/media/mp4/box_reader.h |
| @@ -0,0 +1,201 @@ |
| +// 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. |
| + |
| +#ifndef MEDIA_MP4_BOX_READER_H_ |
| +#define MEDIA_MP4_BOX_READER_H_ |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/logging.h" |
| +#include "media/mp4/box_definitions.h" |
| +#include "media/mp4/fourccs.h" |
| +#include "media/mp4/rcheck.h" |
| +#include <map> |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
System headers go at the top
strobe_
2012/06/07 16:33:54
Done.
|
| +#include <vector> |
| + |
| +namespace media { |
| +namespace mp4 { |
| + |
| +class BufferReader { |
| + public: |
| + BufferReader(const uint8* buf, const uint64 size) |
| + : buf_(buf), size_(size), pos_(0) {} |
| + |
| + bool HasBytes(uint32 count) { return (pos() + count <= size()); } |
| + |
| + // Read a value from the stream, perfoming endian correction, and advance the |
| + // stream pointer. |
| + bool Read(int8* v) WARN_UNUSED_RESULT; |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
Add size to signatures like Read4 does below. It m
strobe_
2012/06/07 22:04:22
Done.
|
| + bool Read(int16* v) WARN_UNUSED_RESULT; |
| + bool Read(int32* v) WARN_UNUSED_RESULT; |
| + bool Read(int64* v) WARN_UNUSED_RESULT; |
| + bool Read(uint8* v) WARN_UNUSED_RESULT; |
| + bool Read(uint16* v) WARN_UNUSED_RESULT; |
| + bool Read(uint32* v) WARN_UNUSED_RESULT; |
| + bool Read(uint64* v) WARN_UNUSED_RESULT; |
| + |
| + bool Read(std::vector<uint8>* t, size_t count) WARN_UNUSED_RESULT; |
| + |
| + // These variants read a 4-byte integer of the corresponding signedness and |
| + // store it in the 8-byte return type. |
| + bool Read4(int64* v) WARN_UNUSED_RESULT; |
| + bool Read4(uint64* v) WARN_UNUSED_RESULT; |
| + |
| + // Advance the stream by this many bytes. |
| + bool Skip(size_t nbytes) WARN_UNUSED_RESULT; |
| + |
| + uint64 size() { return size_; } |
| + uint64 pos() { return pos_; } |
| + |
| + protected: |
| + const uint8* buf_; |
| + uint64 size_; |
| + uint64 pos_; |
| + |
| + template<typename T> bool Read_(T* t) WARN_UNUSED_RESULT; |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
Remove _.
strobe_
2012/06/07 22:04:22
Done.
|
| +}; |
| + |
| +class BoxReader : public BufferReader { |
| + public: |
| + ~BoxReader(); |
| + |
| + // Create a BoxReader from a buffer. Note that this function may return NULL |
| + // if an intact, complete box was not available in the buffer. If '*err' is |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
'*err' -> |*err|
You should also add information
strobe_
2012/06/07 16:33:54
Done.
|
| + // set, there was a stream-level error when creating the box; otherwise, NULL |
| + // values are only expected when insufficient data is available. |
| + static BoxReader* ReadTopLevelBox(const uint8* buf, |
| + const size_t buf_size, |
| + bool* err); |
| + |
| + // Read the box header from the current buffer. This function returns true if |
| + // there is enough data to read the header and the header is sane; that is, it |
| + // does not check to ensure the entire box is in the buffer before returning |
| + // true. The semantics of '*err' are the same as above. |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
'*err' -> |*err|
strobe_
2012/06/07 16:33:54
Done.
|
| + static bool StartTopLevelBox(const uint8* buf, |
| + const size_t buf_size, |
| + FourCC* type, |
| + size_t* box_size, |
| + bool* err) WARN_UNUSED_RESULT; |
| + |
| + // Scan through all boxes within the current box, starting at the current |
| + // buffer position. Must be called before any of the *Child functions work. |
| + bool ScanChildren() WARN_UNUSED_RESULT; |
| + |
| + // Read exactly one child matching the given type from the list of children. |
| + // The argument should be a Box with an associated Parse method and FourCC. |
| + template<typename T> bool ReadChild(T* child) WARN_UNUSED_RESULT; |
| + |
| + // Read one child if available. Returns false on error, true on successful |
| + // read or on child absent. |
| + template<typename T> bool MaybeReadChild(T* child) WARN_UNUSED_RESULT; |
| + |
| + // Read at least one child. False means error or no such child present. |
| + template<typename T> bool ReadChildren(std::vector<T>* children) |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
nit: I believe the line break should be after the
|
| + WARN_UNUSED_RESULT; |
| + |
| + // Read any number of children. False means error. |
| + template<typename T> bool MaybeReadChildren(std::vector<T>* children) |
| + WARN_UNUSED_RESULT; |
| + |
| + // Read all children, regardless of FourCC. This is used from exactly one box, |
| + // corresponding to a rather significant inconsistency in the BMFF spec. |
| + template<typename T> bool ReadAllChildren(std::vector<T>* children) |
| + WARN_UNUSED_RESULT; |
| + |
| + // Populate the values of 'version()' and 'flags()' from a full box header. |
| + // Many boxes, but not all, use these values. This call should happen after |
| + // the box has been initialized, and does not re-read the main box header. |
| + bool ReadFullBoxHeader() WARN_UNUSED_RESULT; |
| + |
| + FourCC type() const { return type_; } |
| + uint8 version() const { return version_; } |
| + uint32 flags() const { return flags_; } |
| + private: |
| + BoxReader(const uint8* buf, const uint64 size); |
| + |
| + // 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, and that the box must not be used further. If (result && !err), |
| + // 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). |
| + bool ReadHeader(bool* err); |
| + |
| + FourCC type_; |
| + uint8 version_; |
| + uint32 flags_; |
| + |
| + // The set of child box FourCCs and their corresponding buffer readers. Only |
| + // valid if scanned_ is true. |
| + std::multimap<FourCC, BoxReader> children_; |
| + bool scanned_; |
| +}; |
| + |
| +// Template definitions |
| +// TODO(strobe): Reassess my life choices vis-a-vis templates |
| +template<typename T> bool BoxReader::ReadChild(T* child) { |
| + DCHECK(scanned_); |
| + // TODO(strobe): decide how to handle inappropriately repeated boxes. |
| + // If we want to be as permissive as possible, we can ignore all |
| + // boxes that need repeating. On the other hand, we may simply wish to be |
| + // strict about the files that we accept. |
| + FourCC child_type = GetBoxType<T>(); |
| + |
| + auto itr = children_.find(child_type); |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
You might want to verify that this construct build
|
| + RCHECK(itr != children_.end()); |
| + DVLOG(2) << "Found a " << FourCCToString(child_type) << " box."; |
| + RCHECK(Parse(&itr->second, child)); |
|
acolwell GONE FROM CHROMIUM
2012/06/06 17:32:39
Ideally I'd like to see this become
RCHECK(child->
strobe_
2012/06/07 22:04:22
Done.
|
| + children_.erase(itr); |
| + return true; |
| +} |
| + |
| +template<typename T> bool BoxReader::MaybeReadChild(T* child) { |
| + FourCC child_type = GetBoxType<T>(); |
| + if (!children_.count(child_type)) return true; |
| + return ReadChild(child); |
| +} |
| + |
| +template<typename T> bool BoxReader::ReadChildren(std::vector<T>* children) { |
| + RCHECK(MaybeReadChildren(children) && !children->empty()); |
| + return true; |
| +} |
| + |
| +template<typename T> |
| +bool BoxReader::MaybeReadChildren(std::vector<T>* children) { |
| + DCHECK(scanned_); |
| + DCHECK(children->empty()); |
| + FourCC child_type = GetBoxType<T>(); |
| + auto start_itr = children_.lower_bound(child_type); |
| + auto end_itr = children_.upper_bound(child_type); |
| + children->resize(std::distance(start_itr, end_itr)); |
| + auto child_itr = children->begin(); |
| + for (auto itr = start_itr; itr != end_itr; ++itr) { |
| + RCHECK(Parse(&itr->second, &(*child_itr))); |
| + ++child_itr; |
| + } |
| + children_.erase(start_itr, end_itr); |
| + DVLOG(2) << "Found " << children->size() << " " |
| + << FourCCToString(child_type) << " boxes."; |
| + return true; |
| +} |
| + |
| +template<typename T> |
| +bool BoxReader::ReadAllChildren(std::vector<T>* children) { |
| + DCHECK(scanned_); |
| + DCHECK(children->empty()); |
| + RCHECK(!children_.empty()); |
| + |
| + children->resize(children_.size()); |
| + auto child_itr = children->begin(); |
| + for (auto itr = children_.begin(); itr != children_.end(); ++itr) { |
| + RCHECK(Parse(&itr->second, &(*child_itr))); |
| + ++child_itr; |
| + } |
| + children_.clear(); |
| + return true; |
| +} |
| + |
| +} // namespace mp4 |
| +} // namespace media |
| + |
| +#endif // MEDIA_MP4_BOX_READER_H_ |