At lines 577, 692 and 699, zeroing of effective region of code-block rather than...
[openjpeg.git] / libopenjpeg / t1.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2002-2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "t1.h"
30 #include "j2k.h"
31 #include "mqc.h"
32 #include "raw.h"                /* Antonin */
33 #include "int.h"
34 #include "mct.h"
35 #include "dwt.h"
36 #include "fix.h"
37 #include <stdio.h>
38 #include <math.h>
39 #include <stdlib.h>
40
41 #define T1_MAXCBLKW 1024
42 #define T1_MAXCBLKH 1024
43
44 #define T1_SIG_NE 0x0001
45 #define T1_SIG_SE 0x0002
46 #define T1_SIG_SW 0x0004
47 #define T1_SIG_NW 0x0008
48 #define T1_SIG_N 0x0010
49 #define T1_SIG_E 0x0020
50 #define T1_SIG_S 0x0040
51 #define T1_SIG_W 0x0080
52 #define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)
53 #define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)
54
55 #define T1_SGN_N 0x0100
56 #define T1_SGN_E 0x0200
57 #define T1_SGN_S 0x0400
58 #define T1_SGN_W 0x0800
59 #define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)
60
61 #define T1_SIG 0x1000
62 #define T1_REFINE 0x2000
63 #define T1_VISIT 0x4000
64
65 #define T1_NUMCTXS_AGG 1
66 #define T1_NUMCTXS_ZC 9
67 #define T1_NUMCTXS_MAG 3
68 #define T1_NUMCTXS_SC 5
69 #define T1_NUMCTXS_UNI 1
70
71 #define T1_CTXNO_AGG 0
72 #define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
73 #define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
74 #define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
75 #define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC)
76 #define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
77
78 #define T1_NMSEDEC_BITS 7
79 #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
80
81 /* add TONY */
82 #define T1_TYPE_MQ 0
83 #define T1_TYPE_RAW 1
84 /* dda */
85
86 static int t1_lut_ctxno_zc[1024];
87 static int t1_lut_ctxno_sc[256];
88 static int t1_lut_ctxno_mag[4096];
89 static int t1_lut_spb[256];
90 static int t1_lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
91 static int t1_lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
92 static int t1_lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
93 static int t1_lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
94
95 static int t1_data[T1_MAXCBLKH][T1_MAXCBLKW];
96 static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];
97
98 int t1_getctxno_zc(int f, int orient)
99 {
100   return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
101 }
102
103 int t1_getctxno_sc(int f)
104 {
105   return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
106 }
107
108 int t1_getctxno_mag(int f)
109 {
110   return t1_lut_ctxno_mag[(f & T1_SIG_OTH) |
111                           (((f & T1_REFINE) != 0) << 11)];
112 }
113
114 int t1_getspb(int f)
115 {
116   return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
117 }
118
119 int t1_getnmsedec_sig(int x, int bitpos)
120 {
121   if (bitpos > T1_NMSEDEC_FRACBITS)
122     return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
123                               ((1 << T1_NMSEDEC_BITS) - 1)];
124   else
125     return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
126 }
127
128 int t1_getnmsedec_ref(int x, int bitpos)
129 {
130   if (bitpos > T1_NMSEDEC_FRACBITS)
131     return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
132                               ((1 << T1_NMSEDEC_BITS) - 1)];
133   else
134     return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
135 }
136
137 void t1_updateflags(int *fp, int s)
138 {
139   int *np = fp - (T1_MAXCBLKW + 2);
140   int *sp = fp + (T1_MAXCBLKW + 2);
141   np[-1] |= T1_SIG_SE;
142   np[1] |= T1_SIG_SW;
143   sp[-1] |= T1_SIG_NE;
144   sp[1] |= T1_SIG_NW;
145   *np |= T1_SIG_S;
146   *sp |= T1_SIG_N;
147   fp[-1] |= T1_SIG_E;
148   fp[1] |= T1_SIG_W;
149   if (s) {
150     *np |= T1_SGN_S;
151     *sp |= T1_SGN_N;
152     fp[-1] |= T1_SGN_E;
153     fp[1] |= T1_SGN_W;
154   }
155 }
156
157 void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
158                          int *nmsedec, char type, int vsc)
159 {
160   int v, flag;
161   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
162     : (*fp);
163   if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
164     v = int_abs(*dp) & one ? 1 : 0;
165     if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
166       mqc_setcurctx(t1_getctxno_zc(flag, orient));      /* ESSAI */
167       mqc_bypass_enc(v);
168     } else {
169       mqc_setcurctx(t1_getctxno_zc(flag, orient));
170       mqc_encode(v);
171     }
172     if (v) {
173       v = *dp < 0 ? 1 : 0;
174       *nmsedec +=
175         t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
176       if (type == T1_TYPE_RAW) {        /* BYPASS/LAZY MODE */
177         mqc_setcurctx(t1_getctxno_sc(flag));    /* ESSAI */
178         mqc_bypass_enc(v);
179       } else {
180         mqc_setcurctx(t1_getctxno_sc(flag));
181         mqc_encode(v ^ t1_getspb(flag));
182       }
183       t1_updateflags(fp, v);
184       *fp |= T1_SIG;
185     }
186     *fp |= T1_VISIT;
187   }
188 }
189
190
191
192 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
193                          char type, int vsc)
194 {
195   int v, flag;
196   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
197     : (*fp);
198   if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
199     if (type == T1_TYPE_RAW) {
200       if (raw_decode()) {
201         v = raw_decode();       /* ESSAI */
202         *dp = v ? -oneplushalf : oneplushalf;
203         t1_updateflags(fp, v);
204         *fp |= T1_SIG;
205       }
206     } else {
207       mqc_setcurctx(t1_getctxno_zc(flag, orient));
208       if (mqc_decode()) {
209         mqc_setcurctx(t1_getctxno_sc(flag));
210         v = mqc_decode() ^ t1_getspb(flag);
211         *dp = v ? -oneplushalf : oneplushalf;
212         t1_updateflags(fp, v);
213         *fp |= T1_SIG;
214       }
215     }
216     *fp |= T1_VISIT;
217   }
218 }                               /* VSC and  BYPASS by Antonin */
219
220 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
221                     char type, int cblksty)
222 {
223   int i, j, k, one, vsc;
224   *nmsedec = 0;
225   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
226   for (k = 0; k < h; k += 4) {
227     for (i = 0; i < w; i++) {
228       for (j = k; j < k + 4 && j < h; j++) {
229         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
230                && (j == k + 3 || j == h - 1)) ? 1 : 0;
231         t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],
232                             &t1_data[j][i], orient, bpno, one,
233                             nmsedec, type, vsc);
234       }
235     }
236   }
237 }
238
239 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
240                     int cblksty)
241 {
242   int i, j, k, one, half, oneplushalf, vsc;
243   one = 1 << bpno;
244   half = one >> 1;
245   oneplushalf = one | half;
246   for (k = 0; k < h; k += 4) {
247     for (i = 0; i < w; i++) {
248       for (j = k; j < k + 4 && j < h; j++) {
249         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
250                && (j == k + 3 || j == h - 1)) ? 1 : 0;
251         t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
252                             &t1_data[j][i], 
253
254                             orient, 
255
256                             oneplushalf,
257                             type, vsc);
258       }
259     }
260   }
261 }                               /* VSC and  BYPASS by Antonin */
262
263 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
264                          char type, int vsc)
265 {
266   int v, flag;
267   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
268     : (*fp);
269   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
270     *nmsedec +=
271       t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
272     v = int_abs(*dp) & one ? 1 : 0;
273     if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
274       mqc_setcurctx(t1_getctxno_mag(flag));     /* ESSAI */
275       mqc_bypass_enc(v);
276     } else {
277       mqc_setcurctx(t1_getctxno_mag(flag));
278       mqc_encode(v);
279     }
280     *fp |= T1_REFINE;
281   }
282 }
283
284
285
286 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
287                          char type, int vsc)
288 {
289   int v, t, flag;
290   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
291     : (*fp);
292   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
293     if (type == T1_TYPE_RAW) {
294       mqc_setcurctx(t1_getctxno_mag(flag));     /* ESSAI */
295       v = raw_decode();
296     } else {
297       mqc_setcurctx(t1_getctxno_mag(flag));
298       v = mqc_decode();
299     }
300     t = v ? poshalf : neghalf;
301     *dp += *dp < 0 ? -t : t;
302     *fp |= T1_REFINE;
303   }
304 }                               /* VSC and  BYPASS by Antonin  */
305
306 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
307                     int cblksty)
308 {
309   int i, j, k, one, vsc;
310   *nmsedec = 0;
311   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
312   for (k = 0; k < h; k += 4) {
313     for (i = 0; i < w; i++) {
314       for (j = k; j < k + 4 && j < h; j++) {
315         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
316                && (j == k + 3 || j == h - 1)) ? 1 : 0;
317         t1_enc_refpass_step(&t1_flags[1 + j][1 + i],
318                             &t1_data[j][i], bpno, one, nmsedec, type, vsc);
319       }
320     }
321   }
322 }
323
324 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
325 {
326   int i, j, k, one, poshalf, neghalf;
327   int vsc;
328   one = 1 << bpno;
329   poshalf = one >> 1;
330   neghalf = bpno > 0 ? -poshalf : -1;
331   for (k = 0; k < h; k += 4) {
332     for (i = 0; i < w; i++) {
333       for (j = k; j < k + 4 && j < h; j++) {
334         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
335                && (j == k + 3 || j == h - 1)) ? 1 : 0;
336         t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
337                             &t1_data[j][i], 
338
339                             poshalf, 
340
341                             neghalf, 
342
343                             type, vsc);
344       }
345     }
346   }
347 }                               /* VSC and  BYPASS by Antonin */
348
349 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
350                          int *nmsedec, int partial, int vsc)
351 {
352   int v, flag;
353   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
354     : (*fp);
355   if (partial)
356     goto label_partial;
357   if (!(*fp & (T1_SIG | T1_VISIT))) {
358     mqc_setcurctx(t1_getctxno_zc(flag, orient));
359     v = int_abs(*dp) & one ? 1 : 0;
360     mqc_encode(v);
361     if (v) {
362     label_partial:
363       *nmsedec +=
364         t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
365       mqc_setcurctx(t1_getctxno_sc(flag));
366       v = *dp < 0 ? 1 : 0;
367       mqc_encode(v ^ t1_getspb(flag));
368       t1_updateflags(fp, v);
369       *fp |= T1_SIG;
370     }
371   }
372   *fp &= ~T1_VISIT;
373 }
374
375
376
377
378 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
379                          int partial, int vsc)
380 {
381   int v, flag;
382   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
383     : (*fp);
384   if (partial)
385     goto label_partial;
386   if (!(flag & (T1_SIG | T1_VISIT))) {
387     mqc_setcurctx(t1_getctxno_zc(flag, orient));
388     if (mqc_decode()) {
389     label_partial:
390       mqc_setcurctx(t1_getctxno_sc(flag));
391       v = mqc_decode() ^ t1_getspb(flag);
392       *dp = v ? -oneplushalf : oneplushalf;
393       t1_updateflags(fp, v);
394       *fp |= T1_SIG;
395     }
396   }
397   *fp &= ~T1_VISIT;
398 }                               /* VSC and  BYPASS by Antonin */
399
400 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
401                     int cblksty)
402 {
403   int i, j, k, one, agg, runlen, vsc;
404   *nmsedec = 0;
405   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
406   for (k = 0; k < h; k += 4) {
407     for (i = 0; i < w; i++) {
408       if (k + 3 < h) {
409         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
410           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
411                   || t1_flags[1 + k + 1][1 +
412                                          i] & (T1_SIG | T1_VISIT |
413                                                T1_SIG_OTH)
414                   || t1_flags[1 + k + 2][1 +
415                                          i] & (T1_SIG | T1_VISIT |
416                                                T1_SIG_OTH)
417                   || (t1_flags[1 + k + 3][1 + i] &
418                       (~
419                        (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
420                         T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
421         } else {
422           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
423                   || t1_flags[1 + k + 1][1 +
424                                          i] & (T1_SIG | T1_VISIT |
425                                                T1_SIG_OTH)
426                   || t1_flags[1 + k + 2][1 +
427                                          i] & (T1_SIG | T1_VISIT |
428                                                T1_SIG_OTH)
429                   || t1_flags[1 + k + 3][1 +
430                                          i] & (T1_SIG | T1_VISIT |
431                                                T1_SIG_OTH));
432         }
433       } else {
434         agg = 0;
435       }
436       if (agg) {
437         for (runlen = 0; runlen < 4; runlen++) {
438           if (int_abs(t1_data[k + runlen][i]) & one)
439             break;
440         }
441         mqc_setcurctx(T1_CTXNO_AGG);
442         mqc_encode(runlen != 4);
443         if (runlen == 4) {
444           continue;
445         }
446         mqc_setcurctx(T1_CTXNO_UNI);
447         mqc_encode(runlen >> 1);
448         mqc_encode(runlen & 1);
449       } else {
450         runlen = 0;
451       }
452       for (j = k + runlen; j < k + 4 && j < h; j++) {
453         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
454                && (j == k + 3 || j == h - 1)) ? 1 : 0;
455         t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],
456                             &t1_data[j][i], orient, bpno, one,
457                             nmsedec, agg && (j == k + runlen), vsc);
458       }
459     }
460   }
461 }
462
463 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
464 {
465   int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
466   int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
467   one = 1 << bpno;
468   half = one >> 1;
469   oneplushalf = one | half;
470   for (k = 0; k < h; k += 4) {
471     for (i = 0; i < w; i++) {
472       if (k + 3 < h) {
473         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
474           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
475                   || t1_flags[1 + k + 1][1 +
476                                          i] & (T1_SIG | T1_VISIT |
477                                                T1_SIG_OTH)
478                   || t1_flags[1 + k + 2][1 +
479                                          i] & (T1_SIG | T1_VISIT |
480                                                T1_SIG_OTH)
481                   || (t1_flags[1 + k + 3][1 + i] &
482                       (~
483                        (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
484                         T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
485         } else {
486           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
487                   || t1_flags[1 + k + 1][1 +
488                                          i] & (T1_SIG | T1_VISIT |
489                                                T1_SIG_OTH)
490                   || t1_flags[1 + k + 2][1 +
491                                          i] & (T1_SIG | T1_VISIT |
492                                                T1_SIG_OTH)
493                   || t1_flags[1 + k + 3][1 +
494                                          i] & (T1_SIG | T1_VISIT |
495                                                T1_SIG_OTH));
496         }
497       } else {
498         agg = 0;
499       }
500       if (agg) {
501         mqc_setcurctx(T1_CTXNO_AGG);
502         if (!mqc_decode()) {
503           continue;
504         }
505         mqc_setcurctx(T1_CTXNO_UNI);
506         runlen = mqc_decode();
507         runlen = (runlen << 1) | mqc_decode();
508       } else {
509         runlen = 0;
510       }
511       for (j = k + runlen; j < k + 4 && j < h; j++) {
512         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
513                && (j == k + 3 || j == h - 1)) ? 1 : 0;
514         t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
515                             &t1_data[j][i], 
516
517                             orient, 
518
519                             oneplushalf,
520                             agg && (j == k + runlen), vsc);
521       }
522     }
523   }
524   if (segsym) {
525     int v = 0;
526     mqc_setcurctx(T1_CTXNO_UNI);
527     v = mqc_decode();
528     v = (v << 1) | mqc_decode();
529     v = (v << 1) | mqc_decode();
530     v = (v << 1) | mqc_decode();
531     /*  if (v!=0xa) 
532        {
533        fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
534        } */
535   }
536 }                               /* VSC and  BYPASS by Antonin */
537
538 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps)       //mod fixed_quality
539 {
540   double w1, w2, wmsedec;
541   if (qmfbid == 1) {
542     w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
543     w2 = dwt_getnorm(level, orient);
544   } else {                      /* if (qmfbid == 0) */
545     w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
546     w2 = dwt_getnorm_real(level, orient);
547   }
548   wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
549   wmsedec *= wmsedec * nmsedec / 8192.0;
550   return wmsedec;
551 }
552
553 void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, tcd_tile_t * tile)    //mod fixed_quality
554 {
555   int i, j;
556   int w, h;
557   int passno;
558   int bpno, passtype;
559   int max;
560   int nmsedec;
561   double cumwmsedec = 0;
562   char type = T1_TYPE_MQ;
563
564   w = cblk->x1 - cblk->x0;
565   h = cblk->y1 - cblk->y0;
566
567   max = 0;
568   for (j = 0; j < h; j++) {
569     for (i = 0; i < w; i++) {
570       max = int_max(max, int_abs(t1_data[j][i]));
571     }
572   }
573
574   cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
575
576   /* Changed by Dmitry Kolyadin */
577   for (i = 0; i <= w; i++)
578     for (j = 0; j <= h; j++){
579       t1_flags[j][i] = 0;
580     }
581
582   bpno = cblk->numbps - 1;
583   passtype = 2;
584
585   mqc_resetstates();
586   mqc_setstate(T1_CTXNO_UNI, 0, 46);
587   mqc_setstate(T1_CTXNO_AGG, 0, 3);
588   mqc_setstate(T1_CTXNO_ZC, 0, 4);
589   mqc_init_enc(cblk->data);
590
591   for (passno = 0; bpno >= 0; passno++) {
592     tcd_pass_t *pass = &cblk->passes[passno];
593     int correction = 3;
594     type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
595             && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
596       T1_TYPE_MQ;
597
598     switch (passtype) {
599     case 0:
600       t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
601       break;
602     case 1:
603       t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
604       break;
605     case 2:
606       t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
607       /* code switch SEGMARK (i.e. SEGSYM) */
608       if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
609         mqc_segmark_enc();
610       break;
611     }
612
613     cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);      //mod fixed_quality
614     tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //add antonin quality
615
616
617     /* Code switch "RESTART" (i.e. TERMALL) */
618     if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
619         && !((passtype == 2) && (bpno - 1 < 0))) {
620       if (type == T1_TYPE_RAW) {
621         mqc_flush();
622         correction = 1;
623         /* correction = mqc_bypass_flush_enc(); */
624       } else {                  /* correction = mqc_restart_enc(); */
625         mqc_flush();
626         correction = 1;
627       }
628       pass->term = 1;
629     } else {
630       if (((bpno < (cblk->numbps - 4) && (passtype > 0))
631            || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
632           && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
633         if (type == T1_TYPE_RAW) {
634           mqc_flush();
635           correction = 1;
636           /* correction = mqc_bypass_flush_enc(); */
637         } else {                /* correction = mqc_restart_enc(); */
638           mqc_flush();
639           correction = 1;
640         }
641         pass->term = 1;
642       } else {
643         pass->term = 0;
644       }
645     }
646
647     if (++passtype == 3) {
648       passtype = 0;
649       bpno--;
650     }
651
652     if (pass->term && bpno > 0) {
653       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
654               && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
655         T1_TYPE_MQ;
656       if (type == T1_TYPE_RAW)
657         mqc_bypass_init_enc();
658       else
659         mqc_restart_init_enc();
660     }
661
662     pass->distortiondec = cumwmsedec;
663     pass->rate = mqc_numbytes() + correction;   /* FIXME */
664     pass->len =
665       pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
666
667     /* Code-switch "RESET" */
668     if (cblksty & J2K_CCP_CBLKSTY_RESET)
669       mqc_reset_enc();
670   }
671
672   /* Code switch "ERTERM" (i.e. PTERM) */
673   if (cblksty & J2K_CCP_CBLKSTY_PTERM)
674     mqc_erterm_enc();
675   else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
676     mqc_flush();
677
678   cblk->totalpasses = passno;
679 }
680
681 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
682                     int cblksty)
683 {
684   int i, j, w, h;
685   int bpno, passtype;
686   int segno, passno;
687   char type = T1_TYPE_MQ; //BYPASS mode
688   
689   w = cblk->x1 - cblk->x0;
690   h = cblk->y1 - cblk->y0;
691   
692   /* Changed by Dmitry Kolyadin */
693   for (j = 0; j <= h; j++){
694     for (i = 0; i <= w; i++) {
695       t1_flags[j][i] = 0;
696   }
697   }
698     
699   /* Changed by Dmitry Kolyadin */
700   for (i = 0; i < w; i++) {
701       for (j = 0; j < h; j++){
702         t1_data[j][i] = 0;
703       }
704   }
705       
706   bpno = roishift + cblk->numbps - 1;
707   passtype = 2;
708
709   mqc_resetstates();
710   mqc_setstate(T1_CTXNO_UNI, 0, 46);
711   mqc_setstate(T1_CTXNO_AGG, 0, 3);
712   mqc_setstate(T1_CTXNO_ZC, 0, 4);
713
714   for (segno = 0; segno < cblk->numsegs; segno++) {
715     tcd_seg_t *seg = &cblk->segs[segno];
716     
717     // Add BYPASS mode 
718     type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
719       && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
720     T1_TYPE_MQ;
721     if (type == T1_TYPE_RAW) 
722       raw_init_dec(seg->data, seg->len);
723     else
724       mqc_init_dec(seg->data, seg->len);
725     // ddA
726
727     if (bpno==0) cblk->lastbp=1;  // Add Antonin : quantizbug1
728     
729     for (passno = 0; passno < seg->numpasses; passno++) {
730       switch (passtype) {
731       case 0:
732         t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
733         break;
734       case 1:
735         t1_dec_refpass(w, h, bpno, type, cblksty);
736         break;
737       case 2:
738         t1_dec_clnpass(w, h, bpno, orient, cblksty);
739         break;
740       }
741       
742       if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
743         mqc_resetstates();
744
745         mqc_setstate(T1_CTXNO_UNI, 0, 46);
746
747         mqc_setstate(T1_CTXNO_AGG, 0, 3);
748
749         mqc_setstate(T1_CTXNO_ZC, 0, 4);
750
751       }
752       
753       if (++passtype == 3) {
754         passtype = 0;
755         bpno--;
756       }
757     }
758   }
759 }
760
761 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
762 {
763   int compno, resno, bandno, precno, cblkno;
764   int x, y, i, j, orient;
765   tcd_tilecomp_t *tilec;
766   tcd_resolution_t *res;
767   tcd_band_t *band;
768   tcd_precinct_t *prc;
769   tcd_cblk_t *cblk;
770
771   tile->distotile = 0;          //add fixed_quality
772
773   for (compno = 0; compno < tile->numcomps; compno++) {
774     tilec = &tile->comps[compno];
775     for (resno = 0; resno < tilec->numresolutions; resno++) {
776       res = &tilec->resolutions[resno];
777       for (bandno = 0; bandno < res->numbands; bandno++) {
778         band = &res->bands[bandno];
779         for (precno = 0; precno < res->pw * res->ph; precno++) {
780           prc = &band->precincts[precno];
781           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
782             cblk = &prc->cblks[cblkno];
783
784             if (band->bandno == 0) {
785               x = cblk->x0 - band->x0;
786               y = cblk->y0 - band->y0;
787             } else if (band->bandno == 1) {
788               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
789               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
790               y = cblk->y0 - band->y0;
791             } else if (band->bandno == 2) {
792               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
793               x = cblk->x0 - band->x0;
794               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
795             } else {            /* if (band->bandno == 3) */
796               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
797               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
798               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
799             }
800
801             if (tcp->tccps[compno].qmfbid == 1) {
802
803               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
804                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
805                   t1_data[j][i] =
806                     tilec->data[(x + i) +
807                                 (y + j) * (tilec->x1 -
808                                            tilec->
809                                            x0)] << T1_NMSEDEC_FRACBITS;
810                 }
811               }
812             } else if (tcp->tccps[compno].qmfbid == 0) {
813               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
814                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
815                   t1_data[j][i] =
816                     fix_mul(tilec->
817                             data[x + i +
818                                  (y + j) * (tilec->x1 -
819                                             tilec->
820                                             x0)],
821                             8192 * 8192 /
822                             band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
823                 }
824               }
825             }
826             orient = band->bandno;      /* FIXME */
827             if (orient == 2) {
828               orient = 1;
829             } else if (orient == 1) {
830               orient = 2;
831             }
832             t1_encode_cblk(cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);       //mod fixed_quality
833           }                     /* cblkno */
834         }                       /* precno */
835       }                         /* bandno */
836     }                           /* resno  */
837   }                             /* compo  */
838 }
839
840
841 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
842 {
843   int compno, resno, bandno, precno, cblkno;
844
845   for (compno = 0; compno < tile->numcomps; compno++) {
846     tcd_tilecomp_t *tilec = &tile->comps[compno];
847     for (resno = 0; resno < tilec->numresolutions; resno++) {
848       tcd_resolution_t *res = &tilec->resolutions[resno];
849       for (bandno = 0; bandno < res->numbands; bandno++) {
850         tcd_band_t *band = &res->bands[bandno];
851         for (precno = 0; precno < res->pw * res->ph; precno++) {
852           tcd_precinct_t *prc = &band->precincts[precno];
853           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
854             int x, y, i, j, orient;
855             tcd_cblk_t *cblk = &prc->cblks[cblkno];
856             orient = band->bandno;      /* FIXME */
857             if (orient == 2)
858               orient = 1;
859             else if (orient == 1)
860               orient = 2;
861             t1_decode_cblk(cblk, orient,
862                            tcp->tccps[compno].roishift,
863                            tcp->tccps[compno].cblksty);
864             if (band->bandno == 0) {
865               x = cblk->x0 - band->x0;
866               y = cblk->y0 - band->y0;
867             } else if (band->bandno == 1) {
868               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
869               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
870               y = cblk->y0 - band->y0;
871             } else if (band->bandno == 2) {
872               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
873               x = cblk->x0 - band->x0;
874               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
875             } else {            /* if (band->bandno == 3) */
876               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
877               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
878               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
879             }
880
881             if (tcp->tccps[compno].roishift) {
882               int thresh, val, mag;
883               thresh = 1 << tcp->tccps[compno].roishift;
884               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
885                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
886                   val = t1_data[j][i];
887                   mag = int_abs(val);
888                   if (mag >= thresh) {
889                     mag >>= tcp->tccps[compno].roishift;
890                     t1_data[j][i] = val < 0 ? -mag : mag;
891                   }
892                 }
893               }
894             }
895
896             if (tcp->tccps[compno].qmfbid == 1) {
897               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
898                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
899                   tilec->data[x + i +
900                               (y + j) * (tilec->x1 -
901                                          tilec->x0)] = t1_data[j][i];
902                 }
903               }
904             } else {            /* if (tcp->tccps[compno].qmfbid == 0) */
905
906               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
907                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
908                   if (t1_data[j][i] == 0) {
909                     tilec->data[x + i +
910                       (y + j) * (tilec->x1 - tilec->x0)] = 0;
911                   } else {
912
913                     // Add antonin : quantizbug1
914
915                     t1_data[j][i]<<=1;
916
917                     //if (cblk->lastbp) 
918
919                     t1_data[j][i]+=t1_data[j][i]>0?1:-1;
920
921                     // ddA
922                     tilec->data[x + i +
923                                 (y + j) * (tilec->x1 -
924                                            tilec->
925                                            x0)] =
926                       fix_mul(t1_data[j][i] << 12, band->stepsize); //Mod Antonin : quantizbug1 (before : << 13)
927                   }
928                 }
929               }
930             }
931           }
932         }
933       }
934     }
935   }
936 }
937
938 int t1_init_ctxno_zc(int f, int orient)
939 {
940   int h, v, d, n, t, hv;
941   n = 0;
942   h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
943   v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
944   d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
945                                 0) + ((f & T1_SIG_SE) !=
946                                       0) + ((f & T1_SIG_SW) != 0);
947   switch (orient) {
948   case 2:
949     t = h;
950     h = v;
951     v = t;
952   case 0:
953   case 1:
954     if (!h) {
955       if (!v) {
956         if (!d)
957           n = 0;
958         else if (d == 1)
959           n = 1;
960         else
961           n = 2;
962       } else if (v == 1)
963         n = 3;
964       else
965         n = 4;
966     } else if (h == 1) {
967       if (!v) {
968         if (!d)
969           n = 5;
970         else
971           n = 6;
972       } else
973         n = 7;
974     } else
975       n = 8;
976     break;
977   case 3:
978     hv = h + v;
979     if (!d) {
980       if (!hv)
981         n = 0;
982       else if (hv == 1)
983         n = 1;
984       else
985         n = 2;
986     } else if (d == 1) {
987       if (!hv)
988         n = 3;
989       else if (hv == 1)
990         n = 4;
991       else
992         n = 5;
993     } else if (d == 2) {
994       if (!hv)
995         n = 6;
996       else
997         n = 7;
998     } else
999       n = 8;
1000     break;
1001   }
1002   return T1_CTXNO_ZC + n;
1003 }
1004
1005 int t1_init_ctxno_sc(int f)
1006 {
1007   int hc, vc, n;
1008   n = 0;
1009   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1010                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1011                1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1012                              (T1_SIG_E | T1_SGN_E)) +
1013                             ((f & (T1_SIG_W | T1_SGN_W)) ==
1014                              (T1_SIG_W | T1_SGN_W)), 1);
1015   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1016                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1017                1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1018                              (T1_SIG_N | T1_SGN_N)) +
1019                             ((f & (T1_SIG_S | T1_SGN_S)) ==
1020                              (T1_SIG_S | T1_SGN_S)), 1);
1021   if (hc < 0) {
1022     hc = -hc;
1023     vc = -vc;
1024   }
1025   if (!hc) {
1026     if (vc == -1)
1027       n = 1;
1028     else if (!vc)
1029       n = 0;
1030     else
1031       n = 1;
1032   } else if (hc == 1) {
1033     if (vc == -1)
1034       n = 2;
1035     else if (!vc)
1036       n = 3;
1037     else
1038       n = 4;
1039   }
1040   return T1_CTXNO_SC + n;
1041 }
1042
1043 int t1_init_ctxno_mag(int f)
1044 {
1045   int n;
1046   if (!(f & T1_REFINE))
1047     n = (f & (T1_SIG_OTH)) ? 1 : 0;
1048   else
1049     n = 2;
1050   return T1_CTXNO_MAG + n;
1051 }
1052
1053 int t1_init_spb(int f)
1054 {
1055   int hc, vc, n;
1056   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1057                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1058                1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1059                              (T1_SIG_E | T1_SGN_E)) +
1060                             ((f & (T1_SIG_W | T1_SGN_W)) ==
1061                              (T1_SIG_W | T1_SGN_W)), 1);
1062   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1063                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1064                1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1065                              (T1_SIG_N | T1_SGN_N)) +
1066                             ((f & (T1_SIG_S | T1_SGN_S)) ==
1067                              (T1_SIG_S | T1_SGN_S)), 1);
1068   if (!hc && !vc)
1069     n = 0;
1070   else
1071     n = (!(hc > 0 || (!hc && vc > 0)));
1072   return n;
1073 }
1074
1075 void t1_init_luts()
1076 {
1077   int i, j;
1078   double u, v, t;
1079   for (j = 0; j < 4; j++) {
1080     for (i = 0; i < 256; ++i) {
1081       t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
1082     }
1083   }
1084   for (i = 0; i < 256; i++) {
1085     t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
1086   }
1087   for (j = 0; j < 2; j++) {
1088     for (i = 0; i < 2048; ++i) {
1089       t1_lut_ctxno_mag[(j << 11) + i] =
1090         t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
1091     }
1092   }
1093   for (i = 0; i < 256; ++i) {
1094     t1_lut_spb[i] = t1_init_spb(i << 4);
1095   }
1096   /* FIXME FIXME FIXME */
1097   /* fprintf(stdout,"nmsedec luts:\n"); */
1098   for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1099     t = i / pow(2, T1_NMSEDEC_FRACBITS);
1100     u = t;
1101     v = t - 1.5;
1102     t1_lut_nmsedec_sig[i] =
1103       int_max(0,
1104               (int) (floor
1105                      ((u * u - v * v) * pow(2,
1106                                             T1_NMSEDEC_FRACBITS) +
1107                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1108     t1_lut_nmsedec_sig0[i] =
1109       int_max(0,
1110               (int) (floor
1111                      ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1112                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1113     u = t - 1.0;
1114     if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1115       v = t - 1.5;
1116     } else {
1117       v = t - 0.5;
1118     }
1119     t1_lut_nmsedec_ref[i] =
1120       int_max(0,
1121               (int) (floor
1122                      ((u * u - v * v) * pow(2,
1123                                             T1_NMSEDEC_FRACBITS) +
1124                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1125     t1_lut_nmsedec_ref0[i] =
1126       int_max(0,
1127               (int) (floor
1128                      ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1129                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1130   }
1131 }