Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp4 / box_reader.h
blobcf72343f1e810d47145dd08076a6994aa336f418
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.
5 #ifndef MEDIA_FORMATS_MP4_BOX_READER_H_
6 #define MEDIA_FORMATS_MP4_BOX_READER_H_
8 #include <map>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "media/base/media_export.h"
14 #include "media/base/media_log.h"
15 #include "media/formats/mp4/fourccs.h"
16 #include "media/formats/mp4/rcheck.h"
18 namespace media {
19 namespace mp4 {
21 class BoxReader;
23 struct MEDIA_EXPORT Box {
24 virtual ~Box();
26 // Parse errors may be logged using the BoxReader's media log.
27 virtual bool Parse(BoxReader* reader) = 0;
29 virtual FourCC BoxType() const = 0;
32 class MEDIA_EXPORT BufferReader {
33 public:
34 BufferReader(const uint8* buf, const int size)
35 : buf_(buf), size_(size), pos_(0) {
36 CHECK(buf);
37 CHECK_GE(size, 0);
40 bool HasBytes(int count) { return (pos() + count <= size()); }
42 // Read a value from the stream, perfoming endian correction, and advance the
43 // stream pointer.
44 bool Read1(uint8* v) WARN_UNUSED_RESULT;
45 bool Read2(uint16* v) WARN_UNUSED_RESULT;
46 bool Read2s(int16* v) WARN_UNUSED_RESULT;
47 bool Read4(uint32* v) WARN_UNUSED_RESULT;
48 bool Read4s(int32* v) WARN_UNUSED_RESULT;
49 bool Read8(uint64* v) WARN_UNUSED_RESULT;
50 bool Read8s(int64* v) WARN_UNUSED_RESULT;
52 bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT;
54 bool ReadVec(std::vector<uint8>* t, int count) WARN_UNUSED_RESULT;
56 // These variants read a 4-byte integer of the corresponding signedness and
57 // store it in the 8-byte return type.
58 bool Read4Into8(uint64* v) WARN_UNUSED_RESULT;
59 bool Read4sInto8s(int64* v) WARN_UNUSED_RESULT;
61 // Advance the stream by this many bytes.
62 bool SkipBytes(int nbytes) WARN_UNUSED_RESULT;
64 const uint8* data() const { return buf_; }
65 int size() const { return size_; }
66 int pos() const { return pos_; }
68 protected:
69 const uint8* buf_;
70 int size_;
71 int pos_;
73 template<typename T> bool Read(T* t) WARN_UNUSED_RESULT;
76 class MEDIA_EXPORT BoxReader : public BufferReader {
77 public:
78 ~BoxReader();
80 // Create a BoxReader from a buffer. Note that this function may return NULL
81 // if an intact, complete box was not available in the buffer. If |*err| is
82 // set, there was a stream-level error when creating the box; otherwise, NULL
83 // values are only expected when insufficient data is available.
85 // |buf| is retained but not owned, and must outlive the BoxReader instance.
86 static BoxReader* ReadTopLevelBox(const uint8* buf,
87 const int buf_size,
88 const scoped_refptr<MediaLog>& media_log,
89 bool* err);
91 // Read the box header from the current buffer. This function returns true if
92 // there is enough data to read the header and the header is sane; that is, it
93 // does not check to ensure the entire box is in the buffer before returning
94 // true. The semantics of |*err| are the same as above.
96 // |buf| is not retained.
97 static bool StartTopLevelBox(const uint8* buf,
98 const int buf_size,
99 const scoped_refptr<MediaLog>& media_log,
100 FourCC* type,
101 int* box_size,
102 bool* err) WARN_UNUSED_RESULT;
104 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as
105 // errors are returned when sufficient data is not available. |buf| can start
106 // with any type of box -- it does not have to be IsValidTopLevelBox().
108 // |buf| is retained but not owned, and must outlive the BoxReader instance.
109 static BoxReader* ReadConcatentatedBoxes(const uint8* buf,
110 const int buf_size);
112 // Returns true if |type| is recognized to be a top-level box, false
113 // otherwise. This returns true for some boxes which we do not parse.
114 // Helpful in debugging misaligned appends.
115 static bool IsValidTopLevelBox(const FourCC& type,
116 const scoped_refptr<MediaLog>& media_log);
118 // Scan through all boxes within the current box, starting at the current
119 // buffer position. Must be called before any of the *Child functions work.
120 bool ScanChildren() WARN_UNUSED_RESULT;
122 // Return true if child with type |child.BoxType()| exists.
123 bool HasChild(Box* child) WARN_UNUSED_RESULT;
125 // Read exactly one child box from the set of children. The type of the child
126 // will be determined by the BoxType() method of |child|.
127 bool ReadChild(Box* child) WARN_UNUSED_RESULT;
129 // Read one child if available. Returns false on error, true on successful
130 // read or on child absent.
131 bool MaybeReadChild(Box* child) WARN_UNUSED_RESULT;
133 // Read at least one child. False means error or no such child present.
134 template<typename T> bool ReadChildren(
135 std::vector<T>* children) WARN_UNUSED_RESULT;
137 // Read any number of children. False means error.
138 template<typename T> bool MaybeReadChildren(
139 std::vector<T>* children) WARN_UNUSED_RESULT;
141 // Read all children, regardless of FourCC. This is used from exactly one box,
142 // corresponding to a rather significant inconsistency in the BMFF spec.
143 // Note that this method is mutually exclusive with ScanChildren() and
144 // ReadAllChildrenAndCheckFourCC().
145 template <typename T>
146 bool ReadAllChildren(std::vector<T>* children) WARN_UNUSED_RESULT;
148 // Read all children and verify that the FourCC matches what is expected.
149 // Returns true if all children are successfully parsed and have the correct
150 // box type for |T|. Note that this method is mutually exclusive with
151 // ScanChildren() and ReadAllChildren().
152 template <typename T>
153 bool ReadAllChildrenAndCheckFourCC(std::vector<T>* children)
154 WARN_UNUSED_RESULT;
156 // Populate the values of 'version()' and 'flags()' from a full box header.
157 // Many boxes, but not all, use these values. This call should happen after
158 // the box has been initialized, and does not re-read the main box header.
159 bool ReadFullBoxHeader() WARN_UNUSED_RESULT;
161 FourCC type() const { return type_; }
162 uint8 version() const { return version_; }
163 uint32 flags() const { return flags_; }
165 const scoped_refptr<MediaLog>& media_log() const { return media_log_; }
167 private:
168 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is
169 // complete stream (i.e. no additional data is expected to be appended).
170 BoxReader(const uint8* buf,
171 const int size,
172 const scoped_refptr<MediaLog>& media_log,
173 bool is_EOS);
175 // Must be called immediately after init. If the return is false, this
176 // indicates that the box header and its contents were not available in the
177 // stream or were nonsensical, and that the box must not be used further. In
178 // this case, if |*err| is false, the problem was simply a lack of data, and
179 // should only be an error condition if some higher-level component knows that
180 // no more data is coming (i.e. EOS or end of containing box). If |*err| is
181 // true, the error is unrecoverable and the stream should be aborted.
182 bool ReadHeader(bool* err);
184 // Read all children, optionally checking FourCC. Returns true if all
185 // children are successfully parsed and, if |check_box_type|, have the
186 // correct box type for |T|. Note that this method is mutually exclusive
187 // with ScanChildren().
188 template <typename T>
189 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type);
191 scoped_refptr<MediaLog> media_log_;
192 FourCC type_;
193 uint8 version_;
194 uint32 flags_;
196 typedef std::multimap<FourCC, BoxReader> ChildMap;
198 // The set of child box FourCCs and their corresponding buffer readers. Only
199 // valid if scanned_ is true.
200 ChildMap children_;
201 bool scanned_;
203 // True if the buffer provided to the reader is the complete stream.
204 const bool is_EOS_;
207 // Template definitions
208 template<typename T> bool BoxReader::ReadChildren(std::vector<T>* children) {
209 RCHECK(MaybeReadChildren(children) && !children->empty());
210 return true;
213 template<typename T>
214 bool BoxReader::MaybeReadChildren(std::vector<T>* children) {
215 DCHECK(scanned_);
216 DCHECK(children->empty());
218 children->resize(1);
219 FourCC child_type = (*children)[0].BoxType();
221 ChildMap::iterator start_itr = children_.lower_bound(child_type);
222 ChildMap::iterator end_itr = children_.upper_bound(child_type);
223 children->resize(std::distance(start_itr, end_itr));
224 typename std::vector<T>::iterator child_itr = children->begin();
225 for (ChildMap::iterator itr = start_itr; itr != end_itr; ++itr) {
226 RCHECK(child_itr->Parse(&itr->second));
227 ++child_itr;
229 children_.erase(start_itr, end_itr);
231 DVLOG(2) << "Found " << children->size() << " "
232 << FourCCToString(child_type) << " boxes.";
233 return true;
236 template <typename T>
237 bool BoxReader::ReadAllChildren(std::vector<T>* children) {
238 return ReadAllChildrenInternal(children, false);
241 template <typename T>
242 bool BoxReader::ReadAllChildrenAndCheckFourCC(std::vector<T>* children) {
243 return ReadAllChildrenInternal(children, true);
246 template <typename T>
247 bool BoxReader::ReadAllChildrenInternal(std::vector<T>* children,
248 bool check_box_type) {
249 DCHECK(!scanned_);
250 scanned_ = true;
252 bool err = false;
253 while (pos_ < size_) {
254 BoxReader child_reader(&buf_[pos_], size_ - pos_, media_log_, is_EOS_);
255 if (!child_reader.ReadHeader(&err)) break;
256 T child;
257 RCHECK(!check_box_type || child_reader.type() == child.BoxType());
258 RCHECK(child.Parse(&child_reader));
259 children->push_back(child);
260 pos_ += child_reader.size();
263 return !err;
266 } // namespace mp4
267 } // namespace media
269 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_