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