2 * Copyright (c) 2001-2003, David Janssens
\r
3 * Copyright (c) 2002-2003, Yannick Verschueren
\r
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
\r
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
\r
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
\r
7 * All rights reserved.
\r
9 * Redistribution and use in source and binary forms, with or without
\r
10 * modification, are permitted provided that the following conditions
\r
12 * 1. Redistributions of source code must retain the above copyright
\r
13 * notice, this list of conditions and the following disclaimer.
\r
14 * 2. Redistributions in binary form must reproduce the above copyright
\r
15 * notice, this list of conditions and the following disclaimer in the
\r
16 * documentation and/or other materials provided with the distribution.
\r
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
\r
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
28 * POSSIBILITY OF SUCH DAMAGE.
\r
31 #include "opj_includes.h"
\r
33 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
\r
36 /** @name Local static functions */
\r
39 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
\r
40 static int t1_getctxno_sc(opj_t1_t *t1, int f);
\r
41 static int t1_getctxno_mag(opj_t1_t *t1, int f);
\r
42 static int t1_getspb(opj_t1_t *t1, int f);
\r
43 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
\r
44 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
\r
45 static void t1_updateflags(int *fp, int s);
\r
47 Encode significant pass
\r
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);
\r
51 Decode significant pass
\r
53 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
\r
55 Encode significant pass
\r
57 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
\r
59 Decode significant pass
\r
61 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
\r
63 Encode refinement pass
\r
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);
\r
67 Decode refinement pass
\r
69 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
\r
71 Encode refinement pass
\r
73 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
\r
75 Decode refinement pass
\r
77 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
\r
79 Encode clean-up pass
\r
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);
\r
83 Decode clean-up pass
\r
85 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
\r
87 Encode clean-up pass
\r
89 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
\r
91 Decode clean-up pass
\r
93 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
\r
94 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
\r
98 @param cblk Code-block coding parameters
\r
100 @param compno Component number
\r
104 @param cblksty Code-block style
\r
108 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
\r
110 Decode 1 code-block
\r
111 @param t1 T1 handle
\r
112 @param cblk Code-block coding parameters
\r
114 @param roishift Region of interest shifting value
\r
115 @param cblksty Code-block style
\r
117 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
\r
118 static int t1_init_ctxno_zc(int f, int orient);
\r
119 static int t1_init_ctxno_sc(int f);
\r
120 static int t1_init_ctxno_mag(int f);
\r
121 static int t1_init_spb(int f);
\r
123 Initialize the look-up tables of the Tier-1 coder/decoder
\r
124 @param t1 T1 handle
\r
126 static void t1_init_luts(opj_t1_t *t1);
\r
132 /* ----------------------------------------------------------------------- */
\r
134 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
\r
135 return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
\r
138 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
\r
139 return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
\r
142 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
\r
143 return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
\r
146 static int t1_getspb(opj_t1_t *t1, int f) {
\r
147 return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
\r
150 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
\r
151 if (bitpos > T1_NMSEDEC_FRACBITS) {
\r
152 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
\r
155 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
\r
158 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
\r
159 if (bitpos > T1_NMSEDEC_FRACBITS) {
\r
160 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
\r
163 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
\r
166 static void t1_updateflags(int *fp, int s) {
\r
167 int *np = fp - (T1_MAXCBLKW + 2);
\r
168 int *sp = fp + (T1_MAXCBLKW + 2);
\r
169 np[-1] |= T1_SIG_SE;
\r
170 np[1] |= T1_SIG_SW;
\r
171 sp[-1] |= T1_SIG_NE;
\r
172 sp[1] |= T1_SIG_NW;
\r
175 fp[-1] |= T1_SIG_E;
\r
180 fp[-1] |= T1_SGN_E;
\r
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) {
\r
188 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
190 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
191 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
\r
192 v = int_abs(*dp) & one ? 1 : 0;
\r
193 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
\r
194 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient)); /* ESSAI */
\r
195 mqc_bypass_enc(mqc, v);
\r
197 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
\r
198 mqc_encode(mqc, v);
\r
201 v = *dp < 0 ? 1 : 0;
\r
202 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
\r
203 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
\r
204 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag)); /* ESSAI */
\r
205 mqc_bypass_enc(mqc, v);
\r
207 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
\r
208 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
\r
210 t1_updateflags(fp, v);
\r
217 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
\r
220 opj_raw_t *raw = t1->raw; /* RAW component */
\r
221 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
223 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
224 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
\r
225 if (type == T1_TYPE_RAW) {
\r
226 if (raw_decode(raw)) {
\r
227 v = raw_decode(raw); /* ESSAI */
\r
228 *dp = v ? -oneplushalf : oneplushalf;
\r
229 t1_updateflags(fp, v);
\r
233 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
\r
234 if (mqc_decode(mqc)) {
\r
235 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
\r
236 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
\r
237 *dp = v ? -oneplushalf : oneplushalf;
\r
238 t1_updateflags(fp, v);
\r
244 } /* VSC and BYPASS by Antonin */
\r
246 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
\r
247 int i, j, k, one, vsc;
\r
249 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
\r
250 for (k = 0; k < h; k += 4) {
\r
251 for (i = 0; i < w; i++) {
\r
252 for (j = k; j < k + 4 && j < h; j++) {
\r
253 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
254 t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
\r
260 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
\r
261 int i, j, k, one, half, oneplushalf, vsc;
\r
264 oneplushalf = one | half;
\r
265 for (k = 0; k < h; k += 4) {
\r
266 for (i = 0; i < w; i++) {
\r
267 for (j = k; j < k + 4 && j < h; j++) {
\r
268 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
269 t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
\r
273 } /* VSC and BYPASS by Antonin */
\r
275 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
\r
278 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
280 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
281 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
\r
282 *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
\r
283 v = int_abs(*dp) & one ? 1 : 0;
\r
284 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
\r
285 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
\r
286 mqc_bypass_enc(mqc, v);
\r
288 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
\r
289 mqc_encode(mqc, v);
\r
295 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
\r
298 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
299 opj_raw_t *raw = t1->raw; /* RAW component */
\r
301 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
302 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
\r
303 if (type == T1_TYPE_RAW) {
\r
304 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag)); /* ESSAI */
\r
305 v = raw_decode(raw);
\r
307 mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
\r
308 v = mqc_decode(mqc);
\r
310 t = v ? poshalf : neghalf;
\r
311 *dp += *dp < 0 ? -t : t;
\r
314 } /* VSC and BYPASS by Antonin */
\r
316 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
\r
317 int i, j, k, one, vsc;
\r
319 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
\r
320 for (k = 0; k < h; k += 4) {
\r
321 for (i = 0; i < w; i++) {
\r
322 for (j = k; j < k + 4 && j < h; j++) {
\r
323 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
324 t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
\r
330 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
\r
331 int i, j, k, one, poshalf, neghalf;
\r
334 poshalf = one >> 1;
\r
335 neghalf = bpno > 0 ? -poshalf : -1;
\r
336 for (k = 0; k < h; k += 4) {
\r
337 for (i = 0; i < w; i++) {
\r
338 for (j = k; j < k + 4 && j < h; j++) {
\r
339 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
340 t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
\r
344 } /* VSC and BYPASS by Antonin */
\r
346 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) {
\r
349 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
351 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
353 goto LABEL_PARTIAL;
\r
355 if (!(*fp & (T1_SIG | T1_VISIT))) {
\r
356 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
\r
357 v = int_abs(*dp) & one ? 1 : 0;
\r
358 mqc_encode(mqc, v);
\r
361 *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
\r
362 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
\r
363 v = *dp < 0 ? 1 : 0;
\r
364 mqc_encode(mqc, v ^ t1_getspb(t1, flag));
\r
365 t1_updateflags(fp, v);
\r
372 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
\r
375 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
377 flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
\r
379 goto LABEL_PARTIAL;
\r
381 if (!(flag & (T1_SIG | T1_VISIT))) {
\r
382 mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
\r
383 if (mqc_decode(mqc)) {
\r
385 mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
\r
386 v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
\r
387 *dp = v ? -oneplushalf : oneplushalf;
\r
388 t1_updateflags(fp, v);
\r
393 } /* VSC and BYPASS by Antonin */
\r
395 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
\r
396 int i, j, k, one, agg, runlen, vsc;
\r
398 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
401 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
\r
402 for (k = 0; k < h; k += 4) {
\r
403 for (i = 0; i < w; i++) {
\r
405 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
\r
406 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
407 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
408 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
409 || (t1->flags[1 + k + 3][1 + i]
\r
410 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
\r
412 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
413 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
414 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
415 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
\r
421 for (runlen = 0; runlen < 4; runlen++) {
\r
422 if (int_abs(t1->data[k + runlen][i]) & one)
\r
425 mqc_setcurctx(mqc, T1_CTXNO_AGG);
\r
426 mqc_encode(mqc, runlen != 4);
\r
430 mqc_setcurctx(mqc, T1_CTXNO_UNI);
\r
431 mqc_encode(mqc, runlen >> 1);
\r
432 mqc_encode(mqc, runlen & 1);
\r
436 for (j = k + runlen; j < k + 4 && j < h; j++) {
\r
437 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
438 t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
\r
444 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
\r
445 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
\r
446 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
\r
448 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
452 oneplushalf = one | half;
\r
453 for (k = 0; k < h; k += 4) {
\r
454 for (i = 0; i < w; i++) {
\r
456 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
\r
457 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
458 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
459 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
460 || (t1->flags[1 + k + 3][1 + i]
\r
461 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
\r
463 agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
464 || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
465 || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
\r
466 || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
\r
472 mqc_setcurctx(mqc, T1_CTXNO_AGG);
\r
473 if (!mqc_decode(mqc)) {
\r
476 mqc_setcurctx(mqc, T1_CTXNO_UNI);
\r
477 runlen = mqc_decode(mqc);
\r
478 runlen = (runlen << 1) | mqc_decode(mqc);
\r
482 for (j = k + runlen; j < k + 4 && j < h; j++) {
\r
483 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
\r
484 t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
\r
490 mqc_setcurctx(mqc, T1_CTXNO_UNI);
\r
491 v = mqc_decode(mqc);
\r
492 v = (v << 1) | mqc_decode(mqc);
\r
493 v = (v << 1) | mqc_decode(mqc);
\r
494 v = (v << 1) | mqc_decode(mqc);
\r
497 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
\r
501 } /* VSC and BYPASS by Antonin */
\r
504 /** mod fixed_quality */
\r
505 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) {
\r
506 double w1, w2, wmsedec;
\r
508 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
\r
509 w2 = dwt_getnorm(level, orient);
\r
510 } else { /* if (qmfbid == 0) */
\r
511 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
\r
512 w2 = dwt_getnorm_real(level, orient);
\r
514 wmsedec = w1 * w2 * stepsize * (1 << bpno);
\r
515 wmsedec *= wmsedec * nmsedec / 8192.0;
\r
520 /** mod fixed_quality */
\r
521 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
\r
525 int bpno, passtype;
\r
528 double cumwmsedec = 0;
\r
529 char type = T1_TYPE_MQ;
\r
531 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
533 w = cblk->x1 - cblk->x0;
\r
534 h = cblk->y1 - cblk->y0;
\r
537 for (j = 0; j < h; j++) {
\r
538 for (i = 0; i < w; i++) {
\r
539 max = int_max(max, int_abs(t1->data[j][i]));
\r
543 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
\r
545 /* Changed by Dmitry Kolyadin */
\r
546 for (i = 0; i <= w; i++) {
\r
547 for (j = 0; j <= h; j++) {
\r
548 t1->flags[j][i] = 0;
\r
552 bpno = cblk->numbps - 1;
\r
555 mqc_resetstates(mqc);
\r
556 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
\r
557 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
\r
558 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
\r
559 mqc_init_enc(mqc, cblk->data);
\r
561 for (passno = 0; bpno >= 0; passno++) {
\r
562 opj_tcd_pass_t *pass = &cblk->passes[passno];
\r
563 int correction = 3;
\r
564 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
\r
566 switch (passtype) {
\r
568 t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
\r
571 t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
\r
574 t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
\r
575 /* code switch SEGMARK (i.e. SEGSYM) */
\r
576 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
\r
577 mqc_segmark_enc(mqc);
\r
581 /* fixed_quality */
\r
582 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
\r
583 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
\r
585 /* Code switch "RESTART" (i.e. TERMALL) */
\r
586 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
\r
587 if (type == T1_TYPE_RAW) {
\r
590 /* correction = mqc_bypass_flush_enc(); */
\r
591 } else { /* correction = mqc_restart_enc(); */
\r
597 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
\r
598 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
\r
599 if (type == T1_TYPE_RAW) {
\r
602 /* correction = mqc_bypass_flush_enc(); */
\r
603 } else { /* correction = mqc_restart_enc(); */
\r
613 if (++passtype == 3) {
\r
618 if (pass->term && bpno > 0) {
\r
619 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
\r
620 if (type == T1_TYPE_RAW)
\r
621 mqc_bypass_init_enc(mqc);
\r
623 mqc_restart_init_enc(mqc);
\r
626 pass->distortiondec = cumwmsedec;
\r
627 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
\r
628 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
\r
630 /* Code-switch "RESET" */
\r
631 if (cblksty & J2K_CCP_CBLKSTY_RESET)
\r
632 mqc_reset_enc(mqc);
\r
635 /* Code switch "ERTERM" (i.e. PTERM) */
\r
636 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
\r
637 mqc_erterm_enc(mqc);
\r
638 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
\r
641 cblk->totalpasses = passno;
\r
644 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
\r
646 int bpno, passtype;
\r
648 char type = T1_TYPE_MQ; /* BYPASS mode */
\r
650 opj_raw_t *raw = t1->raw; /* RAW component */
\r
651 opj_mqc_t *mqc = t1->mqc; /* MQC component */
\r
653 w = cblk->x1 - cblk->x0;
\r
654 h = cblk->y1 - cblk->y0;
\r
656 /* Changed by Dmitry Kolyadin */
\r
657 for (j = 0; j <= h; j++) {
\r
658 for (i = 0; i <= w; i++) {
\r
659 t1->flags[j][i] = 0;
\r
663 /* Changed by Dmitry Kolyadin */
\r
664 for (i = 0; i < w; i++) {
\r
665 for (j = 0; j < h; j++){
\r
666 t1->data[j][i] = 0;
\r
670 bpno = roishift + cblk->numbps - 1;
\r
673 mqc_resetstates(mqc);
\r
674 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
\r
675 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
\r
676 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
\r
678 for (segno = 0; segno < cblk->numsegs; segno++) {
\r
679 opj_tcd_seg_t *seg = &cblk->segs[segno];
\r
682 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
\r
683 if (type == T1_TYPE_RAW) {
\r
684 raw_init_dec(raw, seg->data, seg->len);
\r
686 mqc_init_dec(mqc, seg->data, seg->len);
\r
689 for (passno = 0; passno < seg->numpasses; passno++) {
\r
690 switch (passtype) {
\r
692 t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
\r
695 t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
\r
698 t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
\r
702 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
\r
703 mqc_resetstates(mqc);
\r
704 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
\r
705 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
\r
706 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
\r
708 if (++passtype == 3) {
\r
716 static int t1_init_ctxno_zc(int f, int orient) {
\r
717 int h, v, d, n, t, hv;
\r
719 h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
\r
720 v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
\r
721 d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
\r
738 } else if (v == 1) {
\r
743 } else if (h == 1) {
\r
760 } else if (hv == 1) {
\r
765 } else if (d == 1) {
\r
768 } else if (hv == 1) {
\r
773 } else if (d == 2) {
\r
785 return (T1_CTXNO_ZC + n);
\r
788 static int t1_init_ctxno_sc(int f) {
\r
792 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
\r
793 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
\r
794 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
\r
795 (T1_SIG_E | T1_SGN_E)) +
\r
796 ((f & (T1_SIG_W | T1_SGN_W)) ==
\r
797 (T1_SIG_W | T1_SGN_W)), 1);
\r
799 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
\r
800 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
\r
801 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
\r
802 (T1_SIG_N | T1_SGN_N)) +
\r
803 ((f & (T1_SIG_S | T1_SGN_S)) ==
\r
804 (T1_SIG_S | T1_SGN_S)), 1);
\r
817 } else if (hc == 1) {
\r
826 return (T1_CTXNO_SC + n);
\r
829 static int t1_init_ctxno_mag(int f) {
\r
831 if (!(f & T1_REFINE))
\r
832 n = (f & (T1_SIG_OTH)) ? 1 : 0;
\r
836 return (T1_CTXNO_MAG + n);
\r
839 static int t1_init_spb(int f) {
\r
842 hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
\r
843 T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
\r
844 1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
\r
845 (T1_SIG_E | T1_SGN_E)) +
\r
846 ((f & (T1_SIG_W | T1_SGN_W)) ==
\r
847 (T1_SIG_W | T1_SGN_W)), 1);
\r
849 vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
\r
850 T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
\r
851 1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
\r
852 (T1_SIG_N | T1_SGN_N)) +
\r
853 ((f & (T1_SIG_S | T1_SGN_S)) ==
\r
854 (T1_SIG_S | T1_SGN_S)), 1);
\r
859 n = (!(hc > 0 || (!hc && vc > 0)));
\r
864 static void t1_init_luts(opj_t1_t *t1) {
\r
867 for (j = 0; j < 4; j++) {
\r
868 for (i = 0; i < 256; ++i) {
\r
869 t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
\r
872 for (i = 0; i < 256; i++) {
\r
873 t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
\r
875 for (j = 0; j < 2; j++) {
\r
876 for (i = 0; i < 2048; ++i) {
\r
877 t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
\r
880 for (i = 0; i < 256; ++i) {
\r
881 t1->lut_spb[i] = t1_init_spb(i << 4);
\r
883 /* FIXME FIXME FIXME */
\r
884 /* fprintf(stdout,"nmsedec luts:\n"); */
\r
885 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
\r
886 t = i / pow(2, T1_NMSEDEC_FRACBITS);
\r
889 t1->lut_nmsedec_sig[i] =
\r
891 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
\r
892 t1->lut_nmsedec_sig0[i] =
\r
894 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
\r
896 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
\r
901 t1->lut_nmsedec_ref[i] =
\r
903 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
\r
904 t1->lut_nmsedec_ref0[i] =
\r
906 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
\r
910 /* ----------------------------------------------------------------------- */
\r
912 opj_t1_t* t1_create(opj_common_ptr cinfo) {
\r
913 opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
\r
916 /* create MQC and RAW handles */
\r
917 t1->mqc = mqc_create();
\r
918 t1->raw = raw_create();
\r
919 /* initialize the look-up tables of the Tier-1 coder/decoder */
\r
925 void t1_destroy(opj_t1_t *t1) {
\r
927 /* destroy MQC and RAW handles */
\r
928 mqc_destroy(t1->mqc);
\r
929 raw_destroy(t1->raw);
\r
934 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
\r
935 int compno, resno, bandno, precno, cblkno;
\r
936 int x, y, i, j, orient;
\r
938 tile->distotile = 0; /* fixed_quality */
\r
940 for (compno = 0; compno < tile->numcomps; compno++) {
\r
941 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
\r
943 for (resno = 0; resno < tilec->numresolutions; resno++) {
\r
944 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
\r
946 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
947 opj_tcd_band_t *band = &res->bands[bandno];
\r
949 for (precno = 0; precno < res->pw * res->ph; precno++) {
\r
950 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
952 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
\r
953 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
955 if (band->bandno == 0) {
\r
956 x = cblk->x0 - band->x0;
\r
957 y = cblk->y0 - band->y0;
\r
958 } else if (band->bandno == 1) {
\r
959 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
960 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
\r
961 y = cblk->y0 - band->y0;
\r
962 } else if (band->bandno == 2) {
\r
963 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
964 x = cblk->x0 - band->x0;
\r
965 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
\r
966 } else { /* if (band->bandno == 3) */
\r
967 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
968 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
\r
969 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
\r
972 if (tcp->tccps[compno].qmfbid == 1) {
\r
973 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
\r
974 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
\r
976 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
\r
979 } else if (tcp->tccps[compno].qmfbid == 0) {
\r
980 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
\r
981 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
\r
984 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
\r
985 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
\r
989 orient = band->bandno; /* FIXME */
\r
992 } else if (orient == 1) {
\r
996 t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
\r
1005 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
\r
1006 int compno, resno, bandno, precno, cblkno;
\r
1008 for (compno = 0; compno < tile->numcomps; compno++) {
\r
1009 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
\r
1011 for (resno = 0; resno < tilec->numresolutions; resno++) {
\r
1012 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
\r
1014 for (bandno = 0; bandno < res->numbands; bandno++) {
\r
1015 opj_tcd_band_t *band = &res->bands[bandno];
\r
1017 for (precno = 0; precno < res->pw * res->ph; precno++) {
\r
1018 opj_tcd_precinct_t *prc = &band->precincts[precno];
\r
1020 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
\r
1021 int x, y, i, j, orient;
\r
1022 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
\r
1024 orient = band->bandno; /* FIXME */
\r
1025 if (orient == 2) {
\r
1027 } else if (orient == 1) {
\r
1031 t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
\r
1033 if (band->bandno == 0) {
\r
1034 x = cblk->x0 - band->x0;
\r
1035 y = cblk->y0 - band->y0;
\r
1036 } else if (band->bandno == 1) {
\r
1037 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
1038 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
\r
1039 y = cblk->y0 - band->y0;
\r
1040 } else if (band->bandno == 2) {
\r
1041 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
1042 x = cblk->x0 - band->x0;
\r
1043 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
\r
1044 } else { /* if (band->bandno == 3) */
\r
1045 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
\r
1046 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
\r
1047 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
\r
1050 if (tcp->tccps[compno].roishift) {
\r
1051 int thresh, val, mag;
\r
1052 thresh = 1 << tcp->tccps[compno].roishift;
\r
1053 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
\r
1054 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
\r
1055 val = t1->data[j][i];
\r
1056 mag = int_abs(val);
\r
1057 if (mag >= thresh) {
\r
1058 mag >>= tcp->tccps[compno].roishift;
\r
1059 t1->data[j][i] = val < 0 ? -mag : mag;
\r
1065 if (tcp->tccps[compno].qmfbid == 1) {
\r
1066 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
\r
1067 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
\r
1068 int tmp = t1->data[j][i];
\r
1069 if(tmp >> 1 == 0) {
\r
1070 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
\r
1072 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
\r
1076 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
\r
1077 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
\r
1078 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
\r
1079 double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
\r
1080 if (t1->data[j][i] >> 1 == 0) {
\r
1081 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
\r
1083 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
\r
1084 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);
\r