mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-23 02:41:05 +01:00
media: read AAC configuration through the shared byte source (#4046)
Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
1 parent
887c08b31e
commit
f02e6412ce
4 files changed
+20
-13
No files matched your search
@@ -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<AudioSpecificConfig> {
|
||||
pub(crate) fn config(header: Option<&[u8]>, file: Option<&dyn ByteSource>) -> Option<AudioSpecificConfig> {
|
||||
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<Vec<u8>> {
|
||||
fn mp4(header: &[u8], file: Option<&dyn ByteSource>) -> Option<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
|
||||
/// 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<Vec<u8>> {
|
||||
fn matroska(file: &dyn ByteSource) -> Option<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
/// 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<Vec<u8>> {
|
||||
fn latm(file: &dyn ByteSource) -> Option<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
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<()> {
|
||||
|
||||
@@ -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)
|
||||
});
|
||||
|
||||
@@ -191,12 +191,12 @@ pub(crate) fn open_source(_path: &Path) -> Option<(Result<MediaSource>, 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<Box<dyn filmcraft_media::ByteReader>> {
|
||||
pub(crate) fn file_reader(path: &Path) -> Option<Box<dyn craft_media::ByteSource>> {
|
||||
Some(Box::new(FileReader::open(path).ok()?))
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
pub(crate) fn file_reader(_path: &Path) -> Option<Box<dyn filmcraft_media::ByteReader>> {
|
||||
pub(crate) fn file_reader(_path: &Path) -> Option<Box<dyn craft_media::ByteSource>> {
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user