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

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

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 #include "media/mp4/box_reader.h"
6
7 #include <string.h>
8 #include <algorithm>
9 #include <map>
10 #include <set>
11
12 #include "base/logging.h"
13 #include "media/mp4/box_definitions.h"
14 #include "media/mp4/rcheck.h"
15
16 namespace media {
17 namespace mp4 {
18
19 Box::~Box() {}
20
21 bool BufferReader::Read1(uint8* v) {
22 RCHECK(HasBytes(1));
23 *v = buf_[pos_++];
24 return true;
25 }
26
27 // Internal implementation of multi-byte reads
28 template<typename T> bool BufferReader::Read(T* v) {
29 RCHECK(HasBytes(sizeof(T)));
30
31 T tmp = 0;
32 for (size_t i = 0; i < sizeof(T); i++) {
33 tmp <<= 8;
34 tmp += buf_[pos_++];
35 }
36 *v = tmp;
37 return true;
38 }
39
40 bool BufferReader::Read2(uint16* v) { return Read(v); }
41 bool BufferReader::Read2s(int16* v) { return Read(v); }
42 bool BufferReader::Read4(uint32* v) { return Read(v); }
43 bool BufferReader::Read4s(int32* v) { return Read(v); }
44 bool BufferReader::Read8(uint64* v) { return Read(v); }
45 bool BufferReader::Read8s(int64* v) { return Read(v); }
46
47 bool BufferReader::ReadFourCC(FourCC* v) {
48 return Read4(reinterpret_cast<uint32*>(v));
49 }
50
51 bool BufferReader::ReadVec(std::vector<uint8>* vec, int count) {
52 RCHECK(HasBytes(count));
53 vec->clear();
54 vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count);
55 pos_ += count;
56 return true;
57 }
58
59 bool BufferReader::SkipBytes(int bytes) {
60 RCHECK(HasBytes(bytes));
61 pos_ += bytes;
62 return true;
63 }
64
65 bool BufferReader::Read4Into8(uint64* v) {
66 uint32 tmp;
67 RCHECK(Read4(&tmp));
68 *v = tmp;
69 return true;
70 }
71
72 bool BufferReader::Read4sInto8s(int64* v) {
73 // Beware of the need for sign extension.
74 int32 tmp;
75 RCHECK(Read4s(&tmp));
76 *v = tmp;
77 return true;
78 }
79
80
81 BoxReader::BoxReader(const uint8* buf, const int size)
82 : BufferReader(buf, size), scanned_(false) {
83 }
84
85 BoxReader::~BoxReader() {
86 if (scanned_ && !children_.empty()) {
87 for (ChildMap::iterator itr = children_.begin();
88 itr != children_.end(); ++itr) {
89 DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
90 }
91 }
92 }
93
94 BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
95 const int buf_size,
96 bool* err) {
97 BoxReader* reader = new BoxReader(buf, buf_size);
98 if (reader->ReadHeader(err) && reader->size() <= buf_size) {
99 return reader;
100 }
101 delete reader;
102 return NULL;
103 }
104
105 // static
106 bool BoxReader::StartTopLevelBox(const uint8* buf,
107 const int buf_size,
108 FourCC* type,
109 int* box_size,
110 bool* err) {
111 BoxReader reader(buf, buf_size);
112 if (!reader.ReadHeader(err)) return false;
113 *type = reader.type();
114 *box_size = reader.size();
115 return true;
116 }
117
118 bool BoxReader::ScanChildren() {
119 DCHECK(!scanned_);
120 scanned_ = true;
121
122 bool err;
123 // TODO(strobe): Check or correct for multimap not inserting elements in
124 // consistent order.
125 while (pos() < size()) {
126 BoxReader child(&buf_[pos_], size_ - pos_);
127 if (!child.ReadHeader(&err)) break;
128
129 children_.insert(std::pair<FourCC, BoxReader>(child.type(), child));
130 pos_ += child.size();
131 }
132
133 DCHECK(!err);
134 return !err && pos() == size();
135 }
136
137 bool BoxReader::ReadChild(Box* child) {
138 DCHECK(scanned_);
139 FourCC child_type = child->BoxType();
140
141 ChildMap::iterator itr = children_.find(child_type);
142 RCHECK(itr != children_.end());
143 DVLOG(2) << "Found a " << FourCCToString(child_type) << " box.";
144 RCHECK(child->Parse(&itr->second));
145 children_.erase(itr);
146 return true;
147 }
148
149 bool BoxReader::MaybeReadChild(Box* child) {
150 if (!children_.count(child->BoxType())) return true;
151 return ReadChild(child);
152 }
153
154 bool BoxReader::ReadFullBoxHeader() {
155 uint32 vflags;
156 RCHECK(Read4(&vflags));
157 version_ = vflags >> 24;
158 flags_ = vflags & 0xffffff;
159 return true;
160 }
161
162 bool BoxReader::ReadHeader(bool* err) {
163 uint64 size = 0;
164 *err = false;
165
166 if (!HasBytes(8)) return false;
167 CHECK(Read4Into8(&size) && ReadFourCC(&type_));
168
169 if (size == 0) {
170 // Media Source specific: we do not support boxes that run to EOS.
171 *err = true;
172 return false;
173 } else if (size == 1) {
174 if (!HasBytes(8)) return false;
175 CHECK(Read8(&size));
176 }
177
178 // Implementation-specific: support for boxes larger than 2^31 has been
179 // removed.
180 if (size < static_cast<uint64>(pos_) ||
181 size > static_cast<uint64>(kint32max)) {
182 *err = true;
183 return false;
184 }
185
186 // Note that the pos_ head has advanced to the byte immediately after the
187 // header, which is where we want it.
188 size_ = size;
189 return true;
190 }
191
192 } // namespace mp4
193 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698