ps-proto.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. package mpeg2
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "os"
  6. "github.com/yapingcat/gomedia/codec"
  7. )
  8. type Error interface {
  9. NeedMore() bool
  10. ParserError() bool
  11. StreamIdNotFound() bool
  12. }
  13. var errNeedMore error = &needmoreError{}
  14. type needmoreError struct{}
  15. func (e *needmoreError) Error() string { return "need more bytes" }
  16. func (e *needmoreError) NeedMore() bool { return true }
  17. func (e *needmoreError) ParserError() bool { return false }
  18. func (e *needmoreError) StreamIdNotFound() bool { return false }
  19. var errParser error = &parserError{}
  20. type parserError struct{}
  21. func (e *parserError) Error() string { return "parser packet error" }
  22. func (e *parserError) NeedMore() bool { return false }
  23. func (e *parserError) ParserError() bool { return true }
  24. func (e *parserError) StreamIdNotFound() bool { return false }
  25. var errNotFound error = &sidNotFoundError{}
  26. type sidNotFoundError struct{}
  27. func (e *sidNotFoundError) Error() string { return "stream id not found" }
  28. func (e *sidNotFoundError) NeedMore() bool { return false }
  29. func (e *sidNotFoundError) ParserError() bool { return false }
  30. func (e *sidNotFoundError) StreamIdNotFound() bool { return true }
  31. type PS_STREAM_TYPE int
  32. const (
  33. PS_STREAM_UNKNOW PS_STREAM_TYPE = 0xFF
  34. PS_STREAM_AAC PS_STREAM_TYPE = 0x0F
  35. PS_STREAM_H264 PS_STREAM_TYPE = 0x1B
  36. PS_STREAM_H265 PS_STREAM_TYPE = 0x24
  37. PS_STREAM_G711A PS_STREAM_TYPE = 0x90
  38. PS_STREAM_G711U PS_STREAM_TYPE = 0x91
  39. )
  40. // Table 2-33 – Program Stream pack header
  41. // pack_header() {
  42. // pack_start_code 32 bslbf
  43. // '01' 2 bslbf
  44. // system_clock_reference_base [32..30] 3 bslbf
  45. // marker_bit 1 bslbf
  46. // system_clock_reference_base [29..15] 15 bslbf
  47. // marker_bit 1 bslbf
  48. // system_clock_reference_base [14..0] 15 bslbf
  49. // marker_bit 1 bslbf
  50. // system_clock_reference_extension 9 uimsbf
  51. // marker_bit 1 bslbf
  52. // program_mux_rate 22 uimsbf
  53. // marker_bit 1 bslbf
  54. // marker_bit 1 bslbf
  55. // reserved 5 bslbf
  56. // pack_stuffing_length 3 uimsbf
  57. // for (i = 0; i < pack_stuffing_length; i++) {
  58. // stuffing_byte 8 bslbf
  59. // }
  60. // if (nextbits() == system_header_start_code) {
  61. // system_header ()
  62. // }
  63. // }
  64. type PSPackHeader struct {
  65. IsMpeg1 bool
  66. System_clock_reference_base uint64 //33 bits
  67. System_clock_reference_extension uint16 //9 bits
  68. Program_mux_rate uint32 //22 bits
  69. Pack_stuffing_length uint8 //3 bitss
  70. }
  71. func (ps_pkg_hdr *PSPackHeader) PrettyPrint(file *os.File) {
  72. file.WriteString(fmt.Sprintf("IsMpeg1:%t\n", ps_pkg_hdr.IsMpeg1))
  73. file.WriteString(fmt.Sprintf("System_clock_reference_base:%d\n", ps_pkg_hdr.System_clock_reference_base))
  74. file.WriteString(fmt.Sprintf("System_clock_reference_extension:%d\n", ps_pkg_hdr.System_clock_reference_extension))
  75. file.WriteString(fmt.Sprintf("Program_mux_rate:%d\n", ps_pkg_hdr.Program_mux_rate))
  76. file.WriteString(fmt.Sprintf("Pack_stuffing_length:%d\n", ps_pkg_hdr.Pack_stuffing_length))
  77. }
  78. func (ps_pkg_hdr *PSPackHeader) Decode(bs *codec.BitStream) error {
  79. if bs.RemainBytes() < 5 {
  80. return errNeedMore
  81. }
  82. if bs.Uint32(32) != 0x000001BA {
  83. panic("ps header must start with 000001BA")
  84. }
  85. if bs.NextBits(2) == 0x01 { //mpeg2
  86. if bs.RemainBytes() < 10 {
  87. return errNeedMore
  88. }
  89. return ps_pkg_hdr.decodeMpeg2(bs)
  90. } else if bs.NextBits(4) == 0x02 { //mpeg1
  91. if bs.RemainBytes() < 8 {
  92. return errNeedMore
  93. }
  94. ps_pkg_hdr.IsMpeg1 = true
  95. return ps_pkg_hdr.decodeMpeg1(bs)
  96. } else {
  97. return errParser
  98. }
  99. }
  100. func (ps_pkg_hdr *PSPackHeader) decodeMpeg2(bs *codec.BitStream) error {
  101. bs.SkipBits(2)
  102. ps_pkg_hdr.System_clock_reference_base = bs.GetBits(3)
  103. bs.SkipBits(1)
  104. ps_pkg_hdr.System_clock_reference_base = ps_pkg_hdr.System_clock_reference_base<<15 | bs.GetBits(15)
  105. bs.SkipBits(1)
  106. ps_pkg_hdr.System_clock_reference_base = ps_pkg_hdr.System_clock_reference_base<<15 | bs.GetBits(15)
  107. bs.SkipBits(1)
  108. ps_pkg_hdr.System_clock_reference_extension = bs.Uint16(9)
  109. bs.SkipBits(1)
  110. ps_pkg_hdr.Program_mux_rate = bs.Uint32(22)
  111. bs.SkipBits(1)
  112. bs.SkipBits(1)
  113. bs.SkipBits(5)
  114. ps_pkg_hdr.Pack_stuffing_length = bs.Uint8(3)
  115. if bs.RemainBytes() < int(ps_pkg_hdr.Pack_stuffing_length) {
  116. bs.UnRead(10 * 8)
  117. return errNeedMore
  118. }
  119. bs.SkipBits(int(ps_pkg_hdr.Pack_stuffing_length) * 8)
  120. return nil
  121. }
  122. func (ps_pkg_hdr *PSPackHeader) decodeMpeg1(bs *codec.BitStream) error {
  123. bs.SkipBits(4)
  124. ps_pkg_hdr.System_clock_reference_base = bs.GetBits(3)
  125. bs.SkipBits(1)
  126. ps_pkg_hdr.System_clock_reference_base = ps_pkg_hdr.System_clock_reference_base<<15 | bs.GetBits(15)
  127. bs.SkipBits(1)
  128. ps_pkg_hdr.System_clock_reference_base = ps_pkg_hdr.System_clock_reference_base<<15 | bs.GetBits(15)
  129. bs.SkipBits(1)
  130. ps_pkg_hdr.System_clock_reference_extension = 1
  131. ps_pkg_hdr.Program_mux_rate = bs.Uint32(7)
  132. bs.SkipBits(1)
  133. ps_pkg_hdr.Program_mux_rate = ps_pkg_hdr.Program_mux_rate<<15 | bs.Uint32(15)
  134. bs.SkipBits(1)
  135. return nil
  136. }
  137. func (ps_pkg_hdr *PSPackHeader) Encode(bsw *codec.BitStreamWriter) {
  138. bsw.PutBytes([]byte{0x00, 0x00, 0x01, 0xBA})
  139. bsw.PutUint8(1, 2)
  140. bsw.PutUint64(ps_pkg_hdr.System_clock_reference_base>>30, 3)
  141. bsw.PutUint8(1, 1)
  142. bsw.PutUint64(ps_pkg_hdr.System_clock_reference_base>>15, 15)
  143. bsw.PutUint8(1, 1)
  144. bsw.PutUint64(ps_pkg_hdr.System_clock_reference_base, 15)
  145. bsw.PutUint8(1, 1)
  146. bsw.PutUint16(ps_pkg_hdr.System_clock_reference_extension, 9)
  147. bsw.PutUint8(1, 1)
  148. bsw.PutUint32(ps_pkg_hdr.Program_mux_rate, 22)
  149. bsw.PutUint8(1, 1)
  150. bsw.PutUint8(1, 1)
  151. bsw.PutUint8(0x1F, 5)
  152. bsw.PutUint8(ps_pkg_hdr.Pack_stuffing_length, 3)
  153. bsw.PutRepetValue(0xFF, int(ps_pkg_hdr.Pack_stuffing_length))
  154. }
  155. type Elementary_Stream struct {
  156. Stream_id uint8
  157. P_STD_buffer_bound_scale uint8
  158. P_STD_buffer_size_bound uint16
  159. }
  160. func NewElementary_Stream(sid uint8) *Elementary_Stream {
  161. return &Elementary_Stream{
  162. Stream_id: sid,
  163. }
  164. }
  165. // system_header () {
  166. // system_header_start_code 32 bslbf
  167. // header_length 16 uimsbf
  168. // marker_bit 1 bslbf
  169. // rate_bound 22 uimsbf
  170. // marker_bit 1 bslbf
  171. // audio_bound 6 uimsbf
  172. // fixed_flag 1 bslbf
  173. // CSPS_flag 1 bslbf
  174. // system_audio_lock_flag 1 bslbf
  175. // system_video_lock_flag 1 bslbf
  176. // marker_bit 1 bslbf
  177. // video_bound 5 uimsbf
  178. // packet_rate_restriction_flag 1 bslbf
  179. // reserved_bits 7 bslbf
  180. // while (nextbits () == '1') {
  181. // stream_id 8 uimsbf
  182. // '11' 2 bslbf
  183. // P-STD_buffer_bound_scale 1 bslbf
  184. // P-STD_buffer_size_bound 13 uimsbf
  185. // }
  186. // }
  187. type System_header struct {
  188. Header_length uint16
  189. Rate_bound uint32
  190. Audio_bound uint8
  191. Fixed_flag uint8
  192. CSPS_flag uint8
  193. System_audio_lock_flag uint8
  194. System_video_lock_flag uint8
  195. Video_bound uint8
  196. Packet_rate_restriction_flag uint8
  197. Streams []*Elementary_Stream
  198. }
  199. func (sh *System_header) PrettyPrint(file *os.File) {
  200. file.WriteString(fmt.Sprintf("Header_length:%d\n", sh.Header_length))
  201. file.WriteString(fmt.Sprintf("Rate_bound:%d\n", sh.Rate_bound))
  202. file.WriteString(fmt.Sprintf("Audio_bound:%d\n", sh.Audio_bound))
  203. file.WriteString(fmt.Sprintf("Fixed_flag:%d\n", sh.Fixed_flag))
  204. file.WriteString(fmt.Sprintf("CSPS_flag:%d\n", sh.CSPS_flag))
  205. file.WriteString(fmt.Sprintf("System_audio_lock_flag:%d\n", sh.System_audio_lock_flag))
  206. file.WriteString(fmt.Sprintf("System_video_lock_flag:%d\n", sh.System_video_lock_flag))
  207. file.WriteString(fmt.Sprintf("Video_bound:%d\n", sh.Video_bound))
  208. file.WriteString(fmt.Sprintf("Packet_rate_restriction_flag:%d\n", sh.Packet_rate_restriction_flag))
  209. for i, es := range sh.Streams {
  210. file.WriteString(fmt.Sprintf("----streams %d\n", i))
  211. file.WriteString(fmt.Sprintf(" Stream_id:%d\n", es.Stream_id))
  212. file.WriteString(fmt.Sprintf(" P_STD_buffer_bound_scale:%d\n", es.P_STD_buffer_bound_scale))
  213. file.WriteString(fmt.Sprintf(" P_STD_buffer_size_bound:%d\n", es.P_STD_buffer_size_bound))
  214. }
  215. }
  216. func (sh *System_header) Encode(bsw *codec.BitStreamWriter) {
  217. bsw.PutBytes([]byte{0x00, 0x00, 0x01, 0xBB})
  218. loc := bsw.ByteOffset()
  219. bsw.PutUint16(0, 16)
  220. bsw.Markdot()
  221. bsw.PutUint8(1, 1)
  222. bsw.PutUint32(sh.Rate_bound, 22)
  223. bsw.PutUint8(1, 1)
  224. bsw.PutUint8(sh.Audio_bound, 6)
  225. bsw.PutUint8(sh.Fixed_flag, 1)
  226. bsw.PutUint8(sh.CSPS_flag, 1)
  227. bsw.PutUint8(sh.System_audio_lock_flag, 1)
  228. bsw.PutUint8(sh.System_video_lock_flag, 1)
  229. bsw.PutUint8(1, 1)
  230. bsw.PutUint8(sh.Video_bound, 5)
  231. bsw.PutUint8(sh.Packet_rate_restriction_flag, 1)
  232. bsw.PutUint8(0x7F, 7)
  233. for _, stream := range sh.Streams {
  234. bsw.PutUint8(stream.Stream_id, 8)
  235. bsw.PutUint8(3, 2)
  236. bsw.PutUint8(stream.P_STD_buffer_bound_scale, 1)
  237. bsw.PutUint16(stream.P_STD_buffer_size_bound, 13)
  238. }
  239. length := bsw.DistanceFromMarkDot() / 8
  240. bsw.SetUint16(uint16(length), loc)
  241. }
  242. func (sh *System_header) Decode(bs *codec.BitStream) error {
  243. if bs.RemainBytes() < 12 {
  244. return errNeedMore
  245. }
  246. if bs.Uint32(32) != 0x000001BB {
  247. panic("system header must start with 000001BB")
  248. }
  249. sh.Header_length = bs.Uint16(16)
  250. if bs.RemainBytes() < int(sh.Header_length) {
  251. bs.UnRead(6 * 8)
  252. return errNeedMore
  253. }
  254. if sh.Header_length < 6 || (sh.Header_length-6)%3 != 0 {
  255. return errParser
  256. }
  257. bs.SkipBits(1)
  258. sh.Rate_bound = bs.Uint32(22)
  259. bs.SkipBits(1)
  260. sh.Audio_bound = bs.Uint8(6)
  261. sh.Fixed_flag = bs.Uint8(1)
  262. sh.CSPS_flag = bs.Uint8(1)
  263. sh.System_audio_lock_flag = bs.Uint8(1)
  264. sh.System_video_lock_flag = bs.Uint8(1)
  265. bs.SkipBits(1)
  266. sh.Video_bound = bs.Uint8(5)
  267. sh.Packet_rate_restriction_flag = bs.Uint8(1)
  268. bs.SkipBits(7)
  269. sh.Streams = sh.Streams[:0]
  270. least := sh.Header_length - 6
  271. for least > 0 && bs.NextBits(1) == 0x01 {
  272. es := new(Elementary_Stream)
  273. es.Stream_id = bs.Uint8(8)
  274. bs.SkipBits(2)
  275. es.P_STD_buffer_bound_scale = bs.GetBit()
  276. es.P_STD_buffer_size_bound = bs.Uint16(13)
  277. sh.Streams = append(sh.Streams, es)
  278. least -= 3
  279. }
  280. if least > 0 {
  281. return errParser
  282. }
  283. return nil
  284. }
  285. type Elementary_stream_elem struct {
  286. Stream_type uint8
  287. Elementary_stream_id uint8
  288. Elementary_stream_info_length uint16
  289. }
  290. func NewElementary_stream_elem(stype uint8, esid uint8) *Elementary_stream_elem {
  291. return &Elementary_stream_elem{
  292. Stream_type: stype,
  293. Elementary_stream_id: esid,
  294. }
  295. }
  296. // program_stream_map() {
  297. // packet_start_code_prefix 24 bslbf
  298. // map_stream_id 8 uimsbf
  299. // program_stream_map_length 16 uimsbf
  300. // current_next_indicator 1 bslbf
  301. // reserved 2 bslbf
  302. // program_stream_map_version 5 uimsbf
  303. // reserved 7 bslbf
  304. // marker_bit 1 bslbf
  305. // program_stream_info_length 16 uimsbf
  306. // for (i = 0; i < N; i++) {
  307. // descriptor()
  308. // }
  309. // elementary_stream_map_length 16 uimsbf
  310. // for (i = 0; i < N1; i++) {
  311. // stream_type 8 uimsbf
  312. // elementary_stream_id 8 uimsbf
  313. // elementary_stream_info_length 16 uimsbf
  314. // for (i = 0; i < N2; i++) {
  315. // descriptor()
  316. // }
  317. // }
  318. // CRC_32 32 rpchof
  319. // }
  320. type Program_stream_map struct {
  321. Map_stream_id uint8
  322. Program_stream_map_length uint16
  323. Current_next_indicator uint8
  324. Program_stream_map_version uint8
  325. Program_stream_info_length uint16
  326. Elementary_stream_map_length uint16
  327. Stream_map []*Elementary_stream_elem
  328. }
  329. func (psm *Program_stream_map) PrettyPrint(file *os.File) {
  330. file.WriteString(fmt.Sprintf("map_stream_id:%d\n", psm.Map_stream_id))
  331. file.WriteString(fmt.Sprintf("program_stream_map_length:%d\n", psm.Program_stream_map_length))
  332. file.WriteString(fmt.Sprintf("current_next_indicator:%d\n", psm.Current_next_indicator))
  333. file.WriteString(fmt.Sprintf("program_stream_map_version:%d\n", psm.Program_stream_map_version))
  334. file.WriteString(fmt.Sprintf("program_stream_info_length:%d\n", psm.Program_stream_info_length))
  335. file.WriteString(fmt.Sprintf("elementary_stream_map_length:%d\n", psm.Elementary_stream_map_length))
  336. for i, es := range psm.Stream_map {
  337. file.WriteString(fmt.Sprintf("----ES stream %d\n", i))
  338. if es.Stream_type == uint8(PS_STREAM_AAC) {
  339. file.WriteString(" stream_type:AAC\n")
  340. } else if es.Stream_type == uint8(PS_STREAM_G711A) {
  341. file.WriteString(" stream_type:G711A\n")
  342. } else if es.Stream_type == uint8(PS_STREAM_G711U) {
  343. file.WriteString(" stream_type:G711U\n")
  344. } else if es.Stream_type == uint8(PS_STREAM_H264) {
  345. file.WriteString(" stream_type:H264\n")
  346. } else if es.Stream_type == uint8(PS_STREAM_H265) {
  347. file.WriteString(" stream_type:H265\n")
  348. }
  349. file.WriteString(fmt.Sprintf(" elementary_stream_id:%d\n", es.Elementary_stream_id))
  350. file.WriteString(fmt.Sprintf(" elementary_stream_info_length:%d\n", es.Elementary_stream_info_length))
  351. }
  352. }
  353. func (psm *Program_stream_map) Encode(bsw *codec.BitStreamWriter) {
  354. bsw.PutBytes([]byte{0x00, 0x00, 0x01, 0xBC})
  355. loc := bsw.ByteOffset()
  356. bsw.PutUint16(psm.Program_stream_map_length, 16)
  357. bsw.Markdot()
  358. bsw.PutUint8(psm.Current_next_indicator, 1)
  359. bsw.PutUint8(3, 2)
  360. bsw.PutUint8(psm.Program_stream_map_version, 5)
  361. bsw.PutUint8(0x7F, 7)
  362. bsw.PutUint8(1, 1)
  363. bsw.PutUint16(0, 16)
  364. psm.Elementary_stream_map_length = uint16(len(psm.Stream_map) * 4)
  365. bsw.PutUint16(psm.Elementary_stream_map_length, 16)
  366. for _, streaminfo := range psm.Stream_map {
  367. bsw.PutUint8(streaminfo.Stream_type, 8)
  368. bsw.PutUint8(streaminfo.Elementary_stream_id, 8)
  369. bsw.PutUint16(0, 16)
  370. }
  371. length := bsw.DistanceFromMarkDot()/8 + 4
  372. bsw.SetUint16(uint16(length), loc)
  373. crc := codec.CalcCrc32(0xffffffff, bsw.Bits()[bsw.ByteOffset()-int(length-4)-4:bsw.ByteOffset()])
  374. tmpcrc := make([]byte, 4)
  375. binary.LittleEndian.PutUint32(tmpcrc, crc)
  376. bsw.PutBytes(tmpcrc)
  377. }
  378. func (psm *Program_stream_map) Decode(bs *codec.BitStream) error {
  379. if bs.RemainBytes() < 16 {
  380. return errNeedMore
  381. }
  382. if bs.Uint32(24) != 0x000001 {
  383. panic("program stream map must startwith 0x000001")
  384. }
  385. psm.Map_stream_id = bs.Uint8(8)
  386. if psm.Map_stream_id != 0xBC {
  387. panic("map stream id must be 0xBC")
  388. }
  389. psm.Program_stream_map_length = bs.Uint16(16)
  390. if bs.RemainBytes() < int(psm.Program_stream_map_length) {
  391. bs.UnRead(6 * 8)
  392. return errNeedMore
  393. }
  394. psm.Current_next_indicator = bs.Uint8(1)
  395. bs.SkipBits(2)
  396. psm.Program_stream_map_version = bs.Uint8(5)
  397. bs.SkipBits(8)
  398. psm.Program_stream_info_length = bs.Uint16(16)
  399. if bs.RemainBytes() < int(psm.Program_stream_info_length)+2 {
  400. bs.UnRead(10 * 8)
  401. return errNeedMore
  402. }
  403. bs.SkipBits(int(psm.Program_stream_info_length) * 8)
  404. psm.Elementary_stream_map_length = bs.Uint16(16)
  405. if psm.Program_stream_map_length != 6+psm.Program_stream_info_length+psm.Elementary_stream_map_length+4 {
  406. return errParser
  407. }
  408. if bs.RemainBytes() < int(psm.Elementary_stream_map_length)+4 {
  409. bs.UnRead(12*8 + int(psm.Program_stream_info_length)*8)
  410. return errNeedMore
  411. }
  412. i := 0
  413. psm.Stream_map = psm.Stream_map[:0]
  414. for i < int(psm.Elementary_stream_map_length) {
  415. elem := new(Elementary_stream_elem)
  416. elem.Stream_type = bs.Uint8(8)
  417. elem.Elementary_stream_id = bs.Uint8(8)
  418. elem.Elementary_stream_info_length = bs.Uint16(16)
  419. //TODO Parser descriptor
  420. if bs.RemainBytes() < int(elem.Elementary_stream_info_length) {
  421. return errParser
  422. }
  423. bs.SkipBits(int(elem.Elementary_stream_info_length) * 8)
  424. i += int(4 + elem.Elementary_stream_info_length)
  425. psm.Stream_map = append(psm.Stream_map, elem)
  426. }
  427. if i != int(psm.Elementary_stream_map_length) {
  428. return errParser
  429. }
  430. bs.SkipBits(32)
  431. return nil
  432. }
  433. type Program_stream_directory struct {
  434. PES_packet_length uint16
  435. }
  436. func (psd *Program_stream_directory) Decode(bs *codec.BitStream) error {
  437. if bs.RemainBytes() < 6 {
  438. return errNeedMore
  439. }
  440. if bs.Uint32(32) != 0x000001FF {
  441. panic("program stream directory 000001FF")
  442. }
  443. psd.PES_packet_length = bs.Uint16(16)
  444. if bs.RemainBytes() < int(psd.PES_packet_length) {
  445. bs.UnRead(6 * 8)
  446. return errNeedMore
  447. }
  448. //TODO Program Stream directory
  449. bs.SkipBits(int(psd.PES_packet_length) * 8)
  450. return nil
  451. }
  452. type CommonPesPacket struct {
  453. Stream_id uint8
  454. PES_packet_length uint16
  455. }
  456. func (compes *CommonPesPacket) Decode(bs *codec.BitStream) error {
  457. if bs.RemainBytes() < 6 {
  458. return errNeedMore
  459. }
  460. bs.SkipBits(24)
  461. compes.Stream_id = bs.Uint8(8)
  462. compes.PES_packet_length = bs.Uint16(16)
  463. if bs.RemainBytes() < int(compes.PES_packet_length) {
  464. bs.UnRead(6 * 8)
  465. return errNeedMore
  466. }
  467. bs.SkipBits(int(compes.PES_packet_length) * 8)
  468. return nil
  469. }
  470. type PSPacket struct {
  471. Header *PSPackHeader
  472. System *System_header
  473. Psm *Program_stream_map
  474. Psd *Program_stream_directory
  475. CommPes *CommonPesPacket
  476. Pes *PesPacket
  477. }