jdarith.c 24 KB

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