5 #include "packager/media/formats/mp4/box_reader.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/memory/scoped_ptr.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/media/formats/mp4/box.h"
20 BoxReader::BoxReader(
const uint8_t* buf,
size_t size)
21 : BufferReader(buf, size), type_(FOURCC_NULL), scanned_(false) {
26 BoxReader::~BoxReader() {
27 if (scanned_ && !children_.empty()) {
28 for (ChildMap::iterator itr = children_.begin(); itr != children_.end();
30 DVLOG(1) <<
"Skipping unknown box: " << FourCCToString(itr->first);
38 const size_t buf_size,
40 scoped_ptr<BoxReader> reader(
new BoxReader(buf, buf_size));
41 if (!reader->ReadHeader(err))
45 if (reader->type() == FOURCC_mdat)
46 return reader.release();
53 if (reader->size() <= buf_size)
54 return reader.release();
61 const size_t buf_size,
66 if (!reader.ReadHeader(err))
72 *type = reader.type();
73 *box_size = reader.size();
99 LOG(ERROR) <<
"Unrecognized top-level box type 0x" << std::hex << type;
108 while (pos() < size()) {
109 scoped_ptr<BoxReader> child(
110 new BoxReader(&data()[pos()], size() - pos()));
112 if (!child->ReadHeader(&err))
115 FourCC box_type = child->type();
116 size_t box_size = child->size();
117 children_.insert(std::pair<FourCC, BoxReader*>(box_type, child.release()));
126 FourCC child_type = child->
BoxType();
128 ChildMap::iterator itr = children_.find(child_type);
129 RCHECK(itr != children_.end());
130 DVLOG(2) <<
"Found a " << FourCCToString(child_type) <<
" box.";
131 RCHECK(child->
Parse(itr->second));
133 children_.erase(itr);
138 return children_.count(child->
BoxType()) > 0;
142 if (!children_.count(child->
BoxType()))
147 bool BoxReader::ReadHeader(
bool* err) {
156 NOTIMPLEMENTED() << base::StringPrintf(
"Box '%s' run to EOS.",
157 FourCCToString(type_).c_str());
160 }
else if (size == 1) {
167 LOG(ERROR) << base::StringPrintf(
"Box '%s' with size (%" PRIu64
169 FourCCToString(type_).c_str(),
176 if (size > static_cast<uint64_t>(std::numeric_limits<int32_t>::max()) &&
177 type_ != FOURCC_mdat) {
178 LOG(ERROR) << base::StringPrintf(
"Box '%s' size (%" PRIu64
180 FourCCToString(type_).c_str(),