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 */
629 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
631 /* Code-switch "RESET" */
632 if (cblksty & J2K_CCP_CBLKSTY_RESET)
636 /* Code switch "ERTERM" (i.e. PTERM) */
637 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
639 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
642 cblk->totalpasses = passno;
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 /* Changed by Dmitry Kolyadin */
658 for (j = 0; j <= h; j++) {
659 for (i = 0; i <= w; i++) {
664 /* Changed by Dmitry Kolyadin */
665 for (i = 0; i < w; i++) {
666 for (j = 0; j < h; j++){
671 bpno = roishift + cblk->numbps - 1;
674 mqc_resetstates(mqc);
675 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
676 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
677 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
679 for (segno = 0; segno < cblk->numsegs; segno++) {
680 opj_tcd_seg_t *seg = &cblk->segs[segno];
683 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
684 if (type == T1_TYPE_RAW) {
685 raw_init_dec(raw, seg->data, seg->len);
687 mqc_init_dec(mqc, seg->data, seg->len);
690 for (passno = 0; passno < seg->numpasses; passno++) {
693 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
696 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
699 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
703 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
704 mqc_resetstates(mqc);
705 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
706 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
707 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
709 if (++passtype == 3) {
717 static int t1_init_ctxno_zc(int f, int orient) {
718 int h, v, d, n, t, hv;
720 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
721 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
722 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
761 } else if (hv == 1) {
769 } else if (hv == 1) {
786 return (T1_CTXNO_ZC + n);
789 static int t1_init_ctxno_sc(int f) {
793 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
794 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
795 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
796 (T1_SIG_E | T1_SGN_E)) +
797 ((f & (T1_SIG_W | T1_SGN_W)) ==
798 (T1_SIG_W | T1_SGN_W)), 1);
800 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
801 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
802 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
803 (T1_SIG_N | T1_SGN_N)) +
804 ((f & (T1_SIG_S | T1_SGN_S)) ==
805 (T1_SIG_S | T1_SGN_S)), 1);
818 } else if (hc == 1) {
827 return (T1_CTXNO_SC + n);
830 static int t1_init_ctxno_mag(int f) {
832 if (!(f & T1_REFINE))
833 n = (f & (T1_SIG_OTH)) ? 1 : 0;
837 return (T1_CTXNO_MAG + n);
840 static int t1_init_spb(int f) {
843 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
844 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
845 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
846 (T1_SIG_E | T1_SGN_E)) +
847 ((f & (T1_SIG_W | T1_SGN_W)) ==
848 (T1_SIG_W | T1_SGN_W)), 1);
850 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
851 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
852 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
853 (T1_SIG_N | T1_SGN_N)) +
854 ((f & (T1_SIG_S | T1_SGN_S)) ==
855 (T1_SIG_S | T1_SGN_S)), 1);
860 n = (!(hc > 0 || (!hc && vc > 0)));
865 static void t1_init_luts(opj_t1_t *t1) {
868 for (j = 0; j < 4; j++) {
869 for (i = 0; i < 256; ++i) {
870 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
873 for (i = 0; i < 256; i++) {
874 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
876 for (j = 0; j < 2; j++) {
877 for (i = 0; i < 2048; ++i) {
878 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
881 for (i = 0; i < 256; ++i) {
882 t1->lut_spb[i] = t1_init_spb(i << 4);
884 /* FIXME FIXME FIXME */
885 /* fprintf(stdout,"nmsedec luts:\n"); */
886 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
887 t = i / pow(2, T1_NMSEDEC_FRACBITS);
890 t1->lut_nmsedec_sig[i] =
892 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
893 t1->lut_nmsedec_sig0[i] =
895 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
897 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
902 t1->lut_nmsedec_ref[i] =
904 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
905 t1->lut_nmsedec_ref0[i] =
907 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
911 /* ----------------------------------------------------------------------- */
913 opj_t1_t* t1_create(opj_common_ptr cinfo) {
914 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
917 /* create MQC and RAW handles */
918 t1->mqc = mqc_create();
919 t1->raw = raw_create();
920 /* initialize the look-up tables of the Tier-1 coder/decoder */
926 void t1_destroy(opj_t1_t *t1) {
928 /* destroy MQC and RAW handles */
929 mqc_destroy(t1->mqc);
930 raw_destroy(t1->raw);
935 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
936 int compno, resno, bandno, precno, cblkno;
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 int x, y, w, i, j, orient;
954 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
956 x = cblk->x0 - band->x0;
957 y = cblk->y0 - band->y0;
958 if (band->bandno & 1) {
959 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
960 x += pres->x1 - pres->x0;
962 if (band->bandno & 2) {
963 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
964 y += pres->y1 - pres->y0;
967 w = tilec->x1 - tilec->x0;
968 if (tcp->tccps[compno].qmfbid == 1) {
969 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
970 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
972 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
975 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
976 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
977 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
980 tilec->data[x + i + (y + j) * w],
981 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
985 orient = band->bandno; /* FIXME */
988 } else if (orient == 1) {
992 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1001 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1002 int compno, resno, bandno, precno, cblkno;
1004 for (compno = 0; compno < tile->numcomps; compno++) {
1005 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1007 for (resno = 0; resno < tilec->numresolutions; resno++) {
1008 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1010 for (bandno = 0; bandno < res->numbands; bandno++) {
1011 opj_tcd_band_t *band = &res->bands[bandno];
1013 for (precno = 0; precno < res->pw * res->ph; precno++) {
1014 opj_tcd_precinct_t *prc = &band->precincts[precno];
1016 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1017 int x, y, w, i, j, orient;
1018 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1020 orient = band->bandno; /* FIXME */
1023 } else if (orient == 1) {
1027 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1029 x = cblk->x0 - band->x0;
1030 y = cblk->y0 - band->y0;
1031 if (band->bandno & 1) {
1032 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1033 x += pres->x1 - pres->x0;
1035 if (band->bandno & 2) {
1036 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1037 y += pres->y1 - pres->y0;
1040 if (tcp->tccps[compno].roishift) {
1041 int thresh = 1 << tcp->tccps[compno].roishift;
1042 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1043 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1044 int val = t1->data[j][i];
1045 int mag = int_abs(val);
1046 if (mag >= thresh) {
1047 mag >>= tcp->tccps[compno].roishift;
1048 t1->data[j][i] = val < 0 ? -mag : mag;
1054 w = tilec->x1 - tilec->x0;
1055 if (tcp->tccps[compno].qmfbid == 1) {
1056 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1057 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1058 tilec->data[x + i + (y + j) * w] = t1->data[j][i]/2;
1061 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1062 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1063 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1064 if (t1->data[j][i] >> 1 == 0) {
1065 tilec->data[x + i + (y + j) * w] = 0;
1067 double tmp = (double)((t1->data[j][i] << 12) * band->stepsize);
1068 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1069 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);