2019-09-08 07:22:58 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
decode.go provides slice decoding functionality.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package h264dec
|
2019-09-09 04:58:43 +03:00
|
|
|
|
2019-09-09 09:48:33 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NB: this is a placeholder.
|
|
|
|
func decode(vid *VideoStream, ctx *SliceContext) error {
|
|
|
|
var err error
|
|
|
|
vid.topFieldOrderCnt, vid.bottomFieldOrderCnt, err = decodePicOrderCnt(vid, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not derive topFieldOrderCnt and bottomFieldOrderCnt, failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// According to 8.2.1 after decoding picture.
|
|
|
|
if ctx.elements[0].MemoryManagementControlOperation == 5 {
|
|
|
|
tempPicOrderCnt := picOrderCnt(ctx)
|
|
|
|
vid.topFieldOrderCnt = vid.topFieldOrderCnt - tempPicOrderCnt
|
|
|
|
vid.bottomFieldOrderCnt = vid.bottomFieldOrderCnt - tempPicOrderCnt
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: complete this. Unsure how to determine if pic is frame or a
|
|
|
|
// complementary field pair.
|
|
|
|
// picOrderCnt as defined by section 8.2.1.
|
|
|
|
func picOrderCnt(ctx *SliceContext) int {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodePicOrderCnt derives topFieldOrderCnt and bottomFieldOrderCnt based
|
|
|
|
// on the PicOrderCntType using the process defined in section 8.2.1 of the
|
|
|
|
// specifications.
|
|
|
|
func decodePicOrderCnt(vid *VideoStream, ctx *SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int, err error) {
|
|
|
|
// There are some steps listed in 8.2.1 regarding cases when not an IDR frame,
|
|
|
|
// but we're not yet dealing with that.
|
|
|
|
// TODO: write steps dealing with not IDR frame.
|
|
|
|
if !vid.idrPicFlag {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ctx.PicOrderCountType {
|
|
|
|
case 0:
|
|
|
|
topFieldOrderCnt, bottomFieldOrderCnt = decodePicOrderCntType0(vid, ctx)
|
|
|
|
case 1:
|
|
|
|
topFieldOrderCnt, bottomFieldOrderCnt = decodePicOrderCntType1(vid, ctx)
|
|
|
|
case 2:
|
|
|
|
topFieldOrderCnt, bottomFieldOrderCnt = decodePicOrderCntType2(vid, ctx)
|
|
|
|
default:
|
|
|
|
err = errors.New("invalid PicOrderCountType")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check DiffPicOrderCnt( picA, picB ) once picOrderCnt( picX ) is
|
|
|
|
// worked out.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-09 04:58:43 +03:00
|
|
|
// picOrderCntType0 is used to return topFieldOrderCnt and bottomFieldOrderCnt
|
|
|
|
// when pic_order_cnt_type i.e vid.PicOrderCntType == 0, using the process
|
2019-09-09 06:33:11 +03:00
|
|
|
// defined in section 8.2.1.1 of the specifications. If topFieldOrderCnt or
|
|
|
|
// bottomFieldOrderCnt are -1 they are unset.
|
2019-09-09 09:48:33 +03:00
|
|
|
func decodePicOrderCntType0(vid *VideoStream, ctx *SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) {
|
2019-09-09 04:58:43 +03:00
|
|
|
prevPicOrderCntMsb, prevPicOrderCntLsb := 0, 0
|
|
|
|
topFieldOrderCnt, bottomFieldOrderCnt = -1, -1
|
|
|
|
|
|
|
|
// NB: We're currently only handling IDRs so panic.
|
2019-09-09 06:33:11 +03:00
|
|
|
if !vid.idrPicFlag {
|
2019-09-09 04:58:43 +03:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case (ctx.PicOrderCntLsb < prevPicOrderCntLsb) && ((prevPicOrderCntLsb - ctx.PicOrderCntLsb) >= (vid.maxPicOrderCntLsb / 2)):
|
|
|
|
vid.picOrderCntMsb = prevPicOrderCntMsb + vid.maxPicOrderCntLsb
|
|
|
|
case (ctx.PicOrderCntLsb > prevPicOrderCntLsb) && ((ctx.PicOrderCntLsb - prevPicOrderCntLsb) > (vid.maxPicOrderCntLsb / 2)):
|
|
|
|
vid.picOrderCntMsb = prevPicOrderCntMsb - vid.maxPicOrderCntLsb
|
|
|
|
default:
|
|
|
|
vid.picOrderCntMsb = prevPicOrderCntMsb
|
|
|
|
}
|
|
|
|
|
2019-09-09 06:33:11 +03:00
|
|
|
if !ctx.BottomField {
|
2019-09-09 04:58:43 +03:00
|
|
|
topFieldOrderCnt = vid.picOrderCntMsb + ctx.PicOrderCntLsb
|
2019-09-09 06:33:11 +03:00
|
|
|
} else {
|
2019-09-09 04:58:43 +03:00
|
|
|
if !ctx.FieldPic {
|
|
|
|
bottomFieldOrderCnt = topFieldOrderCnt + ctx.DeltaPicOrderCntBottom
|
|
|
|
} else {
|
|
|
|
bottomFieldOrderCnt = vid.picOrderCntMsb + ctx.PicOrderCntLsb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-09-09 06:33:11 +03:00
|
|
|
|
|
|
|
// 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.
|
2019-09-09 09:48:33 +03:00
|
|
|
func decodePicOrderCntType1(vid *VideoStream, ctx *SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) {
|
2019-09-09 06:33:11 +03:00
|
|
|
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
|
|
|
|
}
|
2019-09-09 06:56:46 +03:00
|
|
|
|
|
|
|
// picOrderCntType2 is used to return topFieldOrderCnt and bottomFieldOrderCnt
|
|
|
|
// when vic.PicOrderCntType == 1 according to logic defined in section 8.2.1.3
|
|
|
|
// of the specifications. If topFieldOrderCnt or bottomFieldOrderCnt are -1,
|
|
|
|
// then they are considered unset.
|
2019-09-09 09:48:33 +03:00
|
|
|
func decodePicOrderCntType2(vid *VideoStream, ctx *SliceContext) (topFieldOrderCnt, bottomFieldOrderCnt int) {
|
2019-09-09 06:56:46 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var tempPicOrderCnt int
|
|
|
|
switch {
|
|
|
|
case vid.idrPicFlag:
|
|
|
|
tempPicOrderCnt = 0
|
|
|
|
case ctx.RefIdc == 0:
|
|
|
|
tempPicOrderCnt = 2*(vid.frameNumOffset+ctx.FrameNum) - 1
|
|
|
|
default:
|
|
|
|
tempPicOrderCnt = 2 * (vid.frameNumOffset + ctx.FrameNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.FieldPic:
|
|
|
|
topFieldOrderCnt = tempPicOrderCnt
|
|
|
|
bottomFieldOrderCnt = tempPicOrderCnt
|
|
|
|
case ctx.BottomField:
|
|
|
|
bottomFieldOrderCnt = tempPicOrderCnt
|
|
|
|
default:
|
|
|
|
topFieldOrderCnt = tempPicOrderCnt
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|