merge ws/heaac

This commit is contained in:
4DBug committed 2026-11-02 01:32:15 -06:00
commit dde6be117c
32 files changed
+18681 -1371

No files matched your search

+4269 -35
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
Binary file not shown.
+3 -3
View File
@@ -1,5 +1,5 @@
{
"Core_1": [[8, "src/Rosetta/init.luau"], [888, "src/Rosetta/Registry.luau"], [3297, "src/Rosetta/Types.luau"], [4410, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [5164, "src/Rosetta/codecs/image/Yuv.luau"], [5469, "src/Rosetta/codecs/model/Rbx/init.luau"], [5706, "src/Rosetta/codecs/video/Video/init.luau"], [6264, "src/Rosetta/containers/AudioWrap.luau"], [6767, "src/Rosetta/containers/Ogg/Mux.luau"], [6956, "src/Rosetta/core/Bytes.luau"], [7483, "src/Rosetta/core/Crc32.luau"], [7634, "src/Rosetta/core/Err.luau"], [7716, "src/Rosetta/core/Input.luau"], [7792, "src/Rosetta/host/init.luau"]],
"Core_1": [[8, "src/Rosetta/init.luau"], [892, "src/Rosetta/Registry.luau"], [3301, "src/Rosetta/Types.luau"], [4414, "src/Rosetta/codecs/audio/Adpcm/init.luau"], [5168, "src/Rosetta/codecs/image/Yuv.luau"], [5473, "src/Rosetta/codecs/model/Rbx/init.luau"], [5710, "src/Rosetta/codecs/video/Video/init.luau"], [6268, "src/Rosetta/containers/AudioWrap.luau"], [6778, "src/Rosetta/containers/Ogg/Mux.luau"], [6967, "src/Rosetta/core/Bytes.luau"], [7494, "src/Rosetta/core/Crc32.luau"], [7645, "src/Rosetta/core/Err.luau"], [7727, "src/Rosetta/core/Input.luau"], [7803, "src/Rosetta/host/init.luau"]],
"Core_2": [[8, "src/Rosetta/ops/ImageOps/init.luau"], [656, "src/Rosetta/ops/ImageOps/Kernels.luau"]],
"Mpeg2": [[8, "src/Rosetta/codecs/video/Dsp.luau"], [846, "src/Rosetta/codecs/video/Mpeg1/init.luau"], [859, "src/Rosetta/codecs/video/Mpeg2/init.luau"], [1227, "src/Rosetta/codecs/video/Mpeg2/Decoder.luau"], [2754, "src/Rosetta/codecs/video/Mpeg2/Tables.luau"], [2894, "src/Rosetta/codecs/video/Mpeg2/Vlc.luau"]],
"H263": [[8, "src/Rosetta/codecs/video/H263/init.luau"], [165, "src/Rosetta/codecs/video/H263/Decoder.luau"], [1023, "src/Rosetta/codecs/video/H263/Tables.luau"]],
@@ -19,7 +19,7 @@
"Mpa": [[8, "src/Rosetta/codecs/audio/Mpa/init.luau"], [23, "src/Rosetta/codecs/audio/Mpa/Analysis.luau"], [82, "src/Rosetta/codecs/audio/Mpa/Core.luau"], [1740, "src/Rosetta/codecs/audio/Mpa/EncTables.luau"], [2033, "src/Rosetta/codecs/audio/Mpa/Encode2.luau"], [2418, "src/Rosetta/codecs/audio/Mpa/Encode3.luau"], [3059, "src/Rosetta/codecs/audio/Mpa/Header.luau"], [3200, "src/Rosetta/codecs/audio/Mpa/Tables.luau"]],
"Opus": [[8, "src/Rosetta/codecs/audio/Opus/init.luau"], [23, "src/Rosetta/codecs/audio/Opus/Celt.luau"], [1895, "src/Rosetta/codecs/audio/Opus/CeltEncoder.luau"], [2960, "src/Rosetta/codecs/audio/Opus/CeltTables.luau"], [3021, "src/Rosetta/codecs/audio/Opus/Decoder.luau"], [3485, "src/Rosetta/codecs/audio/Opus/Mdct.luau"], [3836, "src/Rosetta/codecs/audio/Opus/Packet.luau"], [4069, "src/Rosetta/codecs/audio/Opus/Silk.luau"], [6111, "src/Rosetta/codecs/audio/Opus/SilkTables.luau"]],
"Vorbis": [[8, "src/Rosetta/codecs/audio/Vorbis/init.luau"], [22, "src/Rosetta/codecs/audio/Vorbis/Codebook.luau"], [406, "src/Rosetta/codecs/audio/Vorbis/Packet.luau"], [979, "src/Rosetta/codecs/audio/Vorbis/Setup.luau"], [1401, "src/Rosetta/codecs/audio/Vorbis/Tables.luau"]],
"Aac": [[8, "src/Rosetta/codecs/audio/Aac/init.luau"], [328, "src/Rosetta/codecs/audio/Aac/Decode.luau"], [1227, "src/Rosetta/codecs/audio/Aac/Tables.luau"]],
"Aac": [[8, "src/Rosetta/codecs/audio/Aac/init.luau"], [416, "src/Rosetta/codecs/audio/Aac/Decode.luau"], [1392, "src/Rosetta/codecs/audio/Aac/Ps.luau"], [1982, "src/Rosetta/codecs/audio/Aac/Sbr.luau"], [3049, "src/Rosetta/codecs/audio/Aac/SbrBits.luau"], [3168, "src/Rosetta/codecs/audio/Aac/SbrDct.luau"], [4410, "src/Rosetta/codecs/audio/Aac/SbrHf.luau"], [4902, "src/Rosetta/codecs/audio/Aac/SbrQmf.luau"], [5019, "src/Rosetta/codecs/audio/Aac/SbrTables.luau"], [5432, "src/Rosetta/codecs/audio/Aac/Tables.luau"]],
"Flac": [[8, "src/Rosetta/codecs/audio/Flac/init.luau"], [20, "src/Rosetta/codecs/audio/Flac/Encode.luau"], [969, "src/Rosetta/codecs/audio/Flac/Frame.luau"]],
"Musepack": [[8, "src/Rosetta/codecs/audio/Musepack/init.luau"], [354, "src/Rosetta/codecs/audio/Musepack/Decode.luau"], [1248, "src/Rosetta/codecs/audio/Musepack/Tables.luau"]],
"WavPack": [[8, "src/Rosetta/codecs/audio/WavPack/init.luau"], [231, "src/Rosetta/codecs/audio/WavPack/Block.luau"], [1686, "src/Rosetta/codecs/audio/WavPack/Tables.luau"]],
@@ -29,7 +29,7 @@
"Draco": [[8, "src/Rosetta/codecs/model/Draco/init.luau"], [340, "src/Rosetta/codecs/model/Draco/Attributes.luau"], [809, "src/Rosetta/codecs/model/Draco/Connectivity.luau"], [1998, "src/Rosetta/codecs/model/Draco/Int64.luau"], [2147, "src/Rosetta/codecs/model/Draco/KdTree.luau"], [2506, "src/Rosetta/codecs/model/Draco/Predict.luau"], [3468, "src/Rosetta/codecs/model/Draco/Reader.luau"]],
"archive_1": [[8, "src/Rosetta/archive/7z/init.luau"], [393, "src/Rosetta/archive/7z/Header.luau"], [920, "src/Rosetta/archive/adf/init.luau"], [1202, "src/Rosetta/archive/applesingle/init.luau"], [1388, "src/Rosetta/archive/ar/init.luau"], [1791, "src/Rosetta/archive/arj/init.luau"], [2001, "src/Rosetta/archive/binhex/init.luau"], [2250, "src/Rosetta/archive/br/init.luau"], [2325, "src/Rosetta/archive/bz2/init.luau"], [2389, "src/Rosetta/archive/cab/init.luau"], [2802, "src/Rosetta/archive/cab/Sum.luau"], [2843, "src/Rosetta/archive/cpio/init.luau"], [3011, "src/Rosetta/archive/d64/init.luau"], [3425, "src/Rosetta/archive/fat/init.luau"], [3547, "src/Rosetta/archive/grp/init.luau"], [3661, "src/Rosetta/archive/gzip/init.luau"], [3846, "src/Rosetta/archive/iso/init.luau"], [3918, "src/Rosetta/archive/lha/init.luau"], [4343, "src/Rosetta/archive/lz4/init.luau"], [4687, "src/Rosetta/archive/macbinary/init.luau"], [4885, "src/Rosetta/archive/mpq/init.luau"], [5291, "src/Rosetta/archive/mpq/Crypt.luau"], [5356, "src/Rosetta/archive/pak/init.luau"], [5490, "src/Rosetta/archive/rpm/init.luau"], [5814, "src/Rosetta/archive/tar/init.luau"], [5932, "src/Rosetta/archive/unitypackage/init.luau"], [6168, "src/Rosetta/archive/uu/init.luau"], [6438, "src/Rosetta/archive/vpk/init.luau"], [6749, "src/Rosetta/archive/vswap/init.luau"], [7019, "src/Rosetta/archive/wad/init.luau"], [7473, "src/Rosetta/archive/wad/Picture.luau"], [7575, "src/Rosetta/archive/wad2/init.luau"], [7857, "src/Rosetta/archive/xz/init.luau"]],
"archive_2": [[8, "src/Rosetta/archive/yenc/init.luau"], [280, "src/Rosetta/archive/z/init.luau"], [376, "src/Rosetta/archive/zip/init.luau"], [595, "src/Rosetta/archive/zoo/init.luau"], [791, "src/Rosetta/archive/zst/init.luau"]],
"audio": [[8, "src/Rosetta/audio/aac/init.luau"], [215, "src/Rosetta/audio/ac3/init.luau"], [362, "src/Rosetta/audio/adpcm/init.luau"], [508, "src/Rosetta/audio/adx/init.luau"], [740, "src/Rosetta/audio/aiff/init.luau"], [1132, "src/Rosetta/audio/ape/init.luau"], [1261, "src/Rosetta/audio/au/init.luau"], [1423, "src/Rosetta/audio/brstm/init.luau"], [1715, "src/Rosetta/audio/caf/init.luau"], [2378, "src/Rosetta/audio/flac/init.luau"], [2527, "src/Rosetta/audio/flac/Meta.luau"], [2749, "src/Rosetta/audio/flac/Stream.luau"], [3119, "src/Rosetta/audio/m4a/init.luau"], [3454, "src/Rosetta/audio/mp3/init.luau"], [3715, "src/Rosetta/audio/mp3/Index.luau"], [3906, "src/Rosetta/audio/mp3/Stream.luau"], [4133, "src/Rosetta/audio/mp3/Tags.luau"], [4445, "src/Rosetta/audio/mpc/init.luau"], [4540, "src/Rosetta/audio/opus/init.luau"], [4919, "src/Rosetta/audio/opus/Encode.luau"], [5031, "src/Rosetta/audio/qoa/init.luau"], [5212, "src/Rosetta/audio/tta/init.luau"], [5331, "src/Rosetta/audio/vag/init.luau"], [5555, "src/Rosetta/audio/voc/init.luau"], [5831, "src/Rosetta/audio/vorbis/init.luau"], [6286, "src/Rosetta/audio/vorbis/Encode.luau"], [7099, "src/Rosetta/audio/wav/init.luau"], [7720, "src/Rosetta/audio/wav/Codecs.luau"], [7899, "src/Rosetta/audio/wavpack/init.luau"]],
"audio": [[8, "src/Rosetta/audio/aac/init.luau"], [221, "src/Rosetta/audio/ac3/init.luau"], [368, "src/Rosetta/audio/adpcm/init.luau"], [514, "src/Rosetta/audio/adx/init.luau"], [746, "src/Rosetta/audio/aiff/init.luau"], [1138, "src/Rosetta/audio/ape/init.luau"], [1267, "src/Rosetta/audio/au/init.luau"], [1429, "src/Rosetta/audio/brstm/init.luau"], [1721, "src/Rosetta/audio/caf/init.luau"], [2388, "src/Rosetta/audio/flac/init.luau"], [2537, "src/Rosetta/audio/flac/Meta.luau"], [2759, "src/Rosetta/audio/flac/Stream.luau"], [3129, "src/Rosetta/audio/m4a/init.luau"], [3472, "src/Rosetta/audio/mp3/init.luau"], [3733, "src/Rosetta/audio/mp3/Index.luau"], [3924, "src/Rosetta/audio/mp3/Stream.luau"], [4151, "src/Rosetta/audio/mp3/Tags.luau"], [4463, "src/Rosetta/audio/mpc/init.luau"], [4558, "src/Rosetta/audio/opus/init.luau"], [4937, "src/Rosetta/audio/opus/Encode.luau"], [5049, "src/Rosetta/audio/qoa/init.luau"], [5230, "src/Rosetta/audio/tta/init.luau"], [5349, "src/Rosetta/audio/vag/init.luau"], [5573, "src/Rosetta/audio/voc/init.luau"], [5849, "src/Rosetta/audio/vorbis/init.luau"], [6304, "src/Rosetta/audio/vorbis/Encode.luau"], [7117, "src/Rosetta/audio/wav/init.luau"], [7738, "src/Rosetta/audio/wav/Codecs.luau"], [7917, "src/Rosetta/audio/wavpack/init.luau"]],
"cad": [[8, "src/Rosetta/cad/step/init.luau"], [390, "src/Rosetta/cad/step/Brep.luau"], [625, "src/Rosetta/cad/step/Parse.luau"], [1182, "src/Rosetta/cad/step/Write.luau"]],
"codecs_audio": [[8, "src/Rosetta/codecs/audio/Ac3/init.luau"], [168, "src/Rosetta/codecs/audio/Ac3/Decode.luau"], [1053, "src/Rosetta/codecs/audio/Ac3/Tables.luau"], [1155, "src/Rosetta/codecs/audio/Adpcm/Decoder.luau"], [1458, "src/Rosetta/codecs/audio/Alac/init.luau"], [1693, "src/Rosetta/codecs/audio/Alac/Decode.luau"], [2112, "src/Rosetta/codecs/audio/Alac/Encode.luau"], [2524, "src/Rosetta/codecs/audio/Ape/init.luau"], [2740, "src/Rosetta/codecs/audio/Ape/Decode.luau"], [3501, "src/Rosetta/codecs/audio/ApeTag.luau"], [3599, "src/Rosetta/codecs/audio/Mdct/init.luau"], [3819, "src/Rosetta/codecs/audio/Mdct/Fft.luau"], [4073, "src/Rosetta/codecs/audio/MidiRender.luau"], [4420, "src/Rosetta/codecs/audio/Pcm/init.luau"], [4684, "src/Rosetta/codecs/audio/Pcm/Convert.luau"], [4981, "src/Rosetta/codecs/audio/Qoa/init.luau"], [5263, "src/Rosetta/codecs/audio/Resample/init.luau"], [5658, "src/Rosetta/codecs/audio/Stream.luau"], [5820, "src/Rosetta/codecs/audio/Tta/init.luau"], [5938, "src/Rosetta/codecs/audio/Tta/Decode.luau"], [6190, "src/Rosetta/codecs/audio/VorbisComment.luau"]],
"codecs_compress": [[8, "src/Rosetta/codecs/compress/Bzip2/init.luau"], [52, "src/Rosetta/codecs/compress/Bzip2/Decode.luau"], [565, "src/Rosetta/codecs/compress/Explode.luau"], [761, "src/Rosetta/codecs/compress/Lz4.luau"], [1030, "src/Rosetta/codecs/compress/Lzh/init.luau"], [1634, "src/Rosetta/codecs/compress/Lzw/init.luau"], [1902, "src/Rosetta/codecs/compress/Lzx.luau"], [2336, "src/Rosetta/codecs/compress/PackBits.luau"], [2426, "src/Rosetta/codecs/compress/Quantum.luau"], [2663, "src/Rosetta/codecs/compress/UnixCompress.luau"], [2967, "src/Rosetta/codecs/compress/Xxh32.luau"]],
+4277 -36
View File
File diff suppressed because it is too large. Load diff
+4267 -33
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -45,7 +45,7 @@ VideoDecoder saved to an image format (`gif`, `apng`, `webp`) becomes an animate
one category. Extras:
- `F.video.open(src, opts?)`: `opts.maxSide` shrinks the frames (longer side at most n pixels), `opts.yield` is a
cooperative yield point for slow codecs (H.264; see the video guide), `opts.audio = false` skips the audio track.
cooperative yield point for slow codecs (H.264; see the video guide), `opts.audio = false` skips the audio track, `opts.sbr = false` decodes only the AAC-LC core of an HE-AAC track.
It also opens animated images (GIF / APNG / WebP) as videos; `F.video.fromImage(img)`,
`F.video.fromFrames(images, delays?)` (an image sequence: `F.save(F.video.fromFrames(list, 1 / 25), "out.avi")`),
`F.video.codecs` (the codec registry `codecs/video/Video`).
+1 -1
View File
@@ -208,7 +208,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
Codecs (layer 3, shared by formats; not loadable on their own), by domain:
- **audio**
- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block.
- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block;
- codecs/audio/Ac3: AC-3 (Dolby Digital, ATSC A/52) syncframes: syncinfo / BSI, CRC check and decoder.
- codecs/audio/Adpcm: the 4-bit ADPCM family, decode (and encode where it is cheap).
- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder.
+15 -1
View File
@@ -102,6 +102,7 @@ the guide's [README](README.md#inputs)). Options:
| `maxFrames` | wav, flac, mp3, aiff, au, voc, aac, mpc, ac3 | `Err.LIMIT` past this many frames |
| `drc` | ac3 | dynamic range control: 0 = off (default), 1 = as the stream says |
| `stereo` | opus | downmix surround streams to stereo |
| `sbr` | aac, m4a, the AAC tracks of mp4 / mkv / ts | `false`: decode only the AAC-LC core of HE-AAC (half the rate, mono for HE-AACv2, about 4x faster) |
```luau
local tags = assert(F.audio.open(mp3Bytes)).tags -- opening reads the tags; nothing is decoded yet
@@ -112,6 +113,19 @@ local bad, err = F.audio.load("not audio at all")
assert(bad == nil and err.code == F.Err.UNKNOWN_FORMAT)
```
### HE-AAC
HE-AAC (SBR) and HE-AACv2 (SBR + parametric stereo) decode at the full output rate in every AAC path: ADTS, M4A,
and the audio tracks of MP4, Matroska and MPEG-TS (HLS radio). ADTS cannot signal SBR, so the decoder looks for SBR
and PS data in the first frames, as ffmpeg does; `sampleRate`, `channels` and `frames` report the output.
```luau
local dec = assert(F.audio.open(heAacBytes)) -- HE-AACv2 in ADTS: a 24 kHz mono core
assert(dec.sampleRate == 48000 and dec.channels == 2) -- SBR doubles the rate, PS makes it stereo
local core = assert(F.audio.open(heAacBytes, { sbr = false })) -- the AAC-LC core only, for slow devices
assert(core.sampleRate == 24000 and core.channels == 1 and core.frames * 2 == dec.frames)
```
## Editing: `F.ops.audio`
Every function returns a new Audio, or `nil, err` for bad arguments. Times are in seconds. Rates and channel counts
@@ -192,7 +206,7 @@ Tags are written from `audio.tags`, or from the Decoder's `tags`, wherever the f
| Read and write | Read only |
|---|---|
| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC), qoa, adx, vag, adpcm (raw IMA / MS) | aac (ADTS), m4a / caf AAC, mpc (Musepack SV7 / SV8), ac3 (Dolby Digital), brstm / bcstm / bfstm, KVAG, wav / aifc / caf IMA and MS ADPCM, wavpack (.wv + .wvc), ape, tta |
| wav (+ RF64), flac (+ Ogg FLAC), mp3 / mp2, vorbis (.ogg), opus, aiff / aifc, au, voc, caf, m4a (ALAC), qoa, adx, vag, adpcm (raw IMA / MS) | aac (ADTS; AAC-LC, HE-AAC, HE-AACv2), m4a / caf AAC, mpc (Musepack SV7 / SV8), ac3 (Dolby Digital), brstm / bcstm / bfstm, KVAG, wav / aifc / caf IMA and MS ADPCM, wavpack (.wv + .wvc), ape, tta |
Each format's capabilities are listed in [FORMATS.md](../FORMATS.md). The audio tracks of video files open as
Decoders too: see `v.audio` in [video.md](video.md). Music files (MOD, MIDI, ...) render to a Decoder: see
+1
View File
@@ -60,6 +60,7 @@
| `src/Rosetta/codecs/image/Bcn/Decode.luau` (the BC1..BC7 block decoders and the BPTC tables; changed to follow the D3D11 rules) | Pillow 12 `src/libImaging/BcnDecode.c` | CC0 1.0: [pillow-bcn-CC0.txt](pillow-bcn-CC0.txt) |
| `src/Rosetta/image/exr/Piz.luau`, `src/Rosetta/image/exr/Codec.luau` (the PIZ Huffman / wavelet / LUT steps and the ZIP / RLE byte predictor, re-implemented in Luau after the OpenEXR algorithms) | OpenEXR 3.x (ImfPizCompressor, ImfHuf, ImfWav, ImfZip, ImfRle) | BSD-3-Clause: [openexr-LICENSE.md](openexr-LICENSE.md) |
| `src/Rosetta/model/godot/Primitives.luau` (BoxMesh, PlaneMesh, QuadMesh, SphereMesh, CylinderMesh generation, ported to Luau without UV2) | Godot Engine 4.4 `scene/resources/3d/primitive_meshes.cpp` | MIT: [godot-LICENSE.txt](godot-LICENSE.txt) |
| `src/Rosetta/codecs/audio/Aac/Sbr.luau`, `SbrHf.luau`, `Ps.luau`, `SbrTables.luau` (HE-AAC SBR / PS: the bitstream, band tables, HF generation and adjustment, the PS hybrid filterbank / decorrelator / mixing, ported to Luau; the QMF banks are new) | syom 0.8.1 `src/engine/sbr_*.rs`, `ps_*.rs` (Evgeny Khodzitsky; its tables are ISO/IEC 14496-3 numbers) | MIT: [syom-LICENSE.txt](syom-LICENSE.txt) |
Ports of third-party code (minimp3, stb_vorbis, libopus, ...) add a row here and their upstream licence text in this
folder (see docs/STYLE.md, "Absorbing an existing repo").
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Evgeny Khodzitsky
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING WITHOUT LIMITATION THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+14 -8
View File
@@ -1,8 +1,9 @@
--!strict
--!optimize 2
-- audio/aac: raw AAC in ADTS framing (ISO/IEC 13818-7 / 14496-3 1.A.3). Supports: AAC-LC via codecs/audio/Aac (1-8
-- channels, every rate), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame; HE-AAC plays its
-- AAC-LC core. Not supported: writing, ADIF, LATM.
-- channels, every rate), HE-AAC / HE-AACv2 (SBR / PS found in the stream: ADTS cannot signal them; opts.sbr = false
-- keeps the AAC-LC core), CRC-protected headers, a leading ID3v2 tag, resync, seeking by frame. Not supported: writing,
-- ADIF, LATM.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -25,6 +26,7 @@ local AacFormat = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
maxFrames: number?, -- default 2^22
sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -150,14 +152,18 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?,
return nil, Err.new(Err.UNSUPPORTED, "ADTS with a program config element (channel configuration 0)")
end
local channels = ({ 1, 2, 3, 4, 5, 6, 8 })[h.channelConfig] :: number
local dec, derr = Aac.decoder({ sfIndex = h.sfIndex, channels = channels, channelConfig = h.channelConfig })
local dec, derr = Aac.decoder({ sfIndex = h.sfIndex, channels = channels, channelConfig = h.channelConfig }, { sbr = o.sbr })
if not dec then
return nil, derr
end
local d = dec :: Aac.Decoder
local sr = d.sampleRate
if Aac.probeFrames(d, r.buf, offs, sizes) then
d:reset()
end
local sr, fl = d.sampleRate, d.frameLength
channels = d.channels
local nframes = #offs
local total = nframes * 1024
local total = nframes * fl
local bytes = 0
for _, s in sizes do
bytes += s
@@ -174,11 +180,11 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?,
tags = tags,
codec = "aac",
}
-- 1024 samples per ADTS frame; one frame of pre-roll for the overlap
-- 1024 (2048 with SBR) samples per ADTS frame; pre-roll for the overlap (and the SBR filterbanks / time deltas)
return Stream.packets(out, {
count = nframes,
start = function(i: number): number
return (i - 1) * 1024
return (i - 1) * fl
end,
decode = function(i: number): (number?, Err.Err?)
return d:decode(buf, offs[i], sizes[i])
@@ -189,7 +195,7 @@ function AacFormat.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?,
reset = function()
d:reset()
end,
preroll = 1,
preroll = if d.sbrOutput then 3 else 1,
})
end
+4
View File
@@ -398,6 +398,10 @@ local function source(r: Bytes.Reader, p: Parsed): (Source?, Err.Err?)
if not dec then
return nil, err
end
-- HE-AAC: SBR output only when the description is at the SBR rate (else the AAC-LC core)
if dec.sbrOutput and dec.sampleRate ~= d.rate then
dec = Aac.decoder(asc :: buffer, { sbr = false })
end
local ad = dec :: Aac.Decoder
return {
channels = ad.channels,
+18 -10
View File
@@ -1,8 +1,8 @@
--!strict
--!optimize 2
-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC (HE-AAC plays
-- its core), ALAC, edit-list gapless trimming, seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8
-- channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files.
-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC, HE-AAC /
-- HE-AACv2 (explicit or implicit SBR / PS; opts.sbr = false keeps the AAC-LC core), ALAC, edit-list gapless trimming,
-- seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8 channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -29,6 +29,7 @@ export type DecodeOptions = {
track: number?, -- the track id to play (default: the first AAC / ALAC track)
gapless: boolean?, -- apply the edit list (default true)
maxSamples: number?, -- default 2^25
sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true
}
export type EncodeOptions = {
bitDepth: number?, -- 16 (default), 20 or 24
@@ -63,9 +64,10 @@ type PacketDecoder = {
pcm: { number },
decode: (buf: buffer, off: number, len: number) -> (number?, Err.Err?),
reset: () -> (),
sbr: boolean?,
}
local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
local function packetDecoder(t: IsoBmff.Track, buf: buffer, sbr: boolean?): (PacketDecoder?, Err.Err?)
if not t.config then
return nil, Err.new(Err.CORRUPT, "track " .. t.id .. " (" .. t.codec .. ") has no codec configuration")
end
@@ -97,11 +99,14 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
}
return pd, nil
elseif t.codec == "aac" then
local dec, err = Aac.decoder(t.config :: buffer)
local dec, err = Aac.decoder(t.config :: buffer, { sbr = sbr })
if not dec then
return nil, err
end
local d = dec :: Aac.Decoder
if Aac.probeFrames(d, buf, t.offsets, t.sizes) then
d:reset()
end
local pd: PacketDecoder = {
channels = d.channels,
sampleRate = d.sampleRate,
@@ -113,6 +118,7 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
reset = function()
d:reset()
end,
sbr = d.sbrOutput,
}
return pd, nil
end
@@ -146,7 +152,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
return nil, Err.new(Err.CORRUPT, "no audio track")
end
local t = track :: IsoBmff.Track
local pd, perr = packetDecoder(t)
local pd, perr = packetDecoder(t, r.buf, o.sbr)
if not pd then
return nil, perr
end
@@ -171,9 +177,11 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
for w in string.gmatch(smpb, "%x+") do
table.insert(f, tonumber(w, 16) :: number)
end
if #f >= 4 and f[2] and f[4] and f[4] > 0 and f[2] + f[4] <= total then
skip = f[2]
total = f[2] + f[4]
-- in media ticks (the core rate of HE-AAC, as ffmpeg reads it)
local pre, len = math.floor((f[2] or 0) * tickScale + 0.5), math.floor((f[4] or 0) * tickScale + 0.5)
if #f >= 4 and len > 0 and pre + len <= total then
skip = pre
total = pre + len
end
end
local frames = math.max(0, total - skip)
@@ -210,7 +218,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
reset = p.reset,
skip = skip,
stop = total,
preroll = if t.codec == "aac" then 1 else 0,
preroll = if p.sbr then 3 elseif t.codec == "aac" then 1 else 0,
cumulative = true,
})
end
+78 -1
View File
@@ -5,6 +5,7 @@
-- Written from the standard; no third-party code. PNS uses FFmpeg's LCG so the noise matches it.
local Mdct = require("@rosetta/codecs/audio/Mdct")
local Tables = require("@rosetta/codecs/audio/Aac/Tables")
local Sbr = require("@rosetta/codecs/audio/Aac/Sbr")
local readu32, byteswap, lshift, rshift, band = buffer.readu32, bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band
local floor, sqrt, abs = math.floor, math.sqrt, math.abs
@@ -150,6 +151,15 @@ export type State = {
extensions: number,
sbr: boolean,
map: { number }?,
-- SBR output (codecs/audio/Aac/Sbr): sbrRate = the output rate when on, psOut = a mono stream output as stereo
sbrOn: boolean,
probe: boolean, -- parse only (SBR / PS detection): no output
sbrRate: number,
psOut: boolean,
sbrElems: { any },
elemKind: { number },
elemFirst: { number },
core: { { number } },
}
local function newChannel(): Channel
@@ -730,6 +740,50 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
c.prevShape = shape
end
local function sbrElement(st: State, i: number, nch: number): any
local e = st.sbrElems[i]
if not e or e.nch ~= nch then
e = Sbr.element(nch, st.sbrRate, st.psOut and nch == 1)
st.sbrElems[i] = e
end
return e
end
local OFFS = { 0, 0 }
local CORE2 = { {}, {} }
-- every element through SBR at the doubled rate (2048 samples per channel); the PS output of a mono element is stereo
local function sbrOutput(st: State, order: { Channel }, nElem: number): number
local n = #order
local ps = st.psOut and n == 1
local stride = if ps then 2 else n
local map = st.map
local core = st.core
for i, c in order do
local t = core[i]
if not t then
t = table.create(1024, 0)
core[i] = t
end
filterbank(st, c, t, 1, 1)
end
for ei = 1, nElem do
local first = st.elemFirst[ei]
local nch = if st.elemKind[ei] == 1 then 2 else 1
local e = sbrElement(st, ei, nch)
for j = 1, nch do
local idx = first + j - 1
OFFS[j] = if map and map[idx] then map[idx] else idx
CORE2[j] = core[idx]
end
if ps then
OFFS[2] = 2
end
Sbr.process(e, CORE2, st.pcm, stride, OFFS)
end
return stride
end
function Decode.frame(st: State, buf: buffer, off: number, len: number): (number?, string?)
if buffer.len(B) < len + 16 then
B = buffer.create(len + 256)
@@ -741,6 +795,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
local nextCh = 1
local order: { Channel } = {}
local lastElement = -1
local nElem = 0
local elemKind, elemFirst = st.elemKind, st.elemFirst
while true do
if BP + 3 > BL then
break -- tolerate a missing END
@@ -762,6 +818,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
end
applyTns(st, c)
table.insert(order, c)
nElem += 1
elemKind[nElem], elemFirst[nElem] = id, #order
nextCh += 1
lastElement = id
elseif id == 1 then -- CPE
@@ -818,6 +876,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
applyTns(st, r)
table.insert(order, l)
table.insert(order, r)
nElem += 1
elemKind[nElem], elemFirst[nElem] = id, #order - 1
nextCh += 2
lastElement = id
elseif id == 2 then
@@ -845,6 +905,9 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
local kind = getBits(4)
if kind == 13 or kind == 14 then -- EXT_SBR_DATA / EXT_SBR_DATA_CRC
st.sbr = true
if st.sbrOn and nElem > 0 and lastElement ~= 3 then
Sbr.extension(sbrElement(st, nElem, if lastElement == 1 then 2 else 1), B, start + 4, count * 8 - 4, kind == 14)
end
end
st.extensions += 1
local hook = st.onExtension
@@ -865,6 +928,12 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
if n == 0 then
return 0, nil
end
if st.probe then
return n, nil
end
if st.sbrOn then
return sbrOutput(st, order, nElem), nil
end
local pcm = st.pcm
local map = st.map
for i, c in order do
@@ -880,7 +949,7 @@ function Decode.state(sfIndex: number): State
channels = 0,
frameLength = 1024,
ch = {},
pcm = table.create(1024 * 8, 0),
pcm = table.create(2048 * 8, 0),
random = 0x1f2e3d4c,
time = table.create(2048, 0),
frame = table.create(2048, 0),
@@ -889,6 +958,14 @@ function Decode.state(sfIndex: number): State
extensions = 0,
sbr = false,
map = nil,
sbrOn = false,
probe = false,
sbrRate = 0,
psOut = false,
sbrElems = {},
elemKind = {},
elemFirst = {},
core = {},
}
end
+588
View File
@@ -0,0 +1,588 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Aac/Ps: baseline Parametric Stereo (ISO/IEC 14496-3 8.6.4, syntax 8.3): ps_data with IID / ICC (10,
-- 20 or 34 bands, mapped to 20), the 10/20-band hybrid analysis filterbank, the all-pass / delay decorrelator with
-- transient attenuation, and mixing procedures Ra / Rb. IPD / OPD (the ps_extension) are skipped, as baseline PS
-- allows. Ported from syom 0.8.1 src/engine/ps_*.rs (MIT, licenses/syom-LICENSE.txt).
local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables")
local SbrBits = require("@rosetta/codecs/audio/Aac/SbrBits")
local cos, sin, sqrt, pi, acos, atan = math.cos, math.sin, math.sqrt, math.pi, math.acos, math.atan
local bits, huff, codebook = SbrBits.bits, SbrBits.huff, SbrBits.codebook
local Ps = {}
local IID_DF = codebook(SbrTables.IID_DF)
local IID_DT = codebook(SbrTables.IID_DT)
local IID_FINE_DF = codebook(SbrTables.IID_FINE_DF)
local IID_FINE_DT = codebook(SbrTables.IID_FINE_DT)
local ICC_DF = codebook(SbrTables.ICC_DF)
local ICC_DT = codebook(SbrTables.ICC_DT)
local NR_PAR = { 10, 20, 34, 10, 20, 34 }
local NUM_ENV = { 0, 1, 2, 4, 1, 2, 3, 4 }
-- Table 8.25 / 8.26: IID steps (dB) and ICC values
local IID_COARSE = { -25, -18, -14, -10, -7, -4, -2, 0, 2, 4, 7, 10, 14, 18, 25 }
local IID_FINE = { -50, -45, -40, -35, -30, -25, -22, -19, -16, -13, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 13, 16, 19, 22,
25, 30, 35, 40, 45, 50 }
local ICC_RHO = { 1.0, 0.937, 0.84118, 0.60092, 0.36764, 0.0, -0.589, -1.0 }
for i, v in IID_COARSE do
IID_COARSE[i] = 10 ^ (v / 20)
end
for i, v in IID_FINE do
IID_FINE[i] = 10 ^ (v / 20)
end
-- 34 -> 20 parameter bands (8.6.4.6.1): { i, j, wi, wj } weights over 34-band indexes (0-based)
local MAP34 = {
{ 0, 1, 2, 1 }, { 1, 2, 1, 2 }, { 3, 4, 2, 1 }, { 4, 5, 1, 2 }, { 6, 7, 1, 1 }, { 8, 9, 1, 1 }, { 10, 10, 1, 0 },
{ 11, 11, 1, 0 }, { 12, 13, 1, 1 }, { 14, 15, 1, 1 }, { 16, 16, 1, 0 }, { 17, 17, 1, 0 }, { 18, 18, 1, 0 },
{ 19, 19, 1, 0 }, { 20, 21, 1, 1 }, { 22, 23, 1, 1 }, { 24, 25, 1, 1 }, { 26, 27, 1, 1 }, { 28, 29, 1, 1 },
{ 32, 33, 1, 1 },
}
-- Table 8.48: parameter band of each of the 71 hybrid bands (20-band configuration)
local B_K = { 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17 }
for _ = 1, 12 do
table.insert(B_K, 18)
end
while #B_K < 71 do
table.insert(B_K, 19)
end
-- 8.6.4.3 hybrid prototype filters (13 taps)
local G0 = { 0.00746082949812, 0.02270420949825, 0.04546865930473, 0.07266113929591, 0.09885108575264, 0.11793710567217,
0.125, 0.11793710567217, 0.09885108575264, 0.07266113929591, 0.04546865930473, 0.02270420949825, 0.00746082949812 }
local G12 = { 0.0, 0.01899487526049, 0.0, -0.07293139167538, 0.0, 0.30596630545168, 0.5, 0.30596630545168, 0.0,
-0.07293139167538, 0.0, 0.01899487526049, 0.0 }
-- band 0, type A, Q = 8: A0C / A0S[q * 6 + m'] = g[m] cos / sin(2 pi / 8 (q + 1/2) (m - 6)) for m = m' - 1 = 0 .. 5
local A0C, A0S = table.create(4 * 6, 0), table.create(4 * 6, 0)
for q = 0, 3 do
for m = 0, 5 do
local a = 2 * pi / 8 * (q + 0.5) * (m - 6)
A0C[q * 6 + m + 1] = G0[m + 1] * cos(a)
A0S[q * 6 + m + 1] = G0[m + 1] * sin(a)
end
end
-- 8.6.4.5 decorrelator constants (20-band configuration)
local AP_A = { 0.65143905753106, 0.56471812200776, 0.48954165955695 }
local Q_FRACT = { 0.43, 0.75, 0.347 }
local F_CENTER = { -3 / 8, -1 / 8, 1 / 8, 3 / 8, 5 / 8, 7 / 8, 5 / 4, 7 / 4, 9 / 4, 11 / 4 }
local NR_AP = 30
local PHI_R, PHI_I = table.create(NR_AP, 0), table.create(NR_AP, 0)
local QF_R, QF_I = table.create(NR_AP * 3, 0), table.create(NR_AP * 3, 0)
local G_DECAY = table.create(NR_AP, 0)
local CO = table.create(NR_AP * 3, 0)
for k = 0, NR_AP - 1 do
local f = if k < 10 then F_CENTER[k + 1] else k + 0.5 - 7
PHI_R[k + 1], PHI_I[k + 1] = cos(-pi * 0.39 * f), sin(-pi * 0.39 * f)
for m = 0, 2 do
QF_R[k * 3 + m + 1], QF_I[k * 3 + m + 1] = cos(-pi * Q_FRACT[m + 1] * f), sin(-pi * Q_FRACT[m + 1] * f)
end
G_DECAY[k + 1] = if k > 10 then math.max(0, 1 - 0.05 * (k - 10)) else 1
for m = 0, 2 do
CO[k * 3 + m + 1] = AP_A[m + 1] * G_DECAY[k + 1]
end
end
local PEAK_DECAY, A_SMOOTH, GAMMA = 0.76592833836465, 0.25, 1.5
function Ps.state(): any
return {
haveConfig = false,
enableIid = false,
iidMode = 0,
enableIcc = false,
iccMode = 0,
enableExt = false,
iidPrev = table.create(34, 0),
iidPrevN = 0,
iccPrev = table.create(34, 0),
iccPrevN = 0,
hist = table.create(3 * 6 * 2, 0), -- hybrid input history: [(p * 6 + j) * 2 + 1 / 2]
apIn = table.create(NR_AP * 4, 0), -- z^-2 delay: in[n-1], in[n-2] (re, im)
apW = table.create(NR_AP * 3 * 5 * 2, 0), -- all-pass links: [k * 30 + (m * 5 + pos) * 2 + 1 / 2]
apCnt = 0,
dl = table.create(41 * 14 * 2, 0), -- plain delays of hybrid bands 30 .. 70
dlCnt = 0,
peak = table.create(20, 0),
smooth = table.create(20, 0),
smoothDiff = table.create(20, 0),
hPrev = table.create(20 * 4, 0),
prevHadPs = false,
nbPrev = 71,
-- frame parameters
nEnv = 0,
border = table.create(5, 0),
iid = table.create(4 * 20, 0),
icc = table.create(4 * 20, 0),
-- hybrid rows [(n * 71 + k) + 1], decorrelated rows
sr = table.create(32 * 71, 0),
si = table.create(32 * 71, 0),
dr = table.create(32 * 71, 0),
di = table.create(32 * 71, 0),
hs = table.create(32 * 20 * 4, 0),
}
end
local TMP = table.create(34, 0)
-- integer division truncating toward zero (the 8.6.4.6.1 mapping averages)
local function tdiv(a: number, b: number): number
local q = a / b
return if q >= 0 then math.floor(q) else -math.floor(-q)
end
-- one parameter kind: nEnv rows into out[(e - 1) * 20 + b] (mapped to 20 bands)
local function readKind(st: any, nEnv: number, nr: number, fine: boolean, iid: boolean, out: { number }): boolean
local prev = if iid then st.iidPrev else st.iccPrev
local prevN = if iid then st.iidPrevN else st.iccPrevN
local lo, hi = 0, 7
if iid then
lo = if fine then -15 else -7
hi = -lo
end
for e = 1, nEnv do
local dt = bits(1) == 1
local bk
if iid then
bk = if fine then (if dt then IID_FINE_DT else IID_FINE_DF) else (if dt then IID_DT else IID_DF)
else
bk = if dt then ICC_DT else ICC_DF
end
local acc = 0
for b = 1, nr do
local d = huff(bk)
if not d then
return false
end
local v
if dt then
v = (if prevN == nr then prev[b] else 0) + d
else
acc += d
v = acc
end
if v < lo or v > hi then
return false
end
TMP[b] = v
end
for b = 1, nr do
prev[b] = TMP[b]
end
prevN = nr
local base = (e - 1) * 20
if nr == 20 then
for b = 1, 20 do
out[base + b] = TMP[b]
end
elseif nr == 10 then
for b = 1, 20 do
out[base + b] = TMP[(b - 1) // 2 + 1]
end
else
for b = 1, 20 do
local mp = MAP34[b]
local v = 0
if b == 19 then
v = tdiv(TMP[29] + TMP[30] + TMP[31] + TMP[32], 4)
else
v = tdiv(TMP[mp[1] + 1] * mp[3] + TMP[mp[2] + 1] * mp[4], mp[3] + mp[4])
end
out[base + b] = v
end
end
end
if iid then
st.iidPrevN = prevN
else
st.iccPrevN = prevN
end
return true
end
-- ps_data (8.3 Table 8.1) at bit position pos; false when undecodable
function Ps.parse(st: any, buf: buffer, pos: number, count: number): boolean
SbrBits.start(buf, pos, count)
if bits(1) == 1 then
st.enableIid = bits(1) == 1
st.iidMode = if st.enableIid then bits(3) else 0
st.enableIcc = bits(1) == 1
st.iccMode = if st.enableIcc then bits(3) else 0
st.enableExt = bits(1) == 1
if st.iidMode > 5 or st.iccMode > 5 then
st.haveConfig = false
return false
end
st.haveConfig = true
end
if not st.haveConfig then
return false
end
local class = bits(1)
local nEnv = NUM_ENV[class * 4 + bits(2) + 1]
st.nEnv = nEnv
for e = 1, nEnv do
st.border[e] = if class == 1 then math.min(bits(5), 31) else (32 * e) // nEnv - 1
end
local iid, icc = st.iid, st.icc
if st.enableIid then
if not readKind(st, nEnv, NR_PAR[st.iidMode + 1], st.iidMode >= 3, true, iid) then
return false
end
else
st.iidPrevN = 0
for i = 1, 4 * 20 do
iid[i] = 0
end
end
if st.enableIcc then
if not readKind(st, nEnv, NR_PAR[st.iccMode + 1], false, false, icc) then
return false
end
else
st.iccPrevN = 0
for i = 1, 4 * 20 do
icc[i] = 0
end
end
if st.enableExt then
local cnt = bits(4)
if cnt == 15 then
cnt += bits(8)
end
SbrBits.skip(cnt * 8)
end
return not SbrBits.over()
end
-- 8.6.4.6.2 mixing matrix for one envelope's parameters (Ra, or Rb for icc modes 3 .. 5)
local function mixMatrix(st: any, e: number, out: { number })
local fine = st.iidMode >= 3
local rb = st.iccMode >= 3
local base = (e - 1) * 20
for b = 1, 20 do
local ii = st.iid[base + b]
local c = if fine then IID_FINE[ii + 16] else IID_COARSE[ii + 8]
local rho = ICC_RHO[st.icc[base + b] + 1]
local h11, h12, h21, h22
if rb then
rho = math.max(rho, 0.05)
local alpha
if math.abs(c - 1) < 1e-12 then
alpha = pi / 4
else
alpha = 0.5 * atan(2 * c * rho / (c * c - 1))
end
if alpha < 0 then
alpha += pi / 2
end
local cc = c + 1 / c
local mu = 1 + (4 * rho * rho - 4) / (cc * cc)
local gamma = atan(sqrt(math.max((1 - mu) / (1 + mu), 0)))
local s2 = sqrt(2)
h11 = s2 * cos(alpha) * cos(gamma)
h12 = s2 * sin(alpha) * cos(gamma)
h21 = -s2 * sin(alpha) * sin(gamma)
h22 = s2 * cos(alpha) * sin(gamma)
else
local den = sqrt(1 + c * c)
local c1 = sqrt(2) / den
local c2 = sqrt(2) * c / den
local alpha = 0.5 * acos(math.clamp(rho, -1, 1))
local beta = alpha * (c1 - c2) / sqrt(2)
h11 = cos(alpha + beta) * c2
h12 = cos(beta - alpha) * c1
h21 = sin(alpha + beta) * c2
h22 = sin(beta - alpha) * c1
end
local o = (b - 1) * 4
out[o + 1], out[o + 2], out[o + 3], out[o + 4] = h11, h12, h21, h22
end
end
local H_TO = table.create(80, 0)
local H_FROM = table.create(80, 0)
-- per-slot matrices hs[(n * 20 + b) * 4 + c] interpolated between envelope borders
local function fillMatrices(st: any)
local hs, hPrev = st.hs, st.hPrev
local nEnv = st.nEnv
for i = 1, 80 do
H_FROM[i] = hPrev[i]
end
local nFrom = -1
for e = 1, nEnv do
local ne = st.border[e]
mixMatrix(st, e, H_TO)
local den = math.max(ne - nFrom, 1)
for n = nFrom + 1, math.min(ne, 31) do
local t = (n - nFrom) / den
local o = n * 80
for i = 1, 80 do
local f = H_FROM[i]
hs[o + i] = f + (H_TO[i] - f) * t
end
end
for i = 1, 80 do
H_FROM[i] = H_TO[i]
end
if ne > nFrom then
nFrom = ne
end
end
for n = nFrom + 1, 31 do
local o = n * 80
for i = 1, 80 do
hs[o + i] = H_FROM[i]
end
end
for i = 1, 80 do
hPrev[i] = H_FROM[i]
end
end
-- 8.6.4.3 hybrid analysis: QMF bands 0 .. 2 of slots 0 .. 37 (6 look-ahead) -> hybrid bands 0 .. 9; bands 3 .. 63
-- pass through as hybrid bands 10 .. 70
local BR = table.create(44, 0)
local BI = table.create(44, 0)
local function hybrid(st: any, xr: { number }, xi: { number }, nb: number)
local sr, si, hist = st.sr, st.si, st.hist
for n = 0, 31 do
local o, x = n * 71, n * 64
for k = 0, 9 do
sr[o + k + 1], si[o + k + 1] = 0, 0
end
for k = 3, nb - 8 do
sr[o + k + 8], si[o + k + 8] = xr[x + k + 1], xi[x + k + 1]
end
end
for p = 0, 2 do
for j = 0, 5 do
BR[j + 1], BI[j + 1] = hist[(p * 6 + j) * 2 + 1], hist[(p * 6 + j) * 2 + 2]
end
for j = 0, 37 do
BR[j + 7], BI[j + 7] = xr[j * 64 + p + 1], xi[j * 64 + p + 1]
end
if p == 0 then
-- type A, Q = 8, symmetric g: y_q = A_q + i B_q, y_(7-q) = A_q - i B_q with A_q = sum_m g[m] cos(t (m - 6))
-- (x[m] + x[12 - m]), B_q = sum_m g[m] sin(t (m - 6)) (x[m] - x[12 - m]), t = 2 pi / 8 (q + 1/2)
for n = 0, 31 do
local o = n * 71
local cr, ci = BR[n + 7] * G0[7], BI[n + 7] * G0[7]
for q = 0, 3 do
local ar, ai, br, bi = cr, ci, 0, 0
local hb = q * 6
for m = 1, 6 do
-- buf[n + 12 - m'] with m' = m - 1 (the newer tap) and its mirror buf[n + m']
local xr, xi = BR[n + 14 - m], BI[n + 14 - m]
local yr, yi = BR[n + m], BI[n + m]
local c, sn = A0C[hb + m], A0S[hb + m]
ar += c * (xr + yr)
ai += c * (xi + yi)
br += sn * (xr - yr)
bi += sn * (xi - yi)
end
-- y_q = A + i B, y_(7-q) = A - i B
local q0r, q0i, q1r, q1i = ar - bi, ai + br, ar + bi, ai - br
if q == 0 then
sr[o + 3], si[o + 3] = q0r, q0i
sr[o + 2], si[o + 2] = q1r, q1i
elseif q == 1 then
sr[o + 4], si[o + 4] = q0r, q0i
sr[o + 1], si[o + 1] = q1r, q1i
elseif q == 2 then
sr[o + 5], si[o + 5] = q0r + q1r, q0i + q1i
else
sr[o + 6], si[o + 6] = q0r + q1r, q0i + q1i
end
end
end
else
-- type B, Q = 2: q0 = g * x (low pass), q1 = g * (-1)^(m - 6) x (high pass)
local lowTo = if p == 1 then 8 else 9
local highTo = if p == 1 then 7 else 10
for n = 0, 31 do
local er, ei = BR[n + 7] * 0.5, BI[n + 7] * 0.5
local orr, oi = 0, 0
for m = 1, 11, 2 do
local g = G12[m + 1]
orr += g * BR[n + 13 - m]
oi += g * BI[n + 13 - m]
end
local o = n * 71
sr[o + lowTo], si[o + lowTo] = er + orr, ei + oi
sr[o + highTo], si[o + highTo] = er - orr, ei - oi
end
end
for j = 0, 5 do
hist[(p * 6 + j) * 2 + 1], hist[(p * 6 + j) * 2 + 2] = xr[(26 + j) * 64 + p + 1], xi[(26 + j) * 64 + p + 1]
end
end
end
local PW = table.create(20, 0)
local GR = table.create(20, 0)
-- 8.6.4.5 decorrelation of the hybrid rows
local function decorrelate(st: any, nb: number)
local sr, si, dr, di = st.sr, st.si, st.dr, st.di
local peak, smooth, sdiff = st.peak, st.smooth, st.smoothDiff
local apIn, apW, dl = st.apIn, st.apW, st.dl
local nAp = math.min(NR_AP, nb)
for n = 0, 31 do
local o = n * 71
for i = 1, 20 do
PW[i] = 0
end
for k = 1, nb do
local r, im = sr[o + k], si[o + k]
local b = B_K[k] + 1
PW[b] += r * r + im * im
end
for i = 1, 20 do
local p = PW[i]
local pk = PEAK_DECAY * peak[i]
if pk < p then
pk = p
end
peak[i] = pk
local sm = smooth[i] + A_SMOOTH * (p - smooth[i])
local sd = sdiff[i] + A_SMOOTH * (pk - p - sdiff[i])
smooth[i], sdiff[i] = sm, sd
GR[i] = if GAMMA * sd > sm then sm / (GAMMA * sd) else 1
end
-- the all-pass links' ring positions (delays 3, 4, 5) and the plain delay's (14) are shared by every band
local cnt = st.apCnt
local w0, w1, w2 = (cnt % 3) * 2, (5 + cnt % 4) * 2, (10 + cnt % 5) * 2
st.apCnt = (cnt + 1) % 60
for k = 0, nAp - 1 do
local a4 = k * 4
local ur, ui = apIn[a4 + 3], apIn[a4 + 4]
apIn[a4 + 3], apIn[a4 + 4] = apIn[a4 + 1], apIn[a4 + 2]
apIn[a4 + 1], apIn[a4 + 2] = sr[o + k + 1], si[o + k + 1]
local pr, pi2 = PHI_R[k + 1], PHI_I[k + 1]
ur, ui = pr * ur - pi2 * ui, pr * ui + pi2 * ur
local qb, wb = k * 3, k * 30
-- link m: w[n] = u[n] + a g Q w[n - d], v[n] = Q w[n - d] - a g w[n]
local coef, qr, qim = CO[qb + 1], QF_R[qb + 1], QF_I[qb + 1]
local w = wb + w0
local wr, wi = apW[w + 1], apW[w + 2]
local tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
local nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
coef, qr, qim = CO[qb + 2], QF_R[qb + 2], QF_I[qb + 2]
w = wb + w1
wr, wi = apW[w + 1], apW[w + 2]
tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
coef, qr, qim = CO[qb + 3], QF_R[qb + 3], QF_I[qb + 3]
w = wb + w2
wr, wi = apW[w + 1], apW[w + 2]
tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
local gr = GR[B_K[k + 1] + 1]
dr[o + k + 1], di[o + k + 1] = ur * gr, ui * gr
end
local dc = st.dlCnt
st.dlCnt = (dc + 1) % 14
for k = NR_AP, nb - 1 do
local w = (k - NR_AP) * 28 + (if k < 42 then dc * 2 else 0)
local vr, vi = dl[w + 1], dl[w + 2]
dl[w + 1], dl[w + 2] = sr[o + k + 1], si[o + k + 1]
local gr = GR[B_K[k + 1] + 1]
dr[o + k + 1], di[o + k + 1] = vr * gr, vi * gr
end
end
end
local function clear(t: { number })
for i = 1, #t do
t[i] = 0
end
end
-- one frame: X (38 slots x 64 bands, [n * 64 + k + 1], silent from QMF band top) -> L / R QMF slots (32 x 64)
function Ps.process(st: any, had: boolean, top: number, xr: { number }, xi: { number }, lr: { number }, li: { number }, rr: { number }, ri: { number })
if not st.prevHadPs then
clear(st.apIn)
clear(st.apW)
clear(st.dl)
end
if not had then
st.nEnv = 0
end
-- hybrid bands above the SBR range (QMF band top and up) are silent: skipped, their delay lines cleared
local nb = math.clamp(top + 7, 10, 71)
if nb < st.nbPrev then
for k = nb, st.nbPrev - 1 do
if k < NR_AP then
for i = k * 4 + 1, k * 4 + 4 do
st.apIn[i] = 0
end
for i = k * 30 + 1, k * 30 + 30 do
st.apW[i] = 0
end
else
local d = k - NR_AP
for i = d * 28 + 1, d * 28 + 28 do
st.dl[i] = 0
end
end
end
end
st.nbPrev = nb
fillMatrices(st)
hybrid(st, xr, xi, nb)
decorrelate(st, nb)
local sr, si, dr, di, hs = st.sr, st.si, st.dr, st.di, st.hs
for n = 0, 31 do
local o, x, hb = n * 71, n * 64, n * 80
local l0r, l0i, r0r, r0i = 0, 0, 0, 0
local l1r, l1i, r1r, r1i = 0, 0, 0, 0
local l2r, l2i, r2r, r2i = 0, 0, 0, 0
for k = nb - 6, 64 do
lr[x + k], li[x + k], rr[x + k], ri[x + k] = 0, 0, 0, 0
end
for k = 1, nb do
local h = hb + B_K[k] * 4
local h11, h12, h21, h22 = hs[h + 1], hs[h + 2], hs[h + 3], hs[h + 4]
local s_r, s_i, d_r, d_i = sr[o + k], si[o + k], dr[o + k], di[o + k]
local Lr, Li = h11 * s_r + h21 * d_r, h11 * s_i + h21 * d_i
local Rr, Ri = h12 * s_r + h22 * d_r, h12 * s_i + h22 * d_i
if k <= 6 then
l0r += Lr
l0i += Li
r0r += Rr
r0i += Ri
elseif k <= 8 then
l1r += Lr
l1i += Li
r1r += Rr
r1i += Ri
elseif k <= 10 then
l2r += Lr
l2i += Li
r2r += Rr
r2i += Ri
else
lr[x + k - 7], li[x + k - 7] = Lr, Li
rr[x + k - 7], ri[x + k - 7] = Rr, Ri
end
end
lr[x + 1], li[x + 1], rr[x + 1], ri[x + 1] = l0r, l0i, r0r, r0i
lr[x + 2], li[x + 2], rr[x + 2], ri[x + 2] = l1r, l1i, r1r, r1i
lr[x + 3], li[x + 3], rr[x + 3], ri[x + 3] = l2r, l2i, r2r, r2i
end
st.prevHadPs = had
end
return Ps
File diff suppressed because it is too large. Load diff
+117
View File
@@ -0,0 +1,117 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Aac/SbrBits: the MSB-first bit cursor and Huffman decoding shared by the SBR and PS payload parsers
-- (ISO/IEC 14496-3 4.6.18 / 8.6.4; codebooks of codecs/audio/Aac/SbrTables). One payload at a time, over the core
-- decoder's zero-padded frame copy.
local readu32, byteswap, lshift, rshift, band = buffer.readu32, bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band
local SbrBits = {}
local RB: buffer = buffer.create(16)
local RP = 0
local RE = 0
local ROVER = false
-- the buffer must hold 4 readable bytes past (pos + count) / 8
function SbrBits.start(buf: buffer, pos: number, count: number)
local n = buffer.len(buf)
if pos < 0 or count < 0 or (pos + count + 7) // 8 + 4 > n then
RB, RP, RE, ROVER = buf, 0, 0, true
return
end
RB, RP, RE, ROVER = buf, pos, pos + count, false
end
-- n = 1 .. 25
local function bits(n: number): number
local p = RP
RP = p + n
if RP > RE then
ROVER = true
RP = RE
return 0
end
return rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - n)
end
SbrBits.bits = bits
function SbrBits.skip(n: number)
RP += n
if RP > RE then
ROVER = true
RP = RE
end
end
function SbrBits.pos(): number
return RP
end
function SbrBits.over(): boolean
return ROVER
end
-- codebook from { lav, lengths, codes }: { lav, peek[2^8] = sym * 32 + len (0: longer), long[len * 2^20 + code] =
-- sym, maxLen } (sym 1-based; value = sym - 1 - lav)
local PEEK = 8
function SbrBits.codebook(t: { any }): { any }
local lav, lens, codes = t[1], t[2], t[3]
local peek = table.create(2 ^ PEEK, 0)
local long: { [number]: number } = {}
local maxLen = 0
for s = 1, #lens do
local l, c = lens[s], codes[s]
if l > maxLen then
maxLen = l
end
if l <= PEEK then
local span = 2 ^ (PEEK - l)
for k = 0, span - 1 do
peek[c * span + k + 1] = s * 32 + l
end
else
long[l * 1048576 + c] = s
end
end
return { lav, peek, long, maxLen }
end
-- the decoded value, or nil on an invalid code / the end of the payload
function SbrBits.huff(bk: { any }): number?
local p = RP
local left = RE - p
if left <= 0 then
ROVER = true
return nil
end
local w
if left >= PEEK then
w = rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - PEEK)
else
w = lshift(rshift(lshift(byteswap(readu32(RB, rshift(p, 3))), band(p, 7)), 32 - left), PEEK - left)
end
local e = bk[2][w + 1]
if e > 0 then
local l = band(e, 31)
if l > left then
ROVER = true
RP = RE
return nil
end
RP = p + l
return rshift(e, 5) - 1 - bk[1]
end
local long = bk[3]
local code = bits(PEEK)
for l = PEEK + 1, bk[4] do
code = code * 2 + bits(1)
local s = long[l * 1048576 + code]
if s then
return s - 1 - bk[1]
end
end
return nil
end
return SbrBits
File diff suppressed because it is too large. Load diff
+490
View File
@@ -0,0 +1,490 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Aac/SbrHf: SBR HF generation (4.6.18.6: chirp factors, covariance prediction, patching) and HF
-- adjustment (4.6.18.7: mapping, current energies, gains, limiter, boost, noise floor, sinusoids, smoothing).
-- Ported from syom 0.8.1 src/engine/sbr_hf_gen.rs, sbr_env_adjust.rs (MIT, licenses/syom-LICENSE.txt).
local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables")
local sqrt, min = math.sqrt, math.min
local SbrHf = {}
local NOISE_R, NOISE_I = table.create(512, 0), table.create(512, 0)
for i = 0, 511 do
NOISE_R[i + 1], NOISE_I[i + 1] = SbrTables.NOISE[2 * i + 1], SbrTables.NOISE[2 * i + 2]
end
local LIM_GAIN = { 0.70795, 1.0, 1.41254, 1e10 }
local H_SMOOTH = { 0.33333333333333, 0.30150283239582, 0.21816949906249, 0.11516383427084, 0.03183050093751 }
local EPS0 = 1e-12
local MAX_BOOST = 1.584893192
-- 4.6.18.6.2 Table 4.183: newBw from (previous, current) bs_invf_mode
local NEW_BW = { 0, 0.6, 0.9, 0.98, 0.6, 0.75, 0.9, 0.98, 0, 0.75, 0.9, 0.98, 0, 0.75, 0.9, 0.98 }
-- per-channel HF state
function SbrHf.state(): any
return {
Yr = table.create(40 * 64, 0), -- X_high / Y[col * 64 + k + 1], col = slot + tHFAdj
Yi = table.create(40 * 64, 0),
Pr = table.create(40 * 64, 0), -- the previous frame's Y
Pi = table.create(40 * 64, 0),
bw = table.create(5, 0),
prevBw = table.create(5, 0),
prevInvf = table.create(5, 0),
tEPrev = 16,
kxPrev = 32,
mPrev = 0,
-- envelope adjuster
gTail = table.create(4 * 64, 0),
qTail = table.create(4 * 64, 0),
tailM = -1,
indexNoise = 0,
indexSine = 0,
sPrev = table.create(64, 0),
sPrevKx = 0,
lAPrev = -1,
lEPrev = 0,
started = false,
}
end
local alpha0r = table.create(32, 0)
local alpha0i = table.create(32, 0)
local alpha1r = table.create(32, 0)
local alpha1i = table.create(32, 0)
local alphaDone = table.create(32, 0)
local alphaFrame = 0
-- 4.6.18.6.2 covariance method for source subband p (0-based), cached per frame
local function prediction(xr: { number }, xi: { number }, p: number)
local i = p + 1
if alphaDone[i] == alphaFrame then
return
end
alphaDone[i] = alphaFrame
local p01r, p01i, p02r, p02i, p11, p12r, p12i, p22 = 0, 0, 0, 0, 0, 0, 0, 0
local x2r, x2i = xr[i], xi[i]
local x1r, x1i = xr[32 + i], xi[32 + i]
for col = 2, 39 do
local q = col * 32 + i
local x0r, x0i = xr[q], xi[q]
p01r += x0r * x1r + x0i * x1i
p01i += x0i * x1r - x0r * x1i
p02r += x0r * x2r + x0i * x2i
p02i += x0i * x2r - x0r * x2i
p11 += x1r * x1r + x1i * x1i
p12r += x1r * x2r + x1i * x2i
p12i += x1i * x2r - x1r * x2i
p22 += x2r * x2r + x2i * x2i
x2r, x2i = x1r, x1i
x1r, x1i = x0r, x0i
end
local a1r, a1i, a0r, a0i = 0, 0, 0, 0
local d = p22 * p11 - (p12r * p12r + p12i * p12i) / (1 + 1e-6)
if d ~= 0 then
-- (phi01 * phi12 - phi02 * phi11) / d
a1r = (p01r * p12r - p01i * p12i - p02r * p11) / d
a1i = (p01r * p12i + p01i * p12r - p02i * p11) / d
end
if p11 ~= 0 then
-- -(phi01 + alpha1 * conj(phi12)) / phi11
a0r = -(p01r + a1r * p12r + a1i * p12i) / p11
a0i = -(p01i + a1i * p12r - a1r * p12i) / p11
end
if a0r * a0r + a0i * a0i >= 16 or a1r * a1r + a1i * a1i >= 16 then
a0r, a0i, a1r, a1i = 0, 0, 0, 0
end
alpha0r[i], alpha0i[i], alpha1r[i], alpha1i[i] = a0r, a0i, a1r, a1i
end
-- 4.6.18.6: X_high for the frame's envelope span; chirp factors from the invf modes
function SbrHf.generate(e: any, c: any, h: any, xr: { number }, xi: { number })
local yr, yi = h.Yr, h.Yi
for i = 1, 40 * 64 do
yr[i], yi[i] = 0, 0
end
local nQ = e.nQ
local bw, prevBw, prevInvf, invf = h.bw, h.prevBw, h.prevInvf, c.invf
for i = 1, nQ do
local nb = NEW_BW[(prevInvf[i] or 0) * 4 + (invf[i] or 0) + 1] or 0
local t = if nb < prevBw[i] then 0.75 * nb + 0.25 * prevBw[i] else 0.90625 * nb + 0.09375 * prevBw[i]
bw[i] = if t < 0.015625 then 0 else t
end
alphaFrame += 1
local c0 = 2 * c.tE[1] + 2
local c1 = 2 * c.tE[c.nEnv + 1] + 2 - 1
local kx, mapQ = e.kx, e.mapQ
local k = kx
for pi, num in e.patchNum do
local start = e.patchStart[pi]
for x = 0, num - 1 do
local p = start + x
prediction(xr, xi, p)
local i = p + 1
local b = bw[(mapQ[k - kx + 1] or 0) + 1] or 0
local b2 = b * b
local a0r, a0i = alpha0r[i] * b, alpha0i[i] * b
local a1r, a1i = alpha1r[i] * b2, alpha1i[i] * b2
local q = c0 * 32 + i
local o = c0 * 64 + k + 1
local m1r, m1i = xr[q - 32], xi[q - 32]
local m2r, m2i = xr[q - 64], xi[q - 64]
for _ = c0, c1 do
local x0r, x0i = xr[q], xi[q]
yr[o] = x0r + a0r * m1r - a0i * m1i + a1r * m2r - a1i * m2i
yi[o] = x0i + a0r * m1i + a0i * m1r + a1r * m2i + a1i * m2r
m2r, m2i = m1r, m1i
m1r, m1i = x0r, x0i
q += 32
o += 64
end
k += 1
end
end
for i = 1, nQ do
prevBw[i] = bw[i]
prevInvf[i] = invf[i]
end
end
-- per-envelope scratch: [ (l - 1) * 64 + m ]
local E_MAP = table.create(5 * 64, 0)
local Q_MAP = table.create(5 * 64, 0)
local S_IDX = table.create(5 * 64, 0)
local S_MAP = table.create(5 * 64, 0)
local E_CURR = table.create(5 * 64, 0)
local G_B = table.create(5 * 64, 0)
local Q_B = table.create(5 * 64, 0)
local S_B = table.create(5 * 64, 0)
local QM = table.create(64, 0)
local SM = table.create(64, 0)
local GG = table.create(64, 0)
local GMAX = table.create(64, 0)
local QLIM = table.create(64, 0)
local GLIM = table.create(64, 0)
local G_T = table.create(44 * 64, 0)
local Q_T = table.create(44 * 64, 0)
local ENV_OF = table.create(40, 0)
local Q_Z = table.create(5 * 64, 0)
local SINES = table.create(5, 0)
-- 4.6.18.7.2 mapping of E_orig / Q_orig / sinusoids onto the subbands of each envelope
local function mapping(e: any, c: any, h: any): number
local kx, M = e.kx, e.M
local nEnv = c.nEnv
local mapH, mapL, mapQ = e.mapH, e.mapL, e.mapQ
local eOrig, qOrig, tE, tQ = c.eOrig, c.qOrig, c.tE, c.tQ
local fHigh = e.fHigh
for l = 1, nEnv do
local base = (l - 1) * 64
local map = if c.freqRes[l] == 1 then mapH else mapL
local nq = if c.nNoise > 1 and tE[l] >= tQ[2] then 1 else 0
for m = 1, M do
E_MAP[base + m] = eOrig[base + map[m] + 1]
Q_MAP[base + m] = qOrig[nq * 8 + mapQ[m] + 1]
S_IDX[base + m] = 0
S_MAP[base + m] = 0
end
if c.hasHarm then
for i = 1, e.nHigh do
if c.harm[i] == 1 then
local mid = (fHigh[i + 1] + fHigh[i]) // 2
local mr = mid - kx
if mr >= 0 and mr < M then
local pr = mid - h.sPrevKx
local prevOn = pr >= 0 and pr < 64 and h.sPrev[pr + 1] == 1
if l - 1 >= c.lA or prevOn then
S_IDX[base + mr + 1] = 1
end
end
end
end
local f = if c.freqRes[l] == 1 then fHigh else e.fLow
for i = 1, #f - 1 do
local lo, hi = f[i] - kx, f[i + 1] - kx
local any = false
for j = lo, hi - 1 do
if S_IDX[base + j + 1] == 1 then
any = true
end
end
if any then
for j = lo, hi - 1 do
S_MAP[base + j + 1] = 1
end
end
end
end
end
return nEnv
end
-- 4.6.18.7.3 current envelope energies E_curr
local function energies(e: any, c: any, hdr: any, yr: { number }, yi: { number })
local kx, M = e.kx, e.M
for l = 1, c.nEnv do
local base = (l - 1) * 64
local lo = 2 * c.tE[l] + 2
local hi = 2 * c.tE[l + 1] + 2
local inv = 1 / (hi - lo)
if hdr.interpolFreq then
for m = 1, M do
local s = 0
local o = lo * 64 + kx + m
for _ = lo, hi - 1 do
local r, i = yr[o], yi[o]
s += r * r + i * i
o += 64
end
E_CURR[base + m] = s * inv
end
else
local f = if c.freqRes[l] == 1 then e.fHigh else e.fLow
for p = 1, #f - 1 do
local kl, kh = f[p], f[p + 1] - 1
local s = 0
for j = kl, kh do
local o = lo * 64 + j + 1
for _ = lo, hi - 1 do
local r, i = yr[o], yi[o]
s += r * r + i * i
o += 64
end
end
local avg = s * inv / (kh - kl + 1)
for j = kl, kh do
E_CURR[base + j - kx + 1] = avg
end
end
end
end
end
-- 4.6.18.7.4 / 4.6.18.7.5 gains, limiter and boost per envelope
local function gains(e: any, c: any, hdr: any, lAPrev: number)
local kx, M = e.kx, e.M
local fLim = e.fLim
local limGain = LIM_GAIN[hdr.limiterGains + 1] or 1
for l = 1, c.nEnv do
local base = (l - 1) * 64
local li = l - 1
local trans = li == c.lA or li == lAPrev
local deltaL = if trans then 0 else 1
for m = 1, M do
local i = base + m
local eo, q = E_MAP[i], Q_MAP[i]
local ec = E_CURR[i]
QM[m] = sqrt(eo * q / (1 + q))
SM[m] = if S_IDX[i] == 1 then sqrt(eo / (1 + q)) else 0
if S_MAP[i] == 1 then
GG[m] = sqrt(eo / (1 + ec) * (q / (1 + q)))
else
GG[m] = sqrt(eo / ((1 + ec) * (1 + deltaL * q)))
end
end
for b = 1, #fLim - 1 do
local lo = math.max(fLim[b] - kx, 0)
local hi = min(math.max(fLim[b + 1] - kx, 0), M)
local num, den = EPS0, EPS0
for m = lo + 1, hi do
num += E_MAP[base + m]
den += E_CURR[base + m]
end
local gmax = min(sqrt(num / den) * limGain, 1e5)
for m = lo + 1, hi do
GMAX[m] = gmax
end
end
for m = 1, M do
local g, gm, qm = GG[m], GMAX[m], QM[m]
QLIM[m] = if g > 0 then min(qm, qm * gm / g) else qm
GLIM[m] = min(g, gm)
end
for b = 1, #fLim - 1 do
local lo = math.max(fLim[b] - kx, 0)
local hi = min(math.max(fLim[b + 1] - kx, 0), M)
local num, den = EPS0, EPS0
for m = lo + 1, hi do
num += E_MAP[base + m]
local sm, gl = SM[m], GLIM[m]
den += E_CURR[base + m] * gl * gl + sm * sm
if not (sm ~= 0 or trans) then
den += QLIM[m] * QLIM[m]
end
end
local boost = min(sqrt(num / den), MAX_BOOST)
for m = lo + 1, hi do
G_B[base + m] = GLIM[m] * boost
Q_B[base + m] = QLIM[m] * boost
S_B[base + m] = SM[m] * boost
end
end
end
end
-- 4.6.18.7.6 assembly of Y over the frame's envelope span
local function assemble(e: any, c: any, hdr: any, h: any, lAPrev: number, yr: { number }, yi: { number })
local kx, M = e.kx, e.M
local nEnv = c.nEnv
local tE = c.tE
local i0, iEnd = 2 * tE[1], 2 * tE[nEnv + 1]
local nCols = iEnd - i0
local hSl = if hdr.smoothingMode then 0 else 4
for col = 0, nCols - 1 do
local t = i0 + col
local l = nEnv
for x = 1, nEnv do
if t >= 2 * tE[x] and t < 2 * tE[x + 1] then
l = x
break
end
end
ENV_OF[col + 1] = l
end
if hSl > 0 then
local tail = h.tailM == M
for j = 0, hSl - 1 do
local d = j * 64
for m = 1, M do
G_T[d + m] = if tail then h.gTail[d + m] else G_B[m]
Q_T[d + m] = if tail then h.qTail[d + m] else Q_B[m]
end
end
for col = 0, nCols - 1 do
local d = (col + hSl) * 64
local s = (ENV_OF[col + 1] - 1) * 64
for m = 1, M do
G_T[d + m] = G_B[s + m]
Q_T[d + m] = Q_B[s + m]
end
end
end
local indexNoise, indexSine = h.indexNoise, h.indexSine
local fSine = indexSine
-- the noise level is 0 where a sinusoid is added; per envelope: any sinusoid
for l = 1, nEnv do
local base = (l - 1) * 64
local any = 0
for m = 1, M do
if S_B[base + m] ~= 0 then
Q_Z[base + m] = 0
else
Q_Z[base + m] = Q_B[base + m]
end
any += S_IDX[base + m]
end
SINES[l] = any
end
for col = 0, nCols - 1 do
local l = ENV_OF[col + 1]
local li = l - 1
local base = (l - 1) * 64
local trans = li == c.lA or li == lAPrev
fSine = (indexSine + col) % 4
local o = (i0 + col + 2) * 64 + kx
local ni = (indexNoise + col * M) % 512
if hSl ~= 0 and not trans then
local d = (col + hSl) * 64
for m = 1, M do
local gf, qf = 0, 0
for j = 0, hSl do
gf += G_T[d - j * 64 + m] * H_SMOOTH[j + 1]
qf += Q_T[d - j * 64 + m] * H_SMOOTH[j + 1]
end
if S_B[base + m] ~= 0 then
qf = 0
end
ni += 1
if ni >= 512 then
ni = 0
end
yr[o + m] = yr[o + m] * gf + qf * NOISE_R[ni + 1]
yi[o + m] = yi[o + m] * gf + qf * NOISE_I[ni + 1]
end
elseif trans then
local gsrc, gb = G_B, base
if hSl ~= 0 then
gsrc, gb = G_T, (col + hSl) * 64
end
for m = 1, M do
local gf = gsrc[gb + m]
yr[o + m] *= gf
yi[o + m] *= gf
end
else
for m = 1, M do
local gf, qf = G_B[base + m], Q_Z[base + m]
ni += 1
if ni >= 512 then
ni = 0
end
yr[o + m] = yr[o + m] * gf + qf * NOISE_R[ni + 1]
yi[o + m] = yi[o + m] * gf + qf * NOISE_I[ni + 1]
end
end
if SINES[l] > 0 then
local sinRe = if fSine == 0 then 1 elseif fSine == 2 then -1 else 0
local sinIm = if fSine == 1 then 1 elseif fSine == 3 then -1 else 0
for m = 1, M do
if S_IDX[base + m] == 1 then
local sb = S_B[base + m]
yr[o + m] += sb * sinRe
if (m - 1 + kx) % 2 == 1 then
yi[o + m] -= sb * sinIm
else
yi[o + m] += sb * sinIm
end
end
end
end
end
local fNoise = (indexNoise + nCols * M) % 512
if nCols > 0 then
h.indexNoise = fNoise
h.indexSine = (fSine + 1) % 4
end
if hSl > 0 then
for j = 0, hSl - 1 do
local s = (nCols + j) * 64
local d = j * 64
for m = 1, M do
h.gTail[d + m] = G_T[s + m]
h.qTail[d + m] = Q_T[s + m]
end
end
h.tailM = M
else
h.tailM = -1
end
end
-- 4.6.18.7: adjust X_high (h.Yr / h.Yi) in place into Y
function SbrHf.adjust(e: any, c: any, hdr: any, h: any)
if e.reset or not h.started then
h.indexNoise, h.indexSine = 0, 0
h.tailM = -1
h.lAPrev, h.lEPrev = -1, 0
h.sPrevKx = 0
table.clear(h.sPrev)
for i = 1, 64 do
h.sPrev[i] = 0
end
h.started = true
end
local lAPrev = if h.lAPrev == h.lEPrev then 0 else -1
mapping(e, c, h)
local yr, yi = h.Yr, h.Yi
energies(e, c, hdr, yr, yi)
gains(e, c, hdr, lAPrev)
assemble(e, c, hdr, h, lAPrev, yr, yi)
local lb = (c.nEnv - 1) * 64
local sp = h.sPrev
for m = 1, 64 do
sp[m] = if m <= e.M then S_IDX[lb + m] else 0
end
h.sPrevKx = e.kx
h.lAPrev = c.lA
h.lEPrev = c.nEnv
end
return SbrHf
+115
View File
@@ -0,0 +1,115 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Aac/SbrQmf: the SBR 32-band complex analysis and 64-band synthesis QMF banks (ISO/IEC 14496-3
-- 4.6.18.4.1 / 4.6.18.4.2), through DCT-IV factorisations of the modulation (codecs/audio/Aac/SbrDct). Written from
-- the standard; the window is codecs/audio/Aac/SbrTables.QMF_WINDOW.
-- analysis: W(k) = 2 e^(-i 3 pi (k + 1/2) / 128) (DCT4_32(u(m) - u(63 - m)) + i DST4_32(u(m) + u(63 - m)))(k)
-- synthesis: v(n) = -(DCT4_64(Re X) - DST4_64(Im X))(n) / 64, v(127 - n) = (DCT4_64(Re X) + DST4_64(Im X))(n) / 64
local SbrDct = require("@rosetta/codecs/audio/Aac/SbrDct")
local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables")
local SbrQmf = {}
local C = SbrTables.QMF_WINDOW
local dct32, ana32, dct64, syn64 = SbrDct.dct32, SbrDct.ana32, SbrDct.dct64, SbrDct.syn64
-- the core decoder's samples are scaled by 1 / 32768; the SBR energies assume 16-bit sample values
local IN_SCALE = 32768
-- analysis taps per output n: CA[n] = { c[2n + 128j] * IN_SCALE, j = 0 .. 4 }
local CA: { { number } } = {}
for n = 0, 63 do
local w = table.create(5, 0)
for j = 0, 4 do
w[j + 1] = C[2 * n + 128 * j + 1] * IN_SCALE
end
CA[n + 1] = w
end
-- synthesis taps per output k: CS[k] = { c[64i + k], i = 0 .. 9 }
local CS: { { number } } = {}
for k = 1, 64 do
local w = table.create(10, 0)
for i = 0, 9 do
w[i + 1] = C[64 * i + k]
end
CS[k] = w
end
local A = table.create(32, 0)
local B = table.create(32, 0)
local CR = table.create(64, 0)
-- per-channel filterbank state
function SbrQmf.state(): any
return {
qa = table.create(320 + 1024, 0), -- analysis input: 320 history samples, then the frame
qs = table.create(41 * 128, 0), -- synthesis v blocks: 9 history blocks, then 32 for the frame
XLr = table.create(40 * 32, 0), -- X_low[col * 32 + k + 1], col 0..7 = the previous frame's 32..39
XLi = table.create(40 * 32, 0),
}
end
-- 32 analysis slots of one frame: 1024 core samples from inp[io + i * is] -> X_low columns 8 .. 39
function SbrQmf.analysis(s: any, inp: { number }, io: number, is: number)
local x: { number } = s.qa
local p = io
for i = 321, 1344 do
x[i] = inp[p]
p += is
end
local xr: { number }, xi: { number } = s.XLr, s.XLi
table.move(xr, 1025, 1280, 1)
table.move(xi, 1025, 1280, 1)
local a, b, cr = A, B, CR
for l = 0, 31 do
local t = 353 + 32 * l
for m = 1, 32 do
local q, w = t - m, CA[m]
local u0 = x[q] * w[1] + x[q - 64] * w[2] + x[q - 128] * w[3] + x[q - 192] * w[4] + x[q - 256] * w[5]
q, w = t - 65 + m, CA[65 - m]
local u1 = x[q] * w[1] + x[q - 64] * w[2] + x[q - 128] * w[3] + x[q - 192] * w[4] + x[q - 256] * w[5]
a[m] = u0 - u1
b[m] = u0 + u1
end
dct32(a, 0, cr)
ana32(b, 0, cr, xr, xi, (8 + l) * 32)
end
table.move(x, 1025, 1344, 1)
end
-- one synthesis slot: 64 complex subbands (xr / xi at [xo + k], k = 1 .. 64) -> 64 samples at out[o + j * os]
function SbrQmf.synthesis(s: any, slot: number, xr: { number }, xi: { number }, xo: number, out: { number }, o: number, os: number)
local v: { number } = s.qs
local base = (9 + slot) * 128
dct64(xr, xo, CR)
syn64(xi, xo, CR, v, base)
-- out(k) = sum_i v_(newest - 2i)[k] c[128i + k] + v_(newest - 2i - 1)[64 + k] c[128i + 64 + k]
local p = o
for k = 1, 64 do
local q = base + k
local w = CS[k]
out[p] = v[q] * w[1] + v[q - 64] * w[2] + v[q - 256] * w[3] + v[q - 320] * w[4] + v[q - 512] * w[5]
+ v[q - 576] * w[6] + v[q - 768] * w[7] + v[q - 832] * w[8] + v[q - 1024] * w[9] + v[q - 1088] * w[10]
p += os
end
end
function SbrQmf.endFrame(s: any)
local v: { number } = s.qs
table.move(v, 32 * 128 + 1, 41 * 128, 1)
end
function SbrQmf.reset(s: any)
for i = 1, 1344 do
s.qa[i] = 0
end
for i = 1, 41 * 128 do
s.qs[i] = 0
end
for i = 1, 1280 do
s.XLr[i], s.XLi[i] = 0, 0
end
end
return SbrQmf
+411
View File
@@ -0,0 +1,411 @@
--!strict
--!optimize 2
-- codecs/audio/Aac/SbrTables: SBR / PS constant tables (ISO/IEC 14496-3 4.A.6.1, 4.A.6.4, 8.A).
-- Numbers from syom 0.8.1 src/engine (MIT, licenses/syom-LICENSE.txt), transcribed there from the standard.
local SbrTables = {}
-- Table 4.A.89: c[i], the 640-tap QMF prototype window
SbrTables.QMF_WINDOW = {
0.0000000000, -0.0005525286, -0.0005617692, -0.0004947518, -0.0004875227, -0.0004893791, -0.0005040714, -0.0005226564,
-0.0005466565, -0.0005677802, -0.0005870930, -0.0006132747, -0.0006312493, -0.0006540333, -0.0006777690, -0.0006941614,
-0.0007157736, -0.0007255043, -0.0007440941, -0.0007490598, -0.0007681371, -0.0007724848, -0.0007834332, -0.0007779869,
-0.0007803664, -0.0007801449, -0.0007757977, -0.0007630793, -0.0007530001, -0.0007319357, -0.0007215391, -0.0006917937,
-0.0006650415, -0.0006341594, -0.0005946118, -0.0005564576, -0.0005145572, -0.0004606325, -0.0004095121, -0.0003501175,
-0.0002896981, -0.0002098337, -0.0001446380, -0.0000617334, 0.0000134949, 0.0001094383, 0.0002043017, 0.0002949531,
0.0004026540, 0.0005107388, 0.0006239376, 0.0007458025, 0.0008608443, 0.0009885988, 0.0011250155, 0.0012577884,
0.0013902494, 0.0015443219, 0.0016868083, 0.0018348265, 0.0019841140, 0.0021461583, 0.0023017254, 0.0024625616,
0.0026201758, 0.0027870464, 0.0029469447, 0.0031125420, 0.0032739613, 0.0034418874, 0.0036008268, 0.0037603922,
0.0039207432, 0.0040819753, 0.0042264269, 0.0043730719, 0.0045209852, 0.0046606460, 0.0047932560, 0.0049137603,
0.0050393022, 0.0051407353, 0.0052461166, 0.0053471681, 0.0054196775, 0.0054876040, 0.0055475714, 0.0055938023,
0.0056220643, 0.0056455196, 0.0056389199, 0.0056266114, 0.0055917128, 0.0055404363, 0.0054753783, 0.0053838975,
0.0052715758, 0.0051382275, 0.0049839687, 0.0048109469, 0.0046039530, 0.0043801861, 0.0041251642, 0.0038456408,
0.0035401246, 0.0032091885, 0.0028446757, 0.0024508540, 0.0020274176, 0.0015784682, 0.0010902329, 0.0005832264,
0.0000276045, -0.0005464280, -0.0011568135, -0.0018039472, -0.0024826723, -0.0031933778, -0.0039401124, -0.0047222596,
-0.0055337211, -0.0063792293, -0.0072615816, -0.0081798233, -0.0091325329, -0.0101150215, -0.0111315548, -0.0121849995,
0.0132718220, 0.0143904666, 0.0155405553, 0.0167324712, 0.0179433381, 0.0191872431, 0.0204531793, 0.0217467550,
0.0230680169, 0.0244160992, 0.0257875847, 0.0271859429, 0.0286072173, 0.0300502657, 0.0315017608, 0.0329754081,
0.0344620948, 0.0359697560, 0.0374812850, 0.0390053679, 0.0405349170, 0.0420649094, 0.0436097542, 0.0451488405,
0.0466843027, 0.0482165720, 0.0497385755, 0.0512556155, 0.0527630746, 0.0542452768, 0.0557173648, 0.0571616450,
0.0585915683, 0.0599837480, 0.0613455171, 0.0626857808, 0.0639715898, 0.0652247106, 0.0664367512, 0.0676075985,
0.0687043828, 0.0697630244, 0.0707628710, 0.0717002673, 0.0725682583, 0.0733620255, 0.0741003642, 0.0747452558,
0.0753137336, 0.0758008358, 0.0761992479, 0.0764992170, 0.0767093490, 0.0768173975, 0.0768230011, 0.0767204924,
0.0765050718, 0.0761748321, 0.0757305756, 0.0751576255, 0.0744664394, 0.0736406005, 0.0726774642, 0.0715826364,
0.0703533073, 0.0689664013, 0.0674525021, 0.0657690668, 0.0639444805, 0.0619602779, 0.0598166570, 0.0575152691,
0.0550460034, 0.0524093821, 0.0495978676, 0.0466303305, 0.0434768782, 0.0401458278, 0.0366418116, 0.0329583930,
0.0290824006, 0.0250307561, 0.0207997072, 0.0163701258, 0.0117623832, 0.0069636862, 0.0019765601, -0.0032086896,
-0.0085711749, -0.0141288827, -0.0198834129, -0.0258227288, -0.0319531274, -0.0382776572, -0.0447806821, -0.0514804176,
-0.0583705326, -0.0654409853, -0.0726943300, -0.0801372934, -0.0877547536, -0.0955533352, -0.1035329531, -0.1116826931,
-0.1200077984, -0.1285002850, -0.1371551761, -0.1459766491, -0.1549607071, -0.1640958855, -0.1733808172, -0.1828172548,
-0.1923966745, -0.2021250176, -0.2119735853, -0.2219652696, -0.2320690870, -0.2423016884, -0.2526480309, -0.2631053299,
-0.2736634040, -0.2843214189, -0.2950716717, -0.3059098575, -0.3168278913, -0.3278113727, -0.3388722693, -0.3499914122,
0.3611589903, 0.3723795546, 0.3836350013, 0.3949211761, 0.4062317676, 0.4175696896, 0.4289119920, 0.4402553754,
0.4515996535, 0.4629308085, 0.4742453214, 0.4855253091, 0.4967708254, 0.5079817500, 0.5191234970, 0.5302240895,
0.5412553448, 0.5522051258, 0.5630789140, 0.5738524131, 0.5845403235, 0.5951123086, 0.6055783538, 0.6159109932,
0.6261242695, 0.6361980107, 0.6461269695, 0.6559016302, 0.6655139880, 0.6749663190, 0.6842353293, 0.6933282376,
0.7022388719, 0.7109410426, 0.7194462634, 0.7277448900, 0.7358211758, 0.7436827863, 0.7513137456, 0.7587080760,
0.7658674865, 0.7727780881, 0.7794287519, 0.7858353120, 0.7919735841, 0.7978466413, 0.8034485751, 0.8087695004,
0.8138191270, 0.8185776004, 0.8230419890, 0.8272275347, 0.8311038457, 0.8346937361, 0.8379717337, 0.8409541392,
0.8436238281, 0.8459818469, 0.8480315777, 0.8497805198, 0.8511971524, 0.8523047035, 0.8531020949, 0.8535720573,
0.8537385600, 0.8535720573, 0.8531020949, 0.8523047035, 0.8511971524, 0.8497805198, 0.8480315777, 0.8459818469,
0.8436238281, 0.8409541392, 0.8379717337, 0.8346937361, 0.8311038457, 0.8272275347, 0.8230419890, 0.8185776004,
0.8138191270, 0.8087695004, 0.8034485751, 0.7978466413, 0.7919735841, 0.7858353120, 0.7794287519, 0.7727780881,
0.7658674865, 0.7587080760, 0.7513137456, 0.7436827863, 0.7358211758, 0.7277448900, 0.7194462634, 0.7109410426,
0.7022388719, 0.6933282376, 0.6842353293, 0.6749663190, 0.6655139880, 0.6559016302, 0.6461269695, 0.6361980107,
0.6261242695, 0.6159109932, 0.6055783538, 0.5951123086, 0.5845403235, 0.5738524131, 0.5630789140, 0.5522051258,
0.5412553448, 0.5302240895, 0.5191234970, 0.5079817500, 0.4967708254, 0.4855253091, 0.4742453214, 0.4629308085,
0.4515996535, 0.4402553754, 0.4289119920, 0.4175696896, 0.4062317676, 0.3949211761, 0.3836350013, 0.3723795546,
-0.3611589903, -0.3499914122, -0.3388722693, -0.3278113727, -0.3168278913, -0.3059098575, -0.2950716717, -0.2843214189,
-0.2736634040, -0.2631053299, -0.2526480309, -0.2423016884, -0.2320690870, -0.2219652696, -0.2119735853, -0.2021250176,
-0.1923966745, -0.1828172548, -0.1733808172, -0.1640958855, -0.1549607071, -0.1459766491, -0.1371551761, -0.1285002850,
-0.1200077984, -0.1116826931, -0.1035329531, -0.0955533352, -0.0877547536, -0.0801372934, -0.0726943300, -0.0654409853,
-0.0583705326, -0.0514804176, -0.0447806821, -0.0382776572, -0.0319531274, -0.0258227288, -0.0198834129, -0.0141288827,
-0.0085711749, -0.0032086896, 0.0019765601, 0.0069636862, 0.0117623832, 0.0163701258, 0.0207997072, 0.0250307561,
0.0290824006, 0.0329583930, 0.0366418116, 0.0401458278, 0.0434768782, 0.0466303305, 0.0495978676, 0.0524093821,
0.0550460034, 0.0575152691, 0.0598166570, 0.0619602779, 0.0639444805, 0.0657690668, 0.0674525021, 0.0689664013,
0.0703533073, 0.0715826364, 0.0726774642, 0.0736406005, 0.0744664394, 0.0751576255, 0.0757305756, 0.0761748321,
0.0765050718, 0.0767204924, 0.0768230011, 0.0768173975, 0.0767093490, 0.0764992170, 0.0761992479, 0.0758008358,
0.0753137336, 0.0747452558, 0.0741003642, 0.0733620255, 0.0725682583, 0.0717002673, 0.0707628710, 0.0697630244,
0.0687043828, 0.0676075985, 0.0664367512, 0.0652247106, 0.0639715898, 0.0626857808, 0.0613455171, 0.0599837480,
0.0585915683, 0.0571616450, 0.0557173648, 0.0542452768, 0.0527630746, 0.0512556155, 0.0497385755, 0.0482165720,
0.0466843027, 0.0451488405, 0.0436097542, 0.0420649094, 0.0405349170, 0.0390053679, 0.0374812850, 0.0359697560,
0.0344620948, 0.0329754081, 0.0315017608, 0.0300502657, 0.0286072173, 0.0271859429, 0.0257875847, 0.0244160992,
0.0230680169, 0.0217467550, 0.0204531793, 0.0191872431, 0.0179433381, 0.0167324712, 0.0155405553, 0.0143904666,
-0.0132718220, -0.0121849995, -0.0111315548, -0.0101150215, -0.0091325329, -0.0081798233, -0.0072615816, -0.0063792293,
-0.0055337211, -0.0047222596, -0.0039401124, -0.0031933778, -0.0024826723, -0.0018039472, -0.0011568135, -0.0005464280,
0.0000276045, 0.0005832264, 0.0010902329, 0.0015784682, 0.0020274176, 0.0024508540, 0.0028446757, 0.0032091885,
0.0035401246, 0.0038456408, 0.0041251642, 0.0043801861, 0.0046039530, 0.0048109469, 0.0049839687, 0.0051382275,
0.0052715758, 0.0053838975, 0.0054753783, 0.0055404363, 0.0055917128, 0.0056266114, 0.0056389199, 0.0056455196,
0.0056220643, 0.0055938023, 0.0055475714, 0.0054876040, 0.0054196775, 0.0053471681, 0.0052461166, 0.0051407353,
0.0050393022, 0.0049137603, 0.0047932560, 0.0046606460, 0.0045209852, 0.0043730719, 0.0042264269, 0.0040819753,
0.0039207432, 0.0037603922, 0.0036008268, 0.0034418874, 0.0032739613, 0.0031125420, 0.0029469447, 0.0027870464,
0.0026201758, 0.0024625616, 0.0023017254, 0.0021461583, 0.0019841140, 0.0018348265, 0.0016868083, 0.0015443219,
0.0013902494, 0.0012577884, 0.0011250155, 0.0009885988, 0.0008608443, 0.0007458025, 0.0006239376, 0.0005107388,
0.0004026540, 0.0002949531, 0.0002043017, 0.0001094383, 0.0000134949, -0.0000617334, -0.0001446380, -0.0002098337,
-0.0002896981, -0.0003501175, -0.0004095121, -0.0004606325, -0.0005145572, -0.0005564576, -0.0005946118, -0.0006341594,
-0.0006650415, -0.0006917937, -0.0007215391, -0.0007319357, -0.0007530001, -0.0007630793, -0.0007757977, -0.0007801449,
-0.0007803664, -0.0007779869, -0.0007834332, -0.0007724848, -0.0007681371, -0.0007490598, -0.0007440941, -0.0007255043,
-0.0007157736, -0.0006941614, -0.0006777690, -0.0006540333, -0.0006312493, -0.0006132747, -0.0005870930, -0.0005677802,
-0.0005466565, -0.0005226564, -0.0005040714, -0.0004893791, -0.0004875227, -0.0004947518, -0.0005617692, -0.000552528,
}
-- Table 4.A.88: V[k], the SBR noise table (re, im interleaved)
SbrTables.NOISE = {
-0.99948153278296, -0.59483417516607, 0.97113454393991, -0.67528515225647, 0.14130051758487, -0.95090983575689, -0.47005496701697, -0.37340549728647,
0.80705063769351, 0.29653668284408, -0.38981478896926, 0.89572605717087, -0.01053049862020, -0.66959058036166, -0.91266367957293, -0.11522938140034,
0.54840422910309, 0.75221367176302, 0.40009252867955, -0.98929400334421, -0.99867974711855, -0.88147068645358, -0.95531076805040, 0.90908757154593,
-0.45725933317144, -0.56716323646760, -0.72929675029275, -0.98008272727324, 0.75622801399036, 0.20950329995549, 0.07069442601050, -0.78247898470706,
0.74496252926055, -0.91169004445807, -0.96440182703856, -0.94739918296622, 0.30424629369539, -0.49438267012479, 0.66565033746925, 0.64652935542491,
0.91697008020594, 0.17514097332009, -0.70774918760427, 0.52548653416543, -0.70051415345560, -0.45340028808763, -0.99496513054797, -0.90071908066973,
0.98164490790123, -0.77463155528697, -0.54671580548181, -0.02570928536004, -0.01689629065389, 0.00287506445732, -0.86110349531986, 0.42548583726477,
-0.98892980586032, -0.87881132267556, 0.51756627678691, 0.66926784710139, -0.99635026409640, -0.58107730574765, -0.99969370862163, 0.98369989360250,
0.55266258627194, 0.59449057465591, 0.34581177741673, 0.94879421061866, 0.62664209577999, -0.74402970906471, -0.77149701404973, -0.33883658042801,
-0.91592244254432, 0.03687901376713, -0.76285492357887, -0.91371867919124, 0.79788337195331, -0.93180971199849, 0.54473080610200, -0.11919206037186,
-0.85639281671058, 0.42429854760451, -0.92882402971423, 0.27871809078609, -0.11708371046774, -0.99800843444966, 0.21356749817493, -0.90716295627033,
-0.76191692573909, 0.99768118356265, 0.98111043100884, -0.95854459734407, -0.85913269895572, 0.95766566168880, -0.93307242253692, 0.49431757696466,
0.30485754879632, -0.70540034357529, 0.85289650925190, 0.46766131791044, 0.91328082618125, -0.99839597361769, -0.05890199924154, 0.70741827819497,
0.28398686150148, 0.34633555702188, 0.95258164539612, -0.54893416026939, -0.78566324168507, -0.75568541079691, -0.95789495447877, -0.20423194696966,
0.82411158711197, 0.96654618432562, -0.65185446735885, -0.88734990773289, -0.93643603134666, 0.99870790442385, 0.91427159529618, -0.98290505544444,
-0.70395684036886, 0.58796798221039, 0.00563771969365, 0.61768196727244, 0.89065051931895, 0.52783352697585, -0.68683707712762, 0.80806944710339,
0.72165342518718, -0.69259857349564, -0.62928247730667, 0.13627037407335, 0.29938434065514, -0.46051329682246, -0.91781958879280, -0.74012716684186,
0.99298717043688, 0.40816610075661, 0.82368298622748, -0.74036047190173, -0.98512833386833, -0.99972330709594, -0.95915368242257, -0.99237800466040,
-0.21411126572790, -0.93424819052545, -0.68821476106884, -0.26892306315457, 0.91851997982317, 0.09358228901785, -0.96062769559127, 0.36099095133739,
0.51646184922287, -0.71373332873917, 0.61130721139669, 0.46950141175917, 0.47336129371299, -0.27333178296162, 0.90998308703519, 0.96715662938132,
0.44844799194357, 0.99211574628306, 0.66614891079092, 0.96590176169121, 0.74922239129237, -0.89879858826087, -0.99571588506485, 0.52785521494349,
0.97401082477563, -0.16855870075190, 0.72683747733879, -0.48060774432251, 0.95432193457128, 0.68849603408441, -0.72962208425191, -0.76608443420917,
-0.85359479233537, 0.88738125901579, -0.81412430338535, -0.97480768049637, -0.87930772356786, 0.74748307690436, -0.71573331064977, -0.98570608178923,
0.83524300028228, 0.83702537075163, -0.48086065601423, -0.98848504923531, 0.97139128574778, 0.80093621198236, 0.51992825347895, 0.80247631400510,
-0.00848591195325, -0.76670128000486, -0.70294374303036, 0.55359910445577, -0.95894428168140, -0.43265504344783, 0.97079252950321, 0.09325857238682,
-0.92404293670797, 0.85507704027855, -0.69506469500450, 0.98633412625459, 0.26559203620024, 0.73314307966524, 0.28038443336943, 0.14537913654427,
-0.74138124825523, 0.99310339807762, -0.01752795995444, -0.82616635284178, -0.55126773094930, -0.98898543862153, 0.97960898850996, -0.94021446752851,
-0.99196309146936, 0.67019017358456, -0.67684928085260, 0.12631491649378, 0.09140039465500, -0.20537731453108, -0.71658965751996, -0.97788200391224,
0.81014640078925, 0.53722648362443, 0.40616991671205, -0.26469008598449, -0.67680188682972, 0.94502052337695, 0.86849774348749, -0.18333598647899,
-0.99500381284851, -0.02634122068550, 0.84329189340667, 0.10406957462213, -0.09215968531446, 0.69540012101253, 0.99956173327206, -0.12358542001404,
-0.79732779473535, -0.91582524736159, 0.96349973642406, 0.96640458041000, -0.79942778496547, 0.64323902822857, -0.11566039853896, 0.28587846253726,
-0.39922954514662, 0.94129601616966, 0.99089197565987, -0.92062625581587, 0.28631285179909, -0.91035047143603, -0.83302725605608, -0.67330410892084,
0.95404443402072, 0.49162765398743, -0.06449863579434, 0.03250560813135, -0.99575054486311, 0.42389784469507, -0.65501142790847, 0.82546114655624,
-0.81254441908887, -0.51627234660629, -0.99646369485481, 0.84490533520752, 0.00287840603348, 0.64768261158166, 0.70176989408455, -0.20453028573322,
0.96361882270190, 0.40706967140989, -0.68883758192426, 0.91338958840772, -0.34875585502238, 0.71472290693300, 0.91980081243087, 0.66507455644919,
-0.99009048343881, 0.85868021604848, 0.68865791458395, 0.55660316809678, -0.99484402129368, -0.20052559254934, 0.94214511408023, -0.99696425367461,
-0.67414626793544, 0.49548221180078, -0.47339353684664, -0.85904328834047, 0.14323651387360, -0.94145598222488, -0.29268293575672, 0.05759224927952,
0.43793861458754, -0.78904969892724, -0.36345126374441, 0.64874435357162, -0.08750604656825, 0.97686944362527, -0.96495267812511, -0.53960305946511,
0.55526940659947, 0.78891523734774, 0.73538215752630, 0.96452072373404, -0.30889773919437, -0.80664389776860, 0.03574995626194, -0.97325616900959,
0.98720684660488, 0.48409133691962, -0.81689296271203, -0.90827703628298, 0.67866860118215, 0.81284503870856, -0.15808569732583, 0.85279555024382,
0.80723395114371, -0.24717418514605, 0.47788757329038, -0.46333147839295, 0.96367554763201, 0.38486749303242, -0.99143875716818, -0.24945277239809,
0.83081876925833, -0.94780851414763, -0.58753191905341, 0.01290772389163, 0.95538108220960, -0.85557052096538, -0.96490920476211, -0.64020970923102,
-0.97327101028521, 0.12378128133110, 0.91400366022124, 0.57972471346930, -0.99925837363824, 0.71084847864067, -0.86875903507313, -0.20291699203564,
-0.26240034795124, -0.68264554369108, -0.24664412953388, -0.87642273115183, 0.02416275806869, 0.27192914288905, 0.82068619590515, -0.85087787994476,
0.88547373760759, -0.89636802901469, -0.18173078152226, -0.26152145156800, 0.09355476558534, 0.54845123045604, -0.54668414224090, 0.95980774020221,
0.37050990604091, -0.59910140383171, -0.70373594262891, 0.91227665827081, -0.34600785879594, -0.99441426144200, -0.68774481731008, -0.30238837956299,
-0.26843291251234, 0.83115668004362, 0.49072334613242, -0.45359708737775, 0.38975993093975, 0.95515358099121, -0.97757125224150, 0.05305894580606,
-0.17325552859616, -0.92770672250494, 0.99948035025744, 0.58285545563426, -0.64946246527458, 0.68645507104960, -0.12016920576437, -0.57147322153312,
-0.58947456517751, -0.34847132454388, -0.41815140454465, 0.16276422358861, 0.99885650204884, 0.11136095490444, -0.56649614128386, -0.90494866361587,
0.94138021032330, 0.35281916733018, -0.75725076534641, 0.53650549640587, 0.20541973692630, -0.94435144369918, 0.99980371023351, 0.79835913565599,
0.29078277605775, 0.35393777921520, -0.62858772103030, 0.38765693387102, 0.43440904467688, -0.98546330463232, -0.98298583762390, 0.21021524625209,
0.19513029146934, -0.94239832251867, -0.95476662400101, 0.98364554179143, 0.93379635304810, -0.70881994583682, -0.85235410573336, -0.08342347966410,
-0.86425093011245, -0.45795025029466, 0.38879779059045, 0.97274429344593, 0.92045124735495, -0.62433652524220, 0.89162532251878, 0.54950955570563,
-0.36834336949252, 0.96458298020975, 0.93891760988045, -0.89968353740388, 0.99267657565094, -0.03757034316958, -0.94063471614176, 0.41332338538963,
0.99740224117019, -0.16830494996370, -0.35899413170555, -0.46633226649613, 0.05237237274947, -0.25640361602661, 0.36703583957424, -0.38653265641875,
0.91653180367913, -0.30587628726597, 0.69000803499316, 0.90952171386132, -0.38658751133527, 0.99501571208985, -0.29250814029851, 0.37444994344615,
-0.60182204677608, 0.86779651036123, -0.97418588163217, 0.96468523666475, 0.88461574003963, 0.57508405276414, 0.05198933055162, 0.21269661669964,
-0.53499621979720, 0.97241553731237, -0.49429560226497, 0.98183865291903, -0.98935142339139, -0.40249159006933, -0.98081380091130, -0.72856895534041,
-0.27338148835532, 0.99950922447209, 0.06310802338302, -0.54539587529618, -0.20461677199539, -0.14209977628489, 0.66223843141647, 0.72528579940326,
-0.84764345483665, 0.02372316801261, -0.89039863483811, 0.88866581484602, 0.95903308477986, 0.76744927173873, 0.73504123909879, -0.03747203173192,
-0.31744434966056, -0.36834111883652, -0.34110827591623, 0.40211222807691, 0.47803883714199, -0.39423219786288, 0.98299195879514, 0.01989791390047,
-0.30963073129751, -0.18076720599336, 0.99992588229018, -0.26281872094289, -0.93149731080767, -0.98313162570490, 0.99923472302773, -0.80142993767554,
-0.26024169633417, -0.75999759855752, -0.35712514743563, 0.19298963768574, -0.99899084509530, 0.74645156992493, 0.86557171579452, 0.55593866696299,
0.33408042438752, 0.86185953874709, 0.99010736374716, 0.04602397576623, -0.66694269691195, -0.91643611810148, 0.64016792079480, 0.15649530836856,
0.99570534804836, 0.45844586038111, -0.63431466947340, 0.21079116459234, -0.07706847005931, -0.89581437101329, 0.98590090577724, 0.88241721133981,
0.80099335254678, -0.36851896710853, 0.78368131392666, 0.45506999802597, 0.08707806671691, 0.80938994918745, -0.86811883080712, 0.39347308654705,
-0.39466529740375, -0.66809432114456, 0.97875325649683, -0.72467840967746, -0.95038560288864, 0.89563219587625, 0.17005239424212, 0.54683053962658,
-0.76910792026848, -0.96226617549298, 0.99743281016846, 0.42697157037567, 0.95437383549973, 0.97002324109952, 0.99578905365569, -0.54106826257356,
0.28058259829990, -0.85361420634036, 0.85256524470573, -0.64567607735589, -0.50608540105128, -0.65846015480300, -0.97210735183243, -0.23095213067791,
0.95424048234441, -0.99240147091219, -0.96926570524023, 0.73775654896574, 0.30872163214726, 0.41514960556126, -0.24523839572639, 0.63206633394807,
-0.33813265086024, -0.38661779441897, -0.05826828420146, -0.06940774188029, -0.22898461455054, 0.97054853316316, -0.18509915019881, 0.47565762892084,
-0.10488238045009, -0.87769947402394, -0.71886586182037, 0.78030982480538, 0.99793873738654, 0.90041310491497, 0.57563307626120, -0.91034337352097,
0.28909646383717, 0.96307783970534, 0.42188998312520, 0.48148651230437, 0.93335049681047, -0.43537023883588, -0.97087374418267, 0.86636445711364,
0.36722871286923, 0.65291654172961, -0.81093025665696, 0.08778370229363, -0.26240603062237, -0.92774095379098, 0.83996497984604, 0.55839849139647,
-0.99909615720225, -0.96024605713970, 0.74649464155061, 0.12144893606462, -0.74774595569805, -0.26898062008959, 0.95781667469567, -0.79047927052628,
0.95472308713099, -0.08588776019550, 0.48708332746299, 0.99999041579432, 0.46332038247497, 0.10964126185063, -0.76497004940162, 0.89210929242238,
0.57397389364339, 0.35289703373760, 0.75374316974495, 0.96705214651335, -0.59174397685714, -0.89405370422752, 0.75087906691890, -0.29612672982396,
-0.98607857336230, 0.25034911730023, -0.40761056640505, -0.90045573444695, 0.66929266740477, 0.98629493401748, -0.97463695257310, -0.00190223301301,
0.90145509409859, 0.99781390365446, -0.87259289048043, 0.99233587353666, -0.91529461447692, -0.15698707534206, -0.03305738840705, -0.37205262859764,
0.07223051368337, -0.88805001733626, 0.99498012188353, 0.97094358113387, -0.74904939500519, 0.99985483641521, 0.04585228574211, 0.99812337444082,
-0.89054954257993, -0.31791913188064, -0.83782144651251, 0.97637632547466, 0.33454804933804, -0.86231516800408, -0.99707579362824, 0.93237990079441,
-0.22827527843994, 0.18874759397997, 0.67248046289143, -0.03646211390569, -0.05146538187944, -0.92599700120679, 0.99947295749905, 0.93625229707912,
0.66951124390363, 0.98905825623893, -0.99602956559179, -0.44654715757688, 0.82104905483590, 0.99540741724928, 0.99186510988782, 0.72023001312947,
-0.65284592392918, 0.52186723253637, 0.93885443798188, -0.74895312615259, 0.96735248738388, 0.90891816978629, -0.22225968841114, 0.57124029781228,
-0.44132783753414, -0.92688840659280, -0.85694974219574, 0.88844532719844, 0.91783042091762, -0.46356892383970, 0.72556974415690, -0.99899555770747,
-0.99711581834508, 0.58211560180426, 0.77638976371966, 0.94321834873819, 0.07717324253925, 0.58638399856595, -0.56049829194163, 0.82522301569036,
0.98398893639988, 0.39467440420569, 0.47546946844938, 0.68613044836811, 0.65675089314631, 0.18331637134880, 0.03273375457980, -0.74933109564108,
-0.38684144784738, 0.51337349030406, -0.97346267944545, -0.96549364384098, -0.53282156061942, -0.91423265091354, 0.99817310731176, 0.61133572482148,
-0.50254500772635, -0.88829338134294, 0.01995873238855, 0.85223515096765, 0.99930381973804, 0.94578896296649, 0.82907767600783, -0.06323442598128,
-0.58660709669728, 0.96840773806582, -0.17573736667267, -0.48166920859485, 0.83434292401346, -0.13023450646997, 0.05946491307025, 0.20511047074866,
0.81505484574602, -0.94685947861369, -0.44976380954860, 0.40894572671545, -0.89746474625671, 0.99846578838537, 0.39677256130792, -0.74854668609359,
-0.07588948563079, 0.74096214084170, 0.76343198951445, 0.41746629422634, -0.74490104699626, 0.94725911744610, 0.64880119792759, 0.41336660830571,
0.62319537462542, -0.93098313552599, 0.42215817594807, -0.07712787385208, 0.02704554141885, -0.05417518053666, 0.80001773566818, 0.91542195141039,
-0.79351832348816, -0.36208897989136, 0.63872359151636, 0.08128252493444, 0.52890520960295, 0.60048872455592, 0.74238552914587, 0.04491915291044,
0.99096131449250, -0.19451182854402, -0.80412329643109, -0.88513818199457, -0.64612616129736, 0.72198674804544, 0.11657770663191, -0.83662833815041,
-0.95053182488101, -0.96939905138082, -0.62228872928622, 0.82767262846661, 0.03004475787316, -0.99738896333384, -0.97987214341034, 0.36526129686425,
-0.99986980746200, -0.36021610299715, 0.89110648599879, -0.97894250343044, 0.10407960510582, 0.77357793811619, 0.95964737821728, -0.35435818285502,
0.50843233159162, 0.96107691266205, 0.17006334670615, -0.76854025314829, 0.25872675063360, 0.99893303933816, -0.01115998681937, 0.98496019742444,
-0.79598702973261, 0.97138411318894, -0.99264708948101, -0.99542822402536, -0.99829663752818, 0.01877138824311, -0.70801016548184, 0.33680685948117,
-0.70467057786826, 0.93272777501857, 0.99846021905254, -0.98725746254433, -0.63364968534650, -0.16473594423746, -0.16258217500792, -0.95939125400802,
-0.43645594360633, -0.94805030113284, -0.99848471702976, 0.96245166923809, -0.16796458968998, -0.98987511890470, -0.87979225745213, -0.71725725041680,
0.44183099021786, -0.93568974498761, 0.93310180125532, -0.99913308068246, -0.93941931782002, -0.56409379640356, -0.88590003188677, 0.47624600491382,
0.99971463703691, -0.83889954253462, -0.75376385639978, 0.00814643438625, 0.93887685615875, -0.11284528204636, 0.85126435782309, 0.52349251543547,
0.39701421446381, 0.81779634174316, -0.37024464187437, -0.87071656222959, -0.36024828242896, 0.34655735648287, -0.93388812549209, -0.84476541096429,
-0.65298804552119, -0.18439575450921, 0.11960319006843, 0.99899346780168, 0.94292565553160, 0.83163906518293, 0.75081145286948, -0.35533223142265,
0.56721979748394, -0.24076836414499, 0.46857766746029, -0.30140233457198, 0.97312313923635, -0.99548191630031, -0.38299976567017, 0.98516909715427,
0.41025800019463, 0.02116736935734, 0.09638062008048, 0.04411984381457, -0.85283249275397, 0.91475563922421, 0.88866808958124, -0.99735267083226,
-0.48202429536989, -0.96805608884164, 0.27572582416567, 0.58634753335832, -0.65889129659168, 0.58835634138583, 0.98838086953732, 0.99994349600236,
-0.20651349620689, 0.54593044066355, -0.62126416356920, -0.59893681700392, 0.20320105410437, -0.86879180355289, -0.97790548600584, 0.96290806999242,
0.11112534735126, 0.21484763313301, -0.41368337314182, 0.28216837680365, 0.24133038992960, 0.51294362630238, -0.66393410674885, -0.08249679629081,
-0.53697829178752, -0.97649903936228, -0.97224737889348, 0.22081333579837, 0.87392477144549, -0.12796173740361, 0.19050361015753, 0.01602615387195,
-0.46353441212724, -0.95249041539006, -0.07064096339021, -0.94479803205886, -0.92444085484466, -0.10457590187436, -0.83822593578728, -0.01695043208885,
0.75214681811150, -0.99955681042665, -0.42102998829339, 0.99720941999394, -0.72094786237696, -0.35008961934255, 0.78843311019251, 0.52851398958271,
0.97394027897442, -0.26695944086561, 0.99206463477946, -0.57010120849429, 0.76789609461795, -0.76519356730966, -0.82002421836409, -0.73530179553767,
0.81924990025724, 0.99698425250579, -0.26719850873357, 0.68903369776193, -0.43311260380975, 0.85321815947490, 0.99194979673836, 0.91876249766422,
-0.80692001248487, -0.32627540663214, 0.43080003649976, -0.21919095636638, 0.67709491937357, -0.95478075822906, 0.56151770568316, -0.70693811747778,
0.10831862810749, -0.08628837174592, 0.91229417540436, -0.65987351408410, -0.48972893932274, 0.56289246362686, -0.89033658689697, -0.71656563987082,
0.65269447475094, 0.65916004833932, 0.67439478141121, -0.81684380846796, -0.47770832416973, -0.16789556203025, -0.99715979260878, -0.93565784007648,
-0.90889593602546, 0.62034397054380, -0.06618622548177, -0.23812217221359, 0.99430266919728, 0.18812555317553, 0.97686402381843, -0.28664534366620,
0.94813650221268, -0.97506640027128, -0.95434497492853, -0.79607978501983, -0.49104783137150, 0.32895214359663, 0.99881175120751, 0.88993983831354,
0.50449166760303, -0.85995072408434, 0.47162891065108, -0.18680204049569, -0.62081581361840, 0.75000676218956, -0.43867015250812, 0.99998069244322,
0.98630563232075, -0.53578899600662, -0.61510362277374, -0.89515019899997, -0.03841517601843, -0.69888815681179, -0.30102157304644, -0.07667808922205,
0.41881284182683, 0.02188098922282, -0.86135454941237, 0.98947480909359, 0.67226861393788, -0.13494389011014, -0.70737398842068, -0.76547349325992,
0.94044946687963, 0.09026201157416, -0.82386352534327, 0.08924768823676, -0.32070666698656, 0.50143421908753, 0.57593163224487, -0.98966422921509,
-0.36326018419965, 0.07440243123228, 0.99979044674350, -0.14130287347405, -0.92366023326932, -0.97979298068180, -0.44607178518598, -0.54233252016394,
0.44226800932956, 0.71326756742752, 0.03671907158312, 0.63606389366675, 0.52175424682195, -0.85396826735705, -0.94701139690956, -0.01826348194255,
-0.98759606946049, 0.82288714303073, 0.87434794743625, 0.89399495655433, -0.93412041758744, 0.41374052024363, 0.96063943315511, 0.93116709541280,
0.97534253457837, 0.86150930812689, 0.99642466504163, 0.70190043427512, -0.94705089665984, -0.29580042814306, 0.91599807087376, -0.98147830385781,
}
-- Huffman codebooks (4.A.6.1 / 8.A.1): { lav, lengths, codes }; symbol index - lav = value
SbrTables.T_ENV15 = {
60,
{ 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 17, 18, 16, 17, 18, 17, 16, 16, 16, 16, 15, 14, 14, 13, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 14, 15, 16, 17, 16, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 },
{
262102, 262103, 262104, 262105, 262106, 262107, 524216, 524217, 524218, 524219,
524220, 524221, 524222, 524223, 524224, 524225, 524226, 524227, 524228, 524229,
524230, 524231, 524232, 524233, 524234, 524235, 524236, 524237, 524238, 524239,
524240, 524241, 524242, 524243, 131046, 262100, 65520, 131049, 262101, 131047,
65521, 65516, 65517, 65518, 32756, 16377, 16375, 8186, 8185, 4091,
2044, 1020, 509, 253, 125, 61, 29, 13, 5, 1,
0, 4, 12, 28, 60, 124, 252, 508, 1021, 4090,
8184, 16374, 16376, 32757, 65519, 131048, 65522, 524244, 524245, 524246,
524247, 524248, 524249, 524250, 524251, 524252, 524253, 524254, 524255, 524256,
524257, 524258, 524259, 524260, 524261, 524262, 524263, 524264, 524265, 524266,
524267, 524268, 524269, 524270, 524271, 524272, 524273, 524274, 524275, 524276,
524277, 524278, 524279, 524280, 524281, 524282, 524283, 524284, 524285, 524286,
524287,
},
}
SbrTables.F_ENV15 = {
60,
{ 19, 19, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 19, 20, 19, 19, 20, 18, 20, 20, 20, 19, 20, 20, 20, 19, 20, 19, 18, 19, 18, 18, 17, 18, 17, 17, 17, 16, 16, 16, 15, 15, 14, 13, 13, 12, 12, 11, 10, 9, 9, 8, 7, 6, 5, 4, 3, 2, 2, 3, 4, 5, 6, 8, 8, 9, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 16, 16, 17, 17, 18, 18, 18, 18, 18, 18, 18, 20, 19, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 19, 20, 18, 20, 20, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 },
{
524263, 524264, 1048530, 1048531, 1048532, 1048533, 1048534, 1048535, 1048536, 524250,
1048537, 1048538, 1048539, 1048540, 524251, 1048541, 524252, 524253, 1048542, 262116,
1048543, 1048544, 1048545, 524254, 1048546, 1048547, 1048548, 524255, 1048549, 524256,
262120, 524257, 262112, 262121, 131055, 262117, 131052, 131053, 131054, 65524,
65523, 65520, 32759, 32758, 16378, 8186, 8185, 4090, 4088, 2041,
1019, 508, 506, 251, 124, 60, 28, 12, 5, 1,
0, 4, 13, 29, 61, 250, 252, 507, 1018, 2040,
2042, 2043, 4089, 4091, 8184, 8187, 16376, 16377, 65521, 65522,
131050, 131051, 262113, 262114, 262122, 262115, 262118, 262119, 262123, 1048550,
524258, 1048551, 1048552, 1048553, 1048554, 1048555, 1048556, 524259, 1048557, 1048558,
1048559, 1048560, 524260, 1048561, 262124, 1048562, 1048563, 524261, 524262, 1048564,
1048565, 1048566, 1048567, 1048568, 1048569, 1048570, 1048571, 1048572, 1048573, 1048574,
1048575,
},
}
SbrTables.T_BAL15 = {
24,
{ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 12, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 11, 12, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
{
65508, 65509, 65510, 65511, 65512, 65513, 65514, 65515, 65516, 65517,
65518, 65519, 65520, 65521, 65522, 65523, 65524, 65506, 4092, 2044,
510, 126, 30, 6, 0, 2, 14, 62, 254, 2045,
4093, 32752, 65507, 65525, 65526, 65527, 65528, 65529, 65530, 131062,
131063, 131064, 131065, 131066, 131067, 131068, 131069, 131070, 131071,
},
}
SbrTables.F_BAL15 = {
24,
{ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 16, 17, 14, 11, 11, 8, 7, 4, 2, 1, 3, 5, 6, 9, 11, 12, 15, 16, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19 },
{
262114, 262115, 262116, 262117, 262118, 262119, 262120, 262121, 262122, 262123,
262124, 262125, 262126, 262127, 262128, 65527, 131056, 16380, 2046, 2044,
254, 126, 14, 2, 0, 6, 30, 62, 510, 2045,
4094, 32762, 65526, 262129, 262130, 262131, 262132, 262133, 262134, 262135,
262136, 262137, 262138, 262139, 262140, 262141, 262142, 524286, 524287,
},
}
SbrTables.T_ENV30 = {
31,
{ 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 17, 16, 16, 16, 14, 14, 14, 13, 12, 11, 8, 6, 4, 2, 1, 3, 5, 7, 9, 11, 13, 14, 14, 15, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 },
{
262125, 262126, 524254, 524255, 524256, 524257, 524258, 524259, 524260, 524261,
524262, 524263, 524264, 524265, 524266, 524267, 524268, 131060, 65527, 65529,
65528, 16379, 16378, 16376, 8186, 4092, 2044, 254, 62, 14,
2, 0, 6, 30, 126, 510, 2045, 8187, 16377, 16380,
32762, 65526, 131061, 262124, 524269, 524270, 524271, 524272, 524273, 524274,
524275, 524276, 524277, 524278, 524279, 524280, 524281, 524282, 524283, 524284,
524285, 524286, 524287,
},
}
SbrTables.F_ENV30 = {
31,
{ 20, 20, 20, 20, 20, 20, 20, 18, 19, 19, 19, 19, 18, 18, 20, 19, 17, 18, 17, 16, 16, 15, 14, 12, 11, 10, 9, 8, 6, 4, 2, 1, 3, 5, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 20, 19, 19, 19, 20, 19, 19, 20, 20, 20, 20, 20, 20 },
{
1048560, 1048561, 1048562, 1048563, 1048564, 1048565, 1048566, 262131, 524277, 524270,
524271, 524278, 262132, 262130, 1048567, 524272, 131061, 262128, 131060, 65527,
65526, 32760, 16379, 4093, 2045, 1021, 509, 253, 62, 14,
2, 0, 6, 30, 252, 508, 1020, 2044, 4092, 8188,
16378, 32761, 32762, 65528, 65529, 131062, 131063, 262133, 262134, 262129,
1048568, 524273, 524274, 524275, 1048569, 524279, 524276, 1048570, 1048571, 1048572,
1048573, 1048574, 1048575,
},
}
SbrTables.T_BAL30 = {
12,
{ 13, 13, 13, 13, 13, 13, 13, 12, 8, 7, 4, 3, 1, 2, 5, 6, 9, 13, 13, 13, 13, 13, 13, 14, 14 },
{
8178, 8179, 8180, 8181, 8182, 8183, 8184, 4088, 254, 126,
14, 6, 0, 2, 30, 62, 510, 8185, 8186, 8187,
8188, 8189, 8190, 16382, 16383,
},
}
SbrTables.F_BAL30 = {
12,
{ 13, 13, 13, 13, 13, 14, 14, 11, 8, 7, 4, 2, 1, 3, 5, 6, 9, 12, 13, 14, 14, 14, 14, 14, 14 },
{
8183, 8184, 8185, 8186, 8187, 16376, 16377, 2044, 254, 126,
14, 2, 0, 6, 30, 62, 510, 4090, 8182, 16378,
16379, 16380, 16381, 16382, 16383,
},
}
SbrTables.T_NOISE30 = {
31,
{ 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 11, 8, 6, 4, 3, 1, 2, 5, 8, 10, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14 },
{
8142, 8143, 8144, 8145, 8146, 8147, 8148, 8149, 8150, 8151,
8152, 8153, 8154, 8155, 8156, 8157, 8158, 8159, 8160, 8161,
8162, 8163, 8164, 8165, 8166, 8167, 2034, 253, 62, 14,
6, 0, 2, 30, 252, 1016, 8140, 8168, 8169, 8170,
8171, 8172, 8141, 8173, 8174, 8175, 8176, 8177, 8178, 8179,
8180, 8181, 8182, 8183, 8184, 8185, 8186, 8187, 8188, 8189,
8190, 16382, 16383,
},
}
SbrTables.T_NOISEBAL30 = {
12,
{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 2, 1, 3, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
{
236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
28, 2, 0, 6, 58, 246, 247, 248, 249, 250,
251, 252, 253, 254, 255,
},
}
SbrTables.IID_DF = {
14,
{ 17, 17, 17, 17, 16, 15, 13, 10, 9, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 6, 8, 11, 13, 14, 14, 15, 17, 18, 18 },
{
131067, 131068, 131069, 131066, 65532, 32764, 8189, 1022, 510, 126,
60, 29, 13, 5, 0, 4, 12, 28, 61, 62,
254, 2046, 8188, 16380, 16381, 32765, 131070, 262142, 262143,
},
}
SbrTables.IID_DT = {
14,
{ 19, 19, 19, 20, 20, 20, 17, 15, 12, 10, 8, 6, 4, 2, 1, 3, 5, 7, 9, 11, 13, 14, 17, 19, 20, 20, 20, 20, 20 },
{
524281, 524282, 524283, 1048568, 1048569, 1048570, 131069, 32766, 4094, 1022,
254, 62, 14, 2, 0, 6, 30, 126, 510, 2046,
8190, 16382, 131068, 524280, 1048571, 1048572, 1048573, 1048574, 1048575,
},
}
SbrTables.IID_FINE_DF = {
30,
{ 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 16, 16, 15, 14, 14, 13, 12, 12, 11, 10, 10, 8, 7, 6, 5, 4, 3, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 16, 17, 17, 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18 },
{
130740, 130741, 130422, 130423, 130420, 130421, 130698, 130699, 130696, 65152,
130742, 65154, 65208, 32578, 32686, 16303, 8145, 8169, 4073, 2026,
2043, 1019, 507, 511, 124, 60, 28, 12, 0, 1,
1, 2, 1, 13, 29, 61, 125, 252, 508, 1020,
1012, 2027, 4074, 8170, 8150, 16336, 32687, 32579, 65209, 65155,
130743, 65153, 130697, 130702, 130703, 130700, 130701, 130738, 130739, 130736,
130737,
},
}
SbrTables.IID_FINE_DT = {
30,
{ 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 10, 9, 9, 7, 6, 5, 3, 1, 2, 5, 6, 7, 8, 9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
{
20180, 20181, 20174, 20175, 20172, 20182, 20184, 20294, 20320, 10008,
10009, 10084, 10085, 10093, 10161, 5047, 5078, 2503, 2537, 2541,
1262, 1271, 632, 313, 154, 159, 32, 17, 10, 3,
1, 0, 11, 18, 33, 76, 155, 314, 633, 624,
1263, 1250, 2538, 2520, 5079, 5072, 10162, 10146, 10010, 10011,
20326, 20327, 20321, 20295, 20185, 20183, 20173, 20178, 20179, 20176,
20177,
},
}
SbrTables.ICC_DF = {
7,
{ 14, 14, 12, 10, 7, 5, 3, 1, 2, 4, 6, 8, 9, 11, 13 },
{
16383, 16382, 4094, 1022, 126, 30, 6, 0, 2, 14,
62, 254, 510, 2046, 8190,
},
}
SbrTables.ICC_DT = {
7,
{ 14, 13, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12, 14 },
{
16382, 8190, 2046, 510, 126, 30, 6, 0, 2, 14,
62, 254, 1022, 4094, 16383,
},
}
return SbrTables
+101 -13
View File
@@ -1,11 +1,13 @@
--!strict
--!optimize 2
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block.
-- Written from the standard; no third-party code. HE-AAC decodes its AAC-LC core at the core rate. Not supported: Main
-- prediction, LTP, SSR, coupling channels, 960-sample frames, ER / USAC (UNSUPPORTED).
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block;
-- HE-AAC (SBR, Aac/Sbr) and HE-AACv2 (baseline PS, Aac/Ps) at the doubled rate, signalled explicitly or found in the
-- stream (Decoder:probe). Written from the standard. Not supported: Main prediction, LTP, SSR, coupling channels,
-- 960-sample frames, down-sampled SBR, ER / USAC (UNSUPPORTED).
local Err = require("@rosetta/core/Err")
local Bits = require("@rosetta/core/Bits")
local Decode = require("@rosetta/codecs/audio/Aac/Decode")
local Sbr = require("@rosetta/codecs/audio/Aac/Sbr")
local Aac = {}
@@ -218,17 +220,25 @@ function Aac.adts(b: buffer, o: number, limit: number): Adts?
}
end
export type DecoderOptions = {
sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC at the core rate (faster); default true
}
export type Decoder = {
config: Config,
channels: number,
sampleRate: number,
frameLength: number,
channels: number, -- output channels (2 for a mono core with parametric stereo)
sampleRate: number, -- output rate (twice the core rate with SBR)
frameLength: number, -- samples per channel per frame: 1024, 2048 with SBR
pcm: { number },
sbr: boolean,
sbr: boolean, -- SBR data seen in the stream
sbrOutput: boolean, -- SBR decoding on (sampleRate is the SBR rate)
ps: boolean, -- parametric stereo output
onExtension: ((kind: number, element: number, buf: buffer, bitPos: number, bits: number) -> ())?,
decode: (self: Decoder, buf: buffer, offset: number?, length: number?) -> (number?, Err.Err?),
probe: (self: Decoder, buf: buffer, offset: number?, length: number?) -> boolean,
reset: (self: Decoder) -> (),
state: Decode.State,
allowSbr: boolean,
}
local function decode(self: Decoder, buf: buffer, offset: number?, length: number?): (number?, Err.Err?)
@@ -246,16 +256,17 @@ local function decode(self: Decoder, buf: buffer, offset: number?, length: numbe
if st.sbr then
self.sbr = true
end
local fl = self.frameLength
if chans == 0 then
for i = 1, 1024 * self.channels do
for i = 1, fl * self.channels do
st.pcm[i] = 0
end
return 1024, nil
return fl, nil
end
if chans ~= self.channels then
return nil, Err.new(Err.CORRUPT, string.format("frame has %d channels, the configuration %d", chans, self.channels), o)
end
return 1024, nil
return fl, nil
end
local function reset(self: Decoder)
@@ -265,9 +276,76 @@ local function reset(self: Decoder)
end
c.prevShape = 0
end
for _, e in self.state.sbrElems do
Sbr.restart(e)
end
end
function Aac.decoder(asc: any): (Decoder?, Err.Err?)
local function enableSbr(self: Decoder, rate: number, ps: boolean)
local st = self.state
st.sbrOn, st.sbrRate = true, rate
st.psOut = ps and self.config.channels == 1
st.sbrElems = {}
self.sbrOutput = true
self.ps = st.psOut
self.sampleRate = rate
self.channels = if st.psOut then 2 else self.config.channels
self.frameLength = 2048
end
-- implicit signalling (14496-3 1.6.5.3): SBR / PS data in this frame switch the output to the doubled rate (and a mono
-- core with PS to stereo); call on the first frames before reading sampleRate / channels. true when it switched.
local function probe(self: Decoder, buf: buffer, offset: number?, length: number?): boolean
local cfg = self.config
if not self.allowSbr or self.ps or (self.sbrOutput and cfg.channels ~= 1) or type(buf) ~= "buffer" then
return false
end
local o = offset or 0
local n = length or (buffer.len(buf) - o)
if o < 0 or n < 0 or o + n > buffer.len(buf) or cfg.sampleRate * 2 > 96000 then
return false
end
local scratch = Decode.state(cfg.sfIndex)
scratch.channels = cfg.channels
scratch.map = self.state.map
scratch.sbrOn = true
scratch.probe = true
scratch.sbrRate = cfg.sampleRate * 2
scratch.psOut = cfg.channels == 1
local chans = Decode.frame(scratch, buf, o, n)
if not chans or not scratch.sbr then
return false
end
local ps = false
for _, e in scratch.sbrElems do
if e.ps and e.ps.haveConfig then
ps = true
end
end
if self.sbrOutput and not ps then
return false
end
enableSbr(self, self.config.sampleRate * 2, ps)
return true
end
-- probe the first frames (offs / sizes: 1-based packet lists) then reset; true when the output format changed
function Aac.probeFrames(d: Decoder, buf: buffer, offs: { number }, sizes: { number }, maxFrames: number?): boolean
local changed = false
for i = 1, math.min(#offs, maxFrames or 8) do
if d:probe(buf, offs[i], sizes[i]) then
changed = true
end
-- SBR data is in every frame of an HE-AAC stream; a PS header may come later
if i >= 2 and not d.sbrOutput or d.ps or (d.sbrOutput and d.config.channels ~= 1) then
break
end
end
return changed
end
function Aac.decoder(asc: any, opts: DecoderOptions?): (Decoder?, Err.Err?)
local allowSbr = not (opts and opts.sbr == false)
local cfg: Config
if type(asc) == "buffer" then
local c, err = Aac.config(asc)
@@ -301,18 +379,28 @@ function Aac.decoder(asc: any): (Decoder?, Err.Err?)
if cfg.channelConfig >= 3 then
st.map = CONFIG_MAP[cfg.channelConfig]
end
return {
local d: Decoder = {
config = cfg,
channels = cfg.channels,
sampleRate = cfg.sampleRate,
frameLength = 1024,
pcm = st.pcm,
sbr = cfg.sbr,
sbrOutput = false,
ps = false,
onExtension = nil,
decode = decode,
probe = probe,
reset = reset,
state = st,
}, nil
allowSbr = allowSbr,
}
-- explicit signalling; an extension rate equal to the core rate is down-sampled SBR (the core is decoded alone)
local ext = cfg.extensionRate or cfg.sampleRate * 2
if allowSbr and cfg.sbr and ext == cfg.sampleRate * 2 and ext <= 96000 then
enableSbr(d, ext, cfg.ps)
end
return d, nil
end
return Aac
+7
View File
@@ -310,6 +310,13 @@ local function adts(track: Types.Track, packets: { Types.Packet }): (buffer?, Er
profile = b0 // 8 -- audio object type
rateIndex = (b0 % 8) * 2 + b1 // 128
chanConfig = (b1 // 8) % 16
-- HE-AAC (5) / HE-AACv2 (29), 14496-3 1.6.2.1: ADTS carries the AAC-LC core (the decoder finds SBR / PS in it)
if (profile == 5 or profile == 29) and rateIndex ~= 15 and buffer.len(c) >= 4 then
local v = buffer.readu8(c, 1) * 65536 + buffer.readu8(c, 2) * 256 + buffer.readu8(c, 3)
local extIndex = (v // 2 ^ 15) % 16 -- after aot (5), sfi (4), channels (4): bits 13 .. 16
local core = if extIndex == 15 then -1 else (v // 2 ^ 10) % 32 -- bits 17 .. 21
profile = if core == 2 then 2 else -1
end
else
local rates = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 }
profile = 2
+7 -3
View File
@@ -304,12 +304,12 @@ end
-- api hooks (a format never requires another format): opts.decodeImage / encodeImage, opts.plugins, opts.decodeModule,
-- opts.openAudio (containers/AudioWrap: PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, MP3 / FLAC / AC-3 as they are)
local function openAudio(demuxer: any, track: any): (any, Err.Err?)
local function openAudio(demuxer: any, track: any, sbr: boolean?): (any, Err.Err?)
local buf, fmt, err = AudioWrap.wrap(demuxer, track)
if not buf then
return nil, err
end
local dec, oerr = F.open(buf, { format = fmt, category = "audio" })
local dec, oerr = F.open(buf, { format = fmt, category = "audio", sbr = sbr })
-- Matroska CodecDelay: ffmpeg drops it for MPEG audio / AAC; Opus has its own pre-skip and Vorbis's delay is
-- already the decoder's own (its first packet returns nothing)
-- MP4 edit lists add the play length (the encoder's end padding is cut, as ffmpeg does)
@@ -332,7 +332,11 @@ local function withHooks(o: Options): Options
c.plugins = F.plugins
end
if c.openAudio == nil then
c.openAudio = openAudio
-- opts.sbr reaches the AAC decoder of a video's audio track
local sbr = o.sbr
c.openAudio = function(demuxer: any, track: any): (any, Err.Err?)
return openAudio(demuxer, track, sbr)
end
end
if c.decodeImage == nil then
c.decodeImage = function(b: buffer): (any, Err.Err?)
+3 -1
View File
@@ -50,7 +50,9 @@ end
for _, c in He do
if string.sub(c.name, -4) == ".aac" then
local a, err = AacF.decode(reader(c.data))
t:check(a and a.sampleRate == c.rate // 2 and a.frames > 0, c.name .. ": core decode: " .. tostring(err))
t:check(a and a.sampleRate == c.rate and a.frames > 0, c.name .. ": SBR found in ADTS: " .. tostring(err))
local core, cerr = AacF.decode(reader(c.data), { sbr = false })
t:check(core and core.sampleRate == c.rate // 2 and core.frames * 2 == a.frames, c.name .. ": sbr = false: " .. tostring(cerr))
end
end
+201
View File
@@ -0,0 +1,201 @@
-- HE-AAC tests: SBR (mono, stereo, 5.1) and SBR + PS from fdkaac in ADTS, M4A (explicit and implicit signalling),
-- MPEG-TS, MP4 and Matroska against ffmpeg's decode (SNR, output rate, length, energy above the core's Nyquist),
-- opts.sbr = false, backward-compatible explicit signalling, seeking, and the fuzz pass. Fixtures: tools/fixtures/heaac.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Bytes = require("@rosetta/core/Bytes")
local Aac = require("@rosetta/codecs/audio/Aac")
local AacF = require("@rosetta/audio/aac")
local M4a = require("@rosetta/audio/m4a")
local Fx = require("@tests/gen/heaac")
local t = T.suite("heaac")
local byName: { [string]: any } = {}
for _, c in Fx do
byName[c.name] = c
end
local function reader(s: string): Bytes.Reader
return Bytes.reader(buffer.fromstring(s))
end
-- every frame of a Decoder as one f32 buffer
local function drain(d: any): (buffer, number)
local parts, total = {}, 0
while true do
local b, n = d:read(4096)
if not n or n == 0 then
break
end
table.insert(parts, buffer.readstring(b, 0, n * d.channels * 4))
total += n
end
return buffer.fromstring(table.concat(parts)), total
end
local function open(c: any, opts: any?): any
if c.container == "adts" then
return AacF.open(reader(c.data), opts)
elseif c.container == "m4a" then
return M4a.open(reader(c.data), opts)
end
local o = opts or {}
local v, err = F.video.open(buffer.fromstring(c.data), { sbr = o.sbr })
if not v then
return nil, err
end
return v.audio, v.audioError
end
-- a 63-tap windowed-sinc high-pass at a quarter of the output rate: the band SBR fills
local HP = table.create(63, 0)
for i = 0, 62 do
local x = i - 31
local lp = if x == 0 then 0.5 else math.sin(math.pi * 0.5 * x) / (math.pi * x)
HP[i + 1] = ((if x == 0 then 1 else 0) - lp) * (0.54 - 0.46 * math.cos(2 * math.pi * i / 62))
end
-- SNR (dB) of ours (f32) against ref (s16le) over n frames; the high band's energy ours / ref (dB) and its share of
-- the reference's energy
local function compare(samples: buffer, ref: string, ch: number, n: number): (number, number, number)
local rb = buffer.fromstring(ref)
local m = math.min(n, buffer.len(rb) // (2 * ch), buffer.len(samples) // (4 * ch))
local sig, err = 0, 0
local r, x = table.create(m, 0), table.create(m, 0)
local hiOurs, hiRef = 0, 0
for c = 0, ch - 1 do
for i = 0, m - 1 do
local k = i * ch + c
r[i + 1] = buffer.readi16(rb, k * 2) / 32768
x[i + 1] = buffer.readf32(samples, k * 4)
sig += r[i + 1] ^ 2
err += (x[i + 1] - r[i + 1]) ^ 2
end
for i = 64, m - 64, 3 do
local hr, hx = 0, 0
for j = 1, 63 do
hr += r[i + j - 31] * HP[j]
hx += x[i + j - 31] * HP[j]
end
hiRef += hr * hr
hiOurs += hx * hx
end
end
local snr = if err == 0 then math.huge else 10 * math.log10(sig / err)
return snr, 10 * math.log10((hiOurs + 1e-30) / (hiRef + 1e-30)), 3 * hiRef / (sig + 1e-30)
end
-- 1. every fixture against ffmpeg
local decoded: { [string]: buffer } = {}
for _, c in Fx do
local d, err = open(c)
t:check(d, c.name .. ": " .. tostring(err))
t:eq(d.sampleRate, c.rate, c.name .. ": output rate")
t:eq(d.channels, c.channels, c.name .. ": channels")
local samples, n = drain(d)
decoded[c.name] = samples
if c.container == "mkv" then
-- (Matroska's end trimming is the container's; the start is checked through the SNR)
t:check(n >= c.frames, c.name .. ": frames " .. n)
else
t:eq(n, c.frames, c.name .. ": frames")
t:eq(d.frames, c.frames, c.name .. ": the stream info's frames")
end
local ref = c.ref or (byName[c.same] :: any).ref
local snr, hiDb, hiShare = compare(samples, ref, c.channels, math.min(n, c.frames))
t:check(snr > 60, string.format("%s: SNR %.1f dB", c.name, snr))
t:check(math.abs(hiDb) < 0.1 and hiShare > 1e-3, string.format("%s: high band %.3f dB vs ffmpeg (%.1f dB of the signal)", c.name, hiDb, 10 * math.log10(hiShare)))
t:note("%s (%s, %s%s): %d ch %d Hz, %d frames, SNR %.1f dB vs ffmpeg", c.name, c.container, c.signalling,
if c.ps then ", PS" else "", d.channels, d.sampleRate, n, snr)
end
-- 2. opts.sbr = false: the AAC-LC core at the core rate
for _, name in { "v1_stereo_48k.aac", "v1_stereo_48k.m4a", "v2_48k.aac", "v1_stereo_48k.ts" } do
local c = byName[name]
local d, err = open(c, { sbr = false })
t:check(d, name .. ": sbr = false: " .. tostring(err))
t:eq(d.sampleRate, c.core, name .. ": sbr = false: core rate")
t:eq(d.channels, if c.ps then 1 else c.channels, name .. ": sbr = false: core channels")
local _, n = drain(d)
t:check(n > 0 and math.abs(n * 2 - c.frames) <= 4096, name .. ": sbr = false: frames " .. n)
end
-- 3. backward-compatible explicit signalling (14496-3 1.6.5.2: AAC-LC config + sync extension 0x2b7, SBR, ext rate)
do
local c = byName["v1_stereo_48k.aac"]
local data = buffer.fromstring(c.data)
local h = Aac.adts(data, 0, buffer.len(data)) :: Aac.Adts
-- aot 2 (5) sfi (4) ch (4) GASpecific 000 (3) | 0x2b7 (11) aot 5 (5) sbr 1 (1) extSfi (4) = 37 bits
local bitsOut = { { 2, 5 }, { h.sfIndex, 4 }, { 2, 4 }, { 0, 3 }, { 0x2b7, 11 }, { 5, 5 }, { 1, 1 }, { h.sfIndex - 3, 4 } }
local asc = buffer.create(5)
local pos = 0
for _, f in bitsOut do
for b = f[2] - 1, 0, -1 do
if bit32.band(bit32.rshift(f[1], b), 1) == 1 then
local byte = pos // 8
buffer.writeu8(asc, byte, bit32.bor(buffer.readu8(asc, byte), bit32.lshift(1, 7 - pos % 8)))
end
pos += 1
end
end
local cfg = Aac.config(asc) :: Aac.Config
t:check(cfg and cfg.sbr and cfg.extensionRate == c.rate, "backward-compatible signalling parsed")
local d = Aac.decoder(asc) :: Aac.Decoder
t:check(d.sampleRate == c.rate and d.frameLength == 2048 and d.sbrOutput, "explicit SBR without probing")
local whole = decoded["v1_stereo_48k.aac"]
local p, frame, maxd = 0, 0, 0
while true do
local fh = Aac.adts(data, p, buffer.len(data))
if not fh then
break
end
local n = d:decode(data, p + fh.headerSize, fh.frameSize - fh.headerSize) :: number
for i = 1, n * 2 do
maxd = math.max(maxd, math.abs(d.pcm[i] - buffer.readf32(whole, (frame * 4096 + i - 1) * 4)))
end
frame += 1
p += fh.frameSize
end
t:check(maxd < 1e-6, "explicit decoder = the ADTS path (max diff " .. maxd .. ")")
local lc = Aac.decoder(asc, { sbr = false }) :: Aac.Decoder
t:check(lc.sampleRate == c.core and not lc.sbrOutput, "sbr = false with explicit signalling")
end
-- 4. seeking: the position is exact; the SBR noise phase restarts, so compare energies after the pre-roll
for _, name in { "v1_stereo_48k.m4a", "v2_48k.aac" } do
local c = byName[name]
local whole = decoded[name]
local d = open(c)
for _, at in { 0.5, 0.123 } do
d:seek(at)
local b, n = d:read(2048)
local f0 = math.floor(at * c.rate + 0.5)
local e1, e2, diff = 0, 0, 0
for i = 0, n * c.channels - 1 do
local x, y = buffer.readf32(b, i * 4), buffer.readf32(whole, (f0 * c.channels + i) * 4)
e1 += x * x
e2 += y * y
diff += (x - y) ^ 2
end
local snr = 10 * math.log10(e2 / math.max(diff, 1e-30))
t:check(n == 2048 and snr > 15, string.format("%s: seek %.3f s (SNR %.1f dB vs a straight decode)", name, at, snr))
end
end
-- 5. the fuzz pass
do
local samples = {}
for _, name in { "v1_stereo_48k.aac", "v2_48k.aac", "v1_mono_48k.aac", "v1_51_48k.aac" } do
table.insert(samples, string.sub(byName[name].data, 1, 3000))
end
t:fuzz("adts", function(b: buffer)
return AacF.decode(Bytes.reader(b))
end, samples, { mutations = 40 })
local m4as = { string.sub(byName["v2_48k.m4a"].data, 1, 6000), byName["v1_stereo_48k_implicit.m4a"].data }
t:fuzz("m4a", function(b: buffer)
return M4a.decode(Bytes.reader(b))
end, m4as, { mutations = 20, keepHeader = 2000 })
end
t:done()
+11 -9
View File
@@ -79,20 +79,22 @@ for _, c in Aac do
t:note("%s: %d ch %d Hz, %d frames, SNR %.1f dB vs ffmpeg", c.name, c.channels, c.rate, a.frames, s)
end
-- HE-AAC: the AAC-LC core at half the rate, SBR seen
-- HE-AAC: SBR output at the full rate (tests/audio/heaac.luau checks it against ffmpeg); opts.sbr = false: the core
for _, c in He do
if string.sub(c.name, -4) ~= ".m4a" then
continue
end
local dec, err = M4a.open(reader(c.data))
t:check(dec, c.name .. ": " .. tostring(err))
t:eq(dec.sampleRate, c.rate // 2, c.name .. ": core rate")
local b, n = dec:read(8192)
local e = 0
for i = 0, n * dec.channels - 1 do
e += buffer.readf32(b, i * 4) ^ 2
for _, sbr in { true, false } do
local dec, err = M4a.open(reader(c.data), { sbr = sbr })
t:check(dec, c.name .. ": " .. tostring(err))
t:eq(dec.sampleRate, if sbr then c.rate else c.rate // 2, c.name .. (if sbr then ": SBR rate" else ": core rate"))
local b, n = dec:read(8192)
local e = 0
for i = 0, n * dec.channels - 1 do
e += buffer.readf32(b, i * 4) ^ 2
end
t:check(n == 8192 and e > 1, c.name .. ": decodes (energy " .. e .. ")")
end
t:check(n == 8192 and e > 1, c.name .. ": the core decodes (energy " .. e .. ")")
end
do
+8
View File
@@ -101,6 +101,14 @@ end, 3)
decodeBench("aac fdk m4a stereo", M4a, function()
return case(require("@tests/gen/m4a_aac"), "fdk_stereo.m4a").data
end, 3)
-- HE-AAC: the 1 s fdkaac fixtures, ADTS frames repeated 4x (4.4 s)
local AacF = require("@rosetta/audio/aac")
for _, name in { "v1_stereo_48k", "v2_48k" } do
local label = if name == "v2_48k" then "he-aac v2 adts 48k" else "he-aac v1 adts stereo 48k"
decodeBench(label, AacF, function()
return string.rep(case(require("@tests/gen/heaac"), name .. ".aac").data, 4)
end, 5)
end
decodeBench("alac m4a stereo 16", M4a, function()
return require("@tests/gen/m4a_alac")[1].data
end, 3)
+29
View File
@@ -0,0 +1,29 @@
# Docs input for the HE-AAC section of docs/guide/audio.md (tools/fixtures.py; tools/docs_check.py reads it): half a
# second of HE-AACv2 (SBR + PS) in ADTS from fdkaac, 48 kHz output from a 24 kHz mono core.
# tests/gen/docs_heaac.luau { files = { ["radio.aac"] = bytes }, vars = { heAacBytes = "radio.aac" } }
import importlib.util, math, os, struct, tempfile
INPUTS = ["tools/docs_check.py"]
def generate(ctx):
spec = importlib.util.spec_from_file_location("docs_heaac_check", os.path.join(ctx.root, "tools", "docs_check.py"))
dc = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dc)
rate, n = 48000, 24000
ints = []
for i in range(n):
t = i / rate
for c in range(2):
v = 0.3 * math.sin(2 * math.pi * (330 + 110 * c) * t) + 0.1 * math.sin(2 * math.pi * 7000 * t + c)
ints.append(int(v * 32767))
data = struct.pack("<%dh" % len(ints), *ints)
fmt = struct.pack("<HHIIHH", 1, 2, rate, rate * 4, 4, 16)
wav = b"RIFF" + struct.pack("<I", 36 + len(data)) + b"WAVEfmt " + struct.pack("<I", 16) + fmt + b"data" + struct.pack("<I", len(data)) + data
with tempfile.TemporaryDirectory() as d:
with open(os.path.join(d, "in.wav"), "wb") as f:
f.write(wav)
ctx.nix(["fdk-aac-encoder"], ["sh", "-c", "cd '%s' && fdkaac -S -p 29 -b 32000 -f 2 -o radio.aac in.wav" % d])
with open(os.path.join(d, "radio.aac"), "rb") as f:
aac = f.read()
dc.emit_inputs(ctx, {"radio.aac": aac}, {"heAacBytes": "radio.aac"})
+89
View File
@@ -0,0 +1,89 @@
# HE-AAC (SBR) / HE-AACv2 (SBR + PS) fixtures (tools/fixtures.py). fdkaac encodes; ffmpeg's native decoder is the
# reference; ffmpeg also remuxes into M4A (implicit signalling), MPEG-TS, MP4 and Matroska (with a tiny MPEG-1 video).
# tests/gen/heaac.luau { name, data, container ("adts" | "m4a" | "ts" | "mp4" | "mkv"), signalling ("explicit" |
# "implicit"), ps, rate (the output rate), core (the core rate), channels (our output: a mono
# core without PS stays mono; ffmpeg's duplicate channel is dropped from ref), frames (ffmpeg's),
# ref = ffmpeg's decode as s16le, or same = the name of an earlier case ffmpeg decodes identically }
import math, os, random, struct, tempfile
PKGS = ["ffmpeg", "fdk-aac-encoder"]
INPUTS = []
def music(frames, channels, rate, seed):
rng = random.Random(seed)
out = []
for i in range(frames):
t = i / rate
for c in range(channels):
v = 0.2 * math.sin(2 * math.pi * (220 * (c + 1)) * t + 3 * math.sin(2 * math.pi * 5 * t))
v += 0.1 * math.sin(2 * math.pi * 660 * t) + 0.06 * math.sin(2 * math.pi * 1320.5 * t + c)
v += 0.04 * math.sin(2 * math.pi * (5000 + 300 * c) * t) + 0.03 * math.sin(2 * math.pi * (9000 + 700 * c) * t)
v += 0.08 * (rng.random() * 2 - 1)
if (i // (rate // 8)) % 3 == 1:
v += 0.12 * (rng.random() * 2 - 1)
if i % (rate // 3) < 40:
v += 0.5 * (1 - (i % (rate // 3)) / 40) * (1 if (i // 7) % 2 else -1)
out.append(max(-32768, min(32767, int(round(v * 20000)))))
return out
def wav(ints, ch, rate):
data = struct.pack("<%dh" % len(ints), *ints)
fmt = struct.pack("<HHIIHH", 1, ch, rate, rate * ch * 2, ch * 2, 16)
return b"RIFF" + struct.pack("<I", 36 + len(data)) + b"WAVEfmt " + struct.pack("<I", 16) + fmt + b"data" + struct.pack("<I", len(data)) + data
def generate(ctx):
cases = []
with tempfile.TemporaryDirectory() as d:
def sh(cmd):
return ctx.nix(PKGS, ["sh", "-c", "cd '%s' && %s" % (d, cmd)])
def read(name):
with open(os.path.join(d, name), "rb") as f:
return f.read()
def add(name, container, signalling, ps, rate, ch, outch):
raw = sh("ffmpeg -v error -i %s -vn -f s16le -" % name)
ffch = 2 if ch == 1 else ch # ffmpeg outputs a mono SBR stream as stereo (implicit PS)
frames = len(raw) // (2 * ffch)
if outch != ffch:
s = struct.unpack("<%dh" % (frames * ffch), raw[: frames * ffch * 2])
raw = struct.pack("<%dh" % frames, *s[0::ffch])
case = {"name": name, "data": read(name), "container": container, "signalling": signalling, "ps": ps,
"rate": rate, "core": rate // 2, "channels": outch, "frames": frames}
for c in cases:
if c.get("ref") == raw:
case["same"] = c["name"] # the same decode as an earlier case: its ref is not repeated
break
else:
case["ref"] = raw
cases.append(case)
video = "-f lavfi -i testsrc=s=32x32:d=1:r=24"
for base, prof, br, ch, rate, extra in (
("v1_stereo_48k", 5, 64000, 2, 48000, ("m4a_implicit", "ts", "mp4", "mkv")),
("v1_mono_48k", 5, 32000, 1, 48000, ()),
("v2_48k", 29, 32000, 2, 48000, ("ts",)),
("v1_stereo_44k", 5, 48000, 2, 44100, ()),
("v1_stereo_32k", 5, 32000, 2, 32000, ()),
("v1_51_48k", 5, 160000, 6, 48000, ()),
):
with open(os.path.join(d, base + ".wav"), "wb") as f:
f.write(wav(music(rate, ch, rate, 7 + ch), ch, rate))
sh("fdkaac -S -p %d -b %d -f 2 -o %s.aac %s.wav" % (prof, br, base, base))
ps = prof == 29
outch = 2 if ps else ch
add(base + ".aac", "adts", "implicit", ps, rate, ch, outch)
if ch <= 2:
sh("fdkaac -S -p %d -b %d -o %s.m4a %s.wav" % (prof, br, base, base))
add(base + ".m4a", "m4a", "explicit", ps, rate, ch, outch)
for x in extra:
if x == "m4a_implicit":
sh("ffmpeg -v error -i %s.aac -c copy %s_implicit.m4a" % (base, base))
add(base + "_implicit.m4a", "m4a", "implicit", ps, rate, ch, outch)
else:
sh("ffmpeg -v error %s -i %s.m4a -map 0:v -map 1:a -c:v mpeg1video -b:v 50k -c:a copy %s.%s" % (video, base, base, x))
add(base + "." + x, x, "explicit", ps, rate, ch, outch)
ctx.emit("heaac", cases)