codecs/video: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
8e78869f0e
36 files changed
+219
-809
No files matched your search
@@ -1,24 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/video/Mpeg2/Decoder: the MPEG-1 / MPEG-2 video decoding core (native, never throws).
|
||||
-- ISO/IEC 11172-2 and ISO/IEC 13818-2 Main profile: I, P and B pictures; progressive and interlaced sequences; frame
|
||||
-- pictures (frame / field motion compensation, frame / field DCT) and field pictures (field and 16x8 motion
|
||||
-- compensation, the second field predicted from the first); dual-prime; 4:2:0 and 4:2:2; intra DC precision 8..11;
|
||||
-- linear and non-linear quantiser scales; Table B-15 intra VLCs; the alternate scan; custom (and chroma) quantiser
|
||||
-- matrices; mismatch control; concealment motion vectors; sequence headers anywhere (size changes). Not supported:
|
||||
-- 4:4:4, the scalable / multiview extensions, D-pictures (skipped).
|
||||
--
|
||||
-- Grown from the MPEG-1 core absorbed from mpeg1-luau e2e2898 (same author; a port of jsmpeg's decoder, src/mpeg1.js
|
||||
-- by Dominic Szablewski, MIT, licenses/jsmpeg-MIT.txt, with table-driven VLCs, B-pictures and SWAR motion
|
||||
-- compensation). The IDCT and motion compensation are codecs/video/Dsp (the IDCT bit-exact with ffmpeg's -idct int),
|
||||
-- and dequantisation follows ffmpeg's arithmetic, so the output planes equal `ffmpeg -idct int` decoding for valid
|
||||
-- streams (tests/codecs/mpeg2.luau, tests/codecs/mpeg1.luau). Coefficients are saturated to -2048..2047 (13818-2
|
||||
-- 7.4.3; ffmpeg omits it, valid streams never reach it) and motion vectors pointing outside the reference are
|
||||
-- clamped (ffmpeg skips the prediction): both only matter for corrupt data.
|
||||
--
|
||||
-- Everything works on flat buffers allocated per sequence size: no allocation per block, macroblock or picture.
|
||||
-- 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.
|
||||
-- codecs/video/Mpeg2/Decoder: the MPEG-1 / MPEG-2 video decoding core (ISO/IEC 11172-2, 13818-2 Main profile).
|
||||
-- From mpeg1-luau e2e2898 (same author; a port of jsmpeg's src/mpeg1.js by Dominic Szablewski, MIT,
|
||||
-- licenses/jsmpeg-MIT.txt). Dequantisation follows ffmpeg's arithmetic; coefficients are saturated to -2048..2047
|
||||
-- (13818-2 7.4.3; ffmpeg omits it) and vectors outside the reference are clamped (ffmpeg skips the prediction).
|
||||
local V = require("@rosetta/codecs/video/Mpeg2/Vlc")
|
||||
local T = require("@rosetta/codecs/video/Mpeg2/Tables")
|
||||
local Dsp = require("@rosetta/codecs/video/Dsp")
|
||||
@@ -83,17 +68,13 @@ 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
|
||||
|
||||
-- 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
|
||||
-- clamp by lookup: RGB_CLAMP[v + COFF] for v in [-COFF, COFF + 255]; the colour biases include COFF * 256
|
||||
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.)
|
||||
-- 4:2:0 -> RGBA8 with nearest chroma, like ffmpeg's default swscale path; hook is pcall'ed (can't throw through here)
|
||||
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
|
||||
@@ -187,16 +168,10 @@ export type Store = {
|
||||
}
|
||||
|
||||
export type Core = {
|
||||
-- data
|
||||
write: (src: buffer, off: number, n: number) -> (),
|
||||
-- decodes the next picture; atEnd: the end of the data ends the picture. Returns the store decoded into (nil when
|
||||
-- no picture is complete or it could not be decoded), and whether more data is needed. A frame coded as two field
|
||||
-- pictures comes back once, after its second field.
|
||||
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,
|
||||
@@ -244,7 +219,6 @@ function D.new(maxPixels: number?): Core
|
||||
refNew = nil :: Store?,
|
||||
}
|
||||
|
||||
-- bitstream
|
||||
local data = buffer.create(64 * 1024 + PAD)
|
||||
local dataLen = 0
|
||||
local readByte = 0 -- next byte to look at for a start code
|
||||
@@ -252,7 +226,6 @@ function D.new(maxPixels: number?): Core
|
||||
local pos = 0 -- bit position inside data
|
||||
local err = false
|
||||
|
||||
-- sequence
|
||||
local hasSeq = false
|
||||
local mpeg2 = false
|
||||
local chroma = 1 -- 1 = 4:2:0, 2 = 4:2:2
|
||||
@@ -273,7 +246,6 @@ function D.new(maxPixels: number?): Core
|
||||
-- the sequence header's values, applied (and the stores allocated) once its extension has been seen
|
||||
local seqW, seqH = 0, 0
|
||||
|
||||
-- picture
|
||||
local ptype = 0
|
||||
local fc00, fc01, fc10, fc11 = 1, 1, 1, 1 -- f_code[s][t]
|
||||
local fullPelFw, fullPelBw = false, false
|
||||
@@ -299,7 +271,6 @@ function D.new(maxPixels: number?): Core
|
||||
local pendingFw: Store? = nil
|
||||
local pendingBw: Store? = nil
|
||||
|
||||
-- slice / macroblock
|
||||
local qscale = 2
|
||||
local mbAddr, mbRow, mbCol = 0, 0, 0
|
||||
local dcY, dcCb, dcCr = 128, 128, 128
|
||||
@@ -342,7 +313,6 @@ function D.new(maxPixels: number?): Core
|
||||
return band(e, 0xFFFF) - 32768
|
||||
end
|
||||
|
||||
-- byte offset of the next 00 00 01 xx at or after from (with xx before to), or -1
|
||||
local function findStart(from: number, to: number): number
|
||||
local i = from
|
||||
while i + 3 < to do
|
||||
@@ -396,7 +366,6 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- the sequence (header + extension) takes effect: sizes and stores
|
||||
local function applySequence(): boolean
|
||||
local w, h = seqW, seqH
|
||||
if w == 0 or h == 0 or w * h > state.maxPixels then
|
||||
@@ -428,7 +397,6 @@ function D.new(maxPixels: number?): Core
|
||||
return true
|
||||
end
|
||||
|
||||
-- sequence header at byte p (after the start code); false when invalid
|
||||
local function sequenceHeader(p: number): boolean
|
||||
pos = p * 8
|
||||
local w = read(12)
|
||||
@@ -545,11 +513,6 @@ function D.new(maxPixels: number?): Core
|
||||
return (pred + d + range) % (2 * range) - range
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ prediction
|
||||
-- One prediction of the current macroblock part from the reference planes rY / rCb / rCr: field-based (sp >= 0:
|
||||
-- source field sp, destination field dp of the frame, line stride 2 * cw) or frame-based (sp < 0); rowY / h: the
|
||||
-- first luma line of the part in those lines and its height; (mvx, mvy) in half samples; avg averages into the
|
||||
-- first prediction.
|
||||
local EDGE = buffer.create(Dsp.EDGE_SIZE)
|
||||
local function mcPart(rY: buffer, rCb: buffer, rCr: buffer, sp: number, dp: number, rowY: number, h: number, mvx: number, mvy: number, avg: boolean)
|
||||
local stride, sb, db, ph = cw, 0, 0, lumaH
|
||||
@@ -558,7 +521,6 @@ function D.new(maxPixels: number?): Core
|
||||
stride, sb, db, ph = 2 * cw, sp * cw, dp * cw, lumaH // 2
|
||||
cs, csb, cdb = 2 * chw, sp * chw, dp * chw
|
||||
end
|
||||
-- luma
|
||||
local hx, hy = band(mvx, 1), band(mvy, 1)
|
||||
local sx = mbCol * 16 + (mvx - hx) // 2
|
||||
local sy = rowY + (mvy - hy) // 2
|
||||
@@ -623,7 +585,6 @@ function D.new(maxPixels: number?): Core
|
||||
return (bwS or cur) :: Store
|
||||
end
|
||||
|
||||
-- the prediction of direction d (0 forward, 1 backward); avg: the second one of a bidirectional macroblock
|
||||
local function predictDir(d: number, avg: boolean)
|
||||
local o = d * 8
|
||||
if not fieldPic then
|
||||
@@ -667,7 +628,6 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- the prediction of a non-intra macroblock from the current motion state
|
||||
local function predictMb()
|
||||
if ptype == 2 then
|
||||
predictDir(0, false)
|
||||
@@ -701,10 +661,8 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- Macroblocks no slice covers. ffmpeg's encoder skips the last macroblock of a row at the end of an MPEG-1
|
||||
-- slice (the next slice starts on the next row), and ffmpeg's decoder conceals it as a skipped macroblock; so do
|
||||
-- we: P (and a damaged I) copies the reference with a zero vector, B repeats the prediction of the macroblock
|
||||
-- before the gap.
|
||||
-- macroblocks no slice covers: ffmpeg's encoder skips a row's last macroblock at an MPEG-1 slice end and its decoder
|
||||
-- conceals it as skipped; so do we (P / damaged I: zero-vector copy, B: the prediction before the gap)
|
||||
local covered = 0
|
||||
local gapFw, gapBw = false, false
|
||||
local GAP = table.create(16, 0)
|
||||
@@ -761,9 +719,7 @@ function D.new(maxPixels: number?): Core
|
||||
FSEL[1], FSEL[2], FSEL[3], FSEL[4] = f1, f2, f3, f4
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ blocks
|
||||
-- one 8x8 block b (0..3 luma, then Cb / Cr alternating): coefficients, dequantisation, IDCT into the picture.
|
||||
-- False on a bitstream error.
|
||||
-- block b: 0..3 luma, then Cb / Cr alternating
|
||||
local function decodeBlock(b: number): boolean
|
||||
local n = 0
|
||||
local q: buffer
|
||||
@@ -828,7 +784,6 @@ function D.new(maxPixels: number?): Core
|
||||
local qs = qscale
|
||||
local m2 = mpeg2
|
||||
while true do
|
||||
-- inline peek
|
||||
local bp = rshift(pos, 3)
|
||||
local w = bswap(readu32(data, bp))
|
||||
local s = band(pos, 7)
|
||||
@@ -911,7 +866,7 @@ function D.new(maxPixels: number?): Core
|
||||
if m2 then
|
||||
parityOdd = bxor(parityOdd, band(m, 1))
|
||||
end
|
||||
-- saturate
|
||||
-- saturation (13818-2 7.4.3)
|
||||
if m > 2047 then
|
||||
m = 2047
|
||||
elseif m < -2048 then
|
||||
@@ -929,7 +884,6 @@ function D.new(maxPixels: number?): Core
|
||||
writei32(blk, 252, if v % 2 ~= 0 then v - 1 else v + 1)
|
||||
onlyDc = false
|
||||
end
|
||||
-- reconstruction
|
||||
local dst, off, stride
|
||||
if luma then
|
||||
dst = curY
|
||||
@@ -973,7 +927,6 @@ function D.new(maxPixels: number?): Core
|
||||
return true
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ macroblocks
|
||||
local function getQscale(code: number): number
|
||||
if qScaleType == 1 then
|
||||
return readu8(NON_LINEAR, code)
|
||||
@@ -1051,7 +1004,6 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- one macroblock (with the skipped ones before it). False on a bitstream error or the end of the slice data.
|
||||
local function decodeMacroblock(first: boolean, endBits: number): boolean
|
||||
local inc = 0
|
||||
local t = vlc(MBAI, MBAI_BITS)
|
||||
@@ -1253,7 +1205,6 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ pictures
|
||||
-- the frame a picture completes becomes the newest reference (I / P)
|
||||
local function finishFrame(s: Store)
|
||||
if s.ptype ~= 3 then
|
||||
@@ -1262,8 +1213,7 @@ function D.new(maxPixels: number?): Core
|
||||
end
|
||||
end
|
||||
|
||||
-- The picture header at byte p (after 00 00 01 00) and its extensions, up to stop: the byte of the first slice
|
||||
-- start code (-1: none) and the temporal reference, or nil when the picture is skipped
|
||||
-- the picture header (after 00 00 01 00) and its extensions
|
||||
local function pictureHeader(p: number, stop: number): (number?, number)
|
||||
pos = p * 8
|
||||
local tref = read(10)
|
||||
@@ -1316,7 +1266,6 @@ function D.new(maxPixels: number?): Core
|
||||
return at, tref
|
||||
end
|
||||
|
||||
-- the frame the picture decodes into (the pending one for a second field) and its references; false: skipped
|
||||
local function selectTarget(tref: number, tag: any): boolean
|
||||
fieldPic = picStruct ~= 3
|
||||
parity = if picStruct == 2 then 1 else 0
|
||||
@@ -1370,7 +1319,6 @@ function D.new(maxPixels: number?): Core
|
||||
return true
|
||||
end
|
||||
|
||||
-- plane addressing of the current picture
|
||||
local function setAddressing()
|
||||
local target = cur :: Store
|
||||
curY, curCb, curCr = target.y, target.cb, target.cr
|
||||
@@ -1390,15 +1338,12 @@ function D.new(maxPixels: number?): Core
|
||||
mbIntra = false
|
||||
end
|
||||
|
||||
-- The picture whose header starts at byte p (after 00 00 01 00) and ends at byte stop: the decoded frame, or nil
|
||||
-- (skipped, or the first field of a frame).
|
||||
local function decodePicture(p: number, stop: number, tag: any): Store?
|
||||
local first, tref = pictureHeader(p, stop)
|
||||
if first == nil or not selectTarget(tref, tag) then
|
||||
return nil
|
||||
end
|
||||
setAddressing()
|
||||
-- slices
|
||||
local at = first :: number
|
||||
while at >= 0 do
|
||||
local code = readu8(data, at + 3)
|
||||
@@ -1443,7 +1388,6 @@ function D.new(maxPixels: number?): Core
|
||||
if n <= 0 then
|
||||
return
|
||||
end
|
||||
-- drop consumed bytes, keeping the scan state relative
|
||||
if readByte > 0 then
|
||||
local keep = dataLen - readByte
|
||||
if keep > 0 then
|
||||
@@ -1468,7 +1412,6 @@ function D.new(maxPixels: number?): Core
|
||||
buffer.fill(data, dataLen, 0, PAD)
|
||||
end
|
||||
|
||||
-- the sequence header unit at byte at (up to stop): the header, then its extensions
|
||||
local function sequenceUnit(at: number, stop: number)
|
||||
local ok = sequenceHeader(at + 4)
|
||||
local s = findStart(at + 4, stop)
|
||||
|
||||
Reference in new issue
Block a user