2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 /* ----------------------------------------------------------------------- */
35 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
36 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
39 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
40 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
43 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
44 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
47 static int t1_getspb(opj_t1_t *t1, int f) {
48 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
51 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
52 if (bitpos > T1_NMSEDEC_FRACBITS) {
53 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
56 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
59 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
60 if (bitpos > T1_NMSEDEC_FRACBITS) {
61 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
64 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
67 static void t1_updateflags(int *fp, int s) {
68 int *np = fp - (T1_MAXCBLKW + 2);
69 int *sp = fp + (T1_MAXCBLKW + 2);
86 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
89 opj_mqc_t *mqc = t1->mqc; /* MQC component */
91 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
92 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
93 v = int_abs(*dp) & one ? 1 : 0;
94 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
95 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
96 mqc_bypass_enc(mqc, v);
98 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
103 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
104 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
105 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
106 mqc_bypass_enc(mqc, v);
108 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
109 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
111 t1_updateflags(fp, v);
118 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
121 opj_raw_t *raw = t1->raw; /* RAW component */
122 opj_mqc_t *mqc = t1->mqc; /* MQC component */
124 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
125 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
126 if (type == T1_TYPE_RAW) {
127 if (raw_decode(raw)) {
128 v = raw_decode(raw); /* ESSAI */
129 *dp = v ? -oneplushalf : oneplushalf;
130 t1_updateflags(fp, v);
134 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
135 if (mqc_decode(mqc)) {
136 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
137 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
138 *dp = v ? -oneplushalf : oneplushalf;
139 t1_updateflags(fp, v);
145 } /* VSC and BYPASS by Antonin */
147 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
148 int i, j, k, one, vsc;
150 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
151 for (k = 0; k < h; k += 4) {
152 for (i = 0; i < w; i++) {
153 for (j = k; j < k + 4 && j < h; j++) {
154 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
155 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
161 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
162 int i, j, k, one, half, oneplushalf, vsc;
165 oneplushalf = one | half;
166 for (k = 0; k < h; k += 4) {
167 for (i = 0; i < w; i++) {
168 for (j = k; j < k + 4 && j < h; j++) {
169 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
170 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
174 } /* VSC and BYPASS by Antonin */
176 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
179 opj_mqc_t *mqc = t1->mqc; /* MQC component */
181 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
182 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
183 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
184 v = int_abs(*dp) & one ? 1 : 0;
185 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
186 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
187 mqc_bypass_enc(mqc, v);
189 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
196 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
199 opj_mqc_t *mqc = t1->mqc; /* MQC component */
200 opj_raw_t *raw = t1->raw; /* RAW component */
202 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
203 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
204 if (type == T1_TYPE_RAW) {
205 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
208 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
211 t = v ? poshalf : neghalf;
212 *dp += *dp < 0 ? -t : t;
215 } /* VSC and BYPASS by Antonin */
217 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
218 int i, j, k, one, vsc;
220 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
221 for (k = 0; k < h; k += 4) {
222 for (i = 0; i < w; i++) {
223 for (j = k; j < k + 4 && j < h; j++) {
224 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
225 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
231 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
232 int i, j, k, one, poshalf, neghalf;
236 neghalf = bpno > 0 ? -poshalf : -1;
237 for (k = 0; k < h; k += 4) {
238 for (i = 0; i < w; i++) {
239 for (j = k; j < k + 4 && j < h; j++) {
240 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
241 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
245 } /* VSC and BYPASS by Antonin */
247 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
250 opj_mqc_t *mqc = t1->mqc; /* MQC component */
252 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
256 if (!(*fp & (T1_SIG | T1_VISIT))) {
257 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
258 v = int_abs(*dp) & one ? 1 : 0;
262 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
263 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
265 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
266 t1_updateflags(fp, v);
273 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
276 opj_mqc_t *mqc = t1->mqc; /* MQC component */
278 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
282 if (!(flag & (T1_SIG | T1_VISIT))) {
283 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
284 if (mqc_decode(mqc)) {
286 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
287 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
288 *dp = v ? -oneplushalf : oneplushalf;
289 t1_updateflags(fp, v);
294 } /* VSC and BYPASS by Antonin */
296 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
297 int i, j, k, one, agg, runlen, vsc;
299 opj_mqc_t *mqc = t1->mqc; /* MQC component */
302 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
303 for (k = 0; k < h; k += 4) {
304 for (i = 0; i < w; i++) {
306 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
307 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
308 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
309 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
310 || (t1->flags[1 + k + 3][1 + i]
311 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
313 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
314 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
315 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
316 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
322 for (runlen = 0; runlen < 4; runlen++) {
323 if (int_abs(t1->data[k + runlen][i]) & one)
326 mqc_setcurctx(mqc, T1_CTXNO_AGG);
327 mqc_encode(mqc, runlen != 4);
331 mqc_setcurctx(mqc, T1_CTXNO_UNI);
332 mqc_encode(mqc, runlen >> 1);
333 mqc_encode(mqc, runlen & 1);
337 for (j = k + runlen; j < k + 4 && j < h; j++) {
338 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
339 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
345 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
346 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
347 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
349 opj_mqc_t *mqc = t1->mqc; /* MQC component */
353 oneplushalf = one | half;
354 for (k = 0; k < h; k += 4) {
355 for (i = 0; i < w; i++) {
357 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
358 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
359 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
360 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
361 || (t1->flags[1 + k + 3][1 + i]
362 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
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 | T1_SIG_OTH)
366 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
367 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
373 mqc_setcurctx(mqc, T1_CTXNO_AGG);
374 if (!mqc_decode(mqc)) {
377 mqc_setcurctx(mqc, T1_CTXNO_UNI);
378 runlen = mqc_decode(mqc);
379 runlen = (runlen << 1) | mqc_decode(mqc);
383 for (j = k + runlen; j < k + 4 && j < h; j++) {
384 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
385 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
391 mqc_setcurctx(mqc, T1_CTXNO_UNI);
393 v = (v << 1) | mqc_decode(mqc);
394 v = (v << 1) | mqc_decode(mqc);
395 v = (v << 1) | mqc_decode(mqc);
398 opg_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
402 } /* VSC and BYPASS by Antonin */
405 /** mod fixed_quality */
406 static double t1_getwmsedec(opj_t1_t *t1, int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) {
407 double w1, w2, wmsedec;
409 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
410 w2 = dwt_getnorm(level, orient);
411 } else { /* if (qmfbid == 0) */
412 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
413 w2 = dwt_getnorm_real(level, orient);
415 wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
416 wmsedec *= wmsedec * nmsedec / 8192.0;
421 /** mod fixed_quality */
422 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
429 double cumwmsedec = 0;
430 char type = T1_TYPE_MQ;
432 opj_mqc_t *mqc = t1->mqc; /* MQC component */
434 w = cblk->x1 - cblk->x0;
435 h = cblk->y1 - cblk->y0;
438 for (j = 0; j < h; j++) {
439 for (i = 0; i < w; i++) {
440 max = int_max(max, int_abs(t1->data[j][i]));
444 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
446 /* Changed by Dmitry Kolyadin */
447 for (i = 0; i <= w; i++) {
448 for (j = 0; j <= h; j++) {
453 bpno = cblk->numbps - 1;
456 mqc_resetstates(mqc);
457 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
458 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
459 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
460 mqc_init_enc(mqc, cblk->data);
462 for (passno = 0; bpno >= 0; passno++) {
463 opj_tcd_pass_t *pass = &cblk->passes[passno];
465 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
469 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
472 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
475 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
476 /* code switch SEGMARK (i.e. SEGSYM) */
477 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
478 mqc_segmark_enc(mqc);
483 cumwmsedec += t1_getwmsedec(t1, nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
484 tile->distotile += t1_getwmsedec(t1, nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
486 /* Code switch "RESTART" (i.e. TERMALL) */
487 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
488 if (type == T1_TYPE_RAW) {
491 /* correction = mqc_bypass_flush_enc(); */
492 } else { /* correction = mqc_restart_enc(); */
498 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
499 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
500 if (type == T1_TYPE_RAW) {
503 /* correction = mqc_bypass_flush_enc(); */
504 } else { /* correction = mqc_restart_enc(); */
514 if (++passtype == 3) {
519 if (pass->term && bpno > 0) {
520 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
521 if (type == T1_TYPE_RAW)
522 mqc_bypass_init_enc(mqc);
524 mqc_restart_init_enc(mqc);
527 pass->distortiondec = cumwmsedec;
528 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
529 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
531 /* Code-switch "RESET" */
532 if (cblksty & J2K_CCP_CBLKSTY_RESET)
536 /* Code switch "ERTERM" (i.e. PTERM) */
537 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
539 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
542 cblk->totalpasses = passno;
545 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
549 char type = T1_TYPE_MQ; /* BYPASS mode */
551 opj_raw_t *raw = t1->raw; /* RAW component */
552 opj_mqc_t *mqc = t1->mqc; /* MQC component */
554 w = cblk->x1 - cblk->x0;
555 h = cblk->y1 - cblk->y0;
557 /* Changed by Dmitry Kolyadin */
558 for (j = 0; j <= h; j++) {
559 for (i = 0; i <= w; i++) {
564 /* Changed by Dmitry Kolyadin */
565 for (i = 0; i < w; i++) {
566 for (j = 0; j < h; j++){
571 bpno = roishift + cblk->numbps - 1;
574 mqc_resetstates(mqc);
575 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
576 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
577 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
579 for (segno = 0; segno < cblk->numsegs; segno++) {
580 opj_tcd_seg_t *seg = &cblk->segs[segno];
583 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
584 if (type == T1_TYPE_RAW) {
585 raw_init_dec(raw, seg->data, seg->len);
587 mqc_init_dec(mqc, seg->data, seg->len);
590 for (passno = 0; passno < seg->numpasses; passno++) {
593 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
596 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
599 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
603 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
604 mqc_resetstates(mqc);
605 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
606 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
607 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
609 if (++passtype == 3) {
617 static int t1_init_ctxno_zc(int f, int orient) {
618 int h, v, d, n, t, hv;
620 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
621 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
622 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
661 } else if (hv == 1) {
669 } else if (hv == 1) {
686 return (T1_CTXNO_ZC + n);
689 static int t1_init_ctxno_sc(int f) {
693 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
694 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
695 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
696 (T1_SIG_E | T1_SGN_E)) +
697 ((f & (T1_SIG_W | T1_SGN_W)) ==
698 (T1_SIG_W | T1_SGN_W)), 1);
700 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
701 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
702 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
703 (T1_SIG_N | T1_SGN_N)) +
704 ((f & (T1_SIG_S | T1_SGN_S)) ==
705 (T1_SIG_S | T1_SGN_S)), 1);
718 } else if (hc == 1) {
727 return (T1_CTXNO_SC + n);
730 static int t1_init_ctxno_mag(int f) {
732 if (!(f & T1_REFINE))
733 n = (f & (T1_SIG_OTH)) ? 1 : 0;
737 return (T1_CTXNO_MAG + n);
740 static int t1_init_spb(int f) {
743 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
744 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
745 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
746 (T1_SIG_E | T1_SGN_E)) +
747 ((f & (T1_SIG_W | T1_SGN_W)) ==
748 (T1_SIG_W | T1_SGN_W)), 1);
750 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
751 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
752 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
753 (T1_SIG_N | T1_SGN_N)) +
754 ((f & (T1_SIG_S | T1_SGN_S)) ==
755 (T1_SIG_S | T1_SGN_S)), 1);
760 n = (!(hc > 0 || (!hc && vc > 0)));
765 static void t1_init_luts(opj_t1_t *t1) {
768 for (j = 0; j < 4; j++) {
769 for (i = 0; i < 256; ++i) {
770 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
773 for (i = 0; i < 256; i++) {
774 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
776 for (j = 0; j < 2; j++) {
777 for (i = 0; i < 2048; ++i) {
778 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
781 for (i = 0; i < 256; ++i) {
782 t1->lut_spb[i] = t1_init_spb(i << 4);
784 /* FIXME FIXME FIXME */
785 /* fprintf(stdout,"nmsedec luts:\n"); */
786 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
787 t = i / pow(2, T1_NMSEDEC_FRACBITS);
790 t1->lut_nmsedec_sig[i] =
792 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
793 t1->lut_nmsedec_sig0[i] =
795 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
797 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
802 t1->lut_nmsedec_ref[i] =
804 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
805 t1->lut_nmsedec_ref0[i] =
807 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
811 /* ----------------------------------------------------------------------- */
813 opj_t1_t* t1_create(opj_common_ptr cinfo) {
814 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
817 /* create MQC and RAW handles */
818 t1->mqc = mqc_create();
819 t1->raw = raw_create();
820 /* initialize the look-up tables of the Tier-1 coder/decoder */
826 void t1_destroy(opj_t1_t *t1) {
828 /* destroy MQC and RAW handles */
829 mqc_destroy(t1->mqc);
830 raw_destroy(t1->raw);
835 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
836 int compno, resno, bandno, precno, cblkno;
837 int x, y, i, j, orient;
839 tile->distotile = 0; /* fixed_quality */
841 for (compno = 0; compno < tile->numcomps; compno++) {
842 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
844 for (resno = 0; resno < tilec->numresolutions; resno++) {
845 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
847 for (bandno = 0; bandno < res->numbands; bandno++) {
848 opj_tcd_band_t *band = &res->bands[bandno];
850 for (precno = 0; precno < res->pw * res->ph; precno++) {
851 opj_tcd_precinct_t *prc = &band->precincts[precno];
853 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
854 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
856 if (band->bandno == 0) {
857 x = cblk->x0 - band->x0;
858 y = cblk->y0 - band->y0;
859 } else if (band->bandno == 1) {
860 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
861 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
862 y = cblk->y0 - band->y0;
863 } else if (band->bandno == 2) {
864 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
865 x = cblk->x0 - band->x0;
866 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
867 } else { /* if (band->bandno == 3) */
868 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
869 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
870 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
873 if (tcp->tccps[compno].qmfbid == 1) {
874 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
875 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
877 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
880 } else if (tcp->tccps[compno].qmfbid == 0) {
881 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
882 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
885 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
886 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
890 orient = band->bandno; /* FIXME */
893 } else if (orient == 1) {
897 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
906 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
907 int compno, resno, bandno, precno, cblkno;
909 for (compno = 0; compno < tile->numcomps; compno++) {
910 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
912 for (resno = 0; resno < tilec->numresolutions; resno++) {
913 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
915 for (bandno = 0; bandno < res->numbands; bandno++) {
916 opj_tcd_band_t *band = &res->bands[bandno];
918 for (precno = 0; precno < res->pw * res->ph; precno++) {
919 opj_tcd_precinct_t *prc = &band->precincts[precno];
921 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
922 int x, y, i, j, orient;
923 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
925 orient = band->bandno; /* FIXME */
928 } else if (orient == 1) {
932 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
934 if (band->bandno == 0) {
935 x = cblk->x0 - band->x0;
936 y = cblk->y0 - band->y0;
937 } else if (band->bandno == 1) {
938 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
939 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
940 y = cblk->y0 - band->y0;
941 } else if (band->bandno == 2) {
942 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
943 x = cblk->x0 - band->x0;
944 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
945 } else { /* if (band->bandno == 3) */
946 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
947 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
948 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
951 if (tcp->tccps[compno].roishift) {
952 int thresh, val, mag;
953 thresh = 1 << tcp->tccps[compno].roishift;
954 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
955 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
956 val = t1->data[j][i];
959 mag >>= tcp->tccps[compno].roishift;
960 t1->data[j][i] = val < 0 ? -mag : mag;
966 if (tcp->tccps[compno].qmfbid == 1) {
967 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
968 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
969 int tmp = t1->data[j][i];
971 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
973 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
977 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
978 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
979 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
980 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
981 if (t1->data[j][i] >> 1 == 0) {
982 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
984 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
985 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);