/src/FreeImage/Source/LibJPEG/jdarith.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 776 lines · 497 code · 101 blank · 178 comment · 189 complexity · d17d1f9dfb57b0cfbc2419a302a21cc6 MD5 · raw file

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