lzma_encoder_optimum_normal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_encoder_optimum_normal.c
  4. //
  5. // Author: Igor Pavlov
  6. //
  7. // This file has been put into the public domain.
  8. // You can do whatever you want with this file.
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "lzma_encoder_private.h"
  12. #include "fastpos.h"
  13. #include "memcmplen.h"
  14. ////////////
  15. // Prices //
  16. ////////////
  17. static uint32_t
  18. get_literal_price(const lzma_lzma1_encoder *const coder, const uint32_t pos,
  19. const uint32_t prev_byte, const bool match_mode,
  20. uint32_t match_byte, uint32_t symbol)
  21. {
  22. const probability *const subcoder = literal_subcoder(coder->literal,
  23. coder->literal_context_bits, coder->literal_pos_mask,
  24. pos, prev_byte);
  25. uint32_t price = 0;
  26. if (!match_mode) {
  27. price = rc_bittree_price(subcoder, 8, symbol);
  28. } else {
  29. uint32_t offset = 0x100;
  30. symbol += UINT32_C(1) << 8;
  31. do {
  32. match_byte <<= 1;
  33. const uint32_t match_bit = match_byte & offset;
  34. const uint32_t subcoder_index
  35. = offset + match_bit + (symbol >> 8);
  36. const uint32_t bit = (symbol >> 7) & 1;
  37. price += rc_bit_price(subcoder[subcoder_index], bit);
  38. symbol <<= 1;
  39. offset &= ~(match_byte ^ symbol);
  40. } while (symbol < (UINT32_C(1) << 16));
  41. }
  42. return price;
  43. }
  44. static inline uint32_t
  45. get_len_price(const lzma_length_encoder *const lencoder,
  46. const uint32_t len, const uint32_t pos_state)
  47. {
  48. // NOTE: Unlike the other price tables, length prices are updated
  49. // in lzma_encoder.c
  50. return lencoder->prices[pos_state][len - MATCH_LEN_MIN];
  51. }
  52. static inline uint32_t
  53. get_short_rep_price(const lzma_lzma1_encoder *const coder,
  54. const lzma_lzma_state state, const uint32_t pos_state)
  55. {
  56. return rc_bit_0_price(coder->is_rep0[state])
  57. + rc_bit_0_price(coder->is_rep0_long[state][pos_state]);
  58. }
  59. static inline uint32_t
  60. get_pure_rep_price(const lzma_lzma1_encoder *const coder, const uint32_t rep_index,
  61. const lzma_lzma_state state, uint32_t pos_state)
  62. {
  63. uint32_t price;
  64. if (rep_index == 0) {
  65. price = rc_bit_0_price(coder->is_rep0[state]);
  66. price += rc_bit_1_price(coder->is_rep0_long[state][pos_state]);
  67. } else {
  68. price = rc_bit_1_price(coder->is_rep0[state]);
  69. if (rep_index == 1) {
  70. price += rc_bit_0_price(coder->is_rep1[state]);
  71. } else {
  72. price += rc_bit_1_price(coder->is_rep1[state]);
  73. price += rc_bit_price(coder->is_rep2[state],
  74. rep_index - 2);
  75. }
  76. }
  77. return price;
  78. }
  79. static inline uint32_t
  80. get_rep_price(const lzma_lzma1_encoder *const coder, const uint32_t rep_index,
  81. const uint32_t len, const lzma_lzma_state state,
  82. const uint32_t pos_state)
  83. {
  84. return get_len_price(&coder->rep_len_encoder, len, pos_state)
  85. + get_pure_rep_price(coder, rep_index, state, pos_state);
  86. }
  87. static inline uint32_t
  88. get_dist_len_price(const lzma_lzma1_encoder *const coder, const uint32_t dist,
  89. const uint32_t len, const uint32_t pos_state)
  90. {
  91. const uint32_t dist_state = get_dist_state(len);
  92. uint32_t price;
  93. if (dist < FULL_DISTANCES) {
  94. price = coder->dist_prices[dist_state][dist];
  95. } else {
  96. const uint32_t dist_slot = get_dist_slot_2(dist);
  97. price = coder->dist_slot_prices[dist_state][dist_slot]
  98. + coder->align_prices[dist & ALIGN_MASK];
  99. }
  100. price += get_len_price(&coder->match_len_encoder, len, pos_state);
  101. return price;
  102. }
  103. static void
  104. fill_dist_prices(lzma_lzma1_encoder *coder)
  105. {
  106. for (uint32_t dist_state = 0; dist_state < DIST_STATES; ++dist_state) {
  107. uint32_t *const dist_slot_prices
  108. = coder->dist_slot_prices[dist_state];
  109. // Price to encode the dist_slot.
  110. for (uint32_t dist_slot = 0;
  111. dist_slot < coder->dist_table_size; ++dist_slot)
  112. dist_slot_prices[dist_slot] = rc_bittree_price(
  113. coder->dist_slot[dist_state],
  114. DIST_SLOT_BITS, dist_slot);
  115. // For matches with distance >= FULL_DISTANCES, add the price
  116. // of the direct bits part of the match distance. (Align bits
  117. // are handled by fill_align_prices()).
  118. for (uint32_t dist_slot = DIST_MODEL_END;
  119. dist_slot < coder->dist_table_size;
  120. ++dist_slot)
  121. dist_slot_prices[dist_slot] += rc_direct_price(
  122. ((dist_slot >> 1) - 1) - ALIGN_BITS);
  123. // Distances in the range [0, 3] are fully encoded with
  124. // dist_slot, so they are used for coder->dist_prices
  125. // as is.
  126. for (uint32_t i = 0; i < DIST_MODEL_START; ++i)
  127. coder->dist_prices[dist_state][i]
  128. = dist_slot_prices[i];
  129. }
  130. // Distances in the range [4, 127] depend on dist_slot and
  131. // dist_special. We do this in a loop separate from the above
  132. // loop to avoid redundant calls to get_dist_slot().
  133. for (uint32_t i = DIST_MODEL_START; i < FULL_DISTANCES; ++i) {
  134. const uint32_t dist_slot = get_dist_slot(i);
  135. const uint32_t footer_bits = ((dist_slot >> 1) - 1);
  136. const uint32_t base = (2 | (dist_slot & 1)) << footer_bits;
  137. const uint32_t price = rc_bittree_reverse_price(
  138. coder->dist_special + base - dist_slot - 1,
  139. footer_bits, i - base);
  140. for (uint32_t dist_state = 0; dist_state < DIST_STATES;
  141. ++dist_state)
  142. coder->dist_prices[dist_state][i]
  143. = price + coder->dist_slot_prices[
  144. dist_state][dist_slot];
  145. }
  146. coder->match_price_count = 0;
  147. return;
  148. }
  149. static void
  150. fill_align_prices(lzma_lzma1_encoder *coder)
  151. {
  152. for (uint32_t i = 0; i < ALIGN_SIZE; ++i)
  153. coder->align_prices[i] = rc_bittree_reverse_price(
  154. coder->dist_align, ALIGN_BITS, i);
  155. coder->align_price_count = 0;
  156. return;
  157. }
  158. /////////////
  159. // Optimal //
  160. /////////////
  161. static inline void
  162. make_literal(lzma_optimal *optimal)
  163. {
  164. optimal->back_prev = UINT32_MAX;
  165. optimal->prev_1_is_literal = false;
  166. }
  167. static inline void
  168. make_short_rep(lzma_optimal *optimal)
  169. {
  170. optimal->back_prev = 0;
  171. optimal->prev_1_is_literal = false;
  172. }
  173. #define is_short_rep(optimal) \
  174. ((optimal).back_prev == 0)
  175. static void
  176. backward(lzma_lzma1_encoder *restrict coder, uint32_t *restrict len_res,
  177. uint32_t *restrict back_res, uint32_t cur)
  178. {
  179. coder->opts_end_index = cur;
  180. uint32_t pos_mem = coder->opts[cur].pos_prev;
  181. uint32_t back_mem = coder->opts[cur].back_prev;
  182. do {
  183. if (coder->opts[cur].prev_1_is_literal) {
  184. make_literal(&coder->opts[pos_mem]);
  185. coder->opts[pos_mem].pos_prev = pos_mem - 1;
  186. if (coder->opts[cur].prev_2) {
  187. coder->opts[pos_mem - 1].prev_1_is_literal
  188. = false;
  189. coder->opts[pos_mem - 1].pos_prev
  190. = coder->opts[cur].pos_prev_2;
  191. coder->opts[pos_mem - 1].back_prev
  192. = coder->opts[cur].back_prev_2;
  193. }
  194. }
  195. const uint32_t pos_prev = pos_mem;
  196. const uint32_t back_cur = back_mem;
  197. back_mem = coder->opts[pos_prev].back_prev;
  198. pos_mem = coder->opts[pos_prev].pos_prev;
  199. coder->opts[pos_prev].back_prev = back_cur;
  200. coder->opts[pos_prev].pos_prev = cur;
  201. cur = pos_prev;
  202. } while (cur != 0);
  203. coder->opts_current_index = coder->opts[0].pos_prev;
  204. *len_res = coder->opts[0].pos_prev;
  205. *back_res = coder->opts[0].back_prev;
  206. return;
  207. }
  208. //////////
  209. // Main //
  210. //////////
  211. static inline uint32_t
  212. helper1(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf,
  213. uint32_t *restrict back_res, uint32_t *restrict len_res,
  214. uint32_t position)
  215. {
  216. const uint32_t nice_len = mf->nice_len;
  217. uint32_t len_main;
  218. uint32_t matches_count;
  219. if (mf->read_ahead == 0) {
  220. len_main = mf_find(mf, &matches_count, coder->matches);
  221. } else {
  222. assert(mf->read_ahead == 1);
  223. len_main = coder->longest_match_length;
  224. matches_count = coder->matches_count;
  225. }
  226. const uint32_t buf_avail = my_min(mf_avail(mf) + 1, MATCH_LEN_MAX);
  227. if (buf_avail < 2) {
  228. *back_res = UINT32_MAX;
  229. *len_res = 1;
  230. return UINT32_MAX;
  231. }
  232. const uint8_t *const buf = mf_ptr(mf) - 1;
  233. uint32_t rep_lens[REPS];
  234. uint32_t rep_max_index = 0;
  235. for (uint32_t i = 0; i < REPS; ++i) {
  236. const uint8_t *const buf_back = buf - coder->reps[i] - 1;
  237. if (not_equal_16(buf, buf_back)) {
  238. rep_lens[i] = 0;
  239. continue;
  240. }
  241. rep_lens[i] = lzma_memcmplen(buf, buf_back, 2, buf_avail);
  242. if (rep_lens[i] > rep_lens[rep_max_index])
  243. rep_max_index = i;
  244. }
  245. if (rep_lens[rep_max_index] >= nice_len) {
  246. *back_res = rep_max_index;
  247. *len_res = rep_lens[rep_max_index];
  248. mf_skip(mf, *len_res - 1);
  249. return UINT32_MAX;
  250. }
  251. if (len_main >= nice_len) {
  252. *back_res = coder->matches[matches_count - 1].dist + REPS;
  253. *len_res = len_main;
  254. mf_skip(mf, len_main - 1);
  255. return UINT32_MAX;
  256. }
  257. const uint8_t current_byte = *buf;
  258. const uint8_t match_byte = *(buf - coder->reps[0] - 1);
  259. if (len_main < 2 && current_byte != match_byte
  260. && rep_lens[rep_max_index] < 2) {
  261. *back_res = UINT32_MAX;
  262. *len_res = 1;
  263. return UINT32_MAX;
  264. }
  265. coder->opts[0].state = coder->state;
  266. const uint32_t pos_state = position & coder->pos_mask;
  267. coder->opts[1].price = rc_bit_0_price(
  268. coder->is_match[coder->state][pos_state])
  269. + get_literal_price(coder, position, buf[-1],
  270. !is_literal_state(coder->state),
  271. match_byte, current_byte);
  272. make_literal(&coder->opts[1]);
  273. const uint32_t match_price = rc_bit_1_price(
  274. coder->is_match[coder->state][pos_state]);
  275. const uint32_t rep_match_price = match_price
  276. + rc_bit_1_price(coder->is_rep[coder->state]);
  277. if (match_byte == current_byte) {
  278. const uint32_t short_rep_price = rep_match_price
  279. + get_short_rep_price(
  280. coder, coder->state, pos_state);
  281. if (short_rep_price < coder->opts[1].price) {
  282. coder->opts[1].price = short_rep_price;
  283. make_short_rep(&coder->opts[1]);
  284. }
  285. }
  286. const uint32_t len_end = my_max(len_main, rep_lens[rep_max_index]);
  287. if (len_end < 2) {
  288. *back_res = coder->opts[1].back_prev;
  289. *len_res = 1;
  290. return UINT32_MAX;
  291. }
  292. coder->opts[1].pos_prev = 0;
  293. for (uint32_t i = 0; i < REPS; ++i)
  294. coder->opts[0].backs[i] = coder->reps[i];
  295. uint32_t len = len_end;
  296. do {
  297. coder->opts[len].price = RC_INFINITY_PRICE;
  298. } while (--len >= 2);
  299. for (uint32_t i = 0; i < REPS; ++i) {
  300. uint32_t rep_len = rep_lens[i];
  301. if (rep_len < 2)
  302. continue;
  303. const uint32_t price = rep_match_price + get_pure_rep_price(
  304. coder, i, coder->state, pos_state);
  305. do {
  306. const uint32_t cur_and_len_price = price
  307. + get_len_price(
  308. &coder->rep_len_encoder,
  309. rep_len, pos_state);
  310. if (cur_and_len_price < coder->opts[rep_len].price) {
  311. coder->opts[rep_len].price = cur_and_len_price;
  312. coder->opts[rep_len].pos_prev = 0;
  313. coder->opts[rep_len].back_prev = i;
  314. coder->opts[rep_len].prev_1_is_literal = false;
  315. }
  316. } while (--rep_len >= 2);
  317. }
  318. const uint32_t normal_match_price = match_price
  319. + rc_bit_0_price(coder->is_rep[coder->state]);
  320. len = rep_lens[0] >= 2 ? rep_lens[0] + 1 : 2;
  321. if (len <= len_main) {
  322. uint32_t i = 0;
  323. while (len > coder->matches[i].len)
  324. ++i;
  325. for(; ; ++len) {
  326. const uint32_t dist = coder->matches[i].dist;
  327. const uint32_t cur_and_len_price = normal_match_price
  328. + get_dist_len_price(coder,
  329. dist, len, pos_state);
  330. if (cur_and_len_price < coder->opts[len].price) {
  331. coder->opts[len].price = cur_and_len_price;
  332. coder->opts[len].pos_prev = 0;
  333. coder->opts[len].back_prev = dist + REPS;
  334. coder->opts[len].prev_1_is_literal = false;
  335. }
  336. if (len == coder->matches[i].len)
  337. if (++i == matches_count)
  338. break;
  339. }
  340. }
  341. return len_end;
  342. }
  343. static inline uint32_t
  344. helper2(lzma_lzma1_encoder *coder, uint32_t *reps, const uint8_t *buf,
  345. uint32_t len_end, uint32_t position, const uint32_t cur,
  346. const uint32_t nice_len, const uint32_t buf_avail_full)
  347. {
  348. uint32_t matches_count = coder->matches_count;
  349. uint32_t new_len = coder->longest_match_length;
  350. uint32_t pos_prev = coder->opts[cur].pos_prev;
  351. lzma_lzma_state state;
  352. if (coder->opts[cur].prev_1_is_literal) {
  353. --pos_prev;
  354. if (coder->opts[cur].prev_2) {
  355. state = coder->opts[coder->opts[cur].pos_prev_2].state;
  356. if (coder->opts[cur].back_prev_2 < REPS)
  357. update_long_rep(state);
  358. else
  359. update_match(state);
  360. } else {
  361. state = coder->opts[pos_prev].state;
  362. }
  363. update_literal(state);
  364. } else {
  365. state = coder->opts[pos_prev].state;
  366. }
  367. if (pos_prev == cur - 1) {
  368. if (is_short_rep(coder->opts[cur]))
  369. update_short_rep(state);
  370. else
  371. update_literal(state);
  372. } else {
  373. uint32_t pos;
  374. if (coder->opts[cur].prev_1_is_literal
  375. && coder->opts[cur].prev_2) {
  376. pos_prev = coder->opts[cur].pos_prev_2;
  377. pos = coder->opts[cur].back_prev_2;
  378. update_long_rep(state);
  379. } else {
  380. pos = coder->opts[cur].back_prev;
  381. if (pos < REPS)
  382. update_long_rep(state);
  383. else
  384. update_match(state);
  385. }
  386. if (pos < REPS) {
  387. reps[0] = coder->opts[pos_prev].backs[pos];
  388. uint32_t i;
  389. for (i = 1; i <= pos; ++i)
  390. reps[i] = coder->opts[pos_prev].backs[i - 1];
  391. for (; i < REPS; ++i)
  392. reps[i] = coder->opts[pos_prev].backs[i];
  393. } else {
  394. reps[0] = pos - REPS;
  395. for (uint32_t i = 1; i < REPS; ++i)
  396. reps[i] = coder->opts[pos_prev].backs[i - 1];
  397. }
  398. }
  399. coder->opts[cur].state = state;
  400. for (uint32_t i = 0; i < REPS; ++i)
  401. coder->opts[cur].backs[i] = reps[i];
  402. const uint32_t cur_price = coder->opts[cur].price;
  403. const uint8_t current_byte = *buf;
  404. const uint8_t match_byte = *(buf - reps[0] - 1);
  405. const uint32_t pos_state = position & coder->pos_mask;
  406. const uint32_t cur_and_1_price = cur_price
  407. + rc_bit_0_price(coder->is_match[state][pos_state])
  408. + get_literal_price(coder, position, buf[-1],
  409. !is_literal_state(state), match_byte, current_byte);
  410. bool next_is_literal = false;
  411. if (cur_and_1_price < coder->opts[cur + 1].price) {
  412. coder->opts[cur + 1].price = cur_and_1_price;
  413. coder->opts[cur + 1].pos_prev = cur;
  414. make_literal(&coder->opts[cur + 1]);
  415. next_is_literal = true;
  416. }
  417. const uint32_t match_price = cur_price
  418. + rc_bit_1_price(coder->is_match[state][pos_state]);
  419. const uint32_t rep_match_price = match_price
  420. + rc_bit_1_price(coder->is_rep[state]);
  421. if (match_byte == current_byte
  422. && !(coder->opts[cur + 1].pos_prev < cur
  423. && coder->opts[cur + 1].back_prev == 0)) {
  424. const uint32_t short_rep_price = rep_match_price
  425. + get_short_rep_price(coder, state, pos_state);
  426. if (short_rep_price <= coder->opts[cur + 1].price) {
  427. coder->opts[cur + 1].price = short_rep_price;
  428. coder->opts[cur + 1].pos_prev = cur;
  429. make_short_rep(&coder->opts[cur + 1]);
  430. next_is_literal = true;
  431. }
  432. }
  433. if (buf_avail_full < 2)
  434. return len_end;
  435. const uint32_t buf_avail = my_min(buf_avail_full, nice_len);
  436. if (!next_is_literal && match_byte != current_byte) { // speed optimization
  437. // try literal + rep0
  438. const uint8_t *const buf_back = buf - reps[0] - 1;
  439. const uint32_t limit = my_min(buf_avail_full, nice_len + 1);
  440. const uint32_t len_test = lzma_memcmplen(buf, buf_back, 1, limit) - 1;
  441. if (len_test >= 2) {
  442. lzma_lzma_state state_2 = state;
  443. update_literal(state_2);
  444. const uint32_t pos_state_next = (position + 1) & coder->pos_mask;
  445. const uint32_t next_rep_match_price = cur_and_1_price
  446. + rc_bit_1_price(coder->is_match[state_2][pos_state_next])
  447. + rc_bit_1_price(coder->is_rep[state_2]);
  448. //for (; len_test >= 2; --len_test) {
  449. const uint32_t offset = cur + 1 + len_test;
  450. while (len_end < offset)
  451. coder->opts[++len_end].price = RC_INFINITY_PRICE;
  452. const uint32_t cur_and_len_price = next_rep_match_price
  453. + get_rep_price(coder, 0, len_test,
  454. state_2, pos_state_next);
  455. if (cur_and_len_price < coder->opts[offset].price) {
  456. coder->opts[offset].price = cur_and_len_price;
  457. coder->opts[offset].pos_prev = cur + 1;
  458. coder->opts[offset].back_prev = 0;
  459. coder->opts[offset].prev_1_is_literal = true;
  460. coder->opts[offset].prev_2 = false;
  461. }
  462. //}
  463. }
  464. }
  465. uint32_t start_len = 2; // speed optimization
  466. for (uint32_t rep_index = 0; rep_index < REPS; ++rep_index) {
  467. const uint8_t *const buf_back = buf - reps[rep_index] - 1;
  468. if (not_equal_16(buf, buf_back))
  469. continue;
  470. uint32_t len_test = lzma_memcmplen(buf, buf_back, 2, buf_avail);
  471. while (len_end < cur + len_test)
  472. coder->opts[++len_end].price = RC_INFINITY_PRICE;
  473. const uint32_t len_test_temp = len_test;
  474. const uint32_t price = rep_match_price + get_pure_rep_price(
  475. coder, rep_index, state, pos_state);
  476. do {
  477. const uint32_t cur_and_len_price = price
  478. + get_len_price(&coder->rep_len_encoder,
  479. len_test, pos_state);
  480. if (cur_and_len_price < coder->opts[cur + len_test].price) {
  481. coder->opts[cur + len_test].price = cur_and_len_price;
  482. coder->opts[cur + len_test].pos_prev = cur;
  483. coder->opts[cur + len_test].back_prev = rep_index;
  484. coder->opts[cur + len_test].prev_1_is_literal = false;
  485. }
  486. } while (--len_test >= 2);
  487. len_test = len_test_temp;
  488. if (rep_index == 0)
  489. start_len = len_test + 1;
  490. uint32_t len_test_2 = len_test + 1;
  491. const uint32_t limit = my_min(buf_avail_full,
  492. len_test_2 + nice_len);
  493. for (; len_test_2 < limit
  494. && buf[len_test_2] == buf_back[len_test_2];
  495. ++len_test_2) ;
  496. len_test_2 -= len_test + 1;
  497. if (len_test_2 >= 2) {
  498. lzma_lzma_state state_2 = state;
  499. update_long_rep(state_2);
  500. uint32_t pos_state_next = (position + len_test) & coder->pos_mask;
  501. const uint32_t cur_and_len_literal_price = price
  502. + get_len_price(&coder->rep_len_encoder,
  503. len_test, pos_state)
  504. + rc_bit_0_price(coder->is_match[state_2][pos_state_next])
  505. + get_literal_price(coder, position + len_test,
  506. buf[len_test - 1], true,
  507. buf_back[len_test], buf[len_test]);
  508. update_literal(state_2);
  509. pos_state_next = (position + len_test + 1) & coder->pos_mask;
  510. const uint32_t next_rep_match_price = cur_and_len_literal_price
  511. + rc_bit_1_price(coder->is_match[state_2][pos_state_next])
  512. + rc_bit_1_price(coder->is_rep[state_2]);
  513. //for(; len_test_2 >= 2; len_test_2--) {
  514. const uint32_t offset = cur + len_test + 1 + len_test_2;
  515. while (len_end < offset)
  516. coder->opts[++len_end].price = RC_INFINITY_PRICE;
  517. const uint32_t cur_and_len_price = next_rep_match_price
  518. + get_rep_price(coder, 0, len_test_2,
  519. state_2, pos_state_next);
  520. if (cur_and_len_price < coder->opts[offset].price) {
  521. coder->opts[offset].price = cur_and_len_price;
  522. coder->opts[offset].pos_prev = cur + len_test + 1;
  523. coder->opts[offset].back_prev = 0;
  524. coder->opts[offset].prev_1_is_literal = true;
  525. coder->opts[offset].prev_2 = true;
  526. coder->opts[offset].pos_prev_2 = cur;
  527. coder->opts[offset].back_prev_2 = rep_index;
  528. }
  529. //}
  530. }
  531. }
  532. //for (uint32_t len_test = 2; len_test <= new_len; ++len_test)
  533. if (new_len > buf_avail) {
  534. new_len = buf_avail;
  535. matches_count = 0;
  536. while (new_len > coder->matches[matches_count].len)
  537. ++matches_count;
  538. coder->matches[matches_count++].len = new_len;
  539. }
  540. if (new_len >= start_len) {
  541. const uint32_t normal_match_price = match_price
  542. + rc_bit_0_price(coder->is_rep[state]);
  543. while (len_end < cur + new_len)
  544. coder->opts[++len_end].price = RC_INFINITY_PRICE;
  545. uint32_t i = 0;
  546. while (start_len > coder->matches[i].len)
  547. ++i;
  548. for (uint32_t len_test = start_len; ; ++len_test) {
  549. const uint32_t cur_back = coder->matches[i].dist;
  550. uint32_t cur_and_len_price = normal_match_price
  551. + get_dist_len_price(coder,
  552. cur_back, len_test, pos_state);
  553. if (cur_and_len_price < coder->opts[cur + len_test].price) {
  554. coder->opts[cur + len_test].price = cur_and_len_price;
  555. coder->opts[cur + len_test].pos_prev = cur;
  556. coder->opts[cur + len_test].back_prev
  557. = cur_back + REPS;
  558. coder->opts[cur + len_test].prev_1_is_literal = false;
  559. }
  560. if (len_test == coder->matches[i].len) {
  561. // Try Match + Literal + Rep0
  562. const uint8_t *const buf_back = buf - cur_back - 1;
  563. uint32_t len_test_2 = len_test + 1;
  564. const uint32_t limit = my_min(buf_avail_full,
  565. len_test_2 + nice_len);
  566. for (; len_test_2 < limit &&
  567. buf[len_test_2] == buf_back[len_test_2];
  568. ++len_test_2) ;
  569. len_test_2 -= len_test + 1;
  570. if (len_test_2 >= 2) {
  571. lzma_lzma_state state_2 = state;
  572. update_match(state_2);
  573. uint32_t pos_state_next
  574. = (position + len_test) & coder->pos_mask;
  575. const uint32_t cur_and_len_literal_price = cur_and_len_price
  576. + rc_bit_0_price(
  577. coder->is_match[state_2][pos_state_next])
  578. + get_literal_price(coder,
  579. position + len_test,
  580. buf[len_test - 1],
  581. true,
  582. buf_back[len_test],
  583. buf[len_test]);
  584. update_literal(state_2);
  585. pos_state_next = (pos_state_next + 1) & coder->pos_mask;
  586. const uint32_t next_rep_match_price
  587. = cur_and_len_literal_price
  588. + rc_bit_1_price(
  589. coder->is_match[state_2][pos_state_next])
  590. + rc_bit_1_price(coder->is_rep[state_2]);
  591. // for(; len_test_2 >= 2; --len_test_2) {
  592. const uint32_t offset = cur + len_test + 1 + len_test_2;
  593. while (len_end < offset)
  594. coder->opts[++len_end].price = RC_INFINITY_PRICE;
  595. cur_and_len_price = next_rep_match_price
  596. + get_rep_price(coder, 0, len_test_2,
  597. state_2, pos_state_next);
  598. if (cur_and_len_price < coder->opts[offset].price) {
  599. coder->opts[offset].price = cur_and_len_price;
  600. coder->opts[offset].pos_prev = cur + len_test + 1;
  601. coder->opts[offset].back_prev = 0;
  602. coder->opts[offset].prev_1_is_literal = true;
  603. coder->opts[offset].prev_2 = true;
  604. coder->opts[offset].pos_prev_2 = cur;
  605. coder->opts[offset].back_prev_2
  606. = cur_back + REPS;
  607. }
  608. //}
  609. }
  610. if (++i == matches_count)
  611. break;
  612. }
  613. }
  614. }
  615. return len_end;
  616. }
  617. extern void
  618. lzma_lzma_optimum_normal(lzma_lzma1_encoder *restrict coder,
  619. lzma_mf *restrict mf,
  620. uint32_t *restrict back_res, uint32_t *restrict len_res,
  621. uint32_t position)
  622. {
  623. // If we have symbols pending, return the next pending symbol.
  624. if (coder->opts_end_index != coder->opts_current_index) {
  625. assert(mf->read_ahead > 0);
  626. *len_res = coder->opts[coder->opts_current_index].pos_prev
  627. - coder->opts_current_index;
  628. *back_res = coder->opts[coder->opts_current_index].back_prev;
  629. coder->opts_current_index = coder->opts[
  630. coder->opts_current_index].pos_prev;
  631. return;
  632. }
  633. // Update the price tables. In LZMA SDK <= 4.60 (and possibly later)
  634. // this was done in both initialization function and in the main loop.
  635. // In liblzma they were moved into this single place.
  636. if (mf->read_ahead == 0) {
  637. if (coder->match_price_count >= (1 << 7))
  638. fill_dist_prices(coder);
  639. if (coder->align_price_count >= ALIGN_SIZE)
  640. fill_align_prices(coder);
  641. }
  642. // TODO: This needs quite a bit of cleaning still. But splitting
  643. // the original function into two pieces makes it at least a little
  644. // more readable, since those two parts don't share many variables.
  645. uint32_t len_end = helper1(coder, mf, back_res, len_res, position);
  646. if (len_end == UINT32_MAX)
  647. return;
  648. uint32_t reps[REPS];
  649. memcpy(reps, coder->reps, sizeof(reps));
  650. uint32_t cur;
  651. for (cur = 1; cur < len_end; ++cur) {
  652. assert(cur < OPTS);
  653. coder->longest_match_length = mf_find(
  654. mf, &coder->matches_count, coder->matches);
  655. if (coder->longest_match_length >= mf->nice_len)
  656. break;
  657. len_end = helper2(coder, reps, mf_ptr(mf) - 1, len_end,
  658. position + cur, cur, mf->nice_len,
  659. my_min(mf_avail(mf) + 1, OPTS - 1 - cur));
  660. }
  661. backward(coder, len_res, back_res, cur);
  662. return;
  663. }