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) |
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);
190 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
194 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
196 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
197 if (type == T1_TYPE_RAW) {
199 v = raw_decode(); /* ESSAI */
200 *dp = v ? -oneplushalf : oneplushalf;
201 t1_updateflags(fp, v);
205 mqc_setcurctx(t1_getctxno_zc(flag, orient));
207 mqc_setcurctx(t1_getctxno_sc(flag));
208 v = mqc_decode() ^ t1_getspb(flag);
209 *dp = v ? -oneplushalf : oneplushalf;
210 t1_updateflags(fp, v);
216 } /* VSC and BYPASS by Antonin */
218 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
219 char type, int cblksty)
221 int i, j, k, one, vsc;
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,
237 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
240 int i, j, k, one, half, oneplushalf, vsc;
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,
255 } /* VSC and BYPASS by Antonin */
257 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
261 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
263 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
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 */
271 mqc_setcurctx(t1_getctxno_mag(flag));
278 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
282 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
284 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
285 if (type == T1_TYPE_RAW) {
286 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
289 mqc_setcurctx(t1_getctxno_mag(flag));
292 t = v ? poshalf : neghalf;
293 *dp += *dp < 0 ? -t : t;
296 } /* VSC and BYPASS by Antonin */
298 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
301 int i, j, k, one, vsc;
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);
316 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
318 int i, j, k, one, poshalf, neghalf;
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);
333 } /* VSC and BYPASS by Antonin */
335 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
336 int *nmsedec, int partial, int vsc)
339 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
343 if (!(*fp & (T1_SIG | T1_VISIT))) {
344 mqc_setcurctx(t1_getctxno_zc(flag, orient));
345 v = int_abs(*dp) & one ? 1 : 0;
350 t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
351 mqc_setcurctx(t1_getctxno_sc(flag));
353 mqc_encode(v ^ t1_getspb(flag));
354 t1_updateflags(fp, v);
361 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
362 int partial, int vsc)
365 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
369 if (!(flag & (T1_SIG | T1_VISIT))) {
370 mqc_setcurctx(t1_getctxno_zc(flag, orient));
373 mqc_setcurctx(t1_getctxno_sc(flag));
374 v = mqc_decode() ^ t1_getspb(flag);
375 *dp = v ? -oneplushalf : oneplushalf;
376 t1_updateflags(fp, v);
381 } /* VSC and BYPASS by Antonin */
383 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
386 int i, j, k, one, agg, runlen, vsc;
388 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
389 for (k = 0; k < h; k += 4) {
390 for (i = 0; i < w; i++) {
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 |
397 || t1_flags[1 + k + 2][1 +
398 i] & (T1_SIG | T1_VISIT |
400 || (t1_flags[1 + k + 3][1 + i] &
402 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
403 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
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 |
409 || t1_flags[1 + k + 2][1 +
410 i] & (T1_SIG | T1_VISIT |
412 || t1_flags[1 + k + 3][1 +
413 i] & (T1_SIG | T1_VISIT |
420 for (runlen = 0; runlen < 4; runlen++) {
421 if (int_abs(t1_data[k + runlen][i]) & one)
424 mqc_setcurctx(T1_CTXNO_AGG);
425 mqc_encode(runlen != 4);
429 mqc_setcurctx(T1_CTXNO_UNI);
430 mqc_encode(runlen >> 1);
431 mqc_encode(runlen & 1);
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);
446 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
448 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
449 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
452 oneplushalf = one | half;
453 for (k = 0; k < h; k += 4) {
454 for (i = 0; i < w; i++) {
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 |
461 || t1_flags[1 + k + 2][1 +
462 i] & (T1_SIG | T1_VISIT |
464 || (t1_flags[1 + k + 3][1 + i] &
466 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
467 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
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 |
473 || t1_flags[1 + k + 2][1 +
474 i] & (T1_SIG | T1_VISIT |
476 || t1_flags[1 + k + 3][1 +
477 i] & (T1_SIG | T1_VISIT |
484 mqc_setcurctx(T1_CTXNO_AGG);
488 mqc_setcurctx(T1_CTXNO_UNI);
489 runlen = mqc_decode();
490 runlen = (runlen << 1) | mqc_decode();
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);
505 mqc_setcurctx(T1_CTXNO_UNI);
507 v = (v << 1) | mqc_decode();
508 v = (v << 1) | mqc_decode();
509 v = (v << 1) | mqc_decode();
512 fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
515 } /* VSC and BYPASS by Antonin */
517 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) //mod fixed_quality
519 double w1, w2, wmsedec;
521 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
522 w2 = dwt_getnorm(level, orient);
523 } else { /* if (qmfbid == 0) */
524 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
525 w2 = dwt_getnorm_real(level, orient);
527 wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
528 wmsedec *= wmsedec * nmsedec / 8192.0;
532 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
540 double cumwmsedec = 0;
541 char type = T1_TYPE_MQ;
543 w = cblk->x1 - cblk->x0;
544 h = cblk->y1 - cblk->y0;
547 for (j = 0; j < h; j++) {
548 for (i = 0; i < w; i++) {
549 max = int_max(max, int_abs(t1_data[j][i]));
553 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
555 for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
556 ((int *) t1_flags)[i] = 0;
557 bpno = cblk->numbps - 1;
561 mqc_setstate(T1_CTXNO_UNI, 0, 46);
562 mqc_setstate(T1_CTXNO_AGG, 0, 3);
563 mqc_setstate(T1_CTXNO_ZC, 0, 4);
564 mqc_init_enc(cblk->data);
566 for (passno = 0; bpno >= 0; passno++) {
567 tcd_pass_t *pass = &cblk->passes[passno];
569 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
570 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
575 t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
578 t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
581 t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
582 /* code switch SEGMARK (i.e. SEGSYM) */
583 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
588 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //mod fixed_quality
589 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //add antonin quality
592 /* Code switch "RESTART" (i.e. TERMALL) */
593 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
594 && !((passtype == 2) && (bpno - 1 < 0))) {
595 if (type == T1_TYPE_RAW) {
598 /* correction = mqc_bypass_flush_enc(); */
599 } else { /* correction = mqc_restart_enc(); */
605 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
606 || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
607 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
608 if (type == T1_TYPE_RAW) {
611 /* correction = mqc_bypass_flush_enc(); */
612 } else { /* correction = mqc_restart_enc(); */
622 if (++passtype == 3) {
627 if (pass->term && bpno > 0) {
628 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
629 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
631 if (type == T1_TYPE_RAW)
632 mqc_bypass_init_enc();
634 mqc_restart_init_enc();
637 pass->distortiondec = cumwmsedec;
638 pass->rate = mqc_numbytes() + correction; /* FIXME */
640 pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
642 /* Code-switch "RESET" */
643 if (cblksty & J2K_CCP_CBLKSTY_RESET)
647 /* Code switch "ERTERM" (i.e. PTERM) */
648 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
650 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
653 cblk->totalpasses = passno;
656 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
664 char type = T1_TYPE_MQ;
667 for (i = 0; i < sizeof(t1_data) / sizeof(int); i++)
668 ((int *) t1_data)[i] = 0;
669 for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
670 ((int *) t1_flags)[i] = 0;
672 w = cblk->x1 - cblk->x0;
673 h = cblk->y1 - cblk->y0;
674 bpno = roishift + cblk->numbps - 1;
678 mqc_setstate(T1_CTXNO_UNI, 0, 46);
679 mqc_setstate(T1_CTXNO_AGG, 0, 3);
680 mqc_setstate(T1_CTXNO_ZC, 0, 4);
682 for (segno = 0; segno < cblk->numsegs; segno++) {
683 tcd_seg_t *seg = &cblk->segs[segno];
686 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
687 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
689 if (type == T1_TYPE_RAW)
690 raw_init_dec(seg->data, seg->len);
692 mqc_init_dec(seg->data, seg->len);
695 for (passno = 0; passno < seg->numpasses; passno++) {
698 t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
701 t1_dec_refpass(w, h, bpno, type, cblksty);
704 t1_dec_clnpass(w, h, bpno, orient, cblksty);
708 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ)
711 if (++passtype == 3) {
719 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
721 int compno, resno, bandno, precno, cblkno;
722 int x, y, i, j, orient;
723 tcd_tilecomp_t *tilec;
724 tcd_resolution_t *res;
729 tile->distotile = 0; //add fixed_quality
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];
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;
759 if (tcp->tccps[compno].qmfbid == 1) {
761 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
762 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
764 tilec->data[(x + i) +
765 (y + j) * (tilec->x1 -
767 x0)] << T1_NMSEDEC_FRACBITS;
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++) {
776 (y + j) * (tilec->x1 -
780 band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
784 orient = band->bandno; /* FIXME */
787 } else if (orient == 1) {
790 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
799 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
801 int compno, resno, bandno, precno, cblkno;
803 for (compno = 0; compno < tile->numcomps; compno++) {
804 tcd_tilecomp_t *tilec = &tile->comps[compno];
805 for (resno = 0; resno < tilec->numresolutions; resno++) {
806 tcd_resolution_t *res = &tilec->resolutions[resno];
807 for (bandno = 0; bandno < res->numbands; bandno++) {
808 tcd_band_t *band = &res->bands[bandno];
809 for (precno = 0; precno < res->pw * res->ph; precno++) {
810 tcd_precinct_t *prc = &band->precincts[precno];
811 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
812 int x, y, i, j, orient;
813 tcd_cblk_t *cblk = &prc->cblks[cblkno];
814 orient = band->bandno; /* FIXME */
817 else if (orient == 1)
819 t1_decode_cblk(cblk, orient,
820 tcp->tccps[compno].roishift,
821 tcp->tccps[compno].cblksty);
822 if (band->bandno == 0) {
823 x = cblk->x0 - band->x0;
824 y = cblk->y0 - band->y0;
825 } else if (band->bandno == 1) {
826 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
827 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
828 y = cblk->y0 - band->y0;
829 } else if (band->bandno == 2) {
830 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
831 x = cblk->x0 - band->x0;
832 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
833 } else { /* if (band->bandno == 3) */
834 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
835 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
836 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
839 if (tcp->tccps[compno].roishift) {
840 int thresh, val, mag;
841 thresh = 1 << tcp->tccps[compno].roishift;
842 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
843 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
847 mag >>= tcp->tccps[compno].roishift;
848 t1_data[j][i] = val < 0 ? -mag : mag;
854 if (tcp->tccps[compno].qmfbid == 1) {
855 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
856 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
858 (y + j) * (tilec->x1 -
859 tilec->x0)] = t1_data[j][i];
862 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
864 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
865 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
866 if (t1_data[j][i] == 0) {
868 (y + j) * (tilec->x1 - tilec->x0)] = 0;
871 (y + j) * (tilec->x1 -
874 fix_mul(t1_data[j][i] << 13, band->stepsize);
886 int t1_init_ctxno_zc(int f, int orient)
888 int h, v, d, n, t, hv;
890 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
891 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
892 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
893 0) + ((f & T1_SIG_SE) !=
894 0) + ((f & T1_SIG_SW) != 0);
950 return T1_CTXNO_ZC + n;
953 int t1_init_ctxno_sc(int f)
957 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
958 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
959 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
960 (T1_SIG_E | T1_SGN_E)) +
961 ((f & (T1_SIG_W | T1_SGN_W)) ==
962 (T1_SIG_W | T1_SGN_W)), 1);
963 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
964 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
965 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
966 (T1_SIG_N | T1_SGN_N)) +
967 ((f & (T1_SIG_S | T1_SGN_S)) ==
968 (T1_SIG_S | T1_SGN_S)), 1);
980 } else if (hc == 1) {
988 return T1_CTXNO_SC + n;
991 int t1_init_ctxno_mag(int f)
994 if (!(f & T1_REFINE))
995 n = (f & (T1_SIG_OTH)) ? 1 : 0;
998 return T1_CTXNO_MAG + n;
1001 int t1_init_spb(int f)
1004 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1005 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1006 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1007 (T1_SIG_E | T1_SGN_E)) +
1008 ((f & (T1_SIG_W | T1_SGN_W)) ==
1009 (T1_SIG_W | T1_SGN_W)), 1);
1010 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1011 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1012 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1013 (T1_SIG_N | T1_SGN_N)) +
1014 ((f & (T1_SIG_S | T1_SGN_S)) ==
1015 (T1_SIG_S | T1_SGN_S)), 1);
1019 n = (!(hc > 0 || (!hc && vc > 0)));
1027 for (j = 0; j < 4; j++) {
1028 for (i = 0; i < 256; ++i) {
1029 t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
1032 for (i = 0; i < 256; i++) {
1033 t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
1035 for (j = 0; j < 2; j++) {
1036 for (i = 0; i < 2048; ++i) {
1037 t1_lut_ctxno_mag[(j << 11) + i] =
1038 t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
1041 for (i = 0; i < 256; ++i) {
1042 t1_lut_spb[i] = t1_init_spb(i << 4);
1044 /* FIXME FIXME FIXME */
1045 /* printf("nmsedec luts:\n"); */
1046 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1047 t = i / pow(2, T1_NMSEDEC_FRACBITS);
1050 t1_lut_nmsedec_sig[i] =
1053 ((u * u - v * v) * pow(2,
1054 T1_NMSEDEC_FRACBITS) +
1055 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1056 t1_lut_nmsedec_sig0[i] =
1059 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1060 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1062 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1067 t1_lut_nmsedec_ref[i] =
1070 ((u * u - v * v) * pow(2,
1071 T1_NMSEDEC_FRACBITS) +
1072 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1073 t1_lut_nmsedec_ref0[i] =
1076 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1077 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));