jpc_t1cod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. * $Id: jpc_t1cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  64. */
  65. /******************************************************************************\
  66. * Includes.
  67. \******************************************************************************/
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <assert.h>
  71. #include <math.h>
  72. #include "jasper/jas_types.h"
  73. #include "jasper/jas_math.h"
  74. #include "jpc_bs.h"
  75. #include "jpc_dec.h"
  76. #include "jpc_cs.h"
  77. #include "jpc_mqcod.h"
  78. #include "jpc_t1cod.h"
  79. #include "jpc_tsfb.h"
  80. double jpc_pow2i(int n);
  81. /******************************************************************************\
  82. * Global data.
  83. \******************************************************************************/
  84. int jpc_zcctxnolut[4 * 256];
  85. int jpc_spblut[256];
  86. int jpc_scctxnolut[256];
  87. int jpc_magctxnolut[4096];
  88. jpc_fix_t jpc_signmsedec[1 << JPC_NMSEDEC_BITS];
  89. jpc_fix_t jpc_refnmsedec[1 << JPC_NMSEDEC_BITS];
  90. jpc_fix_t jpc_signmsedec0[1 << JPC_NMSEDEC_BITS];
  91. jpc_fix_t jpc_refnmsedec0[1 << JPC_NMSEDEC_BITS];
  92. jpc_mqctx_t jpc_mqctxs[JPC_NUMCTXS];
  93. /******************************************************************************\
  94. *
  95. \******************************************************************************/
  96. void jpc_initmqctxs(void);
  97. /******************************************************************************\
  98. * Code.
  99. \******************************************************************************/
  100. int JPC_PASSTYPE(int passno)
  101. {
  102. int passtype;
  103. switch (passno % 3) {
  104. case 0:
  105. passtype = JPC_CLNPASS;
  106. break;
  107. case 1:
  108. passtype = JPC_SIGPASS;
  109. break;
  110. case 2:
  111. passtype = JPC_REFPASS;
  112. break;
  113. default:
  114. passtype = -1;
  115. assert(0);
  116. break;
  117. }
  118. return passtype;
  119. }
  120. int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient)
  121. {
  122. /* Avoid compiler warnings about unused parameters. */
  123. numlvls = 0;
  124. if (qmfbid == JPC_COX_INS) {
  125. return 0;
  126. }
  127. assert(qmfbid == JPC_COX_RFT);
  128. if (lvlno == 0) {
  129. assert(orient == JPC_TSFB_LL);
  130. return 0;
  131. } else {
  132. switch (orient) {
  133. case JPC_TSFB_LH:
  134. case JPC_TSFB_HL:
  135. return 1;
  136. break;
  137. case JPC_TSFB_HH:
  138. return 2;
  139. break;
  140. }
  141. }
  142. abort();
  143. }
  144. /******************************************************************************\
  145. * Coding pass related functions.
  146. \******************************************************************************/
  147. int JPC_SEGTYPE(int passno, int firstpassno, int bypass)
  148. {
  149. int passtype;
  150. if (bypass) {
  151. passtype = JPC_PASSTYPE(passno);
  152. if (passtype == JPC_CLNPASS) {
  153. return JPC_SEG_MQ;
  154. }
  155. return ((passno < firstpassno + 10) ? JPC_SEG_MQ : JPC_SEG_RAW);
  156. } else {
  157. return JPC_SEG_MQ;
  158. }
  159. }
  160. int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass, int termall)
  161. {
  162. int ret;
  163. int passtype;
  164. if (termall) {
  165. ret = 1;
  166. } else if (bypass) {
  167. if (passno < firstpassno + 10) {
  168. ret = 10 - (passno - firstpassno);
  169. } else {
  170. passtype = JPC_PASSTYPE(passno);
  171. switch (passtype) {
  172. case JPC_SIGPASS:
  173. ret = 2;
  174. break;
  175. case JPC_REFPASS:
  176. ret = 1;
  177. break;
  178. case JPC_CLNPASS:
  179. ret = 1;
  180. break;
  181. default:
  182. ret = -1;
  183. assert(0);
  184. break;
  185. }
  186. }
  187. } else {
  188. ret = JPC_PREC * 3 - 2;
  189. }
  190. ret = JAS_MIN(ret, numpasses - passno);
  191. return ret;
  192. }
  193. int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
  194. int lazy)
  195. {
  196. int ret;
  197. int n;
  198. if (passno - firstpassno == numpasses - 1) {
  199. ret = 1;
  200. } else {
  201. n = JPC_SEGPASSCNT(passno, firstpassno, numpasses, lazy, termall);
  202. ret = (n <= 1) ? 1 : 0;
  203. }
  204. return ret;
  205. }
  206. /******************************************************************************\
  207. * Lookup table code.
  208. \******************************************************************************/
  209. void jpc_initluts()
  210. {
  211. int i;
  212. int orient;
  213. int refine;
  214. float u;
  215. float v;
  216. float t;
  217. /* XXX - hack */
  218. jpc_initmqctxs();
  219. for (orient = 0; orient < 4; ++orient) {
  220. for (i = 0; i < 256; ++i) {
  221. jpc_zcctxnolut[(orient << 8) | i] = jpc_getzcctxno(i, orient);
  222. }
  223. }
  224. for (i = 0; i < 256; ++i) {
  225. jpc_spblut[i] = jpc_getspb(i << 4);
  226. }
  227. for (i = 0; i < 256; ++i) {
  228. jpc_scctxnolut[i] = jpc_getscctxno(i << 4);
  229. }
  230. for (refine = 0; refine < 2; ++refine) {
  231. for (i = 0; i < 2048; ++i) {
  232. jpc_magctxnolut[(refine << 11) + i] = jpc_getmagctxno((refine ? JPC_REFINE : 0) | i);
  233. }
  234. }
  235. for (i = 0; i < (1 << JPC_NMSEDEC_BITS); ++i) {
  236. t = i * jpc_pow2i(-JPC_NMSEDEC_FRACBITS);
  237. u = t;
  238. v = t - 1.5;
  239. jpc_signmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
  240. /* XXX - this calc is not correct */
  241. jpc_signmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
  242. u = t - 1.0;
  243. if (i & (1 << (JPC_NMSEDEC_BITS - 1))) {
  244. v = t - 1.5;
  245. } else {
  246. v = t - 0.5;
  247. }
  248. jpc_refnmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
  249. /* XXX - this calc is not correct */
  250. jpc_refnmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
  251. }
  252. }
  253. jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos)
  254. {
  255. jpc_fix_t y;
  256. assert(!(x & (~JAS_ONES(bitpos + 1))));
  257. y = jpc_getsignmsedec_macro(x, bitpos);
  258. return y;
  259. }
  260. int jpc_getzcctxno(int f, int orient)
  261. {
  262. int h;
  263. int v;
  264. int d;
  265. int n;
  266. int t;
  267. int hv;
  268. /* Avoid compiler warning. */
  269. n = 0;
  270. h = ((f & JPC_WSIG) != 0) + ((f & JPC_ESIG) != 0);
  271. v = ((f & JPC_NSIG) != 0) + ((f & JPC_SSIG) != 0);
  272. d = ((f & JPC_NWSIG) != 0) + ((f & JPC_NESIG) != 0) + ((f & JPC_SESIG) != 0) + ((f & JPC_SWSIG) != 0);
  273. switch (orient) {
  274. case JPC_TSFB_HL:
  275. t = h;
  276. h = v;
  277. v = t;
  278. case JPC_TSFB_LL:
  279. case JPC_TSFB_LH:
  280. if (!h) {
  281. if (!v) {
  282. if (!d) {
  283. n = 0;
  284. } else if (d == 1) {
  285. n = 1;
  286. } else {
  287. n = 2;
  288. }
  289. } else if (v == 1) {
  290. n = 3;
  291. } else {
  292. n = 4;
  293. }
  294. } else if (h == 1) {
  295. if (!v) {
  296. if (!d) {
  297. n = 5;
  298. } else {
  299. n = 6;
  300. }
  301. } else {
  302. n = 7;
  303. }
  304. } else {
  305. n = 8;
  306. }
  307. break;
  308. case JPC_TSFB_HH:
  309. hv = h + v;
  310. if (!d) {
  311. if (!hv) {
  312. n = 0;
  313. } else if (hv == 1) {
  314. n = 1;
  315. } else {
  316. n = 2;
  317. }
  318. } else if (d == 1) {
  319. if (!hv) {
  320. n = 3;
  321. } else if (hv == 1) {
  322. n = 4;
  323. } else {
  324. n = 5;
  325. }
  326. } else if (d == 2) {
  327. if (!hv) {
  328. n = 6;
  329. } else {
  330. n = 7;
  331. }
  332. } else {
  333. n = 8;
  334. }
  335. break;
  336. }
  337. assert(n < JPC_NUMZCCTXS);
  338. return JPC_ZCCTXNO + n;
  339. }
  340. int jpc_getspb(int f)
  341. {
  342. int hc;
  343. int vc;
  344. int n;
  345. hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG), 1) -
  346. JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) + ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
  347. vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG), 1) -
  348. JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) + ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
  349. if (!hc && !vc) {
  350. n = 0;
  351. } else {
  352. n = (!(hc > 0 || (!hc && vc > 0)));
  353. }
  354. return n;
  355. }
  356. int jpc_getscctxno(int f)
  357. {
  358. int hc;
  359. int vc;
  360. int n;
  361. /* Avoid compiler warning. */
  362. n = 0;
  363. hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG),
  364. 1) - JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) +
  365. ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
  366. vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG),
  367. 1) - JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) +
  368. ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
  369. assert(hc >= -1 && hc <= 1 && vc >= -1 && vc <= 1);
  370. if (hc < 0) {
  371. hc = -hc;
  372. vc = -vc;
  373. }
  374. if (!hc) {
  375. if (vc == -1) {
  376. n = 1;
  377. } else if (!vc) {
  378. n = 0;
  379. } else {
  380. n = 1;
  381. }
  382. } else if (hc == 1) {
  383. if (vc == -1) {
  384. n = 2;
  385. } else if (!vc) {
  386. n = 3;
  387. } else {
  388. n = 4;
  389. }
  390. }
  391. assert(n < JPC_NUMSCCTXS);
  392. return JPC_SCCTXNO + n;
  393. }
  394. int jpc_getmagctxno(int f)
  395. {
  396. int n;
  397. if (!(f & JPC_REFINE)) {
  398. n = (f & (JPC_OTHSIGMSK)) ? 1 : 0;
  399. } else {
  400. n = 2;
  401. }
  402. assert(n < JPC_NUMMAGCTXS);
  403. return JPC_MAGCTXNO + n;
  404. }
  405. void jpc_initctxs(jpc_mqctx_t *ctxs)
  406. {
  407. jpc_mqctx_t *ctx;
  408. int i;
  409. ctx = ctxs;
  410. for (i = 0; i < JPC_NUMCTXS; ++i) {
  411. ctx->mps = 0;
  412. switch (i) {
  413. case JPC_UCTXNO:
  414. ctx->ind = 46;
  415. break;
  416. case JPC_ZCCTXNO:
  417. ctx->ind = 4;
  418. break;
  419. case JPC_AGGCTXNO:
  420. ctx->ind = 3;
  421. break;
  422. default:
  423. ctx->ind = 0;
  424. break;
  425. }
  426. ++ctx;
  427. }
  428. }
  429. void jpc_initmqctxs()
  430. {
  431. jpc_initctxs(jpc_mqctxs);
  432. }
  433. /* Calculate the real quantity exp2(n), where x is an integer. */
  434. double jpc_pow2i(int n)
  435. {
  436. double x;
  437. double a;
  438. x = 1.0;
  439. if (n < 0) {
  440. a = 0.5;
  441. n = -n;
  442. } else {
  443. a = 2.0;
  444. }
  445. while (--n >= 0) {
  446. x *= a;
  447. }
  448. return x;
  449. }