mpegps_pes.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package mpegps
  2. import (
  3. "io"
  4. )
  5. func (es *MpegPsEsStream) parsePESPacket(payload []byte) (result MpegPsEsStream, err error) {
  6. if len(payload) < 4 {
  7. err = io.ErrShortBuffer
  8. return
  9. }
  10. //data_alignment_indicator := (payload[0]&0b0001_0000)>>4 == 1
  11. flag := payload[1]
  12. ptsFlag := flag>>7 == 1
  13. dtsFlag := (flag&0b0100_0000)>>6 == 1
  14. pesHeaderDataLen := payload[2]
  15. if len(payload) < int(pesHeaderDataLen) {
  16. err = io.ErrShortBuffer
  17. return
  18. }
  19. payload = payload[3:]
  20. extraData := payload[:pesHeaderDataLen]
  21. pts, dts := es.PTS, es.DTS
  22. if ptsFlag && len(extraData) > 4 {
  23. pts = uint32(extraData[0]&0b0000_1110) << 29
  24. pts |= uint32(extraData[1]) << 22
  25. pts |= uint32(extraData[2]&0b1111_1110) << 14
  26. pts |= uint32(extraData[3]) << 7
  27. pts |= uint32(extraData[4]) >> 1
  28. if dtsFlag && len(extraData) > 9 {
  29. dts = uint32(extraData[5]&0b0000_1110) << 29
  30. dts |= uint32(extraData[6]) << 22
  31. dts |= uint32(extraData[7]&0b1111_1110) << 14
  32. dts |= uint32(extraData[8]) << 7
  33. dts |= uint32(extraData[9]) >> 1
  34. } else {
  35. dts = pts
  36. }
  37. }
  38. if pts != es.PTS && es.Buffer.CanRead() {
  39. result = *es
  40. // fmt.Println("clone", es.PTS, es.Buffer[4]&0x0f)
  41. es.Buffer = nil
  42. }
  43. es.PTS, es.DTS = pts, dts
  44. // fmt.Println("append", es.PTS, payload[pesHeaderDataLen+4]&0x0f)
  45. es.Buffer.Write(payload[pesHeaderDataLen:])
  46. // es.Buffer = append(es.Buffer, payload[pesHeaderDataLen:]...)
  47. return
  48. }