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, int bpno, int one, int *nmsedec, char type, int vsc);
56 Decode significant pass
58 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
60 Encode significant pass
62 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);
64 Decode significant pass
66 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
68 Encode refinement pass
70 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
72 Decode refinement pass
74 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
76 Encode refinement pass
78 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
80 Decode refinement pass
82 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
86 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);
90 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
94 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
98 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
102 @param cblk Code-block coding parameters
104 @param compno Component number
108 @param cblksty Code-block style
112 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);
116 @param cblk Code-block coding parameters
118 @param roishift Region of interest shifting value
119 @param cblksty Code-block style
121 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
123 static int t1_init_ctxno_zc(int f, int orient);
124 static int t1_init_ctxno_sc(int f);
125 static int t1_init_ctxno_mag(int f);
126 static int t1_init_spb(int f);
128 Initialize the look-up tables of the Tier-1 coder/decoder
131 static void t1_init_luts(opj_t1_t *t1);
137 /* ----------------------------------------------------------------------- */
139 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
140 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
143 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
144 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
147 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
148 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
151 static int t1_getspb(opj_t1_t *t1, int f) {
152 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
155 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
156 if (bitpos > T1_NMSEDEC_FRACBITS) {
157 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
160 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
163 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
164 if (bitpos > T1_NMSEDEC_FRACBITS) {
165 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
168 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
171 static void t1_updateflags(int *fp, int s) {
172 int *np = fp - (T1_MAXCBLKW + 2);
173 int *sp = fp + (T1_MAXCBLKW + 2);
190 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) {
193 opj_mqc_t *mqc = t1->mqc; /* MQC component */
195 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
196 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
197 v = int_abs(*dp) & one ? 1 : 0;
198 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
199 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
200 mqc_bypass_enc(mqc, v);
202 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
207 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
208 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
209 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
210 mqc_bypass_enc(mqc, v);
212 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
213 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
215 t1_updateflags(fp, v);
222 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
225 opj_raw_t *raw = t1->raw; /* RAW component */
226 opj_mqc_t *mqc = t1->mqc; /* MQC component */
228 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
229 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
230 if (type == T1_TYPE_RAW) {
231 if (raw_decode(raw)) {
232 v = raw_decode(raw); /* ESSAI */
233 *dp = v ? -oneplushalf : oneplushalf;
234 t1_updateflags(fp, v);
238 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
239 if (mqc_decode(mqc)) {
240 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
241 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
242 *dp = v ? -oneplushalf : oneplushalf;
243 t1_updateflags(fp, v);
249 } /* VSC and BYPASS by Antonin */
251 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) {
252 int i, j, k, m, one, vsc;
254 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
255 for (m = 0; m < l; m++) {
256 for (k = 0; k < h; k += 4) {
257 for (i = 0; i < w; i++) {
258 for (j = k; j < k + 4 && j < h; j++) {
259 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
260 t1_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
267 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
268 int i, j, k, m, one, half, oneplushalf, vsc;
271 oneplushalf = one | half;
272 for (m = 0; m < l; m++) {
273 for (k = 0; k < h; k += 4) {
274 for (i = 0; i < w; i++) {
275 for (j = k; j < k + 4 && j < h; j++) {
276 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
277 t1_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
282 } /* VSC and BYPASS by Antonin */
284 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
287 opj_mqc_t *mqc = t1->mqc; /* MQC component */
289 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
290 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
291 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
292 v = int_abs(*dp) & one ? 1 : 0;
293 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
294 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
295 mqc_bypass_enc(mqc, v);
297 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
304 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
307 opj_mqc_t *mqc = t1->mqc; /* MQC component */
308 opj_raw_t *raw = t1->raw; /* RAW component */
310 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
311 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
312 if (type == T1_TYPE_RAW) {
313 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
316 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
319 t = v ? poshalf : neghalf;
320 *dp += *dp < 0 ? -t : t;
323 } /* VSC and BYPASS by Antonin */
325 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
326 int i, j, k, m, one, vsc;
328 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
329 for (m = 0; m < l; m++) {
330 for (k = 0; k < h; k += 4) {
331 for (i = 0; i < w; i++) {
332 for (j = k; j < k + 4 && j < h; j++) {
333 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
334 t1_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
341 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
342 int i, j, k, m, one, poshalf, neghalf;
346 neghalf = bpno > 0 ? -poshalf : -1;
347 for (m = 0; m < l; m++) {
348 for (k = 0; k < h; k += 4) {
349 for (i = 0; i < w; i++) {
350 for (j = k; j < k + 4 && j < h; j++) {
351 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
352 t1_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
357 } /* VSC and BYPASS by Antonin */
359 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) {
362 opj_mqc_t *mqc = t1->mqc; /* MQC component */
364 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
368 if (!(*fp & (T1_SIG | T1_VISIT))) {
369 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
370 v = int_abs(*dp) & one ? 1 : 0;
374 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
375 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
377 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
378 t1_updateflags(fp, v);
385 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
388 opj_mqc_t *mqc = t1->mqc; /* MQC component */
390 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
394 if (!(flag & (T1_SIG | T1_VISIT))) {
395 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
396 if (mqc_decode(mqc)) {
398 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
399 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
400 *dp = v ? -oneplushalf : oneplushalf;
401 t1_updateflags(fp, v);
406 } /* VSC and BYPASS by Antonin */
408 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
409 int i, j, k, m, one, agg, runlen, vsc;
411 opj_mqc_t *mqc = t1->mqc; /* MQC component */
414 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
415 for (m = 0; m < l; m++) {
416 for (k = 0; k < h; k += 4) {
417 for (i = 0; i < w; i++) {
419 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
420 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
421 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
422 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
423 || (t1->flags[1 + m][1 + k + 3][1 + i]
424 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
426 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
427 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
428 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
429 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
435 for (runlen = 0; runlen < 4; runlen++) {
436 if (int_abs(t1->data[m][k + runlen][i]) & one)
439 mqc_setcurctx(mqc, T1_CTXNO_AGG);
440 mqc_encode(mqc, runlen != 4);
444 mqc_setcurctx(mqc, T1_CTXNO_UNI);
445 mqc_encode(mqc, runlen >> 1);
446 mqc_encode(mqc, runlen & 1);
450 for (j = k + runlen; j < k + 4 && j < h; j++) {
451 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
452 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);
459 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
460 int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
461 int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
463 opj_mqc_t *mqc = t1->mqc; /* MQC component */
467 oneplushalf = one | half;
468 for (m = 0; m < l; m++) {
469 for (k = 0; k < h; k += 4) {
470 for (i = 0; i < w; i++) {
472 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
473 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
474 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
475 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
476 || (t1->flags[1 + m][1 + k + 3][1 + i]
477 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
479 agg = !(t1->flags[1 + m][1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
480 || t1->flags[1 + m][1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
481 || t1->flags[1 + m][1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
482 || t1->flags[1 + m][1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
488 mqc_setcurctx(mqc, T1_CTXNO_AGG);
489 if (!mqc_decode(mqc)) {
492 mqc_setcurctx(mqc, T1_CTXNO_UNI);
493 runlen = mqc_decode(mqc);
494 runlen = (runlen << 1) | mqc_decode(mqc);
498 for (j = k + runlen; j < k + 4 && j < h; j++) {
499 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
500 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);
507 mqc_setcurctx(mqc, T1_CTXNO_UNI);
509 v = (v << 1) | mqc_decode(mqc);
510 v = (v << 1) | mqc_decode(mqc);
511 v = (v << 1) | mqc_decode(mqc);
514 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
518 } /* VSC and BYPASS by Antonin */
521 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) {
528 double cumwmsedec = 0;
529 char type = T1_TYPE_MQ;
531 opj_mqc_t *mqc = t1->mqc; /* MQC component */
533 w = cblk->x1 - cblk->x0;
534 h = cblk->y1 - cblk->y0;
535 l = cblk->z1 - cblk->z0;
538 for (k = 0; k < l; k++) {
539 for (j = 0; j < h; j++) {
540 for (i = 0; i < w; i++) {
541 max = int_max(max, int_abs(t1->data[k][j][i]));
545 for (k = 0; k <= l; k++) {
546 for (j = 0; j <= h; j++) {
547 for (i = 0; i <= w; i++) {
548 t1->flags[k][j][i] = 0;
553 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
555 bpno = cblk->numbps - 1;
559 mqc_init_enc(mqc, cblk->data);
561 for (passno = 0; bpno >= 0; passno++) {
562 opj_tcd_pass_t *pass = &cblk->passes[passno];
565 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
566 /*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);*/
570 t1_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
573 t1_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
576 /*fprintf(stdout,"w %d h %d l %d bpno %d orient %d \n",w,h,l,bpno,orient);*/
577 t1_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
578 /* code switch SEGMARK (i.e. SEGSYM) */
579 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
580 mqc_segmark_enc(mqc);
585 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
586 cumwmsedec += tmpwmsedec;
587 tile->distotile += tmpwmsedec;
589 /* Code switch "RESTART" (i.e. TERMALL) */
590 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
591 if (type == T1_TYPE_RAW) {
594 /* correction = mqc_bypass_flush_enc(); */
595 } else { /* correction = mqc_restart_enc(); */
601 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
602 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
603 if (type == T1_TYPE_RAW) {
606 /* correction = mqc_bypass_flush_enc(); */
607 } else { /* correction = mqc_restart_enc(); */
617 if (++passtype == 3) {
622 if (pass->term && bpno > 0) {
623 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
624 if (type == T1_TYPE_RAW)
625 mqc_bypass_init_enc(mqc);
627 mqc_restart_init_enc(mqc);
630 pass->distortiondec = cumwmsedec;
631 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
632 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
634 /* Code-switch "RESET" */
635 if (cblksty & J3D_CCP_CBLKSTY_RESET)
639 /* Code switch "ERTERM" (i.e. PTERM) */
640 if (cblksty & J3D_CCP_CBLKSTY_PTERM)
642 else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
645 cblk->totalpasses = passno;
648 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
649 int i, j, k, w, h, l;
652 char type = T1_TYPE_MQ; /* BYPASS mode */
654 opj_raw_t *raw = t1->raw; /* RAW component */
655 opj_mqc_t *mqc = t1->mqc; /* MQC component */
657 w = cblk->x1 - cblk->x0;
658 h = cblk->y1 - cblk->y0;
659 l = cblk->z1 - cblk->z0;
661 for (k = 0; k < l; k++) {
662 for (j = 0; j < h; j++) {
663 for (i = 0; i < w; i++) {
664 t1->data[k][j][i] = 0;
669 for (k = 0; k <= l; k++) {
670 for (j = 0; j <= h; j++) {
671 for (i = 0; i <= w; i++) {
672 t1->flags[k][j][i] = 0;
677 bpno = roishift + cblk->numbps - 1;
682 for (segno = 0; segno < cblk->numsegs; segno++) {
683 opj_tcd_seg_t *seg = &cblk->segs[segno];
686 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
687 if (type == T1_TYPE_RAW) {
688 raw_init_dec(raw, seg->data, seg->len);
690 mqc_init_dec(mqc, seg->data, seg->len);
693 for (passno = 0; passno < seg->numpasses; passno++) {
696 t1_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
699 t1_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
702 t1_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
706 if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
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);
931 /*opj_free(t1->data);*/
932 /*opj_free(t1->flags);*/
937 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
938 int compno, resno, bandno, precno, cblkno;
939 int x, y, z, i, j, k, orient;
943 /* char filename[10];*/
944 tile->distotile = 0; /* fixed_quality */
946 for (compno = 0; compno < tile->numcomps; compno++) {
947 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
949 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
950 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
952 /* Weighted first order entropy
953 sprintf(filename,"res%d.txt",resno);
954 if ((fid = fopen(filename,"w")) == 0){
955 fprintf(stdout,"Error while opening %s\n", filename);
959 for (bandno = 0; bandno < res->numbands; bandno++) {
960 opj_tcd_band_t *band = &res->bands[bandno];
961 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
962 opj_tcd_precinct_t *prc = &band->precincts[precno];
964 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
965 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
967 /*fprintf(stdout,"Precno %d Cblkno %d \n",precno,cblkno);*/
968 if (band->bandno == 0) {
969 x = cblk->x0 - band->x0;
970 y = cblk->y0 - band->y0;
971 z = cblk->z0 - band->z0;
972 } else if (band->bandno == 1) {
973 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
974 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
975 y = cblk->y0 - band->y0;
976 z = cblk->z0 - band->z0;
977 } else if (band->bandno == 2) {
978 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
979 x = 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 == 3) {
983 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
984 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
985 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
986 z = cblk->z0 - band->z0;
987 } else if (band->bandno == 4) {
988 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
989 x = 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 == 5) {
993 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
994 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
995 y = cblk->y0 - band->y0;
996 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
997 } else if (band->bandno == 6) {
998 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
999 x = cblk->x0 - band->x0;
1000 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1001 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1002 } else if (band->bandno == 7) {
1003 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1004 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1005 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1006 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1009 if (tcp->tccps[compno].reversible == 1) {
1010 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1011 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1012 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1014 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1015 /*fprintf(fid," %d",t1->data[k][j][i]);*/
1019 } else if (tcp->tccps[compno].reversible == 0) {
1020 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1021 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1022 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1023 t1->data[k][j][i] = fix_mul(
1024 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1025 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
1031 orient = band->bandno; /* FIXME */
1034 } else if (orient == 1) {
1037 for (i = 0; i < 3; i++)
1038 level[i] = tilec->numresolution[i] - 1 - resno;
1039 /*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);*/
1040 t1_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1044 /*fprintf(fid,"\n");*/
1051 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1052 int compno, resno, bandno, precno, cblkno;
1054 for (compno = 0; compno < tile->numcomps; compno++) {
1055 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1057 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1058 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1060 for (bandno = 0; bandno < res->numbands; bandno++) {
1061 opj_tcd_band_t *band = &res->bands[bandno];
1063 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1064 opj_tcd_precinct_t *prc = &band->precincts[precno];
1066 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1067 int x, y, k, i, j, z, orient;
1068 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1070 orient = band->bandno; /* FIXME */
1073 } else if (orient == 1) {
1077 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1079 if (band->bandno == 0) {
1080 x = cblk->x0 - band->x0;
1081 y = cblk->y0 - band->y0;
1082 z = cblk->z0 - band->z0;
1083 } else if (band->bandno == 1) {
1084 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1085 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1086 y = cblk->y0 - band->y0;
1087 z = cblk->z0 - band->z0;
1088 } else if (band->bandno == 2) {
1089 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1090 x = 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 == 3) {
1094 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1095 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1096 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1097 z = cblk->z0 - band->z0;
1098 } else if (band->bandno == 4) {
1099 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1100 x = 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 == 5) {
1104 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1105 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1106 y = cblk->y0 - band->y0;
1107 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1108 } else if (band->bandno == 6) {
1109 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1110 x = cblk->x0 - band->x0;
1111 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1112 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1113 } else if (band->bandno == 7) {
1114 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1115 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1116 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1117 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1120 if (tcp->tccps[compno].roishift) {
1121 int thresh, val, mag;
1122 thresh = 1 << tcp->tccps[compno].roishift;
1123 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1124 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1125 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1126 val = t1->data[k][j][i];
1128 if (mag >= thresh) {
1129 mag >>= tcp->tccps[compno].roishift;
1130 t1->data[k][j][i] = val < 0 ? -mag : mag;
1137 if (tcp->tccps[compno].reversible == 1) {
1138 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1139 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1140 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1141 int tmp = t1->data[k][j][i];
1142 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
1146 } else { /* if (tcp->tccps[compno].reversible == 0) */
1147 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1148 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1149 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1150 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1151 if (t1->data[k][j][i] >> 1 == 0) {
1152 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1154 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1155 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);
1169 /** mod fixed_quality */
1170 double t1_getwmsedec(int nmsedec, int compno, int level[3], int orient, int bpno, double stepsize, int numcomps, int dwtid[3]) {
1171 double w1, w2, wmsedec;
1173 if (dwtid[0] == 1 || dwtid[1] == 1 || dwtid[2] == 1) {
1174 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
1176 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
1178 w2 = dwt_getnorm(orient, level, dwtid);
1180 /*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);*/
1181 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1182 wmsedec *= wmsedec * nmsedec / 8192.0;
1186 /** mod fixed_quality */