Skip to content

Commit d76011b

Browse files
committed
test: more tests for hevc
1 parent 6832a65 commit d76011b

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

hevc/hevc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ func FindNaluTypesUpToFirstVideoNalu(sample []byte) []NaluType {
133133
func ContainsNaluType(sample []byte, specificNaluType NaluType) bool {
134134
var pos uint32 = 0
135135
length := len(sample)
136+
if length < 4 {
137+
return false
138+
}
136139
for pos < uint32(length-4) {
137140
naluLength := binary.BigEndian.Uint32(sample[pos : pos+4])
138141
pos += 4

hevc/hevc_test.go

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package hevc
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/go-test/deep"
78
)
89

910
func TestGetNaluTypes(t *testing.T) {
1011
testCases := []struct {
11-
name string
12-
input []byte
13-
wanted []NaluType
12+
name string
13+
input []byte
14+
wanted []NaluType
15+
nalusUpToFirstVideo []NaluType
16+
containsVPS bool
17+
isRapSample bool
18+
isIDRSample bool
1419
}{
1520
{
1621
"AUD",
1722
[]byte{0, 0, 0, 2, 70, 0},
1823
[]NaluType{NALU_AUD},
24+
[]NaluType{NALU_AUD},
25+
false,
26+
false,
27+
false,
1928
},
2029
{
2130
"AUD, VPS, SPS, PPS, and IDR ",
@@ -26,14 +35,45 @@ func TestGetNaluTypes(t *testing.T) {
2635
0, 0, 0, 3, 68, 3, 3,
2736
0, 0, 0, 3, 40, 4, 4},
2837
[]NaluType{NALU_AUD, NALU_VPS, NALU_SPS, NALU_PPS, NALU_IDR_N_LP},
38+
[]NaluType{NALU_AUD, NALU_VPS, NALU_SPS, NALU_PPS, NALU_IDR_N_LP},
39+
true,
40+
true,
41+
true,
42+
},
43+
{
44+
"too short",
45+
[]byte{0, 0, 0},
46+
[]NaluType{},
47+
[]NaluType{},
48+
false,
49+
false,
50+
false,
2951
},
3052
}
3153

3254
for _, tc := range testCases {
33-
got := FindNaluTypes(tc.input)
34-
if diff := deep.Equal(got, tc.wanted); diff != nil {
35-
t.Errorf("%s: %v", tc.name, diff)
36-
}
55+
t.Run(tc.name, func(t *testing.T) {
56+
got := FindNaluTypes(tc.input)
57+
if diff := deep.Equal(got, tc.wanted); diff != nil {
58+
t.Errorf("nalulist diff: %v", diff)
59+
}
60+
got = FindNaluTypesUpToFirstVideoNalu(tc.input)
61+
if diff := deep.Equal(got, tc.nalusUpToFirstVideo); diff != nil {
62+
t.Errorf("nalus before first video diff: %v", diff)
63+
}
64+
hasVPS := ContainsNaluType(tc.input, NALU_VPS)
65+
if hasVPS != tc.containsVPS {
66+
t.Errorf("got %t instead of %t", hasVPS, tc.containsVPS)
67+
}
68+
isRAP := IsRAPSample(tc.input)
69+
if isRAP != tc.isRapSample {
70+
t.Errorf("got %t instead of %t", isRAP, tc.isRapSample)
71+
}
72+
isIDR := IsIDRSample(tc.input)
73+
if isIDR != tc.isIDRSample {
74+
t.Errorf("got %t instead of %t", isIDR, tc.isIDRSample)
75+
}
76+
})
3777
}
3878
}
3979

@@ -61,10 +101,12 @@ func TestHasParameterSets(t *testing.T) {
61101
}
62102

63103
for _, tc := range testCases {
64-
got := HasParameterSets(tc.input)
65-
if got != tc.wanted {
66-
t.Errorf("%s: got %t instead of %t", tc.name, got, tc.wanted)
67-
}
104+
t.Run(tc.name, func(t *testing.T) {
105+
got := HasParameterSets(tc.input)
106+
if got != tc.wanted {
107+
t.Errorf("got %t instead of %t", got, tc.wanted)
108+
}
109+
})
68110
}
69111
}
70112

@@ -96,15 +138,30 @@ func TestGetParameterSets(t *testing.T) {
96138
}
97139

98140
for _, tc := range testCases {
99-
gotVPS, gotSPS, gotPPS := GetParameterSets(tc.input)
100-
if diff := deep.Equal(gotVPS, tc.wantedVPS); diff != nil {
101-
t.Errorf("%s VPS: %v", tc.name, diff)
102-
}
103-
if diff := deep.Equal(gotSPS, tc.wantedSPS); diff != nil {
104-
t.Errorf("%s SPS: %v", tc.name, diff)
105-
}
106-
if diff := deep.Equal(gotPPS, tc.wantedPPS); diff != nil {
107-
t.Errorf("%s PPS: %v", tc.name, diff)
141+
t.Run(tc.name, func(t *testing.T) {
142+
gotVPS, gotSPS, gotPPS := GetParameterSets(tc.input)
143+
if diff := deep.Equal(gotVPS, tc.wantedVPS); diff != nil {
144+
t.Errorf("VPS diff: %v", diff)
145+
}
146+
if diff := deep.Equal(gotSPS, tc.wantedSPS); diff != nil {
147+
t.Errorf("SPS diff: %v", diff)
148+
}
149+
if diff := deep.Equal(gotPPS, tc.wantedPPS); diff != nil {
150+
t.Errorf("PPS diff: %v", diff)
151+
}
152+
})
153+
}
154+
}
155+
156+
func TestNaluTypeStrings(t *testing.T) {
157+
named := 0
158+
for n := NaluType(0); n < NaluType(64); n++ {
159+
desc := n.String()
160+
if !strings.HasPrefix(desc, "Other") {
161+
named++
108162
}
109163
}
164+
if named != 22 {
165+
t.Errorf("got %d named instead of 22", named)
166+
}
110167
}

hevc/hevcdecoderconfigurationrecord_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hevc
22

33
import (
4+
"bytes"
45
"encoding/hex"
56
"strings"
67
"testing"
@@ -96,7 +97,6 @@ func TestDecodeConfRec(t *testing.T) {
9697
t.Error(err)
9798
}
9899
for _, na := range hdcr.NaluArrays {
99-
100100
switch na.NaluType() {
101101
case NALU_VPS, NALU_PPS:
102102
case NALU_SPS:
@@ -127,4 +127,10 @@ func TestDecodeConfRec(t *testing.T) {
127127
t.Errorf("strange nalu type %s", na.NaluType())
128128
}
129129
}
130+
131+
out := bytes.Buffer{}
132+
err = hdcr.Encode(&out)
133+
if err != nil {
134+
t.Error(err)
135+
}
130136
}

0 commit comments

Comments
 (0)