ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9c553b9d1a
commit
434a304315
283 files changed
+1877
-1660
No files matched your search
@@ -1,13 +0,0 @@
|
||||
--!strict
|
||||
-- codecs/RangeCoder: the RFC 6716 range coder (libopus celt/entdec.c + entenc.c, BSD: licenses/libopus-BSD.txt):
|
||||
-- symbols range-coded from the front of a frame, raw bits from its back. Opus (CELT and SILK) uses it; LZMA, VP8 and
|
||||
-- CABAC have their own (different) binary coders. Moved here from audio/opus by the cleanup pass (same code).
|
||||
--
|
||||
-- local RD = require("@formats/codecs/RangeCoder/Decoder") -- RD.new(buf, base, size), RD.icdf / bits / uint ...
|
||||
-- local RE = require("@formats/codecs/RangeCoder/Encoder") -- RE.new(size), RE.icdf / bits / uint, RE.done
|
||||
local RangeCoder = {
|
||||
Decoder = require("@formats/codecs/RangeCoder/Decoder"),
|
||||
Encoder = require("@formats/codecs/RangeCoder/Encoder"),
|
||||
}
|
||||
|
||||
return RangeCoder
|
||||
@@ -1,14 +1,14 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Aac/Decode: the AAC-LC raw_data_block decoder (ISO/IEC 14496-3 subpart 4): SCE / CPE / LFE / DSE / PCE /
|
||||
-- codecs/audio/Aac/Decode: the AAC-LC raw_data_block decoder (ISO/IEC 14496-3 subpart 4): SCE / CPE / LFE / DSE / PCE /
|
||||
-- FIL / END elements, ics_info with window grouping, section data, scalefactors (with noise energies and intensity
|
||||
-- positions), pulse data, TNS, the eleven spectral codebooks with escapes, dequantisation, M/S and intensity stereo,
|
||||
-- perceptual noise substitution (FFmpeg's LCG so the noise matches it), the filterbank (sine / KBD windows, the four
|
||||
-- window sequences, IMDCT through codecs/Mdct, overlap-add). FIL extension payloads (SBR / PS) are reported to a hook
|
||||
-- window sequences, IMDCT through codecs/audio/Mdct, overlap-add). FIL extension payloads (SBR / PS) are reported to a hook
|
||||
-- and otherwise skipped. Written from the standard; no third-party code. Never throws: returns nil, message.
|
||||
local Mdct = require("@formats/codecs/Mdct")
|
||||
local Tables = require("@formats/codecs/Aac/Tables")
|
||||
local Mdct = require("@formats/codecs/audio/Mdct")
|
||||
local Tables = require("@formats/codecs/audio/Aac/Tables")
|
||||
|
||||
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
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Aac/Tables: the AAC-LC tables of ISO/IEC 14496-3 (subpart 4, Annex 4.A): the scalefactor and the eleven
|
||||
-- codecs/audio/Aac/Tables: the AAC-LC tables of ISO/IEC 14496-3 (subpart 4, Annex 4.A): the scalefactor and the eleven
|
||||
-- spectral Huffman codebooks (code, length per symbol), the scalefactor band offsets per sampling rate (1024 and 128
|
||||
-- lines) and the TNS band limits. Transcribed from FFmpeg libavcodec/aactab.c (tables of the standard, no code;
|
||||
-- see licenses/NOTICE.md). Generated; do not edit.
|
||||
@@ -1,15 +1,15 @@
|
||||
--!strict
|
||||
-- codecs/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): the AudioSpecificConfig (with a program
|
||||
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): the AudioSpecificConfig (with a program
|
||||
-- config element, explicit and backward-compatible SBR signalling), ADTS headers, and raw_data_block decoding
|
||||
-- (codecs/Aac/Decode). A codec (layer 1): audio/m4a, audio/aac (ADTS), and later MP4 / MKV / MPEG-TS video build on it.
|
||||
-- Written from the standard (tables: codecs/Aac/Tables); no third-party code.
|
||||
-- (codecs/audio/Aac/Decode). A codec (layer 1): audio/m4a, audio/aac (ADTS), and later MP4 / MKV / MPEG-TS video build on it.
|
||||
-- Written from the standard (tables: codecs/audio/Aac/Tables); no third-party code.
|
||||
-- Supports: AAC-LC (object type 2) 1 - 8 channels, all sampling rates, long / short / start / stop windows, sine and KBD
|
||||
-- windows, M/S, intensity stereo, PNS, TNS, pulses. HE-AAC (SBR, PS: object types 5 / 29, or implicit SBR payloads)
|
||||
-- decodes its AAC-LC core at the core rate; `decoder.onExtension` receives every FIL extension payload (the hook for a
|
||||
-- later SBR / PS module), `decoder.sbr` says whether one was seen. Not supported: AAC Main prediction, LTP, SSR gain
|
||||
-- control, coupling channels, 960-sample frames, ER / USAC object types (UNSUPPORTED).
|
||||
--
|
||||
-- local Aac = require("@formats/codecs/Aac")
|
||||
-- local Aac = require("@formats/codecs/audio/Aac")
|
||||
-- local dec, err = Aac.decoder(asc) -- an AudioSpecificConfig (buffer) or { objectType, sfIndex, channels }
|
||||
-- local frames, err = dec:decode(buf, offset, length) -- one raw_data_block -> dec.pcm (frames * channels floats)
|
||||
-- local h = Aac.adts(buf, offset, limit) -- an ADTS header: { headerSize, frameSize, objectType, sfIndex, ... }
|
||||
@@ -17,7 +17,7 @@
|
||||
-- Never throws: bad data returns nil, Err.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bits = require("@formats/core/Bits")
|
||||
local Decode = require("@formats/codecs/Aac/Decode")
|
||||
local Decode = require("@formats/codecs/audio/Aac/Decode")
|
||||
|
||||
local Aac = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Alac/Decode: the ALAC frame decoder: elements SCE / CPE / LFE / DSE / FIL / END, the adaptive Golomb-Rice
|
||||
-- codecs/audio/Alac/Decode: the ALAC frame decoder: elements SCE / CPE / LFE / DSE / FIL / END, the adaptive Golomb-Rice
|
||||
-- residuals with zero runs, the adaptive FIR predictor (orders 0, 31 and 1 .. 30), stereo unmixing, shifted low bytes,
|
||||
-- uncompressed (escape) frames, 16 / 20 / 24 / 32-bit samples. Written from the ALAC format as published by Apple
|
||||
-- (ALACDecoder / ag_dec / dp_dec, Apache 2.0; behaviour, not code). Never throws: returns nil, message.
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Alac/Encode: an ALAC frame encoder: per element (SCE / CPE) the stereo mix (mixBits 2, the best mixRes of
|
||||
-- codecs/audio/Alac/Encode: an ALAC frame encoder: per element (SCE / CPE) the stereo mix (mixBits 2, the best mixRes of
|
||||
-- 0 .. 4 by first-difference energy), per channel LPC coefficients (Levinson-Durbin on a windowed autocorrelation,
|
||||
-- orders 4 and 8 tried, quantised with denShift 9), residuals through the same adaptive FIR the decoder runs, the
|
||||
-- adaptive Golomb-Rice code with zero runs; an uncompressed (escape) frame when that is smaller. 16 / 20 / 24-bit
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
-- codecs/Alac: Apple Lossless (ALAC) decoder and encoder, the codec inside m4a / mov / caf. A codec (layer 1).
|
||||
-- codecs/audio/Alac: Apple Lossless (ALAC) decoder and encoder, the codec inside m4a / mov / caf. A codec (layer 1).
|
||||
-- Written from the ALAC format as Apple published it (ALACSpecificConfig, the element syntax, adaptive Golomb-Rice
|
||||
-- residuals, the adaptive FIR predictor); no third-party code. 1 - 8 channels, 16 / 20 / 24 / 32-bit decode, 16 / 20 /
|
||||
-- 24-bit encode. Channels come out (and go in) in WAV order (ALAC stores C L R ... for 3+ channels).
|
||||
--
|
||||
-- local Alac = require("@formats/codecs/Alac")
|
||||
-- local Alac = require("@formats/codecs/audio/Alac")
|
||||
-- local cfg, err = Alac.config(cookie) -- the 24-byte ALACSpecificConfig (frma / alac atoms skipped)
|
||||
-- local dec = Alac.decoder(cfg)
|
||||
-- local frames, err = dec:decode(buf, offset, length) -- one packet -> dec.pcm (interleaved integers, WAV order)
|
||||
@@ -15,8 +15,8 @@
|
||||
-- Never throws: bad data returns nil, Err (CORRUPT / TRUNCATED / UNSUPPORTED), bad arguments Err.ARG.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Decode = require("@formats/codecs/Alac/Decode")
|
||||
local Encode = require("@formats/codecs/Alac/Encode")
|
||||
local Decode = require("@formats/codecs/audio/Alac/Decode")
|
||||
local Encode = require("@formats/codecs/audio/Alac/Encode")
|
||||
|
||||
local Alac = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Mdct/Fft: mixed-radix complex FFT of any size (radix 4, 2, 3, 5 butterflies, a generic odd-radix pass for
|
||||
-- codecs/audio/Mdct/Fft: mixed-radix complex FFT of any size (radix 4, 2, 3, 5 butterflies, a generic odd-radix pass for
|
||||
-- other primes), self-sorting Stockham passes ping-ponging between the data and a scratch array (the FFTPACK /
|
||||
-- pocketfft pass structure, written from the textbook decimation-in-frequency derivation; no third-party code).
|
||||
-- Data are separate real / imaginary arrays, 1-based. Never throws.
|
||||
@@ -1,8 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Mdct: the MDCT / IMDCT of any window length N divisible by 4 (N / 2 coefficients), through an N / 4-point
|
||||
-- complex FFT (codecs/Mdct/Fft, mixed radix: AAC 2048 / 256, Vorbis 64 .. 8192, CELT 1920 / 240 / ...), plus the
|
||||
-- codecs/audio/Mdct: the MDCT / IMDCT of any window length N divisible by 4 (N / 2 coefficients), through an N / 4-point
|
||||
-- complex FFT (codecs/audio/Mdct/Fft, mixed radix: AAC 2048 / 256, Vorbis 64 .. 8192, CELT 1920 / 240 / ...), plus the
|
||||
-- DCT-IV, a plain FFT and the sine / KBD / Vorbis windows. Shared by AAC, Vorbis and Opus (decoders and encoders).
|
||||
-- Written from the textbook definitions (MDCT = DCT-IV of the folded input; DCT-IV through a half-size complex FFT
|
||||
-- with pre / post twiddles); no third-party code.
|
||||
@@ -15,7 +15,7 @@
|
||||
-- are Luau arrays; `xi` / `yi` are the 1-based index of element 0, coefficient arrays may be strided (CELT
|
||||
-- interleaves its short blocks).
|
||||
--
|
||||
-- local Mdct = require("@formats/codecs/Mdct")
|
||||
-- local Mdct = require("@formats/codecs/audio/Mdct")
|
||||
-- local plan = Mdct.new(2048) -- nil when N is not a multiple of 4
|
||||
-- Mdct.forward(plan, x, 1, X, 1) -- 2048 samples -> 1024 coefficients
|
||||
-- Mdct.inverse(plan, X, 1, 1, y, 1, 2 / 2048) -- 1024 coefficients -> 2048 samples
|
||||
@@ -23,7 +23,7 @@
|
||||
--
|
||||
-- Never throws: bad sizes return nil from new(); the transforms trust the plan and the array ranges (the caller
|
||||
-- sizes them).
|
||||
local Fft = require("@formats/codecs/Mdct/Fft")
|
||||
local Fft = require("@formats/codecs/audio/Mdct/Fft")
|
||||
|
||||
local cos, sin, pi, sqrt = math.cos, math.sin, math.pi, math.sqrt
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Pcm/Convert: raw sample formats <-> interleaved f32 (the hot loops of codecs/Pcm). Integers map to
|
||||
-- codecs/audio/Pcm/Convert: raw sample formats <-> interleaved f32 (the hot loops of codecs/audio/Pcm). Integers map to
|
||||
-- v / 2^(bits-1) (as ffmpeg does); encoding rounds to nearest and clips. G.711 A-law / mu-law per ITU-T G.711 (the
|
||||
-- classic Sun g711.c algorithm, written from the spec).
|
||||
--
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Pcm: PCM plumbing shared by audio formats: raw sample formats, streaming Decoders, encoder input sources.
|
||||
-- codecs/audio/Pcm: PCM plumbing shared by audio formats: raw sample formats, streaming Decoders, encoder input sources.
|
||||
-- An encoder accepts a Types.Audio (fully decoded) or a Types.Decoder (streamed); Pcm.source turns either into one
|
||||
-- pull interface, so encoders stream in blocks without caring which they got. Uncompressed containers (aiff, au,
|
||||
-- voc, ...) get their Decoder from Pcm.rawDecoder (over a sample region of the file) or Pcm.audioDecoder.
|
||||
@@ -10,7 +10,7 @@
|
||||
-- local dec = Pcm.rawDecoder(buf, offset, frames, channels, rate, "s16be", tags?) -- a Types.Decoder
|
||||
-- local dec = Pcm.audioDecoder(audio) -- a Types.Decoder over a decoded Audio
|
||||
-- local raw = Pcm.encode(src, "s24le", writer) -- every frame of src appended to a core/Bytes Writer
|
||||
-- Pcm.Convert.decode / encode / SIZE / BITS -- the raw sample loops (codecs/Pcm/Convert)
|
||||
-- Pcm.Convert.decode / encode / SIZE / BITS -- the raw sample loops (codecs/audio/Pcm/Convert)
|
||||
-- local s16 = Pcm.toInt(x, 16) -- round, clip to [-32768, 32767]
|
||||
-- local x = Pcm.fromInt(s16, 16) -- s / 32768
|
||||
--
|
||||
@@ -19,7 +19,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Convert = require("@formats/codecs/Pcm/Convert")
|
||||
local Convert = require("@formats/codecs/audio/Pcm/Convert")
|
||||
|
||||
local Pcm = {}
|
||||
Pcm.Convert = Convert
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Resample: audio sample-rate conversion by band-limited (Kaiser-windowed sinc) interpolation, streaming or
|
||||
-- codecs/audio/Resample: audio sample-rate conversion by band-limited (Kaiser-windowed sinc) interpolation, streaming or
|
||||
-- whole-buffer, any channel count, interleaved f32 (the Types.Audio / Decoder layout). Used by the Opus, MP3 and MP2
|
||||
-- encoders (their input rates are fixed) and ops/AudioOps.resample. New in formats-luau (the textbook polyphase
|
||||
-- filter; the kernel is the one the Opus encoder carried).
|
||||
@@ -22,7 +22,7 @@
|
||||
-- local rs = Resample.new(44100, 48000, 2) -- nil, message for bad rates
|
||||
-- local b, n = rs:process(chunk, chunkFrames) -- the output frames this input completes
|
||||
-- local b2, n2 = rs:flush() -- the rest (the input end padded with zeros)
|
||||
-- local src2 = Resample.source(pcmSource, 48000) -- a codecs/Pcm Source at the new rate
|
||||
-- local src2 = Resample.source(pcmSource, 48000) -- a codecs/audio/Pcm Source at the new rate
|
||||
--
|
||||
-- Never throws: bad arguments return nil, message (the api turns them into Err.ARG).
|
||||
local Resample = {}
|
||||
@@ -322,7 +322,7 @@ function Resample.buffer(src: buffer, frames: number, channels: number, from: nu
|
||||
return out, got
|
||||
end
|
||||
|
||||
-- source(src, to, opts?) -> a pull source ({ sampleRate, channels, frames, tags, read, err }, the codecs/Pcm Source
|
||||
-- source(src, to, opts?) -> a pull source ({ sampleRate, channels, frames, tags, read, err }, the codecs/audio/Pcm Source
|
||||
-- shape) reading src (any such source: Pcm.source(audioOrDecoder)) at the rate `to` | nil, message
|
||||
function Resample.source(src: any, to: number, opts: Options?): (any, string?)
|
||||
if type(src) ~= "table" or type(src.read) ~= "function" then
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
--!strict
|
||||
-- codecs/SoundFont/Fallback: the built-in General MIDI set used when no SoundFont is given. A generated SoundFont
|
||||
-- codecs/audio/SoundFont/Fallback: the built-in General MIDI set used when no SoundFont is given. A generated SoundFont
|
||||
-- (the same shape Parse returns, so the same synth plays it): single-cycle sine / triangle / square / saw waves and a
|
||||
-- looped noise sample, one preset per GM family of 8 programs (piano, chromatic percussion, organ, guitar, bass,
|
||||
-- strings, ensemble, brass, reed, pipe, lead, pad, synth fx, ethnic, percussive, sound fx) with family envelopes,
|
||||
-- filters and vibrato, plus a drum kit on bank 128 (pitch-swept sine kicks and toms, filtered noise snares, hats with
|
||||
-- an exclusive class, cymbals). Written for this library (no third-party code).
|
||||
local Parse = require("@formats/codecs/SoundFont/Parse")
|
||||
local Parse = require("@formats/codecs/audio/SoundFont/Parse")
|
||||
|
||||
local Fallback = {}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/SoundFont/Parse: SoundFont 2.0x (sfbk RIFF) reader. The INFO list, the sdta samples (smpl 16-bit, + sm24
|
||||
-- codecs/audio/SoundFont/Parse: SoundFont 2.0x (sfbk RIFF) reader. The INFO list, the sdta samples (smpl 16-bit, + sm24
|
||||
-- when present) as one f32 buffer, and the pdta "hydra" (phdr pbag pmod pgen inst ibag imod igen shdr), flattened
|
||||
-- into playable regions per preset: the instrument global zone, the preset global zone and the preset zone's
|
||||
-- generators merged the way TinySoundFont (tsf.h v0.9, MIT, (c) Bernhard Schelling; licenses/TinySoundFont-LICENSE.txt)
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/SoundFont/Synth: a SoundFont 2 sample synthesizer with MIDI channels. A port of TinySoundFont (tsf.h v0.9,
|
||||
-- codecs/audio/SoundFont/Synth: a SoundFont 2 sample synthesizer with MIDI channels. A port of TinySoundFont (tsf.h v0.9,
|
||||
-- MIT, (c) 2017-2025 Bernhard Schelling, based on SFZero (c) 2012 Steve Folta; licenses/TinySoundFont-LICENSE.txt):
|
||||
-- voices with the DAHDSR volume envelope (exponential decay / release), the modulation envelope to pitch and filter
|
||||
-- cutoff, the modulation and vibrato LFOs (triangle) to pitch / filter / volume, a resonant biquad low-pass, linear
|
||||
+5
-5
@@ -1,11 +1,11 @@
|
||||
--!strict
|
||||
-- codecs/SoundFont: SoundFont 2 banks and the sample synth that plays MIDI through them. A codec (layer 1) shared by
|
||||
-- codecs/audio/SoundFont: SoundFont 2 banks and the sample synth that plays MIDI through them. A codec (layer 1) shared by
|
||||
-- music/sf2 (the file format) and music/midi (rendering). The synth is a port of TinySoundFont (tsf.h v0.9, MIT,
|
||||
-- (c) 2017-2025 Bernhard Schelling; licenses/TinySoundFont-LICENSE.txt); see Parse / Synth for what is covered.
|
||||
-- Without a bank, fallback() is a generated General MIDI set of simple waveforms (sine / triangle / square / saw /
|
||||
-- noise) with a drum kit.
|
||||
--
|
||||
-- local SoundFont = require("@formats/codecs/SoundFont")
|
||||
-- local SoundFont = require("@formats/codecs/audio/SoundFont")
|
||||
-- local sf, err = SoundFont.parse(buf) -- buffer | string | core/Bytes Reader -> SoundFont | nil, Err
|
||||
-- local sf = SoundFont.fallback() -- the built-in waveform set
|
||||
-- local s = SoundFont.synth(sf, 44100, { gain = 0.5 }) -- a synth State; SoundFont.Synth.midi(s, 0x90, 60, 100)
|
||||
@@ -14,9 +14,9 @@
|
||||
-- Never throws: bad data returns nil, Err.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Parse = require("@formats/codecs/SoundFont/Parse")
|
||||
local Synth = require("@formats/codecs/SoundFont/Synth")
|
||||
local Fallback = require("@formats/codecs/SoundFont/Fallback")
|
||||
local Parse = require("@formats/codecs/audio/SoundFont/Parse")
|
||||
local Synth = require("@formats/codecs/audio/SoundFont/Synth")
|
||||
local Fallback = require("@formats/codecs/audio/SoundFont/Fallback")
|
||||
|
||||
local SoundFont = {}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
--!strict
|
||||
-- codecs/audio/Stream: the streaming Decoder (Types.Decoder: read / seek / position) over a packetised codec, the part
|
||||
-- every packet-based audio format repeated: decode packets in order into a pending float block, return frames in
|
||||
-- read()-sized pieces, drop the encoder priming at the start and the padding at the end (gapless), and seek by
|
||||
-- finding the packet holding the target (with codec pre-roll) and dropping up to it. AAC (ADTS), CAF and M4A use it;
|
||||
-- formats with their own seeking (Opus granule bisection, FLAC, MP3, Vorbis) keep their streams. New in formats-luau
|
||||
-- (the loop the three formats carried).
|
||||
--
|
||||
-- local dec, err = Stream.packets(dec, { -- dec: the Decoder table with its fields (sampleRate, channels,
|
||||
-- count = n, -- frames, duration, bitrate, tags, ...); read / seek / position
|
||||
-- start = function(i) return ... end, -- are added. start(i): packet i's first frame (absolute, before
|
||||
-- decode = function(i) return n, err end,-- trimming); decode(i) -> frames | nil, Err, the samples in pcm()
|
||||
-- pcm = function() return floats end, -- (interleaved, 1-based) until the next decode
|
||||
-- reset = function() end, -- forget codec state (before a seek)
|
||||
-- skip = priming, stop = lastFrame, -- output = absolute frames [skip, stop)
|
||||
-- preroll = 1, -- packets decoded before the one holding a seek target (AAC: 1)
|
||||
-- })
|
||||
--
|
||||
-- A decode failure ends the stream and is kept in dec.error (Pcm.drain reports it); a failing first packet is
|
||||
-- returned by packets() as the open error. Never throws.
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local Stream = {}
|
||||
|
||||
export type Spec = {
|
||||
count: number,
|
||||
start: (i: number) -> number,
|
||||
decode: (i: number) -> (number?, Err.Err?),
|
||||
pcm: () -> { number },
|
||||
reset: () -> (),
|
||||
skip: number?, -- default 0
|
||||
stop: number?, -- default start(count + 1)
|
||||
preroll: number?, -- default 0
|
||||
cumulative: boolean?, -- frames after a packet = frames before + its output (default: start(i) + its output)
|
||||
}
|
||||
|
||||
-- packets(dec, spec) -> dec (with read / seek / position) | nil, Err (the first packet failed)
|
||||
function Stream.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
local sr, ch = dec.sampleRate, dec.channels
|
||||
local count = spec.count
|
||||
local skip = spec.skip or 0
|
||||
local stopAt = spec.stop or spec.start(count + 1)
|
||||
local frames = math.max(0, stopAt - skip)
|
||||
local preroll = spec.preroll or 0
|
||||
local decodeP, pcmOf, startOf = spec.decode, spec.pcm, spec.start
|
||||
local nextPacket = 1
|
||||
local decoded = 0 -- absolute frame after the packets decoded so far
|
||||
local pendPos, pendN = 0, 0
|
||||
local dropUntil = skip
|
||||
local pos = 0
|
||||
local failed: Err.Err? = nil
|
||||
dec.error = nil
|
||||
|
||||
local function fill(): boolean
|
||||
while true do
|
||||
if nextPacket > count or decoded >= stopAt then
|
||||
return false
|
||||
end
|
||||
local i = nextPacket
|
||||
nextPacket += 1
|
||||
local first = if spec.cumulative then decoded else startOf(i)
|
||||
local n, e = decodeP(i)
|
||||
if not n then
|
||||
failed = e or Err.new(Err.CORRUPT, "packet " .. i .. " failed")
|
||||
dec.error = failed
|
||||
return false
|
||||
end
|
||||
decoded = first + n
|
||||
local a = math.max(first, dropUntil)
|
||||
local b = math.min(decoded, stopAt)
|
||||
if b > a then
|
||||
pendPos, pendN = a - first, b - first
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function dec.read(_self: any, n: number, out: buffer?): (buffer, number)
|
||||
local want = math.max(0, math.floor(n))
|
||||
local need = want * ch * 4
|
||||
local ob = if out and buffer.len(out) >= need then out else buffer.create(need)
|
||||
local got = 0
|
||||
local writef32 = buffer.writef32
|
||||
while got < want do
|
||||
if pendPos >= pendN then
|
||||
if failed or not fill() then
|
||||
break
|
||||
end
|
||||
end
|
||||
local take = math.min(want - got, pendN - pendPos)
|
||||
local src = pcmOf()
|
||||
local q = got * ch * 4
|
||||
local s = pendPos * ch
|
||||
for k = 1, take * ch do
|
||||
writef32(ob, q, src[s + k])
|
||||
q += 4
|
||||
end
|
||||
pendPos += take
|
||||
got += take
|
||||
end
|
||||
pos += got
|
||||
return ob, got
|
||||
end
|
||||
|
||||
function dec.seek(_self: any, seconds: number)
|
||||
local sec = if type(seconds) == "number" and seconds == seconds then seconds else 0
|
||||
local target = math.clamp(math.floor(sec * sr + 0.5), 0, frames) + skip -- absolute frame
|
||||
-- the last packet starting at or before the target, minus the codec's pre-roll
|
||||
local lo, hi, found = 1, count, 1
|
||||
while lo <= hi do
|
||||
local mid = (lo + hi) // 2
|
||||
if startOf(mid) <= target then
|
||||
found = mid
|
||||
lo = mid + 1
|
||||
else
|
||||
hi = mid - 1
|
||||
end
|
||||
end
|
||||
found = math.max(1, found - preroll)
|
||||
spec.reset()
|
||||
nextPacket = found
|
||||
decoded = if count > 0 then startOf(found) else 0
|
||||
dropUntil = target
|
||||
pendPos, pendN = 0, 0
|
||||
failed = nil
|
||||
dec.error = nil
|
||||
pos = target - skip
|
||||
end
|
||||
|
||||
function dec.position(_self: any): number
|
||||
return pos / sr
|
||||
end
|
||||
|
||||
-- prime: surface a broken first packet as an open error
|
||||
if count > 0 and frames > 0 and not fill() and failed then
|
||||
return nil, failed
|
||||
end
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
return Stream
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Tracker/Mixer: mixes the player's active voices into an interleaved stereo f32 buffer. Per voice: sample
|
||||
-- codecs/audio/Tracker/Mixer: mixes the player's active voices into an interleaved stereo f32 buffer. Per voice: sample
|
||||
-- loops (forward, ping-pong, IT sustain loops while the key is held), linear or no interpolation, the IT two-pole
|
||||
-- resonant filter, and a short gain ramp per tick against clicks. Written for this library (no third-party code).
|
||||
--
|
||||
-- Never throws: sample indexes are clamped to the sample, every loop is bounded, and the caller (codecs/Tracker)
|
||||
-- Never throws: sample indexes are clamped to the sample, every loop is bounded, and the caller (codecs/audio/Tracker)
|
||||
-- validates the sample buffers' sizes.
|
||||
local floor, ceil, min = math.floor, math.ceil, math.min
|
||||
local readf32, writef32 = buffer.readf32, buffer.writef32
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Tracker/Pcm: sample data conversions shared by the tracker formats. Raw 8 / 16-bit PCM (signed or unsigned,
|
||||
-- codecs/audio/Tracker/Pcm: sample data conversions shared by the tracker formats. Raw 8 / 16-bit PCM (signed or unsigned,
|
||||
-- plain or delta coded as in XM / IT 2.15, interleaved or planar stereo) to the engine's f32 samples, and back.
|
||||
--
|
||||
-- Never throws: reads past the source are refused up front (decode returns nil).
|
||||
+5
-5
@@ -1,15 +1,15 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Tracker/Player: the sequencer of the tracker engine. One player for MOD, S3M, XM and IT: rows, orders,
|
||||
-- codecs/audio/Tracker/Player: the sequencer of the tracker engine. One player for MOD, S3M, XM and IT: rows, orders,
|
||||
-- speed / tempo, every effect of the four formats (interpreted per format family: ProTracker / FastTracker 2 numeric
|
||||
-- effects, ScreamTracker 3 / Impulse Tracker letter effects), instruments with volume / panning / pitch envelopes,
|
||||
-- fadeout and auto-vibrato, IT new note actions (background voices), duplicate checks and resonant filters, and song
|
||||
-- end / loop detection. Written from the format documents (ProTracker 2.3 docs, the XM spec and FT2 behaviour notes,
|
||||
-- ST3 TECH.DOC, ITTECH.TXT); no third-party code. Mixing is codecs/Tracker/Mixer.
|
||||
-- ST3 TECH.DOC, ITTECH.TXT); no third-party code. Mixing is codecs/audio/Tracker/Mixer.
|
||||
--
|
||||
-- Never throws: the module is validated by codecs/Tracker before a player is made, and every table lookup here is
|
||||
-- Never throws: the module is validated by codecs/audio/Tracker before a player is made, and every table lookup here is
|
||||
-- guarded.
|
||||
local Tables = require("@formats/codecs/Tracker/Tables")
|
||||
local Tables = require("@formats/codecs/audio/Tracker/Tables")
|
||||
|
||||
local wave64, autoWave = Tables.wave64, Tables.autoWave
|
||||
local AMIGA = Tables.AMIGA
|
||||
@@ -322,7 +322,7 @@ local function advanceRow(st: any)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ pitch
|
||||
-- the period of a note for a sample (see codecs/Tracker for the two period spaces)
|
||||
-- the period of a note for a sample (see codecs/audio/Tracker for the two period spaces)
|
||||
local function notePeriod(st: any, note: number, smp: any, ft: number?): (number, number)
|
||||
local base, ref, tr
|
||||
if st.fmt == MOD or st.fmt == XM then
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Tracker/Tables: lookup tables of the tracker engine. Waveforms for vibrato / tremolo / panbrello and the
|
||||
-- codecs/audio/Tracker/Tables: lookup tables of the tracker engine. Waveforms for vibrato / tremolo / panbrello and the
|
||||
-- auto-vibrato, the ProTracker period table (finetune 0, used to map MOD periods to notes and back) and the IT / S3M
|
||||
-- volume-column tone-portamento and retrigger tables. All values are from the public format documents (ProTracker
|
||||
-- 2.3 effect docs, FT2 / XM spec, ST3 TECH.DOC, ITTECH.TXT).
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
-- codecs/Tracker: the shared tracker engine behind music/mod, music/s3m, music/xm and music/it. The four loaders read
|
||||
-- codecs/audio/Tracker: the shared tracker engine behind music/mod, music/s3m, music/xm and music/it. The four loaders read
|
||||
-- their files into one Module structure (below); render(module, opts) plays any of them through the standard audio
|
||||
-- Decoder (Types.Decoder: stereo f32). Written for this library from the format documents (ProTracker 2.3, the XM
|
||||
-- spec + FT2 behaviour, ST3 TECH.DOC, ITTECH.TXT); libxmp / OpenMPT were used only as reference players for tests.
|
||||
--
|
||||
-- local Tracker = require("@formats/codecs/Tracker")
|
||||
-- local Tracker = require("@formats/codecs/audio/Tracker")
|
||||
-- local dec, err = Tracker.render(module, { sampleRate = 44100 }) -- a Types.Decoder
|
||||
-- local seconds = Tracker.duration(module) -- one pass, song loops detected
|
||||
--
|
||||
@@ -48,10 +48,10 @@
|
||||
-- (S2x), random volume / pan variation, OPL (AdLib) instruments, MOD Amiga low-pass filter emulation.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Player = require("@formats/codecs/Tracker/Player")
|
||||
local Mixer = require("@formats/codecs/Tracker/Mixer")
|
||||
local Pcm = require("@formats/codecs/Tracker/Pcm")
|
||||
local Tables = require("@formats/codecs/Tracker/Tables")
|
||||
local Player = require("@formats/codecs/audio/Tracker/Player")
|
||||
local Mixer = require("@formats/codecs/audio/Tracker/Mixer")
|
||||
local Pcm = require("@formats/codecs/audio/Tracker/Pcm")
|
||||
local Tables = require("@formats/codecs/audio/Tracker/Tables")
|
||||
|
||||
local Tracker = {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/VorbisComment: Vorbis comment fields ("KEY=value", the tag block of Vorbis, Opus and FLAC) <-> Types.Tags.
|
||||
-- codecs/audio/VorbisComment: Vorbis comment fields ("KEY=value", the tag block of Vorbis, Opus and FLAC) <-> Types.Tags.
|
||||
-- Tag keys are lower case everywhere in formats-luau (the same names ID3 / MP4 / RIFF INFO map to): title, artist,
|
||||
-- album, albumartist, date, genre, track, tracktotal, disc, disctotal, comment, composer, ...; TRACKNUMBER becomes
|
||||
-- track, DISCNUMBER disc, DESCRIPTION comment, other fields their lower-cased name. Repeated fields are joined with
|
||||
@@ -52,7 +52,7 @@ function VorbisComment.fields(tags: { [any]: any }): { string }
|
||||
end
|
||||
end
|
||||
table.sort(keys)
|
||||
local out = table.create(#keys)
|
||||
local out: { string } = table.create(#keys) :: any
|
||||
for i, k in keys do
|
||||
out[i] = VorbisComment.fieldName(k) .. "=" .. tags[k]
|
||||
end
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Brotli/Decode: a Brotli (RFC 7932) decoder over a whole in-memory stream. Written from the RFC, with the
|
||||
-- codecs/compress/Brotli/Decode: a Brotli (RFC 7932) decoder over a whole in-memory stream. Written from the RFC, with the
|
||||
-- reference decoder (google/brotli c/dec/decode.c, MIT) as the authority on edge cases (simple prefix code alphabet
|
||||
-- widths, repeat codes, padding checks, the large-window extension). Supports every meta-block kind (compressed,
|
||||
-- uncompressed, metadata), block types / counts, the four literal context modes, context maps with RLE and inverse
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Brotli/Static: GENERATED by tools/brotli_static.py from the brotli C sources (MIT, Google; see
|
||||
-- codecs/compress/Brotli/Static: GENERATED by tools/brotli_static.py from the brotli C sources (MIT, Google; see
|
||||
-- licenses/brotli-LICENSE.txt) - do not edit. The RFC 7932 static dictionary (122784 bytes, deflated, base64),
|
||||
-- the context lookup table (Appendix B, 4 modes x 512) and the 121 word transforms (Appendix B).
|
||||
return {
|
||||
@@ -1,12 +1,12 @@
|
||||
--!strict
|
||||
-- codecs/Brotli: Brotli (RFC 7932) decompression. A codec (layer 3): archive/br and font/woff2 build on it. Written
|
||||
-- from the RFC (decoder in codecs/Brotli/Decode); the static dictionary, context table and word transforms come from
|
||||
-- google/brotli (MIT, licenses/brotli-LICENSE.txt) through codecs/Brotli/Static (tools/brotli_static.py).
|
||||
-- codecs/compress/Brotli: Brotli (RFC 7932) decompression. A codec (layer 3): archive/br and font/woff2 build on it. Written
|
||||
-- from the RFC (decoder in codecs/compress/Brotli/Decode); the static dictionary, context table and word transforms come from
|
||||
-- google/brotli (MIT, licenses/brotli-LICENSE.txt) through codecs/compress/Brotli/Static (tools/brotli_static.py).
|
||||
-- Supports every RFC 7932 stream (window sizes 10..24, metadata / uncompressed / compressed meta-blocks, the static
|
||||
-- dictionary with all 121 transforms) and the large-window extension (opts.largeWindow). Not supported: compression,
|
||||
-- shared / compound dictionaries (draft "shared brotli").
|
||||
--
|
||||
-- local Brotli = require("@formats/codecs/Brotli")
|
||||
-- local Brotli = require("@formats/codecs/compress/Brotli")
|
||||
-- local out, consumedEnd = Brotli.decompress(buf) -- buffer | string
|
||||
-- local out, err = Brotli.decompress(buf, offset, length, { maxBytes = 2^24 })
|
||||
--
|
||||
@@ -15,9 +15,9 @@ local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Decode = require("@formats/codecs/Brotli/Decode")
|
||||
local StaticData = require("@formats/codecs/Brotli/Static")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Decode = require("@formats/codecs/compress/Brotli/Decode")
|
||||
local StaticData = require("@formats/codecs/compress/Brotli/Static")
|
||||
|
||||
local Brotli = {}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Bzip2/Decode: bzip2 stream decoder (Julian Seward's format, as documented by the bzip2 1.0.x sources and
|
||||
-- codecs/compress/Bzip2/Decode: bzip2 stream decoder (Julian Seward's format, as documented by the bzip2 1.0.x sources and
|
||||
-- Joe Tsai's "bzip2 format specification"). Written from the format description; no third-party code.
|
||||
-- Per block: MSB-first bit reader on a local 32-bit bit buffer, Huffman groups (10-bit lookup + canonical fallback for
|
||||
-- codes up to 20 bits), MTF + RUNA/RUNB, inverse BWT through one u32 vector (byte in the low 8 bits, next index
|
||||
@@ -1,17 +1,17 @@
|
||||
--!strict
|
||||
-- codecs/Bzip2: bzip2 decompressor (BWT + MTF + Huffman, the .bz2 stream format of bzip2 0.9.5 / 1.0.x). A codec
|
||||
-- codecs/compress/Bzip2: bzip2 decompressor (BWT + MTF + Huffman, the .bz2 stream format of bzip2 0.9.5 / 1.0.x). A codec
|
||||
-- (layer 3): archive/bz2 and tar.bz2 build on it. Written from the format description (no third-party code).
|
||||
-- Supports: block sizes 1..9, every Huffman group layout, block and stream CRCs, concatenated streams (as bzip2 -d).
|
||||
-- Not supported: randomised blocks (bzip2 0.9.0 and older; Err.UNSUPPORTED), compression.
|
||||
--
|
||||
-- local Bzip2 = require("@formats/codecs/Bzip2")
|
||||
-- local Bzip2 = require("@formats/codecs/compress/Bzip2")
|
||||
-- local out, consumedEnd = Bzip2.decompress(buf) -- | nil, Err
|
||||
-- local out = Bzip2.decompress(buf, offset, length, { maxBytes = 2^24 })
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / LIMIT / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Decode = require("@formats/codecs/Bzip2/Decode")
|
||||
local Decode = require("@formats/codecs/compress/Bzip2/Decode")
|
||||
|
||||
local Bzip2 = {}
|
||||
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Deflate/Compress: a DEFLATE (RFC 1951) compressor. LZ77 over a 32 KB window with hash chains (chain depth
|
||||
-- and lazy matching by level), then per block the cheapest of stored / fixed-Huffman / dynamic-Huffman (core/Huffman
|
||||
-- codecs/compress/Deflate/Compress: a DEFLATE (RFC 1951) compressor. LZ77 over a 32 KB window with hash chains (chain depth
|
||||
-- and lazy matching by level), then per block the cheapest of stored / fixed-Huffman / dynamic-Huffman (codecs/entropy/Huffman
|
||||
-- length-limited codes). Level 0 = stored blocks only.
|
||||
--
|
||||
-- raw(buf, off, len, level) -> buffer. Never throws (the caller validated the arguments).
|
||||
local Huffman = require("@formats/core/Huffman")
|
||||
local Huffman = require("@formats/codecs/entropy/Huffman")
|
||||
|
||||
local readu8, writeu8, writeu16 = buffer.readu8, buffer.writeu8, buffer.writeu16
|
||||
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
|
||||
+4
-4
@@ -1,17 +1,17 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Deflate/Inflate: raw DEFLATE (RFC 1951) decoder: stored, fixed-Huffman and dynamic-Huffman blocks.
|
||||
-- Absorbed from deflate-luau src/Deflate/Inflate.luau (same author); the table builder moved to core/Huffman, whose
|
||||
-- codecs/compress/Deflate/Inflate: raw DEFLATE (RFC 1951) decoder: stored, fixed-Huffman and dynamic-Huffman blocks.
|
||||
-- Absorbed from deflate-luau src/Deflate/Inflate.luau (same author); the table builder moved to codecs/entropy/Huffman, whose
|
||||
-- entries are symbol * 32 + length (5 length bits instead of 4).
|
||||
--
|
||||
-- Bits are read LSB first into a 32-bit bit buffer (refilled 4 bytes at a time with one readu32 while the input
|
||||
-- lasts). Huffman codes decode through a core/Huffman LSB table indexed by the next LITBITS / DISTBITS bits; a longer
|
||||
-- lasts). Huffman codes decode through a codecs/entropy/Huffman LSB table indexed by the next LITBITS / DISTBITS bits; a longer
|
||||
-- code's primary slot links to a subtable, so a symbol is one or two table reads.
|
||||
--
|
||||
-- Nothing in this module throws on any input: every buffer read and write is bounds-checked, and corrupt data
|
||||
-- returns nil, message. (Native code must never throw: it crashes Roblox Studio.)
|
||||
local Huffman = require("@formats/core/Huffman")
|
||||
local Huffman = require("@formats/codecs/entropy/Huffman")
|
||||
|
||||
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
+4
-4
@@ -1,9 +1,9 @@
|
||||
--!strict
|
||||
-- codecs/Deflate: DEFLATE (RFC 1951) decoder and compressor, with the zlib (RFC 1950) and gzip (RFC 1952) wrappers.
|
||||
-- codecs/compress/Deflate: DEFLATE (RFC 1951) decoder and compressor, with the zlib (RFC 1950) and gzip (RFC 1952) wrappers.
|
||||
-- A codec (layer 1), not a format: png, zip, gzip, woff, tiff, ... build on it. Absorbed from deflate-luau 1.0.0
|
||||
-- (same author; no third-party code); the compressor is new.
|
||||
--
|
||||
-- local Deflate = require("@formats/codecs/Deflate")
|
||||
-- local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
-- local out, err = Deflate.inflate(buf) -- raw deflate -> buffer, consumedEnd | nil, Err
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size) -- a range, with the exact output size if known
|
||||
-- local out, err = Deflate.zlib(buf) -- checks the Adler-32
|
||||
@@ -15,8 +15,8 @@ local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Adler32 = require("@formats/core/Adler32")
|
||||
local Inflate = require("@formats/codecs/Deflate/Inflate")
|
||||
local Compress = require("@formats/codecs/Deflate/Compress")
|
||||
local Inflate = require("@formats/codecs/compress/Deflate/Inflate")
|
||||
local Compress = require("@formats/codecs/compress/Deflate/Compress")
|
||||
|
||||
local Deflate = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), the xz check type 4. The 64-bit
|
||||
-- codecs/compress/Lzma/Crc64: CRC-64/XZ (ECMA-182 polynomial, reflected: 0xC96C5795D7870F42), the xz check type 4. The 64-bit
|
||||
-- value is kept as two u32 halves (hi, lo). Written from the xz file format specification. Never throws.
|
||||
local band, bxor, rshift, lshift, bor = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift, bit32.bor
|
||||
local readu8 = buffer.readu8
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Lzma/Decode: the LZMA range decoder and LZ state machine, shared by raw LZMA (.lzma, 7z) and LZMA2 (xz, 7z).
|
||||
-- codecs/compress/Lzma/Decode: the LZMA range decoder and LZ state machine, shared by raw LZMA (.lzma, 7z) and LZMA2 (xz, 7z).
|
||||
-- Written from the LZMA specification and its reference decoder (LzmaSpec.cpp, Igor Pavlov, public domain); the
|
||||
-- structure (probability model layout, state transitions, distance / length decoding) follows that document.
|
||||
--
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Lzma/Filters: the branch-call-jump (BCJ) and delta decoding filters of xz and 7z, run in place over a whole
|
||||
-- codecs/compress/Lzma/Filters: the branch-call-jump (BCJ) and delta decoding filters of xz and 7z, run in place over a whole
|
||||
-- decoded buffer range. Written from the xz file format specification and xz-embedded's xz_dec_bcj.c (Lasse Collin,
|
||||
-- public domain): x86, ARM, ARM-Thumb, PowerPC, SPARC, ARM64 and delta. Not supported: IA-64, RISC-V.
|
||||
-- Never throws: every access is inside [o, o + n).
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
-- codecs/Lzma: LZMA and LZMA2 decoders, the .lzma ("LZMA alone") wrapper and the .xz container. A codec (layer 3):
|
||||
-- codecs/compress/Lzma: LZMA and LZMA2 decoders, the .lzma ("LZMA alone") wrapper and the .xz container. A codec (layer 3):
|
||||
-- archive/xz, archive/7z (and anything else holding LZMA data) build on it. Decode only.
|
||||
-- Written from the LZMA specification (LzmaSpec.cpp, Igor Pavlov, public domain) and the xz file format specification
|
||||
-- 1.2 (Lasse Collin / Igor Pavlov, public domain); the BCJ filters follow xz-embedded (public domain).
|
||||
--
|
||||
-- local Lzma = require("@formats/codecs/Lzma")
|
||||
-- local Lzma = require("@formats/codecs/compress/Lzma")
|
||||
-- local out, e = Lzma.lzma(buf, off, len, props, unpackSize?, opts?) -- raw LZMA; props = 5-byte buffer / string
|
||||
-- -- or { lc, lp, pb, dictSize }
|
||||
-- local out, e = Lzma.lzma2(buf, off, len, dictSize?, unpackSize?, opts?) -- raw LZMA2 chunks
|
||||
@@ -19,9 +19,9 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Decode = require("@formats/codecs/Lzma/Decode")
|
||||
local Filters = require("@formats/codecs/Lzma/Filters")
|
||||
local Crc64 = require("@formats/codecs/Lzma/Crc64")
|
||||
local Decode = require("@formats/codecs/compress/Lzma/Decode")
|
||||
local Filters = require("@formats/codecs/compress/Lzma/Filters")
|
||||
local Crc64 = require("@formats/codecs/compress/Lzma/Crc64")
|
||||
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Lzw: LZW decoder and encoder in the two variants file formats use. A codec (layer 1): image/gif and
|
||||
-- codecs/compress/Lzw: LZW decoder and encoder in the two variants file formats use. A codec (layer 1): image/gif and
|
||||
-- image/tiff build on it. New code (no port), written from the GIF89a spec (appendix F) and TIFF 6.0 section 13.
|
||||
-- gif LSB-first codes, a minimum code size 1..11 (2..8 in GIF), clear = 2^min, EOI = clear + 1, the code width
|
||||
-- grows when the next free code reaches 2^width (max 12 bits; a full table stays frozen until a clear).
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Zstd/Decode: Zstandard frame decoder (RFC 8878), written from the specification (no code copied from the
|
||||
-- codecs/compress/Zstd/Decode: Zstandard frame decoder (RFC 8878), written from the specification (no code copied from the
|
||||
-- BSD reference implementation). Frames (any number, concatenated), skippable frames, raw / RLE / compressed blocks,
|
||||
-- Huffman literals (1 or 4 streams, new or repeated table), FSE sequence tables (predefined / RLE / compressed /
|
||||
-- repeat), repeat offsets, the XXH64 content checksum. Not supported: dictionaries (a non-zero dictionary ID).
|
||||
@@ -11,7 +11,7 @@
|
||||
-- byte (o - n) / 8, bits before the stream start read as zeros.
|
||||
--
|
||||
-- Nothing here throws on any input: every read is bounds-checked; failures return nil, code, message, offset.
|
||||
local Xxh64 = require("@formats/codecs/Zstd/Xxh64")
|
||||
local Xxh64 = require("@formats/codecs/compress/Zstd/Xxh64")
|
||||
|
||||
local band, lshift, rshift = bit32.band, bit32.lshift, bit32.rshift
|
||||
local readu8, readu16, readu32, writeu8, writeu16, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu16, buffer.writeu32
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Zstd/Xxh64: XXH64 (seed 0) of a buffer range, computed on 32-bit halves held in doubles (Luau and LuaJIT have
|
||||
-- codecs/compress/Zstd/Xxh64: XXH64 (seed 0) of a buffer range, computed on 32-bit halves held in doubles (Luau and LuaJIT have
|
||||
-- no 64-bit integers). 64 x 64 -> 64 products split the low operand into 16-bit limbs so every partial product is
|
||||
-- exact below 2^53. From the xxHash specification (Yann Collet, BSD-2); no code copied. zstd stores the low 32 bits
|
||||
-- of the hash as the frame content checksum. Never throws (the caller passes an in-range buffer slice).
|
||||
@@ -1,18 +1,18 @@
|
||||
--!strict
|
||||
-- codecs/Zstd: Zstandard decompression (RFC 8878). A codec (layer 3): archive/zst and anything else carrying zstd
|
||||
-- codecs/compress/Zstd: Zstandard decompression (RFC 8878). A codec (layer 3): archive/zst and anything else carrying zstd
|
||||
-- data build on it. Written from the specification. Supports every frame feature a dictionary-less stream uses:
|
||||
-- concatenated and skippable frames, raw / RLE / compressed blocks, Huffman literals (1 / 4 streams, repeated tables),
|
||||
-- predefined / RLE / compressed / repeated FSE tables, repeat offsets, the XXH64 content checksum (verified).
|
||||
-- Not supported: dictionaries (Err.UNSUPPORTED), compression.
|
||||
--
|
||||
-- local Zstd = require("@formats/codecs/Zstd")
|
||||
-- local Zstd = require("@formats/codecs/compress/Zstd")
|
||||
-- local out, err = Zstd.decompress(buf) -- -> buffer, consumedEnd | nil, Err
|
||||
-- local out, err = Zstd.decompress(buf, off, len, { maxBytes = 2^24, verify = false })
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / UNSUPPORTED / LIMIT / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Decode = require("@formats/codecs/Zstd/Decode")
|
||||
local Decode = require("@formats/codecs/compress/Zstd/Decode")
|
||||
|
||||
local Zstd = {}
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order) for decoders and encoders.
|
||||
--
|
||||
-- Decoding tables (built by buildLsb / buildMsb) are u32 buffers, indexed by the next P bits of the stream:
|
||||
-- entry = symbol * 32 + length a code of `length` <= P bits
|
||||
-- entry = offset * 32 (length 0) a link: index offset + the following S bits (subtable of 2^S entries)
|
||||
-- entry = 0 no code (incomplete code set): the stream is corrupt
|
||||
-- so a symbol is one or two table reads. LSB tables (DEFLATE, VP8L) are indexed by bit-reversed codes, MSB tables
|
||||
-- (JPEG, MPEG) by the code itself. Code lengths are 1..MAXLEN (16), symbols < 2^27.
|
||||
-- The hot loops (codecs/compress/Deflate/Inflate) inline the lookup on their own bit buffer; decodeLsb / decodeMsb do it on a
|
||||
-- core/Bits reader.
|
||||
--
|
||||
-- Encoding: lengths(freqs, maxLen) -> length-limited code lengths; codes(lengths) -> canonical code values.
|
||||
-- Never throws: bad code sets return -1 / nil.
|
||||
local Bits = require("@formats/core/Bits")
|
||||
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu32, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu32
|
||||
local bfill = buffer.fill
|
||||
|
||||
local MAXLEN = 16
|
||||
|
||||
local Huffman = {}
|
||||
Huffman.MAXLEN = MAXLEN
|
||||
|
||||
-- 8-bit reversal
|
||||
local REV = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
local r, v = 0, i
|
||||
for _ = 1, 8 do
|
||||
r = r * 2 + v % 2
|
||||
v = v // 2
|
||||
end
|
||||
REV[i + 1] = r
|
||||
end
|
||||
|
||||
-- reverse(code, len) -> the low len bits of code reversed (len <= 16)
|
||||
local function reverse(code: number, len: number): number
|
||||
return rshift(bor(lshift(REV[band(code, 255) + 1], 8), REV[band(rshift(code, 8), 255) + 1]), 16 - len)
|
||||
end
|
||||
Huffman.reverse = reverse
|
||||
|
||||
-- capacity(nsym, P, maxLen) -> the table entries (u32) needed for nsym symbols, primary width P, codes up to maxLen
|
||||
function Huffman.capacity(nsym: number, P: number, maxLen: number): number
|
||||
return 2 ^ P + (if maxLen > P then nsym * 2 ^ (maxLen - P) else 0)
|
||||
end
|
||||
|
||||
local count = table.create(MAXLEN + 1, 0)
|
||||
local nextc = table.create(MAXLEN + 1, 0)
|
||||
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean): number
|
||||
for i = 1, MAXLEN + 1 do
|
||||
count[i] = 0
|
||||
end
|
||||
for s = 0, n - 1 do
|
||||
local l = readu8(lens, off + s)
|
||||
if l > MAXLEN then
|
||||
return -1
|
||||
end
|
||||
count[l + 1] += 1
|
||||
end
|
||||
count[1] = 0
|
||||
local left = 1
|
||||
local maxLen = 0
|
||||
for l = 1, MAXLEN do
|
||||
left = left * 2 - count[l + 1]
|
||||
if left < 0 then
|
||||
return -1
|
||||
end
|
||||
if count[l + 1] > 0 then
|
||||
maxLen = l
|
||||
end
|
||||
end
|
||||
local code = 0
|
||||
for l = 1, MAXLEN do
|
||||
code = (code + count[l]) * 2
|
||||
nextc[l] = code
|
||||
end
|
||||
local psize = lshift(1, P)
|
||||
bfill(tab, 0, 0, psize * 4)
|
||||
local S = if maxLen > P then maxLen - P else 0
|
||||
local ssize = lshift(1, S)
|
||||
local nextSub = psize
|
||||
for s = 0, n - 1 do
|
||||
local l = readu8(lens, off + s)
|
||||
if l > 0 then
|
||||
local c = nextc[l]
|
||||
nextc[l] = c + 1
|
||||
local entry = s * 32 + l
|
||||
if l <= P then
|
||||
if msb then
|
||||
-- the code is the top l bits of the P-bit index: fill every completion of the low bits
|
||||
local first = lshift(c, P - l)
|
||||
local last = first + lshift(1, P - l) - 1
|
||||
for i = first, last do
|
||||
writeu32(tab, i * 4, entry)
|
||||
end
|
||||
else
|
||||
local step = lshift(1, l)
|
||||
local i = reverse(c, l)
|
||||
while i < psize do
|
||||
writeu32(tab, i * 4, entry)
|
||||
i += step
|
||||
end
|
||||
end
|
||||
else
|
||||
local pre, rest
|
||||
if msb then
|
||||
pre = rshift(c, l - P)
|
||||
rest = band(c, lshift(1, l - P) - 1)
|
||||
else
|
||||
local rev = reverse(c, l)
|
||||
pre = band(rev, psize - 1)
|
||||
rest = rshift(rev, P)
|
||||
end
|
||||
local e = readu32(tab, pre * 4)
|
||||
local sub
|
||||
if e == 0 then
|
||||
sub = nextSub
|
||||
nextSub += ssize
|
||||
bfill(tab, sub * 4, 0, ssize * 4)
|
||||
writeu32(tab, pre * 4, sub * 32)
|
||||
else
|
||||
sub = rshift(e, 5)
|
||||
end
|
||||
local k = l - P -- bits of the code in the subtable index
|
||||
if msb then
|
||||
local first = lshift(rest, S - k)
|
||||
for i = first, first + lshift(1, S - k) - 1 do
|
||||
writeu32(tab, (sub + i) * 4, entry)
|
||||
end
|
||||
else
|
||||
local step = lshift(1, k)
|
||||
local i = rest
|
||||
while i < ssize do
|
||||
writeu32(tab, (sub + i) * 4, entry)
|
||||
i += step
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return S
|
||||
end
|
||||
|
||||
-- buildLsb(lens, off, n, tab, P) -> S: a table for the code lengths lens[off .. off + n - 1] (u8), indexed by the next
|
||||
-- P bits LSB-first (bit-reversed codes). S = the subtable width (0 when no code is longer than P), -1 = an
|
||||
-- over-subscribed or invalid code set. tab must hold capacity(n, P, maxLen) u32 entries.
|
||||
function Huffman.buildLsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, false)
|
||||
end
|
||||
|
||||
-- buildMsb(lens, off, n, tab, P) -> S: the same, indexed by the next P bits MSB-first
|
||||
function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, true)
|
||||
end
|
||||
|
||||
-- decodeLsb(br, tab, P, S) -> symbol, or -1 for an invalid code (br: a core/Bits LSB reader)
|
||||
function Huffman.decodeLsb(br: Bits.BitReader, tab: buffer, P: number, S: number): number
|
||||
local e = readu32(tab, br:peek(P) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return -1
|
||||
end
|
||||
local idx = br:peek(P + S)
|
||||
e = readu32(tab, (rshift(e, 5) + rshift(idx, P)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
br:skip(l)
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- decodeMsb(br, tab, P, S) -> symbol, or -1 (br: a core/Bits MSB reader)
|
||||
function Huffman.decodeMsb(br: Bits.BitReader, tab: buffer, P: number, S: number): number
|
||||
local e = readu32(tab, br:peek(P) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return -1
|
||||
end
|
||||
local idx = br:peek(P + S)
|
||||
e = readu32(tab, (rshift(e, 5) + band(idx, lshift(1, S) - 1)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
br:skip(l)
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- codes(lengths) -> canonical code values (MSB-first, as written by an MSB writer; reverse() them for LSB streams).
|
||||
-- lengths: { number } indexed 1..n (symbol + 1), 0 = unused.
|
||||
function Huffman.codes(lengths: { number }): { number }
|
||||
local cnt = table.create(MAXLEN + 1, 0)
|
||||
for _, l in lengths do
|
||||
if l > 0 then
|
||||
cnt[l + 1] += 1
|
||||
end
|
||||
end
|
||||
local nxt = table.create(MAXLEN + 1, 0)
|
||||
local code = 0
|
||||
for l = 1, MAXLEN do
|
||||
code = (code + cnt[l]) * 2
|
||||
nxt[l + 1] = code
|
||||
end
|
||||
local out = table.create(#lengths, 0)
|
||||
for i, l in lengths do
|
||||
if l > 0 then
|
||||
out[i] = nxt[l + 1]
|
||||
nxt[l + 1] += 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- lengths(freqs, maxLen) -> code lengths ({ number }, same indexing as freqs), at most maxLen bits, optimal without
|
||||
-- the limit; over the limit, the JPEG Annex K.3 adjustment. A single used symbol gets length 1.
|
||||
function Huffman.lengths(freqs: { number }, maxLen: number): { number }
|
||||
local n = #freqs
|
||||
local out = table.create(n, 0)
|
||||
-- leaves sorted by frequency (ties by symbol, for determinism)
|
||||
local syms = {}
|
||||
for i = 1, n do
|
||||
if freqs[i] > 0 then
|
||||
table.insert(syms, i)
|
||||
end
|
||||
end
|
||||
if #syms == 0 then
|
||||
return out
|
||||
end
|
||||
if #syms == 1 then
|
||||
out[syms[1]] = 1
|
||||
return out
|
||||
end
|
||||
table.sort(syms, function(a: number, b: number): boolean
|
||||
if freqs[a] ~= freqs[b] then
|
||||
return freqs[a] < freqs[b]
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
-- two-queue Huffman: leaves (sorted) and internal nodes (created in increasing weight order)
|
||||
local m = #syms
|
||||
local weight = table.create(2 * m, 0)
|
||||
local parent = table.create(2 * m, 0)
|
||||
for i = 1, m do
|
||||
weight[i] = freqs[syms[i]]
|
||||
end
|
||||
local li, ni, nn = 1, m + 1, m + 1 -- next leaf, next internal node to take, next free node
|
||||
local function take(): number
|
||||
if li <= m and (ni >= nn or weight[li] <= weight[ni]) then
|
||||
li += 1
|
||||
return li - 1
|
||||
end
|
||||
ni += 1
|
||||
return ni - 1
|
||||
end
|
||||
for _ = 1, m - 1 do
|
||||
local a = take()
|
||||
local b = take()
|
||||
weight[nn] = weight[a] + weight[b]
|
||||
parent[a] = nn
|
||||
parent[b] = nn
|
||||
nn += 1
|
||||
end
|
||||
-- depths: the root is nn - 1
|
||||
local depth = table.create(2 * m, 0)
|
||||
for i = nn - 2, 1, -1 do
|
||||
depth[i] = depth[parent[i]] + 1
|
||||
end
|
||||
-- count codes per length; limit (JPEG K.3: move pairs from the deepest level up)
|
||||
local bl = table.create(m + maxLen + 2, 0)
|
||||
local deepest = 0
|
||||
for i = 1, m do
|
||||
bl[depth[i]] += 1
|
||||
deepest = math.max(deepest, depth[i])
|
||||
end
|
||||
for l = deepest, maxLen + 1, -1 do
|
||||
while bl[l] > 0 do
|
||||
local j = l - 2
|
||||
while j > 0 and bl[j] == 0 do
|
||||
j -= 1
|
||||
end
|
||||
bl[l] -= 2
|
||||
bl[l - 1] += 1
|
||||
bl[j + 1] += 2
|
||||
bl[j] -= 1
|
||||
end
|
||||
end
|
||||
-- the longest codes go to the least frequent symbols (syms is sorted ascending)
|
||||
local k = 1
|
||||
for l = math.min(deepest, maxLen), 1, -1 do
|
||||
for _ = 1, bl[l] do
|
||||
out[syms[k]] = l
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Huffman
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/RangeCoder/Decoder: the Opus range decoder (RFC 6716 section 4.1, libopus celt/entdec.c). Reads one Opus
|
||||
-- codecs/entropy/RangeCoder/Decoder: the Opus range decoder (RFC 6716 section 4.1, libopus celt/entdec.c). Reads one Opus
|
||||
-- frame from a `buffer` slice [base, base + storage). Raw bits (ec_dec_bits) are read from the end of the slice
|
||||
-- backwards.
|
||||
-- All values stay below 2^32, so plain Luau numbers hold them exactly; floor division is exact below 2^52.
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/RangeCoder/Encoder: the Opus range encoder (RFC 6716 section 5.1), a port of libopus celt/entenc.c (BSD:
|
||||
-- licenses/libopus-BSD.txt), the mirror of codecs/RangeCoder/Decoder. Writes one fixed-size frame: the range-coded
|
||||
-- codecs/entropy/RangeCoder/Encoder: the Opus range encoder (RFC 6716 section 5.1), a port of libopus celt/entenc.c (BSD:
|
||||
-- licenses/libopus-BSD.txt), the mirror of codecs/entropy/RangeCoder/Decoder. Writes one fixed-size frame: the range-coded
|
||||
-- symbols from the front, the raw bits (ec_enc_bits) from the back, zeros between. Values stay below 2^32 as plain
|
||||
-- numbers. Never throws: running out of room sets `error`.
|
||||
local RangeEncoder = {}
|
||||
@@ -0,0 +1,13 @@
|
||||
--!strict
|
||||
-- codecs/entropy/RangeCoder: the RFC 6716 range coder (libopus celt/entdec.c + entenc.c, BSD: licenses/libopus-BSD.txt):
|
||||
-- symbols range-coded from the front of a frame, raw bits from its back. Opus (CELT and SILK) uses it; LZMA, VP8 and
|
||||
-- CABAC have their own (different) binary coders. Moved here from audio/opus by the cleanup pass (same code).
|
||||
--
|
||||
-- local RD = require("@formats/codecs/entropy/RangeCoder/Decoder") -- RD.new(buf, base, size), RD.icdf / bits / uint ...
|
||||
-- local RE = require("@formats/codecs/entropy/RangeCoder/Encoder") -- RE.new(size), RE.icdf / bits / uint, RE.done
|
||||
local RangeCoder = {
|
||||
Decoder = require("@formats/codecs/entropy/RangeCoder/Decoder"),
|
||||
Encoder = require("@formats/codecs/entropy/RangeCoder/Encoder"),
|
||||
}
|
||||
|
||||
return RangeCoder
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177): the outlines of OpenType fonts
|
||||
-- codecs/font/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177): the outlines of OpenType fonts
|
||||
-- with a "CFF " table (.otf). Parses the header, the INDEXes, the Top / Private DICTs (name-keyed and CID-keyed
|
||||
-- fonts with FDArray / FDSelect formats 0 and 3), local and global subroutines, and runs charstrings to path commands
|
||||
-- (moveTo / lineTo / cubicTo / close, font units, y up). The charstring interpreter is ported from stb_truetype 1.26
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/TrueType/Glyf: TrueType glyph outlines (the glyf / loca tables): simple glyphs (contours of on- and
|
||||
-- codecs/font/TrueType/Glyf: TrueType glyph outlines (the glyf / loca tables): simple glyphs (contours of on- and
|
||||
-- off-curve points, with the implied on-curve midpoints) and composite glyphs (offsets or point matching, scale /
|
||||
-- x-y scale / 2x2 transforms, SCALED_COMPONENT_OFFSET, nesting up to 8 levels). Ported from stb_truetype 1.26
|
||||
-- (public domain / MIT, Sean Barrett) through imgui-luau's imstb_truetype.lua, made bounds-checked, with point
|
||||
@@ -320,7 +320,7 @@ contours = function(f: Face, g: number, depth: number): ({ Contour }?, string?)
|
||||
return composite(f, o, len, depth)
|
||||
end
|
||||
|
||||
-- outline(f, g) -> path commands (codecs/Raster format, font units, y up) | nil, message
|
||||
-- outline(f, g) -> path commands (codecs/image/Raster format, font units, y up) | nil, message
|
||||
function Glyf.outline(f: Face, g: number): ({ any }?, string?)
|
||||
local cs, err = contours(f, g, 0)
|
||||
if not cs then
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/TrueType/Tables: the sfnt tables besides the outlines: cmap (formats 0, 4, 6, 10, 12, 13; the best Unicode
|
||||
-- codecs/font/TrueType/Tables: the sfnt tables besides the outlines: cmap (formats 0, 4, 6, 10, 12, 13; the best Unicode
|
||||
-- subtable, symbol fonts through U+F000), kern (Microsoft version 0 and Apple version 1, format 0 pairs), GPOS pair
|
||||
-- kerning (lookup type 2 formats 1 and 2, reached through the 'kern' feature, including extension lookups, type 9),
|
||||
-- name (UTF-16BE and Mac Roman strings) and OS/2. Written from the OpenType spec, with stb_truetype 1.26 (public
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
-- codecs/TrueType: the sfnt font container (TrueType .ttf, OpenType .otf with CFF outlines, collections .ttc) parsed
|
||||
-- codecs/font/TrueType: the sfnt font container (TrueType .ttf, OpenType .otf with CFF outlines, collections .ttc) parsed
|
||||
-- into the standard Font value (Types.Font): metrics (head / hhea / hmtx / OS/2 / post), cmap lookups, glyph outlines
|
||||
-- (codecs/TrueType/Glyf for glyf, codecs/Cff for "CFF "), kerning ('kern' table and GPOS 'kern' pair adjustments),
|
||||
-- names, and rasterize(glyph, px) through codecs/Raster. A codec, not a format: font/ttf, font/woff and font/woff2 all
|
||||
-- (codecs/font/TrueType/Glyf for glyf, codecs/font/Cff for "CFF "), kerning ('kern' table and GPOS 'kern' pair adjustments),
|
||||
-- names, and rasterize(glyph, px) through codecs/image/Raster. A codec, not a format: font/ttf, font/woff and font/woff2 all
|
||||
-- build an sfnt and call TrueType.font(). Ported and modernised from stb_truetype 1.26 (public domain / MIT, Sean
|
||||
-- Barrett and contributors) through imgui-luau's imstb_truetype.lua; the rasterizer is codecs/Raster instead of stb's.
|
||||
-- Barrett and contributors) through imgui-luau's imstb_truetype.lua; the rasterizer is codecs/image/Raster instead of stb's.
|
||||
-- Not supported: hinting, variable fonts (fvar / gvar / CFF2), vertical metrics, bitmap strikes (EBDT / CBDT / sbix),
|
||||
-- colour layers (COLR), GSUB substitutions.
|
||||
--
|
||||
@@ -17,10 +17,10 @@
|
||||
-- Never throws: malformed fonts return nil, Err from font(), and nil from the per-glyph methods.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Raster = require("@formats/codecs/Raster")
|
||||
local Cff = require("@formats/codecs/Cff")
|
||||
local Glyf = require("@formats/codecs/TrueType/Glyf")
|
||||
local Tables = require("@formats/codecs/TrueType/Tables")
|
||||
local Raster = require("@formats/codecs/image/Raster")
|
||||
local Cff = require("@formats/codecs/font/Cff")
|
||||
local Glyf = require("@formats/codecs/font/TrueType/Glyf")
|
||||
local Tables = require("@formats/codecs/font/TrueType/Tables")
|
||||
|
||||
local TrueType = {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Blend: layer compositing on rgba8 buffers (straight alpha): source-over with the separable and non-separable
|
||||
-- codecs/image/Blend: layer compositing on rgba8 buffers (straight alpha): source-over with the separable and non-separable
|
||||
-- blend modes of the W3C Compositing and Blending Level 1 spec (the Photoshop / Aseprite set), opacity, a grey layer
|
||||
-- mask, clipping (source-atop: the backdrop keeps its alpha) and dissolve. Shared by ops/LayerOps (flatten) and the
|
||||
-- layered formats that must write a composite (layered/psd). Never throws: rectangles are clipped to both buffers.
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws).
|
||||
-- codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws).
|
||||
-- Inverse: libjpeg's islow (jidctint.c: the Loeffler-Ligtenberg-Moschytz algorithm, 13-bit constants, two passes with
|
||||
-- PASS1_BITS of scaling between them), plus the reduced 4x4 / 2x2 / 1x1 IDCTs for scaled decodes (jidctred.c); the
|
||||
-- results match libjpeg-turbo's djpeg bit for bit. Absorbed from jpeg-luau 1.0 src/Jpeg/Idct.luau (same author).
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Jpeg/Arith: arithmetic-coded scans (ITU T.81 annex D / F.1.4, sequential and progressive) into quantised
|
||||
-- codecs/image/Jpeg/Arith: arithmetic-coded scans (ITU T.81 annex D / F.1.4, sequential and progressive) into quantised
|
||||
-- DCT coefficients (native; never throws). A Luau translation of libjpeg-turbo's jdarith.c (Guido Vollbeding's
|
||||
-- decoder, IJG licence: licenses/libjpeg-turbo-LICENSE.md) and the jaricom.c probability table.
|
||||
--
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Jpeg/Color: chroma upsampling and colour conversion to RGBA8, one output row at a time (native; never
|
||||
-- codecs/image/Jpeg/Color: chroma upsampling and colour conversion to RGBA8, one output row at a time (native; never
|
||||
-- throws). Absorbed from jpeg-luau 1.0 src/Jpeg/Color.luau (same author); a Luau translation of libjpeg-turbo's
|
||||
-- jdsample.c / jdcolor.c (IJG licence: licenses/libjpeg-turbo-LICENSE.md).
|
||||
--
|
||||
@@ -1,16 +1,16 @@
|
||||
--!strict
|
||||
-- codecs/Jpeg/Decode: the JPEG marker parser and output stage (interpreted; it validates everything the native Scan /
|
||||
-- codecs/image/Jpeg/Decode: the JPEG marker parser and output stage (interpreted; it validates everything the native Scan /
|
||||
-- Arith / Dct / Color / Yuv loops index). Absorbed from jpeg-luau 1.0 src/Jpeg/init.luau (same author) and extended:
|
||||
-- arithmetic-coded frames (SOF9 / SOF10, DAC), a byte range instead of a whole buffer, APP1 EXIF / APP2 ICC / COM
|
||||
-- / JFIF density collected for meta, the AVI1 APP0 field polarity and the end of the image (Motion-JPEG), and a
|
||||
-- yuv420p output straight from the IDCT planes (outputYuv). The IDCT size choice for scaled decodes follows
|
||||
-- libjpeg-turbo's jdmaster.c. Moved from image/jpeg/Decode to codecs/Jpeg/Decode for the video formats.
|
||||
local Scan = require("@formats/codecs/Jpeg/Scan")
|
||||
local Arith = require("@formats/codecs/Jpeg/Arith")
|
||||
local Color = require("@formats/codecs/Jpeg/Color")
|
||||
local Tables = require("@formats/codecs/Jpeg/Tables")
|
||||
local Yuv = require("@formats/codecs/Jpeg/Yuv")
|
||||
local Dct = require("@formats/codecs/Dct")
|
||||
-- libjpeg-turbo's jdmaster.c. Moved from image/jpeg/Decode to codecs/image/Jpeg/Decode for the video formats.
|
||||
local Scan = require("@formats/codecs/image/Jpeg/Scan")
|
||||
local Arith = require("@formats/codecs/image/Jpeg/Arith")
|
||||
local Color = require("@formats/codecs/image/Jpeg/Color")
|
||||
local Tables = require("@formats/codecs/image/Jpeg/Tables")
|
||||
local Yuv = require("@formats/codecs/image/Jpeg/Yuv")
|
||||
local Dct = require("@formats/codecs/image/Dct")
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
@@ -1,16 +1,16 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Jpeg/Encode: the JPEG encoder (native; never throws). Baseline (SOF0) with the standard or optimised Huffman
|
||||
-- codecs/image/Jpeg/Encode: the JPEG encoder (native; never throws). Baseline (SOF0) with the standard or optimised Huffman
|
||||
-- tables and progressive (SOF2, libjpeg's jpeg_simple_progression script, optimised tables per scan); grey or YCbCr
|
||||
-- 4:4:4 / 4:2:2 / 4:2:0, or yuv420p planes coded as they are (4:2:0, no colour conversion: the Motion-JPEG
|
||||
-- encoders); restart intervals. Moved from image/jpeg/Encode to codecs/Jpeg/Encode. It follows libjpeg-turbo's
|
||||
-- encoders); restart intervals. Moved from image/jpeg/Encode to codecs/image/Jpeg/Encode. It follows libjpeg-turbo's
|
||||
-- encoder step for step so that its output is byte-identical to cjpeg's for the same settings: jccolor.c
|
||||
-- (fixed-point RGB -> YCbCr), jcsample.c (box downsampling with alternating rounding bias, right-edge replication),
|
||||
-- jccoefct.c (dummy edge blocks), jfdctint.c + jcdctmgr.c (codecs/Dct), jchuff.c (sequential coding, jpeg_gen_optimal_table) and jcphuff.c (progressive coding: EOB runs,
|
||||
-- jccoefct.c (dummy edge blocks), jfdctint.c + jcdctmgr.c (codecs/image/Dct), jchuff.c (sequential coding, jpeg_gen_optimal_table) and jcphuff.c (progressive coding: EOB runs,
|
||||
-- correction-bit buffering). Luau translation of IJG code: licenses/libjpeg-turbo-LICENSE.md.
|
||||
local Dct = require("@formats/codecs/Dct")
|
||||
local Tables = require("@formats/codecs/Jpeg/Tables")
|
||||
local Dct = require("@formats/codecs/image/Dct")
|
||||
local Tables = require("@formats/codecs/image/Jpeg/Tables")
|
||||
|
||||
local readu8, writeu8, readi16, writei16, readu16, writeu16 = buffer.readu8, buffer.writeu8, buffer.readi16, buffer.writei16, buffer.readu16, buffer.writeu16
|
||||
local band, countlz = bit32.band, bit32.countlz
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Jpeg/Scan: Huffman-coded scan decoding into quantised DCT coefficients (native; never throws). Absorbed from
|
||||
-- codecs/image/Jpeg/Scan: Huffman-coded scan decoding into quantised DCT coefficients (native; never throws). Absorbed from
|
||||
-- jpeg-luau 1.0 src/Jpeg/Scan.luau (same author); unchanged but for this header.
|
||||
--
|
||||
-- `destuff` copies one scan's entropy-coded bytes into a clean buffer: it drops the stuffed 0x00 after each 0xFF,
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Jpeg/Tables: the standard tables of ITU T.81 annex K: the example Huffman tables (K.3, which every baseline
|
||||
-- codecs/image/Jpeg/Tables: the standard tables of ITU T.81 annex K: the example Huffman tables (K.3, which every baseline
|
||||
-- encoder without optimisation writes and Motion-JPEG frames leave out) and the luminance / chrominance quantisation
|
||||
-- tables (K.1) with libjpeg's quality scaling (jcparam.c jpeg_quality_scaling / jpeg_add_quant_table).
|
||||
local Tables = {}
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Jpeg/Yuv: the yuv420p output of the JPEG decoder (native; never throws): the IDCT output planes (u16
|
||||
-- codecs/image/Jpeg/Yuv: the yuv420p output of the JPEG decoder (native; never throws): the IDCT output planes (u16
|
||||
-- samples, block-padded) cropped into u8 yuv420p planes without colour conversion or upsampling, 4:2:2 / 4:4:4
|
||||
-- chroma reduced to 4:2:0 by averaging, and the interleaving of two fields (AVI1 interlaced Motion-JPEG).
|
||||
-- The callers (codecs/Jpeg/Decode, codecs/Jpeg) size every buffer; each loop reads and writes inside those sizes.
|
||||
-- The callers (codecs/image/Jpeg/Decode, codecs/image/Jpeg) size every buffer; each loop reads and writes inside those sizes.
|
||||
local readu16, writeu8, readu32, writeu32 = buffer.readu16, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
|
||||
local Yuv = {}
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
-- codecs/Jpeg: the JPEG codec (ITU T.81) on byte ranges: image/jpeg is a thin format over it, and the Motion-JPEG
|
||||
-- codecs/image/Jpeg: the JPEG codec (ITU T.81) on byte ranges: image/jpeg is a thin format over it, and the Motion-JPEG
|
||||
-- video tracks (AVI / Matroska / QuickTime) decode and encode their frames with it. The engine (Huffman and
|
||||
-- arithmetic scan decoding, IDCTs through codecs/Dct, libjpeg-turbo's upsampling and colour conversion, the encoder)
|
||||
-- arithmetic scan decoding, IDCTs through codecs/image/Dct, libjpeg-turbo's upsampling and colour conversion, the encoder)
|
||||
-- was absorbed from jpeg-luau 1.0 (same author) into image/jpeg and moved down here; it is bit-exact with libjpeg-turbo's
|
||||
-- djpeg and byte-identical with cjpeg. IJG-derived parts: licenses/libjpeg-turbo-LICENSE.md.
|
||||
--
|
||||
@@ -31,9 +31,9 @@
|
||||
-- Never throws: bad data or arguments return nil, Err.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Decode = require("@formats/codecs/Jpeg/Decode")
|
||||
local Encode = require("@formats/codecs/Jpeg/Encode")
|
||||
local Yuv = require("@formats/codecs/Jpeg/Yuv")
|
||||
local Decode = require("@formats/codecs/image/Jpeg/Decode")
|
||||
local Encode = require("@formats/codecs/image/Jpeg/Encode")
|
||||
local Yuv = require("@formats/codecs/image/Jpeg/Yuv")
|
||||
|
||||
local Jpeg = {}
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/image/Pixels: row loops between packed pixel layouts and rgba8 (native), shared by the bitmap formats (BMP,
|
||||
-- the DIBs inside ICO / CUR, ...): 1 / 2 / 4 / 8-bit indexes through a palette, BGR / BGRA, 5-5-5, bit-masked 16 /
|
||||
-- 24 / 32-bit pixels, RLE4 / RLE8, rows bottom-up or top-down; and the encoder direction (rgba8 -> BGR / BGRA /
|
||||
-- 8-bit indexes, bottom-up). Absorbed from bmp-luau b86aed6 (same author, no third-party code); the encoder loops
|
||||
-- are new. They never throw: the caller validates every size first and hands in a source buffer that holds every
|
||||
-- byte an uncompressed loop can read (a truncated file is copied into a zero-padded buffer); the RLE loop checks each
|
||||
-- read against its end offset. Output: rgba8, top-down rows, one u32 per pixel (bytes R, G, B, A).
|
||||
local readu8, readu16, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu32, buffer.writeu8
|
||||
local band, rshift, bor, lshift = bit32.band, bit32.rshift, bit32.bor, bit32.lshift
|
||||
|
||||
local Pixels = {}
|
||||
|
||||
-- 1, 2, 4 and 8 bpp through a 256-entry u32 palette (pal)
|
||||
function Pixels.indexed(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number,
|
||||
topDown: boolean, pal: buffer)
|
||||
local mask = lshift(1, bpp) - 1
|
||||
for r = 0, h - 1 do
|
||||
local so = base + r * stride
|
||||
local d = (if topDown then r else h - 1 - r) * w * 4
|
||||
if bpp == 8 then
|
||||
for x = 0, w - 1 do
|
||||
writeu32(dst, d + x * 4, readu32(pal, readu8(src, so + x) * 4))
|
||||
end
|
||||
elseif bpp == 4 then
|
||||
for x = 0, w - 1 do
|
||||
local b = readu8(src, so + rshift(x, 1))
|
||||
local i = if band(x, 1) == 0 then rshift(b, 4) else band(b, 15)
|
||||
writeu32(dst, d + x * 4, readu32(pal, i * 4))
|
||||
end
|
||||
else
|
||||
for x = 0, w - 1 do
|
||||
local bit = x * bpp
|
||||
local b = readu8(src, so + rshift(bit, 3))
|
||||
local i = band(rshift(b, 8 - bpp - band(bit, 7)), mask)
|
||||
writeu32(dst, d + x * 4, readu32(pal, i * 4))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 16 bpp X-R-G-B 1-5-5-5 (BI_RGB 16-bit), opaque; 5-bit channels scaled to 8 bits with rounding
|
||||
function Pixels.rgb555(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean)
|
||||
for r = 0, h - 1 do
|
||||
local so = base + r * stride
|
||||
local d = (if topDown then r else h - 1 - r) * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local s = readu16(src, so + x * 2)
|
||||
local cr, cg, cb = band(rshift(s, 10), 31), band(rshift(s, 5), 31), band(s, 31)
|
||||
writeu32(dst, d + x * 4, (cr * 255 + 15) // 31 + ((cg * 255 + 15) // 31) * 256 + ((cb * 255 + 15) // 31) * 65536 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 24 bpp BI_RGB: B, G, R
|
||||
function Pixels.bgr24(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number, topDown: boolean)
|
||||
for r = 0, h - 1 do
|
||||
local so = base + r * stride
|
||||
local d = (if topDown then r else h - 1 - r) * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local s = so + x * 3
|
||||
writeu32(dst, d + x * 4,
|
||||
bor(readu8(src, s + 2), lshift(readu8(src, s + 1), 8), lshift(readu8(src, s), 16), 0xFF000000))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 32 bpp B, G, R, A (BI_RGB, or BITFIELDS with the standard masks). Returns the OR of every alpha byte.
|
||||
function Pixels.bgra32(src: buffer, base: number, stride: number, dst: buffer, w: number, h: number,
|
||||
topDown: boolean): number
|
||||
local aor = 0
|
||||
for r = 0, h - 1 do
|
||||
local so = base + r * stride
|
||||
local d = (if topDown then r else h - 1 - r) * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local p = readu32(src, so + x * 4)
|
||||
-- swap R and B: B G R A -> R G B A
|
||||
writeu32(dst, d + x * 4, bor(band(p, 0xFF00FF00), band(rshift(p, 16), 0xFF), lshift(band(p, 0xFF), 16)))
|
||||
aor = bor(aor, p)
|
||||
end
|
||||
end
|
||||
return rshift(aor, 24)
|
||||
end
|
||||
|
||||
-- 16 / 24 / 32 bpp through bit masks. sh* = shift of each mask, m* = mask >> shift, t* = scale tables (u8, one entry
|
||||
-- per masked value). A channel without a mask has m = 0 and a 1-entry table (0 for colour, 255 for alpha).
|
||||
-- Returns the OR of every masked alpha value (before scaling).
|
||||
function Pixels.masked(src: buffer, base: number, stride: number, bpp: number, dst: buffer, w: number, h: number,
|
||||
topDown: boolean, shr: number, mr: number, tr: buffer, shg: number, mg: number, tg: buffer, shb: number,
|
||||
mb: number, tb: buffer, sha: number, ma: number, ta: buffer): number
|
||||
local bytes = rshift(bpp, 3)
|
||||
local aor = 0
|
||||
for r = 0, h - 1 do
|
||||
local so = base + r * stride
|
||||
local d = (if topDown then r else h - 1 - r) * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local s = so + x * bytes
|
||||
local p
|
||||
if bytes == 2 then
|
||||
p = readu16(src, s)
|
||||
elseif bytes == 4 then
|
||||
p = readu32(src, s)
|
||||
else
|
||||
p = bor(readu16(src, s), lshift(readu8(src, s + 2), 16))
|
||||
end
|
||||
local a = band(rshift(p, sha), ma)
|
||||
aor = bor(aor, a)
|
||||
local o = d + x * 4
|
||||
writeu8(dst, o, readu8(tr, band(rshift(p, shr), mr)))
|
||||
writeu8(dst, o + 1, readu8(tg, band(rshift(p, shg), mg)))
|
||||
writeu8(dst, o + 2, readu8(tb, band(rshift(p, shb), mb)))
|
||||
writeu8(dst, o + 3, readu8(ta, a))
|
||||
end
|
||||
end
|
||||
return aor
|
||||
end
|
||||
|
||||
-- Sets every alpha byte to 255 (32-bit images whose alpha channel is all zero are opaque).
|
||||
function Pixels.opaque(dst: buffer, n: number)
|
||||
for i = 0, n - 1 do
|
||||
writeu8(dst, i * 4 + 3, 255)
|
||||
end
|
||||
end
|
||||
|
||||
-- RLE8 / RLE4 from src[pos, stop) into dst (pre-filled with transparent black; pixels the stream skips with a
|
||||
-- delta, an early end of line or an early end of bitmap stay transparent). Malformed or truncated data stops the
|
||||
-- decode and keeps what was drawn. Returns the number of pixels written.
|
||||
function Pixels.rle(src: buffer, pos: number, stop: number, rle4: boolean, dst: buffer, w: number, h: number,
|
||||
topDown: boolean, pal: buffer): number
|
||||
local x, y = 0, 0
|
||||
local written = 0
|
||||
local row = (if topDown then 0 else h - 1) * w * 4
|
||||
local rowStep = if topDown then w * 4 else -w * 4
|
||||
while y < h and pos + 1 < stop do
|
||||
local n = readu8(src, pos)
|
||||
local v = readu8(src, pos + 1)
|
||||
pos += 2
|
||||
if n > 0 then
|
||||
-- encoded run: n pixels of index v (RLE4: alternating high / low nibble)
|
||||
local e = math.min(x + n, w)
|
||||
if rle4 then
|
||||
local hi, lo = readu32(pal, rshift(v, 4) * 4), readu32(pal, band(v, 15) * 4)
|
||||
for i = x, e - 1 do
|
||||
writeu32(dst, row + i * 4, if band(i - x, 1) == 0 then hi else lo)
|
||||
end
|
||||
else
|
||||
local c = readu32(pal, v * 4)
|
||||
for i = x, e - 1 do
|
||||
writeu32(dst, row + i * 4, c)
|
||||
end
|
||||
end
|
||||
if e > x then
|
||||
written += e - x
|
||||
end
|
||||
x += n
|
||||
elseif v == 0 then
|
||||
-- end of line
|
||||
x = 0
|
||||
y += 1
|
||||
row += rowStep
|
||||
elseif v == 1 then
|
||||
break -- end of bitmap
|
||||
elseif v == 2 then
|
||||
-- delta: right dx, up (in file rows) dy
|
||||
if pos + 1 >= stop then
|
||||
break
|
||||
end
|
||||
local dx, dy = readu8(src, pos), readu8(src, pos + 1)
|
||||
pos += 2
|
||||
x += dx
|
||||
y += dy
|
||||
row += rowStep * dy
|
||||
else
|
||||
-- absolute run of v pixels, padded to a 16-bit boundary
|
||||
local nb = if rle4 then rshift(v + 1, 1) else v
|
||||
if pos + nb > stop then
|
||||
break
|
||||
end
|
||||
local e = math.min(x + v, w)
|
||||
for i = x, e - 1 do
|
||||
local k = i - x
|
||||
local idx
|
||||
if rle4 then
|
||||
local b = readu8(src, pos + rshift(k, 1))
|
||||
idx = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15)
|
||||
else
|
||||
idx = readu8(src, pos + k)
|
||||
end
|
||||
writeu32(dst, row + i * 4, readu32(pal, idx * 4))
|
||||
end
|
||||
if e > x then
|
||||
written += e - x
|
||||
end
|
||||
x += v
|
||||
pos += nb + band(nb, 1)
|
||||
end
|
||||
end
|
||||
return written
|
||||
end
|
||||
|
||||
-- Encoder: rgba8 (top-down) -> 24 bpp BGR rows, bottom-up, each padded to stride (dst is zero-filled)
|
||||
function Pixels.encode24(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number)
|
||||
for y = 0, h - 1 do
|
||||
local s = y * w * 4
|
||||
local d = base + (h - 1 - y) * stride
|
||||
for x = 0, w - 1 do
|
||||
local v = readu32(src, s + x * 4)
|
||||
writeu8(dst, d, band(rshift(v, 16), 255))
|
||||
writeu8(dst, d + 1, band(rshift(v, 8), 255))
|
||||
writeu8(dst, d + 2, band(v, 255))
|
||||
d += 3
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- rgba8 -> 32 bpp BGRA, bottom-up
|
||||
function Pixels.encode32(src: buffer, w: number, h: number, dst: buffer, base: number)
|
||||
for y = 0, h - 1 do
|
||||
local s = y * w * 4
|
||||
local d = base + (h - 1 - y) * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local v = readu32(src, s + x * 4)
|
||||
-- R G B A (little-endian u32: R | G << 8 | B << 16 | A << 24) -> B G R A
|
||||
writeu32(dst, d + x * 4, bor(band(v, 0xFF00FF00), band(rshift(v, 16), 255), lshift(band(v, 255), 16)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- rgba8 -> 8 bpp indexes through idx (a table colour -> index, colours as R | G << 8 | B << 16), bottom-up
|
||||
function Pixels.encode8(src: buffer, w: number, h: number, dst: buffer, base: number, stride: number, idx: { [number]: number })
|
||||
for y = 0, h - 1 do
|
||||
local s = y * w * 4
|
||||
local d = base + (h - 1 - y) * stride
|
||||
for x = 0, w - 1 do
|
||||
writeu8(dst, d + x, idx[band(readu32(src, s + x * 4), 0xFFFFFF)] or 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- colours(src, n, max) -> the distinct RGB colours in first-appearance order (R | G << 8 | B << 16), whether any
|
||||
-- alpha is below 255; stops counting at max + 1 colours
|
||||
function Pixels.colours(src: buffer, n: number, max: number): ({ number }, boolean)
|
||||
local seen: { [number]: boolean } = {}
|
||||
local list: { number } = {}
|
||||
local alpha = false
|
||||
for i = 0, n - 1 do
|
||||
local v = readu32(src, i * 4)
|
||||
if v < 0xFF000000 then
|
||||
alpha = true
|
||||
end
|
||||
local c = band(v, 0xFFFFFF)
|
||||
if #list <= max and not seen[c] then
|
||||
seen[c] = true
|
||||
list[#list + 1] = c
|
||||
end
|
||||
end
|
||||
return list, alpha
|
||||
end
|
||||
|
||||
return Pixels
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Quantize: palette quantization of rgba8 pixels. A codec (layer 1): image/gif (and later any palette
|
||||
-- codecs/image/Quantize: palette quantization of rgba8 pixels. A codec (layer 1): image/gif (and later any palette
|
||||
-- writer) builds on it. New code: Heckbert's median cut and Gervautz-Purgathofer octree, both over a 5-bit-per-channel
|
||||
-- weighted histogram (palette colours are the exact means of the pixels they cover), nearest-colour mapping with
|
||||
-- optional Floyd-Steinberg error diffusion. Deterministic: the same palette and indexes on every runtime (no table
|
||||
@@ -1,14 +1,14 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Raster: an antialiased scanline rasterizer for paths of lines, quadratic and cubic Béziers, under an affine
|
||||
-- codecs/image/Raster: an antialiased scanline rasterizer for paths of lines, quadratic and cubic Béziers, under an affine
|
||||
-- transform, with the nonzero and even-odd fill rules, plus a polygon stroker. Used by the fonts (glyph outlines) and
|
||||
-- by vector/svg. New code; the coverage accumulation is the signed-area method of font-rs (Raph Levien, Apache-2.0 /
|
||||
-- MIT, an algorithm description, no code copied) and of stb_truetype v2: every edge adds its exact signed area to an
|
||||
-- accumulation row; a running sum along the row gives the winding coverage of each pixel. The rules map the summed
|
||||
-- coverage a as FreeType does: nonzero = min(1, |a|), even-odd = |a| folded modulo 2.
|
||||
--
|
||||
-- local R = require("@formats/codecs/Raster")
|
||||
-- local R = require("@formats/codecs/image/Raster")
|
||||
-- local p = R.path():moveTo(0, 0):lineTo(10, 0):quadTo(10, 10, 0, 10):close() -- or a plain command list:
|
||||
-- local cmds = { { op = "moveTo", 0, 0 }, { op = "lineTo", 10, 0 }, { op = "cubicTo", 12, 2, 12, 8, 10, 10 },
|
||||
-- { op = "close" } } -- (the Font:outline() format)
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Vp8/Decode: the VP8 frame decoder (RFC 6386): the boolean entropy decoder, frame / segment / filter /
|
||||
-- codecs/image/Vp8/Decode: the VP8 frame decoder (RFC 6386): the boolean entropy decoder, frame / segment / filter /
|
||||
-- quantizer headers, token probability updates, intra modes, residual tokens, the inverse WHT and DCT, intra
|
||||
-- prediction (16x16, 4x4, chroma), and the normal and simple loop filters: a port of libwebp 1.6 src/dec/vp8_dec.c,
|
||||
-- tree_dec.c, quant_dec.c, frame_dec.c, src/dsp/dec.c and src/utils/bit_reader* (BSD-3-Clause,
|
||||
@@ -9,15 +9,15 @@
|
||||
-- vp8/common/findnearmv.c, vp8_loopfilter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt): reference updates and sign
|
||||
-- bias, the frame context saved around frames that do not refresh it, persistent segment maps and loop filter deltas,
|
||||
-- inter modes from the near vectors, motion vectors, split macroblocks, intra macroblocks in inter frames, per
|
||||
-- reference / mode loop filter levels; the prediction itself is codecs/Vp8/Predict. Bit-exact with libvpx / ffmpeg
|
||||
-- reference / mode loop filter levels; the prediction itself is codecs/image/Vp8/Predict. Bit-exact with libvpx / ffmpeg
|
||||
-- (and with libwebp on key frames). Where libvpx and ffmpeg disagree on streams no encoder writes (an intermediate
|
||||
-- clamp of segment filter levels, copying golden to altref and altref to golden in one frame), this follows ffmpeg.
|
||||
--
|
||||
-- The decoder state (Decode.new) keeps what persists between frames: the frame context, segment / filter headers, the
|
||||
-- segment map, the last / golden / altref frames. Never throws: bad data returns nil, "message" ("truncated: ..." when
|
||||
-- the data ends early).
|
||||
local Tables = require("@formats/codecs/Vp8/Tables")
|
||||
local Predict = require("@formats/codecs/Vp8/Predict")
|
||||
local Tables = require("@formats/codecs/image/Vp8/Tables")
|
||||
local Predict = require("@formats/codecs/image/Vp8/Predict")
|
||||
|
||||
local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift
|
||||
local readu8, writeu8, readi16, writei16 = buffer.readu8, buffer.writeu8, buffer.readi16, buffer.writei16
|
||||
@@ -1,14 +1,14 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Vp8/Predict: VP8 inter prediction (RFC 6386 section 18): the six-tap sub-pixel filters (version 0), the
|
||||
-- codecs/image/Vp8/Predict: VP8 inter prediction (RFC 6386 section 18): the six-tap sub-pixel filters (version 0), the
|
||||
-- bilinear ones (versions 1-3; version 3 also rounds chroma vectors to full pixels), chroma vectors derived from the
|
||||
-- luma ones, split macroblocks, and the reference frame borders. After libvpx 1.17 vp8/common/reconinter.c and
|
||||
-- filter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt). Out-of-frame motion reads the reference as if its edge pixels
|
||||
-- extended forever, which is exactly what libvpx computes with its 32 pixel border plus its motion vector clamping (the
|
||||
-- clamps only kick in once every tap is in the border). The planes keep a BORDER pixel border filled by extend(); a
|
||||
-- block whose taps leave it is first copied with clamped coordinates. Never throws.
|
||||
local Tables = require("@formats/codecs/Vp8/Tables")
|
||||
local Tables = require("@formats/codecs/image/Vp8/Tables")
|
||||
|
||||
local readu8, writeu8, bcopy, bcreate, bfill = buffer.readu8, buffer.writeu8, buffer.copy, buffer.create, buffer.fill
|
||||
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
||||
@@ -20,7 +20,7 @@ local Predict = {}
|
||||
local BORDER = 32
|
||||
Predict.BORDER = BORDER
|
||||
|
||||
-- the work buffer stride (codecs/Vp8/Decode: BPS)
|
||||
-- the work buffer stride (codecs/image/Vp8/Decode: BPS)
|
||||
local BPS = 32
|
||||
|
||||
local emu = bcreate(32 * 32) -- a block with its taps, edges replicated
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Vp8/Tables: the constant VP8 tables of RFC 6386 (sections 13.5, 13.4, 11.5, 14.1), copied from libwebp 1.6
|
||||
-- codecs/image/Vp8/Tables: the constant VP8 tables of RFC 6386 (sections 13.5, 13.4, 11.5, 14.1), copied from libwebp 1.6
|
||||
-- src/dec/tree_dec.c and src/dec/quant_dec.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt); the inter frame tables
|
||||
-- (sections 16-18: mode / motion vector probabilities, mode contexts, split partitions, sub-pixel filters) from
|
||||
-- libvpx 1.17 vp8/common (entropymode.c, vp8_entropymodedata.h, entropymv.c, modecont.c, decodemv.c, filter.c;
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Vp8/Yuv: 4:2:0 Y'CbCr (BT.601, limited range) to rgba8 the way libwebp's decoder does it, bit-exact: the
|
||||
-- codecs/image/Vp8/Yuv: 4:2:0 Y'CbCr (BT.601, limited range) to rgba8 the way libwebp's decoder does it, bit-exact: the
|
||||
-- 14-bit fixed-point VP8YuvToRgb of src/dsp/yuv.h, with either the "fancy" upsampler (src/dsp/upsampling.c: a 9-3-3-1
|
||||
-- bilinear filter on the chroma, libwebp's default) or point sampling (dwebp -nofancy). A port of libwebp 1.6
|
||||
-- (BSD-3-Clause, licenses/libwebp-LICENSE.txt).
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Vp8: the VP8 video codec (RFC 6386), decode only. A codec (layer 1): image/webp uses it for lossy frames, the
|
||||
-- codecs/image/Vp8: the VP8 video codec (RFC 6386), decode only. A codec (layer 1): image/webp uses it for lossy frames, the
|
||||
-- WebM / Matroska and IVF video tracks use its decoder object. Key frames are a port of libwebp 1.6 (BSD-3-Clause,
|
||||
-- licenses/libwebp-LICENSE.txt), bit-exact with libwebp: the yuv420p planes equal `dwebp -yuv`, and toRgba equals
|
||||
-- `dwebp -pam` (fancy upsampling) or `dwebp -nofancy -pam`. Inter frames follow libvpx 1.17 (BSD-3-Clause,
|
||||
@@ -12,7 +12,7 @@
|
||||
-- Speed (one Zen 5 core): 640x480 inter frames ~190 fps under luau --codegen, ~220 fps under LuaJIT with the trace
|
||||
-- limits the host module sets (maxtrace=8000; with LuaJIT's default 1000 traces the decoder thrashes: ~70 fps).
|
||||
--
|
||||
-- local Vp8 = require("@formats/codecs/Vp8")
|
||||
-- local Vp8 = require("@formats/codecs/image/Vp8")
|
||||
-- local frame, err = Vp8.decodeFrame(buf, off, len) -- one key frame: a yuv420p Types.Image
|
||||
-- local rgba = Vp8.toRgba(frame) -- rgba8 Image (libwebp's fancy upsampling)
|
||||
-- local dec = Vp8.newDecoder() -- a stream (WebM, IVF): key and inter frames
|
||||
@@ -22,8 +22,8 @@
|
||||
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / UNSUPPORTED / LIMIT / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Decode = require("@formats/codecs/Vp8/Decode")
|
||||
local Yuv = require("@formats/codecs/Vp8/Yuv")
|
||||
local Decode = require("@formats/codecs/image/Vp8/Decode")
|
||||
local Yuv = require("@formats/codecs/image/Vp8/Yuv")
|
||||
|
||||
local Vp8 = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Vp8L/Decode: the WebP lossless (VP8L) bitstream decoder: prefix codes (two-level tables), meta prefix codes,
|
||||
-- codecs/image/Vp8L/Decode: the WebP lossless (VP8L) bitstream decoder: prefix codes (two-level tables), meta prefix codes,
|
||||
-- LZ77 backward references with the 2-D distance map, the colour cache, and the four inverse transforms (predictor,
|
||||
-- cross colour, subtract green, colour indexing with pixel bundling). A port of libwebp 1.6 src/dec/vp8l_dec.c,
|
||||
-- src/utils/huffman_utils.c and src/dsp/lossless.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt); bit-exact with it.
|
||||
@@ -1,13 +1,13 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Vp8L/Encode: a WebP lossless (VP8L) encoder, new code written from the specification (RFC 9649 section 3).
|
||||
-- codecs/image/Vp8L/Encode: a WebP lossless (VP8L) encoder, new code written from the specification (RFC 9649 section 3).
|
||||
-- Transforms: colour indexing with pixel bundling (<= 256 colours), else subtract green + predictor (per-tile mode
|
||||
-- chosen from a residual cost) + cross colour (per-tile least-squares multipliers). Entropy coding: LZ77 with hash
|
||||
-- chains, the left / up run candidates and the 2-D distance codes, a colour cache whose size is chosen by an entropy
|
||||
-- estimate, one prefix code group per image, code lengths limited to 15 bits (core/Huffman) and run-length coded.
|
||||
-- estimate, one prefix code group per image, code lengths limited to 15 bits (codecs/entropy/Huffman) and run-length coded.
|
||||
-- Never throws (the init module validates the arguments).
|
||||
local Huffman = require("@formats/core/Huffman")
|
||||
local Huffman = require("@formats/codecs/entropy/Huffman")
|
||||
|
||||
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
|
||||
local readu32, writeu8, writeu32 = buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
@@ -143,7 +143,7 @@ local function half(x: number, y: number): number
|
||||
return clip255(x + (if dd >= 0 then rshift(dd, 1) else -rshift(-dd, 1)))
|
||||
end
|
||||
|
||||
-- predict(mode, L, T, TR, TL): the decoder's predictors (codecs/Vp8L/Decode)
|
||||
-- predict(mode, L, T, TR, TL): the decoder's predictors (codecs/image/Vp8L/Decode)
|
||||
local function predict(mode: number, L: number, T: number, TR: number, TL: number): number
|
||||
if mode == 1 then
|
||||
return L
|
||||
@@ -1,20 +1,20 @@
|
||||
--!strict
|
||||
-- codecs/Vp8L: WebP lossless (VP8L, "WebP Lossless Bitstream Specification", RFC 9649 section 3). A codec (layer 1):
|
||||
-- codecs/image/Vp8L: WebP lossless (VP8L, "WebP Lossless Bitstream Specification", RFC 9649 section 3). A codec (layer 1):
|
||||
-- image/webp uses it for VP8L frames and for compressed ALPH planes; other containers may carry it too.
|
||||
-- Decoder: a port of libwebp 1.6 (BSD-3-Clause, licenses/libwebp-LICENSE.txt), bit-exact with it. Encoder: new
|
||||
-- (subtract green, predictor and cross-colour transforms with per-tile mode search, colour indexing with pixel
|
||||
-- bundling for <= 256 colours, LZ77 with hash chains and the 2-D distance codes, a colour cache chosen by entropy,
|
||||
-- one prefix code group per image or meta codes for big images).
|
||||
--
|
||||
-- local Vp8L = require("@formats/codecs/Vp8L")
|
||||
-- local Vp8L = require("@formats/codecs/image/Vp8L")
|
||||
-- local rgba, w, h, alpha = Vp8L.decode(buf, off, len) -- a VP8L bitstream (0x2f signature) -> rgba8
|
||||
-- local a8 = Vp8L.decodeAlpha(buf, off, len, w, h) -- a headerless stream's green channel (ALPH)
|
||||
-- local bits = Vp8L.encode(rgba, w, h, { effort = 4 }) -- rgba8 -> a VP8L bitstream
|
||||
--
|
||||
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / LIMIT / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Decode = require("@formats/codecs/Vp8L/Decode")
|
||||
local Encode = require("@formats/codecs/Vp8L/Encode")
|
||||
local Decode = require("@formats/codecs/image/Vp8L/Decode")
|
||||
local Encode = require("@formats/codecs/image/Vp8L/Encode")
|
||||
|
||||
local Vp8L = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Yuv: planar 4:2:0 Y'CbCr <-> rgba8 for the video codecs and writers (native; never throws). BT.601 or
|
||||
-- codecs/image/Yuv: planar 4:2:0 Y'CbCr <-> rgba8 for the video codecs and writers (native; never throws). BT.601 or
|
||||
-- BT.709 matrices, limited ("tv", 16..235 / 16..240: VP8, MPEG-1, H.264) or full ("pc", 0..255: MJPEG / JPEG) range.
|
||||
-- yuv -> rgb uses nearest chroma and 16-bit fixed point with swscale's default (non-accurate) coefficients, so the
|
||||
-- result is within about one level of `ffmpeg -pix_fmt rgba`; rgb -> yuv averages each 2x2 block for the chroma
|
||||
@@ -0,0 +1,282 @@
|
||||
--!strict
|
||||
-- codecs/model/Mat4: 4x4 matrix and quaternion helpers for the model formats: column-major flat arrays of 16
|
||||
-- (glTF / FBX / Types.Scene layout: translation at 13..15), quaternions { x, y, z, w }, angles in degrees where a
|
||||
-- format stores degrees. Shared by FBX, Collada, USD and MeshOps instead of a copy in each. New in formats-luau (the
|
||||
-- functions come from the former model/fbx/Xform, model/dae/Math and model/usda/Build helpers).
|
||||
--
|
||||
-- local m = Mat4.compose({ 1, 2, 3 }, quat, { 2, 2, 2 }) -- T * R * S
|
||||
-- local t, r, s = Mat4.decompose(m) -- (decomposeStrict: nil for shear / projective / singular)
|
||||
-- local w = Mat4.mul(parent, m) -- = Types.Scene.mul
|
||||
-- local inv = Mat4.inverse(m) -- affine inverse | nil
|
||||
-- local x, y, z = Mat4.point(m, px, py, pz)
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Mat4 = {}
|
||||
|
||||
export type M4 = { number }
|
||||
|
||||
local sqrt, abs, sin, cos, rad = math.sqrt, math.abs, math.sin, math.cos, math.rad
|
||||
|
||||
function Mat4.identity(): M4
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
-- mul(a, b) -> a * b
|
||||
Mat4.mul = Types.Scene.mul
|
||||
|
||||
function Mat4.translation(x: number, y: number, z: number): M4
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
|
||||
end
|
||||
|
||||
function Mat4.scaling(x: number, y: number, z: number): M4
|
||||
return { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
-- rotation(axis, degrees) -> the rotation about axis 1 (X), 2 (Y) or 3 (Z)
|
||||
function Mat4.rotation(axis: number, degrees: number): M4
|
||||
local a = rad(degrees)
|
||||
local c, s = cos(a), sin(a)
|
||||
if axis == 1 then
|
||||
return { 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1 }
|
||||
elseif axis == 2 then
|
||||
return { c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
return { c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
local AXIS = { x = 1, y = 2, z = 3 }
|
||||
-- euler(x, y, z, order?) -> the rotation applying the axes in order (default "xyz": R = Rz * Ry * Rx), degrees
|
||||
function Mat4.euler(x: number, y: number, z: number, order: string?): M4
|
||||
local o = order or "xyz"
|
||||
local deg = { x = x, y = y, z = z }
|
||||
local m = Mat4.identity()
|
||||
for i = 1, 3 do
|
||||
local a = string.sub(o, i, i)
|
||||
m = Mat4.mul(Mat4.rotation(AXIS[a] or i, deg[a] or 0), m)
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
-- axisAngle(x, y, z, degrees) -> quaternion
|
||||
function Mat4.axisAngle(x: number, y: number, z: number, degrees: number): { number }
|
||||
local l = sqrt(x * x + y * y + z * z)
|
||||
if l < 1e-12 then
|
||||
return { 0, 0, 0, 1 }
|
||||
end
|
||||
local h = rad(degrees) / 2
|
||||
local s = sin(h) / l
|
||||
return { x * s, y * s, z * s, cos(h) }
|
||||
end
|
||||
|
||||
-- fromQuat(q) -> the rotation matrix of a quaternion
|
||||
function Mat4.fromQuat(q: { number }): M4
|
||||
local x, y, z, w = q[1], q[2], q[3], q[4]
|
||||
local xx, yy, zz, xy, xz, yz, wx, wy, wz = x * x, y * y, z * z, x * y, x * z, y * z, w * x, w * y, w * z
|
||||
return {
|
||||
1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy), 0,
|
||||
2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx), 0,
|
||||
2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy), 0,
|
||||
0, 0, 0, 1,
|
||||
}
|
||||
end
|
||||
|
||||
-- quat(m) -> the unit quaternion of the rotation in m's upper 3x3 (columns assumed orthonormal)
|
||||
function Mat4.quat(m: M4): { number }
|
||||
local m00, m01, m02 = m[1], m[5], m[9]
|
||||
local m10, m11, m12 = m[2], m[6], m[10]
|
||||
local m20, m21, m22 = m[3], m[7], m[11]
|
||||
local tr = m00 + m11 + m22
|
||||
local x, y, z, w
|
||||
if tr > 0 then
|
||||
local s = sqrt(tr + 1) * 2
|
||||
w, x, y, z = s / 4, (m21 - m12) / s, (m02 - m20) / s, (m10 - m01) / s
|
||||
elseif m00 > m11 and m00 > m22 then
|
||||
local s = sqrt(math.max(1 + m00 - m11 - m22, 1e-30)) * 2
|
||||
w, x, y, z = (m21 - m12) / s, s / 4, (m01 + m10) / s, (m02 + m20) / s
|
||||
elseif m11 > m22 then
|
||||
local s = sqrt(math.max(1 + m11 - m00 - m22, 1e-30)) * 2
|
||||
w, x, y, z = (m02 - m20) / s, (m01 + m10) / s, s / 4, (m12 + m21) / s
|
||||
else
|
||||
local s = sqrt(math.max(1 + m22 - m00 - m11, 1e-30)) * 2
|
||||
w, x, y, z = (m10 - m01) / s, (m02 + m20) / s, (m12 + m21) / s, s / 4
|
||||
end
|
||||
local n = sqrt(x * x + y * y + z * z + w * w)
|
||||
if n == 0 or n ~= n then
|
||||
return { 0, 0, 0, 1 }
|
||||
end
|
||||
return { x / n, y / n, z / n, w / n }
|
||||
end
|
||||
|
||||
-- eulerXYZ(q) -> Euler XYZ degrees (R = Rz * Ry * Rx) of a quaternion
|
||||
function Mat4.eulerXYZ(q: { number }): { number }
|
||||
local m = Mat4.fromQuat(q)
|
||||
local sy = math.clamp(-m[3], -1, 1) -- m[3] = r20 = -sin(y)
|
||||
local y = math.asin(sy)
|
||||
local x, z
|
||||
if abs(sy) < 0.9999999 then
|
||||
x = math.atan2(m[7], m[11])
|
||||
z = math.atan2(m[2], m[1])
|
||||
else
|
||||
x = math.atan2(-m[10], m[6])
|
||||
z = 0
|
||||
end
|
||||
return { math.deg(x), math.deg(y), math.deg(z) }
|
||||
end
|
||||
|
||||
-- compose(t?, r?, s?) -> T * R * S (glTF node semantics)
|
||||
function Mat4.compose(t: { number }?, r: { number }?, s: { number }?): M4
|
||||
local m = if r then Mat4.fromQuat(r) else Mat4.identity()
|
||||
if s then
|
||||
for k = 0, 2 do
|
||||
m[k * 4 + 1] *= s[k + 1]
|
||||
m[k * 4 + 2] *= s[k + 1]
|
||||
m[k * 4 + 3] *= s[k + 1]
|
||||
end
|
||||
end
|
||||
if t then
|
||||
m[13], m[14], m[15] = t[1], t[2], t[3]
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
local function det3(m: M4): number
|
||||
return m[1] * (m[6] * m[11] - m[10] * m[7]) - m[5] * (m[2] * m[11] - m[10] * m[3]) + m[9] * (m[2] * m[7] - m[6] * m[3])
|
||||
end
|
||||
|
||||
local function split(m: M4, strict: boolean): ({ number }?, { number }?, { number }?)
|
||||
if strict and (abs(m[4]) > 1e-6 or abs(m[8]) > 1e-6 or abs(m[12]) > 1e-6 or abs(m[16] - 1) > 1e-6) then
|
||||
return nil, nil, nil
|
||||
end
|
||||
local sx = sqrt(m[1] * m[1] + m[2] * m[2] + m[3] * m[3])
|
||||
local sy = sqrt(m[5] * m[5] + m[6] * m[6] + m[7] * m[7])
|
||||
local sz = sqrt(m[9] * m[9] + m[10] * m[10] + m[11] * m[11])
|
||||
if strict and (sx < 1e-12 or sy < 1e-12 or sz < 1e-12) then
|
||||
return nil, nil, nil
|
||||
end
|
||||
if det3(m) < 0 then
|
||||
sx = -sx
|
||||
end
|
||||
local ix = if sx ~= 0 then 1 / sx else 0
|
||||
local iy = if sy ~= 0 then 1 / sy else 0
|
||||
local iz = if sz ~= 0 then 1 / sz else 0
|
||||
local r = { m[1] * ix, m[2] * ix, m[3] * ix, 0, m[5] * iy, m[6] * iy, m[7] * iy, 0, m[9] * iz, m[10] * iz, m[11] * iz, 0, 0, 0, 0, 1 }
|
||||
if strict then
|
||||
local dxy = r[1] * r[5] + r[2] * r[6] + r[3] * r[7]
|
||||
local dxz = r[1] * r[9] + r[2] * r[10] + r[3] * r[11]
|
||||
local dyz = r[5] * r[9] + r[6] * r[10] + r[7] * r[11]
|
||||
if abs(dxy) > 1e-4 or abs(dxz) > 1e-4 or abs(dyz) > 1e-4 then
|
||||
return nil, nil, nil
|
||||
end
|
||||
end
|
||||
return { m[13], m[14], m[15] }, Mat4.quat(r), { sx, sy, sz }
|
||||
end
|
||||
|
||||
-- decompose(m) -> translation, rotation (quaternion), scale (a negative determinant flips the x scale; shear is lost)
|
||||
function Mat4.decompose(m: M4): ({ number }, { number }, { number })
|
||||
local t, r, s = split(m, false)
|
||||
return t :: { number }, r :: { number }, s :: { number }
|
||||
end
|
||||
|
||||
-- decomposeStrict(m) -> translation, rotation, scale | nil for matrices TRS cannot express (a projective row, shear,
|
||||
-- a zero scale)
|
||||
function Mat4.decomposeStrict(m: M4): ({ number }?, { number }?, { number }?)
|
||||
return split(m, true)
|
||||
end
|
||||
|
||||
-- inverse(m) -> the inverse of an affine matrix (general upper 3x3) | nil when singular
|
||||
function Mat4.inverse(m: M4): M4?
|
||||
local a, b, c = m[1], m[5], m[9]
|
||||
local d, e, f = m[2], m[6], m[10]
|
||||
local g, h, i = m[3], m[7], m[11]
|
||||
local A, B, C = e * i - f * h, -(d * i - f * g), d * h - e * g
|
||||
local det = a * A + b * B + c * C
|
||||
if det == 0 or det ~= det then
|
||||
return nil
|
||||
end
|
||||
local k = 1 / det
|
||||
local r = {
|
||||
A * k, B * k, C * k, 0,
|
||||
-(b * i - c * h) * k, (a * i - c * g) * k, -(a * h - b * g) * k, 0,
|
||||
(b * f - c * e) * k, -(a * f - c * d) * k, (a * e - b * d) * k, 0,
|
||||
0, 0, 0, 1,
|
||||
}
|
||||
local tx, ty, tz = m[13], m[14], m[15]
|
||||
r[13] = -(r[1] * tx + r[5] * ty + r[9] * tz)
|
||||
r[14] = -(r[2] * tx + r[6] * ty + r[10] * tz)
|
||||
r[15] = -(r[3] * tx + r[7] * ty + r[11] * tz)
|
||||
return r
|
||||
end
|
||||
|
||||
-- inverseFull(m) -> the inverse of any invertible 4x4 (projective rows too) | nil when singular
|
||||
function Mat4.inverseFull(m: M4): M4?
|
||||
local a = m
|
||||
local inv = table.create(16, 0)
|
||||
inv[1] = a[6] * a[11] * a[16] - a[6] * a[12] * a[15] - a[10] * a[7] * a[16] + a[10] * a[8] * a[15] + a[14] * a[7] * a[12] - a[14] * a[8] * a[11]
|
||||
inv[5] = -a[5] * a[11] * a[16] + a[5] * a[12] * a[15] + a[9] * a[7] * a[16] - a[9] * a[8] * a[15] - a[13] * a[7] * a[12] + a[13] * a[8] * a[11]
|
||||
inv[9] = a[5] * a[10] * a[16] - a[5] * a[12] * a[14] - a[9] * a[6] * a[16] + a[9] * a[8] * a[14] + a[13] * a[6] * a[12] - a[13] * a[8] * a[10]
|
||||
inv[13] = -a[5] * a[10] * a[15] + a[5] * a[11] * a[14] + a[9] * a[6] * a[15] - a[9] * a[7] * a[14] - a[13] * a[6] * a[11] + a[13] * a[7] * a[10]
|
||||
inv[2] = -a[2] * a[11] * a[16] + a[2] * a[12] * a[15] + a[10] * a[3] * a[16] - a[10] * a[4] * a[15] - a[14] * a[3] * a[12] + a[14] * a[4] * a[11]
|
||||
inv[6] = a[1] * a[11] * a[16] - a[1] * a[12] * a[15] - a[9] * a[3] * a[16] + a[9] * a[4] * a[15] + a[13] * a[3] * a[12] - a[13] * a[4] * a[11]
|
||||
inv[10] = -a[1] * a[10] * a[16] + a[1] * a[12] * a[14] + a[9] * a[2] * a[16] - a[9] * a[4] * a[14] - a[13] * a[2] * a[12] + a[13] * a[4] * a[10]
|
||||
inv[14] = a[1] * a[10] * a[15] - a[1] * a[11] * a[14] - a[9] * a[2] * a[15] + a[9] * a[3] * a[14] + a[13] * a[2] * a[11] - a[13] * a[3] * a[10]
|
||||
inv[3] = a[2] * a[7] * a[16] - a[2] * a[8] * a[15] - a[6] * a[3] * a[16] + a[6] * a[4] * a[15] + a[14] * a[3] * a[8] - a[14] * a[4] * a[7]
|
||||
inv[7] = -a[1] * a[7] * a[16] + a[1] * a[8] * a[15] + a[5] * a[3] * a[16] - a[5] * a[4] * a[15] - a[13] * a[3] * a[8] + a[13] * a[4] * a[7]
|
||||
inv[11] = a[1] * a[6] * a[16] - a[1] * a[8] * a[14] - a[5] * a[2] * a[16] + a[5] * a[4] * a[14] + a[13] * a[2] * a[8] - a[13] * a[4] * a[6]
|
||||
inv[15] = -a[1] * a[6] * a[15] + a[1] * a[7] * a[14] + a[5] * a[2] * a[15] - a[5] * a[3] * a[14] - a[13] * a[2] * a[7] + a[13] * a[3] * a[6]
|
||||
inv[4] = -a[2] * a[7] * a[12] + a[2] * a[8] * a[11] + a[6] * a[3] * a[12] - a[6] * a[4] * a[11] - a[10] * a[3] * a[8] + a[10] * a[4] * a[7]
|
||||
inv[8] = a[1] * a[7] * a[12] - a[1] * a[8] * a[11] - a[5] * a[3] * a[12] + a[5] * a[4] * a[11] + a[9] * a[3] * a[8] - a[9] * a[4] * a[7]
|
||||
inv[12] = -a[1] * a[6] * a[12] + a[1] * a[8] * a[10] + a[5] * a[2] * a[12] - a[5] * a[4] * a[10] - a[9] * a[2] * a[8] + a[9] * a[4] * a[6]
|
||||
inv[16] = a[1] * a[6] * a[11] - a[1] * a[7] * a[10] - a[5] * a[2] * a[11] + a[5] * a[3] * a[10] + a[9] * a[2] * a[7] - a[9] * a[3] * a[6]
|
||||
local det = a[1] * inv[1] + a[2] * inv[5] + a[3] * inv[9] + a[4] * inv[13]
|
||||
if det == 0 or det ~= det then
|
||||
return nil
|
||||
end
|
||||
for k = 1, 16 do
|
||||
inv[k] /= det
|
||||
end
|
||||
return inv
|
||||
end
|
||||
|
||||
-- transpose3(m) -> m with its upper 3x3 transposed and no translation (the inverse of a pure rotation)
|
||||
function Mat4.transpose3(m: M4): M4
|
||||
return { m[1], m[5], m[9], 0, m[2], m[6], m[10], 0, m[3], m[7], m[11], 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
-- fromRows(v, at?) -> column-major from 16 row-major values at v[at] (default 1; missing values: identity)
|
||||
function Mat4.fromRows(v: { number }, at: number?): M4
|
||||
local o = (at or 1) - 1
|
||||
local m = table.create(16, 0)
|
||||
for r = 0, 3 do
|
||||
for c = 0, 3 do
|
||||
m[c * 4 + r + 1] = v[o + r * 4 + c + 1] or (if r == c then 1 else 0)
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
-- toRows(m) -> the 16 values row-major
|
||||
function Mat4.toRows(m: M4): { number }
|
||||
local v = table.create(16, 0)
|
||||
for r = 0, 3 do
|
||||
for c = 0, 3 do
|
||||
v[r * 4 + c + 1] = m[c * 4 + r + 1]
|
||||
end
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- point(m, x, y, z) -> m * (x, y, z, 1)
|
||||
function Mat4.point(m: M4, x: number, y: number, z: number): (number, number, number)
|
||||
return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
|
||||
end
|
||||
|
||||
-- normalMatrix(m) -> the inverse transpose of the upper 3x3 (for normals) as a 4x4 (identity when singular)
|
||||
function Mat4.normalMatrix(m: M4): M4
|
||||
local inv = Mat4.inverse({ m[1], m[2], m[3], 0, m[5], m[6], m[7], 0, m[9], m[10], m[11], 0, 0, 0, 0, 1 })
|
||||
if not inv then
|
||||
return Mat4.identity()
|
||||
end
|
||||
return Mat4.transpose3(inv)
|
||||
end
|
||||
|
||||
return Mat4
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/MeshFlatten: a Scene to one world-space mesh, for the writers of mesh-only formats (STL, PLY, OFF). Walks
|
||||
-- codecs/model/MeshFlatten: a Scene to one world-space mesh, for the writers of mesh-only formats (STL, PLY, OFF). Walks
|
||||
-- scene.roots recursively (TRS or column-major `matrix` per node; a scene without nodes uses its meshes as they are),
|
||||
-- applies the transforms to POSITION (and the inverse transpose to NORMAL), reverses the winding under mirroring
|
||||
-- transforms, reads every accessor type to numbers and concatenates all primitives (triangles with or without indices,
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Json/Decode: the RFC 8259 parser behind codecs/Json. Iterative (an explicit container stack, so deep nesting
|
||||
-- codecs/text/Json/Decode: the RFC 8259 parser behind codecs/text/Json. Iterative (an explicit container stack, so deep nesting
|
||||
-- is a LIMIT error, never a stack overflow), strict grammar (no comments, trailing commas, leading zeros, NaN, single
|
||||
-- quotes or raw control characters), \u escapes with surrogate pairs (lone surrogates become WTF-8 bytes). Also the
|
||||
-- pull tokenizer behind Json.events. Never throws: failures return nil, message, offset (0-based byte offset).
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Decode = {}
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Json/Encode: the serializer behind codecs/Json. Compact or pretty (indent), objects in their Types.object key
|
||||
-- codecs/text/Json/Encode: the serializer behind codecs/text/Json. Compact or pretty (indent), objects in their Types.object key
|
||||
-- order (plain tables: sorted keys), arrays for tables with keys 1..n, shortest round-trip number text, optional
|
||||
-- ASCII-only output. Never throws: bad values (cycles, NaN / inf, functions, non-string keys that are not numbers)
|
||||
-- return nil, message.
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
-- codecs/Json: JSON (RFC 8259 / ECMA-404) parse and serialize, the engine data/json, glTF, TMJ, ... use. New code.
|
||||
-- codecs/text/Json: JSON (RFC 8259 / ECMA-404) parse and serialize, the engine data/json, glTF, TMJ, ... use. New code.
|
||||
-- Values are plain Luau: objects -> tables (string keys), arrays -> arrays, null -> Types.null (opts.null), numbers
|
||||
-- -> doubles (opts.bigInts = "string" keeps integers past 2^53 as their text). Empty objects are Types.object() so
|
||||
-- they write back as {}; opts.ordered makes every object a Types.object with its key order.
|
||||
--
|
||||
-- local Json = require("@formats/codecs/Json")
|
||||
-- local Json = require("@formats/codecs/text/Json")
|
||||
-- local v, err = Json.decode('{"a": [1, 2, null]}') -- string | buffer; nil, Err on bad input
|
||||
-- local s = Json.encode(v, { indent = 2 }) -- string | nil, Err
|
||||
-- for ev, x in Json.events(text) do ... end -- pull events without building the tree
|
||||
@@ -15,8 +15,8 @@
|
||||
-- writer() builds output incrementally and flush() hands back the text so far. Never throws.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Decode = require("@formats/codecs/Json/Decode")
|
||||
local Encode = require("@formats/codecs/Json/Encode")
|
||||
local Decode = require("@formats/codecs/text/Json/Decode")
|
||||
local Encode = require("@formats/codecs/text/Json/Encode")
|
||||
|
||||
local Json = {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/Text: text decoding helpers for the text formats: a UTF-8 BOM is stripped, text that is not valid UTF-8 is
|
||||
-- codecs/text/Text: text decoding helpers for the text formats: a UTF-8 BOM is stripped, text that is not valid UTF-8 is
|
||||
-- read as Windows-1252 (classic .m3u / .pls / .ini / .srt files), lines split on CRLF / CR / LF, trim. Absorbed from
|
||||
-- playlist-luau 3fc7fd3 (same author, no third-party code). Never throws.
|
||||
local Text = {}
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/Url: RFC 3986 reference resolution (section 5.2), the classification of a playlist / manifest location (URL,
|
||||
-- codecs/text/Url: RFC 3986 reference resolution (section 5.2), the classification of a playlist / manifest location (URL,
|
||||
-- Windows / UNC / POSIX local path, relative reference) and percent-encoding of bytes that may not appear in a URI
|
||||
-- (raw spaces and UTF-8 in playlists). Shared by the playlist formats (m3u / HLS, pls, xspf); later DASH / glTF URIs.
|
||||
-- Absorbed from playlist-luau 3fc7fd3 (same author, no third-party code): Url.luau, plus locate / base from its
|
||||
@@ -1,13 +1,13 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Xml/Parse: the XML 1.0 pull parser behind codecs/Xml. Input decoding (UTF-8, UTF-16 LE / BE by BOM or
|
||||
-- codecs/text/Xml/Parse: the XML 1.0 pull parser behind codecs/text/Xml. Input decoding (UTF-8, UTF-16 LE / BE by BOM or
|
||||
-- by the first bytes, ISO-8859-1 / US-ASCII / Windows-1252 by the declaration), line-ending normalization, the
|
||||
-- well-formedness rules that matter (one root, matched tags, unique attributes, quoted values, no '<' in values,
|
||||
-- legal names, character references in range, no "--" in comments, no "]]>" in text), the five predefined entities,
|
||||
-- character references and internal general entities from the DOCTYPE (with an expansion limit). Not supported:
|
||||
-- external entities / DTDs (references to them are errors), entity values containing markup (taken as text),
|
||||
-- validation. Never throws: errors are events.
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Text = require("@formats/codecs/text/Text")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Parse = {}
|
||||
@@ -1,11 +1,11 @@
|
||||
--!strict
|
||||
-- codecs/Xml: XML 1.0 parse (DOM and pull events) and serialize, the engine data/xml, SVG, DAE, 3MF, TMX, plist, ...
|
||||
-- codecs/text/Xml: XML 1.0 parse (DOM and pull events) and serialize, the engine data/xml, SVG, DAE, 3MF, TMX, plist, ...
|
||||
-- use. New code. Supports: UTF-8 / UTF-16 / Latin-1 input, the predefined entities, character references, internal
|
||||
-- DOCTYPE entities (expansion limited), CDATA, comments, processing instructions, the XML declaration, namespaces
|
||||
-- (names stay qualified "prefix:local"; opts.namespaces adds each element's URI as node.ns; Xml.clark gives
|
||||
-- "{uri}local"), xml:space. Not supported: external entities / DTDs, validation, entity values containing markup.
|
||||
--
|
||||
-- local Xml = require("@formats/codecs/Xml")
|
||||
-- local Xml = require("@formats/codecs/text/Xml")
|
||||
-- local doc, err = Xml.decode(text) -- a "#document" node; doc.root is the root element
|
||||
-- for _, item in Xml.children(doc.root, "item") do print(item.attrs.id, Xml.text(item)) end
|
||||
-- for ev, a, b in Xml.events(text) do ... end -- "start" name attrs / "end" name / "text" s / ... (SAX-like)
|
||||
@@ -18,7 +18,7 @@
|
||||
-- target); otherwise CDATA merges into the text and the others are dropped. Never throws.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Parse = require("@formats/codecs/Xml/Parse")
|
||||
local Parse = require("@formats/codecs/text/Xml/Parse")
|
||||
|
||||
local Xml = {}
|
||||
|
||||
@@ -57,7 +57,7 @@ local function errCode(msg: string, at: number, n: number): string
|
||||
return Err.CORRUPT
|
||||
end
|
||||
|
||||
-- events(src, opts?) -> iterator: event, a, b, c (see codecs/Xml/Parse: "decl", "doctype", "start" name attrs ns,
|
||||
-- events(src, opts?) -> iterator: event, a, b, c (see codecs/text/Xml/Parse: "decl", "doctype", "start" name attrs ns,
|
||||
-- "end" name, "text" s, "cdata" s, "comment" s, "pi" target data, and on bad input "error" Err as the last event)
|
||||
function Xml.events(src: string | buffer, opts: Parse.Options?): () -> (string?, any, any, any)
|
||||
local s, terr = text(src)
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Bitstream: NAL unit framing and the RBSP bit reader of ITU-T H.264 (7.3 / 7.4 / 9.1): Annex B start
|
||||
-- codecs/video/H264/Bitstream: NAL unit framing and the RBSP bit reader of ITU-T H.264 (7.3 / 7.4 / 9.1): Annex B start
|
||||
-- code splitting, AVCC length-prefixed samples, avcC (AVCDecoderConfigurationRecord, ISO/IEC 14496-15 5.3.3)
|
||||
-- parsing, emulation-prevention removal, and the u(n) / ue(v) / se(v) / te(v) readers. Written from the spec.
|
||||
-- Never throws: reads past the end of an RBSP return zero bits (the buffer carries 8 zero bytes of padding and every
|
||||
@@ -1,15 +1,15 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Cabac: slice data and macroblock layer with CABAC entropy coding (ITU-T H.264 7.3.4, 7.3.5, 9.3): the
|
||||
-- codecs/video/H264/Cabac: slice data and macroblock layer with CABAC entropy coding (ITU-T H.264 7.3.4, 7.3.5, 9.3): the
|
||||
-- arithmetic decoding engine (9.3.1.2 / 9.3.3.2), context initialisation (9.3.1.1), the binarizations and context
|
||||
-- index increments of every syntax element (9.3.2 / 9.3.3.1) and residual_block_cabac for 4x4, 8x8 and DC blocks.
|
||||
-- Coefficients are dequantised as they are placed. Written from the spec; tables from Tables.
|
||||
-- The engine keeps codIOffset with `nb` look-ahead bits (low = offset * 2^nb + next bits), so renormalisation is one
|
||||
-- multiply and bytes are fetched one at a time. Never throws: returns false, "message" on bad data.
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
local Mb = require("@formats/codecs/H264/Mb")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/video/H264/Tables")
|
||||
local Mb = require("@formats/codecs/video/H264/Mb")
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local min = math.min
|
||||
@@ -1,14 +1,14 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Cavlc: slice data and macroblock layer with CAVLC entropy coding (ITU-T H.264 7.3.4, 7.3.5, 9.2):
|
||||
-- codecs/video/H264/Cavlc: slice data and macroblock layer with CAVLC entropy coding (ITU-T H.264 7.3.4, 7.3.5, 9.2):
|
||||
-- mb_skip_run, mb_type / sub_mb_type, intra modes, ref_idx (te), mvd, coded_block_pattern (me), mb_qp_delta and
|
||||
-- residual_block_cavlc (coeff_token, level_prefix / suffix, total_zeros, run_before) with the 8x8 interleaving.
|
||||
-- Coefficients are dequantised as they are placed. Written from the spec; VLC tables from Tables.
|
||||
-- Never throws: returns false, "message" on bad data.
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
local Mb = require("@formats/codecs/H264/Mb")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/video/H264/Tables")
|
||||
local Mb = require("@formats/codecs/video/H264/Mb")
|
||||
|
||||
local u, u1, ue, se = Bitstream.u, Bitstream.u1, Bitstream.ue, Bitstream.se
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Deblock: the in-loop deblocking filter of ITU-T H.264 8.7 for frame macroblocks, 8-bit 4:2:0:
|
||||
-- codecs/video/H264/Deblock: the in-loop deblocking filter of ITU-T H.264 8.7 for frame macroblocks, 8-bit 4:2:0:
|
||||
-- boundary strengths (8.7.2.1, from the per-4x4 intra / coefficient / reference / motion grids), alpha / beta / tC0
|
||||
-- (tables 8-16 / 8-17), the bS < 4 and bS = 4 luma and chroma filters, disable_deblocking_filter_idc 0 / 1 / 2 and the
|
||||
-- slice alpha / beta offsets. Written from the spec. Runs over the whole picture in macroblock order once all its
|
||||
-- slices are decoded (the result equals filtering each macroblock as it is decoded).
|
||||
-- Never throws.
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
local Tables = require("@formats/codecs/video/H264/Tables")
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local ALPHA, BETA, TC0 = Tables.ALPHA, Tables.BETA, Tables.TC0
|
||||
@@ -1,19 +1,19 @@
|
||||
--!strict
|
||||
-- codecs/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): NAL unit
|
||||
-- codecs/video/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): NAL unit
|
||||
-- dispatch, picture boundaries (7.4.1.2.4), picture order count types 0 / 1 / 2 (8.2.1), frame_num gaps (8.2.5.2),
|
||||
-- reference picture lists with modification (8.2.4), sliding window and adaptive (MMCO 1-6) marking (8.2.5), output
|
||||
-- in POC order through a reorder buffer (num_reorder_frames, else the DPB size), concealment of lost macroblocks,
|
||||
-- deblocking and border extension. Written from the spec.
|
||||
-- Never throws: errors are returned (a corrupt slice is skipped and its macroblocks concealed).
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Params = require("@formats/codecs/H264/Params")
|
||||
local Slice = require("@formats/codecs/H264/Slice")
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
local Mb = require("@formats/codecs/H264/Mb")
|
||||
local Cavlc = require("@formats/codecs/H264/Cavlc")
|
||||
local Cabac = require("@formats/codecs/H264/Cabac")
|
||||
local Deblock = require("@formats/codecs/H264/Deblock")
|
||||
local Inter = require("@formats/codecs/H264/Inter")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
local Params = require("@formats/codecs/video/H264/Params")
|
||||
local Slice = require("@formats/codecs/video/H264/Slice")
|
||||
local Tables = require("@formats/codecs/video/H264/Tables")
|
||||
local Mb = require("@formats/codecs/video/H264/Mb")
|
||||
local Cavlc = require("@formats/codecs/video/H264/Cavlc")
|
||||
local Cabac = require("@formats/codecs/video/H264/Cabac")
|
||||
local Deblock = require("@formats/codecs/video/H264/Deblock")
|
||||
local Inter = require("@formats/codecs/video/H264/Inter")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local PAD, PADC = Inter.PAD, Inter.PADC
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Inter: inter prediction sample interpolation and weighting (ITU-T H.264 8.4.2.2 / 8.4.2.3): the 6-tap
|
||||
-- codecs/video/H264/Inter: inter prediction sample interpolation and weighting (ITU-T H.264 8.4.2.2 / 8.4.2.3): the 6-tap
|
||||
-- quarter-sample luma filter, eighth-sample bilinear chroma, default / explicit / implicit weighted sample
|
||||
-- prediction, and the picture border extension that makes out-of-picture reference reads equal to the clamped
|
||||
-- sample positions of 8.4.2.2.1. Written from the spec.
|
||||
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Intra: intra prediction (ITU-T H.264 8.3): Intra_4x4 and Intra_8x8 (nine directional modes, with the
|
||||
-- codecs/video/H264/Intra: intra prediction (ITU-T H.264 8.3): Intra_4x4 and Intra_8x8 (nine directional modes, with the
|
||||
-- 8x8 reference sample filter of 8.3.2.2.1), Intra_16x16 and chroma (DC / horizontal / vertical / plane), 8-bit.
|
||||
-- Written from the spec. Predictions are written straight into the picture plane (row stride `s`, block origin `o`).
|
||||
-- Never throws: every sample read is inside the padded plane (callers pass availability for the edges).
|
||||
@@ -1,15 +1,15 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Mb: macroblock reconstruction shared by the CAVLC and CABAC parsers (ITU-T H.264 8.3 - 8.5):
|
||||
-- codecs/video/H264/Mb: macroblock reconstruction shared by the CAVLC and CABAC parsers (ITU-T H.264 8.3 - 8.5):
|
||||
-- neighbour availability (6.4.11), Intra4x4 / Intra8x8 mode prediction (8.3.1.1 / 8.3.2.1), motion vector prediction
|
||||
-- (8.4.1.3), P_Skip (8.4.1.1), B direct spatial and temporal (8.4.1.2), motion compensation with weighting, intra
|
||||
-- prediction and the residual. Written from the spec; the per-4x4 motion / mode grids follow the usual decoder layout
|
||||
-- (picture-wide arrays, one entry per 4x4 luma block, 1-based).
|
||||
-- Never throws: every index is in range by construction; reference indices past the list use the first entry.
|
||||
local Intra = require("@formats/codecs/H264/Intra")
|
||||
local Transform = require("@formats/codecs/H264/Transform")
|
||||
local Inter = require("@formats/codecs/H264/Inter")
|
||||
local Intra = require("@formats/codecs/video/H264/Intra")
|
||||
local Transform = require("@formats/codecs/video/H264/Transform")
|
||||
local Inter = require("@formats/codecs/video/H264/Inter")
|
||||
|
||||
local Mb = {}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/H264/Params: sequence and picture parameter sets (ITU-T H.264 7.3.2.1 / 7.3.2.2 / E.1), scaling lists with
|
||||
-- codecs/video/H264/Params: sequence and picture parameter sets (ITU-T H.264 7.3.2.1 / 7.3.2.2 / E.1), scaling lists with
|
||||
-- the fall-back rules A and B (7.4.2.1.1 / table 7-2), and the dequantisation tables built from them (8.5.9: the
|
||||
-- LevelScale4x4 / LevelScale8x8 functions, premultiplied by 2^(qP / 6)). Written from the spec.
|
||||
-- Never throws: parse functions return nil, "message".
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
local Tables = require("@formats/codecs/video/H264/Tables")
|
||||
|
||||
local u, u1, ue, se = Bitstream.u, Bitstream.u1, Bitstream.ue, Bitstream.se
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/H264/Slice: the slice header (ITU-T H.264 7.3.3): ref_pic_list_modification, pred_weight_table and
|
||||
-- codecs/video/H264/Slice: the slice header (ITU-T H.264 7.3.3): ref_pic_list_modification, pred_weight_table and
|
||||
-- dec_ref_pic_marking. Written from the spec. Frame coding only: field pictures (field_pic_flag = 1), MBAFF,
|
||||
-- SP / SI slices, separate colour planes and redundant pictures return "unsupported: ...".
|
||||
-- Never throws: header() returns nil, "message".
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
|
||||
local u, u1, ue, se = Bitstream.u, Bitstream.u1, Bitstream.ue, Bitstream.se
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/H264/Tables: the constant tables of ITU-T H.264 (CAVLC code tables 9-5, 9-7, 9-8, 9-9, 9-10; CABAC context
|
||||
-- codecs/video/H264/Tables: the constant tables of ITU-T H.264 (CAVLC code tables 9-5, 9-7, 9-8, 9-9, 9-10; CABAC context
|
||||
-- initialisation tables 9-12..9-24, rangeTabLPS 9-44 and the state transitions 9-45; the chroma QP table 8-15; the
|
||||
-- me(v) coded_block_pattern mapping 9-4; default scaling lists 7-3 / 7-4; zig-zag scans 8-12 / 8-13; table 9-43; the
|
||||
-- deblocking alpha / beta / tC0 tables 8-16 / 8-17). Values extracted from openh264 2.x (BSD-2-Clause,
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Transform: the inverse transforms of ITU-T H.264 8.5: the 4x4 and 8x8 integer IDCTs with the residual
|
||||
-- codecs/video/H264/Transform: the inverse transforms of ITU-T H.264 8.5: the 4x4 and 8x8 integer IDCTs with the residual
|
||||
-- added to the prediction in the plane (8.5.12.2 / 8.5.13.2 / 8.5.14), the 4x4 luma DC and 2x2 chroma DC Hadamard
|
||||
-- transforms with their scaling (8.5.10 / 8.5.11.2). Written from the spec. Coefficients are dequantised already
|
||||
-- (Params.dequant), raster order, in a 1-based table at `base + 1 ...`; the functions zero what they read.
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/H264: an H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) video decoder in pure Luau. A codec (layer 1): the
|
||||
-- codecs/video/H264: an H.264 / AVC (ITU-T H.264 | ISO/IEC 14496-10) video decoder in pure Luau. A codec (layer 1): the
|
||||
-- video/h264 format (raw Annex B streams) and the MP4 / MKV / AVI / TS video tracks use it. Clean-room, written from the
|
||||
-- spec; constant tables taken from openh264 (BSD-2-Clause, licenses/openh264-LICENSE.txt). Bit-exact with the
|
||||
-- reference decoder (checked against ffmpeg per frame).
|
||||
@@ -14,7 +14,7 @@
|
||||
-- lossless (transform bypass), FMO / ASO slice groups, SP / SI slices, data partitioning, SVC / MVC enhancement layers
|
||||
-- (their NAL units are ignored).
|
||||
--
|
||||
-- local H264 = require("@formats/codecs/H264")
|
||||
-- local H264 = require("@formats/codecs/video/H264")
|
||||
-- local dec = H264.newDecoder() -- or { avcC = buffer } for MP4 samples (length-prefixed)
|
||||
-- for _, au in accessUnits do
|
||||
-- local frames, err = dec:decode(au, pts) -- frames: { { image (yuv420p Types.Image), pts, poc, keyframe } }
|
||||
@@ -29,9 +29,9 @@
|
||||
-- concealed (dec.errors counts them, dec.lastError says why).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Bitstream = require("@formats/codecs/H264/Bitstream")
|
||||
local Decoder = require("@formats/codecs/H264/Decoder")
|
||||
local Params = require("@formats/codecs/H264/Params")
|
||||
local Bitstream = require("@formats/codecs/video/H264/Bitstream")
|
||||
local Decoder = require("@formats/codecs/video/H264/Decoder")
|
||||
local Params = require("@formats/codecs/video/H264/Params")
|
||||
|
||||
local H264 = {}
|
||||
|
||||
@@ -201,7 +201,7 @@ function H264.parseAvcC(data: any): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ video layer
|
||||
-- newVideoDecoder(track, opts?) -> a Types.VideoCodecDecoder (the codecs/Video registry entry for "h264"):
|
||||
-- newVideoDecoder(track, opts?) -> a Types.VideoCodecDecoder (the codecs/video/Video registry entry for "h264"):
|
||||
-- track.config = avcC (MP4 / Matroska; packets length-prefixed) or nil (Annex B packets: MPEG-TS, AVI, raw .h264).
|
||||
-- Pictures come out in display order with the packet timestamps sorted (display order); a track with
|
||||
-- `ptsInOutputOrder` (raw Annex B, no timestamps) numbers them in output order instead.
|
||||
@@ -1,6 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Mpeg1/Decoder: the MPEG-1 video decoding core (ISO/IEC 11172-2; native, never throws): I, P and B pictures,
|
||||
-- codecs/video/Mpeg1/Decoder: the MPEG-1 video decoding core (ISO/IEC 11172-2; native, never throws): I, P and B pictures,
|
||||
-- half-pel motion compensation, custom quantiser matrices, sequence headers anywhere in the stream. Absorbed from
|
||||
-- mpeg1-luau e2e2898 (same author; src/Mpeg1/Decoder.luau, unchanged but for this header and the requires), a port
|
||||
-- of jsmpeg's decoder (src/mpeg1.js by Dominic Szablewski, MIT, licenses/jsmpeg-MIT.txt) with these changes for
|
||||
@@ -13,7 +13,7 @@
|
||||
-- * motion compensation four bytes at a time (SWAR rounding averages);
|
||||
-- * BT.601 limited-range YCbCr -> RGBA8 (jsmpeg's canvas path treats Y as full range).
|
||||
--
|
||||
-- The IDCT is not codecs/Dct's: it computes the same islow transform (its put mode is bit-identical to
|
||||
-- The IDCT is not codecs/image/Dct's: it computes the same islow transform (its put mode is bit-identical to
|
||||
-- Dct.idctBlock with a unit quantiser and no level shift, checked on 20000 random blocks), but MPEG-1 adds the
|
||||
-- residual of P / B blocks to the prediction before clamping, dequantises into i32 coefficients itself (MPEG
|
||||
-- rounding and oddification, not JPEG's q multiply) and writes u8 samples four per writeu32, where Dct clamps to
|
||||
@@ -23,8 +23,8 @@
|
||||
-- Native code must never throw (an error unwinding through native code crashes Roblox Studio): every read
|
||||
-- position is checked against the data end (plus a zero pad that bounds the overrun of one macroblock), motion
|
||||
-- vectors are clamped to the reference picture, and bad data ends the slice instead of raising.
|
||||
local V = require("@formats/codecs/Mpeg1/Vlc")
|
||||
local T = require("@formats/codecs/Mpeg1/Tables")
|
||||
local V = require("@formats/codecs/video/Mpeg1/Vlc")
|
||||
local T = require("@formats/codecs/video/Mpeg1/Tables")
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local writeu8 = buffer.writeu8
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Mpeg1/Tables: the constant tables of jsmpeg's MPEG-1 video decoder (https://github.com/phoboslab/jsmpeg,
|
||||
-- codecs/video/Mpeg1/Tables: the constant tables of jsmpeg's MPEG-1 video decoder (https://github.com/phoboslab/jsmpeg,
|
||||
-- Dominic Szablewski, MIT, licenses/jsmpeg-MIT.txt), generated by mpeg1-luau's tools/gen_tables.py (absorbed from
|
||||
-- mpeg1-luau e2e2898). Lists are 0-based like the JS arrays.
|
||||
local T = {}
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Mpeg1/Vlc: lookup tables for the MPEG-1 variable-length codes, built at load time from jsmpeg's binary trees (Tables.luau).
|
||||
-- codecs/video/Mpeg1/Vlc: lookup tables for the MPEG-1 variable-length codes, built at load time from jsmpeg's binary trees (Tables.luau).
|
||||
--
|
||||
-- A flat table is indexed by the next K bits of the stream (K = the longest code) and holds
|
||||
-- len * 65536 + (value + 32768) per entry; len = 0 marks an invalid code. The DCT coefficient tables are two-level
|
||||
@@ -8,7 +8,7 @@
|
||||
-- first one of a block, where "10" is end_of_block and "11" is run 0 / level 1 (jsmpeg reads this as "1" and one
|
||||
-- more bit); DCT_FIRST keeps "1" as run 0 / level 1. Absorbed from mpeg1-luau e2e2898 (same author), unchanged but
|
||||
-- for this header and the require.
|
||||
local T = require("@formats/codecs/Mpeg1/Tables")
|
||||
local T = require("@formats/codecs/video/Mpeg1/Tables")
|
||||
|
||||
local V = {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Mpeg1: an MPEG-1 video decoder (ISO/IEC 11172-2) for the MPEG-TS / MPEG-PS video formats. Absorbed from
|
||||
-- codecs/video/Mpeg1: an MPEG-1 video decoder (ISO/IEC 11172-2) for the MPEG-TS / MPEG-PS video formats. Absorbed from
|
||||
-- mpeg1-luau e2e2898 (same author; README, docs/api.md and docs/performance.md there), a port of jsmpeg's decoder
|
||||
-- (https://github.com/phoboslab/jsmpeg, Dominic Szablewski, MIT: licenses/jsmpeg-MIT.txt) extended with B-pictures,
|
||||
-- an islow integer IDCT, table-driven VLC decoding and BT.601 RGBA8 output; added here: frameImage.
|
||||
@@ -17,7 +17,7 @@
|
||||
--
|
||||
-- Nothing here throws: bad arguments return nil / false, bad data is skipped (see Decoder.luau).
|
||||
local Types = require("@formats/Types")
|
||||
local D = require("@formats/codecs/Mpeg1/Decoder")
|
||||
local D = require("@formats/codecs/video/Mpeg1/Decoder")
|
||||
|
||||
local Mpeg1 = {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Video/Source: what a video writer (Matroska / WebM, AVI, Y4M, MP4) gets to write, from whatever it was given:
|
||||
-- codecs/video/Video/Source: what a video writer (Matroska / WebM, AVI, Y4M, MP4) gets to write, from whatever it was given:
|
||||
-- a VideoDecoder (F.load / F.video.open of any video or animated image) or a Demuxer. Each output stream is either
|
||||
-- copied (the source track's packets, when the target container takes that codec: remuxing, e.g. VP8 into WebM,
|
||||
-- since VP8 is decode-only) or produced (decoded frames encoded with a registered encoder such as MJPEG, raw
|
||||
@@ -13,7 +13,7 @@
|
||||
-- -- track, packets = { { pts, duration (seconds), keyframe, data } } }
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Yuv = require("@formats/codecs/Yuv")
|
||||
local Yuv = require("@formats/codecs/image/Yuv")
|
||||
|
||||
local Source = {}
|
||||
|
||||
@@ -77,7 +77,7 @@ end
|
||||
local function encodeFrames(v: any, codec: string, opts: any): (Stream?, Err.Err?)
|
||||
local encode: ((img: Types.Image, opts: any) -> (buffer?, Err.Err?))? = nil
|
||||
if codec ~= "rawvideo" then
|
||||
local Video = require("@formats/codecs/Video")
|
||||
local Video = require("@formats/codecs/video/Video")
|
||||
local e = Video.entry(codec)
|
||||
encode = e and e.encode
|
||||
if not encode then
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/Video: the video codec registry and the generic VideoDecoder. New in formats-luau.
|
||||
-- codecs/video/Video: the video codec registry and the generic VideoDecoder. New in formats-luau.
|
||||
--
|
||||
-- Registry: our codec ids ("vp8", "mjpeg", "mpeg1", "h264", "rawvideo", ...) with the containers' names for them
|
||||
-- (AVI / MOV FourCCs, Matroska CodecIDs, MPEG stream types) and, for the codecs we can decode, a constructor of a
|
||||
@@ -7,7 +7,7 @@
|
||||
-- Video.fromMatroska so a track's codec is known even without a decoder (remuxing still works). Another codec plugs
|
||||
-- in with one line in BUILTIN (static: Roblox has no dynamic require) or at run time with Video.register(entry).
|
||||
--
|
||||
-- local Video = require("@formats/codecs/Video")
|
||||
-- local Video = require("@formats/codecs/video/Video")
|
||||
-- Video.fromFourcc("MJPG") --> "mjpeg" Video.fromMatroska("V_VP8") --> "vp8"
|
||||
-- local dec = Video.decoder(track, opts) -- a VideoCodecDecoder | nil, Err (UNSUPPORTED: no decoder)
|
||||
-- local v = Video.open(demuxer, opts) -- a Types.VideoDecoder over a Types.Demuxer (formats' open())
|
||||
@@ -16,7 +16,7 @@
|
||||
-- Never throws: bad data / missing codecs come back as nil, Err.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Yuv = require("@formats/codecs/Yuv")
|
||||
local Yuv = require("@formats/codecs/image/Yuv")
|
||||
|
||||
local Video = {}
|
||||
|
||||
@@ -190,7 +190,7 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec
|
||||
end
|
||||
|
||||
local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Vp8 = require("@formats/codecs/Vp8")
|
||||
local Vp8 = require("@formats/codecs/image/Vp8")
|
||||
local dec = Vp8.newDecoder()
|
||||
local o = { maxPixels = opts and opts.maxPixels }
|
||||
return Video.simpleDecoder(function(data: buffer): (Types.Image?, Err.Err?)
|
||||
@@ -216,7 +216,7 @@ end
|
||||
-- Motion JPEG: every packet one JPEG (or two fields), yuv420p straight from the planes (full range) when the layout
|
||||
-- allows, else rgba8
|
||||
local function mjpegDecoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Jpeg = require("@formats/codecs/Jpeg")
|
||||
local Jpeg = require("@formats/codecs/image/Jpeg")
|
||||
local o: Jpeg.DecodeOptions = { yuv = true, maxPixels = opts and opts.maxPixels }
|
||||
return Video.simpleDecoder(function(data: buffer): (Types.Image?, Err.Err?)
|
||||
local img, err = Jpeg.decode(data, 0, buffer.len(data), o)
|
||||
@@ -229,7 +229,7 @@ end
|
||||
|
||||
-- the MJPEG encoder (writers): JPEG is full-range, so limited-range yuv420p frames are remapped first
|
||||
local function mjpegEncode(img: Types.Image, opts: any?): (buffer?, Err.Err?)
|
||||
local Jpeg = require("@formats/codecs/Jpeg")
|
||||
local Jpeg = require("@formats/codecs/image/Jpeg")
|
||||
local src = img
|
||||
if img.format == "yuv420p" then
|
||||
local range = Yuv.info(img)
|
||||
@@ -242,7 +242,7 @@ end
|
||||
|
||||
-- MPEG-1 video: pictures in decode order, reordered for display (the packet's pts travels with its picture)
|
||||
local function mpeg1Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local Mpeg1 = require("@formats/codecs/Mpeg1")
|
||||
local Mpeg1 = require("@formats/codecs/video/Mpeg1")
|
||||
local dec = Mpeg1.new({ reorder = true, rgba = false, maxPixels = opts and opts.maxPixels })
|
||||
local queue: { any } = {}
|
||||
local function push(f: any)
|
||||
@@ -287,10 +287,10 @@ local function mpeg1Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecD
|
||||
return self :: any, nil
|
||||
end
|
||||
|
||||
-- H.264 / AVC (codecs/H264): avcC from track.config (MP4 / Matroska) or Annex B packets (MPEG-TS, AVI, raw .h264);
|
||||
-- H.264 / AVC (codecs/video/H264): avcC from track.config (MP4 / Matroska) or Annex B packets (MPEG-TS, AVI, raw .h264);
|
||||
-- pictures in display order with the pts of their packet
|
||||
local function h264Decoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local H264 = require("@formats/codecs/H264")
|
||||
local H264 = require("@formats/codecs/video/H264")
|
||||
return H264.newVideoDecoder(track, opts)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user