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
8 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
39 /** @name Local static functions */
42 static char t1_getctxno_zc(int f, int orient);
43 static char t1_getctxno_sc(int f);
44 static char t1_getctxno_mag(int f);
45 static char t1_getspb(int f);
46 static short t1_getnmsedec_sig(int x, int bitpos);
47 static short t1_getnmsedec_ref(int x, int bitpos);
48 static void t1_updateflags(int *fp, int s);
50 Encode significant pass
52 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);
54 Decode significant pass
56 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
58 Encode significant pass
60 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
62 Decode significant pass
64 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
66 Encode refinement pass
68 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
70 Decode refinement pass
72 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
74 Encode refinement pass
76 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
78 Decode refinement pass
80 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
84 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);
88 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
92 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
96 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
97 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
101 @param cblk Code-block coding parameters
103 @param compno Component number
107 @param cblksty Code-block style
111 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);
115 @param cblk Code-block coding parameters
117 @param roishift Region of interest shifting value
118 @param cblksty Code-block style
120 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
126 /* ----------------------------------------------------------------------- */
128 static char t1_getctxno_zc(int f, int orient) {
129 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
132 static char t1_getctxno_sc(int f) {
133 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
136 static char t1_getctxno_mag(int f) {
137 return lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
140 static char t1_getspb(int f) {
141 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
144 static short t1_getnmsedec_sig(int x, int bitpos) {
145 if (bitpos > T1_NMSEDEC_FRACBITS) {
146 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
149 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
152 static short t1_getnmsedec_ref(int x, int bitpos) {
153 if (bitpos > T1_NMSEDEC_FRACBITS) {
154 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
157 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
160 static void t1_updateflags(int *fp, int s) {
161 int *np = fp - (T1_MAXCBLKW + 2);
162 int *sp = fp + (T1_MAXCBLKW + 2);
179 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) {
182 opj_mqc_t *mqc = t1->mqc; /* MQC component */
184 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
185 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
186 v = int_abs(*dp) & one ? 1 : 0;
187 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
188 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
189 mqc_bypass_enc(mqc, v);
191 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
196 *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
197 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
198 mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
199 mqc_bypass_enc(mqc, v);
201 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
202 mqc_encode(mqc, v ^ t1_getspb(flag));
204 t1_updateflags(fp, v);
211 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
214 opj_raw_t *raw = t1->raw; /* RAW component */
215 opj_mqc_t *mqc = t1->mqc; /* MQC component */
217 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
218 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
219 if (type == T1_TYPE_RAW) {
220 if (raw_decode(raw)) {
221 v = raw_decode(raw); /* ESSAI */
222 *dp = v ? -oneplushalf : oneplushalf;
223 t1_updateflags(fp, v);
227 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
228 if (mqc_decode(mqc)) {
229 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
230 v = mqc_decode(mqc) ^ t1_getspb(flag);
231 *dp = v ? -oneplushalf : oneplushalf;
232 t1_updateflags(fp, v);
238 } /* VSC and BYPASS by Antonin */
240 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
241 int i, j, k, one, vsc;
243 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
244 for (k = 0; k < h; k += 4) {
245 for (i = 0; i < w; i++) {
246 for (j = k; j < k + 4 && j < h; j++) {
247 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
248 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
254 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
255 int i, j, k, one, half, oneplushalf, vsc;
258 oneplushalf = one | half;
259 for (k = 0; k < h; k += 4) {
260 for (i = 0; i < w; i++) {
261 for (j = k; j < k + 4 && j < h; j++) {
262 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
263 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
267 } /* VSC and BYPASS by Antonin */
269 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
272 opj_mqc_t *mqc = t1->mqc; /* MQC component */
274 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
275 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
276 *nmsedec += t1_getnmsedec_ref(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
277 v = int_abs(*dp) & one ? 1 : 0;
278 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
279 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
280 mqc_bypass_enc(mqc, v);
282 mqc_setcurctx(mqc, t1_getctxno_mag(flag));
289 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
292 opj_mqc_t *mqc = t1->mqc; /* MQC component */
293 opj_raw_t *raw = t1->raw; /* RAW component */
295 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
296 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
297 if (type == T1_TYPE_RAW) {
298 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
301 mqc_setcurctx(mqc, t1_getctxno_mag(flag));
304 t = v ? poshalf : neghalf;
305 *dp += *dp < 0 ? -t : t;
308 } /* VSC and BYPASS by Antonin */
310 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
311 int i, j, k, one, vsc;
313 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
314 for (k = 0; k < h; k += 4) {
315 for (i = 0; i < w; i++) {
316 for (j = k; j < k + 4 && j < h; j++) {
317 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
318 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
324 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
325 int i, j, k, one, poshalf, neghalf;
329 neghalf = bpno > 0 ? -poshalf : -1;
330 for (k = 0; k < h; k += 4) {
331 for (i = 0; i < w; i++) {
332 for (j = k; j < k + 4 && j < h; j++) {
333 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
334 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
338 } /* VSC and BYPASS by Antonin */
340 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) {
343 opj_mqc_t *mqc = t1->mqc; /* MQC component */
345 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
349 if (!(*fp & (T1_SIG | T1_VISIT))) {
350 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
351 v = int_abs(*dp) & one ? 1 : 0;
355 *nmsedec += t1_getnmsedec_sig(int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
356 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
358 mqc_encode(mqc, v ^ t1_getspb(flag));
359 t1_updateflags(fp, v);
366 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
369 opj_mqc_t *mqc = t1->mqc; /* MQC component */
371 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
375 if (!(flag & (T1_SIG | T1_VISIT))) {
376 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
377 if (mqc_decode(mqc)) {
379 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
380 v = mqc_decode(mqc) ^ t1_getspb(flag);
381 *dp = v ? -oneplushalf : oneplushalf;
382 t1_updateflags(fp, v);
387 } /* VSC and BYPASS by Antonin */
389 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
390 int i, j, k, one, agg, runlen, vsc;
392 opj_mqc_t *mqc = t1->mqc; /* MQC component */
395 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
396 for (k = 0; k < h; k += 4) {
397 for (i = 0; i < w; i++) {
399 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
400 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
401 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
402 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
403 || (t1->flags[1 + k + 3][1 + i]
404 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
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] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
415 for (runlen = 0; runlen < 4; runlen++) {
416 if (int_abs(t1->data[k + runlen][i]) & one)
419 mqc_setcurctx(mqc, T1_CTXNO_AGG);
420 mqc_encode(mqc, runlen != 4);
424 mqc_setcurctx(mqc, T1_CTXNO_UNI);
425 mqc_encode(mqc, runlen >> 1);
426 mqc_encode(mqc, runlen & 1);
430 for (j = k + runlen; j < k + 4 && j < h; j++) {
431 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
432 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
438 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
439 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
440 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
442 opj_mqc_t *mqc = t1->mqc; /* MQC component */
446 oneplushalf = one | half;
447 for (k = 0; k < h; k += 4) {
448 for (i = 0; i < w; i++) {
450 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
451 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
452 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
453 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
454 || (t1->flags[1 + k + 3][1 + i]
455 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
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] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
466 mqc_setcurctx(mqc, T1_CTXNO_AGG);
467 if (!mqc_decode(mqc)) {
470 mqc_setcurctx(mqc, T1_CTXNO_UNI);
471 runlen = mqc_decode(mqc);
472 runlen = (runlen << 1) | mqc_decode(mqc);
476 for (j = k + runlen; j < k + 4 && j < h; j++) {
477 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
478 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
484 mqc_setcurctx(mqc, T1_CTXNO_UNI);
486 v = (v << 1) | mqc_decode(mqc);
487 v = (v << 1) | mqc_decode(mqc);
488 v = (v << 1) | mqc_decode(mqc);
491 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
495 } /* VSC and BYPASS by Antonin */
498 /** mod fixed_quality */
499 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) {
500 double w1, w2, wmsedec;
502 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
503 w2 = dwt_getnorm(level, orient);
504 } else { /* if (qmfbid == 0) */
505 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
506 w2 = dwt_getnorm_real(level, orient);
508 wmsedec = w1 * w2 * stepsize * (1 << bpno);
509 wmsedec *= wmsedec * nmsedec / 8192.0;
514 /** mod fixed_quality */
515 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) {
522 double cumwmsedec = 0;
523 char type = T1_TYPE_MQ;
525 opj_mqc_t *mqc = t1->mqc; /* MQC component */
527 w = cblk->x1 - cblk->x0;
528 h = cblk->y1 - cblk->y0;
531 for (j = 0; j < h; j++) {
532 for (i = 0; i < w; i++) {
533 max = int_max(max, int_abs(t1->data[j][i]));
537 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
539 for (i = 0; i <= h; ++i) {
540 memset(&t1->flags[i], 0, (w+1) * sizeof(int));
543 bpno = cblk->numbps - 1;
546 mqc_resetstates(mqc);
547 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
548 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
549 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
550 mqc_init_enc(mqc, cblk->data);
552 for (passno = 0; bpno >= 0; passno++) {
553 opj_tcd_pass_t *pass = &cblk->passes[passno];
555 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
559 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
562 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
565 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
566 /* code switch SEGMARK (i.e. SEGSYM) */
567 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
568 mqc_segmark_enc(mqc);
573 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
574 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
576 /* Code switch "RESTART" (i.e. TERMALL) */
577 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
578 if (type == T1_TYPE_RAW) {
581 /* correction = mqc_bypass_flush_enc(); */
582 } else { /* correction = mqc_restart_enc(); */
588 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
589 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
590 if (type == T1_TYPE_RAW) {
593 /* correction = mqc_bypass_flush_enc(); */
594 } else { /* correction = mqc_restart_enc(); */
604 if (++passtype == 3) {
609 if (pass->term && bpno > 0) {
610 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
611 if (type == T1_TYPE_RAW)
612 mqc_bypass_init_enc(mqc);
614 mqc_restart_init_enc(mqc);
617 pass->distortiondec = cumwmsedec;
618 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
620 /* Code-switch "RESET" */
621 if (cblksty & J2K_CCP_CBLKSTY_RESET)
625 /* Code switch "ERTERM" (i.e. PTERM) */
626 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
628 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
631 cblk->totalpasses = passno;
633 for (passno = 0; passno<cblk->totalpasses; passno++) {
634 opj_tcd_pass_t *pass = &cblk->passes[passno];
635 if (pass->rate > mqc_numbytes(mqc))
636 pass->rate = mqc_numbytes(mqc);
637 /*Preventing generation of FF as last data byte of a pass*/
638 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
641 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
645 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
649 char type = T1_TYPE_MQ; /* BYPASS mode */
651 opj_raw_t *raw = t1->raw; /* RAW component */
652 opj_mqc_t *mqc = t1->mqc; /* MQC component */
654 w = cblk->x1 - cblk->x0;
655 h = cblk->y1 - cblk->y0;
657 for (i = 0; i <= h; ++i) {
658 memset(&t1->flags[i], 0, (w + 1) * sizeof(int));
661 for (i = 0; i < h; ++i) {
662 memset(&t1->data[i], 0, w * sizeof(int));
665 bpno = roishift + cblk->numbps - 1;
668 mqc_resetstates(mqc);
669 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
670 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
671 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
673 for (segno = 0; segno < cblk->numsegs; segno++) {
674 opj_tcd_seg_t *seg = &cblk->segs[segno];
677 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
678 if (type == T1_TYPE_RAW) {
679 raw_init_dec(raw, seg->data, seg->len);
681 mqc_init_dec(mqc, seg->data, seg->len);
684 for (passno = 0; passno < seg->numpasses; passno++) {
687 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
690 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
693 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
697 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
698 mqc_resetstates(mqc);
699 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
700 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
701 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
703 if (++passtype == 3) {
711 /* ----------------------------------------------------------------------- */
713 opj_t1_t* t1_create(opj_common_ptr cinfo) {
714 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
717 /* create MQC and RAW handles */
718 t1->mqc = mqc_create();
719 t1->raw = raw_create();
724 void t1_destroy(opj_t1_t *t1) {
726 /* destroy MQC and RAW handles */
727 mqc_destroy(t1->mqc);
728 raw_destroy(t1->raw);
733 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
734 int compno, resno, bandno, precno, cblkno;
736 tile->distotile = 0; /* fixed_quality */
738 for (compno = 0; compno < tile->numcomps; compno++) {
739 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
741 for (resno = 0; resno < tilec->numresolutions; resno++) {
742 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
744 for (bandno = 0; bandno < res->numbands; bandno++) {
745 opj_tcd_band_t *band = &res->bands[bandno];
747 for (precno = 0; precno < res->pw * res->ph; precno++) {
748 opj_tcd_precinct_t *prc = &band->precincts[precno];
750 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
751 int x, y, w, i, j, orient;
752 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
754 x = cblk->x0 - band->x0;
755 y = cblk->y0 - band->y0;
756 if (band->bandno & 1) {
757 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
758 x += pres->x1 - pres->x0;
760 if (band->bandno & 2) {
761 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
762 y += pres->y1 - pres->y0;
765 w = tilec->x1 - tilec->x0;
766 if (tcp->tccps[compno].qmfbid == 1) {
767 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
768 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
770 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
773 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
774 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
775 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
778 tilec->data[x + i + (y + j) * w],
779 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
783 orient = band->bandno; /* FIXME */
786 } else if (orient == 1) {
790 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
799 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
800 int compno, resno, bandno, precno, cblkno;
802 for (compno = 0; compno < tile->numcomps; compno++) {
803 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
805 for (resno = 0; resno < tilec->numresolutions; resno++) {
806 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
808 for (bandno = 0; bandno < res->numbands; bandno++) {
809 opj_tcd_band_t *band = &res->bands[bandno];
811 for (precno = 0; precno < res->pw * res->ph; precno++) {
812 opj_tcd_precinct_t *prc = &band->precincts[precno];
814 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
815 int x, y, w, i, j, orient;
816 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
818 orient = band->bandno; /* FIXME */
821 } else if (orient == 1) {
825 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
827 x = cblk->x0 - band->x0;
828 y = cblk->y0 - band->y0;
829 if (band->bandno & 1) {
830 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
831 x += pres->x1 - pres->x0;
833 if (band->bandno & 2) {
834 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
835 y += pres->y1 - pres->y0;
838 if (tcp->tccps[compno].roishift) {
839 int thresh = 1 << tcp->tccps[compno].roishift;
840 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
841 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
842 int val = t1->data[j][i];
843 int mag = int_abs(val);
845 mag >>= tcp->tccps[compno].roishift;
846 t1->data[j][i] = val < 0 ? -mag : mag;
852 w = tilec->x1 - tilec->x0;
853 if (tcp->tccps[compno].qmfbid == 1) {
854 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
855 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
856 tilec->data[x + i + (y + j) * w] = t1->data[j][i]/2;
859 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
860 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
861 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
862 if (t1->data[j][i] >> 1 == 0) {
863 tilec->data[x + i + (y + j) * w] = 0;
865 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
866 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
867 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);