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) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
42 /* #define CONSISTENCY_CHECK */
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
47 /** @name Local static functions */
50 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
52 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
53 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
54 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
55 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
58 Encode significant pass
60 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
70 Decode significant pass
73 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
77 OPJ_INT32 oneplushalf,
82 static INLINE void opj_t1_dec_sigpass_step_raw(
85 opj_colflag_t* colflagsp,
87 OPJ_INT32 oneplushalf,
90 static INLINE void opj_t1_dec_sigpass_step_mqc(
93 opj_colflag_t* colflagsp,
95 OPJ_INT32 oneplushalf,
97 OPJ_INT32 flags_stride);
98 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
101 opj_colflag_t* colflagsp,
103 OPJ_INT32 oneplushalf,
109 Encode significant pass
111 static void opj_t1_enc_sigpass( opj_t1_t *t1,
118 Decode significant pass
120 static void opj_t1_dec_sigpass_raw(
124 static void opj_t1_dec_sigpass_mqc_vsc(
131 Encode refinement pass
133 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
144 Encode refinement pass
146 static void opj_t1_enc_refpass( opj_t1_t *t1,
153 Decode refinement pass
155 static void opj_t1_dec_refpass_raw(
159 static void opj_t1_dec_refpass_mqc_vsc(
165 Decode refinement pass
168 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
177 static INLINE void opj_t1_dec_refpass_step_raw(
180 opj_colflag_t *colflagsp,
185 static INLINE void opj_t1_dec_refpass_step_mqc(
188 opj_colflag_t *colflagsp,
193 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
196 opj_colflag_t *colflagsp,
208 static void opj_t1_enc_clnpass_step(
220 static void opj_t1_dec_clnpass_step_partial(
223 opj_colflag_t *colflagsp,
225 OPJ_INT32 oneplushalf,
227 static void opj_t1_dec_clnpass_step(
230 opj_colflag_t *colflagsp,
232 OPJ_INT32 oneplushalf,
234 static void opj_t1_dec_clnpass_step_vsc(
237 opj_colflag_t *colflagsp,
239 OPJ_INT32 oneplushalf,
246 static void opj_t1_enc_clnpass(
254 static void opj_t1_dec_clnpass(
259 static OPJ_FLOAT64 opj_t1_getwmsedec(
266 OPJ_FLOAT64 stepsize,
268 const OPJ_FLOAT64 * mct_norms,
269 OPJ_UINT32 mct_numcomps);
271 static void opj_t1_encode_cblk( opj_t1_t *t1,
272 opj_tcd_cblk_enc_t* cblk,
277 OPJ_FLOAT64 stepsize,
280 opj_tcd_tile_t * tile,
281 const OPJ_FLOAT64 * mct_norms,
282 OPJ_UINT32 mct_numcomps);
287 @param cblk Code-block coding parameters
289 @param roishift Region of interest shifting value
290 @param cblksty Code-block style
292 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
293 opj_tcd_cblk_dec_t* cblk,
298 static OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
306 /* ----------------------------------------------------------------------- */
308 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f) {
309 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
312 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
313 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
316 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
317 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
318 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
322 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
323 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
326 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
328 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
331 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
334 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
336 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
339 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
342 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
343 opj_flag_t *np = flagsp - stride;
344 opj_flag_t *sp = flagsp + stride;
346 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
347 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
348 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
350 opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
353 np[0] |= flag_N << 2;
356 flagsp[-1] |= flag_N << 1;
358 flagsp[1] |= flag_N << 3;
365 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp, opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_INT32 row)
367 opj_t1_updateflags(flagsp, s, stride);
370 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
371 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
372 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
373 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
374 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
375 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
376 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
377 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
378 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
382 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
383 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
384 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
385 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
386 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
387 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS* (row-1)));
388 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
389 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
390 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
394 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
395 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
396 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
397 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
398 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
399 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
400 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
401 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
402 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
406 static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
419 opj_mqc_t *mqc = t1->mqc; /* MQC component */
421 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
422 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
423 v = (opj_int_abs(*datap) & one) ? 1 : 0;
424 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag)); /* ESSAI */
425 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
426 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
428 opj_mqc_encode(mqc, (OPJ_UINT32)v);
431 v = *datap < 0 ? 1 : 0;
432 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
433 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
434 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
435 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
437 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
439 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
446 static INLINE void opj_t1_dec_sigpass_step_raw(
449 opj_colflag_t* colflagsp,
451 OPJ_INT32 oneplushalf,
456 opj_raw_t *raw = t1->raw; /* RAW component */
458 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
459 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
460 if (opj_raw_decode(raw)) {
461 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
462 *datap = v ? -oneplushalf : oneplushalf;
463 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
465 #ifdef CONSISTENCY_CHECK
468 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
472 static INLINE void opj_t1_dec_sigpass_step_mqc(
475 opj_colflag_t* colflagsp,
477 OPJ_INT32 oneplushalf,
479 OPJ_INT32 flags_stride)
483 opj_mqc_t *mqc = t1->mqc; /* MQC component */
484 #ifdef CONSISTENCY_CHECK
485 assert( ((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
486 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
487 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) );
489 if( (*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
490 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) ) {
492 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
493 if (opj_mqc_decode(mqc)) {
494 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
495 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
496 *datap = v ? -oneplushalf : oneplushalf;
497 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
499 #ifdef CONSISTENCY_CHECK
502 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
504 } /* VSC and BYPASS by Antonin */
506 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
509 opj_colflag_t* colflagsp,
511 OPJ_INT32 oneplushalf,
517 opj_mqc_t *mqc = t1->mqc; /* MQC component */
519 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
520 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
521 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
522 if (opj_mqc_decode(mqc)) {
523 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
524 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
525 *datap = v ? -oneplushalf : oneplushalf;
526 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
528 #ifdef CONSISTENCY_CHECK
531 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
533 } /* VSC and BYPASS by Antonin */
537 static void opj_t1_enc_sigpass(opj_t1_t *t1,
544 OPJ_UINT32 i, j, k, vsc;
548 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
549 for (k = 0; k < t1->h; k += 4) {
550 for (i = 0; i < t1->w; ++i) {
551 for (j = k; j < k + 4 && j < t1->h; ++j) {
552 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
553 opj_t1_enc_sigpass_step(
555 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
556 &t1->data[(j * t1->data_stride) + i],
567 static void opj_t1_dec_sigpass_raw(
572 OPJ_INT32 one, half, oneplushalf, vsc;
574 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
577 oneplushalf = one | half;
578 for (k = 0; k < t1->h; k += 4) {
579 for (i = 0; i < t1->w; ++i) {
580 opj_colflag_t *colflags2 = colflags1 + i;
581 for (j = k; j < k + 4 && j < t1->h; ++j) {
582 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
583 opj_t1_dec_sigpass_step_raw(
585 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
587 &t1->data[(j * t1->w) + i],
593 colflags1 += t1->flags_stride;
595 } /* VSC and BYPASS by Antonin */
597 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
599 OPJ_INT32 one, half, oneplushalf; \
600 OPJ_UINT32 i, j, k; \
601 OPJ_INT32 *data1 = t1->data; \
602 opj_flag_t *flags1 = &t1->flags[1]; \
603 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
606 oneplushalf = one | half; \
607 for (k = 0; k < (h & ~3u); k += 4) { \
608 for (i = 0; i < w; ++i) { \
609 OPJ_INT32 *data2 = data1 + i; \
610 opj_flag_t *flags2 = flags1 + i; \
611 opj_colflag_t *colflags2 = colflags1 + i; \
612 if( *colflags2 == 0 ) continue; \
613 flags2 += flags_stride; \
614 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0, flags_stride); \
616 flags2 += flags_stride; \
617 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1, flags_stride); \
619 flags2 += flags_stride; \
620 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2, flags_stride); \
622 flags2 += flags_stride; \
623 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3, flags_stride); \
627 flags1 += flags_stride << 2; \
628 colflags1 += flags_stride; \
630 for (i = 0; i < w; ++i) { \
631 OPJ_INT32 *data2 = data1 + i; \
632 opj_flag_t *flags2 = flags1 + i; \
633 opj_colflag_t *colflags2 = colflags1 + i; \
634 for (j = k; j < h; ++j) { \
635 flags2 += flags_stride; \
636 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
642 static void opj_t1_dec_sigpass_mqc_64x64(
646 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
649 static void opj_t1_dec_sigpass_mqc_generic(
653 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
656 /* VSC and BYPASS by Antonin */
657 static void opj_t1_dec_sigpass_mqc_vsc(
661 OPJ_INT32 one, half, oneplushalf, vsc;
663 OPJ_INT32 *data1 = t1->data;
664 opj_flag_t *flags1 = &t1->flags[1];
665 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
668 oneplushalf = one | half;
669 for (k = 0; k < (t1->h & ~3); k += 4) {
670 for (i = 0; i < t1->w; ++i) {
671 OPJ_INT32 *data2 = data1 + i;
672 opj_flag_t *flags2 = flags1 + i;
673 opj_colflag_t *colflags2 = colflags1 + i;
674 flags2 += t1->flags_stride;
675 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 0);
677 flags2 += t1->flags_stride;
678 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 1);
680 flags2 += t1->flags_stride;
681 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 2);
683 flags2 += t1->flags_stride;
684 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1, 3);
688 flags1 += t1->flags_stride << 2;
689 colflags1 += t1->flags_stride;
691 for (i = 0; i < t1->w; ++i) {
692 opj_colflag_t *colflags2 = colflags1 + i;
693 for (j = k; j < t1->h; ++j) {
694 vsc = (j == t1->h - 1) ? 1 : 0;
695 opj_t1_dec_sigpass_step_mqc_vsc(
697 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
699 &t1->data[(j * t1->w) + i],
705 } /* VSC and BYPASS by Antonin */
709 static void opj_t1_enc_refpass_step( opj_t1_t *t1,
721 opj_mqc_t *mqc = t1->mqc; /* MQC component */
723 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
724 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
725 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
726 v = (opj_int_abs(*datap) & one) ? 1 : 0;
727 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
728 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
729 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
731 opj_mqc_encode(mqc, (OPJ_UINT32)v);
733 *flagsp |= T1_REFINE;
737 static INLINE void opj_t1_dec_refpass_step_raw(
740 opj_colflag_t *colflagsp,
748 opj_raw_t *raw = t1->raw; /* RAW component */
750 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
751 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
752 v = (OPJ_INT32)opj_raw_decode(raw);
753 t = v ? poshalf : neghalf;
754 *datap += *datap < 0 ? -t : t;
755 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
757 } /* VSC and BYPASS by Antonin */
759 static INLINE void opj_t1_dec_refpass_step_mqc(
761 #ifdef CONSISTENCY_CHECK
764 opj_flag_t *flagsp_unused,
766 opj_colflag_t *colflagsp,
774 opj_mqc_t *mqc = t1->mqc; /* MQC component */
775 #ifdef CONSISTENCY_CHECK
776 assert( ((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
777 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) );
779 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
780 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
781 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
782 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
783 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
784 v = opj_mqc_decode(mqc);
785 t = v ? poshalf : neghalf;
786 *datap += *datap < 0 ? -t : t;
787 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
789 } /* VSC and BYPASS by Antonin */
791 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
794 opj_colflag_t *colflagsp,
801 OPJ_INT32 v, t, flag;
803 opj_mqc_t *mqc = t1->mqc; /* MQC component */
805 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
806 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
807 OPJ_INT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
808 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
809 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
810 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
811 v = opj_mqc_decode(mqc);
812 t = v ? poshalf : neghalf;
813 *datap += *datap < 0 ? -t : t;
814 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
816 } /* VSC and BYPASS by Antonin */
819 static void opj_t1_enc_refpass(
826 OPJ_UINT32 i, j, k, vsc;
830 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
831 for (k = 0; k < t1->h; k += 4) {
832 for (i = 0; i < t1->w; ++i) {
833 for (j = k; j < k + 4 && j < t1->h; ++j) {
834 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
835 opj_t1_enc_refpass_step(
837 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
838 &t1->data[(j * t1->data_stride) + i],
849 static void opj_t1_dec_refpass_raw(
854 OPJ_INT32 one, poshalf, neghalf;
857 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
860 neghalf = bpno > 0 ? -poshalf : -1;
861 for (k = 0; k < t1->h; k += 4) {
862 for (i = 0; i < t1->w; ++i) {
863 opj_colflag_t *colflags2 = colflags1 + i;
864 for (j = k; j < k + 4 && j < t1->h; ++j) {
865 opj_t1_dec_refpass_step_raw(
867 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
869 &t1->data[(j * t1->w) + i],
874 colflags1 += t1->flags_stride;
876 } /* VSC and BYPASS by Antonin */
878 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
880 OPJ_INT32 one, poshalf, neghalf; \
881 OPJ_UINT32 i, j, k; \
882 OPJ_INT32 *data1 = t1->data; \
883 opj_flag_t *flags1 = &t1->flags[1]; \
884 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
886 poshalf = one >> 1; \
887 neghalf = bpno > 0 ? -poshalf : -1; \
888 for (k = 0; k < (h & ~3u); k += 4) { \
889 for (i = 0; i < w; ++i) { \
890 OPJ_INT32 *data2 = data1 + i; \
891 opj_flag_t *flags2 = flags1 + i; \
892 opj_colflag_t *colflags2 = colflags1 + i; \
893 if( *colflags2 == 0 ) continue; \
894 flags2 += flags_stride; \
895 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0); \
897 flags2 += flags_stride; \
898 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1); \
900 flags2 += flags_stride; \
901 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2); \
903 flags2 += flags_stride; \
904 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3); \
908 flags1 += flags_stride << 2; \
909 colflags1 += flags_stride; \
911 for (i = 0; i < w; ++i) { \
912 OPJ_INT32 *data2 = data1 + i; \
913 opj_flag_t *flags2 = flags1 + i; \
914 opj_colflag_t *colflags2 = colflags1 + i; \
915 for (j = k; j < h; ++j) { \
916 flags2 += flags_stride; \
917 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
923 static void opj_t1_dec_refpass_mqc_64x64(
927 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
930 static void opj_t1_dec_refpass_mqc_generic(
934 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
937 /* VSC and BYPASS by Antonin */
938 static void opj_t1_dec_refpass_mqc_vsc(
942 OPJ_INT32 one, poshalf, neghalf;
945 OPJ_INT32 *data1 = t1->data;
946 opj_flag_t *flags1 = &t1->flags[1];
947 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
950 neghalf = bpno > 0 ? -poshalf : -1;
951 for (k = 0; k < (t1->h & ~3); k += 4) {
952 for (i = 0; i < t1->w; ++i) {
953 OPJ_INT32 *data2 = data1 + i;
954 opj_flag_t *flags2 = flags1 + i;
955 opj_colflag_t *colflags2 = colflags1 + i;
956 flags2 += t1->flags_stride;
957 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 0);
959 flags2 += t1->flags_stride;
960 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 1);
962 flags2 += t1->flags_stride;
963 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 2);
965 flags2 += t1->flags_stride;
966 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 1, 3);
970 flags1 += t1->flags_stride << 2;
971 colflags1 += t1->flags_stride;
973 for (i = 0; i < t1->w; ++i) {
974 opj_colflag_t *colflags2 = colflags1 + i;
975 for (j = k; j < t1->h; ++j) {
976 vsc = (j == t1->h - 1) ? 1 : 0;
977 opj_t1_dec_refpass_step_mqc_vsc(
979 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
981 &t1->data[(j * t1->w) + i],
987 } /* VSC and BYPASS by Antonin */
990 static void opj_t1_enc_clnpass_step(
1003 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1005 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
1009 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1010 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1011 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1012 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1015 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
1016 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1017 v = *datap < 0 ? 1 : 0;
1018 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1019 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1022 *flagsp &= ~T1_VISIT;
1025 static void opj_t1_dec_clnpass_step_partial(
1028 opj_colflag_t *colflagsp,
1030 OPJ_INT32 oneplushalf,
1034 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1037 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1038 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1039 *datap = v ? -oneplushalf : oneplushalf;
1040 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1041 #ifdef CONSISTENCY_CHECK
1042 *flagsp &= ~T1_VISIT;
1044 } /* VSC and BYPASS by Antonin */
1046 static void opj_t1_dec_clnpass_step(
1049 opj_colflag_t *colflagsp,
1051 OPJ_INT32 oneplushalf,
1056 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1057 #ifdef CONSISTENCY_CHECK
1058 assert( (!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) );
1060 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) {
1062 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1063 if (opj_mqc_decode(mqc)) {
1064 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1065 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1066 *datap = v ? -oneplushalf : oneplushalf;
1067 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1070 #ifdef CONSISTENCY_CHECK
1071 *flagsp &= ~T1_VISIT;
1073 } /* VSC and BYPASS by Antonin */
1075 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1078 opj_colflag_t *colflagsp,
1080 OPJ_INT32 oneplushalf,
1082 OPJ_INT32 flags_stride)
1087 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1090 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1092 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1093 if (opj_mqc_decode(mqc)) {
1094 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1095 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1096 *datap = v ? -oneplushalf : oneplushalf;
1097 opj_t1_updateflagscolflags(flagsp, colflagsp, v, flags_stride, row);
1100 /*flagsp &= ~T1_VISIT;*/
1103 static void opj_t1_dec_clnpass_step_vsc(
1106 opj_colflag_t *colflagsp,
1108 OPJ_INT32 oneplushalf,
1115 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1117 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1121 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
1122 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1123 if (opj_mqc_decode(mqc)) {
1125 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1126 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1127 *datap = v ? -oneplushalf : oneplushalf;
1128 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1131 #ifdef CONSISTENCY_CHECK
1132 *flagsp &= ~T1_VISIT;
1136 static void opj_t1_enc_clnpass(
1144 OPJ_UINT32 agg, runlen, vsc;
1146 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1149 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1150 for (k = 0; k < t1->h; k += 4) {
1151 for (i = 0; i < t1->w; ++i) {
1152 if (k + 3 < t1->h) {
1153 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1154 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1155 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1156 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1157 || (MACRO_t1_flags(1 + k + 3,1 + i)
1158 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1160 agg = !((MACRO_t1_flags(1 + k,1 + i) |
1161 MACRO_t1_flags(1 + k + 1,1 + i) |
1162 MACRO_t1_flags(1 + k + 2,1 + i) |
1163 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1169 for (runlen = 0; runlen < 4; ++runlen) {
1170 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1173 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1174 opj_mqc_encode(mqc, runlen != 4);
1178 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1179 opj_mqc_encode(mqc, runlen >> 1);
1180 opj_mqc_encode(mqc, runlen & 1);
1184 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1185 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1186 opj_t1_enc_clnpass_step(
1188 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1189 &t1->data[(j * t1->data_stride) + i],
1193 agg && (j == k + runlen),
1200 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1202 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1204 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1205 OPJ_UINT32 i, j, k; \
1206 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1208 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1212 oneplushalf = one | half; \
1213 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1214 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1215 for (k = 0; k < h; k += 4) { \
1216 for (i = 0; i < w; ++i) { \
1217 opj_colflag_t *colflags2 = colflags1 + i; \
1219 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1220 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1221 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1222 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1223 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1224 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1229 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1230 if (!opj_mqc_decode(mqc)) { \
1233 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1234 runlen = opj_mqc_decode(mqc); \
1235 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1239 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1240 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1241 opj_t1_dec_clnpass_step_vsc( \
1243 &t1->flags[((j+1) * flags_stride) + i + 1], \
1245 &t1->data[(j * w) + i], \
1247 agg && (j == k + (OPJ_UINT32)runlen), \
1250 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1252 colflags1 += flags_stride; \
1255 OPJ_INT32 *data1 = t1->data; \
1256 opj_flag_t *flags1 = &t1->flags[1]; \
1257 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1258 for (k = 0; k < (h & ~3u); k += 4) { \
1259 for (i = 0; i < w; ++i) { \
1260 OPJ_INT32 *data2 = data1 + i; \
1261 opj_flag_t *flags2 = flags1 + i; \
1262 opj_colflag_t *colflags2 = colflags1 + i; \
1263 opj_colflag_t colflags = *colflags2; \
1264 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1265 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1266 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1267 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1268 if( consistency_check ) { \
1269 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1270 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1271 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1272 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1275 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1276 if (!opj_mqc_decode(mqc)) { \
1279 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1280 runlen = opj_mqc_decode(mqc); \
1281 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1282 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1283 data2 += (OPJ_UINT32)runlen * w; \
1284 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1285 flags2 += flags_stride; \
1286 if (j == (OPJ_UINT32)runlen) { \
1287 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1289 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1294 flags2 += flags_stride; \
1295 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1296 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1297 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0, flags_stride); \
1299 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1301 flags2 += flags_stride; \
1302 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1303 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1304 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1, flags_stride); \
1306 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1308 flags2 += flags_stride; \
1309 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1310 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1311 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2, flags_stride); \
1313 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1315 flags2 += flags_stride; \
1316 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1317 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1318 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3, flags_stride); \
1320 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1323 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1326 flags1 += flags_stride << 2; \
1327 colflags1 += flags_stride; \
1329 for (i = 0; i < w; ++i) { \
1330 OPJ_INT32 *data2 = data1 + i; \
1331 opj_flag_t *flags2 = flags1 + i; \
1332 opj_colflag_t *colflags2 = colflags1 + i; \
1333 for (j = k; j < h; ++j) { \
1334 flags2 += flags_stride; \
1335 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1338 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1344 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1345 v = opj_mqc_decode(mqc); \
1346 v = (v << 1) | opj_mqc_decode(mqc); \
1347 v = (v << 1) | opj_mqc_decode(mqc); \
1348 v = (v << 1) | opj_mqc_decode(mqc); \
1351 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1355 } /* VSC and BYPASS by Antonin */
1357 static void opj_t1_dec_clnpass_64x64(
1362 #ifdef CONSISTENCY_CHECK
1363 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1365 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1369 static void opj_t1_dec_clnpass_generic(
1374 #ifdef CONSISTENCY_CHECK
1375 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1377 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1382 /** mod fixed_quality */
1383 static OPJ_FLOAT64 opj_t1_getwmsedec(
1390 OPJ_FLOAT64 stepsize,
1391 OPJ_UINT32 numcomps,
1392 const OPJ_FLOAT64 * mct_norms,
1393 OPJ_UINT32 mct_numcomps)
1395 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1396 OPJ_ARG_NOT_USED(numcomps);
1398 if (mct_norms && (compno < mct_numcomps)) {
1399 w1 = mct_norms[compno];
1403 w2 = opj_dwt_getnorm(level, orient);
1404 } else { /* if (qmfbid == 0) */
1405 w2 = opj_dwt_getnorm_real(level, orient);
1408 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1409 wmsedec *= wmsedec * nmsedec / 8192.0;
1414 static OPJ_BOOL opj_t1_allocate_buffers(
1419 OPJ_UINT32 datasize=w * h;
1420 OPJ_UINT32 flagssize;
1422 /* encoder uses tile buffer, so no need to allocate */
1424 if(datasize > t1->datasize){
1425 opj_aligned_free(t1->data);
1426 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1428 /* FIXME event manager error callback */
1431 t1->datasize=datasize;
1433 /* memset first arg is declared to never be null by gcc */
1434 if (t1->data != NULL) {
1435 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1438 t1->flags_stride=w+2;
1439 flagssize=t1->flags_stride * (h+2);
1441 if(flagssize > t1->flagssize){
1442 opj_aligned_free(t1->flags);
1443 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1445 /* FIXME event manager error callback */
1448 t1->flagssize=flagssize;
1450 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1453 OPJ_UINT32 colflags_size=t1->flags_stride * ((h+3) / 4 + 2);
1455 if(colflags_size > t1->colflags_size){
1456 opj_aligned_free(t1->colflags);
1457 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1459 /* FIXME event manager error callback */
1462 t1->colflags_size=colflags_size;
1464 memset(t1->colflags,0,colflags_size * sizeof(opj_colflag_t));
1473 /* ----------------------------------------------------------------------- */
1475 /* ----------------------------------------------------------------------- */
1477 * Creates a new Tier 1 handle
1478 * and initializes the look-up tables of the Tier-1 coder/decoder
1479 * @return a new T1 handle if successful, returns NULL otherwise
1481 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1483 opj_t1_t *l_t1 = 00;
1485 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1490 /* create MQC and RAW handles */
1491 l_t1->mqc = opj_mqc_create();
1493 opj_t1_destroy(l_t1);
1497 l_t1->raw = opj_raw_create();
1499 opj_t1_destroy(l_t1);
1502 l_t1->encoder = isEncoder;
1509 * Destroys a previously created T1 handle
1511 * @param p_t1 Tier 1 handle to destroy
1513 void opj_t1_destroy(opj_t1_t *p_t1)
1519 /* destroy MQC and RAW handles */
1520 opj_mqc_destroy(p_t1->mqc);
1522 opj_raw_destroy(p_t1->raw);
1525 /* encoder uses tile buffer, so no need to free */
1526 if (!p_t1->encoder && p_t1->data) {
1527 opj_aligned_free(p_t1->data);
1532 opj_aligned_free(p_t1->flags);
1536 if (p_t1->colflags) {
1537 opj_aligned_free(p_t1->colflags);
1538 p_t1->colflags = 00;
1546 opj_tcd_cblk_dec_t* cblk;
1547 opj_tcd_band_t* band;
1548 opj_tcd_tilecomp_t* tilec;
1550 volatile OPJ_BOOL* pret;
1551 } opj_t1_cblk_decode_processing_job_t;
1553 static void opj_t1_destroy_wrapper(void* t1)
1555 opj_t1_destroy( (opj_t1_t*) t1 );
1558 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1560 opj_tcd_cblk_dec_t* cblk;
1561 opj_tcd_band_t* band;
1562 opj_tcd_tilecomp_t* tilec;
1564 OPJ_INT32* restrict datap;
1565 OPJ_UINT32 cblk_w, cblk_h;
1568 opj_t1_cblk_decode_processing_job_t* job;
1573 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1579 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1587 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1590 t1 = opj_t1_create( OPJ_FALSE );
1591 opj_tls_set( tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper );
1594 if (OPJ_FALSE == opj_t1_decode_cblk(
1598 (OPJ_UINT32)tccp->roishift,
1600 *(job->pret) = OPJ_FALSE;
1605 x = cblk->x0 - band->x0;
1606 y = cblk->y0 - band->y0;
1607 if (band->bandno & 1) {
1608 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1609 x += pres->x1 - pres->x0;
1611 if (band->bandno & 2) {
1612 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1613 y += pres->y1 - pres->y0;
1620 if (tccp->roishift) {
1621 OPJ_INT32 thresh = 1 << tccp->roishift;
1622 for (j = 0; j < cblk_h; ++j) {
1623 for (i = 0; i < cblk_w; ++i) {
1624 OPJ_INT32 val = datap[(j * cblk_w) + i];
1625 OPJ_INT32 mag = abs(val);
1626 if (mag >= thresh) {
1627 mag >>= tccp->roishift;
1628 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1633 if (tccp->qmfbid == 1) {
1634 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1635 for (j = 0; j < cblk_h; ++j) {
1637 for (; i < (cblk_w & ~3); i += 4) {
1638 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i];
1639 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i+1];
1640 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i+2];
1641 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i+3];
1642 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp0/2;
1643 ((OPJ_INT32*)tiledp)[(j * tile_w) + i+1] = tmp1/2;
1644 ((OPJ_INT32*)tiledp)[(j * tile_w) + i+2] = tmp2/2;
1645 ((OPJ_INT32*)tiledp)[(j * tile_w) + i+3] = tmp3/2;
1647 for (; i < cblk_w; ++i) {
1648 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1649 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp/2;
1652 } else { /* if (tccp->qmfbid == 0) */
1653 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1654 for (j = 0; j < cblk_h; ++j) {
1655 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1656 for (i = 0; i < cblk_w; ++i) {
1657 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1670 void opj_t1_decode_cblks( opj_thread_pool_t* tp,
1671 volatile OPJ_BOOL* pret,
1672 opj_tcd_tilecomp_t* tilec,
1676 OPJ_UINT32 resno, bandno, precno, cblkno;
1678 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1679 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1681 for (bandno = 0; bandno < res->numbands; ++bandno) {
1682 opj_tcd_band_t* restrict band = &res->bands[bandno];
1684 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1685 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1687 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1688 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1689 opj_t1_cblk_decode_processing_job_t* job;
1691 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, sizeof(opj_t1_cblk_decode_processing_job_t));
1703 opj_thread_pool_submit_job( tp, opj_t1_clbl_decode_processor, job );
1715 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1716 opj_tcd_cblk_dec_t* cblk,
1718 OPJ_UINT32 roishift,
1721 opj_raw_t *raw = t1->raw; /* RAW component */
1722 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1724 OPJ_INT32 bpno_plus_one;
1725 OPJ_UINT32 passtype;
1726 OPJ_UINT32 segno, passno;
1727 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1729 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1731 if(!opj_t1_allocate_buffers(
1733 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1734 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1739 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1742 opj_mqc_resetstates(mqc);
1743 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1744 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1745 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1747 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1748 opj_tcd_seg_t *seg = &cblk->segs[segno];
1751 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1752 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1753 if(seg->data == 00){
1756 if (type == T1_TYPE_RAW) {
1757 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1759 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1764 if( t1->w == 64 && t1->h == 64 )
1766 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1769 if (type == T1_TYPE_RAW) {
1770 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1772 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1773 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1775 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1780 if (type == T1_TYPE_RAW) {
1781 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1783 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1784 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1786 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1791 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1795 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1796 opj_mqc_resetstates(mqc);
1797 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1798 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1799 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1801 if (++passtype == 3) {
1809 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1812 if (type == T1_TYPE_RAW) {
1813 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1815 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1816 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1818 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1823 if (type == T1_TYPE_RAW) {
1824 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1826 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1827 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1829 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1834 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1838 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1839 opj_mqc_resetstates(mqc);
1840 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1841 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1842 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1844 if (++passtype == 3) {
1857 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1858 opj_tcd_tile_t *tile,
1860 const OPJ_FLOAT64 * mct_norms,
1861 OPJ_UINT32 mct_numcomps
1864 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1866 tile->distotile = 0; /* fixed_quality */
1868 for (compno = 0; compno < tile->numcomps; ++compno) {
1869 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1870 opj_tccp_t* tccp = &tcp->tccps[compno];
1871 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1873 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1874 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1876 for (bandno = 0; bandno < res->numbands; ++bandno) {
1877 opj_tcd_band_t* restrict band = &res->bands[bandno];
1878 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1880 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1881 opj_tcd_precinct_t *prc = &band->precincts[precno];
1883 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1884 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1885 OPJ_INT32* restrict tiledp;
1888 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1890 OPJ_INT32 x = cblk->x0 - band->x0;
1891 OPJ_INT32 y = cblk->y0 - band->y0;
1892 if (band->bandno & 1) {
1893 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1894 x += pres->x1 - pres->x0;
1896 if (band->bandno & 2) {
1897 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1898 y += pres->y1 - pres->y0;
1901 if(!opj_t1_allocate_buffers(
1903 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1904 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1911 tileLineAdvance = tile_w - cblk_w;
1913 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1915 t1->data_stride = tile_w;
1916 if (tccp->qmfbid == 1) {
1917 for (j = 0; j < cblk_h; ++j) {
1918 for (i = 0; i < cblk_w; ++i) {
1919 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1922 tileIndex += tileLineAdvance;
1924 } else { /* if (tccp->qmfbid == 0) */
1925 for (j = 0; j < cblk_h; ++j) {
1926 for (i = 0; i < cblk_w; ++i) {
1927 OPJ_INT32 tmp = tiledp[tileIndex];
1934 tileIndex += tileLineAdvance;
1943 tilec->numresolutions - 1 - resno,
1960 /** mod fixed_quality */
1961 static void opj_t1_encode_cblk(opj_t1_t *t1,
1962 opj_tcd_cblk_enc_t* cblk,
1967 OPJ_FLOAT64 stepsize,
1969 OPJ_UINT32 numcomps,
1970 opj_tcd_tile_t * tile,
1971 const OPJ_FLOAT64 * mct_norms,
1972 OPJ_UINT32 mct_numcomps)
1974 OPJ_FLOAT64 cumwmsedec = 0.0;
1976 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1980 OPJ_UINT32 passtype;
1981 OPJ_INT32 nmsedec = 0;
1984 OPJ_BYTE type = T1_TYPE_MQ;
1985 OPJ_FLOAT64 tempwmsedec;
1987 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1990 for (i = 0; i < t1->w; ++i) {
1991 for (j = 0; j < t1->h; ++j) {
1992 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1993 max = opj_int_max(max, tmp);
1997 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1999 bpno = (OPJ_INT32)(cblk->numbps - 1);
2002 opj_mqc_resetstates(mqc);
2003 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2004 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2005 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2006 opj_mqc_init_enc(mqc, cblk->data);
2008 for (passno = 0; bpno >= 0; ++passno) {
2009 opj_tcd_pass_t *pass = &cblk->passes[passno];
2010 OPJ_UINT32 correction = 3;
2011 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2015 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2018 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2021 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2022 /* code switch SEGMARK (i.e. SEGSYM) */
2023 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2024 opj_mqc_segmark_enc(mqc);
2029 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
2030 cumwmsedec += tempwmsedec;
2031 tile->distotile += tempwmsedec;
2033 /* Code switch "RESTART" (i.e. TERMALL) */
2034 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2035 if (type == T1_TYPE_RAW) {
2038 /* correction = mqc_bypass_flush_enc(); */
2039 } else { /* correction = mqc_restart_enc(); */
2045 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2046 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2047 if (type == T1_TYPE_RAW) {
2050 /* correction = mqc_bypass_flush_enc(); */
2051 } else { /* correction = mqc_restart_enc(); */
2061 if (++passtype == 3) {
2066 if (pass->term && bpno > 0) {
2067 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2068 if (type == T1_TYPE_RAW)
2069 opj_mqc_bypass_init_enc(mqc);
2071 opj_mqc_restart_init_enc(mqc);
2074 pass->distortiondec = cumwmsedec;
2075 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2077 /* Code-switch "RESET" */
2078 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2079 opj_mqc_reset_enc(mqc);
2082 /* Code switch "ERTERM" (i.e. PTERM) */
2083 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2084 opj_mqc_erterm_enc(mqc);
2085 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2088 cblk->totalpasses = passno;
2090 for (passno = 0; passno<cblk->totalpasses; passno++) {
2091 opj_tcd_pass_t *pass = &cblk->passes[passno];
2092 if (pass->rate > opj_mqc_numbytes(mqc))
2093 pass->rate = opj_mqc_numbytes(mqc);
2094 /*Preventing generation of FF as last data byte of a pass*/
2095 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2098 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2103 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
2114 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2115 opj_raw_t *raw = t1->raw; /* RAW component */
2117 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2118 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2119 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2120 if (type == T1_TYPE_RAW) {
2121 v = opj_raw_decode(raw);
2123 v = opj_mqc_decode(mqc);
2125 t = v ? poshalf : neghalf;
2126 *datap += *datap < 0 ? -t : t;
2127 *flagsp |= T1_REFINE;
2129 } /* VSC and BYPASS by Antonin */
2135 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
2139 OPJ_INT32 oneplushalf,
2145 opj_raw_t *raw = t1->raw; /* RAW component */
2146 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2148 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2149 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2150 if (type == T1_TYPE_RAW) {
2151 if (opj_raw_decode(raw)) {
2152 v = opj_raw_decode(raw); /* ESSAI */
2153 *datap = v ? -oneplushalf : oneplushalf;
2154 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2157 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2158 if (opj_mqc_decode(mqc)) {
2159 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2160 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2161 *datap = v ? -oneplushalf : oneplushalf;
2162 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2165 *flagsp |= T1_VISIT;
2167 } /* VSC and BYPASS by Antonin */