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>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2014-09-30 23:52:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.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
|
2016-05-27 21:38:14 +00:00
|
|
|
BoxReader* BoxReader::ReadBox(const uint8_t* buf,
|
|
|
|
const size_t buf_size,
|
|
|
|
bool* err) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_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 01:35:40 +00:00
|
|
|
if (reader->size() <= buf_size)
|
|
|
|
return reader.release();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-05-27 21:38:14 +00:00
|
|
|
bool BoxReader::StartBox(const uint8_t* buf,
|
|
|
|
const size_t buf_size,
|
|
|
|
FourCC* type,
|
|
|
|
uint64_t* 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 01:35:40 +00:00
|
|
|
*type = reader.type();
|
|
|
|
*box_size = reader.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BoxReader::ScanChildren() {
|
|
|
|
DCHECK(!scanned_);
|
|
|
|
scanned_ = true;
|
|
|
|
|
|
|
|
while (pos() < size()) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> child(
|
2013-11-22 21:24:25 +00:00
|
|
|
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
|