jcarith.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * jcarith.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Developed 1997-2009 by Guido Vollbeding.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2015, 2018, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file contains portable arithmetic entropy encoding routines for JPEG
  12. * (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1).
  13. *
  14. * Both sequential and progressive modes are supported in this single module.
  15. *
  16. * Suspension is not currently supported in this module.
  17. *
  18. * NOTE: All referenced figures are from
  19. * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
  20. */
  21. #define JPEG_INTERNALS
  22. #include "jinclude.h"
  23. #include "jpeglib.h"
  24. #include <math.h>
  25. /* Expanded entropy encoder object for arithmetic encoding. */
  26. typedef struct {
  27. struct jpeg_entropy_encoder pub; /* public fields */
  28. JLONG c; /* C register, base of coding interval, layout as in sec. D.1.3 */
  29. JLONG a; /* A register, normalized size of coding interval */
  30. JLONG sc; /* counter for stacked 0xFF values which might overflow */
  31. JLONG zc; /* counter for pending 0x00 output values which might *
  32. * be discarded at the end ("Pacman" termination) */
  33. int ct; /* bit shift counter, determines when next byte will be written */
  34. int buffer; /* buffer for most recent output byte != 0xFF */
  35. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  36. int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
  37. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  38. int next_restart_num; /* next restart number to write (0-7) */
  39. /* Pointers to statistics areas (these workspaces have image lifespan) */
  40. unsigned char *dc_stats[NUM_ARITH_TBLS];
  41. unsigned char *ac_stats[NUM_ARITH_TBLS];
  42. /* Statistics bin for coding with fixed probability 0.5 */
  43. unsigned char fixed_bin[4];
  44. } arith_entropy_encoder;
  45. typedef arith_entropy_encoder *arith_entropy_ptr;
  46. /* The following two definitions specify the allocation chunk size
  47. * for the statistics area.
  48. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
  49. * 49 statistics bins for DC, and 245 statistics bins for AC coding.
  50. *
  51. * We use a compact representation with 1 byte per statistics bin,
  52. * thus the numbers directly represent byte sizes.
  53. * This 1 byte per statistics bin contains the meaning of the MPS
  54. * (more probable symbol) in the highest bit (mask 0x80), and the
  55. * index into the probability estimation state machine table
  56. * in the lower bits (mask 0x7F).
  57. */
  58. #define DC_STAT_BINS 64
  59. #define AC_STAT_BINS 256
  60. /* NOTE: Uncomment the following #define if you want to use the
  61. * given formula for calculating the AC conditioning parameter Kx
  62. * for spectral selection progressive coding in section G.1.3.2
  63. * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4).
  64. * Although the spec and P&M authors claim that this "has proven
  65. * to give good results for 8 bit precision samples", I'm not
  66. * convinced yet that this is really beneficial.
  67. * Early tests gave only very marginal compression enhancements
  68. * (a few - around 5 or so - bytes even for very large files),
  69. * which would turn out rather negative if we'd suppress the
  70. * DAC (Define Arithmetic Conditioning) marker segments for
  71. * the default parameters in the future.
  72. * Note that currently the marker writing module emits 12-byte
  73. * DAC segments for a full-component scan in a color image.
  74. * This is not worth worrying about IMHO. However, since the
  75. * spec defines the default values to be used if the tables
  76. * are omitted (unlike Huffman tables, which are required
  77. * anyway), one might optimize this behaviour in the future,
  78. * and then it would be disadvantageous to use custom tables if
  79. * they don't provide sufficient gain to exceed the DAC size.
  80. *
  81. * On the other hand, I'd consider it as a reasonable result
  82. * that the conditioning has no significant influence on the
  83. * compression performance. This means that the basic
  84. * statistical model is already rather stable.
  85. *
  86. * Thus, at the moment, we use the default conditioning values
  87. * anyway, and do not use the custom formula.
  88. *
  89. #define CALCULATE_SPECTRAL_CONDITIONING
  90. */
  91. /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
  92. * We assume that int right shift is unsigned if JLONG right shift is,
  93. * which should be safe.
  94. */
  95. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  96. #define ISHIFT_TEMPS int ishift_temp;
  97. #define IRIGHT_SHIFT(x, shft) \
  98. ((ishift_temp = (x)) < 0 ? \
  99. (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
  100. (ishift_temp >> (shft)))
  101. #else
  102. #define ISHIFT_TEMPS
  103. #define IRIGHT_SHIFT(x, shft) ((x) >> (shft))
  104. #endif
  105. LOCAL(void)
  106. emit_byte(int val, j_compress_ptr cinfo)
  107. /* Write next output byte; we do not support suspension in this module. */
  108. {
  109. struct jpeg_destination_mgr *dest = cinfo->dest;
  110. /* Do not emit bytes during trellis passes */
  111. if (cinfo->master->trellis_passes)
  112. return;
  113. *dest->next_output_byte++ = (JOCTET) val;
  114. if (--dest->free_in_buffer == 0)
  115. if (!(*dest->empty_output_buffer) (cinfo))
  116. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  117. }
  118. /*
  119. * Finish up at the end of an arithmetic-compressed scan.
  120. */
  121. METHODDEF(void)
  122. finish_pass(j_compress_ptr cinfo)
  123. {
  124. arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;
  125. JLONG temp;
  126. /* Section D.1.8: Termination of encoding */
  127. /* Find the e->c in the coding interval with the largest
  128. * number of trailing zero bits */
  129. if ((temp = (e->a - 1 + e->c) & 0xFFFF0000UL) < e->c)
  130. e->c = temp + 0x8000L;
  131. else
  132. e->c = temp;
  133. /* Send remaining bytes to output */
  134. e->c <<= e->ct;
  135. if (e->c & 0xF8000000UL) {
  136. /* One final overflow has to be handled */
  137. if (e->buffer >= 0) {
  138. if (e->zc)
  139. do emit_byte(0x00, cinfo);
  140. while (--e->zc);
  141. emit_byte(e->buffer + 1, cinfo);
  142. if (e->buffer + 1 == 0xFF)
  143. emit_byte(0x00, cinfo);
  144. }
  145. e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
  146. e->sc = 0;
  147. } else {
  148. if (e->buffer == 0)
  149. ++e->zc;
  150. else if (e->buffer >= 0) {
  151. if (e->zc)
  152. do emit_byte(0x00, cinfo);
  153. while (--e->zc);
  154. emit_byte(e->buffer, cinfo);
  155. }
  156. if (e->sc) {
  157. if (e->zc)
  158. do emit_byte(0x00, cinfo);
  159. while (--e->zc);
  160. do {
  161. emit_byte(0xFF, cinfo);
  162. emit_byte(0x00, cinfo);
  163. } while (--e->sc);
  164. }
  165. }
  166. /* Output final bytes only if they are not 0x00 */
  167. if (e->c & 0x7FFF800L) {
  168. if (e->zc) /* output final pending zero bytes */
  169. do emit_byte(0x00, cinfo);
  170. while (--e->zc);
  171. emit_byte((e->c >> 19) & 0xFF, cinfo);
  172. if (((e->c >> 19) & 0xFF) == 0xFF)
  173. emit_byte(0x00, cinfo);
  174. if (e->c & 0x7F800L) {
  175. emit_byte((e->c >> 11) & 0xFF, cinfo);
  176. if (((e->c >> 11) & 0xFF) == 0xFF)
  177. emit_byte(0x00, cinfo);
  178. }
  179. }
  180. }
  181. /*
  182. * The core arithmetic encoding routine (common in JPEG and JBIG).
  183. * This needs to go as fast as possible.
  184. * Machine-dependent optimization facilities
  185. * are not utilized in this portable implementation.
  186. * However, this code should be fairly efficient and
  187. * may be a good base for further optimizations anyway.
  188. *
  189. * Parameter 'val' to be encoded may be 0 or 1 (binary decision).
  190. *
  191. * Note: I've added full "Pacman" termination support to the
  192. * byte output routines, which is equivalent to the optional
  193. * Discard_final_zeros procedure (Figure D.15) in the spec.
  194. * Thus, we always produce the shortest possible output
  195. * stream compliant to the spec (no trailing zero bytes,
  196. * except for FF stuffing).
  197. *
  198. * I've also introduced a new scheme for accessing
  199. * the probability estimation state machine table,
  200. * derived from Markus Kuhn's JBIG implementation.
  201. */
  202. LOCAL(void)
  203. arith_encode(j_compress_ptr cinfo, unsigned char *st, int val)
  204. {
  205. register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;
  206. register unsigned char nl, nm;
  207. register JLONG qe, temp;
  208. register int sv;
  209. /* Fetch values from our compact representation of Table D.2:
  210. * Qe values and probability estimation state machine
  211. */
  212. sv = *st;
  213. qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
  214. nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
  215. nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
  216. /* Encode & estimation procedures per sections D.1.4 & D.1.5 */
  217. e->a -= qe;
  218. if (val != (sv >> 7)) {
  219. /* Encode the less probable symbol */
  220. if (e->a >= qe) {
  221. /* If the interval size (qe) for the less probable symbol (LPS)
  222. * is larger than the interval size for the MPS, then exchange
  223. * the two symbols for coding efficiency, otherwise code the LPS
  224. * as usual: */
  225. e->c += e->a;
  226. e->a = qe;
  227. }
  228. *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
  229. } else {
  230. /* Encode the more probable symbol */
  231. if (e->a >= 0x8000L)
  232. return; /* A >= 0x8000 -> ready, no renormalization required */
  233. if (e->a < qe) {
  234. /* If the interval size (qe) for the less probable symbol (LPS)
  235. * is larger than the interval size for the MPS, then exchange
  236. * the two symbols for coding efficiency: */
  237. e->c += e->a;
  238. e->a = qe;
  239. }
  240. *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
  241. }
  242. /* Renormalization & data output per section D.1.6 */
  243. do {
  244. e->a <<= 1;
  245. e->c <<= 1;
  246. if (--e->ct == 0) {
  247. /* Another byte is ready for output */
  248. temp = e->c >> 19;
  249. if (temp > 0xFF) {
  250. /* Handle overflow over all stacked 0xFF bytes */
  251. if (e->buffer >= 0) {
  252. if (e->zc)
  253. do emit_byte(0x00, cinfo);
  254. while (--e->zc);
  255. emit_byte(e->buffer + 1, cinfo);
  256. if (e->buffer + 1 == 0xFF)
  257. emit_byte(0x00, cinfo);
  258. }
  259. e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
  260. e->sc = 0;
  261. /* Note: The 3 spacer bits in the C register guarantee
  262. * that the new buffer byte can't be 0xFF here
  263. * (see page 160 in the P&M JPEG book). */
  264. e->buffer = temp & 0xFF; /* new output byte, might overflow later */
  265. } else if (temp == 0xFF) {
  266. ++e->sc; /* stack 0xFF byte (which might overflow later) */
  267. } else {
  268. /* Output all stacked 0xFF bytes, they will not overflow any more */
  269. if (e->buffer == 0)
  270. ++e->zc;
  271. else if (e->buffer >= 0) {
  272. if (e->zc)
  273. do emit_byte(0x00, cinfo);
  274. while (--e->zc);
  275. emit_byte(e->buffer, cinfo);
  276. }
  277. if (e->sc) {
  278. if (e->zc)
  279. do emit_byte(0x00, cinfo);
  280. while (--e->zc);
  281. do {
  282. emit_byte(0xFF, cinfo);
  283. emit_byte(0x00, cinfo);
  284. } while (--e->sc);
  285. }
  286. e->buffer = temp & 0xFF; /* new output byte (can still overflow) */
  287. }
  288. e->c &= 0x7FFFFL;
  289. e->ct += 8;
  290. }
  291. } while (e->a < 0x8000L);
  292. }
  293. /*
  294. * Emit a restart marker & resynchronize predictions.
  295. */
  296. LOCAL(void)
  297. emit_restart(j_compress_ptr cinfo, int restart_num)
  298. {
  299. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  300. int ci;
  301. jpeg_component_info *compptr;
  302. finish_pass(cinfo);
  303. emit_byte(0xFF, cinfo);
  304. emit_byte(JPEG_RST0 + restart_num, cinfo);
  305. /* Re-initialize statistics areas */
  306. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  307. compptr = cinfo->cur_comp_info[ci];
  308. /* DC needs no table for refinement scan */
  309. if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  310. MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
  311. /* Reset DC predictions to 0 */
  312. entropy->last_dc_val[ci] = 0;
  313. entropy->dc_context[ci] = 0;
  314. }
  315. /* AC needs no table when not present */
  316. if (cinfo->progressive_mode == 0 || cinfo->Se) {
  317. MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
  318. }
  319. }
  320. /* Reset arithmetic encoding variables */
  321. entropy->c = 0;
  322. entropy->a = 0x10000L;
  323. entropy->sc = 0;
  324. entropy->zc = 0;
  325. entropy->ct = 11;
  326. entropy->buffer = -1; /* empty */
  327. }
  328. /*
  329. * MCU encoding for DC initial scan (either spectral selection,
  330. * or first pass of successive approximation).
  331. */
  332. METHODDEF(boolean)
  333. encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  334. {
  335. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  336. JBLOCKROW block;
  337. unsigned char *st;
  338. int blkn, ci, tbl;
  339. int v, v2, m;
  340. ISHIFT_TEMPS
  341. /* Emit restart marker if needed */
  342. if (cinfo->restart_interval) {
  343. if (entropy->restarts_to_go == 0) {
  344. emit_restart(cinfo, entropy->next_restart_num);
  345. entropy->restarts_to_go = cinfo->restart_interval;
  346. entropy->next_restart_num++;
  347. entropy->next_restart_num &= 7;
  348. }
  349. entropy->restarts_to_go--;
  350. }
  351. /* Encode the MCU data blocks */
  352. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  353. block = MCU_data[blkn];
  354. ci = cinfo->MCU_membership[blkn];
  355. tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
  356. /* Compute the DC value after the required point transform by Al.
  357. * This is simply an arithmetic right shift.
  358. */
  359. m = IRIGHT_SHIFT((int)((*block)[0]), cinfo->Al);
  360. /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
  361. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  362. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  363. /* Figure F.4: Encode_DC_DIFF */
  364. if ((v = m - entropy->last_dc_val[ci]) == 0) {
  365. arith_encode(cinfo, st, 0);
  366. entropy->dc_context[ci] = 0; /* zero diff category */
  367. } else {
  368. entropy->last_dc_val[ci] = m;
  369. arith_encode(cinfo, st, 1);
  370. /* Figure F.6: Encoding nonzero value v */
  371. /* Figure F.7: Encoding the sign of v */
  372. if (v > 0) {
  373. arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
  374. st += 2; /* Table F.4: SP = S0 + 2 */
  375. entropy->dc_context[ci] = 4; /* small positive diff category */
  376. } else {
  377. v = -v;
  378. arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
  379. st += 3; /* Table F.4: SN = S0 + 3 */
  380. entropy->dc_context[ci] = 8; /* small negative diff category */
  381. }
  382. /* Figure F.8: Encoding the magnitude category of v */
  383. m = 0;
  384. if (v -= 1) {
  385. arith_encode(cinfo, st, 1);
  386. m = 1;
  387. v2 = v;
  388. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  389. while (v2 >>= 1) {
  390. arith_encode(cinfo, st, 1);
  391. m <<= 1;
  392. st += 1;
  393. }
  394. }
  395. arith_encode(cinfo, st, 0);
  396. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  397. if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
  398. entropy->dc_context[ci] = 0; /* zero diff category */
  399. else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
  400. entropy->dc_context[ci] += 8; /* large diff category */
  401. /* Figure F.9: Encoding the magnitude bit pattern of v */
  402. st += 14;
  403. while (m >>= 1)
  404. arith_encode(cinfo, st, (m & v) ? 1 : 0);
  405. }
  406. }
  407. return TRUE;
  408. }
  409. /*
  410. * MCU encoding for AC initial scan (either spectral selection,
  411. * or first pass of successive approximation).
  412. */
  413. METHODDEF(boolean)
  414. encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  415. {
  416. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  417. JBLOCKROW block;
  418. unsigned char *st;
  419. int tbl, k, ke;
  420. int v, v2, m;
  421. /* Emit restart marker if needed */
  422. if (cinfo->restart_interval) {
  423. if (entropy->restarts_to_go == 0) {
  424. emit_restart(cinfo, entropy->next_restart_num);
  425. entropy->restarts_to_go = cinfo->restart_interval;
  426. entropy->next_restart_num++;
  427. entropy->next_restart_num &= 7;
  428. }
  429. entropy->restarts_to_go--;
  430. }
  431. /* Encode the MCU data block */
  432. block = MCU_data[0];
  433. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  434. /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
  435. /* Establish EOB (end-of-block) index */
  436. for (ke = cinfo->Se; ke > 0; ke--)
  437. /* We must apply the point transform by Al. For AC coefficients this
  438. * is an integer division with rounding towards 0. To do this portably
  439. * in C, we shift after obtaining the absolute value.
  440. */
  441. if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) {
  442. if (v >>= cinfo->Al) break;
  443. } else {
  444. v = -v;
  445. if (v >>= cinfo->Al) break;
  446. }
  447. /* Figure F.5: Encode_AC_Coefficients */
  448. for (k = cinfo->Ss; k <= ke; k++) {
  449. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  450. arith_encode(cinfo, st, 0); /* EOB decision */
  451. for (;;) {
  452. if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
  453. if (v >>= cinfo->Al) {
  454. arith_encode(cinfo, st + 1, 1);
  455. arith_encode(cinfo, entropy->fixed_bin, 0);
  456. break;
  457. }
  458. } else {
  459. v = -v;
  460. if (v >>= cinfo->Al) {
  461. arith_encode(cinfo, st + 1, 1);
  462. arith_encode(cinfo, entropy->fixed_bin, 1);
  463. break;
  464. }
  465. }
  466. arith_encode(cinfo, st + 1, 0); st += 3; k++;
  467. }
  468. st += 2;
  469. /* Figure F.8: Encoding the magnitude category of v */
  470. m = 0;
  471. if (v -= 1) {
  472. arith_encode(cinfo, st, 1);
  473. m = 1;
  474. v2 = v;
  475. if (v2 >>= 1) {
  476. arith_encode(cinfo, st, 1);
  477. m <<= 1;
  478. st = entropy->ac_stats[tbl] +
  479. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  480. while (v2 >>= 1) {
  481. arith_encode(cinfo, st, 1);
  482. m <<= 1;
  483. st += 1;
  484. }
  485. }
  486. }
  487. arith_encode(cinfo, st, 0);
  488. /* Figure F.9: Encoding the magnitude bit pattern of v */
  489. st += 14;
  490. while (m >>= 1)
  491. arith_encode(cinfo, st, (m & v) ? 1 : 0);
  492. }
  493. /* Encode EOB decision only if k <= cinfo->Se */
  494. if (k <= cinfo->Se) {
  495. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  496. arith_encode(cinfo, st, 1);
  497. }
  498. return TRUE;
  499. }
  500. /*
  501. * MCU encoding for DC successive approximation refinement scan.
  502. */
  503. METHODDEF(boolean)
  504. encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  505. {
  506. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  507. unsigned char *st;
  508. int Al, blkn;
  509. /* Emit restart marker if needed */
  510. if (cinfo->restart_interval) {
  511. if (entropy->restarts_to_go == 0) {
  512. emit_restart(cinfo, entropy->next_restart_num);
  513. entropy->restarts_to_go = cinfo->restart_interval;
  514. entropy->next_restart_num++;
  515. entropy->next_restart_num &= 7;
  516. }
  517. entropy->restarts_to_go--;
  518. }
  519. st = entropy->fixed_bin; /* use fixed probability estimation */
  520. Al = cinfo->Al;
  521. /* Encode the MCU data blocks */
  522. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  523. /* We simply emit the Al'th bit of the DC coefficient value. */
  524. arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1);
  525. }
  526. return TRUE;
  527. }
  528. /*
  529. * MCU encoding for AC successive approximation refinement scan.
  530. */
  531. METHODDEF(boolean)
  532. encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  533. {
  534. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  535. JBLOCKROW block;
  536. unsigned char *st;
  537. int tbl, k, ke, kex;
  538. int v;
  539. /* Emit restart marker if needed */
  540. if (cinfo->restart_interval) {
  541. if (entropy->restarts_to_go == 0) {
  542. emit_restart(cinfo, entropy->next_restart_num);
  543. entropy->restarts_to_go = cinfo->restart_interval;
  544. entropy->next_restart_num++;
  545. entropy->next_restart_num &= 7;
  546. }
  547. entropy->restarts_to_go--;
  548. }
  549. /* Encode the MCU data block */
  550. block = MCU_data[0];
  551. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  552. /* Section G.1.3.3: Encoding of AC coefficients */
  553. /* Establish EOB (end-of-block) index */
  554. for (ke = cinfo->Se; ke > 0; ke--)
  555. /* We must apply the point transform by Al. For AC coefficients this
  556. * is an integer division with rounding towards 0. To do this portably
  557. * in C, we shift after obtaining the absolute value.
  558. */
  559. if ((v = (*block)[jpeg_natural_order[ke]]) >= 0) {
  560. if (v >>= cinfo->Al) break;
  561. } else {
  562. v = -v;
  563. if (v >>= cinfo->Al) break;
  564. }
  565. /* Establish EOBx (previous stage end-of-block) index */
  566. for (kex = ke; kex > 0; kex--)
  567. if ((v = (*block)[jpeg_natural_order[kex]]) >= 0) {
  568. if (v >>= cinfo->Ah) break;
  569. } else {
  570. v = -v;
  571. if (v >>= cinfo->Ah) break;
  572. }
  573. /* Figure G.10: Encode_AC_Coefficients_SA */
  574. for (k = cinfo->Ss; k <= ke; k++) {
  575. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  576. if (k > kex)
  577. arith_encode(cinfo, st, 0); /* EOB decision */
  578. for (;;) {
  579. if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
  580. if (v >>= cinfo->Al) {
  581. if (v >> 1) /* previously nonzero coef */
  582. arith_encode(cinfo, st + 2, (v & 1));
  583. else { /* newly nonzero coef */
  584. arith_encode(cinfo, st + 1, 1);
  585. arith_encode(cinfo, entropy->fixed_bin, 0);
  586. }
  587. break;
  588. }
  589. } else {
  590. v = -v;
  591. if (v >>= cinfo->Al) {
  592. if (v >> 1) /* previously nonzero coef */
  593. arith_encode(cinfo, st + 2, (v & 1));
  594. else { /* newly nonzero coef */
  595. arith_encode(cinfo, st + 1, 1);
  596. arith_encode(cinfo, entropy->fixed_bin, 1);
  597. }
  598. break;
  599. }
  600. }
  601. arith_encode(cinfo, st + 1, 0); st += 3; k++;
  602. }
  603. }
  604. /* Encode EOB decision only if k <= cinfo->Se */
  605. if (k <= cinfo->Se) {
  606. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  607. arith_encode(cinfo, st, 1);
  608. }
  609. return TRUE;
  610. }
  611. /*
  612. * Encode and output one MCU's worth of arithmetic-compressed coefficients.
  613. */
  614. METHODDEF(boolean)
  615. encode_mcu(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  616. {
  617. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  618. jpeg_component_info *compptr;
  619. JBLOCKROW block;
  620. unsigned char *st;
  621. int blkn, ci, tbl, k, ke;
  622. int v, v2, m;
  623. /* Emit restart marker if needed */
  624. if (cinfo->restart_interval) {
  625. if (entropy->restarts_to_go == 0) {
  626. emit_restart(cinfo, entropy->next_restart_num);
  627. entropy->restarts_to_go = cinfo->restart_interval;
  628. entropy->next_restart_num++;
  629. entropy->next_restart_num &= 7;
  630. }
  631. entropy->restarts_to_go--;
  632. }
  633. /* Encode the MCU data blocks */
  634. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  635. block = MCU_data[blkn];
  636. ci = cinfo->MCU_membership[blkn];
  637. compptr = cinfo->cur_comp_info[ci];
  638. /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
  639. tbl = compptr->dc_tbl_no;
  640. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  641. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  642. /* Figure F.4: Encode_DC_DIFF */
  643. if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) {
  644. arith_encode(cinfo, st, 0);
  645. entropy->dc_context[ci] = 0; /* zero diff category */
  646. } else {
  647. entropy->last_dc_val[ci] = (*block)[0];
  648. arith_encode(cinfo, st, 1);
  649. /* Figure F.6: Encoding nonzero value v */
  650. /* Figure F.7: Encoding the sign of v */
  651. if (v > 0) {
  652. arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
  653. st += 2; /* Table F.4: SP = S0 + 2 */
  654. entropy->dc_context[ci] = 4; /* small positive diff category */
  655. } else {
  656. v = -v;
  657. arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
  658. st += 3; /* Table F.4: SN = S0 + 3 */
  659. entropy->dc_context[ci] = 8; /* small negative diff category */
  660. }
  661. /* Figure F.8: Encoding the magnitude category of v */
  662. m = 0;
  663. if (v -= 1) {
  664. arith_encode(cinfo, st, 1);
  665. m = 1;
  666. v2 = v;
  667. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  668. while (v2 >>= 1) {
  669. arith_encode(cinfo, st, 1);
  670. m <<= 1;
  671. st += 1;
  672. }
  673. }
  674. arith_encode(cinfo, st, 0);
  675. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  676. if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
  677. entropy->dc_context[ci] = 0; /* zero diff category */
  678. else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
  679. entropy->dc_context[ci] += 8; /* large diff category */
  680. /* Figure F.9: Encoding the magnitude bit pattern of v */
  681. st += 14;
  682. while (m >>= 1)
  683. arith_encode(cinfo, st, (m & v) ? 1 : 0);
  684. }
  685. /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
  686. tbl = compptr->ac_tbl_no;
  687. /* Establish EOB (end-of-block) index */
  688. for (ke = DCTSIZE2 - 1; ke > 0; ke--)
  689. if ((*block)[jpeg_natural_order[ke]]) break;
  690. /* Figure F.5: Encode_AC_Coefficients */
  691. for (k = 1; k <= ke; k++) {
  692. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  693. arith_encode(cinfo, st, 0); /* EOB decision */
  694. while ((v = (*block)[jpeg_natural_order[k]]) == 0) {
  695. arith_encode(cinfo, st + 1, 0); st += 3; k++;
  696. }
  697. arith_encode(cinfo, st + 1, 1);
  698. /* Figure F.6: Encoding nonzero value v */
  699. /* Figure F.7: Encoding the sign of v */
  700. if (v > 0) {
  701. arith_encode(cinfo, entropy->fixed_bin, 0);
  702. } else {
  703. v = -v;
  704. arith_encode(cinfo, entropy->fixed_bin, 1);
  705. }
  706. st += 2;
  707. /* Figure F.8: Encoding the magnitude category of v */
  708. m = 0;
  709. if (v -= 1) {
  710. arith_encode(cinfo, st, 1);
  711. m = 1;
  712. v2 = v;
  713. if (v2 >>= 1) {
  714. arith_encode(cinfo, st, 1);
  715. m <<= 1;
  716. st = entropy->ac_stats[tbl] +
  717. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  718. while (v2 >>= 1) {
  719. arith_encode(cinfo, st, 1);
  720. m <<= 1;
  721. st += 1;
  722. }
  723. }
  724. }
  725. arith_encode(cinfo, st, 0);
  726. /* Figure F.9: Encoding the magnitude bit pattern of v */
  727. st += 14;
  728. while (m >>= 1)
  729. arith_encode(cinfo, st, (m & v) ? 1 : 0);
  730. }
  731. /* Encode EOB decision only if k <= DCTSIZE2 - 1 */
  732. if (k <= DCTSIZE2 - 1) {
  733. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  734. arith_encode(cinfo, st, 1);
  735. }
  736. }
  737. return TRUE;
  738. }
  739. /*
  740. * Initialize for an arithmetic-compressed scan.
  741. */
  742. METHODDEF(void)
  743. start_pass(j_compress_ptr cinfo, boolean gather_statistics)
  744. {
  745. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  746. int ci, tbl;
  747. jpeg_component_info *compptr;
  748. boolean progressive_mode;
  749. if (gather_statistics)
  750. /* Make sure to avoid that in the master control logic!
  751. * We are fully adaptive here and need no extra
  752. * statistics gathering pass!
  753. */
  754. ERREXIT(cinfo, JERR_NOT_COMPILED);
  755. /* We assume jcmaster.c already validated the progressive scan parameters. */
  756. /* Trellis optimization does DC and AC in same pass and without refinement
  757. * so consider progressive mode to be off in such case */
  758. progressive_mode = (cinfo->master->trellis_passes) ? FALSE : cinfo->progressive_mode;
  759. /* Select execution routines */
  760. if (progressive_mode) {
  761. if (cinfo->Ah == 0) {
  762. if (cinfo->Ss == 0)
  763. entropy->pub.encode_mcu = encode_mcu_DC_first;
  764. else
  765. entropy->pub.encode_mcu = encode_mcu_AC_first;
  766. } else {
  767. if (cinfo->Ss == 0)
  768. entropy->pub.encode_mcu = encode_mcu_DC_refine;
  769. else
  770. entropy->pub.encode_mcu = encode_mcu_AC_refine;
  771. }
  772. } else
  773. entropy->pub.encode_mcu = encode_mcu;
  774. /* Allocate & initialize requested statistics areas */
  775. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  776. compptr = cinfo->cur_comp_info[ci];
  777. /* DC needs no table for refinement scan */
  778. if (progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  779. tbl = compptr->dc_tbl_no;
  780. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  781. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  782. if (entropy->dc_stats[tbl] == NULL)
  783. entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
  784. ((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS);
  785. MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
  786. /* Initialize DC predictions to 0 */
  787. entropy->last_dc_val[ci] = 0;
  788. entropy->dc_context[ci] = 0;
  789. }
  790. /* AC needs no table when not present */
  791. if (progressive_mode == 0 || cinfo->Se) {
  792. tbl = compptr->ac_tbl_no;
  793. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  794. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  795. if (entropy->ac_stats[tbl] == NULL)
  796. entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
  797. ((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS);
  798. MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
  799. #ifdef CALCULATE_SPECTRAL_CONDITIONING
  800. if (progressive_mode)
  801. /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
  802. cinfo->arith_ac_K[tbl] = cinfo->Ss +
  803. ((8 + cinfo->Se - cinfo->Ss) >> 4);
  804. #endif
  805. }
  806. }
  807. /* Initialize arithmetic encoding variables */
  808. entropy->c = 0;
  809. entropy->a = 0x10000L;
  810. entropy->sc = 0;
  811. entropy->zc = 0;
  812. entropy->ct = 11;
  813. entropy->buffer = -1; /* empty */
  814. /* Initialize restart stuff */
  815. entropy->restarts_to_go = cinfo->restart_interval;
  816. entropy->next_restart_num = 0;
  817. }
  818. /*
  819. * Module initialization routine for arithmetic entropy encoding.
  820. */
  821. GLOBAL(void)
  822. jinit_arith_encoder(j_compress_ptr cinfo)
  823. {
  824. arith_entropy_ptr entropy;
  825. int i;
  826. entropy = (arith_entropy_ptr)
  827. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  828. sizeof(arith_entropy_encoder));
  829. cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
  830. entropy->pub.start_pass = start_pass;
  831. entropy->pub.finish_pass = finish_pass;
  832. /* Mark tables unallocated */
  833. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  834. entropy->dc_stats[i] = NULL;
  835. entropy->ac_stats[i] = NULL;
  836. }
  837. /* Initialize index for fixed probability estimation */
  838. entropy->fixed_bin[0] = 113;
  839. }
  840. GLOBAL(void)
  841. jget_arith_rates (j_compress_ptr cinfo, int dc_tbl_no, int ac_tbl_no, arith_rates *r)
  842. {
  843. int i;
  844. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  845. r->arith_dc_L = cinfo->arith_dc_L[dc_tbl_no];
  846. r->arith_dc_U = cinfo->arith_dc_U[dc_tbl_no];
  847. r->arith_ac_K = cinfo->arith_ac_K[ac_tbl_no];
  848. for (i = 0; i < DC_STAT_BINS; i++) {
  849. int state = entropy->dc_stats[dc_tbl_no][i];
  850. int mps_val = state >> 7;
  851. float prob_lps = (jpeg_aritab[state & 0x7f] >> 16) / 46340.95; /* 32768*sqrt(2) */
  852. float prob_0 = (mps_val) ? prob_lps : 1.0 - prob_lps;
  853. float prob_1 = 1.0 - prob_0;
  854. r->rate_dc[i][0] = -log(prob_0) / log(2.0);
  855. r->rate_dc[i][1] = -log(prob_1) / log(2.0);
  856. }
  857. for (i = 0; i < AC_STAT_BINS; i++) {
  858. int state = entropy->ac_stats[ac_tbl_no][i];
  859. int mps_val = state >> 7;
  860. float prob_lps = (jpeg_aritab[state & 0x7f] >> 16) / 46340.95;
  861. float prob_0 = (mps_val) ? prob_lps : 1.0 - prob_lps;
  862. float prob_1 = 1.0 - prob_0;
  863. r->rate_ac[i][0] = -log(prob_0) / log(2.0);
  864. r->rate_ac[i][1] = -log(prob_1) / log(2.0);
  865. }
  866. }