t1.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2012, Carl Hetherington
  15. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #ifndef OPJ_T1_H
  40. #define OPJ_T1_H
  41. /**
  42. @file t1.h
  43. @brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
  44. The functions in T1.C have for goal to realize the tier-1 coding operation. The functions
  45. in T1.C are used by some function in TCD.C.
  46. */
  47. /** @defgroup T1 T1 - Implementation of the tier-1 coding */
  48. /*@{*/
  49. /* ----------------------------------------------------------------------- */
  50. #define T1_NMSEDEC_BITS 7
  51. #define T1_NUMCTXS_ZC 9
  52. #define T1_NUMCTXS_SC 5
  53. #define T1_NUMCTXS_MAG 3
  54. #define T1_NUMCTXS_AGG 1
  55. #define T1_NUMCTXS_UNI 1
  56. #define T1_CTXNO_ZC 0
  57. #define T1_CTXNO_SC (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
  58. #define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC)
  59. #define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
  60. #define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
  61. #define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
  62. #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
  63. #define T1_TYPE_MQ 0 /**< Normal coding using entropy coder */
  64. #define T1_TYPE_RAW 1 /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/
  65. /* BEGINNING of flags that apply to opj_flag_t */
  66. /** We hold the state of individual data points for the T1 encoder using
  67. * a single 32-bit flags word to hold the state of 4 data points. This corresponds
  68. * to the 4-point-high columns that the data is processed in.
  69. *
  70. * These \#defines declare the layout of a 32-bit flags word.
  71. *
  72. * This is currently done for encoding only.
  73. * The values must NOT be changed, otherwise this is going to break a lot of
  74. * assumptions.
  75. */
  76. /* SIGMA: significance state (3 cols x 6 rows)
  77. * CHI: state for negative sample value (1 col x 6 rows)
  78. * MU: state for visited in refinement pass (1 col x 4 rows)
  79. * PI: state for visited in significance pass (1 col * 4 rows)
  80. */
  81. #define T1_SIGMA_0 (1U << 0)
  82. #define T1_SIGMA_1 (1U << 1)
  83. #define T1_SIGMA_2 (1U << 2)
  84. #define T1_SIGMA_3 (1U << 3)
  85. #define T1_SIGMA_4 (1U << 4)
  86. #define T1_SIGMA_5 (1U << 5)
  87. #define T1_SIGMA_6 (1U << 6)
  88. #define T1_SIGMA_7 (1U << 7)
  89. #define T1_SIGMA_8 (1U << 8)
  90. #define T1_SIGMA_9 (1U << 9)
  91. #define T1_SIGMA_10 (1U << 10)
  92. #define T1_SIGMA_11 (1U << 11)
  93. #define T1_SIGMA_12 (1U << 12)
  94. #define T1_SIGMA_13 (1U << 13)
  95. #define T1_SIGMA_14 (1U << 14)
  96. #define T1_SIGMA_15 (1U << 15)
  97. #define T1_SIGMA_16 (1U << 16)
  98. #define T1_SIGMA_17 (1U << 17)
  99. #define T1_CHI_0 (1U << 18)
  100. #define T1_CHI_0_I 18
  101. #define T1_CHI_1 (1U << 19)
  102. #define T1_CHI_1_I 19
  103. #define T1_MU_0 (1U << 20)
  104. #define T1_PI_0 (1U << 21)
  105. #define T1_CHI_2 (1U << 22)
  106. #define T1_CHI_2_I 22
  107. #define T1_MU_1 (1U << 23)
  108. #define T1_PI_1 (1U << 24)
  109. #define T1_CHI_3 (1U << 25)
  110. #define T1_MU_2 (1U << 26)
  111. #define T1_PI_2 (1U << 27)
  112. #define T1_CHI_4 (1U << 28)
  113. #define T1_MU_3 (1U << 29)
  114. #define T1_PI_3 (1U << 30)
  115. #define T1_CHI_5 (1U << 31)
  116. #define T1_CHI_5_I 31
  117. /** As an example, the bits T1_SIGMA_3, T1_SIGMA_4 and T1_SIGMA_5
  118. * indicate the significance state of the west neighbour of data point zero
  119. * of our four, the point itself, and its east neighbour respectively.
  120. * Many of the bits are arranged so that given a flags word, you can
  121. * look at the values for the data point 0, then shift the flags
  122. * word right by 3 bits and look at the same bit positions to see the
  123. * values for data point 1.
  124. *
  125. * The \#defines below help a bit with this; say you have a flags word
  126. * f, you can do things like
  127. *
  128. * (f & T1_SIGMA_THIS)
  129. *
  130. * to see the significance bit of data point 0, then do
  131. *
  132. * ((f >> 3) & T1_SIGMA_THIS)
  133. *
  134. * to see the significance bit of data point 1.
  135. */
  136. #define T1_SIGMA_NW T1_SIGMA_0
  137. #define T1_SIGMA_N T1_SIGMA_1
  138. #define T1_SIGMA_NE T1_SIGMA_2
  139. #define T1_SIGMA_W T1_SIGMA_3
  140. #define T1_SIGMA_THIS T1_SIGMA_4
  141. #define T1_SIGMA_E T1_SIGMA_5
  142. #define T1_SIGMA_SW T1_SIGMA_6
  143. #define T1_SIGMA_S T1_SIGMA_7
  144. #define T1_SIGMA_SE T1_SIGMA_8
  145. #define T1_SIGMA_NEIGHBOURS (T1_SIGMA_NW | T1_SIGMA_N | T1_SIGMA_NE | T1_SIGMA_W | T1_SIGMA_E | T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE)
  146. #define T1_CHI_THIS T1_CHI_1
  147. #define T1_CHI_THIS_I T1_CHI_1_I
  148. #define T1_MU_THIS T1_MU_0
  149. #define T1_PI_THIS T1_PI_0
  150. #define T1_CHI_S T1_CHI_2
  151. #define T1_LUT_SGN_W (1U << 0)
  152. #define T1_LUT_SIG_N (1U << 1)
  153. #define T1_LUT_SGN_E (1U << 2)
  154. #define T1_LUT_SIG_W (1U << 3)
  155. #define T1_LUT_SGN_N (1U << 4)
  156. #define T1_LUT_SIG_E (1U << 5)
  157. #define T1_LUT_SGN_S (1U << 6)
  158. #define T1_LUT_SIG_S (1U << 7)
  159. /* END of flags that apply to opj_flag_t */
  160. /* ----------------------------------------------------------------------- */
  161. /** Flags for 4 consecutive rows of a column */
  162. typedef OPJ_UINT32 opj_flag_t;
  163. /**
  164. Tier-1 coding (coding of code-block coefficients)
  165. */
  166. typedef struct opj_t1 {
  167. /** MQC component */
  168. opj_mqc_t mqc;
  169. OPJ_INT32 *data;
  170. /** Flags used by decoder and encoder.
  171. * Such that flags[1+0] is for state of col=0,row=0..3,
  172. flags[1+1] for col=1, row=0..3, flags[1+flags_stride] for col=0,row=4..7, ...
  173. This array avoids too much cache trashing when processing by 4 vertical samples
  174. as done in the various decoding steps. */
  175. opj_flag_t *flags;
  176. OPJ_UINT32 w;
  177. OPJ_UINT32 h;
  178. OPJ_UINT32 datasize;
  179. OPJ_UINT32 flagssize;
  180. OPJ_BOOL encoder;
  181. /* The 3 variables below are only used by the decoder */
  182. /* set to TRUE in multithreaded context */
  183. OPJ_BOOL mustuse_cblkdatabuffer;
  184. /* Temporary buffer to concatenate all chunks of a codebock */
  185. OPJ_BYTE *cblkdatabuffer;
  186. /* Maximum size available in cblkdatabuffer */
  187. OPJ_UINT32 cblkdatabuffersize;
  188. } opj_t1_t;
  189. /** @name Exported functions */
  190. /*@{*/
  191. /* ----------------------------------------------------------------------- */
  192. /**
  193. Encode the code-blocks of a tile
  194. @param tcd TCD handle
  195. @param tile The tile to encode
  196. @param tcp Tile coding parameters
  197. @param mct_norms FIXME DOC
  198. @param mct_numcomps Number of components used for MCT
  199. */
  200. OPJ_BOOL opj_t1_encode_cblks(opj_tcd_t* tcd,
  201. opj_tcd_tile_t *tile,
  202. opj_tcp_t *tcp,
  203. const OPJ_FLOAT64 * mct_norms,
  204. OPJ_UINT32 mct_numcomps);
  205. /**
  206. Decode the code-blocks of a tile
  207. @param tcd TCD handle
  208. @param pret Pointer to return value
  209. @param tilec The tile to decode
  210. @param tccp Tile coding parameters
  211. @param p_manager the event manager
  212. @param p_manager_mutex mutex for the event manager
  213. @param check_pterm whether PTERM correct termination should be checked
  214. */
  215. void opj_t1_decode_cblks(opj_tcd_t* tcd,
  216. volatile OPJ_BOOL* pret,
  217. opj_tcd_tilecomp_t* tilec,
  218. opj_tccp_t* tccp,
  219. opj_event_mgr_t *p_manager,
  220. opj_mutex_t* p_manager_mutex,
  221. OPJ_BOOL check_pterm);
  222. /**
  223. * Creates a new Tier 1 handle
  224. * and initializes the look-up tables of the Tier-1 coder/decoder
  225. * @return a new T1 handle if successful, returns NULL otherwise
  226. */
  227. opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder);
  228. /**
  229. * Destroys a previously created T1 handle
  230. *
  231. * @param p_t1 Tier 1 handle to destroy
  232. */
  233. void opj_t1_destroy(opj_t1_t *p_t1);
  234. /* ----------------------------------------------------------------------- */
  235. /*@}*/
  236. /*@}*/
  237. #endif /* OPJ_T1_H */