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 <map>
9#include <vector>
10
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"
17
18namespace media {
19namespace mp4 {
20
21class BoxReader;
22
23struct MEDIA_EXPORT Box {
24  virtual ~Box();
25  virtual bool Parse(BoxReader* reader) = 0;
26  virtual FourCC BoxType() const = 0;
27};
28
29class MEDIA_EXPORT BufferReader {
30 public:
31  BufferReader(const uint8* buf, const int size)
32      : buf_(buf), size_(size), pos_(0) {
33    CHECK(buf);
34    CHECK_GE(size, 0);
35  }
36
37  bool HasBytes(int count) { return (pos() + count <= size()); }
38
39  // Read a value from the stream, perfoming endian correction, and advance the
40  // stream pointer.
41  bool Read1(uint8* v)  WARN_UNUSED_RESULT;
42  bool Read2(uint16* v) WARN_UNUSED_RESULT;
43  bool Read2s(int16* v) WARN_UNUSED_RESULT;
44  bool Read4(uint32* v) WARN_UNUSED_RESULT;
45  bool Read4s(int32* v) WARN_UNUSED_RESULT;
46  bool Read8(uint64* v) WARN_UNUSED_RESULT;
47  bool Read8s(int64* v) WARN_UNUSED_RESULT;
48
49  bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT;
50
51  bool ReadVec(std::vector<uint8>* t, int count) WARN_UNUSED_RESULT;
52
53  // These variants read a 4-byte integer of the corresponding signedness and
54  // store it in the 8-byte return type.
55  bool Read4Into8(uint64* v) WARN_UNUSED_RESULT;
56  bool Read4sInto8s(int64* v) WARN_UNUSED_RESULT;
57
58  // Advance the stream by this many bytes.
59  bool SkipBytes(int nbytes) WARN_UNUSED_RESULT;
60
61  const uint8* data() const { return buf_; }
62  int size() const { return size_; }
63  int pos() const { return pos_; }
64
65 protected:
66  const uint8* buf_;
67  int size_;
68  int pos_;
69
70  template<typename T> bool Read(T* t) WARN_UNUSED_RESULT;
71};
72
73class MEDIA_EXPORT BoxReader : public BufferReader {
74 public:
75  ~BoxReader();
76
77  // Create a BoxReader from a buffer. Note that this function may return NULL
78  // if an intact, complete box was not available in the buffer. If |*err| is
79  // set, there was a stream-level error when creating the box; otherwise, NULL
80  // values are only expected when insufficient data is available.
81  //
82  // |buf| is retained but not owned, and must outlive the BoxReader instance.
83  static BoxReader* ReadTopLevelBox(const uint8* buf,
84                                    const int buf_size,
85                                    const LogCB& log_cb,
86                                    bool* err);
87
88  // Read the box header from the current buffer. This function returns true if
89  // there is enough data to read the header and the header is sane; that is, it
90  // does not check to ensure the entire box is in the buffer before returning
91  // true. The semantics of |*err| are the same as above.
92  //
93  // |buf| is not retained.
94  static bool StartTopLevelBox(const uint8* buf,
95                               const int buf_size,
96                               const LogCB& log_cb,
97                               FourCC* type,
98                               int* box_size,
99                               bool* err) WARN_UNUSED_RESULT;
100
101  // Returns true if |type| is recognized to be a top-level box, false
102  // otherwise. This returns true for some boxes which we do not parse.
103  // Helpful in debugging misaligned appends.
104  static bool IsValidTopLevelBox(const FourCC& type,
105                                 const LogCB& log_cb);
106
107  // Scan through all boxes within the current box, starting at the current
108  // buffer position. Must be called before any of the *Child functions work.
109  bool ScanChildren() WARN_UNUSED_RESULT;
110
111  // Return true if child with type |child.BoxType()| exists.
112  bool HasChild(Box* child) WARN_UNUSED_RESULT;
113
114  // Read exactly one child box from the set of children. The type of the child
115  // will be determined by the BoxType() method of |child|.
116  bool ReadChild(Box* child) WARN_UNUSED_RESULT;
117
118  // Read one child if available. Returns false on error, true on successful
119  // read or on child absent.
120  bool MaybeReadChild(Box* child) WARN_UNUSED_RESULT;
121
122  // Read at least one child. False means error or no such child present.
123  template<typename T> bool ReadChildren(
124      std::vector<T>* children) WARN_UNUSED_RESULT;
125
126  // Read any number of children. False means error.
127  template<typename T> bool MaybeReadChildren(
128      std::vector<T>* children) WARN_UNUSED_RESULT;
129
130  // Read all children, regardless of FourCC. This is used from exactly one box,
131  // corresponding to a rather significant inconsistency in the BMFF spec.
132  // Note that this method is mutually exclusive with ScanChildren().
133  template<typename T> bool ReadAllChildren(
134      std::vector<T>* children) WARN_UNUSED_RESULT;
135
136  // Populate the values of 'version()' and 'flags()' from a full box header.
137  // Many boxes, but not all, use these values. This call should happen after
138  // the box has been initialized, and does not re-read the main box header.
139  bool ReadFullBoxHeader() WARN_UNUSED_RESULT;
140
141  FourCC type() const   { return type_; }
142  uint8 version() const { return version_; }
143  uint32 flags() const  { return flags_; }
144
145  const LogCB& log_cb() const { return log_cb_; }
146
147 private:
148  BoxReader(const uint8* buf, const int size, const LogCB& log_cb);
149
150  // Must be called immediately after init. If the return is false, this
151  // indicates that the box header and its contents were not available in the
152  // stream or were nonsensical, and that the box must not be used further. In
153  // this case, if |*err| is false, the problem was simply a lack of data, and
154  // should only be an error condition if some higher-level component knows that
155  // no more data is coming (i.e. EOS or end of containing box). If |*err| is
156  // true, the error is unrecoverable and the stream should be aborted.
157  bool ReadHeader(bool* err);
158
159  LogCB log_cb_;
160  FourCC type_;
161  uint8 version_;
162  uint32 flags_;
163
164  typedef std::multimap<FourCC, BoxReader> ChildMap;
165
166  // The set of child box FourCCs and their corresponding buffer readers. Only
167  // valid if scanned_ is true.
168  ChildMap children_;
169  bool scanned_;
170};
171
172// Template definitions
173template<typename T> bool BoxReader::ReadChildren(std::vector<T>* children) {
174  RCHECK(MaybeReadChildren(children) && !children->empty());
175  return true;
176}
177
178template<typename T>
179bool BoxReader::MaybeReadChildren(std::vector<T>* children) {
180  DCHECK(scanned_);
181  DCHECK(children->empty());
182
183  children->resize(1);
184  FourCC child_type = (*children)[0].BoxType();
185
186  ChildMap::iterator start_itr = children_.lower_bound(child_type);
187  ChildMap::iterator end_itr = children_.upper_bound(child_type);
188  children->resize(std::distance(start_itr, end_itr));
189  typename std::vector<T>::iterator child_itr = children->begin();
190  for (ChildMap::iterator itr = start_itr; itr != end_itr; ++itr) {
191    RCHECK(child_itr->Parse(&itr->second));
192    ++child_itr;
193  }
194  children_.erase(start_itr, end_itr);
195
196  DVLOG(2) << "Found " << children->size() << " "
197           << FourCCToString(child_type) << " boxes.";
198  return true;
199}
200
201template<typename T>
202bool BoxReader::ReadAllChildren(std::vector<T>* children) {
203  DCHECK(!scanned_);
204  scanned_ = true;
205
206  bool err = false;
207  while (pos() < size()) {
208    BoxReader child_reader(&buf_[pos_], size_ - pos_, log_cb_);
209    if (!child_reader.ReadHeader(&err)) break;
210    T child;
211    RCHECK(child.Parse(&child_reader));
212    children->push_back(child);
213    pos_ += child_reader.size();
214  }
215
216  return !err;
217}
218
219}  // namespace mp4
220}  // namespace media
221
222#endif  // MEDIA_FORMATS_MP4_BOX_READER_H_
223