-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmt.go
374 lines (305 loc) · 6.5 KB
/
pmt.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package mpegts
import (
"encoding/binary"
"errors"
"fmt"
"github.com/cesbo/go-mpegts/crc32"
)
const (
PmtHeaderSize = 12
PmtMaximumSize = 1024
PmtItemSize = 5
)
// PMT is Program Map Table
type PMT struct {
header []byte
Items []*PmtItem
}
// PMT Item contains information about elementary stream
type PmtItem struct {
header []byte
}
var (
ErrPmtFormat = errors.New("pmt: invalid format")
)
var (
emptyPmt = []byte{
0x02, // table_id
0x80 | 0x30, // section_length_1
0x00, // section_length 2
0x00, // program_number 1
0x00, // program_number 2
0xC0 | 0x01, // version
0x00, // section_number
0x00, // last_section_number
0xE0, // pcr_pid 1
0x00, // pcr_pid 2
0xF0, // program_info_length 1
0x00, // program_info_length 2
}
emptyPmtItem = []byte{
0x00, // stream_type
0xE0, // elementary_pid 1
0x00, // elementary_pid 2
0xF0, // es_info_length 1
0x00, // es_info_length 2
}
)
func NewPmt() *PMT {
p := new(PMT)
p.header = make([]byte, len(emptyPmt))
copy(p.header, emptyPmt)
return p
}
func (p *PMT) ParsePmtSection(b []byte) error {
if len(b) < (PmtHeaderSize + crc32.Size) {
return ErrPmtFormat
}
next := PmtHeaderSize
end := len(b) - crc32.Size
pmtDescLen := binary.BigEndian.Uint16(b[10:]) & 0x0FFF
if pmtDescLen > 0 {
if pmtDescLen > 0x03FF {
return ErrPmtFormat
}
next += int(pmtDescLen)
if next > end {
return ErrPmtFormat
}
pmtDesc := Descriptors(b[PmtHeaderSize:next])
if err := pmtDesc.Check(); err != nil {
return fmt.Errorf("pmt: %w", err)
}
}
// copy header only from first section
if b[6] == 0 {
p.header = make([]byte, next)
copy(p.header, b)
}
skip := next
for skip < end {
next += PmtItemSize
if next > end {
return ErrPmtFormat
}
esDescLen := binary.BigEndian.Uint16(b[skip+3:]) & 0x0FFF
if esDescLen > 0 {
if esDescLen > 0x03FF {
return ErrPmtFormat
}
next += int(esDescLen)
if next > end {
return ErrPmtFormat
}
esDesc := Descriptors(b[skip+PmtItemSize : next])
if err := esDesc.Check(); err != nil {
return fmt.Errorf("pmt: %w", err)
}
}
item := new(PmtItem)
item.header = make([]byte, next-skip)
copy(item.header, b[skip:])
p.Items = append(p.Items, item)
skip = next
}
return nil
}
func (p *PMT) Version() uint8 {
return (p.header[5] & 0x3E) >> 1
}
func (p *PMT) SetVersion(version uint8) {
p.header[5] &^= 0x3E
p.header[5] |= (version << 1) & 0x3E
}
func (p *PMT) PNR() uint16 {
return binary.BigEndian.Uint16(p.header[3:])
}
func (p *PMT) SetPNR(pnr uint16) {
binary.BigEndian.PutUint16(p.header[3:], pnr)
}
func (p *PMT) PCR() PID {
return getPID(p.header[8:])
}
func (p *PMT) SetPCR(pcr PID) {
setPID(p.header[8:], pcr)
}
func (p *PMT) Descriptors() Descriptors {
return Descriptors(p.header[PmtHeaderSize:])
}
func (p *PMT) AppendDescriptors(desc Descriptors) {
if len(desc) == 0 {
return
}
ds := len(p.header) - PmtHeaderSize + len(desc)
if ds > 0x03FF {
panic("failed to append pmt descriptors: size limit")
}
p.header = append(p.header, desc...)
binary.BigEndian.PutUint16(p.header[10:], 0xF000|uint16(ds))
}
// Calculates LastSectionNumber
func (p *PMT) Finalize() {
p.header[6] = 0
p.header[7] = 0
size := len(p.header)
remain := PmtMaximumSize - size - crc32.Size
for _, item := range p.Items {
is := len(item.header)
if is > remain {
remain = PmtMaximumSize - PmtHeaderSize - crc32.Size
p.header[7] += 1
}
remain -= is
}
}
// Packetizer returns a new PsiPacketizer to get TS packets from PMT
func (p *PMT) Packetizer() *PsiPacketizer {
return newPsiPacketizer(p)
}
func (p *PMT) sectionSize(i int) int {
if i == len(p.Items) {
return 0
}
size := crc32.Size
if i == -1 {
size += len(p.header)
i = 0
} else {
size += PmtHeaderSize
}
for i < len(p.Items) {
is := len(p.Items[i].header)
if (size + is) > PmtMaximumSize {
break
} else {
size += is
i += 1
}
}
return size
}
func (p *PMT) sectionHeader(i int) []byte {
if i == -1 {
p.header[6] = 0
s := uint16(len(p.header) - PmtHeaderSize)
p.header[10] = 0xF0 | byte(s>>8)
p.header[11] = byte(s)
} else {
p.header[6] += 1
p.header[10] = 0xF0
p.header[11] = 0x00
}
return p.header[:PmtHeaderSize]
}
func (p *PMT) sectionItem(i int) []byte {
if i == -1 {
return p.header[PmtHeaderSize:]
}
if i < len(p.Items) {
return p.Items[i].header
}
return nil
}
func NewPmtItem() *PmtItem {
p := new(PmtItem)
p.header = make([]byte, len(emptyPmtItem))
copy(p.header, emptyPmtItem)
return p
}
func (p *PmtItem) Type() uint8 {
return p.header[0]
}
func (p *PmtItem) SetType(ty uint8) {
p.header[0] = ty
}
func (p *PmtItem) PID() PID {
return getPID(p.header[1:])
}
func (p *PmtItem) SetPID(pid PID) {
setPID(p.header[1:], pid)
}
func (p *PmtItem) Descriptors() Descriptors {
return Descriptors(p.header[PmtItemSize:])
}
func (p *PmtItem) AppendDescriptors(desc Descriptors) {
if len(desc) == 0 {
return
}
ds := len(p.header) - PmtItemSize + len(desc)
if ds > 0x03FF {
panic("failed to append pmt item descriptors: size limit")
}
p.header = append(p.header, desc...)
binary.BigEndian.PutUint16(p.header[3:], 0xF000|uint16(ds))
}
func (p *PmtItem) checkData05() StreamType {
d := p.Descriptors()
for len(d) != 0 {
if d[0] == 0x6F {
return StreamDataAIT
}
d = d.Next()
}
return StreamData
}
func (p *PmtItem) checkData06() StreamType {
d := p.Descriptors()
for len(d) != 0 {
switch d[0] {
case 0x56:
return StreamDataTeletext
case 0x59:
return StreamDataSubtitles
case 0x6A, 0x81:
return StreamAudioAC3
case 0x7A:
return StreamAudioEAC3
}
d = d.Next()
}
return StreamData
}
// StreamType returns stream type by element ID and related descriptors
func (p *PmtItem) StreamType() StreamType {
switch p.Type() {
// Video
case 0x01:
return StreamVideoH261
case 0x02:
return StreamVideoH262
case 0x10:
return StreamVideoH263
case 0x1B:
return StreamVideoH264
case 0x24:
return StreamVideoH265
// Audio
case 0x03:
return StreamAudioMP2
case 0x04:
return StreamAudioMP3
case 0x0F:
return StreamAudioAAC
case 0x11:
return StreamAudioLATM
case 0x81:
return StreamAudioAC3
case 0x87:
return StreamAudioEAC3
// Data
case 0x05:
return p.checkData05()
case 0x06:
return p.checkData06()
case 0x86:
return StreamDataSCTE35
default:
return StreamData
}
}
func (p *PmtItem) Clone() *PmtItem {
c := new(PmtItem)
c.header = make([]byte, len(p.header))
copy(c.header, p.header)
return c
}