reader.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package bits
  2. type Reader struct {
  3. EOF bool // if end of buffer raised during reading
  4. buf []byte // total buf
  5. byte byte // current byte
  6. bits byte // bits left in byte
  7. pos int // current pos in buf
  8. }
  9. func NewReader(b []byte) *Reader {
  10. return &Reader{buf: b}
  11. }
  12. //goland:noinspection GoStandardMethods
  13. func (r *Reader) ReadByte() byte {
  14. if r.bits != 0 {
  15. return r.ReadBits8(8)
  16. }
  17. if r.pos >= len(r.buf) {
  18. r.EOF = true
  19. return 0
  20. }
  21. b := r.buf[r.pos]
  22. r.pos++
  23. return b
  24. }
  25. func (r *Reader) ReadUint16() uint16 {
  26. if r.bits != 0 {
  27. return r.ReadBits16(16)
  28. }
  29. return uint16(r.ReadByte())<<8 | uint16(r.ReadByte())
  30. }
  31. func (r *Reader) ReadUint24() uint32 {
  32. if r.bits != 0 {
  33. return r.ReadBits(24)
  34. }
  35. return uint32(r.ReadByte())<<16 | uint32(r.ReadByte())<<8 | uint32(r.ReadByte())
  36. }
  37. func (r *Reader) ReadUint32() uint32 {
  38. if r.bits != 0 {
  39. return r.ReadBits(32)
  40. }
  41. return uint32(r.ReadByte())<<24 | uint32(r.ReadByte())<<16 | uint32(r.ReadByte())<<8 | uint32(r.ReadByte())
  42. }
  43. func (r *Reader) ReadBit() byte {
  44. if r.bits == 0 {
  45. r.byte = r.ReadByte()
  46. r.bits = 7
  47. } else {
  48. r.bits--
  49. }
  50. return (r.byte >> r.bits) & 0b1
  51. }
  52. func (r *Reader) ReadBits(n byte) (res uint32) {
  53. for i := n - 1; i != 255; i-- {
  54. res |= uint32(r.ReadBit()) << i
  55. }
  56. return
  57. }
  58. func (r *Reader) ReadBits8(n byte) (res uint8) {
  59. for i := n - 1; i != 255; i-- {
  60. res |= r.ReadBit() << i
  61. }
  62. return
  63. }
  64. func (r *Reader) ReadBits16(n byte) (res uint16) {
  65. for i := n - 1; i != 255; i-- {
  66. res |= uint16(r.ReadBit()) << i
  67. }
  68. return
  69. }
  70. func (r *Reader) ReadBits64(n byte) (res uint64) {
  71. for i := n - 1; i != 255; i-- {
  72. res |= uint64(r.ReadBit()) << i
  73. }
  74. return
  75. }
  76. func (r *Reader) ReadBytes(n int) (b []byte) {
  77. if r.bits == 0 {
  78. if r.pos+n > len(r.buf) {
  79. r.EOF = true
  80. return nil
  81. }
  82. b = r.buf[r.pos : r.pos+n]
  83. r.pos += n
  84. } else {
  85. b = make([]byte, n)
  86. for i := 0; i < n; i++ {
  87. b[i] = r.ReadByte()
  88. }
  89. }
  90. return
  91. }
  92. // ReadUEGolomb - ReadExponentialGolomb (unsigned)
  93. func (r *Reader) ReadUEGolomb() uint32 {
  94. var size byte
  95. for size = 0; size < 32; size++ {
  96. if b := r.ReadBit(); b != 0 || r.EOF {
  97. break
  98. }
  99. }
  100. return r.ReadBits(size) + (1 << size) - 1
  101. }
  102. // ReadSEGolomb - ReadSignedExponentialGolomb
  103. func (r *Reader) ReadSEGolomb() int32 {
  104. if b := r.ReadUEGolomb(); b%2 == 0 {
  105. return -int32(b >> 1)
  106. } else {
  107. return int32(b >> 1)
  108. }
  109. }
  110. func (r *Reader) Left() []byte {
  111. return r.buf[r.pos:]
  112. }
  113. func (r *Reader) Pos() (int, byte) {
  114. return r.pos - 1, r.bits
  115. }