lz_encoder.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lz_encoder.h
  4. /// \brief LZ in window and match finder API
  5. ///
  6. // Authors: Igor Pavlov
  7. // Lasse Collin
  8. //
  9. // This file has been put into the public domain.
  10. // You can do whatever you want with this file.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #ifndef LZMA_LZ_ENCODER_H
  14. #define LZMA_LZ_ENCODER_H
  15. #include "common.h"
  16. /// A table of these is used by the LZ-based encoder to hold
  17. /// the length-distance pairs found by the match finder.
  18. typedef struct {
  19. uint32_t len;
  20. uint32_t dist;
  21. } lzma_match;
  22. typedef struct lzma_mf_s lzma_mf;
  23. struct lzma_mf_s {
  24. ///////////////
  25. // In Window //
  26. ///////////////
  27. /// Pointer to buffer with data to be compressed
  28. uint8_t *buffer;
  29. /// Total size of the allocated buffer (that is, including all
  30. /// the extra space)
  31. uint32_t size;
  32. /// Number of bytes that must be kept available in our input history.
  33. /// That is, once keep_size_before bytes have been processed,
  34. /// buffer[read_pos - keep_size_before] is the oldest byte that
  35. /// must be available for reading.
  36. uint32_t keep_size_before;
  37. /// Number of bytes that must be kept in buffer after read_pos.
  38. /// That is, read_pos <= write_pos - keep_size_after as long as
  39. /// action is LZMA_RUN; when action != LZMA_RUN, read_pos is allowed
  40. /// to reach write_pos so that the last bytes get encoded too.
  41. uint32_t keep_size_after;
  42. /// Match finders store locations of matches using 32-bit integers.
  43. /// To avoid adjusting several megabytes of integers every time the
  44. /// input window is moved with move_window, we only adjust the
  45. /// offset of the buffer. Thus, buffer[value_in_hash_table - offset]
  46. /// is the byte pointed by value_in_hash_table.
  47. uint32_t offset;
  48. /// buffer[read_pos] is the next byte to run through the match
  49. /// finder. This is incremented in the match finder once the byte
  50. /// has been processed.
  51. uint32_t read_pos;
  52. /// Number of bytes that have been ran through the match finder, but
  53. /// which haven't been encoded by the LZ-based encoder yet.
  54. uint32_t read_ahead;
  55. /// As long as read_pos is less than read_limit, there is enough
  56. /// input available in buffer for at least one encoding loop.
  57. ///
  58. /// Because of the stateful API, read_limit may and will get greater
  59. /// than read_pos quite often. This is taken into account when
  60. /// calculating the value for keep_size_after.
  61. uint32_t read_limit;
  62. /// buffer[write_pos] is the first byte that doesn't contain valid
  63. /// uncompressed data; that is, the next input byte will be copied
  64. /// to buffer[write_pos].
  65. uint32_t write_pos;
  66. /// Number of bytes not hashed before read_pos. This is needed to
  67. /// restart the match finder after LZMA_SYNC_FLUSH.
  68. uint32_t pending;
  69. //////////////////
  70. // Match Finder //
  71. //////////////////
  72. /// Find matches. Returns the number of distance-length pairs written
  73. /// to the matches array. This is called only via lzma_mf_find().
  74. uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
  75. /// Skips num bytes. This is like find() but doesn't make the
  76. /// distance-length pairs available, thus being a little faster.
  77. /// This is called only via mf_skip().
  78. void (*skip)(lzma_mf *mf, uint32_t num);
  79. uint32_t *hash;
  80. uint32_t *son;
  81. uint32_t cyclic_pos;
  82. uint32_t cyclic_size; // Must be dictionary size + 1.
  83. uint32_t hash_mask;
  84. /// Maximum number of loops in the match finder
  85. uint32_t depth;
  86. /// Maximum length of a match that the match finder will try to find.
  87. uint32_t nice_len;
  88. /// Maximum length of a match supported by the LZ-based encoder.
  89. /// If the longest match found by the match finder is nice_len,
  90. /// mf_find() tries to expand it up to match_len_max bytes.
  91. uint32_t match_len_max;
  92. /// When running out of input, binary tree match finders need to know
  93. /// if it is due to flushing or finishing. The action is used also
  94. /// by the LZ-based encoders themselves.
  95. lzma_action action;
  96. /// Number of elements in hash[]
  97. uint32_t hash_count;
  98. /// Number of elements in son[]
  99. uint32_t sons_count;
  100. };
  101. typedef struct {
  102. /// Extra amount of data to keep available before the "actual"
  103. /// dictionary.
  104. size_t before_size;
  105. /// Size of the history buffer
  106. size_t dict_size;
  107. /// Extra amount of data to keep available after the "actual"
  108. /// dictionary.
  109. size_t after_size;
  110. /// Maximum length of a match that the LZ-based encoder can accept.
  111. /// This is used to extend matches of length nice_len to the
  112. /// maximum possible length.
  113. size_t match_len_max;
  114. /// Match finder will search matches up to this length.
  115. /// This must be less than or equal to match_len_max.
  116. size_t nice_len;
  117. /// Type of the match finder to use
  118. lzma_match_finder match_finder;
  119. /// Maximum search depth
  120. uint32_t depth;
  121. /// TODO: Comment
  122. const uint8_t *preset_dict;
  123. uint32_t preset_dict_size;
  124. } lzma_lz_options;
  125. // The total usable buffer space at any moment outside the match finder:
  126. // before_size + dict_size + after_size + match_len_max
  127. //
  128. // In reality, there's some extra space allocated to prevent the number of
  129. // memmove() calls reasonable. The bigger the dict_size is, the bigger
  130. // this extra buffer will be since with bigger dictionaries memmove() would
  131. // also take longer.
  132. //
  133. // A single encoder loop in the LZ-based encoder may call the match finder
  134. // (mf_find() or mf_skip()) at most after_size times. In other words,
  135. // a single encoder loop may increment lzma_mf.read_pos at most after_size
  136. // times. Since matches are looked up to
  137. // lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
  138. // amount of extra buffer needed after dict_size becomes
  139. // after_size + match_len_max.
  140. //
  141. // before_size has two uses. The first one is to keep literals available
  142. // in cases when the LZ-based encoder has made some read ahead.
  143. // TODO: Maybe this could be changed by making the LZ-based encoders to
  144. // store the actual literals as they do with length-distance pairs.
  145. //
  146. // Algorithms such as LZMA2 first try to compress a chunk, and then check
  147. // if the encoded result is smaller than the uncompressed one. If the chunk
  148. // was uncompressible, it is better to store it in uncompressed form in
  149. // the output stream. To do this, the whole uncompressed chunk has to be
  150. // still available in the history buffer. before_size achieves that.
  151. typedef struct {
  152. /// Data specific to the LZ-based encoder
  153. void *coder;
  154. /// Function to encode from *dict to out[]
  155. lzma_ret (*code)(void *coder,
  156. lzma_mf *restrict mf, uint8_t *restrict out,
  157. size_t *restrict out_pos, size_t out_size);
  158. /// Free allocated resources
  159. void (*end)(void *coder, const lzma_allocator *allocator);
  160. /// Update the options in the middle of the encoding.
  161. lzma_ret (*options_update)(void *coder, const lzma_filter *filter);
  162. } lzma_lz_encoder;
  163. // Basic steps:
  164. // 1. Input gets copied into the dictionary.
  165. // 2. Data in dictionary gets run through the match finder byte by byte.
  166. // 3. The literals and matches are encoded using e.g. LZMA.
  167. //
  168. // The bytes that have been ran through the match finder, but not encoded yet,
  169. // are called `read ahead'.
  170. /// Get pointer to the first byte not ran through the match finder
  171. static inline const uint8_t *
  172. mf_ptr(const lzma_mf *mf)
  173. {
  174. return mf->buffer + mf->read_pos;
  175. }
  176. /// Get the number of bytes that haven't been ran through the match finder yet.
  177. static inline uint32_t
  178. mf_avail(const lzma_mf *mf)
  179. {
  180. return mf->write_pos - mf->read_pos;
  181. }
  182. /// Get the number of bytes that haven't been encoded yet (some of these
  183. /// bytes may have been ran through the match finder though).
  184. static inline uint32_t
  185. mf_unencoded(const lzma_mf *mf)
  186. {
  187. return mf->write_pos - mf->read_pos + mf->read_ahead;
  188. }
  189. /// Calculate the absolute offset from the beginning of the most recent
  190. /// dictionary reset. Only the lowest four bits are important, so there's no
  191. /// problem that we don't know the 64-bit size of the data encoded so far.
  192. ///
  193. /// NOTE: When moving the input window, we need to do it so that the lowest
  194. /// bits of dict->read_pos are not modified to keep this macro working
  195. /// as intended.
  196. static inline uint32_t
  197. mf_position(const lzma_mf *mf)
  198. {
  199. return mf->read_pos - mf->read_ahead;
  200. }
  201. /// Since everything else begins with mf_, use it also for lzma_mf_find().
  202. #define mf_find lzma_mf_find
  203. /// Skip the given number of bytes. This is used when a good match was found.
  204. /// For example, if mf_find() finds a match of 200 bytes long, the first byte
  205. /// of that match was already consumed by mf_find(), and the rest 199 bytes
  206. /// have to be skipped with mf_skip(mf, 199).
  207. static inline void
  208. mf_skip(lzma_mf *mf, uint32_t amount)
  209. {
  210. if (amount != 0) {
  211. mf->skip(mf, amount);
  212. mf->read_ahead += amount;
  213. }
  214. }
  215. /// Copies at most *left number of bytes from the history buffer
  216. /// to out[]. This is needed by LZMA2 to encode uncompressed chunks.
  217. static inline void
  218. mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
  219. size_t *left)
  220. {
  221. const size_t out_avail = out_size - *out_pos;
  222. const size_t copy_size = my_min(out_avail, *left);
  223. assert(mf->read_ahead == 0);
  224. assert(mf->read_pos >= *left);
  225. memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
  226. copy_size);
  227. *out_pos += copy_size;
  228. *left -= copy_size;
  229. return;
  230. }
  231. extern lzma_ret lzma_lz_encoder_init(
  232. lzma_next_coder *next, const lzma_allocator *allocator,
  233. const lzma_filter_info *filters,
  234. lzma_ret (*lz_init)(lzma_lz_encoder *lz,
  235. const lzma_allocator *allocator, const void *options,
  236. lzma_lz_options *lz_options));
  237. extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
  238. // These are only for LZ encoder's internal use.
  239. extern uint32_t lzma_mf_find(
  240. lzma_mf *mf, uint32_t *count, lzma_match *matches);
  241. extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
  242. extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
  243. extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
  244. extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
  245. extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
  246. extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
  247. extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
  248. extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
  249. extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
  250. extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
  251. #endif