Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: media/mp4/box_reader.h

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

Powered by Google App Engine
This is Rietveld 408576698