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

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

Powered by Google App Engine
This is Rietveld 408576698