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
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 #include "raw.h" /* Antonin */
41 #define T1_MAXCBLKW 1024
42 #define T1_MAXCBLKH 1024
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)
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)
62 #define T1_REFINE 0x2000
63 #define T1_VISIT 0x4000
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
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)
78 #define T1_NMSEDEC_BITS 7
79 #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
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];
95 static int t1_data[T1_MAXCBLKH][T1_MAXCBLKH];
96 static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];
98 int t1_getctxno_zc(int f, int orient)
100 return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
103 int t1_getctxno_sc(int f)
105 return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
108 int t1_getctxno_mag(int f)
110 return t1_lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
115 return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
118 int t1_getnmsedec_sig(int x, int bitpos)
120 if (bitpos > T1_NMSEDEC_FRACBITS)
121 return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
123 return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
126 int t1_getnmsedec_ref(int x, int bitpos)
128 if (bitpos > T1_NMSEDEC_FRACBITS)
129 return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
131 return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
134 void t1_updateflags(int *fp, int s)
136 int *np = fp - (T1_MAXCBLKW + 2);
137 int *sp = fp + (T1_MAXCBLKW + 2);
154 void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc)
157 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
158 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
159 v = int_abs(*dp) & one ? 1 : 0;
160 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
161 mqc_setcurctx(t1_getctxno_zc(flag, orient)); /* ESSAI */
164 mqc_setcurctx(t1_getctxno_zc(flag, orient));
169 *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
170 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
171 mqc_setcurctx(t1_getctxno_sc(flag)); /* ESSAI */
174 mqc_setcurctx(t1_getctxno_sc(flag));
175 mqc_encode(v ^ t1_getspb(flag));
177 t1_updateflags(fp, v);
184 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf, char type, int vsc)
188 vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
190 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
191 if (type == T1_TYPE_RAW) {
193 v = raw_decode(); /* ESSAI */
194 *dp = v ? -oneplushalf : oneplushalf;
195 t1_updateflags(fp, v);
199 mqc_setcurctx(t1_getctxno_zc(flag, orient));
201 mqc_setcurctx(t1_getctxno_sc(flag));
202 v = mqc_decode() ^ t1_getspb(flag);
203 *dp = v ? -oneplushalf : oneplushalf;
204 t1_updateflags(fp, v);
210 } /* VSC and BYPASS by Antonin */
212 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty)
214 int i, j, k, one, vsc;
216 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
217 for (k = 0; k < h; k += 4) {
218 for (i = 0; i < w; i++) {
219 for (j = k; j < k + 4 && j < h; j++) {
220 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
221 t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, type, vsc);
227 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type, int cblksty)
229 int i, j, k, one, half, oneplushalf, vsc;
232 oneplushalf = one | half;
233 for (k = 0; k < h; k += 4) {
234 for (i = 0; i < w; i++) {
235 for (j = k; j < k + 4 && j < h; j++) {
236 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
237 && (j == k + 3 || j == h - 1)) ? 1 : 0;
238 t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf, type, vsc);
242 } /* VSC and BYPASS by Antonin */
244 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc)
247 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
248 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
249 *nmsedec += t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
250 v = int_abs(*dp) & one ? 1 : 0;
251 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
252 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
255 mqc_setcurctx(t1_getctxno_mag(flag));
262 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf, char type, int vsc)
265 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
266 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
267 if (type == T1_TYPE_RAW) {
268 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
271 mqc_setcurctx(t1_getctxno_mag(flag));
274 t = v ? poshalf : neghalf;
275 *dp += *dp < 0 ? -t : t;
278 } /* VSC and BYPASS by Antonin */
280 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type, int cblksty)
282 int i, j, k, one, vsc;
284 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
285 for (k = 0; k < h; k += 4) {
286 for (i = 0; i < w; i++) {
287 for (j = k; j < k + 4 && j < h; j++) {
288 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
289 t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno, one, nmsedec, type, vsc);
295 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
297 int i, j, k, one, poshalf, neghalf;
301 neghalf = bpno > 0 ? -poshalf : -1;
302 for (k = 0; k < h; k += 4) {
303 for (i = 0; i < w; i++) {
304 for (j = k; j < k + 4 && j < h; j++) {
305 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
306 t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], poshalf, neghalf, type, vsc);
310 } /* VSC and BYPASS by Antonin */
312 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc)
315 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
318 if (!(*fp & (T1_SIG | T1_VISIT))) {
319 mqc_setcurctx(t1_getctxno_zc(flag, orient));
320 v = int_abs(*dp) & one ? 1 : 0;
324 *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
325 mqc_setcurctx(t1_getctxno_sc(flag));
327 mqc_encode(v ^ t1_getspb(flag));
328 t1_updateflags(fp, v);
335 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc)
338 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
341 if (!(flag & (T1_SIG | T1_VISIT))) {
342 mqc_setcurctx(t1_getctxno_zc(flag, orient));
345 mqc_setcurctx(t1_getctxno_sc(flag));
346 v = mqc_decode() ^ t1_getspb(flag);
347 *dp = v ? -oneplushalf : oneplushalf;
348 t1_updateflags(fp, v);
353 } /* VSC and BYPASS by Antonin */
355 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec, int cblksty)
357 int i, j, k, one, agg, runlen, vsc;
359 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
360 for (k = 0; k < h; k += 4) {
361 for (i = 0; i < w; i++) {
363 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
364 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
365 || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
366 T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
367 T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE
368 | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
370 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
371 || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
372 || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
373 || t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
379 for (runlen = 0; runlen < 4; runlen++) {
380 if (int_abs(t1_data[k + runlen][i]) & one)
383 mqc_setcurctx(T1_CTXNO_AGG);
384 mqc_encode(runlen != 4);
388 mqc_setcurctx(T1_CTXNO_UNI);
389 mqc_encode(runlen >> 1);
390 mqc_encode(runlen & 1);
394 for (j = k + runlen; j < k + 4 && j < h; j++) {
395 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
396 t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, bpno, one, nmsedec, agg
397 && (j == k + runlen), vsc);
403 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
405 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
406 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
409 oneplushalf = one | half;
410 for (k = 0; k < h; k += 4) {
411 for (i = 0; i < w; i++) {
413 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
414 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
415 || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
416 T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
417 T1_SIG_OTH) || (t1_flags[1 + k + 3][1 + i] & (~(T1_SIG_S | T1_SIG_SE |
418 T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
420 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
421 || t1_flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT |
422 T1_SIG_OTH) || t1_flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT |
423 T1_SIG_OTH) || t1_flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT |
430 mqc_setcurctx(T1_CTXNO_AGG);
434 mqc_setcurctx(T1_CTXNO_UNI);
435 runlen = mqc_decode();
436 runlen = (runlen << 1) | mqc_decode();
440 for (j = k + runlen; j < k + 4 && j < h; j++) {
441 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
442 t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], orient, oneplushalf,
443 agg && (j == k + runlen), vsc);
449 mqc_setcurctx(T1_CTXNO_UNI);
451 v = (v << 1) | mqc_decode();
452 v = (v << 1) | mqc_decode();
453 v = (v << 1) | mqc_decode();
456 fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
459 } /* VSC and BYPASS by Antonin */
461 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize)
463 double w1, w2, wmsedec;
465 w1 = mct_getnorm(compno);
466 w2 = dwt_getnorm(level, orient);
467 } else { /* if (qmfbid == 0) */
468 w1 = mct_getnorm_real(compno);
469 w2 = dwt_getnorm_real(level, orient);
471 wmsedec = w1 * w2 * stepsize * (1 << bpno);
472 wmsedec *= wmsedec * nmsedec / 8192.0;
476 void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty)
484 double cumwmsedec = 0;
485 char type = T1_TYPE_MQ;
487 w = cblk->x1 - cblk->x0;
488 h = cblk->y1 - cblk->y0;
491 for (j = 0; j < h; j++) {
492 for (i = 0; i < w; i++) {
493 max = int_max(max, int_abs(t1_data[j][i]));
497 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
499 for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
500 ((int *) t1_flags)[i] = 0;
501 bpno = cblk->numbps - 1;
505 mqc_setstate(T1_CTXNO_UNI, 0, 46);
506 mqc_setstate(T1_CTXNO_AGG, 0, 3);
507 mqc_setstate(T1_CTXNO_ZC, 0, 4);
508 mqc_init_enc(cblk->data);
510 for (passno = 0; bpno >= 0; passno++) {
511 tcd_pass_t *pass = &cblk->passes[passno];
513 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
514 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
518 t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
521 t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
524 t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
525 /* code switch SEGMARK (i.e. SEGSYM) */
526 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
531 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize);
533 /* Code switch "RESTART" (i.e. TERMALL) */
534 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0)))
536 if (type == T1_TYPE_RAW)
540 /* correction = mqc_bypass_flush_enc();*/
543 { /* correction = mqc_restart_enc(); */
550 if (((bpno < (cblk->numbps - 4) && (passtype > 0)) || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
551 && (cblksty & J2K_CCP_CBLKSTY_LAZY))
553 if (type == T1_TYPE_RAW)
557 /* correction = mqc_bypass_flush_enc();*/
560 { /* correction = mqc_restart_enc();*/
577 if (pass->term && bpno > 0) {
578 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
579 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
580 if (type == T1_TYPE_RAW)
581 mqc_bypass_init_enc();
583 mqc_restart_init_enc();
586 pass->distortiondec = cumwmsedec;
587 pass->rate = mqc_numbytes() + correction; /* FIXME */
588 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
590 /* Code-switch "RESET" */
591 if (cblksty & J2K_CCP_CBLKSTY_RESET)
595 /* Code switch "ERTERM" (i.e. PTERM) */
596 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
598 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
601 cblk->totalpasses = passno;
604 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift, int cblksty)
611 char type = T1_TYPE_MQ;
614 for (i = 0; i < sizeof(t1_data) / sizeof(int); i++)
615 ((int *) t1_data)[i] = 0;
616 for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
617 ((int *) t1_flags)[i] = 0;
619 w = cblk->x1 - cblk->x0;
620 h = cblk->y1 - cblk->y0;
621 bpno = roishift + cblk->numbps - 1;
625 mqc_setstate(T1_CTXNO_UNI, 0, 46);
626 mqc_setstate(T1_CTXNO_AGG, 0, 3);
627 mqc_setstate(T1_CTXNO_ZC, 0, 4);
629 for (segno = 0; segno < cblk->numsegs; segno++) {
630 tcd_seg_t *seg = &cblk->segs[segno];
633 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
634 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
635 if (type == T1_TYPE_RAW)
636 raw_init_dec(seg->data, seg->len);
638 mqc_init_dec(seg->data, seg->len);
641 for (passno = 0; passno < seg->numpasses; passno++) {
644 t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
647 t1_dec_refpass(w, h, bpno, type, cblksty);
650 t1_dec_clnpass(w, h, bpno, orient, cblksty);
654 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ)
657 if (++passtype == 3) {
665 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
667 int compno, resno, bandno, precno, cblkno;
668 int x, y, i, j, orient;
669 tcd_tilecomp_t *tilec;
670 tcd_resolution_t *res;
675 for (compno = 0; compno < tile->numcomps; compno++) {
676 tilec = &tile->comps[compno];
677 for (resno = 0; resno < tilec->numresolutions; resno++) {
678 res = &tilec->resolutions[resno];
679 for (bandno = 0; bandno < res->numbands; bandno++) {
680 band = &res->bands[bandno];
681 for (precno = 0; precno < res->pw * res->ph; precno++) {
682 prc = &band->precincts[precno];
683 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
684 cblk = &prc->cblks[cblkno];
686 if (band->bandno == 0) {
687 x = cblk->x0 - band->x0;
688 y = cblk->y0 - band->y0;
689 } else if (band->bandno == 1) {
690 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
691 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
692 y = cblk->y0 - band->y0;
693 } else if (band->bandno == 2) {
694 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
695 x = cblk->x0 - band->x0;
696 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
697 } else { /* if (band->bandno == 3) */
698 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
699 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
700 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
703 if (tcp->tccps[compno].qmfbid == 1) {
705 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
706 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
707 t1_data[j][i] = tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
710 } else if (tcp->tccps[compno].qmfbid == 0) {
711 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
712 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
713 t1_data[j][i] = fix_mul(tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
714 8192 * 8192 / band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
718 orient = band->bandno; /* FIXME */
721 } else if (orient == 1) {
724 t1_encode_cblk(cblk, orient, compno,
725 tilec->numresolutions - 1 - resno,
726 tcp->tccps[compno].qmfbid, band->stepsize,
727 tcp->tccps[compno].cblksty);
736 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
738 int compno, resno, bandno, precno, cblkno;
740 for (compno = 0; compno < tile->numcomps; compno++)
742 tcd_tilecomp_t *tilec = &tile->comps[compno];
743 for (resno = 0; resno < tilec->numresolutions; resno++)
745 tcd_resolution_t *res = &tilec->resolutions[resno];
746 for (bandno = 0; bandno < res->numbands; bandno++)
748 tcd_band_t *band = &res->bands[bandno];
749 for (precno = 0; precno < res->pw * res->ph; precno++)
751 tcd_precinct_t *prc = &band->precincts[precno];
752 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++)
754 int x, y, i, j, orient;
755 tcd_cblk_t *cblk = &prc->cblks[cblkno];
756 orient = band->bandno; /* FIXME */
759 else if (orient == 1)
761 t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
762 if (band->bandno == 0) {
763 x = cblk->x0 - band->x0;
764 y = cblk->y0 - band->y0;
765 } else if (band->bandno == 1) {
766 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
767 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
768 y = cblk->y0 - band->y0;
769 } else if (band->bandno == 2) {
770 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
771 x = cblk->x0 - band->x0;
772 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
773 } else { /* if (band->bandno == 3) */
774 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
775 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
776 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
779 if (tcp->tccps[compno].roishift)
781 int thresh, val, mag;
782 thresh = 1 << tcp->tccps[compno].roishift;
783 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
784 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
788 mag >>= tcp->tccps[compno].roishift;
789 t1_data[j][i] = val < 0 ? -mag : mag;
795 if (tcp->tccps[compno].qmfbid == 1) {
796 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
797 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
798 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = t1_data[j][i];
801 } else /* if (tcp->tccps[compno].qmfbid == 0)*/ {
802 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
803 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
804 if (t1_data[j][i] == 0) {
805 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
807 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = fix_mul(t1_data[j][i] << 13, band->stepsize);
819 int t1_init_ctxno_zc(int f, int orient)
821 int h, v, d, n, t, hv;
823 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
824 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
825 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
881 return T1_CTXNO_ZC + n;
884 int t1_init_ctxno_sc(int f)
888 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
889 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
890 (T1_SIG_W | T1_SGN_W)), 1);
891 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
892 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
893 (T1_SIG_S | T1_SGN_S)), 1);
905 } else if (hc == 1) {
913 return T1_CTXNO_SC + n;
916 int t1_init_ctxno_mag(int f)
919 if (!(f & T1_REFINE))
920 n = (f & (T1_SIG_OTH)) ? 1 : 0;
923 return T1_CTXNO_MAG + n;
926 int t1_init_spb(int f)
929 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) == T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
930 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) == (T1_SIG_E | T1_SGN_E)) + ((f & (T1_SIG_W | T1_SGN_W)) ==
931 (T1_SIG_W | T1_SGN_W)), 1);
932 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) == T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
933 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) == (T1_SIG_N | T1_SGN_N)) + ((f & (T1_SIG_S | T1_SGN_S)) ==
934 (T1_SIG_S | T1_SGN_S)), 1);
938 n = (!(hc > 0 || (!hc && vc > 0)));
946 for (j = 0; j < 4; j++) {
947 for (i = 0; i < 256; ++i) {
948 t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
951 for (i = 0; i < 256; i++) {
952 t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
954 for (j = 0; j < 2; j++) {
955 for (i = 0; i < 2048; ++i) {
956 t1_lut_ctxno_mag[(j << 11) + i] =
957 t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
960 for (i = 0; i < 256; ++i) {
961 t1_lut_spb[i] = t1_init_spb(i << 4);
963 /* FIXME FIXME FIXME */
964 /* printf("nmsedec luts:\n"); */
965 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
966 t = i / pow(2, T1_NMSEDEC_FRACBITS);
969 t1_lut_nmsedec_sig[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
970 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
971 t1_lut_nmsedec_sig0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
972 pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
974 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
979 t1_lut_nmsedec_ref[i] = int_max(0, (int) (floor((u * u - v * v) * pow(2,T1_NMSEDEC_FRACBITS) +
980 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
981 t1_lut_nmsedec_ref0[i] = int_max(0, (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
982 pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));