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 /* Changed by Dmitry Kolyadin */
547 for (i = 0; i <= w; i++) {
548 for (j = 0; j <= h; j++) {
553 bpno = cblk->numbps - 1;
556 mqc_resetstates(mqc);
557 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
558 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
559 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
560 mqc_init_enc(mqc, cblk->data);
562 for (passno = 0; bpno >= 0; passno++) {
563 opj_tcd_pass_t *pass = &cblk->passes[passno];
565 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
569 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
572 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
575 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
576 /* code switch SEGMARK (i.e. SEGSYM) */
577 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
578 mqc_segmark_enc(mqc);
583 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
584 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
586 /* Code switch "RESTART" (i.e. TERMALL) */
587 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
588 if (type == T1_TYPE_RAW) {
591 /* correction = mqc_bypass_flush_enc(); */
592 } else { /* correction = mqc_restart_enc(); */
598 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
599 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
600 if (type == T1_TYPE_RAW) {
603 /* correction = mqc_bypass_flush_enc(); */
604 } else { /* correction = mqc_restart_enc(); */
614 if (++passtype == 3) {
619 if (pass->term && bpno > 0) {
620 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
621 if (type == T1_TYPE_RAW)
622 mqc_bypass_init_enc(mqc);
624 mqc_restart_init_enc(mqc);
627 pass->distortiondec = cumwmsedec;
628 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
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;
643 for (passno = 0; passno<cblk->totalpasses; passno++) {
644 opj_tcd_pass_t *pass = &cblk->passes[passno];
645 if (pass->rate > mqc_numbytes(mqc))
646 pass->rate = mqc_numbytes(mqc);
647 /*Preventing generation of FF as last data byte of a pass*/
648 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
651 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
655 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
659 char type = T1_TYPE_MQ; /* BYPASS mode */
661 opj_raw_t *raw = t1->raw; /* RAW component */
662 opj_mqc_t *mqc = t1->mqc; /* MQC component */
664 w = cblk->x1 - cblk->x0;
665 h = cblk->y1 - cblk->y0;
667 /* Changed by Dmitry Kolyadin */
668 for (j = 0; j <= h; j++) {
669 for (i = 0; i <= w; i++) {
674 /* Changed by Dmitry Kolyadin */
675 for (i = 0; i < w; i++) {
676 for (j = 0; j < h; j++){
681 bpno = roishift + cblk->numbps - 1;
684 mqc_resetstates(mqc);
685 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
686 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
687 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
689 for (segno = 0; segno < cblk->numsegs; segno++) {
690 opj_tcd_seg_t *seg = &cblk->segs[segno];
693 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
694 if (type == T1_TYPE_RAW) {
695 raw_init_dec(raw, seg->data, seg->len);
697 mqc_init_dec(mqc, seg->data, seg->len);
700 for (passno = 0; passno < seg->numpasses; passno++) {
703 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
706 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
709 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
713 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
714 mqc_resetstates(mqc);
715 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
716 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
717 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
719 if (++passtype == 3) {
727 static int t1_init_ctxno_zc(int f, int orient) {
728 int h, v, d, n, t, hv;
730 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
731 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
732 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
771 } else if (hv == 1) {
779 } else if (hv == 1) {
796 return (T1_CTXNO_ZC + n);
799 static int t1_init_ctxno_sc(int f) {
803 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
804 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
805 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
806 (T1_SIG_E | T1_SGN_E)) +
807 ((f & (T1_SIG_W | T1_SGN_W)) ==
808 (T1_SIG_W | T1_SGN_W)), 1);
810 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
811 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
812 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
813 (T1_SIG_N | T1_SGN_N)) +
814 ((f & (T1_SIG_S | T1_SGN_S)) ==
815 (T1_SIG_S | T1_SGN_S)), 1);
828 } else if (hc == 1) {
837 return (T1_CTXNO_SC + n);
840 static int t1_init_ctxno_mag(int f) {
842 if (!(f & T1_REFINE))
843 n = (f & (T1_SIG_OTH)) ? 1 : 0;
847 return (T1_CTXNO_MAG + n);
850 static int t1_init_spb(int f) {
853 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
854 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
855 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
856 (T1_SIG_E | T1_SGN_E)) +
857 ((f & (T1_SIG_W | T1_SGN_W)) ==
858 (T1_SIG_W | T1_SGN_W)), 1);
860 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
861 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
862 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
863 (T1_SIG_N | T1_SGN_N)) +
864 ((f & (T1_SIG_S | T1_SGN_S)) ==
865 (T1_SIG_S | T1_SGN_S)), 1);
870 n = (!(hc > 0 || (!hc && vc > 0)));
875 static void t1_init_luts(opj_t1_t *t1) {
878 for (j = 0; j < 4; j++) {
879 for (i = 0; i < 256; ++i) {
880 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
883 for (i = 0; i < 256; i++) {
884 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
886 for (j = 0; j < 2; j++) {
887 for (i = 0; i < 2048; ++i) {
888 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
891 for (i = 0; i < 256; ++i) {
892 t1->lut_spb[i] = t1_init_spb(i << 4);
894 /* FIXME FIXME FIXME */
895 /* fprintf(stdout,"nmsedec luts:\n"); */
896 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
897 t = i / pow(2, T1_NMSEDEC_FRACBITS);
900 t1->lut_nmsedec_sig[i] =
902 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
903 t1->lut_nmsedec_sig0[i] =
905 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
907 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
912 t1->lut_nmsedec_ref[i] =
914 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
915 t1->lut_nmsedec_ref0[i] =
917 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
921 /* ----------------------------------------------------------------------- */
923 opj_t1_t* t1_create(opj_common_ptr cinfo) {
924 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
927 /* create MQC and RAW handles */
928 t1->mqc = mqc_create();
929 t1->raw = raw_create();
930 /* initialize the look-up tables of the Tier-1 coder/decoder */
936 void t1_destroy(opj_t1_t *t1) {
938 /* destroy MQC and RAW handles */
939 mqc_destroy(t1->mqc);
940 raw_destroy(t1->raw);
945 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
946 int compno, resno, bandno, precno, cblkno;
948 tile->distotile = 0; /* fixed_quality */
950 for (compno = 0; compno < tile->numcomps; compno++) {
951 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
953 for (resno = 0; resno < tilec->numresolutions; resno++) {
954 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
956 for (bandno = 0; bandno < res->numbands; bandno++) {
957 opj_tcd_band_t *band = &res->bands[bandno];
959 for (precno = 0; precno < res->pw * res->ph; precno++) {
960 opj_tcd_precinct_t *prc = &band->precincts[precno];
962 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
963 int x, y, w, i, j, orient;
964 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
966 x = cblk->x0 - band->x0;
967 y = cblk->y0 - band->y0;
968 if (band->bandno & 1) {
969 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
970 x += pres->x1 - pres->x0;
972 if (band->bandno & 2) {
973 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
974 y += pres->y1 - pres->y0;
977 w = tilec->x1 - tilec->x0;
978 if (tcp->tccps[compno].qmfbid == 1) {
979 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
980 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
982 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
985 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
986 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
987 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
990 tilec->data[x + i + (y + j) * w],
991 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
995 orient = band->bandno; /* FIXME */
998 } else if (orient == 1) {
1002 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1011 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1012 int compno, resno, bandno, precno, cblkno;
1014 for (compno = 0; compno < tile->numcomps; compno++) {
1015 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1017 for (resno = 0; resno < tilec->numresolutions; resno++) {
1018 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1020 for (bandno = 0; bandno < res->numbands; bandno++) {
1021 opj_tcd_band_t *band = &res->bands[bandno];
1023 for (precno = 0; precno < res->pw * res->ph; precno++) {
1024 opj_tcd_precinct_t *prc = &band->precincts[precno];
1026 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1027 int x, y, w, i, j, orient;
1028 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1030 orient = band->bandno; /* FIXME */
1033 } else if (orient == 1) {
1037 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1039 x = cblk->x0 - band->x0;
1040 y = cblk->y0 - band->y0;
1041 if (band->bandno & 1) {
1042 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1043 x += pres->x1 - pres->x0;
1045 if (band->bandno & 2) {
1046 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1047 y += pres->y1 - pres->y0;
1050 if (tcp->tccps[compno].roishift) {
1051 int thresh = 1 << tcp->tccps[compno].roishift;
1052 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1053 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1054 int val = t1->data[j][i];
1055 int mag = int_abs(val);
1056 if (mag >= thresh) {
1057 mag >>= tcp->tccps[compno].roishift;
1058 t1->data[j][i] = val < 0 ? -mag : mag;
1064 w = tilec->x1 - tilec->x0;
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 tilec->data[x + i + (y + j) * w] = t1->data[j][i]/2;
1071 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1072 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1073 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1074 if (t1->data[j][i] >> 1 == 0) {
1075 tilec->data[x + i + (y + j) * w] = 0;
1077 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
1078 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1079 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);