From f02e6412ce80bbe28c5185ae47da4d64f2ff2a73 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:03:27 +0700 Subject: [PATCH] media: read AAC configuration through the shared byte source (#4046) Co-authored-by: Codex --- crates/media/src/aac.rs | 16 ++++++++-------- crates/media/src/pool.rs | 6 +++--- crates/media/src/stream.rs | 4 ++-- crates/media/tests/it/aac_layout.rs | 7 +++++++ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/crates/media/src/aac.rs b/crates/media/src/aac.rs index 356a8dff5..0aa987bf0 100644 --- a/crates/media/src/aac.rs +++ b/crates/media/src/aac.rs @@ -7,7 +7,7 @@ use std::io; use craft_audio::aac::AudioSpecificConfig; -use filmcraft_media::ByteReader; +use craft_media::ByteSource; /// How far into an MPEG TS / PS file the audio streams are looked for: the stream is indexed as /// far as this, not through the whole file. @@ -16,7 +16,7 @@ const MPEG_HEAD: u64 = 8 << 20; /// The configuration of the audio track FilmCraft plays, if it is AAC: from an MP4 / MOV `header` /// (its ftyp and moov, or the whole file) and `file`, or else from `file`, read as Matroska or /// MPEG TS / PS. -pub(crate) fn config(header: Option<&[u8]>, file: Option<&dyn ByteReader>) -> Option { +pub(crate) fn config(header: Option<&[u8]>, file: Option<&dyn ByteSource>) -> Option { let asc = match header.and_then(|h| mp4(h, file)) { Some(asc) => asc, None => { @@ -32,7 +32,7 @@ pub(crate) fn config(header: Option<&[u8]>, file: Option<&dyn ByteReader>) -> Op /// The track FilmCraft's MP4 source plays: the first audio track with samples, a sample /// description and a timescale, read from the whole file as it is (a fragmented movie's samples /// are in its fragments, not the `moov`). -fn mp4(header: &[u8], file: Option<&dyn ByteReader>) -> Option> { +fn mp4(header: &[u8], file: Option<&dyn ByteSource>) -> Option> { let mp4 = match file { Some(f) => craft_media::isobmff::open(Head(f, f.len())), None => craft_media::isobmff::open(header), @@ -50,7 +50,7 @@ fn mp4(header: &[u8], file: Option<&dyn ByteReader>) -> Option> { /// The track FilmCraft's Matroska source plays: the first audio track with frames, found by /// indexing the clusters as that source does. -fn matroska(file: &dyn ByteReader) -> Option> { +fn matroska(file: &dyn ByteSource) -> Option> { let mkv = craft_media::matroska::open(Head(file, file.len())).ok()?; match &mkv.tracks.iter().find(|t| t.kind == craft_media::matroska::TrackKind::Audio && !t.samples.is_empty())?.codec { craft_media::matroska::Codec::Aac { asc } => Some(asc.clone()), @@ -61,12 +61,12 @@ fn matroska(file: &dyn ByteReader) -> Option> { /// The configuration of the stream FilmCraft's MPEG source plays: the first audio stream whose /// first unit it can read. Only AAC is asked about, so the streams before that one were refused /// whatever their codec, and an ADTS stream played has no configuration outside its frames. -fn latm(file: &dyn ByteReader) -> Option> { +fn latm(file: &dyn ByteSource) -> Option> { use craft_media::mpegts::{self, Codec}; let head = Head(file, file.len().min(MPEG_HEAD)); let mpeg = mpegts::open(&head).ok()?; let audio = mpeg.streams.iter().enumerate().filter(|(_, s)| s.kind() == mpegts::Kind::Audio && !s.units.is_empty()); - let played = audio.take(filmcraft_media::MAX_AUDIO_STREAMS).find_map(|(i, s)| { + let played = audio.take(craft_media_source::MAX_AUDIO_STREAMS).find_map(|(i, s)| { let first = mpeg.read_unit(&head, i, 0).ok()?; match s.codec { Codec::AacAdts => mpegts::frame_info(&s.codec, &first).map(|_| None), @@ -77,8 +77,8 @@ fn latm(file: &dyn ByteReader) -> Option> { played? } -/// The first `.1` bytes of a file, as the byte source FilmCraft's demuxers read. -struct Head<'a>(&'a dyn ByteReader, u64); +/// The first `.1` bytes available to the AAC container scan. +struct Head<'a>(&'a dyn ByteSource, u64); impl Head<'_> { fn read(&self, offset: u64, buf: &mut [u8]) -> io::Result<()> { diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 7bb4ae78b..041b235db 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -568,7 +568,7 @@ struct Opened { impl Opened { /// `src` with what its container says: `header` is an MP4/MOV's ftyp and moov (or the whole /// file), `file` the file for other containers' AAC configuration. - fn new(src: MediaSource, header: Option<&[u8]>, file: Option<&dyn filmcraft_media::ByteReader>) -> Opened { + fn new(src: MediaSource, header: Option<&[u8]>, file: Option<&dyn craft_media::ByteSource>) -> Opened { let turn = header.map_or(Orientation::NoTransforms, crate::probe::video_turn); Opened { order: channel_order(&src, header, file), src, turn } } @@ -576,7 +576,7 @@ impl Opened { /// The order `src`'s audio channels decode in; multichannel AAC's by its configuration, where /// the container's header shows it ([`crate::aac::config`]). -fn channel_order(src: &MediaSource, header: Option<&[u8]>, file: Option<&dyn filmcraft_media::ByteReader>) -> Order { +fn channel_order(src: &MediaSource, header: Option<&[u8]>, file: Option<&dyn craft_media::ByteSource>) -> Order { match src.primary_audio() { Some((_, channels, codec)) if codec.contains("AAC") && channels > 2 => { crate::aac::config(header, file).map_or(Order::Aac, |asc| crate::downmix::aac_order(&asc)) @@ -1220,7 +1220,7 @@ impl Inner { let opened = bytes.and_then(|bytes| { let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); let src = crate::stream::open_bytes(&name, bytes.clone())?; - Ok(Opened::new(src, Some(&bytes), Some(&filmcraft_media::reader::MemReader(bytes.clone())))) + Ok(Opened::new(src, Some(&bytes), Some(&bytes))) }); (opened, read) }); diff --git a/crates/media/src/stream.rs b/crates/media/src/stream.rs index 3bcb9b970..5bebba64a 100644 --- a/crates/media/src/stream.rs +++ b/crates/media/src/stream.rs @@ -191,12 +191,12 @@ pub(crate) fn open_source(_path: &Path) -> Option<(Result, FileStam /// `path` read in place (`None` when it can't be opened, or without a native file system). #[cfg(any(unix, windows))] -pub(crate) fn file_reader(path: &Path) -> Option> { +pub(crate) fn file_reader(path: &Path) -> Option> { Some(Box::new(FileReader::open(path).ok()?)) } #[cfg(not(any(unix, windows)))] -pub(crate) fn file_reader(_path: &Path) -> Option> { +pub(crate) fn file_reader(_path: &Path) -> Option> { None } diff --git a/crates/media/tests/it/aac_layout.rs b/crates/media/tests/it/aac_layout.rs index 5adf762b2..014655a40 100644 --- a/crates/media/tests/it/aac_layout.rs +++ b/crates/media/tests/it/aac_layout.rs @@ -300,6 +300,13 @@ fn mpeg_aac_mixes_by_the_stream_played() { } let p = write(&format!("latm-first-{latm_first}.ts"), &ts.out); check(label, case.content, &mix(&MediaPool::new(), &probe(&p).expect("probe"))); + + let path = format!("/mem/latm-first-{latm_first}.ts"); + let bytes: Arc<[u8]> = ts.out.into(); + let pool = MediaPool::new(); + pool.add_bytes(&path, bytes.clone()); + let footage = probe_bytes(&path, bytes).expect("probe registered MPEG bytes"); + check(&format!("{label} in memory"), case.content, &mix(&pool, &footage)); } }