codecs: JPEG / MPEG-1/2 yield hooks (per MCU row, per slice), banded Mpeg2.toRgba with RGB clamp table; build.py: reserved child names pass (work from the userspace session)
This commit is contained in:
1 parent
551fd937b9
commit
05eedbc8cf
10 files changed
+246
-14
No files matched your search
@@ -246,9 +246,13 @@ end
|
||||
-- Inverse-transforms every block of a component: coef (i16, 64 per block, natural order) on a bpl x bpc block
|
||||
-- grid, quantisation table q (64 u16, natural order), into plane (u16 samples, (bpl * size) per row) at size x
|
||||
-- size samples per block. p1 is PASS1_BITS (2 for 8-bit, 1 for 12-bit), center the level shift, maxv the top.
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number)
|
||||
-- hook (optional): a cooperative yield point after every block row (pcall'ed: it can't throw through here).
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?)
|
||||
local stride = bpl * size
|
||||
for by = 0, bpc - 1 do
|
||||
if hook and by > 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
for bx = 0, bpl - 1 do
|
||||
local off = (by * bpl + bx) * 128
|
||||
local out = by * size * stride + bx * size
|
||||
|
||||
@@ -60,6 +60,7 @@ export type Params = {
|
||||
crg: buffer,
|
||||
cbg: buffer,
|
||||
toByte: buffer?, -- maxv + 1 u8 entries (12-bit), nil for 8-bit
|
||||
hook: (() -> ())?, -- a cooperative yield point every 8 rows (pcall'ed: it can't throw through here)
|
||||
}
|
||||
|
||||
-- jdcolor.c build_ycc_rgb_table
|
||||
@@ -160,7 +161,11 @@ function Color.run(P: Params)
|
||||
local crr, cbb, crg, cbg = P.crr, P.cbb, P.crg, P.cbg
|
||||
local toByte = P.toByte
|
||||
local o = 0
|
||||
local hook = P.hook
|
||||
for y = 0, outH - 1 do
|
||||
if hook and y > 0 and y % 8 == 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
local s0, b0 = fetch(comps[1], y, outW)
|
||||
if space == 0 or nc < 3 then
|
||||
for x = 0, outW - 1 do
|
||||
|
||||
@@ -232,6 +232,7 @@ export type State = {
|
||||
scale: number,
|
||||
avi1: number?, -- the AVI1 APP0 field polarity (0 = progressive, 1 / 2 = interlaced: two fields back to back)
|
||||
endPos: number, -- the byte after the EOI marker (or the end of the data)
|
||||
hook: (() -> ())?, -- opts.yield: the cooperative yield point (scans, IDCT, colour conversion)
|
||||
}
|
||||
|
||||
-- parse(b, base, limit, opts, headerOnly) -> State | nil, code, message. Walks the markers in b[base, limit); with
|
||||
@@ -264,6 +265,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
|
||||
scale = scale,
|
||||
avi1 = nil,
|
||||
endPos = n,
|
||||
hook = if opts and type(opts.yield) == "function" then opts.yield else nil,
|
||||
}
|
||||
local iccChunks: { buffer } = {}
|
||||
local ri = 0
|
||||
@@ -527,6 +529,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
|
||||
ss = ss,
|
||||
se = se,
|
||||
al = al,
|
||||
hook = st.hook,
|
||||
dcL = dcL,
|
||||
dcU = dcU,
|
||||
acK = acK,
|
||||
@@ -609,7 +612,7 @@ function Decode.output(st: State): (number?, any, any, string?)
|
||||
end
|
||||
q = zeroQ -- the component never appeared in a scan: mid grey
|
||||
end
|
||||
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv)
|
||||
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv, st.hook)
|
||||
local hIn, hOut = c.h * ssize // minSize, f.hmax
|
||||
local vIn, vOut = c.v * ssize // minSize, f.vmax
|
||||
local downW = math.ceil(f.width * c.h * ssize / (f.hmax * 8))
|
||||
@@ -674,6 +677,7 @@ function Decode.output(st: State): (number?, any, any, string?)
|
||||
crg = t[3],
|
||||
cbg = t[4],
|
||||
toByte = tb,
|
||||
hook = st.hook,
|
||||
})
|
||||
return outW, outH, pixels, space
|
||||
end
|
||||
@@ -746,7 +750,7 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
|
||||
zeroQ = zeroQ or buffer.create(128) -- the component never appeared in a scan: mid grey
|
||||
q = zeroQ
|
||||
end
|
||||
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255)
|
||||
Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255, st.hook)
|
||||
if i == 1 then
|
||||
Yuv.crop(plane, stride, w, h, out, 0)
|
||||
elseif mode == "420" then
|
||||
|
||||
@@ -374,6 +374,7 @@ export type ScanParams = {
|
||||
ss: number,
|
||||
se: number,
|
||||
al: number,
|
||||
hook: (() -> ())?, -- a cooperative yield point after every MCU row (pcall'ed: it can't throw through here)
|
||||
}
|
||||
|
||||
-- Decodes one scan from the clean data d (cleanLen bytes, then PAD zeros). Returns the final bit position and
|
||||
@@ -397,7 +398,11 @@ function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P:
|
||||
local pos, eobrun, rstIdx = 0, 0, 0
|
||||
local truncated, skipping = false, false
|
||||
local bit = lshift(1, al)
|
||||
local hook = P.hook
|
||||
for u = 0, units - 1 do
|
||||
if hook and u > 0 and u % ux == 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
if ri > 0 and u > 0 and u % ri == 0 then
|
||||
rstIdx += 1
|
||||
if rstIdx <= nrst then
|
||||
|
||||
@@ -45,6 +45,10 @@ export type DecodeOptions = {
|
||||
fields: boolean?, -- two JPEGs back to back are two fields of one frame (default: when the AVI1 segment says so)
|
||||
raw: boolean?, -- no colour conversion: 3 / 4 components as R, G, B (, A) bytes (default false; BLP1 BGRA JPEGs)
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
-- a cooperative yield point, run after every MCU row of a scan, every block row of the IDCT and every 8 output
|
||||
-- rows: e.g. a function that calls coroutine.yield() when a time budget is spent, so one image is decoded over
|
||||
-- several frames (decode then runs inside a coroutine). Errors in it are ignored.
|
||||
yield: (() -> ())?,
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
quality: number?, -- 1..100 (default 90), libjpeg's scaling of the T.81 annex K tables
|
||||
|
||||
@@ -83,17 +83,32 @@ local RBIAS = COFF * 256 - 298 * 16 - 409 * 128 - 128
|
||||
local GBIAS = COFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
|
||||
local BBIAS = COFF * 256 - 298 * 16 - 516 * 128 - 72
|
||||
|
||||
-- YCbCr 4:2:0 planes (coded strides) -> RGBA8 (alpha 255), w x h, nearest chroma like ffmpeg's default swscale path
|
||||
local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer)
|
||||
-- clamp to 0..255 by lookup: RGB_CLAMP[v + COFF] = clamp(v, 0, 255) for v in [-COFF, COFF + 255] (the colour biases
|
||||
-- include COFF * 256, so the shifted sums below are indices): the same values as math.clamp, 2-3x faster natively
|
||||
local RGB_CLAMP = buffer.create(2 * COFF + 256)
|
||||
for i = 0, 2 * COFF + 255 do
|
||||
buffer.writeu8(RGB_CLAMP, i, math.clamp(i - COFF, 0, 255))
|
||||
end
|
||||
|
||||
-- YCbCr 4:2:0 planes (coded strides) -> RGBA8 (alpha 255), w x h, nearest chroma like ffmpeg's default swscale path.
|
||||
-- Rows row0 .. row1 - 1 of the planes (row0 even; default 0 .. h) into out from byte oo (default 0). hook (optional)
|
||||
-- runs every 16 rows (a cooperative yield point; it is pcall'ed, so it can't throw through here). (From mpeg1-luau's
|
||||
-- later banded conversion.)
|
||||
local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer, row0: number?, row1: number?, oo: number?, hook: (() -> ())?)
|
||||
-- 2 x 2 pixels per chroma sample: the chroma terms are computed once per block of four
|
||||
local r0: number = row0 or 0
|
||||
local r1: number = row1 or h
|
||||
local base: number = oo or 0
|
||||
local chw = cw // 2
|
||||
local w2 = w // 2 -- whole pixel pairs per row
|
||||
local stride = w * 4
|
||||
for y = 0, h - 1, 2 do
|
||||
local C = RGB_CLAMP
|
||||
local n = 0
|
||||
for y = r0, r1 - 1, 2 do
|
||||
local yo = y * cw
|
||||
local co = (y // 2) * chw
|
||||
local o = y * stride
|
||||
local two = y + 1 < h
|
||||
local o = base + (y - r0) * stride
|
||||
local two = y + 1 < r1
|
||||
for _ = 1, w2 do
|
||||
local cb, cr = readu8(Cb, co), readu8(Cr, co)
|
||||
co += 1
|
||||
@@ -101,14 +116,14 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
|
||||
local gg = GBIAS - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + BBIAS
|
||||
local yv = readu8(Y, yo) * 298
|
||||
writeu32(out, o, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
|
||||
writeu32(out, o, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
yv = readu8(Y, yo + 1) * 298
|
||||
writeu32(out, o + 4, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
|
||||
writeu32(out, o + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
if two then
|
||||
yv = readu8(Y, yo + cw) * 298
|
||||
writeu32(out, o + stride, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
|
||||
writeu32(out, o + stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
yv = readu8(Y, yo + cw + 1) * 298
|
||||
writeu32(out, o + stride + 4, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
|
||||
writeu32(out, o + stride + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
end
|
||||
yo += 2
|
||||
o += 8
|
||||
@@ -121,9 +136,14 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
|
||||
local bb = cb * 516 + BBIAS
|
||||
for k = 0, if two then 1 else 0 do
|
||||
local yv = readu8(Y, yo + k * cw) * 298
|
||||
writeu32(out, o + k * stride, math.clamp(rshift(yv + rr, 8) - COFF, 0, 255) + math.clamp(rshift(yv + gg, 8) - COFF, 0, 255) * 256 + math.clamp(rshift(yv + bb, 8) - COFF, 0, 255) * 65536 + 0xFF000000)
|
||||
writeu32(out, o + k * stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
n += 2
|
||||
if hook and n >= 16 then
|
||||
n = 0
|
||||
pcall(hook)
|
||||
end
|
||||
end
|
||||
end
|
||||
D.toRgba = toRgba
|
||||
@@ -175,6 +195,9 @@ export type Core = {
|
||||
nextPicture: (atEnd: boolean, tag: any) -> (Store?, boolean),
|
||||
toRgba: (s: Store, out: buffer) -> (),
|
||||
reset: () -> (),
|
||||
-- a cooperative yield point (e.g. coroutine.yield when over a time budget), run after every slice and every 16
|
||||
-- rows of colour conversion; nil = none. pcall'ed: it can't throw through the native code.
|
||||
setYield: (hook: (() -> ())?) -> (),
|
||||
state: {
|
||||
width: number,
|
||||
height: number,
|
||||
@@ -290,6 +313,7 @@ function D.new(maxPixels: number?): Core
|
||||
local PMV = table.create(8, 0)
|
||||
local FSEL = table.create(4, 0)
|
||||
local blk = buffer.create(256)
|
||||
local yieldHook: (() -> ())? = nil
|
||||
|
||||
local function peek32(): number
|
||||
local bp = rshift(pos, 3)
|
||||
@@ -1382,6 +1406,11 @@ function D.new(maxPixels: number?): Core
|
||||
pos = (at + 4) * 8
|
||||
err = false
|
||||
decodeSlice(code, stop)
|
||||
local hook = yieldHook
|
||||
if hook then
|
||||
-- the hook may suspend this coroutine: the picture state is all upvalues of this core
|
||||
pcall(hook)
|
||||
end
|
||||
local nb = rshift(pos + 7, 3)
|
||||
if nb >= stop then
|
||||
break
|
||||
@@ -1510,10 +1539,14 @@ function D.new(maxPixels: number?): Core
|
||||
if s.chroma == 2 then
|
||||
toRgba422(s.y, s.cb, s.cr, s.cw, s.w, s.h, out)
|
||||
else
|
||||
toRgba(s.y, s.cb, s.cr, s.cw, s.w, s.h, out)
|
||||
toRgba(s.y, s.cb, s.cr, s.cw, s.w, s.h, out, 0, s.h, 0, yieldHook)
|
||||
end
|
||||
end
|
||||
|
||||
function core.setYield(hook: (() -> ())?)
|
||||
yieldHook = hook
|
||||
end
|
||||
|
||||
function core.reset()
|
||||
dataLen = 0
|
||||
readByte = 0
|
||||
|
||||
@@ -58,6 +58,11 @@ export type Options = {
|
||||
-- display order for streams without B-frames.
|
||||
rgba: boolean?, -- default true; false skips the RGBA conversion (use frame.y / cb / cr)
|
||||
maxPixels: number?, -- larger sequence headers are rejected (default 4096 * 2304)
|
||||
-- a cooperative yield point, run after every slice and every 16 rows of colour conversion: e.g. a function that
|
||||
-- calls coroutine.yield() when a time budget is spent, to spread a picture over several frames (the decode call
|
||||
-- then runs inside a coroutine). Don't call this decoder again while a decode is suspended. Errors in it are
|
||||
-- ignored. See dec:setYield.
|
||||
yield: (() -> ())?,
|
||||
}
|
||||
|
||||
local Decoder = {}
|
||||
@@ -83,6 +88,9 @@ export type Decoder = typeof(setmetatable(
|
||||
function Mpeg2.new(opts: Options?): Decoder
|
||||
local o: Options = if type(opts) == "table" then opts else {}
|
||||
local core = D.new(o.maxPixels)
|
||||
if type(o.yield) == "function" then
|
||||
core.setYield(o.yield)
|
||||
end
|
||||
return setmetatable({
|
||||
_core = core,
|
||||
_reorder = o.reorder == true,
|
||||
@@ -232,12 +240,54 @@ function Decoder.flush(self: Decoder): Frame?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Sets (or clears, nil) the cooperative yield hook (Options.yield).
|
||||
function Decoder.setYield(self: Decoder, hook: (() -> ())?)
|
||||
self._core.setYield(if type(hook) == "function" then hook else nil)
|
||||
end
|
||||
|
||||
-- Forgets buffered data and references (after a seek: the next picture decoded is the next I-picture).
|
||||
function Decoder.reset(self: Decoder)
|
||||
self._core.reset()
|
||||
self._held = nil
|
||||
end
|
||||
|
||||
-- YCbCr 4:2:0 planes (a frame's y / cb / cr / codedWidth, or copies of them) -> RGBA8 rows row0 .. row1 - 1 of a
|
||||
-- width x height picture, written to out from byte outOffset (default 0) as (row1 - row0) rows of width * 4 bytes.
|
||||
-- row0 is rounded down to even (rows share chroma in pairs); defaults 0 and height. Lets the conversion run in
|
||||
-- bands (on several threads) or only for the frames that are shown. The planes may hold only rows planeRow0 ..
|
||||
-- (planeRow0 even, default 0; their row 0 is picture row planeRow0). hook: a cooperative yield point every 16 rows.
|
||||
-- Returns false (writing nothing) when an argument is out of range. (4:2:0 only: a 4:2:2 frame's chroma has as many
|
||||
-- rows as its luma; use frameImage(frame, true) for those.)
|
||||
function Mpeg2.toRgba(y: buffer, cb: buffer, cr: buffer, codedWidth: number, width: number, height: number, out: buffer, row0: number?, row1: number?, outOffset: number?, planeRow0: number?, hook: (() -> ())?): boolean
|
||||
if type(y) ~= "buffer" or type(cb) ~= "buffer" or type(cr) ~= "buffer" or type(out) ~= "buffer" then
|
||||
return false
|
||||
end
|
||||
local cw, w, h = tonumber(codedWidth), tonumber(width), tonumber(height)
|
||||
if not cw or not w or not h or cw % 2 ~= 0 or w < 1 or h < 1 or w > cw or cw ~= math.floor(cw) or w ~= math.floor(w) or h ~= math.floor(h) then
|
||||
return false
|
||||
end
|
||||
local r0 = math.floor(tonumber(row0) or 0)
|
||||
r0 -= r0 % 2
|
||||
local r1 = math.floor(tonumber(row1) or h)
|
||||
local pr = math.floor(tonumber(planeRow0) or 0)
|
||||
local oo = math.floor(tonumber(outOffset) or 0)
|
||||
if r0 < 0 or r1 > h or r1 <= r0 or pr < 0 or pr % 2 ~= 0 or pr > r0 or oo < 0 or oo % 4 ~= 0 then
|
||||
return false
|
||||
end
|
||||
-- the planes must cover rows pr .. r1 - 1 (luma) and their chroma rows; out the band
|
||||
local lumaRows = r1 - pr
|
||||
local chromaRows = (r1 + 1) // 2 - pr // 2
|
||||
if buffer.len(y) < lumaRows * cw or buffer.len(cb) < chromaRows * (cw // 2) or buffer.len(cr) < chromaRows * (cw // 2) then
|
||||
return false
|
||||
end
|
||||
if buffer.len(out) < oo + (r1 - r0) * w * 4 then
|
||||
return false
|
||||
end
|
||||
-- rows relative to the planes' first row
|
||||
D.toRgba(y, cb, cr, cw, w, h, out, r0 - pr, r1 - pr, oo, if type(hook) == "function" then hook else nil)
|
||||
return true
|
||||
end
|
||||
|
||||
-- frameImage(frame, rgba?) -> Types.Image: a copy of a decoded frame (the decoder reuses its planes), cropped to
|
||||
-- width x height: yuv420p from frame.y / cb / cr (BT.601 limited range, meta.source.range = "limited"; 4:2:2 chroma
|
||||
-- averaged over line pairs of the same field), or rgba8 (the decoder's BT.601 conversion) when rgba is true. nil for
|
||||
|
||||
Reference in new issue
Block a user