lzma_encoder_private.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_encoder_private.h
  4. /// \brief Private definitions for LZMA encoder
  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_LZMA_ENCODER_PRIVATE_H
  14. #define LZMA_LZMA_ENCODER_PRIVATE_H
  15. #include "lz_encoder.h"
  16. #include "range_encoder.h"
  17. #include "lzma_common.h"
  18. #include "lzma_encoder.h"
  19. // Macro to compare if the first two bytes in two buffers differ. This is
  20. // needed in lzma_lzma_optimum_*() to test if the match is at least
  21. // MATCH_LEN_MIN bytes. Unaligned access gives tiny gain so there's no
  22. // reason to not use it when it is supported.
  23. #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
  24. # define not_equal_16(a, b) \
  25. (*(const uint16_t *)(a) != *(const uint16_t *)(b))
  26. #else
  27. # define not_equal_16(a, b) \
  28. ((a)[0] != (b)[0] || (a)[1] != (b)[1])
  29. #endif
  30. // Optimal - Number of entries in the optimum array.
  31. #define OPTS (1 << 12)
  32. typedef struct {
  33. probability choice;
  34. probability choice2;
  35. probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
  36. probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
  37. probability high[LEN_HIGH_SYMBOLS];
  38. uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
  39. uint32_t table_size;
  40. uint32_t counters[POS_STATES_MAX];
  41. } lzma_length_encoder;
  42. typedef struct {
  43. lzma_lzma_state state;
  44. bool prev_1_is_literal;
  45. bool prev_2;
  46. uint32_t pos_prev_2;
  47. uint32_t back_prev_2;
  48. uint32_t price;
  49. uint32_t pos_prev; // pos_next;
  50. uint32_t back_prev;
  51. uint32_t backs[REPS];
  52. } lzma_optimal;
  53. struct lzma_lzma1_encoder_s {
  54. /// Range encoder
  55. lzma_range_encoder rc;
  56. /// State
  57. lzma_lzma_state state;
  58. /// The four most recent match distances
  59. uint32_t reps[REPS];
  60. /// Array of match candidates
  61. lzma_match matches[MATCH_LEN_MAX + 1];
  62. /// Number of match candidates in matches[]
  63. uint32_t matches_count;
  64. /// Variable to hold the length of the longest match between calls
  65. /// to lzma_lzma_optimum_*().
  66. uint32_t longest_match_length;
  67. /// True if using getoptimumfast
  68. bool fast_mode;
  69. /// True if the encoder has been initialized by encoding the first
  70. /// byte as a literal.
  71. bool is_initialized;
  72. /// True if the range encoder has been flushed, but not all bytes
  73. /// have been written to the output buffer yet.
  74. bool is_flushed;
  75. uint32_t pos_mask; ///< (1 << pos_bits) - 1
  76. uint32_t literal_context_bits;
  77. uint32_t literal_pos_mask;
  78. // These are the same as in lzma_decoder.c. See comments there.
  79. probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
  80. probability is_match[STATES][POS_STATES_MAX];
  81. probability is_rep[STATES];
  82. probability is_rep0[STATES];
  83. probability is_rep1[STATES];
  84. probability is_rep2[STATES];
  85. probability is_rep0_long[STATES][POS_STATES_MAX];
  86. probability dist_slot[DIST_STATES][DIST_SLOTS];
  87. probability dist_special[FULL_DISTANCES - DIST_MODEL_END];
  88. probability dist_align[ALIGN_SIZE];
  89. // These are the same as in lzma_decoder.c except that the encoders
  90. // include also price tables.
  91. lzma_length_encoder match_len_encoder;
  92. lzma_length_encoder rep_len_encoder;
  93. // Price tables
  94. uint32_t dist_slot_prices[DIST_STATES][DIST_SLOTS];
  95. uint32_t dist_prices[DIST_STATES][FULL_DISTANCES];
  96. uint32_t dist_table_size;
  97. uint32_t match_price_count;
  98. uint32_t align_prices[ALIGN_SIZE];
  99. uint32_t align_price_count;
  100. // Optimal
  101. uint32_t opts_end_index;
  102. uint32_t opts_current_index;
  103. lzma_optimal opts[OPTS];
  104. };
  105. extern void lzma_lzma_optimum_fast(
  106. lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf,
  107. uint32_t *restrict back_res, uint32_t *restrict len_res);
  108. extern void lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder,
  109. lzma_mf *restrict mf, uint32_t *restrict back_res,
  110. uint32_t *restrict len_res, uint32_t position);
  111. #endif