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

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

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

Powered by Google App Engine
This is Rietveld 408576698