3937289b52069fc1fe5d75bf2d10593c2082c60c
[openjpeg.git] / libopenjpeg / t1.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34 #include "t1_luts.h"
35
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
37 /*@{*/
38
39 /** @name Local static functions */
40 /*@{*/
41
42 static char t1_getctxno_zc(int f, int orient);
43 static char t1_getctxno_sc(int f);
44 static char t1_getctxno_mag(int f);
45 static char t1_getspb(int f);
46 static short t1_getnmsedec_sig(int x, int bitpos);
47 static short t1_getnmsedec_ref(int x, int bitpos);
48 static void t1_updateflags(int *fp, int s);
49 /**
50 Encode significant pass
51 */
52 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
53 /**
54 Decode significant pass
55 */
56 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
57 /**
58 Encode significant pass
59 */
60 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
61 /**
62 Decode significant pass
63 */
64 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
65 /**
66 Encode refinement pass
67 */
68 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
69 /**
70 Decode refinement pass
71 */
72 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
73 /**
74 Encode refinement pass
75 */
76 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
77 /**
78 Decode refinement pass
79 */
80 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
81 /**
82 Encode clean-up pass
83 */
84 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
85 /**
86 Decode clean-up pass
87 */
88 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
89 /**
90 Encode clean-up pass
91 */
92 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
93 /**
94 Decode clean-up pass
95 */
96 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
97 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
98 /**
99 Encode 1 code-block
100 @param t1 T1 handle
101 @param cblk Code-block coding parameters
102 @param orient
103 @param compno Component number
104 @param level
105 @param qmfbid
106 @param stepsize
107 @param cblksty Code-block style
108 @param numcomps
109 @param tile
110 */
111 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
112 /**
113 Decode 1 code-block
114 @param t1 T1 handle
115 @param cblk Code-block coding parameters
116 @param orient
117 @param roishift Region of interest shifting value
118 @param cblksty Code-block style
119 */
120 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
121
122 /*@}*/
123
124 /*@}*/
125
126 /* ----------------------------------------------------------------------- */
127
128 static char t1_getctxno_zc(int f, int orient) {
129         return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
130 }
131
132 static char t1_getctxno_sc(int f) {
133         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
134 }
135
136 static char t1_getctxno_mag(int f) {
137         return lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
138 }
139
140 static char t1_getspb(int f) {
141         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
142 }
143
144 static short t1_getnmsedec_sig(int x, int bitpos) {
145         if (bitpos > T1_NMSEDEC_FRACBITS) {
146                 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
147         }
148         
149         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
150 }
151
152 static short t1_getnmsedec_ref(int x, int bitpos) {
153         if (bitpos > T1_NMSEDEC_FRACBITS) {
154                 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
155         }
156
157     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
158 }
159
160 static void t1_updateflags(int *fp, int s) {
161         int *np = fp - (T1_MAXCBLKW + 2);
162         int *sp = fp + (T1_MAXCBLKW + 2);
163         np[-1] |= T1_SIG_SE;
164         np[1] |= T1_SIG_SW;
165         sp[-1] |= T1_SIG_NE;
166         sp[1] |= T1_SIG_NW;
167         *np |= T1_SIG_S;
168         *sp |= T1_SIG_N;
169         fp[-1] |= T1_SIG_E;
170         fp[1] |= T1_SIG_W;
171         if (s) {
172                 *np |= T1_SGN_S;
173                 *sp |= T1_SGN_N;
174                 fp[-1] |= T1_SGN_E;
175                 fp[1] |= T1_SGN_W;
176         }
177 }
178
179 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
180         int v, flag;
181         
182         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
183         
184         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
185         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
186                 v = int_abs(*dp) & one ? 1 : 0;
187                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
188                         mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));       /* ESSAI */
189                         mqc_bypass_enc(mqc, v);
190                 } else {
191                         mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
192                         mqc_encode(mqc, v);
193                 }
194                 if (v) {
195                         v = *dp < 0 ? 1 : 0;
196                         *nmsedec +=     t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
197                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
198                                 mqc_setcurctx(mqc, t1_getctxno_sc(flag));       /* ESSAI */
199                                 mqc_bypass_enc(mqc, v);
200                         } else {
201                                 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
202                                 mqc_encode(mqc, v ^ t1_getspb(flag));
203                         }
204                         t1_updateflags(fp, v);
205                         *fp |= T1_SIG;
206                 }
207                 *fp |= T1_VISIT;
208         }
209 }
210
211 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
212         int v, flag;
213         
214         opj_raw_t *raw = t1->raw;       /* RAW component */
215         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
216         
217         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
218         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
219                 if (type == T1_TYPE_RAW) {
220                         if (raw_decode(raw)) {
221                                 v = raw_decode(raw);    /* ESSAI */
222                                 *dp = v ? -oneplushalf : oneplushalf;
223                                 t1_updateflags(fp, v);
224                                 *fp |= T1_SIG;
225                         }
226                 } else {
227                         mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
228                         if (mqc_decode(mqc)) {
229                                 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
230                                 v = mqc_decode(mqc) ^ t1_getspb(flag);
231                                 *dp = v ? -oneplushalf : oneplushalf;
232                                 t1_updateflags(fp, v);
233                                 *fp |= T1_SIG;
234                         }
235                 }
236                 *fp |= T1_VISIT;
237         }
238 }                               /* VSC and  BYPASS by Antonin */
239
240 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
241         int i, j, k, one, vsc;
242         *nmsedec = 0;
243         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
244         for (k = 0; k < h; k += 4) {
245                 for (i = 0; i < w; i++) {
246                         for (j = k; j < k + 4 && j < h; j++) {
247                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
248                                 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
249                         }
250                 }
251         }
252 }
253
254 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
255         int i, j, k, one, half, oneplushalf, vsc;
256         one = 1 << bpno;
257         half = one >> 1;
258         oneplushalf = one | half;
259         for (k = 0; k < h; k += 4) {
260                 for (i = 0; i < w; i++) {
261                         for (j = k; j < k + 4 && j < h; j++) {
262                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
263                                 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
264                         }
265                 }
266         }
267 }                               /* VSC and  BYPASS by Antonin */
268
269 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
270         int v, flag;
271         
272         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
273         
274         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
275         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
276                 *nmsedec += t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
277                 v = int_abs(*dp) & one ? 1 : 0;
278                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
279                         mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
280                         mqc_bypass_enc(mqc, v);
281                 } else {
282                         mqc_setcurctx(mqc, t1_getctxno_mag(flag));
283                         mqc_encode(mqc, v);
284                 }
285                 *fp |= T1_REFINE;
286         }
287 }
288
289 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
290         int v, t, flag;
291         
292         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
293         opj_raw_t *raw = t1->raw;       /* RAW component */
294         
295         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
296         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
297                 if (type == T1_TYPE_RAW) {
298                         mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
299                         v = raw_decode(raw);
300                 } else {
301                         mqc_setcurctx(mqc, t1_getctxno_mag(flag));
302                         v = mqc_decode(mqc);
303                 }
304                 t = v ? poshalf : neghalf;
305                 *dp += *dp < 0 ? -t : t;
306                 *fp |= T1_REFINE;
307         }
308 }                               /* VSC and  BYPASS by Antonin  */
309
310 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
311         int i, j, k, one, vsc;
312         *nmsedec = 0;
313         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
314         for (k = 0; k < h; k += 4) {
315                 for (i = 0; i < w; i++) {
316                         for (j = k; j < k + 4 && j < h; j++) {
317                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
318                                 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
319                         }
320                 }
321         }
322 }
323
324 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
325         int i, j, k, one, poshalf, neghalf;
326         int vsc;
327         one = 1 << bpno;
328         poshalf = one >> 1;
329         neghalf = bpno > 0 ? -poshalf : -1;
330         for (k = 0; k < h; k += 4) {
331                 for (i = 0; i < w; i++) {
332                         for (j = k; j < k + 4 && j < h; j++) {
333                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
334                                 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
335                         }
336                 }
337         }
338 }                               /* VSC and  BYPASS by Antonin */
339
340 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
341         int v, flag;
342         
343         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
344         
345         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
346         if (partial) {
347                 goto LABEL_PARTIAL;
348         }
349         if (!(*fp & (T1_SIG | T1_VISIT))) {
350                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
351                 v = int_abs(*dp) & one ? 1 : 0;
352                 mqc_encode(mqc, v);
353                 if (v) {
354 LABEL_PARTIAL:
355                         *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
356                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
357                         v = *dp < 0 ? 1 : 0;
358                         mqc_encode(mqc, v ^ t1_getspb(flag));
359                         t1_updateflags(fp, v);
360                         *fp |= T1_SIG;
361                 }
362         }
363         *fp &= ~T1_VISIT;
364 }
365
366 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
367         int v, flag;
368         
369         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
370         
371         flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
372         if (partial) {
373                 goto LABEL_PARTIAL;
374         }
375         if (!(flag & (T1_SIG | T1_VISIT))) {
376                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
377                 if (mqc_decode(mqc)) {
378 LABEL_PARTIAL:
379                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
380                         v = mqc_decode(mqc) ^ t1_getspb(flag);
381                         *dp = v ? -oneplushalf : oneplushalf;
382                         t1_updateflags(fp, v);
383                         *fp |= T1_SIG;
384                 }
385         }
386         *fp &= ~T1_VISIT;
387 }                               /* VSC and  BYPASS by Antonin */
388
389 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
390         int i, j, k, one, agg, runlen, vsc;
391         
392         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
393         
394         *nmsedec = 0;
395         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
396         for (k = 0; k < h; k += 4) {
397                 for (i = 0; i < w; i++) {
398                         if (k + 3 < h) {
399                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
400                                         agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
401                                                 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
402                                                 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
403                                                 || (t1->flags[1 + k + 3][1 + i] 
404                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
405                                 } else {
406                                         agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
407                                                 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
408                                                 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
409                                                 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
410                                 }
411                         } else {
412                                 agg = 0;
413                         }
414                         if (agg) {
415                                 for (runlen = 0; runlen < 4; runlen++) {
416                                         if (int_abs(t1->data[k + runlen][i]) & one)
417                                                 break;
418                                 }
419                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
420                                 mqc_encode(mqc, runlen != 4);
421                                 if (runlen == 4) {
422                                         continue;
423                                 }
424                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
425                                 mqc_encode(mqc, runlen >> 1);
426                                 mqc_encode(mqc, runlen & 1);
427                         } else {
428                                 runlen = 0;
429                         }
430                         for (j = k + runlen; j < k + 4 && j < h; j++) {
431                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
432                                 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
433                         }
434                 }
435         }
436 }
437
438 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
439         int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
440         int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
441         
442         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
443         
444         one = 1 << bpno;
445         half = one >> 1;
446         oneplushalf = one | half;
447         for (k = 0; k < h; k += 4) {
448                 for (i = 0; i < w; i++) {
449                         if (k + 3 < h) {
450                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
451                                         agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
452                                                 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
453                                                 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
454                                                 || (t1->flags[1 + k + 3][1 + i] 
455                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
456                                 } else {
457                                         agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
458                                                 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
459                                                 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
460                                                 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
461                                 }
462                         } else {
463                                 agg = 0;
464                         }
465                         if (agg) {
466                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
467                                 if (!mqc_decode(mqc)) {
468                                         continue;
469                                 }
470                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
471                                 runlen = mqc_decode(mqc);
472                                 runlen = (runlen << 1) | mqc_decode(mqc);
473                         } else {
474                                 runlen = 0;
475                         }
476                         for (j = k + runlen; j < k + 4 && j < h; j++) {
477                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
478                                 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
479                         }
480                 }
481         }
482         if (segsym) {
483                 int v = 0;
484                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
485                 v = mqc_decode(mqc);
486                 v = (v << 1) | mqc_decode(mqc);
487                 v = (v << 1) | mqc_decode(mqc);
488                 v = (v << 1) | mqc_decode(mqc);
489                 /*
490                 if (v!=0xa) {
491                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
492                 } 
493                 */
494         }
495 }                               /* VSC and  BYPASS by Antonin */
496
497
498 /** mod fixed_quality */
499 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps)        {
500         double w1, w2, wmsedec;
501         if (qmfbid == 1) {
502                 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
503                 w2 = dwt_getnorm(level, orient);
504         } else {                        /* if (qmfbid == 0) */
505                 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
506                 w2 = dwt_getnorm_real(level, orient);
507         }
508         wmsedec = w1 * w2 * stepsize * (1 << bpno);
509         wmsedec *= wmsedec * nmsedec / 8192.0;
510         
511         return wmsedec;
512 }
513
514 /** mod fixed_quality */
515 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
516         int i, j;
517         int w, h;
518         int passno;
519         int bpno, passtype;
520         int max;
521         int nmsedec = 0;
522         double cumwmsedec = 0;
523         char type = T1_TYPE_MQ;
524         
525         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
526         
527         w = cblk->x1 - cblk->x0;
528         h = cblk->y1 - cblk->y0;
529         
530         max = 0;
531         for (j = 0; j < h; j++) {
532                 for (i = 0; i < w; i++) {
533                         max = int_max(max, int_abs(t1->data[j][i]));
534                 }
535         }
536         
537         cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
538         
539         for (i = 0; i <= h; ++i) {
540                 memset(&t1->flags[i], 0, (w+1) * sizeof(int));
541         }
542         
543         bpno = cblk->numbps - 1;
544         passtype = 2;
545         
546         mqc_resetstates(mqc);
547         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
548         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
549         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
550         mqc_init_enc(mqc, cblk->data);
551         
552         for (passno = 0; bpno >= 0; passno++) {
553                 opj_tcd_pass_t *pass = &cblk->passes[passno];
554                 int correction = 3;
555                 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
556                 
557                 switch (passtype) {
558                         case 0:
559                                 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
560                                 break;
561                         case 1:
562                                 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
563                                 break;
564                         case 2:
565                                 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
566                                 /* code switch SEGMARK (i.e. SEGSYM) */
567                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
568                                         mqc_segmark_enc(mqc);
569                                 break;
570                 }
571                 
572                 /* fixed_quality */
573                 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
574                 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
575                 
576                 /* Code switch "RESTART" (i.e. TERMALL) */
577                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
578                         if (type == T1_TYPE_RAW) {
579                                 mqc_flush(mqc);
580                                 correction = 1;
581                                 /* correction = mqc_bypass_flush_enc(); */
582                         } else {                        /* correction = mqc_restart_enc(); */
583                                 mqc_flush(mqc);
584                                 correction = 1;
585                         }
586                         pass->term = 1;
587                 } else {
588                         if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
589                                 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
590                                 if (type == T1_TYPE_RAW) {
591                                         mqc_flush(mqc);
592                                         correction = 1;
593                                         /* correction = mqc_bypass_flush_enc(); */
594                                 } else {                /* correction = mqc_restart_enc(); */
595                                         mqc_flush(mqc);
596                                         correction = 1;
597                                 }
598                                 pass->term = 1;
599                         } else {
600                                 pass->term = 0;
601                         }
602                 }
603                 
604                 if (++passtype == 3) {
605                         passtype = 0;
606                         bpno--;
607                 }
608                 
609                 if (pass->term && bpno > 0) {
610                         type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
611                         if (type == T1_TYPE_RAW)
612                                 mqc_bypass_init_enc(mqc);
613                         else
614                                 mqc_restart_init_enc(mqc);
615                 }
616                 
617                 pass->distortiondec = cumwmsedec;
618                 pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
619                 
620                 /* Code-switch "RESET" */
621                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
622                         mqc_reset_enc(mqc);
623         }
624         
625         /* Code switch "ERTERM" (i.e. PTERM) */
626         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
627                 mqc_erterm_enc(mqc);
628         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
629                 mqc_flush(mqc);
630         
631         cblk->totalpasses = passno;
632
633         for (passno = 0; passno<cblk->totalpasses; passno++) {
634                 opj_tcd_pass_t *pass = &cblk->passes[passno];
635                 if (pass->rate > mqc_numbytes(mqc))
636                         pass->rate = mqc_numbytes(mqc);
637                 /*Preventing generation of FF as last data byte of a pass*/
638                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
639                         pass->rate--;
640                 }
641                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);             
642         }
643 }
644
645 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
646         int i, w, h;
647         int bpno, passtype;
648         int segno, passno;
649         char type = T1_TYPE_MQ; /* BYPASS mode */
650         
651         opj_raw_t *raw = t1->raw;       /* RAW component */
652         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
653         
654         w = cblk->x1 - cblk->x0;
655         h = cblk->y1 - cblk->y0;
656         
657         for (i = 0; i <= h; ++i) {
658                 memset(&t1->flags[i], 0, (w + 1) * sizeof(int));
659         }
660         
661         for (i = 0; i < h; ++i) {
662                 memset(&t1->data[i], 0, w * sizeof(int));
663         }
664         
665         bpno = roishift + cblk->numbps - 1;
666         passtype = 2;
667         
668         mqc_resetstates(mqc);
669         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
670         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
671         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
672         
673         for (segno = 0; segno < cblk->numsegs; segno++) {
674                 opj_tcd_seg_t *seg = &cblk->segs[segno];
675                 
676                 /* BYPASS mode */
677                 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
678                 if (type == T1_TYPE_RAW) {
679                         raw_init_dec(raw, seg->data, seg->len);
680                 } else {
681                         mqc_init_dec(mqc, seg->data, seg->len);
682                 }
683                 
684                 for (passno = 0; passno < seg->numpasses; passno++) {
685                         switch (passtype) {
686                                 case 0:
687                                         t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
688                                         break;
689                                 case 1:
690                                         t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
691                                         break;
692                                 case 2:
693                                         t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
694                                         break;
695                         }
696                         
697                         if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
698                                 mqc_resetstates(mqc);
699                                 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);                         
700                                 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
701                                 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
702                         }
703                         if (++passtype == 3) {
704                                 passtype = 0;
705                                 bpno--;
706                         }
707                 }
708         }
709 }
710
711 /* ----------------------------------------------------------------------- */
712
713 opj_t1_t* t1_create(opj_common_ptr cinfo) {
714         opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
715         if(t1) {
716                 t1->cinfo = cinfo;
717                 /* create MQC and RAW handles */
718                 t1->mqc = mqc_create();
719                 t1->raw = raw_create();
720         }
721         return t1;
722 }
723
724 void t1_destroy(opj_t1_t *t1) {
725         if(t1) {
726                 /* destroy MQC and RAW handles */
727                 mqc_destroy(t1->mqc);
728                 raw_destroy(t1->raw);
729                 opj_free(t1);
730         }
731 }
732
733 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
734         int compno, resno, bandno, precno, cblkno;
735
736         tile->distotile = 0;            /* fixed_quality */
737
738         for (compno = 0; compno < tile->numcomps; compno++) {
739                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
740
741                 for (resno = 0; resno < tilec->numresolutions; resno++) {
742                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
743
744                         for (bandno = 0; bandno < res->numbands; bandno++) {
745                                 opj_tcd_band_t *band = &res->bands[bandno];
746
747                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
748                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
749
750                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
751                                                 int x, y, w, i, j, orient;
752                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
753
754                                                 x = cblk->x0 - band->x0;
755                                                 y = cblk->y0 - band->y0;
756                                                 if (band->bandno & 1) {
757                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
758                                                         x += pres->x1 - pres->x0;
759                                                 }
760                                                 if (band->bandno & 2) {
761                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
762                                                         y += pres->y1 - pres->y0;
763                                                 }
764                                                 
765                                                 w = tilec->x1 - tilec->x0;
766                                                 if (tcp->tccps[compno].qmfbid == 1) {
767                                                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
768                                                                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
769                                                                         t1->data[j][i] = 
770                                                                                 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
771                                                                 }
772                                                         }
773                                                 } else {                /* if (tcp->tccps[compno].qmfbid == 0) */
774                                                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
775                                                                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
776                                                                         t1->data[j][i] = 
777                                                                                 fix_mul(
778                                                                                 tilec->data[x + i + (y + j) * w],
779                                                                                 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
780                                                                 }
781                                                         }
782                                                 }
783                                                 orient = band->bandno;  /* FIXME */
784                                                 if (orient == 2) {
785                                                         orient = 1;
786                                                 } else if (orient == 1) {
787                                                         orient = 2;
788                                                 }
789
790                                                 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
791
792                                         } /* cblkno */
793                                 } /* precno */
794                         } /* bandno */
795                 } /* resno  */
796         } /* compno  */
797 }
798
799 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
800         int compno, resno, bandno, precno, cblkno;
801
802         for (compno = 0; compno < tile->numcomps; compno++) {
803                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
804
805                 for (resno = 0; resno < tilec->numresolutions; resno++) {
806                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
807
808                         for (bandno = 0; bandno < res->numbands; bandno++) {
809                                 opj_tcd_band_t *band = &res->bands[bandno];
810
811                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
812                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
813
814                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
815                                                 int x, y, w, i, j, orient;
816                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
817
818                                                 orient = band->bandno;  /* FIXME */
819                                                 if (orient == 2) {
820                                                         orient = 1;
821                                                 } else if (orient == 1) {
822                                                         orient = 2;
823                                                 }
824                                                 
825                                                 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
826
827                                                 x = cblk->x0 - band->x0;
828                                                 y = cblk->y0 - band->y0;
829                                                 if (band->bandno & 1) {
830                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
831                                                         x += pres->x1 - pres->x0;
832                                                 }
833                                                 if (band->bandno & 2) {
834                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
835                                                         y += pres->y1 - pres->y0;
836                                                 }
837                                                 
838                                                 if (tcp->tccps[compno].roishift) {
839                                                         int thresh = 1 << tcp->tccps[compno].roishift;
840                                                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
841                                                                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
842                                                                         int val = t1->data[j][i];
843                                                                         int mag = int_abs(val);
844                                                                         if (mag >= thresh) {
845                                                                                 mag >>= tcp->tccps[compno].roishift;
846                                                                                 t1->data[j][i] = val < 0 ? -mag : mag;
847                                                                         }
848                                                                 }
849                                                         }
850                                                 }
851                                                 
852                                                 w = tilec->x1 - tilec->x0;
853                                                 if (tcp->tccps[compno].qmfbid == 1) {
854                                                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
855                                                                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
856                                                                         tilec->data[x + i + (y + j) * w] = t1->data[j][i]/2;
857                                                                 }
858                                                         }
859                                                 } else {                /* if (tcp->tccps[compno].qmfbid == 0) */
860                                                         for (j = 0; j < cblk->y1 - cblk->y0; j++) {
861                                                                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
862                                                                         if (t1->data[j][i] >> 1 == 0) {
863                                                                                 tilec->data[x + i + (y + j) * w] = 0;
864                                                                         } else {
865                                                                                 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
866                                                                                 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
867                                                                                 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);
868                                                                         }
869                                                                 }
870                                                         }
871                                                 }
872                                         } /* cblkno */
873                                 } /* precno */
874                         } /* bandno */
875                 } /* resno */
876         } /* compno */
877 }
878
879