partition.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package vp8
  5. // Each VP8 frame consists of between 2 and 9 bitstream partitions.
  6. // Each partition is byte-aligned and is independently arithmetic-encoded.
  7. //
  8. // This file implements decoding a partition's bitstream, as specified in
  9. // chapter 7. The implementation follows libwebp's approach instead of the
  10. // specification's reference C implementation. For example, we use a look-up
  11. // table instead of a for loop to recalibrate the encoded range.
  12. var (
  13. lutShift = [127]uint8{
  14. 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  15. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  16. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  17. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  18. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  19. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  20. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  21. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  22. }
  23. lutRangeM1 = [127]uint8{
  24. 127,
  25. 127, 191,
  26. 127, 159, 191, 223,
  27. 127, 143, 159, 175, 191, 207, 223, 239,
  28. 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239, 247,
  29. 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179, 183, 187,
  30. 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251,
  31. 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157,
  32. 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189,
  33. 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221,
  34. 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253,
  35. }
  36. )
  37. // uniformProb represents a 50% probability that the next bit is 0.
  38. const uniformProb = 128
  39. // partition holds arithmetic-coded bits.
  40. type partition struct {
  41. // buf is the input bytes.
  42. buf []byte
  43. // r is how many of buf's bytes have been consumed.
  44. r int
  45. // rangeM1 is range minus 1, where range is in the arithmetic coding sense,
  46. // not the Go language sense.
  47. rangeM1 uint32
  48. // bits and nBits hold those bits shifted out of buf but not yet consumed.
  49. bits uint32
  50. nBits uint8
  51. // unexpectedEOF tells whether we tried to read past buf.
  52. unexpectedEOF bool
  53. }
  54. // init initializes the partition.
  55. func (p *partition) init(buf []byte) {
  56. p.buf = buf
  57. p.r = 0
  58. p.rangeM1 = 254
  59. p.bits = 0
  60. p.nBits = 0
  61. p.unexpectedEOF = false
  62. }
  63. // readBit returns the next bit.
  64. func (p *partition) readBit(prob uint8) bool {
  65. if p.nBits < 8 {
  66. if p.r >= len(p.buf) {
  67. p.unexpectedEOF = true
  68. return false
  69. }
  70. // Expression split for 386 compiler.
  71. x := uint32(p.buf[p.r])
  72. p.bits |= x << (8 - p.nBits)
  73. p.r++
  74. p.nBits += 8
  75. }
  76. split := (p.rangeM1*uint32(prob))>>8 + 1
  77. bit := p.bits >= split<<8
  78. if bit {
  79. p.rangeM1 -= split
  80. p.bits -= split << 8
  81. } else {
  82. p.rangeM1 = split - 1
  83. }
  84. if p.rangeM1 < 127 {
  85. shift := lutShift[p.rangeM1]
  86. p.rangeM1 = uint32(lutRangeM1[p.rangeM1])
  87. p.bits <<= shift
  88. p.nBits -= shift
  89. }
  90. return bit
  91. }
  92. // readUint returns the next n-bit unsigned integer.
  93. func (p *partition) readUint(prob, n uint8) uint32 {
  94. var u uint32
  95. for n > 0 {
  96. n--
  97. if p.readBit(prob) {
  98. u |= 1 << n
  99. }
  100. }
  101. return u
  102. }
  103. // readInt returns the next n-bit signed integer.
  104. func (p *partition) readInt(prob, n uint8) int32 {
  105. u := p.readUint(prob, n)
  106. b := p.readBit(prob)
  107. if b {
  108. return -int32(u)
  109. }
  110. return int32(u)
  111. }
  112. // readOptionalInt returns the next n-bit signed integer in an encoding
  113. // where the likely result is zero.
  114. func (p *partition) readOptionalInt(prob, n uint8) int32 {
  115. if !p.readBit(prob) {
  116. return 0
  117. }
  118. return p.readInt(prob, n)
  119. }