Files
rosetta/src/Rosetta/codecs/video/H264/Slice.luau
T

250 lines
6.5 KiB
Luau

--!strict
--!native
-- codecs/video/H264/Slice: the slice header (ITU-T H.264 7.3.3): ref_pic_list_modification, pred_weight_table and
-- dec_ref_pic_marking. Frame coding only: field pictures, MBAFF, SP / SI, separate colour planes and redundant
-- pictures return "unsupported: ...".
local Bitstream = require("@rosetta/codecs/video/H264/Bitstream")
local u, u1, ue, se = Bitstream.u, Bitstream.u1, Bitstream.ue, Bitstream.se
local Slice = {}
Slice.P, Slice.B, Slice.I, Slice.SP, Slice.SI = 0, 1, 2, 3, 4
local function modifications(r: Bitstream.Reader): { number }?
local ops = {}
if u1(r) == 1 then
for _ = 1, 100 do
local idc = ue(r)
if idc == 3 then
return ops
end
if idc > 5 then
return nil
end
table.insert(ops, idc)
table.insert(ops, ue(r))
if r.pos > r.size then
return nil
end
end
return nil
end
return ops
end
local function weights(r: Bitstream.Reader, n: number, lw: number, cw: number, chroma: boolean): { number }?
-- per entry: lumaW, lumaO, cbW, cbO, crW, crO, lumaFlag, chromaFlag (8 numbers)
local t = table.create(n * 8, 0)
for i = 0, n - 1 do
local b = i * 8
t[b + 1], t[b + 2], t[b + 3], t[b + 4], t[b + 5], t[b + 6] = lw, 0, cw, 0, cw, 0
if u1(r) == 1 then
t[b + 1] = se(r)
t[b + 2] = se(r)
t[b + 7] = 1
if t[b + 1] < -128 or t[b + 1] > 127 or t[b + 2] < -128 or t[b + 2] > 127 then
return nil
end
end
if chroma and u1(r) == 1 then
for k = 0, 1 do
t[b + 3 + k * 2] = se(r)
t[b + 4 + k * 2] = se(r)
if t[b + 3 + k * 2] < -128 or t[b + 3 + k * 2] > 127 or t[b + 4 + k * 2] < -128 or t[b + 4 + k * 2] > 127 then
return nil
end
end
t[b + 8] = 1
end
end
return t
end
function Slice.header(r: Bitstream.Reader, nalType: number, nalRefIdc: number, spsTable: { [number]: any }, ppsTable: { [number]: any }): (any, string?)
local sh: any = {}
sh.nalType = nalType
sh.nalRefIdc = nalRefIdc
sh.idr = nalType == 5
sh.firstMb = ue(r)
local st = ue(r)
if st > 9 then
return nil, "slice_type out of range"
end
sh.type = st % 5
if sh.type == Slice.SP or sh.type == Slice.SI then
return nil, "unsupported: SP / SI slices (Extended profile)"
end
sh.ppsId = ue(r)
local pps = ppsTable[sh.ppsId]
if not pps then
return nil, "slice refers to a missing pps"
end
local sps = spsTable[pps.spsId]
if not sps then
return nil, "slice refers to a missing sps"
end
sh.pps, sh.sps = pps, sps
if sps.separatePlanes then
return nil, "unsupported: separate colour planes"
end
sh.frameNum = u(r, sps.log2MaxFrameNum)
if not sps.frameMbsOnly then
if u1(r) == 1 then
return nil, "unsupported: field pictures (PAFF)"
end
if sps.mbaff then
return nil, "unsupported: MBAFF"
end
end
if sh.idr then
sh.idrPicId = ue(r)
end
sh.pocLsb, sh.deltaPocBottom, sh.deltaPoc0, sh.deltaPoc1 = 0, 0, 0, 0
if sps.pocType == 0 then
sh.pocLsb = u(r, sps.log2MaxPocLsb)
if pps.bottomFieldPocPresent then
sh.deltaPocBottom = se(r)
end
elseif sps.pocType == 1 and not sps.deltaPocAlwaysZero then
sh.deltaPoc0 = se(r)
if pps.bottomFieldPocPresent then
sh.deltaPoc1 = se(r)
end
end
if pps.redundantPicCnt then
sh.redundantPicCnt = ue(r)
end
sh.numRefIdxL0, sh.numRefIdxL1 = 0, 0
if sh.type == Slice.B then
sh.directSpatial = u1(r) == 1
end
if sh.type ~= Slice.I then
sh.numRefIdxL0 = pps.numRefIdxL0
sh.numRefIdxL1 = if sh.type == Slice.B then pps.numRefIdxL1 else 0
if u1(r) == 1 then
sh.numRefIdxL0 = ue(r) + 1
if sh.type == Slice.B then
sh.numRefIdxL1 = ue(r) + 1
end
end
if sh.numRefIdxL0 > 32 or sh.numRefIdxL1 > 32 then
return nil, "num_ref_idx_active out of range"
end
local m = modifications(r)
if not m then
return nil, "bad ref_pic_list_modification"
end
sh.mod0 = m
if sh.type == Slice.B then
m = modifications(r)
if not m then
return nil, "bad ref_pic_list_modification"
end
sh.mod1 = m
end
end
sh.weightMode = 0 -- 0 default, 1 explicit, 2 implicit
if (pps.weightedPred and sh.type == Slice.P) or (pps.weightedBipred == 1 and sh.type == Slice.B) then
sh.weightMode = 1
sh.lumaDenom = ue(r)
if sh.lumaDenom > 7 then
return nil, "luma_log2_weight_denom out of range"
end
local chroma = sps.chromaFormat ~= 0
sh.chromaDenom = 0
if chroma then
sh.chromaDenom = ue(r)
if sh.chromaDenom > 7 then
return nil, "chroma_log2_weight_denom out of range"
end
end
local w = weights(r, sh.numRefIdxL0, 2 ^ sh.lumaDenom, 2 ^ sh.chromaDenom, chroma)
if not w then
return nil, "bad pred_weight_table"
end
sh.weights0 = w
if sh.type == Slice.B then
w = weights(r, sh.numRefIdxL1, 2 ^ sh.lumaDenom, 2 ^ sh.chromaDenom, chroma)
if not w then
return nil, "bad pred_weight_table"
end
sh.weights1 = w
end
elseif pps.weightedBipred == 2 and sh.type == Slice.B then
sh.weightMode = 2
end
if nalRefIdc ~= 0 then
if sh.idr then
sh.noOutputOfPriorPics = u1(r) == 1
sh.longTermReference = u1(r) == 1
else
sh.adaptive = u1(r) == 1
if sh.adaptive then
local ops = {}
for _ = 1, 100 do
local op = ue(r)
if op == 0 then
break
end
if op > 6 then
return nil, "bad memory_management_control_operation"
end
local a, b = 0, 0
if op == 1 or op == 3 then
a = ue(r) + 1 -- difference_of_pic_nums
end
if op == 2 then
a = ue(r) -- long_term_pic_num
end
if op == 3 or op == 6 then
b = ue(r) -- long_term_frame_idx
end
if op == 4 then
a = ue(r) -- max_long_term_frame_idx_plus1
end
if r.pos > r.size then
return nil, "truncated: dec_ref_pic_marking"
end
table.insert(ops, op)
table.insert(ops, a)
table.insert(ops, b)
end
sh.mmco = ops
end
end
end
sh.cabacInit = 0
if pps.cabac and sh.type ~= Slice.I then
sh.cabacInit = ue(r)
if sh.cabacInit > 2 then
return nil, "cabac_init_idc out of range"
end
end
sh.qp = pps.initQp + se(r)
if sh.qp < 0 or sh.qp > 51 then
return nil, "slice QP out of range"
end
sh.deblock = 0
sh.alphaOffset, sh.betaOffset = 0, 0
if pps.deblockingControl then
sh.deblock = ue(r)
if sh.deblock > 2 then
return nil, "disable_deblocking_filter_idc out of range"
end
if sh.deblock ~= 1 then
sh.alphaOffset = se(r) * 2
sh.betaOffset = se(r) * 2
if sh.alphaOffset < -12 or sh.alphaOffset > 12 or sh.betaOffset < -12 or sh.betaOffset > 12 then
return nil, "slice_alpha / beta_offset out of range"
end
end
end
if r.pos > r.size then
return nil, "truncated: slice header"
end
return sh, nil
end
return Slice