jdarith.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * jdarith.c
  3. *
  4. * Developed 1997 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. } arith_entropy_decoder;
  34. typedef arith_entropy_decoder * arith_entropy_ptr;
  35. /* The following two definitions specify the allocation chunk size
  36. * for the statistics area.
  37. * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
  38. * 49 statistics bins for DC, and 245 statistics bins for AC coding.
  39. * Note that we use one additional AC bin for codings with fixed
  40. * probability (0.5), thus the minimum number for AC is 246.
  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. extern const INT32 jaritab[];
  92. register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
  93. register unsigned char nl, nm;
  94. register INT32 qe, temp;
  95. register int sv, data;
  96. /* Renormalization & data input per section D.2.6 */
  97. while (e->a < 0x8000L) {
  98. if (--e->ct < 0) {
  99. /* Need to fetch next data byte */
  100. if (cinfo->unread_marker)
  101. data = 0; /* stuff zero data */
  102. else {
  103. data = get_byte(cinfo); /* read next input byte */
  104. if (data == 0xFF) { /* zero stuff or marker code */
  105. do data = get_byte(cinfo);
  106. while (data == 0xFF); /* swallow extra 0xFF bytes */
  107. if (data == 0)
  108. data = 0xFF; /* discard stuffed zero byte */
  109. else {
  110. /* Note: Different from the Huffman decoder, hitting
  111. * a marker while processing the compressed data
  112. * segment is legal in arithmetic coding.
  113. * The convention is to supply zero data
  114. * then until decoding is complete.
  115. */
  116. cinfo->unread_marker = data;
  117. data = 0;
  118. }
  119. }
  120. }
  121. e->c = (e->c << 8) | data; /* insert data into C register */
  122. if ((e->ct += 8) < 0) /* update bit shift counter */
  123. /* Need more initial bytes */
  124. if (++e->ct == 0)
  125. /* Got 2 initial bytes -> re-init A and exit loop */
  126. e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
  127. }
  128. e->a <<= 1;
  129. }
  130. /* Fetch values from our compact representation of Table D.2:
  131. * Qe values and probability estimation state machine
  132. */
  133. sv = *st;
  134. qe = jaritab[sv & 0x7F]; /* => Qe_Value */
  135. nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
  136. nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
  137. /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
  138. temp = e->a - qe;
  139. e->a = temp;
  140. temp <<= e->ct;
  141. if (e->c >= temp) {
  142. e->c -= temp;
  143. /* Conditional LPS (less probable symbol) exchange */
  144. if (e->a < qe) {
  145. e->a = qe;
  146. *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
  147. } else {
  148. e->a = qe;
  149. *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
  150. sv ^= 0x80; /* Exchange LPS/MPS */
  151. }
  152. } else if (e->a < 0x8000L) {
  153. /* Conditional MPS (more probable symbol) exchange */
  154. if (e->a < qe) {
  155. *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
  156. sv ^= 0x80; /* Exchange LPS/MPS */
  157. } else {
  158. *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
  159. }
  160. }
  161. return sv >> 7;
  162. }
  163. /*
  164. * Check for a restart marker & resynchronize decoder.
  165. */
  166. LOCAL(void)
  167. process_restart (j_decompress_ptr cinfo)
  168. {
  169. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  170. int ci;
  171. jpeg_component_info * compptr;
  172. /* Advance past the RSTn marker */
  173. if (! (*cinfo->marker->read_restart_marker) (cinfo))
  174. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  175. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  176. compptr = cinfo->cur_comp_info[ci];
  177. /* Re-initialize statistics areas */
  178. if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  179. MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
  180. /* Reset DC predictions to 0 */
  181. entropy->last_dc_val[ci] = 0;
  182. entropy->dc_context[ci] = 0;
  183. }
  184. if (cinfo->progressive_mode == 0 || 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) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
  254. entropy->dc_context[ci] = 0; /* zero diff category */
  255. else if (m > (int) (((INT32) 1 << 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. /* Process restart marker if needed */
  285. if (cinfo->restart_interval) {
  286. if (entropy->restarts_to_go == 0)
  287. process_restart(cinfo);
  288. entropy->restarts_to_go--;
  289. }
  290. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  291. /* There is always only one block per MCU */
  292. block = MCU_data[0];
  293. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  294. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  295. /* Figure F.20: Decode_AC_coefficients */
  296. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  297. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  298. if (arith_decode(cinfo, st)) break; /* EOB flag */
  299. while (arith_decode(cinfo, st + 1) == 0) {
  300. st += 3; k++;
  301. if (k > cinfo->Se) {
  302. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  303. entropy->ct = -1; /* spectral overflow */
  304. return TRUE;
  305. }
  306. }
  307. /* Figure F.21: Decoding nonzero value v */
  308. /* Figure F.22: Decoding the sign of v */
  309. entropy->ac_stats[tbl][245] = 0;
  310. sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
  311. st += 2;
  312. /* Figure F.23: Decoding the magnitude category of v */
  313. if ((m = arith_decode(cinfo, st)) != 0) {
  314. if (arith_decode(cinfo, st)) {
  315. m <<= 1;
  316. st = entropy->ac_stats[tbl] +
  317. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  318. while (arith_decode(cinfo, st)) {
  319. if ((m <<= 1) == 0x8000) {
  320. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  321. entropy->ct = -1; /* magnitude overflow */
  322. return TRUE;
  323. }
  324. st += 1;
  325. }
  326. }
  327. }
  328. v = m;
  329. /* Figure F.24: Decoding the magnitude bit pattern of v */
  330. st += 14;
  331. while (m >>= 1)
  332. if (arith_decode(cinfo, st)) v |= m;
  333. v += 1; if (sign) v = -v;
  334. /* Scale and output coefficient in natural (dezigzagged) order */
  335. (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
  336. }
  337. return TRUE;
  338. }
  339. /*
  340. * MCU decoding for DC successive approximation refinement scan.
  341. */
  342. METHODDEF(boolean)
  343. decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  344. {
  345. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  346. unsigned char st[4];
  347. int p1, blkn;
  348. /* Process restart marker if needed */
  349. if (cinfo->restart_interval) {
  350. if (entropy->restarts_to_go == 0)
  351. process_restart(cinfo);
  352. entropy->restarts_to_go--;
  353. }
  354. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  355. /* Outer loop handles each block in the MCU */
  356. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  357. st[0] = 0; /* use fixed probability estimation */
  358. /* Encoded data is simply the next bit of the two's-complement DC value */
  359. if (arith_decode(cinfo, st))
  360. MCU_data[blkn][0][0] |= p1;
  361. }
  362. return TRUE;
  363. }
  364. /*
  365. * MCU decoding for AC successive approximation refinement scan.
  366. */
  367. METHODDEF(boolean)
  368. decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  369. {
  370. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  371. JBLOCKROW block;
  372. JCOEFPTR thiscoef;
  373. unsigned char *st;
  374. int tbl, k, kex;
  375. int p1, m1;
  376. /* Process restart marker if needed */
  377. if (cinfo->restart_interval) {
  378. if (entropy->restarts_to_go == 0)
  379. process_restart(cinfo);
  380. entropy->restarts_to_go--;
  381. }
  382. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  383. /* There is always only one block per MCU */
  384. block = MCU_data[0];
  385. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  386. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  387. m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
  388. /* Establish EOBx (previous stage end-of-block) index */
  389. for (kex = cinfo->Se + 1; kex > 1; kex--)
  390. if ((*block)[jpeg_natural_order[kex - 1]]) break;
  391. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  392. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  393. if (k >= kex)
  394. if (arith_decode(cinfo, st)) break; /* EOB flag */
  395. for (;;) {
  396. thiscoef = *block + jpeg_natural_order[k];
  397. if (*thiscoef) { /* previously nonzero coef */
  398. if (arith_decode(cinfo, st + 2)) {
  399. if (*thiscoef < 0)
  400. *thiscoef += m1;
  401. else
  402. *thiscoef += p1;
  403. }
  404. break;
  405. }
  406. if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
  407. entropy->ac_stats[tbl][245] = 0;
  408. if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245))
  409. *thiscoef = m1;
  410. else
  411. *thiscoef = p1;
  412. break;
  413. }
  414. st += 3; k++;
  415. if (k > cinfo->Se) {
  416. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  417. entropy->ct = -1; /* spectral overflow */
  418. return TRUE;
  419. }
  420. }
  421. }
  422. return TRUE;
  423. }
  424. /*
  425. * Decode one MCU's worth of arithmetic-compressed coefficients.
  426. */
  427. METHODDEF(boolean)
  428. decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  429. {
  430. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  431. jpeg_component_info * compptr;
  432. JBLOCKROW block;
  433. unsigned char *st;
  434. int blkn, ci, tbl, sign, k;
  435. int v, m;
  436. /* Process restart marker if needed */
  437. if (cinfo->restart_interval) {
  438. if (entropy->restarts_to_go == 0)
  439. process_restart(cinfo);
  440. entropy->restarts_to_go--;
  441. }
  442. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  443. /* Outer loop handles each block in the MCU */
  444. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  445. block = MCU_data[blkn];
  446. ci = cinfo->MCU_membership[blkn];
  447. compptr = cinfo->cur_comp_info[ci];
  448. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  449. tbl = compptr->dc_tbl_no;
  450. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  451. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  452. /* Figure F.19: Decode_DC_DIFF */
  453. if (arith_decode(cinfo, st) == 0)
  454. entropy->dc_context[ci] = 0;
  455. else {
  456. /* Figure F.21: Decoding nonzero value v */
  457. /* Figure F.22: Decoding the sign of v */
  458. sign = arith_decode(cinfo, st + 1);
  459. st += 2; st += sign;
  460. /* Figure F.23: Decoding the magnitude category of v */
  461. if ((m = arith_decode(cinfo, st)) != 0) {
  462. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  463. while (arith_decode(cinfo, st)) {
  464. if ((m <<= 1) == 0x8000) {
  465. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  466. entropy->ct = -1; /* magnitude overflow */
  467. return TRUE;
  468. }
  469. st += 1;
  470. }
  471. }
  472. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  473. if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
  474. entropy->dc_context[ci] = 0; /* zero diff category */
  475. else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
  476. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  477. else
  478. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  479. v = m;
  480. /* Figure F.24: Decoding the magnitude bit pattern of v */
  481. st += 14;
  482. while (m >>= 1)
  483. if (arith_decode(cinfo, st)) v |= m;
  484. v += 1; if (sign) v = -v;
  485. entropy->last_dc_val[ci] += v;
  486. }
  487. (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
  488. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  489. tbl = compptr->ac_tbl_no;
  490. /* Figure F.20: Decode_AC_coefficients */
  491. for (k = 1; k < DCTSIZE2; k++) {
  492. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  493. if (arith_decode(cinfo, st)) break; /* EOB flag */
  494. while (arith_decode(cinfo, st + 1) == 0) {
  495. st += 3; k++;
  496. if (k >= DCTSIZE2) {
  497. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  498. entropy->ct = -1; /* spectral overflow */
  499. return TRUE;
  500. }
  501. }
  502. /* Figure F.21: Decoding nonzero value v */
  503. /* Figure F.22: Decoding the sign of v */
  504. entropy->ac_stats[tbl][245] = 0;
  505. sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
  506. st += 2;
  507. /* Figure F.23: Decoding the magnitude category of v */
  508. if ((m = arith_decode(cinfo, st)) != 0) {
  509. if (arith_decode(cinfo, st)) {
  510. m <<= 1;
  511. st = entropy->ac_stats[tbl] +
  512. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  513. while (arith_decode(cinfo, st)) {
  514. if ((m <<= 1) == 0x8000) {
  515. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  516. entropy->ct = -1; /* magnitude overflow */
  517. return TRUE;
  518. }
  519. st += 1;
  520. }
  521. }
  522. }
  523. v = m;
  524. /* Figure F.24: Decoding the magnitude bit pattern of v */
  525. st += 14;
  526. while (m >>= 1)
  527. if (arith_decode(cinfo, st)) v |= m;
  528. v += 1; if (sign) v = -v;
  529. (*block)[jpeg_natural_order[k]] = (JCOEF) v;
  530. }
  531. }
  532. return TRUE;
  533. }
  534. /*
  535. * Initialize for an arithmetic-compressed scan.
  536. */
  537. METHODDEF(void)
  538. start_pass (j_decompress_ptr cinfo)
  539. {
  540. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  541. int ci, tbl;
  542. jpeg_component_info * compptr;
  543. if (cinfo->progressive_mode) {
  544. /* Validate progressive scan parameters */
  545. if (cinfo->Ss == 0) {
  546. if (cinfo->Se != 0)
  547. goto bad;
  548. } else {
  549. /* need not check Ss/Se < 0 since they came from unsigned bytes */
  550. if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2)
  551. goto bad;
  552. /* AC scans may have only one component */
  553. if (cinfo->comps_in_scan != 1)
  554. goto bad;
  555. }
  556. if (cinfo->Ah != 0) {
  557. /* Successive approximation refinement scan: must have Al = Ah-1. */
  558. if (cinfo->Ah-1 != cinfo->Al)
  559. goto bad;
  560. }
  561. if (cinfo->Al > 13) { /* need not check for < 0 */
  562. bad:
  563. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  564. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  565. }
  566. /* Update progression status, and verify that scan order is legal.
  567. * Note that inter-scan inconsistencies are treated as warnings
  568. * not fatal errors ... not clear if this is right way to behave.
  569. */
  570. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  571. int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
  572. int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
  573. if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  574. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  575. for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  576. int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  577. if (cinfo->Ah != expected)
  578. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  579. coef_bit_ptr[coefi] = cinfo->Al;
  580. }
  581. }
  582. /* Select MCU decoding routine */
  583. if (cinfo->Ah == 0) {
  584. if (cinfo->Ss == 0)
  585. entropy->pub.decode_mcu = decode_mcu_DC_first;
  586. else
  587. entropy->pub.decode_mcu = decode_mcu_AC_first;
  588. } else {
  589. if (cinfo->Ss == 0)
  590. entropy->pub.decode_mcu = decode_mcu_DC_refine;
  591. else
  592. entropy->pub.decode_mcu = decode_mcu_AC_refine;
  593. }
  594. } else {
  595. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  596. * This ought to be an error condition, but we make it a warning because
  597. * there are some baseline files out there with all zeroes in these bytes.
  598. */
  599. if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
  600. cinfo->Ah != 0 || cinfo->Al != 0)
  601. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  602. /* Select MCU decoding routine */
  603. entropy->pub.decode_mcu = decode_mcu;
  604. }
  605. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  606. compptr = cinfo->cur_comp_info[ci];
  607. /* Allocate & initialize requested statistics areas */
  608. if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  609. tbl = compptr->dc_tbl_no;
  610. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  611. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  612. if (entropy->dc_stats[tbl] == NULL)
  613. entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  614. ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
  615. MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
  616. /* Initialize DC predictions to 0 */
  617. entropy->last_dc_val[ci] = 0;
  618. entropy->dc_context[ci] = 0;
  619. }
  620. if (cinfo->progressive_mode == 0 || cinfo->Ss) {
  621. tbl = compptr->ac_tbl_no;
  622. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  623. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  624. if (entropy->ac_stats[tbl] == NULL)
  625. entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  626. ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
  627. MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
  628. }
  629. }
  630. /* Initialize arithmetic decoding variables */
  631. entropy->c = 0;
  632. entropy->a = 0;
  633. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  634. /* Initialize restart counter */
  635. entropy->restarts_to_go = cinfo->restart_interval;
  636. }
  637. /*
  638. * Module initialization routine for arithmetic entropy decoding.
  639. */
  640. GLOBAL(void)
  641. jinit_arith_decoder (j_decompress_ptr cinfo)
  642. {
  643. arith_entropy_ptr entropy;
  644. int i;
  645. entropy = (arith_entropy_ptr)
  646. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  647. SIZEOF(arith_entropy_decoder));
  648. cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  649. entropy->pub.start_pass = start_pass;
  650. /* Mark tables unallocated */
  651. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  652. entropy->dc_stats[i] = NULL;
  653. entropy->ac_stats[i] = NULL;
  654. }
  655. if (cinfo->progressive_mode) {
  656. /* Create progression status table */
  657. int *coef_bit_ptr, ci;
  658. cinfo->coef_bits = (int (*)[DCTSIZE2])
  659. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  660. cinfo->num_components*DCTSIZE2*SIZEOF(int));
  661. coef_bit_ptr = & cinfo->coef_bits[0][0];
  662. for (ci = 0; ci < cinfo->num_components; ci++)
  663. for (i = 0; i < DCTSIZE2; i++)
  664. *coef_bit_ptr++ = -1;
  665. }
  666. }