Fix SPS parsing in some cases

This commit is contained in:
Alex X
2025-03-27 20:52:49 +03:00
parent ed5581d1d9
commit d99bf122ea
3 changed files with 14 additions and 4 deletions
+2 -2
View File
@@ -122,9 +122,9 @@ func (r *Reader) ReadUEGolomb() uint32 {
// ReadSEGolomb - ReadSignedExponentialGolomb // ReadSEGolomb - ReadSignedExponentialGolomb
func (r *Reader) ReadSEGolomb() int32 { func (r *Reader) ReadSEGolomb() int32 {
if b := r.ReadUEGolomb(); b%2 == 0 { if b := r.ReadUEGolomb(); b%2 == 0 {
return -int32(b >> 1) return -int32(b / 2)
} else { } else {
return int32(b >> 1) return int32((b + 1) / 2)
} }
} }
+10 -2
View File
@@ -5,7 +5,6 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -91,5 +90,14 @@ func TestDecodeSPS2(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
sps := DecodeSPS(b) sps := DecodeSPS(b)
assert.Nil(t, sps) // broken SPS? require.Equal(t, uint16(928), sps.Width())
require.Equal(t, uint16(576), sps.Height())
s = "Z2QAHq2EAQwgCGEAQwgCGEAQwgCEO1BQF/yzcBAQFAAAD6AAAXcCEA==" // unknown
b, err = base64.StdEncoding.DecodeString(s)
require.Nil(t, err)
sps = DecodeSPS(b)
require.Equal(t, uint16(640), sps.Width())
require.Equal(t, uint16(360), sps.Height())
} }
+2
View File
@@ -88,6 +88,8 @@ func (s *SPS) Height() uint16 {
} }
func DecodeSPS(sps []byte) *SPS { func DecodeSPS(sps []byte) *SPS {
// https://developer.ridgerun.com/wiki/index.php/H264_Analysis_Tools
// ffmpeg -i file.h264 -c copy -bsf:v trace_headers -f null -
r := bits.NewReader(sps) r := bits.NewReader(sps)
hdr := r.ReadByte() hdr := r.ReadByte()