jdarith.c 24 KB

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