jpc_t1dec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2003 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Tier 1 Decoder
  64. *
  65. * $Id: jpc_t1dec.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <assert.h>
  73. #include "jasper/jas_fix.h"
  74. #include "jasper/jas_stream.h"
  75. #include "jasper/jas_math.h"
  76. #include "jpc_bs.h"
  77. #include "jpc_mqdec.h"
  78. #include "jpc_t1dec.h"
  79. #include "jpc_t1cod.h"
  80. #include "jpc_dec.h"
  81. /******************************************************************************\
  82. *
  83. \******************************************************************************/
  84. static int jpc_dec_decodecblk(jpc_dec_t *dec, jpc_dec_tile_t *tile, jpc_dec_tcomp_t *tcomp, jpc_dec_band_t *band,
  85. jpc_dec_cblk_t *cblk, int dopartial, int maxlyrs);
  86. static int dec_sigpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int orient,
  87. int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data);
  88. static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos,
  89. int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data);
  90. static int dec_refpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int vcausalflag,
  91. jas_matrix_t *flags, jas_matrix_t *data);
  92. static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos,
  93. int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data);
  94. static int dec_clnpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int orient,
  95. int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data);
  96. #if defined(DEBUG)
  97. static long t1dec_cnt = 0;
  98. #endif
  99. #if !defined(DEBUG)
  100. #define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \
  101. ((v) = jpc_mqdec_getbit(mqdec))
  102. #else
  103. #define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \
  104. { \
  105. (v) = jpc_mqdec_getbit(mqdec); \
  106. if (jas_getdbglevel() >= 100) { \
  107. jas_eprintf("index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \
  108. ++t1dec_cnt; \
  109. } \
  110. }
  111. #endif
  112. #define JPC_T1D_GETBITNOSKEW(mqdec, v, passtypename, symtypename) \
  113. JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename)
  114. #if !defined(DEBUG)
  115. #define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \
  116. ((v) = jpc_bitstream_getbit(bitstream))
  117. #else
  118. #define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \
  119. { \
  120. (v) = jpc_bitstream_getbit(bitstream); \
  121. if (jas_getdbglevel() >= 100) { \
  122. jas_eprintf("index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \
  123. ++t1dec_cnt; \
  124. } \
  125. }
  126. #endif
  127. /******************************************************************************\
  128. * Code.
  129. \******************************************************************************/
  130. int jpc_dec_decodecblks(jpc_dec_t *dec, jpc_dec_tile_t *tile)
  131. {
  132. jpc_dec_tcomp_t *tcomp;
  133. int compcnt;
  134. jpc_dec_rlvl_t *rlvl;
  135. int rlvlcnt;
  136. jpc_dec_band_t *band;
  137. int bandcnt;
  138. jpc_dec_prc_t *prc;
  139. int prccnt;
  140. jpc_dec_cblk_t *cblk;
  141. int cblkcnt;
  142. for (compcnt = dec->numcomps, tcomp = tile->tcomps; compcnt > 0;
  143. --compcnt, ++tcomp) {
  144. for (rlvlcnt = tcomp->numrlvls, rlvl = tcomp->rlvls;
  145. rlvlcnt > 0; --rlvlcnt, ++rlvl) {
  146. if (!rlvl->bands) {
  147. continue;
  148. }
  149. for (bandcnt = rlvl->numbands, band = rlvl->bands;
  150. bandcnt > 0; --bandcnt, ++band) {
  151. if (!band->data) {
  152. continue;
  153. }
  154. for (prccnt = rlvl->numprcs, prc = band->prcs;
  155. prccnt > 0; --prccnt, ++prc) {
  156. if (!prc->cblks) {
  157. continue;
  158. }
  159. for (cblkcnt = prc->numcblks,
  160. cblk = prc->cblks; cblkcnt > 0;
  161. --cblkcnt, ++cblk) {
  162. if (jpc_dec_decodecblk(dec, tile, tcomp,
  163. band, cblk, 1, JPC_MAXLYRS)) {
  164. return -1;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. return 0;
  172. }
  173. static int jpc_dec_decodecblk(jpc_dec_t *dec, jpc_dec_tile_t *tile, jpc_dec_tcomp_t *tcomp, jpc_dec_band_t *band,
  174. jpc_dec_cblk_t *cblk, int dopartial, int maxlyrs)
  175. {
  176. jpc_dec_seg_t *seg;
  177. int i;
  178. int bpno;
  179. int passtype;
  180. int ret;
  181. int compno;
  182. int filldata;
  183. int fillmask;
  184. jpc_dec_ccp_t *ccp;
  185. compno = tcomp - tile->tcomps;
  186. if (!cblk->flags) {
  187. /* Note: matrix is assumed to be zeroed */
  188. if (!(cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) +
  189. 2, jas_matrix_numcols(cblk->data) + 2))) {
  190. return -1;
  191. }
  192. }
  193. seg = cblk->segs.head;
  194. while (seg && (seg != cblk->curseg || dopartial) && (maxlyrs < 0 ||
  195. seg->lyrno < maxlyrs)) {
  196. assert(seg->numpasses >= seg->maxpasses || dopartial);
  197. assert(seg->stream);
  198. jas_stream_rewind(seg->stream);
  199. jas_stream_setrwcount(seg->stream, 0);
  200. if (seg->type == JPC_SEG_MQ) {
  201. if (!cblk->mqdec) {
  202. if (!(cblk->mqdec = jpc_mqdec_create(JPC_NUMCTXS, 0))) {
  203. return -1;
  204. }
  205. jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs);
  206. }
  207. jpc_mqdec_setinput(cblk->mqdec, seg->stream);
  208. jpc_mqdec_init(cblk->mqdec);
  209. } else {
  210. assert(seg->type == JPC_SEG_RAW);
  211. if (!cblk->nulldec) {
  212. if (!(cblk->nulldec = jpc_bitstream_sopen(seg->stream, "r"))) {
  213. assert(0);
  214. }
  215. }
  216. }
  217. for (i = 0; i < seg->numpasses; ++i) {
  218. if (cblk->numimsbs > band->numbps) {
  219. ccp = &tile->cp->ccps[compno];
  220. if (ccp->roishift <= 0) {
  221. jas_eprintf("warning: corrupt code stream\n");
  222. } else {
  223. if (cblk->numimsbs < ccp->roishift - band->numbps) {
  224. jas_eprintf("warning: corrupt code stream\n");
  225. }
  226. }
  227. }
  228. bpno = band->roishift + band->numbps - 1 - (cblk->numimsbs +
  229. (seg->passno + i - cblk->firstpassno + 2) / 3);
  230. if (bpno < 0) {
  231. goto premature_exit;
  232. }
  233. #if 1
  234. passtype = (seg->passno + i + 2) % 3;
  235. #else
  236. passtype = JPC_PASSTYPE(seg->passno + i + 2);
  237. #endif
  238. assert(bpno >= 0 && bpno < 31);
  239. switch (passtype) {
  240. case JPC_SIGPASS:
  241. ret = (seg->type == JPC_SEG_MQ) ? dec_sigpass(dec,
  242. cblk->mqdec, bpno, band->orient,
  243. (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
  244. cblk->flags, cblk->data) :
  245. dec_rawsigpass(dec, cblk->nulldec, bpno,
  246. (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
  247. cblk->flags, cblk->data);
  248. break;
  249. case JPC_REFPASS:
  250. ret = (seg->type == JPC_SEG_MQ) ?
  251. dec_refpass(dec, cblk->mqdec, bpno,
  252. (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
  253. cblk->flags, cblk->data) :
  254. dec_rawrefpass(dec, cblk->nulldec, bpno,
  255. (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
  256. cblk->flags, cblk->data);
  257. break;
  258. case JPC_CLNPASS:
  259. assert(seg->type == JPC_SEG_MQ);
  260. ret = dec_clnpass(dec, cblk->mqdec, bpno,
  261. band->orient, (tile->cp->ccps[compno].cblkctx &
  262. JPC_COX_VSC) != 0, (tile->cp->ccps[compno].cblkctx &
  263. JPC_COX_SEGSYM) != 0, cblk->flags,
  264. cblk->data);
  265. break;
  266. default:
  267. ret = -1;
  268. break;
  269. }
  270. /* Do we need to reset after each coding pass? */
  271. if (tile->cp->ccps[compno].cblkctx & JPC_COX_RESET) {
  272. jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs);
  273. }
  274. if (ret) {
  275. jas_eprintf("coding pass failed passtype=%d segtype=%d\n", passtype, seg->type);
  276. return -1;
  277. }
  278. }
  279. if (seg->type == JPC_SEG_MQ) {
  280. /* Note: dont destroy mq decoder because context info will be lost */
  281. } else {
  282. assert(seg->type == JPC_SEG_RAW);
  283. if (tile->cp->ccps[compno].cblkctx & JPC_COX_PTERM) {
  284. fillmask = 0x7f;
  285. filldata = 0x2a;
  286. } else {
  287. fillmask = 0;
  288. filldata = 0;
  289. }
  290. if ((ret = jpc_bitstream_inalign(cblk->nulldec, fillmask,
  291. filldata)) < 0) {
  292. return -1;
  293. } else if (ret > 0) {
  294. jas_eprintf("warning: bad termination pattern detected\n");
  295. }
  296. jpc_bitstream_close(cblk->nulldec);
  297. cblk->nulldec = 0;
  298. }
  299. cblk->curseg = seg->next;
  300. jpc_seglist_remove(&cblk->segs, seg);
  301. jpc_seg_destroy(seg);
  302. seg = cblk->curseg;
  303. }
  304. assert(dopartial ? (!cblk->curseg) : 1);
  305. premature_exit:
  306. return 0;
  307. }
  308. /******************************************************************************\
  309. * Code for significance pass.
  310. \******************************************************************************/
  311. #define jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, orient, mqdec, vcausalflag) \
  312. { \
  313. int f; \
  314. int v; \
  315. f = *(fp); \
  316. if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
  317. jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO(f, (orient))); \
  318. JPC_T1D_GETBIT((mqdec), v, "SIG", "ZC"); \
  319. if (v) { \
  320. jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \
  321. JPC_T1D_GETBIT((mqdec), v, "SIG", "SC"); \
  322. v ^= JPC_GETSPB(f); \
  323. JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
  324. *(fp) |= JPC_SIG; \
  325. *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \
  326. } \
  327. *(fp) |= JPC_VISIT; \
  328. } \
  329. }
  330. static int dec_sigpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient,
  331. int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data)
  332. {
  333. int i;
  334. int j;
  335. int one;
  336. int half;
  337. int oneplushalf;
  338. int vscanlen;
  339. int width;
  340. int height;
  341. jpc_fix_t *fp;
  342. int frowstep;
  343. int fstripestep;
  344. jpc_fix_t *fstripestart;
  345. jpc_fix_t *fvscanstart;
  346. jpc_fix_t *dp;
  347. int drowstep;
  348. int dstripestep;
  349. jpc_fix_t *dstripestart;
  350. jpc_fix_t *dvscanstart;
  351. int k;
  352. /* Avoid compiler warning about unused parameters. */
  353. dec = 0;
  354. width = jas_matrix_numcols(data);
  355. height = jas_matrix_numrows(data);
  356. frowstep = jas_matrix_rowstep(flags);
  357. drowstep = jas_matrix_rowstep(data);
  358. fstripestep = frowstep << 2;
  359. dstripestep = drowstep << 2;
  360. one = 1 << bitpos;
  361. half = one >> 1;
  362. oneplushalf = one | half;
  363. fstripestart = jas_matrix_getref(flags, 1, 1);
  364. dstripestart = jas_matrix_getref(data, 0, 0);
  365. for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
  366. dstripestart += dstripestep) {
  367. fvscanstart = fstripestart;
  368. dvscanstart = dstripestart;
  369. vscanlen = JAS_MIN(i, 4);
  370. for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
  371. fp = fvscanstart;
  372. dp = dvscanstart;
  373. k = vscanlen;
  374. /* Process first sample in vertical scan. */
  375. jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
  376. orient, mqdec, vcausalflag);
  377. if (--k <= 0) {
  378. continue;
  379. }
  380. fp += frowstep;
  381. dp += drowstep;
  382. /* Process second sample in vertical scan. */
  383. jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
  384. orient, mqdec, 0);
  385. if (--k <= 0) {
  386. continue;
  387. }
  388. fp += frowstep;
  389. dp += drowstep;
  390. /* Process third sample in vertical scan. */
  391. jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
  392. orient, mqdec, 0);
  393. if (--k <= 0) {
  394. continue;
  395. }
  396. fp += frowstep;
  397. dp += drowstep;
  398. /* Process fourth sample in vertical scan. */
  399. jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
  400. orient, mqdec, 0);
  401. }
  402. }
  403. return 0;
  404. }
  405. #define jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, vcausalflag) \
  406. { \
  407. jpc_fix_t f = *(fp); \
  408. jpc_fix_t v; \
  409. if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
  410. JPC_T1D_RAWGETBIT(in, v, "SIG", "ZC"); \
  411. if (v < 0) { \
  412. return -1; \
  413. } \
  414. if (v) { \
  415. JPC_T1D_RAWGETBIT(in, v, "SIG", "SC"); \
  416. if (v < 0) { \
  417. return -1; \
  418. } \
  419. JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
  420. *(fp) |= JPC_SIG; \
  421. *(dp) = v ? (-oneplushalf) : (oneplushalf); \
  422. } \
  423. *(fp) |= JPC_VISIT; \
  424. } \
  425. }
  426. static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag,
  427. jas_matrix_t *flags, jas_matrix_t *data)
  428. {
  429. int i;
  430. int j;
  431. int k;
  432. int one;
  433. int half;
  434. int oneplushalf;
  435. int vscanlen;
  436. int width;
  437. int height;
  438. jpc_fix_t *fp;
  439. int frowstep;
  440. int fstripestep;
  441. jpc_fix_t *fstripestart;
  442. jpc_fix_t *fvscanstart;
  443. jpc_fix_t *dp;
  444. int drowstep;
  445. int dstripestep;
  446. jpc_fix_t *dstripestart;
  447. jpc_fix_t *dvscanstart;
  448. /* Avoid compiler warning about unused parameters. */
  449. dec = 0;
  450. width = jas_matrix_numcols(data);
  451. height = jas_matrix_numrows(data);
  452. frowstep = jas_matrix_rowstep(flags);
  453. drowstep = jas_matrix_rowstep(data);
  454. fstripestep = frowstep << 2;
  455. dstripestep = drowstep << 2;
  456. one = 1 << bitpos;
  457. half = one >> 1;
  458. oneplushalf = one | half;
  459. fstripestart = jas_matrix_getref(flags, 1, 1);
  460. dstripestart = jas_matrix_getref(data, 0, 0);
  461. for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
  462. dstripestart += dstripestep) {
  463. fvscanstart = fstripestart;
  464. dvscanstart = dstripestart;
  465. vscanlen = JAS_MIN(i, 4);
  466. for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
  467. fp = fvscanstart;
  468. dp = dvscanstart;
  469. k = vscanlen;
  470. /* Process first sample in vertical scan. */
  471. jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
  472. in, vcausalflag);
  473. if (--k <= 0) {
  474. continue;
  475. }
  476. fp += frowstep;
  477. dp += drowstep;
  478. /* Process second sample in vertical scan. */
  479. jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
  480. in, 0);
  481. if (--k <= 0) {
  482. continue;
  483. }
  484. fp += frowstep;
  485. dp += drowstep;
  486. /* Process third sample in vertical scan. */
  487. jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
  488. in, 0);
  489. if (--k <= 0) {
  490. continue;
  491. }
  492. fp += frowstep;
  493. dp += drowstep;
  494. /* Process fourth sample in vertical scan. */
  495. jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
  496. in, 0);
  497. }
  498. }
  499. return 0;
  500. }
  501. /******************************************************************************\
  502. * Code for refinement pass.
  503. \******************************************************************************/
  504. #define jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, vcausalflag) \
  505. { \
  506. int v; \
  507. int t; \
  508. if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
  509. jpc_mqdec_setcurctx((mqdec), JPC_GETMAGCTXNO(*(fp))); \
  510. JPC_T1D_GETBITNOSKEW((mqdec), v, "REF", "MR"); \
  511. t = (v ? (poshalf) : (neghalf)); \
  512. *(dp) += (*(dp) < 0) ? (-t) : t; \
  513. *(fp) |= JPC_REFINE; \
  514. } \
  515. }
  516. static int dec_refpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos,
  517. int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data)
  518. {
  519. int i;
  520. int j;
  521. int vscanlen;
  522. int width;
  523. int height;
  524. int one;
  525. int poshalf;
  526. int neghalf;
  527. jpc_fix_t *fp;
  528. int frowstep;
  529. int fstripestep;
  530. jpc_fix_t *fstripestart;
  531. jpc_fix_t *fvscanstart;
  532. jpc_fix_t *dp;
  533. int drowstep;
  534. int dstripestep;
  535. jpc_fix_t *dstripestart;
  536. jpc_fix_t *dvscanstart;
  537. int k;
  538. /* Avoid compiler warning about unused parameters. */
  539. dec = 0;
  540. vcausalflag = 0;
  541. width = jas_matrix_numcols(data);
  542. height = jas_matrix_numrows(data);
  543. frowstep = jas_matrix_rowstep(flags);
  544. drowstep = jas_matrix_rowstep(data);
  545. fstripestep = frowstep << 2;
  546. dstripestep = drowstep << 2;
  547. one = 1 << bitpos;
  548. poshalf = one >> 1;
  549. neghalf = (bitpos > 0) ? (-poshalf) : (-1);
  550. fstripestart = jas_matrix_getref(flags, 1, 1);
  551. dstripestart = jas_matrix_getref(data, 0, 0);
  552. for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
  553. dstripestart += dstripestep) {
  554. fvscanstart = fstripestart;
  555. dvscanstart = dstripestart;
  556. vscanlen = JAS_MIN(i, 4);
  557. for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
  558. fp = fvscanstart;
  559. dp = dvscanstart;
  560. k = vscanlen;
  561. /* Process first sample in vertical scan. */
  562. jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec,
  563. vcausalflag);
  564. if (--k <= 0) {
  565. continue;
  566. }
  567. fp += frowstep;
  568. dp += drowstep;
  569. /* Process second sample in vertical scan. */
  570. jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
  571. if (--k <= 0) {
  572. continue;
  573. }
  574. fp += frowstep;
  575. dp += drowstep;
  576. /* Process third sample in vertical scan. */
  577. jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
  578. if (--k <= 0) {
  579. continue;
  580. }
  581. fp += frowstep;
  582. dp += drowstep;
  583. /* Process fourth sample in vertical scan. */
  584. jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
  585. }
  586. }
  587. return 0;
  588. }
  589. #define jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, vcausalflag) \
  590. { \
  591. jpc_fix_t v; \
  592. jpc_fix_t t; \
  593. if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
  594. JPC_T1D_RAWGETBIT(in, v, "REF", "MAGREF"); \
  595. if (v < 0) { \
  596. return -1; \
  597. } \
  598. t = (v ? poshalf : neghalf); \
  599. *(dp) += (*(dp) < 0) ? (-t) : t; \
  600. *(fp) |= JPC_REFINE; \
  601. } \
  602. }
  603. static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag,
  604. jas_matrix_t *flags, jas_matrix_t *data)
  605. {
  606. int i;
  607. int j;
  608. int k;
  609. int vscanlen;
  610. int width;
  611. int height;
  612. int one;
  613. int poshalf;
  614. int neghalf;
  615. jpc_fix_t *fp;
  616. int frowstep;
  617. int fstripestep;
  618. jpc_fix_t *fstripestart;
  619. jpc_fix_t *fvscanstart;
  620. jpc_fix_t *dp;
  621. int drowstep;
  622. int dstripestep;
  623. jpc_fix_t *dstripestart;
  624. jpc_fix_t *dvscanstart;
  625. /* Avoid compiler warning about unused parameters. */
  626. dec = 0;
  627. vcausalflag = 0;
  628. width = jas_matrix_numcols(data);
  629. height = jas_matrix_numrows(data);
  630. frowstep = jas_matrix_rowstep(flags);
  631. drowstep = jas_matrix_rowstep(data);
  632. fstripestep = frowstep << 2;
  633. dstripestep = drowstep << 2;
  634. one = 1 << bitpos;
  635. poshalf = one >> 1;
  636. neghalf = (bitpos > 0) ? (-poshalf) : (-1);
  637. fstripestart = jas_matrix_getref(flags, 1, 1);
  638. dstripestart = jas_matrix_getref(data, 0, 0);
  639. for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
  640. dstripestart += dstripestep) {
  641. fvscanstart = fstripestart;
  642. dvscanstart = dstripestart;
  643. vscanlen = JAS_MIN(i, 4);
  644. for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
  645. fp = fvscanstart;
  646. dp = dvscanstart;
  647. k = vscanlen;
  648. /* Process first sample in vertical scan. */
  649. jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in,
  650. vcausalflag);
  651. if (--k <= 0) {
  652. continue;
  653. }
  654. fp += frowstep;
  655. dp += drowstep;
  656. /* Process second sample in vertical scan. */
  657. jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
  658. if (--k <= 0) {
  659. continue;
  660. }
  661. fp += frowstep;
  662. dp += drowstep;
  663. /* Process third sample in vertical scan. */
  664. jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
  665. if (--k <= 0) {
  666. continue;
  667. }
  668. fp += frowstep;
  669. dp += drowstep;
  670. /* Process fourth sample in vertical scan. */
  671. jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
  672. }
  673. }
  674. return 0;
  675. }
  676. /******************************************************************************\
  677. * Code for cleanup pass.
  678. \******************************************************************************/
  679. #define jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, flabel, plabel, vcausalflag) \
  680. { \
  681. int v; \
  682. flabel \
  683. if (!((f) & (JPC_SIG | JPC_VISIT))) { \
  684. jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO((f), (orient))); \
  685. JPC_T1D_GETBIT((mqdec), v, "CLN", "ZC"); \
  686. if (v) { \
  687. plabel \
  688. /* Coefficient is significant. */ \
  689. jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \
  690. JPC_T1D_GETBIT((mqdec), v, "CLN", "SC"); \
  691. v ^= JPC_GETSPB(f); \
  692. *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \
  693. JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
  694. *(fp) |= JPC_SIG; \
  695. } \
  696. } \
  697. /* XXX - Is this correct? Can aggregation cause some VISIT bits not to be reset? Check. */ \
  698. *(fp) &= ~JPC_VISIT; \
  699. }
  700. static int dec_clnpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient,
  701. int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data)
  702. {
  703. int i;
  704. int j;
  705. int k;
  706. int vscanlen;
  707. int v;
  708. int half;
  709. int runlen;
  710. int f;
  711. int width;
  712. int height;
  713. int one;
  714. int oneplushalf;
  715. jpc_fix_t *fp;
  716. int frowstep;
  717. int fstripestep;
  718. jpc_fix_t *fstripestart;
  719. jpc_fix_t *fvscanstart;
  720. jpc_fix_t *dp;
  721. int drowstep;
  722. int dstripestep;
  723. jpc_fix_t *dstripestart;
  724. jpc_fix_t *dvscanstart;
  725. /* Avoid compiler warning about unused parameters. */
  726. dec = 0;
  727. one = 1 << bitpos;
  728. half = one >> 1;
  729. oneplushalf = one | half;
  730. width = jas_matrix_numcols(data);
  731. height = jas_matrix_numrows(data);
  732. frowstep = jas_matrix_rowstep(flags);
  733. drowstep = jas_matrix_rowstep(data);
  734. fstripestep = frowstep << 2;
  735. dstripestep = drowstep << 2;
  736. fstripestart = jas_matrix_getref(flags, 1, 1);
  737. dstripestart = jas_matrix_getref(data, 0, 0);
  738. for (i = 0; i < height; i += 4, fstripestart += fstripestep,
  739. dstripestart += dstripestep) {
  740. fvscanstart = fstripestart;
  741. dvscanstart = dstripestart;
  742. vscanlen = JAS_MIN(4, height - i);
  743. for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
  744. fp = fvscanstart;
  745. if (vscanlen >= 4 && (!((*fp) & (JPC_SIG | JPC_VISIT |
  746. JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG |
  747. JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) &
  748. (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep,
  749. !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) {
  750. jpc_mqdec_setcurctx(mqdec, JPC_AGGCTXNO);
  751. JPC_T1D_GETBIT(mqdec, v, "CLN", "AGG");
  752. if (!v) {
  753. continue;
  754. }
  755. jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO);
  756. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL");
  757. runlen = v;
  758. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL");
  759. runlen = (runlen << 1) | v;
  760. f = *(fp = fvscanstart + frowstep * runlen);
  761. dp = dvscanstart + drowstep * runlen;
  762. k = vscanlen - runlen;
  763. switch (runlen) {
  764. case 0:
  765. goto clnpass_partial0;
  766. break;
  767. case 1:
  768. goto clnpass_partial1;
  769. break;
  770. case 2:
  771. goto clnpass_partial2;
  772. break;
  773. case 3:
  774. goto clnpass_partial3;
  775. break;
  776. }
  777. } else {
  778. f = *(fp = fvscanstart);
  779. dp = dvscanstart;
  780. k = vscanlen;
  781. goto clnpass_full0;
  782. }
  783. /* Process first sample in vertical scan. */
  784. jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
  785. mqdec, clnpass_full0:, clnpass_partial0:,
  786. vcausalflag);
  787. if (--k <= 0) {
  788. continue;
  789. }
  790. fp += frowstep;
  791. dp += drowstep;
  792. /* Process second sample in vertical scan. */
  793. f = *fp;
  794. jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
  795. mqdec, ;, clnpass_partial1:, 0);
  796. if (--k <= 0) {
  797. continue;
  798. }
  799. fp += frowstep;
  800. dp += drowstep;
  801. /* Process third sample in vertical scan. */
  802. f = *fp;
  803. jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
  804. mqdec, ;, clnpass_partial2:, 0);
  805. if (--k <= 0) {
  806. continue;
  807. }
  808. fp += frowstep;
  809. dp += drowstep;
  810. /* Process fourth sample in vertical scan. */
  811. f = *fp;
  812. jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
  813. mqdec, ;, clnpass_partial3:, 0);
  814. }
  815. }
  816. if (segsymflag) {
  817. int segsymval;
  818. segsymval = 0;
  819. jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO);
  820. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
  821. segsymval = (segsymval << 1) | (v & 1);
  822. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
  823. segsymval = (segsymval << 1) | (v & 1);
  824. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
  825. segsymval = (segsymval << 1) | (v & 1);
  826. JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
  827. segsymval = (segsymval << 1) | (v & 1);
  828. if (segsymval != 0xa) {
  829. jas_eprintf("warning: bad segmentation symbol\n");
  830. }
  831. }
  832. return 0;
  833. }