-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_header.go
More file actions
83 lines (68 loc) · 2.28 KB
/
segment_header.go
File metadata and controls
83 lines (68 loc) · 2.28 KB
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
package dvb
import "encoding/binary"
const (
segmentHeaderSize = 6
)
// SegmentHeader represents common fields of each segment
type SegmentHeader struct {
// This field shall contain the value '0000 1111' (0x0F)
SyncByte uint8
// This field shall contain the value 0x13, as listed in table 7 (0x13)
SegmentType uint8
// The page_id identifies the subtitle service of the data contained in this subtitling_segment. Segments with a
// page_id value signalled in the subtitling descriptor as the composition page id, carry subtitling data specific for one
// subtitle service. Accordingly, segments with the page_id signalled in the subtitling descriptor as the ancillary page id,
// carry data that may be shared by multiple subtitle services.
PageID uint16
// This field shall indicate the number of bytes contained in the segment following the segment_length field
SegmentLength uint16
}
func (h SegmentHeader) MarshalSize() int {
return segmentHeaderSize
}
func (h SegmentHeader) Marshal() ([]byte, error) {
if h.SyncByte != segmentSyncByte {
return nil, ErrIncorrectSyncByte
}
buf := make([]byte, h.MarshalSize())
copy(buf[0:], []uint8{h.SyncByte, h.SegmentType})
copy(buf[2:], U16toU8LE(h.PageID))
copy(buf[4:], U16toU8LE(h.SegmentLength))
return buf, nil
}
// nolint makezero
func (h SegmentHeader) MarshalSlow() ([]byte, error) {
if h.SyncByte != segmentSyncByte {
return nil, ErrIncorrectSyncByte
}
buf := make([]byte, h.MarshalSize())
buf = append(buf, []byte{h.SyncByte, h.SegmentType}...)
buf = append(buf, U16toU8LE(h.PageID)...)
buf = append(buf, U16toU8LE(h.SegmentLength)...)
return buf, nil
}
func (h *SegmentHeader) Unmarshal(buf []byte) error {
if buf == nil {
return ErrNilBuffer
}
if len(buf) < segmentHeaderSize {
return ErrShortBuffer
}
if buf[0] != segmentSyncByte {
return ErrIncorrectSyncByte
}
h.SyncByte = buf[0]
h.SegmentType = buf[1]
h.PageID = binary.LittleEndian.Uint16(buf[2:4])
// NOTE: have an exception for one type of segment
h.SegmentLength = binary.BigEndian.Uint16(buf[4:6])
return nil
}
func createSegmentHeader(segmentType uint8, pageID uint16, segmentLength uint16) SegmentHeader {
return SegmentHeader{
SyncByte: segmentSyncByte,
SegmentType: segmentType,
PageID: pageID,
SegmentLength: segmentLength,
}
}