jdarith.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * jdarith.c
  3. *
  4. * Developed 1997-2009 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.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->Ss) {
  184. MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
  185. }
  186. }
  187. /* Reset arithmetic decoding variables */
  188. entropy->c = 0;
  189. entropy->a = 0;
  190. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  191. /* Reset restart counter */
  192. entropy->restarts_to_go = cinfo->restart_interval;
  193. }
  194. /*
  195. * Arithmetic MCU decoding.
  196. * Each of these routines decodes and returns one MCU's worth of
  197. * arithmetic-compressed coefficients.
  198. * The coefficients are reordered from zigzag order into natural array order,
  199. * but are not dequantized.
  200. *
  201. * The i'th block of the MCU is stored into the block pointed to by
  202. * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  203. */
  204. /*
  205. * MCU decoding for DC initial scan (either spectral selection,
  206. * or first pass of successive approximation).
  207. */
  208. METHODDEF(boolean)
  209. decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  210. {
  211. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  212. JBLOCKROW block;
  213. unsigned char *st;
  214. int blkn, ci, tbl, sign;
  215. int v, m;
  216. /* Process restart marker if needed */
  217. if (cinfo->restart_interval) {
  218. if (entropy->restarts_to_go == 0)
  219. process_restart(cinfo);
  220. entropy->restarts_to_go--;
  221. }
  222. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  223. /* Outer loop handles each block in the MCU */
  224. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  225. block = MCU_data[blkn];
  226. ci = cinfo->MCU_membership[blkn];
  227. tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
  228. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  229. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  230. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  231. /* Figure F.19: Decode_DC_DIFF */
  232. if (arith_decode(cinfo, st) == 0)
  233. entropy->dc_context[ci] = 0;
  234. else {
  235. /* Figure F.21: Decoding nonzero value v */
  236. /* Figure F.22: Decoding the sign of v */
  237. sign = arith_decode(cinfo, st + 1);
  238. st += 2; st += sign;
  239. /* Figure F.23: Decoding the magnitude category of v */
  240. if ((m = arith_decode(cinfo, st)) != 0) {
  241. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  242. while (arith_decode(cinfo, st)) {
  243. if ((m <<= 1) == 0x8000) {
  244. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  245. entropy->ct = -1; /* magnitude overflow */
  246. return TRUE;
  247. }
  248. st += 1;
  249. }
  250. }
  251. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  252. if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
  253. entropy->dc_context[ci] = 0; /* zero diff category */
  254. else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
  255. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  256. else
  257. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  258. v = m;
  259. /* Figure F.24: Decoding the magnitude bit pattern of v */
  260. st += 14;
  261. while (m >>= 1)
  262. if (arith_decode(cinfo, st)) v |= m;
  263. v += 1; if (sign) v = -v;
  264. entropy->last_dc_val[ci] += v;
  265. }
  266. /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
  267. (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
  268. }
  269. return TRUE;
  270. }
  271. /*
  272. * MCU decoding for AC initial scan (either spectral selection,
  273. * or first pass of successive approximation).
  274. */
  275. METHODDEF(boolean)
  276. decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  277. {
  278. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  279. JBLOCKROW block;
  280. unsigned char *st;
  281. int tbl, sign, k;
  282. int v, m;
  283. /* Process restart marker if needed */
  284. if (cinfo->restart_interval) {
  285. if (entropy->restarts_to_go == 0)
  286. process_restart(cinfo);
  287. entropy->restarts_to_go--;
  288. }
  289. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  290. /* There is always only one block per MCU */
  291. block = MCU_data[0];
  292. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  293. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  294. /* Figure F.20: Decode_AC_coefficients */
  295. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  296. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  297. if (arith_decode(cinfo, st)) break; /* EOB flag */
  298. while (arith_decode(cinfo, st + 1) == 0) {
  299. st += 3; k++;
  300. if (k > cinfo->Se) {
  301. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  302. entropy->ct = -1; /* spectral overflow */
  303. return TRUE;
  304. }
  305. }
  306. /* Figure F.21: Decoding nonzero value v */
  307. /* Figure F.22: Decoding the sign of v */
  308. sign = arith_decode(cinfo, entropy->fixed_bin);
  309. st += 2;
  310. /* Figure F.23: Decoding the magnitude category of v */
  311. if ((m = arith_decode(cinfo, st)) != 0) {
  312. if (arith_decode(cinfo, st)) {
  313. m <<= 1;
  314. st = entropy->ac_stats[tbl] +
  315. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  316. while (arith_decode(cinfo, st)) {
  317. if ((m <<= 1) == 0x8000) {
  318. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  319. entropy->ct = -1; /* magnitude overflow */
  320. return TRUE;
  321. }
  322. st += 1;
  323. }
  324. }
  325. }
  326. v = m;
  327. /* Figure F.24: Decoding the magnitude bit pattern of v */
  328. st += 14;
  329. while (m >>= 1)
  330. if (arith_decode(cinfo, st)) v |= m;
  331. v += 1; if (sign) v = -v;
  332. /* Scale and output coefficient in natural (dezigzagged) order */
  333. (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
  334. }
  335. return TRUE;
  336. }
  337. /*
  338. * MCU decoding for DC successive approximation refinement scan.
  339. */
  340. METHODDEF(boolean)
  341. decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  342. {
  343. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  344. unsigned char *st;
  345. int p1, blkn;
  346. /* Process restart marker if needed */
  347. if (cinfo->restart_interval) {
  348. if (entropy->restarts_to_go == 0)
  349. process_restart(cinfo);
  350. entropy->restarts_to_go--;
  351. }
  352. st = entropy->fixed_bin; /* use fixed probability estimation */
  353. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  354. /* Outer loop handles each block in the MCU */
  355. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  356. /* Encoded data is simply the next bit of the two's-complement DC value */
  357. if (arith_decode(cinfo, st))
  358. MCU_data[blkn][0][0] |= p1;
  359. }
  360. return TRUE;
  361. }
  362. /*
  363. * MCU decoding for AC successive approximation refinement scan.
  364. */
  365. METHODDEF(boolean)
  366. decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  367. {
  368. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  369. JBLOCKROW block;
  370. JCOEFPTR thiscoef;
  371. unsigned char *st;
  372. int tbl, k, kex;
  373. int p1, m1;
  374. /* Process restart marker if needed */
  375. if (cinfo->restart_interval) {
  376. if (entropy->restarts_to_go == 0)
  377. process_restart(cinfo);
  378. entropy->restarts_to_go--;
  379. }
  380. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  381. /* There is always only one block per MCU */
  382. block = MCU_data[0];
  383. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  384. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  385. m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
  386. /* Establish EOBx (previous stage end-of-block) index */
  387. for (kex = cinfo->Se; kex > 0; kex--)
  388. if ((*block)[jpeg_natural_order[kex]]) break;
  389. for (k = cinfo->Ss; k <= cinfo->Se; k++) {
  390. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  391. if (k > kex)
  392. if (arith_decode(cinfo, st)) break; /* EOB flag */
  393. for (;;) {
  394. thiscoef = *block + jpeg_natural_order[k];
  395. if (*thiscoef) { /* previously nonzero coef */
  396. if (arith_decode(cinfo, st + 2)) {
  397. if (*thiscoef < 0)
  398. *thiscoef += m1;
  399. else
  400. *thiscoef += p1;
  401. }
  402. break;
  403. }
  404. if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
  405. if (arith_decode(cinfo, entropy->fixed_bin))
  406. *thiscoef = m1;
  407. else
  408. *thiscoef = p1;
  409. break;
  410. }
  411. st += 3; k++;
  412. if (k > cinfo->Se) {
  413. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  414. entropy->ct = -1; /* spectral overflow */
  415. return TRUE;
  416. }
  417. }
  418. }
  419. return TRUE;
  420. }
  421. /*
  422. * Decode one MCU's worth of arithmetic-compressed coefficients.
  423. */
  424. METHODDEF(boolean)
  425. decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  426. {
  427. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  428. jpeg_component_info * compptr;
  429. JBLOCKROW block;
  430. unsigned char *st;
  431. int blkn, ci, tbl, sign, k;
  432. int v, m;
  433. /* Process restart marker if needed */
  434. if (cinfo->restart_interval) {
  435. if (entropy->restarts_to_go == 0)
  436. process_restart(cinfo);
  437. entropy->restarts_to_go--;
  438. }
  439. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  440. /* Outer loop handles each block in the MCU */
  441. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  442. block = MCU_data[blkn];
  443. ci = cinfo->MCU_membership[blkn];
  444. compptr = cinfo->cur_comp_info[ci];
  445. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  446. tbl = compptr->dc_tbl_no;
  447. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  448. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  449. /* Figure F.19: Decode_DC_DIFF */
  450. if (arith_decode(cinfo, st) == 0)
  451. entropy->dc_context[ci] = 0;
  452. else {
  453. /* Figure F.21: Decoding nonzero value v */
  454. /* Figure F.22: Decoding the sign of v */
  455. sign = arith_decode(cinfo, st + 1);
  456. st += 2; st += sign;
  457. /* Figure F.23: Decoding the magnitude category of v */
  458. if ((m = arith_decode(cinfo, st)) != 0) {
  459. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  460. while (arith_decode(cinfo, st)) {
  461. if ((m <<= 1) == 0x8000) {
  462. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  463. entropy->ct = -1; /* magnitude overflow */
  464. return TRUE;
  465. }
  466. st += 1;
  467. }
  468. }
  469. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  470. if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
  471. entropy->dc_context[ci] = 0; /* zero diff category */
  472. else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
  473. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  474. else
  475. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  476. v = m;
  477. /* Figure F.24: Decoding the magnitude bit pattern of v */
  478. st += 14;
  479. while (m >>= 1)
  480. if (arith_decode(cinfo, st)) v |= m;
  481. v += 1; if (sign) v = -v;
  482. entropy->last_dc_val[ci] += v;
  483. }
  484. (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
  485. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  486. tbl = compptr->ac_tbl_no;
  487. /* Figure F.20: Decode_AC_coefficients */
  488. for (k = 1; k <= DCTSIZE2 - 1; k++) {
  489. st = entropy->ac_stats[tbl] + 3 * (k - 1);
  490. if (arith_decode(cinfo, st)) break; /* EOB flag */
  491. while (arith_decode(cinfo, st + 1) == 0) {
  492. st += 3; k++;
  493. if (k > DCTSIZE2 - 1) {
  494. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  495. entropy->ct = -1; /* spectral overflow */
  496. return TRUE;
  497. }
  498. }
  499. /* Figure F.21: Decoding nonzero value v */
  500. /* Figure F.22: Decoding the sign of v */
  501. sign = arith_decode(cinfo, entropy->fixed_bin);
  502. st += 2;
  503. /* Figure F.23: Decoding the magnitude category of v */
  504. if ((m = arith_decode(cinfo, st)) != 0) {
  505. if (arith_decode(cinfo, st)) {
  506. m <<= 1;
  507. st = entropy->ac_stats[tbl] +
  508. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  509. while (arith_decode(cinfo, st)) {
  510. if ((m <<= 1) == 0x8000) {
  511. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  512. entropy->ct = -1; /* magnitude overflow */
  513. return TRUE;
  514. }
  515. st += 1;
  516. }
  517. }
  518. }
  519. v = m;
  520. /* Figure F.24: Decoding the magnitude bit pattern of v */
  521. st += 14;
  522. while (m >>= 1)
  523. if (arith_decode(cinfo, st)) v |= m;
  524. v += 1; if (sign) v = -v;
  525. (*block)[jpeg_natural_order[k]] = (JCOEF) v;
  526. }
  527. }
  528. return TRUE;
  529. }
  530. /*
  531. * Initialize for an arithmetic-compressed scan.
  532. */
  533. METHODDEF(void)
  534. start_pass (j_decompress_ptr cinfo)
  535. {
  536. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  537. int ci, tbl;
  538. jpeg_component_info * compptr;
  539. if (cinfo->progressive_mode) {
  540. /* Validate progressive scan parameters */
  541. if (cinfo->Ss == 0) {
  542. if (cinfo->Se != 0)
  543. goto bad;
  544. } else {
  545. /* need not check Ss/Se < 0 since they came from unsigned bytes */
  546. if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1)
  547. goto bad;
  548. /* AC scans may have only one component */
  549. if (cinfo->comps_in_scan != 1)
  550. goto bad;
  551. }
  552. if (cinfo->Ah != 0) {
  553. /* Successive approximation refinement scan: must have Al = Ah-1. */
  554. if (cinfo->Ah-1 != cinfo->Al)
  555. goto bad;
  556. }
  557. if (cinfo->Al > 13) { /* need not check for < 0 */
  558. bad:
  559. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  560. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  561. }
  562. /* Update progression status, and verify that scan order is legal.
  563. * Note that inter-scan inconsistencies are treated as warnings
  564. * not fatal errors ... not clear if this is right way to behave.
  565. */
  566. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  567. int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
  568. int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
  569. if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  570. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  571. for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  572. int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  573. if (cinfo->Ah != expected)
  574. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  575. coef_bit_ptr[coefi] = cinfo->Al;
  576. }
  577. }
  578. /* Select MCU decoding routine */
  579. if (cinfo->Ah == 0) {
  580. if (cinfo->Ss == 0)
  581. entropy->pub.decode_mcu = decode_mcu_DC_first;
  582. else
  583. entropy->pub.decode_mcu = decode_mcu_AC_first;
  584. } else {
  585. if (cinfo->Ss == 0)
  586. entropy->pub.decode_mcu = decode_mcu_DC_refine;
  587. else
  588. entropy->pub.decode_mcu = decode_mcu_AC_refine;
  589. }
  590. } else {
  591. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  592. * This ought to be an error condition, but we make it a warning.
  593. */
  594. if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
  595. (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1))
  596. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  597. /* Select MCU decoding routine */
  598. entropy->pub.decode_mcu = decode_mcu;
  599. }
  600. /* Allocate & initialize requested statistics areas */
  601. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  602. compptr = cinfo->cur_comp_info[ci];
  603. if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  604. tbl = compptr->dc_tbl_no;
  605. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  606. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  607. if (entropy->dc_stats[tbl] == NULL)
  608. entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  609. ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
  610. MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
  611. /* Initialize DC predictions to 0 */
  612. entropy->last_dc_val[ci] = 0;
  613. entropy->dc_context[ci] = 0;
  614. }
  615. if (! cinfo->progressive_mode || cinfo->Ss) {
  616. tbl = compptr->ac_tbl_no;
  617. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  618. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  619. if (entropy->ac_stats[tbl] == NULL)
  620. entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  621. ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
  622. MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
  623. }
  624. }
  625. /* Initialize arithmetic decoding variables */
  626. entropy->c = 0;
  627. entropy->a = 0;
  628. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  629. /* Initialize restart counter */
  630. entropy->restarts_to_go = cinfo->restart_interval;
  631. }
  632. /*
  633. * Module initialization routine for arithmetic entropy decoding.
  634. */
  635. GLOBAL(void)
  636. jinit_arith_decoder (j_decompress_ptr cinfo)
  637. {
  638. arith_entropy_ptr entropy;
  639. int i;
  640. entropy = (arith_entropy_ptr)
  641. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  642. SIZEOF(arith_entropy_decoder));
  643. cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  644. entropy->pub.start_pass = start_pass;
  645. /* Mark tables unallocated */
  646. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  647. entropy->dc_stats[i] = NULL;
  648. entropy->ac_stats[i] = NULL;
  649. }
  650. /* Initialize index for fixed probability estimation */
  651. entropy->fixed_bin[0] = 113;
  652. if (cinfo->progressive_mode) {
  653. /* Create progression status table */
  654. int *coef_bit_ptr, ci;
  655. cinfo->coef_bits = (int (*)[DCTSIZE2])
  656. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  657. cinfo->num_components*DCTSIZE2*SIZEOF(int));
  658. coef_bit_ptr = & cinfo->coef_bits[0][0];
  659. for (ci = 0; ci < cinfo->num_components; ci++)
  660. for (i = 0; i < DCTSIZE2; i++)
  661. *coef_bit_ptr++ = -1;
  662. }
  663. }