2
0

h265reader.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Package h265reader implements a H265 Annex-B Reader
  2. package gb28181
  3. import (
  4. "bytes"
  5. "errors"
  6. "io"
  7. )
  8. type NalUnitType uint8
  9. // Enums for NalUnitTypes
  10. const (
  11. NaluTypeSliceTrailN NalUnitType = 0 // 0x0
  12. NaluTypeSliceTrailR NalUnitType = 1 // 0x01
  13. NaluTypeSliceTsaN NalUnitType = 2 // 0x02
  14. NaluTypeSliceTsaR NalUnitType = 3 // 0x03
  15. NaluTypeSliceStsaN NalUnitType = 4 // 0x04
  16. NaluTypeSliceStsaR NalUnitType = 5 // 0x05
  17. NaluTypeSliceRadlN NalUnitType = 6 // 0x06
  18. NaluTypeSliceRadlR NalUnitType = 7 // 0x07
  19. NaluTypeSliceRaslN NalUnitType = 8 // 0x06
  20. NaluTypeSliceRaslR NalUnitType = 9 // 0x09
  21. NaluTypeSliceBlaWlp NalUnitType = 16 // 0x10
  22. NaluTypeSliceBlaWradl NalUnitType = 17 // 0x11
  23. NaluTypeSliceBlaNlp NalUnitType = 18 // 0x12
  24. NaluTypeSliceIdr NalUnitType = 19 // 0x13
  25. NaluTypeSliceIdrNlp NalUnitType = 20 // 0x14
  26. NaluTypeSliceCranut NalUnitType = 21 // 0x15
  27. NaluTypeSliceRsvIrapVcl22 NalUnitType = 22 // 0x16
  28. NaluTypeSliceRsvIrapVcl23 NalUnitType = 23 // 0x17
  29. NaluTypeVps NalUnitType = 32 // 0x20
  30. NaluTypeSps NalUnitType = 33 // 0x21
  31. NaluTypePps NalUnitType = 34 // 0x22
  32. NaluTypeAud NalUnitType = 35 // 0x23
  33. NaluTypeSei NalUnitType = 39 // 0x27
  34. NaluTypeSeiSuffix NalUnitType = 40 // 0x28
  35. NaluTypeUnspecified NalUnitType = 48 // 0x30
  36. )
  37. // H265Reader reads data from stream and constructs h265 nal units
  38. type H265Reader struct {
  39. stream io.Reader
  40. nalBuffer []byte
  41. countOfConsecutiveZeroBytes int
  42. nalPrefixParsed bool
  43. readBuffer []byte
  44. }
  45. var (
  46. errNilReader = errors.New("stream is nil")
  47. errDataIsNotH265Stream = errors.New("data is not a H265 bitstream")
  48. )
  49. // NewReader creates new H265Reader
  50. func NewReader(in io.Reader) (*H265Reader, error) {
  51. if in == nil {
  52. return nil, errNilReader
  53. }
  54. reader := &H265Reader{
  55. stream: in,
  56. nalBuffer: make([]byte, 0),
  57. nalPrefixParsed: false,
  58. readBuffer: make([]byte, 0),
  59. }
  60. return reader, nil
  61. }
  62. // NAL H.265 Network Abstraction Layer
  63. type NAL struct {
  64. PictureOrderCount uint32
  65. // NAL header
  66. ForbiddenZeroBit bool
  67. UnitType NalUnitType
  68. NuhLayerId uint8
  69. NuhTemporalIdPlus1 uint8
  70. Data []byte // header byte + rbsp
  71. }
  72. func (reader *H265Reader) read(numToRead int) (data []byte) {
  73. for len(reader.readBuffer) < numToRead {
  74. buf := make([]byte, 4096)
  75. n, err := reader.stream.Read(buf)
  76. if n == 0 || err != nil {
  77. break
  78. }
  79. buf = buf[0:n]
  80. reader.readBuffer = append(reader.readBuffer, buf...)
  81. }
  82. var numShouldRead int
  83. if numToRead <= len(reader.readBuffer) {
  84. numShouldRead = numToRead
  85. } else {
  86. numShouldRead = len(reader.readBuffer)
  87. }
  88. data = reader.readBuffer[0:numShouldRead]
  89. reader.readBuffer = reader.readBuffer[numShouldRead:]
  90. return data
  91. }
  92. func (reader *H265Reader) bitStreamStartsWithH265Prefix() (prefixLength int, e error) {
  93. nalPrefix3Bytes := []byte{0, 0, 1}
  94. nalPrefix4Bytes := []byte{0, 0, 0, 1}
  95. prefixBuffer := reader.read(4)
  96. n := len(prefixBuffer)
  97. if n == 0 {
  98. return 0, io.EOF
  99. }
  100. if n < 3 {
  101. return 0, errDataIsNotH265Stream
  102. }
  103. nalPrefix3BytesFound := bytes.Equal(nalPrefix3Bytes, prefixBuffer[:3])
  104. if n == 3 {
  105. if nalPrefix3BytesFound {
  106. return 0, io.EOF
  107. }
  108. return 0, errDataIsNotH265Stream
  109. }
  110. // n == 4
  111. if nalPrefix3BytesFound {
  112. reader.nalBuffer = append(reader.nalBuffer, prefixBuffer[3])
  113. return 3, nil
  114. }
  115. nalPrefix4BytesFound := bytes.Equal(nalPrefix4Bytes, prefixBuffer)
  116. if nalPrefix4BytesFound {
  117. return 4, nil
  118. }
  119. return 0, errDataIsNotH265Stream
  120. }
  121. // NextNAL reads from stream and returns then next NAL,
  122. // and an error if there is incomplete frame data.
  123. // Returns all nil values when no more NALs are available.
  124. func (reader *H265Reader) NextNAL() (*NAL, error) {
  125. if !reader.nalPrefixParsed {
  126. _, err := reader.bitStreamStartsWithH265Prefix()
  127. if err != nil {
  128. return nil, err
  129. }
  130. reader.nalPrefixParsed = true
  131. }
  132. for {
  133. buffer := reader.read(1)
  134. n := len(buffer)
  135. if n != 1 {
  136. break
  137. }
  138. readByte := buffer[0]
  139. nalFound := reader.processByte(readByte)
  140. if nalFound {
  141. nal := newNal(reader.nalBuffer)
  142. nal.parseHeader()
  143. if nal.UnitType == NaluTypeSeiSuffix || nal.UnitType == NaluTypeSei {
  144. reader.nalBuffer = nil
  145. continue
  146. } else {
  147. break
  148. }
  149. }
  150. reader.nalBuffer = append(reader.nalBuffer, readByte)
  151. }
  152. if len(reader.nalBuffer) == 0 {
  153. return nil, io.EOF
  154. }
  155. nal := newNal(reader.nalBuffer)
  156. reader.nalBuffer = nil
  157. nal.parseHeader()
  158. return nal, nil
  159. }
  160. func (reader *H265Reader) processByte(readByte byte) (nalFound bool) {
  161. nalFound = false
  162. switch readByte {
  163. case 0:
  164. reader.countOfConsecutiveZeroBytes++
  165. case 1:
  166. if reader.countOfConsecutiveZeroBytes >= 2 {
  167. countOfConsecutiveZeroBytesInPrefix := 2
  168. if reader.countOfConsecutiveZeroBytes > 2 {
  169. countOfConsecutiveZeroBytesInPrefix = 3
  170. }
  171. nalUnitLength := len(reader.nalBuffer) - countOfConsecutiveZeroBytesInPrefix
  172. reader.nalBuffer = reader.nalBuffer[0:nalUnitLength]
  173. reader.countOfConsecutiveZeroBytes = 0
  174. nalFound = true
  175. } else {
  176. reader.countOfConsecutiveZeroBytes = 0
  177. }
  178. default:
  179. reader.countOfConsecutiveZeroBytes = 0
  180. }
  181. return nalFound
  182. }
  183. func newNal(data []byte) *NAL {
  184. return &NAL{PictureOrderCount: 0, ForbiddenZeroBit: false, UnitType: NaluTypeUnspecified, Data: data}
  185. }
  186. func (h *NAL) parseHeader() {
  187. firstByte := h.Data[0]
  188. h.ForbiddenZeroBit = (((firstByte & 0x80) >> 7) == 1) // 0x80 = 0b10000000
  189. h.UnitType = NalUnitType((firstByte & 0x7E) >> 1) // 0x1F = 0b01111110
  190. }