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 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
100 opj_colflag_t* colflagsp,
102 OPJ_INT32 oneplushalf,
108 Encode significant pass
110 static void opj_t1_enc_sigpass( opj_t1_t *t1,
117 Decode significant pass
119 static void opj_t1_dec_sigpass_raw(
123 static void opj_t1_dec_sigpass_mqc_vsc(
130 Encode refinement pass
132 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
143 Encode refinement pass
145 static void opj_t1_enc_refpass( opj_t1_t *t1,
152 Decode refinement pass
154 static void opj_t1_dec_refpass_raw(
158 static void opj_t1_dec_refpass_mqc_vsc(
164 Decode refinement pass
167 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
176 static INLINE void opj_t1_dec_refpass_step_raw(
179 opj_colflag_t *colflagsp,
184 static INLINE void opj_t1_dec_refpass_step_mqc(
187 opj_colflag_t *colflagsp,
192 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
195 opj_colflag_t *colflagsp,
207 static void opj_t1_enc_clnpass_step(
219 static void opj_t1_dec_clnpass_step_partial(
222 opj_colflag_t *colflagsp,
224 OPJ_INT32 oneplushalf,
226 static void opj_t1_dec_clnpass_step(
229 opj_colflag_t *colflagsp,
231 OPJ_INT32 oneplushalf,
233 static void opj_t1_dec_clnpass_step_vsc(
236 opj_colflag_t *colflagsp,
238 OPJ_INT32 oneplushalf,
245 static void opj_t1_enc_clnpass(
253 static void opj_t1_dec_clnpass(
258 static OPJ_FLOAT64 opj_t1_getwmsedec(
265 OPJ_FLOAT64 stepsize,
267 const OPJ_FLOAT64 * mct_norms,
268 OPJ_UINT32 mct_numcomps);
270 static void opj_t1_encode_cblk( opj_t1_t *t1,
271 opj_tcd_cblk_enc_t* cblk,
276 OPJ_FLOAT64 stepsize,
279 opj_tcd_tile_t * tile,
280 const OPJ_FLOAT64 * mct_norms,
281 OPJ_UINT32 mct_numcomps);
286 @param cblk Code-block coding parameters
288 @param roishift Region of interest shifting value
289 @param cblksty Code-block style
291 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
292 opj_tcd_cblk_dec_t* cblk,
297 static OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
305 /* ----------------------------------------------------------------------- */
307 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f) {
308 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
311 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
312 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
315 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
316 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
317 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
321 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
322 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
325 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
327 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
330 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
333 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
335 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
338 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
341 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
342 opj_flag_t *np = flagsp - stride;
343 opj_flag_t *sp = flagsp + stride;
345 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
346 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
347 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
349 opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
352 np[0] |= flag_N << 2;
355 flagsp[-1] |= flag_N << 1;
357 flagsp[1] |= flag_N << 3;
364 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp, opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_INT32 row)
366 opj_t1_updateflags(flagsp, s, stride);
369 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
370 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
371 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
372 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
373 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
374 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
375 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
376 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
377 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
381 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
382 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
383 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
384 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
385 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
386 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS* (row-1)));
387 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
388 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
389 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
393 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
394 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
395 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
396 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
397 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
398 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
399 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
400 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
401 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
405 static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
418 opj_mqc_t *mqc = t1->mqc; /* MQC component */
420 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
421 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
422 v = (opj_int_abs(*datap) & one) ? 1 : 0;
423 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag)); /* ESSAI */
424 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
425 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
427 opj_mqc_encode(mqc, (OPJ_UINT32)v);
430 v = *datap < 0 ? 1 : 0;
431 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
432 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
433 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
434 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
436 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
438 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
445 static INLINE void opj_t1_dec_sigpass_step_raw(
448 opj_colflag_t* colflagsp,
450 OPJ_INT32 oneplushalf,
455 opj_raw_t *raw = t1->raw; /* RAW component */
457 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
458 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
459 if (opj_raw_decode(raw)) {
460 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
461 *datap = v ? -oneplushalf : oneplushalf;
462 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
464 #ifdef CONSISTENCY_CHECK
467 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
471 static INLINE void opj_t1_dec_sigpass_step_mqc(
474 opj_colflag_t* colflagsp,
476 OPJ_INT32 oneplushalf,
481 opj_mqc_t *mqc = t1->mqc; /* MQC component */
482 #ifdef CONSISTENCY_CHECK
483 assert( ((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
484 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
485 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) );
487 if( (*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
488 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) ) {
490 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
491 if (opj_mqc_decode(mqc)) {
492 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
493 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
494 *datap = v ? -oneplushalf : oneplushalf;
495 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
497 #ifdef CONSISTENCY_CHECK
500 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
502 } /* VSC and BYPASS by Antonin */
504 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
507 opj_colflag_t* colflagsp,
509 OPJ_INT32 oneplushalf,
515 opj_mqc_t *mqc = t1->mqc; /* MQC component */
517 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
518 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
519 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
520 if (opj_mqc_decode(mqc)) {
521 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
522 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
523 *datap = v ? -oneplushalf : oneplushalf;
524 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
526 #ifdef CONSISTENCY_CHECK
529 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
531 } /* VSC and BYPASS by Antonin */
535 static void opj_t1_enc_sigpass(opj_t1_t *t1,
542 OPJ_UINT32 i, j, k, vsc;
546 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
547 for (k = 0; k < t1->h; k += 4) {
548 for (i = 0; i < t1->w; ++i) {
549 for (j = k; j < k + 4 && j < t1->h; ++j) {
550 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
551 opj_t1_enc_sigpass_step(
553 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
554 &t1->data[(j * t1->data_stride) + i],
565 static void opj_t1_dec_sigpass_raw(
570 OPJ_INT32 one, half, oneplushalf, vsc;
572 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
575 oneplushalf = one | half;
576 for (k = 0; k < t1->h; k += 4) {
577 for (i = 0; i < t1->w; ++i) {
578 opj_colflag_t *colflags2 = colflags1 + i;
579 for (j = k; j < k + 4 && j < t1->h; ++j) {
580 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
581 opj_t1_dec_sigpass_step_raw(
583 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
585 &t1->data[(j * t1->w) + i],
591 colflags1 += t1->flags_stride;
593 } /* VSC and BYPASS by Antonin */
595 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
597 OPJ_INT32 one, half, oneplushalf; \
598 OPJ_UINT32 i, j, k; \
599 OPJ_INT32 *data1 = t1->data; \
600 opj_flag_t *flags1 = &t1->flags[1]; \
601 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
604 oneplushalf = one | half; \
605 for (k = 0; k < (h & ~3u); k += 4) { \
606 for (i = 0; i < w; ++i) { \
607 OPJ_INT32 *data2 = data1 + i; \
608 opj_flag_t *flags2 = flags1 + i; \
609 opj_colflag_t *colflags2 = colflags1 + i; \
610 if( *colflags2 == 0 ) continue; \
611 flags2 += flags_stride; \
612 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0); \
614 flags2 += flags_stride; \
615 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1); \
617 flags2 += flags_stride; \
618 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2); \
620 flags2 += flags_stride; \
621 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3); \
625 flags1 += flags_stride << 2; \
626 colflags1 += flags_stride; \
628 for (i = 0; i < w; ++i) { \
629 OPJ_INT32 *data2 = data1 + i; \
630 opj_flag_t *flags2 = flags1 + i; \
631 opj_colflag_t *colflags2 = colflags1 + i; \
632 for (j = k; j < h; ++j) { \
633 flags2 += flags_stride; \
634 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k); \
640 static void opj_t1_dec_sigpass_mqc_64x64(
644 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
647 static void opj_t1_dec_sigpass_mqc_generic(
651 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
654 /* VSC and BYPASS by Antonin */
655 static void opj_t1_dec_sigpass_mqc_vsc(
659 OPJ_INT32 one, half, oneplushalf, vsc;
661 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
664 oneplushalf = one | half;
665 for (k = 0; k < t1->h; k += 4) {
666 for (i = 0; i < t1->w; ++i) {
667 opj_colflag_t *colflags2 = colflags1 + i;
668 for (j = k; j < k + 4 && j < t1->h; ++j) {
669 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
670 opj_t1_dec_sigpass_step_mqc_vsc(
672 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
674 &t1->data[(j * t1->w) + i],
680 colflags1 += t1->flags_stride;
682 } /* VSC and BYPASS by Antonin */
686 static void opj_t1_enc_refpass_step( opj_t1_t *t1,
698 opj_mqc_t *mqc = t1->mqc; /* MQC component */
700 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
701 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
702 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
703 v = (opj_int_abs(*datap) & one) ? 1 : 0;
704 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
705 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
706 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
708 opj_mqc_encode(mqc, (OPJ_UINT32)v);
710 *flagsp |= T1_REFINE;
714 static INLINE void opj_t1_dec_refpass_step_raw(
717 opj_colflag_t *colflagsp,
725 opj_raw_t *raw = t1->raw; /* RAW component */
727 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
728 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
729 v = (OPJ_INT32)opj_raw_decode(raw);
730 t = v ? poshalf : neghalf;
731 *datap += *datap < 0 ? -t : t;
732 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
734 } /* VSC and BYPASS by Antonin */
736 static INLINE void opj_t1_dec_refpass_step_mqc(
738 #ifdef CONSISTENCY_CHECK
741 opj_flag_t *flagsp_unused,
743 opj_colflag_t *colflagsp,
751 opj_mqc_t *mqc = t1->mqc; /* MQC component */
752 #ifdef CONSISTENCY_CHECK
753 assert( ((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
754 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) );
756 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
757 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
758 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
759 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
760 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
761 v = opj_mqc_decode(mqc);
762 t = v ? poshalf : neghalf;
763 *datap += *datap < 0 ? -t : t;
764 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
766 } /* VSC and BYPASS by Antonin */
768 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
771 opj_colflag_t *colflagsp,
778 OPJ_INT32 v, t, flag;
780 opj_mqc_t *mqc = t1->mqc; /* MQC component */
782 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
783 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
784 OPJ_INT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
785 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
786 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
787 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
788 v = opj_mqc_decode(mqc);
789 t = v ? poshalf : neghalf;
790 *datap += *datap < 0 ? -t : t;
791 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
793 } /* VSC and BYPASS by Antonin */
796 static void opj_t1_enc_refpass(
803 OPJ_UINT32 i, j, k, vsc;
807 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
808 for (k = 0; k < t1->h; k += 4) {
809 for (i = 0; i < t1->w; ++i) {
810 for (j = k; j < k + 4 && j < t1->h; ++j) {
811 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
812 opj_t1_enc_refpass_step(
814 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
815 &t1->data[(j * t1->data_stride) + i],
826 static void opj_t1_dec_refpass_raw(
831 OPJ_INT32 one, poshalf, neghalf;
834 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
837 neghalf = bpno > 0 ? -poshalf : -1;
838 for (k = 0; k < t1->h; k += 4) {
839 for (i = 0; i < t1->w; ++i) {
840 opj_colflag_t *colflags2 = colflags1 + i;
841 for (j = k; j < k + 4 && j < t1->h; ++j) {
842 opj_t1_dec_refpass_step_raw(
844 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
846 &t1->data[(j * t1->w) + i],
851 colflags1 += t1->flags_stride;
853 } /* VSC and BYPASS by Antonin */
855 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
857 OPJ_INT32 one, poshalf, neghalf; \
858 OPJ_UINT32 i, j, k; \
859 OPJ_INT32 *data1 = t1->data; \
860 opj_flag_t *flags1 = &t1->flags[1]; \
861 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
863 poshalf = one >> 1; \
864 neghalf = bpno > 0 ? -poshalf : -1; \
865 for (k = 0; k < (h & ~3u); k += 4) { \
866 for (i = 0; i < w; ++i) { \
867 OPJ_INT32 *data2 = data1 + i; \
868 opj_flag_t *flags2 = flags1 + i; \
869 opj_colflag_t *colflags2 = colflags1 + i; \
870 if( *colflags2 == 0 ) continue; \
871 flags2 += flags_stride; \
872 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0); \
874 flags2 += flags_stride; \
875 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1); \
877 flags2 += flags_stride; \
878 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2); \
880 flags2 += flags_stride; \
881 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3); \
885 flags1 += flags_stride << 2; \
886 colflags1 += flags_stride; \
888 for (i = 0; i < w; ++i) { \
889 OPJ_INT32 *data2 = data1 + i; \
890 opj_flag_t *flags2 = flags1 + i; \
891 opj_colflag_t *colflags2 = colflags1 + i; \
892 for (j = k; j < h; ++j) { \
893 flags2 += flags_stride; \
894 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
900 static void opj_t1_dec_refpass_mqc_64x64(
904 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
907 static void opj_t1_dec_refpass_mqc_generic(
911 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
914 /* VSC and BYPASS by Antonin */
915 static void opj_t1_dec_refpass_mqc_vsc(
919 OPJ_INT32 one, poshalf, neghalf;
922 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
925 neghalf = bpno > 0 ? -poshalf : -1;
926 for (k = 0; k < t1->h; k += 4) {
927 for (i = 0; i < t1->w; ++i) {
928 opj_colflag_t *colflags2 = colflags1 + i;
929 for (j = k; j < k + 4 && j < t1->h; ++j) {
930 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
931 opj_t1_dec_refpass_step_mqc_vsc(
933 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
935 &t1->data[(j * t1->w) + i],
941 colflags1 += t1->flags_stride;
943 } /* VSC and BYPASS by Antonin */
946 static void opj_t1_enc_clnpass_step(
959 opj_mqc_t *mqc = t1->mqc; /* MQC component */
961 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
965 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
966 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
967 v = (opj_int_abs(*datap) & one) ? 1 : 0;
968 opj_mqc_encode(mqc, (OPJ_UINT32)v);
971 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
972 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
973 v = *datap < 0 ? 1 : 0;
974 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
975 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
978 *flagsp &= ~T1_VISIT;
981 static void opj_t1_dec_clnpass_step_partial(
984 opj_colflag_t *colflagsp,
986 OPJ_INT32 oneplushalf,
990 opj_mqc_t *mqc = t1->mqc; /* MQC component */
993 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
994 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
995 *datap = v ? -oneplushalf : oneplushalf;
996 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
997 #ifdef CONSISTENCY_CHECK
998 *flagsp &= ~T1_VISIT;
1000 } /* VSC and BYPASS by Antonin */
1002 static void opj_t1_dec_clnpass_step(
1005 opj_colflag_t *colflagsp,
1007 OPJ_INT32 oneplushalf,
1012 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1013 #ifdef CONSISTENCY_CHECK
1014 assert( (!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) );
1016 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) {
1018 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1019 if (opj_mqc_decode(mqc)) {
1020 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1021 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1022 *datap = v ? -oneplushalf : oneplushalf;
1023 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1026 #ifdef CONSISTENCY_CHECK
1027 *flagsp &= ~T1_VISIT;
1029 } /* VSC and BYPASS by Antonin */
1031 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1034 opj_colflag_t *colflagsp,
1036 OPJ_INT32 oneplushalf,
1042 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1045 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1047 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1048 if (opj_mqc_decode(mqc)) {
1049 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1050 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1051 *datap = v ? -oneplushalf : oneplushalf;
1052 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1055 /*flagsp &= ~T1_VISIT;*/
1058 static void opj_t1_dec_clnpass_step_vsc(
1061 opj_colflag_t *colflagsp,
1063 OPJ_INT32 oneplushalf,
1070 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1072 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1076 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
1077 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1078 if (opj_mqc_decode(mqc)) {
1080 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1081 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1082 *datap = v ? -oneplushalf : oneplushalf;
1083 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1086 #ifdef CONSISTENCY_CHECK
1087 *flagsp &= ~T1_VISIT;
1091 static void opj_t1_enc_clnpass(
1099 OPJ_UINT32 agg, runlen, vsc;
1101 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1104 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1105 for (k = 0; k < t1->h; k += 4) {
1106 for (i = 0; i < t1->w; ++i) {
1107 if (k + 3 < t1->h) {
1108 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1109 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1110 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1111 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1112 || (MACRO_t1_flags(1 + k + 3,1 + i)
1113 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1115 agg = !((MACRO_t1_flags(1 + k,1 + i) |
1116 MACRO_t1_flags(1 + k + 1,1 + i) |
1117 MACRO_t1_flags(1 + k + 2,1 + i) |
1118 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1124 for (runlen = 0; runlen < 4; ++runlen) {
1125 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1128 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1129 opj_mqc_encode(mqc, runlen != 4);
1133 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1134 opj_mqc_encode(mqc, runlen >> 1);
1135 opj_mqc_encode(mqc, runlen & 1);
1139 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1140 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1141 opj_t1_enc_clnpass_step(
1143 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1144 &t1->data[(j * t1->data_stride) + i],
1148 agg && (j == k + runlen),
1155 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1157 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1159 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1160 OPJ_UINT32 i, j, k; \
1161 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1163 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1167 oneplushalf = one | half; \
1168 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1169 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1170 for (k = 0; k < h; k += 4) { \
1171 for (i = 0; i < w; ++i) { \
1172 opj_colflag_t *colflags2 = colflags1 + i; \
1174 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1175 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1176 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1177 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1178 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1179 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1184 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1185 if (!opj_mqc_decode(mqc)) { \
1188 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1189 runlen = opj_mqc_decode(mqc); \
1190 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1194 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1195 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1196 opj_t1_dec_clnpass_step_vsc( \
1198 &t1->flags[((j+1) * flags_stride) + i + 1], \
1200 &t1->data[(j * w) + i], \
1202 agg && (j == k + (OPJ_UINT32)runlen), \
1205 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1207 colflags1 += flags_stride; \
1210 OPJ_INT32 *data1 = t1->data; \
1211 opj_flag_t *flags1 = &t1->flags[1]; \
1212 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1213 for (k = 0; k < (h & ~3u); k += 4) { \
1214 for (i = 0; i < w; ++i) { \
1215 OPJ_INT32 *data2 = data1 + i; \
1216 opj_flag_t *flags2 = flags1 + i; \
1217 opj_colflag_t *colflags2 = colflags1 + i; \
1218 opj_colflag_t colflags = *colflags2; \
1219 agg = !(colflags & (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 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1223 if( consistency_check ) { \
1224 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1225 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1226 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1227 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1230 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1231 if (!opj_mqc_decode(mqc)) { \
1234 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1235 runlen = opj_mqc_decode(mqc); \
1236 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1237 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1238 data2 += (OPJ_UINT32)runlen * w; \
1239 for (j = (OPJ_UINT32)runlen; j < 4 && j < h; ++j) { \
1240 flags2 += flags_stride; \
1241 if (j == (OPJ_UINT32)runlen) { \
1242 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1244 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1249 flags2 += flags_stride; \
1250 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1251 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1252 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0); \
1254 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1256 flags2 += flags_stride; \
1257 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1258 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1259 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1); \
1261 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1263 flags2 += flags_stride; \
1264 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1265 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1266 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2); \
1268 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1270 flags2 += flags_stride; \
1271 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1272 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1273 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3); \
1275 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1278 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1281 flags1 += flags_stride << 2; \
1282 colflags1 += flags_stride; \
1284 for (i = 0; i < w; ++i) { \
1285 OPJ_INT32 *data2 = data1 + i; \
1286 opj_flag_t *flags2 = flags1 + i; \
1287 opj_colflag_t *colflags2 = colflags1 + i; \
1288 for (j = k; j < h; ++j) { \
1289 flags2 += flags_stride; \
1290 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1293 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1299 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1300 v = opj_mqc_decode(mqc); \
1301 v = (v << 1) | opj_mqc_decode(mqc); \
1302 v = (v << 1) | opj_mqc_decode(mqc); \
1303 v = (v << 1) | opj_mqc_decode(mqc); \
1306 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1310 } /* VSC and BYPASS by Antonin */
1312 static void opj_t1_dec_clnpass_64x64(
1317 #ifdef CONSISTENCY_CHECK
1318 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1320 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1324 static void opj_t1_dec_clnpass_generic(
1329 #ifdef CONSISTENCY_CHECK
1330 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1332 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1337 /** mod fixed_quality */
1338 static OPJ_FLOAT64 opj_t1_getwmsedec(
1345 OPJ_FLOAT64 stepsize,
1346 OPJ_UINT32 numcomps,
1347 const OPJ_FLOAT64 * mct_norms,
1348 OPJ_UINT32 mct_numcomps)
1350 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1351 OPJ_ARG_NOT_USED(numcomps);
1353 if (mct_norms && (compno < mct_numcomps)) {
1354 w1 = mct_norms[compno];
1358 w2 = opj_dwt_getnorm(level, orient);
1359 } else { /* if (qmfbid == 0) */
1360 w2 = opj_dwt_getnorm_real(level, orient);
1363 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1364 wmsedec *= wmsedec * nmsedec / 8192.0;
1369 static OPJ_BOOL opj_t1_allocate_buffers(
1374 OPJ_UINT32 datasize=w * h;
1375 OPJ_UINT32 flagssize;
1377 /* encoder uses tile buffer, so no need to allocate */
1379 if(datasize > t1->datasize){
1380 opj_aligned_free(t1->data);
1381 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1383 /* FIXME event manager error callback */
1386 t1->datasize=datasize;
1388 /* memset first arg is declared to never be null by gcc */
1389 if (t1->data != NULL) {
1390 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1393 t1->flags_stride=w+2;
1394 flagssize=t1->flags_stride * (h+2);
1396 if(flagssize > t1->flagssize){
1397 opj_aligned_free(t1->flags);
1398 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1400 /* FIXME event manager error callback */
1403 t1->flagssize=flagssize;
1405 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1408 OPJ_UINT32 colflags_size=t1->flags_stride * ((h+3) / 4 + 2);
1410 if(colflags_size > t1->colflags_size){
1411 opj_aligned_free(t1->colflags);
1412 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1414 /* FIXME event manager error callback */
1417 t1->colflags_size=colflags_size;
1419 memset(t1->colflags,0,colflags_size * sizeof(opj_colflag_t));
1428 /* ----------------------------------------------------------------------- */
1430 /* ----------------------------------------------------------------------- */
1432 * Creates a new Tier 1 handle
1433 * and initializes the look-up tables of the Tier-1 coder/decoder
1434 * @return a new T1 handle if successful, returns NULL otherwise
1436 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1438 opj_t1_t *l_t1 = 00;
1440 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1445 /* create MQC and RAW handles */
1446 l_t1->mqc = opj_mqc_create();
1448 opj_t1_destroy(l_t1);
1452 l_t1->raw = opj_raw_create();
1454 opj_t1_destroy(l_t1);
1457 l_t1->encoder = isEncoder;
1464 * Destroys a previously created T1 handle
1466 * @param p_t1 Tier 1 handle to destroy
1468 void opj_t1_destroy(opj_t1_t *p_t1)
1474 /* destroy MQC and RAW handles */
1475 opj_mqc_destroy(p_t1->mqc);
1477 opj_raw_destroy(p_t1->raw);
1480 /* encoder uses tile buffer, so no need to free */
1481 if (!p_t1->encoder && p_t1->data) {
1482 opj_aligned_free(p_t1->data);
1487 opj_aligned_free(p_t1->flags);
1491 if (p_t1->colflags) {
1492 opj_aligned_free(p_t1->colflags);
1493 p_t1->colflags = 00;
1498 OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
1499 opj_tcd_tilecomp_t* tilec,
1503 OPJ_UINT32 resno, bandno, precno, cblkno;
1504 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1506 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1507 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1509 for (bandno = 0; bandno < res->numbands; ++bandno) {
1510 opj_tcd_band_t* restrict band = &res->bands[bandno];
1512 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1513 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1515 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1516 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1517 OPJ_INT32* restrict datap;
1518 OPJ_UINT32 cblk_w, cblk_h;
1522 if (OPJ_FALSE == opj_t1_decode_cblk(
1526 (OPJ_UINT32)tccp->roishift,
1531 x = cblk->x0 - band->x0;
1532 y = cblk->y0 - band->y0;
1533 if (band->bandno & 1) {
1534 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1535 x += pres->x1 - pres->x0;
1537 if (band->bandno & 2) {
1538 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1539 y += pres->y1 - pres->y0;
1546 if (tccp->roishift) {
1547 OPJ_INT32 thresh = 1 << tccp->roishift;
1548 for (j = 0; j < cblk_h; ++j) {
1549 for (i = 0; i < cblk_w; ++i) {
1550 OPJ_INT32 val = datap[(j * cblk_w) + i];
1551 OPJ_INT32 mag = abs(val);
1552 if (mag >= thresh) {
1553 mag >>= tccp->roishift;
1554 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1559 if (tccp->qmfbid == 1) {
1560 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1561 for (j = 0; j < cblk_h; ++j) {
1562 for (i = 0; i < cblk_w; ++i) {
1563 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1564 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp/2;
1567 } else { /* if (tccp->qmfbid == 0) */
1568 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1569 for (j = 0; j < cblk_h; ++j) {
1570 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1571 for (i = 0; i < cblk_w; ++i) {
1572 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1588 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1589 opj_tcd_cblk_dec_t* cblk,
1591 OPJ_UINT32 roishift,
1594 opj_raw_t *raw = t1->raw; /* RAW component */
1595 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1597 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1599 OPJ_INT32 bpno_plus_one;
1600 OPJ_UINT32 passtype;
1601 OPJ_UINT32 segno, passno;
1602 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1604 if(!opj_t1_allocate_buffers(
1606 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1607 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1612 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1615 opj_mqc_resetstates(mqc);
1616 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1617 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1618 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1620 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1621 opj_tcd_seg_t *seg = &cblk->segs[segno];
1624 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1625 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1626 if(seg->data == 00){
1629 if (type == T1_TYPE_RAW) {
1630 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1632 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1637 if( t1->w == 64 && t1->h == 64 )
1639 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1642 if (type == T1_TYPE_RAW) {
1643 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1645 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1646 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1648 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1653 if (type == T1_TYPE_RAW) {
1654 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1656 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1657 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1659 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1664 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1668 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1669 opj_mqc_resetstates(mqc);
1670 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1671 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1672 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1674 if (++passtype == 3) {
1682 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1685 if (type == T1_TYPE_RAW) {
1686 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1688 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1689 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1691 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1696 if (type == T1_TYPE_RAW) {
1697 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1699 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1700 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1702 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1707 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1711 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1712 opj_mqc_resetstates(mqc);
1713 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1714 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1715 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1717 if (++passtype == 3) {
1730 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1731 opj_tcd_tile_t *tile,
1733 const OPJ_FLOAT64 * mct_norms,
1734 OPJ_UINT32 mct_numcomps
1737 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1739 tile->distotile = 0; /* fixed_quality */
1741 for (compno = 0; compno < tile->numcomps; ++compno) {
1742 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1743 opj_tccp_t* tccp = &tcp->tccps[compno];
1744 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1746 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1747 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1749 for (bandno = 0; bandno < res->numbands; ++bandno) {
1750 opj_tcd_band_t* restrict band = &res->bands[bandno];
1751 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1753 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1754 opj_tcd_precinct_t *prc = &band->precincts[precno];
1756 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1757 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1758 OPJ_INT32* restrict tiledp;
1761 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1763 OPJ_INT32 x = cblk->x0 - band->x0;
1764 OPJ_INT32 y = cblk->y0 - band->y0;
1765 if (band->bandno & 1) {
1766 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1767 x += pres->x1 - pres->x0;
1769 if (band->bandno & 2) {
1770 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1771 y += pres->y1 - pres->y0;
1774 if(!opj_t1_allocate_buffers(
1776 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1777 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1784 tileLineAdvance = tile_w - cblk_w;
1786 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1788 t1->data_stride = tile_w;
1789 if (tccp->qmfbid == 1) {
1790 for (j = 0; j < cblk_h; ++j) {
1791 for (i = 0; i < cblk_w; ++i) {
1792 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1795 tileIndex += tileLineAdvance;
1797 } else { /* if (tccp->qmfbid == 0) */
1798 for (j = 0; j < cblk_h; ++j) {
1799 for (i = 0; i < cblk_w; ++i) {
1800 OPJ_INT32 tmp = tiledp[tileIndex];
1807 tileIndex += tileLineAdvance;
1816 tilec->numresolutions - 1 - resno,
1833 /** mod fixed_quality */
1834 static void opj_t1_encode_cblk(opj_t1_t *t1,
1835 opj_tcd_cblk_enc_t* cblk,
1840 OPJ_FLOAT64 stepsize,
1842 OPJ_UINT32 numcomps,
1843 opj_tcd_tile_t * tile,
1844 const OPJ_FLOAT64 * mct_norms,
1845 OPJ_UINT32 mct_numcomps)
1847 OPJ_FLOAT64 cumwmsedec = 0.0;
1849 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1850 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1854 OPJ_UINT32 passtype;
1855 OPJ_INT32 nmsedec = 0;
1858 OPJ_BYTE type = T1_TYPE_MQ;
1859 OPJ_FLOAT64 tempwmsedec;
1862 for (i = 0; i < t1->w; ++i) {
1863 for (j = 0; j < t1->h; ++j) {
1864 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1865 max = opj_int_max(max, tmp);
1869 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1871 bpno = (OPJ_INT32)(cblk->numbps - 1);
1874 opj_mqc_resetstates(mqc);
1875 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1876 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1877 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1878 opj_mqc_init_enc(mqc, cblk->data);
1880 for (passno = 0; bpno >= 0; ++passno) {
1881 opj_tcd_pass_t *pass = &cblk->passes[passno];
1882 OPJ_UINT32 correction = 3;
1883 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1887 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
1890 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1893 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
1894 /* code switch SEGMARK (i.e. SEGSYM) */
1895 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1896 opj_mqc_segmark_enc(mqc);
1901 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1902 cumwmsedec += tempwmsedec;
1903 tile->distotile += tempwmsedec;
1905 /* Code switch "RESTART" (i.e. TERMALL) */
1906 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1907 if (type == T1_TYPE_RAW) {
1910 /* correction = mqc_bypass_flush_enc(); */
1911 } else { /* correction = mqc_restart_enc(); */
1917 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1918 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1919 if (type == T1_TYPE_RAW) {
1922 /* correction = mqc_bypass_flush_enc(); */
1923 } else { /* correction = mqc_restart_enc(); */
1933 if (++passtype == 3) {
1938 if (pass->term && bpno > 0) {
1939 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1940 if (type == T1_TYPE_RAW)
1941 opj_mqc_bypass_init_enc(mqc);
1943 opj_mqc_restart_init_enc(mqc);
1946 pass->distortiondec = cumwmsedec;
1947 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1949 /* Code-switch "RESET" */
1950 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1951 opj_mqc_reset_enc(mqc);
1954 /* Code switch "ERTERM" (i.e. PTERM) */
1955 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1956 opj_mqc_erterm_enc(mqc);
1957 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1960 cblk->totalpasses = passno;
1962 for (passno = 0; passno<cblk->totalpasses; passno++) {
1963 opj_tcd_pass_t *pass = &cblk->passes[passno];
1964 if (pass->rate > opj_mqc_numbytes(mqc))
1965 pass->rate = opj_mqc_numbytes(mqc);
1966 /*Preventing generation of FF as last data byte of a pass*/
1967 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1970 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1975 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
1986 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1987 opj_raw_t *raw = t1->raw; /* RAW component */
1989 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1990 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1991 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1992 if (type == T1_TYPE_RAW) {
1993 v = opj_raw_decode(raw);
1995 v = opj_mqc_decode(mqc);
1997 t = v ? poshalf : neghalf;
1998 *datap += *datap < 0 ? -t : t;
1999 *flagsp |= T1_REFINE;
2001 } /* VSC and BYPASS by Antonin */
2007 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
2011 OPJ_INT32 oneplushalf,
2017 opj_raw_t *raw = t1->raw; /* RAW component */
2018 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2020 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2021 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2022 if (type == T1_TYPE_RAW) {
2023 if (opj_raw_decode(raw)) {
2024 v = opj_raw_decode(raw); /* ESSAI */
2025 *datap = v ? -oneplushalf : oneplushalf;
2026 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2029 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2030 if (opj_mqc_decode(mqc)) {
2031 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2032 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2033 *datap = v ? -oneplushalf : oneplushalf;
2034 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2037 *flagsp |= T1_VISIT;
2039 } /* VSC and BYPASS by Antonin */