diff --git a/codec/h264/h264dec/decode.go b/codec/h264/h264dec/decode.go index 5a914263..b3ec42dc 100644 --- a/codec/h264/h264dec/decode.go +++ b/codec/h264/h264dec/decode.go @@ -26,13 +26,14 @@ package h264dec // picOrderCntType0 is used to return topFieldOrderCnt and bottomFieldOrderCnt // when pic_order_cnt_type i.e vid.PicOrderCntType == 0, using the process -// defined in section 8.2.1.1 of the specifications. -func picOrderCntType0(vid VideoStream, ctx SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) { +// defined in section 8.2.1.1 of the specifications. If topFieldOrderCnt or +// bottomFieldOrderCnt are -1 they are unset. +func picOrderCntType0(vid *VideoStream, ctx SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) { prevPicOrderCntMsb, prevPicOrderCntLsb := 0, 0 topFieldOrderCnt, bottomFieldOrderCnt = -1, -1 // NB: We're currently only handling IDRs so panic. - if !vid.currPic.isIDR { + if !vid.idrPicFlag { panic("not implemented") } @@ -45,11 +46,9 @@ func picOrderCntType0(vid VideoStream, ctx SliceContext) (topFieldOrderCnt, bott vid.picOrderCntMsb = prevPicOrderCntMsb } - if !vid.currPic.isBottomField { + if !ctx.BottomField { topFieldOrderCnt = vid.picOrderCntMsb + ctx.PicOrderCntLsb - } - - if !vid.currPic.isTopField { + } else { if !ctx.FieldPic { bottomFieldOrderCnt = topFieldOrderCnt + ctx.DeltaPicOrderCntBottom } else { @@ -58,3 +57,54 @@ func picOrderCntType0(vid VideoStream, ctx SliceContext) (topFieldOrderCnt, bott } return } + +// picOrderCntType1 is used to return topFieldOrderCnt and bottomFieldOrderCnt +// when vic.PicOrderCntType == 1 according to logic defined in section 8.2.1.2 +// of the specifications. If topFieldOrderCnt or bottomFieldOrderCnt are -1, +// then they are considered unset. +func picOrderCntType1(vid VideoStream, ctx SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) { + topFieldOrderCnt, bottomFieldOrderCnt = -1, -1 + + // TODO: this will be prevFrameNum when we do frames other than IDR. + _ = vid.priorPic.FrameNum + + if vid.idrPicFlag { + vid.frameNumOffset = 0 + } else { + panic("not implemented") + } + + absFrameNum := 0 + if ctx.NumRefFramesInPicOrderCntCycle != 0 { + absFrameNum = vid.frameNumOffset + ctx.FrameNum + } + + if ctx.RefIdc == 0 && absFrameNum > 0 { + absFrameNum = absFrameNum - 1 + } + + var expectedPicOrderCnt int + if absFrameNum > 0 { + picOrderCntCycleCnt := (absFrameNum - 1) / int(ctx.NumRefFramesInPicOrderCntCycle) + frameNumInPicOrderCntCycle := (absFrameNum - 1) % int(ctx.NumRefFramesInPicOrderCntCycle) + expectedPicOrderCnt = picOrderCntCycleCnt * vid.expectedDeltaPerPicOrderCntCycle + for i := 0; i <= frameNumInPicOrderCntCycle; i++ { + expectedPicOrderCnt = expectedPicOrderCnt + ctx.OffsetForRefFrameList[i] + } + } + + if ctx.RefIdc == 0 { + expectedPicOrderCnt = expectedPicOrderCnt + int(ctx.OffsetForNonRefPic) + } + + switch { + case !ctx.FieldPic: + topFieldOrderCnt = expectedPicOrderCnt + ctx.DeltaPicOrderCnt[0] + bottomFieldOrderCnt = topFieldOrderCnt + int(ctx.OffsetForTopToBottomField) + ctx.DeltaPicOrderCnt[1] + case !ctx.BottomField: + topFieldOrderCnt = expectedPicOrderCnt + ctx.DeltaPicOrderCnt[0] + default: + bottomFieldOrderCnt = expectedPicOrderCnt + int(ctx.OffsetForTopToBottomField) + ctx.DeltaPicOrderCnt[0] + } + return +} diff --git a/codec/h264/h264dec/slice.go b/codec/h264/h264dec/slice.go index 83e3b7bf..3a109a41 100644 --- a/codec/h264/h264dec/slice.go +++ b/codec/h264/h264dec/slice.go @@ -36,6 +36,7 @@ const ( ) type picture struct { + *SliceContext isIDR bool isBottomField bool isTopField bool @@ -46,13 +47,19 @@ type VideoStream struct { *PPS Slices []*SliceContext - ChromaArrayType int - currPic picture - maxPicOrderCntLsb int - picOrderCntMsb int + ChromaArrayType int + priorPic *picture + currPic *picture + maxPicOrderCntLsb int + picOrderCntMsb int + idrPicFlag bool + frameNumOffset int + expectedDeltaPerPicOrderCntCycle int } type SliceContext struct { + *SPS + *PPS *NALUnit *Slice }