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
3 // found in the LICENSE file.
5 #include "media/base/bit_reader_core.h"
8 #include <base/sys_byteorder.h>
11 const int kRegWidthInBits
= sizeof(uint64
) * 8;
16 BitReaderCore::ByteStreamProvider::ByteStreamProvider() {
19 BitReaderCore::ByteStreamProvider::~ByteStreamProvider() {
22 BitReaderCore::BitReaderCore(ByteStreamProvider
* byte_stream_provider
)
23 : byte_stream_provider_(byte_stream_provider
),
31 BitReaderCore::~BitReaderCore() {
34 bool BitReaderCore::ReadFlag(bool* flag
) {
35 if (nbits_
== 0 && !Refill(1))
38 *flag
= (reg_
& (GG_UINT64_C(1) << (kRegWidthInBits
- 1))) != 0;
45 int BitReaderCore::PeekBitsMsbAligned(int num_bits
, uint64
* out
) {
46 // Try to have at least |num_bits| in the bit register.
47 if (nbits_
< num_bits
)
54 bool BitReaderCore::SkipBitsSmall(int num_bits
) {
55 DCHECK_GE(num_bits
, 0);
57 while (num_bits
>= kRegWidthInBits
) {
58 if (!ReadBitsInternal(kRegWidthInBits
, &dummy
))
60 num_bits
-= kRegWidthInBits
;
62 return ReadBitsInternal(num_bits
, &dummy
);
65 bool BitReaderCore::SkipBits(int num_bits
) {
66 DCHECK_GE(num_bits
, 0);
68 const int remaining_bits
= nbits_
+ nbits_next_
;
69 if (remaining_bits
>= num_bits
)
70 return SkipBitsSmall(num_bits
);
72 // Skip first the remaining available bits.
73 num_bits
-= remaining_bits
;
74 bits_read_
+= remaining_bits
;
80 // Next, skip an integer number of bytes.
81 const int nbytes
= num_bits
/ 8;
83 const uint8
* byte_stream_window
;
84 const int window_size
=
85 byte_stream_provider_
->GetBytes(nbytes
, &byte_stream_window
);
86 DCHECK_GE(window_size
, 0);
87 DCHECK_LE(window_size
, nbytes
);
88 if (window_size
< nbytes
)
90 num_bits
-= 8 * nbytes
;
91 bits_read_
+= 8 * nbytes
;
94 // Skip the remaining bits.
95 return SkipBitsSmall(num_bits
);
98 int BitReaderCore::bits_read() const {
102 bool BitReaderCore::ReadBitsInternal(int num_bits
, uint64
* out
) {
103 DCHECK_GE(num_bits
, 0);
110 if (num_bits
> nbits_
&& !Refill(num_bits
)) {
111 // Any subsequent ReadBits should fail:
112 // empty the current bit register for that purpose.
118 bits_read_
+= num_bits
;
120 if (num_bits
== kRegWidthInBits
) {
121 // Special case needed since for example for a 64 bit integer "a"
122 // "a << 64" is not defined by the C/C++ standard.
129 *out
= reg_
>> (kRegWidthInBits
- num_bits
);
135 bool BitReaderCore::Refill(int min_nbits
) {
136 DCHECK_LE(min_nbits
, kRegWidthInBits
);
138 // Transfer from the next to the current register.
139 RefillCurrentRegister();
140 if (min_nbits
<= nbits_
)
142 DCHECK_EQ(nbits_next_
, 0);
143 DCHECK_EQ(reg_next_
, 0u);
145 // Max number of bytes to refill.
146 int max_nbytes
= sizeof(reg_next_
);
149 const uint8
* byte_stream_window
;
151 byte_stream_provider_
->GetBytes(max_nbytes
, &byte_stream_window
);
152 DCHECK_GE(window_size
, 0);
153 DCHECK_LE(window_size
, max_nbytes
);
154 if (window_size
== 0)
158 memcpy(®_next_
, byte_stream_window
, window_size
);
159 reg_next_
= base::NetToHost64(reg_next_
);
160 nbits_next_
= window_size
* 8;
162 // Transfer from the next to the current register.
163 RefillCurrentRegister();
165 return (nbits_
>= min_nbits
);
168 void BitReaderCore::RefillCurrentRegister() {
169 // No refill possible if the destination register is full
170 // or the source register is empty.
171 if (nbits_
== kRegWidthInBits
|| nbits_next_
== 0)
174 reg_
|= (reg_next_
>> nbits_
);
176 int free_nbits
= kRegWidthInBits
- nbits_
;
177 if (free_nbits
>= nbits_next_
) {
178 nbits_
+= nbits_next_
;
184 nbits_
+= free_nbits
;
185 reg_next_
<<= free_nbits
;
186 nbits_next_
-= free_nbits
;