Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(492)

Unified Diff: media/mp4/box_reader.cc

Issue 10536014: Implement ISO BMFF support in Media Source. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: A thorough sign-ification Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/mp4/box_reader.cc
diff --git a/media/mp4/box_reader.cc b/media/mp4/box_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2b6b15433c29a3886d1056212a489b6bbdc66583
--- /dev/null
+++ b/media/mp4/box_reader.cc
@@ -0,0 +1,193 @@
+// 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.
+
+#include "media/mp4/box_reader.h"
+
+#include <algorithm>
+#include <map>
+#include <set>
+#include <string.h>
acolwell GONE FROM CHROMIUM 2012/06/08 16:10:38 lint nit: Apparently C system headers must come be
strobe_ 2012/06/11 18:44:21 Done.
+
+#include "base/logging.h"
+#include "media/mp4/rcheck.h"
+#include "media/mp4/box_definitions.h"
acolwell GONE FROM CHROMIUM 2012/06/08 16:10:38 lint nit: alphabetical order.
strobe_ 2012/06/11 18:44:21 Apparently I never learned my ABCs. Done.
+
+namespace media {
+namespace mp4 {
+
+bool BufferReader::Read1(uint8* v) {
+ RCHECK(HasBytes(1));
+ *v = buf_[pos_++];
+ return true;
+}
+
+// Internal implementation of multi-byte reads
+template<typename T> bool BufferReader::Read(T* v) {
+ RCHECK(HasBytes(sizeof(T)));
+
+ T tmp = 0;
+ for (size_t i = 0; i < sizeof(T); i++) {
+ tmp <<= 8;
+ tmp += buf_[pos_++];
+ }
+ *v = tmp;
+ return true;
+}
+
+bool BufferReader::Read2(uint16* v) { return Read(v); }
+bool BufferReader::Read2s(int16* v) { return Read(v); }
+bool BufferReader::Read4(uint32* v) { return Read(v); }
+bool BufferReader::Read4s(int32* v) { return Read(v); }
+bool BufferReader::Read8(uint64* v) { return Read(v); }
+bool BufferReader::Read8s(int64* v) { return Read(v); }
+
+bool BufferReader::ReadFourCC(FourCC* v) {
+ return Read4(reinterpret_cast<uint32*>(v));
+}
+
+bool BufferReader::ReadVec(std::vector<uint8>* vec, int count) {
+ RCHECK(HasBytes(count));
+ vec->clear();
+ vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count);
+ pos_ += count;
+ return true;
+}
+
+bool BufferReader::SkipBytes(int bytes) {
+ RCHECK(HasBytes(bytes));
+ pos_ += bytes;
+ return true;
+}
+
+bool BufferReader::Read4Into8(uint64* v) {
+ uint32 tmp;
+ RCHECK(Read4(&tmp));
+ *v = tmp;
+ return true;
+}
+
+bool BufferReader::Read4sInto8s(int64* v) {
+ // Beware of the need for sign extension.
+ int32 tmp;
+ RCHECK(Read4s(&tmp));
+ *v = tmp;
+ return true;
+}
+
+
+BoxReader::BoxReader(const uint8* buf, const int size)
+ : BufferReader(buf, size), scanned_(false) {};
acolwell GONE FROM CHROMIUM 2012/06/08 16:10:38 lint nit: on next line and remove ;
strobe_ 2012/06/11 18:44:21 Done.
+
+BoxReader::~BoxReader() {
+ if (scanned_ && !children_.empty()) {
+ for (auto itr = children_.begin(); itr != children_.end(); ++itr) {
+ DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
+ }
+ }
+}
+
+BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
+ const int buf_size,
+ bool* err) {
+ BoxReader* r = new BoxReader(buf, buf_size);
acolwell GONE FROM CHROMIUM 2012/06/08 16:10:38 r-> reader.
strobe_ 2012/06/11 18:44:21 Done.
+ if (r->ReadHeader(err) && r->size() <= buf_size) {
+ return r;
+ }
+ delete r;
+ return NULL;
+}
+
+// static
+bool BoxReader::StartTopLevelBox(const uint8* buf,
+ const int buf_size,
+ FourCC* type,
+ int* box_size,
+ bool* err) {
+ BoxReader reader(buf, buf_size);
+ if (!reader.ReadHeader(err)) return false;
+ *type = reader.type();
+ *box_size = reader.size();
+ return true;
+}
+
+bool BoxReader::ScanChildren() {
+ DCHECK(!scanned_);
+ scanned_ = true;
+
+ bool err;
+ // TODO(strobe): Check or correct for multimap not inserting elements in
+ // consistent order.
+ while (pos() < size()) {
+ BoxReader child(&buf_[pos_], size_ - pos_);
+ if (!child.ReadHeader(&err)) break;
+
+ children_.insert(std::pair<FourCC, BoxReader>(child.type(), child));
+ pos_ += child.size();
+ }
+
+ DCHECK(!err);
+ return !err && pos() == size();
+}
+
+bool BoxReader::ReadChild(Box* child) {
+ DCHECK(scanned_);
+ // TODO(strobe): decide how to handle inappropriately repeated boxes.
+ // If we want to be as permissive as possible, we can ignore all
+ // boxes that need repeating. On the other hand, we may simply wish to be
+ // strict about the files that we accept.
+ FourCC child_type = child->BoxType();
+
+ auto itr = children_.find(child_type);
+ RCHECK(itr != children_.end());
+ DVLOG(2) << "Found a " << FourCCToString(child_type) << " box.";
+ RCHECK(child->Parse(&itr->second));
+ children_.erase(itr);
+ return true;
+}
+
+bool BoxReader::MaybeReadChild(Box* child) {
+ if (!children_.count(child->BoxType())) return true;
+ return ReadChild(child);
+}
+
+bool BoxReader::ReadFullBoxHeader() {
+ uint32 vflags;
+ RCHECK(Read4(&vflags));
+ version_ = vflags >> 24;
+ flags_ = vflags & 0xffffff;
+ return true;
+}
+
+bool BoxReader::ReadHeader(bool* err) {
+ uint64 size;
+ *err = false;
+
+ if (!HasBytes(8)) return false;
+ CHECK(Read4Into8(&size) && ReadFourCC(&type_));
+
+ if (size == 0) {
+ // Media Source specific: we do not support boxes that run to EOS.
+ *err = true;
+ return false;
+ } else if (size == 1) {
+ if (!HasBytes(8)) return false;
+ CHECK(Read8(&size));
+ }
+
+ // Implementation-specific: support for boxes larger than 2^31 has been
+ // removed.
+ if (size < static_cast<uint64>(pos_) ||
+ size > static_cast<uint64>(kint32max)) {
+ *err = true;
+ return false;
+ }
+
+ // Note that the pos_ head has advanced to the byte immediately after the
+ // header, which is where we want it.
+ size_ = size;
+ return true;
+}
+
+} // namespace mp4
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698