symphonia_core/io/
mod.rs

1// Symphonia
2// Copyright (c) 2019-2022 The Project Symphonia Developers.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
8//! The `io` module implements composable bit- and byte-level I/O.
9//!
10//! The following nomenclature is used to denote where the data being read is sourced from:
11//!  * A `Stream` consumes any source implementing [`ReadBytes`] one byte at a time.
12//!  * A `Reader` consumes a `&[u8]`.
13//!
14//! The sole exception to this rule is [`MediaSourceStream`] which consumes sources implementing
15//! [`MediaSource`] (aka. [`std::io::Read`]).
16//!
17//! All `Reader`s and `Stream`s operating on bytes of data at a time implement the [`ReadBytes`]
18//! trait. Likewise, all `Reader`s and `Stream`s operating on bits of data at a time implement
19//! either the [`ReadBitsLtr`] or [`ReadBitsRtl`] traits depending on the order in which they
20//! consume bits.
21
22use std::io;
23use std::mem;
24
25mod bit;
26mod buf_reader;
27mod media_source_stream;
28mod monitor_stream;
29mod scoped_stream;
30
31pub use bit::*;
32pub use buf_reader::BufReader;
33pub use media_source_stream::{MediaSourceStream, MediaSourceStreamOptions};
34pub use monitor_stream::{Monitor, MonitorStream};
35pub use scoped_stream::ScopedStream;
36
37/// `MediaSource` is a composite trait of [`std::io::Read`] and [`std::io::Seek`]. A source *must*
38/// implement this trait to be used by [`MediaSourceStream`].
39///
40/// Despite requiring the [`std::io::Seek`] trait, seeking is an optional capability that can be
41/// queried at runtime.
42pub trait MediaSource: io::Read + io::Seek + Send + Sync {
43    /// Returns if the source is seekable. This may be an expensive operation.
44    fn is_seekable(&self) -> bool;
45
46    /// Returns the length in bytes, if available. This may be an expensive operation.
47    fn byte_len(&self) -> Option<u64>;
48}
49
50impl MediaSource for std::fs::File {
51    /// Returns if the `std::io::File` backing the `MediaSource` is seekable.
52    ///
53    /// Note: This operation involves querying the underlying file descriptor for information and
54    /// may be moderately expensive. Therefore it is recommended to cache this value if used often.
55    fn is_seekable(&self) -> bool {
56        // If the file's metadata is available, and the file is a regular file (i.e., not a FIFO,
57        // etc.), then the MediaSource will be seekable. Otherwise assume it is not. Note that
58        // metadata() follows symlinks.
59        match self.metadata() {
60            Ok(metadata) => metadata.is_file(),
61            _ => false,
62        }
63    }
64
65    /// Returns the length in bytes of the `std::io::File` backing the `MediaSource`.
66    ///
67    /// Note: This operation involves querying the underlying file descriptor for information and
68    /// may be moderately expensive. Therefore it is recommended to cache this value if used often.
69    fn byte_len(&self) -> Option<u64> {
70        match self.metadata() {
71            Ok(metadata) => Some(metadata.len()),
72            _ => None,
73        }
74    }
75}
76
77impl<T: std::convert::AsRef<[u8]> + Send + Sync> MediaSource for io::Cursor<T> {
78    /// Always returns true since a `io::Cursor<u8>` is always seekable.
79    fn is_seekable(&self) -> bool {
80        true
81    }
82
83    /// Returns the length in bytes of the `io::Cursor<u8>` backing the `MediaSource`.
84    fn byte_len(&self) -> Option<u64> {
85        // Get the underlying container, usually &Vec<T>.
86        let inner = self.get_ref();
87        // Get slice from the underlying container, &[T], for the len() function.
88        Some(inner.as_ref().len() as u64)
89    }
90}
91
92/// `ReadOnlySource` wraps any source implementing [`std::io::Read`] in an unseekable
93/// [`MediaSource`].
94pub struct ReadOnlySource<R: io::Read> {
95    inner: R,
96}
97
98impl<R: io::Read + Send> ReadOnlySource<R> {
99    /// Instantiates a new `ReadOnlySource<R>` by taking ownership and wrapping the provided
100    /// `Read`er.
101    pub fn new(inner: R) -> Self {
102        ReadOnlySource { inner }
103    }
104
105    /// Gets a reference to the underlying reader.
106    pub fn get_ref(&self) -> &R {
107        &self.inner
108    }
109
110    /// Gets a mutable reference to the underlying reader.
111    pub fn get_mut(&mut self) -> &mut R {
112        &mut self.inner
113    }
114
115    /// Unwraps this `ReadOnlySource<R>`, returning the underlying reader.
116    pub fn into_inner(self) -> R {
117        self.inner
118    }
119}
120
121impl<R: io::Read + Send + Sync> MediaSource for ReadOnlySource<R> {
122    fn is_seekable(&self) -> bool {
123        false
124    }
125
126    fn byte_len(&self) -> Option<u64> {
127        None
128    }
129}
130
131impl<R: io::Read> io::Read for ReadOnlySource<R> {
132    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
133        self.inner.read(buf)
134    }
135}
136
137impl<R: io::Read> io::Seek for ReadOnlySource<R> {
138    fn seek(&mut self, _: io::SeekFrom) -> io::Result<u64> {
139        Err(io::Error::new(io::ErrorKind::Other, "source does not support seeking"))
140    }
141}
142
143/// `ReadBytes` provides methods to read bytes and interpret them as little- or big-endian
144/// unsigned integers or floating-point values of standard widths.
145pub trait ReadBytes {
146    /// Reads a single byte from the stream and returns it or an error.
147    fn read_byte(&mut self) -> io::Result<u8>;
148
149    /// Reads two bytes from the stream and returns them in read-order or an error.
150    fn read_double_bytes(&mut self) -> io::Result<[u8; 2]>;
151
152    /// Reads three bytes from the stream and returns them in read-order or an error.
153    fn read_triple_bytes(&mut self) -> io::Result<[u8; 3]>;
154
155    /// Reads four bytes from the stream and returns them in read-order or an error.
156    fn read_quad_bytes(&mut self) -> io::Result<[u8; 4]>;
157
158    /// Reads up-to the number of bytes required to fill buf or returns an error.
159    fn read_buf(&mut self, buf: &mut [u8]) -> io::Result<usize>;
160
161    /// Reads exactly the number of bytes required to fill be provided buffer or returns an error.
162    fn read_buf_exact(&mut self, buf: &mut [u8]) -> io::Result<()>;
163
164    /// Reads a single unsigned byte from the stream and returns it or an error.
165    #[inline(always)]
166    fn read_u8(&mut self) -> io::Result<u8> {
167        self.read_byte()
168    }
169
170    /// Reads a single signed byte from the stream and returns it or an error.
171    #[inline(always)]
172    fn read_i8(&mut self) -> io::Result<i8> {
173        Ok(self.read_byte()? as i8)
174    }
175
176    /// Reads two bytes from the stream and interprets them as an unsigned 16-bit little-endian
177    /// integer or returns an error.
178    #[inline(always)]
179    fn read_u16(&mut self) -> io::Result<u16> {
180        Ok(u16::from_le_bytes(self.read_double_bytes()?))
181    }
182
183    /// Reads two bytes from the stream and interprets them as an signed 16-bit little-endian
184    /// integer or returns an error.
185    #[inline(always)]
186    fn read_i16(&mut self) -> io::Result<i16> {
187        Ok(i16::from_le_bytes(self.read_double_bytes()?))
188    }
189
190    /// Reads two bytes from the stream and interprets them as an unsigned 16-bit big-endian
191    /// integer or returns an error.
192    #[inline(always)]
193    fn read_be_u16(&mut self) -> io::Result<u16> {
194        Ok(u16::from_be_bytes(self.read_double_bytes()?))
195    }
196
197    /// Reads two bytes from the stream and interprets them as an signed 16-bit big-endian
198    /// integer or returns an error.
199    #[inline(always)]
200    fn read_be_i16(&mut self) -> io::Result<i16> {
201        Ok(i16::from_be_bytes(self.read_double_bytes()?))
202    }
203
204    /// Reads three bytes from the stream and interprets them as an unsigned 24-bit little-endian
205    /// integer or returns an error.
206    #[inline(always)]
207    fn read_u24(&mut self) -> io::Result<u32> {
208        let mut buf = [0u8; mem::size_of::<u32>()];
209        buf[0..3].clone_from_slice(&self.read_triple_bytes()?);
210        Ok(u32::from_le_bytes(buf))
211    }
212
213    /// Reads three bytes from the stream and interprets them as an signed 24-bit little-endian
214    /// integer or returns an error.
215    #[inline(always)]
216    fn read_i24(&mut self) -> io::Result<i32> {
217        Ok(((self.read_u24()? << 8) as i32) >> 8)
218    }
219
220    /// Reads three bytes from the stream and interprets them as an unsigned 24-bit big-endian
221    /// integer or returns an error.
222    #[inline(always)]
223    fn read_be_u24(&mut self) -> io::Result<u32> {
224        let mut buf = [0u8; mem::size_of::<u32>()];
225        buf[0..3].clone_from_slice(&self.read_triple_bytes()?);
226        Ok(u32::from_be_bytes(buf) >> 8)
227    }
228
229    /// Reads three bytes from the stream and interprets them as an signed 24-bit big-endian
230    /// integer or returns an error.
231    #[inline(always)]
232    fn read_be_i24(&mut self) -> io::Result<i32> {
233        Ok(((self.read_be_u24()? << 8) as i32) >> 8)
234    }
235
236    /// Reads four bytes from the stream and interprets them as an unsigned 32-bit little-endian
237    /// integer or returns an error.
238    #[inline(always)]
239    fn read_u32(&mut self) -> io::Result<u32> {
240        Ok(u32::from_le_bytes(self.read_quad_bytes()?))
241    }
242
243    /// Reads four bytes from the stream and interprets them as an signed 32-bit little-endian
244    /// integer or returns an error.
245    #[inline(always)]
246    fn read_i32(&mut self) -> io::Result<i32> {
247        Ok(i32::from_le_bytes(self.read_quad_bytes()?))
248    }
249
250    /// Reads four bytes from the stream and interprets them as an unsigned 32-bit big-endian
251    /// integer or returns an error.
252    #[inline(always)]
253    fn read_be_u32(&mut self) -> io::Result<u32> {
254        Ok(u32::from_be_bytes(self.read_quad_bytes()?))
255    }
256
257    /// Reads four bytes from the stream and interprets them as a signed 32-bit big-endian
258    /// integer or returns an error.
259    #[inline(always)]
260    fn read_be_i32(&mut self) -> io::Result<i32> {
261        Ok(i32::from_be_bytes(self.read_quad_bytes()?))
262    }
263
264    /// Reads eight bytes from the stream and interprets them as an unsigned 64-bit little-endian
265    /// integer or returns an error.
266    #[inline(always)]
267    fn read_u64(&mut self) -> io::Result<u64> {
268        let mut buf = [0u8; mem::size_of::<u64>()];
269        self.read_buf_exact(&mut buf)?;
270        Ok(u64::from_le_bytes(buf))
271    }
272
273    /// Reads eight bytes from the stream and interprets them as an signed 64-bit little-endian
274    /// integer or returns an error.
275    #[inline(always)]
276    fn read_i64(&mut self) -> io::Result<i64> {
277        let mut buf = [0u8; mem::size_of::<i64>()];
278        self.read_buf_exact(&mut buf)?;
279        Ok(i64::from_le_bytes(buf))
280    }
281
282    /// Reads eight bytes from the stream and interprets them as an unsigned 64-bit big-endian
283    /// integer or returns an error.
284    #[inline(always)]
285    fn read_be_u64(&mut self) -> io::Result<u64> {
286        let mut buf = [0u8; mem::size_of::<u64>()];
287        self.read_buf_exact(&mut buf)?;
288        Ok(u64::from_be_bytes(buf))
289    }
290
291    /// Reads eight bytes from the stream and interprets them as an signed 64-bit big-endian
292    /// integer or returns an error.
293    #[inline(always)]
294    fn read_be_i64(&mut self) -> io::Result<i64> {
295        let mut buf = [0u8; mem::size_of::<i64>()];
296        self.read_buf_exact(&mut buf)?;
297        Ok(i64::from_be_bytes(buf))
298    }
299
300    /// Reads four bytes from the stream and interprets them as a 32-bit little-endian IEEE-754
301    /// floating-point value.
302    #[inline(always)]
303    fn read_f32(&mut self) -> io::Result<f32> {
304        Ok(f32::from_le_bytes(self.read_quad_bytes()?))
305    }
306
307    /// Reads four bytes from the stream and interprets them as a 32-bit big-endian IEEE-754
308    /// floating-point value.
309    #[inline(always)]
310    fn read_be_f32(&mut self) -> io::Result<f32> {
311        Ok(f32::from_be_bytes(self.read_quad_bytes()?))
312    }
313
314    /// Reads four bytes from the stream and interprets them as a 64-bit little-endian IEEE-754
315    /// floating-point value.
316    #[inline(always)]
317    fn read_f64(&mut self) -> io::Result<f64> {
318        let mut buf = [0u8; mem::size_of::<u64>()];
319        self.read_buf_exact(&mut buf)?;
320        Ok(f64::from_le_bytes(buf))
321    }
322
323    /// Reads four bytes from the stream and interprets them as a 64-bit big-endian IEEE-754
324    /// floating-point value.
325    #[inline(always)]
326    fn read_be_f64(&mut self) -> io::Result<f64> {
327        let mut buf = [0u8; mem::size_of::<u64>()];
328        self.read_buf_exact(&mut buf)?;
329        Ok(f64::from_be_bytes(buf))
330    }
331
332    /// Reads up-to the number of bytes requested, and returns a boxed slice of the data or an
333    /// error.
334    fn read_boxed_slice(&mut self, len: usize) -> io::Result<Box<[u8]>> {
335        let mut buf = vec![0u8; len];
336        let actual_len = self.read_buf(&mut buf)?;
337        buf.truncate(actual_len);
338        Ok(buf.into_boxed_slice())
339    }
340
341    /// Reads exactly the number of bytes requested, and returns a boxed slice of the data or an
342    /// error.
343    fn read_boxed_slice_exact(&mut self, len: usize) -> io::Result<Box<[u8]>> {
344        let mut buf = vec![0u8; len];
345        self.read_buf_exact(&mut buf)?;
346        Ok(buf.into_boxed_slice())
347    }
348
349    /// Reads bytes from the stream into a supplied buffer until a byte pattern is matched. Returns
350    /// a mutable slice to the valid region of the provided buffer.
351    #[inline(always)]
352    fn scan_bytes<'a>(&mut self, pattern: &[u8], buf: &'a mut [u8]) -> io::Result<&'a mut [u8]> {
353        self.scan_bytes_aligned(pattern, 1, buf)
354    }
355
356    /// Reads bytes from a stream into a supplied buffer until a byte patter is matched on an
357    /// aligned byte boundary. Returns a mutable slice to the valid region of the provided buffer.
358    fn scan_bytes_aligned<'a>(
359        &mut self,
360        pattern: &[u8],
361        align: usize,
362        buf: &'a mut [u8],
363    ) -> io::Result<&'a mut [u8]>;
364
365    /// Ignores the specified number of bytes from the stream or returns an error.
366    fn ignore_bytes(&mut self, count: u64) -> io::Result<()>;
367
368    /// Gets the position of the stream.
369    fn pos(&self) -> u64;
370}
371
372impl<'b, R: ReadBytes> ReadBytes for &'b mut R {
373    #[inline(always)]
374    fn read_byte(&mut self) -> io::Result<u8> {
375        (*self).read_byte()
376    }
377
378    #[inline(always)]
379    fn read_double_bytes(&mut self) -> io::Result<[u8; 2]> {
380        (*self).read_double_bytes()
381    }
382
383    #[inline(always)]
384    fn read_triple_bytes(&mut self) -> io::Result<[u8; 3]> {
385        (*self).read_triple_bytes()
386    }
387
388    #[inline(always)]
389    fn read_quad_bytes(&mut self) -> io::Result<[u8; 4]> {
390        (*self).read_quad_bytes()
391    }
392
393    #[inline(always)]
394    fn read_buf(&mut self, buf: &mut [u8]) -> io::Result<usize> {
395        (*self).read_buf(buf)
396    }
397
398    #[inline(always)]
399    fn read_buf_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
400        (*self).read_buf_exact(buf)
401    }
402
403    #[inline(always)]
404    fn scan_bytes_aligned<'a>(
405        &mut self,
406        pattern: &[u8],
407        align: usize,
408        buf: &'a mut [u8],
409    ) -> io::Result<&'a mut [u8]> {
410        (*self).scan_bytes_aligned(pattern, align, buf)
411    }
412
413    #[inline(always)]
414    fn ignore_bytes(&mut self, count: u64) -> io::Result<()> {
415        (*self).ignore_bytes(count)
416    }
417
418    #[inline(always)]
419    fn pos(&self) -> u64 {
420        (**self).pos()
421    }
422}
423
424impl<'b, S: SeekBuffered> SeekBuffered for &'b mut S {
425    fn ensure_seekback_buffer(&mut self, len: usize) {
426        (*self).ensure_seekback_buffer(len)
427    }
428
429    fn unread_buffer_len(&self) -> usize {
430        (**self).unread_buffer_len()
431    }
432
433    fn read_buffer_len(&self) -> usize {
434        (**self).read_buffer_len()
435    }
436
437    fn seek_buffered(&mut self, pos: u64) -> u64 {
438        (*self).seek_buffered(pos)
439    }
440
441    fn seek_buffered_rel(&mut self, delta: isize) -> u64 {
442        (*self).seek_buffered_rel(delta)
443    }
444}
445
446/// `SeekBuffered` provides methods to seek within the buffered portion of a stream.
447pub trait SeekBuffered {
448    /// Ensures that `len` bytes will be available for backwards seeking if `len` bytes have been
449    /// previously read.
450    fn ensure_seekback_buffer(&mut self, len: usize);
451
452    /// Get the number of bytes buffered but not yet read.
453    ///
454    /// Note: This is the maximum number of bytes that can be seeked forwards within the buffer.
455    fn unread_buffer_len(&self) -> usize;
456
457    /// Gets the number of bytes buffered and read.
458    ///
459    /// Note: This is the maximum number of bytes that can be seeked backwards within the buffer.
460    fn read_buffer_len(&self) -> usize;
461
462    /// Seek within the buffered data to an absolute position in the stream. Returns the position
463    /// seeked to.
464    fn seek_buffered(&mut self, pos: u64) -> u64;
465
466    /// Seek within the buffered data relative to the current position in the stream. Returns the
467    /// position seeked to.
468    ///
469    /// The range of `delta` is clamped to the inclusive range defined by
470    /// `-read_buffer_len()..=unread_buffer_len()`.
471    fn seek_buffered_rel(&mut self, delta: isize) -> u64;
472
473    /// Seek backwards within the buffered data.
474    ///
475    /// This function is identical to [`SeekBuffered::seek_buffered_rel`] when a negative delta is
476    /// provided.
477    fn seek_buffered_rev(&mut self, delta: usize) {
478        assert!(delta < std::isize::MAX as usize);
479        self.seek_buffered_rel(-(delta as isize));
480    }
481}
482
483impl<'b, F: FiniteStream> FiniteStream for &'b mut F {
484    fn byte_len(&self) -> u64 {
485        (**self).byte_len()
486    }
487
488    fn bytes_read(&self) -> u64 {
489        (**self).bytes_read()
490    }
491
492    fn bytes_available(&self) -> u64 {
493        (**self).bytes_available()
494    }
495}
496
497/// A `FiniteStream` is a stream that has a known length in bytes.
498pub trait FiniteStream {
499    /// Returns the length of the the stream in bytes.
500    fn byte_len(&self) -> u64;
501
502    /// Returns the number of bytes that have been read.
503    fn bytes_read(&self) -> u64;
504
505    /// Returns the number of bytes available for reading.
506    fn bytes_available(&self) -> u64;
507}