-
Notifications
You must be signed in to change notification settings - Fork 84
/
slice_test.go
168 lines (158 loc) · 4.11 KB
/
slice_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package avc
import (
"encoding/hex"
"os"
"testing"
"github.com/go-test/deep"
)
const videoNaluStart = "25888040ffde08e47a7bff05ab"
func TestSliceTypeParser(t *testing.T) {
byteData, _ := hex.DecodeString(videoNaluStart)
want := SLICE_I
got, err := GetSliceTypeFromNALU(byteData)
if err != nil {
t.Error(err)
}
if got != want {
t.Errorf("got %s want %s", got, want)
}
}
func TestSliceTypeStrings(t *testing.T) {
cases := []struct {
sliceType SliceType
want string
}{
{SLICE_P, "P"},
{SLICE_B, "B"},
{SLICE_I, "I"},
{SLICE_SP, "SP"},
{SLICE_SI, "SI"},
{SliceType(12), ""},
}
for _, c := range cases {
got := c.sliceType.String()
if got != c.want {
t.Errorf("got %s want %s", got, c.want)
}
}
}
func TestParseSliceHeader_BlackFrame(t *testing.T) {
wantedHdr := SliceHeader{
SliceType: 7,
SliceQPDelta: 6,
SliceAlphaC0OffsetDiv2: -3,
SliceBetaOffsetDiv2: -3,
Size: 7,
}
data, err := os.ReadFile("testdata/blackframe.264")
if err != nil {
t.Error(err)
}
nalus := ExtractNalusFromByteStream(data)
spsMap := make(map[uint32]*SPS, 1)
ppsMap := make(map[uint32]*PPS, 1)
var gotHdr *SliceHeader
for _, nalu := range nalus {
switch GetNaluType(nalu[0]) {
case NALU_SPS:
sps, err := ParseSPSNALUnit(nalu, true)
if err != nil {
t.Error(err)
}
spsMap[uint32(sps.ParameterID)] = sps
case NALU_PPS:
pps, err := ParsePPSNALUnit(nalu, spsMap)
if err != nil {
t.Error(err)
}
ppsMap[uint32(pps.PicParameterSetID)] = pps
case NALU_IDR:
gotHdr, err = ParseSliceHeader(nalu, spsMap, ppsMap)
if err != nil {
t.Error(err)
}
}
}
if diff := deep.Equal(wantedHdr, *gotHdr); diff != nil {
t.Errorf("Got slice header %+v. Diff=%v", *gotHdr, diff)
}
}
func TestParseSliceHeader_TwoFrames(t *testing.T) {
wantedIdrHdr := SliceHeader{SliceType: SLICE_I, IDRPicID: 1, SliceQPDelta: 8, Size: 5}
wantedNonIdrHdr := SliceHeader{
SliceType: SLICE_P, FrameNum: 1, ModificationOfPicNumsIDC: 3, SliceQPDelta: 13,
Size: 5, NumRefIdxActiveOverrideFlag: true, RefPicListModificationL0Flag: true,
}
data, err := os.ReadFile("testdata/two-frames.264")
if err != nil {
t.Error(err)
}
nalus, err := GetNalusFromSample(data)
if err != nil {
t.Error(err)
}
spsMap := make(map[uint32]*SPS, 1)
ppsMap := make(map[uint32]*PPS, 1)
var gotIdrHdr *SliceHeader
var gotNonIdrHdr *SliceHeader
for _, nalu := range nalus {
switch GetNaluType(nalu[0]) {
case NALU_SPS:
sps, err := ParseSPSNALUnit(nalu, true)
if err != nil {
t.Error(err)
}
spsMap[uint32(sps.ParameterID)] = sps
case NALU_PPS:
pps, err := ParsePPSNALUnit(nalu, spsMap)
if err != nil {
t.Error(err)
}
ppsMap[uint32(pps.PicParameterSetID)] = pps
case NALU_IDR:
gotIdrHdr, err = ParseSliceHeader(nalu, spsMap, ppsMap)
if err != nil {
t.Error(err)
}
case NALU_NON_IDR:
gotNonIdrHdr, err = ParseSliceHeader(nalu, spsMap, ppsMap)
if err != nil {
t.Error(err)
}
}
}
if diff := deep.Equal(wantedIdrHdr, *gotIdrHdr); diff != nil {
t.Errorf("Got IDR Slice Header: %+v\n Diff is: %v", *gotIdrHdr, diff)
}
if diff := deep.Equal(wantedNonIdrHdr, *gotNonIdrHdr); diff != nil {
t.Errorf("Got NON_IDR Slice Header: %+v\n Diff is: %v", *gotNonIdrHdr, diff)
}
}
func TestParseSliceHeaderLength(t *testing.T) {
spsHex := "6764001eacd940a02ff9610000030001000003003c8f162d96"
ppsHex := "68ebecb22c"
naluStartHex := "419a6649e10f2653022fff8700000302c8a32d32"
spsData, _ := hex.DecodeString(spsHex)
sps, err := ParseSPSNALUnit(spsData, true)
if err != nil {
t.Error(err)
}
spsMap := make(map[uint32]*SPS, 1)
spsMap[uint32(sps.ParameterID)] = sps
ppsData, _ := hex.DecodeString(ppsHex)
pps, err := ParsePPSNALUnit(ppsData, spsMap)
if err != nil {
t.Error(err)
}
ppsMap := make(map[uint32]*PPS, 1)
ppsMap[uint32(pps.PicParameterSetID)] = pps
naluStart, _ := hex.DecodeString(naluStartHex)
sh, err := ParseSliceHeader(naluStart, spsMap, ppsMap)
if err != nil {
t.Error(err)
}
wantedSliceHeaderSize := uint32(11)
if sh.Size != wantedSliceHeaderSize {
t.Errorf("got %d want %d", sh.Size, wantedSliceHeaderSize)
}
}