| 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 <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "media/base/media_export.h" | 13 #include "media/base/media_export.h" |
| 14 #include "media/base/media_log.h" | 14 #include "media/base/media_log.h" |
| 15 #include "media/formats/mp4/fourccs.h" | 15 #include "media/formats/mp4/fourccs.h" |
| 16 #include "media/formats/mp4/rcheck.h" | 16 #include "media/formats/mp4/rcheck.h" |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 namespace mp4 { | 19 namespace mp4 { |
| 20 | 20 |
| 21 class BoxReader; | 21 class BoxReader; |
| 22 | 22 |
| 23 struct MEDIA_EXPORT Box { | 23 struct MEDIA_EXPORT Box { |
| 24 virtual ~Box(); | 24 virtual ~Box(); |
| 25 | 25 |
| 26 // Parse errors may be logged using the BoxReader's log callback. | 26 // Parse errors may be logged using the BoxReader's media log. |
| 27 virtual bool Parse(BoxReader* reader) = 0; | 27 virtual bool Parse(BoxReader* reader) = 0; |
| 28 | 28 |
| 29 virtual FourCC BoxType() const = 0; | 29 virtual FourCC BoxType() const = 0; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 class MEDIA_EXPORT BufferReader { | 32 class MEDIA_EXPORT BufferReader { |
| 33 public: | 33 public: |
| 34 BufferReader(const uint8* buf, const int size) | 34 BufferReader(const uint8* buf, const int size) |
| 35 : buf_(buf), size_(size), pos_(0) { | 35 : buf_(buf), size_(size), pos_(0) { |
| 36 CHECK(buf); | 36 CHECK(buf); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 ~BoxReader(); | 78 ~BoxReader(); |
| 79 | 79 |
| 80 // Create a BoxReader from a buffer. Note that this function may return NULL | 80 // Create a BoxReader from a buffer. Note that this function may return NULL |
| 81 // if an intact, complete box was not available in the buffer. If |*err| is | 81 // if an intact, complete box was not available in the buffer. If |*err| is |
| 82 // set, there was a stream-level error when creating the box; otherwise, NULL | 82 // set, there was a stream-level error when creating the box; otherwise, NULL |
| 83 // values are only expected when insufficient data is available. | 83 // values are only expected when insufficient data is available. |
| 84 // | 84 // |
| 85 // |buf| is retained but not owned, and must outlive the BoxReader instance. | 85 // |buf| is retained but not owned, and must outlive the BoxReader instance. |
| 86 static BoxReader* ReadTopLevelBox(const uint8* buf, | 86 static BoxReader* ReadTopLevelBox(const uint8* buf, |
| 87 const int buf_size, | 87 const int buf_size, |
| 88 const LogCB& log_cb, | 88 const scoped_refptr<MediaLog>& media_log, |
| 89 bool* err); | 89 bool* err); |
| 90 | 90 |
| 91 // Read the box header from the current buffer. This function returns true if | 91 // Read the box header from the current buffer. This function returns true if |
| 92 // there is enough data to read the header and the header is sane; that is, it | 92 // there is enough data to read the header and the header is sane; that is, it |
| 93 // does not check to ensure the entire box is in the buffer before returning | 93 // does not check to ensure the entire box is in the buffer before returning |
| 94 // true. The semantics of |*err| are the same as above. | 94 // true. The semantics of |*err| are the same as above. |
| 95 // | 95 // |
| 96 // |buf| is not retained. | 96 // |buf| is not retained. |
| 97 static bool StartTopLevelBox(const uint8* buf, | 97 static bool StartTopLevelBox(const uint8* buf, |
| 98 const int buf_size, | 98 const int buf_size, |
| 99 const LogCB& log_cb, | 99 const scoped_refptr<MediaLog>& media_log, |
| 100 FourCC* type, | 100 FourCC* type, |
| 101 int* box_size, | 101 int* box_size, |
| 102 bool* err) WARN_UNUSED_RESULT; | 102 bool* err) WARN_UNUSED_RESULT; |
| 103 | 103 |
| 104 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as | 104 // Create a BoxReader from a buffer. |buf| must be the complete buffer, as |
| 105 // errors are returned when sufficient data is not available. |buf| can start | 105 // errors are returned when sufficient data is not available. |buf| can start |
| 106 // with any type of box -- it does not have to be IsValidTopLevelBox(). | 106 // with any type of box -- it does not have to be IsValidTopLevelBox(). |
| 107 // | 107 // |
| 108 // |buf| is retained but not owned, and must outlive the BoxReader instance. | 108 // |buf| is retained but not owned, and must outlive the BoxReader instance. |
| 109 static BoxReader* ReadConcatentatedBoxes(const uint8* buf, | 109 static BoxReader* ReadConcatentatedBoxes(const uint8* buf, |
| 110 const int buf_size); | 110 const int buf_size); |
| 111 | 111 |
| 112 // Returns true if |type| is recognized to be a top-level box, false | 112 // Returns true if |type| is recognized to be a top-level box, false |
| 113 // otherwise. This returns true for some boxes which we do not parse. | 113 // otherwise. This returns true for some boxes which we do not parse. |
| 114 // Helpful in debugging misaligned appends. | 114 // Helpful in debugging misaligned appends. |
| 115 static bool IsValidTopLevelBox(const FourCC& type, | 115 static bool IsValidTopLevelBox(const FourCC& type, |
| 116 const LogCB& log_cb); | 116 const scoped_refptr<MediaLog>& media_log); |
| 117 | 117 |
| 118 // Scan through all boxes within the current box, starting at the current | 118 // Scan through all boxes within the current box, starting at the current |
| 119 // buffer position. Must be called before any of the *Child functions work. | 119 // buffer position. Must be called before any of the *Child functions work. |
| 120 bool ScanChildren() WARN_UNUSED_RESULT; | 120 bool ScanChildren() WARN_UNUSED_RESULT; |
| 121 | 121 |
| 122 // Return true if child with type |child.BoxType()| exists. | 122 // Return true if child with type |child.BoxType()| exists. |
| 123 bool HasChild(Box* child) WARN_UNUSED_RESULT; | 123 bool HasChild(Box* child) WARN_UNUSED_RESULT; |
| 124 | 124 |
| 125 // Read exactly one child box from the set of children. The type of the child | 125 // Read exactly one child box from the set of children. The type of the child |
| 126 // will be determined by the BoxType() method of |child|. | 126 // will be determined by the BoxType() method of |child|. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 155 | 155 |
| 156 // Populate the values of 'version()' and 'flags()' from a full box header. | 156 // Populate the values of 'version()' and 'flags()' from a full box header. |
| 157 // Many boxes, but not all, use these values. This call should happen after | 157 // Many boxes, but not all, use these values. This call should happen after |
| 158 // the box has been initialized, and does not re-read the main box header. | 158 // the box has been initialized, and does not re-read the main box header. |
| 159 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; | 159 bool ReadFullBoxHeader() WARN_UNUSED_RESULT; |
| 160 | 160 |
| 161 FourCC type() const { return type_; } | 161 FourCC type() const { return type_; } |
| 162 uint8 version() const { return version_; } | 162 uint8 version() const { return version_; } |
| 163 uint32 flags() const { return flags_; } | 163 uint32 flags() const { return flags_; } |
| 164 | 164 |
| 165 const LogCB& log_cb() const { return log_cb_; } | 165 const scoped_refptr<MediaLog>& media_log() const { return media_log_; } |
| 166 | 166 |
| 167 private: | 167 private: |
| 168 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is | 168 // Create a BoxReader from |buf|. |is_EOS| should be true if |buf| is |
| 169 // complete stream (i.e. no additional data is expected to be appended). | 169 // complete stream (i.e. no additional data is expected to be appended). |
| 170 BoxReader(const uint8* buf, const int size, const LogCB& log_cb, bool is_EOS); | 170 BoxReader(const uint8* buf, |
| 171 const int size, |
| 172 const scoped_refptr<MediaLog>& media_log, |
| 173 bool is_EOS); |
| 171 | 174 |
| 172 // Must be called immediately after init. If the return is false, this | 175 // Must be called immediately after init. If the return is false, this |
| 173 // indicates that the box header and its contents were not available in the | 176 // indicates that the box header and its contents were not available in the |
| 174 // stream or were nonsensical, and that the box must not be used further. In | 177 // stream or were nonsensical, and that the box must not be used further. In |
| 175 // this case, if |*err| is false, the problem was simply a lack of data, and | 178 // this case, if |*err| is false, the problem was simply a lack of data, and |
| 176 // should only be an error condition if some higher-level component knows that | 179 // should only be an error condition if some higher-level component knows that |
| 177 // no more data is coming (i.e. EOS or end of containing box). If |*err| is | 180 // no more data is coming (i.e. EOS or end of containing box). If |*err| is |
| 178 // true, the error is unrecoverable and the stream should be aborted. | 181 // true, the error is unrecoverable and the stream should be aborted. |
| 179 bool ReadHeader(bool* err); | 182 bool ReadHeader(bool* err); |
| 180 | 183 |
| 181 // Read all children, optionally checking FourCC. Returns true if all | 184 // Read all children, optionally checking FourCC. Returns true if all |
| 182 // children are successfully parsed and, if |check_box_type|, have the | 185 // children are successfully parsed and, if |check_box_type|, have the |
| 183 // correct box type for |T|. Note that this method is mutually exclusive | 186 // correct box type for |T|. Note that this method is mutually exclusive |
| 184 // with ScanChildren(). | 187 // with ScanChildren(). |
| 185 template <typename T> | 188 template <typename T> |
| 186 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type); | 189 bool ReadAllChildrenInternal(std::vector<T>* children, bool check_box_type); |
| 187 | 190 |
| 188 LogCB log_cb_; | 191 scoped_refptr<MediaLog> media_log_; |
| 189 FourCC type_; | 192 FourCC type_; |
| 190 uint8 version_; | 193 uint8 version_; |
| 191 uint32 flags_; | 194 uint32 flags_; |
| 192 | 195 |
| 193 typedef std::multimap<FourCC, BoxReader> ChildMap; | 196 typedef std::multimap<FourCC, BoxReader> ChildMap; |
| 194 | 197 |
| 195 // The set of child box FourCCs and their corresponding buffer readers. Only | 198 // The set of child box FourCCs and their corresponding buffer readers. Only |
| 196 // valid if scanned_ is true. | 199 // valid if scanned_ is true. |
| 197 ChildMap children_; | 200 ChildMap children_; |
| 198 bool scanned_; | 201 bool scanned_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 244 } |
| 242 | 245 |
| 243 template <typename T> | 246 template <typename T> |
| 244 bool BoxReader::ReadAllChildrenInternal(std::vector<T>* children, | 247 bool BoxReader::ReadAllChildrenInternal(std::vector<T>* children, |
| 245 bool check_box_type) { | 248 bool check_box_type) { |
| 246 DCHECK(!scanned_); | 249 DCHECK(!scanned_); |
| 247 scanned_ = true; | 250 scanned_ = true; |
| 248 | 251 |
| 249 bool err = false; | 252 bool err = false; |
| 250 while (pos_ < size_) { | 253 while (pos_ < size_) { |
| 251 BoxReader child_reader(&buf_[pos_], size_ - pos_, log_cb_, is_EOS_); | 254 BoxReader child_reader(&buf_[pos_], size_ - pos_, media_log_, is_EOS_); |
| 252 if (!child_reader.ReadHeader(&err)) break; | 255 if (!child_reader.ReadHeader(&err)) break; |
| 253 T child; | 256 T child; |
| 254 RCHECK(!check_box_type || child_reader.type() == child.BoxType()); | 257 RCHECK(!check_box_type || child_reader.type() == child.BoxType()); |
| 255 RCHECK(child.Parse(&child_reader)); | 258 RCHECK(child.Parse(&child_reader)); |
| 256 children->push_back(child); | 259 children->push_back(child); |
| 257 pos_ += child_reader.size(); | 260 pos_ += child_reader.size(); |
| 258 } | 261 } |
| 259 | 262 |
| 260 return !err; | 263 return !err; |
| 261 } | 264 } |
| 262 | 265 |
| 263 } // namespace mp4 | 266 } // namespace mp4 |
| 264 } // namespace media | 267 } // namespace media |
| 265 | 268 |
| 266 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_ | 269 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_ |
| OLD | NEW |