2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
41 /** @name Local static functions */
44 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
45 static int t1_getctxno_sc(opj_t1_t *t1, int f);
46 static int t1_getctxno_mag(opj_t1_t *t1, int f);
47 static int t1_getspb(opj_t1_t *t1, int f);
48 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
49 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
50 static void t1_updateflags(int *fp, int s);
52 Encode significant pass
54 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
55 int bpno, int one, int *nmsedec, char type, int vsc);
57 Decode significant pass
59 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
60 int oneplushalf, char type, int vsc);
62 Encode significant pass
64 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno,
65 int orient, int *nmsedec, char type, int cblksty);
67 Decode significant pass
69 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno,
70 int orient, char type, int cblksty);
72 Encode refinement pass
74 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno,
75 int one, int *nmsedec, char type, int vsc);
77 Decode refinement pass
79 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf,
80 int neghalf, char type, int vsc);
82 Encode refinement pass
84 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno,
85 int *nmsedec, char type, int cblksty);
87 Decode refinement pass
89 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno,
90 char type, int cblksty);
94 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
95 int bpno, int one, int *nmsedec, int partial, int vsc);
99 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
100 int oneplushalf, int partial, int vsc);
104 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno,
105 int orient, int *nmsedec, int cblksty);
109 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno,
110 int orient, int cblksty);
114 @param cblk Code-block coding parameters
116 @param compno Component number
120 @param cblksty Code-block style
124 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient,
125 int compno, int level[3], int dwtid[3], double stepsize, int cblksty,
126 int numcomps, opj_tcd_tile_t * tile);
130 @param cblk Code-block coding parameters
132 @param roishift Region of interest shifting value
133 @param cblksty Code-block style
135 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient,
136 int roishift, int cblksty);
138 static int t1_init_ctxno_zc(int f, int orient);
139 static int t1_init_ctxno_sc(int f);
140 static int t1_init_ctxno_mag(int f);
141 static int t1_init_spb(int f);
143 Initialize the look-up tables of the Tier-1 coder/decoder
146 static void t1_init_luts(opj_t1_t *t1);
152 /* ----------------------------------------------------------------------- */
154 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient)
156 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
159 static int t1_getctxno_sc(opj_t1_t *t1, int f)
161 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
164 static int t1_getctxno_mag(opj_t1_t *t1, int f)
166 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
169 static int t1_getspb(opj_t1_t *t1, int f)
171 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
174 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos)
176 if (bitpos > T1_NMSEDEC_FRACBITS) {
177 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((
178 1 << T1_NMSEDEC_BITS) - 1)];
181 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
184 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos)
186 if (bitpos > T1_NMSEDEC_FRACBITS) {
187 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((
188 1 << T1_NMSEDEC_BITS) - 1)];
191 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
194 static void t1_updateflags(int *fp, int s)
196 int *np = fp - (T1_MAXCBLKW + 2);
197 int *sp = fp + (T1_MAXCBLKW + 2);
214 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
215 int bpno, int one, int *nmsedec, char type, int vsc)
219 opj_mqc_t *mqc = t1->mqc; /* MQC component */
221 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
222 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
223 v = int_abs(*dp) & one ? 1 : 0;
224 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
225 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
226 mqc_bypass_enc(mqc, v);
228 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
233 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
234 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
235 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
236 mqc_bypass_enc(mqc, v);
238 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
239 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
241 t1_updateflags(fp, v);
248 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
249 int oneplushalf, char type, int vsc)
253 opj_raw_t *raw = t1->raw; /* RAW component */
254 opj_mqc_t *mqc = t1->mqc; /* MQC component */
256 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
257 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
258 if (type == T1_TYPE_RAW) {
259 if (raw_decode(raw)) {
260 v = raw_decode(raw); /* ESSAI */
261 *dp = v ? -oneplushalf : oneplushalf;
262 t1_updateflags(fp, v);
266 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
267 if (mqc_decode(mqc)) {
268 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
269 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
270 *dp = v ? -oneplushalf : oneplushalf;
271 t1_updateflags(fp, v);
277 } /* VSC and BYPASS by Antonin */
279 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno,
280 int orient, int *nmsedec, char type, int cblksty)
282 int i, j, k, m, one, vsc;
284 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
285 for (m = 0; m < l; m++) {
286 for (k = 0; k < h; k += 4) {
287 for (i = 0; i < w; i++) {
288 for (j = k; j < k + 4 && j < h; j++) {
289 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
290 t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i],
291 orient, bpno, one, nmsedec, type, vsc);
298 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno,
299 int orient, char type, int cblksty)
301 int i, j, k, m, one, half, oneplushalf, vsc;
304 oneplushalf = one | half;
305 for (m = 0; m < l; m++) {
306 for (k = 0; k < h; k += 4) {
307 for (i = 0; i < w; i++) {
308 for (j = k; j < k + 4 && j < h; j++) {
309 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
310 t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i],
311 orient, oneplushalf, type, vsc);
316 } /* VSC and BYPASS by Antonin */
318 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno,
319 int one, int *nmsedec, char type, int vsc)
323 opj_mqc_t *mqc = t1->mqc; /* MQC component */
325 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
326 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
327 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
328 v = int_abs(*dp) & one ? 1 : 0;
329 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
330 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
331 mqc_bypass_enc(mqc, v);
333 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
340 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf,
341 int neghalf, char type, int vsc)
345 opj_mqc_t *mqc = t1->mqc; /* MQC component */
346 opj_raw_t *raw = t1->raw; /* RAW component */
348 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
349 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
350 if (type == T1_TYPE_RAW) {
351 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
354 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
357 t = v ? poshalf : neghalf;
358 *dp += *dp < 0 ? -t : t;
361 } /* VSC and BYPASS by Antonin */
363 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno,
364 int *nmsedec, char type, int cblksty)
366 int i, j, k, m, one, vsc;
368 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
369 for (m = 0; m < l; m++) {
370 for (k = 0; k < h; k += 4) {
371 for (i = 0; i < w; i++) {
372 for (j = k; j < k + 4 && j < h; j++) {
373 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
374 t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i],
375 bpno, one, nmsedec, type, vsc);
382 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno,
383 char type, int cblksty)
385 int i, j, k, m, one, poshalf, neghalf;
389 neghalf = bpno > 0 ? -poshalf : -1;
390 for (m = 0; m < l; m++) {
391 for (k = 0; k < h; k += 4) {
392 for (i = 0; i < w; i++) {
393 for (j = k; j < k + 4 && j < h; j++) {
394 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
395 t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i],
396 poshalf, neghalf, type, vsc);
401 } /* VSC and BYPASS by Antonin */
403 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
404 int bpno, int one, int *nmsedec, int partial, int vsc)
408 opj_mqc_t *mqc = t1->mqc; /* MQC component */
410 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
414 if (!(*fp & (T1_SIG | T1_VISIT))) {
415 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
416 v = int_abs(*dp) & one ? 1 : 0;
420 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
421 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
423 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
424 t1_updateflags(fp, v);
431 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient,
432 int oneplushalf, int partial, int vsc)
436 opj_mqc_t *mqc = t1->mqc; /* MQC component */
438 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
442 if (!(flag & (T1_SIG | T1_VISIT))) {
443 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
444 if (mqc_decode(mqc)) {
446 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
447 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
448 *dp = v ? -oneplushalf : oneplushalf;
449 t1_updateflags(fp, v);
454 } /* VSC and BYPASS by Antonin */
456 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno,
457 int orient, int *nmsedec, int cblksty)
459 int i, j, k, m, one, agg, runlen, vsc;
461 opj_mqc_t *mqc = t1->mqc; /* MQC component */
464 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
465 for (m = 0; m < l; m++) {
466 for (k = 0; k < h; k += 4) {
467 for (i = 0; i < w; i++) {
469 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
470 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
471 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
472 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
473 || (t1->flags[1 + m][1 + k + 3][1 + i]
474 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT |
477 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
478 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
479 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
480 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
486 for (runlen = 0; runlen < 4; runlen++) {
487 if (int_abs(t1->data[m][k + runlen][i]) & one) {
491 mqc_setcurctx(mqc, T1_CTXNO_AGG);
492 mqc_encode(mqc, runlen != 4);
496 mqc_setcurctx(mqc, T1_CTXNO_UNI);
497 mqc_encode(mqc, runlen >> 1);
498 mqc_encode(mqc, runlen & 1);
502 for (j = k + runlen; j < k + 4 && j < h; j++) {
503 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
504 t1_enc_clnpass_step(t1, &(t1->flags[1 + m][1 + j][1 + i]), &(t1->data[m][j][i]),
505 orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
512 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno,
513 int orient, int cblksty)
515 int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
516 int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
518 opj_mqc_t *mqc = t1->mqc; /* MQC component */
522 oneplushalf = one | half;
523 for (m = 0; m < l; m++) {
524 for (k = 0; k < h; k += 4) {
525 for (i = 0; i < w; i++) {
527 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
528 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
529 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
530 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
531 || (t1->flags[1 + m][1 + k + 3][1 + i]
532 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT |
535 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
536 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
537 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
538 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
544 mqc_setcurctx(mqc, T1_CTXNO_AGG);
545 if (!mqc_decode(mqc)) {
548 mqc_setcurctx(mqc, T1_CTXNO_UNI);
549 runlen = mqc_decode(mqc);
550 runlen = (runlen << 1) | mqc_decode(mqc);
554 for (j = k + runlen; j < k + 4 && j < h; j++) {
555 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
556 t1_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i],
557 orient, oneplushalf, agg && (j == k + runlen), vsc);
564 mqc_setcurctx(mqc, T1_CTXNO_UNI);
566 v = (v << 1) | mqc_decode(mqc);
567 v = (v << 1) | mqc_decode(mqc);
568 v = (v << 1) | mqc_decode(mqc);
571 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
575 } /* VSC and BYPASS by Antonin */
578 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient,
579 int compno, int level[3], int dwtid[3], double stepsize, int cblksty,
580 int numcomps, opj_tcd_tile_t * tile)
588 double cumwmsedec = 0;
589 char type = T1_TYPE_MQ;
591 opj_mqc_t *mqc = t1->mqc; /* MQC component */
593 w = cblk->x1 - cblk->x0;
594 h = cblk->y1 - cblk->y0;
595 l = cblk->z1 - cblk->z0;
598 for (k = 0; k < l; k++) {
599 for (j = 0; j < h; j++) {
600 for (i = 0; i < w; i++) {
601 max = int_max(max, int_abs(t1->data[k][j][i]));
605 for (k = 0; k <= l; k++) {
606 for (j = 0; j <= h; j++) {
607 for (i = 0; i <= w; i++) {
608 t1->flags[k][j][i] = 0;
613 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
615 bpno = cblk->numbps - 1;
619 mqc_init_enc(mqc, cblk->data);
621 for (passno = 0; bpno >= 0; passno++) {
622 opj_tcd_pass_t *pass = &cblk->passes[passno];
625 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) &&
626 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
627 /*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);*/
631 t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
634 t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
637 /*fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient);*/
638 t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
639 /* code switch SEGMARK (i.e. SEGSYM) */
640 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM) {
641 mqc_segmark_enc(mqc);
647 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize,
649 cumwmsedec += tmpwmsedec;
650 tile->distotile += tmpwmsedec;
652 /* Code switch "RESTART" (i.e. TERMALL) */
653 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
655 if (type == T1_TYPE_RAW) {
658 /* correction = mqc_bypass_flush_enc(); */
659 } else { /* correction = mqc_restart_enc(); */
665 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
666 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) &&
667 (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
668 if (type == T1_TYPE_RAW) {
671 /* correction = mqc_bypass_flush_enc(); */
672 } else { /* correction = mqc_restart_enc(); */
682 if (++passtype == 3) {
687 if (pass->term && bpno > 0) {
688 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) &&
689 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
690 if (type == T1_TYPE_RAW) {
691 mqc_bypass_init_enc(mqc);
693 mqc_restart_init_enc(mqc);
697 pass->distortiondec = cumwmsedec;
698 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
699 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
701 /* Code-switch "RESET" */
702 if (cblksty & J3D_CCP_CBLKSTY_RESET) {
707 /* Code switch "ERTERM" (i.e. PTERM) */
708 if (cblksty & J3D_CCP_CBLKSTY_PTERM) {
710 } else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY)) {
714 cblk->totalpasses = passno;
717 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient,
718 int roishift, int cblksty)
720 int i, j, k, w, h, l;
723 char type = T1_TYPE_MQ; /* BYPASS mode */
725 opj_raw_t *raw = t1->raw; /* RAW component */
726 opj_mqc_t *mqc = t1->mqc; /* MQC component */
728 w = cblk->x1 - cblk->x0;
729 h = cblk->y1 - cblk->y0;
730 l = cblk->z1 - cblk->z0;
732 for (k = 0; k < l; k++) {
733 for (j = 0; j < h; j++) {
734 for (i = 0; i < w; i++) {
735 t1->data[k][j][i] = 0;
740 for (k = 0; k <= l; k++) {
741 for (j = 0; j <= h; j++) {
742 for (i = 0; i <= w; i++) {
743 t1->flags[k][j][i] = 0;
748 bpno = roishift + cblk->numbps - 1;
753 for (segno = 0; segno < cblk->numsegs; segno++) {
754 opj_tcd_seg_t *seg = &cblk->segs[segno];
757 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) &&
758 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
759 if (type == T1_TYPE_RAW) {
760 raw_init_dec(raw, seg->data, seg->len);
762 mqc_init_dec(mqc, seg->data, seg->len);
765 for (passno = 0; passno < seg->numpasses; passno++) {
768 t1_dec_sigpass(t1, w, h, l, bpno + 1, orient, type, cblksty);
771 t1_dec_refpass(t1, w, h, l, bpno + 1, type, cblksty);
774 t1_dec_clnpass(t1, w, h, l, bpno + 1, orient, cblksty);
778 if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
781 if (++passtype == 3) {
789 static int t1_init_ctxno_zc(int f, int orient)
791 int h, v, d, n, t, hv;
793 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
794 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
795 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((
796 f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
838 } else if (hv == 1) {
846 } else if (hv == 1) {
863 return (T1_CTXNO_ZC + n);
866 static int t1_init_ctxno_sc(int f)
871 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
872 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
873 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
874 (T1_SIG_E | T1_SGN_E)) +
875 ((f & (T1_SIG_W | T1_SGN_W)) ==
876 (T1_SIG_W | T1_SGN_W)), 1);
878 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
879 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
880 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
881 (T1_SIG_N | T1_SGN_N)) +
882 ((f & (T1_SIG_S | T1_SGN_S)) ==
883 (T1_SIG_S | T1_SGN_S)), 1);
897 } else if (hc == 1) {
907 return (T1_CTXNO_SC + n);
910 static int t1_init_ctxno_mag(int f)
913 if (!(f & T1_REFINE)) {
914 n = (f & (T1_SIG_OTH)) ? 1 : 0;
919 return (T1_CTXNO_MAG + n);
922 static int t1_init_spb(int f)
926 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
927 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
928 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
929 (T1_SIG_E | T1_SGN_E)) +
930 ((f & (T1_SIG_W | T1_SGN_W)) ==
931 (T1_SIG_W | T1_SGN_W)), 1);
933 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
934 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
935 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
936 (T1_SIG_N | T1_SGN_N)) +
937 ((f & (T1_SIG_S | T1_SGN_S)) ==
938 (T1_SIG_S | T1_SGN_S)), 1);
943 n = (!(hc > 0 || (!hc && vc > 0)));
949 static void t1_init_luts(opj_t1_t *t1)
953 for (j = 0; j < 4; j++) {
954 for (i = 0; i < 256; ++i) {
955 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
958 for (i = 0; i < 256; i++) {
959 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
961 for (j = 0; j < 2; j++) {
962 for (i = 0; i < 2048; ++i) {
963 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
966 for (i = 0; i < 256; ++i) {
967 t1->lut_spb[i] = t1_init_spb(i << 4);
969 /* FIXME FIXME FIXME */
970 /* fprintf(stdout,"nmsedec luts:\n"); */
971 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
972 t = i / pow(2, T1_NMSEDEC_FRACBITS);
975 t1->lut_nmsedec_sig[i] =
977 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
978 T1_NMSEDEC_FRACBITS) * 8192.0));
979 t1->lut_nmsedec_sig0[i] =
981 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
982 T1_NMSEDEC_FRACBITS) * 8192.0));
984 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
989 t1->lut_nmsedec_ref[i] =
991 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
992 T1_NMSEDEC_FRACBITS) * 8192.0));
993 t1->lut_nmsedec_ref0[i] =
995 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
996 T1_NMSEDEC_FRACBITS) * 8192.0));
1000 /* ----------------------------------------------------------------------- */
1002 opj_t1_t* t1_create(opj_common_ptr cinfo)
1004 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
1007 /* create MQC and RAW handles */
1008 t1->mqc = mqc_create();
1009 t1->raw = raw_create();
1010 /* initialize the look-up tables of the Tier-1 coder/decoder */
1016 void t1_destroy(opj_t1_t *t1)
1019 /* destroy MQC and RAW handles */
1020 mqc_destroy(t1->mqc);
1021 raw_destroy(t1->raw);
1022 /*opj_free(t1->data);*/
1023 /*opj_free(t1->flags);*/
1028 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp)
1030 int compno, resno, bandno, precno, cblkno;
1031 int x, y, z, i, j, k, orient;
1035 /* char filename[10];*/
1036 tile->distotile = 0; /* fixed_quality */
1038 for (compno = 0; compno < tile->numcomps; compno++) {
1039 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1041 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1042 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1044 /* Weighted first order entropy
1045 sprintf(filename,"res%d.txt",resno);
1046 if ((fid = fopen(filename,"w")) == 0){
1047 fprintf(stdout,"Error while opening %s\n", filename);
1051 for (bandno = 0; bandno < res->numbands; bandno++) {
1052 opj_tcd_band_t *band = &res->bands[bandno];
1053 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2];
1055 opj_tcd_precinct_t *prc = &band->precincts[precno];
1057 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
1059 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1061 /*fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno);*/
1062 if (band->bandno == 0) {
1063 x = cblk->x0 - band->x0;
1064 y = cblk->y0 - band->y0;
1065 z = cblk->z0 - band->z0;
1066 } else if (band->bandno == 1) {
1067 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1068 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1069 y = cblk->y0 - band->y0;
1070 z = cblk->z0 - band->z0;
1071 } else if (band->bandno == 2) {
1072 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1073 x = cblk->x0 - band->x0;
1074 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1075 z = cblk->z0 - band->z0;
1076 } else if (band->bandno == 3) {
1077 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1078 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1079 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1080 z = cblk->z0 - band->z0;
1081 } else if (band->bandno == 4) {
1082 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1083 x = cblk->x0 - band->x0;
1084 y = cblk->y0 - band->y0;
1085 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1086 } else if (band->bandno == 5) {
1087 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1088 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1089 y = cblk->y0 - band->y0;
1090 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1091 } else if (band->bandno == 6) {
1092 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1093 x = cblk->x0 - band->x0;
1094 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1095 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1096 } else if (band->bandno == 7) {
1097 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1098 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1099 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1100 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1103 if (tcp->tccps[compno].reversible == 1) {
1104 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1105 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1106 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1108 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1109 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1110 /*fprintf(fid," %d",t1->data[k][j][i]);*/
1114 } else if (tcp->tccps[compno].reversible == 0) {
1115 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1116 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1117 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1118 t1->data[k][j][i] = fix_mul(
1119 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1120 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1121 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 -
1122 T1_NMSEDEC_FRACBITS);
1128 orient = band->bandno; /* FIXME */
1131 } else if (orient == 1) {
1134 for (i = 0; i < 3; i++) {
1135 level[i] = tilec->numresolution[i] - 1 - resno;
1137 /*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);*/
1138 t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid,
1139 band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1143 /*fprintf(fid,"\n");*/
1150 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp)
1152 int compno, resno, bandno, precno, cblkno;
1154 for (compno = 0; compno < tile->numcomps; compno++) {
1155 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1157 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1158 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1160 for (bandno = 0; bandno < res->numbands; bandno++) {
1161 opj_tcd_band_t *band = &res->bands[bandno];
1163 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2];
1165 opj_tcd_precinct_t *prc = &band->precincts[precno];
1167 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
1169 int x, y, k, i, j, z, orient;
1170 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1172 orient = band->bandno; /* FIXME */
1175 } else if (orient == 1) {
1179 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift,
1180 tcp->tccps[compno].cblksty);
1182 if (band->bandno == 0) {
1183 x = cblk->x0 - band->x0;
1184 y = cblk->y0 - band->y0;
1185 z = cblk->z0 - band->z0;
1186 } else if (band->bandno == 1) {
1187 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1188 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1189 y = cblk->y0 - band->y0;
1190 z = cblk->z0 - band->z0;
1191 } else if (band->bandno == 2) {
1192 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1193 x = cblk->x0 - band->x0;
1194 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1195 z = cblk->z0 - band->z0;
1196 } else if (band->bandno == 3) {
1197 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1198 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1199 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1200 z = cblk->z0 - band->z0;
1201 } else if (band->bandno == 4) {
1202 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1203 x = cblk->x0 - band->x0;
1204 y = cblk->y0 - band->y0;
1205 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1206 } else if (band->bandno == 5) {
1207 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1208 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1209 y = cblk->y0 - band->y0;
1210 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1211 } else if (band->bandno == 6) {
1212 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1213 x = cblk->x0 - band->x0;
1214 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1215 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1216 } else if (band->bandno == 7) {
1217 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1218 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1219 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1220 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1223 if (tcp->tccps[compno].roishift) {
1224 int thresh, val, mag;
1225 thresh = 1 << tcp->tccps[compno].roishift;
1226 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1227 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1228 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1229 val = t1->data[k][j][i];
1231 if (mag >= thresh) {
1232 mag >>= tcp->tccps[compno].roishift;
1233 t1->data[k][j][i] = val < 0 ? -mag : mag;
1240 if (tcp->tccps[compno].reversible == 1) {
1241 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1242 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1243 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1244 int tmp = t1->data[k][j][i];
1245 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1246 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp / 2;
1250 } else { /* if (tcp->tccps[compno].reversible == 0) */
1251 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1252 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1253 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1254 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1255 if (t1->data[k][j][i] >> 1 == 0) {
1256 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1257 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1259 int tmp2 = ((int)(floor(fabs(tmp)))) + ((int) floor(fabs(tmp * 2)) % 2);
1260 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1261 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp < 0) ? -tmp2 : tmp2);
1275 /** mod fixed_quality */
1276 double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient,
1277 int bpno, double stepsize, int numcomps, int dwtid[3])
1279 double w1, w2, wmsedec;
1281 if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) {
1282 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
1284 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
1286 w2 = dwt_getnorm(orient, level, dwtid);
1288 /*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);*/
1289 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1290 wmsedec *= wmsedec * nmsedec / 8192.0;
1294 /** mod fixed_quality */