MCPcopy Create free account
hub / github.com/Monibuca/engine / ReadTsHeader

Function ReadTsHeader

codec/mpegts/mpegts.go:208–378  ·  view source on GitHub ↗
(r io.Reader)

Source from the content-addressed store, hash-verified

206}
207
208func ReadTsHeader(r io.Reader) (header MpegTsHeader, err error) {
209 var h uint32
210
211 // MPEGTS Header 4个字节
212 h, err = util.ReadByteToUint32(r, true)
213 if err != nil {
214 return
215 }
216
217 // payloadUnitStartIndicator
218 // 为1时,表示在4个字节后,有一个调整字节.包头后需要除去一个字节才是有效数据(payload_unit_start_indicator="1")
219 // header.payloadUnitStartIndicator = uint8(h & 0x400000)
220
221 // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 |
222
223 // | 1111 1111 | 0000 0000 | 0000 0000 | 0000 0000 |
224 header.SyncByte = byte((h & 0xff000000) >> 24)
225
226 if header.SyncByte != 0x47 {
227 err = errors.New("mpegts header sync error!")
228 return
229 }
230
231 // | 0000 0000 | 1000 0000 | 0000 0000 | 0000 0000 |
232 header.TransportErrorIndicator = byte((h & 0x800000) >> 23)
233
234 // | 0000 0000 | 0100 0000 | 0000 0000 | 0000 0000 |
235 header.PayloadUnitStartIndicator = byte((h & 0x400000) >> 22)
236
237 // | 0000 0000 | 0010 0000 | 0000 0000 | 0000 0000 |
238 header.TransportPriority = byte((h & 0x200000) >> 21)
239
240 // | 0000 0000 | 0001 1111 | 1111 1111 | 0000 0000 |
241 header.Pid = uint16((h & 0x1fff00) >> 8)
242
243 // | 0000 0000 | 0000 0000 | 0000 0000 | 1100 0000 |
244 header.TransportScramblingControl = byte((h & 0xc0) >> 6)
245
246 // | 0000 0000 | 0000 0000 | 0000 0000 | 0011 0000 |
247 // 0x30 , 0x20 -> adaptation_field, 0x10 -> Payload
248 header.AdaptionFieldControl = byte((h & 0x30) >> 4)
249
250 // | 0000 0000 | 0000 0000 | 0000 0000 | 0000 1111 |
251 header.ContinuityCounter = byte(h & 0xf)
252
253 // | 0010 0000 |
254 // adaptionFieldControl
255 // 表示TS分组首部后面是否跟随有调整字段和有效负载.
256 // 01仅含有效负载(没有adaptation_field)
257 // 10仅含调整字段(没有Payload)
258 // 11含有调整字段和有效负载(有adaptation_field,adaptation_field之后是Payload).
259 // 为00的话解码器不进行处理.空分组没有调整字段
260 // 当值为'11时,adaptation_field_length 值必须在0 到182 的区间内.
261 // 当值为'10'时,adaptation_field_length 值必须为183.
262 // 对于承载PES 包的传输流包,只要存在欠充足的PES 包数据就需要通过填充来完全填满传输流包的有效载荷字节.
263 // 填充通过规定自适应字段长度比自适应字段中数据元的长度总和还要长来实现,以致于自适应字段在完全容纳有效的PES 包数据后,有效载荷字节仍有剩余.自适应字段中额外空间采用填充字节填满.
264 if header.AdaptionFieldControl >= 2 {
265 // adaptationFieldLength

Callers 2

ReadTsPacketFunction · 0.85
FeedMethod · 0.85

Calls 5

ReadByteToUint32Function · 0.92
ReadByteToUint8Function · 0.92
ReadByteToUint48Function · 0.92
NewMethod · 0.80
ReadMethod · 0.65

Tested by

no test coverage detected