Fix typos in comments and string
[openjpeg.git] / src / lib / openjp3d / t1_3d.c
1 /*
2  * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "opj_includes.h"
28
29 /** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */
30 /*@{*/
31
32 /** @name Local static functions */
33 /*@{*/
34
35 static int t1_3d_getctxno_zc(unsigned int f, int orient);
36 static int t1_3d_getctxno_sc(unsigned int f);
37 static int t1_3d_getctxno_mag(unsigned int f, int fsvr);
38 static int t1_3d_getspb(unsigned int f);
39 static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos);
40 static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos);
41 static void t1_3d_updateflags(unsigned int *fp, int s);
42 /**
43 Encode significant pass
44 */
45 static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
46 /**
47 Decode significant pass
48 */
49 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc);
50 /**
51 Encode significant pass
52 */
53 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);
54 /**
55 Decode significant pass
56 */
57 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
58 /**
59 Encode refinement pass
60 */
61 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
62 /**
63 Decode refinement pass
64 */
65 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc);
66 /**
67 Encode refinement pass
68 */
69 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
70 /**
71 Decode refinement pass
72 */
73 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
74 /**
75 Encode clean-up pass
76 */
77 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
78 /**
79 Decode clean-up pass
80 */
81 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc);
82 /**
83 Encode clean-up pass
84 */
85 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
86 /**
87 Decode clean-up pass
88 */
89 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
90 /**
91 Encode 1 code-block
92 @param t1 T1 handle
93 @param cblk Code-block coding parameters
94 @param orient
95 @param compno Component number
96 @param level[3]
97 @param dwtid[3]
98 @param stepsize
99 @param cblksty Code-block style
100 @param numcomps
101 @param tile
102 */
103 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno,  int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
104 /**
105 Decode 1 code-block
106 @param t1 T1 handle
107 @param cblk Code-block coding parameters
108 @param orient
109 @param roishift Region of interest shifting value
110 @param cblksty Code-block style
111 */
112 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
113 static int t1_3d_init_ctxno_zc(unsigned int f, int orient);
114 static int t1_3d_init_ctxno_sc(unsigned int f);
115 static int t1_3d_init_ctxno_mag(unsigned int f, int f2);
116 static int t1_3d_init_spb(unsigned int f);
117 /**
118 Initialize the look-up tables of the Tier-1 coder/decoder
119 @param t1 T1 handle
120 */
121 static void t1_3d_init_luts(opj_t1_3d_t *t1);
122
123 /*@}*/
124
125 /*@}*/
126
127 /* ----------------------------------------------------------------------- */
128
129 static int t1_3d_getctxno_zc(unsigned int f, int orient) {
130         return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient);
131 }
132
133 static int t1_3d_getctxno_sc(unsigned int f) {
134         return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
135         /*return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
136 }
137
138 static int t1_3d_getctxno_mag(unsigned int f, int fsvr) {
139         return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr);
140 }
141
142 static int t1_3d_getspb(unsigned int f) {
143         return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
144         /*return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
145 }
146
147 static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos) {
148         if (bitpos > T1_NMSEDEC_FRACBITS) {
149                 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
150         }
151         
152         return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
153 }
154
155 static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos) {
156         if (bitpos > T1_NMSEDEC_FRACBITS) {
157                 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
158         }
159
160     return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
161 }
162
163 static void t1_3d_updateflags(unsigned int *fp, int s) {
164         unsigned int *np = fp - (T1_MAXCBLKW + 2);
165         unsigned int *sp = fp + (T1_MAXCBLKW + 2);
166
167         unsigned int *bwp = fp + ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
168         unsigned int *bnp = bwp - (T1_MAXCBLKW + 2);
169         unsigned int *bsp = bwp + (T1_MAXCBLKW + 2);
170         
171         unsigned int *fwp = fp - ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
172         unsigned int *fnp = fwp - (T1_MAXCBLKW + 2);
173         unsigned int *fsp = fwp + (T1_MAXCBLKW + 2);
174
175         np[-1] |= T1_3D_SIG_SE;
176         np[1] |= T1_3D_SIG_SW;
177         sp[-1] |= T1_3D_SIG_NE;
178         sp[1] |= T1_3D_SIG_NW;
179         *np |= T1_3D_SIG_S;
180         *sp |= T1_3D_SIG_N;
181         fp[-1] |= T1_3D_SIG_E;
182         fp[1] |= T1_3D_SIG_W;
183
184         *fwp |= T1_3D_SIG_FC;
185         *bwp |= T1_3D_SIG_BC;
186
187         fnp[-1] |= T1_3D_SIG_FSE;
188         fnp[1] |= T1_3D_SIG_FSW;
189         fsp[-1] |= T1_3D_SIG_FNE;
190         fsp[1] |= T1_3D_SIG_FNW;
191         *fnp |= T1_3D_SIG_FS;
192         *fsp |= T1_3D_SIG_FN;
193         fwp[-1] |= T1_3D_SIG_FE;
194         fwp[1] |= T1_3D_SIG_FW;
195
196         bnp[-1] |= T1_3D_SIG_BSE;
197         bnp[1] |= T1_3D_SIG_BSW;
198         bsp[-1] |= T1_3D_SIG_BNE;
199         bsp[1] |= T1_3D_SIG_BNW;
200         *bnp |= T1_3D_SIG_BS;
201         *bsp |= T1_3D_SIG_BN;
202         bwp[-1] |= T1_3D_SIG_BE;
203         bwp[1] |= T1_3D_SIG_BW;
204
205         if (s) {
206                 *np |= (T1_3D_SGN_S << 16);
207                 *sp |= (T1_3D_SGN_N << 16);
208                 fp[-1] |= (T1_3D_SGN_E << 16);
209                 fp[1] |= (T1_3D_SGN_W << 16);
210                 *fwp |= (T1_3D_SGN_F << 16);
211                 *bwp |= (T1_3D_SGN_B << 16);
212         }
213 }
214
215 static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
216         int v, flagsvr;
217         unsigned int flag;
218
219         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
220         
221         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
222         flagsvr = (*fsvr);
223         if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
224                 v = int_abs(*dp) & one ? 1 : 0;
225                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
226                         mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));    /* ESSAI */
227                         mqc_bypass_enc(mqc, v);
228                 } else {
229                         mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
230                         mqc_encode(mqc, v);
231                 }
232                 if (v) {
233                         v = *dp < 0 ? 1 : 0;
234                         *nmsedec +=     t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
235                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
236                                 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));    /* ESSAI */
237                                 mqc_bypass_enc(mqc, v);
238                         } else {
239                                 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
240                                 mqc_encode(mqc, v ^ t1_3d_getspb(flag));
241                         }
242                         t1_3d_updateflags(fp, v);
243                         *fsvr |= T1_3D_SIG;
244                 }
245                 *fsvr |= T1_3D_VISIT;
246         }
247 }
248
249 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc) {
250         int v, flagsvr;
251         unsigned int flag;
252         
253         opj_raw_t *raw = t1->raw;       /* RAW component */
254         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
255         
256         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
257         flagsvr = (*fsvr);
258         if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
259                 if (type == T1_TYPE_RAW) {
260                         if (raw_decode(raw)) {
261                                 v = raw_decode(raw);    /* ESSAI */
262                                 *dp = v ? -oneplushalf : oneplushalf;
263                                 t1_3d_updateflags(fp, v);
264                                 *fsvr |= T1_3D_SIG;
265                         }
266                 } else {
267                         mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
268                         if (mqc_decode(mqc)) {
269                                 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
270                                 v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
271                                 *dp = v ? -oneplushalf : oneplushalf;
272                                 t1_3d_updateflags(fp, v);
273                                 *fsvr |= T1_3D_SIG;
274                         }
275                 }
276                 *fsvr |= T1_3D_VISIT;
277         }
278 }                               /* VSC and  BYPASS by Antonin */
279
280 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {
281         int i, j, k, m, one, vsc;
282         *nmsedec = 0;
283         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
284         for (m = 0; m < l; m++) {
285                 for (k = 0; k < h; k += 4) {
286                         for (i = 0; i < w; i++) {
287                                 for (j = k; j < k + 4 && j < h; j++) {
288                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
289                                         t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
290                                 }
291                         }
292                 }
293         }
294 }
295
296 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
297         int i, j, k, m, one, half, oneplushalf, vsc;
298         one = 1 << bpno;
299         half = one >> 1;
300         oneplushalf = one | half;
301         for (m = 0; m < l; m++) {
302                 for (k = 0; k < h; k += 4) {
303                         for (i = 0; i < w; i++) {
304                                 for (j = k; j < k + 4 && j < h; j++) {
305                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
306                                         t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
307                                 }
308                         }
309                 }
310         }
311 }                               /* VSC and  BYPASS by Antonin */
312
313 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
314         int v, flagsvr;
315         unsigned int flag;
316
317         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
318         
319         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
320         flagsvr = (*fsvr);
321         if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
322                 *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
323                 v = int_abs(*dp) & one ? 1 : 0;
324                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
325                         mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */
326                         mqc_bypass_enc(mqc, v);
327                 } else {
328                         mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
329                         mqc_encode(mqc, v);
330                 }
331                 *fsvr |= T1_3D_REFINE;
332         }
333 }
334
335 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc) {
336         int v, t, flagsvr;
337         unsigned int flag;
338
339         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
340         opj_raw_t *raw = t1->raw;       /* RAW component */
341         
342         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
343         flagsvr = (*fsvr);
344         if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
345                 if (type == T1_TYPE_RAW) {
346                         mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));  /* ESSAI */
347                         v = raw_decode(raw);
348                 } else {
349                         mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
350                         v = mqc_decode(mqc);
351                 }
352                 t = v ? poshalf : neghalf;
353                 *dp += *dp < 0 ? -t : t;
354                 *fsvr |= T1_3D_REFINE;
355         }
356 }                               /* VSC and  BYPASS by Antonin  */
357
358 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
359         int i, j, k, m, one, vsc;
360         *nmsedec = 0;
361         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
362         for (m = 0; m < l; m++){
363                 for (k = 0; k < h; k += 4) {
364                         for (i = 0; i < w; i++) {
365                                 for (j = k; j < k + 4 && j < h; j++) {
366                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
367                                         t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
368                                 }
369                         }
370                 }
371         }       
372 }
373
374 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
375         int i, j, k, m, one, poshalf, neghalf;
376         int vsc;
377         one = 1 << bpno;
378         poshalf = one >> 1;
379         neghalf = bpno > 0 ? -poshalf : -1;
380         for (m = 0; m < l; m++) {
381                 for (k = 0; k < h; k += 4) {
382                         for (i = 0; i < w; i++) {
383                                 for (j = k; j < k + 4 && j < h; j++) {
384                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
385                                         t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
386                                 }
387                         }
388                 }
389         }
390 }                               /* VSC and  BYPASS by Antonin */
391
392 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
393         int v, flagsvr;
394         unsigned int flag;
395
396         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
397         
398         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
399         flagsvr = (*fsvr);
400         if (partial) {
401                 goto LABEL_PARTIAL;
402         }
403         if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) {
404                 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
405                 v = int_abs(*dp) & one ? 1 : 0;
406                 mqc_encode(mqc, v);
407                 if (v) {
408 LABEL_PARTIAL:
409                         *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
410                         mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
411                         v = *dp < 0 ? 1 : 0;
412                         mqc_encode(mqc, v ^ t1_3d_getspb(flag));
413                         t1_3d_updateflags(fp, v);
414                         *fsvr |= T1_3D_SIG;
415                 }
416         }
417         *fsvr &= ~T1_3D_VISIT;
418 }
419
420 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc) {
421         int v, flagsvr;
422         unsigned int flag;
423
424         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
425         
426         flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
427         flagsvr = (*fsvr);
428         if (partial) {
429                 goto LABEL_PARTIAL;
430         }
431         if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
432                 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
433                 if (mqc_decode(mqc)) {
434 LABEL_PARTIAL:
435                         mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
436                         v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
437                         *dp = v ? -oneplushalf : oneplushalf;
438                         t1_3d_updateflags(fp, v);
439                         *fsvr |= T1_3D_SIG;
440                 }
441         }
442         *fsvr &= ~T1_3D_VISIT;
443 }                               /* VSC and  BYPASS by Antonin */
444
445 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
446         int i, j, k, m, one, agg, runlen, vsc;
447         
448         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
449         
450         *nmsedec = 0;
451         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
452         for (m = 0; m < l; m++) {
453                 for (k = 0; k < h; k += 4) {
454                         for (i = 0; i < w; i++) {
455                                 if (k + 3 < h) {
456                                         if (cblksty & J3D_CCP_CBLKSTY_VSC) {
457                                                 agg = !( ((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
458                                                         ||   ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
459                                                         ||   ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
460                                                         ||   ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
461                                                         );
462                                         } else {
463                                                 agg = !(
464                                                         ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
465                                                         || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
466                                                         || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
467                                                         || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
468                                                         );
469                                         }
470                                 } else {
471                                         agg = 0;
472                                 }
473                                 if (agg) {
474                                         for (runlen = 0; runlen < 4; runlen++) {
475                                                 if (int_abs(t1->data[m][k + runlen][i]) & one)
476                                                         break;
477                                         }
478                                         mqc_setcurctx(mqc, T1_CTXNO_AGG);
479                                         mqc_encode(mqc, runlen != 4);
480                                         if (runlen == 4) {
481                                                 continue;
482                                         }
483                                         mqc_setcurctx(mqc, T1_CTXNO_UNI);
484                                         mqc_encode(mqc, runlen >> 1);
485                                         mqc_encode(mqc, runlen & 1);
486                                 } else {
487                                         runlen = 0;
488                                 }
489                                 for (j = k + runlen; j < k + 4 && j < h; j++) {
490                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
491                                         t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
492                                 }
493                         }
494                 }
495         }
496 }
497
498 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
499         int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
500         int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
501         
502         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
503         
504         one = 1 << bpno;
505         half = one >> 1;
506         oneplushalf = one | half;
507         for (m = 0; m < l; m++) {
508                 for (k = 0; k < h; k += 4) {
509                         for (i = 0; i < w; i++) {
510                                 if (k + 3 < h) {
511                                         if (cblksty & J3D_CCP_CBLKSTY_VSC) {
512                                                 agg = !(
513                                                         ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
514                                                         || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
515                                                         || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
516                                                         || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
517                                                         );
518                                         } else {
519                                                 agg = !(
520                                                         ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
521                                                         || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
522                                                         || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
523                                                         || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
524                                                         );
525                                         }
526                                 } else {
527                                         agg = 0;
528                                 }
529                                 if (agg) {
530                                         mqc_setcurctx(mqc, T1_CTXNO_AGG);
531                                         if (!mqc_decode(mqc)) {
532                                                 continue;
533                                         }
534                                         mqc_setcurctx(mqc, T1_CTXNO_UNI);
535                                         runlen = mqc_decode(mqc);
536                                         runlen = (runlen << 1) | mqc_decode(mqc);
537                                 } else {
538                                         runlen = 0;
539                                 }
540                                 for (j = k + runlen; j < k + 4 && j < h; j++) {
541                                         vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
542                                         t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
543                                 }
544                         }
545                 }
546         }
547         if (segsym) {
548                 int v = 0;
549                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
550                 v = mqc_decode(mqc);
551                 v = (v << 1) | mqc_decode(mqc);
552                 v = (v << 1) | mqc_decode(mqc);
553                 v = (v << 1) | mqc_decode(mqc);
554                 /*
555                 if (v!=0xa) {
556                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
557                 } 
558                 */
559         }
560 }                               /* VSC and  BYPASS by Antonin */
561
562
563 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
564         int i, j, k;
565         int w, h, l;
566         int passno;
567         int bpno, passtype;
568         int max;
569         int nmsedec = 0;
570         double cumwmsedec = 0;
571         char type = T1_TYPE_MQ;
572         
573         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
574         
575         w = cblk->x1 - cblk->x0;
576         h = cblk->y1 - cblk->y0;
577         l = cblk->z1 - cblk->z0;
578
579         max = 0;
580         for (k = 0; k < l; k++) {
581                 for (j = 0; j < h; j++) {
582                         for (i = 0; i < w; i++) {
583                                 max = int_max(max, int_abs(t1->data[k][j][i]));
584                         }
585                 }
586         }
587         for (k = 0; k <= l; k++) {
588                 for (j = 0; j <= h; j++) {
589                         for (i = 0; i <= w; i++) {
590                                 t1->flags[k][j][i] = 0;
591                                 t1->flagSVR[k][j][i] = 0;
592                         }
593                 }
594         }
595         
596         cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
597         
598         bpno = cblk->numbps - 1;
599         passtype = 2;
600         
601         mqc_reset_enc(mqc);
602         mqc_init_enc(mqc, cblk->data);
603         
604         for (passno = 0; bpno >= 0; passno++) {
605                 opj_tcd_pass_t *pass = &cblk->passes[passno];
606                 int correction = 3;
607                 double tmpwmsedec;
608                 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
609                 
610                 switch (passtype) {
611                         case 0:
612                                 t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
613                                 break;
614                         case 1:
615                                 t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
616                                 break;
617                         case 2:
618                                 t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
619                                 /* code switch SEGMARK (i.e. SEGSYM) */
620                                 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
621                                         mqc_segmark_enc(mqc);
622                                 break;
623                 }
624                 
625                 /* fixed_quality */
626                 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
627                 cumwmsedec += tmpwmsedec;
628                 tile->distotile += tmpwmsedec;
629                 
630                 /* Code switch "RESTART" (i.e. TERMALL) */
631                 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
632                         if (type == T1_TYPE_RAW) {
633                                 mqc_flush(mqc);
634                                 correction = 1;
635                                 /* correction = mqc_bypass_flush_enc(); */
636                         } else {                        /* correction = mqc_restart_enc(); */
637                                 mqc_flush(mqc);
638                                 correction = 1;
639                         }
640                         pass->term = 1;
641                 } else {
642                         if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
643                                 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
644                                 if (type == T1_TYPE_RAW) {
645                                         mqc_flush(mqc);
646                                         correction = 1;
647                                 } else {        
648                                         mqc_flush(mqc);
649                                         correction = 1;
650                                 }
651                                 pass->term = 1;
652                         } else {
653                                 pass->term = 0;
654                         }
655                 }
656                 
657                 if (++passtype == 3) {
658                         passtype = 0;
659                         bpno--;
660                 }
661                 
662                 if (pass->term && bpno > 0) {
663                         type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
664                         if (type == T1_TYPE_RAW)
665                                 mqc_bypass_init_enc(mqc);
666                         else
667                                 mqc_restart_init_enc(mqc);
668                 }
669                 
670                 pass->distortiondec = cumwmsedec;
671                 pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
672                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
673
674                 /* Code-switch "RESET" */
675                 if (cblksty & J3D_CCP_CBLKSTY_RESET)
676                         mqc_reset_enc(mqc);
677         }
678         
679         /* Code switch "ERTERM" (i.e. PTERM) */
680         if (cblksty & J3D_CCP_CBLKSTY_PTERM)
681                 mqc_erterm_enc(mqc);
682         else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
683                 mqc_flush(mqc);
684         
685         cblk->totalpasses = passno;
686 }
687
688 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
689         int i, j, k;
690         int w, h, l;
691         int bpno, passtype;
692         int segno, passno;
693         char type = T1_TYPE_MQ; /* BYPASS mode */
694         opj_raw_t *raw = t1->raw;       /* RAW component */
695         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
696         
697         w = cblk->x1 - cblk->x0;
698         h = cblk->y1 - cblk->y0;
699         l = cblk->z1 - cblk->z0;
700
701     for (k = 0; k < l; k++) {
702                 for (j = 0; j < h; j++) {
703                         for (i = 0; i < w; i++) {
704                                 t1->data[k][j][i] = 0;
705                         }
706                 }
707         }
708         
709         for (k = 0; k <= l; k++) {
710                 for (j = 0; j <= h; j++) {
711                         for (i = 0; i <= w; i++) {
712                                 t1->flags[k][j][i] = 0;
713                                 t1->flagSVR[k][j][i] = 0;
714                         }
715                 }
716         }
717
718         
719         bpno = roishift + cblk->numbps - 1;
720         passtype = 2;
721         
722         mqc_reset_enc(mqc);
723         
724         for (segno = 0; segno < cblk->numsegs; segno++) {
725                 opj_tcd_seg_t *seg = &cblk->segs[segno];
726                 
727                 /* BYPASS mode */
728                 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
729                 if (type == T1_TYPE_RAW) {
730                         raw_init_dec(raw, seg->data, seg->len);
731                 } else {
732                         mqc_init_dec(mqc, seg->data, seg->len);
733                 }
734                 
735                 for (passno = 0; passno < seg->numpasses; passno++) {
736                         switch (passtype) {
737                                 case 0:
738                                         t1_3d_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
739                                         break;
740                                 case 1:
741                                         t1_3d_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
742                                         break;
743                                 case 2:
744                                         t1_3d_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
745                                         break;
746                         }
747                         
748                         if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
749                                 mqc_reset_enc(mqc);
750                         }
751                         if (++passtype == 3) {
752                                 passtype = 0;
753                                 bpno--;
754                         }
755
756                 }
757         }
758 }
759
760 static int t1_3d_init_ctxno_zc(unsigned int f, int orient) {
761         unsigned int h, v, c;
762         unsigned int d2xy, d2xz, d2yz, d3;
763         int n;
764         unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz;
765         n = 0;
766         h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0);
767         v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0);
768         c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0);
769         d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0);
770         d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0);
771         d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0);
772     d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0) 
773                 + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0);
774         
775         switch (orient) {
776                 case 0: /*LLL*/
777                 case 7: /*HHH*/
778                         hvc = h + v + c;
779                         d2 = d2xy + d2xz + d2yz;
780                         if (!hvc) {
781                                 if (!d2) {
782                     n = (!d3) ? 0 : 1;
783                                 } else if (d2 == 1) {
784                                         n = (!d3) ? 2 : 3;
785                                 } else {
786                                         n = (!d3) ? 4 : 5;
787                                 }
788                         } else if (hvc == 1) {
789                                 if (!d2) {
790                     n = (!d3) ? 6 : 7;
791                                 } else if (d2 == 1) {
792                                         n = (!d3) ? 8 : 9;
793                                 } else {
794                                         n = 10;
795                                 }
796                         } else if (hvc == 2) {
797                                 if (!d2) {
798                     n = (!d3) ? 11 : 12;
799                                 } else {
800                                         n = 13;
801                                 }
802                         } else if (hvc == 3) {
803                                 n = 14;
804                         } else {
805                                 n = 15;
806                         }
807                         break;
808                 /*LHL, HLL, LLH*/
809                 case 1:
810                 case 2:
811                 case 4:
812                         hc = h + c;
813                         d2xy2yz = d2xy + d2yz;
814             if (!hc) {
815                                 if (!v) {
816                                         if (!d2xy) {
817                                                 n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3);     
818                                         } else if (d2xy == 1) {
819                                                 n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6;   
820                                         } else { /*>=2*/
821                         n = 7;
822                                         }
823                                 } else {
824                                         n = (v == 1) ? 8 : 9; /* =1 or =2*/
825                                 } 
826                         } else if (hc == 1) {
827                                 n = (!v) ? ( (!d2xy) ? ( (!d2xy2yz) ? ( (!d3) ? 10 : 11) : (12) ) : (13) ) : (14);
828                         } else { /*if (hc >= 2)*/
829                                 n = 15;
830                         }
831                         break;
832                 /*HLH, HHL, LHH*/
833                 case 3:
834                 case 5:
835                 case 6:
836                         hc = h + c;
837                         d2xy2xz = d2xy + d2xz;
838                         if (!v) {
839                                 if (!d2xz) {
840                                         if (!hc && !d2xy2xz) {
841                                                 n = (!d3) ? 0 : 1;
842                                         } else if (hc == 1) {
843                                                 n = (!d2xy2xz) ?  2 : 3;
844                                         } else { /*if >= 2*/
845                                                 n = 4;
846                                         }
847                                 } else if ( d2xz>=1 && !hc ) {
848                                         n = 5;
849                                 } else if ( hc>=1 ) {
850                                         n = (d2xz==1) ? 6 : 7;
851                                 } 
852                         } else if (v == 1) {
853                                 if (!d2xz) {
854                                         n = (!hc) ? 8 : 9;
855                                 } else if (d2xz == 1) {
856                                         n = (!hc) ? 10 : 11;
857                                 } else if (d2xz == 2) {
858                                         n = (!hc) ? 12 : 13;
859                                 } else { /* if (d2xz >= 3) {*/
860                                         n = 14;
861                                 }
862                         } else if (v == 2) {
863                                 n = 15;
864                         } 
865                         break;
866         }
867         
868         return (T1_3D_CTXNO_ZC + n);
869 }
870
871 static int t1_3d_init_ctxno_sc(unsigned int f) {
872         int hc, vc, cc;
873         int n = 0;
874
875         hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) 
876                                         + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) 
877                 - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) 
878                                         + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
879         
880         vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) 
881                                         + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) 
882                 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) 
883                                         + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
884         
885         cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) 
886                                         + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) 
887                 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) 
888                                         + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
889         if (hc < 0) {
890                 hc = -hc;
891                 vc = -vc;
892                 cc = -cc;
893         }
894
895         if (!hc) {
896                 if (!vc) 
897                         n = (!cc) ? 0 : 1;
898                 else if (vc == -1)
899                         n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
900                 else if (vc == 1)
901                         n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
902         } else if (hc == 1) {
903                 if (!vc)
904                         n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
905                 else if (vc == 1)
906                         n = (!cc) ? 4 : ( (cc>0) ? 5 : 3);
907                 else if (vc == -1)
908                         n = (!cc) ? 2 : 3;
909         } else if (hc == -1) {
910                 if (!vc)
911                         n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
912                 else if (vc == 1)
913                         n = (!cc) ? 2 : 3;
914                 else if (vc == -1)
915                         n = (!cc) ? 4 : ( (cc<0) ? 5 : 3);
916         }
917         
918         return (T1_3D_CTXNO_SC + n);
919 }
920
921 static int t1_3d_init_ctxno_mag(unsigned int f, int f2) {
922         int n;
923         if (!(f2 & T1_3D_REFINE))       /*First refinement for this coefficient (no previous refinement)*/
924                 n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0;
925         else
926                 n = 2;
927         
928         return (T1_3D_CTXNO_MAG + n);
929 }
930
931 static int t1_3d_init_spb(unsigned int f) {
932         int hc, vc, cc;
933         int n = 0;
934         
935         hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E ) 
936                                         + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1) 
937                 - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) ) 
938                                         + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
939         
940         vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N) 
941                                         + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1) 
942                 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N)) 
943                                         + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
944         
945         cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC) 
946                                         + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1) 
947                 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F)) 
948                                         + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
949         
950         n = ((hc + vc + cc) < 0); 
951         
952         return n;
953 }
954
955 static void t1_3d_init_luts(opj_t1_3d_t *t1) {
956         int i;
957         double u, v, t;
958         /*for (j = 0; j < 4; j++) {
959                 for (i = 0; i < 256; ++i) {
960                         t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j);
961                 }
962         }
963         for (i = 0; i < 4096; i++) {
964                 t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4);
965         }
966         for (j = 0; j < 2; j++) {
967                 for (i = 0; i < 2048; ++i) {
968                         t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i);
969                 }
970         }
971         for (i = 0; i < 4096; ++i) {
972                 t1->lut_spb[i] = t1_3d_init_spb(i << 4);
973         }*/
974         /* FIXME FIXME FIXME */
975         for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
976                 t = i / pow(2, T1_NMSEDEC_FRACBITS);
977                 u = t;
978                 v = t - 1.5;
979                 t1->lut_nmsedec_sig[i] = 
980                         int_max(0, 
981                         (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
982                 t1->lut_nmsedec_sig0[i] =
983                         int_max(0,
984                         (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
985                 u = t - 1.0;
986                 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
987                         v = t - 1.5;
988                 } else {
989                         v = t - 0.5;
990                 }
991                 t1->lut_nmsedec_ref[i] =
992                         int_max(0,
993                         (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
994                 t1->lut_nmsedec_ref0[i] =
995                         int_max(0,
996                         (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
997         }
998 }
999
1000 /* ----------------------------------------------------------------------- */
1001
1002 opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo) {
1003         opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t));
1004         if(t1) {
1005                 t1->cinfo = cinfo;
1006                 /* create MQC and RAW handles */
1007                 t1->mqc = mqc_create();
1008                 t1->raw = raw_create();
1009                 /* initialize the look-up tables of the Tier-1 coder/decoder */
1010                 t1_3d_init_luts(t1);
1011         }
1012         return t1;
1013 }
1014
1015 void t1_3d_destroy(opj_t1_3d_t *t1) {
1016         if(t1) {
1017                 /* destroy MQC and RAW handles */
1018                 mqc_destroy(t1->mqc);
1019                 raw_destroy(t1->raw);
1020                 opj_free(t1);
1021         }
1022 }
1023
1024 void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1025         int compno, resno, bandno, precno, cblkno;
1026         int x, y, z, i, j, k, orient;
1027         int level[3];
1028         tile->distotile = 0;            /* fixed_quality */
1029
1030         for (compno = 0; compno < tile->numcomps; compno++) {
1031                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1032
1033                 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1034                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1035
1036                         for (bandno = 0; bandno < res->numbands; bandno++) {
1037                                 opj_tcd_band_t *band = &res->bands[bandno];
1038
1039                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1040                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1041
1042                                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1043                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1044
1045                                                 if (band->bandno == 0) {
1046                                                         x = cblk->x0 - band->x0;
1047                                                         y = cblk->y0 - band->y0;
1048                                                         z = cblk->z0 - band->z0;
1049                                                 } else if (band->bandno == 1) {
1050                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1051                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1052                                                         y = cblk->y0 - band->y0;
1053                                                         z = cblk->z0 - band->z0;
1054                                                 } else if (band->bandno == 2) {
1055                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1056                                                         x = cblk->x0 - band->x0;
1057                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1058                                                         z = cblk->z0 - band->z0;
1059                                                 } else if (band->bandno == 3) {         
1060                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1061                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1062                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1063                                                         z = cblk->z0 - band->z0;
1064                                                 } else if (band->bandno == 4) {
1065                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1066                                                         x = cblk->x0 - band->x0;
1067                                                         y = cblk->y0 - band->y0;
1068                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1069                                                 } else if (band->bandno == 5) {
1070                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1071                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1072                                                         y = cblk->y0 - band->y0;
1073                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1074                                                 } else if (band->bandno == 6) {
1075                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1076                                                         x = cblk->x0 - band->x0;
1077                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1078                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1079                                                 } else if (band->bandno == 7) {         
1080                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1081                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1082                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1083                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1084                                                 }
1085
1086                                                 if (tcp->tccps[compno].reversible == 1) {
1087                                                         for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1088                                                                 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1089                                     for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1090                                         t1->data[k][j][i] = 
1091                                                                                 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1092                                                                         }
1093                                                                 }
1094                                                         }
1095                                                 } else if (tcp->tccps[compno].reversible == 0) {
1096                                                         for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1097                                                                 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1098                                     for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1099                                         t1->data[k][j][i] = fix_mul(
1100                                                                                 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1101                                                                                 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
1102                                                                         }
1103                                                                 }
1104                                                         }
1105                                                 }
1106                                                 orient = band->bandno;  /* FIXME */
1107                                                 for (i = 0; i < 3; i++) 
1108                                                         level[i] = tilec->numresolution[i] - 1 - resno;
1109
1110                                                 t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1111                                                         
1112                                         } /* cblkno */
1113                                 } /* precno */
1114                         } /* bandno */
1115                 } /* resno  */
1116         } /* compno  */
1117 }
1118
1119 void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1120         int compno, resno, bandno, precno, cblkno;
1121         
1122         for (compno = 0; compno < tile->numcomps; compno++) {
1123                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1124
1125                 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1126                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1127
1128                         for (bandno = 0; bandno < res->numbands; bandno++) {
1129                                 opj_tcd_band_t *band = &res->bands[bandno];
1130
1131                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1132                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1133
1134                                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1135                                                 int x, y, z, i, j, k, orient;
1136                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1137
1138                                                 orient = band->bandno;  /* FIXME */
1139
1140                                                 /*fprintf(stdout,"[INFO] t1_3d_decode_cblk(t1, cblk, orient(%d), tcp->tccps[compno].roishift (%d), tcp->tccps[compno].cblksty (%d));\n",orient,tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);*/
1141                                                 t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1142
1143                                                 if (band->bandno == 0) {
1144                                                         x = cblk->x0 - band->x0;
1145                                                         y = cblk->y0 - band->y0;
1146                                                         z = cblk->z0 - band->z0;
1147                                                 } else if (band->bandno == 1) {
1148                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1149                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1150                                                         y = cblk->y0 - band->y0;
1151                                                         z = cblk->z0 - band->z0;
1152                                                 } else if (band->bandno == 2) {
1153                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1154                                                         x = cblk->x0 - band->x0;
1155                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1156                                                         z = cblk->z0 - band->z0;
1157                                                 } else if (band->bandno == 3) {         
1158                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1159                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1160                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1161                                                         z = cblk->z0 - band->z0;
1162                                                 } else if (band->bandno == 4) {
1163                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1164                                                         x = cblk->x0 - band->x0;
1165                                                         y = cblk->y0 - band->y0;
1166                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1167                                                 } else if (band->bandno == 5) {
1168                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1169                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1170                                                         y = cblk->y0 - band->y0;
1171                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1172                                                 } else if (band->bandno == 6) {
1173                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1174                                                         x = cblk->x0 - band->x0;
1175                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1176                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1177                                                 } else if (band->bandno == 7) {         
1178                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1179                                                         x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1180                                                         y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1181                                                         z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1182                                                 }
1183                                         
1184                                                 if (tcp->tccps[compno].roishift) {
1185                                                         int thresh, val, mag;
1186                                                         thresh = 1 << tcp->tccps[compno].roishift;
1187                                                         for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1188                                                                 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1189                                                                         for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1190                                                                                 val = t1->data[k][j][i];
1191                                                                                 mag = int_abs(val);
1192                                                                                 if (mag >= thresh) {
1193                                                                                         mag >>= tcp->tccps[compno].roishift;
1194                                                                                         t1->data[k][j][i] = val < 0 ? -mag : mag;
1195                                                                                 }
1196                                                                         }
1197                                                                 }
1198                                                         }
1199                                                 }
1200                                                 
1201                                                 if (tcp->tccps[compno].reversible == 1) {
1202                                                         for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1203                                                                 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1204                                                                         for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1205                                                                                 int tmp = t1->data[k][j][i];
1206                                                                                 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
1207                                                                         }
1208                                                                 }
1209                                                         }
1210                                                 } else {                /* if (tcp->tccps[compno].reversible == 0) */
1211                                                         for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1212                                                                 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1213                                                                         for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1214                                                                                 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1215                                                                                 if (t1->data[k][j][i] >> 1 == 0) {
1216                                                                                         tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1217                                                                                 } else {
1218                                                                                         int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1219                                                                                         tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);
1220                                                                                 }
1221                                                                         }
1222                                                                 }
1223                                                         }
1224                                                 }
1225                                         } /* cblkno */
1226                                 } /* precno */
1227                         } /* bandno */
1228                 } /* resno */
1229         } /* compno */
1230 }