jdarith.c 24 KB

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