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

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