f5ce91086fdeaa4186b2a3984a6b5ee10117fd7a
[openjpeg.git] / src / lib / openjp2 / t1.h
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  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #ifndef __T1_H
39 #define __T1_H
40 /**
41 @file t1.h
42 @brief Implementation of the tier-1 coding (coding of code-block coefficients) (T1)
43
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
48 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
49 /*@{*/
50
51 /* ----------------------------------------------------------------------- */
52 #define T1_NMSEDEC_BITS 7
53
54 /* CAUTION: the value of those constants must not be changed, otherwise the */
55 /* optimization of opj_t1_updateflags() will break! */
56 /* BEGINNING of flags that apply to opj_flag_t */
57 #define T1_SIG_NE 0x0001U /**< Context orientation : North-East direction */
58 #define T1_SIG_SE 0x0002U /**< Context orientation : South-East direction */
59 #define T1_SIG_SW 0x0004U /**< Context orientation : South-West direction */
60 #define T1_SIG_NW 0x0008U /**< Context orientation : North-West direction */
61 #define T1_SIG_N 0x0010U  /**< Context orientation : North direction */
62 #define T1_SIG_E 0x0020U  /**< Context orientation : East direction */
63 #define T1_SIG_S 0x0040U  /**< Context orientation : South direction */
64 #define T1_SIG_W 0x0080U  /**< Context orientation : West direction */
65 #define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)
66 #define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)
67
68 #define T1_SGN_N 0x0100U
69 #define T1_SGN_E 0x0200U
70 #define T1_SGN_S 0x0400U
71 #define T1_SGN_W 0x0800U
72 #define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)
73
74 #define T1_SIG    0x1000U /**< No longer used by decoder */
75 #define T1_REFINE 0x2000U /**< No longer used by decoder */
76 #define T1_VISIT  0x4000U /**< No longer used by decoder */
77 /* END of flags that apply to opj_flag_t */
78
79 #define T1_NUMCTXS_ZC  9
80 #define T1_NUMCTXS_SC  5
81 #define T1_NUMCTXS_MAG 3
82 #define T1_NUMCTXS_AGG 1
83 #define T1_NUMCTXS_UNI 1
84
85 #define T1_CTXNO_ZC  0
86 #define T1_CTXNO_SC  (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
87 #define T1_CTXNO_MAG (T1_CTXNO_SC+T1_NUMCTXS_SC)
88 #define T1_CTXNO_AGG (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
89 #define T1_CTXNO_UNI (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
90 #define T1_NUMCTXS   (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
91
92 #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
93
94 #define T1_TYPE_MQ 0    /**< Normal coding using entropy coder */
95 #define T1_TYPE_RAW 1   /**< No encoding the information is store under raw format in codestream (mode switch RAW)*/
96
97 /* Those flags are used by opj_colflag_t */
98 #define T1_COLFLAG_RBS              4U /* RBS = Row Bit Shift */
99 #define T1_COLFLAG_SIG_OTHER_ROW_0 (1U << 0U)  /**< This sample has at least one significant neighbour */
100 #define T1_COLFLAG_SIG_ROW_0       (1U << 1U)  /**< This sample is significant */
101 #define T1_COLFLAG_VISIT_ROW_0     (1U << 2U)  /**< This sample has been visited */
102 #define T1_COLFLAG_REFINE_ROW_0    (1U << 3U)  /**< This sample has been refined */
103 #define T1_COLFLAG_SIG_OTHER_ROW_1 (T1_COLFLAG_SIG_OTHER_ROW_0 << (1U * T1_COLFLAG_RBS))
104 #define T1_COLFLAG_SIG_ROW_1       (T1_COLFLAG_SIG_ROW_0       << (1U * T1_COLFLAG_RBS))
105 #define T1_COLFLAG_VISIT_ROW_1     (T1_COLFLAG_VISIT_ROW_0     << (1U * T1_COLFLAG_RBS))
106 #define T1_COLFLAG_REFINE_ROW_1    (T1_COLFLAG_REFINE_ROW_0    << (1U * T1_COLFLAG_RBS))
107 #define T1_COLFLAG_SIG_OTHER_ROW_2 (T1_COLFLAG_SIG_OTHER_ROW_0 << (2U * T1_COLFLAG_RBS))
108 #define T1_COLFLAG_SIG_ROW_2       (T1_COLFLAG_SIG_ROW_0       << (2U * T1_COLFLAG_RBS))
109 #define T1_COLFLAG_VISIT_ROW_2     (T1_COLFLAG_VISIT_ROW_0     << (2U * T1_COLFLAG_RBS))
110 #define T1_COLFLAG_REFINE_ROW_2    (T1_COLFLAG_REFINE_ROW_0    << (2U * T1_COLFLAG_RBS))
111 #define T1_COLFLAG_SIG_OTHER_ROW_3 (T1_COLFLAG_SIG_OTHER_ROW_0 << (3U * T1_COLFLAG_RBS))
112 #define T1_COLFLAG_SIG_ROW_3       (T1_COLFLAG_SIG_ROW_0       << (3U * T1_COLFLAG_RBS))
113 #define T1_COLFLAG_VISIT_ROW_3     (T1_COLFLAG_VISIT_ROW_0     << (3U * T1_COLFLAG_RBS))
114 #define T1_COLFLAG_REFINE_ROW_3    (T1_COLFLAG_REFINE_ROW_0    << (3U * T1_COLFLAG_RBS))
115
116
117 /* BEGINNING of flags that apply to opj_flag_enc_t */
118 /** We hold the state of individual data points for the T1 encoder using
119  *  a single 32-bit flags word to hold the state of 4 data points.  This corresponds
120  *  to the 4-point-high columns that the data is processed in.
121  *
122  *  These #defines declare the layout of a 32-bit flags word.
123  *
124  *  This is currently done for encoding only.
125  */
126
127 #define T1_SIGMA_0  (1U << 0)
128 #define T1_SIGMA_1  (1U << 1)
129 #define T1_SIGMA_2  (1U << 2)
130 #define T1_SIGMA_3  (1U << 3)
131 #define T1_SIGMA_4  (1U << 4)
132 #define T1_SIGMA_5  (1U << 5)
133 #define T1_SIGMA_6  (1U << 6)
134 #define T1_SIGMA_7  (1U << 7)
135 #define T1_SIGMA_8  (1U << 8)
136 #define T1_SIGMA_9  (1U << 9)
137 #define T1_SIGMA_10 (1U << 10)
138 #define T1_SIGMA_11 (1U << 11)
139 #define T1_SIGMA_12 (1U << 12)
140 #define T1_SIGMA_13 (1U << 13)
141 #define T1_SIGMA_14 (1U << 14)
142 #define T1_SIGMA_15 (1U << 15)
143 #define T1_SIGMA_16 (1U << 16)
144 #define T1_SIGMA_17 (1U << 17)
145
146 #define T1_CHI_0    (1U << 18)
147 #define T1_CHI_0_I  18
148 #define T1_CHI_1    (1U << 19)
149 #define T1_CHI_1_I  19
150 #define T1_MU_0     (1U << 20)
151 #define T1_PI_0     (1U << 21)
152 #define T1_CHI_2    (1U << 22)
153 #define T1_CHI_2_I  22
154 #define T1_MU_1     (1U << 23)
155 #define T1_PI_1     (1U << 24)
156 #define T1_CHI_3    (1U << 25)
157 #define T1_MU_2     (1U << 26)
158 #define T1_PI_2     (1U << 27)
159 #define T1_CHI_4    (1U << 28)
160 #define T1_MU_3     (1U << 29)
161 #define T1_PI_3     (1U << 30)
162 #define T1_CHI_5    (1U << 31)
163
164
165 /** As an example, the bits T1_SIGMA_3, T1_SIGMA_4 and T1_SIGMA_5
166  *  indicate the significance state of the west neighbour of data point zero
167  *  of our four, the point itself, and its east neighbour respectively.
168  *  Many of the bits are arranged so that given a flags word, you can
169  *  look at the values for the data point 0, then shift the flags
170  *  word right by 3 bits and look at the same bit positions to see the
171  *  values for data point 1.
172  *
173  *  The #defines below help a bit with this; say you have a flags word
174  *  f, you can do things like
175  *
176  *  (f & T1_SIGMA_THIS)
177  *
178  *  to see the significance bit of data point 0, then do
179  *
180  *  ((f >> 3) & T1_SIGMA_THIS)
181  *
182  *  to see the significance bit of data point 1.
183  */
184
185 #define T1_SIGMA_NW   T1_SIGMA_0
186 #define T1_SIGMA_N    T1_SIGMA_1
187 #define T1_SIGMA_NE   T1_SIGMA_2
188 #define T1_SIGMA_W    T1_SIGMA_3
189 #define T1_SIGMA_THIS T1_SIGMA_4
190 #define T1_SIGMA_E    T1_SIGMA_5
191 #define T1_SIGMA_SW   T1_SIGMA_6
192 #define T1_SIGMA_S    T1_SIGMA_7
193 #define T1_SIGMA_SE   T1_SIGMA_8
194 #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)
195
196 #define T1_CHI_THIS   T1_CHI_1
197 #define T1_CHI_THIS_I T1_CHI_1_I
198 #define T1_MU_THIS    T1_MU_0
199 #define T1_PI_THIS    T1_PI_0
200
201 #define T1_LUT_SGN_W (1U << 0)
202 #define T1_LUT_SIG_N (1U << 1)
203 #define T1_LUT_SGN_E (1U << 2)
204 #define T1_LUT_SIG_W (1U << 3)
205 #define T1_LUT_SGN_N (1U << 4)
206 #define T1_LUT_SIG_E (1U << 5)
207 #define T1_LUT_SGN_S (1U << 6)
208 #define T1_LUT_SIG_S (1U << 7)
209 /* END of flags that apply to opj_flag_enc_t */
210
211 /* ----------------------------------------------------------------------- */
212
213 typedef OPJ_UINT16 opj_flag_t;
214
215 /** Flags for 4 consecutive rows of a column */
216 typedef OPJ_UINT16 opj_colflag_t;
217
218 typedef OPJ_UINT32 opj_flag_enc_t;
219
220 /**
221 Tier-1 coding (coding of code-block coefficients)
222 */
223 typedef struct opj_t1 {
224
225     /** MQC component */
226     opj_mqc_t *mqc;
227     /** RAW component */
228     opj_raw_t *raw;
229
230     OPJ_INT32  *data;
231     /** Flags used by decoder */
232     opj_flag_t *flags;
233     /** Addition flag array such that colflags[1+0] is for state of col=0,row=0..3,
234        colflags[1+1] for col=1, row=0..3, colflags[1+flags_stride] for col=0,row=4..7, ...
235        This array avoids too much cache trashing when processing by 4 vertical samples
236        as done in the various decoding steps. */
237     opj_colflag_t* colflags;
238     /** Flags used by encoder */
239     opj_flag_enc_t *enc_flags;
240     OPJ_UINT32 w;
241     OPJ_UINT32 h;
242     OPJ_UINT32 datasize;
243     OPJ_UINT32 flagssize;
244     OPJ_UINT32 flags_stride;
245     OPJ_UINT32 colflags_size;
246     OPJ_UINT32 data_stride;
247     OPJ_BOOL   encoder;
248 } opj_t1_t;
249
250 #define MACRO_t1_flags(x,y) t1->flags[((x)*(t1->flags_stride))+(y)]
251
252 /** @name Exported functions */
253 /*@{*/
254 /* ----------------------------------------------------------------------- */
255
256 /**
257 Encode the code-blocks of a tile
258 @param t1 T1 handle
259 @param tile The tile to encode
260 @param tcp Tile coding parameters
261 @param mct_norms  FIXME DOC
262 @param mct_numcomps Number of components used for MCT
263 */
264 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
265                              opj_tcd_tile_t *tile,
266                              opj_tcp_t *tcp,
267                              const OPJ_FLOAT64 * mct_norms,
268                              OPJ_UINT32 mct_numcomps);
269
270 /**
271 Decode the code-blocks of a tile
272 @param t1 T1 handle
273 @param tilec The tile to decode
274 @param tccp Tile coding parameters
275 */
276 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
277                          volatile OPJ_BOOL* pret,
278                          opj_tcd_tilecomp_t* tilec,
279                          opj_tccp_t* tccp);
280
281
282
283 /**
284  * Creates a new Tier 1 handle
285  * and initializes the look-up tables of the Tier-1 coder/decoder
286  * @return a new T1 handle if successful, returns NULL otherwise
287 */
288 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder);
289
290 /**
291  * Destroys a previously created T1 handle
292  *
293  * @param p_t1 Tier 1 handle to destroy
294 */
295 void opj_t1_destroy(opj_t1_t *p_t1);
296 /* ----------------------------------------------------------------------- */
297 /*@}*/
298
299 /*@}*/
300
301 #endif /* __T1_H */