-
Notifications
You must be signed in to change notification settings - Fork 51
/
data_pes.go
413 lines (360 loc) · 11.2 KB
/
data_pes.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package astits
import (
astibyte "github.com/asticode/go-astitools/byte"
"github.com/pkg/errors"
)
// P-STD buffer scales
const (
PSTDBufferScale128Bytes = 0
PSTDBufferScale1024Bytes = 1
)
// PTS DTS indicator
const (
PTSDTSIndicatorBothPresent = 3
PTSDTSIndicatorIsForbidden = 1
PTSDTSIndicatorNoPTSOrDTS = 0
PTSDTSIndicatorOnlyPTS = 2
)
// Stream IDs
const (
StreamIDPrivateStream1 = 189
StreamIDPaddingStream = 190
StreamIDPrivateStream2 = 191
)
// Trick mode controls
const (
TrickModeControlFastForward = 0
TrickModeControlFastReverse = 3
TrickModeControlFreezeFrame = 2
TrickModeControlSlowMotion = 1
TrickModeControlSlowReverse = 4
)
// PESData represents a PES data
// https://en.wikipedia.org/wiki/Packetized_elementary_stream
// http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
// http://happy.emu.id.au/lab/tut/dttb/dtbtut4b.htm
type PESData struct {
Data []byte
Header *PESHeader
}
// PESHeader represents a packet PES header
type PESHeader struct {
OptionalHeader *PESOptionalHeader
PacketLength uint16 // Specifies the number of bytes remaining in the packet after this field. Can be zero. If the PES packet length is set to zero, the PES packet can be of any length. A value of zero for the PES packet length can be used only when the PES packet payload is a video elementary stream.
StreamID uint8 // Examples: Audio streams (0xC0-0xDF), Video streams (0xE0-0xEF)
}
// PESOptionalHeader represents a PES optional header
type PESOptionalHeader struct {
AdditionalCopyInfo uint8
CRC uint16
DataAlignmentIndicator bool // True indicates that the PES packet header is immediately followed by the video start code or audio syncword
DSMTrickMode *DSMTrickMode
DTS *ClockReference
ESCR *ClockReference
ESRate uint32
Extension2Data []byte
Extension2Length uint8
HasAdditionalCopyInfo bool
HasCRC bool
HasDSMTrickMode bool
HasESCR bool
HasESRate bool
HasExtension bool
HasExtension2 bool
HasOptionalFields bool
HasPackHeaderField bool
HasPrivateData bool
HasProgramPacketSequenceCounter bool
HasPSTDBuffer bool
HeaderLength uint8
IsCopyrighted bool
IsOriginal bool
MarkerBits uint8
MPEG1OrMPEG2ID uint8
OriginalStuffingLength uint8
PacketSequenceCounter uint8
PackField uint8
Priority bool
PrivateData []byte
PSTDBufferScale uint8
PSTDBufferSize uint16
PTS *ClockReference
PTSDTSIndicator uint8
ScramblingControl uint8
}
// DSMTrickMode represents a DSM trick mode
// https://books.google.fr/books?id=vwUrAwAAQBAJ&pg=PT501&lpg=PT501&dq=dsm+trick+mode+control&source=bl&ots=fI-9IHXMRL&sig=PWnhxrsoMWNQcl1rMCPmJGNO9Ds&hl=fr&sa=X&ved=0ahUKEwjogafD8bjXAhVQ3KQKHeHKD5oQ6AEINDAB#v=onepage&q=dsm%20trick%20mode%20control&f=false
type DSMTrickMode struct {
FieldID uint8
FrequencyTruncation uint8
IntraSliceRefresh uint8
RepeatControl uint8
TrickModeControl uint8
}
// parsePESData parses a PES data
func parsePESData(i *astibyte.Iterator) (d *PESData, err error) {
// Create data
d = &PESData{}
// Skip first 3 bytes that are there to identify the PES payload
i.Seek(3)
// Parse header
var dataStart, dataEnd int
if d.Header, dataStart, dataEnd, err = parsePESHeader(i); err != nil {
err = errors.Wrap(err, "astits: parsing PES header failed")
return
}
// Seek to data
i.Seek(dataStart)
// Extract data
if d.Data, err = i.NextBytes(dataEnd - dataStart); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
return
}
// hasPESOptionalHeader checks whether the data has a PES optional header
func hasPESOptionalHeader(streamID uint8) bool {
return streamID != StreamIDPaddingStream && streamID != StreamIDPrivateStream2
}
// parsePESData parses a PES header
func parsePESHeader(i *astibyte.Iterator) (h *PESHeader, dataStart, dataEnd int, err error) {
// Create header
h = &PESHeader{}
// Get next byte
var b byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
// Stream ID
h.StreamID = uint8(b)
// Get next bytes
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
// Length
h.PacketLength = uint16(bs[0])<<8 | uint16(bs[1])
// Update data end
if h.PacketLength > 0 {
dataEnd = i.Offset() + int(h.PacketLength)
} else {
dataEnd = i.Len()
}
// Optional header
if hasPESOptionalHeader(h.StreamID) {
if h.OptionalHeader, dataStart, err = parsePESOptionalHeader(i); err != nil {
err = errors.Wrap(err, "astits: parsing PES optional header failed")
return
}
} else {
dataStart = i.Offset()
}
return
}
// parsePESOptionalHeader parses a PES optional header
func parsePESOptionalHeader(i *astibyte.Iterator) (h *PESOptionalHeader, dataStart int, err error) {
// Create header
h = &PESOptionalHeader{}
// Get next byte
var b byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
// Marker bits
h.MarkerBits = uint8(b) >> 6
// Scrambling control
h.ScramblingControl = uint8(b) >> 4 & 0x3
// Priority
h.Priority = uint8(b)&0x8 > 0
// Data alignment indicator
h.DataAlignmentIndicator = uint8(b)&0x4 > 0
// Copyrighted
h.IsCopyrighted = uint(b)&0x2 > 0
// Original or copy
h.IsOriginal = uint8(b)&0x1 > 0
// Get next byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
// PTS DST indicator
h.PTSDTSIndicator = uint8(b) >> 6 & 0x3
// Flags
h.HasESCR = uint8(b)&0x20 > 0
h.HasESRate = uint8(b)&0x10 > 0
h.HasDSMTrickMode = uint8(b)&0x8 > 0
h.HasAdditionalCopyInfo = uint8(b)&0x4 > 0
h.HasCRC = uint8(b)&0x2 > 0
h.HasExtension = uint8(b)&0x1 > 0
// Get next byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
// Header length
h.HeaderLength = uint8(b)
// Update data start
dataStart = i.Offset() + int(h.HeaderLength)
// PTS/DTS
if h.PTSDTSIndicator == PTSDTSIndicatorOnlyPTS {
if h.PTS, err = parsePTSOrDTS(i); err != nil {
err = errors.Wrap(err, "astits: parsing PTS failed")
return
}
} else if h.PTSDTSIndicator == PTSDTSIndicatorBothPresent {
if h.PTS, err = parsePTSOrDTS(i); err != nil {
err = errors.Wrap(err, "astits: parsing PTS failed")
return
}
if h.DTS, err = parsePTSOrDTS(i); err != nil {
err = errors.Wrap(err, "astits: parsing PTS failed")
return
}
}
// ESCR
if h.HasESCR {
if h.ESCR, err = parseESCR(i); err != nil {
err = errors.Wrap(err, "astits: parsing ESCR failed")
return
}
}
// ES rate
if h.HasESRate {
var bs []byte
if bs, err = i.NextBytes(3); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
h.ESRate = uint32(bs[0])&0x7f<<15 | uint32(bs[1])<<7 | uint32(bs[2])>>1
}
// Trick mode
if h.HasDSMTrickMode {
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
h.DSMTrickMode = parseDSMTrickMode(b)
}
// Additional copy info
if h.HasAdditionalCopyInfo {
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
h.AdditionalCopyInfo = b & 0x7f
}
// CRC
if h.HasCRC {
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
h.CRC = uint16(bs[0])>>8 | uint16(bs[1])
}
// Extension
if h.HasExtension {
// Get next byte
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
// Flags
h.HasPrivateData = b&0x80 > 0
h.HasPackHeaderField = b&0x40 > 0
h.HasProgramPacketSequenceCounter = b&0x20 > 0
h.HasPSTDBuffer = b&0x10 > 0
h.HasExtension2 = b&0x1 > 0
// Private data
if h.HasPrivateData {
if h.PrivateData, err = i.NextBytes(16); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
}
// Pack field length
if h.HasPackHeaderField {
if b, err = i.NextByte(); err != nil {
err = errors.Wrap(err, "astits: fetching next byte failed")
return
}
h.PackField = uint8(b)
}
// Program packet sequence counter
if h.HasProgramPacketSequenceCounter {
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
h.PacketSequenceCounter = uint8(bs[0]) & 0x7f
h.MPEG1OrMPEG2ID = uint8(bs[1]) >> 6 & 0x1
h.OriginalStuffingLength = uint8(bs[1]) & 0x3f
}
// P-STD buffer
if h.HasPSTDBuffer {
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
h.PSTDBufferScale = bs[0] >> 5 & 0x1
h.PSTDBufferSize = uint16(bs[0])&0x1f<<8 | uint16(bs[1])
}
// Extension 2
if h.HasExtension2 {
// Length
var bs []byte
if bs, err = i.NextBytes(2); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
h.Extension2Length = uint8(bs[0]) & 0x7f
// Data
if h.Extension2Data, err = i.NextBytes(int(h.Extension2Length)); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
}
}
return
}
// parseDSMTrickMode parses a DSM trick mode
func parseDSMTrickMode(i byte) (m *DSMTrickMode) {
m = &DSMTrickMode{}
m.TrickModeControl = i >> 5
if m.TrickModeControl == TrickModeControlFastForward || m.TrickModeControl == TrickModeControlFastReverse {
m.FieldID = i >> 3 & 0x3
m.IntraSliceRefresh = i >> 2 & 0x1
m.FrequencyTruncation = i & 0x3
} else if m.TrickModeControl == TrickModeControlFreezeFrame {
m.FieldID = i >> 3 & 0x3
} else if m.TrickModeControl == TrickModeControlSlowMotion || m.TrickModeControl == TrickModeControlSlowReverse {
m.RepeatControl = i & 0x1f
}
return
}
// parsePTSOrDTS parses a PTS or a DTS
func parsePTSOrDTS(i *astibyte.Iterator) (cr *ClockReference, err error) {
var bs []byte
if bs, err = i.NextBytes(5); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
cr = newClockReference(int(uint64(bs[0])>>1&0x7<<30|uint64(bs[1])<<22|uint64(bs[2])>>1&0x7f<<15|uint64(bs[3])<<7|uint64(bs[4])>>1&0x7f), 0)
return
}
// parseESCR parses an ESCR
func parseESCR(i *astibyte.Iterator) (cr *ClockReference, err error) {
var bs []byte
if bs, err = i.NextBytes(6); err != nil {
err = errors.Wrap(err, "astits: fetching next bytes failed")
return
}
escr := uint64(bs[0])>>3&0x7<<39 | uint64(bs[0])&0x3<<37 | uint64(bs[1])<<29 | uint64(bs[2])>>3<<24 | uint64(bs[2])&0x3<<22 | uint64(bs[3])<<14 | uint64(bs[4])>>3<<9 | uint64(bs[4])&0x3<<7 | uint64(bs[5])>>1
cr = newClockReference(int(escr>>9), int(escr&0x1ff))
return
}