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 */
42 #define T1_MAXCBLKW 1024
43 #define T1_MAXCBLKH 1024
45 #define T1_SIG_NE 0x0001
46 #define T1_SIG_SE 0x0002
47 #define T1_SIG_SW 0x0004
48 #define T1_SIG_NW 0x0008
49 #define T1_SIG_N 0x0010
50 #define T1_SIG_E 0x0020
51 #define T1_SIG_S 0x0040
52 #define T1_SIG_W 0x0080
53 #define T1_SIG_OTH (T1_SIG_N|T1_SIG_NE|T1_SIG_E|T1_SIG_SE|T1_SIG_S|T1_SIG_SW|T1_SIG_W|T1_SIG_NW)
54 #define T1_SIG_PRIM (T1_SIG_N|T1_SIG_E|T1_SIG_S|T1_SIG_W)
56 #define T1_SGN_N 0x0100
57 #define T1_SGN_E 0x0200
58 #define T1_SGN_S 0x0400
59 #define T1_SGN_W 0x0800
60 #define T1_SGN (T1_SGN_N|T1_SGN_E|T1_SGN_S|T1_SGN_W)
63 #define T1_REFINE 0x2000
64 #define T1_VISIT 0x4000
66 #define T1_NUMCTXS_AGG 1
67 #define T1_NUMCTXS_ZC 9
68 #define T1_NUMCTXS_MAG 3
69 #define T1_NUMCTXS_SC 5
70 #define T1_NUMCTXS_UNI 1
72 #define T1_CTXNO_AGG 0
73 #define T1_CTXNO_ZC (T1_CTXNO_AGG+T1_NUMCTXS_AGG)
74 #define T1_CTXNO_MAG (T1_CTXNO_ZC+T1_NUMCTXS_ZC)
75 #define T1_CTXNO_SC (T1_CTXNO_MAG+T1_NUMCTXS_MAG)
76 #define T1_CTXNO_UNI (T1_CTXNO_SC+T1_NUMCTXS_SC)
77 #define T1_NUMCTXS (T1_CTXNO_UNI+T1_NUMCTXS_UNI)
79 #define T1_NMSEDEC_BITS 7
80 #define T1_NMSEDEC_FRACBITS (T1_NMSEDEC_BITS-1)
87 static int t1_lut_ctxno_zc[1024];
88 static int t1_lut_ctxno_sc[256];
89 static int t1_lut_ctxno_mag[4096];
90 static int t1_lut_spb[256];
91 static int t1_lut_nmsedec_sig[1 << T1_NMSEDEC_BITS];
92 static int t1_lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
93 static int t1_lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
94 static int t1_lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
96 static int t1_data[T1_MAXCBLKH][T1_MAXCBLKW];
97 static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];
99 int t1_getctxno_zc(int f, int orient)
101 return t1_lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
104 int t1_getctxno_sc(int f)
106 return t1_lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
109 int t1_getctxno_mag(int f)
111 return t1_lut_ctxno_mag[(f & T1_SIG_OTH) |
112 (((f & T1_REFINE) != 0) << 11)];
117 return t1_lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
120 int t1_getnmsedec_sig(int x, int bitpos)
122 if (bitpos > T1_NMSEDEC_FRACBITS)
123 return t1_lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
124 ((1 << T1_NMSEDEC_BITS) - 1)];
126 return t1_lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
129 int t1_getnmsedec_ref(int x, int bitpos)
131 if (bitpos > T1_NMSEDEC_FRACBITS)
132 return t1_lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) &
133 ((1 << T1_NMSEDEC_BITS) - 1)];
135 return t1_lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
138 void t1_updateflags(int *fp, int s)
140 int *np = fp - (T1_MAXCBLKW + 2);
141 int *sp = fp + (T1_MAXCBLKW + 2);
158 void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
159 int *nmsedec, char type, int vsc)
162 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
164 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
165 v = int_abs(*dp) & one ? 1 : 0;
166 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
167 mqc_setcurctx(t1_getctxno_zc(flag, orient)); /* ESSAI */
170 mqc_setcurctx(t1_getctxno_zc(flag, orient));
176 t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
177 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
178 mqc_setcurctx(t1_getctxno_sc(flag)); /* ESSAI */
181 mqc_setcurctx(t1_getctxno_sc(flag));
182 mqc_encode(v ^ t1_getspb(flag));
184 t1_updateflags(fp, v);
193 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
197 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
199 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
200 if (type == T1_TYPE_RAW) {
202 v = raw_decode(); /* ESSAI */
203 *dp = v ? -oneplushalf : oneplushalf;
204 t1_updateflags(fp, v);
208 mqc_setcurctx(t1_getctxno_zc(flag, orient));
210 mqc_setcurctx(t1_getctxno_sc(flag));
211 v = mqc_decode() ^ t1_getspb(flag);
212 *dp = v ? -oneplushalf : oneplushalf;
213 t1_updateflags(fp, v);
219 } /* VSC and BYPASS by Antonin */
221 void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
222 char type, int cblksty)
224 int i, j, k, one, vsc;
226 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
227 for (k = 0; k < h; k += 4) {
228 for (i = 0; i < w; i++) {
229 for (j = k; j < k + 4 && j < h; j++) {
230 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
231 && (j == k + 3 || j == h - 1)) ? 1 : 0;
232 t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],
233 &t1_data[j][i], orient, bpno, one,
240 void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
243 int i, j, k, one, half, oneplushalf, vsc;
246 oneplushalf = one | half;
247 for (k = 0; k < h; k += 4) {
248 for (i = 0; i < w; i++) {
249 for (j = k; j < k + 4 && j < h; j++) {
250 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
251 && (j == k + 3 || j == h - 1)) ? 1 : 0;
252 t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
262 } /* VSC and BYPASS by Antonin */
264 void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
268 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
270 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
272 t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
273 v = int_abs(*dp) & one ? 1 : 0;
274 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
275 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
278 mqc_setcurctx(t1_getctxno_mag(flag));
287 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
291 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
293 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
294 if (type == T1_TYPE_RAW) {
295 mqc_setcurctx(t1_getctxno_mag(flag)); /* ESSAI */
298 mqc_setcurctx(t1_getctxno_mag(flag));
301 t = v ? poshalf : neghalf;
302 *dp += *dp < 0 ? -t : t;
305 } /* VSC and BYPASS by Antonin */
307 void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
310 int i, j, k, one, vsc;
312 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
313 for (k = 0; k < h; k += 4) {
314 for (i = 0; i < w; i++) {
315 for (j = k; j < k + 4 && j < h; j++) {
316 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
317 && (j == k + 3 || j == h - 1)) ? 1 : 0;
318 t1_enc_refpass_step(&t1_flags[1 + j][1 + i],
319 &t1_data[j][i], bpno, one, nmsedec, type, vsc);
325 void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
327 int i, j, k, one, poshalf, neghalf;
331 neghalf = bpno > 0 ? -poshalf : -1;
332 for (k = 0; k < h; k += 4) {
333 for (i = 0; i < w; i++) {
334 for (j = k; j < k + 4 && j < h; j++) {
335 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
336 && (j == k + 3 || j == h - 1)) ? 1 : 0;
337 t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
348 } /* VSC and BYPASS by Antonin */
350 void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
351 int *nmsedec, int partial, int vsc)
354 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
358 if (!(*fp & (T1_SIG | T1_VISIT))) {
359 mqc_setcurctx(t1_getctxno_zc(flag, orient));
360 v = int_abs(*dp) & one ? 1 : 0;
365 t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
366 mqc_setcurctx(t1_getctxno_sc(flag));
368 mqc_encode(v ^ t1_getspb(flag));
369 t1_updateflags(fp, v);
379 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
380 int partial, int vsc)
383 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
387 if (!(flag & (T1_SIG | T1_VISIT))) {
388 mqc_setcurctx(t1_getctxno_zc(flag, orient));
391 mqc_setcurctx(t1_getctxno_sc(flag));
392 v = mqc_decode() ^ t1_getspb(flag);
393 *dp = v ? -oneplushalf : oneplushalf;
394 t1_updateflags(fp, v);
399 } /* VSC and BYPASS by Antonin */
401 void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
404 int i, j, k, one, agg, runlen, vsc;
406 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
407 for (k = 0; k < h; k += 4) {
408 for (i = 0; i < w; i++) {
410 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
411 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
412 || t1_flags[1 + k + 1][1 +
413 i] & (T1_SIG | T1_VISIT |
415 || t1_flags[1 + k + 2][1 +
416 i] & (T1_SIG | T1_VISIT |
418 || (t1_flags[1 + k + 3][1 + i] &
420 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
421 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
423 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
424 || t1_flags[1 + k + 1][1 +
425 i] & (T1_SIG | T1_VISIT |
427 || t1_flags[1 + k + 2][1 +
428 i] & (T1_SIG | T1_VISIT |
430 || t1_flags[1 + k + 3][1 +
431 i] & (T1_SIG | T1_VISIT |
438 for (runlen = 0; runlen < 4; runlen++) {
439 if (int_abs(t1_data[k + runlen][i]) & one)
442 mqc_setcurctx(T1_CTXNO_AGG);
443 mqc_encode(runlen != 4);
447 mqc_setcurctx(T1_CTXNO_UNI);
448 mqc_encode(runlen >> 1);
449 mqc_encode(runlen & 1);
453 for (j = k + runlen; j < k + 4 && j < h; j++) {
454 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
455 && (j == k + 3 || j == h - 1)) ? 1 : 0;
456 t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],
457 &t1_data[j][i], orient, bpno, one,
458 nmsedec, agg && (j == k + runlen), vsc);
464 void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
466 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
467 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
470 oneplushalf = one | half;
471 for (k = 0; k < h; k += 4) {
472 for (i = 0; i < w; i++) {
474 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
475 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
476 || t1_flags[1 + k + 1][1 +
477 i] & (T1_SIG | T1_VISIT |
479 || t1_flags[1 + k + 2][1 +
480 i] & (T1_SIG | T1_VISIT |
482 || (t1_flags[1 + k + 3][1 + i] &
484 (T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
485 T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
487 agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
488 || t1_flags[1 + k + 1][1 +
489 i] & (T1_SIG | T1_VISIT |
491 || t1_flags[1 + k + 2][1 +
492 i] & (T1_SIG | T1_VISIT |
494 || t1_flags[1 + k + 3][1 +
495 i] & (T1_SIG | T1_VISIT |
502 mqc_setcurctx(T1_CTXNO_AGG);
506 mqc_setcurctx(T1_CTXNO_UNI);
507 runlen = mqc_decode();
508 runlen = (runlen << 1) | mqc_decode();
512 for (j = k + runlen; j < k + 4 && j < h; j++) {
513 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
514 && (j == k + 3 || j == h - 1)) ? 1 : 0;
515 t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
521 agg && (j == k + runlen), vsc);
527 mqc_setcurctx(T1_CTXNO_UNI);
529 v = (v << 1) | mqc_decode();
530 v = (v << 1) | mqc_decode();
531 v = (v << 1) | mqc_decode();
534 fprintf(stderr, "warning: bad segmentation symbol %x\n",v);
537 } /* VSC and BYPASS by Antonin */
539 double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) //mod fixed_quality
541 double w1, w2, wmsedec;
543 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
544 w2 = dwt_getnorm(level, orient);
545 } else { /* if (qmfbid == 0) */
546 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
547 w2 = dwt_getnorm_real(level, orient);
549 wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
550 wmsedec *= wmsedec * nmsedec / 8192.0;
554 void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, tcd_tile_t * tile) //mod fixed_quality
562 double cumwmsedec = 0;
563 char type = T1_TYPE_MQ;
565 w = cblk->x1 - cblk->x0;
566 h = cblk->y1 - cblk->y0;
569 for (j = 0; j < h; j++) {
570 for (i = 0; i < w; i++) {
571 max = int_max(max, int_abs(t1_data[j][i]));
575 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
577 memset(t1_flags,0,sizeof(t1_flags));
579 bpno = cblk->numbps - 1;
583 mqc_setstate(T1_CTXNO_UNI, 0, 46);
584 mqc_setstate(T1_CTXNO_AGG, 0, 3);
585 mqc_setstate(T1_CTXNO_ZC, 0, 4);
586 mqc_init_enc(cblk->data);
588 for (passno = 0; bpno >= 0; passno++) {
589 tcd_pass_t *pass = &cblk->passes[passno];
591 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
592 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
597 t1_enc_sigpass(w, h, bpno, orient, &nmsedec, type, cblksty);
600 t1_enc_refpass(w, h, bpno, &nmsedec, type, cblksty);
603 t1_enc_clnpass(w, h, bpno, orient, &nmsedec, cblksty);
604 /* code switch SEGMARK (i.e. SEGSYM) */
605 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
610 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //mod fixed_quality
611 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); //add antonin quality
614 /* Code switch "RESTART" (i.e. TERMALL) */
615 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
616 && !((passtype == 2) && (bpno - 1 < 0))) {
617 if (type == T1_TYPE_RAW) {
620 /* correction = mqc_bypass_flush_enc(); */
621 } else { /* correction = mqc_restart_enc(); */
627 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
628 || ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
629 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
630 if (type == T1_TYPE_RAW) {
633 /* correction = mqc_bypass_flush_enc(); */
634 } else { /* correction = mqc_restart_enc(); */
644 if (++passtype == 3) {
649 if (pass->term && bpno > 0) {
650 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2)
651 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
653 if (type == T1_TYPE_RAW)
654 mqc_bypass_init_enc();
656 mqc_restart_init_enc();
659 pass->distortiondec = cumwmsedec;
660 pass->rate = mqc_numbytes() + correction; /* FIXME */
662 pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
664 /* Code-switch "RESET" */
665 if (cblksty & J2K_CCP_CBLKSTY_RESET)
669 /* Code switch "ERTERM" (i.e. PTERM) */
670 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
672 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
675 cblk->totalpasses = passno;
678 void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
684 char type = T1_TYPE_MQ; //BYPASS mode
686 memset(t1_data,0,sizeof(t1_data));
687 memset(t1_flags,0,sizeof(t1_flags));
689 w = cblk->x1 - cblk->x0;
690 h = cblk->y1 - cblk->y0;
691 bpno = roishift + cblk->numbps - 1;
695 mqc_setstate(T1_CTXNO_UNI, 0, 46);
696 mqc_setstate(T1_CTXNO_AGG, 0, 3);
697 mqc_setstate(T1_CTXNO_ZC, 0, 4);
699 for (segno = 0; segno < cblk->numsegs; segno++) {
700 tcd_seg_t *seg = &cblk->segs[segno];
703 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
704 && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
706 if (type == T1_TYPE_RAW)
707 raw_init_dec(seg->data, seg->len);
709 mqc_init_dec(seg->data, seg->len);
714 if (bpno==0) cblk->lastbp=1; // Add Antonin : quantizbug1
716 for (passno = 0; passno < seg->numpasses; passno++) {
719 t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
722 t1_dec_refpass(w, h, bpno, type, cblksty);
725 t1_dec_clnpass(w, h, bpno, orient, cblksty);
729 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
732 mqc_setstate(T1_CTXNO_UNI, 0, 46);
734 mqc_setstate(T1_CTXNO_AGG, 0, 3);
736 mqc_setstate(T1_CTXNO_ZC, 0, 4);
740 if (++passtype == 3) {
748 void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
750 int compno, resno, bandno, precno, cblkno;
751 int x, y, i, j, orient;
752 tcd_tilecomp_t *tilec;
753 tcd_resolution_t *res;
758 tile->distotile = 0; //add fixed_quality
760 for (compno = 0; compno < tile->numcomps; compno++) {
761 tilec = &tile->comps[compno];
762 for (resno = 0; resno < tilec->numresolutions; resno++) {
763 res = &tilec->resolutions[resno];
764 for (bandno = 0; bandno < res->numbands; bandno++) {
765 band = &res->bands[bandno];
766 for (precno = 0; precno < res->pw * res->ph; precno++) {
767 prc = &band->precincts[precno];
768 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
769 cblk = &prc->cblks[cblkno];
771 if (band->bandno == 0) {
772 x = cblk->x0 - band->x0;
773 y = cblk->y0 - band->y0;
774 } else if (band->bandno == 1) {
775 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
776 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
777 y = cblk->y0 - band->y0;
778 } else if (band->bandno == 2) {
779 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
780 x = cblk->x0 - band->x0;
781 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
782 } else { /* if (band->bandno == 3) */
783 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
784 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
785 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
788 if (tcp->tccps[compno].qmfbid == 1) {
790 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
791 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
793 tilec->data[(x + i) +
794 (y + j) * (tilec->x1 -
796 x0)] << T1_NMSEDEC_FRACBITS;
799 } else if (tcp->tccps[compno].qmfbid == 0) {
800 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
801 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
805 (y + j) * (tilec->x1 -
809 band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS);
813 orient = band->bandno; /* FIXME */
816 } else if (orient == 1) {
819 t1_encode_cblk(cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile); //mod fixed_quality
828 void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
830 int compno, resno, bandno, precno, cblkno;
832 for (compno = 0; compno < tile->numcomps; compno++) {
833 tcd_tilecomp_t *tilec = &tile->comps[compno];
834 for (resno = 0; resno < tilec->numresolutions; resno++) {
835 tcd_resolution_t *res = &tilec->resolutions[resno];
836 for (bandno = 0; bandno < res->numbands; bandno++) {
837 tcd_band_t *band = &res->bands[bandno];
838 for (precno = 0; precno < res->pw * res->ph; precno++) {
839 tcd_precinct_t *prc = &band->precincts[precno];
840 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
841 int x, y, i, j, orient;
842 tcd_cblk_t *cblk = &prc->cblks[cblkno];
843 orient = band->bandno; /* FIXME */
846 else if (orient == 1)
848 t1_decode_cblk(cblk, orient,
849 tcp->tccps[compno].roishift,
850 tcp->tccps[compno].cblksty);
851 if (band->bandno == 0) {
852 x = cblk->x0 - band->x0;
853 y = cblk->y0 - band->y0;
854 } else if (band->bandno == 1) {
855 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
856 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
857 y = cblk->y0 - band->y0;
858 } else if (band->bandno == 2) {
859 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
860 x = cblk->x0 - band->x0;
861 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
862 } else { /* if (band->bandno == 3) */
863 tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
864 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
865 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
868 if (tcp->tccps[compno].roishift) {
869 int thresh, val, mag;
870 thresh = 1 << tcp->tccps[compno].roishift;
871 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
872 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
876 mag >>= tcp->tccps[compno].roishift;
877 t1_data[j][i] = val < 0 ? -mag : mag;
883 if (tcp->tccps[compno].qmfbid == 1) {
884 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
885 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
887 (y + j) * (tilec->x1 -
888 tilec->x0)] = t1_data[j][i];
891 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
893 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
894 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
895 if (t1_data[j][i] == 0) {
897 (y + j) * (tilec->x1 - tilec->x0)] = 0;
900 // Add antonin : quantizbug1
906 t1_data[j][i]+=t1_data[j][i]>0?1:-1;
910 (y + j) * (tilec->x1 -
913 fix_mul(t1_data[j][i] << 12, band->stepsize); //Mod Antonin : quantizbug1 (before : << 13)
925 int t1_init_ctxno_zc(int f, int orient)
927 int h, v, d, n, t, hv;
929 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
930 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
931 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
932 0) + ((f & T1_SIG_SE) !=
933 0) + ((f & T1_SIG_SW) != 0);
989 return T1_CTXNO_ZC + n;
992 int t1_init_ctxno_sc(int f)
996 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
997 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
998 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
999 (T1_SIG_E | T1_SGN_E)) +
1000 ((f & (T1_SIG_W | T1_SGN_W)) ==
1001 (T1_SIG_W | T1_SGN_W)), 1);
1002 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1003 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1004 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1005 (T1_SIG_N | T1_SGN_N)) +
1006 ((f & (T1_SIG_S | T1_SGN_S)) ==
1007 (T1_SIG_S | T1_SGN_S)), 1);
1019 } else if (hc == 1) {
1027 return T1_CTXNO_SC + n;
1030 int t1_init_ctxno_mag(int f)
1033 if (!(f & T1_REFINE))
1034 n = (f & (T1_SIG_OTH)) ? 1 : 0;
1037 return T1_CTXNO_MAG + n;
1040 int t1_init_spb(int f)
1043 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1044 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1045 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
1046 (T1_SIG_E | T1_SGN_E)) +
1047 ((f & (T1_SIG_W | T1_SGN_W)) ==
1048 (T1_SIG_W | T1_SGN_W)), 1);
1049 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1050 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1051 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
1052 (T1_SIG_N | T1_SGN_N)) +
1053 ((f & (T1_SIG_S | T1_SGN_S)) ==
1054 (T1_SIG_S | T1_SGN_S)), 1);
1058 n = (!(hc > 0 || (!hc && vc > 0)));
1066 for (j = 0; j < 4; j++) {
1067 for (i = 0; i < 256; ++i) {
1068 t1_lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
1071 for (i = 0; i < 256; i++) {
1072 t1_lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
1074 for (j = 0; j < 2; j++) {
1075 for (i = 0; i < 2048; ++i) {
1076 t1_lut_ctxno_mag[(j << 11) + i] =
1077 t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
1080 for (i = 0; i < 256; ++i) {
1081 t1_lut_spb[i] = t1_init_spb(i << 4);
1083 /* FIXME FIXME FIXME */
1084 /* fprintf(stdout,"nmsedec luts:\n"); */
1085 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1086 t = i / pow(2, T1_NMSEDEC_FRACBITS);
1089 t1_lut_nmsedec_sig[i] =
1092 ((u * u - v * v) * pow(2,
1093 T1_NMSEDEC_FRACBITS) +
1094 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1095 t1_lut_nmsedec_sig0[i] =
1098 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1099 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1101 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1106 t1_lut_nmsedec_ref[i] =
1109 ((u * u - v * v) * pow(2,
1110 T1_NMSEDEC_FRACBITS) +
1111 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1112 t1_lut_nmsedec_ref0[i] =
1115 ((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
1116 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));