ws-video: video/mkv tests (ffprobe packets, ffmpeg audio, lacing / zlib / unknown clusters / subtitles, WebM remux), zlib content compression, codec-delay trimming of non-Opus audio (AudioWrap.trim), video/y4m, containers/Avi + video/avi (WIP tests)
This commit is contained in:
1 parent
8a905a4510
commit
9411cd97c6
14 files changed
+1862
-20
No files matched your search
@@ -63,31 +63,107 @@ function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) ->
|
||||
return self :: any
|
||||
end
|
||||
|
||||
-- raw video: yuv420p (I420 / IYUV / y4m), yv12 (V before U), rgb24 / bgr24 / bgra (bottom-up AVI DIBs: track.flip)
|
||||
-- raw video: yuv420p (I420 / IYUV / y4m), yv12 (V before U), yuv422p / yuv444p / yuva444p / gray (chroma averaged
|
||||
-- down to 4:2:0, alpha dropped), track.bitDepth 9..16 (little-endian 16-bit samples scaled to 8 bits), rgb24 / bgr24 /
|
||||
-- bgra / rgba (AVI DIBs: track.flip = bottom-up rows, track.stride = padded rows)
|
||||
local function rawPlanar(data: buffer, pf: string, bits: number, w: number, h: number): Types.Image
|
||||
local img = Types.Image.new(w, h, "yuv420p")
|
||||
local px = img.pixels
|
||||
local cw, ch = (w + 1) // 2, (h + 1) // 2
|
||||
local bps = if bits > 8 then 2 else 1
|
||||
local shift = 2 ^ (bits - 8)
|
||||
local function sample(o: number): number
|
||||
if bps == 1 then
|
||||
return buffer.readu8(data, o)
|
||||
end
|
||||
return math.min(255, buffer.readu16(data, o) // shift)
|
||||
end
|
||||
for i = 0, w * h - 1 do
|
||||
buffer.writeu8(px, i, sample(i * bps))
|
||||
end
|
||||
local scw, sch = w, h -- source chroma plane size
|
||||
if pf == "yuv420p" then
|
||||
scw, sch = cw, ch
|
||||
elseif pf == "yuv422p" then
|
||||
scw, sch = cw, h
|
||||
end
|
||||
local uo, vo = w * h * bps, (w * h + scw * sch) * bps
|
||||
for cy = 0, ch - 1 do
|
||||
for cx = 0, cw - 1 do
|
||||
local i = cy * cw + cx
|
||||
local u, v
|
||||
if pf == "gray" then
|
||||
u, v = 128, 128
|
||||
elseif pf == "yuv420p" then
|
||||
u, v = sample(uo + i * bps), sample(vo + i * bps)
|
||||
elseif pf == "yuv422p" then
|
||||
local y0, y1 = cy * 2, math.min(cy * 2 + 1, h - 1)
|
||||
u = (sample(uo + (y0 * scw + cx) * bps) + sample(uo + (y1 * scw + cx) * bps) + 1) // 2
|
||||
v = (sample(vo + (y0 * scw + cx) * bps) + sample(vo + (y1 * scw + cx) * bps) + 1) // 2
|
||||
else
|
||||
local x0, x1 = cx * 2, math.min(cx * 2 + 1, w - 1)
|
||||
local y0, y1 = cy * 2, math.min(cy * 2 + 1, h - 1)
|
||||
local a, b, c, d = (y0 * w + x0) * bps, (y0 * w + x1) * bps, (y1 * w + x0) * bps, (y1 * w + x1) * bps
|
||||
u = (sample(uo + a) + sample(uo + b) + sample(uo + c) + sample(uo + d) + 2) // 4
|
||||
v = (sample(vo + a) + sample(vo + b) + sample(vo + c) + sample(vo + d) + 2) // 4
|
||||
end
|
||||
buffer.writeu8(px, w * h + i, u)
|
||||
buffer.writeu8(px, w * h + cw * ch + i, v)
|
||||
end
|
||||
end
|
||||
return img
|
||||
end
|
||||
|
||||
local PLANAR_SIZE: { [string]: (w: number, h: number, cw: number, ch: number) -> number } = {
|
||||
yuv420p = function(w, h, cw, ch)
|
||||
return w * h + 2 * cw * ch
|
||||
end,
|
||||
yv12 = function(w, h, cw, ch)
|
||||
return w * h + 2 * cw * ch
|
||||
end,
|
||||
yuv422p = function(w, h, cw, _ch)
|
||||
return w * h + 2 * cw * h
|
||||
end,
|
||||
yuv444p = function(w, h)
|
||||
return w * h * 3
|
||||
end,
|
||||
yuva444p = function(w, h)
|
||||
return w * h * 4
|
||||
end,
|
||||
gray = function(w, h)
|
||||
return w * h
|
||||
end,
|
||||
}
|
||||
|
||||
local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local w, h = track.width or 0, track.height or 0
|
||||
if w <= 0 or h <= 0 or w * h > 2 ^ 26 then
|
||||
return nil, Err.new(Err.CORRUPT, "raw video without a valid size")
|
||||
end
|
||||
local pf = track.pixelFormat or "yuv420p"
|
||||
local bits = track.bitDepth or 8
|
||||
local cw, ch = (w + 1) // 2, (h + 1) // 2
|
||||
local flip = track.flip == true
|
||||
local planar = PLANAR_SIZE[pf]
|
||||
return Video.simpleDecoder(function(data: buffer): (Types.Image?, Err.Err?)
|
||||
local n = buffer.len(data)
|
||||
if pf == "yuv420p" or pf == "yv12" then
|
||||
local need = w * h + 2 * cw * ch
|
||||
if planar then
|
||||
local need = planar(w, h, cw, ch) * (if bits > 8 then 2 else 1)
|
||||
if n < need then
|
||||
return nil, Err.new(Err.TRUNCATED, "raw frame: " .. n .. " of " .. need .. " bytes")
|
||||
end
|
||||
local img = Types.Image.new(w, h, "yuv420p")
|
||||
if pf == "yv12" then
|
||||
if pf == "yuv420p" and bits == 8 then
|
||||
local img = Types.Image.new(w, h, "yuv420p")
|
||||
buffer.copy(img.pixels, 0, data, 0, need)
|
||||
return img
|
||||
elseif pf == "yv12" then
|
||||
local img = Types.Image.new(w, h, "yuv420p")
|
||||
buffer.copy(img.pixels, 0, data, 0, w * h)
|
||||
buffer.copy(img.pixels, w * h, data, w * h + cw * ch, cw * ch)
|
||||
buffer.copy(img.pixels, w * h + cw * ch, data, w * h, cw * ch)
|
||||
else
|
||||
buffer.copy(img.pixels, 0, data, 0, need)
|
||||
return img
|
||||
end
|
||||
return img
|
||||
return rawPlanar(data, pf, bits, w, h)
|
||||
end
|
||||
local bpp = if pf == "bgra" or pf == "rgba" then 4 else 3
|
||||
local stride = if track.stride then track.stride else w * bpp
|
||||
|
||||
Reference in new issue
Block a user