Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp4 / box_reader.cc
blobd4683dd9d6c78130ed62e0a0e1f6a117731f80bb
1 // Copyright 2014 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.
5 #include "media/formats/mp4/box_reader.h"
7 #include <string.h>
8 #include <algorithm>
9 #include <set>
11 #include "base/memory/scoped_ptr.h"
12 #include "media/formats/mp4/box_definitions.h"
14 namespace media {
15 namespace mp4 {
17 Box::~Box() {}
19 bool BufferReader::Read1(uint8* v) {
20 RCHECK(HasBytes(1));
21 *v = buf_[pos_++];
22 return true;
25 // Internal implementation of multi-byte reads
26 template<typename T> bool BufferReader::Read(T* v) {
27 RCHECK(HasBytes(sizeof(T)));
29 T tmp = 0;
30 for (size_t i = 0; i < sizeof(T); i++) {
31 tmp <<= 8;
32 tmp += buf_[pos_++];
34 *v = tmp;
35 return true;
38 bool BufferReader::Read2(uint16* v) { return Read(v); }
39 bool BufferReader::Read2s(int16* v) { return Read(v); }
40 bool BufferReader::Read4(uint32* v) { return Read(v); }
41 bool BufferReader::Read4s(int32* v) { return Read(v); }
42 bool BufferReader::Read8(uint64* v) { return Read(v); }
43 bool BufferReader::Read8s(int64* v) { return Read(v); }
45 bool BufferReader::ReadFourCC(FourCC* v) {
46 return Read4(reinterpret_cast<uint32*>(v));
49 bool BufferReader::ReadVec(std::vector<uint8>* vec, int count) {
50 RCHECK(HasBytes(count));
51 vec->clear();
52 vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count);
53 pos_ += count;
54 return true;
57 bool BufferReader::SkipBytes(int bytes) {
58 RCHECK(HasBytes(bytes));
59 pos_ += bytes;
60 return true;
63 bool BufferReader::Read4Into8(uint64* v) {
64 uint32 tmp;
65 RCHECK(Read4(&tmp));
66 *v = tmp;
67 return true;
70 bool BufferReader::Read4sInto8s(int64* v) {
71 // Beware of the need for sign extension.
72 int32 tmp;
73 RCHECK(Read4s(&tmp));
74 *v = tmp;
75 return true;
78 BoxReader::BoxReader(const uint8* buf,
79 const int size,
80 const scoped_refptr<MediaLog>& media_log,
81 bool is_EOS)
82 : BufferReader(buf, size),
83 media_log_(media_log),
84 type_(FOURCC_NULL),
85 version_(0),
86 flags_(0),
87 scanned_(false),
88 is_EOS_(is_EOS) {
91 BoxReader::~BoxReader() {
92 if (scanned_ && !children_.empty()) {
93 for (ChildMap::iterator itr = children_.begin();
94 itr != children_.end(); ++itr) {
95 DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
100 // static
101 BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
102 const int buf_size,
103 const scoped_refptr<MediaLog>& media_log,
104 bool* err) {
105 scoped_ptr<BoxReader> reader(new BoxReader(buf, buf_size, media_log, false));
106 if (!reader->ReadHeader(err))
107 return NULL;
109 if (!IsValidTopLevelBox(reader->type(), media_log)) {
110 *err = true;
111 return NULL;
114 if (reader->size() <= buf_size)
115 return reader.release();
117 return NULL;
120 // static
121 bool BoxReader::StartTopLevelBox(const uint8* buf,
122 const int buf_size,
123 const scoped_refptr<MediaLog>& media_log,
124 FourCC* type,
125 int* box_size,
126 bool* err) {
127 BoxReader reader(buf, buf_size, media_log, false);
128 if (!reader.ReadHeader(err)) return false;
129 if (!IsValidTopLevelBox(reader.type(), media_log)) {
130 *err = true;
131 return false;
133 *type = reader.type();
134 *box_size = reader.size();
135 return true;
138 // static
139 BoxReader* BoxReader::ReadConcatentatedBoxes(const uint8* buf,
140 const int buf_size) {
141 return new BoxReader(buf, buf_size, new MediaLog(), true);
144 // static
145 bool BoxReader::IsValidTopLevelBox(const FourCC& type,
146 const scoped_refptr<MediaLog>& media_log) {
147 switch (type) {
148 case FOURCC_FTYP:
149 case FOURCC_PDIN:
150 case FOURCC_BLOC:
151 case FOURCC_MOOV:
152 case FOURCC_MOOF:
153 case FOURCC_MFRA:
154 case FOURCC_MDAT:
155 case FOURCC_FREE:
156 case FOURCC_SKIP:
157 case FOURCC_META:
158 case FOURCC_MECO:
159 case FOURCC_STYP:
160 case FOURCC_SIDX:
161 case FOURCC_SSIX:
162 case FOURCC_PRFT:
163 case FOURCC_UUID:
164 case FOURCC_EMSG:
165 return true;
166 default:
167 // Hex is used to show nonprintable characters and aid in debugging
168 MEDIA_LOG(DEBUG, media_log) << "Unrecognized top-level box type "
169 << FourCCToString(type);
170 return false;
174 bool BoxReader::ScanChildren() {
175 DCHECK(!scanned_);
176 scanned_ = true;
178 bool err = false;
179 while (pos() < size()) {
180 BoxReader child(&buf_[pos_], size_ - pos_, media_log_, is_EOS_);
181 if (!child.ReadHeader(&err)) break;
183 children_.insert(std::pair<FourCC, BoxReader>(child.type(), child));
184 pos_ += child.size();
187 DCHECK(!err);
188 return !err && pos() == size();
191 bool BoxReader::HasChild(Box* child) {
192 DCHECK(scanned_);
193 DCHECK(child);
194 return children_.count(child->BoxType()) > 0;
197 bool BoxReader::ReadChild(Box* child) {
198 DCHECK(scanned_);
199 FourCC child_type = child->BoxType();
201 ChildMap::iterator itr = children_.find(child_type);
202 RCHECK(itr != children_.end());
203 DVLOG(2) << "Found a " << FourCCToString(child_type) << " box.";
204 RCHECK(child->Parse(&itr->second));
205 children_.erase(itr);
206 return true;
209 bool BoxReader::MaybeReadChild(Box* child) {
210 if (!children_.count(child->BoxType())) return true;
211 return ReadChild(child);
214 bool BoxReader::ReadFullBoxHeader() {
215 uint32 vflags;
216 RCHECK(Read4(&vflags));
217 version_ = vflags >> 24;
218 flags_ = vflags & 0xffffff;
219 return true;
222 bool BoxReader::ReadHeader(bool* err) {
223 uint64 size = 0;
224 *err = false;
226 if (!HasBytes(8)) {
227 // If EOS is known, then this is an error. If not, additional data may be
228 // appended later, so this is a soft error.
229 *err = is_EOS_;
230 return false;
232 CHECK(Read4Into8(&size) && ReadFourCC(&type_));
234 if (size == 0) {
235 if (is_EOS_) {
236 // All the data bytes are expected to be provided.
237 size = size_;
238 } else {
239 MEDIA_LOG(DEBUG, media_log_)
240 << "ISO BMFF boxes that run to EOS are not supported";
241 *err = true;
242 return false;
244 } else if (size == 1) {
245 if (!HasBytes(8)) {
246 // If EOS is known, then this is an error. If not, it's a soft error.
247 *err = is_EOS_;
248 return false;
250 CHECK(Read8(&size));
253 // Implementation-specific: support for boxes larger than 2^31 has been
254 // removed.
255 if (size < static_cast<uint64>(pos_) ||
256 size > static_cast<uint64>(kint32max)) {
257 *err = true;
258 return false;
261 // Make sure the buffer contains at least the expected number of bytes.
262 // Since the data may be appended in pieces, this can only be checked if EOS.
263 if (is_EOS_ && size > static_cast<uint64>(size_)) {
264 *err = true;
265 return false;
268 // Note that the pos_ head has advanced to the byte immediately after the
269 // header, which is where we want it.
270 size_ = size;
271 return true;
274 } // namespace mp4
275 } // namespace media