lzma_encoder_optimum_normal.c 23 KB

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