Skip to content

Commit ae8145f

Browse files
committed
Fix panic on AVCCToCodec #1652
1 parent 7107508 commit ae8145f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

pkg/h264/avcc.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ func AVCCToCodec(avcc []byte) *core.Codec {
8282
buf := bytes.NewBufferString("packetization-mode=1")
8383

8484
for {
85+
n := len(avcc)
86+
if n < 4 {
87+
break
88+
}
89+
8590
size := 4 + int(binary.BigEndian.Uint32(avcc))
91+
if n < size {
92+
break
93+
}
8694

8795
switch NALUType(avcc) {
8896
case NALUTypeSPS:
@@ -95,11 +103,7 @@ func AVCCToCodec(avcc []byte) *core.Codec {
95103
buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size]))
96104
}
97105

98-
if size < len(avcc) {
99-
avcc = avcc[size:]
100-
} else {
101-
break
102-
}
106+
avcc = avcc[size:]
103107
}
104108

105109
return &core.Codec{

pkg/h264/h264_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,10 @@ func TestDecodeSPS2(t *testing.T) {
101101
require.Equal(t, uint16(640), sps.Width())
102102
require.Equal(t, uint16(360), sps.Height())
103103
}
104+
105+
func TestAVCCToCodec(t *testing.T) {
106+
s := "000000196764001fac2484014016ec0440000003004000000c23c60c920000000568ee32c8b0000000d365"
107+
b, _ := hex.DecodeString(s)
108+
codec := AVCCToCodec(b)
109+
require.Equal(t, "packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6wkhAFAFuwEQAAAAwBAAAAMI8YMkg==,aO4yyLA=", codec.FmtpLine)
110+
}

0 commit comments

Comments
 (0)