jdarith.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * jdarith.c
  3. *
  4. * Developed 1997-2015 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 do 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. k = cinfo->Ss - 1;
  299. do {
  300. st = entropy->ac_stats[tbl] + 3 * k;
  301. if (arith_decode(cinfo, st)) break; /* EOB flag */
  302. for (;;) {
  303. k++;
  304. if (arith_decode(cinfo, st + 1)) break;
  305. st += 3;
  306. if (k >= cinfo->Se) {
  307. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  308. entropy->ct = -1; /* spectral overflow */
  309. return TRUE;
  310. }
  311. }
  312. /* Figure F.21: Decoding nonzero value v */
  313. /* Figure F.22: Decoding the sign of v */
  314. sign = arith_decode(cinfo, entropy->fixed_bin);
  315. st += 2;
  316. /* Figure F.23: Decoding the magnitude category of v */
  317. if ((m = arith_decode(cinfo, st)) != 0) {
  318. if (arith_decode(cinfo, st)) {
  319. m <<= 1;
  320. st = entropy->ac_stats[tbl] +
  321. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  322. while (arith_decode(cinfo, st)) {
  323. if ((m <<= 1) == 0x8000) {
  324. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  325. entropy->ct = -1; /* magnitude overflow */
  326. return TRUE;
  327. }
  328. st += 1;
  329. }
  330. }
  331. }
  332. v = m;
  333. /* Figure F.24: Decoding the magnitude bit pattern of v */
  334. st += 14;
  335. while (m >>= 1)
  336. if (arith_decode(cinfo, st)) v |= m;
  337. v += 1; if (sign) v = -v;
  338. /* Scale and output coefficient in natural (dezigzagged) order */
  339. (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
  340. } while (k < cinfo->Se);
  341. return TRUE;
  342. }
  343. /*
  344. * MCU decoding for DC successive approximation refinement scan.
  345. * Note: we assume such scans can be multi-component,
  346. * although the spec is not very clear on the point.
  347. */
  348. METHODDEF(boolean)
  349. decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  350. {
  351. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  352. unsigned char *st;
  353. int p1, blkn;
  354. /* Process restart marker if needed */
  355. if (cinfo->restart_interval) {
  356. if (entropy->restarts_to_go == 0)
  357. process_restart(cinfo);
  358. entropy->restarts_to_go--;
  359. }
  360. st = entropy->fixed_bin; /* use fixed probability estimation */
  361. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  362. /* Outer loop handles each block in the MCU */
  363. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  364. /* Encoded data is simply the next bit of the two's-complement DC value */
  365. if (arith_decode(cinfo, st))
  366. MCU_data[blkn][0][0] |= p1;
  367. }
  368. return TRUE;
  369. }
  370. /*
  371. * MCU decoding for AC successive approximation refinement scan.
  372. */
  373. METHODDEF(boolean)
  374. decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  375. {
  376. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  377. JBLOCKROW block;
  378. JCOEFPTR thiscoef;
  379. unsigned char *st;
  380. int tbl, k, kex;
  381. int p1, m1;
  382. const int * natural_order;
  383. /* Process restart marker if needed */
  384. if (cinfo->restart_interval) {
  385. if (entropy->restarts_to_go == 0)
  386. process_restart(cinfo);
  387. entropy->restarts_to_go--;
  388. }
  389. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  390. natural_order = cinfo->natural_order;
  391. /* There is always only one block per MCU */
  392. block = MCU_data[0];
  393. tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
  394. p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  395. m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
  396. /* Establish EOBx (previous stage end-of-block) index */
  397. kex = cinfo->Se;
  398. do {
  399. if ((*block)[natural_order[kex]]) break;
  400. } while (--kex);
  401. k = cinfo->Ss - 1;
  402. do {
  403. st = entropy->ac_stats[tbl] + 3 * k;
  404. if (k >= kex)
  405. if (arith_decode(cinfo, st)) break; /* EOB flag */
  406. for (;;) {
  407. thiscoef = *block + natural_order[++k];
  408. if (*thiscoef) { /* previously nonzero coef */
  409. if (arith_decode(cinfo, st + 2)) {
  410. if (*thiscoef < 0)
  411. *thiscoef += m1;
  412. else
  413. *thiscoef += p1;
  414. }
  415. break;
  416. }
  417. if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
  418. if (arith_decode(cinfo, entropy->fixed_bin))
  419. *thiscoef = m1;
  420. else
  421. *thiscoef = p1;
  422. break;
  423. }
  424. st += 3;
  425. if (k >= cinfo->Se) {
  426. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  427. entropy->ct = -1; /* spectral overflow */
  428. return TRUE;
  429. }
  430. }
  431. } while (k < cinfo->Se);
  432. return TRUE;
  433. }
  434. /*
  435. * Decode one MCU's worth of arithmetic-compressed coefficients.
  436. */
  437. METHODDEF(boolean)
  438. decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  439. {
  440. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  441. jpeg_component_info * compptr;
  442. JBLOCKROW block;
  443. unsigned char *st;
  444. int blkn, ci, tbl, sign, k;
  445. int v, m;
  446. const int * natural_order;
  447. /* Process restart marker if needed */
  448. if (cinfo->restart_interval) {
  449. if (entropy->restarts_to_go == 0)
  450. process_restart(cinfo);
  451. entropy->restarts_to_go--;
  452. }
  453. if (entropy->ct == -1) return TRUE; /* if error do nothing */
  454. natural_order = cinfo->natural_order;
  455. /* Outer loop handles each block in the MCU */
  456. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  457. block = MCU_data[blkn];
  458. ci = cinfo->MCU_membership[blkn];
  459. compptr = cinfo->cur_comp_info[ci];
  460. /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
  461. tbl = compptr->dc_tbl_no;
  462. /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
  463. st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
  464. /* Figure F.19: Decode_DC_DIFF */
  465. if (arith_decode(cinfo, st) == 0)
  466. entropy->dc_context[ci] = 0;
  467. else {
  468. /* Figure F.21: Decoding nonzero value v */
  469. /* Figure F.22: Decoding the sign of v */
  470. sign = arith_decode(cinfo, st + 1);
  471. st += 2; st += sign;
  472. /* Figure F.23: Decoding the magnitude category of v */
  473. if ((m = arith_decode(cinfo, st)) != 0) {
  474. st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
  475. while (arith_decode(cinfo, st)) {
  476. if ((m <<= 1) == 0x8000) {
  477. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  478. entropy->ct = -1; /* magnitude overflow */
  479. return TRUE;
  480. }
  481. st += 1;
  482. }
  483. }
  484. /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
  485. if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
  486. entropy->dc_context[ci] = 0; /* zero diff category */
  487. else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
  488. entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
  489. else
  490. entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
  491. v = m;
  492. /* Figure F.24: Decoding the magnitude bit pattern of v */
  493. st += 14;
  494. while (m >>= 1)
  495. if (arith_decode(cinfo, st)) v |= m;
  496. v += 1; if (sign) v = -v;
  497. entropy->last_dc_val[ci] += v;
  498. }
  499. (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
  500. /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
  501. if (cinfo->lim_Se == 0) continue;
  502. tbl = compptr->ac_tbl_no;
  503. k = 0;
  504. /* Figure F.20: Decode_AC_coefficients */
  505. do {
  506. st = entropy->ac_stats[tbl] + 3 * k;
  507. if (arith_decode(cinfo, st)) break; /* EOB flag */
  508. for (;;) {
  509. k++;
  510. if (arith_decode(cinfo, st + 1)) break;
  511. st += 3;
  512. if (k >= cinfo->lim_Se) {
  513. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  514. entropy->ct = -1; /* spectral overflow */
  515. return TRUE;
  516. }
  517. }
  518. /* Figure F.21: Decoding nonzero value v */
  519. /* Figure F.22: Decoding the sign of v */
  520. sign = arith_decode(cinfo, entropy->fixed_bin);
  521. st += 2;
  522. /* Figure F.23: Decoding the magnitude category of v */
  523. if ((m = arith_decode(cinfo, st)) != 0) {
  524. if (arith_decode(cinfo, st)) {
  525. m <<= 1;
  526. st = entropy->ac_stats[tbl] +
  527. (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
  528. while (arith_decode(cinfo, st)) {
  529. if ((m <<= 1) == 0x8000) {
  530. WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
  531. entropy->ct = -1; /* magnitude overflow */
  532. return TRUE;
  533. }
  534. st += 1;
  535. }
  536. }
  537. }
  538. v = m;
  539. /* Figure F.24: Decoding the magnitude bit pattern of v */
  540. st += 14;
  541. while (m >>= 1)
  542. if (arith_decode(cinfo, st)) v |= m;
  543. v += 1; if (sign) v = -v;
  544. (*block)[natural_order[k]] = (JCOEF) v;
  545. } while (k < cinfo->lim_Se);
  546. }
  547. return TRUE;
  548. }
  549. /*
  550. * Initialize for an arithmetic-compressed scan.
  551. */
  552. METHODDEF(void)
  553. start_pass (j_decompress_ptr cinfo)
  554. {
  555. arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
  556. int ci, tbl;
  557. jpeg_component_info * compptr;
  558. if (cinfo->progressive_mode) {
  559. /* Validate progressive scan parameters */
  560. if (cinfo->Ss == 0) {
  561. if (cinfo->Se != 0)
  562. goto bad;
  563. } else {
  564. /* need not check Ss/Se < 0 since they came from unsigned bytes */
  565. if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
  566. goto bad;
  567. /* AC scans may have only one component */
  568. if (cinfo->comps_in_scan != 1)
  569. goto bad;
  570. }
  571. if (cinfo->Ah != 0) {
  572. /* Successive approximation refinement scan: must have Al = Ah-1. */
  573. if (cinfo->Ah-1 != cinfo->Al)
  574. goto bad;
  575. }
  576. if (cinfo->Al > 13) { /* need not check for < 0 */
  577. bad:
  578. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  579. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  580. }
  581. /* Update progression status, and verify that scan order is legal.
  582. * Note that inter-scan inconsistencies are treated as warnings
  583. * not fatal errors ... not clear if this is right way to behave.
  584. */
  585. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  586. int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
  587. int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
  588. if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  589. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  590. for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  591. int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  592. if (cinfo->Ah != expected)
  593. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  594. coef_bit_ptr[coefi] = cinfo->Al;
  595. }
  596. }
  597. /* Select MCU decoding routine */
  598. if (cinfo->Ah == 0) {
  599. if (cinfo->Ss == 0)
  600. entropy->pub.decode_mcu = decode_mcu_DC_first;
  601. else
  602. entropy->pub.decode_mcu = decode_mcu_AC_first;
  603. } else {
  604. if (cinfo->Ss == 0)
  605. entropy->pub.decode_mcu = decode_mcu_DC_refine;
  606. else
  607. entropy->pub.decode_mcu = decode_mcu_AC_refine;
  608. }
  609. } else {
  610. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  611. * This ought to be an error condition, but we make it a warning.
  612. */
  613. if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
  614. (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
  615. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  616. /* Select MCU decoding routine */
  617. entropy->pub.decode_mcu = decode_mcu;
  618. }
  619. /* Allocate & initialize requested statistics areas */
  620. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  621. compptr = cinfo->cur_comp_info[ci];
  622. if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
  623. tbl = compptr->dc_tbl_no;
  624. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  625. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  626. if (entropy->dc_stats[tbl] == NULL)
  627. entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  628. ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
  629. MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
  630. /* Initialize DC predictions to 0 */
  631. entropy->last_dc_val[ci] = 0;
  632. entropy->dc_context[ci] = 0;
  633. }
  634. if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
  635. (cinfo->progressive_mode && cinfo->Ss)) {
  636. tbl = compptr->ac_tbl_no;
  637. if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
  638. ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
  639. if (entropy->ac_stats[tbl] == NULL)
  640. entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
  641. ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
  642. MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
  643. }
  644. }
  645. /* Initialize arithmetic decoding variables */
  646. entropy->c = 0;
  647. entropy->a = 0;
  648. entropy->ct = -16; /* force reading 2 initial bytes to fill C */
  649. /* Initialize restart counter */
  650. entropy->restarts_to_go = cinfo->restart_interval;
  651. }
  652. /*
  653. * Finish up at the end of an arithmetic-compressed scan.
  654. */
  655. METHODDEF(void)
  656. finish_pass (j_decompress_ptr cinfo)
  657. {
  658. /* no work necessary here */
  659. }
  660. /*
  661. * Module initialization routine for arithmetic entropy decoding.
  662. */
  663. GLOBAL(void)
  664. jinit_arith_decoder (j_decompress_ptr cinfo)
  665. {
  666. arith_entropy_ptr entropy;
  667. int i;
  668. entropy = (arith_entropy_ptr)
  669. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  670. SIZEOF(arith_entropy_decoder));
  671. cinfo->entropy = &entropy->pub;
  672. entropy->pub.start_pass = start_pass;
  673. entropy->pub.finish_pass = finish_pass;
  674. /* Mark tables unallocated */
  675. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  676. entropy->dc_stats[i] = NULL;
  677. entropy->ac_stats[i] = NULL;
  678. }
  679. /* Initialize index for fixed probability estimation */
  680. entropy->fixed_bin[0] = 113;
  681. if (cinfo->progressive_mode) {
  682. /* Create progression status table */
  683. int *coef_bit_ptr, ci;
  684. cinfo->coef_bits = (int (*)[DCTSIZE2])
  685. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  686. cinfo->num_components*DCTSIZE2*SIZEOF(int));
  687. coef_bit_ptr = & cinfo->coef_bits[0][0];
  688. for (ci = 0; ci < cinfo->num_components; ci++)
  689. for (i = 0; i < DCTSIZE2; i++)
  690. *coef_bit_ptr++ = -1;
  691. }
  692. }