compress_fragment.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. package brotli
  2. import "encoding/binary"
  3. /* Copyright 2015 Google Inc. All Rights Reserved.
  4. Distributed under MIT license.
  5. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  6. */
  7. /* Function for fast encoding of an input fragment, independently from the input
  8. history. This function uses one-pass processing: when we find a backward
  9. match, we immediately emit the corresponding command and literal codes to
  10. the bit stream.
  11. Adapted from the CompressFragment() function in
  12. https://github.com/google/snappy/blob/master/snappy.cc */
  13. const maxDistance_compress_fragment = 262128
  14. func hash5(p []byte, shift uint) uint32 {
  15. var h uint64 = (binary.LittleEndian.Uint64(p) << 24) * uint64(kHashMul32)
  16. return uint32(h >> shift)
  17. }
  18. func hashBytesAtOffset5(v uint64, offset int, shift uint) uint32 {
  19. assert(offset >= 0)
  20. assert(offset <= 3)
  21. {
  22. var h uint64 = ((v >> uint(8*offset)) << 24) * uint64(kHashMul32)
  23. return uint32(h >> shift)
  24. }
  25. }
  26. func isMatch5(p1 []byte, p2 []byte) bool {
  27. return binary.LittleEndian.Uint32(p1) == binary.LittleEndian.Uint32(p2) &&
  28. p1[4] == p2[4]
  29. }
  30. /* Builds a literal prefix code into "depths" and "bits" based on the statistics
  31. of the "input" string and stores it into the bit stream.
  32. Note that the prefix code here is built from the pre-LZ77 input, therefore
  33. we can only approximate the statistics of the actual literal stream.
  34. Moreover, for long inputs we build a histogram from a sample of the input
  35. and thus have to assign a non-zero depth for each literal.
  36. Returns estimated compression ratio millibytes/char for encoding given input
  37. with generated code. */
  38. func buildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte, bits []uint16, storage_ix *uint, storage []byte) uint {
  39. var histogram = [256]uint32{0}
  40. var histogram_total uint
  41. var i uint
  42. if input_size < 1<<15 {
  43. for i = 0; i < input_size; i++ {
  44. histogram[input[i]]++
  45. }
  46. histogram_total = input_size
  47. for i = 0; i < 256; i++ {
  48. /* We weigh the first 11 samples with weight 3 to account for the
  49. balancing effect of the LZ77 phase on the histogram. */
  50. var adjust uint32 = 2 * brotli_min_uint32_t(histogram[i], 11)
  51. histogram[i] += adjust
  52. histogram_total += uint(adjust)
  53. }
  54. } else {
  55. const kSampleRate uint = 29
  56. for i = 0; i < input_size; i += kSampleRate {
  57. histogram[input[i]]++
  58. }
  59. histogram_total = (input_size + kSampleRate - 1) / kSampleRate
  60. for i = 0; i < 256; i++ {
  61. /* We add 1 to each population count to avoid 0 bit depths (since this is
  62. only a sample and we don't know if the symbol appears or not), and we
  63. weigh the first 11 samples with weight 3 to account for the balancing
  64. effect of the LZ77 phase on the histogram (more frequent symbols are
  65. more likely to be in backward references instead as literals). */
  66. var adjust uint32 = 1 + 2*brotli_min_uint32_t(histogram[i], 11)
  67. histogram[i] += adjust
  68. histogram_total += uint(adjust)
  69. }
  70. }
  71. buildAndStoreHuffmanTreeFast(histogram[:], histogram_total, /* max_bits = */
  72. 8, depths, bits, storage_ix, storage)
  73. {
  74. var literal_ratio uint = 0
  75. for i = 0; i < 256; i++ {
  76. if histogram[i] != 0 {
  77. literal_ratio += uint(histogram[i] * uint32(depths[i]))
  78. }
  79. }
  80. /* Estimated encoding ratio, millibytes per symbol. */
  81. return (literal_ratio * 125) / histogram_total
  82. }
  83. }
  84. /* Builds a command and distance prefix code (each 64 symbols) into "depth" and
  85. "bits" based on "histogram" and stores it into the bit stream. */
  86. func buildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) {
  87. var tree [129]huffmanTree
  88. var cmd_depth = [numCommandSymbols]byte{0}
  89. /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */
  90. var cmd_bits [64]uint16
  91. createHuffmanTree(histogram, 64, 15, tree[:], depth)
  92. createHuffmanTree(histogram[64:], 64, 14, tree[:], depth[64:])
  93. /* We have to jump through a few hoops here in order to compute
  94. the command bits because the symbols are in a different order than in
  95. the full alphabet. This looks complicated, but having the symbols
  96. in this order in the command bits saves a few branches in the Emit*
  97. functions. */
  98. copy(cmd_depth[:], depth[:24])
  99. copy(cmd_depth[24:][:], depth[40:][:8])
  100. copy(cmd_depth[32:][:], depth[24:][:8])
  101. copy(cmd_depth[40:][:], depth[48:][:8])
  102. copy(cmd_depth[48:][:], depth[32:][:8])
  103. copy(cmd_depth[56:][:], depth[56:][:8])
  104. convertBitDepthsToSymbols(cmd_depth[:], 64, cmd_bits[:])
  105. copy(bits, cmd_bits[:24])
  106. copy(bits[24:], cmd_bits[32:][:8])
  107. copy(bits[32:], cmd_bits[48:][:8])
  108. copy(bits[40:], cmd_bits[24:][:8])
  109. copy(bits[48:], cmd_bits[40:][:8])
  110. copy(bits[56:], cmd_bits[56:][:8])
  111. convertBitDepthsToSymbols(depth[64:], 64, bits[64:])
  112. {
  113. /* Create the bit length array for the full command alphabet. */
  114. var i uint
  115. for i := 0; i < int(64); i++ {
  116. cmd_depth[i] = 0
  117. } /* only 64 first values were used */
  118. copy(cmd_depth[:], depth[:8])
  119. copy(cmd_depth[64:][:], depth[8:][:8])
  120. copy(cmd_depth[128:][:], depth[16:][:8])
  121. copy(cmd_depth[192:][:], depth[24:][:8])
  122. copy(cmd_depth[384:][:], depth[32:][:8])
  123. for i = 0; i < 8; i++ {
  124. cmd_depth[128+8*i] = depth[40+i]
  125. cmd_depth[256+8*i] = depth[48+i]
  126. cmd_depth[448+8*i] = depth[56+i]
  127. }
  128. storeHuffmanTree(cmd_depth[:], numCommandSymbols, tree[:], storage_ix, storage)
  129. }
  130. storeHuffmanTree(depth[64:], 64, tree[:], storage_ix, storage)
  131. }
  132. /* REQUIRES: insertlen < 6210 */
  133. func emitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) {
  134. if insertlen < 6 {
  135. var code uint = insertlen + 40
  136. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  137. histo[code]++
  138. } else if insertlen < 130 {
  139. var tail uint = insertlen - 2
  140. var nbits uint32 = log2FloorNonZero(tail) - 1
  141. var prefix uint = tail >> nbits
  142. var inscode uint = uint((nbits << 1) + uint32(prefix) + 42)
  143. writeBits(uint(depth[inscode]), uint64(bits[inscode]), storage_ix, storage)
  144. writeBits(uint(nbits), uint64(tail)-(uint64(prefix)<<nbits), storage_ix, storage)
  145. histo[inscode]++
  146. } else if insertlen < 2114 {
  147. var tail uint = insertlen - 66
  148. var nbits uint32 = log2FloorNonZero(tail)
  149. var code uint = uint(nbits + 50)
  150. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  151. writeBits(uint(nbits), uint64(tail)-(uint64(uint(1))<<nbits), storage_ix, storage)
  152. histo[code]++
  153. } else {
  154. writeBits(uint(depth[61]), uint64(bits[61]), storage_ix, storage)
  155. writeBits(12, uint64(insertlen)-2114, storage_ix, storage)
  156. histo[61]++
  157. }
  158. }
  159. func emitLongInsertLen(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) {
  160. if insertlen < 22594 {
  161. writeBits(uint(depth[62]), uint64(bits[62]), storage_ix, storage)
  162. writeBits(14, uint64(insertlen)-6210, storage_ix, storage)
  163. histo[62]++
  164. } else {
  165. writeBits(uint(depth[63]), uint64(bits[63]), storage_ix, storage)
  166. writeBits(24, uint64(insertlen)-22594, storage_ix, storage)
  167. histo[63]++
  168. }
  169. }
  170. func emitCopyLen1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) {
  171. if copylen < 10 {
  172. writeBits(uint(depth[copylen+14]), uint64(bits[copylen+14]), storage_ix, storage)
  173. histo[copylen+14]++
  174. } else if copylen < 134 {
  175. var tail uint = copylen - 6
  176. var nbits uint32 = log2FloorNonZero(tail) - 1
  177. var prefix uint = tail >> nbits
  178. var code uint = uint((nbits << 1) + uint32(prefix) + 20)
  179. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  180. writeBits(uint(nbits), uint64(tail)-(uint64(prefix)<<nbits), storage_ix, storage)
  181. histo[code]++
  182. } else if copylen < 2118 {
  183. var tail uint = copylen - 70
  184. var nbits uint32 = log2FloorNonZero(tail)
  185. var code uint = uint(nbits + 28)
  186. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  187. writeBits(uint(nbits), uint64(tail)-(uint64(uint(1))<<nbits), storage_ix, storage)
  188. histo[code]++
  189. } else {
  190. writeBits(uint(depth[39]), uint64(bits[39]), storage_ix, storage)
  191. writeBits(24, uint64(copylen)-2118, storage_ix, storage)
  192. histo[39]++
  193. }
  194. }
  195. func emitCopyLenLastDistance1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) {
  196. if copylen < 12 {
  197. writeBits(uint(depth[copylen-4]), uint64(bits[copylen-4]), storage_ix, storage)
  198. histo[copylen-4]++
  199. } else if copylen < 72 {
  200. var tail uint = copylen - 8
  201. var nbits uint32 = log2FloorNonZero(tail) - 1
  202. var prefix uint = tail >> nbits
  203. var code uint = uint((nbits << 1) + uint32(prefix) + 4)
  204. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  205. writeBits(uint(nbits), uint64(tail)-(uint64(prefix)<<nbits), storage_ix, storage)
  206. histo[code]++
  207. } else if copylen < 136 {
  208. var tail uint = copylen - 8
  209. var code uint = (tail >> 5) + 30
  210. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  211. writeBits(5, uint64(tail)&31, storage_ix, storage)
  212. writeBits(uint(depth[64]), uint64(bits[64]), storage_ix, storage)
  213. histo[code]++
  214. histo[64]++
  215. } else if copylen < 2120 {
  216. var tail uint = copylen - 72
  217. var nbits uint32 = log2FloorNonZero(tail)
  218. var code uint = uint(nbits + 28)
  219. writeBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage)
  220. writeBits(uint(nbits), uint64(tail)-(uint64(uint(1))<<nbits), storage_ix, storage)
  221. writeBits(uint(depth[64]), uint64(bits[64]), storage_ix, storage)
  222. histo[code]++
  223. histo[64]++
  224. } else {
  225. writeBits(uint(depth[39]), uint64(bits[39]), storage_ix, storage)
  226. writeBits(24, uint64(copylen)-2120, storage_ix, storage)
  227. writeBits(uint(depth[64]), uint64(bits[64]), storage_ix, storage)
  228. histo[39]++
  229. histo[64]++
  230. }
  231. }
  232. func emitDistance1(distance uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) {
  233. var d uint = distance + 3
  234. var nbits uint32 = log2FloorNonZero(d) - 1
  235. var prefix uint = (d >> nbits) & 1
  236. var offset uint = (2 + prefix) << nbits
  237. var distcode uint = uint(2*(nbits-1) + uint32(prefix) + 80)
  238. writeBits(uint(depth[distcode]), uint64(bits[distcode]), storage_ix, storage)
  239. writeBits(uint(nbits), uint64(d)-uint64(offset), storage_ix, storage)
  240. histo[distcode]++
  241. }
  242. func emitLiterals(input []byte, len uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) {
  243. var j uint
  244. for j = 0; j < len; j++ {
  245. var lit byte = input[j]
  246. writeBits(uint(depth[lit]), uint64(bits[lit]), storage_ix, storage)
  247. }
  248. }
  249. /* REQUIRES: len <= 1 << 24. */
  250. func storeMetaBlockHeader1(len uint, is_uncompressed bool, storage_ix *uint, storage []byte) {
  251. var nibbles uint = 6
  252. /* ISLAST */
  253. writeBits(1, 0, storage_ix, storage)
  254. if len <= 1<<16 {
  255. nibbles = 4
  256. } else if len <= 1<<20 {
  257. nibbles = 5
  258. }
  259. writeBits(2, uint64(nibbles)-4, storage_ix, storage)
  260. writeBits(nibbles*4, uint64(len)-1, storage_ix, storage)
  261. /* ISUNCOMPRESSED */
  262. writeSingleBit(is_uncompressed, storage_ix, storage)
  263. }
  264. func updateBits(n_bits uint, bits uint32, pos uint, array []byte) {
  265. for n_bits > 0 {
  266. var byte_pos uint = pos >> 3
  267. var n_unchanged_bits uint = pos & 7
  268. var n_changed_bits uint = brotli_min_size_t(n_bits, 8-n_unchanged_bits)
  269. var total_bits uint = n_unchanged_bits + n_changed_bits
  270. var mask uint32 = (^((1 << total_bits) - 1)) | ((1 << n_unchanged_bits) - 1)
  271. var unchanged_bits uint32 = uint32(array[byte_pos]) & mask
  272. var changed_bits uint32 = bits & ((1 << n_changed_bits) - 1)
  273. array[byte_pos] = byte(changed_bits<<n_unchanged_bits | unchanged_bits)
  274. n_bits -= n_changed_bits
  275. bits >>= n_changed_bits
  276. pos += n_changed_bits
  277. }
  278. }
  279. func rewindBitPosition1(new_storage_ix uint, storage_ix *uint, storage []byte) {
  280. var bitpos uint = new_storage_ix & 7
  281. var mask uint = (1 << bitpos) - 1
  282. storage[new_storage_ix>>3] &= byte(mask)
  283. *storage_ix = new_storage_ix
  284. }
  285. var shouldMergeBlock_kSampleRate uint = 43
  286. func shouldMergeBlock(data []byte, len uint, depths []byte) bool {
  287. var histo = [256]uint{0}
  288. var i uint
  289. for i = 0; i < len; i += shouldMergeBlock_kSampleRate {
  290. histo[data[i]]++
  291. }
  292. {
  293. var total uint = (len + shouldMergeBlock_kSampleRate - 1) / shouldMergeBlock_kSampleRate
  294. var r float64 = (fastLog2(total)+0.5)*float64(total) + 200
  295. for i = 0; i < 256; i++ {
  296. r -= float64(histo[i]) * (float64(depths[i]) + fastLog2(histo[i]))
  297. }
  298. return r >= 0.0
  299. }
  300. }
  301. func shouldUseUncompressedMode(metablock_start []byte, next_emit []byte, insertlen uint, literal_ratio uint) bool {
  302. var compressed uint = uint(-cap(next_emit) + cap(metablock_start))
  303. if compressed*50 > insertlen {
  304. return false
  305. } else {
  306. return literal_ratio > 980
  307. }
  308. }
  309. func emitUncompressedMetaBlock1(begin []byte, end []byte, storage_ix_start uint, storage_ix *uint, storage []byte) {
  310. var len uint = uint(-cap(end) + cap(begin))
  311. rewindBitPosition1(storage_ix_start, storage_ix, storage)
  312. storeMetaBlockHeader1(uint(len), true, storage_ix, storage)
  313. *storage_ix = (*storage_ix + 7) &^ 7
  314. copy(storage[*storage_ix>>3:], begin[:len])
  315. *storage_ix += uint(len << 3)
  316. storage[*storage_ix>>3] = 0
  317. }
  318. var kCmdHistoSeed = [128]uint32{
  319. 0,
  320. 1,
  321. 1,
  322. 1,
  323. 1,
  324. 1,
  325. 1,
  326. 1,
  327. 1,
  328. 1,
  329. 1,
  330. 1,
  331. 1,
  332. 1,
  333. 1,
  334. 1,
  335. 0,
  336. 0,
  337. 0,
  338. 1,
  339. 1,
  340. 1,
  341. 1,
  342. 1,
  343. 1,
  344. 1,
  345. 1,
  346. 1,
  347. 1,
  348. 1,
  349. 1,
  350. 1,
  351. 1,
  352. 1,
  353. 1,
  354. 1,
  355. 1,
  356. 1,
  357. 1,
  358. 1,
  359. 0,
  360. 1,
  361. 1,
  362. 1,
  363. 1,
  364. 1,
  365. 1,
  366. 1,
  367. 1,
  368. 1,
  369. 1,
  370. 1,
  371. 1,
  372. 1,
  373. 1,
  374. 1,
  375. 1,
  376. 1,
  377. 1,
  378. 1,
  379. 1,
  380. 1,
  381. 1,
  382. 1,
  383. 1,
  384. 0,
  385. 0,
  386. 0,
  387. 0,
  388. 0,
  389. 0,
  390. 0,
  391. 0,
  392. 0,
  393. 0,
  394. 0,
  395. 0,
  396. 0,
  397. 0,
  398. 0,
  399. 1,
  400. 1,
  401. 1,
  402. 1,
  403. 1,
  404. 1,
  405. 1,
  406. 1,
  407. 1,
  408. 1,
  409. 1,
  410. 1,
  411. 1,
  412. 1,
  413. 1,
  414. 1,
  415. 1,
  416. 1,
  417. 1,
  418. 1,
  419. 1,
  420. 1,
  421. 1,
  422. 1,
  423. 1,
  424. 1,
  425. 1,
  426. 1,
  427. 1,
  428. 1,
  429. 1,
  430. 1,
  431. 1,
  432. 1,
  433. 1,
  434. 1,
  435. 1,
  436. 1,
  437. 1,
  438. 1,
  439. 1,
  440. 1,
  441. 1,
  442. 1,
  443. 0,
  444. 0,
  445. 0,
  446. 0,
  447. }
  448. var compressFragmentFastImpl_kFirstBlockSize uint = 3 << 15
  449. var compressFragmentFastImpl_kMergeBlockSize uint = 1 << 16
  450. func compressFragmentFastImpl(in []byte, input_size uint, is_last bool, table []int, table_bits uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) {
  451. var cmd_histo [128]uint32
  452. var ip_end int
  453. var next_emit int = 0
  454. var base_ip int = 0
  455. var input int = 0
  456. const kInputMarginBytes uint = windowGap
  457. const kMinMatchLen uint = 5
  458. var metablock_start int = input
  459. var block_size uint = brotli_min_size_t(input_size, compressFragmentFastImpl_kFirstBlockSize)
  460. var total_block_size uint = block_size
  461. var mlen_storage_ix uint = *storage_ix + 3
  462. var lit_depth [256]byte
  463. var lit_bits [256]uint16
  464. var literal_ratio uint
  465. var ip int
  466. var last_distance int
  467. var shift uint = 64 - table_bits
  468. /* "next_emit" is a pointer to the first byte that is not covered by a
  469. previous copy. Bytes between "next_emit" and the start of the next copy or
  470. the end of the input will be emitted as literal bytes. */
  471. /* Save the start of the first block for position and distance computations.
  472. */
  473. /* Save the bit position of the MLEN field of the meta-block header, so that
  474. we can update it later if we decide to extend this meta-block. */
  475. storeMetaBlockHeader1(block_size, false, storage_ix, storage)
  476. /* No block splits, no contexts. */
  477. writeBits(13, 0, storage_ix, storage)
  478. literal_ratio = buildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage)
  479. {
  480. /* Store the pre-compressed command and distance prefix codes. */
  481. var i uint
  482. for i = 0; i+7 < *cmd_code_numbits; i += 8 {
  483. writeBits(8, uint64(cmd_code[i>>3]), storage_ix, storage)
  484. }
  485. }
  486. writeBits(*cmd_code_numbits&7, uint64(cmd_code[*cmd_code_numbits>>3]), storage_ix, storage)
  487. /* Initialize the command and distance histograms. We will gather
  488. statistics of command and distance codes during the processing
  489. of this block and use it to update the command and distance
  490. prefix codes for the next block. */
  491. emit_commands:
  492. copy(cmd_histo[:], kCmdHistoSeed[:])
  493. /* "ip" is the input pointer. */
  494. ip = input
  495. last_distance = -1
  496. ip_end = int(uint(input) + block_size)
  497. if block_size >= kInputMarginBytes {
  498. var len_limit uint = brotli_min_size_t(block_size-kMinMatchLen, input_size-kInputMarginBytes)
  499. var ip_limit int = int(uint(input) + len_limit)
  500. /* For the last block, we need to keep a 16 bytes margin so that we can be
  501. sure that all distances are at most window size - 16.
  502. For all other blocks, we only need to keep a margin of 5 bytes so that
  503. we don't go over the block size with a copy. */
  504. var next_hash uint32
  505. ip++
  506. for next_hash = hash5(in[ip:], shift); ; {
  507. var skip uint32 = 32
  508. var next_ip int = ip
  509. /* Step 1: Scan forward in the input looking for a 5-byte-long match.
  510. If we get close to exhausting the input then goto emit_remainder.
  511. Heuristic match skipping: If 32 bytes are scanned with no matches
  512. found, start looking only at every other byte. If 32 more bytes are
  513. scanned, look at every third byte, etc.. When a match is found,
  514. immediately go back to looking at every byte. This is a small loss
  515. (~5% performance, ~0.1% density) for compressible data due to more
  516. bookkeeping, but for non-compressible data (such as JPEG) it's a huge
  517. win since the compressor quickly "realizes" the data is incompressible
  518. and doesn't bother looking for matches everywhere.
  519. The "skip" variable keeps track of how many bytes there are since the
  520. last match; dividing it by 32 (i.e. right-shifting by five) gives the
  521. number of bytes to move ahead for each iteration. */
  522. var candidate int
  523. assert(next_emit < ip)
  524. trawl:
  525. for {
  526. var hash uint32 = next_hash
  527. var bytes_between_hash_lookups uint32 = skip >> 5
  528. skip++
  529. assert(hash == hash5(in[next_ip:], shift))
  530. ip = next_ip
  531. next_ip = int(uint32(ip) + bytes_between_hash_lookups)
  532. if next_ip > ip_limit {
  533. goto emit_remainder
  534. }
  535. next_hash = hash5(in[next_ip:], shift)
  536. candidate = ip - last_distance
  537. if isMatch5(in[ip:], in[candidate:]) {
  538. if candidate < ip {
  539. table[hash] = int(ip - base_ip)
  540. break
  541. }
  542. }
  543. candidate = base_ip + table[hash]
  544. assert(candidate >= base_ip)
  545. assert(candidate < ip)
  546. table[hash] = int(ip - base_ip)
  547. if isMatch5(in[ip:], in[candidate:]) {
  548. break
  549. }
  550. }
  551. /* Check copy distance. If candidate is not feasible, continue search.
  552. Checking is done outside of hot loop to reduce overhead. */
  553. if ip-candidate > maxDistance_compress_fragment {
  554. goto trawl
  555. }
  556. /* Step 2: Emit the found match together with the literal bytes from
  557. "next_emit" to the bit stream, and then see if we can find a next match
  558. immediately afterwards. Repeat until we find no match for the input
  559. without emitting some literal bytes. */
  560. {
  561. var base int = ip
  562. /* > 0 */
  563. var matched uint = 5 + findMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5)
  564. var distance int = int(base - candidate)
  565. /* We have a 5-byte match at ip, and we need to emit bytes in
  566. [next_emit, ip). */
  567. var insert uint = uint(base - next_emit)
  568. ip += int(matched)
  569. if insert < 6210 {
  570. emitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  571. } else if shouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) {
  572. emitUncompressedMetaBlock1(in[metablock_start:], in[base:], mlen_storage_ix-3, storage_ix, storage)
  573. input_size -= uint(base - input)
  574. input = base
  575. next_emit = input
  576. goto next_block
  577. } else {
  578. emitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  579. }
  580. emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage)
  581. if distance == last_distance {
  582. writeBits(uint(cmd_depth[64]), uint64(cmd_bits[64]), storage_ix, storage)
  583. cmd_histo[64]++
  584. } else {
  585. emitDistance1(uint(distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  586. last_distance = distance
  587. }
  588. emitCopyLenLastDistance1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  589. next_emit = ip
  590. if ip >= ip_limit {
  591. goto emit_remainder
  592. }
  593. /* We could immediately start working at ip now, but to improve
  594. compression we first update "table" with the hashes of some positions
  595. within the last copy. */
  596. {
  597. var input_bytes uint64 = binary.LittleEndian.Uint64(in[ip-3:])
  598. var prev_hash uint32 = hashBytesAtOffset5(input_bytes, 0, shift)
  599. var cur_hash uint32 = hashBytesAtOffset5(input_bytes, 3, shift)
  600. table[prev_hash] = int(ip - base_ip - 3)
  601. prev_hash = hashBytesAtOffset5(input_bytes, 1, shift)
  602. table[prev_hash] = int(ip - base_ip - 2)
  603. prev_hash = hashBytesAtOffset5(input_bytes, 2, shift)
  604. table[prev_hash] = int(ip - base_ip - 1)
  605. candidate = base_ip + table[cur_hash]
  606. table[cur_hash] = int(ip - base_ip)
  607. }
  608. }
  609. for isMatch5(in[ip:], in[candidate:]) {
  610. var base int = ip
  611. /* We have a 5-byte match at ip, and no need to emit any literal bytes
  612. prior to ip. */
  613. var matched uint = 5 + findMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5)
  614. if ip-candidate > maxDistance_compress_fragment {
  615. break
  616. }
  617. ip += int(matched)
  618. last_distance = int(base - candidate) /* > 0 */
  619. emitCopyLen1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  620. emitDistance1(uint(last_distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  621. next_emit = ip
  622. if ip >= ip_limit {
  623. goto emit_remainder
  624. }
  625. /* We could immediately start working at ip now, but to improve
  626. compression we first update "table" with the hashes of some positions
  627. within the last copy. */
  628. {
  629. var input_bytes uint64 = binary.LittleEndian.Uint64(in[ip-3:])
  630. var prev_hash uint32 = hashBytesAtOffset5(input_bytes, 0, shift)
  631. var cur_hash uint32 = hashBytesAtOffset5(input_bytes, 3, shift)
  632. table[prev_hash] = int(ip - base_ip - 3)
  633. prev_hash = hashBytesAtOffset5(input_bytes, 1, shift)
  634. table[prev_hash] = int(ip - base_ip - 2)
  635. prev_hash = hashBytesAtOffset5(input_bytes, 2, shift)
  636. table[prev_hash] = int(ip - base_ip - 1)
  637. candidate = base_ip + table[cur_hash]
  638. table[cur_hash] = int(ip - base_ip)
  639. }
  640. }
  641. ip++
  642. next_hash = hash5(in[ip:], shift)
  643. }
  644. }
  645. emit_remainder:
  646. assert(next_emit <= ip_end)
  647. input += int(block_size)
  648. input_size -= block_size
  649. block_size = brotli_min_size_t(input_size, compressFragmentFastImpl_kMergeBlockSize)
  650. /* Decide if we want to continue this meta-block instead of emitting the
  651. last insert-only command. */
  652. if input_size > 0 && total_block_size+block_size <= 1<<20 && shouldMergeBlock(in[input:], block_size, lit_depth[:]) {
  653. assert(total_block_size > 1<<16)
  654. /* Update the size of the current meta-block and continue emitting commands.
  655. We can do this because the current size and the new size both have 5
  656. nibbles. */
  657. total_block_size += block_size
  658. updateBits(20, uint32(total_block_size-1), mlen_storage_ix, storage)
  659. goto emit_commands
  660. }
  661. /* Emit the remaining bytes as literals. */
  662. if next_emit < ip_end {
  663. var insert uint = uint(ip_end - next_emit)
  664. if insert < 6210 {
  665. emitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  666. emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage)
  667. } else if shouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) {
  668. emitUncompressedMetaBlock1(in[metablock_start:], in[ip_end:], mlen_storage_ix-3, storage_ix, storage)
  669. } else {
  670. emitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage)
  671. emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage)
  672. }
  673. }
  674. next_emit = ip_end
  675. /* If we have more data, write a new meta-block header and prefix codes and
  676. then continue emitting commands. */
  677. next_block:
  678. if input_size > 0 {
  679. metablock_start = input
  680. block_size = brotli_min_size_t(input_size, compressFragmentFastImpl_kFirstBlockSize)
  681. total_block_size = block_size
  682. /* Save the bit position of the MLEN field of the meta-block header, so that
  683. we can update it later if we decide to extend this meta-block. */
  684. mlen_storage_ix = *storage_ix + 3
  685. storeMetaBlockHeader1(block_size, false, storage_ix, storage)
  686. /* No block splits, no contexts. */
  687. writeBits(13, 0, storage_ix, storage)
  688. literal_ratio = buildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage)
  689. buildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, storage_ix, storage)
  690. goto emit_commands
  691. }
  692. if !is_last {
  693. /* If this is not the last block, update the command and distance prefix
  694. codes for the next block and store the compressed forms. */
  695. cmd_code[0] = 0
  696. *cmd_code_numbits = 0
  697. buildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, cmd_code_numbits, cmd_code)
  698. }
  699. }
  700. /* Compresses "input" string to the "*storage" buffer as one or more complete
  701. meta-blocks, and updates the "*storage_ix" bit position.
  702. If "is_last" is 1, emits an additional empty last meta-block.
  703. "cmd_depth" and "cmd_bits" contain the command and distance prefix codes
  704. (see comment in encode.h) used for the encoding of this input fragment.
  705. If "is_last" is 0, they are updated to reflect the statistics
  706. of this input fragment, to be used for the encoding of the next fragment.
  707. "*cmd_code_numbits" is the number of bits of the compressed representation
  708. of the command and distance prefix codes, and "cmd_code" is an array of
  709. at least "(*cmd_code_numbits + 7) >> 3" size that contains the compressed
  710. command and distance prefix codes. If "is_last" is 0, these are also
  711. updated to represent the updated "cmd_depth" and "cmd_bits".
  712. REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
  713. REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24).
  714. REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
  715. REQUIRES: "table_size" is an odd (9, 11, 13, 15) power of two
  716. OUTPUT: maximal copy distance <= |input_size|
  717. OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */
  718. func compressFragmentFast(input []byte, input_size uint, is_last bool, table []int, table_size uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) {
  719. var initial_storage_ix uint = *storage_ix
  720. var table_bits uint = uint(log2FloorNonZero(table_size))
  721. if input_size == 0 {
  722. assert(is_last)
  723. writeBits(1, 1, storage_ix, storage) /* islast */
  724. writeBits(1, 1, storage_ix, storage) /* isempty */
  725. *storage_ix = (*storage_ix + 7) &^ 7
  726. return
  727. }
  728. compressFragmentFastImpl(input, input_size, is_last, table, table_bits, cmd_depth, cmd_bits, cmd_code_numbits, cmd_code, storage_ix, storage)
  729. /* If output is larger than single uncompressed block, rewrite it. */
  730. if *storage_ix-initial_storage_ix > 31+(input_size<<3) {
  731. emitUncompressedMetaBlock1(input, input[input_size:], initial_storage_ix, storage_ix, storage)
  732. }
  733. if is_last {
  734. writeBits(1, 1, storage_ix, storage) /* islast */
  735. writeBits(1, 1, storage_ix, storage) /* isempty */
  736. *storage_ix = (*storage_ix + 7) &^ 7
  737. }
  738. }