jdarith.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * jdarith.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Developed 1997-2015 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. #define NEG_1 ((unsigned int)-1)
  25. /* Expanded entropy decoder object for arithmetic decoding. */
  26. typedef struct {
  27. struct jpeg_entropy_decoder pub; /* public fields */
  28. JLONG c; /* C register, base of coding interval + input bit buffer */
  29. JLONG a; /* A register, normalized size of coding interval */
  30. int ct; /* bit shift counter, # of bits left in bit buffer part of C */
  31. /* init: ct = -16 */
  32. /* run: ct = 0..7 */
  33. /* error: ct = -1 */
  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. /* Pointers to statistics areas (these workspaces have image lifespan) */
  38. unsigned char *dc_stats[NUM_ARITH_TBLS];
  39. unsigned char *ac_stats[NUM_ARITH_TBLS];
  40. /* Statistics bin for coding with fixed probability 0.5 */
  41. unsigned char fixed_bin[4];
  42. } arith_entropy_decoder;
  43. typedef arith_entropy_decoder *arith_entropy_ptr;
  44. /* The following two definitions specify the allocation chunk size
  45. * for the statistics area.
  46. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
  47. * 49 statistics bins for DC, and 245 statistics bins for AC coding.
  48. *
  49. * We use a compact representation with 1 byte per statistics bin,
  50. * thus the numbers directly represent byte sizes.
  51. * This 1 byte per statistics bin contains the meaning of the MPS
  52. * (more probable symbol) in the highest bit (mask 0x80), and the
  53. * index into the probability estimation state machine table
  54. * in the lower bits (mask 0x7F).
  55. */
  56. #define DC_STAT_BINS 64
  57. #define AC_STAT_BINS 256
  58. LOCAL(int)
  59. get_byte(j_decompress_ptr cinfo)
  60. /* Read next input byte; we do not support suspension in this module. */
  61. {
  62. struct jpeg_source_mgr *src = cinfo->src;
  63. if (src->bytes_in_buffer == 0)
  64. if (!(*src->fill_input_buffer) (cinfo))
  65. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  66. src->bytes_in_buffer--;
  67. return GETJOCTET(*src->next_input_byte++);
  68. }
  69. /*
  70. * The core arithmetic decoding routine (common in JPEG and JBIG).
  71. * This needs to go as fast as possible.
  72. * Machine-dependent optimization facilities
  73. * are not utilized in this portable implementation.
  74. * However, this code should be fairly efficient and
  75. * may be a good base for further optimizations anyway.
  76. *
  77. * Return value is 0 or 1 (binary decision).
  78. *
  79. * Note: I've changed the handling of the code base & bit
  80. * buffer register C compared to other implementations
  81. * based on the standards layout & procedures.
  82. * While it also contains both the actual base of the
  83. * coding interval (16 bits) and the next-bits buffer,
  84. * the cut-point between these two parts is floating
  85. * (instead of fixed) with the bit shift counter CT.
  86. * Thus, we also need only one (variable instead of
  87. * fixed size) shift for the LPS/MPS decision, and
  88. * we can do away with any renormalization update
  89. * of C (except for new data insertion, of course).
  90. *
  91. * I've also introduced a new scheme for accessing
  92. * the probability estimation state machine table,
  93. * derived from Markus Kuhn's JBIG implementation.
  94. */
  95. LOCAL(int)
  96. arith_decode(j_decompress_ptr cinfo, unsigned char *st)
  97. {
  98. register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy;
  99. register unsigned char nl, nm;
  100. register JLONG qe, temp;
  101. register int sv, data;
  102. /* Renormalization & data input per section D.2.6 */
  103. while (e->a < 0x8000L) {
  104. if (--e->ct < 0) {
  105. /* Need to fetch next data byte */
  106. if (cinfo->unread_marker)
  107. data = 0; /* stuff zero data */
  108. else {
  109. data = get_byte(cinfo); /* read next input byte */
  110. if (data == 0xFF) { /* zero stuff or marker code */
  111. do data = get_byte(cinfo);
  112. while (data == 0xFF); /* swallow extra 0xFF bytes */
  113. if (data == 0)
  114. data = 0xFF; /* discard stuffed zero byte */
  115. else {
  116. /* Note: Different from the Huffman decoder, hitting
  117. * a marker while processing the compressed data
  118. * segment is legal in arithmetic coding.
  119. * The convention is to supply zero data
  120. * then until decoding is complete.
  121. */
  122. cinfo->unread_marker = data;
  123. data = 0;
  124. }
  125. }
  126. }
  127. e->c = (e->c << 8) | data; /* insert data into C register */
  128. if ((e->ct += 8) < 0) /* update bit shift counter */
  129. /* Need more initial bytes */
  130. if (++e->ct == 0)
  131. /* Got 2 initial bytes -> re-init A and exit loop */
  132. e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
  133. }
  134. e->a <<= 1;
  135. }
  136. /* Fetch values from our compact representation of Table D.2:
  137. * Qe values and probability estimation state machine
  138. */
  139. sv = *st;
  140. qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
  141. nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
  142. nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
  143. /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
  144. temp = e->a - qe;
  145. e->a = temp;
  146. temp <<= e->ct;
  147. if (e->c >= temp) {
  148. e->c -= temp;
  149. /* Conditional LPS (less probable symbol) exchange */
  150. if (e->a < qe) {
  151. e->a = qe;
  152. *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
  153. } else {
  154. e->a = qe;
  155. *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
  156. sv ^= 0x80; /* Exchange LPS/MPS */
  157. }
  158. } else if (e->a < 0x8000L) {
  159. /* Conditional MPS (more probable symbol) exchange */
  160. if (e->a < qe) {
  161. *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
  162. sv ^= 0x80; /* Exchange LPS/MPS */
  163. } else {
  164. *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
  165. }
  166. }
  167. return sv >> 7;
  168. }
  169. /*
  170. * Check for a restart marker & resynchronize decoder.
  171. */
  172. LOCAL(void)
  173. process_restart(j_decompress_ptr cinfo)
  174. {
  175. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  176. int ci;
  177. jpeg_component_info *compptr;
  178. /* Advance past the RSTn marker */
  179. if (!(*cinfo->marker->read_restart_marker) (cinfo))
  180. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  181. /* Re-initialize statistics areas */
  182. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  183. compptr = cinfo->cur_comp_info[ci];
  184. if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  185. MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
  186. /* Reset DC predictions to 0 */
  187. entropy->last_dc_val[ci] = 0;
  188. entropy->dc_context[ci] = 0;
  189. }
  190. if (!cinfo->progressive_mode || cinfo->Ss) {
  191. MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
  192. }
  193. }
  194. /* Reset arithmetic decoding variables */
  195. entropy->c = 0;
  196. entropy->a = 0;
  197. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  198. /* Reset restart counter */
  199. entropy->restarts_to_go = cinfo->restart_interval;
  200. }
  201. /*
  202. * Arithmetic MCU decoding.
  203. * Each of these routines decodes and returns one MCU's worth of
  204. * arithmetic-compressed coefficients.
  205. * The coefficients are reordered from zigzag order into natural array order,
  206. * but are not dequantized.
  207. *
  208. * The i'th block of the MCU is stored into the block pointed to by
  209. * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  210. */
  211. /*
  212. * MCU decoding for DC initial scan (either spectral selection,
  213. * or first pass of successive approximation).
  214. */
  215. METHODDEF(boolean)
  216. decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  217. {
  218. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  219. JBLOCKROW block;
  220. unsigned char *st;
  221. int blkn, ci, tbl, sign;
  222. int v, m;
  223. /* Process restart marker if needed */
  224. if (cinfo->restart_interval) {
  225. if (entropy->restarts_to_go == 0)
  226. process_restart(cinfo);
  227. entropy->restarts_to_go--;
  228. }
  229. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  230. /* Outer loop handles each block in the MCU */
  231. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  232. block = MCU_data[blkn];
  233. ci = cinfo->MCU_membership[blkn];
  234. tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
  235. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  236. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  237. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  238. /* Figure F.19: Decode_DC_DIFF */
  239. if (arith_decode(cinfo, st) == 0)
  240. entropy->dc_context[ci] = 0;
  241. else {
  242. /* Figure F.21: Decoding nonzero value v */
  243. /* Figure F.22: Decoding the sign of v */
  244. sign = arith_decode(cinfo, st + 1);
  245. st += 2; st += sign;
  246. /* Figure F.23: Decoding the magnitude category of v */
  247. if ((m = arith_decode(cinfo, st)) != 0) {
  248. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  249. while (arith_decode(cinfo, st)) {
  250. if ((m <<= 1) == 0x8000) {
  251. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  252. entropy->ct = -1; /* magnitude overflow */
  253. return TRUE;
  254. }
  255. st += 1;
  256. }
  257. }
  258. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  259. if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
  260. entropy->dc_context[ci] = 0; /* zero diff category */
  261. else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
  262. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  263. else
  264. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  265. v = m;
  266. /* Figure F.24: Decoding the magnitude bit pattern of v */
  267. st += 14;
  268. while (m >>= 1)
  269. if (arith_decode(cinfo, st)) v |= m;
  270. v += 1; if (sign) v = -v;
  271. entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
  272. }
  273. /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
  274. (*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
  275. }
  276. return TRUE;
  277. }
  278. /*
  279. * MCU decoding for AC initial scan (either spectral selection,
  280. * or first pass of successive approximation).
  281. */
  282. METHODDEF(boolean)
  283. decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  284. {
  285. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  286. JBLOCKROW block;
  287. unsigned char *st;
  288. int tbl, sign, k;
  289. int v, m;
  290. /* Process restart marker if needed */
  291. if (cinfo->restart_interval) {
  292. if (entropy->restarts_to_go == 0)
  293. process_restart(cinfo);
  294. entropy->restarts_to_go--;
  295. }
  296. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  297. /* There is always only one block per MCU */
  298. block = MCU_data[0];
  299. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  300. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  301. /* Figure F.20: Decode_AC_coefficients */
  302. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  303. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  304. if (arith_decode(cinfo, st)) break; /* EOB flag */
  305. while (arith_decode(cinfo, st + 1) == 0) {
  306. st += 3; k++;
  307. if (k > cinfo->Se) {
  308. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  309. entropy->ct = -1; /* spectral overflow */
  310. return TRUE;
  311. }
  312. }
  313. /* Figure F.21: Decoding nonzero value v */
  314. /* Figure F.22: Decoding the sign of v */
  315. sign = arith_decode(cinfo, entropy->fixed_bin);
  316. st += 2;
  317. /* Figure F.23: Decoding the magnitude category of v */
  318. if ((m = arith_decode(cinfo, st)) != 0) {
  319. if (arith_decode(cinfo, st)) {
  320. m <<= 1;
  321. st = entropy->ac_stats[tbl] +
  322. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  323. while (arith_decode(cinfo, st)) {
  324. if ((m <<= 1) == 0x8000) {
  325. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  326. entropy->ct = -1; /* magnitude overflow */
  327. return TRUE;
  328. }
  329. st += 1;
  330. }
  331. }
  332. }
  333. v = m;
  334. /* Figure F.24: Decoding the magnitude bit pattern of v */
  335. st += 14;
  336. while (m >>= 1)
  337. if (arith_decode(cinfo, st)) v |= m;
  338. v += 1; if (sign) v = -v;
  339. /* Scale and output coefficient in natural (dezigzagged) order */
  340. (*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al);
  341. }
  342. return TRUE;
  343. }
  344. /*
  345. * MCU decoding for DC successive approximation refinement scan.
  346. */
  347. METHODDEF(boolean)
  348. decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  349. {
  350. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  351. unsigned char *st;
  352. int p1, blkn;
  353. /* Process restart marker if needed */
  354. if (cinfo->restart_interval) {
  355. if (entropy->restarts_to_go == 0)
  356. process_restart(cinfo);
  357. entropy->restarts_to_go--;
  358. }
  359. st = entropy->fixed_bin; /* use fixed probability estimation */
  360. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  361. /* Outer loop handles each block in the MCU */
  362. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  363. /* Encoded data is simply the next bit of the two's-complement DC value */
  364. if (arith_decode(cinfo, st))
  365. MCU_data[blkn][0][0] |= p1;
  366. }
  367. return TRUE;
  368. }
  369. /*
  370. * MCU decoding for AC successive approximation refinement scan.
  371. */
  372. METHODDEF(boolean)
  373. decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  374. {
  375. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  376. JBLOCKROW block;
  377. JCOEFPTR thiscoef;
  378. unsigned char *st;
  379. int tbl, k, kex;
  380. int p1, m1;
  381. /* Process restart marker if needed */
  382. if (cinfo->restart_interval) {
  383. if (entropy->restarts_to_go == 0)
  384. process_restart(cinfo);
  385. entropy->restarts_to_go--;
  386. }
  387. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  388. /* There is always only one block per MCU */
  389. block = MCU_data[0];
  390. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  391. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  392. m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
  393. /* Establish EOBx (previous stage end-of-block) index */
  394. for (kex = cinfo->Se; kex > 0; kex--)
  395. if ((*block)[jpeg_natural_order[kex]]) break;
  396. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  397. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  398. if (k > kex)
  399. if (arith_decode(cinfo, st)) break; /* EOB flag */
  400. for (;;) {
  401. thiscoef = *block + jpeg_natural_order[k];
  402. if (*thiscoef) { /* previously nonzero coef */
  403. if (arith_decode(cinfo, st + 2)) {
  404. if (*thiscoef < 0)
  405. *thiscoef += m1;
  406. else
  407. *thiscoef += p1;
  408. }
  409. break;
  410. }
  411. if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
  412. if (arith_decode(cinfo, entropy->fixed_bin))
  413. *thiscoef = m1;
  414. else
  415. *thiscoef = p1;
  416. break;
  417. }
  418. st += 3; k++;
  419. if (k > cinfo->Se) {
  420. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  421. entropy->ct = -1; /* spectral overflow */
  422. return TRUE;
  423. }
  424. }
  425. }
  426. return TRUE;
  427. }
  428. /*
  429. * Decode one MCU's worth of arithmetic-compressed coefficients.
  430. */
  431. METHODDEF(boolean)
  432. decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  433. {
  434. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  435. jpeg_component_info *compptr;
  436. JBLOCKROW block;
  437. unsigned char *st;
  438. int blkn, ci, tbl, sign, k;
  439. int v, m;
  440. /* Process restart marker if needed */
  441. if (cinfo->restart_interval) {
  442. if (entropy->restarts_to_go == 0)
  443. process_restart(cinfo);
  444. entropy->restarts_to_go--;
  445. }
  446. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  447. /* Outer loop handles each block in the MCU */
  448. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  449. block = MCU_data ? MCU_data[blkn] : NULL;
  450. ci = cinfo->MCU_membership[blkn];
  451. compptr = cinfo->cur_comp_info[ci];
  452. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  453. tbl = compptr->dc_tbl_no;
  454. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  455. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  456. /* Figure F.19: Decode_DC_DIFF */
  457. if (arith_decode(cinfo, st) == 0)
  458. entropy->dc_context[ci] = 0;
  459. else {
  460. /* Figure F.21: Decoding nonzero value v */
  461. /* Figure F.22: Decoding the sign of v */
  462. sign = arith_decode(cinfo, st + 1);
  463. st += 2; st += sign;
  464. /* Figure F.23: Decoding the magnitude category of v */
  465. if ((m = arith_decode(cinfo, st)) != 0) {
  466. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  467. while (arith_decode(cinfo, st)) {
  468. if ((m <<= 1) == 0x8000) {
  469. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  470. entropy->ct = -1; /* magnitude overflow */
  471. return TRUE;
  472. }
  473. st += 1;
  474. }
  475. }
  476. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  477. if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
  478. entropy->dc_context[ci] = 0; /* zero diff category */
  479. else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
  480. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  481. else
  482. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  483. v = m;
  484. /* Figure F.24: Decoding the magnitude bit pattern of v */
  485. st += 14;
  486. while (m >>= 1)
  487. if (arith_decode(cinfo, st)) v |= m;
  488. v += 1; if (sign) v = -v;
  489. entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
  490. }
  491. if (block)
  492. (*block)[0] = (JCOEF)entropy->last_dc_val[ci];
  493. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  494. tbl = compptr->ac_tbl_no;
  495. /* Figure F.20: Decode_AC_coefficients */
  496. for (k = 1; k <= DCTSIZE2 - 1; k++) {
  497. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  498. if (arith_decode(cinfo, st)) break; /* EOB flag */
  499. while (arith_decode(cinfo, st + 1) == 0) {
  500. st += 3; k++;
  501. if (k > DCTSIZE2 - 1) {
  502. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  503. entropy->ct = -1; /* spectral overflow */
  504. return TRUE;
  505. }
  506. }
  507. /* Figure F.21: Decoding nonzero value v */
  508. /* Figure F.22: Decoding the sign of v */
  509. sign = arith_decode(cinfo, entropy->fixed_bin);
  510. st += 2;
  511. /* Figure F.23: Decoding the magnitude category of v */
  512. if ((m = arith_decode(cinfo, st)) != 0) {
  513. if (arith_decode(cinfo, st)) {
  514. m <<= 1;
  515. st = entropy->ac_stats[tbl] +
  516. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  517. while (arith_decode(cinfo, st)) {
  518. if ((m <<= 1) == 0x8000) {
  519. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  520. entropy->ct = -1; /* magnitude overflow */
  521. return TRUE;
  522. }
  523. st += 1;
  524. }
  525. }
  526. }
  527. v = m;
  528. /* Figure F.24: Decoding the magnitude bit pattern of v */
  529. st += 14;
  530. while (m >>= 1)
  531. if (arith_decode(cinfo, st)) v |= m;
  532. v += 1; if (sign) v = -v;
  533. if (block)
  534. (*block)[jpeg_natural_order[k]] = (JCOEF)v;
  535. }
  536. }
  537. return TRUE;
  538. }
  539. /*
  540. * Initialize for an arithmetic-compressed scan.
  541. */
  542. METHODDEF(void)
  543. start_pass(j_decompress_ptr cinfo)
  544. {
  545. arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
  546. int ci, tbl;
  547. jpeg_component_info *compptr;
  548. if (cinfo->progressive_mode) {
  549. /* Validate progressive scan parameters */
  550. if (cinfo->Ss == 0) {
  551. if (cinfo->Se != 0)
  552. goto bad;
  553. } else {
  554. /* need not check Ss/Se < 0 since they came from unsigned bytes */
  555. if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
  556. goto bad;
  557. /* AC scans may have only one component */
  558. if (cinfo->comps_in_scan != 1)
  559. goto bad;
  560. }
  561. if (cinfo->Ah != 0) {
  562. /* Successive approximation refinement scan: must have Al = Ah-1. */
  563. if (cinfo->Ah - 1 != cinfo->Al)
  564. goto bad;
  565. }
  566. if (cinfo->Al > 13) { /* need not check for < 0 */
  567. bad:
  568. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  569. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  570. }
  571. /* Update progression status, and verify that scan order is legal.
  572. * Note that inter-scan inconsistencies are treated as warnings
  573. * not fatal errors ... not clear if this is right way to behave.
  574. */
  575. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  576. int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
  577. int *coef_bit_ptr = &cinfo->coef_bits[cindex][0];
  578. if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  579. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  580. for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  581. int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  582. if (cinfo->Ah != expected)
  583. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  584. coef_bit_ptr[coefi] = cinfo->Al;
  585. }
  586. }
  587. /* Select MCU decoding routine */
  588. if (cinfo->Ah == 0) {
  589. if (cinfo->Ss == 0)
  590. entropy->pub.decode_mcu = decode_mcu_DC_first;
  591. else
  592. entropy->pub.decode_mcu = decode_mcu_AC_first;
  593. } else {
  594. if (cinfo->Ss == 0)
  595. entropy->pub.decode_mcu = decode_mcu_DC_refine;
  596. else
  597. entropy->pub.decode_mcu = decode_mcu_AC_refine;
  598. }
  599. } else {
  600. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  601. * This ought to be an error condition, but we make it a warning.
  602. */
  603. if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
  604. (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
  605. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  606. /* Select MCU decoding routine */
  607. entropy->pub.decode_mcu = decode_mcu;
  608. }
  609. /* Allocate & initialize requested statistics areas */
  610. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  611. compptr = cinfo->cur_comp_info[ci];
  612. if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  613. tbl = compptr->dc_tbl_no;
  614. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  615. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  616. if (entropy->dc_stats[tbl] == NULL)
  617. entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
  618. ((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS);
  619. MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
  620. /* Initialize DC predictions to 0 */
  621. entropy->last_dc_val[ci] = 0;
  622. entropy->dc_context[ci] = 0;
  623. }
  624. if (!cinfo->progressive_mode || cinfo->Ss) {
  625. tbl = compptr->ac_tbl_no;
  626. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  627. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  628. if (entropy->ac_stats[tbl] == NULL)
  629. entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small)
  630. ((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS);
  631. MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
  632. }
  633. }
  634. /* Initialize arithmetic decoding variables */
  635. entropy->c = 0;
  636. entropy->a = 0;
  637. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  638. /* Initialize restart counter */
  639. entropy->restarts_to_go = cinfo->restart_interval;
  640. }
  641. /*
  642. * Module initialization routine for arithmetic entropy decoding.
  643. */
  644. GLOBAL(void)
  645. jinit_arith_decoder(j_decompress_ptr cinfo)
  646. {
  647. arith_entropy_ptr entropy;
  648. int i;
  649. entropy = (arith_entropy_ptr)
  650. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  651. sizeof(arith_entropy_decoder));
  652. cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
  653. entropy->pub.start_pass = start_pass;
  654. /* Mark tables unallocated */
  655. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  656. entropy->dc_stats[i] = NULL;
  657. entropy->ac_stats[i] = NULL;
  658. }
  659. /* Initialize index for fixed probability estimation */
  660. entropy->fixed_bin[0] = 113;
  661. if (cinfo->progressive_mode) {
  662. /* Create progression status table */
  663. int *coef_bit_ptr, ci;
  664. cinfo->coef_bits = (int (*)[DCTSIZE2])
  665. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  666. cinfo->num_components * DCTSIZE2 *
  667. sizeof(int));
  668. coef_bit_ptr = &cinfo->coef_bits[0][0];
  669. for (ci = 0; ci < cinfo->num_components; ci++)
  670. for (i = 0; i < DCTSIZE2; i++)
  671. *coef_bit_ptr++ = -1;
  672. }
  673. }