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 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
344 np[0] |= (opj_flag_t)(flag_N << 2U);
347 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
349 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
352 sp[0] |= (opj_flag_t)flag_N;
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 |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
362 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
363 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
364 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
365 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
366 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
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 |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
374 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))));
375 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
376 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))));
377 *(colflagsp + 1) |= (opj_colflag_t)((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) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
386 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))) |
387 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
388 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
389 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))) |
390 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
391 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
392 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))) |
393 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
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_raw_decode(raw); /* ESSAI */
453 *datap = v ? -oneplushalf : oneplushalf;
454 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
456 #ifdef CONSISTENCY_CHECK
459 *colflagsp |= (opj_colflag_t)(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 |= (opj_colflag_t)(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, flag));
513 if (opj_mqc_decode(mqc)) {
514 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
515 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
516 *datap = v ? -oneplushalf : oneplushalf;
517 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
519 #ifdef CONSISTENCY_CHECK
522 *colflagsp |= (opj_colflag_t)(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 |= (opj_colflag_t)(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 |= (opj_colflag_t)(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,
796 opj_mqc_t *mqc = t1->mqc; /* MQC component */
798 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
799 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
800 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
801 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
802 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
803 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
804 v = (OPJ_UINT32)opj_mqc_decode(mqc);
805 t = v ? poshalf : neghalf;
806 *datap += *datap < 0 ? -t : t;
807 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
809 } /* VSC and BYPASS by Antonin */
812 static void opj_t1_enc_refpass(
819 OPJ_UINT32 i, j, k, vsc;
823 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
824 for (k = 0; k < t1->h; k += 4) {
825 for (i = 0; i < t1->w; ++i) {
826 for (j = k; j < k + 4 && j < t1->h; ++j) {
827 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
828 opj_t1_enc_refpass_step(
830 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
831 &t1->data[(j * t1->data_stride) + i],
842 static void opj_t1_dec_refpass_raw(
846 OPJ_INT32 one, poshalf, neghalf;
848 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
851 neghalf = bpno > 0 ? -poshalf : -1;
852 for (k = 0; k < t1->h; k += 4) {
853 for (i = 0; i < t1->w; ++i) {
854 opj_colflag_t *colflags2 = colflags1 + i;
855 for (j = k; j < k + 4 && j < t1->h; ++j) {
856 opj_t1_dec_refpass_step_raw(
859 &t1->data[(j * t1->w) + i],
864 colflags1 += t1->flags_stride;
866 } /* VSC and BYPASS by Antonin */
868 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
870 OPJ_INT32 one, poshalf, neghalf; \
871 OPJ_UINT32 i, j, k; \
872 OPJ_INT32 *data1 = t1->data; \
873 opj_flag_t *flags1 = &t1->flags[1]; \
874 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
876 poshalf = one >> 1; \
877 neghalf = bpno > 0 ? -poshalf : -1; \
878 for (k = 0; k < (h & ~3u); k += 4) { \
879 for (i = 0; i < w; ++i) { \
880 OPJ_INT32 *data2 = data1 + i; \
881 opj_flag_t *flags2 = flags1 + i; \
882 opj_colflag_t *colflags2 = colflags1 + i; \
883 if( *colflags2 == 0 ) continue; \
884 flags2 += flags_stride; \
885 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
887 flags2 += flags_stride; \
888 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
890 flags2 += flags_stride; \
891 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
893 flags2 += flags_stride; \
894 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
898 flags1 += flags_stride << 2; \
899 colflags1 += flags_stride; \
901 for (i = 0; i < w; ++i) { \
902 OPJ_INT32 *data2 = data1 + i; \
903 opj_flag_t *flags2 = flags1 + i; \
904 opj_colflag_t *colflags2 = colflags1 + i; \
905 for (j = k; j < h; ++j) { \
906 flags2 += flags_stride; \
907 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
913 static void opj_t1_dec_refpass_mqc_64x64(
917 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
920 static void opj_t1_dec_refpass_mqc_generic(
924 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
927 /* VSC and BYPASS by Antonin */
928 static void opj_t1_dec_refpass_mqc_vsc(
932 OPJ_INT32 one, poshalf, neghalf;
935 OPJ_INT32 *data1 = t1->data;
936 opj_flag_t *flags1 = &t1->flags[1];
937 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
940 neghalf = bpno > 0 ? -poshalf : -1;
941 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
942 for (i = 0; i < t1->w; ++i) {
943 OPJ_INT32 *data2 = data1 + i;
944 opj_flag_t *flags2 = flags1 + i;
945 opj_colflag_t *colflags2 = colflags1 + i;
946 flags2 += t1->flags_stride;
947 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 0U);
949 flags2 += t1->flags_stride;
950 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 1U);
952 flags2 += t1->flags_stride;
953 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 2U);
955 flags2 += t1->flags_stride;
956 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 1, 3U);
960 flags1 += t1->flags_stride << 2;
961 colflags1 += t1->flags_stride;
963 for (i = 0; i < t1->w; ++i) {
964 opj_colflag_t *colflags2 = colflags1 + i;
965 for (j = k; j < t1->h; ++j) {
966 vsc = (j == t1->h - 1) ? 1 : 0;
967 opj_t1_dec_refpass_step_mqc_vsc(
969 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
971 &t1->data[(j * t1->w) + i],
977 } /* VSC and BYPASS by Antonin */
980 static void opj_t1_enc_clnpass_step(
993 opj_mqc_t *mqc = t1->mqc; /* MQC component */
995 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
999 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1000 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1001 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1002 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1005 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
1006 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1007 v = *datap < 0 ? 1 : 0;
1008 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1009 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1012 *flagsp &= (opj_flag_t)~T1_VISIT;
1015 static void opj_t1_dec_clnpass_step_partial(
1018 opj_colflag_t *colflagsp,
1020 OPJ_INT32 oneplushalf,
1024 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1027 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1028 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1029 *datap = v ? -oneplushalf : oneplushalf;
1030 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1031 #ifdef CONSISTENCY_CHECK
1032 *flagsp &= ~T1_VISIT;
1034 } /* VSC and BYPASS by Antonin */
1036 static void opj_t1_dec_clnpass_step(
1039 opj_colflag_t *colflagsp,
1041 OPJ_INT32 oneplushalf,
1046 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1047 #ifdef CONSISTENCY_CHECK
1048 assert( (!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) );
1050 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) {
1052 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1053 if (opj_mqc_decode(mqc)) {
1054 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1055 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1056 *datap = v ? -oneplushalf : oneplushalf;
1057 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1060 #ifdef CONSISTENCY_CHECK
1061 *flagsp &= ~T1_VISIT;
1063 } /* VSC and BYPASS by Antonin */
1065 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1068 opj_colflag_t *colflagsp,
1070 OPJ_INT32 oneplushalf,
1072 OPJ_UINT32 flags_stride)
1077 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1080 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1082 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1083 if (opj_mqc_decode(mqc)) {
1084 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1085 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1086 *datap = v ? -oneplushalf : oneplushalf;
1087 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1090 /*flagsp &= ~T1_VISIT;*/
1093 static void opj_t1_dec_clnpass_step_vsc(
1096 opj_colflag_t *colflagsp,
1098 OPJ_INT32 oneplushalf,
1105 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1107 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1111 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
1112 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1113 if (opj_mqc_decode(mqc)) {
1115 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1116 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1117 *datap = v ? -oneplushalf : oneplushalf;
1118 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1121 #ifdef CONSISTENCY_CHECK
1122 *flagsp &= ~T1_VISIT;
1126 static void opj_t1_enc_clnpass(
1134 OPJ_UINT32 agg, runlen, vsc;
1136 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1139 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1140 for (k = 0; k < t1->h; k += 4) {
1141 for (i = 0; i < t1->w; ++i) {
1142 if (k + 3 < t1->h) {
1143 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1144 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1145 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1146 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1147 || (MACRO_t1_flags(1 + k + 3,1 + i)
1148 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1150 agg = !((MACRO_t1_flags(1 + k,1 + i) |
1151 MACRO_t1_flags(1 + k + 1,1 + i) |
1152 MACRO_t1_flags(1 + k + 2,1 + i) |
1153 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1159 for (runlen = 0; runlen < 4; ++runlen) {
1160 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1163 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1164 opj_mqc_encode(mqc, runlen != 4);
1168 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1169 opj_mqc_encode(mqc, runlen >> 1);
1170 opj_mqc_encode(mqc, runlen & 1);
1174 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1175 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1176 opj_t1_enc_clnpass_step(
1178 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1179 &t1->data[(j * t1->data_stride) + i],
1183 agg && (j == k + runlen),
1190 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1192 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1194 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1195 OPJ_UINT32 i, j, k; \
1196 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1198 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1202 oneplushalf = one | half; \
1203 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1204 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1205 for (k = 0; k < h; k += 4) { \
1206 for (i = 0; i < w; ++i) { \
1207 opj_colflag_t *colflags2 = colflags1 + i; \
1209 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1210 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1211 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1212 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1213 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1214 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1219 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1220 if (!opj_mqc_decode(mqc)) { \
1223 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1224 runlen = opj_mqc_decode(mqc); \
1225 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1229 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1230 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1231 opj_t1_dec_clnpass_step_vsc( \
1233 &t1->flags[((j+1) * flags_stride) + i + 1], \
1235 &t1->data[(j * w) + i], \
1237 agg && (j == k + (OPJ_UINT32)runlen), \
1240 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1242 colflags1 += flags_stride; \
1245 OPJ_INT32 *data1 = t1->data; \
1246 opj_flag_t *flags1 = &t1->flags[1]; \
1247 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1248 for (k = 0; k < (h & ~3u); k += 4) { \
1249 for (i = 0; i < w; ++i) { \
1250 OPJ_INT32 *data2 = data1 + i; \
1251 opj_flag_t *flags2 = flags1 + i; \
1252 opj_colflag_t *colflags2 = colflags1 + i; \
1253 opj_colflag_t colflags = *colflags2; \
1254 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1255 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1256 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1257 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1258 if( consistency_check ) { \
1259 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1260 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1261 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1262 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1265 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1266 if (!opj_mqc_decode(mqc)) { \
1269 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1270 runlen = opj_mqc_decode(mqc); \
1271 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1272 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1273 data2 += (OPJ_UINT32)runlen * w; \
1274 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1275 flags2 += flags_stride; \
1276 if (j == (OPJ_UINT32)runlen) { \
1277 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1279 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1284 flags2 += flags_stride; \
1285 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1286 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1287 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1289 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1291 flags2 += flags_stride; \
1292 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1293 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1294 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1296 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1298 flags2 += flags_stride; \
1299 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1300 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1301 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1303 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1305 flags2 += flags_stride; \
1306 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1307 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1308 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1310 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1313 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1316 flags1 += flags_stride << 2; \
1317 colflags1 += flags_stride; \
1319 for (i = 0; i < w; ++i) { \
1320 OPJ_INT32 *data2 = data1 + i; \
1321 opj_flag_t *flags2 = flags1 + i; \
1322 opj_colflag_t *colflags2 = colflags1 + i; \
1323 for (j = k; j < h; ++j) { \
1324 flags2 += flags_stride; \
1325 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1328 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1334 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1335 v = opj_mqc_decode(mqc); \
1336 v = (v << 1) | opj_mqc_decode(mqc); \
1337 v = (v << 1) | opj_mqc_decode(mqc); \
1338 v = (v << 1) | opj_mqc_decode(mqc); \
1341 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1345 } /* VSC and BYPASS by Antonin */
1347 static void opj_t1_dec_clnpass_64x64(
1352 #ifdef CONSISTENCY_CHECK
1353 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1355 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1359 static void opj_t1_dec_clnpass_generic(
1364 #ifdef CONSISTENCY_CHECK
1365 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1367 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1372 /** mod fixed_quality */
1373 static OPJ_FLOAT64 opj_t1_getwmsedec(
1380 OPJ_FLOAT64 stepsize,
1381 OPJ_UINT32 numcomps,
1382 const OPJ_FLOAT64 * mct_norms,
1383 OPJ_UINT32 mct_numcomps)
1385 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1386 OPJ_ARG_NOT_USED(numcomps);
1388 if (mct_norms && (compno < mct_numcomps)) {
1389 w1 = mct_norms[compno];
1393 w2 = opj_dwt_getnorm(level, orient);
1394 } else { /* if (qmfbid == 0) */
1395 w2 = opj_dwt_getnorm_real(level, orient);
1398 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1399 wmsedec *= wmsedec * nmsedec / 8192.0;
1404 static OPJ_BOOL opj_t1_allocate_buffers(
1409 OPJ_UINT32 datasize=w * h;
1410 OPJ_UINT32 flagssize;
1412 /* encoder uses tile buffer, so no need to allocate */
1414 if(datasize > t1->datasize){
1415 opj_aligned_free(t1->data);
1416 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1418 /* FIXME event manager error callback */
1421 t1->datasize=datasize;
1423 /* memset first arg is declared to never be null by gcc */
1424 if (t1->data != NULL) {
1425 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1428 t1->flags_stride=w+2;
1429 flagssize=t1->flags_stride * (h+2);
1431 if(flagssize > t1->flagssize){
1432 opj_aligned_free(t1->flags);
1433 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1435 /* FIXME event manager error callback */
1438 t1->flagssize=flagssize;
1440 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1443 OPJ_UINT32 colflags_size=t1->flags_stride * ((h+3) / 4 + 2);
1445 if(colflags_size > t1->colflags_size){
1446 opj_aligned_free(t1->colflags);
1447 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1449 /* FIXME event manager error callback */
1452 t1->colflags_size=colflags_size;
1454 memset(t1->colflags,0,colflags_size * sizeof(opj_colflag_t));
1463 /* ----------------------------------------------------------------------- */
1465 /* ----------------------------------------------------------------------- */
1467 * Creates a new Tier 1 handle
1468 * and initializes the look-up tables of the Tier-1 coder/decoder
1469 * @return a new T1 handle if successful, returns NULL otherwise
1471 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1473 opj_t1_t *l_t1 = 00;
1475 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1480 /* create MQC and RAW handles */
1481 l_t1->mqc = opj_mqc_create();
1483 opj_t1_destroy(l_t1);
1487 l_t1->raw = opj_raw_create();
1489 opj_t1_destroy(l_t1);
1492 l_t1->encoder = isEncoder;
1499 * Destroys a previously created T1 handle
1501 * @param p_t1 Tier 1 handle to destroy
1503 void opj_t1_destroy(opj_t1_t *p_t1)
1509 /* destroy MQC and RAW handles */
1510 opj_mqc_destroy(p_t1->mqc);
1512 opj_raw_destroy(p_t1->raw);
1515 /* encoder uses tile buffer, so no need to free */
1516 if (!p_t1->encoder && p_t1->data) {
1517 opj_aligned_free(p_t1->data);
1522 opj_aligned_free(p_t1->flags);
1526 if (p_t1->colflags) {
1527 opj_aligned_free(p_t1->colflags);
1528 p_t1->colflags = 00;
1536 opj_tcd_cblk_dec_t* cblk;
1537 opj_tcd_band_t* band;
1538 opj_tcd_tilecomp_t* tilec;
1540 volatile OPJ_BOOL* pret;
1541 } opj_t1_cblk_decode_processing_job_t;
1543 static void opj_t1_destroy_wrapper(void* t1)
1545 opj_t1_destroy( (opj_t1_t*) t1 );
1548 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1550 opj_tcd_cblk_dec_t* cblk;
1551 opj_tcd_band_t* band;
1552 opj_tcd_tilecomp_t* tilec;
1554 OPJ_INT32* OPJ_RESTRICT datap;
1555 OPJ_UINT32 cblk_w, cblk_h;
1558 opj_t1_cblk_decode_processing_job_t* job;
1563 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1569 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1577 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1580 t1 = opj_t1_create( OPJ_FALSE );
1581 opj_tls_set( tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper );
1584 if (OPJ_FALSE == opj_t1_decode_cblk(
1588 (OPJ_UINT32)tccp->roishift,
1590 *(job->pret) = OPJ_FALSE;
1595 x = cblk->x0 - band->x0;
1596 y = cblk->y0 - band->y0;
1597 if (band->bandno & 1) {
1598 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1599 x += pres->x1 - pres->x0;
1601 if (band->bandno & 2) {
1602 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1603 y += pres->y1 - pres->y0;
1610 if (tccp->roishift) {
1611 OPJ_INT32 thresh = 1 << tccp->roishift;
1612 for (j = 0; j < cblk_h; ++j) {
1613 for (i = 0; i < cblk_w; ++i) {
1614 OPJ_INT32 val = datap[(j * cblk_w) + i];
1615 OPJ_INT32 mag = abs(val);
1616 if (mag >= thresh) {
1617 mag >>= tccp->roishift;
1618 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1623 if (tccp->qmfbid == 1) {
1624 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1625 for (j = 0; j < cblk_h; ++j) {
1627 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1628 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1629 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1630 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1631 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1632 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1633 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1634 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1635 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1637 for (; i < cblk_w; ++i) {
1638 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1639 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1642 } else { /* if (tccp->qmfbid == 0) */
1643 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1644 for (j = 0; j < cblk_h; ++j) {
1645 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1646 for (i = 0; i < cblk_w; ++i) {
1647 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1660 void opj_t1_decode_cblks( opj_thread_pool_t* tp,
1661 volatile OPJ_BOOL* pret,
1662 opj_tcd_tilecomp_t* tilec,
1666 OPJ_UINT32 resno, bandno, precno, cblkno;
1668 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1669 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1671 for (bandno = 0; bandno < res->numbands; ++bandno) {
1672 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1674 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1675 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1677 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1678 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1679 opj_t1_cblk_decode_processing_job_t* job;
1681 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, sizeof(opj_t1_cblk_decode_processing_job_t));
1693 opj_thread_pool_submit_job( tp, opj_t1_clbl_decode_processor, job );
1705 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1706 opj_tcd_cblk_dec_t* cblk,
1708 OPJ_UINT32 roishift,
1711 opj_raw_t *raw = t1->raw; /* RAW component */
1712 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1714 OPJ_INT32 bpno_plus_one;
1715 OPJ_UINT32 passtype;
1716 OPJ_UINT32 segno, passno;
1717 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1719 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1721 if(!opj_t1_allocate_buffers(
1723 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1724 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1729 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1732 opj_mqc_resetstates(mqc);
1733 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1734 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1735 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1737 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1738 opj_tcd_seg_t *seg = &cblk->segs[segno];
1741 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1742 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1743 if(seg->data == 00){
1746 if (type == T1_TYPE_RAW) {
1747 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1749 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1754 if( t1->w == 64 && t1->h == 64 )
1756 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1759 if (type == T1_TYPE_RAW) {
1760 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1762 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1763 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1765 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1770 if (type == T1_TYPE_RAW) {
1771 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1773 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1774 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1776 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1781 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1785 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1786 opj_mqc_resetstates(mqc);
1787 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1788 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1789 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1791 if (++passtype == 3) {
1799 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1802 if (type == T1_TYPE_RAW) {
1803 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1805 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1806 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1808 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1813 if (type == T1_TYPE_RAW) {
1814 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1816 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1817 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1819 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1824 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1828 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1829 opj_mqc_resetstates(mqc);
1830 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1831 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1832 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1834 if (++passtype == 3) {
1847 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1848 opj_tcd_tile_t *tile,
1850 const OPJ_FLOAT64 * mct_norms,
1851 OPJ_UINT32 mct_numcomps
1854 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1856 tile->distotile = 0; /* fixed_quality */
1858 for (compno = 0; compno < tile->numcomps; ++compno) {
1859 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1860 opj_tccp_t* tccp = &tcp->tccps[compno];
1861 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1863 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1864 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1866 for (bandno = 0; bandno < res->numbands; ++bandno) {
1867 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1868 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1870 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1871 opj_tcd_precinct_t *prc = &band->precincts[precno];
1873 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1874 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1875 OPJ_INT32* OPJ_RESTRICT tiledp;
1878 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1880 OPJ_INT32 x = cblk->x0 - band->x0;
1881 OPJ_INT32 y = cblk->y0 - band->y0;
1882 if (band->bandno & 1) {
1883 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1884 x += pres->x1 - pres->x0;
1886 if (band->bandno & 2) {
1887 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1888 y += pres->y1 - pres->y0;
1891 if(!opj_t1_allocate_buffers(
1893 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1894 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1901 tileLineAdvance = tile_w - cblk_w;
1903 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1905 t1->data_stride = tile_w;
1906 if (tccp->qmfbid == 1) {
1907 for (j = 0; j < cblk_h; ++j) {
1908 for (i = 0; i < cblk_w; ++i) {
1909 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1912 tileIndex += tileLineAdvance;
1914 } else { /* if (tccp->qmfbid == 0) */
1915 for (j = 0; j < cblk_h; ++j) {
1916 for (i = 0; i < cblk_w; ++i) {
1917 OPJ_INT32 tmp = tiledp[tileIndex];
1924 tileIndex += tileLineAdvance;
1933 tilec->numresolutions - 1 - resno,
1950 /** mod fixed_quality */
1951 static void opj_t1_encode_cblk(opj_t1_t *t1,
1952 opj_tcd_cblk_enc_t* cblk,
1957 OPJ_FLOAT64 stepsize,
1959 OPJ_UINT32 numcomps,
1960 opj_tcd_tile_t * tile,
1961 const OPJ_FLOAT64 * mct_norms,
1962 OPJ_UINT32 mct_numcomps)
1964 OPJ_FLOAT64 cumwmsedec = 0.0;
1966 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1970 OPJ_UINT32 passtype;
1971 OPJ_INT32 nmsedec = 0;
1974 OPJ_BYTE type = T1_TYPE_MQ;
1975 OPJ_FLOAT64 tempwmsedec;
1977 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1980 for (i = 0; i < t1->w; ++i) {
1981 for (j = 0; j < t1->h; ++j) {
1982 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1983 max = opj_int_max(max, tmp);
1987 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1989 bpno = (OPJ_INT32)(cblk->numbps - 1);
1992 opj_mqc_resetstates(mqc);
1993 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1994 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1995 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1996 opj_mqc_init_enc(mqc, cblk->data);
1998 for (passno = 0; bpno >= 0; ++passno) {
1999 opj_tcd_pass_t *pass = &cblk->passes[passno];
2000 OPJ_UINT32 correction = 3;
2001 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2005 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2008 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2011 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2012 /* code switch SEGMARK (i.e. SEGSYM) */
2013 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2014 opj_mqc_segmark_enc(mqc);
2019 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
2020 cumwmsedec += tempwmsedec;
2021 tile->distotile += tempwmsedec;
2023 /* Code switch "RESTART" (i.e. TERMALL) */
2024 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2025 if (type == T1_TYPE_RAW) {
2028 /* correction = mqc_bypass_flush_enc(); */
2029 } else { /* correction = mqc_restart_enc(); */
2035 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2036 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2037 if (type == T1_TYPE_RAW) {
2040 /* correction = mqc_bypass_flush_enc(); */
2041 } else { /* correction = mqc_restart_enc(); */
2051 if (++passtype == 3) {
2056 if (pass->term && bpno > 0) {
2057 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2058 if (type == T1_TYPE_RAW)
2059 opj_mqc_bypass_init_enc(mqc);
2061 opj_mqc_restart_init_enc(mqc);
2064 pass->distortiondec = cumwmsedec;
2065 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2067 /* Code-switch "RESET" */
2068 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2069 opj_mqc_reset_enc(mqc);
2072 /* Code switch "ERTERM" (i.e. PTERM) */
2073 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2074 opj_mqc_erterm_enc(mqc);
2075 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2078 cblk->totalpasses = passno;
2080 for (passno = 0; passno<cblk->totalpasses; passno++) {
2081 opj_tcd_pass_t *pass = &cblk->passes[passno];
2082 if (pass->rate > opj_mqc_numbytes(mqc))
2083 pass->rate = opj_mqc_numbytes(mqc);
2084 /*Preventing generation of FF as last data byte of a pass*/
2085 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2088 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2093 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
2104 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2105 opj_raw_t *raw = t1->raw; /* RAW component */
2107 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2108 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2109 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2110 if (type == T1_TYPE_RAW) {
2111 v = opj_raw_decode(raw);
2113 v = opj_mqc_decode(mqc);
2115 t = v ? poshalf : neghalf;
2116 *datap += *datap < 0 ? -t : t;
2117 *flagsp |= T1_REFINE;
2119 } /* VSC and BYPASS by Antonin */
2125 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
2129 OPJ_INT32 oneplushalf,
2135 opj_raw_t *raw = t1->raw; /* RAW component */
2136 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2138 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2139 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2140 if (type == T1_TYPE_RAW) {
2141 if (opj_raw_decode(raw)) {
2142 v = opj_raw_decode(raw); /* ESSAI */
2143 *datap = v ? -oneplushalf : oneplushalf;
2144 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2147 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2148 if (opj_mqc_decode(mqc)) {
2149 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2150 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2151 *datap = v ? -oneplushalf : oneplushalf;
2152 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2155 *flagsp |= T1_VISIT;
2157 } /* VSC and BYPASS by Antonin */