package brotliimport/* Copyright 2015 Google Inc. All Rights Reserved. Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT*//* Function for fast encoding of an input fragment, independently from the input history. This function uses one-pass processing: when we find a backward match, we immediately emit the corresponding command and literal codes to the bit stream. Adapted from the CompressFragment() function in https://github.com/google/snappy/blob/master/snappy.cc */const maxDistance_compress_fragment = 262128func hash5( []byte, uint) uint32 {varuint64 = (binary.LittleEndian.Uint64() << 24) * uint64(kHashMul32)returnuint32( >> )}func hashBytesAtOffset5( uint64, int, uint) uint32 {assert( >= 0)assert( <= 3) {varuint64 = (( >> uint(8*)) << 24) * uint64(kHashMul32)returnuint32( >> ) }}func isMatch5( []byte, []byte) bool {returnbinary.LittleEndian.Uint32() == binary.LittleEndian.Uint32() && [4] == [4]}/* Builds a literal prefix code into "depths" and "bits" based on the statistics of the "input" string and stores it into the bit stream. Note that the prefix code here is built from the pre-LZ77 input, therefore we can only approximate the statistics of the actual literal stream. Moreover, for long inputs we build a histogram from a sample of the input and thus have to assign a non-zero depth for each literal. Returns estimated compression ratio millibytes/char for encoding given input with generated code. */func buildAndStoreLiteralPrefixCode( []byte, uint, []byte, []uint16, *uint, []byte) uint {var = [256]uint32{0}varuintvaruintif < 1<<15 {for = 0; < ; ++ { [[]]++ } = for = 0; < 256; ++ {/* We weigh the first 11 samples with weight 3 to account for the balancing effect of the LZ77 phase on the histogram. */varuint32 = 2 * brotli_min_uint32_t([], 11) [] += += uint() } } else {constuint = 29for = 0; < ; += { [[]]++ } = ( + - 1) / for = 0; < 256; ++ {/* We add 1 to each population count to avoid 0 bit depths (since this is only a sample and we don't know if the symbol appears or not), and we weigh the first 11 samples with weight 3 to account for the balancing effect of the LZ77 phase on the histogram (more frequent symbols are more likely to be in backward references instead as literals). */varuint32 = 1 + 2*brotli_min_uint32_t([], 11) [] += += uint() } }buildAndStoreHuffmanTreeFast([:], , /* max_bits = */8, , , , ) {varuint = 0for = 0; < 256; ++ {if [] != 0 { += uint([] * uint32([])) } }/* Estimated encoding ratio, millibytes per symbol. */return ( * 125) / }}/* Builds a command and distance prefix code (each 64 symbols) into "depth" and "bits" based on "histogram" and stores it into the bit stream. */func buildAndStoreCommandPrefixCode1( []uint32, []byte, []uint16, *uint, []byte) {var [129]huffmanTreevar = [numCommandSymbols]byte{0}/* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */var [64]uint16createHuffmanTree(, 64, 15, [:], )createHuffmanTree([64:], 64, 14, [:], [64:])/* We have to jump through a few hoops here in order to compute the command bits because the symbols are in a different order than in the full alphabet. This looks complicated, but having the symbols in this order in the command bits saves a few branches in the Emit* functions. */copy([:], [:24])copy([24:][:], [40:][:8])copy([32:][:], [24:][:8])copy([40:][:], [48:][:8])copy([48:][:], [32:][:8])copy([56:][:], [56:][:8])convertBitDepthsToSymbols([:], 64, [:])copy(, [:24])copy([24:], [32:][:8])copy([32:], [48:][:8])copy([40:], [24:][:8])copy([48:], [40:][:8])copy([56:], [56:][:8])convertBitDepthsToSymbols([64:], 64, [64:]) {/* Create the bit length array for the full command alphabet. */varuintfor := 0; < int(64); ++ { [] = 0 } /* only 64 first values were used */copy([:], [:8])copy([64:][:], [8:][:8])copy([128:][:], [16:][:8])copy([192:][:], [24:][:8])copy([384:][:], [32:][:8])for = 0; < 8; ++ { [128+8*] = [40+] [256+8*] = [48+] [448+8*] = [56+] }storeHuffmanTree([:], numCommandSymbols, [:], , ) }storeHuffmanTree([64:], 64, [:], , )}/* REQUIRES: insertlen < 6210 */func emitInsertLen1( uint, []byte, []uint16, []uint32, *uint, []byte) {if < 6 {varuint = + 40writeBits(uint([]), uint64([]), , ) []++ } elseif < 130 {varuint = - 2varuint32 = log2FloorNonZero() - 1varuint = >> varuint = uint(( << 1) + uint32() + 42)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64()<<), , ) []++ } elseif < 2114 {varuint = - 66varuint32 = log2FloorNonZero()varuint = uint( + 50)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64(uint(1))<<), , ) []++ } else {writeBits(uint([61]), uint64([61]), , )writeBits(12, uint64()-2114, , ) [61]++ }}func emitLongInsertLen( uint, []byte, []uint16, []uint32, *uint, []byte) {if < 22594 {writeBits(uint([62]), uint64([62]), , )writeBits(14, uint64()-6210, , ) [62]++ } else {writeBits(uint([63]), uint64([63]), , )writeBits(24, uint64()-22594, , ) [63]++ }}func emitCopyLen1( uint, []byte, []uint16, []uint32, *uint, []byte) {if < 10 {writeBits(uint([+14]), uint64([+14]), , ) [+14]++ } elseif < 134 {varuint = - 6varuint32 = log2FloorNonZero() - 1varuint = >> varuint = uint(( << 1) + uint32() + 20)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64()<<), , ) []++ } elseif < 2118 {varuint = - 70varuint32 = log2FloorNonZero()varuint = uint( + 28)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64(uint(1))<<), , ) []++ } else {writeBits(uint([39]), uint64([39]), , )writeBits(24, uint64()-2118, , ) [39]++ }}func emitCopyLenLastDistance1( uint, []byte, []uint16, []uint32, *uint, []byte) {if < 12 {writeBits(uint([-4]), uint64([-4]), , ) [-4]++ } elseif < 72 {varuint = - 8varuint32 = log2FloorNonZero() - 1varuint = >> varuint = uint(( << 1) + uint32() + 4)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64()<<), , ) []++ } elseif < 136 {varuint = - 8varuint = ( >> 5) + 30writeBits(uint([]), uint64([]), , )writeBits(5, uint64()&31, , )writeBits(uint([64]), uint64([64]), , ) []++ [64]++ } elseif < 2120 {varuint = - 72varuint32 = log2FloorNonZero()varuint = uint( + 28)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-(uint64(uint(1))<<), , )writeBits(uint([64]), uint64([64]), , ) []++ [64]++ } else {writeBits(uint([39]), uint64([39]), , )writeBits(24, uint64()-2120, , )writeBits(uint([64]), uint64([64]), , ) [39]++ [64]++ }}func emitDistance1( uint, []byte, []uint16, []uint32, *uint, []byte) {varuint = + 3varuint32 = log2FloorNonZero() - 1varuint = ( >> ) & 1varuint = (2 + ) << varuint = uint(2*(-1) + uint32() + 80)writeBits(uint([]), uint64([]), , )writeBits(uint(), uint64()-uint64(), , ) []++}func emitLiterals( []byte, uint, []byte, []uint16, *uint, []byte) {varuintfor = 0; < ; ++ {varbyte = []writeBits(uint([]), uint64([]), , ) }}/* REQUIRES: len <= 1 << 24. */func storeMetaBlockHeader1( uint, bool, *uint, []byte) {varuint = 6/* ISLAST */writeBits(1, 0, , )if <= 1<<16 { = 4 } elseif <= 1<<20 { = 5 }writeBits(2, uint64()-4, , )writeBits(*4, uint64()-1, , )/* ISUNCOMPRESSED */writeSingleBit(, , )}func updateBits( uint, uint32, uint, []byte) {for > 0 {varuint = >> 3varuint = & 7varuint = brotli_min_size_t(, 8-)varuint = + varuint32 = (^((1 << ) - 1)) | ((1 << ) - 1)varuint32 = uint32([]) & varuint32 = & ((1 << ) - 1) [] = byte(<< | ) -= >>= += }}func rewindBitPosition1( uint, *uint, []byte) {varuint = & 7varuint = (1 << ) - 1 [>>3] &= byte() * = }var shouldMergeBlock_kSampleRate uint = 43func shouldMergeBlock( []byte, uint, []byte) bool {var = [256]uint{0}varuintfor = 0; < ; += shouldMergeBlock_kSampleRate { [[]]++ } {varuint = ( + shouldMergeBlock_kSampleRate - 1) / shouldMergeBlock_kSampleRatevarfloat64 = (fastLog2()+0.5)*float64() + 200for = 0; < 256; ++ { -= float64([]) * (float64([]) + fastLog2([])) }return >= 0.0 }}func shouldUseUncompressedMode( []byte, []byte, uint, uint) bool {varuint = uint(-cap() + cap())if *50 > {returnfalse } else {return > 980 }}func emitUncompressedMetaBlock1( []byte, []byte, uint, *uint, []byte) {varuint = uint(-cap() + cap())rewindBitPosition1(, , )storeMetaBlockHeader1(uint(), true, , ) * = (* + 7) &^ 7copy([*>>3:], [:]) * += uint( << 3) [*>>3] = 0}var kCmdHistoSeed = [128]uint32{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,}var compressFragmentFastImpl_kFirstBlockSize uint = 3 << 15var compressFragmentFastImpl_kMergeBlockSize uint = 1 << 16func compressFragmentFastImpl( []byte, uint, bool, []int, uint, []byte, []uint16, *uint, []byte, *uint, []byte) {var [128]uint32varintvarint = 0varint = 0varint = 0constuint = windowGapconstuint = 5varint = varuint = brotli_min_size_t(, compressFragmentFastImpl_kFirstBlockSize)varuint = varuint = * + 3var [256]bytevar [256]uint16varuintvarintvarintvaruint = 64 - /* "next_emit" is a pointer to the first byte that is not covered by a previous copy. Bytes between "next_emit" and the start of the next copy or the end of the input will be emitted as literal bytes. *//* Save the start of the first block for position and distance computations. *//* Save the bit position of the MLEN field of the meta-block header, so that we can update it later if we decide to extend this meta-block. */storeMetaBlockHeader1(, false, , )/* No block splits, no contexts. */writeBits(13, 0, , ) = buildAndStoreLiteralPrefixCode([:], , [:], [:], , ) {/* Store the pre-compressed command and distance prefix codes. */varuintfor = 0; +7 < *; += 8 {writeBits(8, uint64([>>3]), , ) } }writeBits(*&7, uint64([*>>3]), , )/* Initialize the command and distance histograms. We will gather statistics of command and distance codes during the processing of this block and use it to update the command and distance prefix codes for the next block. */:copy([:], kCmdHistoSeed[:])/* "ip" is the input pointer. */ = = -1 = int(uint() + )if >= {varuint = brotli_min_size_t(-, -)varint = int(uint() + )/* For the last block, we need to keep a 16 bytes margin so that we can be sure that all distances are at most window size - 16. For all other blocks, we only need to keep a margin of 5 bytes so that we don't go over the block size with a copy. */varuint32 ++for = hash5([:], ); ; {varuint32 = 32varint = /* Step 1: Scan forward in the input looking for a 5-byte-long match. If we get close to exhausting the input then goto emit_remainder. Heuristic match skipping: If 32 bytes are scanned with no matches found, start looking only at every other byte. If 32 more bytes are scanned, look at every third byte, etc.. When a match is found, immediately go back to looking at every byte. This is a small loss (~5% performance, ~0.1% density) for compressible data due to more bookkeeping, but for non-compressible data (such as JPEG) it's a huge win since the compressor quickly "realizes" the data is incompressible and doesn't bother looking for matches everywhere. The "skip" variable keeps track of how many bytes there are since the last match; dividing it by 32 (i.e. right-shifting by five) gives the number of bytes to move ahead for each iteration. */varintassert( < ) :for {varuint32 = varuint32 = >> 5 ++assert( == hash5([:], )) = = int(uint32() + )if > {goto } = hash5([:], ) = - ifisMatch5([:], [:]) {if < { [] = int( - )break } } = + []assert( >= )assert( < ) [] = int( - )ifisMatch5([:], [:]) {break } }/* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */if - > maxDistance_compress_fragment {goto }/* Step 2: Emit the found match together with the literal bytes from "next_emit" to the bit stream, and then see if we can find a next match immediately afterwards. Repeat until we find no match for the input without emitting some literal bytes. */ {varint = /* > 0 */varuint = 5 + findMatchLengthWithLimit([+5:], [+5:], uint(-)-5)varint = int( - )/* We have a 5-byte match at ip, and we need to emit bytes in [next_emit, ip). */varuint = uint( - ) += int()if < 6210 {emitInsertLen1(, , , [:], , ) } elseifshouldUseUncompressedMode([:], [:], , ) {emitUncompressedMetaBlock1([:], [:], -3, , ) -= uint( - ) = = goto } else {emitLongInsertLen(, , , [:], , ) }emitLiterals([:], , [:], [:], , )if == {writeBits(uint([64]), uint64([64]), , ) [64]++ } else {emitDistance1(uint(), , , [:], , ) = }emitCopyLenLastDistance1(, , , [:], , ) = if >= {goto }/* We could immediately start working at ip now, but to improve compression we first update "table" with the hashes of some positions within the last copy. */ {varuint64 = binary.LittleEndian.Uint64([-3:])varuint32 = hashBytesAtOffset5(, 0, )varuint32 = hashBytesAtOffset5(, 3, ) [] = int( - - 3) = hashBytesAtOffset5(, 1, ) [] = int( - - 2) = hashBytesAtOffset5(, 2, ) [] = int( - - 1) = + [] [] = int( - ) } }forisMatch5([:], [:]) {varint = /* We have a 5-byte match at ip, and no need to emit any literal bytes prior to ip. */varuint = 5 + findMatchLengthWithLimit([+5:], [+5:], uint(-)-5)if - > maxDistance_compress_fragment {break } += int() = int( - ) /* > 0 */emitCopyLen1(, , , [:], , )emitDistance1(uint(), , , [:], , ) = if >= {goto }/* We could immediately start working at ip now, but to improve compression we first update "table" with the hashes of some positions within the last copy. */ {varuint64 = binary.LittleEndian.Uint64([-3:])varuint32 = hashBytesAtOffset5(, 0, )varuint32 = hashBytesAtOffset5(, 3, ) [] = int( - - 3) = hashBytesAtOffset5(, 1, ) [] = int( - - 2) = hashBytesAtOffset5(, 2, ) [] = int( - - 1) = + [] [] = int( - ) } } ++ = hash5([:], ) } }:assert( <= ) += int() -= = brotli_min_size_t(, compressFragmentFastImpl_kMergeBlockSize)/* Decide if we want to continue this meta-block instead of emitting the last insert-only command. */if > 0 && + <= 1<<20 && shouldMergeBlock([:], , [:]) {assert( > 1<<16)/* Update the size of the current meta-block and continue emitting commands. We can do this because the current size and the new size both have 5 nibbles. */ += updateBits(20, uint32(-1), , )goto }/* Emit the remaining bytes as literals. */if < {varuint = uint( - )if < 6210 {emitInsertLen1(, , , [:], , )emitLiterals([:], , [:], [:], , ) } elseifshouldUseUncompressedMode([:], [:], , ) {emitUncompressedMetaBlock1([:], [:], -3, , ) } else {emitLongInsertLen(, , , [:], , )emitLiterals([:], , [:], [:], , ) } } = /* If we have more data, write a new meta-block header and prefix codes and then continue emitting commands. */:if > 0 { = = brotli_min_size_t(, compressFragmentFastImpl_kFirstBlockSize) = /* Save the bit position of the MLEN field of the meta-block header, so that we can update it later if we decide to extend this meta-block. */ = * + 3storeMetaBlockHeader1(, false, , )/* No block splits, no contexts. */writeBits(13, 0, , ) = buildAndStoreLiteralPrefixCode([:], , [:], [:], , )buildAndStoreCommandPrefixCode1([:], , , , )goto }if ! {/* If this is not the last block, update the command and distance prefix codes for the next block and store the compressed forms. */ [0] = 0 * = 0buildAndStoreCommandPrefixCode1([:], , , , ) }}/* Compresses "input" string to the "*storage" buffer as one or more complete meta-blocks, and updates the "*storage_ix" bit position. If "is_last" is 1, emits an additional empty last meta-block. "cmd_depth" and "cmd_bits" contain the command and distance prefix codes (see comment in encode.h) used for the encoding of this input fragment. If "is_last" is 0, they are updated to reflect the statistics of this input fragment, to be used for the encoding of the next fragment. "*cmd_code_numbits" is the number of bits of the compressed representation of the command and distance prefix codes, and "cmd_code" is an array of at least "(*cmd_code_numbits + 7) >> 3" size that contains the compressed command and distance prefix codes. If "is_last" is 0, these are also updated to represent the updated "cmd_depth" and "cmd_bits". REQUIRES: "input_size" is greater than zero, or "is_last" is 1. REQUIRES: "input_size" is less or equal to maximal metablock size (1 << 24). REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero. REQUIRES: "table_size" is an odd (9, 11, 13, 15) power of two OUTPUT: maximal copy distance <= |input_size| OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */func compressFragmentFast( []byte, uint, bool, []int, uint, []byte, []uint16, *uint, []byte, *uint, []byte) {varuint = *varuint = uint(log2FloorNonZero())if == 0 {assert()writeBits(1, 1, , ) /* islast */writeBits(1, 1, , ) /* isempty */ * = (* + 7) &^ 7return }compressFragmentFastImpl(, , , , , , , , , , )/* If output is larger than single uncompressed block, rewrite it. */if *- > 31+(<<3) {emitUncompressedMetaBlock1(, [:], , , ) }if {writeBits(1, 1, , ) /* islast */writeBits(1, 1, , ) /* isempty */ * = (* + 7) &^ 7 }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.