* Quantization bug fixed when using 9x7 DWT (comment keyword : quantizbug1)
[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 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
192                          char type, int vsc)
193 {
194   int v, flag;
195   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
196     : (*fp);
197   if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
198     if (type == T1_TYPE_RAW) {
199       if (raw_decode()) {
200         v = raw_decode();       /* ESSAI */
201         *dp = v ? -oneplushalf : oneplushalf;
202         t1_updateflags(fp, v);
203         *fp |= T1_SIG;
204       }
205     } else {
206       mqc_setcurctx(t1_getctxno_zc(flag, orient));
207       if (mqc_decode()) {
208         mqc_setcurctx(t1_getctxno_sc(flag));
209         v = mqc_decode() ^ t1_getspb(flag);
210         *dp = v ? -oneplushalf : oneplushalf;
211         t1_updateflags(fp, v);
212         *fp |= T1_SIG;
213       }
214     }
215     *fp |= T1_VISIT;
216   }
217 }                               /* VSC and  BYPASS by Antonin */
218
219 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
220                     char type, int cblksty)
221 {
222   int i, j, k, one, vsc;
223   *nmsedec = 0;
224   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
225   for (k = 0; k < h; k += 4) {
226     for (i = 0; i < w; i++) {
227       for (j = k; j < k + 4 && j < h; j++) {
228         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
229                && (j == k + 3 || j == h - 1)) ? 1 : 0;
230         t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],
231                             &t1_data[j][i], orient, bpno, one,
232                             nmsedec, type, vsc);
233       }
234     }
235   }
236 }
237
238 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
239                     int cblksty)
240 {
241   int i, j, k, one, half, oneplushalf, vsc;
242   one = 1 << bpno;
243   half = one >> 1;
244   oneplushalf = one | half;
245   for (k = 0; k < h; k += 4) {
246     for (i = 0; i < w; i++) {
247       for (j = k; j < k + 4 && j < h; j++) {
248         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
249                && (j == k + 3 || j == h - 1)) ? 1 : 0;
250         t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
251                             &t1_data[j][i], 
252                             orient, 
253                             oneplushalf,
254                             type, vsc);
255       }
256     }
257   }
258 }                               /* VSC and  BYPASS by Antonin */
259
260 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
261                          char type, int vsc)
262 {
263   int v, flag;
264   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
265     : (*fp);
266   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
267     *nmsedec +=
268       t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
269     v = int_abs(*dp) & one ? 1 : 0;
270     if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
271       mqc_setcurctx(t1_getctxno_mag(flag));     /* ESSAI */
272       mqc_bypass_enc(v);
273     } else {
274       mqc_setcurctx(t1_getctxno_mag(flag));
275       mqc_encode(v);
276     }
277     *fp |= T1_REFINE;
278   }
279 }
280
281
282 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
283                          char type, int vsc)
284 {
285   int v, t, flag;
286   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
287     : (*fp);
288   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
289     if (type == T1_TYPE_RAW) {
290       mqc_setcurctx(t1_getctxno_mag(flag));     /* ESSAI */
291       v = raw_decode();
292     } else {
293       mqc_setcurctx(t1_getctxno_mag(flag));
294       v = mqc_decode();
295     }
296     t = v ? poshalf : neghalf;
297     *dp += *dp < 0 ? -t : t;
298     *fp |= T1_REFINE;
299   }
300 }                               /* VSC and  BYPASS by Antonin  */
301
302 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
303                     int cblksty)
304 {
305   int i, j, k, one, vsc;
306   *nmsedec = 0;
307   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
308   for (k = 0; k < h; k += 4) {
309     for (i = 0; i < w; i++) {
310       for (j = k; j < k + 4 && j < h; j++) {
311         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
312                && (j == k + 3 || j == h - 1)) ? 1 : 0;
313         t1_enc_refpass_step(&t1_flags[1 + j][1 + i],
314                             &t1_data[j][i], bpno, one, nmsedec, type, vsc);
315       }
316     }
317   }
318 }
319
320 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
321 {
322   int i, j, k, one, poshalf, neghalf;
323   int vsc;
324   one = 1 << bpno;
325   poshalf = one >> 1;
326   neghalf = bpno > 0 ? -poshalf : -1;
327   for (k = 0; k < h; k += 4) {
328     for (i = 0; i < w; i++) {
329       for (j = k; j < k + 4 && j < h; j++) {
330         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
331                && (j == k + 3 || j == h - 1)) ? 1 : 0;
332         t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
333                             &t1_data[j][i], 
334                             poshalf, 
335                             neghalf, 
336                             type, vsc);
337       }
338     }
339   }
340 }                               /* VSC and  BYPASS by Antonin */
341
342 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
343                          int *nmsedec, int partial, int vsc)
344 {
345   int v, flag;
346   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
347     : (*fp);
348   if (partial)
349     goto label_partial;
350   if (!(*fp & (T1_SIG | T1_VISIT))) {
351     mqc_setcurctx(t1_getctxno_zc(flag, orient));
352     v = int_abs(*dp) & one ? 1 : 0;
353     mqc_encode(v);
354     if (v) {
355     label_partial:
356       *nmsedec +=
357         t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
358       mqc_setcurctx(t1_getctxno_sc(flag));
359       v = *dp < 0 ? 1 : 0;
360       mqc_encode(v ^ t1_getspb(flag));
361       t1_updateflags(fp, v);
362       *fp |= T1_SIG;
363     }
364   }
365   *fp &= ~T1_VISIT;
366 }
367
368
369 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
370                          int partial, int vsc)
371 {
372   int v, flag;
373   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
374     : (*fp);
375   if (partial)
376     goto label_partial;
377   if (!(flag & (T1_SIG | T1_VISIT))) {
378     mqc_setcurctx(t1_getctxno_zc(flag, orient));
379     if (mqc_decode()) {
380     label_partial:
381       mqc_setcurctx(t1_getctxno_sc(flag));
382       v = mqc_decode() ^ t1_getspb(flag);
383       *dp = v ? -oneplushalf : oneplushalf;
384       t1_updateflags(fp, v);
385       *fp |= T1_SIG;
386     }
387   }
388   *fp &= ~T1_VISIT;
389 }                               /* VSC and  BYPASS by Antonin */
390
391 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
392                     int cblksty)
393 {
394   int i, j, k, one, agg, runlen, vsc;
395   *nmsedec = 0;
396   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
397   for (k = 0; k < h; k += 4) {
398     for (i = 0; i < w; i++) {
399       if (k + 3 < h) {
400         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
401           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
402                   || t1_flags[1 + k + 1][1 +
403                                          i] & (T1_SIG | T1_VISIT |
404                                                T1_SIG_OTH)
405                   || t1_flags[1 + k + 2][1 +
406                                          i] & (T1_SIG | T1_VISIT |
407                                                T1_SIG_OTH)
408                   || (t1_flags[1 + k + 3][1 + i] &
409                       (~
410                        (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
411                         T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
412         } else {
413           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
414                   || t1_flags[1 + k + 1][1 +
415                                          i] & (T1_SIG | T1_VISIT |
416                                                T1_SIG_OTH)
417                   || t1_flags[1 + k + 2][1 +
418                                          i] & (T1_SIG | T1_VISIT |
419                                                T1_SIG_OTH)
420                   || t1_flags[1 + k + 3][1 +
421                                          i] & (T1_SIG | T1_VISIT |
422                                                T1_SIG_OTH));
423         }
424       } else {
425         agg = 0;
426       }
427       if (agg) {
428         for (runlen = 0; runlen < 4; runlen++) {
429           if (int_abs(t1_data[k + runlen][i]) & one)
430             break;
431         }
432         mqc_setcurctx(T1_CTXNO_AGG);
433         mqc_encode(runlen != 4);
434         if (runlen == 4) {
435           continue;
436         }
437         mqc_setcurctx(T1_CTXNO_UNI);
438         mqc_encode(runlen >> 1);
439         mqc_encode(runlen & 1);
440       } else {
441         runlen = 0;
442       }
443       for (j = k + runlen; j < k + 4 && j < h; j++) {
444         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
445                && (j == k + 3 || j == h - 1)) ? 1 : 0;
446         t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],
447                             &t1_data[j][i], orient, bpno, one,
448                             nmsedec, agg && (j == k + runlen), vsc);
449       }
450     }
451   }
452 }
453
454 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
455 {
456   int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
457   int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
458   one = 1 << bpno;
459   half = one >> 1;
460   oneplushalf = one | half;
461   for (k = 0; k < h; k += 4) {
462     for (i = 0; i < w; i++) {
463       if (k + 3 < h) {
464         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
465           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
466                   || t1_flags[1 + k + 1][1 +
467                                          i] & (T1_SIG | T1_VISIT |
468                                                T1_SIG_OTH)
469                   || t1_flags[1 + k + 2][1 +
470                                          i] & (T1_SIG | T1_VISIT |
471                                                T1_SIG_OTH)
472                   || (t1_flags[1 + k + 3][1 + i] &
473                       (~
474                        (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
475                         T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
476         } else {
477           agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
478                   || t1_flags[1 + k + 1][1 +
479                                          i] & (T1_SIG | T1_VISIT |
480                                                T1_SIG_OTH)
481                   || t1_flags[1 + k + 2][1 +
482                                          i] & (T1_SIG | T1_VISIT |
483                                                T1_SIG_OTH)
484                   || t1_flags[1 + k + 3][1 +
485                                          i] & (T1_SIG | T1_VISIT |
486                                                T1_SIG_OTH));
487         }
488       } else {
489         agg = 0;
490       }
491       if (agg) {
492         mqc_setcurctx(T1_CTXNO_AGG);
493         if (!mqc_decode()) {
494           continue;
495         }
496         mqc_setcurctx(T1_CTXNO_UNI);
497         runlen = mqc_decode();
498         runlen = (runlen << 1) | mqc_decode();
499       } else {
500         runlen = 0;
501       }
502       for (j = k + runlen; j < k + 4 && j < h; j++) {
503         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
504                && (j == k + 3 || j == h - 1)) ? 1 : 0;
505         t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
506                             &t1_data[j][i], 
507                             orient, 
508                             oneplushalf,
509                             agg && (j == k + runlen), vsc);
510       }
511     }
512   }
513   if (segsym) {
514     int v = 0;
515     mqc_setcurctx(T1_CTXNO_UNI);
516     v = mqc_decode();
517     v = (v << 1) | mqc_decode();
518     v = (v << 1) | mqc_decode();
519     v = (v << 1) | mqc_decode();
520     /*  if (v!=0xa) 
521        {
522        fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
523        } */
524   }
525 }                               /* VSC and  BYPASS by Antonin */
526
527 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps)       //mod fixed_quality
528 {
529   double w1, w2, wmsedec;
530   if (qmfbid == 1) {
531     w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
532     w2 = dwt_getnorm(level, orient);
533   } else {                      /* if (qmfbid == 0) */
534     w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
535     w2 = dwt_getnorm_real(level, orient);
536   }
537   wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
538   wmsedec *= wmsedec * nmsedec / 8192.0;
539   return wmsedec;
540 }
541
542 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
543 {
544   int i, j;
545   int w, h;
546   int passno;
547   int bpno, passtype;
548   int max;
549   int nmsedec;
550   double cumwmsedec = 0;
551   char type = T1_TYPE_MQ;
552
553   w = cblk->x1 - cblk->x0;
554   h = cblk->y1 - cblk->y0;
555
556   max = 0;
557   for (j = 0; j < h; j++) {
558     for (i = 0; i < w; i++) {
559       max = int_max(max, int_abs(t1_data[j][i]));
560     }
561   }
562
563   cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
564
565   for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
566     ((int *) t1_flags)[i] = 0;
567   bpno = cblk->numbps - 1;
568   passtype = 2;
569
570   mqc_resetstates();
571   mqc_setstate(T1_CTXNO_UNI, 0, 46);
572   mqc_setstate(T1_CTXNO_AGG, 0, 3);
573   mqc_setstate(T1_CTXNO_ZC, 0, 4);
574   mqc_init_enc(cblk->data);
575
576   for (passno = 0; bpno >= 0; passno++) {
577     tcd_pass_t *pass = &cblk->passes[passno];
578     int correction = 3;
579     type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
580             && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
581       T1_TYPE_MQ;
582
583     switch (passtype) {
584     case 0:
585       t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
586       break;
587     case 1:
588       t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
589       break;
590     case 2:
591       t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
592       /* code switch SEGMARK (i.e. SEGSYM) */
593       if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
594         mqc_segmark_enc();
595       break;
596     }
597
598     cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);      //mod fixed_quality
599     tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //add antonin quality
600
601
602     /* Code switch "RESTART" (i.e. TERMALL) */
603     if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
604         && !((passtype == 2) && (bpno - 1 < 0))) {
605       if (type == T1_TYPE_RAW) {
606         mqc_flush();
607         correction = 1;
608         /* correction = mqc_bypass_flush_enc(); */
609       } else {                  /* correction = mqc_restart_enc(); */
610         mqc_flush();
611         correction = 1;
612       }
613       pass->term = 1;
614     } else {
615       if (((bpno < (cblk->numbps - 4) && (passtype > 0))
616            || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
617           && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
618         if (type == T1_TYPE_RAW) {
619           mqc_flush();
620           correction = 1;
621           /* correction = mqc_bypass_flush_enc(); */
622         } else {                /* correction = mqc_restart_enc(); */
623           mqc_flush();
624           correction = 1;
625         }
626         pass->term = 1;
627       } else {
628         pass->term = 0;
629       }
630     }
631
632     if (++passtype == 3) {
633       passtype = 0;
634       bpno--;
635     }
636
637     if (pass->term && bpno > 0) {
638       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
639               && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
640         T1_TYPE_MQ;
641       if (type == T1_TYPE_RAW)
642         mqc_bypass_init_enc();
643       else
644         mqc_restart_init_enc();
645     }
646
647     pass->distortiondec = cumwmsedec;
648     pass->rate = mqc_numbytes() + correction;   /* FIXME */
649     pass->len =
650       pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
651
652     /* Code-switch "RESET" */
653     if (cblksty & J2K_CCP_CBLKSTY_RESET)
654       mqc_reset_enc();
655   }
656
657   /* Code switch "ERTERM" (i.e. PTERM) */
658   if (cblksty & J2K_CCP_CBLKSTY_PTERM)
659     mqc_erterm_enc();
660   else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
661     mqc_flush();
662
663   cblk->totalpasses = passno;
664 }
665
666 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
667                     int cblksty)
668 {
669   int i;
670   int w, h;
671   int bpno, passtype;
672   int segno, passno;
673   /* add TONY */
674   char type = T1_TYPE_MQ;
675   /* dda */
676
677   for (i = 0; i < sizeof(t1_data) / sizeof(int); i++)
678     ((int *) t1_data)[i] = 0;
679   for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
680     ((int *) t1_flags)[i] = 0;
681
682   w = cblk->x1 - cblk->x0;
683   h = cblk->y1 - cblk->y0;
684   bpno = roishift + cblk->numbps - 1;
685   passtype = 2;
686
687   mqc_resetstates();
688   mqc_setstate(T1_CTXNO_UNI, 0, 46);
689   mqc_setstate(T1_CTXNO_AGG, 0, 3);
690   mqc_setstate(T1_CTXNO_ZC, 0, 4);
691
692   for (segno = 0; segno < cblk->numsegs; segno++) {
693     tcd_seg_t *seg = &cblk->segs[segno];
694
695     /* add TONY */
696     type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
697             && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
698       T1_TYPE_MQ;
699     if (type == T1_TYPE_RAW)
700       raw_init_dec(seg->data, seg->len);
701     else
702       mqc_init_dec(seg->data, seg->len);
703     /* dda */
704
705     if (bpno==0) cblk->lastbp=1;  // Add Antonin : quantizbug1
706
707     for (passno = 0; passno < seg->numpasses; passno++) {
708       switch (passtype) {
709       case 0:
710         t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
711         break;
712       case 1:
713         t1_dec_refpass(w, h, bpno, type, cblksty);
714         break;
715       case 2:
716         t1_dec_clnpass(w, h, bpno, orient, cblksty);
717         break;
718       }
719
720       if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ)
721         mqc_reset_enc();
722
723       if (++passtype == 3) {
724         passtype = 0;
725         bpno--;
726       }
727     }
728   }
729 }
730
731 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
732 {
733   int compno, resno, bandno, precno, cblkno;
734   int x, y, i, j, orient;
735   tcd_tilecomp_t *tilec;
736   tcd_resolution_t *res;
737   tcd_band_t *band;
738   tcd_precinct_t *prc;
739   tcd_cblk_t *cblk;
740
741   tile->distotile = 0;          //add fixed_quality
742
743   for (compno = 0; compno < tile->numcomps; compno++) {
744     tilec = &tile->comps[compno];
745     for (resno = 0; resno < tilec->numresolutions; resno++) {
746       res = &tilec->resolutions[resno];
747       for (bandno = 0; bandno < res->numbands; bandno++) {
748         band = &res->bands[bandno];
749         for (precno = 0; precno < res->pw * res->ph; precno++) {
750           prc = &band->precincts[precno];
751           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
752             cblk = &prc->cblks[cblkno];
753
754             if (band->bandno == 0) {
755               x = cblk->x0 - band->x0;
756               y = cblk->y0 - band->y0;
757             } else if (band->bandno == 1) {
758               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
759               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
760               y = cblk->y0 - band->y0;
761             } else if (band->bandno == 2) {
762               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
763               x = cblk->x0 - band->x0;
764               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
765             } else {            /* if (band->bandno == 3) */
766               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
767               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
768               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
769             }
770
771             if (tcp->tccps[compno].qmfbid == 1) {
772
773               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
774                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
775                   t1_data[j][i] =
776                     tilec->data[(x + i) +
777                                 (y + j) * (tilec->x1 -
778                                            tilec->
779                                            x0)] << T1_NMSEDEC_FRACBITS;
780                 }
781               }
782             } else if (tcp->tccps[compno].qmfbid == 0) {
783               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
784                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
785                   t1_data[j][i] =
786                     fix_mul(tilec->
787                             data[x + i +
788                                  (y + j) * (tilec->x1 -
789                                             tilec->
790                                             x0)],
791                             8192 * 8192 /
792                             band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
793                 }
794               }
795             }
796             orient = band->bandno;      /* FIXME */
797             if (orient == 2) {
798               orient = 1;
799             } else if (orient == 1) {
800               orient = 2;
801             }
802             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
803           }                     /* cblkno */
804         }                       /* precno */
805       }                         /* bandno */
806     }                           /* resno  */
807   }                             /* compo  */
808 }
809
810
811 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
812 {
813   int compno, resno, bandno, precno, cblkno;
814
815   for (compno = 0; compno < tile->numcomps; compno++) {
816     tcd_tilecomp_t *tilec = &tile->comps[compno];
817     for (resno = 0; resno < tilec->numresolutions; resno++) {
818       tcd_resolution_t *res = &tilec->resolutions[resno];
819       for (bandno = 0; bandno < res->numbands; bandno++) {
820         tcd_band_t *band = &res->bands[bandno];
821         for (precno = 0; precno < res->pw * res->ph; precno++) {
822           tcd_precinct_t *prc = &band->precincts[precno];
823           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
824             int x, y, i, j, orient;
825             tcd_cblk_t *cblk = &prc->cblks[cblkno];
826             orient = band->bandno;      /* FIXME */
827             if (orient == 2)
828               orient = 1;
829             else if (orient == 1)
830               orient = 2;
831             t1_decode_cblk(cblk, orient,
832                            tcp->tccps[compno].roishift,
833                            tcp->tccps[compno].cblksty);
834             if (band->bandno == 0) {
835               x = cblk->x0 - band->x0;
836               y = cblk->y0 - band->y0;
837             } else if (band->bandno == 1) {
838               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
839               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
840               y = cblk->y0 - band->y0;
841             } else if (band->bandno == 2) {
842               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
843               x = cblk->x0 - band->x0;
844               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
845             } else {            /* if (band->bandno == 3) */
846               tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
847               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
848               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
849             }
850
851             if (tcp->tccps[compno].roishift) {
852               int thresh, val, mag;
853               thresh = 1 << tcp->tccps[compno].roishift;
854               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
855                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
856                   val = t1_data[j][i];
857                   mag = int_abs(val);
858                   if (mag >= thresh) {
859                     mag >>= tcp->tccps[compno].roishift;
860                     t1_data[j][i] = val < 0 ? -mag : mag;
861                   }
862                 }
863               }
864             }
865
866             if (tcp->tccps[compno].qmfbid == 1) {
867               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
868                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
869                   tilec->data[x + i +
870                               (y + j) * (tilec->x1 -
871                                          tilec->x0)] = t1_data[j][i];
872                 }
873               }
874             } else {            /* if (tcp->tccps[compno].qmfbid == 0) */
875
876               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
877                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
878                   if (t1_data[j][i] == 0) {
879                     tilec->data[x + i +
880                       (y + j) * (tilec->x1 - tilec->x0)] = 0;
881                   } else {
882                     // Add antonin : quantizbug1
883                     t1_data[j][i]<<=1;
884                     //if (cblk->lastbp) 
885                     t1_data[j][i]+=t1_data[j][i]>0?1:-1;
886                     // ddA
887                     tilec->data[x + i +
888                                 (y + j) * (tilec->x1 -
889                                            tilec->
890                                            x0)] =
891                       fix_mul(t1_data[j][i] << 12, band->stepsize); //Mod Antonin : quantizbug1 (before : << 13)
892                   }
893                 }
894               }
895             }
896           }
897         }
898       }
899     }
900   }
901 }
902
903 int t1_init_ctxno_zc(int f, int orient)
904 {
905   int h, v, d, n, t, hv;
906   n = 0;
907   h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
908   v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
909   d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
910                                 0) + ((f & T1_SIG_SE) !=
911                                       0) + ((f & T1_SIG_SW) != 0);
912   switch (orient) {
913   case 2:
914     t = h;
915     h = v;
916     v = t;
917   case 0:
918   case 1:
919     if (!h) {
920       if (!v) {
921         if (!d)
922           n = 0;
923         else if (d == 1)
924           n = 1;
925         else
926           n = 2;
927       } else if (v == 1)
928         n = 3;
929       else
930         n = 4;
931     } else if (h == 1) {
932       if (!v) {
933         if (!d)
934           n = 5;
935         else
936           n = 6;
937       } else
938         n = 7;
939     } else
940       n = 8;
941     break;
942   case 3:
943     hv = h + v;
944     if (!d) {
945       if (!hv)
946         n = 0;
947       else if (hv == 1)
948         n = 1;
949       else
950         n = 2;
951     } else if (d == 1) {
952       if (!hv)
953         n = 3;
954       else if (hv == 1)
955         n = 4;
956       else
957         n = 5;
958     } else if (d == 2) {
959       if (!hv)
960         n = 6;
961       else
962         n = 7;
963     } else
964       n = 8;
965     break;
966   }
967   return T1_CTXNO_ZC + n;
968 }
969
970 int t1_init_ctxno_sc(int f)
971 {
972   int hc, vc, n;
973   n = 0;
974   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
975                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
976                1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
977                              (T1_SIG_E | T1_SGN_E)) +
978                             ((f & (T1_SIG_W | T1_SGN_W)) ==
979                              (T1_SIG_W | T1_SGN_W)), 1);
980   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
981                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
982                1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
983                              (T1_SIG_N | T1_SGN_N)) +
984                             ((f & (T1_SIG_S | T1_SGN_S)) ==
985                              (T1_SIG_S | T1_SGN_S)), 1);
986   if (hc < 0) {
987     hc = -hc;
988     vc = -vc;
989   }
990   if (!hc) {
991     if (vc == -1)
992       n = 1;
993     else if (!vc)
994       n = 0;
995     else
996       n = 1;
997   } else if (hc == 1) {
998     if (vc == -1)
999       n = 2;
1000     else if (!vc)
1001       n = 3;
1002     else
1003       n = 4;
1004   }
1005   return T1_CTXNO_SC + n;
1006 }
1007
1008 int t1_init_ctxno_mag(int f)
1009 {
1010   int n;
1011   if (!(f & T1_REFINE))
1012     n = (f & (T1_SIG_OTH)) ? 1 : 0;
1013   else
1014     n = 2;
1015   return T1_CTXNO_MAG + n;
1016 }
1017
1018 int t1_init_spb(int f)
1019 {
1020   int hc, vc, n;
1021   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1022                 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1023                1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1024                              (T1_SIG_E | T1_SGN_E)) +
1025                             ((f & (T1_SIG_W | T1_SGN_W)) ==
1026                              (T1_SIG_W | T1_SGN_W)), 1);
1027   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1028                 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1029                1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1030                              (T1_SIG_N | T1_SGN_N)) +
1031                             ((f & (T1_SIG_S | T1_SGN_S)) ==
1032                              (T1_SIG_S | T1_SGN_S)), 1);
1033   if (!hc && !vc)
1034     n = 0;
1035   else
1036     n = (!(hc > 0 || (!hc && vc > 0)));
1037   return n;
1038 }
1039
1040 void t1_init_luts()
1041 {
1042   int i, j;
1043   double u, v, t;
1044   for (j = 0; j < 4; j++) {
1045     for (i = 0; i < 256; ++i) {
1046       t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
1047     }
1048   }
1049   for (i = 0; i < 256; i++) {
1050     t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
1051   }
1052   for (j = 0; j < 2; j++) {
1053     for (i = 0; i < 2048; ++i) {
1054       t1_lut_ctxno_mag[(j << 11) + i] =
1055         t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
1056     }
1057   }
1058   for (i = 0; i < 256; ++i) {
1059     t1_lut_spb[i] = t1_init_spb(i << 4);
1060   }
1061   /* FIXME FIXME FIXME */
1062   /* printf("nmsedec luts:\n"); */
1063   for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1064     t = i / pow(2, T1_NMSEDEC_FRACBITS);
1065     u = t;
1066     v = t - 1.5;
1067     t1_lut_nmsedec_sig[i] =
1068       int_max(0,
1069               (int) (floor
1070                      ((u * u - v * v) * pow(2,
1071                                             T1_NMSEDEC_FRACBITS) +
1072                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1073     t1_lut_nmsedec_sig0[i] =
1074       int_max(0,
1075               (int) (floor
1076                      ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1077                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1078     u = t - 1.0;
1079     if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1080       v = t - 1.5;
1081     } else {
1082       v = t - 0.5;
1083     }
1084     t1_lut_nmsedec_ref[i] =
1085       int_max(0,
1086               (int) (floor
1087                      ((u * u - v * v) * pow(2,
1088                                             T1_NMSEDEC_FRACBITS) +
1089                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1090     t1_lut_nmsedec_ref0[i] =
1091       int_max(0,
1092               (int) (floor
1093                      ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1094                       0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1095   }
1096 }