Files
rosetta/src/Formats/codecs/Pcm/Convert.luau
T

309 lines
8.5 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/Pcm/Convert: raw sample formats <-> interleaved f32 (the hot loops of codecs/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).
--
-- Formats: u8 s8 s16le s16be s24le s24be s32le s32be f32le f32be f64le f64be alaw ulaw (u8: offset binary).
-- Never throws: callers check ranges (Convert.SIZE[fmt] * count bytes) before calling.
local Convert = {}
local readu8, readi8, readi16, readi32, readu32 = buffer.readu8, buffer.readi8, buffer.readi16, buffer.readi32, buffer.readu32
local readf32, readf64, writef32, writef64 = buffer.readf32, buffer.readf64, buffer.writef32, buffer.writef64
local writeu8, writeu16, writeu32, writei16, writei32 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writei16, buffer.writei32
local floor = math.floor
local byteswap = bit32.byteswap
local SIZE: { [string]: number } = {
u8 = 1,
s8 = 1,
s16le = 2,
s16be = 2,
s24le = 3,
s24be = 3,
s32le = 4,
s32be = 4,
f32le = 4,
f32be = 4,
f64le = 8,
f64be = 8,
alaw = 1,
ulaw = 1,
}
Convert.SIZE = SIZE
-- the integer bit depth of each format (float: 32 / 64; G.711: 16 after expansion)
Convert.BITS = {
u8 = 8,
s8 = 8,
s16le = 16,
s16be = 16,
s24le = 24,
s24be = 24,
s32le = 32,
s32be = 32,
f32le = 32,
f32be = 32,
f64le = 64,
f64be = 64,
alaw = 16,
ulaw = 16,
}
-- ------------------------------------------------------------------------------------------------ G.711
local ALAW = table.create(256, 0) -- code -> linear s16
local ULAW = table.create(256, 0)
for code = 0, 255 do
-- A-law
local a = bit32.bxor(code, 0x55)
local t = bit32.band(a, 0x0F) * 16
local seg = bit32.rshift(bit32.band(a, 0x70), 4)
if seg == 0 then
t += 8
elseif seg == 1 then
t += 0x108
else
t = (t + 0x108) * 2 ^ (seg - 1)
end
ALAW[code + 1] = if bit32.band(a, 0x80) ~= 0 then t else -t
-- mu-law
local u = bit32.bxor(code, 0xFF)
local m = (bit32.band(u, 0x0F) * 8 + 0x84) * 2 ^ bit32.rshift(bit32.band(u, 0x70), 4)
ULAW[code + 1] = if bit32.band(u, 0x80) ~= 0 then 0x84 - m else m - 0x84
end
local SEG_AEND = { 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF }
local SEG_UEND = { 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF }
local function segment(v: number, ends: { number }): number
for i = 1, 8 do
if v <= ends[i] then
return i - 1
end
end
return 8
end
-- linear s16 -> A-law code
local function alawEncode(pcm: number): number
local mask
local v = floor(pcm / 8) -- 13-bit magnitude domain
if v >= 0 then
mask = 0xD5
else
mask = 0x55
v = -v - 1
end
local seg = segment(v, SEG_AEND)
local aval
if seg >= 8 then
aval = 0x7F
else
aval = seg * 16
if seg < 2 then
aval += bit32.band(floor(v / 2), 0x0F)
else
aval += bit32.band(floor(v / 2 ^ seg), 0x0F)
end
end
return bit32.bxor(aval, mask)
end
-- linear s16 -> mu-law code
local function ulawEncode(pcm: number): number
local v = floor(pcm / 4) -- 14-bit
local mask
if v < 0 then
v = -v
mask = 0x7F
else
mask = 0xFF
end
if v > 8159 then
v = 8159
end
v += 0x21
local seg = segment(v, SEG_UEND)
local uval
if seg >= 8 then
uval = 0x7F
else
uval = seg * 16 + bit32.band(floor(v / 2 ^ (seg + 1)), 0x0F)
end
return bit32.bxor(uval, mask)
end
Convert.alawEncode = alawEncode
Convert.ulawEncode = ulawEncode
Convert.ALAW = ALAW
Convert.ULAW = ULAW
-- ------------------------------------------------------------------------------------------------ decode
-- decode(src, so, fmt, count, dst, do_) -> true | false (unknown fmt): count samples at src[so] -> f32 at dst[do_]
function Convert.decode(src: buffer, so: number, fmt: string, count: number, dst: buffer, d: number): boolean
if fmt == "s16le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi16(src, so + i * 2) / 32768)
end
elseif fmt == "s16be" then
for i = 0, count - 1 do
local p = so + i * 2
local v = readu8(src, p) * 256 + readu8(src, p + 1)
if v >= 32768 then
v -= 65536
end
writef32(dst, d + i * 4, v / 32768)
end
elseif fmt == "u8" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, (readu8(src, so + i) - 128) / 128)
end
elseif fmt == "s8" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi8(src, so + i) / 128)
end
elseif fmt == "s24le" then
for i = 0, count - 1 do
local p = so + i * 3
local v = readu8(src, p) + readu8(src, p + 1) * 256 + readi8(src, p + 2) * 65536
writef32(dst, d + i * 4, v / 8388608)
end
elseif fmt == "s24be" then
for i = 0, count - 1 do
local p = so + i * 3
local v = readi8(src, p) * 65536 + readu8(src, p + 1) * 256 + readu8(src, p + 2)
writef32(dst, d + i * 4, v / 8388608)
end
elseif fmt == "s32le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readi32(src, so + i * 4) / 2147483648)
end
elseif fmt == "s32be" then
for i = 0, count - 1 do
local v = byteswap(readu32(src, so + i * 4))
if v >= 2147483648 then
v -= 4294967296
end
writef32(dst, d + i * 4, v / 2147483648)
end
elseif fmt == "f32le" then
buffer.copy(dst, d, src, so, count * 4)
elseif fmt == "f32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
end
elseif fmt == "f64le" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, readf64(src, so + i * 8))
end
elseif fmt == "f64be" then
local tmp = buffer.create(8)
for i = 0, count - 1 do
local p = so + i * 8
writeu32(tmp, 0, byteswap(readu32(src, p + 4)))
writeu32(tmp, 4, byteswap(readu32(src, p)))
writef32(dst, d + i * 4, readf64(tmp, 0))
end
elseif fmt == "alaw" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, ALAW[readu8(src, so + i) + 1] / 32768)
end
elseif fmt == "ulaw" then
for i = 0, count - 1 do
writef32(dst, d + i * 4, ULAW[readu8(src, so + i) + 1] / 32768)
end
else
return false
end
return true
end
-- ------------------------------------------------------------------------------------------------ encode
local function q(x: number, s: number): number
local v = floor(x * s + 0.5)
if v ~= v then
return 0
end
if v >= s then
return s - 1
elseif v < -s then
return -s
end
return v
end
-- encode(src, so, count, fmt, dst, do_) -> true | false: count f32 samples at src[so] -> fmt at dst[do_]
function Convert.encode(src: buffer, so: number, count: number, fmt: string, dst: buffer, d: number): boolean
if fmt == "s16le" then
for i = 0, count - 1 do
writei16(dst, d + i * 2, q(readf32(src, so + i * 4), 32768))
end
elseif fmt == "s16be" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 32768) % 65536
writeu16(dst, d + i * 2, (v % 256) * 256 + v // 256)
end
elseif fmt == "u8" then
for i = 0, count - 1 do
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) + 128)
end
elseif fmt == "s8" then
for i = 0, count - 1 do
writeu8(dst, d + i, q(readf32(src, so + i * 4), 128) % 256)
end
elseif fmt == "s24le" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
local p = d + i * 3
writeu8(dst, p, v % 256)
writeu8(dst, p + 1, (v // 256) % 256)
writeu8(dst, p + 2, v // 65536)
end
elseif fmt == "s24be" then
for i = 0, count - 1 do
local v = q(readf32(src, so + i * 4), 8388608) % 16777216
local p = d + i * 3
writeu8(dst, p, v // 65536)
writeu8(dst, p + 1, (v // 256) % 256)
writeu8(dst, p + 2, v % 256)
end
elseif fmt == "s32le" then
for i = 0, count - 1 do
writei32(dst, d + i * 4, q(readf32(src, so + i * 4), 2147483648))
end
elseif fmt == "s32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(q(readf32(src, so + i * 4), 2147483648) % 4294967296))
end
elseif fmt == "f32le" then
buffer.copy(dst, d, src, so, count * 4)
elseif fmt == "f32be" then
for i = 0, count - 1 do
writeu32(dst, d + i * 4, byteswap(readu32(src, so + i * 4)))
end
elseif fmt == "f64le" then
for i = 0, count - 1 do
writef64(dst, d + i * 8, readf32(src, so + i * 4))
end
elseif fmt == "f64be" then
local tmp = buffer.create(8)
for i = 0, count - 1 do
writef64(tmp, 0, readf32(src, so + i * 4))
local p = d + i * 8
writeu32(dst, p, byteswap(readu32(tmp, 4)))
writeu32(dst, p + 4, byteswap(readu32(tmp, 0)))
end
elseif fmt == "alaw" then
for i = 0, count - 1 do
writeu8(dst, d + i, alawEncode(q(readf32(src, so + i * 4), 32768)))
end
elseif fmt == "ulaw" then
for i = 0, count - 1 do
writeu8(dst, d + i, ulawEncode(q(readf32(src, so + i * 4), 32768)))
end
else
return false
end
return true
end
return Convert