From f011fc280b139e3019bd129603b9bb0da3ad0c3f Mon Sep 17 00:00:00 2001 From: atok Date: Sat, 23 Dec 2023 18:42:37 +0100 Subject: [PATCH 01/22] Explicit std, alloc and core dependencies --- symphonia-core/Cargo.toml | 3 ++- symphonia-core/src/audio.rs | 12 ++++++---- symphonia-core/src/checksum/md5.rs | 2 +- symphonia-core/src/codecs.rs | 5 ++-- symphonia-core/src/conv.rs | 4 ++-- symphonia-core/src/dsp/fft.rs | 10 ++++---- symphonia-core/src/dsp/mdct/simd.rs | 7 ++++-- symphonia-core/src/errors.rs | 4 ++-- symphonia-core/src/formats.rs | 4 ++++ symphonia-core/src/io/bit.rs | 24 +++++++++++--------- symphonia-core/src/io/buf_reader.rs | 2 +- symphonia-core/src/io/media_source_stream.rs | 13 +++++++---- symphonia-core/src/io/mod.rs | 6 +++-- symphonia-core/src/io/scoped_stream.rs | 6 ++--- symphonia-core/src/lib.rs | 4 ++++ symphonia-core/src/meta.rs | 14 ++++++++---- symphonia-core/src/probe.rs | 6 +++++ symphonia-core/src/sample.rs | 2 +- symphonia-core/src/units.rs | 12 +++++----- symphonia-core/src/util.rs | 2 +- 20 files changed, 89 insertions(+), 53 deletions(-) diff --git a/symphonia-core/Cargo.toml b/symphonia-core/Cargo.toml index 4db81690..54e5666e 100644 --- a/symphonia-core/Cargo.toml +++ b/symphonia-core/Cargo.toml @@ -13,7 +13,8 @@ edition = "2018" rust-version = "1.53" [features] -default = [] +default = ["std"] +std = [] # SIMD support. opt-simd-sse = ["rustfft/sse"] diff --git a/symphonia-core/src/audio.rs b/symphonia-core/src/audio.rs index 36f431d2..b8f93a42 100644 --- a/symphonia-core/src/audio.rs +++ b/symphonia-core/src/audio.rs @@ -8,11 +8,13 @@ //! The `audio` module provides primitives for working with multi-channel audio buffers of varying //! sample formats. -use std::borrow::Cow; -use std::fmt; -use std::marker::PhantomData; -use std::mem; -use std::vec::Vec; +use alloc::borrow::Cow; +use alloc::boxed::Box; +use alloc::vec; +use core::fmt; +use core::marker::PhantomData; +use core::mem; +use alloc::vec::Vec; use arrayvec::ArrayVec; use bitflags::bitflags; diff --git a/symphonia-core/src/checksum/md5.rs b/symphonia-core/src/checksum/md5.rs index 3f44a25b..1834eae0 100644 --- a/symphonia-core/src/checksum/md5.rs +++ b/symphonia-core/src/checksum/md5.rs @@ -5,7 +5,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::cmp; +use core::cmp; use crate::io::Monitor; diff --git a/symphonia-core/src/codecs.rs b/symphonia-core/src/codecs.rs index 31feb1de..97907413 100644 --- a/symphonia-core/src/codecs.rs +++ b/symphonia-core/src/codecs.rs @@ -8,9 +8,10 @@ //! The `codec` module provides the traits and support structures necessary to implement audio codec //! decoders. +use alloc::boxed::Box; use std::collections::HashMap; -use std::default::Default; -use std::fmt; +use core::default::Default; +use core::fmt; use crate::audio::{AudioBufferRef, Channels, Layout}; use crate::errors::{unsupported_error, Result}; diff --git a/symphonia-core/src/conv.rs b/symphonia-core/src/conv.rs index c41fec3a..408de30b 100644 --- a/symphonia-core/src/conv.rs +++ b/symphonia-core/src/conv.rs @@ -28,7 +28,7 @@ pub mod dither { use super::FromSample; use crate::sample::Sample; use crate::sample::{i24, u24}; - use std::marker::PhantomData; + use core::marker::PhantomData; mod prng { #[inline] @@ -679,7 +679,7 @@ impl ConvertibleSample for S where mod tests { use super::FromSample; use crate::sample::{i24, u24, Sample}; - use std::{i16, i32, i8, u16, u32, u8}; + use core::{i16, i32, i8, u16, u32, u8}; #[test] fn verify_u8_from_sample() { diff --git a/symphonia-core/src/dsp/fft.rs b/symphonia-core/src/dsp/fft.rs index 350ce468..29e375cd 100644 --- a/symphonia-core/src/dsp/fft.rs +++ b/symphonia-core/src/dsp/fft.rs @@ -10,8 +10,9 @@ //! The complex (I)FFT in this module supports a size up-to 65536. The FFT is implemented using the //! radix-2 Cooley-Tukey algorithm. -use std::convert::TryInto; -use std::f32; +use alloc::boxed::Box; +use core::convert::TryInto; +use core::f32; use lazy_static::lazy_static; @@ -25,7 +26,7 @@ macro_rules! fft_twiddle_table { let mut table = [Default::default(); N >> 1]; - let theta = std::f64::consts::PI / (N >> 1) as f64; + let theta = core::f64::consts::PI / (N >> 1) as f64; for (k, t) in table.iter_mut().enumerate() { let angle = theta * k as f64; @@ -408,8 +409,9 @@ fn fft2(x: &mut [Complex; 2]) { #[cfg(test)] mod tests { + use alloc::vec::Vec; use super::*; - use std::f64; + use core::f64; /// Compute a naive DFT. fn dft_naive(x: &[Complex], y: &mut [Complex]) { diff --git a/symphonia-core/src/dsp/mdct/simd.rs b/symphonia-core/src/dsp/mdct/simd.rs index 502095d6..621b5de5 100644 --- a/symphonia-core/src/dsp/mdct/simd.rs +++ b/symphonia-core/src/dsp/mdct/simd.rs @@ -7,7 +7,10 @@ //! The Modified Discrete Cosine Transform (MDCT) implemented with SIMD optimizations. -use std::sync::Arc; +use alloc::boxed::Box; +use alloc::vec::Vec; +use alloc::sync::Arc; +use alloc::vec; use rustfft::num_complex::Complex; @@ -42,7 +45,7 @@ impl Imdct { let mut twiddle = Vec::with_capacity(n2); let alpha = 1.0 / 8.0 + if scale.is_sign_positive() { 0.0 } else { n2 as f64 }; - let pi_n = std::f64::consts::PI / n as f64; + let pi_n = core::f64::consts::PI / n as f64; let sqrt_scale = scale.abs().sqrt(); for k in 0..n2 { diff --git a/symphonia-core/src/errors.rs b/symphonia-core/src/errors.rs index 7959b3b9..ae2565cf 100644 --- a/symphonia-core/src/errors.rs +++ b/symphonia-core/src/errors.rs @@ -8,9 +8,9 @@ //! The `errors` module defines the common error type. use std::error; -use std::fmt; +use core::fmt; use std::io; -use std::result; +use core::result; /// `SeekErrorKind` is a list of generic reasons why a seek may fail. #[derive(Debug)] diff --git a/symphonia-core/src/formats.rs b/symphonia-core/src/formats.rs index 58158f31..e91203ee 100644 --- a/symphonia-core/src/formats.rs +++ b/symphonia-core/src/formats.rs @@ -8,6 +8,9 @@ //! The `format` module provides the traits and support structures necessary to implement media //! demuxers. +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; use crate::codecs::CodecParameters; use crate::errors::Result; use crate::io::{BufReader, MediaSourceStream}; @@ -327,6 +330,7 @@ impl Packet { pub mod util { //! Helper utilities for implementing `FormatReader`s. + use alloc::vec::Vec; use super::Packet; /// A `SeekPoint` is a mapping between a sample or frame number to byte offset within a media diff --git a/symphonia-core/src/io/bit.rs b/symphonia-core/src/io/bit.rs index 82cbe32e..5be17d0a 100644 --- a/symphonia-core/src/io/bit.rs +++ b/symphonia-core/src/io/bit.rs @@ -5,7 +5,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::cmp::min; +use core::cmp::min; use std::io; use crate::io::ReadBytes; @@ -18,9 +18,10 @@ fn end_of_bitstream_error() -> io::Result { pub mod vlc { //! The `vlc` module provides support for decoding variable-length codes (VLC). - use std::cmp::max; - use std::collections::{BTreeMap, VecDeque}; + use core::cmp::max; + use alloc::collections::{BTreeMap, VecDeque}; use std::io; + use alloc::vec::Vec; fn codebook_error(desc: &'static str) -> io::Result { Err(io::Error::new(io::ErrorKind::Other, desc)) @@ -957,7 +958,7 @@ impl<'a> BitReaderLtr<'a> { impl<'a> private::FetchBitsLtr for BitReaderLtr<'a> { #[inline] fn fetch_bits_partial(&mut self) -> io::Result<()> { - let mut buf = [0u8; std::mem::size_of::()]; + let mut buf = [0u8; core::mem::size_of::()]; let read_len = min(self.buf.len(), (u64::BITS - self.n_bits_left) as usize >> 3); @@ -972,9 +973,9 @@ impl<'a> private::FetchBitsLtr for BitReaderLtr<'a> { } fn fetch_bits(&mut self) -> io::Result<()> { - let mut buf = [0u8; std::mem::size_of::()]; + let mut buf = [0u8; core::mem::size_of::()]; - let read_len = min(self.buf.len(), std::mem::size_of::()); + let read_len = min(self.buf.len(), core::mem::size_of::()); if read_len == 0 { return end_of_bitstream_error(); @@ -1406,7 +1407,7 @@ impl<'a> BitReaderRtl<'a> { impl<'a> private::FetchBitsRtl for BitReaderRtl<'a> { #[inline] fn fetch_bits_partial(&mut self) -> io::Result<()> { - let mut buf = [0u8; std::mem::size_of::()]; + let mut buf = [0u8; core::mem::size_of::()]; let read_len = min(self.buf.len(), (u64::BITS - self.n_bits_left) as usize >> 3); @@ -1421,9 +1422,9 @@ impl<'a> private::FetchBitsRtl for BitReaderRtl<'a> { } fn fetch_bits(&mut self) -> io::Result<()> { - let mut buf = [0u8; std::mem::size_of::()]; + let mut buf = [0u8; core::mem::size_of::()]; - let read_len = min(self.buf.len(), std::mem::size_of::()); + let read_len = min(self.buf.len(), core::mem::size_of::()); if read_len == 0 { return end_of_bitstream_error(); @@ -1466,6 +1467,7 @@ impl<'a> FiniteBitStream for BitReaderRtl<'a> { #[cfg(test)] mod tests { + use alloc::vec::Vec; use super::vlc::{BitOrder, Codebook, CodebookBuilder, Entry8x8}; use super::{BitReaderLtr, ReadBitsLtr}; use super::{BitReaderRtl, ReadBitsRtl}; @@ -1830,7 +1832,7 @@ mod tests { let decoded: Vec = (0..text.len()).into_iter().map(|_| bs.read_codebook(&codebook).unwrap().0).collect(); - assert_eq!(text, std::str::from_utf8(&decoded).unwrap()); + assert_eq!(text, core::str::from_utf8(&decoded).unwrap()); } // BitStreamRtl @@ -2126,6 +2128,6 @@ mod tests { let decoded: Vec = (0..text.len()).into_iter().map(|_| bs.read_codebook(&codebook).unwrap().0).collect(); - assert_eq!(text, std::str::from_utf8(&decoded).unwrap()); + assert_eq!(text, core::str::from_utf8(&decoded).unwrap()); } } diff --git a/symphonia-core/src/io/buf_reader.rs b/symphonia-core/src/io/buf_reader.rs index fbbf860b..3cb84e21 100644 --- a/symphonia-core/src/io/buf_reader.rs +++ b/symphonia-core/src/io/buf_reader.rs @@ -5,7 +5,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::cmp; +use core::cmp; use std::io; use super::{FiniteStream, ReadBytes}; diff --git a/symphonia-core/src/io/media_source_stream.rs b/symphonia-core/src/io/media_source_stream.rs index a5bbb234..c3f8877b 100644 --- a/symphonia-core/src/io/media_source_stream.rs +++ b/symphonia-core/src/io/media_source_stream.rs @@ -5,10 +5,12 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::cmp; +use alloc::boxed::Box; +use alloc::vec; +use core::cmp; use std::io; use std::io::{IoSliceMut, Read, Seek}; -use std::ops::Sub; +use core::ops::Sub; use super::SeekBuffered; use super::{MediaSource, ReadBytes}; @@ -436,12 +438,12 @@ impl SeekBuffered for MediaSourceStream { // Forward seek. let delta = if pos > old_pos { - assert!(pos - old_pos < std::isize::MAX as u64); + assert!(pos - old_pos < core::isize::MAX as u64); (pos - old_pos) as isize } else if pos < old_pos { // Backward seek. - assert!(old_pos - pos < std::isize::MAX as u64); + assert!(old_pos - pos < core::isize::MAX as u64); -((old_pos - pos) as isize) } else { @@ -467,6 +469,9 @@ impl SeekBuffered for MediaSourceStream { #[cfg(test)] mod tests { + use alloc::boxed::Box; + use alloc::vec; + use alloc::vec::Vec; use super::{MediaSourceStream, ReadBytes, SeekBuffered}; use std::io::{Cursor, Read}; diff --git a/symphonia-core/src/io/mod.rs b/symphonia-core/src/io/mod.rs index e301cfcf..75da23be 100644 --- a/symphonia-core/src/io/mod.rs +++ b/symphonia-core/src/io/mod.rs @@ -19,8 +19,10 @@ //! either the [`ReadBitsLtr`] or [`ReadBitsRtl`] traits depending on the order in which they //! consume bits. +use alloc::boxed::Box; +use alloc::vec; use std::io; -use std::mem; +use core::mem; mod bit; mod buf_reader; @@ -475,7 +477,7 @@ pub trait SeekBuffered { /// This function is identical to [`SeekBuffered::seek_buffered_rel`] when a negative delta is /// provided. fn seek_buffered_rev(&mut self, delta: usize) { - assert!(delta < std::isize::MAX as usize); + assert!(delta < core::isize::MAX as usize); self.seek_buffered_rel(-(delta as isize)); } } diff --git a/symphonia-core/src/io/scoped_stream.rs b/symphonia-core/src/io/scoped_stream.rs index 12106062..3d2a09cc 100644 --- a/symphonia-core/src/io/scoped_stream.rs +++ b/symphonia-core/src/io/scoped_stream.rs @@ -5,7 +5,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. -use std::cmp; +use core::cmp; use std::io; use super::{FiniteStream, ReadBytes, SeekBuffered}; @@ -184,8 +184,8 @@ impl SeekBuffered for ScopedStream { fn seek_buffered_rel(&mut self, delta: isize) -> u64 { // Clamp the delta value such that the absolute position after the buffered seek will be // within the bounds of the ScopedStream. - let max_back = self.read.min(std::isize::MAX as u64) as isize; - let max_forward = (self.len - self.read).min(std::isize::MAX as u64) as isize; + let max_back = self.read.min(core::isize::MAX as u64) as isize; + let max_forward = (self.len - self.read).min(core::isize::MAX as u64) as isize; self.inner.seek_buffered_rel(delta.clamp(-max_back, max_forward)) } } diff --git a/symphonia-core/src/lib.rs b/symphonia-core/src/lib.rs index 81b0f76f..2adc5301 100644 --- a/symphonia-core/src/lib.rs +++ b/symphonia-core/src/lib.rs @@ -1,3 +1,4 @@ +#![no_std] // Symphonia // Copyright (c) 2019-2022 The Project Symphonia Developers. // @@ -12,6 +13,9 @@ #![allow(clippy::identity_op)] #![allow(clippy::manual_range_contains)] +extern crate alloc; +extern crate std; + pub mod audio; pub mod checksum; pub mod codecs; diff --git a/symphonia-core/src/meta.rs b/symphonia-core/src/meta.rs index c1e30fd8..f3fac2bc 100644 --- a/symphonia-core/src/meta.rs +++ b/symphonia-core/src/meta.rs @@ -7,11 +7,15 @@ //! The `meta` module defines basic metadata elements, and management structures. -use std::borrow::Cow; -use std::collections::VecDeque; -use std::convert::From; -use std::fmt; -use std::num::NonZeroU32; +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::borrow::Cow; +use alloc::collections::VecDeque; +use core::convert::From; +use core::fmt; +use core::num::NonZeroU32; +use alloc::string::ToString; use crate::errors::Result; use crate::io::MediaSourceStream; diff --git a/symphonia-core/src/probe.rs b/symphonia-core/src/probe.rs index 823d19de..8156e425 100644 --- a/symphonia-core/src/probe.rs +++ b/symphonia-core/src/probe.rs @@ -8,6 +8,10 @@ //! The `probe` module provides methods and traits to support auto-detection of media formats from //! arbitrary media streams. +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::Vec; +use alloc::borrow::ToOwned; use crate::errors::{unsupported_error, Result}; use crate::formats::{FormatOptions, FormatReader}; use crate::io::{MediaSourceStream, ReadBytes, SeekBuffered}; @@ -16,6 +20,8 @@ use crate::meta::{Metadata, MetadataLog, MetadataOptions, MetadataReader}; use log::{debug, error, info}; mod bloom { + use alloc::boxed::Box; + use alloc::vec; fn fnv1a32(value: &[u8; 2]) -> u32 { const INIT: u32 = 0x811c_9dc5; diff --git a/symphonia-core/src/sample.rs b/symphonia-core/src/sample.rs index eafb87af..bd195373 100644 --- a/symphonia-core/src/sample.rs +++ b/symphonia-core/src/sample.rs @@ -7,7 +7,7 @@ //! The `sample` module defines the core audio sample trait and any non-primitive sample data types. -use std::fmt; +use core::fmt; use crate::util::clamp::{clamp_f32, clamp_f64, clamp_i24, clamp_u24}; diff --git a/symphonia-core/src/units.rs b/symphonia-core/src/units.rs index 12fc4a7e..5a47dd2b 100644 --- a/symphonia-core/src/units.rs +++ b/symphonia-core/src/units.rs @@ -7,7 +7,7 @@ //! The `units` module provides definitions for common units. -use std::fmt; +use core::fmt; /// A `TimeStamp` represents an instantenous instant in time since the start of a stream. One /// `TimeStamp` "tick" is equivalent to the stream's `TimeBase` in seconds. @@ -118,15 +118,15 @@ impl From for Time { } } -impl From for Time { - fn from(duration: std::time::Duration) -> Self { +impl From for Time { + fn from(duration: core::time::Duration) -> Self { Time::new(duration.as_secs(), f64::from(duration.subsec_nanos()) / 1_000_000_000.0) } } -impl From