Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

M6.2: own AAC-LC decoder replaces the symphonia bootstrap for AAC; AAC encoder for MP4 export

Brandon Thomasefc617cb977a
diff --git a/Cargo.lock b/Cargo.lock
index f5d13472b..efbea46ba 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1348,6 +1348,7 @@ dependencies = [
name = "filmcraft-codecs"
version = "0.1.0"
dependencies = [
+ "filmcraft-aac",
"filmcraft-bitstream",
"filmcraft-color",
"filmcraft-frame",
@@ -1405,6 +1406,7 @@ dependencies = [
name = "filmcraft-export"
version = "0.1.0"
dependencies = [
+ "filmcraft-aac",
"filmcraft-codecs",
"filmcraft-frame",
"filmcraft-isobmff",
diff --git a/apps/filmcraft/src/native_menu.rs b/apps/filmcraft/src/native_menu.rs
index aa6a158a6..e9a562a5a 100644
--- a/apps/filmcraft/src/native_menu.rs
+++ b/apps/filmcraft/src/native_menu.rs
@@ -14,13 +14,8 @@ fn accel(s: &str) -> Option<Accelerator> {
if !(s.contains("Cmd+") || s.contains("Ctrl+") || s.contains("Alt+")) {
return None;
}
- let mapped = s
- .replace("Cmd+", "CmdOrCtrl+")
- .replace(";", "Semicolon")
- .replace("'", "Quote")
- .replace("/", "Slash")
- .replace("=", "Equal")
- .replace("\\", "Backslash");
+ let mapped =
+ s.replace("Cmd+", "CmdOrCtrl+").replace(";", "Semicolon").replace("'", "Quote").replace("/", "Slash").replace("=", "Equal").replace("\\", "Backslash");
mapped.parse().ok()
}
diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml
index aad209a15..67f6ff007 100644
--- a/crates/codecs/Cargo.toml
+++ b/crates/codecs/Cargo.toml
@@ -18,6 +18,7 @@ filmcraft-isobmff = { workspace = true }
filmcraft-bitstream = { workspace = true }
filmcraft-h264 = { workspace = true }
filmcraft-prores = { workspace = true }
+filmcraft-aac = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
image = { version = "0.25", default-features = false, features = ["jpeg"] }
diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs
index c3386454c..31e92e9c5 100644
--- a/crates/codecs/src/audio.rs
+++ b/crates/codecs/src/audio.rs
@@ -14,9 +14,16 @@ use symphonia::core::probe::Hint;
use crate::{CodecError, Result};
+enum Inner {
+ /// Our own AAC-LC decoder.
+ Aac { dec: filmcraft_aac::Decoder, asc: Vec<u8> },
+ /// Bootstrap decoders (MP3, ALAC, FLAC, HE-AAC…) via symphonia.
+ Symphonia(Box<dyn Decoder>),
+}
+
/// A packet decoder producing planar f32.
pub struct PacketDecoder {
- dec: Box<dyn Decoder>,
+ inner: Inner,
pub channels: usize,
}
@@ -28,9 +35,14 @@ impl PacketDecoder {
p.with_extra_data(x.into_boxed_slice());
}
let dec = symphonia::default::get_codecs().make(&p, &DecoderOptions::default()).map_err(|e| CodecError::Unsupported(e.to_string()))?;
- Ok(Self { dec, channels: 0 })
+ Ok(Self { inner: Inner::Symphonia(dec), channels: 0 })
}
+ /// AAC: our decoder for AAC-LC; symphonia for other object types (HE-AAC…).
pub fn aac(asc: &[u8], sample_rate: u32) -> Result<Self> {
+ let lc = asc.first().map(|b| b >> 3) == Some(2);
+ if lc && let Ok(dec) = filmcraft_aac::Decoder::new(asc) {
+ return Ok(Self { inner: Inner::Aac { dec, asc: asc.to_vec() }, channels: 0 });
+ }
Self::new(CODEC_TYPE_AAC, sample_rate, Some(asc.to_vec()))
}
pub fn for_isobmff(c: &filmcraft_isobmff::CodecConfig, rate: u32) -> Result<Self> {
@@ -45,8 +57,16 @@ impl PacketDecoder {
}
/// Decode one packet into planar channels.
pub fn decode(&mut self, data: &[u8], ts: u64) -> Result<Vec<Vec<f32>>> {
+ let dec = match &mut self.inner {
+ Inner::Aac { dec, .. } => {
+ let out = dec.decode(data).map_err(|e| CodecError::Decode(e.to_string()))?;
+ self.channels = out.len();
+ return Ok(out);
+ }
+ Inner::Symphonia(d) => d,
+ };
let pkt = Packet::new_from_slice(0, ts, 0, data);
- let buf = self.dec.decode(&pkt).map_err(|e| CodecError::Decode(e.to_string()))?;
+ let buf = dec.decode(&pkt).map_err(|e| CodecError::Decode(e.to_string()))?;
let spec = *buf.spec();
let ch = spec.channels.count().max(1);
self.channels = ch;
@@ -63,7 +83,14 @@ impl PacketDecoder {
Ok(out)
}
pub fn reset(&mut self) {
- self.dec.reset();
+ match &mut self.inner {
+ Inner::Aac { dec, asc } => {
+ if let Ok(d) = filmcraft_aac::Decoder::new(asc) {
+ *dec = d;
+ }
+ }
+ Inner::Symphonia(d) => d.reset(),
+ }
}
}
diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml
index b5f24cbc6..adac1ea12 100644
--- a/crates/export/Cargo.toml
+++ b/crates/export/Cargo.toml
@@ -17,6 +17,7 @@ filmcraft-project = { workspace = true }
filmcraft-render = { workspace = true }
filmcraft-isobmff = { workspace = true }
filmcraft-prores = { workspace = true }
+filmcraft-aac = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index ffdc76675..2ccaba5e4 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -182,7 +182,7 @@ fn video_factories() -> &'static RwLock<Vec<EncoderFactory>> {
}
fn audio_factories() -> &'static RwLock<Vec<AudioEncoderFactory>> {
static F: OnceLock<RwLock<Vec<AudioEncoderFactory>>> = OnceLock::new();
- F.get_or_init(|| RwLock::new(Vec::new()))
+ F.get_or_init(|| RwLock::new(vec![aac_factory]))
}
pub fn register_encoder(f: EncoderFactory) {
@@ -233,6 +233,40 @@ fn mjpeg_factory(format: Format, w: u32, h: u32, rate: FrameRate, s: &ExportSett
(format == Format::Mjpeg).then(|| Ok(Box::new(MjpegEncoder { w: w as u16, h: h as u16, quality: s.quality.clamp(1, 100), rate }) as Box<dyn VideoEncoder>))
}
+/// AAC-LC (our encoder), 320 kbps stereo by default.
+struct AacEncoder {
+ enc: filmcraft_aac::Encoder,
+ rate: u32,
+ channels: u32,
+}
+
+impl AudioEncoder for AacEncoder {
+ fn sample_entry(&self) -> SampleEntry {
+ SampleEntry::aac(self.enc.audio_specific_config(), self.channels, self.rate)
+ }
+ fn encode(&mut self, planar: &[Vec<f32>]) -> Result<Vec<Vec<u8>>> {
+ let refs: Vec<&[f32]> = planar.iter().map(Vec::as_slice).collect();
+ Ok(self.enc.encode(&refs))
+ }
+ fn flush(&mut self) -> Result<Vec<Vec<u8>>> {
+ Ok(self.enc.flush())
+ }
+ fn priming(&self) -> u32 {
+ self.enc.priming_samples()
+ }
+ fn frame_size(&self) -> u32 {
+ 1024
+ }
+}
+
+fn aac_factory(_format: Format, sample_rate: u32, channels: u32, _s: &ExportSettings) -> Option<Result<Box<dyn AudioEncoder>>> {
+ Some(
+ filmcraft_aac::Encoder::new(filmcraft_aac::EncoderConfig::cbr(sample_rate, channels as usize, 320_000))
+ .map(|enc| Box::new(AacEncoder { enc, rate: sample_rate, channels }) as Box<dyn AudioEncoder>)
+ .map_err(|e| ExportError::Encode(e.to_string())),
+ )
+}
+
/// ProRes 422 HQ encoder: sRGB/709 RGBA8 → 10-bit limited-range BT.709 4:2:2.
struct ProResEncoder {
enc: filmcraft_prores::Encoder,