Skip to content

Commit d99bf12

Browse files
committed
Fix SPS parsing in some cases
1 parent ed5581d commit d99bf12

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

pkg/bits/reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func (r *Reader) ReadUEGolomb() uint32 {
122122
// ReadSEGolomb - ReadSignedExponentialGolomb
123123
func (r *Reader) ReadSEGolomb() int32 {
124124
if b := r.ReadUEGolomb(); b%2 == 0 {
125-
return -int32(b >> 1)
125+
return -int32(b / 2)
126126
} else {
127-
return int32(b >> 1)
127+
return int32((b + 1) / 2)
128128
}
129129
}
130130

pkg/h264/h264_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/hex"
66
"testing"
77

8-
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
109
)
1110

@@ -91,5 +90,14 @@ func TestDecodeSPS2(t *testing.T) {
9190
require.Nil(t, err)
9291

9392
sps := DecodeSPS(b)
94-
assert.Nil(t, sps) // broken SPS?
93+
require.Equal(t, uint16(928), sps.Width())
94+
require.Equal(t, uint16(576), sps.Height())
95+
96+
s = "Z2QAHq2EAQwgCGEAQwgCGEAQwgCEO1BQF/yzcBAQFAAAD6AAAXcCEA==" // unknown
97+
b, err = base64.StdEncoding.DecodeString(s)
98+
require.Nil(t, err)
99+
100+
sps = DecodeSPS(b)
101+
require.Equal(t, uint16(640), sps.Width())
102+
require.Equal(t, uint16(360), sps.Height())
95103
}

pkg/h264/sps.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func (s *SPS) Height() uint16 {
8888
}
8989

9090
func DecodeSPS(sps []byte) *SPS {
91+
// https://developer.ridgerun.com/wiki/index.php/H264_Analysis_Tools
92+
// ffmpeg -i file.h264 -c copy -bsf:v trace_headers -f null -
9193
r := bits.NewReader(sps)
9294

9395
hdr := r.ReadByte()

0 commit comments

Comments
 (0)