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
5 #ifndef MEDIA_FORMATS_MP4_BOX_READER_H_
6 #define MEDIA_FORMATS_MP4_BOX_READER_H_
7
8 #include <stdint.h>
9
10 #include <limits>
11 #include <map>
12 #include <memory>
13 #include <vector>
14
15 #include "base/compiler_specific.h"
16 #include "base/logging.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "media/base/media_export.h"
19 #include "media/base/media_log.h"
20 #include "media/formats/mp4/fourccs.h"
21 #include "media/formats/mp4/parse_result.h"
22 #include "media/formats/mp4/rcheck.h"
23
24 namespace media {
25 namespace mp4 {
26
27 enum DisplayMatrixSize {
28 kDisplayMatrixWidth = 3,
29 kDisplayMatrixHeight = 3,
30 kDisplayMatrixDimension = kDisplayMatrixHeight * kDisplayMatrixWidth
31 };
32
33 using DisplayMatrix = int32_t[kDisplayMatrixDimension];
34
35 class BoxReader;
36
37 struct MEDIA_EXPORT Box {
38 virtual ~Box();
39
40 // Parse errors may be logged using the BoxReader's media log.
41 virtual bool Parse(BoxReader* reader) = 0;
42
43 virtual FourCC BoxType() const = 0;
44 };
45
46 class MEDIA_EXPORT BufferReader {
47 public:
BufferReader(const uint8_t * buf,const size_t buf_size)48 BufferReader(const uint8_t* buf, const size_t buf_size)
49 : buf_(buf), buf_size_(buf_size), pos_(0) {
50 CHECK(buf);
51 }
52
HasBytes(size_t count)53 bool HasBytes(size_t count) {
54 // As the size of a box is implementation limited to 2^31, fail if
55 // attempting to check for too many bytes.
56 const size_t impl_limit =
57 static_cast<size_t>(std::numeric_limits<int32_t>::max());
58 return pos_ <= buf_size_ && count <= impl_limit &&
59 count <= buf_size_ - pos_;
60 }
61
62 // Read a value from the stream, performing endian correction, and advance the
63 // stream pointer.
64 bool Read1(uint8_t* v) WARN_UNUSED_RESULT;
65 bool Read2(uint16_t* v) WARN_UNUSED_RESULT;
66 bool Read2s(int16_t* v) WARN_UNUSED_RESULT;
67 bool Read4(uint32_t* v) WARN_UNUSED_RESULT;
68 bool Read4s(int32_t* v) WARN_UNUSED_RESULT;
69 bool Read8(uint64_t* v) WARN_UNUSED_RESULT;
70 bool Read8s(int64_t* v) WARN_UNUSED_RESULT;
71
72 bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT;
73
74 bool ReadVec(std::vector<uint8_t>* t, uint64_t count) WARN_UNUSED_RESULT;
75
76 // These variants read a 4-byte integer of the corresponding signedness and
77 // store it in the 8-byte return type.
78 bool Read4Into8(uint64_t* v) WARN_UNUSED_RESULT;
79 bool Read4sInto8s(int64_t* v) WARN_UNUSED_RESULT;
80
81 // Advance the stream by this many bytes.
82 bool SkipBytes(uint64_t nbytes) WARN_UNUSED_RESULT;
83
buffer()84 const uint8_t* buffer() const { return buf_; }
85
86 // Returns the size of the buffer. This may not match the size specified
87 // in the mp4 box header and could be less than the box size when the full box
88 // has not been appended. Always consult buffer_size() to avoid OOB reads.
89 // See BoxReader::box_size().
buffer_size()90 size_t buffer_size() const { return buf_size_; }
pos()91 size_t pos() const { return pos_; }
92
93 protected:
94 const uint8_t* buf_;
95 size_t buf_size_;
96 size_t pos_;
97
98 template<typename T> bool Read(T* t) WARN_UNUSED_RESULT;
99 };
100
101 class MEDIA_EXPORT BoxReader : public BufferReader {
102 public:
103 BoxReader(const BoxReader& other);
104 ~BoxReader();
105
106 // Create a BoxReader from a buffer. If the result is kOk, then |out_reader|
107 // will be set, otherwise |out_reader| will be unchanged.
108 //
109 // |buf| is retained but not owned, and must outlive the BoxReader instance.
110 static ParseResult ReadTopLevelBox(const uint8_t* buf,
111 const size_t buf_size,
112 MediaLog* media_log,
113 std::unique_ptr<BoxReader>* out_reader)
114 WARN_UNUSED_RESULT;
115
116 // Read the box header from the current buffer, and return its type and size.
117 // This function returns kNeedMoreData if the box is incomplete, even if the
118 // box header is complete.
119 //
120 // |buf| is not retained.
121 static ParseResult StartTopLevelBox(const uint8_t* buf,
122 const size_t buf_size,
123 MediaLog* media_log,
124 FourCC* out_type,
125 size_t* out_box_size) WARN_UNUSED_RESULT;
126
127 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as
128 // errors are returned when sufficient data is not available. |buf| can start
129 // with any type of box -- it does not have to be IsValidTopLevelBox().
130 //
131 // |buf| is retained but not owned, and must outlive the BoxReader instance.
132 static BoxReader* ReadConcatentatedBoxes(const uint8_t* buf,
133 const size_t buf_size,
134 MediaLog* media_log);
135
136 // Returns true if |type| is recognized to be a top-level box, false
137 // otherwise. This returns true for some boxes which we do not parse.
138 // Helpful in debugging misaligned appends.
139 static bool IsValidTopLevelBox(const FourCC& type, MediaLog* media_log);
140
141 // Scan through all boxes within the current box, starting at the current
142 // buffer position. Must be called before any of the *Child functions work.
143 bool ScanChildren() WARN_UNUSED_RESULT;
144
145 // Return true if child with type |child.BoxType()| exists.
146 bool HasChild(Box* child) WARN_UNUSED_RESULT;
147
148 // Read exactly one child box from the set of children. The type of the child
149 // will be determined by the BoxType() method of |child|.
150 bool ReadChild(Box* child) WARN_UNUSED_RESULT;
151
152 // Read one child if available. Returns false on error, true on successful
153 // read or on child absent.
154 bool MaybeReadChild(Box* child) WARN_UNUSED_RESULT;
155
156 // ISO-BMFF streams files use a 3x3 matrix consisting of 6 16.16 fixed point
157 // decimals and 3 2.30 fixed point decimals.
158 bool ReadDisplayMatrix(DisplayMatrix matrix);
159
160 // Read at least one child. False means error or no such child present.
161 template<typename T> bool ReadChildren(
162 std::vector<T>* children) WARN_UNUSED_RESULT;
163
164 // Read any number of children. False means error.
165 template<typename T> bool MaybeReadChildren(
166 std::vector<T>* children) WARN_UNUSED_RESULT;
167
168 // Read all children, regardless of FourCC. This is used from exactly one box,
169 // corresponding to a rather significant inconsistency in the BMFF spec.
170 // Note that this method is mutually exclusive with ScanChildren() and
171 // ReadAllChildrenAndCheckFourCC().
172 template <typename T>
173 bool ReadAllChildren(std::vector<T>* children) WARN_UNUSED_RESULT;
174
175 // Read all children and verify that the FourCC matches what is expected.
176 // Returns true if all children are successfully parsed and have the correct
177 // box type for |T|. Note that this method is mutually exclusive with
178 // ScanChildren() and ReadAllChildren().
179 template <typename T>
180 bool ReadAllChildrenAndCheckFourCC(std::vector<T>* children)
181 WARN_UNUSED_RESULT;
182
183 // Populate the values of 'version()' and 'flags()' from a full box header.
184 // Many boxes, but not all, use these values. This call should happen after
185 // the box has been initialized, and does not re-read the main box header.
186 bool ReadFullBoxHeader() WARN_UNUSED_RESULT;
187
box_size()188 size_t box_size() const {
189 DCHECK(box_size_known_);
190 return box_size_;
191 }
192
type()193 FourCC type() const { return type_; }
version()194 uint8_t version() const { return version_; }
flags()195 uint32_t flags() const { return flags_; }
196
media_log()197 MediaLog* media_log() const { return media_log_; }
198
199 private:
200 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is
201 // complete stream (i.e. no additional data is expected to be appended).
202 BoxReader(const uint8_t* buf,
203 const size_t buf_size,
204 MediaLog* media_log,
205 bool is_EOS);
206
207 // Must be called immediately after init.
208 ParseResult ReadHeader() WARN_UNUSED_RESULT;
209
210 // Read all children, optionally checking FourCC. Returns true if all
211 // children are successfully parsed and, if |check_box_type|, have the
212 // correct box type for |T|. Note that this method is mutually exclusive
213 // with ScanChildren().
214 template <typename T>
215 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type);
216
217 MediaLog* media_log_;
218 size_t box_size_;
219 bool box_size_known_;
220 FourCC type_;
221 uint8_t version_;
222 uint32_t flags_;
223
224 typedef std::multimap<FourCC, BoxReader> ChildMap;
225
226 // The set of child box FourCCs and their corresponding buffer readers. Only
227 // valid if scanned_ is true.
228 ChildMap children_;
229 bool scanned_;
230
231 // True if the buffer provided to the reader is the complete stream.
232 const bool is_EOS_;
233 };
234
235 // Template definitions
ReadChildren(std::vector<T> * children)236 template<typename T> bool BoxReader::ReadChildren(std::vector<T>* children) {
237 RCHECK(MaybeReadChildren(children) && !children->empty());
238 return true;
239 }
240
241 template<typename T>
MaybeReadChildren(std::vector<T> * children)242 bool BoxReader::MaybeReadChildren(std::vector<T>* children) {
243 DCHECK(scanned_);
244 DCHECK(children->empty());
245
246 children->resize(1);
247 FourCC child_type = (*children)[0].BoxType();
248
249 ChildMap::iterator start_itr = children_.lower_bound(child_type);
250 ChildMap::iterator end_itr = children_.upper_bound(child_type);
251 children->resize(std::distance(start_itr, end_itr));
252 typename std::vector<T>::iterator child_itr = children->begin();
253 for (ChildMap::iterator itr = start_itr; itr != end_itr; ++itr) {
254 RCHECK(child_itr->Parse(&itr->second));
255 ++child_itr;
256 }
257 children_.erase(start_itr, end_itr);
258
259 DVLOG(2) << "Found " << children->size() << " "
260 << FourCCToString(child_type) << " boxes.";
261 return true;
262 }
263
264 template <typename T>
ReadAllChildren(std::vector<T> * children)265 bool BoxReader::ReadAllChildren(std::vector<T>* children) {
266 return ReadAllChildrenInternal(children, false);
267 }
268
269 template <typename T>
ReadAllChildrenAndCheckFourCC(std::vector<T> * children)270 bool BoxReader::ReadAllChildrenAndCheckFourCC(std::vector<T>* children) {
271 return ReadAllChildrenInternal(children, true);
272 }
273
274 template <typename T>
ReadAllChildrenInternal(std::vector<T> * children,bool check_box_type)275 bool BoxReader::ReadAllChildrenInternal(std::vector<T>* children,
276 bool check_box_type) {
277 DCHECK(!scanned_);
278 scanned_ = true;
279
280 // Must know our box size before attempting to parse child boxes.
281 RCHECK(box_size_known_);
282
283 DCHECK_LE(pos_, box_size_);
284 while (pos_ < box_size_) {
285 BoxReader child_reader(&buf_[pos_], box_size_ - pos_, media_log_, is_EOS_);
286
287 if (child_reader.ReadHeader() != ParseResult::kOk)
288 return false;
289
290 T child;
291 RCHECK(!check_box_type || child_reader.type() == child.BoxType());
292 RCHECK(child.Parse(&child_reader));
293 children->push_back(child);
294 pos_ += child_reader.box_size();
295 }
296 DCHECK_EQ(pos_, box_size_);
297 return true;
298 }
299
300 } // namespace mp4
301 } // namespace media
302
303 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_
304