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,8 +131,10 @@ 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*
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
@@ -258,16 +309,9 @@ type SPS_VUI struct { //nolint:revive
LowDelayHrdFlag bool
PicStructPresentFlag bool
BitstreamRestrictionFlag 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,7 +74,7 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
@@ -82,6 +82,7 @@ func TestSPSUnmarshal(t *testing.T) {
MaxDecFrameBuffering: 4,
},
},
},
1280,
720,
30,
@@ -112,13 +113,14 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
MaxDecFrameBuffering: 3,
},
},
},
1920,
1080,
30,
@@ -149,7 +151,7 @@ func TestSPSUnmarshal(t *testing.T) {
NumUnitsInTick: 1,
TimeScale: 60,
},
BitstreamRestrictionFlag: true,
BitstreamRestriction: &SPS_BitstreamRestriction{
MotionVectorsOverPicBoundariesFlag: true,
Log2MaxMvLengthHorizontal: 11,
Log2MaxMvLengthVertical: 11,
@@ -157,6 +159,7 @@ func TestSPSUnmarshal(t *testing.T) {
MaxDecFrameBuffering: 4,
},
},
},
1920,
1080,
30,