jpc_t1cod.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 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.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  64. */
  65. #ifndef JPC_T1COD_H
  66. #define JPC_T1COD_H
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include "jasper/jas_fix.h"
  71. #include "jasper/jas_math.h"
  72. #include "jpc_mqcod.h"
  73. #include "jpc_tsfb.h"
  74. /******************************************************************************\
  75. * Constants.
  76. \******************************************************************************/
  77. /* The number of bits used to index into various lookup tables. */
  78. #define JPC_NMSEDEC_BITS 7
  79. #define JPC_NMSEDEC_FRACBITS (JPC_NMSEDEC_BITS - 1)
  80. /*
  81. * Segment types.
  82. */
  83. /* Invalid. */
  84. #define JPC_SEG_INVALID 0
  85. /* MQ. */
  86. #define JPC_SEG_MQ 1
  87. /* Raw. */
  88. #define JPC_SEG_RAW 2
  89. /* The nominal word size. */
  90. #define JPC_PREC 32
  91. /* Tier-1 coding pass types. */
  92. #define JPC_SIGPASS 0 /* significance */
  93. #define JPC_REFPASS 1 /* refinement */
  94. #define JPC_CLNPASS 2 /* cleanup */
  95. /*
  96. * Per-sample state information for tier-1 coding.
  97. */
  98. /* The northeast neighbour has been found to be significant. */
  99. #define JPC_NESIG 0x0001
  100. /* The southeast neighbour has been found to be significant. */
  101. #define JPC_SESIG 0x0002
  102. /* The southwest neighbour has been found to be significant. */
  103. #define JPC_SWSIG 0x0004
  104. /* The northwest neighbour has been found to be significant. */
  105. #define JPC_NWSIG 0x0008
  106. /* The north neighbour has been found to be significant. */
  107. #define JPC_NSIG 0x0010
  108. /* The east neighbour has been found to be significant. */
  109. #define JPC_ESIG 0x0020
  110. /* The south neighbour has been found to be significant. */
  111. #define JPC_SSIG 0x0040
  112. /* The west neighbour has been found to be significant. */
  113. #define JPC_WSIG 0x0080
  114. /* The significance mask for 8-connected neighbours. */
  115. #define JPC_OTHSIGMSK \
  116. (JPC_NSIG | JPC_NESIG | JPC_ESIG | JPC_SESIG | JPC_SSIG | JPC_SWSIG | JPC_WSIG | JPC_NWSIG)
  117. /* The significance mask for 4-connected neighbours. */
  118. #define JPC_PRIMSIGMSK (JPC_NSIG | JPC_ESIG | JPC_SSIG | JPC_WSIG)
  119. /* The north neighbour is negative in value. */
  120. #define JPC_NSGN 0x0100
  121. /* The east neighbour is negative in value. */
  122. #define JPC_ESGN 0x0200
  123. /* The south neighbour is negative in value. */
  124. #define JPC_SSGN 0x0400
  125. /* The west neighbour is negative in value. */
  126. #define JPC_WSGN 0x0800
  127. /* The sign mask for 4-connected neighbours. */
  128. #define JPC_SGNMSK (JPC_NSGN | JPC_ESGN | JPC_SSGN | JPC_WSGN)
  129. /* This sample has been found to be significant. */
  130. #define JPC_SIG 0x1000
  131. /* The sample has been refined. */
  132. #define JPC_REFINE 0x2000
  133. /* This sample has been processed during the significance pass. */
  134. #define JPC_VISIT 0x4000
  135. /* The number of aggregation contexts. */
  136. #define JPC_NUMAGGCTXS 1
  137. /* The number of zero coding contexts. */
  138. #define JPC_NUMZCCTXS 9
  139. /* The number of magnitude contexts. */
  140. #define JPC_NUMMAGCTXS 3
  141. /* The number of sign coding contexts. */
  142. #define JPC_NUMSCCTXS 5
  143. /* The number of uniform contexts. */
  144. #define JPC_NUMUCTXS 1
  145. /* The context ID for the first aggregation context. */
  146. #define JPC_AGGCTXNO 0
  147. /* The context ID for the first zero coding context. */
  148. #define JPC_ZCCTXNO (JPC_AGGCTXNO + JPC_NUMAGGCTXS)
  149. /* The context ID for the first magnitude context. */
  150. #define JPC_MAGCTXNO (JPC_ZCCTXNO + JPC_NUMZCCTXS)
  151. /* The context ID for the first sign coding context. */
  152. #define JPC_SCCTXNO (JPC_MAGCTXNO + JPC_NUMMAGCTXS)
  153. /* The context ID for the first uniform context. */
  154. #define JPC_UCTXNO (JPC_SCCTXNO + JPC_NUMSCCTXS)
  155. /* The total number of contexts. */
  156. #define JPC_NUMCTXS (JPC_UCTXNO + JPC_NUMUCTXS)
  157. /******************************************************************************\
  158. * External data.
  159. \******************************************************************************/
  160. /* These lookup tables are used by various macros/functions. */
  161. /* Do not access these lookup tables directly. */
  162. extern int jpc_zcctxnolut[];
  163. extern int jpc_spblut[];
  164. extern int jpc_scctxnolut[];
  165. extern int jpc_magctxnolut[];
  166. extern jpc_fix_t jpc_refnmsedec[];
  167. extern jpc_fix_t jpc_signmsedec[];
  168. extern jpc_fix_t jpc_refnmsedec0[];
  169. extern jpc_fix_t jpc_signmsedec0[];
  170. /* The initial settings for the MQ contexts. */
  171. extern jpc_mqctx_t jpc_mqctxs[];
  172. /******************************************************************************\
  173. * Functions and macros.
  174. \******************************************************************************/
  175. /* Initialize the MQ contexts. */
  176. void jpc_initctxs(jpc_mqctx_t *ctxs);
  177. /* Get the zero coding context. */
  178. int jpc_getzcctxno(int f, int orient);
  179. #define JPC_GETZCCTXNO(f, orient) \
  180. (jpc_zcctxnolut[((orient) << 8) | ((f) & JPC_OTHSIGMSK)])
  181. /* Get the sign prediction bit. */
  182. int jpc_getspb(int f);
  183. #define JPC_GETSPB(f) \
  184. (jpc_spblut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4])
  185. /* Get the sign coding context. */
  186. int jpc_getscctxno(int f);
  187. #define JPC_GETSCCTXNO(f) \
  188. (jpc_scctxnolut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4])
  189. /* Get the magnitude context. */
  190. int jpc_getmagctxno(int f);
  191. #define JPC_GETMAGCTXNO(f) \
  192. (jpc_magctxnolut[((f) & JPC_OTHSIGMSK) | ((((f) & JPC_REFINE) != 0) << 11)])
  193. /* Get the normalized MSE reduction for significance passes. */
  194. #define JPC_GETSIGNMSEDEC(x, bitpos) jpc_getsignmsedec_macro(x, bitpos)
  195. jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos);
  196. #define jpc_getsignmsedec_macro(x, bitpos) \
  197. ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_signmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \
  198. (jpc_signmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]))
  199. /* Get the normalized MSE reduction for refinement passes. */
  200. #define JPC_GETREFNMSEDEC(x, bitpos) jpc_getrefnmsedec_macro(x, bitpos)
  201. jpc_fix_t jpc_refsignmsedec_func(jpc_fix_t x, int bitpos);
  202. #define jpc_getrefnmsedec_macro(x, bitpos) \
  203. ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_refnmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \
  204. (jpc_refnmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]))
  205. /* Arithmetic shift right (with ability to shift left also). */
  206. #define JPC_ASR(x, n) \
  207. (((n) >= 0) ? ((x) >> (n)) : ((x) << (-(n))))
  208. /* Update the per-sample state information. */
  209. #define JPC_UPDATEFLAGS4(fp, rowstep, s, vcausalflag) \
  210. { \
  211. register jpc_fix_t *np = (fp) - (rowstep); \
  212. register jpc_fix_t *sp = (fp) + (rowstep); \
  213. if ((vcausalflag)) { \
  214. sp[-1] |= JPC_NESIG; \
  215. sp[1] |= JPC_NWSIG; \
  216. if (s) { \
  217. *sp |= JPC_NSIG | JPC_NSGN; \
  218. (fp)[-1] |= JPC_ESIG | JPC_ESGN; \
  219. (fp)[1] |= JPC_WSIG | JPC_WSGN; \
  220. } else { \
  221. *sp |= JPC_NSIG; \
  222. (fp)[-1] |= JPC_ESIG; \
  223. (fp)[1] |= JPC_WSIG; \
  224. } \
  225. } else { \
  226. np[-1] |= JPC_SESIG; \
  227. np[1] |= JPC_SWSIG; \
  228. sp[-1] |= JPC_NESIG; \
  229. sp[1] |= JPC_NWSIG; \
  230. if (s) { \
  231. *np |= JPC_SSIG | JPC_SSGN; \
  232. *sp |= JPC_NSIG | JPC_NSGN; \
  233. (fp)[-1] |= JPC_ESIG | JPC_ESGN; \
  234. (fp)[1] |= JPC_WSIG | JPC_WSGN; \
  235. } else { \
  236. *np |= JPC_SSIG; \
  237. *sp |= JPC_NSIG; \
  238. (fp)[-1] |= JPC_ESIG; \
  239. (fp)[1] |= JPC_WSIG; \
  240. } \
  241. } \
  242. }
  243. /* Initialize the lookup tables used by the codec. */
  244. void jpc_initluts(void);
  245. /* Get the nominal gain associated with a particular band. */
  246. int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient);
  247. /* Get the coding pass type. */
  248. int JPC_PASSTYPE(int passno);
  249. /* Get the segment type. */
  250. int JPC_SEGTYPE(int passno, int firstpassno, int bypass);
  251. /* Get the number of coding passess in the segment. */
  252. int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass,
  253. int termall);
  254. /* Is the coding pass terminated? */
  255. int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
  256. int lazy);
  257. #endif