--!strict --!native --!optimize 2 -- codecs/video/H264/Params: sequence and picture parameter sets (ITU-T H.264 7.3.2.1 / 7.3.2.2 / E.1), scaling lists -- with the fall-back rules A and B (7.4.2.1.1 / table 7-2), and the dequantisation tables built from them (8.5.9: the -- LevelScale4x4 / LevelScale8x8 functions, premultiplied by 2^(qP / 6)). local Bitstream = require("@rosetta/codecs/video/H264/Bitstream") local Tables = require("@rosetta/codecs/video/H264/Tables") local u, u1, ue, se = Bitstream.u, Bitstream.u1, Bitstream.ue, Bitstream.se local Params = {} local HIGH_PROFILES = { [100] = true, [110] = true, [122] = true, [244] = true, [44] = true, [83] = true, [86] = true, [118] = true, [128] = true, [138] = true, [139] = true, [134] = true, [135] = true } -- MaxDpbMbs by level_idc (table A-1); level 9 is level 1b local MAX_DPB_MBS = { [9] = 396, [10] = 396, [11] = 900, [12] = 2376, [13] = 2376, [20] = 2376, [21] = 4752, [22] = 8100, [30] = 8100, [31] = 18000, [32] = 20480, [40] = 32768, [41] = 32768, [42] = 34816, [50] = 110400, [51] = 184320, [52] = 184320, [60] = 696320, [61] = 696320, [62] = 696320 } local ZIGZAG4, ZIGZAG8 = Tables.ZIGZAG4, Tables.ZIGZAG8 -- scaling_list(): raster order, or "default" (useDefaultScalingMatrixFlag) local function scalingList(r: Bitstream.Reader, size: number): any local list = table.create(size, 0) local last, nxt = 8, 8 local zz = if size == 16 then ZIGZAG4 else ZIGZAG8 for j = 0, size - 1 do if nxt ~= 0 then local d = se(r) if d < -128 or d > 127 then return nil end nxt = (last + d + 256) % 256 if j == 0 and nxt == 0 then return "default" end end local v = if nxt == 0 then last else nxt list[zz[j + 1] + 1] = v last = v end return list end -- lists: 12 entries (false = not present, "default", or a raster list) local function scalingMatrix(r: Bitstream.Reader, count: number): { any }? local lists = table.create(12, false) for i = 1, count do if u1(r) == 1 then local l = scalingList(r, if i <= 6 then 16 else 64) if l == nil then return nil end lists[i] = l end end return lists end local function default(i: number): { number } local out = {} if i <= 6 then local base = if i <= 3 then 0 else 16 for k = 1, 16 do out[k] = Tables.DEFAULT_4x4[base + k] end else local base = if (i - 7) % 2 == 0 then 0 else 64 for k = 1, 64 do out[k] = Tables.DEFAULT_8x8[base + k] end end return out end local FLAT16, FLAT64 = table.create(16, 16), table.create(64, 16) -- fallback nil = rule A (defaults), else the SPS's 12 lists (rule B) local function resolve(lists: { any }, fallback: { { number } }?): { { number } } local out = {} for i = 1, 12 do local l = lists[i] if l == "default" then out[i] = default(i) elseif l then out[i] = l elseif i == 1 or i == 4 or i == 7 or i == 8 then out[i] = if fallback then fallback[i] else default(i) elseif i <= 6 then out[i] = out[i - 1] else out[i] = out[i - 2] end end return out end local function hrd(r: Bitstream.Reader) local cnt = ue(r) if cnt > 31 then return false end u(r, 8) for _ = 0, cnt do ue(r) ue(r) u1(r) end u(r, 20) return true end local function vui(r: Bitstream.Reader, sps: any) if u1(r) == 1 then local idc = u(r, 8) if idc == 255 then sps.sarW = u(r, 16) sps.sarH = u(r, 16) end sps.aspectIdc = idc end if u1(r) == 1 then u1(r) end if u1(r) == 1 then u(r, 3) sps.fullRange = u1(r) == 1 if u1(r) == 1 then sps.colourPrimaries = u(r, 8) sps.transfer = u(r, 8) sps.matrix = u(r, 8) end end if u1(r) == 1 then ue(r) ue(r) end if u1(r) == 1 then sps.unitsInTick = u(r, 32) sps.timeScale = u(r, 32) sps.fixedRate = u1(r) == 1 end local nal = u1(r) == 1 if nal and not hrd(r) then return end local vcl = u1(r) == 1 if vcl and not hrd(r) then return end if nal or vcl then u1(r) end u1(r) if u1(r) == 1 then u1(r) ue(r) ue(r) ue(r) ue(r) local reorder = ue(r) local dpb = ue(r) if r.pos <= r.size and reorder <= 16 and dpb <= 16 then sps.numReorderFrames = reorder sps.maxDecFrameBuffering = dpb end end end function Params.sps(r: Bitstream.Reader): (any, string?) local sps: any = {} sps.profile = u(r, 8) sps.constraints = u(r, 8) sps.level = u(r, 8) sps.id = ue(r) if sps.id > 31 then return nil, "seq_parameter_set_id out of range" end sps.chromaFormat = 1 sps.bitDepth = 8 sps.bitDepthC = 8 sps.bypass = false sps.scaling = nil if HIGH_PROFILES[sps.profile] then sps.chromaFormat = ue(r) if sps.chromaFormat > 3 then return nil, "chroma_format_idc out of range" end if sps.chromaFormat == 3 then sps.separatePlanes = u1(r) == 1 end sps.bitDepth = ue(r) + 8 sps.bitDepthC = ue(r) + 8 if sps.bitDepth > 14 or sps.bitDepthC > 14 then return nil, "bit depth out of range" end sps.bypass = u1(r) == 1 if u1(r) == 1 then local lists = scalingMatrix(r, if sps.chromaFormat ~= 3 then 8 else 12) if not lists then return nil, "bad scaling list" end sps.scaling = resolve(lists, nil) end end sps.log2MaxFrameNum = ue(r) + 4 if sps.log2MaxFrameNum > 16 then return nil, "log2_max_frame_num out of range" end sps.pocType = ue(r) if sps.pocType == 0 then sps.log2MaxPocLsb = ue(r) + 4 if sps.log2MaxPocLsb > 16 then return nil, "log2_max_pic_order_cnt_lsb out of range" end elseif sps.pocType == 1 then sps.deltaPocAlwaysZero = u1(r) == 1 sps.offsetNonRef = se(r) sps.offsetTopToBottom = se(r) local n = ue(r) if n > 255 then return nil, "num_ref_frames_in_pic_order_cnt_cycle out of range" end sps.offsetsForRefFrame = {} local expected = 0 for i = 1, n do sps.offsetsForRefFrame[i] = se(r) expected += sps.offsetsForRefFrame[i] end sps.expectedDeltaPerCycle = expected elseif sps.pocType ~= 2 then return nil, "pic_order_cnt_type out of range" end sps.maxRefFrames = ue(r) if sps.maxRefFrames > 16 then return nil, "max_num_ref_frames out of range" end sps.gapsAllowed = u1(r) == 1 sps.mbWidth = ue(r) + 1 sps.mbHeightMap = ue(r) + 1 sps.frameMbsOnly = u1(r) == 1 if not sps.frameMbsOnly then sps.mbaff = u1(r) == 1 end sps.mbHeight = if sps.frameMbsOnly then sps.mbHeightMap else sps.mbHeightMap * 2 if sps.mbWidth > 1024 or sps.mbHeight > 1024 then return nil, "limit: picture too large" end sps.direct8x8 = u1(r) == 1 sps.cropLeft, sps.cropRight, sps.cropTop, sps.cropBottom = 0, 0, 0, 0 if u1(r) == 1 then sps.cropLeft = ue(r) sps.cropRight = ue(r) sps.cropTop = ue(r) sps.cropBottom = ue(r) end if r.pos > r.size then return nil, "truncated: sequence parameter set" end if u1(r) == 1 then local save = r.pos vui(r, sps) if r.pos > r.size then r.pos = save end end -- derived sizes (4:2:0 crop units: 2 x 2; monochrome / 4:4:4 are rejected later) local cux, cuy = 2, 2 * (if sps.frameMbsOnly then 1 else 2) if sps.chromaFormat == 0 or sps.chromaFormat == 3 then cux, cuy = 1, (if sps.frameMbsOnly then 1 else 2) elseif sps.chromaFormat == 2 then cux, cuy = 2, (if sps.frameMbsOnly then 1 else 2) end local w, h = sps.mbWidth * 16, sps.mbHeight * 16 sps.width = w - cux * (sps.cropLeft + sps.cropRight) sps.height = h - cuy * (sps.cropTop + sps.cropBottom) sps.cropX = cux * sps.cropLeft sps.cropY = cuy * sps.cropTop if sps.width <= 0 or sps.height <= 0 then return nil, "cropping larger than the picture" end local maxMbs = MAX_DPB_MBS[sps.level] or 696320 if sps.level == 11 and sps.constraints % 32 >= 16 and sps.profile ~= 100 then maxMbs = 396 -- level 1b end sps.maxDpbFrames = math.clamp(maxMbs // (sps.mbWidth * sps.mbHeight), 1, 16) return sps, nil end function Params.pps(r: Bitstream.Reader, spsTable: { [number]: any }): (any, string?) local pps: any = {} pps.id = ue(r) if pps.id > 255 then return nil, "pic_parameter_set_id out of range" end pps.spsId = ue(r) local sps = spsTable[pps.spsId] if not sps then return nil, "pps refers to a missing sps" end pps.cabac = u1(r) == 1 pps.bottomFieldPocPresent = u1(r) == 1 pps.sliceGroups = ue(r) + 1 if pps.sliceGroups > 1 then return nil, "unsupported: slice groups (FMO)" end pps.numRefIdxL0 = ue(r) + 1 pps.numRefIdxL1 = ue(r) + 1 if pps.numRefIdxL0 > 32 or pps.numRefIdxL1 > 32 then return nil, "num_ref_idx_default_active out of range" end pps.weightedPred = u1(r) == 1 pps.weightedBipred = u(r, 2) pps.initQp = 26 + se(r) pps.initQs = 26 + se(r) pps.chromaQpOffset = se(r) if pps.initQp < 0 or pps.initQp > 51 or pps.chromaQpOffset < -12 or pps.chromaQpOffset > 12 then return nil, "pic_init_qp / chroma_qp_index_offset out of range" end pps.deblockingControl = u1(r) == 1 pps.constrainedIntra = u1(r) == 1 pps.redundantPicCnt = u1(r) == 1 pps.transform8x8 = false pps.chromaQpOffset2 = pps.chromaQpOffset pps.scalingLists = nil if r.pos > r.size then return nil, "truncated: picture parameter set" end if Bitstream.moreData(r) then pps.transform8x8 = u1(r) == 1 if u1(r) == 1 then local n = 6 + (if pps.transform8x8 then (if sps.chromaFormat ~= 3 then 2 else 6) else 0) local lists = scalingMatrix(r, n) if not lists then return nil, "bad scaling list" end pps.scalingLists = lists end pps.chromaQpOffset2 = se(r) if pps.chromaQpOffset2 < -12 or pps.chromaQpOffset2 > 12 then return nil, "second_chroma_qp_index_offset out of range" end if r.pos > r.size then return nil, "truncated: picture parameter set" end end return pps, nil end local V4 = { { 10, 16, 13 }, { 11, 18, 14 }, { 13, 20, 16 }, { 14, 23, 18 }, { 16, 25, 20 }, { 18, 29, 23 } } local V8 = { { 20, 18, 32, 19, 25, 24 }, { 22, 19, 35, 21, 28, 26 }, { 26, 23, 42, 24, 33, 31 }, { 28, 25, 45, 26, 35, 33 }, { 32, 28, 51, 30, 40, 38 }, { 36, 32, 58, 34, 46, 43 }, } local function norm4(m: number, i: number, j: number): number local v = V4[m + 1] if i % 2 == 0 and j % 2 == 0 then return v[1] elseif i % 2 == 1 and j % 2 == 1 then return v[2] end return v[3] end local function norm8(m: number, i: number, j: number): number local v = V8[m + 1] if i % 4 == 0 and j % 4 == 0 then return v[1] elseif i % 2 == 1 and j % 2 == 1 then return v[2] elseif i % 4 == 2 and j % 4 == 2 then return v[3] elseif (i % 4 == 0 and j % 2 == 1) or (i % 2 == 1 and j % 4 == 0) then return v[4] elseif (i % 4 == 0 and j % 4 == 2) or (i % 4 == 2 and j % 4 == 0) then return v[5] end return v[6] end -- dequant tables: dq.d4[list][qp + 1] = 16 multipliers (raster), list 1..6 = Y/Cb/Cr intra, Y/Cb/Cr inter; -- dq.d8[list][qp + 1] = 64 multipliers, list 1 = Y intra, 2 = Y inter. A 4x4 coefficient c dequantises to -- (c * m + 8) >> 4, an 8x8 one to (c * m + 32) >> 6 (8.5.12.1 / 8.5.13.1 with LevelScale * 2^(qP / 6)). local function build(lists: { { number } }): any local d4, d8 = {}, {} for l = 1, 6 do local w = lists[l] local per = {} for qp = 0, 51 do local m, sh = qp % 6, 2 ^ (qp // 6) local t = table.create(16, 0) for k = 0, 15 do local i, j = k % 4, k // 4 t[k + 1] = w[k + 1] * norm4(m, i, j) * sh end per[qp + 1] = t end d4[l] = per end for l = 1, 2 do local w = lists[6 + l] local per = {} for qp = 0, 51 do local m, sh = qp % 6, 2 ^ (qp // 6) local t = table.create(64, 0) for k = 0, 63 do local i, j = k % 8, k // 8 t[k + 1] = w[k + 1] * norm8(m, i, j) * sh end per[qp + 1] = t end d8[l] = per end return { d4 = d4, d8 = d8 } end function Params.dequant(sps: any, pps: any, cache: { [string]: any }): any local lists: { { number } } if pps.scalingLists then lists = resolve(pps.scalingLists, sps.scaling) elseif sps.scaling then lists = sps.scaling else lists = {} for i = 1, 12 do lists[i] = if i <= 6 then FLAT16 else FLAT64 end end local parts = {} for i = 1, 8 do parts[i] = table.concat(lists[i], ",") end local key = table.concat(parts, ";") local dq = cache[key] if not dq then dq = build(lists) cache[key] = dq end return dq end return Params