video: yield points in H.264 picture setup and VP8 rows, rgba / scale conversion; disposable packets and skipDisposable
- H264: picture and grid allocations split between yield calls (a 1080p picture's tables are 1.6 M slots) - Vp8: Decode.decodeFrame hook three times per macroblock row and once per loop filter row; Options.yield - Yuv.toRgba / Yuv.scale: optional hook every 16 rows; imageToRgba / imageScale take an out Image and the hook - Video.open passes opts.yield to the conversions, the VP8 and MPEG-1/2 decoders - codecs/video/Video/Disposable: H.264 nal_ref_idc 0 (avcC length-prefixed / Annex B), VP8 frames refreshing no reference; containers/Demux sets packet.disposable for h264 / vp8 tracks; Video.isDisposable(track, packet) - skipDisposable: H.264 keeps skipped pictures in output order (pts pool / POC bookkeeping), VP8 applies the header state then skips; receive() returns false for a skipped picture, VideoFrame.index counts it - Types: Packet.disposable, VideoCodecDecoder.receive may return false - tools/fixtures/videoctl.py: VP8 two temporal layers (IVF + WebM)
This commit is contained in:
1 parent
dde6be117c
commit
15e0dfcc86
12 files changed
+469
-74
No files matched your search
@@ -1164,8 +1164,9 @@ end
|
||||
export type Frame = {
|
||||
width: number,
|
||||
height: number,
|
||||
ref: Ref,
|
||||
ref: Ref?, -- nil when skipped
|
||||
header: any,
|
||||
skipped: boolean?,
|
||||
}
|
||||
|
||||
local BORDER = Predict.BORDER
|
||||
@@ -1198,7 +1199,7 @@ local function freeRef(st: State): Ref
|
||||
end
|
||||
|
||||
function Decode.cropTo(f: Frame, dy: buffer, oy: number, du: buffer, ou: number, dv: buffer, ov: number)
|
||||
local r = f.ref
|
||||
local r = f.ref :: Ref
|
||||
local w, h = f.width, f.height
|
||||
local cw, chh = (w + 1) // 2, (h + 1) // 2
|
||||
local Y, U, V, ys, uvs, yb, uvb = r.Y, r.U, r.V, r.ys, r.uvs, r.yb, r.uvb
|
||||
@@ -1211,7 +1212,12 @@ function Decode.cropTo(f: Frame, dy: buffer, oy: number, du: buffer, ou: number,
|
||||
end
|
||||
end
|
||||
|
||||
function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, maxPixels: number): (Frame?, string?)
|
||||
-- hook: called (through pcall) three times per macroblock row (modes, residuals, reconstruction) and once per loop filter
|
||||
-- row, a cooperative yield point; the module
|
||||
-- scratch is per macroblock, so other VP8 decodes may run while one is suspended
|
||||
-- skip: a frame that refreshes no reference and no segment map comes back { skipped = true } after its header (the
|
||||
-- state the header carries is kept, the picture is not reconstructed)
|
||||
function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, maxPixels: number, hook: (() -> ())?, skip: boolean?): (Frame?, string?)
|
||||
local hdr, herr = Decode.header(buf, off, len)
|
||||
if not hdr then
|
||||
return nil, herr
|
||||
@@ -1427,6 +1433,12 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
if br.eof then
|
||||
return fail("truncated: frame header")
|
||||
end
|
||||
if skip and not key and not refreshGolden and not refreshAlt and copyToGolden == 0 and copyToAlt == 0 and not refreshLast and not st.updateMap then
|
||||
if saved then
|
||||
st.proba, st.ymodeProb, st.uvmodeProb, st.mvProb = saved[1], saved[2], saved[3], saved[4]
|
||||
end
|
||||
return { width = width, height = height, ref = nil, header = hdr, skipped = true }, nil
|
||||
end
|
||||
|
||||
-- loop filter levels [segment][reference][mode delta index] and, per level, the interior limit and hev threshold
|
||||
local lvlTab = table.create(64, 0)
|
||||
@@ -1532,6 +1544,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
local miMvR, miMvC, miSubR, miSubC = st.miMvR, st.miMvC, st.miSubR, st.miSubC
|
||||
|
||||
for mby = 0, mbh - 1 do
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
local tbr = parts[band(mby, lastPart) + 1]
|
||||
-- modes of the row (first partition)
|
||||
for k = 1, 4 do
|
||||
@@ -1628,6 +1643,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
return fail("truncated: first partition (modes)")
|
||||
end
|
||||
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
-- residuals (token partition)
|
||||
local leftNz, leftNzDc = 0, 0
|
||||
for mbx = 0, mbw - 1 do
|
||||
@@ -1756,6 +1774,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
bfill(yuv, U_OFF - BPS - 1, 127, 8 + 1)
|
||||
bfill(yuv, V_OFF - BPS - 1, 127, 8 + 1)
|
||||
end
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
for mbx = 0, mbw - 1 do
|
||||
local x1 = mbx + 1
|
||||
local co = mbx * 768
|
||||
@@ -1865,6 +1886,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
-- loop filter over the whole frame, macroblocks in raster order (libwebp DoFilter)
|
||||
if filterType > 0 then
|
||||
for mby = 0, mbh - 1 do
|
||||
if hook then
|
||||
pcall(hook)
|
||||
end
|
||||
for mbx = 0, mbw - 1 do
|
||||
local fi = mby * mbw + mbx + 1
|
||||
local limit = fLimit[fi]
|
||||
|
||||
@@ -14,6 +14,9 @@ local DEFAULT_MAX_PIXELS = 2 ^ 28
|
||||
|
||||
export type Options = {
|
||||
maxPixels: number?,
|
||||
yield: (() -> ())?, -- called through pcall a few times per macroblock row (decode and loop filter)
|
||||
-- decodeFrame returns nil, nil for an inter frame that refreshes no reference (its header still applies)
|
||||
skipDisposable: boolean?,
|
||||
}
|
||||
|
||||
local function fail(msg: string?, offset: number?): Err.Err
|
||||
@@ -60,9 +63,11 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
|
||||
return nil, Err.new(Err.ARG, "decodeFrame: expected a buffer")
|
||||
end
|
||||
local o, l = range(buf, off, len)
|
||||
local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS)
|
||||
local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS, opts and opts.yield, opts and opts.skipDisposable)
|
||||
if not f then
|
||||
return nil, fail(err, o)
|
||||
elseif f.skipped then
|
||||
return nil, nil
|
||||
end
|
||||
local img = Types.Image.new(f.width, f.height, "yuv420p")
|
||||
local ny = f.width * f.height
|
||||
|
||||
@@ -66,7 +66,8 @@ for i = 1, 2304 do
|
||||
CB[i] = v * 65536 + 0xff000000
|
||||
end
|
||||
|
||||
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?): buffer
|
||||
-- hook: called (through pcall) every 16 rows, a cooperative yield point
|
||||
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?, hook: (() -> ())?): buffer
|
||||
local dst = out or bcreate(w * h * 4)
|
||||
local c = coeffs(range, matrix)
|
||||
local crv, cgu, cgv, cbu = c.crv, c.cgu, c.cgv, c.cbu
|
||||
@@ -78,6 +79,9 @@ function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, c
|
||||
local w2 = w // 2
|
||||
local stride = w * 4
|
||||
for y = 0, h - 1, 2 do
|
||||
if hook and y % 16 == 0 and y > 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
local yrow = y * w
|
||||
local ci = uo + (y // 2) * cw
|
||||
local dv = vo - uo
|
||||
@@ -212,7 +216,8 @@ function Yuv.toFullRange(img: Types.Image): Types.Image
|
||||
return out
|
||||
end
|
||||
|
||||
function Yuv.imageToRgba(img: Types.Image): Types.Image?
|
||||
-- out: an rgba8 Image of the same size to write into (else a new one)
|
||||
function Yuv.imageToRgba(img: Types.Image, out: Types.Image?, hook: (() -> ())?): Types.Image?
|
||||
if img.format == "rgba8" then
|
||||
return img
|
||||
end
|
||||
@@ -222,10 +227,13 @@ function Yuv.imageToRgba(img: Types.Image): Types.Image?
|
||||
local w, h = img.width, img.height
|
||||
local uo, vo, cw = Yuv.planes(w, h)
|
||||
local range, matrix = info(img)
|
||||
local out = Types.Image.new(w, h, "rgba8")
|
||||
Yuv.toRgba(img.pixels, w, h, uo, vo, cw, range, matrix, out.pixels)
|
||||
out.meta.source = img.meta.source
|
||||
return out
|
||||
if not out or out.format ~= "rgba8" or out.width ~= w or out.height ~= h or buffer.len(out.pixels) < w * h * 4 then
|
||||
out = Types.Image.new(w, h, "rgba8")
|
||||
end
|
||||
local o = out :: Types.Image
|
||||
Yuv.toRgba(img.pixels, w, h, uo, vo, cw, range, matrix, o.pixels, hook)
|
||||
o.meta.source = img.meta.source
|
||||
return o
|
||||
end
|
||||
|
||||
function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): Types.Image?
|
||||
@@ -242,9 +250,12 @@ function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): T
|
||||
return out
|
||||
end
|
||||
|
||||
local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number)
|
||||
local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number, hook: (() -> ())?)
|
||||
local fx, fy = sw / dw, sh / dh
|
||||
for y = 0, dh - 1 do
|
||||
if hook and y % 16 == 15 then
|
||||
pcall(hook)
|
||||
end
|
||||
local sy = (y + 0.5) * fy - 0.5
|
||||
if sy < 0 then
|
||||
sy = 0
|
||||
@@ -275,7 +286,7 @@ local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst:
|
||||
end
|
||||
end
|
||||
|
||||
function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?): buffer
|
||||
function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?, hook: (() -> ())?): buffer
|
||||
local dw: number = math.max(1, floor(width))
|
||||
local dh: number = math.max(1, floor(height))
|
||||
local cw: number = (w + 1) // 2
|
||||
@@ -287,17 +298,22 @@ function Yuv.scale(src: buffer, w: number, h: number, width: number, height: num
|
||||
if w < 1 or h < 1 or buffer.len(src) < w * h + 2 * cw * ch then
|
||||
return dst
|
||||
end
|
||||
scalePlane(src, 0, w, h, dst, 0, dw, dh)
|
||||
scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch)
|
||||
scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch)
|
||||
scalePlane(src, 0, w, h, dst, 0, dw, dh, hook)
|
||||
scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch, hook)
|
||||
scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch, hook)
|
||||
return dst
|
||||
end
|
||||
|
||||
function Yuv.imageScale(img: Types.Image, dw: number, dh: number): Types.Image
|
||||
local out = Types.Image.new(math.max(1, floor(dw)), math.max(1, floor(dh)), "yuv420p")
|
||||
Yuv.scale(img.pixels, img.width, img.height, out.width, out.height, out.pixels)
|
||||
out.meta = table.clone(img.meta or {})
|
||||
return out
|
||||
-- out: a yuv420p Image of the target size to write into (else a new one)
|
||||
function Yuv.imageScale(img: Types.Image, dw: number, dh: number, out: Types.Image?, hook: (() -> ())?): Types.Image
|
||||
local tw, th = math.max(1, floor(dw)), math.max(1, floor(dh))
|
||||
if not out or out.format ~= "yuv420p" or out.width ~= tw or out.height ~= th then
|
||||
out = Types.Image.new(tw, th, "yuv420p")
|
||||
end
|
||||
local o = out :: Types.Image
|
||||
Yuv.scale(img.pixels, img.width, img.height, tw, th, o.pixels, hook)
|
||||
o.meta = table.clone(img.meta or {})
|
||||
return o
|
||||
end
|
||||
|
||||
return Yuv
|
||||
Reference in new issue
Block a user