-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsei136.go
178 lines (166 loc) · 4.32 KB
/
sei136.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
169
170
171
172
173
174
175
176
177
178
package sei
import (
"bytes"
"fmt"
"github.com/Eyevinn/mp4ff/bits"
)
// TimeCodeSEI carries the data of an SEI 136 TimeCode message.
type TimeCodeSEI struct {
Clocks []ClockTS
}
// ClockTS carries a clock time stamp.
type ClockTS struct {
TimeOffsetValue uint32
NFrames uint16
Hours byte
Minutes byte
Seconds byte
ClockTimeStampFlag bool
UnitsFieldBasedFlag bool
FullTimeStampFlag bool
SecondsFlag bool
MinutesFlag bool
HoursFlag bool
DiscontinuityFlag bool
CntDroppedFlag bool
CountingType byte
TimeOffsetLength byte
}
// String returns time stamp
func (c ClockTS) String() string {
return fmt.Sprintf("%02d:%02d:%02d:%02d offset=%d", c.Hours, c.Minutes, c.Seconds, c.NFrames, c.TimeOffsetValue)
}
// CreateClockTS creates a clock timestamp with time parts set to -1.
func CreateClockTS() ClockTS {
return ClockTS{}
}
func DecodeClockTS(br *bits.Reader) ClockTS {
c := CreateClockTS()
c.ClockTimeStampFlag = br.ReadFlag()
if c.ClockTimeStampFlag {
c.UnitsFieldBasedFlag = br.ReadFlag()
c.CountingType = byte(br.Read(5))
c.FullTimeStampFlag = br.ReadFlag()
c.DiscontinuityFlag = br.ReadFlag()
c.CntDroppedFlag = br.ReadFlag()
c.NFrames = uint16(br.Read(9))
if c.FullTimeStampFlag {
c.Seconds = byte(br.Read(6))
c.Minutes = byte(br.Read(6))
c.Hours = byte(br.Read(5))
} else {
c.SecondsFlag = br.ReadFlag()
if c.SecondsFlag {
c.Seconds = byte(br.Read(6))
c.MinutesFlag = br.ReadFlag()
if c.MinutesFlag {
c.Minutes = byte(br.Read(6))
c.HoursFlag = br.ReadFlag()
if c.HoursFlag {
c.Hours = byte(br.Read(5))
}
}
}
}
c.TimeOffsetLength = byte(br.Read(5))
if c.TimeOffsetLength > 0 {
c.TimeOffsetValue = uint32(br.Read(int(c.TimeOffsetLength)))
}
}
return c
}
// DecodeTimeCodeSEI decodes SEI message 136 TimeCode.
func DecodeTimeCodeSEI(sd *SEIData) (SEIMessage, error) {
buf := bytes.NewBuffer(sd.Payload())
br := bits.NewReader(buf)
numClockTS := int(br.Read(2))
tc := TimeCodeSEI{make([]ClockTS, 0, numClockTS)}
for i := 0; i < numClockTS; i++ {
c := DecodeClockTS(br)
tc.Clocks = append(tc.Clocks, c)
}
return &tc, br.AccError()
}
// Type returns the SEI payload type.
func (s *TimeCodeSEI) Type() uint {
return SEITimeCodeType
}
// Payload returns the SEI raw rbsp payload.
func (s *TimeCodeSEI) Payload() []byte {
sw := bits.NewFixedSliceWriter(int(s.Size()))
sw.WriteBits(uint(len(s.Clocks)), 2)
for _, c := range s.Clocks {
sw.WriteFlag(c.ClockTimeStampFlag)
if c.ClockTimeStampFlag {
sw.WriteFlag(c.UnitsFieldBasedFlag)
sw.WriteBits(uint(c.CountingType), 5)
sw.WriteFlag(c.FullTimeStampFlag)
sw.WriteFlag(c.DiscontinuityFlag)
sw.WriteFlag(c.CntDroppedFlag)
sw.WriteBits(uint(c.NFrames), 9)
if c.FullTimeStampFlag {
sw.WriteBits(uint(c.Seconds), 6)
sw.WriteBits(uint(c.Minutes), 6)
sw.WriteBits(uint(c.Hours), 5)
} else {
sw.WriteFlag(c.SecondsFlag)
if c.SecondsFlag {
sw.WriteBits(uint(c.Seconds), 6)
sw.WriteFlag(c.MinutesFlag)
if c.MinutesFlag {
sw.WriteBits(uint(c.Minutes), 6)
sw.WriteFlag(c.HoursFlag)
if c.HoursFlag {
sw.WriteBits(uint(c.Hours), 5)
}
}
}
}
sw.WriteBits(uint(c.TimeOffsetLength), 5)
if c.TimeOffsetLength > 0 {
sw.WriteBits(uint(c.TimeOffsetValue), int(c.TimeOffsetLength))
}
}
}
sw.WriteFlag(true) // Final 1 and then byte align
sw.FlushBits()
return sw.Bytes()
}
// String returns string representation of TimeCodeSEI.
func (s *TimeCodeSEI) String() string {
msgType := SEIType(s.Type())
msg := fmt.Sprintf("%s, size=%d, time=%s", msgType, s.Size(), s.Clocks[0].String())
if len(s.Clocks) > 1 {
for i := 1; i < len(s.Clocks); i++ {
msg += fmt.Sprintf(", time=%s", s.Clocks[i].String())
}
}
return msg
}
// Size is size in bytes of raw SEI message rbsp payload.
func (s *TimeCodeSEI) Size() uint {
nrBits := 2
for _, c := range s.Clocks {
nrBits++
if c.ClockTimeStampFlag {
nrBits += 18
if c.FullTimeStampFlag {
nrBits += 17
} else {
nrBits++
if c.SecondsFlag {
nrBits += 6 + 1
if c.MinutesFlag {
nrBits += 6 + 1
if c.HoursFlag {
nrBits += 5
}
}
}
}
nrBits += 5
nrBits += int(c.TimeOffsetLength)
}
}
return uint((nrBits + 7) / 8)
}