2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/box.h"
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/media/formats/mp4/box_buffer.h"
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-11-22 21:28:21 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2015-12-21 18:34:21 +00:00
|
|
|
Box::Box() : box_size_(0) {}
|
2013-11-22 21:28:21 +00:00
|
|
|
Box::~Box() {}
|
|
|
|
|
|
|
|
bool Box::Parse(BoxReader* reader) {
|
2015-12-15 00:07:51 +00:00
|
|
|
DCHECK(reader);
|
2013-11-22 21:28:21 +00:00
|
|
|
BoxBuffer buffer(reader);
|
2015-12-15 00:07:51 +00:00
|
|
|
return ReadWriteInternal(&buffer);
|
2013-11-22 21:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box::Write(BufferWriter* writer) {
|
2015-12-15 00:07:51 +00:00
|
|
|
DCHECK(writer);
|
2015-12-21 18:34:21 +00:00
|
|
|
// Compute and update box size.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t size = ComputeSize();
|
2015-12-21 18:34:21 +00:00
|
|
|
DCHECK_EQ(size, box_size_);
|
2013-11-22 21:28:21 +00:00
|
|
|
|
|
|
|
size_t buffer_size_before_write = writer->Size();
|
|
|
|
BoxBuffer buffer(writer);
|
2015-12-15 00:07:51 +00:00
|
|
|
CHECK(ReadWriteInternal(&buffer));
|
2016-04-21 23:28:21 +00:00
|
|
|
DCHECK_EQ(box_size_, writer->Size() - buffer_size_before_write)
|
|
|
|
<< FourCCToString(BoxType());
|
2013-11-22 21:28:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 00:07:51 +00:00
|
|
|
void Box::WriteHeader(BufferWriter* writer) {
|
|
|
|
DCHECK(writer);
|
2015-12-21 18:34:21 +00:00
|
|
|
// Compute and update box size.
|
2015-12-15 00:07:51 +00:00
|
|
|
uint32_t size = ComputeSize();
|
2015-12-21 18:34:21 +00:00
|
|
|
DCHECK_EQ(size, box_size_);
|
2015-12-15 00:07:51 +00:00
|
|
|
|
|
|
|
size_t buffer_size_before_write = writer->Size();
|
|
|
|
BoxBuffer buffer(writer);
|
|
|
|
CHECK(ReadWriteHeaderInternal(&buffer));
|
|
|
|
DCHECK_EQ(HeaderSize(), writer->Size() - buffer_size_before_write);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Box::ComputeSize() {
|
2015-12-21 18:34:21 +00:00
|
|
|
box_size_ = ComputeSizeInternal();
|
|
|
|
return box_size_;
|
2015-12-15 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Box::HeaderSize() const {
|
|
|
|
const uint32_t kFourCCSize = 4;
|
|
|
|
// We don't support 64-bit size.
|
|
|
|
return kFourCCSize + sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Box::ReadWriteHeaderInternal(BoxBuffer* buffer) {
|
2013-11-22 21:28:21 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
// Skip for read mode, which is handled already in BoxReader.
|
|
|
|
} else {
|
2015-12-21 18:34:21 +00:00
|
|
|
CHECK(buffer->ReadWriteUInt32(&box_size_));
|
2013-11-22 21:28:21 +00:00
|
|
|
FourCC fourcc = BoxType();
|
|
|
|
CHECK(buffer->ReadWriteFourCC(&fourcc));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FullBox::FullBox() : version(0), flags(0) {}
|
|
|
|
FullBox::~FullBox() {}
|
|
|
|
|
2015-12-15 00:07:51 +00:00
|
|
|
uint32_t FullBox::HeaderSize() const {
|
|
|
|
// Additional 1-byte version and 3-byte flags.
|
|
|
|
return Box::HeaderSize() + 1 + 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FullBox::ReadWriteHeaderInternal(BoxBuffer* buffer) {
|
|
|
|
RCHECK(Box::ReadWriteHeaderInternal(buffer));
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t vflags;
|
2013-11-22 21:28:21 +00:00
|
|
|
if (buffer->Reading()) {
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&vflags));
|
|
|
|
this->version = vflags >> 24;
|
|
|
|
this->flags = vflags & 0x00FFFFFF;
|
|
|
|
} else {
|
|
|
|
vflags = (this->version << 24) | this->flags;
|
|
|
|
RCHECK(buffer->ReadWriteUInt32(&vflags));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|