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.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/box_reader.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-06-20 21:45:09 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/media/formats/mp4/box.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
BoxReader::BoxReader(const uint8_t* buf, size_t size)
|
2013-11-22 21:24:25 +00:00
|
|
|
: BufferReader(buf, size), type_(FOURCC_NULL), scanned_(false) {
|
|
|
|
DCHECK(buf);
|
2014-03-21 17:26:49 +00:00
|
|
|
DCHECK_LT(0u, 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
|
2014-09-30 21:52:21 +00:00
|
|
|
BoxReader* BoxReader::ReadTopLevelBox(const uint8_t* buf,
|
2014-03-21 17:26:49 +00:00
|
|
|
const size_t buf_size,
|
2013-09-24 01:35:40 +00:00
|
|
|
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;
|
|
|
|
|
2014-03-14 18:42:11 +00:00
|
|
|
// We don't require the complete box to be available for MDAT box.
|
2016-04-06 23:21:45 +00:00
|
|
|
if (reader->type() == FOURCC_mdat)
|
2014-03-14 18:42:11 +00:00
|
|
|
return reader.release();
|
|
|
|
|
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
|
2014-09-30 21:52:21 +00:00
|
|
|
bool BoxReader::StartTopLevelBox(const uint8_t* buf,
|
2014-03-21 17:26:49 +00:00
|
|
|
const size_t buf_size,
|
2013-09-24 01:35:40 +00:00
|
|
|
FourCC* type,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t* box_size,
|
2013-09-24 01:35:40 +00:00
|
|
|
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) {
|
2016-04-06 23:21:45 +00:00
|
|
|
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:
|
2016-05-26 18:55:44 +00:00
|
|
|
case FOURCC_uuid:
|
2013-09-24 01:35:40 +00:00
|
|
|
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) {
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t size = 0;
|
2013-09-24 01:35:40 +00:00
|
|
|
*err = false;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
if (!ReadNBytesInto8(&size, sizeof(uint32_t)) || !ReadFourCC(&type_))
|
2013-11-22 21:24:25 +00:00
|
|
|
return false;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
if (size == 0) {
|
2014-05-05 21:49:45 +00:00
|
|
|
// Boxes that run to EOS are not supported.
|
|
|
|
NOTIMPLEMENTED() << base::StringPrintf("Box '%s' run to EOS.",
|
|
|
|
FourCCToString(type_).c_str());
|
2013-09-24 01:35:40 +00:00
|
|
|
*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
|
|
|
}
|
|
|
|
|
2014-05-05 21:49:45 +00:00
|
|
|
// The box should have at least the size of what have been parsed.
|
|
|
|
if (size < pos()) {
|
2014-06-20 21:45:09 +00:00
|
|
|
LOG(ERROR) << base::StringPrintf("Box '%s' with size (%" PRIu64
|
|
|
|
") is invalid.",
|
2014-05-05 21:49:45 +00:00
|
|
|
FourCCToString(type_).c_str(),
|
|
|
|
size);
|
|
|
|
*err = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'mdat' box could have a 64-bit size; other boxes should be very small.
|
2014-09-30 23:52:58 +00:00
|
|
|
if (size > static_cast<uint64_t>(std::numeric_limits<int32_t>::max()) &&
|
2016-04-06 23:21:45 +00:00
|
|
|
type_ != FOURCC_mdat) {
|
2014-06-20 21:45:09 +00:00
|
|
|
LOG(ERROR) << base::StringPrintf("Box '%s' size (%" PRIu64
|
|
|
|
") is too large.",
|
2014-05-05 21:49:45 +00:00
|
|
|
FourCCToString(type_).c_str(),
|
|
|
|
size);
|
2013-09-24 01:35:40 +00:00
|
|
|
*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
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|