forked from asticode/go-astits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_psi.go
356 lines (311 loc) · 10.8 KB
/
data_psi.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package astits
import (
"fmt"
"github.com/asticode/go-astilog"
"github.com/pkg/errors"
)
// PSI table IDs
const (
PSITableTypeBAT = "BAT"
PSITableTypeDIT = "DIT"
PSITableTypeEIT = "EIT"
PSITableTypeNIT = "NIT"
PSITableTypeNull = "Null"
PSITableTypePAT = "PAT"
PSITableTypePMT = "PMT"
PSITableTypeRST = "RST"
PSITableTypeSDT = "SDT"
PSITableTypeSIT = "SIT"
PSITableTypeST = "ST"
PSITableTypeTDT = "TDT"
PSITableTypeTOT = "TOT"
PSITableTypeUnknown = "Unknown"
)
// PSIData represents a PSI data
// https://en.wikipedia.org/wiki/Program-specific_information
type PSIData struct {
PointerField int // Present at the start of the TS packet payload signaled by the payload_unit_start_indicator bit in the TS header. Used to set packet alignment bytes or content before the start of tabled payload data.
Sections []*PSISection
}
// PSISection represents a PSI section
type PSISection struct {
CRC32 uint32 // A checksum of the entire table excluding the pointer field, pointer filler bytes and the trailing CRC32.
Header *PSISectionHeader
Syntax *PSISectionSyntax
}
// PSISectionHeader represents a PSI section header
type PSISectionHeader struct {
PrivateBit bool // The PAT, PMT, and CAT all set this to 0. Other tables set this to 1.
SectionLength uint16 // The number of bytes that follow for the syntax section (with CRC value) and/or table data. These bytes must not exceed a value of 1021.
SectionSyntaxIndicator bool // A flag that indicates if the syntax section follows the section length. The PAT, PMT, and CAT all set this to 1.
TableID int // Table Identifier, that defines the structure of the syntax section and other contained data. As an exception, if this is the byte that immediately follow previous table section and is set to 0xFF, then it indicates that the repeat of table section end here and the rest of TS data payload shall be stuffed with 0xFF. Consequently the value 0xFF shall not be used for the Table Identifier.
TableType string
}
// PSISectionSyntax represents a PSI section syntax
type PSISectionSyntax struct {
Data *PSISectionSyntaxData
Header *PSISectionSyntaxHeader
}
// PSISectionSyntaxHeader represents a PSI section syntax header
type PSISectionSyntaxHeader struct {
CurrentNextIndicator bool // Indicates if data is current in effect or is for future use. If the bit is flagged on, then the data is to be used at the present moment.
LastSectionNumber uint8 // This indicates which table is the last table in the sequence of tables.
SectionNumber uint8 // This is an index indicating which table this is in a related sequence of tables. The first table starts from 0.
TableIDExtension uint16 // Informational only identifier. The PAT uses this for the transport stream identifier and the PMT uses this for the Program number.
VersionNumber uint8 // Syntax version number. Incremented when data is changed and wrapped around on overflow for values greater than 32.
}
// PSISectionSyntaxData represents a PSI section syntax data
type PSISectionSyntaxData struct {
EIT *EITData
NIT *NITData
PAT *PATData
PMT *PMTData
SDT *SDTData
TOT *TOTData
}
// parsePSIData parses a PSI data
func parsePSIData(i []byte) (d *PSIData, err error) {
// Init data
d = &PSIData{}
var offset int
// Pointer field
d.PointerField = int(i[offset])
offset += 1
// Pointer filler bytes
offset += d.PointerField
// Parse sections
var s *PSISection
var stop bool
for offset < len(i) && !stop {
if s, stop, err = parsePSISection(i, &offset); err != nil {
err = errors.Wrap(err, "astits: parsing PSI table failed")
return
}
d.Sections = append(d.Sections, s)
}
return
}
// parsePSISection parses a PSI section
func parsePSISection(i []byte, offset *int) (s *PSISection, stop bool, err error) {
// Init section
s = &PSISection{}
// Parse header
var offsetStart, offsetSectionsEnd, offsetEnd int
s.Header, offsetStart, _, offsetSectionsEnd, offsetEnd = parsePSISectionHeader(i, offset)
// Check whether we need to stop the parsing
if shouldStopPSIParsing(s.Header.TableType) {
stop = true
return
}
// Check whether there's a syntax section
if s.Header.SectionLength > 0 {
// Parse syntax
s.Syntax = parsePSISectionSyntax(i, offset, s.Header, offsetSectionsEnd)
// Process CRC32
if hasCRC32(s.Header.TableType) {
// Parse CRC32
s.CRC32 = parseCRC32(i[offsetSectionsEnd:offsetEnd])
*offset += 4
// Check CRC32
var c = computeCRC32(i[offsetStart:offsetSectionsEnd])
if c != s.CRC32 {
err = fmt.Errorf("astits: Table CRC32 %x != computed CRC32 %x", s.CRC32, c)
return
}
}
}
return
}
// parseCRC32 parses a CRC32
func parseCRC32(i []byte) uint32 {
return uint32(i[len(i)-4])<<24 | uint32(i[len(i)-3])<<16 | uint32(i[len(i)-2])<<8 | uint32(i[len(i)-1])
}
// computeCRC32 computes a CRC32
// https://stackoverflow.com/questions/35034042/how-to-calculate-crc32-in-psi-si-packet
func computeCRC32(i []byte) (o uint32) {
o = uint32(0xffffffff)
for _, b := range i {
for i := 0; i < 8; i++ {
if (o >= uint32(0x80000000)) != (b >= uint8(0x80)) {
o = (o << 1) ^ 0x04C11DB7
} else {
o = o << 1
}
b <<= 1
}
}
return
}
// shouldStopPSIParsing checks whether the PSI parsing should be stopped
func shouldStopPSIParsing(tableType string) bool {
return tableType == PSITableTypeNull || tableType == PSITableTypeUnknown
}
// parsePSISectionHeader parses a PSI section header
func parsePSISectionHeader(i []byte, offset *int) (h *PSISectionHeader, offsetStart, offsetSectionsStart, offsetSectionsEnd, offsetEnd int) {
// Init
h = &PSISectionHeader{}
offsetStart = *offset
// Table ID
h.TableID = int(i[*offset])
*offset += 1
// Table type
h.TableType = psiTableType(h.TableID)
// Check whether we need to stop the parsing
if shouldStopPSIParsing(h.TableType) {
return
}
// Section syntax indicator
h.SectionSyntaxIndicator = i[*offset]&0x80 > 0
// Private bit
h.PrivateBit = i[*offset]&0x40 > 0
// Section length
h.SectionLength = uint16(i[*offset]&0xf)<<8 | uint16(i[*offset+1])
*offset += 2
// Offsets
offsetSectionsStart = *offset
offsetEnd = offsetSectionsStart + int(h.SectionLength)
offsetSectionsEnd = offsetEnd
if hasCRC32(h.TableType) {
offsetSectionsEnd -= 4
}
return
}
// hasCRC32 checks whether the table has a CRC32
func hasCRC32(tableType string) bool {
return tableType == PSITableTypePAT ||
tableType == PSITableTypePMT ||
tableType == PSITableTypeEIT ||
tableType == PSITableTypeNIT ||
tableType == PSITableTypeTOT ||
tableType == PSITableTypeSDT
}
// psiTableType returns the psi table type based on the table id
// Page: 28 | https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf
func psiTableType(tableID int) string {
switch {
case tableID == 0x4a:
return PSITableTypeBAT
case tableID >= 0x4e && tableID <= 0x6f:
return PSITableTypeEIT
case tableID == 0x7e:
return PSITableTypeDIT
case tableID == 0x40, tableID == 0x41:
return PSITableTypeNIT
case tableID == 0xff:
return PSITableTypeNull
case tableID == 0:
return PSITableTypePAT
case tableID == 2:
return PSITableTypePMT
case tableID == 0x71:
return PSITableTypeRST
case tableID == 0x42, tableID == 0x46:
return PSITableTypeSDT
case tableID == 0x7f:
return PSITableTypeSIT
case tableID == 0x72:
return PSITableTypeST
case tableID == 0x70:
return PSITableTypeTDT
case tableID == 0x73:
return PSITableTypeTOT
}
// TODO Remove this log
astilog.Debugf("astits: unlisted PSI table ID %d", tableID)
return PSITableTypeUnknown
}
// parsePSISectionSyntax parses a PSI section syntax
func parsePSISectionSyntax(i []byte, offset *int, h *PSISectionHeader, offsetSectionsEnd int) (s *PSISectionSyntax) {
// Init
s = &PSISectionSyntax{}
// Header
if hasPSISyntaxHeader(h.TableType) {
s.Header = parsePSISectionSyntaxHeader(i, offset)
}
// Parse data
s.Data = parsePSISectionSyntaxData(i, offset, h, s.Header, offsetSectionsEnd)
return
}
// hasPSISyntaxHeader checks whether the section has a syntax header
func hasPSISyntaxHeader(tableType string) bool {
return tableType == PSITableTypeEIT ||
tableType == PSITableTypeNIT ||
tableType == PSITableTypePAT ||
tableType == PSITableTypePMT ||
tableType == PSITableTypeSDT
}
// parsePSISectionSyntaxHeader parses a PSI section syntax header
func parsePSISectionSyntaxHeader(i []byte, offset *int) (h *PSISectionSyntaxHeader) {
// Init
h = &PSISectionSyntaxHeader{}
// Table ID extension
h.TableIDExtension = uint16(i[*offset])<<8 | uint16(i[*offset+1])
*offset += 2
// Version number
h.VersionNumber = uint8(i[*offset]&0x3f) >> 1
// Current/Next indicator
h.CurrentNextIndicator = i[*offset]&0x1 > 0
*offset += 1
// Section number
h.SectionNumber = uint8(i[*offset])
*offset += 1
// Last section number
h.LastSectionNumber = uint8(i[*offset])
*offset += 1
return
}
// parsePSISectionSyntaxData parses a PSI section data
func parsePSISectionSyntaxData(i []byte, offset *int, h *PSISectionHeader, sh *PSISectionSyntaxHeader, offsetSectionsEnd int) (d *PSISectionSyntaxData) {
// Init
d = &PSISectionSyntaxData{}
// Switch on table type
switch h.TableType {
case PSITableTypeBAT:
// TODO Parse BAT
case PSITableTypeDIT:
// TODO Parse DIT
case PSITableTypeEIT:
d.EIT = parseEITSection(i, offset, offsetSectionsEnd, sh.TableIDExtension)
case PSITableTypeNIT:
d.NIT = parseNITSection(i, offset, sh.TableIDExtension)
case PSITableTypePAT:
d.PAT = parsePATSection(i, offset, offsetSectionsEnd, sh.TableIDExtension)
case PSITableTypePMT:
d.PMT = parsePMTSection(i, offset, offsetSectionsEnd, sh.TableIDExtension)
case PSITableTypeRST:
// TODO Parse RST
case PSITableTypeSDT:
d.SDT = parseSDTSection(i, offset, offsetSectionsEnd, sh.TableIDExtension)
case PSITableTypeSIT:
// TODO Parse SIT
case PSITableTypeST:
// TODO Parse ST
case PSITableTypeTOT:
d.TOT = parseTOTSection(i, offset)
case PSITableTypeTDT:
// TODO Parse TDT
}
return
}
// toData parses the PSI tables and returns a set of Data
func (d *PSIData) toData(firstPacket *Packet, pid uint16) (ds []*Data) {
// Loop through sections
for _, s := range d.Sections {
// Switch on table type
switch s.Header.TableType {
case PSITableTypeEIT:
ds = append(ds, &Data{EIT: s.Syntax.Data.EIT, FirstPacket: firstPacket, PID: pid})
case PSITableTypeNIT:
ds = append(ds, &Data{FirstPacket: firstPacket, NIT: s.Syntax.Data.NIT, PID: pid})
case PSITableTypePAT:
ds = append(ds, &Data{FirstPacket: firstPacket, PAT: s.Syntax.Data.PAT, PID: pid})
case PSITableTypePMT:
ds = append(ds, &Data{FirstPacket: firstPacket, PID: pid, PMT: s.Syntax.Data.PMT})
case PSITableTypeSDT:
ds = append(ds, &Data{FirstPacket: firstPacket, PID: pid, SDT: s.Syntax.Data.SDT})
case PSITableTypeTOT:
ds = append(ds, &Data{FirstPacket: firstPacket, PID: pid, TOT: s.Syntax.Data.TOT})
}
}
return
}