1
0

lzma_encoder_private.h 3.8 KB

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