quality.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Constants and formulas that affect speed-ratio trade-offs and thus define
  6. quality levels. */
  7. #ifndef BROTLI_ENC_QUALITY_H_
  8. #define BROTLI_ENC_QUALITY_H_
  9. #include "../common/platform.h"
  10. #include <brotli/encode.h>
  11. #include "./params.h"
  12. #define FAST_ONE_PASS_COMPRESSION_QUALITY 0
  13. #define FAST_TWO_PASS_COMPRESSION_QUALITY 1
  14. #define ZOPFLIFICATION_QUALITY 10
  15. #define HQ_ZOPFLIFICATION_QUALITY 11
  16. #define MAX_QUALITY_FOR_STATIC_ENTROPY_CODES 2
  17. #define MIN_QUALITY_FOR_BLOCK_SPLIT 4
  18. #define MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS 4
  19. #define MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS 4
  20. #define MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH 5
  21. #define MIN_QUALITY_FOR_CONTEXT_MODELING 5
  22. #define MIN_QUALITY_FOR_HQ_CONTEXT_MODELING 7
  23. #define MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING 10
  24. /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting,
  25. so we buffer at most this much literals and commands. */
  26. #define MAX_NUM_DELAYED_SYMBOLS 0x2FFF
  27. /* Returns hash-table size for quality levels 0 and 1. */
  28. static BROTLI_INLINE size_t MaxHashTableSize(int quality) {
  29. return quality == FAST_ONE_PASS_COMPRESSION_QUALITY ? 1 << 15 : 1 << 17;
  30. }
  31. /* The maximum length for which the zopflification uses distinct distances. */
  32. #define MAX_ZOPFLI_LEN_QUALITY_10 150
  33. #define MAX_ZOPFLI_LEN_QUALITY_11 325
  34. /* Do not thoroughly search when a long copy is found. */
  35. #define BROTLI_LONG_COPY_QUICK_STEP 16384
  36. static BROTLI_INLINE size_t MaxZopfliLen(const BrotliEncoderParams* params) {
  37. return params->quality <= 10 ?
  38. MAX_ZOPFLI_LEN_QUALITY_10 :
  39. MAX_ZOPFLI_LEN_QUALITY_11;
  40. }
  41. /* Number of best candidates to evaluate to expand Zopfli chain. */
  42. static BROTLI_INLINE size_t MaxZopfliCandidates(
  43. const BrotliEncoderParams* params) {
  44. return params->quality <= 10 ? 1 : 5;
  45. }
  46. static BROTLI_INLINE void SanitizeParams(BrotliEncoderParams* params) {
  47. params->quality = BROTLI_MIN(int, BROTLI_MAX_QUALITY,
  48. BROTLI_MAX(int, BROTLI_MIN_QUALITY, params->quality));
  49. if (params->quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES) {
  50. params->large_window = BROTLI_FALSE;
  51. }
  52. if (params->lgwin < BROTLI_MIN_WINDOW_BITS) {
  53. params->lgwin = BROTLI_MIN_WINDOW_BITS;
  54. } else {
  55. int max_lgwin = params->large_window ? BROTLI_LARGE_MAX_WINDOW_BITS :
  56. BROTLI_MAX_WINDOW_BITS;
  57. if (params->lgwin > max_lgwin) params->lgwin = max_lgwin;
  58. }
  59. }
  60. /* Returns optimized lg_block value. */
  61. static BROTLI_INLINE int ComputeLgBlock(const BrotliEncoderParams* params) {
  62. int lgblock = params->lgblock;
  63. if (params->quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
  64. params->quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
  65. lgblock = params->lgwin;
  66. } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
  67. lgblock = 14;
  68. } else if (lgblock == 0) {
  69. lgblock = 16;
  70. if (params->quality >= 9 && params->lgwin > lgblock) {
  71. lgblock = BROTLI_MIN(int, 18, params->lgwin);
  72. }
  73. } else {
  74. lgblock = BROTLI_MIN(int, BROTLI_MAX_INPUT_BLOCK_BITS,
  75. BROTLI_MAX(int, BROTLI_MIN_INPUT_BLOCK_BITS, lgblock));
  76. }
  77. return lgblock;
  78. }
  79. /* Returns log2 of the size of main ring buffer area.
  80. Allocate at least lgwin + 1 bits for the ring buffer so that the newly
  81. added block fits there completely and we still get lgwin bits and at least
  82. read_block_size_bits + 1 bits because the copy tail length needs to be
  83. smaller than ring-buffer size. */
  84. static BROTLI_INLINE int ComputeRbBits(const BrotliEncoderParams* params) {
  85. return 1 + BROTLI_MAX(int, params->lgwin, params->lgblock);
  86. }
  87. static BROTLI_INLINE size_t MaxMetablockSize(
  88. const BrotliEncoderParams* params) {
  89. int bits =
  90. BROTLI_MIN(int, ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS);
  91. return (size_t)1 << bits;
  92. }
  93. /* When searching for backward references and have not seen matches for a long
  94. time, we can skip some match lookups. Unsuccessful match lookups are very
  95. expensive and this kind of a heuristic speeds up compression quite a lot.
  96. At first 8 byte strides are taken and every second byte is put to hasher.
  97. After 4x more literals stride by 16 bytes, every put 4-th byte to hasher.
  98. Applied only to qualities 2 to 9. */
  99. static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch(
  100. const BrotliEncoderParams* params) {
  101. return params->quality < 9 ? 64 : 512;
  102. }
  103. static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
  104. BrotliHasherParams* hparams) {
  105. if (params->quality > 9) {
  106. hparams->type = 10;
  107. } else if (params->quality == 4 && params->size_hint >= (1 << 20)) {
  108. hparams->type = 54;
  109. } else if (params->quality < 5) {
  110. hparams->type = params->quality;
  111. } else if (params->lgwin <= 16) {
  112. hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
  113. } else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
  114. hparams->type = 6;
  115. hparams->block_bits = params->quality - 1;
  116. hparams->bucket_bits = 15;
  117. hparams->hash_len = 5;
  118. hparams->num_last_distances_to_check =
  119. params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
  120. } else {
  121. hparams->type = 5;
  122. hparams->block_bits = params->quality - 1;
  123. hparams->bucket_bits = params->quality < 7 ? 14 : 15;
  124. hparams->num_last_distances_to_check =
  125. params->quality < 7 ? 4 : params->quality < 9 ? 10 : 16;
  126. }
  127. if (params->lgwin > 24) {
  128. /* Different hashers for large window brotli: not for qualities <= 2,
  129. these are too fast for large window. Not for qualities >= 10: their
  130. hasher already works well with large window. So the changes are:
  131. H3 --> H35: for quality 3.
  132. H54 --> H55: for quality 4 with size hint > 1MB
  133. H6 --> H65: for qualities 5, 6, 7, 8, 9. */
  134. if (hparams->type == 3) {
  135. hparams->type = 35;
  136. }
  137. if (hparams->type == 54) {
  138. hparams->type = 55;
  139. }
  140. if (hparams->type == 6) {
  141. hparams->type = 65;
  142. }
  143. }
  144. }
  145. #endif /* BROTLI_ENC_QUALITY_H_ */