1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
use std::fmt::Debug;
use crate::{error::Error, util::tracing_wrappers::*};
use byteorder::{ByteOrder, LittleEndian};
/// Reads bits from a sequence of bytes.
#[derive(Clone)]
pub struct BitReader<'a> {
data: &'a [u8],
bit_buf: u64,
bits_in_buf: usize,
total_bits_read: usize,
initial_bits: usize,
}
impl Debug for BitReader<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"BitReader{{ data: [{} bytes], bit_buf: {:0width$b}, total_bits_read: {} }}",
self.data.len(),
self.bit_buf,
self.total_bits_read,
width = self.bits_in_buf
)
}
}
pub const MAX_BITS_PER_CALL: usize = 56;
impl<'a> BitReader<'a> {
/// Constructs a BitReader for a given range of data.
pub fn new(data: &[u8]) -> BitReader<'_> {
BitReader {
data,
bit_buf: 0,
bits_in_buf: 0,
total_bits_read: 0,
initial_bits: data.len() * 8,
}
}
/// Constructs a BitReader for padded data where `real_len` bytes are valid
/// content and the remaining bytes are zero padding.
///
/// The padding ensures `refill()` can always take the fast 8-byte read path
/// instead of falling back to byte-by-byte `refill_slow()`. The caller must
/// ensure `data.len() >= real_len + 8` (8 bytes of zero padding).
///
/// `initial_bits` is set from `real_len`, so `check_for_error()` and
/// `total_bits_available()` report the correct content length.
pub fn new_padded(data: &[u8], real_len: usize) -> Result<BitReader<'_>, Error> {
if real_len > 0 && data.len() < real_len.saturating_add(8) {
return Err(Error::SectionTooShort);
}
Ok(BitReader {
data,
bit_buf: 0,
bits_in_buf: 0,
total_bits_read: 0,
initial_bits: real_len * 8,
})
}
/// Reads `num` bits from the buffer without consuming them.
#[inline(always)]
pub fn peek(&mut self, num: usize) -> u64 {
debug_assert!(num <= MAX_BITS_PER_CALL);
if self.bits_in_buf < num {
self.refill();
}
self.bit_buf & ((1u64 << num) - 1)
}
/// Advances by `num` bits. Similar to `skip_bits`, but bits must be in the buffer.
pub fn consume(&mut self, num: usize) -> Result<(), Error> {
if self.bits_in_buf < num {
return Err(Error::OutOfBounds((num - self.bits_in_buf).div_ceil(8)));
}
self.bit_buf >>= num;
self.bits_in_buf -= num;
self.total_bits_read = self.total_bits_read.wrapping_add(num);
Ok(())
}
#[inline(always)]
pub fn consume_optimistic(&mut self, num: usize) {
self.bit_buf >>= num;
self.bits_in_buf = self.bits_in_buf.saturating_sub(num);
self.total_bits_read = self.total_bits_read.wrapping_add(num);
}
/// Reads `num` bits from the buffer.
/// ```ignore
/// # use crate::bit_reader::BitReader;
/// let mut br = BitReader::new(&[0, 1]);
/// assert_eq!(br.read(8)?, 0);
/// assert_eq!(br.read(4)?, 1);
/// assert_eq!(br.read(4)?, 0);
/// assert_eq!(br.total_bits_read(), 16);
/// assert!(br.read(1).is_err());
/// # Ok::<(), zenjxl_decoder::api::Error>(())
/// ```
#[inline]
pub fn read(&mut self, num: usize) -> Result<u64, Error> {
let ret = self.peek(num);
self.consume(num)?;
Ok(ret)
}
/// inline(never) wrapper around read, for cold code paths.
#[inline(never)]
pub fn read_noinline(&mut self, num: usize) -> Result<u64, Error> {
self.read(num)
}
#[inline(always)]
pub fn read_optimistic(&mut self, num: usize) -> u64 {
let ret = self.peek(num);
self.consume_optimistic(num);
ret
}
pub fn check_for_error(&self) -> Result<(), Error> {
if self.total_bits_read > self.initial_bits {
Err(Error::OutOfBounds(self.total_bits_read - self.initial_bits))
} else {
Ok(())
}
}
/// Returns the total number of bits that have been read or skipped.
pub fn total_bits_read(&self) -> usize {
self.total_bits_read
}
/// Returns the total number of bits that can still be read or skipped.
pub fn total_bits_available(&self) -> usize {
self.data.len() * 8 + self.bits_in_buf
}
///Â Skips `num` bits.
/// ```ignore
/// # use crate::bit_reader::BitReader;
/// let mut br = BitReader::new(&[0, 1]);
/// assert_eq!(br.read(8)?, 0);
/// br.skip_bits(4)?;
/// assert_eq!(br.total_bits_read(), 12);
/// # Ok::<(), zenjxl_decoder::api::Error>(())
/// ```
#[inline(never)]
pub fn skip_bits(&mut self, mut n: usize) -> Result<(), Error> {
// Check if we can skip within the current buffer
if let Some(next_remaining_bits) = self.bits_in_buf.checked_sub(n) {
self.total_bits_read += n;
self.bits_in_buf = next_remaining_bits;
self.bit_buf >>= n;
return Ok(());
}
// Adjust the number of bits to skip and reset the buffer
n -= self.bits_in_buf;
self.total_bits_read += self.bits_in_buf;
self.bit_buf = 0;
self.bits_in_buf = 0;
// Check if the remaining bits to skip exceed the total bits in `data`
let bits_available = self.data.len() * 8;
if n > bits_available {
self.total_bits_read += bits_available;
return Err(Error::OutOfBounds(n - bits_available));
}
// Skip bytes directly in `data`, then handle leftover bits
self.total_bits_read += n / 8 * 8;
self.data = &self.data[n / 8..];
n %= 8;
// Refill the buffer and adjust for any remaining bits
self.refill();
let to_consume = self.bits_in_buf.min(n);
// The bits loaded by refill() haven't been counted in total_bits_read yet,
// so we add (not subtract) the bits we're consuming. The original code
// incorrectly subtracted here, causing underflow when skip_bits was called
// on a fresh BitReader.
self.total_bits_read += to_consume;
n -= to_consume;
self.bit_buf >>= to_consume;
self.bits_in_buf -= to_consume;
if n > 0 {
Err(Error::OutOfBounds(n))
} else {
Ok(())
}
}
/// Return the number of bits
pub fn bits_to_next_byte(&self) -> usize {
let byte_boundary = self.total_bits_read.div_ceil(8) * 8;
byte_boundary - self.total_bits_read
}
/// Jumps to the next byte boundary. The skipped bytes have to be 0.
/// ```ignore
/// # use crate::bit_reader::BitReader;
/// let mut br = BitReader::new(&[0, 1]);
/// assert_eq!(br.read(8)?, 0);
/// br.skip_bits(4)?;
/// br.jump_to_byte_boundary()?;
/// assert_eq!(br.total_bits_read(), 16);
/// # Ok::<(), zenjxl_decoder::api::Error>(())
/// ```
#[inline(never)]
pub fn jump_to_byte_boundary(&mut self) -> Result<(), Error> {
if self.read(self.bits_to_next_byte())? != 0 {
return Err(Error::NonZeroPadding);
}
Ok(())
}
#[inline(always)]
fn refill(&mut self) {
// See Refill() in C++ code.
if self.data.len() >= 8 {
let bits = LittleEndian::read_u64(self.data);
self.bit_buf |= bits << self.bits_in_buf;
let read_bytes = (63 - self.bits_in_buf) >> 3;
self.bits_in_buf |= 56;
self.data = &self.data[read_bytes..];
debug_assert!(56 <= self.bits_in_buf && self.bits_in_buf < 64);
} else {
self.refill_slow()
}
}
#[inline(never)]
fn refill_slow(&mut self) {
while self.bits_in_buf < 56 {
if self.data.is_empty() {
return;
}
self.bit_buf |= (self.data[0] as u64) << self.bits_in_buf;
self.bits_in_buf += 8;
self.data = &self.data[1..];
}
}
/// Splits off a separate BitReader to handle the next `n` *full* bytes.
/// If `self` is not aligned to a byte boundary, it skips to the next byte boundary.
/// `self` is automatically advanced by `n` bytes.
#[allow(dead_code)] // Used by Frame::sections (TOC-based section splitting)
pub fn split_at(&mut self, n: usize) -> Result<BitReader<'a>, Error> {
self.jump_to_byte_boundary()?;
let mut ret = Self { ..*self };
self.skip_bits(n * 8)?;
let bytes_in_buf = ret.bits_in_buf / 8;
if n > bytes_in_buf {
// Prevent the returned bitreader from over-reading.
ret.data = &ret.data[..n - bytes_in_buf];
} else {
ret.bits_in_buf = n * 8;
ret.bit_buf &= (1u64 << (n * 8)) - 1;
ret.data = &[];
}
debug!(?n, ret=?ret);
Ok(ret)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_skip_bits_on_fresh_reader() {
// This test checks if skip_bits works correctly on a fresh BitReader
let data = [0x12, 0x34, 0x56, 0x78];
let mut br = BitReader::new(&data);
// Try to skip 1 bit on a fresh reader - this should work
br.skip_bits(1)
.expect("skip_bits should work on fresh reader");
assert_eq!(br.total_bits_read(), 1);
// Read the next 7 bits to complete the byte
let val = br.read(7).expect("read should work");
assert_eq!(val, 0x12 >> 1); // Should get the lower 7 bits of 0x12
}
}