forked from Comcast/gots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pesheader.go
306 lines (273 loc) · 10.8 KB
/
pesheader.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
/*
MIT License
Copyright 2016 Comcast Cable Communications Management, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package pes
import (
"errors"
"fmt"
"github.com/DouglasHensley/gots"
)
// stream_id possibilities
const (
STREAM_ID_ALL_AUDIO_STREAMS uint8 = 184
STREAM_ID_ALL_VIDEO_STREAMS = 185
STREAM_ID_PROGRAM_STREAM_MAP = 188
STREAM_ID_PRIVATE_STREAM_1 = 189
STREAM_ID_PADDNG_STREAM = 190
STREAM_ID_PRIVATE_STREAM_2 = 191
STREAM_ID_ECM_STREAM = 240
STREAM_ID_EMM_STREAM = 241
STREAM_ID_DSM_CC_STREAM = 242
STREAM_ID_ISO_IEC_13552_STREAM = 243
STREAM_ID_ITU_T_H222_1_TYPE_A = 244
STREAM_ID_ITU_T_H222_1_TYPE_B = 245
STREAM_ID_ITU_T_H222_1_TYPE_C = 246
STREAM_ID_ITU_T_H222_1_TYPE_D = 247
STREAM_ID_ITU_T_H222_1_TYPE_E = 248
STREAM_ID_ANCILLARY_STREAM = 249
STREAM_ID_MPEG_4_SL_PACKETIZED_STREAM = 250
STREAM_ID_MPEG_4_FLEXMUX_STREAM = 251
STREAM_ID_METADATA_STREAM = 252
STREAM_ID_EXTENDED_STREAM_ID = 253
STREAM_ID_RESERVED = 254
STREAM_ID_PROGRAM_STREAM_DIRECTORY = 255
)
// PESHeader represents operations available on a packetized elementary stream header.
type PESHeader interface {
// HasPTS returns true if the header has a PTS time
HasPTS() bool
//PTS return the PTS time in the header
PTS() uint64
//
SetPTS(uint64)
// HasDTS returns true if the header has a DTS time
HasDTS() bool
//DTS return the DTS time in the header
DTS() uint64
// Data returns the PES data
Data() []byte
// StreamId returns the stream id
StreamId() uint8
// DataAligned returns true if the data_alignment_indicator is set
DataAligned() bool
// PacketStartCodePrefix returns the packet_start_code_prefix. Note that this is a 24 bit value.
PacketStartCodePrefix() uint32
//
Format() string
}
/*
* ============================================================
* NAME | # BITS
* ============================================================
* PACKET START CODE PREFIX (0X000001) | 24
* STREAM ID | 8
* ex: AUDIO STREAMS (0XC0-0XDF) |
* ex: VIDEO STREAMS (0XE0-0XEF) |
* PES PACKET LENGTH | 16
* (CAN BE 0; NOT SPECIFIED FOR VIDEO IN MPEGTS) |
* OPTIONAL PES HEADER | VAR
* STUFFING BYTES | VAR
* DATA |
* |
* +--- OPTIONAL PES HEADER |
* | MARKER BITS (BINARY(10) OR HEX(0X2) | 2
* | SCRAMBLING CONTROL (00 IMPLIES NOT SCRAMBLED) | 2
* | PRIORITY | 1
* | DATA ALIGNMENT INDICATOR | 1
* | 1 INDICATES THAT THE PES PACKET HEADER IS |
* | IMMEDIATELY FOLLOWED BY THE VIDEO START CODE |
* | OR AUDIO SYNCWORD |
* | COPYRIGHT (1 IMPLIES COPYRIGHTED) | 1
* | ORIGINAL OR COPY (1 IMPLIES ORIGINAL) | 1
* | PTS DTS INDICATOR (11=BOTH, 10=ONLY PTS) | 2
* | ESCR FLAG | 1
* | ES RATE FLAG | 1
* | DSM TRICK MODE FLAG | 1
* | ADDITIONAL COPY INFO FLAG | 1
* | CRC FLAG | 1
* | EXTENSION FLAG | 1
* | PES HEADER LENGTH (REMAINDER LENGTH OF HEADER) | 8
* | |
* | +--- OPTIONAL FIELDS (IF PTS_DTS_FLAG = 10) |
* | | MARKER BITS '0010' | 4
* | | PTS 32-30 | 3
* | | MARKER BIT '1' | 1
* | | PTS 29-15 | 15
* | | MARKER BIT '1' | 1
* | | PTS 14-0 | 15
* | | MARKER BIT '1' | 1
* | +--- OPTIONAL FIELDS (IF PTS_DTS_FLAG = 10) |
* | |
* | +--- OPTIONAL FIELDS (IF PTS_DTS_FLAG = 11) |
* | | MARKER BITS '0011' | 4
* | | PTS 32-30 | 3
* | | MARKER BIT '1' | 1
* | | PTS 29-15 | 15
* | | MARKER BIT '1' | 1
* | | PTS 14-0 | 15
* | | MARKER BIT '1' | 1
* | | MARKER BITS '0001' | 4
* | | DTS 32-30 | 3
* | | MARKER BIT '1' | 1
* | | DTS 29-15 | 15
* | | MARKER BIT '1' | 1
* | | DTS 14-0 | 15
* | | MARKER_BIT '1' | 1
* | +--- OPTIONAL FIELDS (IF PTS_DTS_FLAG = 10) |
* | |
* | OPTIONAL FIELDS (PRESENCE IS SET BY FLAG ABOVE) | VAR
* | STUFFING BYTES (0xFF) | VAR
* +--- OPTIONAL PES HEADER |
* ============================================================
*/
type pESHeader struct {
packetStartCodePrefix uint32
dataAlignment bool
streamId uint8
pesPacketLength uint16
ptsDtsIndicator uint8
pts uint64
dts uint64
data []byte
}
// ExtractTime extracts a PTS time
func ExtractTime(bytes []byte) uint64 {
var a, b, c, d, e uint64
a = uint64((bytes[0] >> 1) & 0x07)
b = uint64(bytes[1])
c = uint64((bytes[2] >> 1) & 0x7f)
d = uint64(bytes[3])
e = uint64((bytes[4] >> 1) & 0x7f)
return (a << 30) | (b << 22) | (c << 15) | (d << 7) | e
}
// NewPESHeader creates a new PES header with the provided bytes.
// pesBytes is the packet payload that contains the PES data
func NewPESHeader(pesBytes []byte) (PESHeader, error) {
pes := new(pESHeader)
if !CheckLength(pesBytes, "PES", 6) {
err := errors.New("Invalid length for PES header. Too short to parse")
return pes, err
}
pes.packetStartCodePrefix = uint32(pesBytes[0])<<16 | uint32(pesBytes[1])<<8 | uint32(pesBytes[2])
pes.streamId = uint8(pesBytes[3])
pes.pesPacketLength = uint16(pesBytes[4])<<8 | uint16(pesBytes[5])
pes.dataAlignment = pesBytes[6]&0x04 != 0
dataStartIndex := 6
if pes.optionalFieldsExist() {
if !CheckLength(pesBytes, "Optional Fields", 9) {
err := errors.New("Invalid length for PES header. Too short to parse")
return pes, err
}
ptsDtsIndicator := (uint8(pesBytes[7]) & 0xc0 >> 6)
pesHeaderLength := pesBytes[8]
dataStartIndex = 9 + int(pesHeaderLength)
pes.ptsDtsIndicator = ptsDtsIndicator
// Extract PTS
if ptsDtsIndicator != gots.PTS_DTS_INDICATOR_NONE {
if !CheckLength(pesBytes, "PTS", 14) {
err := errors.New("Invalid length for PES header. Too short to parse")
return pes, err
}
pes.pts = gots.ExtractTime(pesBytes[9:14])
}
// Extract DTS
if pes.ptsDtsIndicator == gots.PTS_DTS_INDICATOR_BOTH {
if !CheckLength(pesBytes, "DTS", 19) {
err := errors.New("Invalid length for PES header. Too short to parse")
return pes, err
}
pes.dts = gots.ExtractTime(pesBytes[14:19])
}
}
// Extract PES Payload
if len(pesBytes) < dataStartIndex {
err := errors.New("Invalid length for PES header. Too short to parse")
return pes, err
}
pes.data = pesBytes[dataStartIndex:]
return pes, nil
}
func (pes *pESHeader) optionalFieldsExist() bool {
if pes.streamId == STREAM_ID_PADDNG_STREAM ||
pes.streamId == STREAM_ID_PRIVATE_STREAM_2 ||
pes.streamId == STREAM_ID_ECM_STREAM ||
pes.streamId == STREAM_ID_EMM_STREAM ||
pes.streamId == STREAM_ID_DSM_CC_STREAM ||
pes.streamId == STREAM_ID_ITU_T_H222_1_TYPE_E ||
pes.streamId == STREAM_ID_PROGRAM_STREAM_DIRECTORY {
return false
}
return true
}
func (pes *pESHeader) PacketStartCodePrefix() uint32 {
return pes.packetStartCodePrefix
}
func (pes *pESHeader) StreamId() uint8 {
return pes.streamId
}
func (pes *pESHeader) PTS() uint64 {
return pes.pts
}
func (pes *pESHeader) SetPTS(pts uint64) {
pes.pts = pts
}
func (pes *pESHeader) DTS() uint64 {
return pes.dts
}
func (pes *pESHeader) Data() []byte {
return pes.data
}
func (pes *pESHeader) HasPTS() bool {
return (pes.ptsDtsIndicator & gots.PTS_DTS_INDICATOR_ONLY_PTS) != 0
}
func (pes *pESHeader) HasDTS() bool {
return pes.ptsDtsIndicator == gots.PTS_DTS_INDICATOR_BOTH
}
func (pes *pESHeader) Format() string {
var f = fmt.Sprintf(
"PES\n"+
"---\n"+
"Packet Start Code Prefix: %X \n"+
"Stream Id: %X \n"+
"PES Packet Length: %d\n",
pes.packetStartCodePrefix,
pes.streamId,
pes.pesPacketLength)
if pes.optionalFieldsExist() {
ptsDtsIndicator := pes.ptsDtsIndicator
f += fmt.Sprintf("PTS DTS Indicator: %b\n", pes.ptsDtsIndicator)
if ptsDtsIndicator == gots.PTS_DTS_INDICATOR_BOTH || ptsDtsIndicator == gots.PTS_DTS_INDICATOR_ONLY_PTS {
f += fmt.Sprintf("PTS: %d\n", pes.pts)
if ptsDtsIndicator == gots.PTS_DTS_INDICATOR_BOTH {
f += fmt.Sprintf("DTS: %d\n", pes.dts)
}
}
}
return f
}
func (pes *pESHeader) DataAligned() bool {
return pes.dataAlignment
}
// CheckLength the length of the byte array to avoid index out of bound panics
func CheckLength(byteArray []byte, name string, min int) bool {
if len(byteArray) < min {
return false
}
return true
}