-
Notifications
You must be signed in to change notification settings - Fork 51
/
data_pmt.go
259 lines (225 loc) · 7.33 KB
/
data_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
package astits
import (
"fmt"
"github.com/asticode/go-astikit"
)
type StreamType uint8
// Stream types
const (
StreamTypeMPEG1Video StreamType = 0x01
StreamTypeMPEG2Video StreamType = 0x02
StreamTypeMPEG1Audio StreamType = 0x03 // ISO/IEC 11172-3
StreamTypeMPEG2HalvedSampleRateAudio StreamType = 0x04 // ISO/IEC 13818-3
StreamTypeMPEG2Audio StreamType = 0x04
StreamTypePrivateSection StreamType = 0x05
StreamTypePrivateData StreamType = 0x06
StreamTypeMPEG2PacketizedData StreamType = 0x06 // Rec. ITU-T H.222 | ISO/IEC 13818-1 i.e., DVB subtitles/VBI and AC-3
StreamTypeADTS StreamType = 0x0F // ISO/IEC 13818-7 Audio with ADTS transport syntax
StreamTypeAACAudio StreamType = 0x0f
StreamTypeMPEG4Video StreamType = 0x10
StreamTypeAACLATMAudio StreamType = 0x11
StreamTypeMetadata StreamType = 0x15
StreamTypeH264Video StreamType = 0x1B // Rec. ITU-T H.264 | ISO/IEC 14496-10
StreamTypeH265Video StreamType = 0x24 // Rec. ITU-T H.265 | ISO/IEC 23008-2
StreamTypeHEVCVideo StreamType = 0x24
StreamTypeCAVSVideo StreamType = 0x42
StreamTypeVC1Video StreamType = 0xea
StreamTypeDIRACVideo StreamType = 0xd1
StreamTypeAC3Audio StreamType = 0x81
StreamTypeDTSAudio StreamType = 0x82
StreamTypeTRUEHDAudio StreamType = 0x83
StreamTypeSCTE35 StreamType = 0x86
StreamTypeEAC3Audio StreamType = 0x87
)
// PMTData represents a PMT data
// https://en.wikipedia.org/wiki/Program-specific_information
type PMTData struct {
ElementaryStreams []*PMTElementaryStream
PCRPID uint16 // The packet identifier that contains the program clock reference used to improve the random access accuracy of the stream's timing that is derived from the program timestamp. If this is unused. then it is set to 0x1FFF (all bits on).
ProgramDescriptors []*Descriptor // Program descriptors
ProgramNumber uint16
}
// PMTElementaryStream represents a PMT elementary stream
type PMTElementaryStream struct {
ElementaryPID uint16 // The packet identifier that contains the stream type data.
ElementaryStreamDescriptors []*Descriptor // Elementary stream descriptors
StreamType StreamType // This defines the structure of the data contained within the elementary packet identifier.
}
// parsePMTSection parses a PMT section
func parsePMTSection(i *astikit.BytesIterator, offsetSectionsEnd int, tableIDExtension uint16) (d *PMTData, err error) {
// Create data
d = &PMTData{ProgramNumber: tableIDExtension}
// Get next bytes
var bs []byte
if bs, err = i.NextBytesNoCopy(2); err != nil {
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}
// PCR PID
d.PCRPID = uint16(bs[0]&0x1f)<<8 | uint16(bs[1])
// Program descriptors
if d.ProgramDescriptors, err = parseDescriptors(i); err != nil {
err = fmt.Errorf("astits: parsing descriptors failed: %w", err)
return
}
// Loop until end of section data is reached
for i.Offset() < offsetSectionsEnd {
// Create stream
e := &PMTElementaryStream{}
// Get next byte
var b byte
if b, err = i.NextByte(); err != nil {
err = fmt.Errorf("astits: fetching next byte failed: %w", err)
return
}
// Stream type
e.StreamType = StreamType(b)
// Get next bytes
if bs, err = i.NextBytesNoCopy(2); err != nil {
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}
// Elementary PID
e.ElementaryPID = uint16(bs[0]&0x1f)<<8 | uint16(bs[1])
// Elementary descriptors
if e.ElementaryStreamDescriptors, err = parseDescriptors(i); err != nil {
err = fmt.Errorf("astits: parsing descriptors failed: %w", err)
return
}
// Add elementary stream
d.ElementaryStreams = append(d.ElementaryStreams, e)
}
return
}
func calcPMTProgramInfoLength(d *PMTData) uint16 {
ret := uint16(2) // program_info_length
ret += calcDescriptorsLength(d.ProgramDescriptors)
for _, es := range d.ElementaryStreams {
ret += 5 // stream_type, elementary_pid, es_info_length
ret += calcDescriptorsLength(es.ElementaryStreamDescriptors)
}
return ret
}
func calcPMTSectionLength(d *PMTData) uint16 {
ret := uint16(4)
ret += calcDescriptorsLength(d.ProgramDescriptors)
for _, es := range d.ElementaryStreams {
ret += 5
ret += calcDescriptorsLength(es.ElementaryStreamDescriptors)
}
return ret
}
func writePMTSection(w *astikit.BitsWriter, d *PMTData) (int, error) {
b := astikit.NewBitsWriterBatch(w)
// TODO split into sections
b.WriteN(uint8(0xff), 3)
b.WriteN(d.PCRPID, 13)
bytesWritten := 2
n, err := writeDescriptorsWithLength(w, d.ProgramDescriptors)
if err != nil {
return 0, err
}
bytesWritten += n
for _, es := range d.ElementaryStreams {
b.Write(uint8(es.StreamType))
b.WriteN(uint8(0xff), 3)
b.WriteN(es.ElementaryPID, 13)
bytesWritten += 3
n, err = writeDescriptorsWithLength(w, es.ElementaryStreamDescriptors)
if err != nil {
return 0, err
}
bytesWritten += n
}
return bytesWritten, b.Err()
}
func (t StreamType) IsVideo() bool {
switch t {
case StreamTypeMPEG1Video,
StreamTypeMPEG2Video,
StreamTypeMPEG4Video,
StreamTypeH264Video,
StreamTypeH265Video,
StreamTypeCAVSVideo,
StreamTypeVC1Video,
StreamTypeDIRACVideo:
return true
}
return false
}
func (t StreamType) IsAudio() bool {
switch t {
case StreamTypeMPEG1Audio,
StreamTypeMPEG2Audio,
StreamTypeAACAudio,
StreamTypeAACLATMAudio,
StreamTypeAC3Audio,
StreamTypeDTSAudio,
StreamTypeTRUEHDAudio,
StreamTypeEAC3Audio:
return true
}
return false
}
func (t StreamType) String() string {
switch t {
case StreamTypeMPEG1Video:
return "MPEG1 Video"
case StreamTypeMPEG2Video:
return "MPEG2 Video"
case StreamTypeMPEG1Audio:
return "MPEG1 Audio"
case StreamTypeMPEG2Audio:
return "MPEG2 Audio"
case StreamTypePrivateSection:
return "Private Section"
case StreamTypePrivateData:
return "Private Data"
case StreamTypeAACAudio:
return "AAC Audio"
case StreamTypeMPEG4Video:
return "MPEG4 Video"
case StreamTypeAACLATMAudio:
return "AAC LATM Audio"
case StreamTypeMetadata:
return "Metadata"
case StreamTypeH264Video:
return "H264 Video"
case StreamTypeH265Video:
return "H265 Video"
case StreamTypeCAVSVideo:
return "CAVS Video"
case StreamTypeVC1Video:
return "VC1 Video"
case StreamTypeDIRACVideo:
return "DIRAC Video"
case StreamTypeAC3Audio:
return "AC3 Audio"
case StreamTypeDTSAudio:
return "DTS Audio"
case StreamTypeTRUEHDAudio:
return "TRUEHD Audio"
case StreamTypeSCTE35:
return "SCTE 35"
case StreamTypeEAC3Audio:
return "EAC3 Audio"
}
return "Unknown"
}
func (t StreamType) ToPESStreamID() uint8 {
switch t {
case StreamTypeMPEG1Video, StreamTypeMPEG2Video, StreamTypeMPEG4Video, StreamTypeH264Video,
StreamTypeH265Video, StreamTypeCAVSVideo, StreamTypeVC1Video:
return 0xe0
case StreamTypeDIRACVideo:
return 0xfd
case StreamTypeMPEG2Audio, StreamTypeAACAudio, StreamTypeAACLATMAudio:
return 0xc0
case StreamTypeAC3Audio, StreamTypeEAC3Audio: // m2ts_mode???
return 0xfd
case StreamTypePrivateSection, StreamTypePrivateData, StreamTypeMetadata:
return 0xfc
default:
return 0xbd
}
}