From 0f3bc4b6b42f4435143ed3f57a5e84274cb2d9b6 Mon Sep 17 00:00:00 2001 From: Kongqun Yang Date: Fri, 22 Nov 2013 13:28:21 -0800 Subject: [PATCH] Implement base mp4 box objects: Box and FullBox. Change-Id: Ic1acc0f7ec3999d5270609a8836dbbde7f0d1c0f --- media/mp4/box.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ media/mp4/box.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 media/mp4/box.cc create mode 100644 media/mp4/box.h diff --git a/media/mp4/box.cc b/media/mp4/box.cc new file mode 100644 index 0000000000..f8d27bf09b --- /dev/null +++ b/media/mp4/box.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2013 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. + +#include "media/mp4/box.h" + +#include "base/logging.h" +#include "media/mp4/box_buffer.h" + +namespace media { +namespace mp4 { + +Box::Box() : atom_size(0) {} +Box::~Box() {} + +bool Box::Parse(BoxReader* reader) { + DCHECK(reader != NULL); + BoxBuffer buffer(reader); + return ReadWrite(&buffer); +} + +void Box::Write(BufferWriter* writer) { + DCHECK(writer != NULL); + uint32 size = ComputeSize(); + DCHECK_EQ(size, this->atom_size); + + size_t buffer_size_before_write = writer->Size(); + BoxBuffer buffer(writer); + CHECK(ReadWrite(&buffer)); + DCHECK_EQ(this->atom_size, writer->Size() - buffer_size_before_write); +} + +bool Box::ReadWrite(BoxBuffer* buffer) { + if (buffer->Reading()) { + // Skip for read mode, which is handled already in BoxReader. + } else { + CHECK(buffer->ReadWriteUInt32(&this->atom_size)); + FourCC fourcc = BoxType(); + CHECK(buffer->ReadWriteFourCC(&fourcc)); + } + return true; +} + +FullBox::FullBox() : version(0), flags(0) {} +FullBox::~FullBox() {} + +bool FullBox::ReadWrite(BoxBuffer* buffer) { + RCHECK(Box::ReadWrite(buffer)); + + uint32 vflags; + 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 diff --git a/media/mp4/box.h b/media/mp4/box.h new file mode 100644 index 0000000000..f8466dc155 --- /dev/null +++ b/media/mp4/box.h @@ -0,0 +1,62 @@ +// Copyright (c) 2013 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. + +#ifndef MEDIA_MP4_BOX_H_ +#define MEDIA_MP4_BOX_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "media/mp4/fourccs.h" + +namespace media { + +class BufferWriter; + +namespace mp4 { + +class BoxBuffer; +class BoxReader; + +// Defines Box and FullBox, the two base ISO BMFF box objects as defined in +// ISO 14496-12:2012 ISO BMFF section 4.2. All ISO BMFF compatible boxes +// inherits either Box or FullBox. +struct Box { + public: + Box(); + virtual ~Box(); + bool Parse(BoxReader* reader); + // Write the box to buffer. + // The function calls ComputeSize internally to compute box size. + void Write(BufferWriter* writer); + // Computer box size. + // The calculated size will be saved in |atom_size| for consumption later. + virtual uint32 ComputeSize() = 0; + virtual FourCC BoxType() const = 0; + + protected: + friend class BoxBuffer; + // Read or write the mp4 box through BoxBuffer. + virtual bool ReadWrite(BoxBuffer* buffer); + + // We don't support 64-bit atom size. 32-bit should be large enough for our + // current needs. + uint32 atom_size; +}; + +struct FullBox : Box { + public: + FullBox(); + virtual ~FullBox(); + + uint8 version; + uint32 flags; + + protected: + virtual bool ReadWrite(BoxBuffer* buffer) OVERRIDE; +}; + +} // namespace mp4 +} // namespace media + +#endif // MEDIA_MP4_BOX_H_