2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "media/mp4/box_reader.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
2013-11-22 21:24:25 +00:00
|
|
|
#include "media/mp4/box.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
BoxReader::BoxReader(const uint8* buf, size_t size)
|
|
|
|
: BufferReader(buf, size), type_(FOURCC_NULL), scanned_(false) {
|
|
|
|
DCHECK(buf);
|
|
|
|
DCHECK_LT(0, size);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BoxReader::~BoxReader() {
|
|
|
|
if (scanned_ && !children_.empty()) {
|
2013-11-22 21:24:25 +00:00
|
|
|
for (ChildMap::iterator itr = children_.begin(); itr != children_.end();
|
|
|
|
++itr) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
|
2013-11-22 21:24:25 +00:00
|
|
|
delete itr->second;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
|
|
|
|
const int buf_size,
|
|
|
|
bool* err) {
|
2013-09-24 04:17:12 +00:00
|
|
|
scoped_ptr<BoxReader> reader(new BoxReader(buf, buf_size));
|
2013-09-24 01:35:40 +00:00
|
|
|
if (!reader->ReadHeader(err))
|
|
|
|
return NULL;
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
if (!IsValidTopLevelBox(reader->type())) {
|
2013-09-24 01:35:40 +00:00
|
|
|
*err = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader->size() <= buf_size)
|
|
|
|
return reader.release();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool BoxReader::StartTopLevelBox(const uint8* buf,
|
|
|
|
const int buf_size,
|
|
|
|
FourCC* type,
|
|
|
|
int* box_size,
|
|
|
|
bool* err) {
|
2013-09-24 04:17:12 +00:00
|
|
|
BoxReader reader(buf, buf_size);
|
2013-11-22 21:24:25 +00:00
|
|
|
if (!reader.ReadHeader(err))
|
|
|
|
return false;
|
2013-09-24 04:17:12 +00:00
|
|
|
if (!IsValidTopLevelBox(reader.type())) {
|
2013-09-24 01:35:40 +00:00
|
|
|
*err = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*type = reader.type();
|
|
|
|
*box_size = reader.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-09-24 04:17:12 +00:00
|
|
|
bool BoxReader::IsValidTopLevelBox(const FourCC& type) {
|
2013-09-24 01:35:40 +00:00
|
|
|
switch (type) {
|
|
|
|
case FOURCC_FTYP:
|
|
|
|
case FOURCC_PDIN:
|
|
|
|
case FOURCC_BLOC:
|
|
|
|
case FOURCC_MOOV:
|
|
|
|
case FOURCC_MOOF:
|
|
|
|
case FOURCC_MFRA:
|
|
|
|
case FOURCC_MDAT:
|
|
|
|
case FOURCC_FREE:
|
|
|
|
case FOURCC_SKIP:
|
|
|
|
case FOURCC_META:
|
|
|
|
case FOURCC_MECO:
|
|
|
|
case FOURCC_STYP:
|
|
|
|
case FOURCC_SIDX:
|
|
|
|
case FOURCC_SSIX:
|
|
|
|
case FOURCC_PRFT:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
// Hex is used to show nonprintable characters and aid in debugging
|
2013-09-24 04:17:12 +00:00
|
|
|
LOG(ERROR) << "Unrecognized top-level box type 0x" << std::hex << type;
|
2013-09-24 01:35:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoxReader::ScanChildren() {
|
|
|
|
DCHECK(!scanned_);
|
|
|
|
scanned_ = true;
|
|
|
|
|
|
|
|
while (pos() < size()) {
|
2013-11-22 21:24:25 +00:00
|
|
|
scoped_ptr<BoxReader> child(
|
|
|
|
new BoxReader(&data()[pos()], size() - pos()));
|
|
|
|
bool err;
|
|
|
|
if (!child->ReadHeader(&err))
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
FourCC box_type = child->type();
|
|
|
|
size_t box_size = child->size();
|
|
|
|
children_.insert(std::pair<FourCC, BoxReader*>(box_type, child.release()));
|
|
|
|
RCHECK(SkipBytes(box_size));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BoxReader::ReadChild(Box* child) {
|
|
|
|
DCHECK(scanned_);
|
|
|
|
FourCC child_type = child->BoxType();
|
|
|
|
|
|
|
|
ChildMap::iterator itr = children_.find(child_type);
|
|
|
|
RCHECK(itr != children_.end());
|
|
|
|
DVLOG(2) << "Found a " << FourCCToString(child_type) << " box.";
|
2013-11-22 21:24:25 +00:00
|
|
|
RCHECK(child->Parse(itr->second));
|
|
|
|
delete itr->second;
|
2013-09-24 01:35:40 +00:00
|
|
|
children_.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:37:58 +00:00
|
|
|
bool BoxReader::ChildExist(Box* child) {
|
|
|
|
return children_.count(child->BoxType()) > 0;
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
bool BoxReader::TryReadChild(Box* child) {
|
|
|
|
if (!children_.count(child->BoxType()))
|
|
|
|
return true;
|
2013-09-24 01:35:40 +00:00
|
|
|
return ReadChild(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoxReader::ReadHeader(bool* err) {
|
|
|
|
uint64 size = 0;
|
|
|
|
*err = false;
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
if (!ReadNBytesInto8(&size, sizeof(uint32)) || !ReadFourCC(&type_))
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
// Media Source specific: we do not support boxes that run to EOS.
|
|
|
|
*err = true;
|
|
|
|
return false;
|
|
|
|
} else if (size == 1) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (!Read8(&size))
|
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation-specific: support for boxes larger than 2^31 has been
|
|
|
|
// removed.
|
2013-11-22 21:24:25 +00:00
|
|
|
if (size < static_cast<uint64>(pos()) ||
|
2013-09-24 01:35:40 +00:00
|
|
|
size > static_cast<uint64>(kint32max)) {
|
|
|
|
*err = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that the pos_ head has advanced to the byte immediately after the
|
|
|
|
// header, which is where we want it.
|
2013-11-22 21:24:25 +00:00
|
|
|
set_size(size);
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|