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_UINT32 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(
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(
178 opj_colflag_t *colflagsp,
183 static INLINE void opj_t1_dec_refpass_step_mqc(
186 opj_colflag_t *colflagsp,
191 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
194 opj_colflag_t *colflagsp,
206 static void opj_t1_enc_clnpass_step(
218 static void opj_t1_dec_clnpass_step_partial(
221 opj_colflag_t *colflagsp,
223 OPJ_INT32 oneplushalf,
225 static void opj_t1_dec_clnpass_step(
228 opj_colflag_t *colflagsp,
230 OPJ_INT32 oneplushalf,
232 static void opj_t1_dec_clnpass_step_vsc(
235 opj_colflag_t *colflagsp,
237 OPJ_INT32 oneplushalf,
244 static void opj_t1_enc_clnpass(
250 static OPJ_FLOAT64 opj_t1_getwmsedec(
257 OPJ_FLOAT64 stepsize,
259 const OPJ_FLOAT64 * mct_norms,
260 OPJ_UINT32 mct_numcomps);
262 static void opj_t1_encode_cblk( opj_t1_t *t1,
263 opj_tcd_cblk_enc_t* cblk,
268 OPJ_FLOAT64 stepsize,
271 opj_tcd_tile_t * tile,
272 const OPJ_FLOAT64 * mct_norms,
273 OPJ_UINT32 mct_numcomps);
278 @param cblk Code-block coding parameters
280 @param roishift Region of interest shifting value
281 @param cblksty Code-block style
283 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
284 opj_tcd_cblk_dec_t* cblk,
289 static OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
297 /* ----------------------------------------------------------------------- */
299 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f) {
300 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
303 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
304 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
307 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
308 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
309 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
313 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
314 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
317 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
319 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
322 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
325 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
327 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
330 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
333 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
334 opj_flag_t *np = flagsp - stride;
335 opj_flag_t *sp = flagsp + stride;
337 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
338 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
339 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
341 opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
344 np[0] |= flag_N << 2;
347 flagsp[-1] |= flag_N << 1;
349 flagsp[1] |= flag_N << 3;
356 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp, opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
358 opj_t1_updateflags(flagsp, s, stride);
361 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
362 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
363 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
364 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
365 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
366 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
367 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
368 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
369 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
373 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
374 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
375 *(colflagsp - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
376 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1)));
377 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
378 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS* (row-1)));
379 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
380 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
381 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
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 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
388 *colflagsp |= (T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
389 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
390 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
391 *(colflagsp + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
392 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1))) |
393 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1)));
397 static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
410 opj_mqc_t *mqc = t1->mqc; /* MQC component */
412 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
413 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
414 v = (opj_int_abs(*datap) & one) ? 1 : 0;
415 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag)); /* ESSAI */
416 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
417 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
419 opj_mqc_encode(mqc, (OPJ_UINT32)v);
422 v = *datap < 0 ? 1 : 0;
423 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
424 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(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 ^ opj_t1_getspb((OPJ_UINT32)flag)));
430 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
437 static INLINE void opj_t1_dec_sigpass_step_raw(
440 opj_colflag_t* colflagsp,
442 OPJ_INT32 oneplushalf,
447 opj_raw_t *raw = t1->raw; /* RAW component */
449 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
450 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
451 if (opj_raw_decode(raw)) {
452 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
453 *datap = v ? -oneplushalf : oneplushalf;
454 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
456 #ifdef CONSISTENCY_CHECK
459 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
463 static INLINE void opj_t1_dec_sigpass_step_mqc(
466 opj_colflag_t* colflagsp,
468 OPJ_INT32 oneplushalf,
470 OPJ_UINT32 flags_stride)
474 opj_mqc_t *mqc = t1->mqc; /* MQC component */
475 #ifdef CONSISTENCY_CHECK
476 assert( ((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
477 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
478 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) );
480 if( (*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
481 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) ) {
483 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
484 if (opj_mqc_decode(mqc)) {
485 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
486 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
487 *datap = v ? -oneplushalf : oneplushalf;
488 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
490 #ifdef CONSISTENCY_CHECK
493 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
495 } /* VSC and BYPASS by Antonin */
497 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
500 opj_colflag_t* colflagsp,
502 OPJ_INT32 oneplushalf,
508 opj_mqc_t *mqc = t1->mqc; /* MQC component */
510 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
511 if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
512 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
513 if (opj_mqc_decode(mqc)) {
514 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
515 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
516 *datap = v ? -oneplushalf : oneplushalf;
517 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
519 #ifdef CONSISTENCY_CHECK
522 *colflagsp |= (T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
524 } /* VSC and BYPASS by Antonin */
528 static void opj_t1_enc_sigpass(opj_t1_t *t1,
535 OPJ_UINT32 i, j, k, vsc;
539 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
540 for (k = 0; k < t1->h; k += 4) {
541 for (i = 0; i < t1->w; ++i) {
542 for (j = k; j < k + 4 && j < t1->h; ++j) {
543 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
544 opj_t1_enc_sigpass_step(
546 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
547 &t1->data[(j * t1->data_stride) + i],
558 static void opj_t1_dec_sigpass_raw(
563 OPJ_INT32 one, half, oneplushalf, vsc;
565 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
568 oneplushalf = one | half;
569 for (k = 0; k < t1->h; k += 4) {
570 for (i = 0; i < t1->w; ++i) {
571 opj_colflag_t *colflags2 = colflags1 + i;
572 for (j = k; j < k + 4 && j < t1->h; ++j) {
573 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
574 opj_t1_dec_sigpass_step_raw(
576 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
578 &t1->data[(j * t1->w) + i],
584 colflags1 += t1->flags_stride;
586 } /* VSC and BYPASS by Antonin */
588 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
590 OPJ_INT32 one, half, oneplushalf; \
591 OPJ_UINT32 i, j, k; \
592 OPJ_INT32 *data1 = t1->data; \
593 opj_flag_t *flags1 = &t1->flags[1]; \
594 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
597 oneplushalf = one | half; \
598 for (k = 0; k < (h & ~3u); k += 4) { \
599 for (i = 0; i < w; ++i) { \
600 OPJ_INT32 *data2 = data1 + i; \
601 opj_flag_t *flags2 = flags1 + i; \
602 opj_colflag_t *colflags2 = colflags1 + i; \
603 if( *colflags2 == 0 ) continue; \
604 flags2 += flags_stride; \
605 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
607 flags2 += flags_stride; \
608 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
610 flags2 += flags_stride; \
611 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
613 flags2 += flags_stride; \
614 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
618 flags1 += flags_stride << 2; \
619 colflags1 += flags_stride; \
621 for (i = 0; i < w; ++i) { \
622 OPJ_INT32 *data2 = data1 + i; \
623 opj_flag_t *flags2 = flags1 + i; \
624 opj_colflag_t *colflags2 = colflags1 + i; \
625 for (j = k; j < h; ++j) { \
626 flags2 += flags_stride; \
627 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
633 static void opj_t1_dec_sigpass_mqc_64x64(
637 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
640 static void opj_t1_dec_sigpass_mqc_generic(
644 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
647 /* VSC and BYPASS by Antonin */
648 static void opj_t1_dec_sigpass_mqc_vsc(
652 OPJ_INT32 one, half, oneplushalf, vsc;
654 OPJ_INT32 *data1 = t1->data;
655 opj_flag_t *flags1 = &t1->flags[1];
656 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
659 oneplushalf = one | half;
660 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
661 for (i = 0; i < t1->w; ++i) {
662 OPJ_INT32 *data2 = data1 + i;
663 opj_flag_t *flags2 = flags1 + i;
664 opj_colflag_t *colflags2 = colflags1 + i;
665 flags2 += t1->flags_stride;
666 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 0U);
668 flags2 += t1->flags_stride;
669 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 1U);
671 flags2 += t1->flags_stride;
672 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 2U);
674 flags2 += t1->flags_stride;
675 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1, 3U);
679 flags1 += t1->flags_stride << 2;
680 colflags1 += t1->flags_stride;
682 for (i = 0; i < t1->w; ++i) {
683 opj_colflag_t *colflags2 = colflags1 + i;
684 for (j = k; j < t1->h; ++j) {
685 vsc = (j == t1->h - 1) ? 1 : 0;
686 opj_t1_dec_sigpass_step_mqc_vsc(
688 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
690 &t1->data[(j * t1->w) + i],
696 } /* VSC and BYPASS by Antonin */
700 static void opj_t1_enc_refpass_step( opj_t1_t *t1,
712 opj_mqc_t *mqc = t1->mqc; /* MQC component */
714 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
715 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
716 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
717 v = (opj_int_abs(*datap) & one) ? 1 : 0;
718 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
719 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
720 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
722 opj_mqc_encode(mqc, (OPJ_UINT32)v);
724 *flagsp |= T1_REFINE;
728 static INLINE void opj_t1_dec_refpass_step_raw(
730 opj_colflag_t *colflagsp,
738 opj_raw_t *raw = t1->raw; /* RAW component */
740 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
741 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
742 v = (OPJ_INT32)opj_raw_decode(raw);
743 t = v ? poshalf : neghalf;
744 *datap += *datap < 0 ? -t : t;
745 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
747 } /* VSC and BYPASS by Antonin */
749 static INLINE void opj_t1_dec_refpass_step_mqc(
751 #ifdef CONSISTENCY_CHECK
754 opj_flag_t *flagsp_unused,
756 opj_colflag_t *colflagsp,
764 opj_mqc_t *mqc = t1->mqc; /* MQC component */
765 #ifdef CONSISTENCY_CHECK
766 assert( ((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
767 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) );
771 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
772 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
773 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
774 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
775 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
776 v = opj_mqc_decode(mqc);
777 t = v ? poshalf : neghalf;
778 *datap += *datap < 0 ? -t : t;
779 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
781 } /* VSC and BYPASS by Antonin */
783 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
786 opj_colflag_t *colflagsp,
795 opj_mqc_t *mqc = t1->mqc; /* MQC component */
797 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
798 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
799 OPJ_INT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
800 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
801 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
802 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
803 v = opj_mqc_decode(mqc);
804 t = v ? poshalf : neghalf;
805 *datap += *datap < 0 ? -t : t;
806 *colflagsp |= (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
808 } /* VSC and BYPASS by Antonin */
811 static void opj_t1_enc_refpass(
818 OPJ_UINT32 i, j, k, vsc;
822 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
823 for (k = 0; k < t1->h; k += 4) {
824 for (i = 0; i < t1->w; ++i) {
825 for (j = k; j < k + 4 && j < t1->h; ++j) {
826 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
827 opj_t1_enc_refpass_step(
829 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
830 &t1->data[(j * t1->data_stride) + i],
841 static void opj_t1_dec_refpass_raw(
845 OPJ_INT32 one, poshalf, neghalf;
847 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
850 neghalf = bpno > 0 ? -poshalf : -1;
851 for (k = 0; k < t1->h; k += 4) {
852 for (i = 0; i < t1->w; ++i) {
853 opj_colflag_t *colflags2 = colflags1 + i;
854 for (j = k; j < k + 4 && j < t1->h; ++j) {
855 opj_t1_dec_refpass_step_raw(
858 &t1->data[(j * t1->w) + i],
863 colflags1 += t1->flags_stride;
865 } /* VSC and BYPASS by Antonin */
867 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
869 OPJ_INT32 one, poshalf, neghalf; \
870 OPJ_UINT32 i, j, k; \
871 OPJ_INT32 *data1 = t1->data; \
872 opj_flag_t *flags1 = &t1->flags[1]; \
873 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
875 poshalf = one >> 1; \
876 neghalf = bpno > 0 ? -poshalf : -1; \
877 for (k = 0; k < (h & ~3u); k += 4) { \
878 for (i = 0; i < w; ++i) { \
879 OPJ_INT32 *data2 = data1 + i; \
880 opj_flag_t *flags2 = flags1 + i; \
881 opj_colflag_t *colflags2 = colflags1 + i; \
882 if( *colflags2 == 0 ) continue; \
883 flags2 += flags_stride; \
884 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
886 flags2 += flags_stride; \
887 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
889 flags2 += flags_stride; \
890 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
892 flags2 += flags_stride; \
893 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
897 flags1 += flags_stride << 2; \
898 colflags1 += flags_stride; \
900 for (i = 0; i < w; ++i) { \
901 OPJ_INT32 *data2 = data1 + i; \
902 opj_flag_t *flags2 = flags1 + i; \
903 opj_colflag_t *colflags2 = colflags1 + i; \
904 for (j = k; j < h; ++j) { \
905 flags2 += flags_stride; \
906 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
912 static void opj_t1_dec_refpass_mqc_64x64(
916 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
919 static void opj_t1_dec_refpass_mqc_generic(
923 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
926 /* VSC and BYPASS by Antonin */
927 static void opj_t1_dec_refpass_mqc_vsc(
931 OPJ_INT32 one, poshalf, neghalf;
934 OPJ_INT32 *data1 = t1->data;
935 opj_flag_t *flags1 = &t1->flags[1];
936 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
939 neghalf = bpno > 0 ? -poshalf : -1;
940 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
941 for (i = 0; i < t1->w; ++i) {
942 OPJ_INT32 *data2 = data1 + i;
943 opj_flag_t *flags2 = flags1 + i;
944 opj_colflag_t *colflags2 = colflags1 + i;
945 flags2 += t1->flags_stride;
946 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 0U);
948 flags2 += t1->flags_stride;
949 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 1U);
951 flags2 += t1->flags_stride;
952 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 2U);
954 flags2 += t1->flags_stride;
955 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 1, 3U);
959 flags1 += t1->flags_stride << 2;
960 colflags1 += t1->flags_stride;
962 for (i = 0; i < t1->w; ++i) {
963 opj_colflag_t *colflags2 = colflags1 + i;
964 for (j = k; j < t1->h; ++j) {
965 vsc = (j == t1->h - 1) ? 1 : 0;
966 opj_t1_dec_refpass_step_mqc_vsc(
968 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
970 &t1->data[(j * t1->w) + i],
976 } /* VSC and BYPASS by Antonin */
979 static void opj_t1_enc_clnpass_step(
992 opj_mqc_t *mqc = t1->mqc; /* MQC component */
994 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
998 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
999 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1000 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1001 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1004 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
1005 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1006 v = *datap < 0 ? 1 : 0;
1007 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1008 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1011 *flagsp &= ~T1_VISIT;
1014 static void opj_t1_dec_clnpass_step_partial(
1017 opj_colflag_t *colflagsp,
1019 OPJ_INT32 oneplushalf,
1023 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1026 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1027 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1028 *datap = v ? -oneplushalf : oneplushalf;
1029 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1030 #ifdef CONSISTENCY_CHECK
1031 *flagsp &= ~T1_VISIT;
1033 } /* VSC and BYPASS by Antonin */
1035 static void opj_t1_dec_clnpass_step(
1038 opj_colflag_t *colflagsp,
1040 OPJ_INT32 oneplushalf,
1045 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1046 #ifdef CONSISTENCY_CHECK
1047 assert( (!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) );
1049 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) {
1051 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1052 if (opj_mqc_decode(mqc)) {
1053 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1054 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1055 *datap = v ? -oneplushalf : oneplushalf;
1056 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1059 #ifdef CONSISTENCY_CHECK
1060 *flagsp &= ~T1_VISIT;
1062 } /* VSC and BYPASS by Antonin */
1064 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1067 opj_colflag_t *colflagsp,
1069 OPJ_INT32 oneplushalf,
1071 OPJ_UINT32 flags_stride)
1076 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1079 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1081 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1082 if (opj_mqc_decode(mqc)) {
1083 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1084 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1085 *datap = v ? -oneplushalf : oneplushalf;
1086 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1089 /*flagsp &= ~T1_VISIT;*/
1092 static void opj_t1_dec_clnpass_step_vsc(
1095 opj_colflag_t *colflagsp,
1097 OPJ_INT32 oneplushalf,
1104 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1106 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1110 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
1111 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1112 if (opj_mqc_decode(mqc)) {
1114 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1115 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1116 *datap = v ? -oneplushalf : oneplushalf;
1117 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1120 #ifdef CONSISTENCY_CHECK
1121 *flagsp &= ~T1_VISIT;
1125 static void opj_t1_enc_clnpass(
1133 OPJ_UINT32 agg, runlen, vsc;
1135 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1138 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1139 for (k = 0; k < t1->h; k += 4) {
1140 for (i = 0; i < t1->w; ++i) {
1141 if (k + 3 < t1->h) {
1142 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1143 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1144 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1145 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1146 || (MACRO_t1_flags(1 + k + 3,1 + i)
1147 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1149 agg = !((MACRO_t1_flags(1 + k,1 + i) |
1150 MACRO_t1_flags(1 + k + 1,1 + i) |
1151 MACRO_t1_flags(1 + k + 2,1 + i) |
1152 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1158 for (runlen = 0; runlen < 4; ++runlen) {
1159 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1162 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1163 opj_mqc_encode(mqc, runlen != 4);
1167 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1168 opj_mqc_encode(mqc, runlen >> 1);
1169 opj_mqc_encode(mqc, runlen & 1);
1173 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1174 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1175 opj_t1_enc_clnpass_step(
1177 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1178 &t1->data[(j * t1->data_stride) + i],
1182 agg && (j == k + runlen),
1189 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1191 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1193 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1194 OPJ_UINT32 i, j, k; \
1195 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1197 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1201 oneplushalf = one | half; \
1202 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1203 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1204 for (k = 0; k < h; k += 4) { \
1205 for (i = 0; i < w; ++i) { \
1206 opj_colflag_t *colflags2 = colflags1 + i; \
1208 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1209 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1210 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1211 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1212 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1213 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1218 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1219 if (!opj_mqc_decode(mqc)) { \
1222 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1223 runlen = opj_mqc_decode(mqc); \
1224 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1228 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1229 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1230 opj_t1_dec_clnpass_step_vsc( \
1232 &t1->flags[((j+1) * flags_stride) + i + 1], \
1234 &t1->data[(j * w) + i], \
1236 agg && (j == k + (OPJ_UINT32)runlen), \
1239 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1241 colflags1 += flags_stride; \
1244 OPJ_INT32 *data1 = t1->data; \
1245 opj_flag_t *flags1 = &t1->flags[1]; \
1246 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1247 for (k = 0; k < (h & ~3u); k += 4) { \
1248 for (i = 0; i < w; ++i) { \
1249 OPJ_INT32 *data2 = data1 + i; \
1250 opj_flag_t *flags2 = flags1 + i; \
1251 opj_colflag_t *colflags2 = colflags1 + i; \
1252 opj_colflag_t colflags = *colflags2; \
1253 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1254 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1255 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1256 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1257 if( consistency_check ) { \
1258 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1259 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1260 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1261 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1264 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1265 if (!opj_mqc_decode(mqc)) { \
1268 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1269 runlen = opj_mqc_decode(mqc); \
1270 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1271 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1272 data2 += (OPJ_UINT32)runlen * w; \
1273 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1274 flags2 += flags_stride; \
1275 if (j == (OPJ_UINT32)runlen) { \
1276 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1278 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1283 flags2 += flags_stride; \
1284 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1285 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1286 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1288 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1290 flags2 += flags_stride; \
1291 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1292 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1293 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1295 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1297 flags2 += flags_stride; \
1298 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1299 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1300 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1302 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1304 flags2 += flags_stride; \
1305 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1306 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1307 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1309 if( consistency_check ) *flags2 &= ~T1_VISIT; \
1312 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1315 flags1 += flags_stride << 2; \
1316 colflags1 += flags_stride; \
1318 for (i = 0; i < w; ++i) { \
1319 OPJ_INT32 *data2 = data1 + i; \
1320 opj_flag_t *flags2 = flags1 + i; \
1321 opj_colflag_t *colflags2 = colflags1 + i; \
1322 for (j = k; j < h; ++j) { \
1323 flags2 += flags_stride; \
1324 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1327 *colflags2 &= ~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1333 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1334 v = opj_mqc_decode(mqc); \
1335 v = (v << 1) | opj_mqc_decode(mqc); \
1336 v = (v << 1) | opj_mqc_decode(mqc); \
1337 v = (v << 1) | opj_mqc_decode(mqc); \
1340 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1344 } /* VSC and BYPASS by Antonin */
1346 static void opj_t1_dec_clnpass_64x64(
1351 #ifdef CONSISTENCY_CHECK
1352 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1354 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1358 static void opj_t1_dec_clnpass_generic(
1363 #ifdef CONSISTENCY_CHECK
1364 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1366 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1371 /** mod fixed_quality */
1372 static OPJ_FLOAT64 opj_t1_getwmsedec(
1379 OPJ_FLOAT64 stepsize,
1380 OPJ_UINT32 numcomps,
1381 const OPJ_FLOAT64 * mct_norms,
1382 OPJ_UINT32 mct_numcomps)
1384 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1385 OPJ_ARG_NOT_USED(numcomps);
1387 if (mct_norms && (compno < mct_numcomps)) {
1388 w1 = mct_norms[compno];
1392 w2 = opj_dwt_getnorm(level, orient);
1393 } else { /* if (qmfbid == 0) */
1394 w2 = opj_dwt_getnorm_real(level, orient);
1397 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1398 wmsedec *= wmsedec * nmsedec / 8192.0;
1403 static OPJ_BOOL opj_t1_allocate_buffers(
1408 OPJ_UINT32 datasize=w * h;
1409 OPJ_UINT32 flagssize;
1411 /* encoder uses tile buffer, so no need to allocate */
1413 if(datasize > t1->datasize){
1414 opj_aligned_free(t1->data);
1415 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1417 /* FIXME event manager error callback */
1420 t1->datasize=datasize;
1422 /* memset first arg is declared to never be null by gcc */
1423 if (t1->data != NULL) {
1424 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1427 t1->flags_stride=w+2;
1428 flagssize=t1->flags_stride * (h+2);
1430 if(flagssize > t1->flagssize){
1431 opj_aligned_free(t1->flags);
1432 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1434 /* FIXME event manager error callback */
1437 t1->flagssize=flagssize;
1439 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1442 OPJ_UINT32 colflags_size=t1->flags_stride * ((h+3) / 4 + 2);
1444 if(colflags_size > t1->colflags_size){
1445 opj_aligned_free(t1->colflags);
1446 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1448 /* FIXME event manager error callback */
1451 t1->colflags_size=colflags_size;
1453 memset(t1->colflags,0,colflags_size * sizeof(opj_colflag_t));
1462 /* ----------------------------------------------------------------------- */
1464 /* ----------------------------------------------------------------------- */
1466 * Creates a new Tier 1 handle
1467 * and initializes the look-up tables of the Tier-1 coder/decoder
1468 * @return a new T1 handle if successful, returns NULL otherwise
1470 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1472 opj_t1_t *l_t1 = 00;
1474 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1479 /* create MQC and RAW handles */
1480 l_t1->mqc = opj_mqc_create();
1482 opj_t1_destroy(l_t1);
1486 l_t1->raw = opj_raw_create();
1488 opj_t1_destroy(l_t1);
1491 l_t1->encoder = isEncoder;
1498 * Destroys a previously created T1 handle
1500 * @param p_t1 Tier 1 handle to destroy
1502 void opj_t1_destroy(opj_t1_t *p_t1)
1508 /* destroy MQC and RAW handles */
1509 opj_mqc_destroy(p_t1->mqc);
1511 opj_raw_destroy(p_t1->raw);
1514 /* encoder uses tile buffer, so no need to free */
1515 if (!p_t1->encoder && p_t1->data) {
1516 opj_aligned_free(p_t1->data);
1521 opj_aligned_free(p_t1->flags);
1525 if (p_t1->colflags) {
1526 opj_aligned_free(p_t1->colflags);
1527 p_t1->colflags = 00;
1535 opj_tcd_cblk_dec_t* cblk;
1536 opj_tcd_band_t* band;
1537 opj_tcd_tilecomp_t* tilec;
1539 volatile OPJ_BOOL* pret;
1540 } opj_t1_cblk_decode_processing_job_t;
1542 static void opj_t1_destroy_wrapper(void* t1)
1544 opj_t1_destroy( (opj_t1_t*) t1 );
1547 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1549 opj_tcd_cblk_dec_t* cblk;
1550 opj_tcd_band_t* band;
1551 opj_tcd_tilecomp_t* tilec;
1553 OPJ_INT32* OPJ_RESTRICT datap;
1554 OPJ_UINT32 cblk_w, cblk_h;
1557 opj_t1_cblk_decode_processing_job_t* job;
1562 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1568 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1576 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1579 t1 = opj_t1_create( OPJ_FALSE );
1580 opj_tls_set( tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper );
1583 if (OPJ_FALSE == opj_t1_decode_cblk(
1587 (OPJ_UINT32)tccp->roishift,
1589 *(job->pret) = OPJ_FALSE;
1594 x = cblk->x0 - band->x0;
1595 y = cblk->y0 - band->y0;
1596 if (band->bandno & 1) {
1597 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1598 x += pres->x1 - pres->x0;
1600 if (band->bandno & 2) {
1601 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1602 y += pres->y1 - pres->y0;
1609 if (tccp->roishift) {
1610 OPJ_INT32 thresh = 1 << tccp->roishift;
1611 for (j = 0; j < cblk_h; ++j) {
1612 for (i = 0; i < cblk_w; ++i) {
1613 OPJ_INT32 val = datap[(j * cblk_w) + i];
1614 OPJ_INT32 mag = abs(val);
1615 if (mag >= thresh) {
1616 mag >>= tccp->roishift;
1617 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1622 if (tccp->qmfbid == 1) {
1623 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1624 for (j = 0; j < cblk_h; ++j) {
1626 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1627 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1628 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1629 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1630 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1631 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1632 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1633 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1634 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1636 for (; i < cblk_w; ++i) {
1637 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1638 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1641 } else { /* if (tccp->qmfbid == 0) */
1642 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1643 for (j = 0; j < cblk_h; ++j) {
1644 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1645 for (i = 0; i < cblk_w; ++i) {
1646 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1659 void opj_t1_decode_cblks( opj_thread_pool_t* tp,
1660 volatile OPJ_BOOL* pret,
1661 opj_tcd_tilecomp_t* tilec,
1665 OPJ_UINT32 resno, bandno, precno, cblkno;
1667 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1668 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1670 for (bandno = 0; bandno < res->numbands; ++bandno) {
1671 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1673 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1674 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1676 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1677 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1678 opj_t1_cblk_decode_processing_job_t* job;
1680 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, sizeof(opj_t1_cblk_decode_processing_job_t));
1692 opj_thread_pool_submit_job( tp, opj_t1_clbl_decode_processor, job );
1704 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1705 opj_tcd_cblk_dec_t* cblk,
1707 OPJ_UINT32 roishift,
1710 opj_raw_t *raw = t1->raw; /* RAW component */
1711 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1713 OPJ_INT32 bpno_plus_one;
1714 OPJ_UINT32 passtype;
1715 OPJ_UINT32 segno, passno;
1716 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1718 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1720 if(!opj_t1_allocate_buffers(
1722 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1723 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1728 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1731 opj_mqc_resetstates(mqc);
1732 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1733 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1734 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1736 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1737 opj_tcd_seg_t *seg = &cblk->segs[segno];
1740 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1741 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1742 if(seg->data == 00){
1745 if (type == T1_TYPE_RAW) {
1746 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1748 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1753 if( t1->w == 64 && t1->h == 64 )
1755 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1758 if (type == T1_TYPE_RAW) {
1759 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1761 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1762 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1764 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1769 if (type == T1_TYPE_RAW) {
1770 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1772 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1773 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1775 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1780 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1784 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1785 opj_mqc_resetstates(mqc);
1786 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1787 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1788 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1790 if (++passtype == 3) {
1798 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1801 if (type == T1_TYPE_RAW) {
1802 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1804 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1805 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1807 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1812 if (type == T1_TYPE_RAW) {
1813 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1815 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1816 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1818 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1823 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1827 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1828 opj_mqc_resetstates(mqc);
1829 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1830 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1831 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1833 if (++passtype == 3) {
1846 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1847 opj_tcd_tile_t *tile,
1849 const OPJ_FLOAT64 * mct_norms,
1850 OPJ_UINT32 mct_numcomps
1853 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1855 tile->distotile = 0; /* fixed_quality */
1857 for (compno = 0; compno < tile->numcomps; ++compno) {
1858 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1859 opj_tccp_t* tccp = &tcp->tccps[compno];
1860 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1862 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1863 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1865 for (bandno = 0; bandno < res->numbands; ++bandno) {
1866 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1867 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1869 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1870 opj_tcd_precinct_t *prc = &band->precincts[precno];
1872 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1873 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1874 OPJ_INT32* OPJ_RESTRICT tiledp;
1877 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1879 OPJ_INT32 x = cblk->x0 - band->x0;
1880 OPJ_INT32 y = cblk->y0 - band->y0;
1881 if (band->bandno & 1) {
1882 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1883 x += pres->x1 - pres->x0;
1885 if (band->bandno & 2) {
1886 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1887 y += pres->y1 - pres->y0;
1890 if(!opj_t1_allocate_buffers(
1892 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1893 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1900 tileLineAdvance = tile_w - cblk_w;
1902 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1904 t1->data_stride = tile_w;
1905 if (tccp->qmfbid == 1) {
1906 for (j = 0; j < cblk_h; ++j) {
1907 for (i = 0; i < cblk_w; ++i) {
1908 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1911 tileIndex += tileLineAdvance;
1913 } else { /* if (tccp->qmfbid == 0) */
1914 for (j = 0; j < cblk_h; ++j) {
1915 for (i = 0; i < cblk_w; ++i) {
1916 OPJ_INT32 tmp = tiledp[tileIndex];
1923 tileIndex += tileLineAdvance;
1932 tilec->numresolutions - 1 - resno,
1949 /** mod fixed_quality */
1950 static void opj_t1_encode_cblk(opj_t1_t *t1,
1951 opj_tcd_cblk_enc_t* cblk,
1956 OPJ_FLOAT64 stepsize,
1958 OPJ_UINT32 numcomps,
1959 opj_tcd_tile_t * tile,
1960 const OPJ_FLOAT64 * mct_norms,
1961 OPJ_UINT32 mct_numcomps)
1963 OPJ_FLOAT64 cumwmsedec = 0.0;
1965 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1969 OPJ_UINT32 passtype;
1970 OPJ_INT32 nmsedec = 0;
1973 OPJ_BYTE type = T1_TYPE_MQ;
1974 OPJ_FLOAT64 tempwmsedec;
1976 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1979 for (i = 0; i < t1->w; ++i) {
1980 for (j = 0; j < t1->h; ++j) {
1981 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1982 max = opj_int_max(max, tmp);
1986 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1988 bpno = (OPJ_INT32)(cblk->numbps - 1);
1991 opj_mqc_resetstates(mqc);
1992 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1993 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1994 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1995 opj_mqc_init_enc(mqc, cblk->data);
1997 for (passno = 0; bpno >= 0; ++passno) {
1998 opj_tcd_pass_t *pass = &cblk->passes[passno];
1999 OPJ_UINT32 correction = 3;
2000 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2004 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2007 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2010 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2011 /* code switch SEGMARK (i.e. SEGSYM) */
2012 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2013 opj_mqc_segmark_enc(mqc);
2018 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
2019 cumwmsedec += tempwmsedec;
2020 tile->distotile += tempwmsedec;
2022 /* Code switch "RESTART" (i.e. TERMALL) */
2023 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2024 if (type == T1_TYPE_RAW) {
2027 /* correction = mqc_bypass_flush_enc(); */
2028 } else { /* correction = mqc_restart_enc(); */
2034 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2035 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2036 if (type == T1_TYPE_RAW) {
2039 /* correction = mqc_bypass_flush_enc(); */
2040 } else { /* correction = mqc_restart_enc(); */
2050 if (++passtype == 3) {
2055 if (pass->term && bpno > 0) {
2056 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2057 if (type == T1_TYPE_RAW)
2058 opj_mqc_bypass_init_enc(mqc);
2060 opj_mqc_restart_init_enc(mqc);
2063 pass->distortiondec = cumwmsedec;
2064 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2066 /* Code-switch "RESET" */
2067 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2068 opj_mqc_reset_enc(mqc);
2071 /* Code switch "ERTERM" (i.e. PTERM) */
2072 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2073 opj_mqc_erterm_enc(mqc);
2074 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2077 cblk->totalpasses = passno;
2079 for (passno = 0; passno<cblk->totalpasses; passno++) {
2080 opj_tcd_pass_t *pass = &cblk->passes[passno];
2081 if (pass->rate > opj_mqc_numbytes(mqc))
2082 pass->rate = opj_mqc_numbytes(mqc);
2083 /*Preventing generation of FF as last data byte of a pass*/
2084 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2087 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2092 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
2103 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2104 opj_raw_t *raw = t1->raw; /* RAW component */
2106 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2107 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2108 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2109 if (type == T1_TYPE_RAW) {
2110 v = opj_raw_decode(raw);
2112 v = opj_mqc_decode(mqc);
2114 t = v ? poshalf : neghalf;
2115 *datap += *datap < 0 ? -t : t;
2116 *flagsp |= T1_REFINE;
2118 } /* VSC and BYPASS by Antonin */
2124 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
2128 OPJ_INT32 oneplushalf,
2134 opj_raw_t *raw = t1->raw; /* RAW component */
2135 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2137 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2138 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2139 if (type == T1_TYPE_RAW) {
2140 if (opj_raw_decode(raw)) {
2141 v = opj_raw_decode(raw); /* ESSAI */
2142 *datap = v ? -oneplushalf : oneplushalf;
2143 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2146 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2147 if (opj_mqc_decode(mqc)) {
2148 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2149 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2150 *datap = v ? -oneplushalf : oneplushalf;
2151 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2154 *flagsp |= T1_VISIT;
2156 } /* VSC and BYPASS by Antonin */