bitstream.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package codec
  2. import (
  3. "encoding/binary"
  4. )
  5. var BitMask [8]byte = [8]byte{0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF}
  6. type BitStream struct {
  7. bits []byte
  8. bytesOffset int
  9. bitsOffset int
  10. bitsmark int
  11. bytemark int
  12. }
  13. func NewBitStream(buf []byte) *BitStream {
  14. return &BitStream{
  15. bits: buf,
  16. bytesOffset: 0,
  17. bitsOffset: 0,
  18. bitsmark: 0,
  19. bytemark: 0,
  20. }
  21. }
  22. func (bs *BitStream) Uint8(n int) uint8 {
  23. return uint8(bs.GetBits(n))
  24. }
  25. func (bs *BitStream) Uint16(n int) uint16 {
  26. return uint16(bs.GetBits(n))
  27. }
  28. func (bs *BitStream) Uint32(n int) uint32 {
  29. return uint32(bs.GetBits(n))
  30. }
  31. func (bs *BitStream) GetBytes(n int) []byte {
  32. if bs.bytesOffset+n > len(bs.bits) {
  33. panic("OUT OF RANGE")
  34. }
  35. if bs.bitsOffset != 0 {
  36. panic("invaild operation")
  37. }
  38. data := make([]byte, n)
  39. copy(data, bs.bits[bs.bytesOffset:bs.bytesOffset+n])
  40. bs.bytesOffset += n
  41. return data
  42. }
  43. //n <= 64
  44. func (bs *BitStream) GetBits(n int) uint64 {
  45. if bs.bytesOffset >= len(bs.bits) {
  46. panic("OUT OF RANGE")
  47. }
  48. var ret uint64 = 0
  49. if 8-bs.bitsOffset >= n {
  50. ret = uint64((bs.bits[bs.bytesOffset] >> (8 - bs.bitsOffset - n)) & BitMask[n-1])
  51. bs.bitsOffset += n
  52. if bs.bitsOffset == 8 {
  53. bs.bytesOffset++
  54. bs.bitsOffset = 0
  55. }
  56. } else {
  57. ret = uint64(bs.bits[bs.bytesOffset] & BitMask[8-bs.bitsOffset-1])
  58. bs.bytesOffset++
  59. n -= 8 - bs.bitsOffset
  60. bs.bitsOffset = 0
  61. for n > 0 {
  62. if bs.bytesOffset >= len(bs.bits) {
  63. panic("OUT OF RANGE")
  64. }
  65. if n >= 8 {
  66. ret = ret<<8 | uint64(bs.bits[bs.bytesOffset])
  67. bs.bytesOffset++
  68. n -= 8
  69. } else {
  70. ret = (ret << n) | uint64((bs.bits[bs.bytesOffset]>>(8-n))&BitMask[n-1])
  71. bs.bitsOffset = n
  72. break
  73. }
  74. }
  75. }
  76. return ret
  77. }
  78. func (bs *BitStream) GetBit() uint8 {
  79. if bs.bytesOffset >= len(bs.bits) {
  80. panic("OUT OF RANGE")
  81. }
  82. ret := bs.bits[bs.bytesOffset] >> (7 - bs.bitsOffset) & 0x01
  83. bs.bitsOffset++
  84. if bs.bitsOffset >= 8 {
  85. bs.bytesOffset++
  86. bs.bitsOffset = 0
  87. }
  88. return ret
  89. }
  90. func (bs *BitStream) SkipBits(n int) {
  91. bytecount := n / 8
  92. bitscount := n % 8
  93. bs.bytesOffset += bytecount
  94. if bs.bitsOffset+bitscount < 8 {
  95. bs.bitsOffset += bitscount
  96. } else {
  97. bs.bytesOffset += 1
  98. bs.bitsOffset += bitscount - 8
  99. }
  100. }
  101. func (bs *BitStream) Markdot() {
  102. bs.bitsmark = bs.bitsOffset
  103. bs.bytemark = bs.bytesOffset
  104. }
  105. func (bs *BitStream) DistanceFromMarkDot() int {
  106. bytecount := bs.bytesOffset - bs.bytemark - 1
  107. bitscount := bs.bitsOffset + (8 - bs.bitsmark)
  108. return bytecount*8 + bitscount
  109. }
  110. func (bs *BitStream) RemainBytes() int {
  111. if bs.bitsOffset > 0 {
  112. return len(bs.bits) - bs.bytesOffset - 1
  113. } else {
  114. return len(bs.bits) - bs.bytesOffset
  115. }
  116. }
  117. func (bs *BitStream) RemainBits() int {
  118. if bs.bitsOffset > 0 {
  119. return bs.RemainBytes()*8 + 8 - bs.bitsOffset
  120. } else {
  121. return bs.RemainBytes() * 8
  122. }
  123. }
  124. func (bs *BitStream) Bits() []byte {
  125. return bs.bits
  126. }
  127. func (bs *BitStream) RemainData() []byte {
  128. return bs.bits[bs.bytesOffset:]
  129. }
  130. //无符号哥伦布熵编码
  131. func (bs *BitStream) ReadUE() uint64 {
  132. leadingZeroBits := 0
  133. for bs.GetBit() == 0 {
  134. leadingZeroBits++
  135. }
  136. if leadingZeroBits == 0 {
  137. return 0
  138. }
  139. info := bs.GetBits(leadingZeroBits)
  140. return uint64(1)<<leadingZeroBits - 1 + info
  141. }
  142. //有符号哥伦布熵编码
  143. func (bs *BitStream) ReadSE() int64 {
  144. v := bs.ReadUE()
  145. if v%2 == 0 {
  146. return -1 * int64(v/2)
  147. } else {
  148. return int64(v+1) / 2
  149. }
  150. }
  151. func (bs *BitStream) ByteOffset() int {
  152. return bs.bytesOffset
  153. }
  154. func (bs *BitStream) UnRead(n int) {
  155. if n-bs.bitsOffset <= 0 {
  156. bs.bitsOffset -= n
  157. } else {
  158. least := n - bs.bitsOffset
  159. for least >= 8 {
  160. bs.bytesOffset--
  161. least -= 8
  162. }
  163. if least > 0 {
  164. bs.bytesOffset--
  165. bs.bitsOffset = 8 - least
  166. }
  167. }
  168. }
  169. func (bs *BitStream) NextBits(n int) uint64 {
  170. r := bs.GetBits(n)
  171. bs.UnRead(n)
  172. return r
  173. }
  174. func (bs *BitStream) EOS() bool {
  175. return bs.bytesOffset == len(bs.bits) && bs.bitsOffset == 0
  176. }
  177. type BitStreamWriter struct {
  178. bits []byte
  179. byteoffset int
  180. bitsoffset int
  181. bitsmark int
  182. bytemark int
  183. }
  184. func NewBitStreamWriter(n int) *BitStreamWriter {
  185. return &BitStreamWriter{
  186. bits: make([]byte, n),
  187. byteoffset: 0,
  188. bitsoffset: 0,
  189. bitsmark: 0,
  190. bytemark: 0,
  191. }
  192. }
  193. func (bsw *BitStreamWriter) expandSpace(n int) {
  194. if (len(bsw.bits)-bsw.byteoffset-1)*8+8-bsw.bitsoffset < n {
  195. newlen := 0
  196. if len(bsw.bits)*8 < n {
  197. newlen = len(bsw.bits) + n/8 + 1
  198. } else {
  199. newlen = len(bsw.bits) * 2
  200. }
  201. tmp := make([]byte, newlen)
  202. copy(tmp, bsw.bits)
  203. bsw.bits = tmp
  204. }
  205. }
  206. func (bsw *BitStreamWriter) ByteOffset() int {
  207. return bsw.byteoffset
  208. }
  209. func (bsw *BitStreamWriter) BitOffset() int {
  210. return bsw.bitsoffset
  211. }
  212. func (bsw *BitStreamWriter) Markdot() {
  213. bsw.bitsmark = bsw.bitsoffset
  214. bsw.bytemark = bsw.byteoffset
  215. }
  216. func (bsw *BitStreamWriter) DistanceFromMarkDot() int {
  217. bytecount := bsw.byteoffset - bsw.bytemark - 1
  218. bitscount := bsw.bitsoffset + (8 - bsw.bitsmark)
  219. return bytecount*8 + bitscount
  220. }
  221. func (bsw *BitStreamWriter) PutByte(v byte) {
  222. bsw.expandSpace(8)
  223. if bsw.bitsoffset == 0 {
  224. bsw.bits[bsw.byteoffset] = v
  225. bsw.byteoffset++
  226. } else {
  227. bsw.bits[bsw.byteoffset] |= v >> byte(bsw.bitsoffset)
  228. bsw.byteoffset++
  229. bsw.bits[bsw.byteoffset] = v & BitMask[bsw.bitsoffset-1]
  230. }
  231. }
  232. func (bsw *BitStreamWriter) PutBytes(v []byte) {
  233. if bsw.bitsoffset != 0 {
  234. panic("bsw.bitsoffset > 0")
  235. }
  236. bsw.expandSpace(8 * len(v))
  237. copy(bsw.bits[bsw.byteoffset:], v)
  238. bsw.byteoffset += len(v)
  239. }
  240. func (bsw *BitStreamWriter) PutRepetValue(v byte, n int) {
  241. if bsw.bitsoffset != 0 {
  242. panic("bsw.bitsoffset > 0")
  243. }
  244. bsw.expandSpace(8 * n)
  245. for i := 0; i < n; i++ {
  246. bsw.bits[bsw.byteoffset] = v
  247. bsw.byteoffset++
  248. }
  249. }
  250. func (bsw *BitStreamWriter) PutUint8(v uint8, n int) {
  251. bsw.PutUint64(uint64(v), n)
  252. }
  253. func (bsw *BitStreamWriter) PutUint16(v uint16, n int) {
  254. bsw.PutUint64(uint64(v), n)
  255. }
  256. func (bsw *BitStreamWriter) PutUint32(v uint32, n int) {
  257. bsw.PutUint64(uint64(v), n)
  258. }
  259. func (bsw *BitStreamWriter) PutUint64(v uint64, n int) {
  260. bsw.expandSpace(n)
  261. if 8-bsw.bitsoffset >= n {
  262. bsw.bits[bsw.byteoffset] |= uint8(v) & BitMask[n-1] << (8 - bsw.bitsoffset - n)
  263. bsw.bitsoffset += n
  264. if bsw.bitsoffset == 8 {
  265. bsw.bitsoffset = 0
  266. bsw.byteoffset++
  267. }
  268. } else {
  269. bsw.bits[bsw.byteoffset] |= uint8(v>>(n-int(8-bsw.bitsoffset))) & BitMask[8-bsw.bitsoffset-1]
  270. bsw.byteoffset++
  271. n -= 8 - bsw.bitsoffset
  272. for n-8 >= 0 {
  273. bsw.bits[bsw.byteoffset] = uint8(v>>(n-8)) & 0xFF
  274. bsw.byteoffset++
  275. n -= 8
  276. }
  277. bsw.bitsoffset = n
  278. if n > 0 {
  279. bsw.bits[bsw.byteoffset] |= (uint8(v) & BitMask[n-1]) << (8 - n)
  280. }
  281. }
  282. }
  283. func (bsw *BitStreamWriter) SetByte(v byte, where int) {
  284. bsw.bits[where] = v
  285. }
  286. func (bsw *BitStreamWriter) SetUint16(v uint16, where int) {
  287. binary.BigEndian.PutUint16(bsw.bits[where:where+2], v)
  288. }
  289. func (bsw *BitStreamWriter) Bits() []byte {
  290. if bsw.byteoffset == len(bsw.bits) {
  291. return bsw.bits
  292. }
  293. if bsw.bitsoffset > 0 {
  294. return bsw.bits[0 : bsw.byteoffset+1]
  295. } else {
  296. return bsw.bits[0:bsw.byteoffset]
  297. }
  298. }
  299. //用v 填充剩余字节
  300. func (bsw *BitStreamWriter) FillRemainData(v byte) {
  301. for i := bsw.byteoffset; i < len(bsw.bits); i++ {
  302. bsw.bits[i] = v
  303. }
  304. bsw.byteoffset = len(bsw.bits)
  305. bsw.bitsoffset = 0
  306. }
  307. func (bsw *BitStreamWriter) Reset() {
  308. for i := 0; i < len(bsw.bits); i++ {
  309. bsw.bits[i] = 0
  310. }
  311. bsw.bitsmark = 0
  312. bsw.bytemark = 0
  313. bsw.bitsoffset = 0
  314. bsw.byteoffset = 0
  315. }