forked from deepch/vdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segindex.go
148 lines (139 loc) · 2.94 KB
/
segindex.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
package fmp4io
import "github.com/blackbeans/vdk/utils/bits/pio"
const SIDX = Tag(0x73696478)
type SegmentIndex struct {
FullAtom
ReferenceID uint32
TimeScale uint32
EarliestPTS uint64
FirstOffset uint64
References []SegmentReference
}
type SegmentReference struct {
ReferencesBox bool
ReferencedSize uint32
SubsegmentDuration uint32
StartsWithSAP bool
SAPType uint8
SAPDeltaTime uint32
}
func (s SegmentIndex) Tag() Tag {
return SIDX
}
func (s SegmentIndex) Len() (n int) {
n = s.FullAtom.atomLen()
n += 4
n += 4
if s.Version == 0 {
n += 4
n += 4
} else {
n += 8
n += 8
}
n += 2
n += 2
n += 12 * len(s.References)
return
}
func (s SegmentIndex) Marshal(b []byte) (n int) {
n = s.FullAtom.marshalAtom(b, SIDX)
pio.PutU32BE(b[n:], s.ReferenceID)
n += 4
pio.PutU32BE(b[n:], s.TimeScale)
n += 4
if s.Version == 0 {
pio.PutU32BE(b[n:], uint32(s.EarliestPTS))
n += 4
pio.PutU32BE(b[n:], uint32(s.FirstOffset))
n += 4
} else {
pio.PutU64BE(b[n:], s.EarliestPTS)
n += 8
pio.PutU64BE(b[n:], s.FirstOffset)
n += 8
}
n += 2
pio.PutU16BE(b[n:], uint16(len(s.References)))
n += 2
for _, ref := range s.References {
v := ref.ReferencedSize
if ref.ReferencesBox {
v |= 1 << 31
}
pio.PutU32BE(b[n:], v)
n += 4
pio.PutU32BE(b[n:], ref.SubsegmentDuration)
n += 4
v = (uint32(ref.SAPType) << 28) | ref.SAPDeltaTime
if ref.StartsWithSAP {
v |= 1 << 31
}
pio.PutU32BE(b[n:], v)
n += 4
}
pio.PutU32BE(b, uint32(n))
return
}
func (s *SegmentIndex) Unmarshal(b []byte, offset int) (n int, err error) {
n, err = s.FullAtom.unmarshalAtom(b, offset)
if err != nil {
return
}
if len(b) < n+8 {
return 0, parseErr("ReferenceID", n+offset, nil)
}
s.ReferenceID = pio.U32BE(b[n:])
n += 4
s.TimeScale = pio.U32BE(b[n:])
n += 4
if s.Version == 0 {
if len(b) < n+8 {
return 0, parseErr("EarliestPTS", n+offset, nil)
}
s.EarliestPTS = uint64(pio.U32BE(b[n:]))
n += 4
s.FirstOffset = uint64(pio.U32BE(b[n:]))
n += 4
} else {
if len(b) < n+16 {
return 0, parseErr("EarliestPTS", n+offset, nil)
}
s.EarliestPTS = pio.U64BE(b[n:])
n += 8
s.FirstOffset = pio.U64BE(b[n:])
n += 8
}
if len(b) < n+4 {
return 0, parseErr("ReferenceCount", n+offset, nil)
}
n += 2
refCount := int(pio.U16BE(b[n:]))
n += 2
if len(b) < n+(12*refCount) {
return 0, parseErr("SegmentReference", n+offset, nil)
}
s.References = make([]SegmentReference, refCount)
for i := range s.References {
ref := &s.References[i]
refSize := pio.U32BE(b[n:])
n += 4
if refSize&(1<<31) != 0 {
ref.ReferencesBox = true
}
ref.ReferencedSize = refSize &^ ((1 << 31) - 1)
ref.SubsegmentDuration = pio.U32BE(b[n:])
n += 4
sapDelta := pio.U32BE(b[:n])
n += 4
if sapDelta&(1<<31) != 0 {
ref.StartsWithSAP = true
}
ref.SAPType = uint8(0x7 & (sapDelta >> 28))
ref.SAPDeltaTime = sapDelta &^ ((1 << 28) - 1)
}
return
}
func (s SegmentIndex) Children() []Atom {
return nil
}