ws-audio: MP3 layer III encoder (Encode3: shine-style CBR, MPEG-1/2/2.5, mono/stereo/joint MS, global-gain rate loop, region/table choice, count1 A/B) + MP2 layer II encoder (Encode2: standard alloc tables, greedy noise allocation, grouping), shared polyphase Analysis, EncTables (PDMP3 public-domain window + Huffman trees -> codeword tables); mp3 init encode() with ID3v2.4; fixtures decode with ffmpeg + lame (no errors, SNR floors); bench lines

This commit is contained in:
4DBug committed 2026-10-31 09:18:23 -05:00
1 parent 3f2be59a65
commit 929b5d6d53
14 files changed
+1859 -12

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| audio | aiff | .aiff .aif .aifc .afc | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, lossy: false | `tests/audio/aiff.luau` |
| audio | au | .au .snd | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, lossy: false | `tests/audio/au.luau` |
| audio | flac | .flac .oga | yes | yes | streaming: true, depths: 4, 8, 12, 16, 20, 24, 32, ogg: true, seek: true | `tests/audio/flac.luau` |
| audio | mp3 | .mp3 .mp2 .mp1 .mpga | yes | - | streaming: true, seek: true, layers: 1, 2, 3, channels: 2 | `tests/audio/mp3.luau` |
| audio | mp3 | .mp3 .mp2 .mp1 .mpga | yes | yes | streaming: true, seek: true, layers: 1, 2, 3, channels: 2 | `tests/audio/mp3.luau` |
| audio | opus | .opus | yes | - | streaming: true, seek: true, lossy: true, channels: 255, rates: 48000 | `tests/audio/opus.luau` |
| audio | voc | .voc | yes | yes | streaming: true, depths: 8, 16, lossy: false | `tests/audio/voc.luau` |
| audio | vorbis | .ogg .oga | yes | - | streaming: true, seek: true, lossy: true, channels: 255 | `tests/audio/vorbis.luau` |
+1
View File
@@ -14,6 +14,7 @@
| `src/Formats/image/bmp/` | bmp-luau b86aed6 (same author), written from the Microsoft BMP documentation | no licence chosen yet; no third-party code |
| `tests/fixtures/bmpsuite/` | BMP Suite 2.8 by Jason Summers (generated images) | public domain: [bmpsuite-NOTICE.txt](bmpsuite-NOTICE.txt) |
| `src/Formats/containers/Zip/` (the reader), `src/Formats/archive/zip/` | zip-luau 121d4b0 (same author), written from PKWARE's APPNOTE.TXT 6.3 | no licence chosen yet; no third-party code |
| `src/Formats/audio/mp3/EncTables.luau` (ws-audio encoders) | PDMP3 by Krister Lagerström et al. (https://github.com/technosaurus/PDMP3): the ISO/IEC 11172-3 synthesis window and layer III Huffman trees | public domain (unlicense): [PDMP3-UNLICENSE.txt](PDMP3-UNLICENSE.txt) |
| `tests/fixtures/pngsuite/` | PngSuite by Willem van Schaik | [PngSuite-LICENSE.txt](PngSuite-LICENSE.txt) |
Ports of third-party code (minimp3, stb_vorbis, libopus, ...) add a row here and their upstream licence text in this
+27
View File
@@ -0,0 +1,27 @@
PDMP3 (https://github.com/technosaurus/PDMP3), public domain decoder by Krister Lagerstrom et al.
Tables used in src/Formats/audio/mp3/EncTables.luau.
Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS 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.
Original version written by Krister Lagerström([email protected])
Website: https://sites.google.com/a/kmlager.com/www/projects
Contributors:
technosaurus
Erik Hofman (added a subset of the libmpg123 compatible streaming API)
+2 -2
View File
@@ -80,8 +80,8 @@ local list: { Entry } = {
path = "audio/mp3",
extensions = { "mp3", "mp2", "mp1", "mpga" },
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = false, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
load = function()
return require("@formats/audio/mp3")
end,
+63
View File
@@ -0,0 +1,63 @@
--!strict
--!native
--!optimize 2
-- audio/mp3/Analysis: the MPEG audio polyphase analysis filterbank (ISO/IEC 11172-3 section C.1.3, figure C.4): 32
-- input samples -> one sample in each of the 32 subbands, with the standard's 512-tap window C = D / 32 (D from
-- audio/mp3/EncTables) and the matrixing M[k][i] = cos((2k + 1)(i - 16) pi / 64). Shared by the layer II and layer III
-- encoders. New code from the standard's flow chart. Never throws.
--
-- local st = Analysis.new()
-- Analysis.run(st, samples, start, out, outStart) -- samples[start .. start + 31] -> out[outStart .. outStart + 31]
local E = require("@formats/audio/mp3/EncTables")
local Analysis = {}
local C = table.create(512, 0)
for i = 1, 512 do
C[i] = E.D[i] / 32
end
local M = table.create(32 * 64, 0) -- M[k * 64 + i + 1]
for k = 0, 31 do
for i = 0, 63 do
M[k * 64 + i + 1] = math.cos((2 * k + 1) * (i - 16) * math.pi / 64)
end
end
export type State = { x: { number }, off: number, y: { number } }
function Analysis.new(): State
return { x = table.create(1024, 0), off = 0, y = table.create(64, 0) }
end
-- run(st, s, si, out, oi): s[si .. si + 31] (oldest first) -> out[oi .. oi + 31] (subbands 0 .. 31)
function Analysis.run(st: State, s: { number }, si: number, out: { number }, oi: number)
-- the window X is kept twice (x[p] and x[p + 512]) so X[i] = x[off + i + 1] without a modulo
local x = st.x
local off = (st.off - 32) % 512
st.off = off
for j = 0, 31 do
local v = s[si + j]
local p = (off + 31 - j) % 512 + 1 -- X[31 - j]: the first new sample goes to X[31], the newest to X[0]
x[p] = v
x[p + 512] = v
end
local y = st.y
for i = 0, 63 do
local acc = 0
local b = off + i + 1
for j = 0, 7 do
acc += C[i + 64 * j + 1] * x[b + 64 * j]
end
y[i + 1] = acc
end
for k = 0, 31 do
local acc = 0
local mb = k * 64
for i = 1, 64 do
acc += M[mb + i] * y[i]
end
out[oi + k] = acc
end
end
return Analysis
+292
View File
@@ -0,0 +1,292 @@
--!strict
-- audio/mp3/EncTables: tables for the MPEG audio encoders, from PDMP3 (public domain / unlicense, by Krister
-- Lagerstrom and contributors: licenses/PDMP3-UNLICENSE.txt), which transcribes ISO/IEC 11172-3: the synthesis window D
-- (the analysis window is C = D / 32) and the layer III Huffman code trees (the encoders derive codeword tables from
-- them). Trees use the standard's decoder layout: an entry with a high byte is a node (high byte: left offset, low
-- byte: right offset), otherwise a leaf (x << 4 | y; count1 tables: v w x y bits).
local E = {}
E.D = {
0.000000000, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000030518,
-0.000030518, -0.000030518, -0.000030518, -0.000045776, -0.000045776, -0.000061035, -0.000061035, -0.000076294,
-0.000076294, -0.000091553, -0.000106812, -0.000106812, -0.000122070, -0.000137329, -0.000152588, -0.000167847,
-0.000198364, -0.000213623, -0.000244141, -0.000259399, -0.000289917, -0.000320435, -0.000366211, -0.000396729,
-0.000442505, -0.000473022, -0.000534058, -0.000579834, -0.000625610, -0.000686646, -0.000747681, -0.000808716,
-0.000885010, -0.000961304, -0.001037598, -0.001113892, -0.001205444, -0.001296997, -0.001388550, -0.001480103,
-0.001586914, -0.001693726, -0.001785278, -0.001907349, -0.002014160, -0.002120972, -0.002243042, -0.002349854,
-0.002456665, -0.002578735, -0.002685547, -0.002792358, -0.002899170, -0.002990723, -0.003082275, -0.003173828,
0.003250122, 0.003326416, 0.003387451, 0.003433228, 0.003463745, 0.003479004, 0.003479004, 0.003463745,
0.003417969, 0.003372192, 0.003280640, 0.003173828, 0.003051758, 0.002883911, 0.002700806, 0.002487183,
0.002227783, 0.001937866, 0.001617432, 0.001266479, 0.000869751, 0.000442505, -0.000030518, -0.000549316,
-0.001098633, -0.001693726, -0.002334595, -0.003005981, -0.003723145, -0.004486084, -0.005294800, -0.006118774,
-0.007003784, -0.007919312, -0.008865356, -0.009841919, -0.010848999, -0.011886597, -0.012939453, -0.014022827,
-0.015121460, -0.016235352, -0.017349243, -0.018463135, -0.019577026, -0.020690918, -0.021789551, -0.022857666,
-0.023910522, -0.024932861, -0.025909424, -0.026840210, -0.027725220, -0.028533936, -0.029281616, -0.029937744,
-0.030532837, -0.031005859, -0.031387329, -0.031661987, -0.031814575, -0.031845093, -0.031738281, -0.031478882,
0.031082153, 0.030517578, 0.029785156, 0.028884888, 0.027801514, 0.026535034, 0.025085449, 0.023422241,
0.021575928, 0.019531250, 0.017257690, 0.014801025, 0.012115479, 0.009231567, 0.006134033, 0.002822876,
-0.000686646, -0.004394531, -0.008316040, -0.012420654, -0.016708374, -0.021179199, -0.025817871, -0.030609131,
-0.035552979, -0.040634155, -0.045837402, -0.051132202, -0.056533813, -0.061996460, -0.067520142, -0.073059082,
-0.078628540, -0.084182739, -0.089706421, -0.095169067, -0.100540161, -0.105819702, -0.110946655, -0.115921021,
-0.120697021, -0.125259399, -0.129562378, -0.133590698, -0.137298584, -0.140670776, -0.143676758, -0.146255493,
-0.148422241, -0.150115967, -0.151306152, -0.151962280, -0.152069092, -0.151596069, -0.150497437, -0.148773193,
-0.146362305, -0.143264771, -0.139450073, -0.134887695, -0.129577637, -0.123474121, -0.116577148, -0.108856201,
0.100311279, 0.090927124, 0.080688477, 0.069595337, 0.057617188, 0.044784546, 0.031082153, 0.016510010,
0.001068115, -0.015228271, -0.032379150, -0.050354004, -0.069168091, -0.088775635, -0.109161377, -0.130310059,
-0.152206421, -0.174789429, -0.198059082, -0.221984863, -0.246505737, -0.271591187, -0.297210693, -0.323318481,
-0.349868774, -0.376800537, -0.404083252, -0.431655884, -0.459472656, -0.487472534, -0.515609741, -0.543823242,
-0.572036743, -0.600219727, -0.628295898, -0.656219482, -0.683914185, -0.711318970, -0.738372803, -0.765029907,
-0.791213989, -0.816864014, -0.841949463, -0.866363525, -0.890090942, -0.913055420, -0.935195923, -0.956481934,
-0.976852417, -0.996246338, -1.014617920, -1.031936646, -1.048156738, -1.063217163, -1.077117920, -1.089782715,
-1.101211548, -1.111373901, -1.120223999, -1.127746582, -1.133926392, -1.138763428, -1.142211914, -1.144287109,
1.144989014, 1.144287109, 1.142211914, 1.138763428, 1.133926392, 1.127746582, 1.120223999, 1.111373901,
1.101211548, 1.089782715, 1.077117920, 1.063217163, 1.048156738, 1.031936646, 1.014617920, 0.996246338,
0.976852417, 0.956481934, 0.935195923, 0.913055420, 0.890090942, 0.866363525, 0.841949463, 0.816864014,
0.791213989, 0.765029907, 0.738372803, 0.711318970, 0.683914185, 0.656219482, 0.628295898, 0.600219727,
0.572036743, 0.543823242, 0.515609741, 0.487472534, 0.459472656, 0.431655884, 0.404083252, 0.376800537,
0.349868774, 0.323318481, 0.297210693, 0.271591187, 0.246505737, 0.221984863, 0.198059082, 0.174789429,
0.152206421, 0.130310059, 0.109161377, 0.088775635, 0.069168091, 0.050354004, 0.032379150, 0.015228271,
-0.001068115, -0.016510010, -0.031082153, -0.044784546, -0.057617188, -0.069595337, -0.080688477, -0.090927124,
0.100311279, 0.108856201, 0.116577148, 0.123474121, 0.129577637, 0.134887695, 0.139450073, 0.143264771,
0.146362305, 0.148773193, 0.150497437, 0.151596069, 0.152069092, 0.151962280, 0.151306152, 0.150115967,
0.148422241, 0.146255493, 0.143676758, 0.140670776, 0.137298584, 0.133590698, 0.129562378, 0.125259399,
0.120697021, 0.115921021, 0.110946655, 0.105819702, 0.100540161, 0.095169067, 0.089706421, 0.084182739,
0.078628540, 0.073059082, 0.067520142, 0.061996460, 0.056533813, 0.051132202, 0.045837402, 0.040634155,
0.035552979, 0.030609131, 0.025817871, 0.021179199, 0.016708374, 0.012420654, 0.008316040, 0.004394531,
0.000686646, -0.002822876, -0.006134033, -0.009231567, -0.012115479, -0.014801025, -0.017257690, -0.019531250,
-0.021575928, -0.023422241, -0.025085449, -0.026535034, -0.027801514, -0.028884888, -0.029785156, -0.030517578,
0.031082153, 0.031478882, 0.031738281, 0.031845093, 0.031814575, 0.031661987, 0.031387329, 0.031005859,
0.030532837, 0.029937744, 0.029281616, 0.028533936, 0.027725220, 0.026840210, 0.025909424, 0.024932861,
0.023910522, 0.022857666, 0.021789551, 0.020690918, 0.019577026, 0.018463135, 0.017349243, 0.016235352,
0.015121460, 0.014022827, 0.012939453, 0.011886597, 0.010848999, 0.009841919, 0.008865356, 0.007919312,
0.007003784, 0.006118774, 0.005294800, 0.004486084, 0.003723145, 0.003005981, 0.002334595, 0.001693726,
0.001098633, 0.000549316, 0.000030518, -0.000442505, -0.000869751, -0.001266479, -0.001617432, -0.001937866,
-0.002227783, -0.002487183, -0.002700806, -0.002883911, -0.003051758, -0.003173828, -0.003280640, -0.003372192,
-0.003417969, -0.003463745, -0.003479004, -0.003479004, -0.003463745, -0.003433228, -0.003387451, -0.003326416,
0.003250122, 0.003173828, 0.003082275, 0.002990723, 0.002899170, 0.002792358, 0.002685547, 0.002578735,
0.002456665, 0.002349854, 0.002243042, 0.002120972, 0.002014160, 0.001907349, 0.001785278, 0.001693726,
0.001586914, 0.001480103, 0.001388550, 0.001296997, 0.001205444, 0.001113892, 0.001037598, 0.000961304,
0.000885010, 0.000808716, 0.000747681, 0.000686646, 0.000625610, 0.000579834, 0.000534058, 0.000473022,
0.000442505, 0.000396729, 0.000366211, 0.000320435, 0.000289917, 0.000259399, 0.000244141, 0.000213623,
0.000198364, 0.000167847, 0.000152588, 0.000137329, 0.000122070, 0.000106812, 0.000106812, 0.000091553,
0.000076294, 0.000076294, 0.000061035, 0.000061035, 0.000045776, 0.000045776, 0.000030518, 0.000030518,
0.000030518, 0.000030518, 0.000015259, 0.000015259, 0.000015259, 0.000015259, 0.000015259, 0.000015259,
}
E.LINBITS = { [0] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 4, 5, 6, 7, 8, 9, 11, 13, 0, 0 }
-- trees by table number (tables 16..23 share 16's tree, 24..31 share 24's)
E.TREES = {
[1] = {
0x0201, 0x0000, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011,
},
[2] = {
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002,
0x0022,
},
[3] = {
0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0011, 0x0201, 0x0010, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002,
0x0022,
},
[5] = {
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0003, 0x0013, 0x0201, 0x0031, 0x0201, 0x0032, 0x0201, 0x0023, 0x0033,
},
[6] = {
0x0601, 0x0401, 0x0201, 0x0000, 0x0010, 0x0011, 0x0601, 0x0201, 0x0001, 0x0201, 0x0020, 0x0021, 0x0601, 0x0201, 0x0012, 0x0201,
0x0002, 0x0022, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0030, 0x0032, 0x0201, 0x0023, 0x0201, 0x0003, 0x0033,
},
[7] = {
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0021, 0x1201, 0x0601,
0x0201, 0x0012, 0x0201, 0x0022, 0x0030, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0004,
0x0a01, 0x0401, 0x0201, 0x0040, 0x0041, 0x0201, 0x0014, 0x0201, 0x0042, 0x0024, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0033, 0x0043,
0x0050, 0x0401, 0x0201, 0x0034, 0x0005, 0x0051, 0x0601, 0x0201, 0x0015, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0035,
0x0401, 0x0201, 0x0053, 0x0054, 0x0201, 0x0045, 0x0055,
},
[8] = {
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0021, 0x0012, 0x0e01, 0x0401, 0x0201, 0x0020,
0x0002, 0x0201, 0x0022, 0x0401, 0x0201, 0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0032, 0x0023,
0x0201, 0x0040, 0x0004, 0x0201, 0x0041, 0x0201, 0x0014, 0x0042, 0x0c01, 0x0601, 0x0201, 0x0024, 0x0201, 0x0033, 0x0050, 0x0401,
0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0201, 0x0015, 0x0201, 0x0005, 0x0052, 0x0601, 0x0201, 0x0025, 0x0201, 0x0044, 0x0035,
0x0201, 0x0053, 0x0201, 0x0045, 0x0201, 0x0054, 0x0055,
},
[9] = {
0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0a01, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201,
0x0002, 0x0022, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0030, 0x0003, 0x0031, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0c01, 0x0401,
0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0040, 0x0033, 0x0201, 0x0042, 0x0024, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0050,
0x0043, 0x0201, 0x0034, 0x0051, 0x0801, 0x0401, 0x0201, 0x0015, 0x0052, 0x0201, 0x0025, 0x0044, 0x0601, 0x0401, 0x0201, 0x0005,
0x0054, 0x0053, 0x0201, 0x0035, 0x0201, 0x0045, 0x0055,
},
[10] = {
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0a01, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
0x1c01, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0031, 0x0013, 0x0801, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023,
0x0040, 0x0401, 0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0004, 0x0033, 0x0201, 0x0042, 0x0024, 0x1c01, 0x0a01, 0x0601, 0x0401,
0x0201, 0x0050, 0x0005, 0x0060, 0x0201, 0x0061, 0x0016, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0201, 0x0015,
0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0026, 0x0036, 0x0071, 0x1401, 0x0801, 0x0201, 0x0017, 0x0401, 0x0201, 0x0044, 0x0053,
0x0006, 0x0601, 0x0401, 0x0201, 0x0035, 0x0045, 0x0062, 0x0201, 0x0070, 0x0201, 0x0007, 0x0064, 0x0e01, 0x0401, 0x0201, 0x0072,
0x0027, 0x0601, 0x0201, 0x0063, 0x0201, 0x0054, 0x0055, 0x0201, 0x0046, 0x0073, 0x0801, 0x0401, 0x0201, 0x0037, 0x0065, 0x0201,
0x0056, 0x0074, 0x0601, 0x0201, 0x0047, 0x0201, 0x0066, 0x0075, 0x0401, 0x0201, 0x0057, 0x0076, 0x0201, 0x0067, 0x0077,
},
[11] = {
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0012, 0x1801, 0x0801,
0x0201, 0x0021, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401,
0x0201, 0x0040, 0x0004, 0x0201, 0x0041, 0x0014, 0x1e01, 0x1001, 0x0a01, 0x0401, 0x0201, 0x0042, 0x0024, 0x0401, 0x0201, 0x0033,
0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0051, 0x0061, 0x0601, 0x0201, 0x0016, 0x0201, 0x0006, 0x0026, 0x0201, 0x0062, 0x0201,
0x0015, 0x0201, 0x0005, 0x0052, 0x1001, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0025, 0x0044, 0x0060, 0x0201, 0x0063, 0x0036, 0x0401,
0x0201, 0x0070, 0x0017, 0x0071, 0x1001, 0x0601, 0x0401, 0x0201, 0x0007, 0x0064, 0x0072, 0x0201, 0x0027, 0x0401, 0x0201, 0x0053,
0x0035, 0x0201, 0x0054, 0x0045, 0x0a01, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037, 0x0201, 0x0065, 0x0056, 0x0a01, 0x0601,
0x0401, 0x0201, 0x0055, 0x0057, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075, 0x0076, 0x0201, 0x0067, 0x0077,
},
[12] = {
0x0c01, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0201, 0x0000, 0x0201, 0x0020, 0x0002, 0x1001, 0x0401, 0x0201, 0x0021,
0x0012, 0x0401, 0x0201, 0x0022, 0x0031, 0x0201, 0x0013, 0x0201, 0x0030, 0x0201, 0x0003, 0x0040, 0x1a01, 0x0801, 0x0401, 0x0201,
0x0032, 0x0023, 0x0201, 0x0041, 0x0033, 0x0a01, 0x0401, 0x0201, 0x0014, 0x0042, 0x0201, 0x0024, 0x0201, 0x0004, 0x0050, 0x0401,
0x0201, 0x0043, 0x0034, 0x0201, 0x0051, 0x0015, 0x1c01, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0052, 0x0025, 0x0201, 0x0053, 0x0035,
0x0401, 0x0201, 0x0060, 0x0016, 0x0061, 0x0401, 0x0201, 0x0062, 0x0026, 0x0601, 0x0401, 0x0201, 0x0005, 0x0006, 0x0044, 0x0201,
0x0054, 0x0045, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0063, 0x0036, 0x0401, 0x0201, 0x0070, 0x0007, 0x0071, 0x0401, 0x0201, 0x0017,
0x0064, 0x0201, 0x0046, 0x0072, 0x0a01, 0x0601, 0x0201, 0x0027, 0x0201, 0x0055, 0x0073, 0x0201, 0x0037, 0x0056, 0x0801, 0x0401,
0x0201, 0x0065, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0076, 0x0201, 0x0067, 0x0077,
},
[13] = {
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x1c01, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021,
0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0003, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0401,
0x0201, 0x0040, 0x0004, 0x0041, 0x4601, 0x1c01, 0x0e01, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401, 0x0201, 0x0024,
0x0050, 0x0201, 0x0043, 0x0034, 0x0401, 0x0201, 0x0051, 0x0015, 0x0401, 0x0201, 0x0005, 0x0052, 0x0201, 0x0025, 0x0201, 0x0044,
0x0053, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0060, 0x0006, 0x0201, 0x0061, 0x0016, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081, 0x1001,
0x0801, 0x0401, 0x0201, 0x0035, 0x0062, 0x0201, 0x0026, 0x0054, 0x0401, 0x0201, 0x0045, 0x0063, 0x0201, 0x0036, 0x0070, 0x0601,
0x0401, 0x0201, 0x0007, 0x0055, 0x0071, 0x0201, 0x0017, 0x0201, 0x0027, 0x0037, 0x4801, 0x1801, 0x0c01, 0x0401, 0x0201, 0x0018,
0x0082, 0x0201, 0x0028, 0x0401, 0x0201, 0x0064, 0x0046, 0x0072, 0x0801, 0x0401, 0x0201, 0x0084, 0x0048, 0x0201, 0x0090, 0x0009,
0x0201, 0x0091, 0x0019, 0x1801, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0073, 0x0065, 0x0201, 0x0056, 0x0074, 0x0401, 0x0201, 0x0047,
0x0066, 0x0083, 0x0601, 0x0201, 0x0038, 0x0201, 0x0075, 0x0057, 0x0201, 0x0092, 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0067,
0x0085, 0x0201, 0x0058, 0x0039, 0x0201, 0x0093, 0x0201, 0x0049, 0x0086, 0x0601, 0x0201, 0x00a0, 0x0201, 0x0068, 0x000a, 0x0201,
0x00a1, 0x001a, 0x4401, 0x1801, 0x0c01, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0401, 0x0201, 0x0095, 0x0059, 0x0201, 0x00a3, 0x003a,
0x0801, 0x0401, 0x0201, 0x004a, 0x0096, 0x0201, 0x00b0, 0x000b, 0x0201, 0x00b1, 0x001b, 0x1401, 0x0801, 0x0201, 0x00b2, 0x0401,
0x0201, 0x0076, 0x0077, 0x0094, 0x0601, 0x0401, 0x0201, 0x0087, 0x0078, 0x00a4, 0x0401, 0x0201, 0x0069, 0x00a5, 0x002b, 0x0c01,
0x0601, 0x0401, 0x0201, 0x005a, 0x0088, 0x00b3, 0x0201, 0x003b, 0x0201, 0x0079, 0x00a6, 0x0601, 0x0401, 0x0201, 0x006a, 0x00b4,
0x00c0, 0x0401, 0x0201, 0x000c, 0x0098, 0x00c1, 0x3c01, 0x1601, 0x0a01, 0x0601, 0x0201, 0x001c, 0x0201, 0x0089, 0x00b5, 0x0201,
0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x003c, 0x0401, 0x0201, 0x00b6, 0x006b, 0x0201, 0x00c4, 0x004c, 0x1001, 0x0801, 0x0401,
0x0201, 0x00a8, 0x008a, 0x0201, 0x00d0, 0x000d, 0x0201, 0x00d1, 0x0201, 0x004b, 0x0201, 0x0097, 0x00a7, 0x0c01, 0x0601, 0x0201,
0x00c3, 0x0201, 0x007a, 0x0099, 0x0401, 0x0201, 0x00c5, 0x005c, 0x00b7, 0x0401, 0x0201, 0x001d, 0x00d2, 0x0201, 0x002d, 0x0201,
0x007b, 0x00d3, 0x3401, 0x1c01, 0x0c01, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4,
0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e0, 0x0a01,
0x0401, 0x0201, 0x00e1, 0x001e, 0x0401, 0x0201, 0x000e, 0x002e, 0x00e2, 0x0801, 0x0401, 0x0201, 0x00e3, 0x006d, 0x0201, 0x008c,
0x00e4, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x00f0, 0x2601, 0x1001, 0x0401, 0x0201, 0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00aa,
0x009b, 0x00b9, 0x0201, 0x003e, 0x0201, 0x00d6, 0x00c8, 0x0c01, 0x0601, 0x0201, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0201, 0x00ab,
0x0201, 0x005e, 0x00c9, 0x0601, 0x0201, 0x000f, 0x0201, 0x009c, 0x006e, 0x0201, 0x00f2, 0x002f, 0x2001, 0x1001, 0x0601, 0x0401,
0x0201, 0x00d8, 0x008d, 0x003f, 0x0601, 0x0201, 0x00f3, 0x0201, 0x00e6, 0x00ca, 0x0201, 0x00f4, 0x004f, 0x0801, 0x0401, 0x0201,
0x00bb, 0x00ac, 0x0201, 0x00e7, 0x00f5, 0x0401, 0x0201, 0x00d9, 0x009d, 0x0201, 0x005f, 0x00e8, 0x1e01, 0x0c01, 0x0601, 0x0201,
0x006f, 0x0201, 0x00f6, 0x00cb, 0x0401, 0x0201, 0x00bc, 0x00ad, 0x00da, 0x0801, 0x0201, 0x00f7, 0x0401, 0x0201, 0x007e, 0x007f,
0x008e, 0x0601, 0x0401, 0x0201, 0x009e, 0x00ae, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x1201, 0x0801, 0x0401, 0x0201, 0x00db, 0x00bd,
0x0201, 0x00ea, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00eb, 0x0201, 0x00be, 0x0201, 0x00cd, 0x00fa, 0x0e01, 0x0401, 0x0201, 0x00dd,
0x00ec, 0x0601, 0x0401, 0x0201, 0x00e9, 0x00af, 0x00dc, 0x0201, 0x00ce, 0x00fb, 0x0801, 0x0401, 0x0201, 0x00bf, 0x00de, 0x0201,
0x00cf, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00ef, 0x0201, 0x00ff, 0x0201, 0x00ed, 0x0201, 0x00fd, 0x0201, 0x00fc, 0x00fe,
},
[15] = {
0x1001, 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
0x3201, 0x1001, 0x0601, 0x0201, 0x0022, 0x0201, 0x0030, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0003, 0x0040, 0x0201, 0x0032,
0x0023, 0x0e01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0014, 0x0041, 0x0401, 0x0201, 0x0033, 0x0042, 0x0201, 0x0024, 0x0043, 0x0a01,
0x0601, 0x0201, 0x0034, 0x0201, 0x0050, 0x0005, 0x0201, 0x0051, 0x0015, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044,
0x0053, 0x0061, 0x5a01, 0x2401, 0x1201, 0x0a01, 0x0601, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0201, 0x0016, 0x0062, 0x0401,
0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0a01, 0x0601, 0x0201, 0x0036, 0x0201, 0x0070, 0x0007, 0x0201, 0x0071, 0x0055,
0x0401, 0x0201, 0x0017, 0x0064, 0x0201, 0x0072, 0x0027, 0x1801, 0x1001, 0x0801, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037,
0x0065, 0x0401, 0x0201, 0x0056, 0x0080, 0x0201, 0x0008, 0x0074, 0x0401, 0x0201, 0x0081, 0x0018, 0x0201, 0x0082, 0x0028, 0x1001,
0x0801, 0x0401, 0x0201, 0x0047, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048, 0x0601,
0x0401, 0x0201, 0x0090, 0x0019, 0x0091, 0x0401, 0x0201, 0x0092, 0x0076, 0x0201, 0x0067, 0x0029, 0x5c01, 0x2401, 0x1201, 0x0a01,
0x0401, 0x0201, 0x0085, 0x0058, 0x0401, 0x0201, 0x0009, 0x0077, 0x0093, 0x0401, 0x0201, 0x0039, 0x0094, 0x0201, 0x0049, 0x0086,
0x0a01, 0x0601, 0x0201, 0x0068, 0x0201, 0x00a0, 0x000a, 0x0201, 0x00a1, 0x001a, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095,
0x0059, 0x1a01, 0x0e01, 0x0601, 0x0201, 0x00a3, 0x0201, 0x003a, 0x0087, 0x0401, 0x0201, 0x0078, 0x00a4, 0x0201, 0x004a, 0x0096,
0x0601, 0x0401, 0x0201, 0x0069, 0x00b0, 0x00b1, 0x0401, 0x0201, 0x001b, 0x00a5, 0x00b2, 0x0e01, 0x0801, 0x0401, 0x0201, 0x005a,
0x002b, 0x0201, 0x0088, 0x0097, 0x0201, 0x00b3, 0x0201, 0x0079, 0x003b, 0x0801, 0x0401, 0x0201, 0x006a, 0x00b4, 0x0201, 0x004b,
0x00c1, 0x0401, 0x0201, 0x0098, 0x0089, 0x0201, 0x001c, 0x00b5, 0x5001, 0x2201, 0x1001, 0x0601, 0x0401, 0x0201, 0x005b, 0x002c,
0x00c2, 0x0601, 0x0401, 0x0201, 0x000b, 0x00c0, 0x00a6, 0x0201, 0x00a7, 0x007a, 0x0a01, 0x0401, 0x0201, 0x00c3, 0x003c, 0x0401,
0x0201, 0x000c, 0x0099, 0x00b6, 0x0401, 0x0201, 0x006b, 0x00c4, 0x0201, 0x004c, 0x00a8, 0x1401, 0x0a01, 0x0401, 0x0201, 0x008a,
0x00c5, 0x0401, 0x0201, 0x00d0, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7, 0x007b, 0x0201, 0x001d, 0x0201, 0x000d, 0x002d, 0x0c01,
0x0401, 0x0201, 0x00d2, 0x00d3, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x00a9, 0x0601, 0x0401, 0x0201, 0x009a, 0x00b8,
0x00d4, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x4401, 0x2201, 0x1201, 0x0a01, 0x0401, 0x0201, 0x00d5, 0x005d,
0x0401, 0x0201, 0x00e0, 0x000e, 0x00e1, 0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x002e, 0x0801, 0x0401, 0x0201, 0x00b9,
0x009b, 0x0201, 0x00e3, 0x00d6, 0x0401, 0x0201, 0x006d, 0x003e, 0x0201, 0x00c8, 0x008c, 0x1001, 0x0801, 0x0401, 0x0201, 0x00e4,
0x004e, 0x0201, 0x00d7, 0x007d, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x0201, 0x00ab, 0x005e, 0x0801, 0x0401, 0x0201, 0x00c9, 0x009c,
0x0201, 0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00f0, 0x006e, 0x00f2, 0x0201, 0x002f, 0x00e6, 0x2601, 0x1201, 0x0801, 0x0401,
0x0201, 0x00d8, 0x00f3, 0x0201, 0x003f, 0x00f4, 0x0601, 0x0201, 0x004f, 0x0201, 0x008d, 0x00d9, 0x0201, 0x00bb, 0x00ca, 0x0801,
0x0401, 0x0201, 0x00ac, 0x00e7, 0x0201, 0x007e, 0x00f5, 0x0801, 0x0401, 0x0201, 0x009d, 0x005f, 0x0201, 0x00e8, 0x008e, 0x0201,
0x00f6, 0x00cb, 0x2201, 0x1201, 0x0a01, 0x0601, 0x0401, 0x0201, 0x000f, 0x00ae, 0x006f, 0x0201, 0x00bc, 0x00da, 0x0401, 0x0201,
0x00ad, 0x00f7, 0x0201, 0x007f, 0x00e9, 0x0801, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x0401, 0x0201, 0x00db,
0x00bd, 0x0201, 0x00ea, 0x00f9, 0x1001, 0x0801, 0x0401, 0x0201, 0x009f, 0x00dc, 0x0201, 0x00cd, 0x00eb, 0x0401, 0x0201, 0x00be,
0x00fa, 0x0201, 0x00af, 0x00dd, 0x0e01, 0x0601, 0x0401, 0x0201, 0x00ec, 0x00ce, 0x00fb, 0x0401, 0x0201, 0x00bf, 0x00ed, 0x0201,
0x00de, 0x00fc, 0x0601, 0x0401, 0x0201, 0x00cf, 0x00fd, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00fe, 0x0201, 0x00ef, 0x00ff,
},
[16] = {
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x2a01, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021,
0x0012, 0x0a01, 0x0601, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0a01, 0x0401, 0x0201, 0x0032, 0x0023,
0x0401, 0x0201, 0x0040, 0x0004, 0x0041, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401, 0x0201, 0x0024, 0x0050, 0x0201,
0x0043, 0x0034, 0x8a01, 0x2801, 0x1001, 0x0601, 0x0401, 0x0201, 0x0005, 0x0015, 0x0051, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401,
0x0201, 0x0044, 0x0035, 0x0053, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0060, 0x0006, 0x0061, 0x0201, 0x0016, 0x0062, 0x0801, 0x0401,
0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0401, 0x0201, 0x0036, 0x0070, 0x0071, 0x2801, 0x1201, 0x0801, 0x0201, 0x0017,
0x0201, 0x0007, 0x0201, 0x0055, 0x0064, 0x0401, 0x0201, 0x0072, 0x0027, 0x0401, 0x0201, 0x0046, 0x0065, 0x0073, 0x0a01, 0x0601,
0x0201, 0x0037, 0x0201, 0x0056, 0x0008, 0x0201, 0x0080, 0x0081, 0x0601, 0x0201, 0x0018, 0x0201, 0x0074, 0x0047, 0x0201, 0x0082,
0x0201, 0x0028, 0x0066, 0x1801, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0083, 0x0038, 0x0201, 0x0075, 0x0084, 0x0401, 0x0201, 0x0048,
0x0090, 0x0091, 0x0601, 0x0201, 0x0019, 0x0201, 0x0009, 0x0076, 0x0201, 0x0092, 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0085,
0x0058, 0x0201, 0x0093, 0x0039, 0x0401, 0x0201, 0x00a0, 0x000a, 0x001a, 0x0801, 0x0201, 0x00a2, 0x0201, 0x0067, 0x0201, 0x0057,
0x0049, 0x0601, 0x0201, 0x0094, 0x0201, 0x0077, 0x0086, 0x0201, 0x00a1, 0x0201, 0x0068, 0x0095, 0xdc01, 0x7e01, 0x3201, 0x1a01,
0x0c01, 0x0601, 0x0201, 0x002a, 0x0201, 0x0059, 0x003a, 0x0201, 0x00a3, 0x0201, 0x0087, 0x0078, 0x0801, 0x0401, 0x0201, 0x00a4,
0x004a, 0x0201, 0x0096, 0x0069, 0x0401, 0x0201, 0x00b0, 0x000b, 0x00b1, 0x0a01, 0x0401, 0x0201, 0x001b, 0x00b2, 0x0201, 0x002b,
0x0201, 0x00a5, 0x005a, 0x0601, 0x0201, 0x00b3, 0x0201, 0x00a6, 0x006a, 0x0401, 0x0201, 0x00b4, 0x004b, 0x0201, 0x000c, 0x00c1,
0x1e01, 0x0e01, 0x0601, 0x0401, 0x0201, 0x00b5, 0x00c2, 0x002c, 0x0401, 0x0201, 0x00a7, 0x00c3, 0x0201, 0x006b, 0x00c4, 0x0801,
0x0201, 0x001d, 0x0401, 0x0201, 0x0088, 0x0097, 0x003b, 0x0401, 0x0201, 0x00d1, 0x00d2, 0x0201, 0x002d, 0x00d3, 0x1201, 0x0601,
0x0401, 0x0201, 0x001e, 0x002e, 0x00e2, 0x0601, 0x0401, 0x0201, 0x0079, 0x0098, 0x00c0, 0x0201, 0x001c, 0x0201, 0x0089, 0x005b,
0x0e01, 0x0601, 0x0201, 0x003c, 0x0201, 0x007a, 0x00b6, 0x0401, 0x0201, 0x004c, 0x0099, 0x0201, 0x00a8, 0x008a, 0x0601, 0x0201,
0x000d, 0x0201, 0x00c5, 0x005c, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x009a, 0x5801, 0x5601, 0x2401, 0x1001, 0x0801,
0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x0401, 0x0201, 0x00d5, 0x005d, 0x0201, 0x00e0, 0x000e, 0x0801, 0x0201,
0x00e3, 0x0401, 0x0201, 0x00d0, 0x00b7, 0x007b, 0x0601, 0x0401, 0x0201, 0x00a9, 0x00b8, 0x00d4, 0x0201, 0x00e1, 0x0201, 0x00aa,
0x00b9, 0x1801, 0x0a01, 0x0601, 0x0401, 0x0201, 0x009b, 0x00d6, 0x006d, 0x0201, 0x003e, 0x00c8, 0x0601, 0x0401, 0x0201, 0x008c,
0x00e4, 0x004e, 0x0401, 0x0201, 0x00d7, 0x00e5, 0x0201, 0x00ba, 0x00ab, 0x0c01, 0x0401, 0x0201, 0x009c, 0x00e6, 0x0401, 0x0201,
0x006e, 0x00d8, 0x0201, 0x008d, 0x00bb, 0x0801, 0x0401, 0x0201, 0x00e7, 0x009d, 0x0201, 0x00e8, 0x008e, 0x0401, 0x0201, 0x00cb,
0x00bc, 0x009e, 0x00f1, 0x0201, 0x001f, 0x0201, 0x000f, 0x002f, 0x4201, 0x3801, 0x0201, 0x00f2, 0x3401, 0x3201, 0x1401, 0x0801,
0x0201, 0x00bd, 0x0201, 0x005e, 0x0201, 0x007d, 0x00c9, 0x0601, 0x0201, 0x00ca, 0x0201, 0x00ac, 0x007e, 0x0401, 0x0201, 0x00da,
0x00ad, 0x00cc, 0x0a01, 0x0601, 0x0201, 0x00ae, 0x0201, 0x00db, 0x00dc, 0x0201, 0x00cd, 0x00be, 0x0601, 0x0401, 0x0201, 0x00eb,
0x00ed, 0x00ee, 0x0601, 0x0401, 0x0201, 0x00d9, 0x00ea, 0x00e9, 0x0201, 0x00de, 0x0401, 0x0201, 0x00dd, 0x00ec, 0x00ce, 0x003f,
0x00f0, 0x0401, 0x0201, 0x00f3, 0x00f4, 0x0201, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0a01, 0x0201, 0x00ff, 0x0401, 0x0201, 0x00f6,
0x006f, 0x0201, 0x00f7, 0x007f, 0x0c01, 0x0601, 0x0201, 0x008f, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00fa, 0x00af,
0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
},
[24] = {
0x3c01, 0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0e01, 0x0601, 0x0401, 0x0201, 0x0020, 0x0002, 0x0021,
0x0201, 0x0012, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0e01, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023,
0x0401, 0x0201, 0x0040, 0x0004, 0x0041, 0x0801, 0x0401, 0x0201, 0x0014, 0x0033, 0x0201, 0x0042, 0x0024, 0x0601, 0x0401, 0x0201,
0x0043, 0x0034, 0x0051, 0x0601, 0x0401, 0x0201, 0x0050, 0x0005, 0x0015, 0x0201, 0x0052, 0x0025, 0xfa01, 0x6201, 0x2201, 0x1201,
0x0a01, 0x0401, 0x0201, 0x0044, 0x0053, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0401, 0x0201, 0x0061, 0x0016, 0x0201, 0x0062,
0x0026, 0x0801, 0x0401, 0x0201, 0x0054, 0x0045, 0x0201, 0x0063, 0x0036, 0x0401, 0x0201, 0x0071, 0x0055, 0x0201, 0x0064, 0x0046,
0x2001, 0x0e01, 0x0601, 0x0201, 0x0072, 0x0201, 0x0027, 0x0037, 0x0201, 0x0073, 0x0401, 0x0201, 0x0070, 0x0007, 0x0017, 0x0a01,
0x0401, 0x0201, 0x0065, 0x0056, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081, 0x0401, 0x0201, 0x0074, 0x0047, 0x0201, 0x0018, 0x0082,
0x1001, 0x0801, 0x0401, 0x0201, 0x0028, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048,
0x0801, 0x0401, 0x0201, 0x0091, 0x0019, 0x0201, 0x0092, 0x0076, 0x0401, 0x0201, 0x0067, 0x0029, 0x0201, 0x0085, 0x0058, 0x5c01,
0x2201, 0x1001, 0x0801, 0x0401, 0x0201, 0x0093, 0x0039, 0x0201, 0x0094, 0x0049, 0x0401, 0x0201, 0x0077, 0x0086, 0x0201, 0x0068,
0x00a1, 0x0801, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x0401, 0x0201, 0x00a3, 0x003a, 0x0201, 0x0087, 0x0201,
0x0078, 0x004a, 0x1601, 0x0c01, 0x0401, 0x0201, 0x00a4, 0x0096, 0x0401, 0x0201, 0x0069, 0x00b1, 0x0201, 0x001b, 0x00a5, 0x0601,
0x0201, 0x00b2, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x00b3, 0x1001, 0x0a01, 0x0601, 0x0201, 0x0090, 0x0201, 0x0009, 0x00a0,
0x0201, 0x0097, 0x0079, 0x0401, 0x0201, 0x00a6, 0x006a, 0x00b4, 0x0c01, 0x0601, 0x0201, 0x001a, 0x0201, 0x000a, 0x00b0, 0x0201,
0x003b, 0x0201, 0x000b, 0x00c0, 0x0401, 0x0201, 0x004b, 0x00c1, 0x0201, 0x0098, 0x0089, 0x4301, 0x2201, 0x1001, 0x0801, 0x0401,
0x0201, 0x001c, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x00a7, 0x0201, 0x007a, 0x00c3, 0x0a01, 0x0601, 0x0201,
0x003c, 0x0201, 0x000c, 0x00d0, 0x0201, 0x00b6, 0x006b, 0x0401, 0x0201, 0x00c4, 0x004c, 0x0201, 0x0099, 0x00a8, 0x1001, 0x0801,
0x0401, 0x0201, 0x008a, 0x00c5, 0x0201, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7, 0x007b, 0x0201, 0x001d, 0x00d2, 0x0901, 0x0401,
0x0201, 0x002d, 0x00d3, 0x0201, 0x003d, 0x00c6, 0x55fa, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4, 0x2001, 0x1001,
0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e1, 0x0801,
0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x00b9, 0x0401, 0x0201, 0x009b, 0x00e3, 0x0201, 0x00d6, 0x006d, 0x1401, 0x0a01,
0x0601, 0x0201, 0x003e, 0x0201, 0x002e, 0x004e, 0x0201, 0x00c8, 0x008c, 0x0401, 0x0201, 0x00e4, 0x00d7, 0x0401, 0x0201, 0x007d,
0x00ab, 0x00e5, 0x0a01, 0x0401, 0x0201, 0x00ba, 0x005e, 0x0201, 0x00c9, 0x0201, 0x009c, 0x006e, 0x0801, 0x0201, 0x00e6, 0x0201,
0x000d, 0x0201, 0x00e0, 0x000e, 0x0401, 0x0201, 0x00d8, 0x008d, 0x0201, 0x00bb, 0x00ca, 0x4a01, 0x0201, 0x00ff, 0x4001, 0x3a01,
0x2001, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ac, 0x00e7, 0x0201, 0x007e, 0x00d9, 0x0401, 0x0201, 0x009d, 0x00e8, 0x0201, 0x008e,
0x00cb, 0x0801, 0x0401, 0x0201, 0x00bc, 0x00da, 0x0201, 0x00ad, 0x00e9, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00db, 0x00bd,
0x1001, 0x0801, 0x0401, 0x0201, 0x00ea, 0x00ae, 0x0201, 0x00dc, 0x00cd, 0x0401, 0x0201, 0x00eb, 0x00be, 0x0201, 0x00dd, 0x00ec,
0x0801, 0x0401, 0x0201, 0x00ce, 0x00ed, 0x0201, 0x00de, 0x00ee, 0x000f, 0x0401, 0x0201, 0x00f0, 0x001f, 0x00f1, 0x0401, 0x0201,
0x00f2, 0x002f, 0x0201, 0x00f3, 0x003f, 0x1201, 0x0801, 0x0401, 0x0201, 0x00f4, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0401, 0x0201,
0x00f6, 0x006f, 0x0201, 0x00f7, 0x0201, 0x007f, 0x008f, 0x0a01, 0x0401, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00af,
0x00fa, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
},
[32] = {
0x0201, 0x0000, 0x0801, 0x0401, 0x0201, 0x0008, 0x0004, 0x0201, 0x0001, 0x0002, 0x0801, 0x0401, 0x0201, 0x000c, 0x000a, 0x0201,
0x0003, 0x0006, 0x0601, 0x0201, 0x0009, 0x0201, 0x0005, 0x0007, 0x0401, 0x0201, 0x000e, 0x000d, 0x0201, 0x000f, 0x000b,
},
[33] = {
0x1001, 0x0801, 0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0002, 0x0003, 0x0401, 0x0201, 0x0004, 0x0005, 0x0201, 0x0006, 0x0007,
0x0801, 0x0401, 0x0201, 0x0008, 0x0009, 0x0201, 0x000a, 0x000b, 0x0401, 0x0201, 0x000c, 0x000d, 0x0201, 0x000e, 0x000f,
},
}
return E
+407
View File
@@ -0,0 +1,407 @@
--!strict
--!native
--!optimize 2
-- audio/mp3/Encode2: an MPEG audio layer II (MP2) encoder: constant bit rate, MPEG-1 (32 / 44.1 / 48 kHz) and MPEG-2
-- low sampling frequencies (16 / 22.05 / 24 kHz), mono or stereo. New code written from ISO/IEC 11172-3 (2.4.2.3,
-- C.1.5.2): the polyphase analysis (audio/mp3/Analysis), one scalefactor per 12-sample part (scfsi 0: all three are
-- sent), the standard bit allocation tables (in minimp3's compact form, CC0: licenses/minimp3-CC0.txt, as the decoder
-- in this folder reads them), a greedy allocation that always gives the next step to the band with the most noise
-- (signal level minus the quantizer's SNR; no psychoacoustic model), uniform midtread quantization and grouping of
-- 3 / 5 / 9-level samples. Never throws.
local T = require("@formats/audio/mp3/Tables")
local Analysis = require("@formats/audio/mp3/Analysis")
local Encode2 = {}
local floor, log = math.floor, math.log
local writeu8 = buffer.writeu8
local POW = table.create(40, 0)
for i = 0, 39 do
POW[i + 1] = 2 ^ i
end
-- the allocation tables: { bit-allocation-code offset into g_bitalloc_code_tab, bits, band count } runs (minimp3)
local ALLOC_L2M1 = { 0, 4, 3, 16, 4, 8, 32, 3, 12, 40, 2, 7 }
local ALLOC_L2M1_LOW = { 44, 4, 2, 44, 3, 10 }
local ALLOC_L2M2 = { 60, 4, 4, 44, 3, 7, 44, 2, 19 }
local CODE = T.g_bitalloc_code_tab
-- quantizer classes (minimp3's ba): 1..16 -> 2^ba - 1 levels in ba bits; 17 / 18 / 19 -> 3 / 5 / 9 levels grouped
local function levels(ba: number): number
if ba >= 17 then
return if ba == 17 then 3 elseif ba == 18 then 5 else 9
end
return POW[ba + 1] - 1
end
-- bits for the 3 samples of one granule
local function tripleBits(ba: number): number
if ba >= 17 then
return if ba == 17 then 5 elseif ba == 18 then 7 else 10
end
return 3 * ba
end
-- the quantizer's signal-to-noise ratio in dB (ISO 11172-3 table C.5)
local SNR: { [number]: number } = {}
do
local byLevels = {
[3] = 7.00,
[5] = 11.00,
[7] = 16.00,
[9] = 20.84,
[15] = 25.28,
[31] = 31.59,
[63] = 37.75,
[127] = 43.84,
[255] = 49.89,
[511] = 55.93,
[1023] = 61.96,
[2047] = 67.98,
[4095] = 74.01,
[8191] = 80.03,
[16383] = 86.05,
[32767] = 92.01,
[65535] = 98.01,
}
for ba = 1, 19 do
SNR[ba] = byLevels[levels(ba)] or (6.02 * ba)
end
end
-- scalefactors 2^(1 - i/3), i = 0 .. 62
local SCF = table.create(63, 0)
for i = 0, 62 do
SCF[i + 1] = 2 ^ (1 - i / 3)
end
local RATES = {
[44100] = { 3, 0 },
[48000] = { 3, 1 },
[32000] = { 3, 2 },
[22050] = { 2, 0 },
[24000] = { 2, 1 },
[16000] = { 2, 2 },
}
Encode2.RATES = RATES
local BITRATES_V1 = { 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }
local BITRATES_V2 = { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
Encode2.BITRATES = { [1] = BITRATES_V1, [2] = BITRATES_V2 }
type W = { buf: buffer, pos: number, acc: number, cnt: number }
local function put(w: W, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
export type Source = {
sampleRate: number,
channels: number,
frames: number,
read: (self: any, n: number, out: buffer?) -> (buffer, number),
err: any,
}
export type Options = { bitrate: number, mode: string, prefix: buffer? }
-- encode(src, opts) -> buffer | nil, message
function Encode2.encode(src: Source, opts: Options): (buffer?, string?)
local rate = src.sampleRate
local info = RATES[rate]
if not info then
return nil, "unsupported sample rate"
end
local version, srIndex = info[1], info[2]
local mpeg1 = version == 3
local inCh = src.channels
local nch = if opts.mode == "mono" then 1 else 2
local kbps = opts.bitrate
local list = if mpeg1 then BITRATES_V1 else BITRATES_V2
local brIndex = table.find(list, kbps)
if not brIndex then
return nil, "unsupported bit rate"
end
-- the allocation table (the decoder's choice from the header)
local alloc, nbands
if not mpeg1 then
alloc, nbands = ALLOC_L2M2, 30
else
local perCh = if nch == 2 then kbps // 2 else kbps
alloc, nbands = ALLOC_L2M1, 27
if perCh < 56 then
alloc, nbands = ALLOC_L2M1_LOW, if srIndex == 2 then 12 else 8
elseif perCh >= 96 and srIndex ~= 1 then
nbands = 30
end
end
local bandOff, bandBits = table.create(nbands, 0), table.create(nbands, 0)
do
local k, ai = 0, 1
local off, bits = 0, 0
for i = 0, nbands - 1 do
if i == k then
off, bits, k = alloc[ai], alloc[ai + 1], k + alloc[ai + 2]
ai += 3
end
bandOff[i + 1], bandBits[i + 1] = off, bits
end
end
-- per band: the classes it can take, in increasing order (index into the code table = the value written)
local steps: { { { number } } } = {}
for i = 1, nbands do
local s = {}
for v = 1, POW[bandBits[i] + 1] - 1 do
local ba = CODE[bandOff[i] + v]
if ba and ba > 0 then
table.insert(s, { v, ba })
end
end
steps[i] = s
end
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
if opts.prefix then
local pf = opts.prefix :: buffer
if buffer.len(pf) > buffer.len(w.buf) - 16 then
w.buf = buffer.create(buffer.len(pf) + 65536)
end
buffer.copy(w.buf, 0, pf, 0, buffer.len(pf))
w.pos = buffer.len(pf)
end
local ana = { Analysis.new(), Analysis.new() }
local pcm = { table.create(1152, 0), table.create(1152, 0) }
-- subband samples: sb[c][band * 36 + t + 1], t = 0 .. 35
local sbs = { table.create(32 * 36, 0), table.create(32 * 36, 0) }
local sub = table.create(32, 0)
local scf = { table.create(32 * 3, 0), table.create(32 * 3, 0) } -- scalefactor index per band and part
local bal = { table.create(32, 0), table.create(32, 0) } -- allocation step (0 = none) per band
local slot = 144000 * kbps
local rest = 0
local tmp: buffer? = nil
local pending: buffer? = nil
local pendN, pendOff = 0, 0
local srcDone, flushed = false, false
while true do
local got = 0
while got < 1152 and not srcDone do
if pendN == 0 then
local b, n = src:read(4096, tmp)
if n <= 0 then
srcDone = true
break
end
tmp = b
pending, pendN, pendOff = b, n, 0
end
local take = math.min(1152 - got, pendN)
local pb = pending :: buffer
for i = 0, take - 1 do
local base = (pendOff + i) * inCh * 4
local l = buffer.readf32(pb, base)
local r = if inCh > 1 then buffer.readf32(pb, base + 4) else l
if nch == 1 then
pcm[1][got + i + 1] = if inCh > 1 then (l + r) * 0.5 else l
else
pcm[1][got + i + 1] = l
pcm[2][got + i + 1] = r
end
end
got += take
pendN -= take
pendOff += take
end
if got == 0 then
if flushed then
break
end
flushed = true
end
for c = 1, nch do
local s = pcm[c]
for i = got + 1, 1152 do
s[i] = 0
end
local sb = sbs[c]
for t = 0, 35 do
Analysis.run(ana[c], s, t * 32 + 1, sub, 1)
for b = 0, 31 do
sb[b * 36 + t + 1] = sub[b + 1]
end
end
-- scalefactors: the smallest scalefactor (largest index) still >= the part's peak
local sc = scf[c]
for b = 0, nbands - 1 do
for part = 0, 2 do
local m = 0
local o = b * 36 + part * 12
for t = 1, 12 do
local v = math.abs(sb[o + t])
if v > m then
m = v
end
end
local idx = 62
if m > 0 then
idx = math.clamp(floor(3 * (1 - log(m) / log(2))), 0, 62)
while idx > 0 and SCF[idx + 1] < m do
idx -= 1
end
while idx < 62 and SCF[idx + 2] >= m do
idx += 1
end
end
sc[b * 3 + part + 1] = idx
end
end
end
-- the frame size and the bits left for scalefactors and samples
local frameBytes = slot // rate
rest += slot % rate
local padding = 0
if rest >= rate then
rest -= rate
padding = 1
frameBytes += 1
end
local avail = frameBytes * 8 - 32
for b = 1, nbands do
avail -= bandBits[b] * nch
end
-- greedy allocation: the band with the most noise gets its next step while it fits
local level = { table.create(nbands, 0), table.create(nbands, 0) } -- signal level (dB) per band
for c = 1, nch do
local sc = scf[c]
for b = 1, nbands do
local best = math.min(sc[(b - 1) * 3 + 1], sc[(b - 1) * 3 + 2], sc[(b - 1) * 3 + 3])
level[c][b] = 20 * math.log10(SCF[best + 1])
bal[c][b] = 0
end
end
while true do
local bestC, bestB, bestNoise, bestCost = 0, 0, -math.huge, 0
for c = 1, nch do
for b = 1, nbands do
local st = bal[c][b]
local s = steps[b]
if st < #s and level[c][b] > -110 then
local cur = if st == 0 then 0 else s[st][2]
local nxt = s[st + 1][2]
local noise = level[c][b] - (if st == 0 then 0 else SNR[cur])
local cost = 12 * tripleBits(nxt) - (if st == 0 then 0 else 12 * tripleBits(cur))
if st == 0 then
cost += 2 + 18 -- scfsi + three scalefactors
end
if cost <= avail and noise > bestNoise then
bestC, bestB, bestNoise, bestCost = c, b, noise, cost
end
end
end
end
if bestC == 0 then
break
end
bal[bestC][bestB] += 1
avail -= bestCost
end
-- write the frame
if w.pos + frameBytes + 64 > buffer.len(w.buf) then
local nb = buffer.create(buffer.len(w.buf) * 2 + frameBytes)
buffer.copy(nb, 0, w.buf, 0, w.pos)
w.buf = nb
end
local start = w.pos
put(w, 0x7FF, 11)
put(w, version, 2)
put(w, 2, 2) -- layer II
put(w, 1, 1) -- no CRC
put(w, brIndex, 4)
put(w, srIndex, 2)
put(w, padding, 1)
put(w, 0, 1)
put(w, if nch == 1 then 3 else 0, 2)
put(w, 0, 2)
put(w, 0, 1)
put(w, 1, 1)
put(w, 0, 2)
-- bit allocation
for b = 1, nbands do
for c = 1, nch do
local st = bal[c][b]
put(w, if st == 0 then 0 else steps[b][st][1], bandBits[b])
end
end
-- scfsi (0: three scalefactors) and the scalefactors
for b = 1, nbands do
for c = 1, nch do
if bal[c][b] > 0 then
put(w, 0, 2)
end
end
end
for b = 1, nbands do
for c = 1, nch do
if bal[c][b] > 0 then
for part = 1, 3 do
put(w, scf[c][(b - 1) * 3 + part], 6)
end
end
end
end
-- samples: 12 granules of 3, band by band, channel by channel
for gr = 0, 11 do
local part = gr // 4
for b = 1, nbands do
for c = 1, nch do
local st = bal[c][b]
if st > 0 then
local ba = steps[b][st][2]
local L = levels(ba)
local inv = 1 / SCF[scf[c][(b - 1) * 3 + part + 1] + 1]
local o = (b - 1) * 36 + gr * 3
local sb = sbs[c]
local q0, q1, q2
do
local function quant(x: number): number
local q = floor((x * inv + 1) * L / 2)
return if q < 0 then 0 elseif q > L - 1 then L - 1 else q
end
q0, q1, q2 = quant(sb[o + 1]), quant(sb[o + 2]), quant(sb[o + 3])
end
if ba >= 17 then
put(w, q0 + L * (q1 + L * q2), tripleBits(ba))
else
put(w, q0, ba)
put(w, q1, ba)
put(w, q2, ba)
end
end
end
end
end
if w.cnt > 0 then
put(w, 0, 8 - w.cnt)
end
while w.pos < start + frameBytes do
writeu8(w.buf, w.pos, 0)
w.pos += 1
end
end
if src.err then
return nil, tostring(src.err)
end
local out = buffer.create(w.pos)
buffer.copy(out, 0, w.buf, 0, w.pos)
return out, nil
end
return Encode2
+680
View File
@@ -0,0 +1,680 @@
--!strict
--!native
--!optimize 2
-- audio/mp3/Encode3: an MPEG audio layer III encoder in the spirit of shine: constant bit rate, long blocks only,
-- global-gain rate control (no psychoacoustic model, scalefactors all zero), no bit reservoir (main_data_begin = 0),
-- MPEG-1 (32 / 44.1 / 48 kHz, two granules), MPEG-2 (16 / 22.05 / 24 kHz) and MPEG-2.5 (8 / 11.025 / 12 kHz), mono,
-- stereo or joint (mid / side) stereo. New code written from ISO/IEC 11172-3 / 13818-3 (no shine / LAME code): the
-- analysis filterbank (audio/mp3/Analysis), the 36-point MDCT with the sine window, the alias-reduction butterflies,
-- the x^(3/4) quantizer, big_values / count1 partitioning with per-region Huffman table choice; the Huffman codes are
-- derived from the standard's decoder trees (audio/mp3/EncTables, from PDMP3, public domain). Never throws.
local E = require("@formats/audio/mp3/EncTables")
local T = require("@formats/audio/mp3/Tables")
local Analysis = require("@formats/audio/mp3/Analysis")
local Encode3 = {}
local floor, sqrt, cos, pi = math.floor, math.sqrt, math.cos, math.pi
local writeu8 = buffer.writeu8
local POW = table.create(40, 0)
for i = 0, 39 do
POW[i + 1] = 2 ^ i
end
-- ------------------------------------------------------------------------------------------------ Huffman codes
-- CODE[t][x * 16 + y + 1], LEN[t][...] for the pair tables 1..31 (16..23 and 24..31 share a tree); count1 tables A
-- (32) and B (33) indexed by v * 8 + w * 4 + x * 2 + y + 1
local CODE: { [number]: { number } } = {}
local LEN: { [number]: { number } } = {}
local XLEN: { [number]: number } = {}
local function walk(tree: { number }, codes: { number }, lens: { number }, p: number, code: number, len: number)
local v = tree[p + 1]
if bit32.band(v, 0xFF00) == 0 then
local idx = bit32.band(v, 0xFF) + 1
if lens[idx] == 0 then
codes[idx], lens[idx] = code, len
end
return
end
-- left (bit 0): offsets of 250+ chain
local q = p
while bit32.rshift(tree[q + 1], 8) >= 250 do
q += bit32.rshift(tree[q + 1], 8)
end
q += bit32.rshift(tree[q + 1], 8)
walk(tree, codes, lens, q, code * 2, len + 1)
q = p
while bit32.band(tree[q + 1], 0xFF) >= 250 do
q += bit32.band(tree[q + 1], 0xFF)
end
q += bit32.band(tree[q + 1], 0xFF)
walk(tree, codes, lens, q, code * 2 + 1, len + 1)
end
for t = 1, 33 do
local src = if t >= 16 and t <= 23 then 16 elseif t >= 24 and t <= 31 then 24 else t
local tree = E.TREES[src]
if tree then
local codes, lens = table.create(256, 0), table.create(256, 0)
walk(tree, codes, lens, 0, 0, 0)
-- the leaf value of pair tables is x << 4 | y: index x * 16 + y + 1 (count1: the 4-bit vwxy)
CODE[t], LEN[t] = codes, lens
local xl = 0
for idx = 1, 256 do
if lens[idx] > 0 then
local x = (idx - 1) // 16
if x + 1 > xl then
xl = x + 1
end
end
end
XLEN[t] = xl
end
end
local LINBITS = E.LINBITS
-- the tables that hold values up to m (m <= 15), smallest first
local SMALL: { [number]: { number } } = {}
for m = 0, 15 do
local list = {}
for _, t in { 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15 } do
if XLEN[t] and XLEN[t] - 1 >= m then
table.insert(list, t)
end
end
SMALL[m] = list
end
-- ------------------------------------------------------------------------------------------------ bit writer
type W = { buf: buffer, pos: number, acc: number, cnt: number }
local function put(w: W, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
Encode3.put = put
-- ------------------------------------------------------------------------------------------------ constants
local RATES = {
-- rate -> version bits (3 MPEG-1, 2 MPEG-2, 0 MPEG-2.5), sample rate index, scalefactor band group (g_scf_long)
[44100] = { 3, 0, 5 },
[48000] = { 3, 1, 6 },
[32000] = { 3, 2, 7 },
[22050] = { 2, 0, 2 },
[24000] = { 2, 1, 3 },
[16000] = { 2, 2, 4 },
[11025] = { 0, 0, 0 },
[12000] = { 0, 1, 0 },
[8000] = { 0, 2, 1 },
}
Encode3.RATES = RATES
local BITRATES_V1 = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 }
local BITRATES_V2 = { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
Encode3.BITRATES = { [1] = BITRATES_V1, [2] = BITRATES_V2 }
-- the 36-point MDCT (long blocks): X[k] = 1/9 sum_i w_i z_i cos(pi/72 (2i + 19)(2k + 1)), w_i = sin(pi/36 (i + 1/2))
local MDCT = table.create(18 * 36, 0)
for k = 0, 17 do
for i = 0, 35 do
MDCT[k * 36 + i + 1] = math.sin(pi / 36 * (i + 0.5)) * cos(pi / 72 * (2 * i + 19) * (2 * k + 1)) / 9
end
end
-- alias reduction coefficients
local CI = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
local CS, CA = table.create(8, 0), table.create(8, 0)
for i = 1, 8 do
local s = sqrt(1 + CI[i] * CI[i])
CS[i], CA[i] = 1 / s, CI[i] / s
end
-- ------------------------------------------------------------------------------------------------ Huffman planning
type Plan = {
bits: number,
bigValues: number, -- pairs
count1: number, -- quadruples
tables: { number }, -- 3 region tables
region0: number,
region1: number,
c1table: number, -- 0 = A (table 32), 1 = B (33)
addr1: number, -- coefficient index where region 1 starts
addr2: number,
}
local function newPlan(): Plan
return { bits = 0, bigValues = 0, count1 = 0, tables = { 0, 0, 0 }, region0 = 0, region1 = 0, c1table = 0, addr1 = 0, addr2 = 0 }
end
-- bits of the pairs ix[a .. b) with table t (ix 1-based: ix[i + 1] is coefficient i)
local function pairBits(ix: { number }, a: number, b: number, t: number): number
if t == 0 then
return 0
end
local lens = LEN[t]
local lb = LINBITS[t]
local bits = 0
for i = a, b - 1, 2 do
local x, y = ix[i + 1], ix[i + 2]
local extra = 0
if x > 14 then
x = 15
extra += lb
end
if y > 14 then
y = 15
extra += lb
end
bits += lens[x * 16 + y + 1] + extra
if x ~= 0 then
bits += 1
end
if y ~= 0 then
bits += 1
end
end
return bits
end
-- the best table for the pairs ix[a .. b): table, bits
local function chooseTable(ix: { number }, a: number, b: number): (number, number)
if b <= a then
return 0, 0
end
local m = 0
for i = a + 1, b do
local v = ix[i]
if v > m then
m = v
end
end
if m == 0 then
return 0, 0
end
if m <= 15 then
local best, bestBits = 0, math.huge
for _, t in SMALL[m] do
local bits = pairBits(ix, a, b, t)
if bits < bestBits then
best, bestBits = t, bits
end
end
return best, bestBits
end
local need = m - 15
local best, bestBits = 0, math.huge
for _, group in { 16, 24 } do
for t = group, group + 7 do
if POW[LINBITS[t] + 1] - 1 >= need then
local bits = pairBits(ix, a, b, t)
if bits < bestBits then
best, bestBits = t, bits
end
break
end
end
end
return best, bestBits
end
-- plan the Huffman coding of ix (576 values, max <= 8206) into p; bounds = scalefactor band starts (1-based list)
local function plan(ix: { number }, bounds: { number }, p: Plan)
local i = 576
while i > 1 and ix[i] == 0 and ix[i - 1] == 0 do
i -= 2
end
-- count1 region: quadruples of values <= 1
local c1 = 0
while i > 3 and ix[i] <= 1 and ix[i - 1] <= 1 and ix[i - 2] <= 1 and ix[i - 3] <= 1 do
i -= 4
c1 += 1
end
local bv = i -- big_values * 2 coefficients
p.bigValues, p.count1 = bv // 2, c1
-- count1 bits, table A vs B
local ba, bb = 0, 0
local lensA = LEN[32]
for q = 0, c1 - 1 do
local o = bv + q * 4
local v, w, x, y = ix[o + 1], ix[o + 2], ix[o + 3], ix[o + 4]
local s = v + w + x + y
ba += lensA[v * 8 + w * 4 + x * 2 + y + 1] + s
bb += 4 + s
end
p.c1table = if bb < ba then 1 else 0
local bits = if bb < ba then bb else ba
-- regions: region 0 / 1 / 2 split at scalefactor band boundaries
local j = 0
while j + 1 < #bounds and bounds[j + 2] <= bv do
j += 1
end
local r0 = math.clamp(floor((j + 1) / 3), 0, 15)
local r1 = math.clamp(floor((j + 1) / 3), 0, 7)
if r0 + r1 + 2 > 22 then
r1 = 22 - r0 - 2
end
local a1 = math.min(bounds[r0 + 2] or 576, bv)
local a2 = math.min(bounds[r0 + r1 + 3] or 576, bv)
p.region0, p.region1, p.addr1, p.addr2 = r0, r1, a1, a2
local t1, b1 = chooseTable(ix, 0, a1)
local t2, b2 = chooseTable(ix, a1, a2)
local t3, b3 = chooseTable(ix, a2, bv)
p.tables[1], p.tables[2], p.tables[3] = t1, t2, t3
p.bits = bits + b1 + b2 + b3
end
local function writePairs(w: W, ix: { number }, sign: { boolean }, a: number, b: number, t: number)
if t == 0 then
return
end
local codes, lens = CODE[t], LEN[t]
local lb = LINBITS[t]
for i = a, b - 1, 2 do
local x, y = ix[i + 1], ix[i + 2]
local cx, cy = if x > 14 then 15 else x, if y > 14 then 15 else y
local idx = cx * 16 + cy + 1
put(w, codes[idx], lens[idx])
if cx == 15 then
put(w, x - 15, lb)
end
if x ~= 0 then
put(w, if sign[i + 1] then 1 else 0, 1)
end
if cy == 15 then
put(w, y - 15, lb)
end
if y ~= 0 then
put(w, if sign[i + 2] then 1 else 0, 1)
end
end
end
local function writeHuffman(w: W, ix: { number }, sign: { boolean }, p: Plan)
local bv = p.bigValues * 2
writePairs(w, ix, sign, 0, p.addr1, p.tables[1])
writePairs(w, ix, sign, p.addr1, p.addr2, p.tables[2])
writePairs(w, ix, sign, p.addr2, bv, p.tables[3])
local codesA, lensA = CODE[32], LEN[32]
for q = 0, p.count1 - 1 do
local o = bv + q * 4
local v, ww, x, y = ix[o + 1], ix[o + 2], ix[o + 3], ix[o + 4]
local idx = v * 8 + ww * 4 + x * 2 + y
if p.c1table == 1 then
put(w, 15 - idx, 4) -- table B: the 4 bits inverted
else
put(w, codesA[idx + 1], lensA[idx + 1])
end
for k = 1, 4 do
if ix[o + k] ~= 0 then
put(w, if sign[o + k] then 1 else 0, 1)
end
end
end
end
-- ------------------------------------------------------------------------------------------------ quantization
type Unit = {
xr: { number }, -- 576 MDCT coefficients
xa: { number }, -- |xr|^(3/4)
ix: { number },
sign: { boolean },
plan: Plan,
gain: number,
}
local function newUnit(): Unit
return {
xr = table.create(576, 0),
xa = table.create(576, 0),
ix = table.create(576, 0),
sign = table.create(576, false),
plan = newPlan(),
gain = 210,
}
end
-- quantize with global gain g: false when a value overflows 8206
local function quantize(u: Unit, g: number): boolean
local step = 2 ^ (-(g - 210) * 3 / 16)
local xa, ix = u.xa, u.ix
for i = 1, 576 do
local v = floor(xa[i] * step + 0.4054)
if v > 8206 then
return false
end
ix[i] = v
end
return true
end
-- the finest global gain whose Huffman size fits budget bits
local function rateLoop(u: Unit, budget: number, bounds: { number })
local xr, xa, sign = u.xr, u.xa, u.sign
local maxa = 0
for i = 1, 576 do
local v = xr[i]
sign[i] = v < 0
local a = math.abs(v) ^ 0.75
xa[i] = a
if a > maxa then
maxa = a
end
end
local p = u.plan
if maxa < 1e-12 then
for i = 1, 576 do
u.ix[i] = 0
end
plan(u.ix, bounds, p)
u.gain = 0
return
end
-- the gain where the largest value quantizes to 0: an upper bound
local lo, hi = 0, 255
local best = -1
while lo <= hi do
local mid = (lo + hi) // 2
if quantize(u, mid) then
plan(u.ix, bounds, p)
if p.bits <= budget then
best = mid
hi = mid - 1
else
lo = mid + 1
end
else
lo = mid + 1
end
end
if best < 0 then
best = 255
end
quantize(u, best)
plan(u.ix, bounds, p)
if p.bits > budget then -- give up on this granule: silence fits any budget
for i = 1, 576 do
u.ix[i] = 0
end
plan(u.ix, bounds, p)
end
u.gain = best
end
-- ------------------------------------------------------------------------------------------------ encoder
export type Source = {
sampleRate: number,
channels: number,
frames: number,
read: (self: any, n: number, out: buffer?) -> (buffer, number),
err: any,
}
export type Options = {
bitrate: number, -- kbps, valid for the version
mode: string, -- "stereo" | "joint" | "mono"
prefix: buffer?, -- bytes written before the first frame (an ID3v2 tag)
}
-- encode(src, opts) -> buffer | nil, message
function Encode3.encode(src: Source, opts: Options): (buffer?, string?)
local rate = src.sampleRate
local info = RATES[rate]
if not info then
return nil, "unsupported sample rate"
end
local version, srIndex, group = info[1], info[2], info[3]
local mpeg1 = version == 3
local granules = if mpeg1 then 2 else 1
local spf = 576 * granules
local inCh = src.channels
local mode = opts.mode
local nch = if mode == "mono" then 1 else 2
local kbps = opts.bitrate
local list = if mpeg1 then BITRATES_V1 else BITRATES_V2
local brIndex = table.find(list, kbps)
if not brIndex then
return nil, "unsupported bit rate"
end
local sideBytes = if mpeg1 then (if nch == 1 then 17 else 32) else (if nch == 1 then 9 else 17)
local slotNum = (if mpeg1 then 144000 else 72000) * kbps -- frame bytes * rate
-- scalefactor band starts (long blocks)
local bounds = { 0 }
do
local acc = 0
for k = 0, 21 do
acc += T.g_scf_long[group * 23 + k]
table.insert(bounds, acc)
end
end
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
if opts.prefix then
local pf = opts.prefix :: buffer
local need = buffer.len(pf)
if need > buffer.len(w.buf) - 16 then
w.buf = buffer.create(need + 65536)
end
buffer.copy(w.buf, 0, pf, 0, need)
w.pos = need
end
local ana = { Analysis.new(), Analysis.new() }
local prev = { table.create(576, 0), table.create(576, 0) } -- previous granule's subband samples [sb * 18 + k + 1]
local cur = { table.create(576, 0), table.create(576, 0) }
local units = {}
for g = 1, 2 do
units[g] = { newUnit(), newUnit() }
end
local pcm = { table.create(spf, 0), table.create(spf, 0) }
local sub = table.create(32, 0)
local z = table.create(36, 0)
local rest = 0
local tmp: buffer? = nil
local pending: buffer? = nil
local pendN, pendOff = 0, 0
local srcDone = false
local flushed = false
while true do
-- one frame of input (zero padded at the end; one more silent frame flushes the filterbank delay)
local got = 0
while got < spf and not srcDone do
if pendN == 0 then
local b, n = src:read(4096, tmp)
if n <= 0 then
srcDone = true
break
end
tmp = b
pending, pendN, pendOff = b, n, 0
end
local take = math.min(spf - got, pendN)
local pb = pending :: buffer
for i = 0, take - 1 do
local base = (pendOff + i) * inCh * 4
local l = buffer.readf32(pb, base)
local r = if inCh > 1 then buffer.readf32(pb, base + 4) else l
if nch == 1 then
if inCh > 1 then
l = (l + r) * 0.5
end
pcm[1][got + i + 1] = l
else
pcm[1][got + i + 1] = l
pcm[2][got + i + 1] = r
end
end
got += take
pendN -= take
pendOff += take
end
if got == 0 then
if flushed then
break
end
flushed = true -- one silent frame after the input pushes its tail through the filterbank delay
end
for c = 1, nch do
local s = pcm[c]
for i = got + 1, spf do
s[i] = 0
end
end
-- analysis + MDCT per granule and channel
for g = 1, granules do
for c = 1, nch do
local cu, pv = cur[c], prev[c]
local s = pcm[c]
for k = 0, 17 do
Analysis.run(ana[c], s, (g - 1) * 576 + k * 32 + 1, sub, 1)
for sb = 0, 31 do
local v = sub[sb + 1]
if sb % 2 == 1 and k % 2 == 1 then
v = -v -- frequency inversion of the odd subbands
end
cu[sb * 18 + k + 1] = v
end
end
local xr = units[g][c].xr
for sb = 0, 31 do
local o = sb * 18
for i = 1, 18 do
z[i] = pv[o + i]
z[i + 18] = cu[o + i]
end
for k = 0, 17 do
local acc = 0
local mb = k * 36
for i = 1, 36 do
acc += MDCT[mb + i] * z[i]
end
xr[o + k + 1] = acc
end
end
-- alias reduction butterflies (the inverse of the decoder's)
for sb = 1, 31 do
local o = sb * 18
for i = 0, 7 do
local a, b = xr[o - i], xr[o + i + 1]
xr[o - i] = a * CS[i + 1] + b * CA[i + 1]
xr[o + i + 1] = b * CS[i + 1] - a * CA[i + 1]
end
end
prev[c], cur[c] = cu, pv
end
if mode == "joint" then
local x0, x1 = units[g][1].xr, units[g][2].xr
local r2 = sqrt(0.5)
for i = 1, 576 do
local l, r = x0[i], x1[i]
x0[i], x1[i] = (l + r) * r2, (l - r) * r2
end
end
end
-- the frame size
local frameBytes = slotNum // rate
rest += slotNum % rate
local padding = 0
if rest >= rate then
rest -= rate
padding = 1
frameBytes += 1
end
local avail = (frameBytes - 4 - sideBytes) * 8
local left = avail
local nUnits = granules * nch
local done = 0
for g = 1, granules do
for c = 1, nch do
local budget = math.min(4095, left // (nUnits - done))
local u = units[g][c]
rateLoop(u, budget, bounds)
left -= u.plan.bits
done += 1
end
end
-- header
if w.pos + frameBytes + 64 > buffer.len(w.buf) then
local nb = buffer.create(buffer.len(w.buf) * 2 + frameBytes)
buffer.copy(nb, 0, w.buf, 0, w.pos)
w.buf = nb
end
local start = w.pos
put(w, 0x7FF, 11)
put(w, version, 2)
put(w, 1, 2) -- layer III
put(w, 1, 1) -- no CRC
put(w, brIndex, 4)
put(w, srIndex, 2)
put(w, padding, 1)
put(w, 0, 1)
put(w, if mode == "mono" then 3 elseif mode == "joint" then 1 else 0, 2)
put(w, if mode == "joint" then 2 else 0, 2) -- mode extension: mid / side on, intensity off
put(w, 0, 1)
put(w, 1, 1) -- original
put(w, 0, 2)
-- side info
if mpeg1 then
put(w, 0, 9) -- main_data_begin
put(w, 0, if nch == 1 then 5 else 3)
for _ = 1, nch do
put(w, 0, 4) -- scfsi
end
else
put(w, 0, 8)
put(w, 0, if nch == 1 then 1 else 2)
end
for g = 1, granules do
for c = 1, nch do
local u = units[g][c]
local p = u.plan
put(w, p.bits, 12) -- part2_3_length (no scalefactor bits)
put(w, p.bigValues, 9)
put(w, u.gain, 8)
put(w, 0, if mpeg1 then 4 else 9) -- scalefac_compress
put(w, 0, 1) -- window switching off
put(w, p.tables[1], 5)
put(w, p.tables[2], 5)
put(w, p.tables[3], 5)
put(w, p.region0, 4)
put(w, p.region1, 3)
if mpeg1 then
put(w, 0, 1) -- preflag
end
put(w, 0, 1) -- scalefac_scale
put(w, p.c1table, 1)
end
end
-- main data
for g = 1, granules do
for c = 1, nch do
local u = units[g][c]
writeHuffman(w, u.ix, u.sign, u.plan)
end
end
if w.cnt > 0 then
put(w, 0, 8 - w.cnt)
end
-- ancillary padding to the frame size
while w.pos < start + frameBytes do
writeu8(w.buf, w.pos, 0)
w.pos += 1
end
end
if src.err then
return nil, tostring(src.err)
end
local out = buffer.create(w.pos)
buffer.copy(out, 0, w.buf, 0, w.pos)
return out, nil
end
return Encode3
+124 -4
View File
@@ -4,8 +4,9 @@
-- layers I / II / III, mono / stereo / joint stereo / dual channel, CBR / VBR / free format, gapless playback (Xing /
-- Info + LAME delay and padding), exact length and sample-accurate seeking from a frame index built on open, resync
-- past garbage, mono / stereo switches (upmixed), ID3v2.2 / 2.3 / 2.4 and ID3v1 / 1.1 tags (APEv2 skipped).
-- Float output (minimp3's MINIMP3_FLOAT_OUTPUT scalar path). Not supported: encoding (see the mp3 encoder in the
-- audio workstream), MPEG-2 multichannel extension, CRC checks.
-- Float output (minimp3's MINIMP3_FLOAT_OUTPUT scalar path). Encode (ws-audio, audio/mp3/Encode3 + Encode2): layer III
-- (shine-style CBR, MPEG-1 / 2 / 2.5) and layer II (MPEG-1 / 2), mono / stereo / joint stereo, an ID3v2.4 tag from
-- the tags. Not supported: MPEG-2 multichannel extension, CRC checks, VBR / Xing output, encoder resampling.
--
-- local dec = Mp3.open(Bytes.reader(buf)) -- the streaming Types.Decoder (+ layer, version, vbr, delay,
-- local pcm, n = dec:read(4096) -- padding, mpegFrames, id3, :seekFrame(i), :readAll())
@@ -19,6 +20,9 @@ local Header = require("@formats/audio/mp3/Header")
local Index = require("@formats/audio/mp3/Index")
local Tags = require("@formats/audio/mp3/Tags")
local Stream = require("@formats/audio/mp3/Stream")
local Encode3 = require("@formats/audio/mp3/Encode3")
local Encode2 = require("@formats/audio/mp3/Encode2")
local Pcm = require("@formats/codecs/Pcm")
-- ------------------------------------------------------------------------------------------------ contract
local Mp3 = {
@@ -26,8 +30,8 @@ local Mp3 = {
category = "audio",
extensions = { "mp3", "mp2", "mp1", "mpga" },
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = false, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
}
-- ------------------------------------------------------------------------------------------------ types
@@ -35,6 +39,13 @@ export type OpenOptions = {
maxFrames: number?, -- refuse streams longer than this many sample frames per channel (default 2^31)
}
export type Decoder = Stream.Decoder
export type EncodeOptions = {
layer: number?, -- 3 (MP3, default) or 2 (MP2)
bitrate: number?, -- kbps, constant (default 128 for layer III, 192 for layer II; MPEG-2 / 2.5 rates: 64 / 96)
mode: ("stereo" | "joint" | "mono")?, -- default "joint" (mid / side) for stereo layer III, "stereo" for layer II,
-- "mono" for mono input; layer II has no joint stereo (intensity) and writes "joint" as "stereo"
id3: boolean?, -- write an ID3v2.4 tag from the tags (default true when there are tags)
}
-- ------------------------------------------------------------------------------------------------ constants
local MAX_FRAMES = 2 ^ 31
@@ -85,6 +96,115 @@ function Mp3.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
local ID3_FRAMES = {
{ "title", "TIT2" },
{ "artist", "TPE1" },
{ "album", "TALB" },
{ "albumArtist", "TPE2" },
{ "year", "TDRC" },
{ "date", "TDRC" },
{ "track", "TRCK" },
{ "genre", "TCON" },
{ "composer", "TCOM" },
}
-- an ID3v2.4 tag (UTF-8 text frames + a COMM frame) | nil when there is nothing to write
local function id3Tag(tags: { [string]: any }): buffer?
local w = Bytes.writer(256)
local seen: { [string]: boolean } = {}
local function syncsafe(v: number)
w:u8((v // 2097152) % 128)
w:u8((v // 16384) % 128)
w:u8((v // 128) % 128)
w:u8(v % 128)
end
for _, f in ID3_FRAMES do
local v = tags[f[1]]
if type(v) == "string" and #v > 0 and not seen[f[2]] then
seen[f[2]] = true
w:string(f[2])
syncsafe(#v + 1)
w:u16be(0)
w:u8(3) -- UTF-8
w:string(v)
end
end
if type(tags.comment) == "string" and #tags.comment > 0 then
local v = tags.comment
w:string("COMM")
syncsafe(#v + 5)
w:u16be(0)
w:u8(3)
w:string("eng")
w:u8(0) -- empty description
w:string(v)
end
local body = w:finish()
if buffer.len(body) == 0 then
return nil
end
local out = Bytes.writer(buffer.len(body) + 10)
out:string("ID3")
out:u8(4)
out:u8(0)
out:u8(0)
local n = buffer.len(body)
out:u8((n // 2097152) % 128)
out:u8((n // 16384) % 128)
out:u8((n // 128) % 128)
out:u8(n % 128)
out:bytes(body)
return out:finish()
end
-- encode(audioOrDecoder, opts?) -> an MP3 (layer III) or MP2 (layer II) file | nil, Err
function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local src, serr = Pcm.source(value)
if not src then
return nil, serr
end
local o: EncodeOptions = opts or {}
local layer = o.layer or 3
if layer ~= 2 and layer ~= 3 then
return nil, Err.new(Err.ARG, "layer must be 2 or 3")
end
local Enc = if layer == 3 then Encode3 else Encode2
local info = Enc.RATES[src.sampleRate]
if not info then
return nil, Err.new(Err.ARG, string.format("layer %d cannot hold %s Hz (no resampling)", layer, tostring(src.sampleRate)))
end
local mpeg1 = info[1] == 3
local mode = o.mode or (if src.channels == 1 then "mono" elseif layer == 3 then "joint" else "stereo")
if mode ~= "stereo" and mode ~= "joint" and mode ~= "mono" then
return nil, Err.new(Err.ARG, "mode must be stereo, joint or mono")
end
if src.channels > 2 then
return nil, Err.new(Err.ARG, "MPEG audio holds 1 or 2 channels")
end
if layer == 2 and mode == "joint" then
mode = "stereo"
end
local bitrate = o.bitrate or (if mpeg1 then (if layer == 3 then 128 else 192) else (if layer == 3 then 64 else 96))
if not table.find(Enc.BITRATES[if mpeg1 then 1 else 2], bitrate) then
return nil, Err.new(Err.ARG, string.format("%s kbps is not a layer %d bit rate at %d Hz", tostring(bitrate), layer, src.sampleRate))
end
if layer == 2 and mpeg1 then
-- the standard's allowed bit rate / mode combinations
if mode == "mono" and bitrate > 192 then
return nil, Err.new(Err.ARG, "layer II mono is at most 192 kbps")
elseif mode ~= "mono" and (bitrate == 32 or bitrate == 48 or bitrate == 56 or bitrate == 80) then
return nil, Err.new(Err.ARG, "layer II stereo needs 64 kbps or more (and not 80)")
end
end
local prefix = if o.id3 == false then nil else id3Tag(src.tags)
local out, msg = Enc.encode(src :: any, { bitrate = bitrate, mode = mode, prefix = prefix })
if not out then
return nil, Err.new(Err.ARG, tostring(msg))
end
return out, nil
end
-- ------------------------------------------------------------------------------------------------ open
-- the string-valued tag fields (title, artist, album, albumArtist, year, track, disc, genre, composer, comment)
local function stringTags(t: any): { [string]: string }
+3 -3
View File
@@ -82,7 +82,7 @@ local rand = T.rng(12345)
t:eq(Mp3.name, "mp3", "name")
t:eq(Mp3.category, "audio", "category")
t:check(table.find(Mp3.extensions, "mp3") ~= nil and table.find(Mp3.extensions, "mp2") ~= nil, "extensions")
t:check(Mp3.caps.decode and not Mp3.caps.encode and Mp3.open ~= nil, "caps")
t:check(Mp3.caps.decode and Mp3.caps.encode and Mp3.open ~= nil, "caps") -- encode: ws-audio (tests/audio/mp3_encode.luau)
-- ------------------------------------------------------------------------------------------------ decode vs references
for _, c in Cases do
@@ -487,8 +487,8 @@ do
t:check(a and a.kind == "audio" and a.frames == 22050 and a.tags.title == "Test Title", "F.audio.load: " .. tostring(err))
local dec = F.audio.open(data)
t:check(dec and dec.sampleRate == 44100 and dec.frames == 22050, "F.audio.open")
local _, serr = F.save(a, nil, { format = "mp3" })
t:check(serr and serr.code == Err.UNSUPPORTED, "F.save mp3: read-only")
local saved, serr = F.save(a, nil, { format = "mp3" }) -- the layer III encoder (ws-audio)
t:check(saved and F.audio.load(saved).sampleRate == 44100, "F.save mp3: " .. tostring(serr))
end
-- ------------------------------------------------------------------------------------------------ fuzz
+143
View File
@@ -0,0 +1,143 @@
-- audio/mp3 encoder tests (layer III and layer II): every fixture-step encode (sample rates of MPEG-1 / 2 / 2.5,
-- bit rates 24 .. 384 kbps, stereo / joint / mono) decodes in ffmpeg without errors and in LAME, with the expected
-- length and an SNR over a per-bit-rate floor after aligning the decoder delay; our own decoder (the minimp3 port)
-- agrees; streaming from a Decoder gives the same bytes; the ID3v2 tag round-trips; option errors.
-- Fixtures: tools/fixtures/mp3_enc.py.
local T = require("@tests/T")
local Mp3 = require("@formats/audio/mp3")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local Enc = require("@tests/gen/mp3_enc")
local Sigs = require("@tests/gen/mp3_enc_sigs")
local t = T.suite("mp3_encode")
local function audio(name: string): Types.Audio
local s = Sigs[name]
local a = Types.Audio.new(s.rate, s.channels, s.frames)
buffer.writestring(a.samples, 0, s.data)
return a
end
-- SNR of y (delayed by lag frames) against x over frames [from, to)
local function snr(x: Types.Audio, y: Types.Audio, lag: number, from: number, to: number): number
local ch = x.channels
local sig, er = 0, 0
for i = from, to - 1 do
local j = i + lag
if j >= 0 and j < y.frames then
for c = 0, ch - 1 do
local a = buffer.readf32(x.samples, (i * ch + c) * 4)
local b = buffer.readf32(y.samples, (j * y.channels + math.min(c, y.channels - 1)) * 4)
sig += a * a
er += (a - b) * (a - b)
end
end
end
if er == 0 then
return 200
end
return if sig > 0 then 10 * math.log10(sig / er) else -200
end
-- the SNR floors (dB) on the music / sine / sweep signals, by layer and bit rate per channel
local function floorFor(e: any): number
if string.find(e.name, "silence", 1, true) then
return -math.huge
elseif string.find(e.name, "noise", 1, true) then
return 8 -- white noise has no spectral structure to exploit
end
local perCh = e.bitrate / (if e.mode == "mono" then 1 else 2)
local nyquistBand = e.rate <= 16000 -- low rates lose little to the encoder's band limit
if e.layer == 3 then
if perCh >= 64 then
return 14
elseif perCh >= 32 or nyquistBand then
return 8
end
return 4
end
if perCh >= 96 then
return 14
elseif perCh >= 48 then
return 8
end
return 4
end
t:check(Mp3.caps.encode and Mp3.encode ~= nil, "caps.encode")
local minByLayer = { [2] = math.huge, [3] = math.huge }
for _, e in Enc do
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
t:check(e.ffRate == e.rate, e.name .. ": rate")
t:check(e.ffChannels == (if e.mode == "mono" then 1 else 2), e.name .. ": channels")
-- the stream covers the input: the encoder adds one frame of flush and the filterbank delay
local spf = if e.layer == 3 and e.rate < 32000 then 576 else 1152
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 3 * spf, string.format("%s: %d decoded frames for %d", e.name, e.ffFrames, e.frames))
local f = floorFor(e)
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB (floor %g)", e.name, e.ffSnr, f))
if e.lameSnr then
t:check(e.lameSnr >= f, string.format("%s: LAME SNR %.1f dB (floor %g)", e.name, e.lameSnr, f))
end
if f > -math.huge then
minByLayer[e.layer] = math.min(minByLayer[e.layer], e.ffSnr)
end
-- our decoder agrees with ffmpeg
local x = audio(e.sig)
local y, err = Mp3.decode(Bytes.reader(buffer.fromstring(e.data)))
t:check(y, e.name .. ": our decoder: " .. tostring(err))
if e.channels == y.channels then
local s = snr(x, y, e.ffLag, 200, x.frames)
t:check(s >= f and math.abs(s - e.ffSnr) < 3, string.format("%s: our decoder %.1f dB, ffmpeg %.1f dB", e.name, s, e.ffSnr))
end
end
t:note("%d encodes: ffmpeg + LAME decode them; lowest SNR layer III %.1f dB, layer II %.1f dB", #Enc, minByLayer[3], minByLayer[2])
-- streaming, tags, defaults
do
local a = audio("music_2_44100")
local f1 = Mp3.encode(a, { bitrate = 128 }) :: buffer
local f2 = Mp3.encode(Pcm.audioDecoder(a), { bitrate = 128 }) :: buffer
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
a.tags = { title = "Ünïcode title", artist = "An Artist", album = "Album", track = "4", comment = "made here" }
local f3 = Mp3.encode(a, { layer = 2 }) :: buffer
t:eq(buffer.readstring(f3, 0, 3), "ID3", "an ID3v2 tag in front")
local back = Mp3.open(Bytes.reader(f3)) :: any
t:eq(back.tags.title, "Ünïcode title", "title")
t:eq(back.tags.artist, "An Artist", "artist")
t:eq(back.tags.album, "Album", "album")
t:eq(back.tags.track, "4", "track")
t:eq(back.layer, 2, "layer II by default bit rate 192")
local f4 = Mp3.encode(a, { id3 = false }) :: buffer
t:check(buffer.readu8(f4, 0) == 0xFF, "id3 = false")
local m = Types.Audio.new(22050, 1, 3000)
local fm = Mp3.encode(m) :: buffer
local dm = Mp3.open(Bytes.reader(fm)) :: any
t:check(dm.channels == 1 and dm.sampleRate == 22050, "mono MPEG-2 default")
local empty = Mp3.encode(Types.Audio.new(44100, 2, 0)) :: buffer
t:check(empty and buffer.len(empty) > 0, "empty input: one flush frame")
end
-- errors
do
local function code(v: any, o: any): string?
local _, e = Mp3.encode(v, o)
return e and e.code
end
local a = Types.Audio.new(44100, 2, 100)
t:eq(code({}, nil), Err.ARG, "junk")
t:eq(code(a, { layer = 1 }), Err.ARG, "layer I")
t:eq(code(a, { bitrate = 100 }), Err.ARG, "100 kbps")
t:eq(code(a, { mode = "dual" }), Err.ARG, "dual mode")
t:eq(code(Types.Audio.new(44000, 2, 10), nil), Err.ARG, "44 kHz")
t:eq(code(Types.Audio.new(8000, 2, 10), { layer = 2 }), Err.ARG, "layer II at 8 kHz")
t:eq(code(Types.Audio.new(44100, 3, 10), nil), Err.ARG, "3 channels")
t:eq(code(a, { layer = 2, bitrate = 32 }), Err.ARG, "layer II stereo at 32 kbps")
t:eq(code(Types.Audio.new(44100, 1, 10), { layer = 2, bitrate = 320 }), Err.ARG, "layer II mono at 320 kbps")
end
-- (the decoder's fuzz pass is in tests/audio/mp3.luau; the encoders take no untrusted bytes)
t:done()
+16
View File
@@ -144,3 +144,19 @@ do
print(string.format("%-8s %-30s %8.0fx real time", runtime, "", 10 / dt))
end
end
-- mp3 / mp2 encode (ws-audio): 10 s of 44.1 kHz stereo tones
do
local M3 = require("@formats/audio/mp3")
local n = 441000
local a = Types.Audio.new(44100, 2, n)
for i = 0, n - 1 do
buffer.writef32(a.samples, i * 8, math.sin(i * 0.031) * 0.4 + math.sin(i * 0.21) * 0.1)
buffer.writef32(a.samples, i * 8 + 4, math.sin(i * 0.017) * 0.3)
end
for _, cfg in { { layer = 3, bitrate = 128 }, { layer = 2, bitrate = 192 } } do
local dt = bench(string.format("mp%d encode %d kbps (10 s st)", cfg.layer, cfg.bitrate), 2, n * 8, function()
M3.encode(a, cfg)
end)
print(string.format("%-8s %-30s %8.1fx real time", runtime, "", 10 / dt))
end
end
+20 -2
View File
@@ -133,9 +133,13 @@ def ffdecode(ctx, data, fmt=None, ext="bin", extra=None):
return rate, ch, from_f32le(raw)
def snr(ref, test, ch, max_lag=0, skip=0):
def snr(ref, test, ch, max_lag=0, skip=0, hints=None):
"""SNR (dB) of test vs ref over their common frames (after `skip` frames), best lag in [-max_lag, max_lag]
(test[i + lag] ~ ref[i]). Returns (snr, lag)."""
(test[i + lag] ~ ref[i]). hints: lags tried first; the search runs only when none gives 10 dB. Returns (snr, lag)."""
if hints:
got = max(snr(ref, test, ch, 0, skip, None) if h == 0 else _snr_at(ref, test, ch, h, skip) for h in hints)
if got[0] >= 10:
return got
best = (-1e9, 0)
nref, ntest = len(ref) // ch, len(test) // ch
lags = range(-max_lag, max_lag + 1) if max_lag else [0]
@@ -170,6 +174,20 @@ def snr(ref, test, ch, max_lag=0, skip=0):
return best
def _snr_at(ref, test, ch, lag, skip):
sig = err = 0.0
nref, ntest = len(ref) // ch, len(test) // ch
for i in range(skip, nref):
j = i + lag
if 0 <= j < ntest:
for c in range(ch):
a = ref[i * ch + c]
e = a - test[j * ch + c]
sig += a * a
err += e * e
return (200.0 if err == 0 else (10 * math.log10(sig / err) if sig > 0 else -200.0), lag)
def luau_bin():
p = shutil.which("luau")
if p:
+80
View File
@@ -0,0 +1,80 @@
# MP3 / MP2 encoder fixtures (tools/fixtures.py): our layer III and layer II encoders run in the fixture step
# (tools/fixtures/_audio.py encode_luau_many) on test signals at every MPEG sample rate group, several bit rates and
# modes; ffmpeg (mp3float / mp2float) and LAME (`lame --decode`, layer III) decode the files. Recorded per file: ffmpeg's
# error output (must be empty), the decoded frame counts, and the SNR against the input after aligning the decoder delay.
# tests/gen/mp3_enc.luau { name, sig, layer, bitrate, mode, rate, channels, frames, ffErrors, ffFrames, ffSnr,
# ffLag, lameFrames?, lameSnr?, size, data }
# tests/gen/mp3_enc_sigs.luau { [sig] = { rate, channels, frames, data (f32le) } }
import importlib.util, io, os, wave
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
A = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(A)
INPUTS = ["src/Formats/audio/mp3", "src/Formats/codecs/Pcm"]
def generate(ctx):
sigs = {}
def sig(kind, ch, secs, rate):
name = "%s_%d_%d" % (kind, ch, rate)
if name not in sigs:
frames = int(secs * rate)
sigs[name] = (A.from_f32le(A.f32le(A.signal(kind, rate, ch, frames, seed=ch * 7 + rate % 97))), rate, ch)
return name
cases = [] # name, sig, layer, bitrate, mode
for kind in ("music", "sine", "sweep"):
for br in (64, 128, 192, 320):
cases.append(("l3-%s-44k-%d-joint" % (kind, br), sig(kind, 2, 1.0, 44100), 3, br, "joint"))
cases.append(("l3-music-44k-128-stereo", sig("music", 2, 1.0, 44100), 3, 128, "stereo"))
cases.append(("l3-music-48k-160-joint", sig("music", 2, 1.0, 48000), 3, 160, "joint"))
cases.append(("l3-music-32k-96-mono", sig("music", 1, 1.0, 32000), 3, 96, "mono"))
cases.append(("l3-noise-44k-128-mono", sig("noise", 1, 0.6, 44100), 3, 128, "mono"))
cases.append(("l3-music-22k-64-joint", sig("music", 2, 1.0, 22050), 3, 64, "joint"))
cases.append(("l3-music-24k-96-stereo", sig("music", 2, 1.0, 24000), 3, 96, "stereo"))
cases.append(("l3-sweep-16k-48-mono", sig("sweep", 1, 1.0, 16000), 3, 48, "mono"))
cases.append(("l3-music-11k-32-mono", sig("music", 1, 1.0, 11025), 3, 32, "mono"))
cases.append(("l3-music-8k-24-joint", sig("music", 2, 1.0, 8000), 3, 24, "joint"))
cases.append(("l3-music-12k-64-stereo", sig("music", 2, 1.0, 12000), 3, 64, "stereo"))
cases.append(("l3-silence-44k-128", sig("silence", 2, 0.3, 44100), 3, 128, "joint"))
cases.append(("l3-loud-44k-128", sig("loud", 2, 0.5, 44100), 3, 128, "joint"))
for kind in ("music", "sine", "sweep"):
for br in (128, 192, 256, 384):
cases.append(("l2-%s-44k-%d" % (kind, br), sig(kind, 2, 1.0, 44100), 2, br, "stereo"))
cases.append(("l2-music-48k-192", sig("music", 2, 1.0, 48000), 2, 192, "stereo"))
cases.append(("l2-music-32k-mono-96", sig("music", 1, 1.0, 32000), 2, 96, "mono"))
cases.append(("l2-music-44k-mono-64", sig("music", 1, 1.0, 44100), 2, 64, "mono"))
cases.append(("l2-music-24k-96", sig("music", 2, 1.0, 24000), 2, 96, "stereo"))
cases.append(("l2-sweep-16k-mono-48", sig("sweep", 1, 1.0, 16000), 2, 48, "mono"))
cases.append(("l2-loud-44k-192", sig("loud", 2, 0.5, 44100), 2, 192, "stereo"))
ctx.emit("mp3_enc_sigs", {n: {"rate": r, "channels": c, "frames": len(x) // c, "data": A.f32le(x)} for n, (x, r, c) in sigs.items()})
jobs = [(c[1], '{ layer = %d, bitrate = %d, mode = "%s" }' % (c[2], c[3], c[4])) for c in cases]
outs = A.encode_luau_many(ctx, "audio/mp3", jobs, "mp3_enc_sigs")
emitted = []
for (name, sigmod, layer, br, mode), data in zip(cases, outs):
samples, rate, ch = sigs[sigmod]
ext = "mp3" if layer == 3 else "mp2"
errs = A.tool(ctx, "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}", "-f", "null", "-"], data, ext).decode(errors="replace")
frate, fch, dec = A.ffdecode(ctx, data, ext=ext)
dch = fch
ref = samples if dch == ch else [samples[i // dch] for i in range(len(samples) // ch * dch)]
snr, lag = A.snr(ref, dec, dch, max_lag=1600, skip=200, hints=[1057 if layer == 3 else 481]) if dch == ch else (-99.0, 0)
e = {"name": name, "sig": sigmod, "layer": layer, "bitrate": br, "mode": mode, "rate": rate, "channels": ch,
"frames": len(samples) // ch, "ffErrors": errs, "ffFrames": len(dec) // max(1, fch), "ffRate": frate,
"ffChannels": fch, "ffSnr": snr, "ffLag": lag, "size": len(data), "data": data}
if layer == 3:
wavb = A.tool(ctx, "lame", ["lame", "--quiet", "--decode", "{in}", "{out}"], data, "mp3", "wav")
with wave.open(io.BytesIO(wavb)) as wf:
lch, raw = wf.getnchannels(), wf.readframes(wf.getnframes())
lame = [int.from_bytes(raw[i:i + 2], "little", signed=True) / 32768 for i in range(0, len(raw), 2)]
if lch == ch:
ls, ll = A.snr(samples, lame, ch, max_lag=1600, skip=200, hints=[1057, 528, 529])
e["lameSnr"], e["lameLag"] = ls, ll
e["lameFrames"] = len(lame) // lch
emitted.append(e)
print(" mp3_enc: %-28s %5d bytes ffmpeg %.1f dB (lag %d)%s%s" % (name, len(data), snr, lag,
(" lame %.1f dB" % e["lameSnr"]) if "lameSnr" in e else "", (" ERR " + errs.strip()[:60]) if errs.strip() else ""))
ctx.emit("mp3_enc", emitted)