]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_encoder_private.h
Some API cleanups
[icculus/xz.git] / src / liblzma / lzma / lzma_encoder_private.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_encoder_private.h
4 /// \brief      Private definitions for LZMA encoder
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #ifndef LZMA_LZMA_ENCODER_PRIVATE_H
22 #define LZMA_LZMA_ENCODER_PRIVATE_H
23
24 #include "lz_encoder.h"
25 #include "range_encoder.h"
26 #include "lzma_common.h"
27 #include "lzma_encoder.h"
28
29
30 // Macro to compare if the first two bytes in two buffers differ. This is
31 // needed in lzma_lzma_optimum_*() to test if the match is at least
32 // MATCH_LEN_MIN bytes. Unaligned access gives tiny gain so there's no
33 // reason to not use it when it is supported.
34 #ifdef HAVE_FAST_UNALIGNED_ACCESS
35 #       define not_equal_16(a, b) \
36                 (*(const uint16_t *)(a) != *(const uint16_t *)(b))
37 #else
38 #       define not_equal_16(a, b) \
39                 ((a)[0] != (b)[0] || (a)[1] != (b)[1])
40 #endif
41
42
43 // Optimal - Number of entries in the optimum array.
44 #define OPTS (1 << 12)
45
46
47 typedef struct {
48         probability choice;
49         probability choice2;
50         probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
51         probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
52         probability high[LEN_HIGH_SYMBOLS];
53
54         uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
55         uint32_t table_size;
56         uint32_t counters[POS_STATES_MAX];
57
58 } lzma_length_encoder;
59
60
61 typedef struct {
62         lzma_lzma_state state;
63
64         bool prev_1_is_literal;
65         bool prev_2;
66
67         uint32_t pos_prev_2;
68         uint32_t back_prev_2;
69
70         uint32_t price;
71         uint32_t pos_prev;  // pos_next;
72         uint32_t back_prev;
73
74         uint32_t backs[REP_DISTANCES];
75
76 } lzma_optimal;
77
78
79 struct lzma_coder_s {
80         /// Range encoder
81         lzma_range_encoder rc;
82
83         /// State
84         lzma_lzma_state state;
85
86         /// The four most recent match distances
87         uint32_t reps[REP_DISTANCES];
88
89         /// Array of match candidates
90         lzma_match matches[MATCH_LEN_MAX + 1];
91
92         /// Number of match candidates in matches[]
93         uint32_t matches_count;
94
95         /// Varibale to hold the length of the longest match between calls
96         /// to lzma_lzma_optimum_*().
97         uint32_t longest_match_length;
98
99         /// True if using getoptimumfast
100         bool fast_mode;
101
102         /// True if the encoder has been initialized by encoding the first
103         /// byte as a literal.
104         bool is_initialized;
105
106         /// True if the range encoder has been flushed, but not all bytes
107         /// have been written to the output buffer yet.
108         bool is_flushed;
109
110         uint32_t pos_mask;         ///< (1 << pos_bits) - 1
111         uint32_t literal_context_bits;
112         uint32_t literal_pos_mask;
113
114         // These are the same as in lzma_decoder.c. See comments there.
115         probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
116         probability is_match[STATES][POS_STATES_MAX];
117         probability is_rep[STATES];
118         probability is_rep0[STATES];
119         probability is_rep1[STATES];
120         probability is_rep2[STATES];
121         probability is_rep0_long[STATES][POS_STATES_MAX];
122         probability pos_slot[LEN_TO_POS_STATES][POS_SLOTS];
123         probability pos_special[FULL_DISTANCES - END_POS_MODEL_INDEX];
124         probability pos_align[ALIGN_TABLE_SIZE];
125
126         // These are the same as in lzma_decoder.c except that the encoders
127         // include also price tables.
128         lzma_length_encoder match_len_encoder;
129         lzma_length_encoder rep_len_encoder;
130
131         // Price tables
132         uint32_t pos_slot_prices[LEN_TO_POS_STATES][POS_SLOTS];
133         uint32_t distances_prices[LEN_TO_POS_STATES][FULL_DISTANCES];
134         uint32_t dist_table_size;
135         uint32_t match_price_count;
136
137         uint32_t align_prices[ALIGN_TABLE_SIZE];
138         uint32_t align_price_count;
139
140         // Optimal
141         uint32_t opts_end_index;
142         uint32_t opts_current_index;
143         lzma_optimal opts[OPTS];
144 };
145
146
147 extern void lzma_lzma_optimum_fast(
148                 lzma_coder *restrict coder, lzma_mf *restrict mf,
149                 uint32_t *restrict back_res, uint32_t *restrict len_res);
150
151 extern void lzma_lzma_optimum_normal(lzma_coder *restrict coder,
152                 lzma_mf *restrict mf, uint32_t *restrict back_res,
153                 uint32_t *restrict len_res, uint32_t position);
154
155 #endif