jcarith.c 29 KB

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