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