2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
36 /** @name Local static functions */
39 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
40 static int t1_getctxno_sc(opj_t1_t *t1, int f);
41 static int t1_getctxno_mag(opj_t1_t *t1, int f);
42 static int t1_getspb(opj_t1_t *t1, int f);
43 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
44 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
45 static void t1_updateflags(int *fp, int s);
47 Encode significant pass
49 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
51 Decode significant pass
53 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
55 Encode significant pass
57 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);
59 Decode significant pass
61 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
63 Encode refinement pass
65 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
67 Decode refinement pass
69 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
71 Encode refinement pass
73 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
75 Decode refinement pass
77 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
81 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
85 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
89 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
93 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
97 @param cblk Code-block coding parameters
99 @param compno Component number
103 @param cblksty Code-block style
107 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
111 @param cblk Code-block coding parameters
113 @param roishift Region of interest shifting value
114 @param cblksty Code-block style
116 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
118 static int t1_init_ctxno_zc(int f, int orient);
119 static int t1_init_ctxno_sc(int f);
120 static int t1_init_ctxno_mag(int f);
121 static int t1_init_spb(int f);
123 Initialize the look-up tables of the Tier-1 coder/decoder
126 static void t1_init_luts(opj_t1_t *t1);
132 /* ----------------------------------------------------------------------- */
134 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
135 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
138 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
139 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
142 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
143 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
146 static int t1_getspb(opj_t1_t *t1, int f) {
147 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
150 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
151 if (bitpos > T1_NMSEDEC_FRACBITS) {
152 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
155 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
158 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
159 if (bitpos > T1_NMSEDEC_FRACBITS) {
160 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
163 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
166 static void t1_updateflags(int *fp, int s) {
167 int *np = fp - (T1_MAXCBLKW + 2);
168 int *sp = fp + (T1_MAXCBLKW + 2);
185 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
188 opj_mqc_t *mqc = t1->mqc; /* MQC component */
190 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
191 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
192 v = int_abs(*dp) & one ? 1 : 0;
193 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
194 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
195 mqc_bypass_enc(mqc, v);
197 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
202 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
203 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
204 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
205 mqc_bypass_enc(mqc, v);
207 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
208 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
210 t1_updateflags(fp, v);
217 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
220 opj_raw_t *raw = t1->raw; /* RAW component */
221 opj_mqc_t *mqc = t1->mqc; /* MQC component */
223 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
224 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
225 if (type == T1_TYPE_RAW) {
226 if (raw_decode(raw)) {
227 v = raw_decode(raw); /* ESSAI */
228 *dp = v ? -oneplushalf : oneplushalf;
229 t1_updateflags(fp, v);
233 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
234 if (mqc_decode(mqc)) {
235 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
236 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
237 *dp = v ? -oneplushalf : oneplushalf;
238 t1_updateflags(fp, v);
244 } /* VSC and BYPASS by Antonin */
246 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {
247 int i, j, k, m, one, vsc;
249 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
250 for (m = 0; m < l; m++) {
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 & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
255 t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
262 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
263 int i, j, k, m, one, half, oneplushalf, vsc;
266 oneplushalf = one | half;
267 for (m = 0; m < l; m++) {
268 for (k = 0; k < h; k += 4) {
269 for (i = 0; i < w; i++) {
270 for (j = k; j < k + 4 && j < h; j++) {
271 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
272 t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
277 } /* VSC and BYPASS by Antonin */
279 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
282 opj_mqc_t *mqc = t1->mqc; /* MQC component */
284 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
285 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
286 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
287 v = int_abs(*dp) & one ? 1 : 0;
288 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
289 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
290 mqc_bypass_enc(mqc, v);
292 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
299 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
302 opj_mqc_t *mqc = t1->mqc; /* MQC component */
303 opj_raw_t *raw = t1->raw; /* RAW component */
305 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
306 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
307 if (type == T1_TYPE_RAW) {
308 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
311 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
314 t = v ? poshalf : neghalf;
315 *dp += *dp < 0 ? -t : t;
318 } /* VSC and BYPASS by Antonin */
320 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
321 int i, j, k, m, one, vsc;
323 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
324 for (m = 0; m < l; m++) {
325 for (k = 0; k < h; k += 4) {
326 for (i = 0; i < w; i++) {
327 for (j = k; j < k + 4 && j < h; j++) {
328 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
329 t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
336 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
337 int i, j, k, m, one, poshalf, neghalf;
341 neghalf = bpno > 0 ? -poshalf : -1;
342 for (m = 0; m < l; m++) {
343 for (k = 0; k < h; k += 4) {
344 for (i = 0; i < w; i++) {
345 for (j = k; j < k + 4 && j < h; j++) {
346 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
347 t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
352 } /* VSC and BYPASS by Antonin */
354 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) {
357 opj_mqc_t *mqc = t1->mqc; /* MQC component */
359 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
363 if (!(*fp & (T1_SIG | T1_VISIT))) {
364 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
365 v = int_abs(*dp) & one ? 1 : 0;
369 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
370 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
372 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
373 t1_updateflags(fp, v);
380 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
383 opj_mqc_t *mqc = t1->mqc; /* MQC component */
385 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
389 if (!(flag & (T1_SIG | T1_VISIT))) {
390 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
391 if (mqc_decode(mqc)) {
393 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
394 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
395 *dp = v ? -oneplushalf : oneplushalf;
396 t1_updateflags(fp, v);
401 } /* VSC and BYPASS by Antonin */
403 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
404 int i, j, k, m, one, agg, runlen, vsc;
406 opj_mqc_t *mqc = t1->mqc; /* MQC component */
409 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
410 for (m = 0; m < l; m++) {
411 for (k = 0; k < h; k += 4) {
412 for (i = 0; i < w; i++) {
414 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
415 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
416 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
417 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
418 || (t1->flags[1 + m][1 + k + 3][1 + i]
419 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
421 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
422 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
423 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
424 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
430 for (runlen = 0; runlen < 4; runlen++) {
431 if (int_abs(t1->data[m][k + runlen][i]) & one)
434 mqc_setcurctx(mqc, T1_CTXNO_AGG);
435 mqc_encode(mqc, runlen != 4);
439 mqc_setcurctx(mqc, T1_CTXNO_UNI);
440 mqc_encode(mqc, runlen >> 1);
441 mqc_encode(mqc, runlen & 1);
445 for (j = k + runlen; j < k + 4 && j < h; j++) {
446 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
447 t1_enc_clnpass_step(t1, &(t1->flags[1 + m][1 + j][1 + i]), &(t1->data[m][j][i]), orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
454 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
455 int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
456 int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
458 opj_mqc_t *mqc = t1->mqc; /* MQC component */
462 oneplushalf = one | half;
463 for (m = 0; m < l; m++) {
464 for (k = 0; k < h; k += 4) {
465 for (i = 0; i < w; i++) {
467 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
468 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
469 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
470 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
471 || (t1->flags[1 + m][1 + k + 3][1 + i]
472 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
474 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
475 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
476 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
477 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
483 mqc_setcurctx(mqc, T1_CTXNO_AGG);
484 if (!mqc_decode(mqc)) {
487 mqc_setcurctx(mqc, T1_CTXNO_UNI);
488 runlen = mqc_decode(mqc);
489 runlen = (runlen << 1) | mqc_decode(mqc);
493 for (j = k + runlen; j < k + 4 && j < h; j++) {
494 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
495 t1_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
502 mqc_setcurctx(mqc, T1_CTXNO_UNI);
504 v = (v << 1) | mqc_decode(mqc);
505 v = (v << 1) | mqc_decode(mqc);
506 v = (v << 1) | mqc_decode(mqc);
509 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
513 } /* VSC and BYPASS by Antonin */
516 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
523 double cumwmsedec = 0;
524 char type = T1_TYPE_MQ;
526 opj_mqc_t *mqc = t1->mqc; /* MQC component */
528 w = cblk->x1 - cblk->x0;
529 h = cblk->y1 - cblk->y0;
530 l = cblk->z1 - cblk->z0;
533 for (k = 0; k < l; k++) {
534 for (j = 0; j < h; j++) {
535 for (i = 0; i < w; i++) {
536 max = int_max(max, int_abs(t1->data[k][j][i]));
540 for (k = 0; k <= l; k++) {
541 for (j = 0; j <= h; j++) {
542 for (i = 0; i <= w; i++) {
543 t1->flags[k][j][i] = 0;
548 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
550 bpno = cblk->numbps - 1;
554 mqc_init_enc(mqc, cblk->data);
556 for (passno = 0; bpno >= 0; passno++) {
557 opj_tcd_pass_t *pass = &cblk->passes[passno];
560 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
561 /*fprintf(stdout,"passno %d passtype %d w %d h %d l %d bpno %d orient %d type %d cblksty %d\n",passno,passtype,w,h,l,bpno,orient,type,cblksty);*/
565 t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
568 t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
571 /*fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient);*/
572 t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
573 /* code switch SEGMARK (i.e. SEGSYM) */
574 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
575 mqc_segmark_enc(mqc);
580 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
581 cumwmsedec += tmpwmsedec;
582 tile->distotile += tmpwmsedec;
584 /* Code switch "RESTART" (i.e. TERMALL) */
585 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
586 if (type == T1_TYPE_RAW) {
589 /* correction = mqc_bypass_flush_enc(); */
590 } else { /* correction = mqc_restart_enc(); */
596 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
597 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
598 if (type == T1_TYPE_RAW) {
601 /* correction = mqc_bypass_flush_enc(); */
602 } else { /* correction = mqc_restart_enc(); */
612 if (++passtype == 3) {
617 if (pass->term && bpno > 0) {
618 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
619 if (type == T1_TYPE_RAW)
620 mqc_bypass_init_enc(mqc);
622 mqc_restart_init_enc(mqc);
625 pass->distortiondec = cumwmsedec;
626 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
627 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
629 /* Code-switch "RESET" */
630 if (cblksty & J3D_CCP_CBLKSTY_RESET)
634 /* Code switch "ERTERM" (i.e. PTERM) */
635 if (cblksty & J3D_CCP_CBLKSTY_PTERM)
637 else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
640 cblk->totalpasses = passno;
643 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
644 int i, j, k, w, h, l;
647 char type = T1_TYPE_MQ; /* BYPASS mode */
649 opj_raw_t *raw = t1->raw; /* RAW component */
650 opj_mqc_t *mqc = t1->mqc; /* MQC component */
652 w = cblk->x1 - cblk->x0;
653 h = cblk->y1 - cblk->y0;
654 l = cblk->z1 - cblk->z0;
656 for (k = 0; k < l; k++) {
657 for (j = 0; j < h; j++) {
658 for (i = 0; i < w; i++) {
659 t1->data[k][j][i] = 0;
664 for (k = 0; k <= l; k++) {
665 for (j = 0; j <= h; j++) {
666 for (i = 0; i <= w; i++) {
667 t1->flags[k][j][i] = 0;
672 bpno = roishift + cblk->numbps - 1;
677 for (segno = 0; segno < cblk->numsegs; segno++) {
678 opj_tcd_seg_t *seg = &cblk->segs[segno];
681 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
682 if (type == T1_TYPE_RAW) {
683 raw_init_dec(raw, seg->data, seg->len);
685 mqc_init_dec(mqc, seg->data, seg->len);
688 for (passno = 0; passno < seg->numpasses; passno++) {
691 t1_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
694 t1_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
697 t1_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
701 if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
704 if (++passtype == 3) {
712 static int t1_init_ctxno_zc(int f, int orient) {
713 int h, v, d, n, t, hv;
715 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
716 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
717 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
756 } else if (hv == 1) {
764 } else if (hv == 1) {
781 return (T1_CTXNO_ZC + n);
784 static int t1_init_ctxno_sc(int f) {
788 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
789 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
790 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
791 (T1_SIG_E | T1_SGN_E)) +
792 ((f & (T1_SIG_W | T1_SGN_W)) ==
793 (T1_SIG_W | T1_SGN_W)), 1);
795 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
796 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
797 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
798 (T1_SIG_N | T1_SGN_N)) +
799 ((f & (T1_SIG_S | T1_SGN_S)) ==
800 (T1_SIG_S | T1_SGN_S)), 1);
813 } else if (hc == 1) {
822 return (T1_CTXNO_SC + n);
825 static int t1_init_ctxno_mag(int f) {
827 if (!(f & T1_REFINE))
828 n = (f & (T1_SIG_OTH)) ? 1 : 0;
832 return (T1_CTXNO_MAG + n);
835 static int t1_init_spb(int f) {
838 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
839 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
840 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
841 (T1_SIG_E | T1_SGN_E)) +
842 ((f & (T1_SIG_W | T1_SGN_W)) ==
843 (T1_SIG_W | T1_SGN_W)), 1);
845 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
846 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
847 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
848 (T1_SIG_N | T1_SGN_N)) +
849 ((f & (T1_SIG_S | T1_SGN_S)) ==
850 (T1_SIG_S | T1_SGN_S)), 1);
855 n = (!(hc > 0 || (!hc && vc > 0)));
860 static void t1_init_luts(opj_t1_t *t1) {
863 for (j = 0; j < 4; j++) {
864 for (i = 0; i < 256; ++i) {
865 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
868 for (i = 0; i < 256; i++) {
869 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
871 for (j = 0; j < 2; j++) {
872 for (i = 0; i < 2048; ++i) {
873 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
876 for (i = 0; i < 256; ++i) {
877 t1->lut_spb[i] = t1_init_spb(i << 4);
879 /* FIXME FIXME FIXME */
880 /* fprintf(stdout,"nmsedec luts:\n"); */
881 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
882 t = i / pow(2, T1_NMSEDEC_FRACBITS);
885 t1->lut_nmsedec_sig[i] =
887 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
888 t1->lut_nmsedec_sig0[i] =
890 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
892 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
897 t1->lut_nmsedec_ref[i] =
899 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
900 t1->lut_nmsedec_ref0[i] =
902 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
906 /* ----------------------------------------------------------------------- */
908 opj_t1_t* t1_create(opj_common_ptr cinfo) {
909 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
912 /* create MQC and RAW handles */
913 t1->mqc = mqc_create();
914 t1->raw = raw_create();
915 /* initialize the look-up tables of the Tier-1 coder/decoder */
921 void t1_destroy(opj_t1_t *t1) {
923 /* destroy MQC and RAW handles */
924 mqc_destroy(t1->mqc);
925 raw_destroy(t1->raw);
926 /*opj_free(t1->data);*/
927 /*opj_free(t1->flags);*/
932 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
933 int compno, resno, bandno, precno, cblkno;
934 int x, y, z, i, j, k, orient;
938 /* char filename[10];*/
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->numresolution[0]; resno++) {
945 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
947 /* Weighted first order entropy
948 sprintf(filename,"res%d.txt",resno);
949 if ((fid = fopen(filename,"w")) == 0){
950 fprintf(stdout,"Error while opening %s\n", filename);
954 for (bandno = 0; bandno < res->numbands; bandno++) {
955 opj_tcd_band_t *band = &res->bands[bandno];
956 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
957 opj_tcd_precinct_t *prc = &band->precincts[precno];
959 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
960 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
962 /*fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno);*/
963 if (band->bandno == 0) {
964 x = cblk->x0 - band->x0;
965 y = cblk->y0 - band->y0;
966 z = cblk->z0 - band->z0;
967 } else if (band->bandno == 1) {
968 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
969 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
970 y = cblk->y0 - band->y0;
971 z = cblk->z0 - band->z0;
972 } else if (band->bandno == 2) {
973 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
974 x = cblk->x0 - band->x0;
975 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
976 z = cblk->z0 - band->z0;
977 } else if (band->bandno == 3) {
978 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
979 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
980 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
981 z = cblk->z0 - band->z0;
982 } else if (band->bandno == 4) {
983 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
984 x = cblk->x0 - band->x0;
985 y = cblk->y0 - band->y0;
986 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
987 } else if (band->bandno == 5) {
988 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
989 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
990 y = cblk->y0 - band->y0;
991 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
992 } else if (band->bandno == 6) {
993 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
994 x = cblk->x0 - band->x0;
995 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
996 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
997 } else if (band->bandno == 7) {
998 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
999 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1000 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1001 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1004 if (tcp->tccps[compno].reversible == 1) {
1005 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1006 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1007 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1009 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1010 /*fprintf(fid," %d",t1->data[k][j][i]);*/
1014 } else if (tcp->tccps[compno].reversible == 0) {
1015 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1016 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1017 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1018 t1->data[k][j][i] = fix_mul(
1019 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1020 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
1026 orient = band->bandno; /* FIXME */
1029 } else if (orient == 1) {
1032 for (i = 0; i < 3; i++)
1033 level[i] = tilec->numresolution[i] - 1 - resno;
1034 /*fprintf(stdout,"t1_encode_cblk(t1, cblk, %d, %d, %d %d %d, %d, %f, %d, %d, tile);\n", orient, compno, level[0], level[1], level[2], tcp->tccps[compno].reversible, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps);*/
1035 t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1039 /*fprintf(fid,"\n");*/
1046 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1047 int compno, resno, bandno, precno, cblkno;
1049 for (compno = 0; compno < tile->numcomps; compno++) {
1050 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1052 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1053 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1055 for (bandno = 0; bandno < res->numbands; bandno++) {
1056 opj_tcd_band_t *band = &res->bands[bandno];
1058 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1059 opj_tcd_precinct_t *prc = &band->precincts[precno];
1061 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1062 int x, y, k, i, j, z, orient;
1063 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1065 orient = band->bandno; /* FIXME */
1068 } else if (orient == 1) {
1072 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1074 if (band->bandno == 0) {
1075 x = cblk->x0 - band->x0;
1076 y = cblk->y0 - band->y0;
1077 z = cblk->z0 - band->z0;
1078 } else if (band->bandno == 1) {
1079 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1080 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1081 y = cblk->y0 - band->y0;
1082 z = cblk->z0 - band->z0;
1083 } else if (band->bandno == 2) {
1084 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1085 x = cblk->x0 - band->x0;
1086 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1087 z = cblk->z0 - band->z0;
1088 } else if (band->bandno == 3) {
1089 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1090 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1091 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1092 z = cblk->z0 - band->z0;
1093 } else if (band->bandno == 4) {
1094 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1095 x = cblk->x0 - band->x0;
1096 y = cblk->y0 - band->y0;
1097 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1098 } else if (band->bandno == 5) {
1099 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1100 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1101 y = cblk->y0 - band->y0;
1102 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1103 } else if (band->bandno == 6) {
1104 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1105 x = cblk->x0 - band->x0;
1106 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1107 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1108 } else if (band->bandno == 7) {
1109 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1110 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1111 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1112 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1115 if (tcp->tccps[compno].roishift) {
1116 int thresh, val, mag;
1117 thresh = 1 << tcp->tccps[compno].roishift;
1118 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1119 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1120 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1121 val = t1->data[k][j][i];
1123 if (mag >= thresh) {
1124 mag >>= tcp->tccps[compno].roishift;
1125 t1->data[k][j][i] = val < 0 ? -mag : mag;
1132 if (tcp->tccps[compno].reversible == 1) {
1133 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1134 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1135 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1136 int tmp = t1->data[k][j][i];
1137 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
1141 } else { /* if (tcp->tccps[compno].reversible == 0) */
1142 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1143 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1144 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1145 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1146 if (t1->data[k][j][i] >> 1 == 0) {
1147 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1149 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1150 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);
1164 /** mod fixed_quality */
1165 double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]) {
1166 double w1, w2, wmsedec;
1168 if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) {
1169 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
1171 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
1173 w2 = dwt_getnorm(orient, level, dwtid);
1175 /*fprintf(stdout,"nmsedec %d level %d %d %d orient %d bpno %d stepsize %f \n",nmsedec ,level[0],level[1],level[2],orient,bpno,stepsize);*/
1176 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1177 wmsedec *= wmsedec * nmsedec / 8192.0;
1181 /** mod fixed_quality */