h264: move SPS bitstream restriction into dedicated struct

This commit is contained in:
aler9
2022-06-22 13:58:49 +02:00
parent e220d8b482
commit 09865015c9
3 changed files with 86 additions and 65 deletions

View File

@@ -131,9 +131,11 @@ func (d *DTSExtractor) extractInner(
d.spsp = &spsp
// in case of B-frames, we have to subtract from DTS the maximum number of reordered frames
if d.spsp.VUI != nil && d.spsp.VUI.TimingInfo != nil {
d.ptsDTSOffset = time.Duration(math.Round(float64(time.Duration(d.spsp.VUI.MaxNumReorderFrames)*time.Second*
time.Duration(d.spsp.VUI.TimingInfo.NumUnitsInTick)*2) / float64(d.spsp.VUI.TimingInfo.TimeScale)))
if d.spsp.VUI != nil && d.spsp.VUI.TimingInfo != nil &&
d.spsp.VUI.BitstreamRestriction != nil {
d.ptsDTSOffset = time.Duration(math.Round(float64(
time.Duration(d.spsp.VUI.BitstreamRestriction.MaxNumReorderFrames)*time.Second*
time.Duration(d.spsp.VUI.TimingInfo.NumUnitsInTick)*2) / float64(d.spsp.VUI.TimingInfo.TimeScale)))
} else {
d.ptsDTSOffset = 0
}
@@ -170,7 +172,8 @@ func (d *DTSExtractor) extractInner(
}
// special case to eliminate errors near 0
if d.spsp.VUI != nil && d.spsp.VUI.TimingInfo != nil && pocDiff == -int32(d.spsp.VUI.MaxNumReorderFrames)*2 {
if d.spsp.VUI != nil && d.spsp.VUI.TimingInfo != nil && d.spsp.VUI.BitstreamRestriction != nil &&
pocDiff == -int32(d.spsp.VUI.BitstreamRestriction.MaxNumReorderFrames)*2 {
return pts, pocDiff, nil
}

View File

@@ -221,6 +221,57 @@ func (t *SPS_TimingInfo) unmarshal(br *bitio.Reader) error {
return nil
}
// SPS_BitstreamRestriction are bitstream restriction infos.
type SPS_BitstreamRestriction struct { //nolint:revive
MotionVectorsOverPicBoundariesFlag bool
MaxBytesPerPicDenom uint32
MaxBitsPerMbDenom uint32
Log2MaxMvLengthHorizontal uint32
Log2MaxMvLengthVertical uint32
MaxNumReorderFrames uint32
MaxDecFrameBuffering uint32
}
func (r *SPS_BitstreamRestriction) unmarshal(br *bitio.Reader) error {
var err error
r.MotionVectorsOverPicBoundariesFlag, err = readFlag(br)
if err != nil {
return err
}
r.MaxBytesPerPicDenom, err = readGolombUnsigned(br)
if err != nil {
return err
}
r.MaxBitsPerMbDenom, err = readGolombUnsigned(br)
if err != nil {
return err
}
r.Log2MaxMvLengthHorizontal, err = readGolombUnsigned(br)
if err != nil {
return err
}
r.Log2MaxMvLengthVertical, err = readGolombUnsigned(br)
if err != nil {
return err
}
r.MaxNumReorderFrames, err = readGolombUnsigned(br)
if err != nil {
return err
}
r.MaxDecFrameBuffering, err = readGolombUnsigned(br)
if err != nil {
return err
}
return nil
}
// SPS_VUI is a video usability information.
type SPS_VUI struct { //nolint:revive
AspectRatioInfoPresentFlag bool
@@ -256,18 +307,11 @@ type SPS_VUI struct { //nolint:revive
// vclHrdParametersPresentFlag == true
VclHRD *SPS_HRD
LowDelayHrdFlag bool
PicStructPresentFlag bool
BitstreamRestrictionFlag bool
LowDelayHrdFlag bool
PicStructPresentFlag bool
// BitstreamRestrictionFlag == true
MotionVectorsOverPicBoundariesFlag bool
MaxBytesPerPicDenom uint32
MaxBitsPerMbDenom uint32
Log2MaxMvLengthHorizontal uint32
Log2MaxMvLengthVertical uint32
MaxNumReorderFrames uint32
MaxDecFrameBuffering uint32
// bitstreamRestrictionFlag == true
BitstreamRestriction *SPS_BitstreamRestriction
}
func (v *SPS_VUI) unmarshal(br *bitio.Reader) error {
@@ -416,43 +460,14 @@ func (v *SPS_VUI) unmarshal(br *bitio.Reader) error {
return err
}
v.BitstreamRestrictionFlag, err = readFlag(br)
bitstreamRestrictionFlag, err := readFlag(br)
if err != nil {
return err
}
if v.BitstreamRestrictionFlag {
v.MotionVectorsOverPicBoundariesFlag, err = readFlag(br)
if err != nil {
return err
}
v.MaxBytesPerPicDenom, err = readGolombUnsigned(br)
if err != nil {
return err
}
v.MaxBitsPerMbDenom, err = readGolombUnsigned(br)
if err != nil {
return err
}
v.Log2MaxMvLengthHorizontal, err = readGolombUnsigned(br)
if err != nil {
return err
}
v.Log2MaxMvLengthVertical, err = readGolombUnsigned(br)
if err != nil {
return err
}
v.MaxNumReorderFrames, err = readGolombUnsigned(br)
if err != nil {
return err
}
v.MaxDecFrameBuffering, err = readGolombUnsigned(br)
if bitstreamRestrictionFlag {
v.BitstreamRestriction = &SPS_BitstreamRestriction{}
err := v.BitstreamRestriction.unmarshal(br)
if err != nil {
return err
}

View File

@@ -74,12 +74,13 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxNumReorderFrames: 2,
MaxDecFrameBuffering: 4,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxNumReorderFrames: 2,
MaxDecFrameBuffering: 4,
},
},
},
1280,
@@ -112,11 +113,12 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxDecFrameBuffering: 3,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxDecFrameBuffering: 3,
},
},
},
1920,
@@ -149,12 +151,13 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxNumReorderFrames: 2,
MaxDecFrameBuffering: 4,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxNumReorderFrames: 2,
MaxDecFrameBuffering: 4,
},
},
},
1920,