Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/mp4/box_reader.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #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.
| |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "media/mp4/rcheck.h" | |
| 14 #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.
| |
| 15 | |
| 16 namespace media { | |
| 17 namespace mp4 { | |
| 18 | |
| 19 bool BufferReader::Read1(uint8* v) { | |
| 20 RCHECK(HasBytes(1)); | |
| 21 *v = buf_[pos_++]; | |
| 22 return true; | |
| 23 } | |
| 24 | |
| 25 // Internal implementation of multi-byte reads | |
| 26 template<typename T> bool BufferReader::Read(T* v) { | |
| 27 RCHECK(HasBytes(sizeof(T))); | |
| 28 | |
| 29 T tmp = 0; | |
| 30 for (size_t i = 0; i < sizeof(T); i++) { | |
| 31 tmp <<= 8; | |
| 32 tmp += buf_[pos_++]; | |
| 33 } | |
| 34 *v = tmp; | |
| 35 return true; | |
| 36 } | |
| 37 | |
| 38 bool BufferReader::Read2(uint16* v) { return Read(v); } | |
| 39 bool BufferReader::Read2s(int16* v) { return Read(v); } | |
| 40 bool BufferReader::Read4(uint32* v) { return Read(v); } | |
| 41 bool BufferReader::Read4s(int32* v) { return Read(v); } | |
| 42 bool BufferReader::Read8(uint64* v) { return Read(v); } | |
| 43 bool BufferReader::Read8s(int64* v) { return Read(v); } | |
| 44 | |
| 45 bool BufferReader::ReadFourCC(FourCC* v) { | |
| 46 return Read4(reinterpret_cast<uint32*>(v)); | |
| 47 } | |
| 48 | |
| 49 bool BufferReader::ReadVec(std::vector<uint8>* vec, int count) { | |
| 50 RCHECK(HasBytes(count)); | |
| 51 vec->clear(); | |
| 52 vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count); | |
| 53 pos_ += count; | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool BufferReader::SkipBytes(int bytes) { | |
| 58 RCHECK(HasBytes(bytes)); | |
| 59 pos_ += bytes; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 bool BufferReader::Read4Into8(uint64* v) { | |
| 64 uint32 tmp; | |
| 65 RCHECK(Read4(&tmp)); | |
| 66 *v = tmp; | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 bool BufferReader::Read4sInto8s(int64* v) { | |
| 71 // Beware of the need for sign extension. | |
| 72 int32 tmp; | |
| 73 RCHECK(Read4s(&tmp)); | |
| 74 *v = tmp; | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 | |
| 79 BoxReader::BoxReader(const uint8* buf, const int size) | |
| 80 : 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.
| |
| 81 | |
| 82 BoxReader::~BoxReader() { | |
| 83 if (scanned_ && !children_.empty()) { | |
| 84 for (auto itr = children_.begin(); itr != children_.end(); ++itr) { | |
| 85 DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first); | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf, | |
| 91 const int buf_size, | |
| 92 bool* err) { | |
| 93 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.
| |
| 94 if (r->ReadHeader(err) && r->size() <= buf_size) { | |
| 95 return r; | |
| 96 } | |
| 97 delete r; | |
| 98 return NULL; | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 bool BoxReader::StartTopLevelBox(const uint8* buf, | |
| 103 const int buf_size, | |
| 104 FourCC* type, | |
| 105 int* box_size, | |
| 106 bool* err) { | |
| 107 BoxReader reader(buf, buf_size); | |
| 108 if (!reader.ReadHeader(err)) return false; | |
| 109 *type = reader.type(); | |
| 110 *box_size = reader.size(); | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 bool BoxReader::ScanChildren() { | |
| 115 DCHECK(!scanned_); | |
| 116 scanned_ = true; | |
| 117 | |
| 118 bool err; | |
| 119 // TODO(strobe): Check or correct for multimap not inserting elements in | |
| 120 // consistent order. | |
| 121 while (pos() < size()) { | |
| 122 BoxReader child(&buf_[pos_], size_ - pos_); | |
| 123 if (!child.ReadHeader(&err)) break; | |
| 124 | |
| 125 children_.insert(std::pair<FourCC, BoxReader>(child.type(), child)); | |
| 126 pos_ += child.size(); | |
| 127 } | |
| 128 | |
| 129 DCHECK(!err); | |
| 130 return !err && pos() == size(); | |
| 131 } | |
| 132 | |
| 133 bool BoxReader::ReadChild(Box* child) { | |
| 134 DCHECK(scanned_); | |
| 135 // TODO(strobe): decide how to handle inappropriately repeated boxes. | |
| 136 // If we want to be as permissive as possible, we can ignore all | |
| 137 // boxes that need repeating. On the other hand, we may simply wish to be | |
| 138 // strict about the files that we accept. | |
| 139 FourCC child_type = child->BoxType(); | |
| 140 | |
| 141 auto itr = children_.find(child_type); | |
| 142 RCHECK(itr != children_.end()); | |
| 143 DVLOG(2) << "Found a " << FourCCToString(child_type) << " box."; | |
| 144 RCHECK(child->Parse(&itr->second)); | |
| 145 children_.erase(itr); | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 bool BoxReader::MaybeReadChild(Box* child) { | |
| 150 if (!children_.count(child->BoxType())) return true; | |
| 151 return ReadChild(child); | |
| 152 } | |
| 153 | |
| 154 bool BoxReader::ReadFullBoxHeader() { | |
| 155 uint32 vflags; | |
| 156 RCHECK(Read4(&vflags)); | |
| 157 version_ = vflags >> 24; | |
| 158 flags_ = vflags & 0xffffff; | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 bool BoxReader::ReadHeader(bool* err) { | |
| 163 uint64 size; | |
| 164 *err = false; | |
| 165 | |
| 166 if (!HasBytes(8)) return false; | |
| 167 CHECK(Read4Into8(&size) && ReadFourCC(&type_)); | |
| 168 | |
| 169 if (size == 0) { | |
| 170 // Media Source specific: we do not support boxes that run to EOS. | |
| 171 *err = true; | |
| 172 return false; | |
| 173 } else if (size == 1) { | |
| 174 if (!HasBytes(8)) return false; | |
| 175 CHECK(Read8(&size)); | |
| 176 } | |
| 177 | |
| 178 // Implementation-specific: support for boxes larger than 2^31 has been | |
| 179 // removed. | |
| 180 if (size < static_cast<uint64>(pos_) || | |
| 181 size > static_cast<uint64>(kint32max)) { | |
| 182 *err = true; | |
| 183 return false; | |
| 184 } | |
| 185 | |
| 186 // Note that the pos_ head has advanced to the byte immediately after the | |
| 187 // header, which is where we want it. | |
| 188 size_ = size; | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 } // namespace mp4 | |
| 193 } // namespace media | |
| OLD | NEW |