ReviewOS

also looking at this

home-lang/home

feat(audio): add verified AAC-LC encoding and reject invalid prototype output

#918
Merged chris wants to merge feat/aac-mp4-recording into main
10 files +318 -471
packages/audio/NATIVE_AAC.mdadded+41-0
Changes to packages/audio/NATIVE_AAC.md
@@ -0,0 +1,41 @@
1# Native AAC-LC encoding
2
3`src/encoding/native_aac.{c,h,zig}` is Home's opt-in streaming AAC-LC adapter.
4It links the standalone FDK AAC library directly, without FFmpeg/Libav. The C
5boundary uses the installed headers and opaque handles; no FDK layouts are
6redeclared in Zig. Home's adapter is MIT; the FDK library retains its own license.
7
8The older `AacEncoder` and `AacFullEncoder` prototypes emitted envelope bytes or
9fixed-width coefficients as if they were AAC. Their encode entry points now
10return `UnsupportedLegacyEncoder`. Configuration helpers remain available;
11use `audio.NativeAacEncoder` or `audio.encoding.native_aac.Encoder` for real AAC.
12The old prototype's partial-allocation cleanup is also fixed.
13
14Compile `native_aac.c` with the system compiler, include the installed FDK header
15root and link `fdk-aac`. Instantiate with an explicit sample rate, mono/stereo
16channels and bitrate in bits/s. Supported rates are 8, 11.025, 12, 16, 22.05, 24,
1732, 44.1 and 48kHz. Bitrate range is 16–320kb/s; unsupported combinations fail.
18No resampling, HE-AAC, implicit input device access or ADTS transport is included.
19
20`encode` accepts 1–1024 S16LE interleaved frames and returns the number consumed
21plus an optional raw access unit. Retain and feed any unconsumed input. Borrowed
22packet bytes expire at the next call. Fixed input/output buffers bound memory;
23there is no frame-sized growing history. Serialize calls on each encoder.
24
25The returned `Info` supplies the real AudioSpecificConfig, frame length and
26codec delay in samples. Keep it with the stream. Drain `flush` until `null`, then
27close. Encoding after drain fails. A codec failure poisons subsequent calls.
28The muxer must account for codec delay and final zero padding; do not timestamp
29packets from callback time or assume the codec delay is 1024/2048 samples.
30
31Validation uses FDK 2.0.3 at commit
32`716f4394641d53f0d79c9ddac3fa93b03a49f278`. Targeted tests exercise partial PCM,
33configuration bytes, delayed drain, padding coverage and state rejection on Zig
340.15.2 and Home's development toolchain. Independent camera integration decodes
35with FAAD2 and checks waveforms, segment sample counts and MP4 edit timelines.
36See `.github/workflows/native-aac.yml`; no hardware performance claim is made.
37
38```sh
39zig test packages/audio/src/encoding/native_aac_test.zig \
40 packages/audio/src/encoding/native_aac.c -lc -lfdk-aac
41```