oximedia_bitstream/read/bit_read2.rs
1// Copyright 2017 Brian Langenberger
2// Copyright 2024-2026 COOLJAPAN OU (Team Kitasan)
3//
4// Licensed under the Apache License, Version 2.0 or the MIT license,
5// at your option. See the LICENSE-APACHE / LICENSE-MIT files for details.
6
7//! Legacy `BitRead2` compatibility trait.
8
9use std::io;
10#[cfg(feature = "alloc")]
11use std::vec::Vec;
12
13use super::{
14 read_to_vec, BitRead, Endianness, FromBitStream, FromBitStreamWith, Integer, Primitive,
15 SignedInteger,
16};
17
18/// A compatibility trait for older code implementing [`BitRead`]
19///
20/// This is a trait largely compatible with older code
21/// from the 2.X.X version,
22/// which one can use with a named import as needed.
23///
24/// New code should prefer the regular [`BitRead`] trait.
25///
26/// # Example
27/// ```
28/// use oximedia_bitstream::BitRead2 as BitRead;
29/// use oximedia_bitstream::{BitReader, BigEndian};
30/// let byte = &[0b1111_0000];
31/// let mut reader = BitReader::endian(byte.as_slice(), BigEndian);
32/// assert_eq!(reader.read::<u8>(4).unwrap(), 0b1111);
33/// assert_eq!(reader.read_in::<4, u8>().unwrap(), 0b0000);
34/// ```
35pub trait BitRead2 {
36 /// Reads a single bit from the stream.
37 /// `true` indicates 1, `false` indicates 0
38 ///
39 /// # Errors
40 ///
41 /// Passes along any I/O error from the underlying stream.
42 fn read_bit(&mut self) -> io::Result<bool>;
43
44 /// Reads an unsigned value from the stream with
45 /// the given number of bits.
46 ///
47 /// # Errors
48 ///
49 /// Passes along any I/O error from the underlying stream.
50 /// Also returns an error if the output type is too small
51 /// to hold the requested number of bits.
52 fn read<I>(&mut self, bits: u32) -> io::Result<I>
53 where
54 I: Integer + Sized;
55
56 /// Reads an unsigned value from the stream with
57 /// the given constant number of bits.
58 ///
59 /// # Errors
60 ///
61 /// Passes along any I/O error from the underlying stream.
62 /// A compile-time error occurs if the given number of bits
63 /// is larger than the output type.
64 fn read_in<const BITS: u32, I>(&mut self) -> io::Result<I>
65 where
66 I: Integer,
67 {
68 self.read(BITS)
69 }
70
71 /// Reads a twos-complement signed value from the stream with
72 /// the given number of bits.
73 ///
74 /// # Errors
75 ///
76 /// Passes along any I/O error from the underlying stream.
77 /// Returns an error if the number of bits is 0,
78 /// since one bit is always needed for the sign.
79 /// Also returns an error if the output type is too small
80 /// to hold the requested number of bits.
81 fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>
82 where
83 S: SignedInteger;
84
85 /// Reads a twos-complement signed value from the stream with
86 /// the given constant number of bits.
87 ///
88 /// # Errors
89 ///
90 /// Passes along any I/O error from the underlying stream.
91 /// A compile-time error occurs if the number of bits is 0,
92 /// since one bit is always needed for the sign.
93 /// A compile-time error occurs if the given number of bits
94 /// is larger than the output type.
95 fn read_signed_in<const BITS: u32, S>(&mut self) -> io::Result<S>
96 where
97 S: SignedInteger,
98 {
99 self.read_signed(BITS)
100 }
101
102 /// Reads whole value from the stream whose size in bits is equal
103 /// to its type's size.
104 ///
105 /// # Errors
106 ///
107 /// Passes along any I/O error from the underlying stream.
108 fn read_to<V>(&mut self) -> io::Result<V>
109 where
110 V: Primitive;
111
112 /// Reads whole value from the stream whose size in bits is equal
113 /// to its type's size in an endianness that may be different
114 /// from the stream's endianness.
115 ///
116 /// # Errors
117 ///
118 /// Passes along any I/O error from the underlying stream.
119 fn read_as_to<F, V>(&mut self) -> io::Result<V>
120 where
121 F: Endianness,
122 V: Primitive;
123
124 /// Skips the given number of bits in the stream.
125 /// Since this method does not need an accumulator,
126 /// it may be slightly faster than reading to an empty variable.
127 /// In addition, since there is no accumulator,
128 /// there is no upper limit on the number of bits
129 /// which may be skipped.
130 /// These bits are still read from the stream, however,
131 /// and are never skipped via a `seek` method.
132 ///
133 /// # Errors
134 ///
135 /// Passes along any I/O error from the underlying stream.
136 fn skip(&mut self, bits: u32) -> io::Result<()>;
137
138 /// Completely fills the given buffer with whole bytes.
139 /// If the stream is already byte-aligned, it will map
140 /// to a faster `read_exact` call. Otherwise it will read
141 /// bytes individually in 8-bit increments.
142 ///
143 /// # Errors
144 ///
145 /// Passes along any I/O error from the underlying stream.
146 fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> {
147 for b in buf.iter_mut() {
148 *b = self.read_in::<8, _>()?;
149 }
150 Ok(())
151 }
152
153 /// Completely fills a whole buffer with bytes and returns it.
154 /// If the stream is already byte-aligned, it will map
155 /// to a faster `read_exact` call. Otherwise it will read
156 /// bytes individually in 8-bit increments.
157 ///
158 /// # Errors
159 ///
160 /// Passes along any I/O error from the underlying stream.
161 #[inline(always)]
162 #[deprecated(since = "1.8.0", note = "use read_to() method instead")]
163 fn read_to_bytes<const SIZE: usize>(&mut self) -> io::Result<[u8; SIZE]> {
164 self.read_to()
165 }
166
167 /// Completely fills a vector of bytes and returns it.
168 /// If the stream is already byte-aligned, it will map
169 /// to a faster `read_exact` call. Otherwise it will read
170 /// bytes individually in 8-bit increments.
171 ///
172 /// # Errors
173 ///
174 /// Passes along any I/O error from the underlying stream.
175 #[cfg(feature = "alloc")]
176 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
177 fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
178 read_to_vec(|buf| self.read_bytes(buf), bytes)
179 }
180
181 /// Counts the number of 1 bits in the stream until the next
182 /// 0 bit and returns the amount read.
183 /// Because this field is variably-sized and may be large,
184 /// its output is always a `u32` type.
185 ///
186 /// # Errors
187 ///
188 /// Passes along any I/O error from the underlying stream.
189 fn read_unary0(&mut self) -> io::Result<u32> {
190 let mut unary = 0;
191 while self.read_bit()? {
192 unary += 1;
193 }
194 Ok(unary)
195 }
196
197 /// Counts the number of 0 bits in the stream until the next
198 /// 1 bit and returns the amount read.
199 /// Because this field is variably-sized and may be large,
200 /// its output is always a `u32` type.
201 ///
202 /// # Errors
203 ///
204 /// Passes along any I/O error from the underlying stream.
205 fn read_unary1(&mut self) -> io::Result<u32> {
206 let mut unary = 0;
207 while !(self.read_bit()?) {
208 unary += 1;
209 }
210 Ok(unary)
211 }
212
213 /// Parses and returns complex type
214 fn parse<F: FromBitStream>(&mut self) -> Result<F, F::Error>
215 where
216 Self: BitRead,
217 {
218 F::from_reader(self)
219 }
220
221 /// Parses and returns complex type with context
222 fn parse_with<'a, F: FromBitStreamWith<'a>>(
223 &mut self,
224 context: &F::Context,
225 ) -> Result<F, F::Error>
226 where
227 Self: BitRead,
228 {
229 F::from_reader(self, context)
230 }
231
232 /// Returns true if the stream is aligned at a whole byte.
233 fn byte_aligned(&self) -> bool;
234
235 /// Throws away all unread bit values until the next whole byte.
236 /// Does nothing if the stream is already aligned.
237 fn byte_align(&mut self);
238
239 /// Given a compiled Huffman tree, reads bits from the stream
240 /// until the next symbol is encountered.
241 ///
242 /// # Errors
243 ///
244 /// Passes along any I/O error from the underlying stream.
245 #[inline]
246 fn read_huffman<T>(&mut self) -> io::Result<T::Symbol>
247 where
248 T: crate::huffman::FromBits,
249 {
250 T::from_bits(|| self.read_bit())
251 }
252}
253
254impl<R: BitRead> BitRead2 for R {
255 #[inline(always)]
256 fn read_bit(&mut self) -> io::Result<bool> {
257 BitRead::read_bit(self)
258 }
259
260 #[inline(always)]
261 fn read<I>(&mut self, bits: u32) -> io::Result<I>
262 where
263 I: Integer + Sized,
264 {
265 self.read_var(bits)
266 }
267
268 #[inline(always)]
269 fn read_in<const BITS: u32, I>(&mut self) -> io::Result<I>
270 where
271 I: Integer,
272 {
273 BitRead::read::<BITS, I>(self)
274 }
275
276 #[inline(always)]
277 fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>
278 where
279 S: SignedInteger,
280 {
281 self.read_signed_var(bits)
282 }
283
284 #[inline(always)]
285 fn read_signed_in<const BITS: u32, S>(&mut self) -> io::Result<S>
286 where
287 S: SignedInteger,
288 {
289 BitRead::read_signed::<BITS, S>(self)
290 }
291
292 #[inline(always)]
293 fn read_to<V>(&mut self) -> io::Result<V>
294 where
295 V: Primitive,
296 {
297 BitRead::read_to::<V>(self)
298 }
299
300 #[inline(always)]
301 fn read_as_to<F, V>(&mut self) -> io::Result<V>
302 where
303 F: Endianness,
304 V: Primitive,
305 {
306 BitRead::read_as_to::<F, V>(self)
307 }
308
309 #[inline(always)]
310 fn skip(&mut self, bits: u32) -> io::Result<()> {
311 BitRead::skip(self, bits)
312 }
313
314 #[inline(always)]
315 fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> {
316 BitRead::read_bytes(self, buf)
317 }
318
319 #[inline(always)]
320 fn read_unary0(&mut self) -> io::Result<u32> {
321 self.read_unary::<0>()
322 }
323
324 #[inline(always)]
325 fn read_unary1(&mut self) -> io::Result<u32> {
326 self.read_unary::<1>()
327 }
328
329 #[inline(always)]
330 fn byte_aligned(&self) -> bool {
331 BitRead::byte_aligned(self)
332 }
333
334 #[inline(always)]
335 fn byte_align(&mut self) {
336 BitRead::byte_align(self);
337 }
338}