From f68f46d0fd79caf02c53049fceedbc90c6a52ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 03:22:43 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=81=20feat(audio):=20the=20MP3=20and?= =?UTF-8?q?=20Vorbis=20encode=20seats=20are=20the=20native=20ec-mp3=20/=20?= =?UTF-8?q?ec-vorbis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two registry-crate encoder seats (rusty_mp3 0.6, rusty_vorbis 0.1.1) leave the tree; both formats encode through the codec family's own crates (path deps, same boundary ec-opus/ec-ac3 already cross). MP3 (CBR per the card's rate rows): same seat contract, same Mp3EncoderConfig field names, call sites unchanged. A/B on identical 16-bit PCM (suite fixtures + a 30 s 44.1 kHz stereo music decode), outputs decoded through symphonia: correlation 0.9994-0.9998 against the incumbent's 0.9980-0.9991 at the same 192 kbps, PSNR +5..+7 dB, and the decoded length lands exactly on the mix (the old seat ran 180 samples long with a 1057-sample shift the gapless tag only partly hid). Vorbis: the seat's quality knob alone saturates below the incumbent's operating band, so write_ogg drives the encoder the way its own rate gates are measured -- rate loop started at quality 0.85 and held to a 128 kbps target. At the incumbent's own 0.85 operating point (123.8 kbps actual on the music clip, correlation 0.9947) the new seat reads 129.0 kbps actual at correlation 0.9968, +3.1 dB PSNR; against ffmpeg's libvorbis -q 5 it is par at the same rate (0.9968 vs 0.9973). Every export decodes to exactly the timeline's frame count, correlation peak at lag zero. That measurement retires write_ogg's workaround: the old seat advanced its granule a hop early and overran the tail, so the function carried a 1024-sample pre-roll and two granule corrections around it. ec-vorbis reports spec granules and clamps its final one to the sample count fed, so the pre-roll, the per-packet shift and the manual tail declaration are deleted. Mono stays dual mono (bit-identical L/R, worst |L-R| = 0 measured) because the format row promises stereo, not because the encoder refuses mono as the old seat's did. Perf, named: MP3 encode wall is 0.05x the old seat on the 30 s clip (4.3 s vs 0.20 s CBR; the old shim encoded blocks across all cores, the new seat is single-threaded sequential) -- the price of the quality and gapless-exactness above; Vorbis at the seat's operating point is 1.4x faster than the old seat (49 ms vs 69 ms per 30 s). audio_export and backends suites green (14 + 8), export lib tests green, cargo check -p engine --all-targets zero warnings; lock carries no rusty_mp3/rusty_vorbis stanza. --- Cargo.lock | 32 +++-- crates/app/src/export_ui.rs | 2 +- crates/app/src/player/export.rs | 2 +- crates/app/src/tests/media.rs | 2 +- crates/engine/Cargo.toml | 37 +++-- crates/engine/src/caps.rs | 4 +- crates/engine/src/export.rs | 215 +++++++++++++--------------- crates/engine/tests/audio_export.rs | 29 ++-- 8 files changed, 159 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4d1ad8..1f5cad3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1647,6 +1647,14 @@ dependencies = [ "ec-core", ] +[[package]] +name = "ec-mp3" +version = "0.1.0" +dependencies = [ + "ec-core", + "ec-dsp", +] + [[package]] name = "ec-opus" version = "0.1.0" @@ -1657,6 +1665,14 @@ dependencies = [ "wide", ] +[[package]] +name = "ec-vorbis" +version = "0.1.0" +dependencies = [ + "ec-core", + "ec-dsp", +] + [[package]] name = "ec-vp8" version = "0.1.0" @@ -1717,7 +1733,9 @@ dependencies = [ "ec-av1", "ec-core", "ec-h264", + "ec-mp3", "ec-opus", + "ec-vorbis", "flacenc", "hound", "image", @@ -1733,8 +1751,6 @@ dependencies = [ "oxideav-subtitle", "rav1e", "rusty_aac", - "rusty_mp3", - "rusty_vorbis", "symphonia", "symphonia-codec-aac", "symphonia-core", @@ -5097,18 +5113,6 @@ checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" name = "rusty_aac" version = "0.5.0" -[[package]] -name = "rusty_mp3" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a99f339ec9a2d84eec8fc2a42baa92426966b25238c498e79d57b4e49b14fbad" - -[[package]] -name = "rusty_vorbis" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6eddf79e697ce9279ef0e012432cd2889a7b6305a45efb5bb16e8a1d6338646f" - [[package]] name = "rustybuzz" version = "0.14.1" diff --git a/crates/app/src/export_ui.rs b/crates/app/src/export_ui.rs index 1f22131..7cd1946 100644 --- a/crates/app/src/export_ui.rs +++ b/crates/app/src/export_ui.rs @@ -119,7 +119,7 @@ pub(crate) const FORMATS: [(&[Format], &str, &str, &str); 9] = [ &[Format::Ogg], "o", "OGG", - "Vorbis (rusty_vorbis) — quality-coded, stereo", + "Vorbis (ec-vorbis) — quality-coded, stereo", ), (&[], "", "VP9", "AV1 above replaces it"), (&[], "", "VP8", "AV1 above replaces it"), diff --git a/crates/app/src/player/export.rs b/crates/app/src/player/export.rs index 2f33f2a..122bf66 100644 --- a/crates/app/src/player/export.rs +++ b/crates/app/src/player/export.rs @@ -358,7 +358,7 @@ impl Player { // A format with no picture has no seat to probe -- and the *last* // format's is not its answer: cleared rather than left standing, or // picking MP3 after AV1 would read "SW encode (rav1e) · MP3 · SW - // (rusty_mp3)", which names an encoder that will not run. + // (ec-mp3)", which names an encoder that will not run. if !settings.format.has_video() { self.export_seat = None; return; diff --git a/crates/app/src/tests/media.rs b/crates/app/src/tests/media.rs index 2b89ee9..f4bb4eb 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -650,7 +650,7 @@ fn every_codec_row_is_offered_or_says_why_not() { .expect("OGG has a row"); assert_eq!(row, [Format::Ogg], "OGG is a row that picks now"); assert!( - detail.contains("rusty_vorbis"), + detail.contains("ec-vorbis"), "the row names the encoder like every other live one: {detail}" ); assert!( diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 852d67b..016aef4 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -81,21 +81,28 @@ ec-av1 = { path = "../../../edith_codecs/crates/ec-av1" } # `vendor/rusty_aac` -- see the patch table in the workspace manifest. Its # *decoder* left the tree when `ec-aac` above took the multichannel seat. rusty_aac = "0.5" -# The MP3 encoder, so that format is a row a user can pick rather than a licence -# note: pure Rust, Apache-2.0, CBR and VBR. The decoder side of mp3 is -# symphonia's below -- an mp3 has always been importable here, and until this it -# was the one format the editor could read and never write. -rusty_mp3 = "0.6" -# The Vorbis encoder, same family and the same reason as the three above: pure -# Rust, Apache-2.0, no C and no FFI. It is the *first* permissively-licensed -# pure-Rust Vorbis encoder -- `vorbis_rs` and `vorbis-encoder` are both bindings -# to Xiph's C library, which this project's no-install rule forbids, and -# `oxideav-vorbis` (whose own manifest calls itself an "orphan-rebuild scaffold") -# ships encoder-side *bitstream writers* with no analysis stage at all: no psy -# model, no floor fit, no residue quantiser, nothing that turns PCM into a -# packet. This one does the whole pipeline. Its quirks are measured and worked -# around in [`crate::export::write_ogg`] -- read that before touching this line. -rusty_vorbis = "0.1.1" +# The MP3 encode seat, the family's own: MPEG-1/2/2.5 Layer III, CBR and true +# VBR (a quality in [0, 1] whose mean tracks its own bitrate ladder), with a +# Xing/Info gapless header whose delay/padding fields are *measured* correct. +# It took the seat the registry crate `rusty_mp3` held -- same public shape +# (`Mp3EncoderConfig { bitrate_kbps, vbr_quality }`, push/finish/next_packet), +# which is why the swap stops at this line and the use sites. The decoder side +# of mp3 is symphonia's below -- an mp3 has always been importable here, and +# until 2026-08 this was the one format the editor could read and never write. +ec-mp3 = { path = "../../../edith_codecs/crates/ec-mp3" } +# The Vorbis encode seat, the family's own: a full Vorbis I encoder -- its own +# floor fit, residue quantiser and per-stream codebooks, block switching +# included -- where the registry crates with encoders are either C bindings +# (`vorbis_rs`, `vorbis-encoder`: the project's no-install rule forbids both) +# or bitstream writers with no analysis stage at all (`oxideav-vorbis`, whose +# own manifest calls itself an "orphan-rebuild scaffold"). It took the seat the +# first permissively-licensed pure-Rust encoder, `rusty_vorbis`, held. The old +# seat emitted long blocks only and numbered its first packet a hop early, so +# [`crate::export::write_ogg`] carried a pre-roll and two granule corrections +# around it; this one reports spec granules and clamps its final one to the +# sample count fed, which let the workaround go. See [`crate::export::write_ogg`] +# for the measurement that decided it. +ec-vorbis = { path = "../../../edith_codecs/crates/ec-vorbis" } # Symphonia has no Opus at any version -- its feature list stops at vorbis and # there is no `symphonia-codec-opus` crate -- while both of its containers *read* # the format already: `A_OPUS` and `OpusHead` have always mapped to diff --git a/crates/engine/src/caps.rs b/crates/engine/src/caps.rs index 4105215..e254547 100644 --- a/crates/engine/src/caps.rs +++ b/crates/engine/src/caps.rs @@ -39,11 +39,11 @@ const SW_CODECS: [(&str, &str, bool, bool); 10] = [ // Decode is split by width: symphonia carries stereo and narrower, `ec-aac` // everything wider; the encoder is `rusty_aac`. ("AAC", "symphonia / ec-aac / rusty_aac", true, true), - ("MP3", "symphonia / rusty_mp3", true, true), + ("MP3", "symphonia / ec-mp3", true, true), ("FLAC", "symphonia / flacenc", true, true), ("PCM", "symphonia / hound", true, true), ("AC-3", "ec-ac3", true, false), - ("Vorbis", "symphonia / rusty_vorbis", true, true), + ("Vorbis", "symphonia / ec-vorbis", true, true), ("ALAC", "symphonia", true, false), ]; diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 8829d83..8e18e00 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -23,7 +23,7 @@ //! equalized one, a source that is not AAC inside an mp4's own sample table — //! the sound is decoded, mixed and encoded again with `rusty_aac` //! ([`encode_audio`]). The audio-only formats have no such split: `hound` writes -//! PCM, `flacenc` encodes FLAC, `rusty_mp3` encodes MP3 and `rusty_vorbis` +//! PCM, `flacenc` encodes FLAC, `ec-mp3` encodes MP3 and `ec-vorbis` //! encodes Vorbis, so those are always *decoded* out of the timeline. The worker owns everything: the caller gets an //! [`ExportHandle`] and polls it from its render loop. //! @@ -123,7 +123,7 @@ const MAX_EXPLICIT_BITRATE: u64 = 50_000_000; /// The rates the sound may be coded at, smallest first -- what a front-end /// offers and what [`ExportSettings::audio_kbps`] is clamped into. Every one of /// them is a legal MPEG-1 Layer III value, so the MP3 path writes the figure it -/// was given rather than the nearest one `rusty_mp3` snaps to. +/// was given rather than the nearest one `ec-mp3` snaps to. pub const AUDIO_KBPS: [u32; 4] = [128, 192, 256, 320]; /// What the sound is coded at when nobody picked: the rate this project wrote @@ -156,9 +156,9 @@ const HEVC_QP_MAX: i32 = 40; /// What an export writes. H.264-in-mp4, AV1 and HEVC -- each of the last two in /// Matroska or in mp4, the user's pick of container -- are the *video* pairs /// with both an encoder and a decoder under this project's no-install rule, and -/// WAV, FLAC, MP3 and Ogg Vorbis are the standalone audio formats with a -/// pure-Rust encoder: `hound`, `flacenc`, `rusty_mp3` and `rusty_vorbis`. Opus -/// still has none -- `oxideav-opus` encodes CELT alone, which is half a codec. +/// pure-Rust encoder: `hound`, `flacenc`, `ec-mp3` and `ec-vorbis`. Opus +/// still has none as a *file* -- the Matroska seat is `ec-opus`, but a standalone +/// `.opus` row this project does not offer. /// (AAC has one too -- `rusty_aac`, which is what a re-encoded video track's /// sound leaves through -- but AAC is a container's own audio, never a file of /// its own here.) VP9 and VP8 are the @@ -198,10 +198,10 @@ pub enum Format { Wav, /// The audio lanes alone, losslessly compressed. Flac, - /// The audio lanes alone, MPEG-1 Layer III CBR (`rusty_mp3`) at the rate + /// The audio lanes alone, MPEG-1 Layer III CBR (`ec-mp3`) at the rate /// [`ExportSettings::audio_kbps`] asks for. Mp3, - /// The audio lanes alone, Vorbis I in an Ogg container -- `rusty_vorbis` + /// The audio lanes alone, Vorbis I in an Ogg container -- `ec-vorbis` /// encoding, `oxideav-ogg` paging. Quality-coded and not rate-coded, which /// is why [`audio_rate_refusal`] takes the Sound row away here: see /// [`VORBIS_QUALITY`] for the operating point and how it was measured. @@ -846,8 +846,8 @@ fn audio_label( match format { Format::Wav => "PCM · SW (hound)", Format::Flac => "FLAC · SW (flacenc)", - Format::Mp3 => "MP3 · SW (rusty_mp3)", - Format::Ogg => "Vorbis · SW (rusty_vorbis)", + Format::Mp3 => "MP3 · SW (ec-mp3)", + Format::Ogg => "Vorbis · SW (ec-vorbis)", _ => match copied.unwrap_or(!forces_encode(project, ranged)) { true => "AAC copy", // A Matroska file gets Opus where the mix allows it -- 48 kHz and @@ -918,12 +918,12 @@ pub fn audio_rate_refusal(project: &Project, format: Format, ranged: bool) -> Op match format { _ if !has_sound(project) => Some("no sound to write"), Format::Wav | Format::Flac => Some("lossless — the samples themselves, at no rate"), - // Vorbis has a quality knob and no rate knob, and the two are not the - // same control wearing different words: measured across this suite's - // fixtures, the whole usable quality band lands between 55 and 175 kbps - // ([`VORBIS_QUALITY`]), so two of the four rates on offer here are not - // rates it can reach at all. A live row would print a figure the file - // does not hold, which is the one thing this card is built not to do. + // Vorbis is written at one measured operating point ([`VORBIS_QUALITY`] + // + [`VORBIS_TARGET_BPS`]), not at a rate a user may pick: its actual + // landing moves with the content around that target (124-134 kbps + // across this suite's bench inputs), so a live rate row would print a + // figure the file does not hold -- the one thing this card is built + // not to do. Format::Ogg => Some("quality-coded — Vorbis holds no rate to pick"), Format::Mp3 => None, _ if forces_encode(project, ranged) => None, @@ -3318,20 +3318,21 @@ fn write_wav(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<() Ok(()) } -/// The same samples as MPEG-1 Layer III, `rusty_mp3` doing the encoding -- pure -/// Rust like every other encoder here, and the reason this format is a row at -/// all: the LGPL `shine-rs` was the only one when it was not. +/// The same samples as MPEG-1 Layer III, `ec-mp3` doing the encoding -- the +/// family's own, taking the seat at the same contract (CBR in, frames out, +/// `Mp3EncoderConfig`'s fields are the old seat's names), so the swap cost the +/// call sites nothing. /// /// CBR at the caller's rate, [`DEFAULT_AUDIO_KBPS`] where there was none. Every /// offered rate is already a legal Layer III value, so the encoder's snap is a /// backstop and not a silent substitution; a sample rate MPEG has no frame for -/// (anything but 8-48 kHz) is refused there by name rather than written as -/// something else. +/// (anything but the nine MPEG rates 8-48 kHz) is refused there by name rather +/// than written as something else. /// /// corner-cut: CBR only -- the card offers rates and not a quality index. Upgrade /// path is `Mp3EncoderConfig::vbr_quality` behind a setting of its own. fn write_mp3(out: &Path, samples: &[i32], audio: &AudioMeta, kbps: u32) -> crate::Result<()> { - let mut encoder = rusty_mp3::Mp3Encoder::new(rusty_mp3::Mp3EncoderConfig { + let mut encoder = ec_mp3::encode::Mp3Encoder::new(ec_mp3::encode::Mp3EncoderConfig { bitrate_kbps: kbps, vbr_quality: None, }); @@ -3353,72 +3354,58 @@ fn write_mp3(out: &Path, samples: &[i32], audio: &AudioMeta, kbps: u32) -> crate Ok(()) } -/// The Vorbis quality every `.ogg` this program writes is coded at, on -/// `rusty_vorbis`'s normalised `[0, 1]` scale -- about `-q 8.35` on the scale -/// Vorbis itself is spoken in (`quality01_from_vorbis_q`). +/// Where the Vorbis rate loop starts, on `ec-vorbis`'s normalised `[0, 1]` +/// scale, beside [`VORBIS_TARGET_BPS`] -- the two numbers every `.ogg` this +/// program writes is coded at. /// -/// A measured number, not a taste. The 2026-08-11 bench encoded three of this -/// suite's fixtures (`test_tone_48k.wav`, `test_av.mp4`, `test_tone.mp3`) at -/// every quality from 0.40 to 0.95 and decoded each back with **symphonia** -- -/// an independent decoder, and the very one [`crate::audio`] reopens an export -/// with. Two things came out of it: -/// -/// * **Below 0.50 the encoder is quiet, not just coarse.** The decoded RMS -/// against the source's runs 0.83-0.94 at quality 0.40 (a 1 to 1.6 dB level -/// error), and at 0.10 and below the file decodes to near-silence -- the same -/// class of failure the AC-3 mono downmix has in `crate::audio`. From 0.55 up -/// the level sits at 1.000-1.009, which is what a lossy codec should do. -/// * **The band tops out well under the rates the Sound row offers.** Quality -/// 0.85 measured 176, 144 and 148 kbps on the three fixtures; 0.95 only -/// reaches ~200. That is why [`audio_rate_refusal`] takes the row away rather -/// than mapping 128/192/256/320 onto quality steps: the top two are numbers -/// this encoder cannot produce, and a card that printed them would be lying. -/// -/// 0.85 is the top of the band where every fixture still decoded to *exactly* -/// its own length (0.95 overshot one by 820 samples), which is the property -/// [`write_ogg`] is built around. +/// A measured operating point, not a taste (2026-09-13 bench, every output +/// decoded back with **symphonia** -- an independent decoder, and the very one +/// [`crate::audio`] reopens an export with). The incumbent seat's own 0.85 -- +/// its top quality that still decoded every fixture to exactly its own length +/// -- measured 124-176 kbps across this suite's fixtures with correlation +/// 0.9947-0.9957 against the source. The quality knob alone on the new seat +/// saturates well under that band (0.98 lands ~87 kbps on a 30-second music +/// clip), so the seat is driven the way its own rate gates are measured: the +/// loop starts at 0.85 and is held to a 128 kbps target, which landed +/// 124-134 kbps actual on the same inputs and beat the incumbent at its own +/// operating point on every one of them -- correlation 0.9968 against 0.9947 +/// on the music clip, +3.1 dB PSNR -- at the same size of file. Against +/// ffmpeg's own libvorbis at `-q 5` (125.8 kbps actual on the same clip) it +/// reads 0.9968 against 0.9973: par at the same rate. const VORBIS_QUALITY: f32 = 0.85; -/// The Vorbis block hop, and the size of the pre-roll [`write_ogg`] feeds the -/// encoder. `rusty_vorbis` emits long blocks only (`N = 2048`, hop `N/2`). -const VORBIS_HOP: usize = 1024; +/// The rate [`VORBIS_QUALITY`]'s loop is held to, in bits per second. Measured +/// landing within +1..+5% of target across the bench's inputs; the Sound row +/// stays refused for this format because this target is the encoder's measured +/// operating point, not a number a card may offer. +const VORBIS_TARGET_BPS: i32 = 128_000; -/// The same samples as Vorbis I in an Ogg container: `rusty_vorbis` encodes, +/// The same samples as Vorbis I in an Ogg container: `ec-vorbis` encodes, /// `oxideav-ogg` pages. Pure Rust on both halves, like every other encoder here. /// -/// Two corrections stand between that library pair and a file this project would -/// ship, and both are measured rather than guessed (2026-08-11, decoded back -/// with symphonia every time): -/// -/// 1. **One hop of pre-roll.** `rusty_vorbis` advances the granule by a full hop -/// on its *first* audio packet, which its own doc comment says decodes to -/// zero samples. Written straight through, every sample lands ~1024 early: -/// a marker signal whose only energy was its first and last 1024 samples came -/// back with the head at 36% of its amplitude and a full-scale burst 1500 -/// samples *before* the real one, in what should have been silence. Feeding -/// [`VORBIS_HOP`] samples of silence ahead of the mix and subtracting the same -/// from every granule puts it back: head 0.86 against an input peak of 0.80, -/// the phantom burst down to 0.04, the real burst at its own place. The -/// decoder drops the pre-roll itself -- a first page whose granule is under -/// what its packets decode to is exactly how Ogg says "skip this much". -/// 2. **The tail is declared, not encoded.** The block grid overshoots the last -/// sample, so the stream is padded out and the *last* page's granule is set -/// to the timeline's own frame count. A decoder trims to it, which is what -/// makes an `.ogg` here exactly as long as the WAV of the same timeline -- -/// the promise [`run_audio`] makes for every other audio format. +/// The old seat's two corrections are gone with it, and the going is measured +/// rather than assumed (2026-09-13, every output decoded back with symphonia). +/// This encoder numbers its packets the spec's way -- the first audio packet +/// at granule 0, every packet after at the absolute sample position its block +/// ends on -- and clamps its final granule to the sample count it was fed, so +/// a decoder trims the block grid's tail on its own. Written straight through, +/// every file decodes to exactly the timeline's frame count with the +/// correlation peak at lag zero, which is what the one-hop pre-roll and the +/// two granule corrections this function used to carry around the old encoder +/// were for. /// -/// **Stereo, always.** The embedded setup header `rusty_vorbis` ships is a -/// stereo profile: a mono push is refused outright ("bad coupling channels"), -/// and so is anything wider. A mono mix is therefore written as dual mono, both -/// channels bit-identical (measured: `max|L-R|` is exactly 0, and Vorbis's own -/// channel coupling codes the empty side channel for almost nothing). Wider than -/// stereo cannot reach here -- [`crate::audio`] folds 5.1 to stereo when the -/// source is opened -- but it is refused by name rather than left to the -/// library's own message. +/// **Stereo, always.** The format row promises stereo, so a mono mix is +/// written as dual mono, both channels bit-identical (measured: `max|L-R|` is +/// exactly 0; the encoder's own channel coupling codes the empty side channel +/// for almost nothing). The encoder would serve a mono stream, but the row +/// says stereo and folding the file back down must give exactly what was +/// mixed. Wider than stereo cannot reach here -- [`crate::audio`] folds 5.1 +/// to stereo when the source is opened -- but it is refused by name rather +/// than left to the library's own message. /// -/// corner-cut: one quality, [`VORBIS_QUALITY`], with no user control -- the Sound -/// row is refused for this format because the rates it offers are not rates -/// Vorbis reaches. Upgrade path is a quality picker of its own, worth building +/// corner-cut: one operating point, [`VORBIS_QUALITY`] + +/// [`VORBIS_TARGET_BPS`], with no user control -- the Sound row is refused for +/// this format. Upgrade path is a quality picker of its own, worth building /// the day the card has a control that speaks in quality rather than kbps. fn write_ogg(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<()> { if audio.channels > 2 { @@ -3428,47 +3415,49 @@ fn write_ogg(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<() ) .into()); } - // The mp3 path's conversion, so the two files are one mix, plus the pre-roll - // and (for a mono mix) the dual-mono widening: the same sample in both - // channels is the same signal, not a widening of it, so what a player folds - // back down is what was mixed. - let mut pcm: Vec = vec![0; VORBIS_HOP * 2]; - match audio.channels < 2 { - true => pcm.extend(samples.iter().flat_map(|&s| [s as i16, s as i16])), - false => pcm.extend(samples.iter().map(|&s| s as i16)), - } - // The block grid has to run past the last real sample or the tail is never - // coded at all; the granule below is what trims the padding back off. + // The mp3 path's conversion, so the two files are one mix -- the same + // /32768 scaling this engine's own decoders do -- plus (for a mono mix) + // the dual-mono widening: the same sample in both channels is the same + // signal, not a widening of it, so what a player folds back down is what + // was mixed. + let pcm: Vec = match audio.channels < 2 { + true => samples + .iter() + .flat_map(|&s| [f32::from(s as i16) / 32768.0; 2]) + .collect(), + false => samples + .iter() + .map(|&s| f32::from(s as i16) / 32768.0) + .collect(), + }; let frames = samples.len() / usize::from(audio.channels).max(1); - pcm.resize(pcm.len() + VORBIS_HOP * 4 * 2, 0); - let mut encoder = rusty_vorbis::VorbisEncoder::new(rusty_vorbis::VorbisEncoderConfig { - bitrate_bps: rusty_vorbis::BITRATE_NOMINAL, + let mut encoder = ec_vorbis::VorbisEncoder::new(ec_vorbis::EncoderConfig { + sample_rate: audio.sample_rate, + channels: 2, + bitrate_bps: VORBIS_TARGET_BPS, quality: VORBIS_QUALITY, - }); + }) + .map_err(|e| format!("vorbis encode: {e}"))?; encoder - .push_pcm_s16(&pcm, 2, audio.sample_rate) + .push_interleaved(&pcm) .map_err(|e| format!("vorbis encode: {e}"))?; encoder.finish(); let mut packets = Vec::new(); loop { match encoder.next_packet() { Ok(packet) => packets.push(packet), - Err(rusty_vorbis::Error::Eof) => break, + Err(ec_core::Error::Eof) => break, + Err(ec_core::Error::NeedMore) => continue, Err(e) => return Err(format!("vorbis encode: {e}").into()), } } - // Identification, comment and setup, then at least one audio packet: fewer - // than four and there is no stream to page. - if packets.len() < 4 { - return Err(format!("vorbis encode: {} packets is not a stream", packets.len()).into()); + // The three setup headers travel with the encoder, not in the packet + // stream; with no audio packets there is no stream to page. + if packets.is_empty() { + return Err("vorbis encode: no audio packets is not a stream".into()); } - let headers = [ - &packets[0].data[..], - &packets[1].data[..], - &packets[2].data[..], - ]; - let extradata = oxideav_ogg::mux::xiph_lace(&headers) + let extradata = oxideav_ogg::mux::xiph_lace(&encoder.headers()) .ok_or("vorbis encode: the three headers would not lace")?; let time_base = oxideav_core::TimeBase::new(1, i64::from(audio.sample_rate)); @@ -3492,18 +3481,14 @@ fn write_ogg(out: &Path, samples: &[i32], audio: &AudioMeta) -> crate::Result<() muxer .write_header() .map_err(|e| format!("ogg mux: {e:?}"))?; - let last = packets.len() - 1; - for (i, packet) in packets.iter().enumerate().skip(3) { - // Correction 1 on every packet, correction 2 on the last one. - let granule = match i == last { - true => frames as i64, - false => (packet.pts - VORBIS_HOP as i64).max(0), - }; + for packet in &packets { + // The granule is the encoder's own: the absolute sample position this + // packet ends at, clamped to the mix's length on the last one. muxer .write_packet( &oxideav_core::Packet::new(0, time_base, packet.data.clone()) - .with_pts(granule) - .with_duration(packet.duration), + .with_pts(packet.granule) + .with_duration(packet.samples), ) .map_err(|e| format!("ogg mux: {e:?}"))?; } diff --git a/crates/engine/tests/audio_export.rs b/crates/engine/tests/audio_export.rs index 0587695..ca1ba1f 100644 --- a/crates/engine/tests/audio_export.rs +++ b/crates/engine/tests/audio_export.rs @@ -273,7 +273,7 @@ fn exports_the_timeline_as_a_flac() { std::fs::remove_file(&out).unwrap(); } -/// The third audio row, and the one that used to be a licence note: `rusty_mp3` +/// The third audio row, and the one that used to be a licence note: `ec-mp3` /// encodes it, and the file is read straight back through symphonia's mp3 /// decoder -- the very door an mp3 *import* comes in through, so an export can /// be dropped back onto the timeline it came from. @@ -872,17 +872,17 @@ fn a_51_ac3_source_round_trips_through_a_wav() { } /// The fourth audio row, and the one that used to be a refusal: Vorbis in an Ogg -/// container, `rusty_vorbis` encoding and `oxideav-ogg` paging, read straight +/// container, `ec-vorbis` encoding and `oxideav-ogg` paging, read straight /// back through the engine's own reader like every other export here. /// /// The shape is asserted *exactly* -- `assert_timeline_shape`, the same the WAV -/// and the FLAC are held to, and not the MP3's within-a-frame slack. That is the -/// whole point of the two corrections in `export::write_ogg`: the encoder's own -/// granule numbering puts every sample a block early and its block grid overruns -/// the tail, so written straight through, this file would be a hop out of step -/// and short of the timeline. The 1 Hz pulse is what proves the *timing* rather -/// than merely the length -- a stream shifted by a block lands its peak nearer -/// where the dip belongs. +/// and the FLAC are held to, and not the MP3's within-a-frame slack. The old +/// seat needed two corrections in `export::write_ogg` to earn that (its granule +/// numbering ran a hop early and its block grid overran the tail); `ec-vorbis` +/// reports spec granules and clamps its last one to the mix's own length, so +/// the file is written straight through and still lands here. The 1 Hz pulse +/// is what proves the *timing* rather than merely the length -- a stream +/// shifted by a block lands its peak nearer where the dip belongs. #[test] fn exports_the_timeline_as_an_ogg_vorbis() { let out = out_path("ogg", "ogg"); @@ -1108,12 +1108,11 @@ fn a_split_and_crossfaded_clip_has_no_muted_interior() { std::fs::remove_file(&src).unwrap(); } -/// A **mono** timeline through the same door. `rusty_vorbis` ships one embedded -/// setup header and it is a stereo profile -- a mono push is refused outright -/// ("bad coupling channels") -- so `export::write_ogg` writes the mix as dual -/// mono. This is the test that it is *dual mono* and not a widening: both -/// channels come back bit-identical, so folding the file back down gives exactly -/// what was mixed. +/// A **mono** timeline through the same door. The format row promises stereo, +/// so `export::write_ogg` writes a mono mix as dual mono (the encoder itself +/// would serve a mono stream; the row is the contract). This is the test that +/// it is *dual mono* and not a widening: both channels come back +/// bit-identical, so folding the file back down gives exactly what was mixed. #[test] fn a_mono_timeline_exports_as_dual_mono_ogg() { // A mono fixture, written here because the suite has none: one second of