huffman.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2009 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 jpeg
  5. import (
  6. "io"
  7. )
  8. // maxCodeLength is the maximum (inclusive) number of bits in a Huffman code.
  9. const maxCodeLength = 16
  10. // maxNCodes is the maximum (inclusive) number of codes in a Huffman tree.
  11. const maxNCodes = 256
  12. // lutSize is the log-2 size of the Huffman decoder's look-up table.
  13. const lutSize = 8
  14. // huffman is a Huffman decoder, specified in section C.
  15. type huffman struct {
  16. // length is the number of codes in the tree.
  17. nCodes int32
  18. // lut is the look-up table for the next lutSize bits in the bit-stream.
  19. // The high 8 bits of the uint16 are the encoded value. The low 8 bits
  20. // are 1 plus the code length, or 0 if the value is too large to fit in
  21. // lutSize bits.
  22. lut [1 << lutSize]uint16
  23. // vals are the decoded values, sorted by their encoding.
  24. vals [maxNCodes]uint8
  25. // minCodes[i] is the minimum code of length i, or -1 if there are no
  26. // codes of that length.
  27. minCodes [maxCodeLength]int32
  28. // maxCodes[i] is the maximum code of length i, or -1 if there are no
  29. // codes of that length.
  30. maxCodes [maxCodeLength]int32
  31. // valsIndices[i] is the index into vals of minCodes[i].
  32. valsIndices [maxCodeLength]int32
  33. }
  34. // errShortHuffmanData means that an unexpected EOF occurred while decoding
  35. // Huffman data.
  36. var errShortHuffmanData = FormatError("short Huffman data")
  37. // ensureNBits reads bytes from the byte buffer to ensure that d.bits.n is at
  38. // least n. For best performance (avoiding function calls inside hot loops),
  39. // the caller is the one responsible for first checking that d.bits.n < n.
  40. func (d *decoder) ensureNBits(n int32) error {
  41. for {
  42. c, err := d.readByteStuffedByte()
  43. if err != nil {
  44. if err == io.EOF {
  45. return errShortHuffmanData
  46. }
  47. return err
  48. }
  49. d.bits.a = d.bits.a<<8 | uint32(c)
  50. d.bits.n += 8
  51. if d.bits.m == 0 {
  52. d.bits.m = 1 << 7
  53. } else {
  54. d.bits.m <<= 8
  55. }
  56. if d.bits.n >= n {
  57. break
  58. }
  59. }
  60. return nil
  61. }
  62. // receiveExtend is the composition of RECEIVE and EXTEND, specified in section
  63. // F.2.2.1.
  64. func (d *decoder) receiveExtend(t uint8) (int32, error) {
  65. if d.bits.n < int32(t) {
  66. if err := d.ensureNBits(int32(t)); err != nil {
  67. return 0, err
  68. }
  69. }
  70. d.bits.n -= int32(t)
  71. d.bits.m >>= t
  72. s := int32(1) << t
  73. x := int32(d.bits.a>>uint8(d.bits.n)) & (s - 1)
  74. if x < s>>1 {
  75. x += ((-1) << t) + 1
  76. }
  77. return x, nil
  78. }
  79. // processDHT processes a Define Huffman Table marker, and initializes a huffman
  80. // struct from its contents. Specified in section B.2.4.2.
  81. func (d *decoder) processDHT(n int) error {
  82. for n > 0 {
  83. if n < 17 {
  84. return FormatError("DHT has wrong length")
  85. }
  86. if err := d.readFull(d.tmp[:17]); err != nil {
  87. return err
  88. }
  89. tc := d.tmp[0] >> 4
  90. if tc > maxTc {
  91. return FormatError("bad Tc value")
  92. }
  93. th := d.tmp[0] & 0x0f
  94. if th > maxTh || !d.progressive && th > 1 {
  95. return FormatError("bad Th value")
  96. }
  97. h := &d.huff[tc][th]
  98. // Read nCodes and h.vals (and derive h.nCodes).
  99. // nCodes[i] is the number of codes with code length i.
  100. // h.nCodes is the total number of codes.
  101. h.nCodes = 0
  102. var nCodes [maxCodeLength]int32
  103. for i := range nCodes {
  104. nCodes[i] = int32(d.tmp[i+1])
  105. h.nCodes += nCodes[i]
  106. }
  107. if h.nCodes == 0 {
  108. return FormatError("Huffman table has zero length")
  109. }
  110. if h.nCodes > maxNCodes {
  111. return FormatError("Huffman table has excessive length")
  112. }
  113. n -= int(h.nCodes) + 17
  114. if n < 0 {
  115. return FormatError("DHT has wrong length")
  116. }
  117. if err := d.readFull(h.vals[:h.nCodes]); err != nil {
  118. return err
  119. }
  120. // Derive the look-up table.
  121. for i := range h.lut {
  122. h.lut[i] = 0
  123. }
  124. var x, code uint32
  125. for i := uint32(0); i < lutSize; i++ {
  126. code <<= 1
  127. for j := int32(0); j < nCodes[i]; j++ {
  128. // The codeLength is 1+i, so shift code by 8-(1+i) to
  129. // calculate the high bits for every 8-bit sequence
  130. // whose codeLength's high bits matches code.
  131. // The high 8 bits of lutValue are the encoded value.
  132. // The low 8 bits are 1 plus the codeLength.
  133. base := uint8(code << (7 - i))
  134. lutValue := uint16(h.vals[x])<<8 | uint16(2+i)
  135. for k := uint8(0); k < 1<<(7-i); k++ {
  136. h.lut[base|k] = lutValue
  137. }
  138. code++
  139. x++
  140. }
  141. }
  142. // Derive minCodes, maxCodes, and valsIndices.
  143. var c, index int32
  144. for i, n := range nCodes {
  145. if n == 0 {
  146. h.minCodes[i] = -1
  147. h.maxCodes[i] = -1
  148. h.valsIndices[i] = -1
  149. } else {
  150. h.minCodes[i] = c
  151. h.maxCodes[i] = c + n - 1
  152. h.valsIndices[i] = index
  153. c += n
  154. index += n
  155. }
  156. c <<= 1
  157. }
  158. }
  159. return nil
  160. }
  161. // decodeHuffman returns the next Huffman-coded value from the bit-stream,
  162. // decoded according to h.
  163. func (d *decoder) decodeHuffman(h *huffman) (uint8, error) {
  164. if h.nCodes == 0 {
  165. return 0, FormatError("uninitialized Huffman table")
  166. }
  167. if d.bits.n < 8 {
  168. if err := d.ensureNBits(8); err != nil {
  169. if err != errMissingFF00 && err != errShortHuffmanData {
  170. return 0, err
  171. }
  172. // There are no more bytes of data in this segment, but we may still
  173. // be able to read the next symbol out of the previously read bits.
  174. // First, undo the readByte that the ensureNBits call made.
  175. d.unreadByteStuffedByte()
  176. goto slowPath
  177. }
  178. }
  179. if v := h.lut[(d.bits.a>>uint32(d.bits.n-lutSize))&0xff]; v != 0 {
  180. n := (v & 0xff) - 1
  181. d.bits.n -= int32(n)
  182. d.bits.m >>= n
  183. return uint8(v >> 8), nil
  184. }
  185. slowPath:
  186. for i, code := 0, int32(0); i < maxCodeLength; i++ {
  187. if d.bits.n == 0 {
  188. if err := d.ensureNBits(1); err != nil {
  189. return 0, err
  190. }
  191. }
  192. if d.bits.a&d.bits.m != 0 {
  193. code |= 1
  194. }
  195. d.bits.n--
  196. d.bits.m >>= 1
  197. if code <= h.maxCodes[i] {
  198. return h.vals[h.valsIndices[i]+code-h.minCodes[i]], nil
  199. }
  200. code <<= 1
  201. }
  202. return 0, FormatError("bad Huffman code")
  203. }
  204. func (d *decoder) decodeBit() (bool, error) {
  205. if d.bits.n == 0 {
  206. if err := d.ensureNBits(1); err != nil {
  207. return false, err
  208. }
  209. }
  210. ret := d.bits.a&d.bits.m != 0
  211. d.bits.n--
  212. d.bits.m >>= 1
  213. return ret, nil
  214. }
  215. func (d *decoder) decodeBits(n int32) (uint32, error) {
  216. if d.bits.n < n {
  217. if err := d.ensureNBits(n); err != nil {
  218. return 0, err
  219. }
  220. }
  221. ret := d.bits.a >> uint32(d.bits.n-n)
  222. ret &= (1 << uint32(n)) - 1
  223. d.bits.n -= n
  224. d.bits.m >>= uint32(n)
  225. return ret, nil
  226. }