2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
39 /** @name Local static functions */
42 static INLINE char t1_getctxno_zc(int f, int orient);
43 static char t1_getctxno_sc(int f);
44 static INLINE int t1_getctxno_mag(int f);
45 static char t1_getspb(int f);
46 static short t1_getnmsedec_sig(int x, int bitpos);
47 static short t1_getnmsedec_ref(int x, int bitpos);
48 static void t1_updateflags(flag_t *flagsp, int s, int stride);
50 Encode significant pass
52 static void t1_enc_sigpass_step(
63 Decode significant pass
65 static void t1_dec_sigpass_step(
74 Encode significant pass
76 static void t1_enc_sigpass(
84 Decode significant pass
86 static void t1_dec_sigpass(
93 Encode refinement pass
95 static void t1_enc_refpass_step(
105 Decode refinement pass
107 static void t1_dec_refpass_step(
116 Encode refinement pass
118 static void t1_enc_refpass(
125 Decode refinement pass
127 static void t1_dec_refpass(
135 static void t1_enc_clnpass_step(
148 static void t1_dec_clnpass_step(
159 static void t1_enc_clnpass(
168 static void t1_dec_clnpass(
173 static double t1_getwmsedec(
185 @param cblk Code-block coding parameters
187 @param compno Component number
191 @param cblksty Code-block style
195 static void t1_encode_cblk(
197 opj_tcd_cblk_t * cblk,
205 opj_tcd_tile_t * tile);
209 @param cblk Code-block coding parameters
211 @param roishift Region of interest shifting value
212 @param cblksty Code-block style
214 static void t1_decode_cblk(
216 opj_tcd_cblk_t * cblk,
225 /* ----------------------------------------------------------------------- */
227 static char t1_getctxno_zc(int f, int orient) {
228 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
231 static char t1_getctxno_sc(int f) {
232 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
235 static int t1_getctxno_mag(int f) {
236 int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
237 int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
241 static char t1_getspb(int f) {
242 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
245 static short t1_getnmsedec_sig(int x, int bitpos) {
246 if (bitpos > T1_NMSEDEC_FRACBITS) {
247 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
250 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
253 static short t1_getnmsedec_ref(int x, int bitpos) {
254 if (bitpos > T1_NMSEDEC_FRACBITS) {
255 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
258 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
261 static void t1_updateflags(flag_t *flagsp, int s, int stride) {
262 flag_t *np = flagsp - stride;
263 flag_t *sp = flagsp + stride;
265 static const flag_t mod[] = {
266 T1_SIG_S, T1_SIG_S|T1_SGN_S,
267 T1_SIG_E, T1_SIG_E|T1_SGN_E,
268 T1_SIG_W, T1_SIG_W|T1_SGN_W,
269 T1_SIG_N, T1_SIG_N|T1_SGN_N
276 flagsp[-1] |= mod[s+2];
278 flagsp[1] |= mod[s+4];
285 static void t1_enc_sigpass_step(
298 opj_mqc_t *mqc = t1->mqc; /* MQC component */
300 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
301 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
302 v = int_abs(*datap) & one ? 1 : 0;
303 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
304 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
305 mqc_bypass_enc(mqc, v);
310 v = *datap < 0 ? 1 : 0;
311 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
312 mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
313 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
314 mqc_bypass_enc(mqc, v);
316 mqc_encode(mqc, v ^ t1_getspb(flag));
318 t1_updateflags(flagsp, v, t1->flags_stride);
324 static void t1_dec_sigpass_step(
335 opj_raw_t *raw = t1->raw; /* RAW component */
336 opj_mqc_t *mqc = t1->mqc; /* MQC component */
338 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
339 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
340 if (type == T1_TYPE_RAW) {
341 if (raw_decode(raw)) {
342 v = raw_decode(raw); /* ESSAI */
343 *datap = v ? -oneplushalf : oneplushalf;
344 t1_updateflags(flagsp, v, t1->flags_stride);
347 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
348 if (mqc_decode(mqc)) {
349 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
350 v = mqc_decode(mqc) ^ t1_getspb(flag);
351 *datap = v ? -oneplushalf : oneplushalf;
352 t1_updateflags(flagsp, v, t1->flags_stride);
357 } /* VSC and BYPASS by Antonin */
359 static void t1_enc_sigpass(
367 int i, j, k, one, vsc;
369 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
370 for (k = 0; k < t1->h; k += 4) {
371 for (i = 0; i < t1->w; ++i) {
372 for (j = k; j < k + 4 && j < t1->h; ++j) {
373 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
376 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
377 &t1->data[(j * t1->w) + i],
389 static void t1_dec_sigpass(
396 int i, j, k, one, half, oneplushalf, vsc;
399 oneplushalf = one | half;
400 for (k = 0; k < t1->h; k += 4) {
401 for (i = 0; i < t1->w; ++i) {
402 for (j = k; j < k + 4 && j < t1->h; ++j) {
403 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
406 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
407 &t1->data[(j * t1->w) + i],
415 } /* VSC and BYPASS by Antonin */
417 static void t1_enc_refpass_step(
429 opj_mqc_t *mqc = t1->mqc; /* MQC component */
431 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
432 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
433 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
434 v = int_abs(*datap) & one ? 1 : 0;
435 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
436 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
437 mqc_bypass_enc(mqc, v);
441 *flagsp |= T1_REFINE;
445 static void t1_dec_refpass_step(
456 opj_mqc_t *mqc = t1->mqc; /* MQC component */
457 opj_raw_t *raw = t1->raw; /* RAW component */
459 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
460 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
461 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
462 if (type == T1_TYPE_RAW) {
467 t = v ? poshalf : neghalf;
468 *datap += *datap < 0 ? -t : t;
469 *flagsp |= T1_REFINE;
471 } /* VSC and BYPASS by Antonin */
473 static void t1_enc_refpass(
480 int i, j, k, one, vsc;
482 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
483 for (k = 0; k < t1->h; k += 4) {
484 for (i = 0; i < t1->w; ++i) {
485 for (j = k; j < k + 4 && j < t1->h; ++j) {
486 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
489 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
490 &t1->data[(j * t1->w) + i],
501 static void t1_dec_refpass(
507 int i, j, k, one, poshalf, neghalf;
511 neghalf = bpno > 0 ? -poshalf : -1;
512 for (k = 0; k < t1->h; k += 4) {
513 for (i = 0; i < t1->w; ++i) {
514 for (j = k; j < k + 4 && j < t1->h; ++j) {
515 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
518 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
519 &t1->data[(j * t1->w) + i],
527 } /* VSC and BYPASS by Antonin */
529 static void t1_enc_clnpass_step(
542 opj_mqc_t *mqc = t1->mqc; /* MQC component */
544 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
548 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
549 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
550 v = int_abs(*datap) & one ? 1 : 0;
554 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
555 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
556 v = *datap < 0 ? 1 : 0;
557 mqc_encode(mqc, v ^ t1_getspb(flag));
558 t1_updateflags(flagsp, v, t1->flags_stride);
561 *flagsp &= ~T1_VISIT;
564 static void t1_dec_clnpass_step(
575 opj_mqc_t *mqc = t1->mqc; /* MQC component */
577 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
581 if (!(flag & (T1_SIG | T1_VISIT))) {
582 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
583 if (mqc_decode(mqc)) {
585 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
586 v = mqc_decode(mqc) ^ t1_getspb(flag);
587 *datap = v ? -oneplushalf : oneplushalf;
588 t1_updateflags(flagsp, v, t1->flags_stride);
591 *flagsp &= ~T1_VISIT;
592 } /* VSC and BYPASS by Antonin */
594 static void t1_enc_clnpass(
601 int i, j, k, one, agg, runlen, vsc;
603 opj_mqc_t *mqc = t1->mqc; /* MQC component */
606 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
607 for (k = 0; k < t1->h; k += 4) {
608 for (i = 0; i < t1->w; ++i) {
610 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
611 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
612 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
613 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
614 || (MACRO_t1_flags(1 + k + 3,1 + i)
615 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
617 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
618 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
619 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
620 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
626 for (runlen = 0; runlen < 4; ++runlen) {
627 if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
630 mqc_setcurctx(mqc, T1_CTXNO_AGG);
631 mqc_encode(mqc, runlen != 4);
635 mqc_setcurctx(mqc, T1_CTXNO_UNI);
636 mqc_encode(mqc, runlen >> 1);
637 mqc_encode(mqc, runlen & 1);
641 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
642 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
645 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
646 &t1->data[(j * t1->w) + i],
651 agg && (j == k + runlen),
658 static void t1_dec_clnpass(
664 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
665 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
667 opj_mqc_t *mqc = t1->mqc; /* MQC component */
671 oneplushalf = one | half;
672 for (k = 0; k < t1->h; k += 4) {
673 for (i = 0; i < t1->w; ++i) {
675 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
676 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
677 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
678 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
679 || (MACRO_t1_flags(1 + k + 3,1 + i)
680 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
682 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
683 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
684 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
685 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
691 mqc_setcurctx(mqc, T1_CTXNO_AGG);
692 if (!mqc_decode(mqc)) {
695 mqc_setcurctx(mqc, T1_CTXNO_UNI);
696 runlen = mqc_decode(mqc);
697 runlen = (runlen << 1) | mqc_decode(mqc);
701 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
702 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
705 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
706 &t1->data[(j * t1->w) + i],
709 agg && (j == k + runlen),
716 mqc_setcurctx(mqc, T1_CTXNO_UNI);
718 v = (v << 1) | mqc_decode(mqc);
719 v = (v << 1) | mqc_decode(mqc);
720 v = (v << 1) | mqc_decode(mqc);
723 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
727 } /* VSC and BYPASS by Antonin */
730 /** mod fixed_quality */
731 static double t1_getwmsedec(
741 double w1, w2, wmsedec;
743 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1.0;
744 w2 = dwt_getnorm(level, orient);
745 } else { /* if (qmfbid == 0) */
746 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1.0;
747 w2 = dwt_getnorm_real(level, orient);
749 wmsedec = w1 * w2 * stepsize * (1 << bpno);
750 wmsedec *= wmsedec * nmsedec / 8192.0;
755 static bool allocate_buffers(
763 if(datasize > t1->datasize){
764 opj_aligned_free(t1->data);
765 t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
769 t1->datasize=datasize;
771 memset(t1->data,0,datasize * sizeof(int));
773 t1->flags_stride=w+2;
774 flagssize=t1->flags_stride * (h+2);
776 if(flagssize > t1->flagssize){
777 opj_aligned_free(t1->flags);
778 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
782 t1->flagssize=flagssize;
784 memset(t1->flags,0,flagssize * sizeof(flag_t));
792 /** mod fixed_quality */
793 static void t1_encode_cblk(
795 opj_tcd_cblk_t * cblk,
803 opj_tcd_tile_t * tile)
810 double cumwmsedec = 0.0;
811 char type = T1_TYPE_MQ;
813 opj_mqc_t *mqc = t1->mqc; /* MQC component */
816 for (j = 0; j < t1->h; ++j) {
817 for (i = 0; i < t1->w; ++i) {
818 max = int_max(max, int_abs(t1->data[(j * t1->w) + i]));
822 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
824 bpno = cblk->numbps - 1;
827 mqc_resetstates(mqc);
828 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
829 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
830 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
831 mqc_init_enc(mqc, cblk->data);
833 for (passno = 0; bpno >= 0; ++passno) {
834 opj_tcd_pass_t *pass = &cblk->passes[passno];
836 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
840 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
843 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
846 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
847 /* code switch SEGMARK (i.e. SEGSYM) */
848 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
849 mqc_segmark_enc(mqc);
854 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
855 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
857 /* Code switch "RESTART" (i.e. TERMALL) */
858 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
859 if (type == T1_TYPE_RAW) {
862 /* correction = mqc_bypass_flush_enc(); */
863 } else { /* correction = mqc_restart_enc(); */
869 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
870 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
871 if (type == T1_TYPE_RAW) {
874 /* correction = mqc_bypass_flush_enc(); */
875 } else { /* correction = mqc_restart_enc(); */
885 if (++passtype == 3) {
890 if (pass->term && bpno > 0) {
891 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
892 if (type == T1_TYPE_RAW)
893 mqc_bypass_init_enc(mqc);
895 mqc_restart_init_enc(mqc);
898 pass->distortiondec = cumwmsedec;
899 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
901 /* Code-switch "RESET" */
902 if (cblksty & J2K_CCP_CBLKSTY_RESET)
906 /* Code switch "ERTERM" (i.e. PTERM) */
907 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
909 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
912 cblk->totalpasses = passno;
914 for (passno = 0; passno<cblk->totalpasses; passno++) {
915 opj_tcd_pass_t *pass = &cblk->passes[passno];
916 if (pass->rate > mqc_numbytes(mqc))
917 pass->rate = mqc_numbytes(mqc);
918 /*Preventing generation of FF as last data byte of a pass*/
919 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
922 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
926 static void t1_decode_cblk(
928 opj_tcd_cblk_t * cblk,
935 char type = T1_TYPE_MQ; /* BYPASS mode */
937 opj_raw_t *raw = t1->raw; /* RAW component */
938 opj_mqc_t *mqc = t1->mqc; /* MQC component */
940 if(!allocate_buffers(
943 cblk->y1 - cblk->y0))
948 bpno = roishift + cblk->numbps - 1;
951 mqc_resetstates(mqc);
952 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
953 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
954 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
956 for (segno = 0; segno < cblk->numsegs; ++segno) {
957 opj_tcd_seg_t *seg = &cblk->segs[segno];
960 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
961 if (type == T1_TYPE_RAW) {
962 raw_init_dec(raw, seg->data, seg->len);
964 mqc_init_dec(mqc, seg->data, seg->len);
967 for (passno = 0; passno < seg->numpasses; ++passno) {
970 t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
973 t1_dec_refpass(t1, bpno+1, type, cblksty);
976 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
980 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
981 mqc_resetstates(mqc);
982 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
983 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
984 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
986 if (++passtype == 3) {
994 /* ----------------------------------------------------------------------- */
996 opj_t1_t* t1_create(opj_common_ptr cinfo) {
997 opj_t1_t *t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1002 /* create MQC and RAW handles */
1003 t1->mqc = mqc_create();
1004 t1->raw = raw_create();
1014 void t1_destroy(opj_t1_t *t1) {
1016 /* destroy MQC and RAW handles */
1017 mqc_destroy(t1->mqc);
1018 raw_destroy(t1->raw);
1019 opj_aligned_free(t1->data);
1020 opj_aligned_free(t1->flags);
1025 void t1_encode_cblks(
1027 opj_tcd_tile_t *tile,
1030 int compno, resno, bandno, precno, cblkno;
1032 tile->distotile = 0; /* fixed_quality */
1034 for (compno = 0; compno < tile->numcomps; ++compno) {
1035 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1037 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1038 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1040 for (bandno = 0; bandno < res->numbands; ++bandno) {
1041 opj_tcd_band_t *band = &res->bands[bandno];
1043 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1044 opj_tcd_precinct_t *prc = &band->precincts[precno];
1046 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1048 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1050 x = cblk->x0 - band->x0;
1051 y = cblk->y0 - band->y0;
1052 if (band->bandno & 1) {
1053 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1054 x += pres->x1 - pres->x0;
1056 if (band->bandno & 2) {
1057 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1058 y += pres->y1 - pres->y0;
1061 if(!allocate_buffers(
1063 cblk->x1 - cblk->x0,
1064 cblk->y1 - cblk->y0))
1069 w = tilec->x1 - tilec->x0;
1070 if (tcp->tccps[compno].qmfbid == 1) {
1071 for (j = 0; j < t1->h; ++j) {
1072 for (i = 0; i < t1->w; ++i) {
1073 t1->data[(j * t1->w) + i] =
1074 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
1077 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1078 for (j = 0; j < t1->h; ++j) {
1079 for (i = 0; i < t1->w; ++i) {
1080 t1->data[(j * t1->w) + i] =
1082 tilec->data[x + i + (y + j) * w],
1083 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
1093 tilec->numresolutions - 1 - resno,
1094 tcp->tccps[compno].qmfbid,
1096 tcp->tccps[compno].cblksty,
1107 void t1_decode_cblks(
1109 opj_tcd_tile_t *tile,
1112 int compno, resno, bandno, precno, cblkno;
1114 for (compno = 0; compno < tile->numcomps; ++compno) {
1115 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1117 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1118 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1120 for (bandno = 0; bandno < res->numbands; ++bandno) {
1121 opj_tcd_band_t *band = &res->bands[bandno];
1123 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1124 opj_tcd_precinct_t *prc = &band->precincts[precno];
1126 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1127 int x, y, w, i, j, cblk_w, cblk_h;
1128 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1134 tcp->tccps[compno].roishift,
1135 tcp->tccps[compno].cblksty);
1137 x = cblk->x0 - band->x0;
1138 y = cblk->y0 - band->y0;
1139 if (band->bandno & 1) {
1140 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1141 x += pres->x1 - pres->x0;
1143 if (band->bandno & 2) {
1144 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1145 y += pres->y1 - pres->y0;
1148 cblk_w = cblk->x1 - cblk->x0;
1149 cblk_h = cblk->y1 - cblk->y0;
1151 if (tcp->tccps[compno].roishift) {
1152 int thresh = 1 << tcp->tccps[compno].roishift;
1153 for (j = 0; j < cblk_h; ++j) {
1154 for (i = 0; i < cblk_w; ++i) {
1155 int val = t1->data[(j * t1->w) + i];
1156 int mag = int_abs(val);
1157 if (mag >= thresh) {
1158 mag >>= tcp->tccps[compno].roishift;
1159 t1->data[(j * t1->w) + i] = val < 0 ? -mag : mag;
1165 w = tilec->x1 - tilec->x0;
1166 if (tcp->tccps[compno].qmfbid == 1) {
1167 for (j = 0; j < cblk_h; ++j) {
1168 for (i = 0; i < cblk_w; ++i) {
1169 tilec->data[x + i + (y + j) * w] = t1->data[(j * t1->w) + i]/2;
1172 } else { /* if (tcp->tccps[compno].qmfbid == 0) */
1173 for (j = 0; j < cblk_h; ++j) {
1174 for (i = 0; i < cblk_w; ++i) {
1175 if (t1->data[(j * t1->w) + i] >> 1 == 0) {
1176 tilec->data[x + i + (y + j) * w] = 0;
1178 double tmp = (double)(t1->data[(j * t1->w) + i] * band->stepsize * 4096.0);
1179 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1180 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);