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 #include "media/base/bit_reader_core.h" | 5 #include "media/base/bit_reader_core.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 const int kRegWidthInBits = sizeof(uint64) * 8; | 12 const int kRegWidthInBits = sizeof(uint64_t) * 8; |
13 } | 13 } |
14 | 14 |
15 namespace media { | 15 namespace media { |
16 | 16 |
17 BitReaderCore::ByteStreamProvider::ByteStreamProvider() { | 17 BitReaderCore::ByteStreamProvider::ByteStreamProvider() { |
18 } | 18 } |
19 | 19 |
20 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() { | 20 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() { |
21 } | 21 } |
22 | 22 |
(...skipping 13 matching lines...) Expand all Loading... |
36 if (nbits_ == 0 && !Refill(1)) | 36 if (nbits_ == 0 && !Refill(1)) |
37 return false; | 37 return false; |
38 | 38 |
39 *flag = (reg_ & (UINT64_C(1) << (kRegWidthInBits - 1))) != 0; | 39 *flag = (reg_ & (UINT64_C(1) << (kRegWidthInBits - 1))) != 0; |
40 reg_ <<= 1; | 40 reg_ <<= 1; |
41 nbits_--; | 41 nbits_--; |
42 bits_read_++; | 42 bits_read_++; |
43 return true; | 43 return true; |
44 } | 44 } |
45 | 45 |
46 int BitReaderCore::PeekBitsMsbAligned(int num_bits, uint64* out) { | 46 int BitReaderCore::PeekBitsMsbAligned(int num_bits, uint64_t* out) { |
47 // Try to have at least |num_bits| in the bit register. | 47 // Try to have at least |num_bits| in the bit register. |
48 if (nbits_ < num_bits) | 48 if (nbits_ < num_bits) |
49 Refill(num_bits); | 49 Refill(num_bits); |
50 | 50 |
51 *out = reg_; | 51 *out = reg_; |
52 return nbits_; | 52 return nbits_; |
53 } | 53 } |
54 | 54 |
55 bool BitReaderCore::SkipBitsSmall(int num_bits) { | 55 bool BitReaderCore::SkipBitsSmall(int num_bits) { |
56 DCHECK_GE(num_bits, 0); | 56 DCHECK_GE(num_bits, 0); |
57 uint64 dummy; | 57 uint64_t dummy; |
58 while (num_bits >= kRegWidthInBits) { | 58 while (num_bits >= kRegWidthInBits) { |
59 if (!ReadBitsInternal(kRegWidthInBits, &dummy)) | 59 if (!ReadBitsInternal(kRegWidthInBits, &dummy)) |
60 return false; | 60 return false; |
61 num_bits -= kRegWidthInBits; | 61 num_bits -= kRegWidthInBits; |
62 } | 62 } |
63 return ReadBitsInternal(num_bits, &dummy); | 63 return ReadBitsInternal(num_bits, &dummy); |
64 } | 64 } |
65 | 65 |
66 bool BitReaderCore::SkipBits(int num_bits) { | 66 bool BitReaderCore::SkipBits(int num_bits) { |
67 DCHECK_GE(num_bits, 0); | 67 DCHECK_GE(num_bits, 0); |
68 | 68 |
69 const int remaining_bits = nbits_ + nbits_next_; | 69 const int remaining_bits = nbits_ + nbits_next_; |
70 if (remaining_bits >= num_bits) | 70 if (remaining_bits >= num_bits) |
71 return SkipBitsSmall(num_bits); | 71 return SkipBitsSmall(num_bits); |
72 | 72 |
73 // Skip first the remaining available bits. | 73 // Skip first the remaining available bits. |
74 num_bits -= remaining_bits; | 74 num_bits -= remaining_bits; |
75 bits_read_ += remaining_bits; | 75 bits_read_ += remaining_bits; |
76 nbits_ = 0; | 76 nbits_ = 0; |
77 reg_ = 0; | 77 reg_ = 0; |
78 nbits_next_ = 0; | 78 nbits_next_ = 0; |
79 reg_next_ = 0; | 79 reg_next_ = 0; |
80 | 80 |
81 // Next, skip an integer number of bytes. | 81 // Next, skip an integer number of bytes. |
82 const int nbytes = num_bits / 8; | 82 const int nbytes = num_bits / 8; |
83 if (nbytes > 0) { | 83 if (nbytes > 0) { |
84 const uint8* byte_stream_window; | 84 const uint8_t* byte_stream_window; |
85 const int window_size = | 85 const int window_size = |
86 byte_stream_provider_->GetBytes(nbytes, &byte_stream_window); | 86 byte_stream_provider_->GetBytes(nbytes, &byte_stream_window); |
87 DCHECK_GE(window_size, 0); | 87 DCHECK_GE(window_size, 0); |
88 DCHECK_LE(window_size, nbytes); | 88 DCHECK_LE(window_size, nbytes); |
89 if (window_size < nbytes) | 89 if (window_size < nbytes) |
90 return false; | 90 return false; |
91 num_bits -= 8 * nbytes; | 91 num_bits -= 8 * nbytes; |
92 bits_read_ += 8 * nbytes; | 92 bits_read_ += 8 * nbytes; |
93 } | 93 } |
94 | 94 |
95 // Skip the remaining bits. | 95 // Skip the remaining bits. |
96 return SkipBitsSmall(num_bits); | 96 return SkipBitsSmall(num_bits); |
97 } | 97 } |
98 | 98 |
99 int BitReaderCore::bits_read() const { | 99 int BitReaderCore::bits_read() const { |
100 return bits_read_; | 100 return bits_read_; |
101 } | 101 } |
102 | 102 |
103 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64* out) { | 103 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64_t* out) { |
104 DCHECK_GE(num_bits, 0); | 104 DCHECK_GE(num_bits, 0); |
105 | 105 |
106 if (num_bits == 0) { | 106 if (num_bits == 0) { |
107 *out = 0; | 107 *out = 0; |
108 return true; | 108 return true; |
109 } | 109 } |
110 | 110 |
111 if (num_bits > nbits_ && !Refill(num_bits)) { | 111 if (num_bits > nbits_ && !Refill(num_bits)) { |
112 // Any subsequent ReadBits should fail: | 112 // Any subsequent ReadBits should fail: |
113 // empty the current bit register for that purpose. | 113 // empty the current bit register for that purpose. |
(...skipping 26 matching lines...) Expand all Loading... |
140 RefillCurrentRegister(); | 140 RefillCurrentRegister(); |
141 if (min_nbits <= nbits_) | 141 if (min_nbits <= nbits_) |
142 return true; | 142 return true; |
143 DCHECK_EQ(nbits_next_, 0); | 143 DCHECK_EQ(nbits_next_, 0); |
144 DCHECK_EQ(reg_next_, 0u); | 144 DCHECK_EQ(reg_next_, 0u); |
145 | 145 |
146 // Max number of bytes to refill. | 146 // Max number of bytes to refill. |
147 int max_nbytes = sizeof(reg_next_); | 147 int max_nbytes = sizeof(reg_next_); |
148 | 148 |
149 // Refill. | 149 // Refill. |
150 const uint8* byte_stream_window; | 150 const uint8_t* byte_stream_window; |
151 int window_size = | 151 int window_size = |
152 byte_stream_provider_->GetBytes(max_nbytes, &byte_stream_window); | 152 byte_stream_provider_->GetBytes(max_nbytes, &byte_stream_window); |
153 DCHECK_GE(window_size, 0); | 153 DCHECK_GE(window_size, 0); |
154 DCHECK_LE(window_size, max_nbytes); | 154 DCHECK_LE(window_size, max_nbytes); |
155 if (window_size == 0) | 155 if (window_size == 0) |
156 return false; | 156 return false; |
157 | 157 |
158 reg_next_ = 0; | 158 reg_next_ = 0; |
159 memcpy(®_next_, byte_stream_window, window_size); | 159 memcpy(®_next_, byte_stream_window, window_size); |
160 reg_next_ = base::NetToHost64(reg_next_); | 160 reg_next_ = base::NetToHost64(reg_next_); |
(...skipping 20 matching lines...) Expand all Loading... |
181 nbits_next_ = 0; | 181 nbits_next_ = 0; |
182 return; | 182 return; |
183 } | 183 } |
184 | 184 |
185 nbits_ += free_nbits; | 185 nbits_ += free_nbits; |
186 reg_next_ <<= free_nbits; | 186 reg_next_ <<= free_nbits; |
187 nbits_next_ -= free_nbits; | 187 nbits_next_ -= free_nbits; |
188 } | 188 } |
189 | 189 |
190 } // namespace media | 190 } // namespace media |
OLD | NEW |