2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
37 /** @name Local static functions */
40 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
41 static int t1_getctxno_sc(opj_t1_t *t1, int f);
42 static int t1_getctxno_mag(opj_t1_t *t1, int f);
43 static int t1_getspb(opj_t1_t *t1, int f);
44 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
45 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
46 static void t1_updateflags(int *fp, int s);
48 Encode significant pass
50 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);
52 Decode significant pass
54 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
56 Encode significant pass
58 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
60 Decode significant pass
62 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
64 Encode refinement pass
66 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
68 Decode refinement pass
70 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
72 Encode refinement pass
74 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
76 Decode refinement pass
78 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
82 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);
86 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
90 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
94 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
95 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
99 @param cblk Code-block coding parameters
101 @param compno Component number
105 @param cblksty Code-block style
109 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);
113 @param cblk Code-block coding parameters
115 @param roishift Region of interest shifting value
116 @param cblksty Code-block style
118 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
119 static int t1_init_ctxno_zc(int f, int orient);
120 static int t1_init_ctxno_sc(int f);
121 static int t1_init_ctxno_mag(int f);
122 static int t1_init_spb(int f);
124 Initialize the look-up tables of the Tier-1 coder/decoder
127 static void t1_init_luts(opj_t1_t *t1);
133 /* ----------------------------------------------------------------------- */
135 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
136 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
139 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
140 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
143 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
144 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
147 static int t1_getspb(opj_t1_t *t1, int f) {
148 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
151 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
152 if (bitpos > T1_NMSEDEC_FRACBITS) {
153 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
156 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
159 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
160 if (bitpos > T1_NMSEDEC_FRACBITS) {
161 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
164 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
167 static void t1_updateflags(int *fp, int s) {
168 int *np = fp - (T1_MAXCBLKW + 2);
169 int *sp = fp + (T1_MAXCBLKW + 2);
186 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) {
189 opj_mqc_t *mqc = t1->mqc; /* MQC component */
191 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
192 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
193 v = int_abs(*dp) & one ? 1 : 0;
194 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
195 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
196 mqc_bypass_enc(mqc, v);
198 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
203 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
204 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
205 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
206 mqc_bypass_enc(mqc, v);
208 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
209 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
211 t1_updateflags(fp, v);
218 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
221 opj_raw_t *raw = t1->raw; /* RAW component */
222 opj_mqc_t *mqc = t1->mqc; /* MQC component */
224 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
225 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
226 if (type == T1_TYPE_RAW) {
227 if (raw_decode(raw)) {
228 v = raw_decode(raw); /* ESSAI */
229 *dp = v ? -oneplushalf : oneplushalf;
230 t1_updateflags(fp, v);
234 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
235 if (mqc_decode(mqc)) {
236 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
237 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
238 *dp = v ? -oneplushalf : oneplushalf;
239 t1_updateflags(fp, v);
245 } /* VSC and BYPASS by Antonin */
247 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
248 int i, j, k, one, vsc;
250 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
251 for (k = 0; k < h; k += 4) {
252 for (i = 0; i < w; i++) {
253 for (j = k; j < k + 4 && j < h; j++) {
254 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
255 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
261 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
262 int i, j, k, one, half, oneplushalf, vsc;
265 oneplushalf = one | half;
266 for (k = 0; k < h; k += 4) {
267 for (i = 0; i < w; i++) {
268 for (j = k; j < k + 4 && j < h; j++) {
269 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
270 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
274 } /* VSC and BYPASS by Antonin */
276 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
279 opj_mqc_t *mqc = t1->mqc; /* MQC component */
281 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
282 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
283 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
284 v = int_abs(*dp) & one ? 1 : 0;
285 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
286 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
287 mqc_bypass_enc(mqc, v);
289 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
296 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
299 opj_mqc_t *mqc = t1->mqc; /* MQC component */
300 opj_raw_t *raw = t1->raw; /* RAW component */
302 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
303 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
304 if (type == T1_TYPE_RAW) {
305 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
308 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
311 t = v ? poshalf : neghalf;
312 *dp += *dp < 0 ? -t : t;
315 } /* VSC and BYPASS by Antonin */
317 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
318 int i, j, k, one, vsc;
320 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
321 for (k = 0; k < h; k += 4) {
322 for (i = 0; i < w; i++) {
323 for (j = k; j < k + 4 && j < h; j++) {
324 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
325 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
331 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
332 int i, j, k, one, poshalf, neghalf;
336 neghalf = bpno > 0 ? -poshalf : -1;
337 for (k = 0; k < h; k += 4) {
338 for (i = 0; i < w; i++) {
339 for (j = k; j < k + 4 && j < h; j++) {
340 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
341 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
345 } /* VSC and BYPASS by Antonin */
347 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) {
350 opj_mqc_t *mqc = t1->mqc; /* MQC component */
352 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
356 if (!(*fp & (T1_SIG | T1_VISIT))) {
357 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
358 v = int_abs(*dp) & one ? 1 : 0;
362 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
363 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
365 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
366 t1_updateflags(fp, v);
373 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
376 opj_mqc_t *mqc = t1->mqc; /* MQC component */
378 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
382 if (!(flag & (T1_SIG | T1_VISIT))) {
383 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
384 if (mqc_decode(mqc)) {
386 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
387 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
388 *dp = v ? -oneplushalf : oneplushalf;
389 t1_updateflags(fp, v);
394 } /* VSC and BYPASS by Antonin */
396 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
397 int i, j, k, one, agg, runlen, vsc;
399 opj_mqc_t *mqc = t1->mqc; /* MQC component */
402 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
403 for (k = 0; k < h; k += 4) {
404 for (i = 0; i < w; i++) {
406 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
407 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
408 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
409 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
410 || (t1->flags[1 + k + 3][1 + i]
411 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
413 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
414 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
415 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
416 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
422 for (runlen = 0; runlen < 4; runlen++) {
423 if (int_abs(t1->data[k + runlen][i]) & one)
426 mqc_setcurctx(mqc, T1_CTXNO_AGG);
427 mqc_encode(mqc, runlen != 4);
431 mqc_setcurctx(mqc, T1_CTXNO_UNI);
432 mqc_encode(mqc, runlen >> 1);
433 mqc_encode(mqc, runlen & 1);
437 for (j = k + runlen; j < k + 4 && j < h; j++) {
438 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
439 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
445 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
446 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
447 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
449 opj_mqc_t *mqc = t1->mqc; /* MQC component */
453 oneplushalf = one | half;
454 for (k = 0; k < h; k += 4) {
455 for (i = 0; i < w; i++) {
457 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
458 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
459 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
460 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
461 || (t1->flags[1 + k + 3][1 + i]
462 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
464 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
465 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
466 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
467 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
473 mqc_setcurctx(mqc, T1_CTXNO_AGG);
474 if (!mqc_decode(mqc)) {
477 mqc_setcurctx(mqc, T1_CTXNO_UNI);
478 runlen = mqc_decode(mqc);
479 runlen = (runlen << 1) | mqc_decode(mqc);
483 for (j = k + runlen; j < k + 4 && j < h; j++) {
484 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
485 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
491 mqc_setcurctx(mqc, T1_CTXNO_UNI);
493 v = (v << 1) | mqc_decode(mqc);
494 v = (v << 1) | mqc_decode(mqc);
495 v = (v << 1) | mqc_decode(mqc);
498 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
502 } /* VSC and BYPASS by Antonin */
505 /** mod fixed_quality */
506 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) {
507 double w1, w2, wmsedec;
509 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
510 w2 = dwt_getnorm(level, orient);
511 } else { /* if (qmfbid == 0) */
512 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
513 w2 = dwt_getnorm_real(level, orient);
515 wmsedec = w1 * w2 * stepsize * (1 << bpno);
516 wmsedec *= wmsedec * nmsedec / 8192.0;
521 /** mod fixed_quality */
522 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) {
529 double cumwmsedec = 0;
530 char type = T1_TYPE_MQ;
532 opj_mqc_t *mqc = t1->mqc; /* MQC component */
534 w = cblk->x1 - cblk->x0;
535 h = cblk->y1 - cblk->y0;
538 for (j = 0; j < h; j++) {
539 for (i = 0; i < w; i++) {
540 max = int_max(max, int_abs(t1->data[j][i]));
544 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
546 for (i = 0; i <= h; ++i) {
547 memset(&t1->flags[i], 0, (w+1) * sizeof(int));
550 bpno = cblk->numbps - 1;
553 mqc_resetstates(mqc);
554 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
555 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
556 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
557 mqc_init_enc(mqc, cblk->data);
559 for (passno = 0; bpno >= 0; passno++) {
560 opj_tcd_pass_t *pass = &cblk->passes[passno];
562 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
566 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
569 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
572 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
573 /* code switch SEGMARK (i.e. SEGSYM) */
574 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
575 mqc_segmark_enc(mqc);
580 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
581 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
583 /* Code switch "RESTART" (i.e. TERMALL) */
584 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
585 if (type == T1_TYPE_RAW) {
588 /* correction = mqc_bypass_flush_enc(); */
589 } else { /* correction = mqc_restart_enc(); */
595 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
596 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
597 if (type == T1_TYPE_RAW) {
600 /* correction = mqc_bypass_flush_enc(); */
601 } else { /* correction = mqc_restart_enc(); */
611 if (++passtype == 3) {
616 if (pass->term && bpno > 0) {
617 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
618 if (type == T1_TYPE_RAW)
619 mqc_bypass_init_enc(mqc);
621 mqc_restart_init_enc(mqc);
624 pass->distortiondec = cumwmsedec;
625 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
627 /* Code-switch "RESET" */
628 if (cblksty & J2K_CCP_CBLKSTY_RESET)
632 /* Code switch "ERTERM" (i.e. PTERM) */
633 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
635 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
638 cblk->totalpasses = passno;
640 for (passno = 0; passno<cblk->totalpasses; passno++) {
641 opj_tcd_pass_t *pass = &cblk->passes[passno];
642 if (pass->rate > mqc_numbytes(mqc))
643 pass->rate = mqc_numbytes(mqc);
644 /*Preventing generation of FF as last data byte of a pass*/
645 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
648 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
652 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
656 char type = T1_TYPE_MQ; /* BYPASS mode */
658 opj_raw_t *raw = t1->raw; /* RAW component */
659 opj_mqc_t *mqc = t1->mqc; /* MQC component */
661 w = cblk->x1 - cblk->x0;
662 h = cblk->y1 - cblk->y0;
664 for (i = 0; i <= h; ++i) {
665 memset(&t1->flags[i], 0, (w + 1) * sizeof(int));
668 for (i = 0; i < h; ++i) {
669 memset(&t1->data[i], 0, w * sizeof(int));
672 bpno = roishift + cblk->numbps - 1;
675 mqc_resetstates(mqc);
676 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
677 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
678 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
680 for (segno = 0; segno < cblk->numsegs; segno++) {
681 opj_tcd_seg_t *seg = &cblk->segs[segno];
684 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
685 if (type == T1_TYPE_RAW) {
686 raw_init_dec(raw, seg->data, seg->len);
688 mqc_init_dec(mqc, seg->data, seg->len);
691 for (passno = 0; passno < seg->numpasses; passno++) {
694 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
697 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
700 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
704 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
705 mqc_resetstates(mqc);
706 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
707 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
708 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
710 if (++passtype == 3) {
718 static int t1_init_ctxno_zc(int f, int orient) {
719 int h, v, d, n, t, hv;
721 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
722 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
723 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
762 } else if (hv == 1) {
770 } else if (hv == 1) {
787 return (T1_CTXNO_ZC + n);
790 static int t1_init_ctxno_sc(int f) {
794 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
795 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
796 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
797 (T1_SIG_E | T1_SGN_E)) +
798 ((f & (T1_SIG_W | T1_SGN_W)) ==
799 (T1_SIG_W | T1_SGN_W)), 1);
801 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
802 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
803 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
804 (T1_SIG_N | T1_SGN_N)) +
805 ((f & (T1_SIG_S | T1_SGN_S)) ==
806 (T1_SIG_S | T1_SGN_S)), 1);
819 } else if (hc == 1) {
828 return (T1_CTXNO_SC + n);
831 static int t1_init_ctxno_mag(int f) {
833 if (!(f & T1_REFINE))
834 n = (f & (T1_SIG_OTH)) ? 1 : 0;
838 return (T1_CTXNO_MAG + n);
841 static int t1_init_spb(int f) {
844 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
845 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
846 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
847 (T1_SIG_E | T1_SGN_E)) +
848 ((f & (T1_SIG_W | T1_SGN_W)) ==
849 (T1_SIG_W | T1_SGN_W)), 1);
851 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
852 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
853 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
854 (T1_SIG_N | T1_SGN_N)) +
855 ((f & (T1_SIG_S | T1_SGN_S)) ==
856 (T1_SIG_S | T1_SGN_S)), 1);
861 n = (!(hc > 0 || (!hc && vc > 0)));
866 static void t1_init_luts(opj_t1_t *t1) {
869 for (j = 0; j < 4; j++) {
870 for (i = 0; i < 256; ++i) {
871 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
874 for (i = 0; i < 256; i++) {
875 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
877 for (j = 0; j < 2; j++) {
878 for (i = 0; i < 2048; ++i) {
879 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
882 for (i = 0; i < 256; ++i) {
883 t1->lut_spb[i] = t1_init_spb(i << 4);
885 /* FIXME FIXME FIXME */
886 /* fprintf(stdout,"nmsedec luts:\n"); */
887 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
888 t = i / pow(2, T1_NMSEDEC_FRACBITS);
891 t1->lut_nmsedec_sig[i] =
893 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
894 t1->lut_nmsedec_sig0[i] =
896 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
898 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
903 t1->lut_nmsedec_ref[i] =
905 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
906 t1->lut_nmsedec_ref0[i] =
908 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
912 /* ----------------------------------------------------------------------- */
914 opj_t1_t* t1_create(opj_common_ptr cinfo) {
915 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
918 /* create MQC and RAW handles */
919 t1->mqc = mqc_create();
920 t1->raw = raw_create();
921 /* initialize the look-up tables of the Tier-1 coder/decoder */
927 void t1_destroy(opj_t1_t *t1) {
929 /* destroy MQC and RAW handles */
930 mqc_destroy(t1->mqc);
931 raw_destroy(t1->raw);
936 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
937 int compno, resno, bandno, precno, cblkno;
939 tile->distotile = 0; /* fixed_quality */
941 for (compno = 0; compno < tile->numcomps; compno++) {
942 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
944 for (resno = 0; resno < tilec->numresolutions; resno++) {
945 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
947 for (bandno = 0; bandno < res->numbands; bandno++) {
948 opj_tcd_band_t *band = &res->bands[bandno];
950 for (precno = 0; precno < res->pw * res->ph; precno++) {
951 opj_tcd_precinct_t *prc = &band->precincts[precno];
953 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
954 int x, y, w, i, j, orient;
955 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
957 x = cblk->x0 - band->x0;
958 y = cblk->y0 - band->y0;
959 if (band->bandno & 1) {
960 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
961 x += pres->x1 - pres->x0;
963 if (band->bandno & 2) {
964 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
965 y += pres->y1 - pres->y0;
968 w = tilec->x1 - tilec->x0;
969 if (tcp->tccps[compno].qmfbid == 1) {
970 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
971 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
973 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
976 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
977 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
978 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
981 tilec->data[x + i + (y + j) * w],
982 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
986 orient = band->bandno; /* FIXME */
989 } else if (orient == 1) {
993 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1002 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1003 int compno, resno, bandno, precno, cblkno;
1005 for (compno = 0; compno < tile->numcomps; compno++) {
1006 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1008 for (resno = 0; resno < tilec->numresolutions; resno++) {
1009 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1011 for (bandno = 0; bandno < res->numbands; bandno++) {
1012 opj_tcd_band_t *band = &res->bands[bandno];
1014 for (precno = 0; precno < res->pw * res->ph; precno++) {
1015 opj_tcd_precinct_t *prc = &band->precincts[precno];
1017 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1018 int x, y, w, i, j, orient;
1019 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1021 orient = band->bandno; /* FIXME */
1024 } else if (orient == 1) {
1028 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1030 x = cblk->x0 - band->x0;
1031 y = cblk->y0 - band->y0;
1032 if (band->bandno & 1) {
1033 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1034 x += pres->x1 - pres->x0;
1036 if (band->bandno & 2) {
1037 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1038 y += pres->y1 - pres->y0;
1041 if (tcp->tccps[compno].roishift) {
1042 int thresh = 1 << tcp->tccps[compno].roishift;
1043 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1044 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1045 int val = t1->data[j][i];
1046 int mag = int_abs(val);
1047 if (mag >= thresh) {
1048 mag >>= tcp->tccps[compno].roishift;
1049 t1->data[j][i] = val < 0 ? -mag : mag;
1055 w = tilec->x1 - tilec->x0;
1056 if (tcp->tccps[compno].qmfbid == 1) {
1057 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1058 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1059 tilec->data[x + i + (y + j) * w] = t1->data[j][i]/2;
1062 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1063 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1064 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1065 if (t1->data[j][i] >> 1 == 0) {
1066 tilec->data[x + i + (y + j) * w] = 0;
1068 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
1069 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1070 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);