pes-proto.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package mpeg2
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/yapingcat/gomedia/codec"
  6. )
  7. var H264_AUD_NALU []byte = []byte{0x00, 0x00, 0x00, 0x01, 0x09, 0xF0} //ffmpeg mpegtsenc.c mpegts_write_packet_internal
  8. var H265_AUD_NALU []byte = []byte{0x00, 0x00, 0x00, 0x01, 0x46, 0x01, 0x50}
  9. type PES_STREMA_ID int
  10. const (
  11. PES_STREAM_END PES_STREMA_ID = 0xB9
  12. PES_STREAM_START PES_STREMA_ID = 0xBA
  13. PES_STREAM_SYSTEM_HEAD PES_STREMA_ID = 0xBB
  14. PES_STREAM_MAP PES_STREMA_ID = 0xBC
  15. PES_STREAM_PRIVATE PES_STREMA_ID = 0xBD
  16. PES_STREAM_AUDIO PES_STREMA_ID = 0xC0
  17. PES_STREAM_VIDEO PES_STREMA_ID = 0xE0
  18. )
  19. func findPESIDByStreamType(cid TS_STREAM_TYPE) PES_STREMA_ID {
  20. if cid == TS_STREAM_AAC {
  21. return PES_STREAM_AUDIO
  22. } else if cid == TS_STREAM_H264 || cid == TS_STREAM_H265 {
  23. return PES_STREAM_VIDEO
  24. } else {
  25. return PES_STREAM_PRIVATE
  26. }
  27. }
  28. type PesPacket struct {
  29. Stream_id uint8
  30. PES_packet_length uint16
  31. PES_scrambling_control uint8
  32. PES_priority uint8
  33. Data_alignment_indicator uint8
  34. Copyright uint8
  35. Original_or_copy uint8
  36. PTS_DTS_flags uint8
  37. ESCR_flag uint8
  38. ES_rate_flag uint8
  39. DSM_trick_mode_flag uint8
  40. Additional_copy_info_flag uint8
  41. PES_CRC_flag uint8
  42. PES_extension_flag uint8
  43. PES_header_data_length uint8
  44. Pts uint64
  45. Dts uint64
  46. ESCR_base uint64
  47. ESCR_extension uint16
  48. ES_rate uint32
  49. Trick_mode_control uint8
  50. Trick_value uint8
  51. Additional_copy_info uint8
  52. Previous_PES_packet_CRC uint16
  53. Pes_payload []byte
  54. //TODO
  55. //if ( PES_extension_flag == '1')
  56. // PES_private_data_flag uint8
  57. // pack_header_field_flag uint8
  58. // program_packet_sequence_counter_flag uint8
  59. // P_STD_buffer_flag uint8
  60. // PES_extension_flag_2 uint8
  61. // PES_private_data [16]byte
  62. }
  63. func NewPesPacket() *PesPacket {
  64. return new(PesPacket)
  65. }
  66. func (pkg *PesPacket) PrettyPrint(file *os.File) {
  67. file.WriteString(fmt.Sprintf("stream_id:%d\n", pkg.Stream_id))
  68. file.WriteString(fmt.Sprintf("PES_packet_length:%d\n", pkg.PES_packet_length))
  69. file.WriteString(fmt.Sprintf("PES_scrambling_control:%d\n", pkg.PES_scrambling_control))
  70. file.WriteString(fmt.Sprintf("PES_priority:%d\n", pkg.PES_priority))
  71. file.WriteString(fmt.Sprintf("data_alignment_indicator:%d\n", pkg.Data_alignment_indicator))
  72. file.WriteString(fmt.Sprintf("copyright:%d\n", pkg.Copyright))
  73. file.WriteString(fmt.Sprintf("original_or_copy:%d\n", pkg.Original_or_copy))
  74. file.WriteString(fmt.Sprintf("PTS_DTS_flags:%d\n", pkg.PTS_DTS_flags))
  75. file.WriteString(fmt.Sprintf("ESCR_flag:%d\n", pkg.ESCR_flag))
  76. file.WriteString(fmt.Sprintf("ES_rate_flag:%d\n", pkg.ES_rate_flag))
  77. file.WriteString(fmt.Sprintf("DSM_trick_mode_flag:%d\n", pkg.DSM_trick_mode_flag))
  78. file.WriteString(fmt.Sprintf("additional_copy_info_flag:%d\n", pkg.Additional_copy_info_flag))
  79. file.WriteString(fmt.Sprintf("PES_CRC_flag:%d\n", pkg.PES_CRC_flag))
  80. file.WriteString(fmt.Sprintf("PES_extension_flag:%d\n", pkg.PES_extension_flag))
  81. file.WriteString(fmt.Sprintf("PES_header_data_length:%d\n", pkg.PES_header_data_length))
  82. if pkg.PTS_DTS_flags&0x02 == 0x02 {
  83. file.WriteString(fmt.Sprintf("PTS:%d\n", pkg.Pts))
  84. }
  85. if pkg.PTS_DTS_flags&0x03 == 0x03 {
  86. file.WriteString(fmt.Sprintf("DTS:%d\n", pkg.Dts))
  87. }
  88. if pkg.ESCR_flag == 1 {
  89. file.WriteString(fmt.Sprintf("ESCR_base:%d\n", pkg.ESCR_base))
  90. file.WriteString(fmt.Sprintf("ESCR_extension:%d\n", pkg.ESCR_extension))
  91. }
  92. if pkg.ES_rate_flag == 1 {
  93. file.WriteString(fmt.Sprintf("ES_rate:%d\n", pkg.ES_rate))
  94. }
  95. if pkg.DSM_trick_mode_flag == 1 {
  96. file.WriteString(fmt.Sprintf("trick_mode_control:%d\n", pkg.Trick_mode_control))
  97. }
  98. if pkg.Additional_copy_info_flag == 1 {
  99. file.WriteString(fmt.Sprintf("additional_copy_info:%d\n", pkg.Additional_copy_info))
  100. }
  101. if pkg.PES_CRC_flag == 1 {
  102. file.WriteString(fmt.Sprintf("previous_PES_packet_CRC:%d\n", pkg.Previous_PES_packet_CRC))
  103. }
  104. file.WriteString("PES_packet_data_byte:\n")
  105. file.WriteString(fmt.Sprintf(" Size: %d\n", len(pkg.Pes_payload)))
  106. file.WriteString(" data:")
  107. for i := 0; i < 12 && i < len(pkg.Pes_payload); i++ {
  108. if i%4 == 0 {
  109. file.WriteString("\n")
  110. file.WriteString(" ")
  111. }
  112. file.WriteString(fmt.Sprintf("0x%02x ", pkg.Pes_payload[i]))
  113. }
  114. file.WriteString("\n")
  115. }
  116. func (pkg *PesPacket) Decode(bs *codec.BitStream) error {
  117. if bs.RemainBytes() < 9 {
  118. return errNeedMore
  119. }
  120. bs.SkipBits(24) //packet_start_code_prefix
  121. pkg.Stream_id = bs.Uint8(8) //stream_id
  122. pkg.PES_packet_length = bs.Uint16(16)
  123. bs.SkipBits(2) //'10'
  124. pkg.PES_scrambling_control = bs.Uint8(2)
  125. pkg.PES_priority = bs.Uint8(1)
  126. pkg.Data_alignment_indicator = bs.Uint8(1)
  127. pkg.Copyright = bs.Uint8(1)
  128. pkg.Original_or_copy = bs.Uint8(1)
  129. pkg.PTS_DTS_flags = bs.Uint8(2)
  130. pkg.ESCR_flag = bs.Uint8(1)
  131. pkg.ES_rate_flag = bs.Uint8(1)
  132. pkg.DSM_trick_mode_flag = bs.Uint8(1)
  133. pkg.Additional_copy_info_flag = bs.Uint8(1)
  134. pkg.PES_CRC_flag = bs.Uint8(1)
  135. pkg.PES_extension_flag = bs.Uint8(1)
  136. pkg.PES_header_data_length = bs.Uint8(8)
  137. if bs.RemainBytes() < int(pkg.PES_header_data_length) {
  138. bs.UnRead(9 * 8)
  139. return errNeedMore
  140. }
  141. bs.Markdot()
  142. if pkg.PTS_DTS_flags&0x02 == 0x02 {
  143. bs.SkipBits(4)
  144. pkg.Pts = bs.GetBits(3)
  145. bs.SkipBits(1)
  146. pkg.Pts = (pkg.Pts << 15) | bs.GetBits(15)
  147. bs.SkipBits(1)
  148. pkg.Pts = (pkg.Pts << 15) | bs.GetBits(15)
  149. bs.SkipBits(1)
  150. }
  151. if pkg.PTS_DTS_flags&0x03 == 0x03 {
  152. bs.SkipBits(4)
  153. pkg.Dts = bs.GetBits(3)
  154. bs.SkipBits(1)
  155. pkg.Dts = (pkg.Dts << 15) | bs.GetBits(15)
  156. bs.SkipBits(1)
  157. pkg.Dts = (pkg.Dts << 15) | bs.GetBits(15)
  158. bs.SkipBits(1)
  159. } else {
  160. pkg.Dts = pkg.Pts
  161. }
  162. if pkg.ESCR_flag == 1 {
  163. bs.SkipBits(2)
  164. pkg.ESCR_base = bs.GetBits(3)
  165. bs.SkipBits(1)
  166. pkg.ESCR_base = (pkg.Pts << 15) | bs.GetBits(15)
  167. bs.SkipBits(1)
  168. pkg.ESCR_base = (pkg.Pts << 15) | bs.GetBits(15)
  169. bs.SkipBits(1)
  170. pkg.ESCR_extension = bs.Uint16(9)
  171. bs.SkipBits(1)
  172. }
  173. if pkg.ES_rate_flag == 1 {
  174. bs.SkipBits(1)
  175. pkg.ES_rate = bs.Uint32(22)
  176. bs.SkipBits(1)
  177. }
  178. if pkg.DSM_trick_mode_flag == 1 {
  179. pkg.Trick_mode_control = bs.Uint8(3)
  180. pkg.Trick_value = bs.Uint8(5)
  181. }
  182. if pkg.Additional_copy_info_flag == 1 {
  183. pkg.Additional_copy_info = bs.Uint8(7)
  184. }
  185. if pkg.PES_CRC_flag == 1 {
  186. pkg.Previous_PES_packet_CRC = bs.Uint16(16)
  187. }
  188. loc := bs.DistanceFromMarkDot()
  189. bs.SkipBits(int(pkg.PES_header_data_length)*8 - loc) // skip remaining header
  190. // the -3 bytes are the combined lengths
  191. // of all fields between PES_packet_length and PES_header_data_length (2 bytes)
  192. // and the PES_header_data_length itself (1 byte)
  193. dataLen := int(pkg.PES_packet_length - 3 - uint16(pkg.PES_header_data_length))
  194. if bs.RemainBytes() < dataLen {
  195. pkg.Pes_payload = bs.RemainData()
  196. bs.UnRead((9 + int(pkg.PES_header_data_length)) * 8)
  197. return errNeedMore
  198. }
  199. if pkg.PES_packet_length == 0 || bs.RemainBytes() <= dataLen {
  200. pkg.Pes_payload = bs.RemainData()
  201. bs.SkipBits(bs.RemainBits())
  202. } else {
  203. pkg.Pes_payload = bs.RemainData()[:dataLen]
  204. bs.SkipBits(dataLen * 8)
  205. }
  206. return nil
  207. }
  208. func (pkg *PesPacket) DecodeMpeg1(bs *codec.BitStream) error {
  209. if bs.RemainBytes() < 6 {
  210. return errNeedMore
  211. }
  212. bs.SkipBits(24) //packet_start_code_prefix
  213. pkg.Stream_id = bs.Uint8(8) //stream_id
  214. pkg.PES_packet_length = bs.Uint16(16)
  215. if pkg.PES_packet_length != 0 && bs.RemainBytes() < int(pkg.PES_packet_length) {
  216. bs.UnRead(6 * 8)
  217. return errNeedMore
  218. }
  219. bs.Markdot()
  220. for bs.NextBits(8) == 0xFF {
  221. bs.SkipBits(8)
  222. }
  223. if bs.NextBits(2) == 0x01 {
  224. bs.SkipBits(16)
  225. }
  226. if bs.NextBits(4) == 0x02 {
  227. bs.SkipBits(4)
  228. pkg.Pts = bs.GetBits(3)
  229. bs.SkipBits(1)
  230. pkg.Pts = pkg.Pts<<15 | bs.GetBits(15)
  231. bs.SkipBits(1)
  232. pkg.Pts = pkg.Pts<<15 | bs.GetBits(15)
  233. bs.SkipBits(1)
  234. } else if bs.NextBits(4) == 0x03 {
  235. bs.SkipBits(4)
  236. pkg.Pts = bs.GetBits(3)
  237. bs.SkipBits(1)
  238. pkg.Pts = pkg.Pts<<15 | bs.GetBits(15)
  239. bs.SkipBits(1)
  240. pkg.Pts = pkg.Pts<<15 | bs.GetBits(15)
  241. bs.SkipBits(1)
  242. pkg.Dts = bs.GetBits(3)
  243. bs.SkipBits(1)
  244. pkg.Dts = pkg.Pts<<15 | bs.GetBits(15)
  245. bs.SkipBits(1)
  246. pkg.Dts = pkg.Pts<<15 | bs.GetBits(15)
  247. bs.SkipBits(1)
  248. } else if bs.NextBits(8) == 0x0F {
  249. bs.SkipBits(8)
  250. } else {
  251. return errParser
  252. }
  253. loc := bs.DistanceFromMarkDot() / 8
  254. if pkg.PES_packet_length < uint16(loc) {
  255. return errParser
  256. }
  257. if pkg.PES_packet_length == 0 ||
  258. bs.RemainBits() <= int(pkg.PES_packet_length-uint16(loc))*8 {
  259. pkg.Pes_payload = bs.RemainData()
  260. bs.SkipBits(bs.RemainBits())
  261. } else {
  262. pkg.Pes_payload = bs.RemainData()[:pkg.PES_packet_length-uint16(loc)]
  263. bs.SkipBits(int(pkg.PES_packet_length-uint16(loc)) * 8)
  264. }
  265. return nil
  266. }
  267. func (pkg *PesPacket) Encode(bsw *codec.BitStreamWriter) {
  268. bsw.PutBytes([]byte{0x00, 0x00, 0x01})
  269. bsw.PutByte(pkg.Stream_id)
  270. bsw.PutUint16(pkg.PES_packet_length, 16)
  271. bsw.PutUint8(0x02, 2)
  272. bsw.PutUint8(pkg.PES_scrambling_control, 2)
  273. bsw.PutUint8(pkg.PES_priority, 1)
  274. bsw.PutUint8(pkg.Data_alignment_indicator, 1)
  275. bsw.PutUint8(pkg.Copyright, 1)
  276. bsw.PutUint8(pkg.Original_or_copy, 1)
  277. bsw.PutUint8(pkg.PTS_DTS_flags, 2)
  278. bsw.PutUint8(pkg.ESCR_flag, 1)
  279. bsw.PutUint8(pkg.ES_rate_flag, 1)
  280. bsw.PutUint8(pkg.DSM_trick_mode_flag, 1)
  281. bsw.PutUint8(pkg.Additional_copy_info_flag, 1)
  282. bsw.PutUint8(pkg.PES_CRC_flag, 1)
  283. bsw.PutUint8(pkg.PES_extension_flag, 1)
  284. bsw.PutByte(pkg.PES_header_data_length)
  285. if pkg.PTS_DTS_flags == 0x02 {
  286. bsw.PutUint8(0x02, 4)
  287. bsw.PutUint64(pkg.Pts>>30, 3)
  288. bsw.PutUint8(0x01, 1)
  289. bsw.PutUint64(pkg.Pts>>15, 15)
  290. bsw.PutUint8(0x01, 1)
  291. bsw.PutUint64(pkg.Pts, 15)
  292. bsw.PutUint8(0x01, 1)
  293. }
  294. if pkg.PTS_DTS_flags == 0x03 {
  295. bsw.PutUint8(0x03, 4)
  296. bsw.PutUint64(pkg.Pts>>30, 3)
  297. bsw.PutUint8(0x01, 1)
  298. bsw.PutUint64(pkg.Pts>>15, 15)
  299. bsw.PutUint8(0x01, 1)
  300. bsw.PutUint64(pkg.Pts, 15)
  301. bsw.PutUint8(0x01, 1)
  302. bsw.PutUint8(0x01, 4)
  303. bsw.PutUint64(pkg.Dts>>30, 3)
  304. bsw.PutUint8(0x01, 1)
  305. bsw.PutUint64(pkg.Dts>>15, 15)
  306. bsw.PutUint8(0x01, 1)
  307. bsw.PutUint64(pkg.Dts, 15)
  308. bsw.PutUint8(0x01, 1)
  309. }
  310. if pkg.ESCR_flag == 1 {
  311. bsw.PutUint8(0x03, 2)
  312. bsw.PutUint64(pkg.ESCR_base>>30, 3)
  313. bsw.PutUint8(0x01, 1)
  314. bsw.PutUint64(pkg.ESCR_base>>15, 15)
  315. bsw.PutUint8(0x01, 1)
  316. bsw.PutUint64(pkg.ESCR_base, 15)
  317. bsw.PutUint8(0x01, 1)
  318. }
  319. bsw.PutBytes(pkg.Pes_payload)
  320. }