OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
s/2013/2014
damienv1
2014/01/13 22:41:06
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/bit_reader_core.h" | |
6 | |
7 #include <base/sys_byteorder.h> | |
8 | |
9 namespace { | |
10 const int kRegWidthInBits = sizeof(uint64) * 8; | |
11 } | |
12 | |
13 namespace media { | |
14 | |
15 BitReaderCore::ByteStreamProvider::ByteStreamProvider() { | |
16 } | |
17 | |
18 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() { | |
19 } | |
20 | |
21 BitReaderCore::BitReaderCore(ByteStreamProvider* byte_stream_provider) | |
22 : byte_stream_provider_(byte_stream_provider), | |
23 bits_read_(0), | |
24 nbits_(0), | |
25 reg_(0), | |
26 nbits_next_(0), | |
27 reg_next_(0) { | |
28 } | |
29 | |
30 BitReaderCore::~BitReaderCore() { | |
31 } | |
32 | |
33 bool BitReaderCore::ReadFlag(bool* flag) { | |
34 if (nbits_ == 0 && !Refill(1)) | |
35 return false; | |
36 | |
37 *flag = (reg_ & (UINT64_C(1) << (kRegWidthInBits - 1))) != 0; | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: I believe this should be GG_UINT64_C based on
damienv1
2014/01/13 22:41:06
Thanks, that's correct (I forgot Chromium did port
| |
38 reg_ <<= 1; | |
39 nbits_--; | |
40 bits_read_++; | |
41 return true; | |
42 } | |
43 | |
44 int BitReaderCore::PeekBitsMsbAligned(int num_bits, uint64* out) { | |
45 // Try to have at least |num_bits| in the bit register. | |
46 if (nbits_ < num_bits) | |
47 Refill(num_bits); | |
48 | |
49 *out = reg_; | |
50 return nbits_; | |
51 } | |
52 | |
53 bool BitReaderCore::SkipBits(int num_bits) { | |
54 DCHECK_GE(num_bits, 0); | |
55 DVLOG_IF(0, num_bits > 100) | |
56 << "BitReader::SkipBits inefficient for large skips"; | |
57 | |
58 uint64 dummy; | |
59 while (num_bits >= kRegWidthInBits) { | |
60 if (!ReadBitsInternal(kRegWidthInBits, &dummy)) | |
Pawel Osciak
2014/01/11 02:30:49
What do you think of instead:
if (num_bits >= kReg
damienv1
2014/01/13 22:41:06
I agree this is more efficient.
However, I would r
| |
61 return false; | |
62 num_bits -= kRegWidthInBits; | |
63 } | |
64 return ReadBitsInternal(num_bits, &dummy); | |
65 } | |
66 | |
67 int BitReaderCore::bits_read() const { | |
68 return bits_read_; | |
69 } | |
70 | |
71 int BitReaderCore::bits_buffered() const { | |
72 return nbits_next_ + nbits_; | |
73 } | |
74 | |
75 bool BitReaderCore::ReadBitsInternal(int num_bits, uint64* out) { | |
76 DCHECK_GE(num_bits, 0); | |
77 | |
78 if (num_bits == 0) { | |
79 *out = 0; | |
80 return true; | |
81 } | |
82 | |
83 if (num_bits > nbits_ && !Refill(num_bits)) { | |
84 // Any subsequent ReadBits should fail: | |
85 // empty the current bit register for that purpose. | |
86 nbits_ = 0; | |
87 reg_ = 0; | |
88 return false; | |
89 } | |
90 | |
91 bits_read_ += num_bits; | |
92 | |
93 if (num_bits == kRegWidthInBits) { | |
94 // Special case needed since for example for a 64 bit integer "a" | |
95 // "a << 64" is not defined by the C/C++ standard. | |
96 *out = reg_; | |
97 reg_ = 0; | |
98 nbits_ = 0; | |
99 return true; | |
100 } | |
101 | |
102 *out = reg_ >> (kRegWidthInBits - num_bits); | |
103 reg_ <<= num_bits; | |
104 nbits_ -= num_bits; | |
105 return true; | |
106 } | |
107 | |
108 bool BitReaderCore::Refill(int min_nbits) { | |
109 DCHECK_LE(min_nbits, kRegWidthInBits); | |
110 | |
111 // Transfer from the next to the current register. | |
112 RefillCurrentRegister(); | |
113 if (min_nbits <= nbits_) | |
114 return true; | |
115 DCHECK_EQ(nbits_next_, 0); | |
116 DCHECK_EQ(reg_next_, 0u); | |
117 | |
118 // Max number of bytes to refill. | |
119 int max_nbytes = sizeof(reg_next_); | |
120 | |
121 // Refill. | |
122 const uint8* byte_stream_window; | |
123 int window_size = | |
124 byte_stream_provider_->GetBytes(max_nbytes, &byte_stream_window); | |
125 if (window_size == 0) | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
Add the following before this line just to allow u
damienv1
2014/01/13 22:41:06
Done.
| |
126 return false; | |
127 | |
128 reg_next_ = 0; | |
129 memcpy(®_next_, byte_stream_window, window_size); | |
130 reg_next_ = base::NetToHost64(reg_next_); | |
131 nbits_next_ = window_size * 8; | |
132 | |
133 // Transfer from the next to the current register. | |
134 RefillCurrentRegister(); | |
135 | |
136 return (nbits_ >= min_nbits); | |
137 } | |
138 | |
139 void BitReaderCore::RefillCurrentRegister() { | |
140 // No refill possible if the destination register is full | |
141 // or the source register is empty. | |
142 if (nbits_ == kRegWidthInBits || nbits_next_ == 0) | |
143 return; | |
144 | |
145 reg_ |= (reg_next_ >> nbits_); | |
146 | |
147 int free_nbits = kRegWidthInBits - nbits_; | |
148 if (free_nbits >= nbits_next_) { | |
149 nbits_ += nbits_next_; | |
150 reg_next_ = 0; | |
151 nbits_next_ = 0; | |
152 } else { | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: early return and drop else.
damienv1
2014/01/13 22:41:06
Done.
| |
153 nbits_ += free_nbits; | |
154 reg_next_ <<= free_nbits; | |
Pawel Osciak
2014/01/11 02:30:49
Would this work instead of l:145-156?
free_nbits
damienv1
2014/01/13 22:41:06
If |bits_to_refill| is equal to 64, then it does n
| |
155 nbits_next_ -= free_nbits; | |
156 } | |
157 } | |
158 | |
159 } // namespace media | |
OLD | NEW |