146 lines
5.7 KiB
Luau
146 lines
5.7 KiB
Luau
--!strict
|
|
-- video/h264: raw H.264 / AVC elementary streams (ITU-T H.264 Annex B byte stream: .h264 / .264 files, x264 / ffmpeg
|
|
-- `-f h264` output, the JVT conformance bitstreams). New in formats-luau.
|
|
-- Read: the stream cut into access units (codecs/H264.accessUnits) as a one-track Demuxer; open() returns a
|
|
-- VideoDecoder decoding through codecs/H264 (yuv420p frames in display order, numbered in output order since a raw
|
|
-- stream has no timestamps; the frame rate comes from the SPS VUI timing, else opts.frameRate, else 25).
|
|
-- Write: H.264 tracks copied from any video (MP4 / MKV samples are converted from length-prefixed NAL units to Annex B,
|
|
-- the avcC parameter sets put in front of every keyframe). Not supported: re-encoding other codecs to H.264 (there is
|
|
-- no H.264 encoder), and whatever codecs/H264 does not decode (interlaced, 4:2:2 / 4:4:4, > 8 bits: UNSUPPORTED).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Demux = require("@formats/containers/Demux")
|
|
local Video = require("@formats/codecs/Video")
|
|
local Source = require("@formats/codecs/Video/Source")
|
|
local H264 = require("@formats/codecs/H264")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Format = {
|
|
name = "h264",
|
|
category = "video",
|
|
extensions = { "h264", "264", "avc", "jsv", "26l", "jvt" },
|
|
mime = { "video/h264" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "containers/Demux", "codecs/Video", "codecs/Video/Source", "codecs/H264" },
|
|
caps = { decode = true, encode = true, streaming = true, video = { "h264" }, depths = { 8 } },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type OpenOptions = {
|
|
rgba: boolean?, -- nextFrame returns rgba8 (default false: yuv420p)
|
|
maxPixels: number?, -- largest picture (default 2^22 luma samples)
|
|
frameRate: number?, -- frames per second when the stream has no VUI timing (default 25)
|
|
deblock: boolean?, -- false skips the deblocking filter (faster, not conformant)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
-- a start code then an SPS / AUD / SEI / PPS NAL header (forbidden bit 0)
|
|
function Format.sniff(r: Bytes.Reader): number
|
|
local s = r:peek(5)
|
|
if not s or #s < 4 then
|
|
return 0
|
|
end
|
|
local hdr
|
|
if string.byte(s, 1) == 0 and string.byte(s, 2) == 0 and string.byte(s, 3) == 1 then
|
|
hdr = string.byte(s, 4)
|
|
elseif #s >= 5 and string.byte(s, 1) == 0 and string.byte(s, 2) == 0 and string.byte(s, 3) == 0 and string.byte(s, 4) == 1 then
|
|
hdr = string.byte(s, 5)
|
|
else
|
|
return 0
|
|
end
|
|
if hdr >= 128 then
|
|
return 0
|
|
end
|
|
local t = hdr % 32
|
|
if t == 7 then
|
|
return 0.8
|
|
elseif t == 9 or t == 6 or t == 8 then
|
|
return 0.5
|
|
elseif t == 5 then
|
|
return 0.4
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- demux(r, opts?) -> the Demuxer (one video track, one packet per access unit) | nil, Err
|
|
function Format.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
|
local buf, base, limit = r.buf, r.base, r.limit
|
|
local units = H264.accessUnits(buf, base, limit - base)
|
|
if #units == 0 then
|
|
return nil, Err.new(Err.CORRUPT, "no access units (no slices after a start code)", base)
|
|
end
|
|
local info = H264.info(buffer.readstring(buf, base, math.min(limit - base, 4096))) or {}
|
|
if info.interlaced or (info.chromaFormat and info.chromaFormat ~= 1) or (info.bitDepth and info.bitDepth ~= 8) then
|
|
-- still demuxable (remuxing works); decoding reports UNSUPPORTED
|
|
info.unsupported = true
|
|
end
|
|
local fps = info.frameRate or (opts and opts.frameRate) or 25
|
|
if fps <= 0 or fps > 1000 then
|
|
fps = 25
|
|
end
|
|
local track: Types.Track = {
|
|
id = 1,
|
|
kind = "video",
|
|
codec = "h264",
|
|
timescale = fps,
|
|
width = info.width,
|
|
height = info.height,
|
|
frameRate = fps,
|
|
profile = info.profile,
|
|
level = info.level,
|
|
ptsInOutputOrder = true,
|
|
default = true,
|
|
}
|
|
local dmx = Demux.new("h264", { track }, buf, { profile = info.profile, level = info.level })
|
|
local k = 0
|
|
for i = 1, #units, 3 do
|
|
dmx:add(1, k, k, 1, units[i + 2] or k == 0, units[i], units[i + 1])
|
|
k += 1
|
|
end
|
|
track.frames = k
|
|
dmx:finish()
|
|
return dmx, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
-- encode(video, opts?) -> an Annex B buffer of the source's H.264 track | nil, Err (UNSUPPORTED for other codecs)
|
|
function Format.encode(value: any, _opts: any?): (buffer?, Err.Err?)
|
|
local streams, err = Source.streams(value, { audio = false }, { video = { copy = { h264 = true } } })
|
|
if not streams then
|
|
return nil, err
|
|
end
|
|
local s = streams[1]
|
|
if not s or s.codec ~= "h264" then
|
|
return nil, Err.new(Err.UNSUPPORTED, "only H.264 tracks can be written as a raw H.264 stream")
|
|
end
|
|
local parts = {}
|
|
for i, p in s.packets do
|
|
local data = p.data
|
|
if s.config then
|
|
local key = i == 1 or p.keyframe
|
|
local out, e = H264.toAnnexB(if key then s.config else nil, data)
|
|
if not out then
|
|
return nil, e
|
|
end
|
|
data = out
|
|
end
|
|
table.insert(parts, buffer.tostring(data))
|
|
end
|
|
return buffer.fromstring(table.concat(parts)), nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
|
function Format.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
|
local dmx, err = Format.demux(r, opts)
|
|
if not dmx then
|
|
return nil, err
|
|
end
|
|
return Video.open(dmx, opts)
|
|
end
|
|
|
|
return Format
|