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_MAXCBLKW];
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) |
111 (((f & T1_REFINE) != 0) << 11)];
116 return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
119 int t1_getnmsedec_sig(int x, int bitpos)
121 if (bitpos > T1_NMSEDEC_FRACBITS)
122 return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
123 ((1 << T1_NMSEDEC_BITS) - 1)];
125 return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
128 int t1_getnmsedec_ref(int x, int bitpos)
130 if (bitpos > T1_NMSEDEC_FRACBITS)
131 return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
132 ((1 << T1_NMSEDEC_BITS) - 1)];
134 return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
137 void t1_updateflags(int *fp, int s)
139 int *np = fp - (T1_MAXCBLKW + 2);
140 int *sp = fp + (T1_MAXCBLKW + 2);
157 void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
158 int *nmsedec, char type, int vsc)
161 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
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 */
169 mqc_setcurctx(t1_getctxno_zc(flag, orient));
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 */
180 mqc_setcurctx(t1_getctxno_sc(flag));
181 mqc_encode(v ^ t1_getspb(flag));
183 t1_updateflags(fp, v);
192 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
196 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
198 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
199 if (type == T1_TYPE_RAW) {
201 v = raw_decode(); /* ESSAI */
202 *dp = v ? -oneplushalf : oneplushalf;
203 t1_updateflags(fp, v);
207 mqc_setcurctx(t1_getctxno_zc(flag, orient));
209 mqc_setcurctx(t1_getctxno_sc(flag));
210 v = mqc_decode() ^ t1_getspb(flag);
211 *dp = v ? -oneplushalf : oneplushalf;
212 t1_updateflags(fp, v);
218 } /* VSC and BYPASS by Antonin */
220 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
221 char type, int cblksty)
223 int i, j, k, one, vsc;
225 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
226 for (k = 0; k < h; k += 4) {
227 for (i = 0; i < w; i++) {
228 for (j = k; j < k + 4 && j < h; j++) {
229 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
230 && (j == k + 3 || j == h - 1)) ? 1 : 0;
231 t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],
232 &t1_data[j][i], orient, bpno, one,
239 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
242 int i, j, k, one, half, oneplushalf, vsc;
245 oneplushalf = one | half;
246 for (k = 0; k < h; k += 4) {
247 for (i = 0; i < w; i++) {
248 for (j = k; j < k + 4 && j < h; j++) {
249 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
250 && (j == k + 3 || j == h - 1)) ? 1 : 0;
251 t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
261 } /* VSC and BYPASS by Antonin */
263 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
267 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
269 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
271 t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
272 v = int_abs(*dp) & one ? 1 : 0;
273 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
274 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
277 mqc_setcurctx(t1_getctxno_mag(flag));
286 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
290 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
292 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
293 if (type == T1_TYPE_RAW) {
294 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
297 mqc_setcurctx(t1_getctxno_mag(flag));
300 t = v ? poshalf : neghalf;
301 *dp += *dp < 0 ? -t : t;
304 } /* VSC and BYPASS by Antonin */
306 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
309 int i, j, k, one, vsc;
311 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
312 for (k = 0; k < h; k += 4) {
313 for (i = 0; i < w; i++) {
314 for (j = k; j < k + 4 && j < h; j++) {
315 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
316 && (j == k + 3 || j == h - 1)) ? 1 : 0;
317 t1_enc_refpass_step(&t1_flags[1 + j][1 + i],
318 &t1_data[j][i], bpno, one, nmsedec, type, vsc);
324 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
326 int i, j, k, one, poshalf, neghalf;
330 neghalf = bpno > 0 ? -poshalf : -1;
331 for (k = 0; k < h; k += 4) {
332 for (i = 0; i < w; i++) {
333 for (j = k; j < k + 4 && j < h; j++) {
334 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
335 && (j == k + 3 || j == h - 1)) ? 1 : 0;
336 t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
347 } /* VSC and BYPASS by Antonin */
349 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
350 int *nmsedec, int partial, int vsc)
353 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
357 if (!(*fp & (T1_SIG | T1_VISIT))) {
358 mqc_setcurctx(t1_getctxno_zc(flag, orient));
359 v = int_abs(*dp) & one ? 1 : 0;
364 t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
365 mqc_setcurctx(t1_getctxno_sc(flag));
367 mqc_encode(v ^ t1_getspb(flag));
368 t1_updateflags(fp, v);
378 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
379 int partial, int vsc)
382 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
386 if (!(flag & (T1_SIG | T1_VISIT))) {
387 mqc_setcurctx(t1_getctxno_zc(flag, orient));
390 mqc_setcurctx(t1_getctxno_sc(flag));
391 v = mqc_decode() ^ t1_getspb(flag);
392 *dp = v ? -oneplushalf : oneplushalf;
393 t1_updateflags(fp, v);
398 } /* VSC and BYPASS by Antonin */
400 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
403 int i, j, k, one, agg, runlen, vsc;
405 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
406 for (k = 0; k < h; k += 4) {
407 for (i = 0; i < w; i++) {
409 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
410 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
411 || t1_flags[1 + k + 1][1 +
412 i] & (T1_SIG | T1_VISIT |
414 || t1_flags[1 + k + 2][1 +
415 i] & (T1_SIG | T1_VISIT |
417 || (t1_flags[1 + k + 3][1 + i] &
419 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
420 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
422 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
423 || t1_flags[1 + k + 1][1 +
424 i] & (T1_SIG | T1_VISIT |
426 || t1_flags[1 + k + 2][1 +
427 i] & (T1_SIG | T1_VISIT |
429 || t1_flags[1 + k + 3][1 +
430 i] & (T1_SIG | T1_VISIT |
437 for (runlen = 0; runlen < 4; runlen++) {
438 if (int_abs(t1_data[k + runlen][i]) & one)
441 mqc_setcurctx(T1_CTXNO_AGG);
442 mqc_encode(runlen != 4);
446 mqc_setcurctx(T1_CTXNO_UNI);
447 mqc_encode(runlen >> 1);
448 mqc_encode(runlen & 1);
452 for (j = k + runlen; j < k + 4 && j < h; j++) {
453 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
454 && (j == k + 3 || j == h - 1)) ? 1 : 0;
455 t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],
456 &t1_data[j][i], orient, bpno, one,
457 nmsedec, agg && (j == k + runlen), vsc);
463 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
465 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
466 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
469 oneplushalf = one | half;
470 for (k = 0; k < h; k += 4) {
471 for (i = 0; i < w; i++) {
473 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
474 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
475 || t1_flags[1 + k + 1][1 +
476 i] & (T1_SIG | T1_VISIT |
478 || t1_flags[1 + k + 2][1 +
479 i] & (T1_SIG | T1_VISIT |
481 || (t1_flags[1 + k + 3][1 + i] &
483 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
484 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
486 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
487 || t1_flags[1 + k + 1][1 +
488 i] & (T1_SIG | T1_VISIT |
490 || t1_flags[1 + k + 2][1 +
491 i] & (T1_SIG | T1_VISIT |
493 || t1_flags[1 + k + 3][1 +
494 i] & (T1_SIG | T1_VISIT |
501 mqc_setcurctx(T1_CTXNO_AGG);
505 mqc_setcurctx(T1_CTXNO_UNI);
506 runlen = mqc_decode();
507 runlen = (runlen << 1) | mqc_decode();
511 for (j = k + runlen; j < k + 4 && j < h; j++) {
512 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
513 && (j == k + 3 || j == h - 1)) ? 1 : 0;
514 t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
520 agg && (j == k + runlen), vsc);
526 mqc_setcurctx(T1_CTXNO_UNI);
528 v = (v << 1) | mqc_decode();
529 v = (v << 1) | mqc_decode();
530 v = (v << 1) | mqc_decode();
533 fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
536 } /* VSC and BYPASS by Antonin */
538 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, float stepsize, int numcomps) /*mod fixed_quality*/
540 double w1, w2, wmsedec;
542 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
543 w2 = dwt_getnorm(level, orient);
544 } else { /* if (qmfbid == 0) */
545 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
546 w2 = dwt_getnorm_real(level, orient);
548 wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
549 wmsedec *= wmsedec * nmsedec / 8192.0;
553 void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, float stepsize, int cblksty, int numcomps, tcd_tile_t * tile) /*mod fixed_quality*/
561 double cumwmsedec = 0;
562 char type = T1_TYPE_MQ;
564 w = cblk->x1 - cblk->x0;
565 h = cblk->y1 - cblk->y0;
568 for (j = 0; j < h; j++) {
569 for (i = 0; i < w; i++) {
570 max = int_max(max, int_abs(t1_data[j][i]));
574 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
576 /* Changed by Dmitry Kolyadin */
577 for (i = 0; i <= w; i++)
578 for (j = 0; j <= h; j++){
582 bpno = cblk->numbps - 1;
586 mqc_setstate(T1_CTXNO_UNI, 0, 46);
587 mqc_setstate(T1_CTXNO_AGG, 0, 3);
588 mqc_setstate(T1_CTXNO_ZC, 0, 4);
589 mqc_init_enc(cblk->data);
591 for (passno = 0; bpno >= 0; passno++) {
592 tcd_pass_t *pass = &cblk->passes[passno];
594 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
595 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
600 t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
603 t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
606 t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
607 /* code switch SEGMARK (i.e. SEGSYM) */
608 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
613 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*mod fixed_quality*/
614 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*add antonin quality*/
617 /* Code switch "RESTART" (i.e. TERMALL) */
618 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
619 && !((passtype == 2) && (bpno - 1 < 0))) {
620 if (type == T1_TYPE_RAW) {
623 /* correction = mqc_bypass_flush_enc(); */
624 } else { /* correction = mqc_restart_enc(); */
630 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
631 || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
632 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
633 if (type == T1_TYPE_RAW) {
636 /* correction = mqc_bypass_flush_enc(); */
637 } else { /* correction = mqc_restart_enc(); */
647 if (++passtype == 3) {
652 if (pass->term && bpno > 0) {
653 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
654 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
656 if (type == T1_TYPE_RAW)
657 mqc_bypass_init_enc();
659 mqc_restart_init_enc();
662 pass->distortiondec = cumwmsedec;
663 pass->rate = mqc_numbytes() + correction; /* FIXME */
665 pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
667 /* Code-switch "RESET" */
668 if (cblksty & J2K_CCP_CBLKSTY_RESET)
672 /* Code switch "ERTERM" (i.e. PTERM) */
673 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
675 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
678 cblk->totalpasses = passno;
681 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
687 char type = T1_TYPE_MQ; /*BYPASS mode*/
689 w = cblk->x1 - cblk->x0;
690 h = cblk->y1 - cblk->y0;
692 /* Changed by Dmitry Kolyadin */
693 for (j = 0; j <= h; j++){
694 for (i = 0; i <= w; i++) {
699 /* Changed by Dmitry Kolyadin */
700 for (i = 0; i < w; i++) {
701 for (j = 0; j < h; j++){
706 bpno = roishift + cblk->numbps - 1;
710 mqc_setstate(T1_CTXNO_UNI, 0, 46);
711 mqc_setstate(T1_CTXNO_AGG, 0, 3);
712 mqc_setstate(T1_CTXNO_ZC, 0, 4);
714 for (segno = 0; segno < cblk->numsegs; segno++) {
715 tcd_seg_t *seg = &cblk->segs[segno];
717 /* Add BYPASS mode */
718 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
719 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
721 if (type == T1_TYPE_RAW)
722 raw_init_dec(seg->data, seg->len);
724 mqc_init_dec(seg->data, seg->len);
727 for (passno = 0; passno < seg->numpasses; passno++) {
730 t1_dec_sigpass(w, h, bpno+1, orient, type, cblksty);
733 t1_dec_refpass(w, h, bpno+1, type, cblksty);
736 t1_dec_clnpass(w, h, bpno+1, orient, cblksty);
740 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
743 mqc_setstate(T1_CTXNO_UNI, 0, 46);
745 mqc_setstate(T1_CTXNO_AGG, 0, 3);
747 mqc_setstate(T1_CTXNO_ZC, 0, 4);
751 if (++passtype == 3) {
759 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
761 int compno, resno, bandno, precno, cblkno;
762 int x, y, i, j, orient;
763 tcd_tilecomp_t *tilec;
764 tcd_resolution_t *res;
769 tile->distotile = 0; /*add fixed_quality*/
771 for (compno = 0; compno < tile->numcomps; compno++) {
772 tilec = &tile->comps[compno];
773 for (resno = 0; resno < tilec->numresolutions; resno++) {
774 res = &tilec->resolutions[resno];
775 for (bandno = 0; bandno < res->numbands; bandno++) {
776 band = &res->bands[bandno];
777 for (precno = 0; precno < res->pw * res->ph; precno++) {
778 prc = &band->precincts[precno];
779 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
780 cblk = &prc->cblks[cblkno];
782 if (band->bandno == 0) {
783 x = cblk->x0 - band->x0;
784 y = cblk->y0 - band->y0;
785 } else if (band->bandno == 1) {
786 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
787 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
788 y = cblk->y0 - band->y0;
789 } else if (band->bandno == 2) {
790 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
791 x = cblk->x0 - band->x0;
792 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
793 } else { /* if (band->bandno == 3) */
794 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
795 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
796 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
799 if (tcp->tccps[compno].qmfbid == 1) {
801 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
802 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
804 tilec->data[(x + i) +
805 (y + j) * (tilec->x1 -
807 x0)] << T1_NMSEDEC_FRACBITS;
810 } else if (tcp->tccps[compno].qmfbid == 0) {
811 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
812 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
816 (y + j) * (tilec->x1 -
820 ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
824 orient = band->bandno; /* FIXME */
827 } else if (orient == 1) {
830 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*/
839 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
841 int compno, resno, bandno, precno, cblkno;
843 for (compno = 0; compno < tile->numcomps; compno++) {
844 tcd_tilecomp_t *tilec = &tile->comps[compno];
845 for (resno = 0; resno < tilec->numresolutions; resno++) {
846 tcd_resolution_t *res = &tilec->resolutions[resno];
847 for (bandno = 0; bandno < res->numbands; bandno++) {
848 tcd_band_t *band = &res->bands[bandno];
849 for (precno = 0; precno < res->pw * res->ph; precno++) {
850 tcd_precinct_t *prc = &band->precincts[precno];
851 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
852 int x, y, i, j, orient;
853 tcd_cblk_t *cblk = &prc->cblks[cblkno];
854 orient = band->bandno; /* FIXME */
857 else if (orient == 1)
859 t1_decode_cblk(cblk, orient,
860 tcp->tccps[compno].roishift,
861 tcp->tccps[compno].cblksty);
862 if (band->bandno == 0) {
863 x = cblk->x0 - band->x0;
864 y = cblk->y0 - band->y0;
865 } else if (band->bandno == 1) {
866 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
867 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
868 y = cblk->y0 - band->y0;
869 } else if (band->bandno == 2) {
870 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
871 x = cblk->x0 - band->x0;
872 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
873 } else { /* if (band->bandno == 3) */
874 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
875 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
876 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
879 if (tcp->tccps[compno].roishift) {
880 int thresh, val, mag;
881 thresh = 1 << tcp->tccps[compno].roishift;
882 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
883 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
887 mag >>= tcp->tccps[compno].roishift;
888 t1_data[j][i] = val < 0 ? -mag : mag;
894 if (tcp->tccps[compno].qmfbid == 1) {
895 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
896 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
897 int tmp=t1_data[j][i];
898 if (tmp>>1==0) tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
899 else tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
902 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
904 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
905 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
906 double tmp=t1_data[j][i] * band->stepsize * 4096.0;
908 if (t1_data[j][i]>>1 == 0) {
909 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
911 tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
912 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);
924 int t1_init_ctxno_zc(int f, int orient)
926 int h, v, d, n, t, hv;
928 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
929 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
930 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
931 0) + ((f & T1_SIG_SE) !=
932 0) + ((f & T1_SIG_SW) != 0);
988 return T1_CTXNO_ZC + n;
991 int t1_init_ctxno_sc(int f)
995 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
996 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
997 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
998 (T1_SIG_E | T1_SGN_E)) +
999 ((f & (T1_SIG_W | T1_SGN_W)) ==
1000 (T1_SIG_W | T1_SGN_W)), 1);
1001 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1002 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1003 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1004 (T1_SIG_N | T1_SGN_N)) +
1005 ((f & (T1_SIG_S | T1_SGN_S)) ==
1006 (T1_SIG_S | T1_SGN_S)), 1);
1018 } else if (hc == 1) {
1026 return T1_CTXNO_SC + n;
1029 int t1_init_ctxno_mag(int f)
1032 if (!(f & T1_REFINE))
1033 n = (f & (T1_SIG_OTH)) ? 1 : 0;
1036 return T1_CTXNO_MAG + n;
1039 int t1_init_spb(int f)
1042 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1043 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1044 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1045 (T1_SIG_E | T1_SGN_E)) +
1046 ((f & (T1_SIG_W | T1_SGN_W)) ==
1047 (T1_SIG_W | T1_SGN_W)), 1);
1048 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1049 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1050 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1051 (T1_SIG_N | T1_SGN_N)) +
1052 ((f & (T1_SIG_S | T1_SGN_S)) ==
1053 (T1_SIG_S | T1_SGN_S)), 1);
1057 n = (!(hc > 0 || (!hc && vc > 0)));
1065 for (j = 0; j < 4; j++) {
1066 for (i = 0; i < 256; ++i) {
1067 t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
1070 for (i = 0; i < 256; i++) {
1071 t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
1073 for (j = 0; j < 2; j++) {
1074 for (i = 0; i < 2048; ++i) {
1075 t1_lut_ctxno_mag[(j << 11) + i] =
1076 t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
1079 for (i = 0; i < 256; ++i) {
1080 t1_lut_spb[i] = t1_init_spb(i << 4);
1082 /* FIXME FIXME FIXME */
1083 /* fprintf(stdout,"nmsedec luts:\n"); */
1084 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1085 t = i / pow(2, T1_NMSEDEC_FRACBITS);
1088 t1_lut_nmsedec_sig[i] =
1091 ((u * u - v * v) * pow(2,
1092 T1_NMSEDEC_FRACBITS) +
1093 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1094 t1_lut_nmsedec_sig0[i] =
1097 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1098 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1100 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1105 t1_lut_nmsedec_ref[i] =
1108 ((u * u - v * v) * pow(2,
1109 T1_NMSEDEC_FRACBITS) +
1110 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1111 t1_lut_nmsedec_ref0[i] =
1114 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1115 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));