--!strict -- codecs/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): NAL unit -- dispatch, picture boundaries (7.4.1.2.4), picture order count types 0 / 1 / 2 (8.2.1), frame_num gaps (8.2.5.2), -- reference picture lists with modification (8.2.4), sliding window and adaptive (MMCO 1-6) marking (8.2.5), output -- in POC order through a reorder buffer (num_reorder_frames, else the DPB size), concealment of lost macroblocks, -- deblocking and border extension. Written from the spec. -- Never throws: errors are returned (a corrupt slice is skipped and its macroblocks concealed). local Bitstream = require("@formats/codecs/H264/Bitstream") local Params = require("@formats/codecs/H264/Params") local Slice = require("@formats/codecs/H264/Slice") local Tables = require("@formats/codecs/H264/Tables") local Mb = require("@formats/codecs/H264/Mb") local Cavlc = require("@formats/codecs/H264/Cavlc") local Cabac = require("@formats/codecs/H264/Cabac") local Deblock = require("@formats/codecs/H264/Deblock") local Inter = require("@formats/codecs/H264/Inter") local Types = require("@formats/Types") local PAD, PADC = Inter.PAD, Inter.PADC local CHROMA_QP = Tables.CHROMA_QP local Decoder = {} Decoder.__index = Decoder export type Options = { maxPixels: number?, -- largest picture accepted (default 2^22: 4096 x 1024) lowDelay: boolean?, -- output pictures in decoding order without reordering (only right for streams without B frames) deblock: boolean?, -- false skips the loop filter (not conformant: faster, for previews); default true } -- new(opts) -> decoder state function Decoder.new(opts: Options?): any local o = opts or {} return setmetatable({ opts = o, maxPixels = o.maxPixels or 2 ^ 22, spsTab = {}, ppsTab = {}, dqCache = {}, cur = nil, curSh = nil, S = nil, M = Mb.new(), refs = {}, -- reference pictures (short and long term) waiting = {}, -- decoded pictures not output yet out = {}, uid = 0, prevRefFrameNum = 0, prevPocMsb = 0, prevPocLsb = 0, prevFrameNumOffset = 0, prevFrameNum = 0, maxLongTermIdx = -1, pool = {}, gray = nil, frames = 0, -- pictures decoded errors = 0, lastError = nil, fatal = nil, seqW = 0, seqH = 0, }, Decoder) end -- ------------------------------------------------------------------------------------------------ pictures local function newPicture(self: any, sps: any): any local mbW, mbH = sps.mbWidth, sps.mbHeight local pool = self.pool for i = #pool, 1, -1 do local p = pool[i] if p.mbW == mbW and p.mbH == mbH then table.remove(pool, i) return p end end local w, h = mbW * 16, mbH * 16 local ys = w + 2 * PAD local cs = w // 2 + 2 * PADC local n4 = mbW * mbH * 16 return { mbW = mbW, mbH = mbH, ys = ys, cs = cs, yo = PAD * ys + PAD, co = PADC * cs + PADC, y = buffer.create(ys * (h + 2 * PAD)), u = buffer.create(cs * (h // 2 + 2 * PADC)), v = buffer.create(cs * (h // 2 + 2 * PADC)), mbIntra = table.create(mbW * mbH, true), ref0 = table.create(n4, -1), ref1 = table.create(n4, -1), uid0 = table.create(n4, -1), uid1 = table.create(n4, -1), mv0 = table.create(n4 * 2, 0), mv1 = table.create(n4 * 2, 0), uid = 0, poc = 0, frameNum = 0, long = false, short = false, longIdx = 0, waiting = false, pts = 0, } end local function release(self: any, p: any) if not p.short and not p.long and not p.waiting and p ~= self.cur and p ~= self.gray then for _, q in self.pool do if q == p then return end end table.insert(self.pool, p) end end local function grayPicture(self: any, sps: any): any local g = self.gray if g and g.mbW == sps.mbWidth and g.mbH == sps.mbHeight then return g end g = newPicture(self, sps) buffer.fill(g.y, 0, 128) buffer.fill(g.u, 0, 128) buffer.fill(g.v, 0, 128) self.uid += 1 g.uid = self.uid self.gray = g return g end -- ------------------------------------------------------------------------------------------------ output local function emit(self: any, p: any) p.waiting = false local sps = p.sps local W, H = sps.width, sps.height local img = Types.Image.new(W, H, "yuv420p") local px = img.pixels local cx, cy = sps.cropX, sps.cropY for y = 0, H - 1 do buffer.copy(px, y * W, p.y, p.yo + (cy + y) * p.ys + cx, W) end local cw, ch = (W + 1) // 2, (H + 1) // 2 local ub, vb = W * H, W * H + cw * ch for y = 0, ch - 1 do buffer.copy(px, ub + y * cw, p.u, p.co + (cy // 2 + y) * p.cs + cx // 2, cw) buffer.copy(px, vb + y * cw, p.v, p.co + (cy // 2 + y) * p.cs + cx // 2, cw) end img.meta.source = { format = "h264", bitDepth = 8, profile = sps.profile, level = sps.level, poc = p.poc, keyframe = p.idr } if sps.sarW and sps.sarH then img.meta.sar = { sps.sarW, sps.sarH } end img.meta.yuv = { range = if sps.fullRange then "full" else "limited", matrix = if sps.matrix == 1 then "bt709" else "bt601" } table.insert(self.out, { image = img, pts = p.pts, duration = 0, poc = p.poc, keyframe = p.idr }) release(self, p) end local function bump(self: any, keep: number) local w = self.waiting while #w > keep do local best = 1 for i = 2, #w do if w[i].poc < w[best].poc then best = i end end local p = table.remove(w, best) emit(self, p) end end local function reorderDepth(self: any, sps: any): number if self.opts.lowDelay then return 0 end if sps.numReorderFrames then return sps.numReorderFrames end if sps.pocType == 2 then return 0 end return sps.maxDecFrameBuffering or sps.maxDpbFrames end -- ------------------------------------------------------------------------------------------------ reference lists local function shortRefs(self: any): { any } local t = {} for _, p in self.refs do if p.short then table.insert(t, p) end end return t end local function longRefs(self: any): { any } local t = {} for _, p in self.refs do if p.long then table.insert(t, p) end end table.sort(t, function(a, b) return a.longIdx < b.longIdx end) return t end local function picNums(self: any, sh: any) local maxFrameNum = 2 ^ sh.sps.log2MaxFrameNum for _, p in self.refs do if p.short then p.picNum = if p.frameNum > sh.frameNum then p.frameNum - maxFrameNum else p.frameNum end end end local function modify(self: any, list: { any }, ops: { number }, n: number, sh: any): boolean if #ops == 0 then return true end local maxPicNum = 2 ^ sh.sps.log2MaxFrameNum local curr = sh.frameNum local pred = curr local idx = 0 for i = 1, #ops, 2 do local idc, v = ops[i], ops[i + 1] local pic = nil if idc == 0 or idc == 1 then local absDiff = v + 1 if absDiff > maxPicNum then return false end local noWrap if idc == 0 then noWrap = pred - absDiff if noWrap < 0 then noWrap += maxPicNum end else noWrap = pred + absDiff if noWrap >= maxPicNum then noWrap -= maxPicNum end end pred = noWrap local picNum = if noWrap > curr then noWrap - maxPicNum else noWrap for _, p in self.refs do if p.short and p.picNum == picNum then pic = p break end end elseif idc == 2 then for _, p in self.refs do if p.long and p.longIdx == v then pic = p break end end else return false end if not pic then return false end if idx >= n then return false end table.insert(list, idx + 1, pic) local k = idx + 2 while k <= #list do if list[k] == pic then table.remove(list, k) else k += 1 end end idx += 1 end return true end -- buildLists(self, sh, cur) -> list0, list1 (arrays of pictures, truncated to num_ref_idx_active) local function buildLists(self: any, sh: any, cur: any): ({ any }, { any }, boolean) picNums(self, sh) local list0, list1 = {}, {} local shorts = shortRefs(self) local longs = longRefs(self) if sh.type == Slice.P then table.sort(shorts, function(a, b) return a.picNum > b.picNum end) for _, p in shorts do table.insert(list0, p) end for _, p in longs do table.insert(list0, p) end else local before, after = {}, {} for _, p in shorts do if p.poc < cur.poc then table.insert(before, p) else table.insert(after, p) end end table.sort(before, function(a, b) return a.poc > b.poc end) table.sort(after, function(a, b) return a.poc < b.poc end) for _, p in before do table.insert(list0, p) end for _, p in after do table.insert(list0, p) end for _, p in after do table.insert(list1, p) end for _, p in before do table.insert(list1, p) end for _, p in longs do table.insert(list0, p) table.insert(list1, p) end if #list1 > 1 and #list0 == #list1 then local same = true for i = 1, #list0 do if list0[i] ~= list1[i] then same = false break end end if same then list1[1], list1[2] = list1[2], list1[1] end end end local n0, n1 = sh.numRefIdxL0, sh.numRefIdxL1 for i = #list0, n0 + 1, -1 do list0[i] = nil end for i = #list1, n1 + 1, -1 do list1[i] = nil end local ok = modify(self, list0, sh.mod0 or {}, n0, sh) if sh.type == Slice.B then ok = modify(self, list1, sh.mod1 or {}, n1, sh) and ok end for i = #list0, n0 + 1, -1 do list0[i] = nil end for i = #list1, n1 + 1, -1 do list1[i] = nil end return list0, list1, ok end -- ------------------------------------------------------------------------------------------------ marking local function unmark(self: any, p: any) p.short = false p.long = false for i, q in self.refs do if q == p then table.remove(self.refs, i) break end end release(self, p) end local function slidingWindow(self: any, sps: any, frameNum: number) local maxRefs = math.max(sps.maxRefFrames, 1) local maxFrameNum = 2 ^ sps.log2MaxFrameNum while #self.refs >= maxRefs do local oldest, ow = nil, math.huge for _, p in self.refs do if p.short then local w = if p.frameNum > frameNum then p.frameNum - maxFrameNum else p.frameNum if w < ow then oldest, ow = p, w end end end if not oldest then -- only long-term references left: drop the lowest index (bad stream) oldest = self.refs[1] end unmark(self, oldest) end end local function addRef(self: any, p: any) for _, q in self.refs do if q == p then return end end table.insert(self.refs, p) end -- mark(self, cur, sh): 8.2.5 after decoding the current (reference) picture; returns true when MMCO 5 was present local function mark(self: any, cur: any, sh: any): boolean local sps = sh.sps if sh.idr then while #self.refs > 0 do unmark(self, self.refs[1]) end if sh.longTermReference then cur.long = true cur.longIdx = 0 self.maxLongTermIdx = 0 else cur.short = true self.maxLongTermIdx = -1 end addRef(self, cur) return false end local mmco5 = false local becameLong = false if sh.adaptive and sh.mmco then picNums(self, sh) local ops = sh.mmco for i = 1, #ops, 3 do local op, a, b = ops[i], ops[i + 1], ops[i + 2] if op == 1 then local picNum = sh.frameNum - a for _, p in self.refs do if p.short and p.picNum == picNum then unmark(self, p) break end end elseif op == 2 then for _, p in self.refs do if p.long and p.longIdx == a then unmark(self, p) break end end elseif op == 3 then local picNum = sh.frameNum - a for _, p in self.refs do if p.long and p.longIdx == b then unmark(self, p) break end end for _, p in self.refs do if p.short and p.picNum == picNum then p.short = false p.long = true p.longIdx = b break end end elseif op == 4 then self.maxLongTermIdx = a - 1 local drop = {} for _, p in self.refs do if p.long and p.longIdx > self.maxLongTermIdx then table.insert(drop, p) end end for _, p in drop do unmark(self, p) end elseif op == 5 then while #self.refs > 0 do unmark(self, self.refs[1]) end self.maxLongTermIdx = -1 mmco5 = true elseif op == 6 then for _, p in self.refs do if p.long and p.longIdx == b then unmark(self, p) break end end cur.long = true cur.longIdx = b becameLong = true end end else slidingWindow(self, sps, sh.frameNum) end if not becameLong then cur.short = true end -- a bad stream may leave too many references local maxRefs = math.max(sps.maxRefFrames, 1) if #self.refs >= maxRefs and not becameLong then slidingWindow(self, sps, sh.frameNum) end addRef(self, cur) return mmco5 end -- ------------------------------------------------------------------------------------------------ poc local function computePoc(self: any, sh: any): (number, number) local sps = sh.sps local maxFrameNum = 2 ^ sps.log2MaxFrameNum if sps.pocType == 0 then local prevMsb, prevLsb = self.prevPocMsb, self.prevPocLsb if sh.idr then prevMsb, prevLsb = 0, 0 end local maxLsb = 2 ^ sps.log2MaxPocLsb local lsb = sh.pocLsb local msb if lsb < prevLsb and prevLsb - lsb >= maxLsb / 2 then msb = prevMsb + maxLsb elseif lsb > prevLsb and lsb - prevLsb > maxLsb / 2 then msb = prevMsb - maxLsb else msb = prevMsb end local top = msb + lsb local bottom = top + sh.deltaPocBottom sh.pocMsb = msb return math.min(top, bottom), top end local offset if sh.idr then offset = 0 elseif self.prevFrameNum > sh.frameNum then offset = self.prevFrameNumOffset + maxFrameNum else offset = self.prevFrameNumOffset end sh.frameNumOffset = offset if sps.pocType == 1 then local n = #sps.offsetsForRefFrame local absFrameNum = if n ~= 0 then offset + sh.frameNum else 0 if sh.nalRefIdc == 0 and absFrameNum > 0 then absFrameNum -= 1 end local expected = 0 if absFrameNum > 0 then local cycle = (absFrameNum - 1) // n local inCycle = (absFrameNum - 1) % n expected = cycle * sps.expectedDeltaPerCycle for i = 1, inCycle + 1 do expected += sps.offsetsForRefFrame[i] end end if sh.nalRefIdc == 0 then expected += sps.offsetNonRef end local top = expected + sh.deltaPoc0 local bottom = top + sps.offsetTopToBottom + sh.deltaPoc1 return math.min(top, bottom), top end local t if sh.idr then t = 0 elseif sh.nalRefIdc == 0 then t = 2 * (offset + sh.frameNum) - 1 else t = 2 * (offset + sh.frameNum) end return t, t end -- ------------------------------------------------------------------------------------------------ picture decode local function setupGrids(self: any, sps: any) local mbW, mbH = sps.mbWidth, sps.mbHeight if self.S and self.S.mbW == mbW and self.S.mbH == mbH then return end local S = Mb.grids(mbW, mbH) S.mbW, S.mbH = mbW, mbH S.w4 = mbW * 4 S.cw4 = mbW * 2 S.qpcOf = table.create(52, 0) S.qpc2Of = table.create(52, 0) self.S = S end -- fill a frame_num gap with "non-existing" frames (8.2.5.2), concealed as copies of the latest reference local function fillGap(self: any, sh: any) local sps = sh.sps local maxFrameNum = 2 ^ sps.log2MaxFrameNum local fn = (self.prevRefFrameNum + 1) % maxFrameNum local guard = 0 while fn ~= sh.frameNum and guard < 32 do guard += 1 local src = nil for _, p in self.refs do if p.short and (src == nil or p.decodeOrder > src.decodeOrder) then src = p end end local p = newPicture(self, sps) if src then buffer.copy(p.y, 0, src.y) buffer.copy(p.u, 0, src.u) buffer.copy(p.v, 0, src.v) else local g = grayPicture(self, sps) buffer.copy(p.y, 0, g.y) buffer.copy(p.u, 0, g.u) buffer.copy(p.v, 0, g.v) end for i = 1, #p.mbIntra do p.mbIntra[i] = true end self.uid += 1 p.uid = self.uid p.frameNum = fn p.poc = if src then src.poc else 0 p.sps = sps p.idr = false p.decodeOrder = self.frames p.long = false slidingWindow(self, sps, fn) p.short = true addRef(self, p) self.prevRefFrameNum = fn self.prevFrameNum = fn fn = (fn + 1) % maxFrameNum end end local function startPicture(self: any, sh: any, pts: number?) local sps = sh.sps if sps.mbWidth ~= self.seqW or sps.mbHeight ~= self.seqH then -- a new sequence size: output everything, drop the references bump(self, 0) while #self.refs > 0 do unmark(self, self.refs[1]) end self.pool = {} self.seqW, self.seqH = sps.mbWidth, sps.mbHeight end setupGrids(self, sps) if not sh.idr then local maxFrameNum = 2 ^ sps.log2MaxFrameNum if sh.frameNum ~= self.prevRefFrameNum and sh.frameNum ~= (self.prevRefFrameNum + 1) % maxFrameNum then fillGap(self, sh) end end local p = newPicture(self, sps) self.uid += 1 p.uid = self.uid p.sps = sps p.frameNum = sh.frameNum p.idr = sh.idr p.refIdc = sh.nalRefIdc p.short, p.long, p.waiting = false, false, false p.poc, p.topPoc = computePoc(self, sh) p.pts = pts or self.frames p.decodeOrder = self.frames self.frames += 1 self.cur = p self.curSh = sh self.decoded = 0 self.sliceNum = 0 self.sliceParams = {} local S = self.S local st = S.sliceTab for i = 1, #st do st[i] = 0 end S.pic = p end local function conceal(self: any, p: any) local S = self.S local st = S.sliceTab local src = nil for _, q in self.refs do if q ~= p and (src == nil or q.decodeOrder > src.decodeOrder) then src = q end end local mbW = S.mbW for addr = 1, #st do if st[addr] == 0 then local mx, my = (addr - 1) % mbW, (addr - 1) // mbW for y = 0, 15 do local o = p.yo + (my * 16 + y) * p.ys + mx * 16 if src then buffer.copy(p.y, o, src.y, o, 16) else buffer.fill(p.y, o, 128, 16) end end for y = 0, 7 do local o = p.co + (my * 8 + y) * p.cs + mx * 8 if src then buffer.copy(p.u, o, src.u, o, 8) buffer.copy(p.v, o, src.v, o, 8) else buffer.fill(p.u, o, 128, 8) buffer.fill(p.v, o, 128, 8) end end p.mbIntra[addr] = true local g0 = my * 4 * S.w4 + mx * 4 + 1 for j = 0, 3 do for i = 0, 3 do local g = g0 + j * S.w4 + i p.ref0[g], p.ref1[g], p.uid0[g], p.uid1[g] = -1, -1, -1, -1 end end S.kind[addr] = 0 end end end local function finishPicture(self: any) local p = self.cur if not p then return end local sh = self.curSh local S = self.S if self.decoded < S.mbW * S.mbH then conceal(self, p) end if self.opts.deblock ~= false then Deblock.picture(S, p, self.sliceParams) end local w, h = S.mbW * 16, S.mbH * 16 Inter.extend(p.y, p.yo, p.ys, w, h, PAD) Inter.extend(p.u, p.co, p.cs, w // 2, h // 2, PADC) Inter.extend(p.v, p.co, p.cs, w // 2, h // 2, PADC) local mmco5 = false if sh.nalRefIdc ~= 0 then mmco5 = mark(self, p, sh) end if sh.idr or mmco5 then bump(self, 0) end if mmco5 then -- 8.2.1: after MMCO 5 the picture counts as frame_num 0 with its POC moved to 0 local t = p.poc p.topPoc -= t p.poc = 0 p.frameNum = 0 end -- POC / frame_num state for the next pictures if sh.nalRefIdc ~= 0 then if mmco5 then self.prevPocMsb = 0 self.prevPocLsb = p.topPoc self.prevRefFrameNum = 0 else self.prevPocMsb = sh.pocMsb or 0 self.prevPocLsb = sh.pocLsb self.prevRefFrameNum = sh.frameNum end end if mmco5 then self.prevFrameNumOffset = 0 self.prevFrameNum = 0 else self.prevFrameNumOffset = sh.frameNumOffset or 0 self.prevFrameNum = sh.frameNum end self.cur = nil self.curSh = nil p.waiting = true table.insert(self.waiting, p) bump(self, reorderDepth(self, sh.sps)) release(self, p) end local function sameNewPicture(a: any, b: any): boolean -- 7.4.1.2.4: true when slice header b starts a new primary picture after a if a.frameNum ~= b.frameNum or a.ppsId ~= b.ppsId or a.idr ~= b.idr then return true end if (a.nalRefIdc == 0) ~= (b.nalRefIdc == 0) then return true end local sps = b.sps if sps.pocType == 0 and (a.pocLsb ~= b.pocLsb or a.deltaPocBottom ~= b.deltaPocBottom) then return true end if sps.pocType == 1 and (a.deltaPoc0 ~= b.deltaPoc0 or a.deltaPoc1 ~= b.deltaPoc1) then return true end if a.idr and b.idr and a.idrPicId ~= b.idrPicId then return true end return false end local function fail(self: any, msg: string) self.errors += 1 self.lastError = msg end local function decodeSlice(self: any, sh: any, r: Bitstream.Reader) local S = self.S local cur = self.cur local sps, pps = sh.sps, sh.pps self.sliceNum += 1 S.slice = self.sliceNum self.sliceParams[self.sliceNum] = { sh.deblock, sh.alphaOffset, sh.betaOffset } S.type = sh.type S.firstMb = sh.firstMb if sh.firstMb >= S.mbW * S.mbH then fail(self, "first_mb_in_slice out of range") return end S.qp = sh.qp for q = 0, 51 do S.qpcOf[q + 1] = CHROMA_QP[math.clamp(q + pps.chromaQpOffset, 0, 51) + 1] S.qpc2Of[q + 1] = CHROMA_QP[math.clamp(q + pps.chromaQpOffset2, 0, 51) + 1] end S.dq = Params.dequant(sps, pps, self.dqCache) S.t8mode = pps.transform8x8 S.constrainedIntra = pps.constrainedIntra S.direct8x8 = sps.direct8x8 S.directSpatial = sh.directSpatial S.n0, S.n1 = sh.numRefIdxL0, sh.numRefIdxL1 S.cabacInit = sh.cabacInit local list0, list1 = {}, {} if sh.type ~= Slice.I then local ok list0, list1, ok = buildLists(self, sh, cur) if not ok then fail(self, "bad ref_pic_list_modification") end if #list0 == 0 then list0 = { grayPicture(self, sps) } end if sh.type == Slice.B and #list1 == 0 then list1 = { list0[1] } end end S.list0, S.list1 = list0, list1 local uids0, uids1 = {}, {} local uidToRef0 = {} for i, p in list0 do uids0[i] = p.uid if uidToRef0[p.uid] == nil then uidToRef0[p.uid] = i - 1 end end for i, p in list1 do uids1[i] = p.uid end S.uids0, S.uids1, S.uidToRef0 = uids0, uids1, uidToRef0 S.weightMode = sh.weightMode S.weights0, S.weights1 = sh.weights0 or {}, sh.weights1 or {} S.lumaDenom, S.chromaDenom = sh.lumaDenom or 0, sh.chromaDenom or 0 if sh.type == Slice.B then local col = list1[1] S.colPic = col S.colShort = not col.long local dsf = {} for i, p in list0 do local d = nil if not p.long then d = Mb.distScale(cur.poc, p.poc, col.poc) end dsf[i] = d or false end S.dsf = dsf if sh.weightMode == 2 then local imp = table.create(32 * 32, 32) for i0, p0 in list0 do for i1, p1 in list1 do local w1 = 32 if not p0.long and not p1.long and p1.poc ~= p0.poc then local d = Mb.distScale(cur.poc, p0.poc, p1.poc) if d and d // 4 >= -64 and d // 4 <= 128 then w1 = d // 4 end end imp[(i0 - 1) * 32 + i1] = w1 end end S.implicit = imp end end local before = 0 local st = S.sliceTab local ok, err if pps.cabac then ok, err = Cabac.slice(S, self.M, r) else ok, err = Cavlc.slice(S, self.M, r) end for i = 1, #st do if st[i] ~= 0 then before += 1 end end self.decoded = before if not ok then fail(self, err or "bad slice data") end end -- nal(self, buf, off, len, pts): one NAL unit (no start code) function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: number?) if len < 1 then return end local hdr = buffer.readu8(buf, off) if hdr >= 128 then fail(self, "forbidden_zero_bit set") return end local refIdc = bit32.rshift(hdr, 5) local t = bit32.band(hdr, 31) if t == 7 then local sps, err = Params.sps(Bitstream.reader(buf, off, len, 1)) if not sps then if err and string.sub(err, 1, 6) == "limit:" then self.fatal = err end fail(self, err or "bad sps") return end if sps.chromaFormat ~= 1 then self.unsupported = "chroma format " .. ({ [0] = "4:0:0 (monochrome)", [2] = "4:2:2", [3] = "4:4:4" })[sps.chromaFormat] elseif sps.bitDepth ~= 8 or sps.bitDepthC ~= 8 then self.unsupported = "bit depth " .. sps.bitDepth elseif not sps.frameMbsOnly then self.unsupported = "interlaced coding (PAFF / MBAFF)" end if sps.mbWidth * sps.mbHeight * 256 > self.maxPixels then self.fatal = "limit: picture of " .. sps.mbWidth * 16 .. " x " .. sps.mbHeight * 16 .. " over maxPixels" end self.spsTab[sps.id] = sps return elseif t == 8 then local pps, err = Params.pps(Bitstream.reader(buf, off, len, 1), self.spsTab) if not pps then if err and string.sub(err, 1, 12) == "unsupported:" then self.unsupported = string.sub(err, 14) end fail(self, err or "bad pps") return end self.ppsTab[pps.id] = pps return elseif t == 1 or t == 5 then local r = Bitstream.reader(buf, off, len, 1) local sh, err = Slice.header(r, t, refIdc, self.spsTab, self.ppsTab) if not sh then if err and string.sub(err, 1, 12) == "unsupported:" then self.unsupported = string.sub(err, 14) end fail(self, err or "bad slice header") return end local sps = sh.sps if sps.chromaFormat ~= 1 or sps.bitDepth ~= 8 or sps.bitDepthC ~= 8 or sps.bypass or not sps.frameMbsOnly then self.unsupported = self.unsupported or "profile features (chroma format / bit depth / lossless)" return end if sps.mbWidth * sps.mbHeight * 256 > self.maxPixels then self.fatal = "limit: picture over maxPixels" return end if sh.redundantPicCnt and sh.redundantPicCnt > 0 then return end if self.cur and (sameNewPicture(self.curSh, sh) or (sh.firstMb == 0 and self.decoded > 0)) then finishPicture(self) end if not self.cur then if not sh.idr and #self.refs == 0 and self.frames == 0 and sh.type ~= Slice.I then -- a stream starting without an intra picture: decode against gray self.lastError = "stream starts with an inter picture" end startPicture(self, sh, pts) end decodeSlice(self, sh, r) if self.decoded >= self.S.mbW * self.S.mbH then finishPicture(self) end return elseif t == 9 or t == 10 or t == 11 then finishPicture(self) return elseif t >= 2 and t <= 4 then self.unsupported = "data partitioning (Extended profile)" return end -- SEI (6), filler (12), SPS extension (13), prefix / SVC / MVC (14, 15, 20) and reserved types are ignored end -- flush(self): finish the current picture and output every waiting picture function Decoder.flush(self: any) finishPicture(self) bump(self, 0) end -- take(self) -> the frames output since the last call function Decoder.take(self: any): { any } local out = self.out self.out = {} return out end return Decoder