ps-demuxer.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package mpeg2
  2. import (
  3. "github.com/yapingcat/gomedia/codec"
  4. )
  5. type psstream struct {
  6. sid uint8
  7. cid PS_STREAM_TYPE
  8. pts uint64
  9. dts uint64
  10. streamBuf []byte
  11. }
  12. func newpsstream(sid uint8, cid PS_STREAM_TYPE) *psstream {
  13. return &psstream{
  14. sid: sid,
  15. cid: cid,
  16. streamBuf: make([]byte, 0, 4096),
  17. }
  18. }
  19. type PSDemuxer struct {
  20. streamMap map[uint8]*psstream
  21. pkg *PSPacket
  22. mpeg1 bool
  23. cache []byte
  24. OnFrame func(frame []byte, cid PS_STREAM_TYPE, pts uint64, dts uint64)
  25. //解ps包过程中,解码回调psm,system header,pes包等
  26. //decodeResult 解码ps包时的产生的错误
  27. //这个回调主要用于debug,查看是否ps包存在问题
  28. OnPacket func(pkg Display, decodeResult error)
  29. }
  30. func NewPSDemuxer() *PSDemuxer {
  31. return &PSDemuxer{
  32. streamMap: make(map[uint8]*psstream),
  33. pkg: new(PSPacket),
  34. cache: make([]byte, 0, 256),
  35. OnFrame: nil,
  36. OnPacket: nil,
  37. }
  38. }
  39. func (psdemuxer *PSDemuxer) Input(data []byte) error {
  40. var bs *codec.BitStream
  41. if len(psdemuxer.cache) > 0 {
  42. psdemuxer.cache = append(psdemuxer.cache, data...)
  43. bs = codec.NewBitStream(psdemuxer.cache)
  44. } else {
  45. bs = codec.NewBitStream(data)
  46. }
  47. saveReseved := func() {
  48. tmpcache := make([]byte, bs.RemainBytes())
  49. copy(tmpcache, bs.RemainData())
  50. psdemuxer.cache = tmpcache
  51. }
  52. var ret error = nil
  53. for !bs.EOS() {
  54. if mpegerr, ok := ret.(Error); ok {
  55. if mpegerr.NeedMore() {
  56. saveReseved()
  57. }
  58. break
  59. }
  60. if bs.RemainBits() < 32 {
  61. ret = errNeedMore
  62. saveReseved()
  63. break
  64. }
  65. prefix_code := bs.NextBits(32)
  66. switch prefix_code {
  67. case 0x000001BA: //pack header
  68. if psdemuxer.pkg.Header == nil {
  69. psdemuxer.pkg.Header = new(PSPackHeader)
  70. }
  71. ret = psdemuxer.pkg.Header.Decode(bs)
  72. psdemuxer.mpeg1 = psdemuxer.pkg.Header.IsMpeg1
  73. if psdemuxer.OnPacket != nil {
  74. psdemuxer.OnPacket(psdemuxer.pkg.Header, ret)
  75. }
  76. case 0x000001BB: //system header
  77. if psdemuxer.pkg.Header == nil {
  78. panic("psdemuxer.pkg.Header must not be nil")
  79. }
  80. if psdemuxer.pkg.System == nil {
  81. psdemuxer.pkg.System = new(System_header)
  82. }
  83. ret = psdemuxer.pkg.System.Decode(bs)
  84. if psdemuxer.OnPacket != nil {
  85. psdemuxer.OnPacket(psdemuxer.pkg.System, ret)
  86. }
  87. case 0x000001BC: //program stream map
  88. if psdemuxer.pkg.Psm == nil {
  89. psdemuxer.pkg.Psm = new(Program_stream_map)
  90. }
  91. if ret = psdemuxer.pkg.Psm.Decode(bs); ret == nil {
  92. for _, streaminfo := range psdemuxer.pkg.Psm.Stream_map {
  93. if _, found := psdemuxer.streamMap[streaminfo.Elementary_stream_id]; !found {
  94. stream := newpsstream(streaminfo.Elementary_stream_id, PS_STREAM_TYPE(streaminfo.Stream_type))
  95. psdemuxer.streamMap[stream.sid] = stream
  96. }
  97. }
  98. }
  99. if psdemuxer.OnPacket != nil {
  100. psdemuxer.OnPacket(psdemuxer.pkg.Psm, ret)
  101. }
  102. case 0x000001BD, 0x000001BE, 0x000001BF, 0x000001F0, 0x000001F1,
  103. 0x000001F2, 0x000001F3, 0x000001F4, 0x000001F5, 0x000001F6,
  104. 0x000001F7, 0x000001F8, 0x000001F9, 0x000001FA, 0x000001FB:
  105. if psdemuxer.pkg.CommPes == nil {
  106. psdemuxer.pkg.CommPes = new(CommonPesPacket)
  107. }
  108. ret = psdemuxer.pkg.CommPes.Decode(bs)
  109. case 0x000001FF: //program stream directory
  110. if psdemuxer.pkg.Psd == nil {
  111. psdemuxer.pkg.Psd = new(Program_stream_directory)
  112. }
  113. ret = psdemuxer.pkg.Psd.Decode(bs)
  114. case 0x000001B9: //MPEG_program_end_code
  115. continue
  116. default:
  117. if prefix_code&0xFFFFFFE0 == 0x000001C0 || prefix_code&0xFFFFFFE0 == 0x000001E0 {
  118. if psdemuxer.pkg.Pes == nil {
  119. psdemuxer.pkg.Pes = NewPesPacket()
  120. }
  121. if psdemuxer.mpeg1 {
  122. ret = psdemuxer.pkg.Pes.DecodeMpeg1(bs)
  123. } else {
  124. ret = psdemuxer.pkg.Pes.Decode(bs)
  125. }
  126. if psdemuxer.OnPacket != nil {
  127. psdemuxer.OnPacket(psdemuxer.pkg.Pes, ret)
  128. }
  129. if ret == nil {
  130. if stream, found := psdemuxer.streamMap[psdemuxer.pkg.Pes.Stream_id]; found {
  131. if psdemuxer.mpeg1 && stream.cid == PS_STREAM_UNKNOW {
  132. psdemuxer.guessCodecid(stream)
  133. }
  134. psdemuxer.demuxPespacket(stream, psdemuxer.pkg.Pes)
  135. } else {
  136. if psdemuxer.mpeg1 {
  137. stream := newpsstream(psdemuxer.pkg.Pes.Stream_id, PS_STREAM_UNKNOW)
  138. psdemuxer.streamMap[stream.sid] = stream
  139. stream.streamBuf = append(stream.streamBuf, psdemuxer.pkg.Pes.Pes_payload...)
  140. stream.pts = psdemuxer.pkg.Pes.Pts
  141. stream.dts = psdemuxer.pkg.Pes.Dts
  142. }
  143. }
  144. }
  145. } else {
  146. bs.SkipBits(8)
  147. }
  148. }
  149. }
  150. if ret == nil && len(psdemuxer.cache) > 0 {
  151. psdemuxer.cache = nil
  152. }
  153. return ret
  154. }
  155. func (psdemuxer *PSDemuxer) Flush() {
  156. for _, stream := range psdemuxer.streamMap {
  157. if len(stream.streamBuf) == 0 {
  158. continue
  159. }
  160. if psdemuxer.OnFrame != nil {
  161. psdemuxer.OnFrame(stream.streamBuf, stream.cid, stream.pts/90, stream.dts/90)
  162. }
  163. }
  164. }
  165. func (psdemuxer *PSDemuxer) guessCodecid(stream *psstream) {
  166. if stream.sid&0xE0 == uint8(PES_STREAM_AUDIO) {
  167. stream.cid = PS_STREAM_AAC
  168. } else if stream.sid&0xE0 == uint8(PES_STREAM_VIDEO) {
  169. h264score := 0
  170. h265score := 0
  171. codec.SplitFrame(stream.streamBuf, func(nalu []byte) bool {
  172. h264nalutype := codec.H264NaluTypeWithoutStartCode(nalu)
  173. h265nalutype := codec.H265NaluTypeWithoutStartCode(nalu)
  174. if h264nalutype == codec.H264_NAL_PPS ||
  175. h264nalutype == codec.H264_NAL_SPS ||
  176. h264nalutype == codec.H264_NAL_I_SLICE {
  177. h264score += 2
  178. } else if h264nalutype < 5 {
  179. h264score += 1
  180. } else if h264nalutype > 20 {
  181. h264score -= 1
  182. }
  183. if h265nalutype == codec.H265_NAL_PPS ||
  184. h265nalutype == codec.H265_NAL_SPS ||
  185. h265nalutype == codec.H265_NAL_VPS ||
  186. (h265nalutype >= codec.H265_NAL_SLICE_BLA_W_LP && h265nalutype <= codec.H265_NAL_SLICE_CRA) {
  187. h265score += 2
  188. } else if h265nalutype >= codec.H265_NAL_Slice_TRAIL_N && h265nalutype <= codec.H265_NAL_SLICE_RASL_R {
  189. h265score += 1
  190. } else if h265nalutype > 40 {
  191. h265score -= 1
  192. }
  193. if h264score > h265score && h264score >= 4 {
  194. stream.cid = PS_STREAM_H264
  195. } else if h264score < h265score && h265score >= 4 {
  196. stream.cid = PS_STREAM_H265
  197. }
  198. return true
  199. })
  200. }
  201. }
  202. func (psdemuxer *PSDemuxer) demuxPespacket(stream *psstream, pes *PesPacket) error {
  203. switch stream.cid {
  204. case PS_STREAM_AAC, PS_STREAM_G711A, PS_STREAM_G711U:
  205. return psdemuxer.demuxAudio(stream, pes)
  206. case PS_STREAM_H264, PS_STREAM_H265:
  207. return psdemuxer.demuxH26x(stream, pes)
  208. case PS_STREAM_UNKNOW:
  209. if stream.pts != pes.Pts {
  210. stream.streamBuf = nil
  211. }
  212. stream.streamBuf = append(stream.streamBuf, pes.Pes_payload...)
  213. stream.pts = pes.Pts
  214. stream.dts = pes.Dts
  215. }
  216. return nil
  217. }
  218. func (psdemuxer *PSDemuxer) demuxAudio(stream *psstream, pes *PesPacket) error {
  219. if stream.pts != pes.Pts && len(stream.streamBuf) > 0 {
  220. if psdemuxer.OnFrame != nil {
  221. psdemuxer.OnFrame(stream.streamBuf, stream.cid, stream.pts/90, stream.dts/90)
  222. }
  223. stream.streamBuf = stream.streamBuf[:0]
  224. }
  225. stream.streamBuf = append(stream.streamBuf, pes.Pes_payload...)
  226. stream.pts = pes.Pts
  227. stream.dts = pes.Dts
  228. return nil
  229. }
  230. func (psdemuxer *PSDemuxer) demuxH26x(stream *psstream, pes *PesPacket) error {
  231. if len(stream.streamBuf) == 0 {
  232. stream.pts = pes.Pts
  233. stream.dts = pes.Dts
  234. }
  235. stream.streamBuf = append(stream.streamBuf, pes.Pes_payload...)
  236. start, sc := codec.FindStartCode(stream.streamBuf, 0)
  237. for start >= 0 {
  238. end, sc2 := codec.FindStartCode(stream.streamBuf, start+int(sc))
  239. if end < 0 {
  240. break
  241. }
  242. if stream.cid == PS_STREAM_H264 {
  243. naluType := codec.H264NaluType(stream.streamBuf[start:])
  244. if naluType != codec.H264_NAL_AUD {
  245. if psdemuxer.OnFrame != nil {
  246. psdemuxer.OnFrame(stream.streamBuf[start:end], stream.cid, stream.pts/90, stream.dts/90)
  247. }
  248. }
  249. } else if stream.cid == PS_STREAM_H265 {
  250. naluType := codec.H265NaluType(stream.streamBuf[start:])
  251. if naluType != codec.H265_NAL_AUD {
  252. if psdemuxer.OnFrame != nil {
  253. psdemuxer.OnFrame(stream.streamBuf[start:end], stream.cid, stream.pts/90, stream.dts/90)
  254. }
  255. }
  256. }
  257. start = end
  258. sc = sc2
  259. }
  260. stream.streamBuf = stream.streamBuf[start:]
  261. stream.pts = pes.Pts
  262. stream.dts = pes.Dts
  263. return nil
  264. }