blob: 71d93a0bcf7cee308676ec62cb04216ecc654f30 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// 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 "base/memory/scoped_ptr.h"
14#include "media/mp4/box_definitions.h"
15#include "media/mp4/rcheck.h"
16
17namespace media {
18namespace mp4 {
19
20Box::~Box() {}
21
22bool BufferReader::Read1(uint8* v) {
23 RCHECK(HasBytes(1));
24 *v = buf_[pos_++];
25 return true;
26}
27
28// Internal implementation of multi-byte reads
29template<typename T> bool BufferReader::Read(T* v) {
30 RCHECK(HasBytes(sizeof(T)));
31
32 T tmp = 0;
33 for (size_t i = 0; i < sizeof(T); i++) {
34 tmp <<= 8;
35 tmp += buf_[pos_++];
36 }
37 *v = tmp;
38 return true;
39}
40
41bool BufferReader::Read2(uint16* v) { return Read(v); }
42bool BufferReader::Read2s(int16* v) { return Read(v); }
43bool BufferReader::Read4(uint32* v) { return Read(v); }
44bool BufferReader::Read4s(int32* v) { return Read(v); }
45bool BufferReader::Read8(uint64* v) { return Read(v); }
46bool BufferReader::Read8s(int64* v) { return Read(v); }
47
48bool BufferReader::ReadFourCC(FourCC* v) {
49 return Read4(reinterpret_cast<uint32*>(v));
50}
51
52bool BufferReader::ReadVec(std::vector<uint8>* vec, int count) {
53 RCHECK(HasBytes(count));
54 vec->clear();
55 vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count);
56 pos_ += count;
57 return true;
58}
59
60bool BufferReader::SkipBytes(int bytes) {
61 RCHECK(HasBytes(bytes));
62 pos_ += bytes;
63 return true;
64}
65
66bool BufferReader::Read4Into8(uint64* v) {
67 uint32 tmp;
68 RCHECK(Read4(&tmp));
69 *v = tmp;
70 return true;
71}
72
73bool BufferReader::Read4sInto8s(int64* v) {
74 // Beware of the need for sign extension.
75 int32 tmp;
76 RCHECK(Read4s(&tmp));
77 *v = tmp;
78 return true;
79}
80
81
82BoxReader::BoxReader(const uint8* buf, const int size,
83 const LogCB& log_cb)
84 : BufferReader(buf, size),
85 log_cb_(log_cb),
86 type_(FOURCC_NULL),
87 version_(0),
88 flags_(0),
89 scanned_(false) {
90}
91
92BoxReader::~BoxReader() {
93 if (scanned_ && !children_.empty()) {
94 for (ChildMap::iterator itr = children_.begin();
95 itr != children_.end(); ++itr) {
96 DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
97 }
98 }
99}
100
101// static
102BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
103 const int buf_size,
104 const LogCB& log_cb,
105 bool* err) {
106 scoped_ptr<BoxReader> reader(new BoxReader(buf, buf_size, log_cb));
107 if (!reader->ReadHeader(err))
108 return NULL;
109
110 if (!IsValidTopLevelBox(reader->type(), log_cb)) {
111 *err = true;
112 return NULL;
113 }
114
115 if (reader->size() <= buf_size)
116 return reader.release();
117
118 return NULL;
119}
120
121// static
122bool BoxReader::StartTopLevelBox(const uint8* buf,
123 const int buf_size,
124 const LogCB& log_cb,
125 FourCC* type,
126 int* box_size,
127 bool* err) {
128 BoxReader reader(buf, buf_size, log_cb);
129 if (!reader.ReadHeader(err)) return false;
130 if (!IsValidTopLevelBox(reader.type(), log_cb)) {
131 *err = true;
132 return false;
133 }
134 *type = reader.type();
135 *box_size = reader.size();
136 return true;
137}
138
139// static
140bool BoxReader::IsValidTopLevelBox(const FourCC& type,
141 const LogCB& log_cb) {
142 switch (type) {
143 case FOURCC_FTYP:
144 case FOURCC_PDIN:
145 case FOURCC_MOOV:
146 case FOURCC_MOOF:
147 case FOURCC_MFRA:
148 case FOURCC_MDAT:
149 case FOURCC_FREE:
150 case FOURCC_SKIP:
151 case FOURCC_META:
152 case FOURCC_MECO:
153 case FOURCC_STYP:
154 case FOURCC_SIDX:
155 case FOURCC_SSIX:
156 case FOURCC_PRFT:
157 return true;
158 default:
159 // Hex is used to show nonprintable characters and aid in debugging
160 MEDIA_LOG(log_cb) << "Unrecognized top-level box type 0x"
161 << std::hex << type;
162 return false;
163 }
164}
165
166bool BoxReader::ScanChildren() {
167 DCHECK(!scanned_);
168 scanned_ = true;
169
170 bool err = false;
171 while (pos() < size()) {
172 BoxReader child(&buf_[pos_], size_ - pos_, log_cb_);
173 if (!child.ReadHeader(&err)) break;
174
175 children_.insert(std::pair<FourCC, BoxReader>(child.type(), child));
176 pos_ += child.size();
177 }
178
179 DCHECK(!err);
180 return !err && pos() == size();
181}
182
183bool BoxReader::ReadChild(Box* child) {
184 DCHECK(scanned_);
185 FourCC child_type = child->BoxType();
186
187 ChildMap::iterator itr = children_.find(child_type);
188 RCHECK(itr != children_.end());
189 DVLOG(2) << "Found a " << FourCCToString(child_type) << " box.";
190 RCHECK(child->Parse(&itr->second));
191 children_.erase(itr);
192 return true;
193}
194
195bool BoxReader::MaybeReadChild(Box* child) {
196 if (!children_.count(child->BoxType())) return true;
197 return ReadChild(child);
198}
199
200bool BoxReader::ReadFullBoxHeader() {
201 uint32 vflags;
202 RCHECK(Read4(&vflags));
203 version_ = vflags >> 24;
204 flags_ = vflags & 0xffffff;
205 return true;
206}
207
208bool BoxReader::ReadHeader(bool* err) {
209 uint64 size = 0;
210 *err = false;
211
212 if (!HasBytes(8)) return false;
213 CHECK(Read4Into8(&size) && ReadFourCC(&type_));
214
215 if (size == 0) {
216 // Media Source specific: we do not support boxes that run to EOS.
217 *err = true;
218 return false;
219 } else if (size == 1) {
220 if (!HasBytes(8)) return false;
221 CHECK(Read8(&size));
222 }
223
224 // Implementation-specific: support for boxes larger than 2^31 has been
225 // removed.
226 if (size < static_cast<uint64>(pos_) ||
227 size > static_cast<uint64>(kint32max)) {
228 *err = true;
229 return false;
230 }
231
232 // Note that the pos_ head has advanced to the byte immediately after the
233 // header, which is where we want it.
234 size_ = size;
235 return true;
236}
237
238} // namespace mp4
239} // namespace media