| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_FORMATS_MP4_BOX_READER_H_ | 5 #ifndef MEDIA_FORMATS_MP4_BOX_READER_H_ |
| 6 #define MEDIA_FORMATS_MP4_BOX_READER_H_ | 6 #define MEDIA_FORMATS_MP4_BOX_READER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <limits> |
| 8 #include <map> | 11 #include <map> |
| 9 #include <vector> | 12 #include <vector> |
| 10 | 13 |
| 11 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 15 #include "base/logging.h" |
| 13 #include "base/numerics/safe_conversions.h" | 16 #include "base/numerics/safe_conversions.h" |
| 14 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
| 15 #include "media/base/media_log.h" | 18 #include "media/base/media_log.h" |
| 16 #include "media/formats/mp4/fourccs.h" | 19 #include "media/formats/mp4/fourccs.h" |
| 17 #include "media/formats/mp4/rcheck.h" | 20 #include "media/formats/mp4/rcheck.h" |
| 18 | 21 |
| 19 namespace media { | 22 namespace media { |
| 20 namespace mp4 { | 23 namespace mp4 { |
| 21 | 24 |
| 22 class BoxReader; | 25 class BoxReader; |
| 23 | 26 |
| 24 struct MEDIA_EXPORT Box { | 27 struct MEDIA_EXPORT Box { |
| 25 virtual ~Box(); | 28 virtual ~Box(); |
| 26 | 29 |
| 27 // Parse errors may be logged using the BoxReader's media log. | 30 // Parse errors may be logged using the BoxReader's media log. |
| 28 virtual bool Parse(BoxReader* reader) = 0; | 31 virtual bool Parse(BoxReader* reader) = 0; |
| 29 | 32 |
| 30 virtual FourCC BoxType() const = 0; | 33 virtual FourCC BoxType() const = 0; |
| 31 }; | 34 }; |
| 32 | 35 |
| 33 class MEDIA_EXPORT BufferReader { | 36 class MEDIA_EXPORT BufferReader { |
| 34 public: | 37 public: |
| 35 BufferReader(const uint8* buf, const int size) : buf_(buf), pos_(0) { | 38 BufferReader(const uint8_t* buf, const int size) : buf_(buf), pos_(0) { |
| 36 CHECK(buf); | 39 CHECK(buf); |
| 37 size_ = base::checked_cast<uint64>(size); | 40 size_ = base::checked_cast<uint64_t>(size); |
| 38 } | 41 } |
| 39 | 42 |
| 40 bool HasBytes(uint64 count) { | 43 bool HasBytes(uint64_t count) { |
| 41 // As the size of a box is implementation limited to 2^31, fail if | 44 // As the size of a box is implementation limited to 2^31, fail if |
| 42 // attempting to check for too many bytes. | 45 // attempting to check for too many bytes. |
| 43 return (pos_ <= size_ && count < static_cast<uint64>(kint32max) && | 46 return (pos_ <= size_ && |
| 47 count < |
| 48 static_cast<uint64_t>(std::numeric_limits<int32_t>::max()) && |
| 44 size_ - pos_ >= count); | 49 size_ - pos_ >= count); |
| 45 } | 50 } |
| 46 | 51 |
| 47 // Read a value from the stream, perfoming endian correction, and advance the | 52 // Read a value from the stream, perfoming endian correction, and advance the |
| 48 // stream pointer. | 53 // stream pointer. |
| 49 bool Read1(uint8* v) WARN_UNUSED_RESULT; | 54 bool Read1(uint8_t* v) WARN_UNUSED_RESULT; |
| 50 bool Read2(uint16* v) WARN_UNUSED_RESULT; | 55 bool Read2(uint16_t* v) WARN_UNUSED_RESULT; |
| 51 bool Read2s(int16* v) WARN_UNUSED_RESULT; | 56 bool Read2s(int16_t* v) WARN_UNUSED_RESULT; |
| 52 bool Read4(uint32* v) WARN_UNUSED_RESULT; | 57 bool Read4(uint32_t* v) WARN_UNUSED_RESULT; |
| 53 bool Read4s(int32* v) WARN_UNUSED_RESULT; | 58 bool Read4s(int32_t* v) WARN_UNUSED_RESULT; |
| 54 bool Read8(uint64* v) WARN_UNUSED_RESULT; | 59 bool Read8(uint64_t* v) WARN_UNUSED_RESULT; |
| 55 bool Read8s(int64* v) WARN_UNUSED_RESULT; | 60 bool Read8s(int64_t* v) WARN_UNUSED_RESULT; |
| 56 | 61 |
| 57 bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT; | 62 bool ReadFourCC(FourCC* v) WARN_UNUSED_RESULT; |
| 58 | 63 |
| 59 bool ReadVec(std::vector<uint8>* t, uint64 count) WARN_UNUSED_RESULT; | 64 bool ReadVec(std::vector<uint8_t>* t, uint64_t count) WARN_UNUSED_RESULT; |
| 60 | 65 |
| 61 // These variants read a 4-byte integer of the corresponding signedness and | 66 // These variants read a 4-byte integer of the corresponding signedness and |
| 62 // store it in the 8-byte return type. | 67 // store it in the 8-byte return type. |
| 63 bool Read4Into8(uint64* v) WARN_UNUSED_RESULT; | 68 bool Read4Into8(uint64_t* v) WARN_UNUSED_RESULT; |
| 64 bool Read4sInto8s(int64* v) WARN_UNUSED_RESULT; | 69 bool Read4sInto8s(int64_t* v) WARN_UNUSED_RESULT; |
| 65 | 70 |
| 66 // Advance the stream by this many bytes. | 71 // Advance the stream by this many bytes. |
| 67 bool SkipBytes(uint64 nbytes) WARN_UNUSED_RESULT; | 72 bool SkipBytes(uint64_t nbytes) WARN_UNUSED_RESULT; |
| 68 | 73 |
| 69 const uint8* data() const { return buf_; } | 74 const uint8_t* data() const { return buf_; } |
| 70 | 75 |
| 71 // This returns the size of the box as specified in the box header. Initially | 76 // This returns the size of the box as specified in the box header. Initially |
| 72 // it is the buffer size until the header is read. Note that the size | 77 // it is the buffer size until the header is read. Note that the size |
| 73 // specified in the box header may be different than the number of bytes | 78 // specified in the box header may be different than the number of bytes |
| 74 // actually provided. | 79 // actually provided. |
| 75 uint64 size() const { return size_; } | 80 uint64_t size() const { return size_; } |
| 76 uint64 pos() const { return pos_; } | 81 uint64_t pos() const { return pos_; } |
| 77 | 82 |
| 78 protected: | 83 protected: |
| 79 const uint8* buf_; | 84 const uint8_t* buf_; |
| 80 uint64 size_; | 85 uint64_t size_; |
| 81 uint64 pos_; | 86 uint64_t pos_; |
| 82 | 87 |
| 83 template<typename T> bool Read(T* t) WARN_UNUSED_RESULT; | 88 template<typename T> bool Read(T* t) WARN_UNUSED_RESULT; |
| 84 }; | 89 }; |
| 85 | 90 |
| 86 class MEDIA_EXPORT BoxReader : public BufferReader { | 91 class MEDIA_EXPORT BoxReader : public BufferReader { |
| 87 public: | 92 public: |
| 88 ~BoxReader(); | 93 ~BoxReader(); |
| 89 | 94 |
| 90 // Create a BoxReader from a buffer. Note that this function may return NULL | 95 // Create a BoxReader from a buffer. Note that this function may return NULL |
| 91 // if an intact, complete box was not available in the buffer. If |*err| is | 96 // if an intact, complete box was not available in the buffer. If |*err| is |
| 92 // set, there was a stream-level error when creating the box; otherwise, NULL | 97 // set, there was a stream-level error when creating the box; otherwise, NULL |
| 93 // values are only expected when insufficient data is available. | 98 // values are only expected when insufficient data is available. |
| 94 // | 99 // |
| 95 // |buf| is retained but not owned, and must outlive the BoxReader instance. | 100 // |buf| is retained but not owned, and must outlive the BoxReader instance. |
| 96 static BoxReader* ReadTopLevelBox(const uint8* buf, | 101 static BoxReader* ReadTopLevelBox(const uint8_t* buf, |
| 97 const int buf_size, | 102 const int buf_size, |
| 98 const scoped_refptr<MediaLog>& media_log, | 103 const scoped_refptr<MediaLog>& media_log, |
| 99 bool* err); | 104 bool* err); |
| 100 | 105 |
| 101 // Read the box header from the current buffer. This function returns true if | 106 // Read the box header from the current buffer. This function returns true if |
| 102 // there is enough data to read the header and the header is sane; that is, it | 107 // there is enough data to read the header and the header is sane; that is, it |
| 103 // does not check to ensure the entire box is in the buffer before returning | 108 // does not check to ensure the entire box is in the buffer before returning |
| 104 // true. The semantics of |*err| are the same as above. | 109 // true. The semantics of |*err| are the same as above. |
| 105 // | 110 // |
| 106 // |buf| is not retained. | 111 // |buf| is not retained. |
| 107 static bool StartTopLevelBox(const uint8* buf, | 112 static bool StartTopLevelBox(const uint8_t* buf, |
| 108 const int buf_size, | 113 const int buf_size, |
| 109 const scoped_refptr<MediaLog>& media_log, | 114 const scoped_refptr<MediaLog>& media_log, |
| 110 FourCC* type, | 115 FourCC* type, |
| 111 int* box_size, | 116 int* box_size, |
| 112 bool* err) WARN_UNUSED_RESULT; | 117 bool* err) WARN_UNUSED_RESULT; |
| 113 | 118 |
| 114 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as | 119 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as |
| 115 // errors are returned when sufficient data is not available. |buf| can start | 120 // errors are returned when sufficient data is not available. |buf| can start |
| 116 // with any type of box -- it does not have to be IsValidTopLevelBox(). | 121 // with any type of box -- it does not have to be IsValidTopLevelBox(). |
| 117 // | 122 // |
| 118 // |buf| is retained but not owned, and must outlive the BoxReader instance. | 123 // |buf| is retained but not owned, and must outlive the BoxReader instance. |
| 119 static BoxReader* ReadConcatentatedBoxes(const uint8* buf, | 124 static BoxReader* ReadConcatentatedBoxes(const uint8_t* buf, |
| 120 const int buf_size); | 125 const int buf_size); |
| 121 | 126 |
| 122 // Returns true if |type| is recognized to be a top-level box, false | 127 // Returns true if |type| is recognized to be a top-level box, false |
| 123 // otherwise. This returns true for some boxes which we do not parse. | 128 // otherwise. This returns true for some boxes which we do not parse. |
| 124 // Helpful in debugging misaligned appends. | 129 // Helpful in debugging misaligned appends. |
| 125 static bool IsValidTopLevelBox(const FourCC& type, | 130 static bool IsValidTopLevelBox(const FourCC& type, |
| 126 const scoped_refptr<MediaLog>& media_log); | 131 const scoped_refptr<MediaLog>& media_log); |
| 127 | 132 |
| 128 // Scan through all boxes within the current box, starting at the current | 133 // Scan through all boxes within the current box, starting at the current |
| 129 // buffer position. Must be called before any of the *Child functions work. | 134 // buffer position. Must be called before any of the *Child functions work. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 template <typename T> | 167 template <typename T> |
| 163 bool ReadAllChildrenAndCheckFourCC(std::vector<T>* children) | 168 bool ReadAllChildrenAndCheckFourCC(std::vector<T>* children) |
| 164 WARN_UNUSED_RESULT; | 169 WARN_UNUSED_RESULT; |
| 165 | 170 |
| 166 // Populate the values of 'version()' and 'flags()' from a full box header. | 171 // Populate the values of 'version()' and 'flags()' from a full box header. |
| 167 // Many boxes, but not all, use these values. This call should happen after | 172 // Many boxes, but not all, use these values. This call should happen after |
| 168 // the box has been initialized, and does not re-read the main box header. | 173 // the box has been initialized, and does not re-read the main box header. |
| 169 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; | 174 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; |
| 170 | 175 |
| 171 FourCC type() const { return type_; } | 176 FourCC type() const { return type_; } |
| 172 uint8 version() const { return version_; } | 177 uint8_t version() const { return version_; } |
| 173 uint32 flags() const { return flags_; } | 178 uint32_t flags() const { return flags_; } |
| 174 | 179 |
| 175 const scoped_refptr<MediaLog>& media_log() const { return media_log_; } | 180 const scoped_refptr<MediaLog>& media_log() const { return media_log_; } |
| 176 | 181 |
| 177 private: | 182 private: |
| 178 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is | 183 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is |
| 179 // complete stream (i.e. no additional data is expected to be appended). | 184 // complete stream (i.e. no additional data is expected to be appended). |
| 180 BoxReader(const uint8* buf, | 185 BoxReader(const uint8_t* buf, |
| 181 const int size, | 186 const int size, |
| 182 const scoped_refptr<MediaLog>& media_log, | 187 const scoped_refptr<MediaLog>& media_log, |
| 183 bool is_EOS); | 188 bool is_EOS); |
| 184 | 189 |
| 185 // Must be called immediately after init. If the return is false, this | 190 // Must be called immediately after init. If the return is false, this |
| 186 // indicates that the box header and its contents were not available in the | 191 // indicates that the box header and its contents were not available in the |
| 187 // stream or were nonsensical, and that the box must not be used further. In | 192 // stream or were nonsensical, and that the box must not be used further. In |
| 188 // this case, if |*err| is false, the problem was simply a lack of data, and | 193 // this case, if |*err| is false, the problem was simply a lack of data, and |
| 189 // should only be an error condition if some higher-level component knows that | 194 // should only be an error condition if some higher-level component knows that |
| 190 // no more data is coming (i.e. EOS or end of containing box). If |*err| is | 195 // no more data is coming (i.e. EOS or end of containing box). If |*err| is |
| 191 // true, the error is unrecoverable and the stream should be aborted. | 196 // true, the error is unrecoverable and the stream should be aborted. |
| 192 bool ReadHeader(bool* err); | 197 bool ReadHeader(bool* err); |
| 193 | 198 |
| 194 // Read all children, optionally checking FourCC. Returns true if all | 199 // Read all children, optionally checking FourCC. Returns true if all |
| 195 // children are successfully parsed and, if |check_box_type|, have the | 200 // children are successfully parsed and, if |check_box_type|, have the |
| 196 // correct box type for |T|. Note that this method is mutually exclusive | 201 // correct box type for |T|. Note that this method is mutually exclusive |
| 197 // with ScanChildren(). | 202 // with ScanChildren(). |
| 198 template <typename T> | 203 template <typename T> |
| 199 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type); | 204 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type); |
| 200 | 205 |
| 201 scoped_refptr<MediaLog> media_log_; | 206 scoped_refptr<MediaLog> media_log_; |
| 202 FourCC type_; | 207 FourCC type_; |
| 203 uint8 version_; | 208 uint8_t version_; |
| 204 uint32 flags_; | 209 uint32_t flags_; |
| 205 | 210 |
| 206 typedef std::multimap<FourCC, BoxReader> ChildMap; | 211 typedef std::multimap<FourCC, BoxReader> ChildMap; |
| 207 | 212 |
| 208 // The set of child box FourCCs and their corresponding buffer readers. Only | 213 // The set of child box FourCCs and their corresponding buffer readers. Only |
| 209 // valid if scanned_ is true. | 214 // valid if scanned_ is true. |
| 210 ChildMap children_; | 215 ChildMap children_; |
| 211 bool scanned_; | 216 bool scanned_; |
| 212 | 217 |
| 213 // True if the buffer provided to the reader is the complete stream. | 218 // True if the buffer provided to the reader is the complete stream. |
| 214 const bool is_EOS_; | 219 const bool is_EOS_; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 pos_ += child_reader.size(); | 275 pos_ += child_reader.size(); |
| 271 } | 276 } |
| 272 | 277 |
| 273 return !err; | 278 return !err; |
| 274 } | 279 } |
| 275 | 280 |
| 276 } // namespace mp4 | 281 } // namespace mp4 |
| 277 } // namespace media | 282 } // namespace media |
| 278 | 283 |
| 279 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_ | 284 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_ |
| OLD | NEW |