jcarith.c 28 KB

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