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 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
36 /** @name Local static functions */
39 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
40 static int t1_getctxno_sc(opj_t1_t *t1, int f);
41 static int t1_getctxno_mag(opj_t1_t *t1, int f);
42 static int t1_getspb(opj_t1_t *t1, int f);
43 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
44 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
45 static void t1_updateflags(int *fp, int s);
47 Encode significant pass
49 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);
51 Decode significant pass
53 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
55 Encode significant pass
57 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
59 Decode significant pass
61 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
63 Encode refinement pass
65 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
67 Decode refinement pass
69 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
71 Encode refinement pass
73 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
75 Decode refinement pass
77 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
81 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);
85 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
89 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
93 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
94 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
98 @param cblk Code-block coding parameters
100 @param compno Component number
104 @param cblksty Code-block style
108 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);
112 @param cblk Code-block coding parameters
114 @param roishift Region of interest shifting value
115 @param cblksty Code-block style
117 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
118 static int t1_init_ctxno_zc(int f, int orient);
119 static int t1_init_ctxno_sc(int f);
120 static int t1_init_ctxno_mag(int f);
121 static int t1_init_spb(int f);
123 Initialize the look-up tables of the Tier-1 coder/decoder
126 static void t1_init_luts(opj_t1_t *t1);
132 /* ----------------------------------------------------------------------- */
134 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
135 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
138 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
139 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
142 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
143 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
146 static int t1_getspb(opj_t1_t *t1, int f) {
147 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
150 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
151 if (bitpos > T1_NMSEDEC_FRACBITS) {
152 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
155 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
158 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
159 if (bitpos > T1_NMSEDEC_FRACBITS) {
160 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
163 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
166 static void t1_updateflags(int *fp, int s) {
167 int *np = fp - (T1_MAXCBLKW + 2);
168 int *sp = fp + (T1_MAXCBLKW + 2);
185 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) {
188 opj_mqc_t *mqc = t1->mqc; /* MQC component */
190 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
191 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
192 v = int_abs(*dp) & one ? 1 : 0;
193 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
194 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
195 mqc_bypass_enc(mqc, v);
197 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
202 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
203 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
204 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
205 mqc_bypass_enc(mqc, v);
207 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
208 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
210 t1_updateflags(fp, v);
217 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
220 opj_raw_t *raw = t1->raw; /* RAW component */
221 opj_mqc_t *mqc = t1->mqc; /* MQC component */
223 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
224 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
225 if (type == T1_TYPE_RAW) {
226 if (raw_decode(raw)) {
227 v = raw_decode(raw); /* ESSAI */
228 *dp = v ? -oneplushalf : oneplushalf;
229 t1_updateflags(fp, v);
233 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
234 if (mqc_decode(mqc)) {
235 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
236 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
237 *dp = v ? -oneplushalf : oneplushalf;
238 t1_updateflags(fp, v);
244 } /* VSC and BYPASS by Antonin */
246 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
247 int i, j, k, one, vsc;
249 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
250 for (k = 0; k < h; k += 4) {
251 for (i = 0; i < w; i++) {
252 for (j = k; j < k + 4 && j < h; j++) {
253 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
254 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
260 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
261 int i, j, k, one, half, oneplushalf, vsc;
264 oneplushalf = one | half;
265 for (k = 0; k < h; k += 4) {
266 for (i = 0; i < w; i++) {
267 for (j = k; j < k + 4 && j < h; j++) {
268 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
269 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
273 } /* VSC and BYPASS by Antonin */
275 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
278 opj_mqc_t *mqc = t1->mqc; /* MQC component */
280 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
281 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
282 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
283 v = int_abs(*dp) & one ? 1 : 0;
284 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
285 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
286 mqc_bypass_enc(mqc, v);
288 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
295 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
298 opj_mqc_t *mqc = t1->mqc; /* MQC component */
299 opj_raw_t *raw = t1->raw; /* RAW component */
301 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
302 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
303 if (type == T1_TYPE_RAW) {
304 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
307 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
310 t = v ? poshalf : neghalf;
311 *dp += *dp < 0 ? -t : t;
314 } /* VSC and BYPASS by Antonin */
316 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
317 int i, j, k, one, vsc;
319 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
320 for (k = 0; k < h; k += 4) {
321 for (i = 0; i < w; i++) {
322 for (j = k; j < k + 4 && j < h; j++) {
323 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
324 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
330 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
331 int i, j, k, one, poshalf, neghalf;
335 neghalf = bpno > 0 ? -poshalf : -1;
336 for (k = 0; k < h; k += 4) {
337 for (i = 0; i < w; i++) {
338 for (j = k; j < k + 4 && j < h; j++) {
339 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
340 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
344 } /* VSC and BYPASS by Antonin */
346 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) {
349 opj_mqc_t *mqc = t1->mqc; /* MQC component */
351 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
355 if (!(*fp & (T1_SIG | T1_VISIT))) {
356 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
357 v = int_abs(*dp) & one ? 1 : 0;
361 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
362 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
364 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
365 t1_updateflags(fp, v);
372 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
375 opj_mqc_t *mqc = t1->mqc; /* MQC component */
377 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
381 if (!(flag & (T1_SIG | T1_VISIT))) {
382 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
383 if (mqc_decode(mqc)) {
385 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
386 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
387 *dp = v ? -oneplushalf : oneplushalf;
388 t1_updateflags(fp, v);
393 } /* VSC and BYPASS by Antonin */
395 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
396 int i, j, k, one, agg, runlen, vsc;
398 opj_mqc_t *mqc = t1->mqc; /* MQC component */
401 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
402 for (k = 0; k < h; k += 4) {
403 for (i = 0; i < w; i++) {
405 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
406 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
407 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
408 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
409 || (t1->flags[1 + k + 3][1 + i]
410 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
412 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
413 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
414 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
415 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
421 for (runlen = 0; runlen < 4; runlen++) {
422 if (int_abs(t1->data[k + runlen][i]) & one)
425 mqc_setcurctx(mqc, T1_CTXNO_AGG);
426 mqc_encode(mqc, runlen != 4);
430 mqc_setcurctx(mqc, T1_CTXNO_UNI);
431 mqc_encode(mqc, runlen >> 1);
432 mqc_encode(mqc, runlen & 1);
436 for (j = k + runlen; j < k + 4 && j < h; j++) {
437 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
438 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
444 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
445 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
446 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
448 opj_mqc_t *mqc = t1->mqc; /* MQC component */
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 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
459 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
460 || (t1->flags[1 + k + 3][1 + i]
461 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
463 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
464 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
465 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
466 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
472 mqc_setcurctx(mqc, T1_CTXNO_AGG);
473 if (!mqc_decode(mqc)) {
476 mqc_setcurctx(mqc, T1_CTXNO_UNI);
477 runlen = mqc_decode(mqc);
478 runlen = (runlen << 1) | mqc_decode(mqc);
482 for (j = k + runlen; j < k + 4 && j < h; j++) {
483 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
484 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
490 mqc_setcurctx(mqc, T1_CTXNO_UNI);
492 v = (v << 1) | mqc_decode(mqc);
493 v = (v << 1) | mqc_decode(mqc);
494 v = (v << 1) | mqc_decode(mqc);
497 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
501 } /* VSC and BYPASS by Antonin */
504 /** mod fixed_quality */
505 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) {
506 double w1, w2, wmsedec;
508 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
509 w2 = dwt_getnorm(level, orient);
510 } else { /* if (qmfbid == 0) */
511 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
512 w2 = dwt_getnorm_real(level, orient);
514 wmsedec = w1 * w2 * stepsize * (1 << bpno);
515 wmsedec *= wmsedec * nmsedec / 8192.0;
520 /** mod fixed_quality */
521 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) {
528 double cumwmsedec = 0;
529 char type = T1_TYPE_MQ;
531 opj_mqc_t *mqc = t1->mqc; /* MQC component */
533 w = cblk->x1 - cblk->x0;
534 h = cblk->y1 - cblk->y0;
537 for (j = 0; j < h; j++) {
538 for (i = 0; i < w; i++) {
539 max = int_max(max, int_abs(t1->data[j][i]));
543 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
545 /* Changed by Dmitry Kolyadin */
546 for (i = 0; i <= w; i++) {
547 for (j = 0; j <= h; j++) {
552 bpno = cblk->numbps - 1;
555 mqc_resetstates(mqc);
556 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
557 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
558 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
559 mqc_init_enc(mqc, cblk->data);
561 for (passno = 0; bpno >= 0; passno++) {
562 opj_tcd_pass_t *pass = &cblk->passes[passno];
564 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
568 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
571 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
574 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
575 /* code switch SEGMARK (i.e. SEGSYM) */
576 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
577 mqc_segmark_enc(mqc);
582 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
583 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
585 /* Code switch "RESTART" (i.e. TERMALL) */
586 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
587 if (type == T1_TYPE_RAW) {
590 /* correction = mqc_bypass_flush_enc(); */
591 } else { /* correction = mqc_restart_enc(); */
597 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
598 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
599 if (type == T1_TYPE_RAW) {
602 /* correction = mqc_bypass_flush_enc(); */
603 } else { /* correction = mqc_restart_enc(); */
613 if (++passtype == 3) {
618 if (pass->term && bpno > 0) {
619 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
620 if (type == T1_TYPE_RAW)
621 mqc_bypass_init_enc(mqc);
623 mqc_restart_init_enc(mqc);
626 pass->distortiondec = cumwmsedec;
627 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
628 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
630 /* Code-switch "RESET" */
631 if (cblksty & J2K_CCP_CBLKSTY_RESET)
635 /* Code switch "ERTERM" (i.e. PTERM) */
636 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
638 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
641 cblk->totalpasses = passno;
644 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
648 char type = T1_TYPE_MQ; /* BYPASS mode */
650 opj_raw_t *raw = t1->raw; /* RAW component */
651 opj_mqc_t *mqc = t1->mqc; /* MQC component */
653 w = cblk->x1 - cblk->x0;
654 h = cblk->y1 - cblk->y0;
656 /* Changed by Dmitry Kolyadin */
657 for (j = 0; j <= h; j++) {
658 for (i = 0; i <= w; i++) {
663 /* Changed by Dmitry Kolyadin */
664 for (i = 0; i < w; i++) {
665 for (j = 0; j < h; j++){
670 bpno = roishift + cblk->numbps - 1;
673 mqc_resetstates(mqc);
674 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
675 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
676 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
678 for (segno = 0; segno < cblk->numsegs; segno++) {
679 opj_tcd_seg_t *seg = &cblk->segs[segno];
682 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
683 if (type == T1_TYPE_RAW) {
684 raw_init_dec(raw, seg->data, seg->len);
686 mqc_init_dec(mqc, seg->data, seg->len);
689 for (passno = 0; passno < seg->numpasses; passno++) {
692 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
695 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
698 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
702 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
703 mqc_resetstates(mqc);
704 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
705 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
706 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
708 if (++passtype == 3) {
716 static int t1_init_ctxno_zc(int f, int orient) {
717 int h, v, d, n, t, hv;
719 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
720 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
721 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
760 } else if (hv == 1) {
768 } else if (hv == 1) {
785 return (T1_CTXNO_ZC + n);
788 static int t1_init_ctxno_sc(int f) {
792 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
793 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
794 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
795 (T1_SIG_E | T1_SGN_E)) +
796 ((f & (T1_SIG_W | T1_SGN_W)) ==
797 (T1_SIG_W | T1_SGN_W)), 1);
799 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
800 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
801 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
802 (T1_SIG_N | T1_SGN_N)) +
803 ((f & (T1_SIG_S | T1_SGN_S)) ==
804 (T1_SIG_S | T1_SGN_S)), 1);
817 } else if (hc == 1) {
826 return (T1_CTXNO_SC + n);
829 static int t1_init_ctxno_mag(int f) {
831 if (!(f & T1_REFINE))
832 n = (f & (T1_SIG_OTH)) ? 1 : 0;
836 return (T1_CTXNO_MAG + n);
839 static int t1_init_spb(int f) {
842 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
843 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
844 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
845 (T1_SIG_E | T1_SGN_E)) +
846 ((f & (T1_SIG_W | T1_SGN_W)) ==
847 (T1_SIG_W | T1_SGN_W)), 1);
849 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
850 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
851 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
852 (T1_SIG_N | T1_SGN_N)) +
853 ((f & (T1_SIG_S | T1_SGN_S)) ==
854 (T1_SIG_S | T1_SGN_S)), 1);
859 n = (!(hc > 0 || (!hc && vc > 0)));
864 static void t1_init_luts(opj_t1_t *t1) {
867 for (j = 0; j < 4; j++) {
868 for (i = 0; i < 256; ++i) {
869 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
872 for (i = 0; i < 256; i++) {
873 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
875 for (j = 0; j < 2; j++) {
876 for (i = 0; i < 2048; ++i) {
877 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
880 for (i = 0; i < 256; ++i) {
881 t1->lut_spb[i] = t1_init_spb(i << 4);
883 /* FIXME FIXME FIXME */
884 /* fprintf(stdout,"nmsedec luts:\n"); */
885 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
886 t = i / pow(2, T1_NMSEDEC_FRACBITS);
889 t1->lut_nmsedec_sig[i] =
891 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
892 t1->lut_nmsedec_sig0[i] =
894 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
896 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
901 t1->lut_nmsedec_ref[i] =
903 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
904 t1->lut_nmsedec_ref0[i] =
906 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
910 /* ----------------------------------------------------------------------- */
912 opj_t1_t* t1_create(opj_common_ptr cinfo) {
913 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
916 /* create MQC and RAW handles */
917 t1->mqc = mqc_create();
918 t1->raw = raw_create();
919 /* initialize the look-up tables of the Tier-1 coder/decoder */
925 void t1_destroy(opj_t1_t *t1) {
927 /* destroy MQC and RAW handles */
928 mqc_destroy(t1->mqc);
929 raw_destroy(t1->raw);
934 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
935 int compno, resno, bandno, precno, cblkno;
936 int x, y, i, j, orient;
938 tile->distotile = 0; /* fixed_quality */
940 for (compno = 0; compno < tile->numcomps; compno++) {
941 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
943 for (resno = 0; resno < tilec->numresolutions; resno++) {
944 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
946 for (bandno = 0; bandno < res->numbands; bandno++) {
947 opj_tcd_band_t *band = &res->bands[bandno];
949 for (precno = 0; precno < res->pw * res->ph; precno++) {
950 opj_tcd_precinct_t *prc = &band->precincts[precno];
952 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
953 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
955 if (band->bandno == 0) {
956 x = cblk->x0 - band->x0;
957 y = cblk->y0 - band->y0;
958 } else if (band->bandno == 1) {
959 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
960 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
961 y = cblk->y0 - band->y0;
962 } else if (band->bandno == 2) {
963 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
964 x = cblk->x0 - band->x0;
965 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
966 } else { /* if (band->bandno == 3) */
967 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
968 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
969 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
972 if (tcp->tccps[compno].qmfbid == 1) {
973 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
974 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
976 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
979 } else if (tcp->tccps[compno].qmfbid == 0) {
980 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
981 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
984 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
985 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
989 orient = band->bandno; /* FIXME */
992 } else if (orient == 1) {
996 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1005 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1006 int compno, resno, bandno, precno, cblkno;
1008 for (compno = 0; compno < tile->numcomps; compno++) {
1009 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1011 for (resno = 0; resno < tilec->numresolutions; resno++) {
1012 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1014 for (bandno = 0; bandno < res->numbands; bandno++) {
1015 opj_tcd_band_t *band = &res->bands[bandno];
1017 for (precno = 0; precno < res->pw * res->ph; precno++) {
1018 opj_tcd_precinct_t *prc = &band->precincts[precno];
1020 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1021 int x, y, i, j, orient;
1022 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1024 orient = band->bandno; /* FIXME */
1027 } else if (orient == 1) {
1031 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1033 if (band->bandno == 0) {
1034 x = cblk->x0 - band->x0;
1035 y = cblk->y0 - band->y0;
1036 } else if (band->bandno == 1) {
1037 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1038 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1039 y = cblk->y0 - band->y0;
1040 } else if (band->bandno == 2) {
1041 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1042 x = cblk->x0 - band->x0;
1043 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1044 } else { /* if (band->bandno == 3) */
1045 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1046 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1047 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1050 if (tcp->tccps[compno].roishift) {
1051 int thresh, val, mag;
1052 thresh = 1 << tcp->tccps[compno].roishift;
1053 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1054 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1055 val = t1->data[j][i];
1057 if (mag >= thresh) {
1058 mag >>= tcp->tccps[compno].roishift;
1059 t1->data[j][i] = val < 0 ? -mag : mag;
1065 if (tcp->tccps[compno].qmfbid == 1) {
1066 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1067 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1068 int tmp = t1->data[j][i];
1070 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
1072 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
1076 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1077 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1078 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1079 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
1080 if (t1->data[j][i] >> 1 == 0) {
1081 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
1083 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1084 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);