ws-video2-flv: H263 slice structure (Annex K, ffmpeg h263p with slice threads), MV prediction across slice starts as ffmpeg

This commit is contained in:
4DBug committed 2026-10-31 14:56:51 -05:00
1 parent 625c1fade2
commit 2a5da31c80
3 files changed
+119 -47

No files matched your search

+108 -41
View File
@@ -8,11 +8,12 @@
-- custom pixel aspect ratios and picture clock frequencies, the rounding type RTYPE), I and P pictures, GOB headers
-- (with or without stuffing), MCBPC / CBPY / MVD / TCOEF VLCs, DQUANT, the H.263 dequantiser, half-sample motion
-- compensation with the H.263 chroma vector rounding, unrestricted motion vectors (Annex D in both header forms, the
-- PLUSPTYPE form with its reversible MVD codes and unlimited range) and the extended escape of Annex T. Sorenson Spark
-- PLUSPTYPE form with its reversible MVD codes and unlimited range), slice structure (Annex K, without rectangular
-- slices) and the extended escape of Annex T. Sorenson Spark
-- (FLV1): its picture header (sizes, disposable inter pictures, which never become the reference), its 7 / 11-bit
-- escape codes (version 1), unrestricted vectors. Not supported (the picture is refused with an "unsupported:" message):
-- syntax-based arithmetic coding, advanced prediction (Annex F: four vectors and OBMC), PB / B / EI / EP pictures,
-- advanced intra coding, the deblocking filter, slice structure, reference picture selection / resampling, reduced
-- advanced intra coding, the deblocking filter, rectangular slices, reference picture selection / resampling, reduced
-- resolution update, independent segments, the alternative inter VLC, modified quantisation (other than the escape).
--
-- The IDCT and motion compensation are codecs/video/Dsp, the dequantiser is the H.263 one (|REC| = QUANT * (2 |LEVEL|
@@ -65,6 +66,7 @@ export type Opp = {
format: number,
customPcf: boolean,
umv: boolean,
ss: boolean, -- slice structured (Annex K)
options: string?, -- the unsupported options set, or nil
width: number,
height: number,
@@ -83,6 +85,7 @@ export type Header = {
umvPlus: boolean, -- unrestricted vectors, PLUSPTYPE form
rounding: number, -- RTYPE (0: half-sample averages round up)
plus: boolean, -- PLUSPTYPE
ss: boolean, -- slice structured (Annex K)
cpm: boolean,
deblock: boolean, -- Sorenson's deblocking hint (not applied, as ffmpeg)
aspect: number?, -- PLUSPTYPE pixel aspect ratio code (15: aspectNum / aspectDen)
@@ -105,6 +108,11 @@ export type Store = {
}
-- ------------------------------------------------------------------------------------------------ header
-- the length of a slice's MBA field for a picture of n macroblocks (table K.2)
function D.mbaBits(n: number): number
return if n <= 48 then 6 elseif n <= 99 then 7 elseif n <= 396 then 9 elseif n <= 1584 then 11 elseif n <= 6336 then 13 else 14
end
-- n (<= 24) bits at bit position p of buf, zeros past endByte
local function bitsAt(buf: buffer, endByte: number, p: number, n: number): number
local v = 0
@@ -158,6 +166,7 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
umvPlus = false,
rounding = 0,
plus = false,
ss = false,
cpm = false,
deblock = false,
bitPos = 0,
@@ -222,13 +231,18 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
"deblocking filter", "slice structure", "reference picture selection", "independent segment decoding",
"alternative inter VLC", "modified quantisation" }
local unsup: string? = nil
local ss = false
for i = 1, 9 do
if bits(1) == 1 and not unsup then
unsup = names[i]
if bits(1) == 1 then
if i == 5 then
ss = true
elseif not unsup then
unsup = names[i]
end
end
end
bits(4) -- 1 000
o = { format = f, customPcf = pcf, umv = umv, options = unsup, width = 0, height = 0 }
o = { format = f, customPcf = pcf, umv = umv, ss = ss, options = unsup, width = 0, height = 0 }
elseif ufep == 0 and opp then
o = opp
else
@@ -283,8 +297,15 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
bits(1)
end
end
if ufep == 1 and o.ss then
if bits(1) == 1 then
return nil, "unsupported: rectangular slices"
end
bits(1) -- arbitrary slice ordering (slices carry their address)
end
h.width, h.height = o.width, o.height
h.umvPlus = o.umv
h.ss = o.ss
h.opp = o
else
return nil, "bad source format " .. format
@@ -306,6 +327,12 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
if h.width < 1 or h.height < 1 then
return nil, "empty picture size"
end
if h.ss then
-- the first slice's SEPB1, MBA and SEPB2
bits(1)
bits(D.mbaBits((h.width + 15) // 16 * ((h.height + 15) // 16)))
bits(1)
end
if p > e * 8 then
return nil, "truncated picture header"
end
@@ -609,9 +636,10 @@ function D.new(flv: boolean, maxPixels: number?): Core
end
end
-- macroblock (col, row) of a picture (P: an inter picture; top: the first row of the picture or of a GOB with a
-- header, whose vector predictions do not look up). false on bad data.
local function macroblock(col: number, row: number, P: boolean, top: boolean): boolean
-- macroblock (col, row) of a picture (P: an inter picture; top: in the first row of the picture or of a GOB /
-- slice with a header, whose vector predictions do not look up; first: the first macroblock of a slice, whose
-- left neighbour is outside). false on bad data.
local function macroblock(col: number, row: number, P: boolean, top: boolean, first: boolean): boolean
if pos > endBits then
return false
end
@@ -662,8 +690,8 @@ function D.new(flv: boolean, maxPixels: number?): Core
cbpy = 15 - cbpy
-- the vector prediction: the median of left, above and above right (outside or past the right edge: 0; the
-- first row of a picture / GOB: the left vector)
local ax = if col > 0 then mvX[col] else 0
local ay = if col > 0 then mvY[col] else 0
local ax = if col > 0 and not first then mvX[col] else 0
local ay = if col > 0 and not first then mvY[col] else 0
local px, py = ax, ay
if not top then
local bx, by = upX[col + 1], upY[col + 1]
@@ -690,23 +718,25 @@ function D.new(flv: boolean, maxPixels: number?): Core
return pos <= endBits
end
-- an optional GOB header at a GOB boundary (GBSC, after zero stuffing to a byte boundary or not): its GN and
-- GQUANT, or nil
local function gobHeader(cpm: boolean): (number?, number?)
local start = pos
-- a GBSC / SSC (17 bits 0..01) next, after zero stuffing to a byte boundary or not: consumed, true
local function startCode(): boolean
local w = peek32()
if rshift(w, 15) ~= 1 then
local s = (8 - pos % 8) % 8
if s == 0 or rshift(w, 32 - s) ~= 0 then
return nil, nil
if s == 0 or rshift(w, 32 - s) ~= 0 or rshift(lshift(w, s), 15) ~= 1 then
return false
end
pos += s
if rshift(peek32(), 15) ~= 1 then
pos = start
return nil, nil
end
end
pos += 17
return true
end
-- an optional GOB header at a GOB boundary: its GN and GQUANT, or nil
local function gobHeader(cpm: boolean): (number?, number?)
if not startCode() then
return nil, nil
end
local gn = read(5)
if cpm then
read(2)
@@ -715,6 +745,24 @@ function D.new(flv: boolean, maxPixels: number?): Core
return gn, read(5)
end
-- an optional slice header (Annex K, no rectangular slices): its MBA and SQUANT, or nil
local function sliceHeader(cpm: boolean, mbaBits: number, sepb2: boolean): (number?, number?)
if not startCode() then
return nil, nil
end
read(1) -- SEPB1
if cpm then
read(2)
end
local mba = read(mbaBits)
if sepb2 then
read(1)
end
local q = read(5)
read(3) -- SEPB3, GFID
return mba, q
end
local function picture(hdr: Header): (Store?, string?)
if hdr.width ~= W or hdr.height ~= H then
if hdr.width * hdr.height > state.maxPixels then
@@ -740,36 +788,55 @@ function D.new(flv: boolean, maxPixels: number?): Core
pos = hdr.bitPos
err = false
local gobRows = if hdr.flv then 0 elseif H <= 400 then 1 elseif H <= 800 then 2 else 4
local top = 0 -- the first row of the current GOB with a header
local row = 0
local ss = hdr.ss
local total = mbW * mbH
local mbaBits = D.mbaBits(total)
local resync = 0 -- the first macroblock of the current GOB with a header / slice
local k = 0
local failAt = -1
while row < mbH do
if gobRows > 0 and row > 0 and row % gobRows == 0 then
local gn, gq = gobHeader(hdr.cpm)
if gn and gq then
local at = gn * gobRows
if gn == 0 or at >= mbH or at < row or gq == 0 then
failAt = row * mbW
mvX, upX = upX, mvX
mvY, upY = upY, mvY
while k < total do
local col, row = k % mbW, k // mbW
if k > 0 and (ss or (gobRows > 0 and col == 0 and row % gobRows == 0)) then
local at, q
if ss then
at, q = sliceHeader(hdr.cpm, mbaBits, total > 1584)
else
local gn, gq = gobHeader(hdr.cpm)
if gn then
at, q = if gn == 0 then -1 else gn * gobRows * mbW, gq
end
end
if at and q then
if at < k or at >= total or q == 0 then
failAt = k
break
end
for k = row * mbW, at * mbW - 1 do -- GOBs missing in between: copied
while k < at do -- macroblocks missing in between: copied
copyMb(k % mbW, k // mbW)
mvX[k % mbW + 1], mvY[k % mbW + 1] = 0, 0
k += 1
if k % mbW == 0 then
mvX, upX = upX, mvX
mvY, upY = upY, mvY
end
end
row, top, quant = at, at, gq
col, row = k % mbW, k // mbW
resync, quant = k, q
end
end
mvX, upX = upX, mvX
mvY, upY = upY, mvY
for col = 0, mbW - 1 do
if not macroblock(col, row, P, row == top) then
failAt = row * mbW + col
break
end
end
if failAt >= 0 then
-- above is outside for the slice's first mbW macroblocks (ffmpeg's first_slice_line: also the start of the
-- next row for a slice starting mid-row)
if not macroblock(col, row, P, k < resync + mbW, k == resync) then
failAt = k
break
end
row += 1
k += 1
if k % mbW == 0 then
mvX, upX = upX, mvX
mvY, upY = upY, mvY
end
end
local msg: string? = nil
if failAt >= 0 then