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 -- 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 -- (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 -- 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 -- (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): -- 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, -- 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). -- 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| -- 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, format: number,
customPcf: boolean, customPcf: boolean,
umv: boolean, umv: boolean,
ss: boolean, -- slice structured (Annex K)
options: string?, -- the unsupported options set, or nil options: string?, -- the unsupported options set, or nil
width: number, width: number,
height: number, height: number,
@@ -83,6 +85,7 @@ export type Header = {
umvPlus: boolean, -- unrestricted vectors, PLUSPTYPE form umvPlus: boolean, -- unrestricted vectors, PLUSPTYPE form
rounding: number, -- RTYPE (0: half-sample averages round up) rounding: number, -- RTYPE (0: half-sample averages round up)
plus: boolean, -- PLUSPTYPE plus: boolean, -- PLUSPTYPE
ss: boolean, -- slice structured (Annex K)
cpm: boolean, cpm: boolean,
deblock: boolean, -- Sorenson's deblocking hint (not applied, as ffmpeg) deblock: boolean, -- Sorenson's deblocking hint (not applied, as ffmpeg)
aspect: number?, -- PLUSPTYPE pixel aspect ratio code (15: aspectNum / aspectDen) aspect: number?, -- PLUSPTYPE pixel aspect ratio code (15: aspectNum / aspectDen)
@@ -105,6 +108,11 @@ export type Store = {
} }
-- ------------------------------------------------------------------------------------------------ header -- ------------------------------------------------------------------------------------------------ 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 -- 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 function bitsAt(buf: buffer, endByte: number, p: number, n: number): number
local v = 0 local v = 0
@@ -158,6 +166,7 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
umvPlus = false, umvPlus = false,
rounding = 0, rounding = 0,
plus = false, plus = false,
ss = false,
cpm = false, cpm = false,
deblock = false, deblock = false,
bitPos = 0, 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", "deblocking filter", "slice structure", "reference picture selection", "independent segment decoding",
"alternative inter VLC", "modified quantisation" } "alternative inter VLC", "modified quantisation" }
local unsup: string? = nil local unsup: string? = nil
local ss = false
for i = 1, 9 do for i = 1, 9 do
if bits(1) == 1 and not unsup then if bits(1) == 1 then
unsup = names[i] if i == 5 then
ss = true
elseif not unsup then
unsup = names[i]
end
end end
end end
bits(4) -- 1 000 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 elseif ufep == 0 and opp then
o = opp o = opp
else else
@@ -283,8 +297,15 @@ function D.header(buf: buffer, off: number, len: number, flv: boolean, opp: Opp?
bits(1) bits(1)
end end
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.width, h.height = o.width, o.height
h.umvPlus = o.umv h.umvPlus = o.umv
h.ss = o.ss
h.opp = o h.opp = o
else else
return nil, "bad source format " .. format 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 if h.width < 1 or h.height < 1 then
return nil, "empty picture size" return nil, "empty picture size"
end 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 if p > e * 8 then
return nil, "truncated picture header" return nil, "truncated picture header"
end end
@@ -609,9 +636,10 @@ function D.new(flv: boolean, maxPixels: number?): Core
end end
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 -- macroblock (col, row) of a picture (P: an inter picture; top: in the first row of the picture or of a GOB /
-- header, whose vector predictions do not look up). false on bad data. -- slice with a header, whose vector predictions do not look up; first: the first macroblock of a slice, whose
local function macroblock(col: number, row: number, P: boolean, top: boolean): boolean -- 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 if pos > endBits then
return false return false
end end
@@ -662,8 +690,8 @@ function D.new(flv: boolean, maxPixels: number?): Core
cbpy = 15 - cbpy cbpy = 15 - cbpy
-- the vector prediction: the median of left, above and above right (outside or past the right edge: 0; the -- 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) -- first row of a picture / GOB: the left vector)
local ax = if col > 0 then mvX[col] else 0 local ax = if col > 0 and not first then mvX[col] else 0
local ay = if col > 0 then mvY[col] else 0 local ay = if col > 0 and not first then mvY[col] else 0
local px, py = ax, ay local px, py = ax, ay
if not top then if not top then
local bx, by = upX[col + 1], upY[col + 1] local bx, by = upX[col + 1], upY[col + 1]
@@ -690,23 +718,25 @@ function D.new(flv: boolean, maxPixels: number?): Core
return pos <= endBits return pos <= endBits
end end
-- an optional GOB header at a GOB boundary (GBSC, after zero stuffing to a byte boundary or not): its GN and -- a GBSC / SSC (17 bits 0..01) next, after zero stuffing to a byte boundary or not: consumed, true
-- GQUANT, or nil local function startCode(): boolean
local function gobHeader(cpm: boolean): (number?, number?)
local start = pos
local w = peek32() local w = peek32()
if rshift(w, 15) ~= 1 then if rshift(w, 15) ~= 1 then
local s = (8 - pos % 8) % 8 local s = (8 - pos % 8) % 8
if s == 0 or rshift(w, 32 - s) ~= 0 then if s == 0 or rshift(w, 32 - s) ~= 0 or rshift(lshift(w, s), 15) ~= 1 then
return nil, nil return false
end end
pos += s pos += s
if rshift(peek32(), 15) ~= 1 then
pos = start
return nil, nil
end
end end
pos += 17 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) local gn = read(5)
if cpm then if cpm then
read(2) read(2)
@@ -715,6 +745,24 @@ function D.new(flv: boolean, maxPixels: number?): Core
return gn, read(5) return gn, read(5)
end 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?) local function picture(hdr: Header): (Store?, string?)
if hdr.width ~= W or hdr.height ~= H then if hdr.width ~= W or hdr.height ~= H then
if hdr.width * hdr.height > state.maxPixels then if hdr.width * hdr.height > state.maxPixels then
@@ -740,36 +788,55 @@ function D.new(flv: boolean, maxPixels: number?): Core
pos = hdr.bitPos pos = hdr.bitPos
err = false err = false
local gobRows = if hdr.flv then 0 elseif H <= 400 then 1 elseif H <= 800 then 2 else 4 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 ss = hdr.ss
local row = 0 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 local failAt = -1
while row < mbH do mvX, upX = upX, mvX
if gobRows > 0 and row > 0 and row % gobRows == 0 then mvY, upY = upY, mvY
local gn, gq = gobHeader(hdr.cpm) while k < total do
if gn and gq then local col, row = k % mbW, k // mbW
local at = gn * gobRows if k > 0 and (ss or (gobRows > 0 and col == 0 and row % gobRows == 0)) then
if gn == 0 or at >= mbH or at < row or gq == 0 then local at, q
failAt = row * mbW 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 break
end 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) 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 end
row, top, quant = at, at, gq col, row = k % mbW, k // mbW
resync, quant = k, q
end end
end end
mvX, upX = upX, mvX -- above is outside for the slice's first mbW macroblocks (ffmpeg's first_slice_line: also the start of the
mvY, upY = upY, mvY -- next row for a slice starting mid-row)
for col = 0, mbW - 1 do if not macroblock(col, row, P, k < resync + mbW, k == resync) then
if not macroblock(col, row, P, row == top) then failAt = k
failAt = row * mbW + col
break
end
end
if failAt >= 0 then
break break
end end
row += 1 k += 1
if k % mbW == 0 then
mvX, upX = upX, mvX
mvY, upY = upY, mvY
end
end end
local msg: string? = nil local msg: string? = nil
if failAt >= 0 then if failAt >= 0 then
+7 -5
View File
@@ -1,6 +1,6 @@
-- codecs/video/H263 tests. Against `ffmpeg -idct int` (tools/fixtures/h263.py): H.263 streams (sub-QCIF .. 4CIF, GOB -- codecs/video/H263 tests. Against `ffmpeg -idct int` (tools/fixtures/h263.py): H.263 streams (sub-QCIF .. 4CIF, GOB
-- headers, DQUANT, q 1 .. high quantisers), H.263+ PLUSPTYPE streams (custom sizes, sizes that are not a multiple of -- headers, DQUANT, q 1 .. high quantisers), H.263+ PLUSPTYPE streams (custom sizes, sizes that are not a multiple of
-- 16, the rounding type, unrestricted vectors with reversible codes) and Sorenson Spark streams (standard, 8-bit and -- 16, the rounding type, unrestricted vectors with reversible codes, slice structure) and Sorenson Spark streams (standard, 8-bit and
-- 16-bit custom sizes, 11-bit escapes, high motion, disposable inter pictures) decode one picture at a time to exactly -- 16-bit custom sizes, 11-bit escapes, high motion, disposable inter pictures) decode one picture at a time to exactly
-- ffmpeg's planes, every frame; header info; frameImage; the registry's decoders (h263, flv1); unsupported options are -- ffmpeg's planes, every frame; header info; frameImage; the registry's decoders (h263, flv1); unsupported options are
-- UNSUPPORTED; truncated and corrupted pictures never throw; the fuzz no-throw pass. -- UNSUPPORTED; truncated and corrupted pictures never throw; the fuzz no-throw pass.
@@ -23,6 +23,8 @@ local STREAMS = {
require("@tests/gen/h263_plus_custom"), require("@tests/gen/h263_plus_custom"),
require("@tests/gen/h263_plus_odd"), require("@tests/gen/h263_plus_odd"),
require("@tests/gen/h263_plus_umv"), require("@tests/gen/h263_plus_umv"),
require("@tests/gen/h263_plus_slices"),
require("@tests/gen/h263_plus_slices_umv"),
require("@tests/gen/h263_flv_320"), require("@tests/gen/h263_flv_320"),
require("@tests/gen/h263_flv_odd"), require("@tests/gen/h263_flv_odd"),
require("@tests/gen/h263_flv_wide"), require("@tests/gen/h263_flv_wide"),
@@ -123,7 +125,7 @@ do
t:check(h.plus and h.width == 320 and h.height == 240 and h.ptype == 1, "plus_custom: PLUSPTYPE custom size, I picture") t:check(h.plus and h.width == 320 and h.height == 240 and h.ptype == 1, "plus_custom: PLUSPTYPE custom size, I picture")
local h2 = assert(H263.header(file, fx.units[2][1], fx.units[2][2])) local h2 = assert(H263.header(file, fx.units[2][1], fx.units[2][2]))
t:check(h2.ptype == 2, "plus_custom: second picture P") t:check(h2.ptype == 2, "plus_custom: second picture P")
fx = STREAMS[12] -- flv_odd fx = STREAMS[14] -- flv_odd
file = b64(fx.file) file = b64(fx.file)
h = assert(H263.header(file, fx.units[1][1], fx.units[1][2], true)) h = assert(H263.header(file, fx.units[1][1], fx.units[1][2], true))
t:check(h.flv and h.width == 100 and h.height == 75 and h.version == 1, "flv_odd: Sorenson 8-bit custom size, version 1") t:check(h.flv and h.width == 100 and h.height == 75 and h.version == 1, "flv_odd: Sorenson 8-bit custom size, version 1")
@@ -143,7 +145,7 @@ end
-- 4. frameImage -- 4. frameImage
do do
local fx = STREAMS[12] local fx = STREAMS[14]
local dec = H263.new({ flv = true }) local dec = H263.new({ flv = true })
local file = b64(fx.file) local file = b64(fx.file)
local f = assert(dec:decode(file, fx.units[1][1], fx.units[1][2])) local f = assert(dec:decode(file, fx.units[1][1], fx.units[1][2]))
@@ -161,7 +163,7 @@ end
do do
t:check(Video.fromFourcc("H263") == "h263" and Video.fromFourcc("s263") == "h263" and Video.fromFourcc("FLV1") == "flv1", "registry names") t:check(Video.fromFourcc("H263") == "h263" and Video.fromFourcc("s263") == "h263" and Video.fromFourcc("FLV1") == "flv1", "registry names")
t:check(Video.canDecode("h263") and Video.canDecode("flv1"), "registry decoders") t:check(Video.canDecode("h263") and Video.canDecode("flv1"), "registry decoders")
for _, idx in { 2, 13 } do for _, idx in { 2, 15 } do
local fx = STREAMS[idx] local fx = STREAMS[idx]
local file = b64(fx.file) local file = b64(fx.file)
local dec = assert(Video.decoder({ id = 1, kind = "video", codec = if fx.flv then "flv1" else "h263", timescale = 25, width = fx.width, height = fx.height } :: any)) local dec = assert(Video.decoder({ id = 1, kind = "video", codec = if fx.flv then "flv1" else "h263", timescale = 25, width = fx.width, height = fx.height } :: any))
@@ -192,7 +194,7 @@ end
-- 6. robustness: truncated and corrupted pictures, garbage (never throws), the fuzz pass -- 6. robustness: truncated and corrupted pictures, garbage (never throws), the fuzz pass
do do
local samples = {} local samples = {}
for _, idx in { 1, 6, 8, 10, 11, 14 } do for _, idx in { 1, 6, 8, 10, 11, 13, 16 } do
local fx = STREAMS[idx] local fx = STREAMS[idx]
local file = b64(fx.file) local file = b64(fx.file)
for k = 1, 2 do for k = 1, 2 do
+4 -1
View File
@@ -1,6 +1,6 @@
# H.263 / Sorenson Spark fixtures for codecs/video/H263 (tools/fixtures.py): streams from ffmpeg's encoders (h263: # H.263 / Sorenson Spark fixtures for codecs/video/H263 (tools/fixtures.py): streams from ffmpeg's encoders (h263:
# sub-QCIF .. 4CIF, GOB headers, adaptive quantisation (DQUANT), low and high bitrates; h263p: PLUSPTYPE with custom # sub-QCIF .. 4CIF, GOB headers, adaptive quantisation (DQUANT), low and high bitrates; h263p: PLUSPTYPE with custom
# sizes, the rounding type, unrestricted vectors; flv: Sorenson Spark with its standard, 8-bit and 16-bit custom # sizes, the rounding type, unrestricted vectors, slice structure; flv: Sorenson Spark with its standard, 8-bit and 16-bit custom
# sizes, 11-bit escapes, disposable inter pictures patched in), high and low motion (testsrc2, mandelbrot, scroll), # sizes, 11-bit escapes, disposable inter pictures patched in), high and low motion (testsrc2, mandelbrot, scroll),
# each with `ffmpeg -flags +bitexact -idct int` decoding as the reference (the integer IDCT codecs/video/Dsp # each with `ffmpeg -flags +bitexact -idct int` decoding as the reference (the integer IDCT codecs/video/Dsp
# reproduces; bitexact: ffmpeg's x86 half-sample averages without rounding are approximations otherwise). # reproduces; bitexact: ffmpeg's x86 half-sample averages without rounding are approximations otherwise).
@@ -47,6 +47,9 @@ CASES = {
"plus_custom": (src("testsrc2", 320, 240, 0.8, vf="scroll=h=0.02:v=0.01") + ["-b:v", "300k", "-g", "15"] + H263P, False), "plus_custom": (src("testsrc2", 320, 240, 0.8, vf="scroll=h=0.02:v=0.01") + ["-b:v", "300k", "-g", "15"] + H263P, False),
"plus_odd": (src("mandelbrot", 196, 148, 0.6) + ["-b:v", "250k", "-g", "8"] + H263P, False), "plus_odd": (src("mandelbrot", 196, 148, 0.6) + ["-b:v", "250k", "-g", "8"] + H263P, False),
"plus_umv": (src("testsrc2", 200, 152, 0.8, vf="scroll=h=0.06:v=0.04") + ["-umv", "1", "-q:v", "5", "-g", "25"] + H263P, False), "plus_umv": (src("testsrc2", 200, 152, 0.8, vf="scroll=h=0.06:v=0.04") + ["-umv", "1", "-q:v", "5", "-g", "25"] + H263P, False),
"plus_slices": (src("mandelbrot", 352, 288, 0.48) + ["-b:v", "600k", "-g", "6"] + H263P + ["-threads", "4", "-slices", "4"], False),
"plus_slices_umv": (src("testsrc2", 176, 144, 0.8, vf="scroll=h=-0.04:v=0.05") + ["-umv", "1", "-q:v", "4", "-g", "20"] + H263P
+ ["-threads", "3", "-slices", "3", "-ps", "200"], False),
"flv_320": (src("testsrc2", 320, 240, 1.0) + ["-b:v", "300k", "-g", "12"] + FLV, True), "flv_320": (src("testsrc2", 320, 240, 1.0) + ["-b:v", "300k", "-g", "12"] + FLV, True),
"flv_odd": (src("mandelbrot", 100, 75, 0.6) + ["-b:v", "150k", "-g", "8"] + FLV, True), "flv_odd": (src("mandelbrot", 100, 75, 0.6) + ["-b:v", "150k", "-g", "8"] + FLV, True),
"flv_wide": (src("testsrc2", 400, 224, 0.4, vf="scroll=h=0.03") + ["-b:v", "400k", "-g", "6"] + FLV, True), "flv_wide": (src("testsrc2", 400, 224, 0.4, vf="scroll=h=0.03") + ["-b:v", "400k", "-g", "6"] + FLV, True),