Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "media/base/bit_reader.h" | 5 #include "media/base/bit_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 9 namespace media { | 7 namespace media { |
| 10 | 8 |
| 11 BitReader::BitReader(const uint8* data, off_t size) | 9 namespace { |
| 12 : data_(data), bytes_left_(size), num_remaining_bits_in_curr_byte_(0) { | 10 |
| 11 class LinearByteStreamProvider : public BitReaderCore::ByteStreamProvider { | |
| 12 public: | |
| 13 LinearByteStreamProvider(const uint8* data, off_t size); | |
| 14 virtual ~LinearByteStreamProvider(); | |
| 15 | |
| 16 // BitReaderCore::ByteStreamProvider implementation. | |
| 17 virtual int GetBytes(int min_n, | |
| 18 int max_n, | |
| 19 const uint8** out) OVERRIDE; | |
| 20 virtual int GetBytesLeft() const OVERRIDE; | |
| 21 | |
| 22 private: | |
| 23 // Pointer to the next unread byte in the stream. | |
| 24 // Does not include bits held in the bit registers. | |
| 25 const uint8* data_; | |
| 26 | |
| 27 // Bytes left in the stream. | |
| 28 // Does not include bits held in the bit registers. | |
| 29 int bytes_left_; | |
| 30 }; | |
| 31 | |
| 32 LinearByteStreamProvider::LinearByteStreamProvider( | |
| 33 const uint8* data, off_t size) | |
| 34 : data_(data), | |
| 35 bytes_left_(size) { | |
| 13 DCHECK(data_ != NULL && bytes_left_ > 0); | 36 DCHECK(data_ != NULL && bytes_left_ > 0); |
| 37 } | |
| 14 | 38 |
| 15 UpdateCurrByte(); | 39 LinearByteStreamProvider::~LinearByteStreamProvider() { |
| 40 } | |
| 41 | |
| 42 int LinearByteStreamProvider::GetBytes( | |
| 43 int min_nbytes, int max_nbytes, const uint8** out) { | |
| 44 int nbytes = max_nbytes; | |
| 45 if (nbytes > bytes_left_) | |
| 46 nbytes = bytes_left_; | |
| 47 | |
| 48 *out = data_; | |
| 49 data_ += nbytes; | |
| 50 bytes_left_ -= nbytes; | |
| 51 return nbytes; | |
| 52 } | |
| 53 | |
| 54 int LinearByteStreamProvider::GetBytesLeft() const { | |
| 55 return bytes_left_; | |
| 56 } | |
| 57 | |
| 58 class H264ByteStreamProvider : public BitReaderCore::ByteStreamProvider { | |
|
acolwell GONE FROM CHROMIUM
2013/12/27 20:35:21
This should not be here. It should be with the H.2
damienv1
2013/12/27 21:23:20
Will do.
| |
| 59 public: | |
| 60 H264ByteStreamProvider(const uint8* data, int size); | |
| 61 virtual ~H264ByteStreamProvider(); | |
| 62 | |
| 63 // BitReaderCore::ByteStreamProvider implementation. | |
| 64 virtual int GetBytes(int min_n, | |
| 65 int max_n, | |
| 66 const uint8** out) OVERRIDE; | |
| 67 virtual int GetBytesLeft() const OVERRIDE; | |
| 68 | |
| 69 private: | |
| 70 // Pointer to the next unread byte in the stream. | |
| 71 // Does not include bits held in the bit registers. | |
| 72 const uint8* data_; | |
| 73 | |
| 74 // Bytes left in the stream. | |
| 75 // Does not include bits held in the bit registers. | |
| 76 int bytes_left_; | |
| 77 | |
| 78 // Array used to return some bytes when emulation prevention bytes | |
| 79 // are detected. | |
| 80 uint8 data_window_[8]; | |
| 81 | |
| 82 // Last two bytes read from the stream. | |
| 83 int prev_two_bytes_; | |
| 84 }; | |
| 85 | |
| 86 H264ByteStreamProvider::H264ByteStreamProvider(const uint8* data, int size) | |
| 87 : data_(data), | |
| 88 bytes_left_(size), | |
| 89 prev_two_bytes_(0xffff) { | |
| 90 DCHECK(data_ != NULL && bytes_left_ > 0); | |
| 91 } | |
| 92 | |
| 93 H264ByteStreamProvider::~H264ByteStreamProvider() { | |
| 94 } | |
| 95 | |
| 96 int H264ByteStreamProvider::GetBytes( | |
| 97 int min_nbytes, int max_nbytes, const uint8** out) { | |
| 98 DCHECK_LE(max_nbytes, static_cast<int>(sizeof(data_window_))); | |
| 99 | |
| 100 const uint8* start = data_; | |
| 101 bool epb = false; | |
| 102 | |
| 103 int nbytes = 0; | |
| 104 *out = data_; | |
| 105 | |
| 106 while (nbytes < max_nbytes && bytes_left_ > 0) { | |
| 107 // Emulation prevention three-byte detection. | |
| 108 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03) | |
| 109 // and starts copying data to |data_window_| which is used as the output. | |
| 110 if (*data_ == 0x3 && (prev_two_bytes_ & 0xffff) == 0) { | |
| 111 if (!epb && nbytes > 0) | |
| 112 memcpy(data_window_, start, nbytes); | |
| 113 epb = true; | |
| 114 *out = data_window_; | |
| 115 // Need another full three bytes before we can detect the sequence again. | |
| 116 prev_two_bytes_ = 0xffff; | |
| 117 continue; | |
| 118 } else { | |
| 119 if (epb) | |
| 120 data_window_[nbytes] = *data_; | |
| 121 ++nbytes; | |
| 122 } | |
| 123 | |
| 124 ++data_; | |
| 125 --bytes_left_; | |
| 126 } | |
| 127 | |
| 128 return nbytes; | |
| 129 } | |
| 130 | |
| 131 int H264ByteStreamProvider::GetBytesLeft() const { | |
| 132 return bytes_left_; | |
| 133 } | |
| 134 | |
| 135 } // namespace | |
| 136 | |
| 137 BitReader::BitReader( | |
| 138 const uint8* data, off_t size, bool use_h264_byte_provider) | |
| 139 : byte_stream_provider_( | |
| 140 use_h264_byte_provider ? | |
| 141 static_cast<BitReaderCore::ByteStreamProvider*>( | |
| 142 new H264ByteStreamProvider(data, size)) : | |
| 143 static_cast<BitReaderCore::ByteStreamProvider*>( | |
| 144 new LinearByteStreamProvider(data, size))), | |
| 145 bit_reader_core_(byte_stream_provider_.get()) { | |
| 16 } | 146 } |
| 17 | 147 |
| 18 BitReader::~BitReader() {} | 148 BitReader::~BitReader() {} |
| 19 | 149 |
| 20 bool BitReader::SkipBits(int num_bits) { | |
| 21 DCHECK_GE(num_bits, 0); | |
| 22 DVLOG_IF(0, num_bits > 100) | |
| 23 << "BitReader::SkipBits inefficient for large skips"; | |
| 24 | |
| 25 // Skip any bits in the current byte waiting to be processed, then | |
| 26 // process full bytes until less than 8 bits remaining. | |
| 27 while (num_bits > 0 && num_bits > num_remaining_bits_in_curr_byte_) { | |
| 28 num_bits -= num_remaining_bits_in_curr_byte_; | |
| 29 num_remaining_bits_in_curr_byte_ = 0; | |
| 30 UpdateCurrByte(); | |
| 31 | |
| 32 // If there is no more data remaining, only return true if we | |
| 33 // skipped all that were requested. | |
| 34 if (num_remaining_bits_in_curr_byte_ == 0) | |
| 35 return (num_bits == 0); | |
| 36 } | |
| 37 | |
| 38 // Less than 8 bits remaining to skip. Use ReadBitsInternal to verify | |
| 39 // that the remaining bits we need exist, and adjust them as necessary | |
| 40 // for subsequent operations. | |
| 41 uint64 not_needed; | |
| 42 return ReadBitsInternal(num_bits, ¬_needed); | |
| 43 } | |
| 44 | |
| 45 int BitReader::bits_available() const { | 150 int BitReader::bits_available() const { |
| 46 return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_; | 151 return (byte_stream_provider_->GetBytesLeft() * CHAR_BIT + |
|
acolwell GONE FROM CHROMIUM
2013/12/27 20:35:21
Why not push this part of the computation down int
damienv1
2013/12/27 21:23:20
You're right. I initially didn't have GetBytesLeft
| |
| 47 } | 152 bit_reader_core_.bits_available()); |
| 48 | |
| 49 bool BitReader::ReadBitsInternal(int num_bits, uint64* out) { | |
| 50 DCHECK_LE(num_bits, 64); | |
| 51 | |
| 52 *out = 0; | |
| 53 | |
| 54 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
| 55 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
| 56 | |
| 57 *out <<= bits_to_take; | |
| 58 *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take); | |
| 59 num_bits -= bits_to_take; | |
| 60 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
| 61 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; | |
| 62 | |
| 63 if (num_remaining_bits_in_curr_byte_ == 0) | |
| 64 UpdateCurrByte(); | |
| 65 } | |
| 66 | |
| 67 return num_bits == 0; | |
| 68 } | |
| 69 | |
| 70 void BitReader::UpdateCurrByte() { | |
| 71 DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0); | |
| 72 | |
| 73 if (bytes_left_ == 0) | |
| 74 return; | |
| 75 | |
| 76 // Load a new byte and advance pointers. | |
| 77 curr_byte_ = *data_; | |
| 78 ++data_; | |
| 79 --bytes_left_; | |
| 80 num_remaining_bits_in_curr_byte_ = 8; | |
| 81 } | 153 } |
| 82 | 154 |
| 83 } // namespace media | 155 } // namespace media |
| OLD | NEW |