Index: media/base/bit_reader_h264.cc |
diff --git a/media/base/bit_reader_h264.cc b/media/base/bit_reader_h264.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..84292afd5d18dd094f23b788538c27aa7b54916b |
--- /dev/null |
+++ b/media/base/bit_reader_h264.cc |
@@ -0,0 +1,97 @@ |
+// Copyright (c) 2013 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/base/bit_reader_h264.h" |
+ |
+namespace media { |
+ |
+namespace { |
+ |
+class H264ByteStreamProvider : public BitReaderCore::ByteStreamProvider { |
+ public: |
+ H264ByteStreamProvider(const uint8* data, off_t size); |
+ virtual ~H264ByteStreamProvider(); |
+ |
+ // BitReaderCore::ByteStreamProvider implementation. |
+ virtual int GetBytes(int min_n, |
+ int max_n, |
+ const uint8** out) OVERRIDE; |
+ virtual int GetBytesLeft() const OVERRIDE; |
+ |
+ private: |
+ // Pointer to the next unread byte in the stream. |
+ // Does not include bits held in the bit registers. |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: Remove this comment since this code shouldn't
damienv1
2013/12/28 02:26:17
Will do.
|
+ const uint8* data_; |
+ |
+ // Bytes left in the stream. |
+ // Does not include bits held in the bit registers. |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
ditto?
damienv1
2013/12/28 02:26:17
Will do.
|
+ int bytes_left_; |
+ |
+ // Array used to return some bytes when emulation prevention bytes |
damienv1
2013/12/28 01:32:25
when *some* ...
damienv1
2013/12/28 02:26:17
Done.
|
+ // are detected. |
+ uint8 data_window_[8]; |
+ |
+ // Last two bytes read from the stream. |
+ int prev_two_bytes_; |
+}; |
+ |
+H264ByteStreamProvider::H264ByteStreamProvider(const uint8* data, off_t size) |
+ : data_(data), |
+ bytes_left_(size), |
+ prev_two_bytes_(0xffff) { |
+ DCHECK(data_ != NULL && bytes_left_ > 0); |
+} |
+ |
+H264ByteStreamProvider::~H264ByteStreamProvider() { |
+} |
+ |
+int H264ByteStreamProvider::GetBytes( |
+ int min_nbytes, int max_nbytes, const uint8** out) { |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit: This implementation and the one used by BitRe
damienv1
2013/12/28 02:26:17
Should be able to remove it. Didn't do it right aw
|
+ DCHECK_LE(max_nbytes, static_cast<int>(sizeof(data_window_))); |
+ |
+ const uint8* start = data_; |
+ bool epb = false; |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
nit:s/epb/copy_to_data_window/ or something simila
damienv1
2013/12/28 04:02:23
Done.
|
+ |
+ int nbytes = 0; |
+ *out = data_; |
+ |
+ while (nbytes < max_nbytes && bytes_left_ > 0) { |
+ // Emulation prevention three-byte detection. |
+ // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03) |
+ // and starts copying data to |data_window_| which is used as the output. |
+ if (*data_ == 0x3 && (prev_two_bytes_ & 0xffff) == 0) { |
+ if (!epb && nbytes > 0) |
+ memcpy(data_window_, start, nbytes); |
+ epb = true; |
+ *out = data_window_; |
+ // Need another full three bytes before we can detect the sequence again. |
+ prev_two_bytes_ = 0xffff; |
+ } else { |
+ if (epb) |
+ data_window_[nbytes] = *data_; |
+ prev_two_bytes_ = (prev_two_bytes_ << 8) | static_cast<int>(*data_); |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
If you make prev_two_bytes_ unsigned does it remov
damienv1
2013/12/28 02:26:17
Will check.
|
+ ++nbytes; |
+ } |
+ |
+ ++data_; |
+ --bytes_left_; |
+ } |
+ |
+ return nbytes; |
+} |
+ |
+int H264ByteStreamProvider::GetBytesLeft() const { |
+ return bytes_left_; |
acolwell GONE FROM CHROMIUM
2013/12/28 01:54:42
This doesn't seem right. Shouldn't this be returni
damienv1
2013/12/28 02:26:17
I implemented this one to be inline with the curre
|
+} |
+ |
+} // namespace |
+ |
+BitReaderH264::BitReaderH264(const uint8* data, off_t size) |
+ : byte_stream_provider_(new H264ByteStreamProvider(data, size)), |
+ bit_reader_core_(byte_stream_provider_.get()) { |
+} |
+ |
+BitReaderH264::~BitReaderH264() {} |
+ |
+} // namespace media |