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,14 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Cinepak: Cinepak ('cvid') frames: 24-bit colour (codebook entries of 4 luma + 2 chroma, or 4
|
||||
-- luma for grey), and 8-bit palettised streams (entries are palette indexes). Written from the format description
|
||||
-- (MultimediaWiki "Cinepak"): a frame header (flags, size, width, height, strip count), strips with their own V1 / V4
|
||||
-- codebooks (full or partial updates, kept per strip between frames, inherited from the strip above unless the frame
|
||||
-- flags say otherwise) and the vector chunks (intra, inter with skip flags, V1-only). Colour codebook entries are
|
||||
-- converted once with the Cinepak YUV formula (r = y + 2v, g = y - u/2 - v, b = y + 2u, clamped). Sega FILM's extra
|
||||
-- header bytes are detected on the first frame. Paints over the previous picture. Bit-exact with ffmpeg's cinepak
|
||||
-- decoder. Never throws: every read is range-checked against its chunk.
|
||||
-- codecs/video/Legacy/Cinepak: Cinepak ('cvid') frames, 24-bit colour and 8-bit palettised.
|
||||
-- From MultimediaWiki "Cinepak", bit-exact with ffmpeg: a frame header (flags, size, width, height, strip count), strips
|
||||
-- with V1 / V4 codebooks (inherited from the strip above unless the flags say otherwise) and vector chunks.
|
||||
-- Colour entries: r = y + 2v, g = y - u/2 - v, b = y + 2u, clamped.
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu32, buffer.writeu32, buffer.writeu8
|
||||
@@ -93,7 +89,7 @@ local function codebook(books: buffer, book: number, id: number, d: buffer, p: n
|
||||
end
|
||||
end
|
||||
|
||||
-- the vectors of a strip (ids 0x30 intra, 0x31 inter with skip flags, 0x32 V1 only); false on truncated data
|
||||
-- the vectors of a strip (ids 0x30 intra, 0x31 inter with skip flags, 0x32 V1 only)
|
||||
local function vectors(pic: Picture.Picture, books: buffer, v4: number, v1: number, id: number, d: buffer, p: number,
|
||||
stop: number, x1: number, y1: number, x2: number, y2: number): boolean
|
||||
local stride = pic.stride
|
||||
@@ -194,8 +190,6 @@ local function vectors(pic: Picture.Picture, books: buffer, v4: number, v1: numb
|
||||
return true
|
||||
end
|
||||
|
||||
-- decode(pic, data, state, hasPalette) -> picture?, message: true = a picture; false + nil = no picture for this
|
||||
-- packet (an empty frame); false + message = an error
|
||||
function Cinepak.decode(pic: Picture.Picture, d: buffer, st: State, newPalette: boolean): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
if n < 10 then
|
||||
@@ -238,7 +232,6 @@ function Cinepak.decode(pic: Picture.Picture, d: buffer, st: State, newPalette:
|
||||
local p = 10 + skip
|
||||
strips = math.min(strips, MAX_STRIPS)
|
||||
local books = st.books
|
||||
-- the coded area: the size rounded up to whole blocks (the picture's plane)
|
||||
local cw, ch = pic.stride, pic.rows
|
||||
local y0 = 0
|
||||
for i = 0, strips - 1 do
|
||||
@@ -263,7 +256,6 @@ function Cinepak.decode(pic: Picture.Picture, d: buffer, st: State, newPalette:
|
||||
if i > 0 and band(flags, 1) == 0 then
|
||||
buffer.copy(books, v4, books, v4 - 2 * BOOK, 2 * BOOK)
|
||||
end
|
||||
-- the strip's chunks: codebooks, then one vector chunk
|
||||
-- (strips start on block boundaries in every known stream: the plane holds whole blocks only)
|
||||
if sx2 > cw or sy2 > ch or sx1 >= sx2 or sy1 >= sy2 or band(sx1, 3) ~= 0 or band(sy1, 3) ~= 0 then
|
||||
return true, nil
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Flic: Autodesk Animator FLI / FLC frames (the F1FA frame chunks; the file format is
|
||||
-- video/fli). Written from the FLIC file format description (Jim Kent's "FLC - Animator Pro Flic Files", Dr. Dobb's
|
||||
-- 1993; MultimediaWiki "Flic Video"). 8-bit palettised: COLOR_256 (4), COLOR_64 (11, 6-bit entries widened by bit
|
||||
-- replication), DELTA_FLC / SS2 (7: word runs, line skips, last-byte opcode), DELTA_FLI / LC (12: byte runs), BLACK
|
||||
-- (13), BYTE_RUN / BRUN (15), FLI_COPY (16, rows padded to 4), PSTAMP (18, skipped). 15 / 16 / 24-bit FLC and FLX
|
||||
-- (DTA): DELTA / DTA_LC (pixel runs), BLACK, BRUN (byte runs), DTA_BRUN (25, pixel runs), COPY / DTA_COPY (16 / 26);
|
||||
-- pixels are 5-5-5 / 5-6-5 little-endian words or B, G, R bytes. Not supported: 1-bit FLICs, the Magic Carpet
|
||||
-- 6-bit COLOR_256 variant. Paints over the previous picture. Bit-exact with ffmpeg's flic decoder on valid frames.
|
||||
-- Never throws: reads past the frame give zeros, every write is range-checked (a damaged frame gives no picture).
|
||||
-- codecs/video/Legacy/Flic: Autodesk Animator FLI / FLC frames (the F1FA frame chunks; the file format is video/fli).
|
||||
-- From Jim Kent's "FLC - Animator Pro Flic Files" (Dr. Dobb's 1993) and MultimediaWiki "Flic Video", bit-exact with
|
||||
-- ffmpeg. 8-bit: COLOR_256 (4), COLOR_64 (11), DELTA_FLC / SS2 (7), DELTA_FLI / LC (12), BLACK (13), BYTE_RUN / BRUN
|
||||
-- (15), FLI_COPY (16, rows padded to 4), PSTAMP (18). 15 / 16 / 24-bit FLC and FLX (DTA): DELTA / DTA_LC, BLACK, BRUN,
|
||||
-- DTA_BRUN (25), COPY / DTA_COPY (16 / 26); 5-5-5 / 5-6-5 little-endian words or B, G, R bytes.
|
||||
-- Not supported: 1-bit FLICs, the Magic Carpet 6-bit COLOR_256 variant.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
@@ -18,14 +15,12 @@ local bfill = buffer.fill
|
||||
|
||||
local Flic = {}
|
||||
|
||||
-- the decoding state of one stream: a byte plane (stride = width * bytes per pixel) and the palette
|
||||
export type State = {
|
||||
pic: Picture.Picture,
|
||||
depth: number, -- 8, 15, 16, 24
|
||||
bpp: number, -- bytes per pixel
|
||||
bpp: number,
|
||||
}
|
||||
|
||||
-- state(w, h, depth) -> State | nil, message
|
||||
function Flic.state(w: number, h: number, depth: number): (State?, string?)
|
||||
local bpp = if depth == 8 then 1 elseif depth == 15 or depth == 16 then 2 elseif depth == 24 then 3 else 0
|
||||
if bpp == 0 then
|
||||
@@ -40,7 +35,6 @@ local function s8(v: number): number
|
||||
return if v >= 128 then v - 256 else v
|
||||
end
|
||||
|
||||
-- decode(st, data) -> true | false, message
|
||||
function Flic.decode(st: State, d: buffer): (boolean, string?)
|
||||
local pic = st.pic
|
||||
local px = pic.idx :: buffer
|
||||
@@ -336,7 +330,6 @@ function Flic.decode(st: State, d: buffer): (boolean, string?)
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- image(st) -> a fresh rgba8 Image
|
||||
function Flic.image(st: State): Types.Image
|
||||
local pic = st.pic
|
||||
if st.bpp == 1 then
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Msvideo1: Microsoft Video 1 (CRAM / MSVC / WHAM) frames, 8-bit palettised and 16-bit
|
||||
-- X-R-G-B 1-5-5-5. Written from the format description (MultimediaWiki "Microsoft Video 1"): 4x4 blocks, block rows
|
||||
-- bottom-up (rows of a block bottom-up too), each block a 16-bit code: a skip count (0x84xx - 0x87xx), a 2-colour
|
||||
-- block (16 flag bits + 2 colours; at 16 bits a first colour with bit 15 set makes it an 8-colour block, one colour
|
||||
-- pair per quadrant; at 8 bits codes >= 0x9000 are 8-colour), or a 1-colour block. Paints over the previous picture.
|
||||
-- Bit-exact with ffmpeg's msvideo1 decoder. Never throws: every read is range-checked (a damaged frame stops there).
|
||||
-- codecs/video/Legacy/Msvideo1: Microsoft Video 1 (CRAM / MSVC / WHAM), 8-bit palettised and 16-bit X-R-G-B 1-5-5-5.
|
||||
-- From MultimediaWiki "Microsoft Video 1", bit-exact with ffmpeg: 4x4 blocks, block rows (and rows in a block) bottom-up;
|
||||
-- a 16-bit code per block: skip (0x84xx - 0x87xx), 2-colour (16 flag bits + 2 colours; at 16 bits c0 bit 15 set = 8-colour,
|
||||
-- a pair per quadrant; at 8 bits codes >= 0x9000 are 8-colour), else 1-colour.
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu16, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
@@ -14,13 +12,11 @@ local band = bit32.band
|
||||
|
||||
local Msvideo1 = {}
|
||||
|
||||
-- the colour of pixel (x, y) of a block (y = 0 is the block's bottom row) for flag bit f: 2-colour c0 / c1
|
||||
-- (bit set = c0), 8-colour: colours[quadrant * 2 + (bit clear)]
|
||||
-- 2-colour: bit set = c0; 8-colour: colours[quadrant * 2 + (bit clear)]
|
||||
local function quadrant(x: number, y: number): number
|
||||
return (if y >= 2 then 4 else 0) + (if x >= 2 then 2 else 0)
|
||||
end
|
||||
|
||||
-- decode(pic, data, bits) -> true | false, message (bits = 8: palettised, else 16-bit)
|
||||
function Msvideo1.decode(pic: Picture.Picture, d: buffer, bits: number): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
local bw, bh = pic.w // 4, pic.h // 4
|
||||
@@ -50,7 +46,6 @@ function Msvideo1.decode(pic: Picture.Picture, d: buffer, bits: number): (boolea
|
||||
if a == 0 and b == 0 and total == 0 then
|
||||
return true, nil
|
||||
end
|
||||
-- the block's bottom-left pixel; its rows go up
|
||||
local o = (by * 4 + 3) * stride + bx * 4
|
||||
if band(b, 0xFC) == 0x84 then
|
||||
skip = (b - 0x84) * 256 + a - 1
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Picture: the picture every legacy decoder keeps between frames (they are all inter-frame
|
||||
-- codecs: a packet paints over the previous picture). Palettised codecs keep a u8 index plane and a 256-entry palette,
|
||||
-- direct-colour ones an rgba8 plane; both planes are padded (stride / rows) so the codecs' block and run writes past
|
||||
-- the visible edge land in range. Also the 5-5-5 / 5-6-5 to 8-bit tables: channels are widened by bit replication
|
||||
-- (c << 3 | c >> 2), as ffmpeg's swscale does, so the output equals ffmpeg's rgba conversion. New in rosetta.
|
||||
-- codecs/video/Legacy/Picture: the picture the legacy inter-frame decoders paint over (u8 index plane + palette or rgba8).
|
||||
-- 5-5-5 / 5-6-5 widen by bit replication (c << 3 | c >> 2), as ffmpeg's swscale does.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
|
||||
@@ -23,7 +20,6 @@ export type Picture = {
|
||||
pal: buffer, -- 256 u32 entries, bytes R, G, B, A
|
||||
}
|
||||
|
||||
-- new(w, h, palettised, padW?, padH?, palette?) -> a black picture; padW / padH round the plane up (blocks of 4, ...)
|
||||
function Picture.new(w: number, h: number, palettised: boolean, padW: number?, padH: number?, palette: buffer?): Picture
|
||||
local pw, ph = padW or 1, padH or 1
|
||||
local stride = ((w + pw - 1) // pw) * pw
|
||||
@@ -41,7 +37,6 @@ function Picture.new(w: number, h: number, palettised: boolean, padW: number?, p
|
||||
return p
|
||||
end
|
||||
|
||||
-- image(p) -> a fresh rgba8 Image of the visible w x h
|
||||
function Picture.image(p: Picture): Types.Image
|
||||
local w, h = p.w, p.h
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
@@ -60,7 +55,6 @@ function Picture.image(p: Picture): Types.Image
|
||||
return img
|
||||
end
|
||||
|
||||
-- setPalette(p, src): a 1024-byte palette (u32 R, G, B, A entries) from a packet or the track
|
||||
function Picture.setPalette(p: Picture, src: buffer)
|
||||
if buffer.len(src) >= 1024 then
|
||||
buffer.copy(p.pal, 0, src, 0, 1024)
|
||||
@@ -70,7 +64,6 @@ end
|
||||
local lut555: buffer? = nil
|
||||
local lut565: buffer? = nil
|
||||
|
||||
-- rgb555() -> u32 table: X-R-G-B 1-5-5-5 value -> rgba8 pixel (opaque); 32768 entries
|
||||
function Picture.rgb555(): buffer
|
||||
local t = lut555
|
||||
if t then
|
||||
@@ -86,7 +79,6 @@ function Picture.rgb555(): buffer
|
||||
return b
|
||||
end
|
||||
|
||||
-- rgb565() -> u32 table: R-G-B 5-6-5 value -> rgba8 pixel; 65536 entries
|
||||
function Picture.rgb565(): buffer
|
||||
local t = lut565
|
||||
if t then
|
||||
@@ -102,7 +94,6 @@ function Picture.rgb565(): buffer
|
||||
return b
|
||||
end
|
||||
|
||||
-- copy of a picture's plane (for decoders that read the previous picture while writing the next)
|
||||
function Picture.clonePlane(p: Picture): buffer
|
||||
local src = (p.idx or p.rgba) :: buffer
|
||||
local c = buffer.create(buffer.len(src))
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/QtRle: QuickTime Animation ('rle ') frames, depths 1, 2, 4, 8 (palettised; 33..40 = grey) and
|
||||
-- 16 (X-R-G-B 1-5-5-5), 24 (R, G, B), 32 (A, R, G, B). Written from the format description (Apple QuickTime File
|
||||
-- Format; MultimediaWiki "Apple QuickTime RLE"): a chunk size, a header (0x0008: start line + line count), then per
|
||||
-- line a skip byte and run codes until -1 (0: another skip; < 0: one pixel group repeated; > 0: literal groups).
|
||||
-- Groups are 4 pixels at 8 bits, 8 at 4 bits, 16 at 2 bits, 1 pixel at 16 / 24 / 32 bits; the 1-bit layout has its
|
||||
-- own (skip, code) pairs where a skip with bit 7 set moves to the next line. Paints over the previous picture; frames
|
||||
-- shorter than 8 bytes repeat it. Bit-exact with ffmpeg's qtrle decoder on valid streams. Never throws: every read
|
||||
-- and write is range-checked (a damaged frame stops where the damage is).
|
||||
-- codecs/video/Legacy/QtRle: QuickTime Animation ('rle ') frames, depths 1, 2, 4, 8 (palettised; 33..40 = grey),
|
||||
-- 16 (X-R-G-B 1-5-5-5), 24 (R, G, B), 32 (A, R, G, B). From Apple QuickTime File Format and MultimediaWiki "Apple
|
||||
-- QuickTime RLE", bit-exact with ffmpeg on valid streams: chunk size, header (0x0008: start line + line count), per line
|
||||
-- a skip byte and codes until -1 (0: another skip; < 0: one group repeated; > 0: literal groups). Groups are 4 pixels at
|
||||
-- 8 bits, 8 at 4, 16 at 2, 1 at 16 / 24 / 32. Frames shorter than 8 bytes repeat the picture.
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
@@ -83,8 +80,8 @@ end
|
||||
|
||||
-- 2 / 4 / 8-bit: groups of 16 / 8 / 4 indexes (4 bytes); skips count groups
|
||||
local function decodeIdx(px: buffer, stride: number, limit: number, d: buffer, p: number, n: number, row: number, lines: number, bpp: number)
|
||||
local group = 32 // bpp -- pixels per 4 bytes
|
||||
local per = 8 // bpp -- pixels per byte
|
||||
local group = 32 // bpp
|
||||
local per = 8 // bpp
|
||||
local mask = 2 ^ bpp - 1
|
||||
for _ = 1, lines do
|
||||
if p >= n then
|
||||
@@ -224,11 +221,10 @@ local function decodeRgb(px: buffer, stride: number, limit: number, d: buffer, p
|
||||
end
|
||||
end
|
||||
|
||||
-- decode(pic, data, depth) -> true (a picture, possibly the previous one again) | false, message
|
||||
function QtRle.decode(pic: Picture.Picture, d: buffer, depth: number): (boolean, string?)
|
||||
local n = buffer.len(d)
|
||||
if n < 8 then
|
||||
return true, nil -- no change
|
||||
return true, nil
|
||||
end
|
||||
local p = 4
|
||||
local header = readu8(d, p) * 256 + readu8(d, p + 1)
|
||||
@@ -264,7 +260,6 @@ function QtRle.decode(pic: Picture.Picture, d: buffer, depth: number): (boolean,
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- palettised(depth) -> whether the depth uses a palette; pad(depth) -> the row padding the plane needs
|
||||
function QtRle.palettised(depth: number): boolean
|
||||
return depth <= 8 or depth > 32
|
||||
end
|
||||
|
||||
@@ -2,17 +2,12 @@
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Smacker: RAD Game Tools' Smacker video (SMK2 / SMK4 frames; the file format is video/smk) and
|
||||
-- the Huffman trees Smacker audio shares. Written from the format description (MultimediaWiki "Smacker"). Bits are
|
||||
-- read LSB first. A tree is a pre-order bit string (1 = node: left subtree then right; 0 = leaf with its value);
|
||||
-- an 8-bit tree's leaves hold bytes, a "big" tree's leaves hold 16-bit values made of a low-byte and a high-byte
|
||||
-- 8-bit tree, three of them being escape leaves that return the three most recently decoded values instead (a
|
||||
-- per-frame cache, reset to 0 at each frame). The four big trees (MMAP, MCLR, FULL, TYPE) come from the file header;
|
||||
-- a frame is 4x4 blocks in raster order, each TYPE code giving a block type and a run length: MONO (two colours and a
|
||||
-- 16-bit mask), FULL (16 pixels as 8 codes; SMK4 adds a 2x2-doubled and a 2x1-doubled mode), SKIP, FILL (one colour).
|
||||
-- Trees are flat arrays (a node stores the size of its left subtree, as a negative number); the frame decoder reads
|
||||
-- 10 bits at a time through a lookup table into them.
|
||||
-- Bit-exact with ffmpeg's smackvid decoder. Never throws: trees are bounded (depth, entries), reads past the data give
|
||||
-- zero bits, a frame whose codes run out of bits gives no picture.
|
||||
-- the Huffman trees Smacker audio shares. From MultimediaWiki "Smacker", bit-exact with ffmpeg's smackvid.
|
||||
-- Bits LSB first. A tree is a pre-order bit string (1 = node: left subtree then right; 0 = leaf). A big tree's 16-bit
|
||||
-- leaves pair a low-byte and a high-byte 8-bit tree; three escape leaves return the three most recently decoded values
|
||||
-- (reset each frame). The header holds the MMAP, MCLR, FULL and TYPE big trees; a frame is 4x4 blocks in raster order,
|
||||
-- each TYPE code a block type and run length: MONO (two colours + 16-bit mask), FULL (16 pixels as 8 codes; SMK4 adds
|
||||
-- 2x2- and 2x1-doubled modes), SKIP, FILL.
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
@@ -30,7 +25,6 @@ for i = 1, 59 do
|
||||
end
|
||||
RUNS[60], RUNS[61], RUNS[62], RUNS[63], RUNS[64] = 128, 256, 512, 1024, 2048
|
||||
|
||||
-- a bit position over buf[0, nbits / 8)
|
||||
export type Bits = { buf: buffer, pos: number, nbits: number }
|
||||
|
||||
function Smacker.bits(buf: buffer, offset: number, length: number): Bits
|
||||
@@ -47,7 +41,6 @@ local function bit(br: Bits): number
|
||||
end
|
||||
Smacker.bit = bit
|
||||
|
||||
-- read(br, n) -> n bits (LSB first; zeros past the end)
|
||||
function Smacker.read(br: Bits, n: number): number
|
||||
local v, m = 0, 1
|
||||
for _ = 1, n do
|
||||
@@ -58,7 +51,7 @@ function Smacker.read(br: Bits, n: number): number
|
||||
end
|
||||
local readBits = Smacker.read
|
||||
|
||||
-- walk(tree, br) -> the leaf value (no end-of-data check: 8-bit trees, as the reference's table lookups)
|
||||
-- no end-of-data check for 8-bit trees, as the reference's table lookups
|
||||
local function walk(tree: { number }, br: Bits): number
|
||||
local i = 1
|
||||
local e = tree[1]
|
||||
@@ -73,7 +66,6 @@ local function walk(tree: { number }, br: Bits): number
|
||||
end
|
||||
Smacker.walk = walk
|
||||
|
||||
-- the pre-order tree reader: leaf(index) -> value | nil, message; returns the flat tree (1-based)
|
||||
local function build(br: Bits, maxDepth: number, maxEntries: number, leaf: (number) -> (number?, string?), bigChecks: boolean): ({ number }?, string?)
|
||||
local vals: { number } = {}
|
||||
local n = 0
|
||||
@@ -120,7 +112,6 @@ local function build(br: Bits, maxDepth: number, maxEntries: number, leaf: (numb
|
||||
end
|
||||
end
|
||||
|
||||
-- tree8(br) -> an 8-bit tree (leaves are bytes) | nil, message
|
||||
function Smacker.tree8(br: Bits): ({ number }?, string?)
|
||||
local leaves = 0
|
||||
return build(br, MAX_DEPTH8, 511, function(_: number): (number?, string?)
|
||||
@@ -138,7 +129,6 @@ local tree8 = Smacker.tree8
|
||||
|
||||
export type BigTree = { vals: { number }, last: { number }, lut: buffer? } -- last: the escape leaves' indexes (1-based)
|
||||
|
||||
-- bigTree(br, size) -> one of the header's trees | nil, message (size = its byte size from the header: bounds entries)
|
||||
local function bigTree(br: Bits, size: number): (BigTree?, string?)
|
||||
if bit(br) == 0 then
|
||||
return { vals = { 0, 0 }, last = { 2, 2, 2 } }, nil -- no tree: every code is 0
|
||||
@@ -184,7 +174,7 @@ end
|
||||
|
||||
export type Trees = { mmap: BigTree, mclr: BigTree, full: BigTree, type: BigTree }
|
||||
|
||||
-- trees(config) -> the four header trees | nil, message. config = the four u32 tree sizes then the tree bits
|
||||
-- config = the four u32 tree sizes, then the tree bits
|
||||
function Smacker.trees(config: buffer): (Trees?, string?)
|
||||
local n = buffer.len(config)
|
||||
if n <= 16 then
|
||||
@@ -241,7 +231,6 @@ local function reset(t: BigTree)
|
||||
vals[last[1]], vals[last[2]], vals[last[3]] = 0, 0, 0
|
||||
end
|
||||
|
||||
-- decode(pic, data, trees, v4) -> true | false, message. pic: palettised, padded to blocks of 4
|
||||
function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boolean): (boolean, string?)
|
||||
local px = pic.idx :: buffer
|
||||
local stride = pic.stride
|
||||
@@ -255,7 +244,6 @@ function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boole
|
||||
local n = buffer.len(d)
|
||||
local nbits = n * 8
|
||||
local pos = 0
|
||||
-- the next value of a tree (updating its recent-value cache) | -1 when the bits run out inside the tree
|
||||
local function code(t: BigTree): number
|
||||
local vals = t.vals
|
||||
local i, p
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Smc: QuickTime Graphics ('smc ') frames, 8-bit palettised. Written from the format description
|
||||
-- (MultimediaWiki "Apple SMC"): 4x4 blocks in raster order, opcodes in the high nibble with a block count (low nibble
|
||||
-- + 1, or the next byte + 1 when bit 4 is set): skip, repeat the last block, repeat the last pair of blocks, 1-colour
|
||||
-- fill, 2 / 4 / 8-colour blocks (the colour groups go into 256-entry rotating tables, or come from them by index:
|
||||
-- the tables restart every frame) and 16-colour blocks. Paints over the previous picture. Bit-exact with ffmpeg's smc
|
||||
-- decoder (for widths whose rows are not padded: the previous block of a row start is the last block of the row
|
||||
-- above). Never throws: reads past the data give zeros, the block count bounds every write.
|
||||
-- codecs/video/Legacy/Smc: QuickTime Graphics ('smc ') frames, 8-bit palettised; from MultimediaWiki "Apple SMC",
|
||||
-- bit-exact with ffmpeg. 4x4 blocks in raster order; opcode in the high nibble, count = low nibble + 1 (bit 4: next
|
||||
-- byte + 1); the colour-group tables restart every frame. As ffmpeg: a row start's previous block ends the row above.
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
@@ -15,7 +11,6 @@ local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local Smc = {}
|
||||
|
||||
-- decode(pic, data, tables) -> true | false, message. tables = Smc.tables() (scratch, reused between frames)
|
||||
function Smc.decode(pic: Picture.Picture, d: buffer, tables: buffer): (boolean, string?)
|
||||
local px = pic.idx :: buffer
|
||||
local n = buffer.len(d)
|
||||
@@ -27,7 +22,6 @@ function Smc.decode(pic: Picture.Picture, d: buffer, tables: buffer): (boolean,
|
||||
end
|
||||
local blocksWide = (pic.w + 3) // 4
|
||||
local p = 4 -- flags byte + 24-bit chunk size
|
||||
-- block position: bx, by (blocks)
|
||||
local bx, by = 0, 0
|
||||
local pairI, quadI, octI = 0, 0, 0
|
||||
-- tables: pairs at 0 (256 * 2), quads at 512 (256 * 4), octets at 1536 (256 * 8)
|
||||
@@ -159,7 +153,6 @@ function Smc.decode(pic: Picture.Picture, d: buffer, tables: buffer): (boolean,
|
||||
advance()
|
||||
end
|
||||
else
|
||||
-- 16-colour blocks
|
||||
for _ = 1, count do
|
||||
if total <= 0 or by >= bh then
|
||||
break
|
||||
@@ -175,7 +168,6 @@ function Smc.decode(pic: Picture.Picture, d: buffer, tables: buffer): (boolean,
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- tables() -> the colour group scratch (pairs, quads, octets)
|
||||
function Smc.tables(): buffer
|
||||
return buffer.create(512 + 1024 + 2048)
|
||||
end
|
||||
|
||||
@@ -1,22 +1,7 @@
|
||||
--!strict
|
||||
-- codecs/video/Legacy: FLI / FLC, Cinepak, Microsoft Video 1, QuickTime RLE / SMC and Smacker video decoders.
|
||||
-- The 1990s inter-frame codecs as codecs/video/Video registry decoders (rgba8 pictures, one per packet): Autodesk
|
||||
-- FLI / FLC ("flic", the frames of video/fli), Cinepak ("cinepak",
|
||||
-- 'cvid'), Microsoft Video 1 ("msvideo1": CRAM / MSVC / WHAM, 8-bit palettised and 16-bit), QuickTime Animation
|
||||
-- ("qtrle", 'rle ': depths 1 / 2 / 4 / 8 / 16 / 24 / 32 and grey), QuickTime Graphics ("smc", 'smc ') and Smacker
|
||||
-- video ("smackvid", the frames of video/smk: track.config = the header's tree sizes and trees, codecTag SMK2 / SMK4).
|
||||
-- New in rosetta, written from the format descriptions (see each submodule); decoding is bit-exact with ffmpeg's
|
||||
-- decoders (tests/codecs/legacy.luau). Every decoder keeps the previous picture (a packet paints over it) and returns
|
||||
-- a fresh copy.
|
||||
--
|
||||
-- Palettes come from the Track: track.palette (256 u32 entries, bytes R, G, B, A: the AVI BITMAPINFO palette, the
|
||||
-- QuickTime sample description's colour table, ...) and a packet's own `palette` (same layout) replaces it from that
|
||||
-- packet on. Depths come from track.bitsPerPixel (AVI biBitCount, the QuickTime depth).
|
||||
--
|
||||
-- local dec, err = Legacy.newDecoder("cinepak", track, { maxPixels = 2^22 }) -- a Types.VideoCodecDecoder
|
||||
-- Legacy.CODECS --> { "flic", "cinepak", "msvideo1", "qtrle", "smc", "smackvid" }
|
||||
--
|
||||
-- Never throws: bad tracks return nil, Err; bad packets return Err from decode().
|
||||
-- From the format descriptions (see each submodule), bit-exact with ffmpeg. Palette: track.palette, replaced by a
|
||||
-- packet's palette (256 u32 entries R, G, B, A); depth: track.bitsPerPixel (AVI biBitCount, QuickTime depth).
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Picture = require("@rosetta/codecs/video/Legacy/Picture")
|
||||
@@ -39,11 +24,10 @@ Legacy.Smacker = Smacker
|
||||
Legacy.CODECS = { "flic", "cinepak", "msvideo1", "qtrle", "smc", "smackvid" }
|
||||
|
||||
export type Options = {
|
||||
maxPixels: number?, -- refuse bigger pictures (LIMIT; default 2^26)
|
||||
maxPixels: number?,
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
-- the per-codec pieces: make(track) -> state, step(state, data, packet) -> image? | nil, Err?
|
||||
type Codec = {
|
||||
make: (track: Types.Track) -> (any, string?),
|
||||
step: (st: any, data: buffer, packet: Types.Packet) -> (Types.Image?, Err.Err?),
|
||||
@@ -165,7 +149,6 @@ local CODECS: { [string]: Codec } = {
|
||||
},
|
||||
}
|
||||
|
||||
-- newDecoder(id, track, opts?) -> a Types.VideoCodecDecoder | nil, Err
|
||||
function Legacy.newDecoder(id: string, track: Types.Track, opts: Options?): (Types.VideoCodecDecoder?, Err.Err?)
|
||||
local codec = CODECS[id]
|
||||
if not codec then
|
||||
@@ -183,7 +166,6 @@ function Legacy.newDecoder(id: string, track: Types.Track, opts: Options?): (Typ
|
||||
if not st then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
-- Video.simpleDecoder, without requiring the registry (which requires this module)
|
||||
local queue: { any } = {}
|
||||
local self = {}
|
||||
function self.decode(_: any, packet: Types.Packet): Err.Err?
|
||||
@@ -206,7 +188,7 @@ function Legacy.newDecoder(id: string, track: Types.Track, opts: Options?): (Typ
|
||||
function self.flush(_: any) end
|
||||
function self.reset(_: any)
|
||||
table.clear(queue)
|
||||
st = codec.make(track) or st -- back to a black picture with the track's palette (decoding restarts at a keyframe)
|
||||
st = codec.make(track) or st
|
||||
end
|
||||
return self :: any, nil
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user