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