-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPsiTables.go
209 lines (180 loc) · 5.8 KB
/
PsiTables.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"errors"
"fmt"
)
// Represents a MPEG TS Program Association Table
// see ISO 13818-1 section 2.4.4.3
type Pat struct {
Tid byte `json:"table_id"`
Ssi bool `json:"section_syntax_indicator"`
Sl int `json:"section_length"`
Tsid int `json:"transport_stream_id"`
Vn int `json:"version_number"`
Cni bool `json:"current_next_indicator"`
Sn byte `json:"section_number"`
Lsn byte `json:"last_section_number"`
Pgms []pgm
Crc uint32 `json:"CRC_32"`
}
type pgm struct {
Pn int `json:"program_number"`
Pmpid int `json:"program_map_PID"`
}
// parses raw section data and returns a ptr to a Pat
// if the section is malformed, error will be set
func NewPat(data []byte) (*Pat, error) {
pat := &Pat{}
if len(data) < 8 {
return pat, errors.New("PAT data too short to parse!")
}
pat.Tid = data[0]
pat.Ssi = data[1]&128 != 0
pat.Sl = (int(data[1]&15) << 8) + int(data[2])
pat.Tsid = (int(data[3]) << 8) + int(data[4])
pat.Vn = int(data[5]&62) >> 1
pat.Cni = data[5]&1 != 0
pat.Sn = data[6]
pat.Lsn = data[7]
if len(data) < pat.Sl+3 {
return pat, errors.New("PAT data length and section_length field mismatch!")
}
pat.Pgms = make([]pgm, 0)
i := 8
for ; i < pat.Sl-4; i += 4 {
pn := (int(data[i]) << 8) + int(data[i+1])
pid := ((int(data[i+2]) & 31) << 8) + int(data[i+3])
pat.Pgms = append(pat.Pgms, pgm{pn, pid})
}
pat.Crc = uint32(data[i+3]) + uint32(data[i+2])<<8 + uint32(data[i+1])<<16 + uint32(data[i])<<24
if crc := CalculateCrc32(data[:i]); crc != pat.Crc {
return pat, errors.New(fmt.Sprintf("CRC error! Calculated %x but PAT says %x", crc, pat.Crc))
}
return pat, nil
}
// Represents a Programme Map Table
// See ISO 13818-1 table 2-28
type Pmt struct {
Tid byte `json:"table_id"`
Ssi bool `json:"section_syntax_indicator"`
Sl int `json:"section_length"`
Pn int `json:"program_number"`
Vn int `json:"version_number"`
Cni bool `json:"current_next_indicator"`
Sn byte `json:"section_number"`
Lsn byte `json:"last_section_number"`
Pcrpid int `json:"PCR_PID"`
Pil int `json:"program_info_length"`
Descriptors []descriptor
Pels []pel
Crc uint32 `json:"CRC_32"`
}
type pel struct {
St byte `json:"stream_type"`
Pid int `json:"elementary_PID"`
Eil int `json:"ES_info_length"`
Descriptors []descriptor
}
// parses raw section data and returns a ptr to a Pmt
// if the section is malformed, error will be set
func NewPmt(data []byte) (*Pmt, error) {
pmt := &Pmt{}
if len(data) < 12 {
return pmt, errors.New("PMT data too short to parse!")
}
if data[0] != 0x2 {
return pmt, errors.New(fmt.Sprintf("Invalid Table ID %x for PMT!", data[0]))
}
pmt.Tid = data[0]
pmt.Ssi = data[1]&128 != 0
pmt.Sl = (int(data[1]&15) << 8) + int(data[2])
pmt.Pn = (int(data[3]) << 8) + int(data[4])
pmt.Vn = int(data[5]&62) >> 1
pmt.Cni = data[5]&1 != 0
pmt.Sn = data[6]
pmt.Lsn = data[7]
pmt.Pcrpid = ((int(data[8]) & 31) << 8) + int(data[9])
pmt.Pil = ((int(data[10]) & 15) << 8) + int(data[11])
if len(data) < pmt.Sl+3 {
return pmt, errors.New("PMT data length and section_length field mismatch!")
}
// TODO parse the descriptors, rather than skipping
i := 12 + pmt.Pil
pmt.Pels = make([]pel, 0)
for i < pmt.Sl-4 {
st := data[i]
pid := ((int(data[i+1]) & 31) << 8) + int(data[i+2])
eil := ((int(data[i+3]) & 15) << 8) + int(data[i+4])
pmt.Pels = append(pmt.Pels, pel{st, pid, eil, nil})
// TODO don't skip the descriptors
i += 5 + eil
}
pmt.Crc = uint32(data[i+3]) + uint32(data[i+2])<<8 + uint32(data[i+1])<<16 + uint32(data[i])<<24
return pmt, nil
}
// Represents a Service Description Table
// See ETSI 300468 section 5.2.3
type Sdt struct {
Tid byte `json:"table_id"`
Ssi bool `json:"section_syntax_indicator"`
Sl int `json:"section_length"`
Tsid int `json:"transport_stream_id"`
Vn int `json:"version_number"`
Cni bool `json:"current_next_indicator"`
Sn byte `json:"section_number"`
Lsn byte `json:"last_section_number"`
Onid int `json:"original_network_id"`
Svcs []svc
Crc uint32 `json:"CRC_32"`
}
type svc struct {
Sid int `json:"service_id"`
Esf bool `json:"EIT_schedule_flag"`
Epf bool `json:"EIT_present_following_flag"`
Rs int `json:"running_status"`
Fcm bool `json:"free_CA_mode"`
Dll int `json:"descriptors_loop_length"`
Descriptors []descriptor
}
// parses raw section data and returns a ptr to an Sdt
// if the section is malformed, error will be set
func NewSdt(data []byte) (*Sdt, error) {
sdt := &Sdt{}
if len(data) < 8 {
return sdt, errors.New("SDT data too short to parse!")
}
sdt.Tid = data[0]
sdt.Ssi = data[1]&128 != 0
sdt.Sl = (int(data[1]&15) << 8) + int(data[2])
sdt.Tsid = (int(data[3]) << 8) + int(data[4])
sdt.Vn = int(data[5]&62) >> 1
sdt.Cni = data[5]&1 != 0
sdt.Sn = data[6]
sdt.Lsn = data[7]
sdt.Onid = (int(data[8]) << 8) + int(data[9])
if len(data) < sdt.Sl+3 {
return sdt, errors.New("SDT data length and section_length field mismatch!")
}
sdt.Svcs = make([]svc, 0)
i := 11
for i < sdt.Sl-4 {
svc := svc{}
svc.Sid = (int(data[i]) << 8) + int(data[i+1])
svc.Esf = data[i+2]&2 != 0
svc.Epf = data[i+2]&1 != 0
svc.Rs = int(data[i+3] >> 5)
svc.Fcm = data[i+3]&16 != 0
svc.Dll = (int(data[i+3]&15) << 8) + int(data[i+4])
if len(data) < i+5+svc.Dll {
return sdt, errors.New("SDT data length and descriptor_loop_length mismatch!")
}
svc.Descriptors = ParseDescriptors(data[i+5 : i+5+svc.Dll])
sdt.Svcs = append(sdt.Svcs, svc)
i += 5 + svc.Dll
}
sdt.Crc = uint32(data[i+3]) + uint32(data[i+2])<<8 + uint32(data[i+1])<<16 + uint32(data[i])<<24
if crc := CalculateCrc32(data[:i]); crc != sdt.Crc {
return sdt, errors.New(fmt.Sprintf("CRC error! Calculated %x but SDT says %x", crc, sdt.Crc))
}
return sdt, nil
}