2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/formats/mp4/box.h>
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
|
|
|
|
#include <packager/media/formats/mp4/box_buffer.h>
|
2013-11-22 21:28:21 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
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() {
|
2016-11-09 02:11:13 +00:00
|
|
|
box_size_ = static_cast<uint32_t>(ComputeSizeInternal());
|
2015-12-21 18:34:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-04 22:07:00 +00:00
|
|
|
FullBox::FullBox() = default;
|
|
|
|
FullBox::~FullBox() = default;
|
2013-11-22 21:28:21 +00:00
|
|
|
|
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
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|