Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

772 lines
23 KiB

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