23 const uint32_t prev_byte,
const bool match_mode,
43 =
offset + match_bit + (symbol >> 8);
48 offset &= ~(match_byte ^ symbol);
50 }
while (symbol < (
UINT32_C(1) << 16));
121 const uint32_t dist_slot = get_dist_slot_2(dist);
161 = dist_slot_prices[
i];
169 const uint32_t footer_bits = ((dist_slot >> 1) - 1);
170 const uint32_t base = (2 | (dist_slot & 1)) << footer_bits;
173 footer_bits,
i - base);
179 dist_state][dist_slot];
219 #define is_short_rep(optimal) \
220 ((optimal).back_prev == 0)
227 coder->opts_end_index = cur;
229 uint32_t pos_mem = coder->opts[cur].pos_prev;
230 uint32_t back_mem = coder->opts[cur].back_prev;
233 if (coder->opts[cur].prev_1_is_literal) {
235 coder->opts[pos_mem].pos_prev = pos_mem - 1;
237 if (coder->opts[cur].prev_2) {
238 coder->opts[pos_mem - 1].prev_1_is_literal
240 coder->opts[pos_mem - 1].pos_prev
241 = coder->opts[cur].pos_prev_2;
242 coder->opts[pos_mem - 1].back_prev
243 = coder->opts[cur].back_prev_2;
250 back_mem = coder->opts[pos_prev].back_prev;
251 pos_mem = coder->opts[pos_prev].pos_prev;
253 coder->opts[pos_prev].back_prev = back_cur;
254 coder->opts[pos_prev].pos_prev = cur;
259 coder->opts_current_index = coder->opts[0].pos_prev;
260 *len_res = coder->opts[0].pos_prev;
261 *back_res = coder->opts[0].back_prev;
276 const uint32_t nice_len = mf->nice_len;
281 if (mf->read_ahead == 0) {
282 len_main =
mf_find(mf, &matches_count, coder->matches);
284 assert(mf->read_ahead == 1);
285 len_main = coder->longest_match_length;
286 matches_count = coder->matches_count;
302 const uint8_t *
const buf_back =
buf - coder->reps[
i] - 1;
309 rep_lens[
i] = lzma_memcmplen(
buf, buf_back, 2, buf_avail);
311 if (rep_lens[
i] > rep_lens[rep_max_index])
315 if (rep_lens[rep_max_index] >= nice_len) {
316 *back_res = rep_max_index;
317 *len_res = rep_lens[rep_max_index];
323 if (len_main >= nice_len) {
324 *back_res = coder->matches[matches_count - 1].dist +
REPS;
331 const uint8_t match_byte = *(
buf - coder->reps[0] - 1);
333 if (len_main < 2 && current_byte != match_byte
334 && rep_lens[rep_max_index] < 2) {
340 coder->opts[0].state = coder->state;
342 const uint32_t pos_state = position & coder->pos_mask;
345 coder->is_match[coder->state][pos_state])
348 match_byte, current_byte);
353 coder->is_match[coder->state][pos_state]);
354 const uint32_t rep_match_price = match_price
357 if (match_byte == current_byte) {
358 const uint32_t short_rep_price = rep_match_price
360 coder, coder->state, pos_state);
362 if (short_rep_price < coder->opts[1].price) {
363 coder->opts[1].price = short_rep_price;
368 const uint32_t len_end =
my_max(len_main, rep_lens[rep_max_index]);
371 *back_res = coder->opts[1].back_prev;
376 coder->opts[1].pos_prev = 0;
379 coder->opts[0].backs[
i] = coder->reps[
i];
384 }
while (--
len >= 2);
393 coder,
i, coder->state, pos_state);
396 const uint32_t cur_and_len_price = price
398 &coder->rep_len_encoder,
401 if (cur_and_len_price < coder->opts[rep_len].price) {
402 coder->opts[rep_len].price = cur_and_len_price;
403 coder->opts[rep_len].pos_prev = 0;
404 coder->opts[rep_len].back_prev =
i;
405 coder->opts[rep_len].prev_1_is_literal =
false;
407 }
while (--rep_len >= 2);
411 const uint32_t normal_match_price = match_price
414 len = rep_lens[0] >= 2 ? rep_lens[0] + 1 : 2;
415 if (
len <= len_main) {
417 while (
len > coder->matches[
i].len)
421 const uint32_t dist = coder->matches[
i].dist;
422 const uint32_t cur_and_len_price = normal_match_price
424 dist,
len, pos_state);
426 if (cur_and_len_price < coder->opts[
len].price) {
427 coder->opts[
len].price = cur_and_len_price;
428 coder->opts[
len].pos_prev = 0;
429 coder->opts[
len].back_prev = dist +
REPS;
430 coder->opts[
len].prev_1_is_literal =
false;
433 if (
len == coder->matches[
i].len)
434 if (++
i == matches_count)
474 if (pos_prev == cur - 1) {
498 for (
i = 1;
i <=
pos; ++
i)
520 const uint8_t match_byte = *(
buf - reps[0] - 1);
524 const uint32_t cur_and_1_price = cur_price
529 bool next_is_literal =
false;
531 if (cur_and_1_price < coder->opts[cur + 1].price) {
532 coder->
opts[cur + 1].
price = cur_and_1_price;
535 next_is_literal =
true;
538 const uint32_t match_price = cur_price
540 const uint32_t rep_match_price = match_price
543 if (match_byte == current_byte
547 const uint32_t short_rep_price = rep_match_price
550 if (short_rep_price <= coder->opts[cur + 1].price) {
551 coder->
opts[cur + 1].
price = short_rep_price;
554 next_is_literal =
true;
558 if (buf_avail_full < 2)
563 if (!next_is_literal && match_byte != current_byte) {
565 const uint8_t *
const buf_back =
buf - reps[0] - 1;
575 const uint32_t next_rep_match_price = cur_and_1_price
585 const uint32_t cur_and_len_price = next_rep_match_price
587 state_2, pos_state_next);
589 if (cur_and_len_price < coder->opts[
offset].price) {
603 for (
uint32_t rep_index = 0; rep_index <
REPS; ++rep_index) {
604 const uint8_t *
const buf_back =
buf - reps[rep_index] - 1;
608 uint32_t len_test = lzma_memcmplen(
buf, buf_back, 2, buf_avail);
610 while (len_end < cur + len_test)
613 const uint32_t len_test_temp = len_test;
615 coder, rep_index,
state, pos_state);
618 const uint32_t cur_and_len_price = price
620 len_test, pos_state);
622 if (cur_and_len_price < coder->opts[cur + len_test].price) {
623 coder->
opts[cur + len_test].
price = cur_and_len_price;
628 }
while (--len_test >= 2);
630 len_test = len_test_temp;
633 start_len = len_test + 1;
638 len_test_2 + nice_len);
641 if (len_test_2 <
limit)
642 len_test_2 = lzma_memcmplen(
buf, buf_back, len_test_2,
limit);
644 len_test_2 -= len_test + 1;
646 if (len_test_2 >= 2) {
652 const uint32_t cur_and_len_literal_price = price
657 buf[len_test - 1],
true,
658 buf_back[len_test],
buf[len_test]);
662 pos_state_next = (position + len_test + 1) & coder->
pos_mask;
664 const uint32_t next_rep_match_price = cur_and_len_literal_price
674 const uint32_t cur_and_len_price = next_rep_match_price
676 state_2, pos_state_next);
678 if (cur_and_len_price < coder->opts[
offset].price) {
693 if (new_len > buf_avail) {
697 while (new_len > coder->
matches[matches_count].
len)
700 coder->
matches[matches_count++].
len = new_len;
704 if (new_len >= start_len) {
705 const uint32_t normal_match_price = match_price
708 while (len_end < cur + new_len)
715 for (
uint32_t len_test = start_len; ; ++len_test) {
717 uint32_t cur_and_len_price = normal_match_price
719 cur_back, len_test, pos_state);
721 if (cur_and_len_price < coder->opts[cur + len_test].price) {
722 coder->
opts[cur + len_test].
price = cur_and_len_price;
731 const uint8_t *
const buf_back =
buf - cur_back - 1;
734 len_test_2 + nice_len);
739 if (len_test_2 <
limit)
740 len_test_2 = lzma_memcmplen(
buf, buf_back,
743 len_test_2 -= len_test + 1;
745 if (len_test_2 >= 2) {
749 = (position + len_test) & coder->
pos_mask;
751 const uint32_t cur_and_len_literal_price = cur_and_len_price
753 coder->
is_match[state_2][pos_state_next])
762 pos_state_next = (pos_state_next + 1) & coder->
pos_mask;
765 = cur_and_len_literal_price
767 coder->
is_match[state_2][pos_state_next])
776 cur_and_len_price = next_rep_match_price
778 state_2, pos_state_next);
780 if (cur_and_len_price < coder->opts[
offset].price) {
793 if (++
i == matches_count)
810 if (coder->opts_end_index != coder->opts_current_index) {
811 assert(mf->read_ahead > 0);
812 *len_res = coder->opts[coder->opts_current_index].pos_prev
813 - coder->opts_current_index;
814 *back_res = coder->opts[coder->opts_current_index].back_prev;
815 coder->opts_current_index = coder->opts[
816 coder->opts_current_index].pos_prev;
823 if (mf->read_ahead == 0) {
824 if (coder->match_price_count >= (1 << 7))
840 memcpy(reps, coder->reps,
sizeof(reps));
843 for (cur = 1; cur < len_end; ++cur) {
846 coder->longest_match_length =
mf_find(
847 mf, &coder->matches_count, coder->matches);
849 if (coder->longest_match_length >= mf->nice_len)
853 position + cur, cur, mf->nice_len,
857 backward(coder, len_res, back_res, cur);
Kind of two-bit version of bit scan reverse.
static uint32_t get_dist_slot(uint32_t dist)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static uint32_t mf_avail(const lzma_mf *mf)
Get the number of bytes that haven't been ran through the match finder yet.
static const uint8_t * mf_ptr(const lzma_mf *mf)
Get pointer to the first byte not ran through the match finder.
static void mf_skip(lzma_mf *mf, uint32_t amount)
#define mf_find
Since everything else begins with mf_, use it also for lzma_mf_find().
#define update_literal(state)
Indicate that the latest state was a literal.
#define is_literal_state(state)
Test if the previous state was a literal.
#define update_match(state)
Indicate that the latest state was a match.
#define get_dist_state(len)
#define literal_subcoder(probs, lc, lp_mask, pos, prev_byte)
#define update_long_rep(state)
Indicate that the latest state was a long repeated match.
#define update_short_rep(state)
Indicate that the latest state was a short match.
static void make_literal(lzma_optimal *optimal)
static uint32_t get_literal_price(const lzma_lzma1_encoder *const coder, const uint32_t pos, const uint32_t prev_byte, const bool match_mode, uint32_t match_byte, uint32_t symbol)
static uint32_t get_dist_len_price(const lzma_lzma1_encoder *const coder, const uint32_t dist, const uint32_t len, const uint32_t pos_state)
static void make_short_rep(lzma_optimal *optimal)
void lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint32_t *restrict back_res, uint32_t *restrict len_res, uint32_t position)
static void fill_align_prices(lzma_lzma1_encoder *coder)
static uint32_t helper2(lzma_lzma1_encoder *coder, uint32_t *reps, const uint8_t *buf, uint32_t len_end, uint32_t position, const uint32_t cur, const uint32_t nice_len, const uint32_t buf_avail_full)
static uint32_t get_pure_rep_price(const lzma_lzma1_encoder *const coder, const uint32_t rep_index, const lzma_lzma_state state, uint32_t pos_state)
#define is_short_rep(optimal)
static uint32_t get_rep_price(const lzma_lzma1_encoder *const coder, const uint32_t rep_index, const uint32_t len, const lzma_lzma_state state, const uint32_t pos_state)
static void fill_dist_prices(lzma_lzma1_encoder *coder)
static uint32_t helper1(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint32_t *restrict back_res, uint32_t *restrict len_res, uint32_t position)
static void backward(lzma_lzma1_encoder *restrict coder, uint32_t *restrict len_res, uint32_t *restrict back_res, uint32_t cur)
static uint32_t get_len_price(const lzma_length_encoder *const lencoder, const uint32_t len, const uint32_t pos_state)
static uint32_t get_short_rep_price(const lzma_lzma1_encoder *const coder, const lzma_lzma_state state, const uint32_t pos_state)
Private definitions for LZMA encoder.
#define not_equal_16(a, b)
Optimized comparison of two buffers.
assert(limit<=UINT32_MAX/2)
static uint32_t const uint8_t uint32_t uint32_t limit
static uint32_t rc_bit_price(const probability prob, const uint32_t bit)
static uint32_t rc_bittree_price(const probability *const probs, const uint32_t bit_levels, uint32_t symbol)
static uint32_t rc_bittree_reverse_price(const probability *const probs, uint32_t bit_levels, uint32_t symbol)
static uint32_t rc_bit_0_price(const probability prob)
static uint32_t rc_bit_1_price(const probability prob)
#define RC_INFINITY_PRICE
static uint32_t rc_direct_price(const uint32_t bits)
uint16_t probability
Type of probabilities used with range coder.
uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS]
uint32_t match_price_count
lzma_match matches[MATCH_LEN_MAX+1]
Array of match candidates.
probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE]
probability is_rep0_long[STATES][POS_STATES_MAX]
uint32_t align_price_count
probability is_rep1[STATES]
uint32_t pos_mask
(1 << pos_bits) - 1
probability is_rep0[STATES]
probability is_rep2[STATES]
uint32_t longest_match_length
probability dist_special[FULL_DISTANCES - DIST_MODEL_END]
uint32_t literal_context_bits
uint32_t align_prices[ALIGN_SIZE]
probability is_rep[STATES]
lzma_length_encoder match_len_encoder
uint32_t dist_prices[DIST_STATES][FULL_DISTANCES]
uint32_t literal_pos_mask
probability dist_slot[DIST_STATES][DIST_SLOTS]
uint32_t dist_slot_prices[DIST_STATES][DIST_SLOTS]
uint32_t matches_count
Number of match candidates in matches[].
probability dist_align[ALIGN_SIZE]
probability is_match[STATES][POS_STATES_MAX]
lzma_length_encoder rep_len_encoder