Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,17 @@ func (d *Decoder) finishPicture() []*frame.Picture {
if d.grid != nil {
pic.Motion = d.grid.motion()
}
if d.buffer != nil && d.lastHdr != nil {
d.buffer.store(pic, d.lastHdr)
var out []*frame.Picture
if d.buffer != nil && d.lastHdr != nil && d.buffer.store(pic, d.lastHdr) {
// Memory management control operation 5 ends the coded video sequence
// as far as output is concerned, as an IDR picture does (Annex C.4.4):
// the pictures before it all come out first, and its own order count
// restarts at 0, below the ones they carried.
out = d.bump(true)
pic.POC = 0
}
d.pending = append(d.pending, pic)
return d.bump(false)
return append(out, d.bump(false)...)
}

func (d *Decoder) handleUnit(ebsp []byte) ([]*frame.Picture, error) {
Expand Down Expand Up @@ -234,6 +240,7 @@ func (d *Decoder) decodeSlice(u nal.Unit) ([]*frame.Picture, error) {
if hdr.IDR {
d.buffer.refs = d.buffer.refs[:0]
}
d.buffer.fillGap(sps, hdr)
d.sps = sps
d.cur = frame.NewPicture(sps.PicWidthInMbs(), sps.FrameHeightInMbs())
d.cur.FrameNum = hdr.FrameNum
Expand Down
118 changes: 112 additions & 6 deletions internal/decoder/dpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type refFrame struct {
picNum int
longTerm bool
longTermIdx int
// nonExisting marks a frame inferred for a gap in frame_num (section
// 8.2.5.2). It holds a reference slot and a frame number but has no
// picture of its own: pic is borrowed from the nearest real reference so
// that a stream which wrongly refers to it still decodes.
nonExisting bool
}

type dpb struct {
Expand All @@ -25,6 +30,16 @@ type dpb struct {
prevPicOrderCntLsb int
prevFrameNumOffset int
prevFrameNum uint32

// prevRefFrameNum is PrevRefFrameNum of section 7.4.3: the frame_num of the
// previous reference picture, 0 after one with operation 5. It exists once
// a reference picture has been stored.
prevRefFrameNum uint32
haveRef bool

// curTop and curBottom are TopFieldOrderCnt and BottomFieldOrderCnt of the
// picture whose order count was derived last.
curTop, curBottom int
}

func newDPB(sps *syntax.SPS) *dpb {
Expand All @@ -43,7 +58,25 @@ func (b *dpb) clear() {
b.prevFrameNum = 0
}

// computePOC returns PicOrderCnt of the frame, Min(TopFieldOrderCnt,
// BottomFieldOrderCnt) (8-1), and keeps both counts for the picture so that
// operation 5 can rebase them (section 8.2.1, tempPicOrderCnt).
func (b *dpb) computePOC(sps *syntax.SPS, hdr *syntax.SliceHeader) int {
top := b.topPOC(sps, hdr)
bottom := top
switch sps.PicOrderCntType {
case 0: // (8-5) for a frame
bottom = top + int(hdr.DeltaPicOrderCntBottom)
case 1: // (8-10)
bottom = top + int(sps.OffsetForTopToBottomField) + int(hdr.DeltaPicOrderCnt[1])
}
b.curTop, b.curBottom = top, bottom
return min(top, bottom)
}

// topPOC derives TopFieldOrderCnt (8.2.1.1 to 8.2.1.3) and advances the
// previous-picture state.
func (b *dpb) topPOC(sps *syntax.SPS, hdr *syntax.SliceHeader) int {
switch sps.PicOrderCntType {
case 0:
maxLsb := int(sps.MaxPicOrderCntLsb())
Expand Down Expand Up @@ -308,30 +341,101 @@ func moveToIndex(list []*refFrame, target *refFrame, idx int) []*refFrame {
return out
}

func (b *dpb) store(pic *frame.Picture, hdr *syntax.SliceHeader) {
// store marks the decoded picture as a reference. It reports whether the
// picture carried memory_management_control_operation 5, after which the
// caller must treat everything decoded before it as outputtable and give the
// picture the picture order count 0 (H.264 section 8.2.1, "tempPicOrderCnt").
func (b *dpb) store(pic *frame.Picture, hdr *syntax.SliceHeader) (mmco5 bool) {
if hdr.NalRefIDC == 0 {
return
return false
}
b.prevRefFrameNum, b.haveRef = hdr.FrameNum, true
if hdr.IDR {
b.refs = b.refs[:0]
entry := &refFrame{pic: pic, frameNum: hdr.FrameNum, longTerm: hdr.LongTermReference}
pic.LongTerm = hdr.LongTermReference
b.refs = append(b.refs, entry)
b.updatePicNums(hdr.FrameNum)
return
return false
}
if hdr.AdaptiveRefPicMarking {
entry := &refFrame{pic: pic, frameNum: hdr.FrameNum}
b.applyMMCO(hdr, entry)
if hasMMCO5(hdr) {
// Section 7.4.3: after operation 5 the picture is inferred to have
// had frame_num 0, and section 8.2.1 restarts the order count
// from it, so the next picture's FrameNumWrap, FrameNumOffset and
// PicOrderCntMsb are all taken against 0.
entry.frameNum = 0
pic.FrameNum = 0
b.prevRefFrameNum = 0
// 8.2.1: tempPicOrderCnt = Min(top, bottom) is taken off both
// counts, and 8.2.1.1 then starts the next picture from
// PicOrderCntMsb 0 and the rebased TopFieldOrderCnt.
b.prevPicOrderCntMsb = 0
b.prevPicOrderCntLsb = b.curTop - min(b.curTop, b.curBottom)
b.prevFrameNumOffset = 0
b.prevFrameNum = 0
mmco5 = true
}
b.refs = append(b.refs, entry)
b.updatePicNums(hdr.FrameNum)
b.updatePicNums(entry.frameNum)
b.enforceCapacity()
return
return mmco5
}
b.refs = append(b.refs, &refFrame{pic: pic, frameNum: hdr.FrameNum})
b.updatePicNums(hdr.FrameNum)
b.slidingWindow()
b.enforceCapacity()
return false
}

// fillGap runs the decoding process for gaps in frame_num (section 8.2.5.2).
// When the slice's frame_num is neither PrevRefFrameNum nor the value after it,
// frames were lost or, with gaps_in_frame_num_value_allowed_flag set, left out
// on purpose (temporal scalability, sub-sequences). Each missing frame number
// becomes a "non-existing" short-term reference frame, so that the sliding
// window moves as the encoder's did and the frame numbers the slice refers to
// still resolve to the right pictures.
//
// Only the last max_num_ref_frames of them can survive the sliding window, so
// a stream cannot make this allocate more than that however wide the gap.
func (b *dpb) fillGap(sps *syntax.SPS, hdr *syntax.SliceHeader) {
if hdr.IDR || !b.haveRef || !sps.GapsInFrameNumValueAllowed || len(b.refs) == 0 {
return
}
max := b.maxFrameNum
next := (b.prevRefFrameNum + 1) % max
if hdr.FrameNum == b.prevRefFrameNum || hdr.FrameNum == next {
return
}
gap := int((hdr.FrameNum + max - next) % max)
donor := b.refs[len(b.refs)-1].pic
for i, fn := 0, next; i < gap; i, fn = i+1, (fn+1)%max {
if sps.PicOrderCntType != 0 {
// The order count of the following picture is measured from the
// last frame number seen, including these.
if fn < b.prevFrameNum {
b.prevFrameNumOffset += int(max)
}
b.prevFrameNum = fn
}
if i >= gap-b.maxNumRefs {
b.refs = append(b.refs, &refFrame{pic: donor, frameNum: fn, nonExisting: true})
b.updatePicNums(fn)
b.slidingWindow()
}
}
b.prevRefFrameNum = (hdr.FrameNum + max - 1) % max
}

func hasMMCO5(hdr *syntax.SliceHeader) bool {
for _, m := range hdr.MMCOs {
if m.Op == 5 {
return true
}
}
return false
}

func (b *dpb) enforceCapacity() {
Expand Down Expand Up @@ -362,7 +466,9 @@ func (b *dpb) applyMMCO(hdr *syntax.SliceHeader, current *refFrame) {
if r := b.shortTermByPicNum(pn); r != nil {
r.longTerm = true
r.longTermIdx = int(m.LongTermFrameIdx)
r.pic.LongTerm = true
if !r.nonExisting {
r.pic.LongTerm = true
}
}
case 4:
max := int(m.MaxLongTermFrameIdxPlus1) - 1
Expand Down
Loading
Loading