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 #ifndef MEDIA_MP4_BOX_READER_H_ | |
| 6 #define MEDIA_MP4_BOX_READER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "media/mp4/fourccs.h" | |
| 14 #include "media/mp4/rcheck.h" | |
| 15 | |
| 16 namespace media { | |
| 17 namespace mp4 { | |
| 18 | |
| 19 class BoxReader; | |
| 20 | |
| 21 struct Box { | |
|
acolwell GONE FROM CHROMIUM
2012/06/08 16:10:38
lint nit: needs virtual destructor.
strobe_
2012/06/11 18:44:21
Done.
| |
| 22 virtual bool Parse(BoxReader* reader) = 0; | |
| 23 virtual FourCC BoxType() const = 0; | |
| 24 }; | |
| 25 | |
| 26 class BufferReader { | |
| 27 public: | |
| 28 BufferReader(const uint8* buf, const int size) | |
| 29 : buf_(buf), size_(size), pos_(0) {} | |
| 30 | |
| 31 bool HasBytes(int count) { return (pos() + count <= size()); } | |
| 32 | |
| 33 // Read a value from the stream, perfoming endian correction, and advance the | |
| 34 // stream pointer. | |
| 35 bool Read1(uint8* v) WARN_UNUSED_RESULT; | |
| 36 bool Read2(uint16* v) WARN_UNUSED_RESULT; | |
| 37 bool Read2s(int16* v) WARN_UNUSED_RESULT; | |
| 38 bool Read4(uint32* v) WARN_UNUSED_RESULT; | |
| 39 bool Read4s(int32* v) WARN_UNUSED_RESULT; | |
| 40 bool Read8(uint64* v) WARN_UNUSED_RESULT; | |
| 41 bool Read8s(int64* v) WARN_UNUSED_RESULT; | |
| 42 | |
| 43 bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT; | |
| 44 | |
| 45 bool ReadVec(std::vector<uint8>* t, int count) WARN_UNUSED_RESULT; | |
| 46 | |
| 47 // These variants read a 4-byte integer of the corresponding signedness and | |
| 48 // store it in the 8-byte return type. | |
| 49 bool Read4Into8(uint64* v) WARN_UNUSED_RESULT; | |
| 50 bool Read4sInto8s(int64* v) WARN_UNUSED_RESULT; | |
| 51 | |
| 52 // Advance the stream by this many bytes. | |
| 53 bool SkipBytes(int nbytes) WARN_UNUSED_RESULT; | |
| 54 | |
| 55 int size() { return size_; } | |
| 56 int pos() { return pos_; } | |
| 57 | |
| 58 protected: | |
| 59 const uint8* buf_; | |
| 60 int size_; | |
| 61 int pos_; | |
| 62 | |
| 63 template<typename T> bool Read(T* t) WARN_UNUSED_RESULT; | |
| 64 }; | |
| 65 | |
| 66 class BoxReader : public BufferReader { | |
| 67 public: | |
| 68 ~BoxReader(); | |
| 69 | |
| 70 // Create a BoxReader from a buffer. Note that this function may return NULL | |
| 71 // if an intact, complete box was not available in the buffer. If |*err| is | |
| 72 // set, there was a stream-level error when creating the box; otherwise, NULL | |
| 73 // values are only expected when insufficient data is available. | |
| 74 // | |
| 75 // |buf| is retained but not owned, and must outlive the BoxReader instance. | |
| 76 static BoxReader* ReadTopLevelBox(const uint8* buf, | |
| 77 const int buf_size, | |
| 78 bool* err); | |
| 79 | |
| 80 // Read the box header from the current buffer. This function returns true if | |
| 81 // there is enough data to read the header and the header is sane; that is, it | |
| 82 // does not check to ensure the entire box is in the buffer before returning | |
| 83 // true. The semantics of |*err| are the same as above. | |
| 84 // | |
| 85 // |buf| is not retained. | |
| 86 static bool StartTopLevelBox(const uint8* buf, | |
| 87 const int buf_size, | |
| 88 FourCC* type, | |
| 89 int* box_size, | |
| 90 bool* err) WARN_UNUSED_RESULT; | |
| 91 | |
| 92 // Scan through all boxes within the current box, starting at the current | |
| 93 // buffer position. Must be called before any of the *Child functions work. | |
| 94 bool ScanChildren() WARN_UNUSED_RESULT; | |
| 95 | |
| 96 // Read exactly one child box from the set of children. The type of the child | |
| 97 // will be determined by the BoxType() method of |child|. | |
| 98 bool ReadChild(Box* child) WARN_UNUSED_RESULT; | |
| 99 | |
| 100 // Read one child if available. Returns false on error, true on successful | |
| 101 // read or on child absent. | |
| 102 bool MaybeReadChild(Box* child) WARN_UNUSED_RESULT; | |
| 103 | |
| 104 // Read at least one child. False means error or no such child present. | |
| 105 template<typename T> bool ReadChildren( | |
| 106 std::vector<T>* children) WARN_UNUSED_RESULT; | |
| 107 | |
| 108 // Read any number of children. False means error. | |
| 109 template<typename T> bool MaybeReadChildren( | |
| 110 std::vector<T>* children) WARN_UNUSED_RESULT; | |
| 111 | |
| 112 // Read all children, regardless of FourCC. This is used from exactly one box, | |
| 113 // corresponding to a rather significant inconsistency in the BMFF spec. | |
| 114 template<typename T> bool ReadAllChildren( | |
| 115 std::vector<T>* children) WARN_UNUSED_RESULT; | |
| 116 | |
| 117 // Populate the values of 'version()' and 'flags()' from a full box header. | |
| 118 // Many boxes, but not all, use these values. This call should happen after | |
| 119 // the box has been initialized, and does not re-read the main box header. | |
| 120 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; | |
| 121 | |
| 122 FourCC type() const { return type_; } | |
| 123 uint8 version() const { return version_; } | |
| 124 uint32 flags() const { return flags_; } | |
| 125 private: | |
|
acolwell GONE FROM CHROMIUM
2012/06/08 16:10:38
lint nit: add a blank before private:
strobe_
2012/06/11 18:44:21
Done.
| |
| 126 BoxReader(const uint8* buf, const int size); | |
| 127 | |
| 128 // Must be called immediately after init. If the return is false, this | |
| 129 // indicates that the box header and its contents were not available in the | |
| 130 // stream, and that the box must not be used further. If (result && !err), | |
|
acolwell GONE FROM CHROMIUM
2012/06/08 16:10:38
Does result mean the return value?
Add || around e
strobe_
2012/06/11 18:44:21
Done.
| |
| 131 // the problem was simply a lack of data, and should only be an error | |
| 132 // condition if some higher-level component knows that no more data is coming | |
| 133 // (i.e. EOS or end of containing box). | |
| 134 bool ReadHeader(bool* err); | |
| 135 | |
| 136 FourCC type_; | |
| 137 uint8 version_; | |
| 138 uint32 flags_; | |
| 139 | |
| 140 // The set of child box FourCCs and their corresponding buffer readers. Only | |
| 141 // valid if scanned_ is true. | |
| 142 std::multimap<FourCC, BoxReader> children_; | |
| 143 bool scanned_; | |
| 144 }; | |
| 145 | |
| 146 // Template definitions | |
| 147 template<typename T> bool BoxReader::ReadChildren(std::vector<T>* children) { | |
| 148 RCHECK(MaybeReadChildren(children) && !children->empty()); | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 template<typename T> | |
| 153 bool BoxReader::MaybeReadChildren(std::vector<T>* children) { | |
| 154 DCHECK(scanned_); | |
| 155 DCHECK(children->empty()); | |
| 156 | |
| 157 children->resize(1); | |
| 158 FourCC child_type = (*children)[0].BoxType(); | |
| 159 | |
| 160 auto start_itr = children_.lower_bound(child_type); | |
| 161 auto end_itr = children_.upper_bound(child_type); | |
| 162 children->resize(std::distance(start_itr, end_itr)); | |
| 163 auto child_itr = children->begin(); | |
| 164 for (auto itr = start_itr; itr != end_itr; ++itr) { | |
| 165 RCHECK(child_itr->Parse(&itr->second)); | |
| 166 ++child_itr; | |
| 167 } | |
| 168 children_.erase(start_itr, end_itr); | |
| 169 | |
| 170 DVLOG(2) << "Found " << children->size() << " " | |
| 171 << FourCCToString(child_type) << " boxes."; | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 template<typename T> | |
| 176 bool BoxReader::ReadAllChildren(std::vector<T>* children) { | |
| 177 DCHECK(scanned_); | |
| 178 DCHECK(children->empty()); | |
| 179 RCHECK(!children_.empty()); | |
| 180 | |
| 181 children->resize(children_.size()); | |
| 182 auto child_itr = children->begin(); | |
| 183 for (auto itr = children_.begin(); itr != children_.end(); ++itr) { | |
| 184 RCHECK(child_itr->Parse(&itr->second)); | |
| 185 ++child_itr; | |
| 186 } | |
| 187 children_.clear(); | |
| 188 return true; | |
| 189 } | |
| 190 | |
| 191 } // namespace mp4 | |
| 192 } // namespace media | |
| 193 | |
| 194 #endif // MEDIA_MP4_BOX_READER_H_ | |
| OLD | NEW |