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,
59 Encode significant pass
61 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
71 Decode significant pass
74 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
78 OPJ_INT32 oneplushalf,
83 static INLINE void opj_t1_dec_sigpass_step_raw(
86 opj_colflag_t* colflagsp,
88 OPJ_INT32 oneplushalf,
91 static INLINE void opj_t1_dec_sigpass_step_mqc(
94 opj_colflag_t* colflagsp,
96 OPJ_INT32 oneplushalf,
98 OPJ_UINT32 flags_stride);
99 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
102 opj_colflag_t* colflagsp,
104 OPJ_INT32 oneplushalf,
110 Encode significant pass
112 static void opj_t1_enc_sigpass(opj_t1_t *t1,
119 Decode significant pass
121 static void opj_t1_dec_sigpass_raw(
125 static void opj_t1_dec_sigpass_mqc_vsc(
132 Encode refinement pass
134 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
145 Encode refinement pass
147 static void opj_t1_enc_refpass(opj_t1_t *t1,
154 Decode refinement pass
156 static void opj_t1_dec_refpass_raw(
159 static void opj_t1_dec_refpass_mqc_vsc(
165 Decode refinement pass
168 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
177 static INLINE void opj_t1_dec_refpass_step_raw(
179 opj_colflag_t *colflagsp,
184 static INLINE void opj_t1_dec_refpass_step_mqc(
187 opj_colflag_t *colflagsp,
192 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
195 opj_colflag_t *colflagsp,
207 static void opj_t1_enc_clnpass_step(
219 static void opj_t1_dec_clnpass_step_partial(
222 opj_colflag_t *colflagsp,
224 OPJ_INT32 oneplushalf,
226 static void opj_t1_dec_clnpass_step(
229 opj_colflag_t *colflagsp,
231 OPJ_INT32 oneplushalf,
233 static void opj_t1_dec_clnpass_step_vsc(
236 opj_colflag_t *colflagsp,
238 OPJ_INT32 oneplushalf,
245 static void opj_t1_enc_clnpass(
251 static OPJ_FLOAT64 opj_t1_getwmsedec(
258 OPJ_FLOAT64 stepsize,
260 const OPJ_FLOAT64 * mct_norms,
261 OPJ_UINT32 mct_numcomps);
263 static void opj_t1_encode_cblk(opj_t1_t *t1,
264 opj_tcd_cblk_enc_t* cblk,
269 OPJ_FLOAT64 stepsize,
272 opj_tcd_tile_t * tile,
273 const OPJ_FLOAT64 * mct_norms,
274 OPJ_UINT32 mct_numcomps);
279 @param cblk Code-block coding parameters
281 @param roishift Region of interest shifting value
282 @param cblksty Code-block style
284 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
285 opj_tcd_cblk_dec_t* cblk,
290 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
298 /* ----------------------------------------------------------------------- */
300 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
302 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
305 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f)
307 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
310 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
312 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
313 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
317 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f)
319 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
322 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
325 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
328 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
331 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
334 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
340 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
343 opj_flag_t *np = flagsp - stride;
344 opj_flag_t *sp = flagsp + stride;
346 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
347 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
348 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
350 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
353 np[0] |= (opj_flag_t)(flag_N << 2U);
356 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
358 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
361 sp[0] = (opj_flag_t)(sp[0] | flag_N);
365 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp,
366 opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
368 opj_t1_updateflags(flagsp, s, stride);
370 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
371 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
372 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
373 (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 <<
376 (T1_COLFLAG_RBS * row)) |
377 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
378 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
379 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
380 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
381 } else if (row == 3U) {
382 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
383 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
384 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
385 (T1_COLFLAG_RBS * row)) |
386 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
387 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
388 (T1_COLFLAG_RBS * row)) |
389 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1))));
390 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
391 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
392 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
394 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
395 (T1_COLFLAG_RBS * row)) |
396 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
397 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
398 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
399 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
400 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
401 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
402 (T1_COLFLAG_RBS * row)) |
403 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
404 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
408 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
421 opj_mqc_t *mqc = t1->mqc; /* MQC component */
423 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
424 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
425 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
426 v = (opj_int_abs(*datap) & one) ? 1 : 0;
427 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag)); /* ESSAI */
428 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
429 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
431 opj_mqc_encode(mqc, (OPJ_UINT32)v);
434 v = *datap < 0 ? 1 : 0;
435 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
437 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
438 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
439 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
441 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
443 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
450 static INLINE void opj_t1_dec_sigpass_step_raw(
453 opj_colflag_t* colflagsp,
455 OPJ_INT32 oneplushalf,
460 opj_raw_t *raw = t1->raw; /* RAW component */
462 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
464 if ((flag & T1_SIG_OTH) &&
465 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
466 (T1_COLFLAG_RBS * row)))) {
467 if (opj_raw_decode(raw)) {
468 v = opj_raw_decode(raw); /* ESSAI */
469 *datap = v ? -oneplushalf : oneplushalf;
470 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
472 #ifdef CONSISTENCY_CHECK
475 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
476 (T1_COLFLAG_RBS * row)));
480 static INLINE void opj_t1_dec_sigpass_step_mqc(
483 opj_colflag_t* colflagsp,
485 OPJ_INT32 oneplushalf,
487 OPJ_UINT32 flags_stride)
491 opj_mqc_t *mqc = t1->mqc; /* MQC component */
492 #ifdef CONSISTENCY_CHECK
493 assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
494 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
495 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
496 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
498 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
499 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
500 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
502 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
503 if (opj_mqc_decode(mqc)) {
504 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
505 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
506 *datap = v ? -oneplushalf : oneplushalf;
507 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
509 #ifdef CONSISTENCY_CHECK
512 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
513 (T1_COLFLAG_RBS * row)));
515 } /* VSC and BYPASS by Antonin */
517 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
520 opj_colflag_t* colflagsp,
522 OPJ_INT32 oneplushalf,
528 opj_mqc_t *mqc = t1->mqc; /* MQC component */
530 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
532 if ((flag & T1_SIG_OTH) &&
533 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
534 (T1_COLFLAG_RBS * row)))) {
535 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
536 if (opj_mqc_decode(mqc)) {
537 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
538 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
539 *datap = v ? -oneplushalf : oneplushalf;
540 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
542 #ifdef CONSISTENCY_CHECK
545 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
546 (T1_COLFLAG_RBS * row)));
548 } /* VSC and BYPASS by Antonin */
552 static void opj_t1_enc_sigpass(opj_t1_t *t1,
559 OPJ_UINT32 i, j, k, vsc;
563 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
564 for (k = 0; k < t1->h; k += 4) {
565 for (i = 0; i < t1->w; ++i) {
566 for (j = k; j < k + 4 && j < t1->h; ++j) {
567 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
568 j == t1->h - 1)) ? 1 : 0;
569 opj_t1_enc_sigpass_step(
571 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
572 &t1->data[(j * t1->data_stride) + i],
583 static void opj_t1_dec_sigpass_raw(
588 OPJ_INT32 one, half, oneplushalf, vsc;
590 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
593 oneplushalf = one | half;
594 for (k = 0; k < t1->h; k += 4) {
595 for (i = 0; i < t1->w; ++i) {
596 opj_colflag_t *colflags2 = colflags1 + i;
597 for (j = k; j < k + 4 && j < t1->h; ++j) {
598 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
599 j == t1->h - 1)) ? 1 : 0;
600 opj_t1_dec_sigpass_step_raw(
602 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
604 &t1->data[(j * t1->w) + i],
610 colflags1 += t1->flags_stride;
612 } /* VSC and BYPASS by Antonin */
614 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
616 OPJ_INT32 one, half, oneplushalf; \
617 OPJ_UINT32 i, j, k; \
618 OPJ_INT32 *data1 = t1->data; \
619 opj_flag_t *flags1 = &t1->flags[1]; \
620 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
623 oneplushalf = one | half; \
624 for (k = 0; k < (h & ~3u); k += 4) { \
625 for (i = 0; i < w; ++i) { \
626 OPJ_INT32 *data2 = data1 + i; \
627 opj_flag_t *flags2 = flags1 + i; \
628 opj_colflag_t *colflags2 = colflags1 + i; \
629 if( *colflags2 == 0 ) continue; \
630 flags2 += flags_stride; \
631 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
633 flags2 += flags_stride; \
634 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
636 flags2 += flags_stride; \
637 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
639 flags2 += flags_stride; \
640 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
644 flags1 += flags_stride << 2; \
645 colflags1 += flags_stride; \
647 for (i = 0; i < w; ++i) { \
648 OPJ_INT32 *data2 = data1 + i; \
649 opj_flag_t *flags2 = flags1 + i; \
650 opj_colflag_t *colflags2 = colflags1 + i; \
651 for (j = k; j < h; ++j) { \
652 flags2 += flags_stride; \
653 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
659 static void opj_t1_dec_sigpass_mqc_64x64(
663 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
666 static void opj_t1_dec_sigpass_mqc_generic(
670 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
673 /* VSC and BYPASS by Antonin */
674 static void opj_t1_dec_sigpass_mqc_vsc(
678 OPJ_INT32 one, half, oneplushalf, vsc;
680 OPJ_INT32 *data1 = t1->data;
681 opj_flag_t *flags1 = &t1->flags[1];
682 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
685 oneplushalf = one | half;
686 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
687 for (i = 0; i < t1->w; ++i) {
688 OPJ_INT32 *data2 = data1 + i;
689 opj_flag_t *flags2 = flags1 + i;
690 opj_colflag_t *colflags2 = colflags1 + i;
691 flags2 += t1->flags_stride;
692 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
695 flags2 += t1->flags_stride;
696 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
699 flags2 += t1->flags_stride;
700 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
703 flags2 += t1->flags_stride;
704 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
709 flags1 += t1->flags_stride << 2;
710 colflags1 += t1->flags_stride;
712 for (i = 0; i < t1->w; ++i) {
713 opj_colflag_t *colflags2 = colflags1 + i;
714 for (j = k; j < t1->h; ++j) {
715 vsc = (j == t1->h - 1) ? 1 : 0;
716 opj_t1_dec_sigpass_step_mqc_vsc(
718 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
720 &t1->data[(j * t1->w) + i],
726 } /* VSC and BYPASS by Antonin */
730 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
742 opj_mqc_t *mqc = t1->mqc; /* MQC component */
744 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
745 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
746 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
747 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
749 v = (opj_int_abs(*datap) & one) ? 1 : 0;
750 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
751 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
752 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
754 opj_mqc_encode(mqc, (OPJ_UINT32)v);
756 *flagsp |= T1_REFINE;
760 static INLINE void opj_t1_dec_refpass_step_raw(
762 opj_colflag_t *colflagsp,
770 opj_raw_t *raw = t1->raw; /* RAW component */
772 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
773 (T1_COLFLAG_RBS * row))) ==
774 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
775 v = (OPJ_INT32)opj_raw_decode(raw);
776 t = v ? poshalf : neghalf;
777 *datap += *datap < 0 ? -t : t;
778 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
779 (T1_COLFLAG_RBS * row)));
781 } /* VSC and BYPASS by Antonin */
783 static INLINE void opj_t1_dec_refpass_step_mqc(
785 #ifdef CONSISTENCY_CHECK
788 opj_flag_t *flagsp_unused,
790 opj_colflag_t *colflagsp,
798 opj_mqc_t *mqc = t1->mqc; /* MQC component */
799 #ifdef CONSISTENCY_CHECK
800 assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
801 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
802 (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
807 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
808 (T1_COLFLAG_RBS * row))) ==
809 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
810 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
811 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
812 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
813 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
814 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
815 v = opj_mqc_decode(mqc);
816 t = v ? poshalf : neghalf;
817 *datap += *datap < 0 ? -t : t;
818 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
819 (T1_COLFLAG_RBS * row)));
821 } /* VSC and BYPASS by Antonin */
823 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
826 opj_colflag_t *colflagsp,
836 opj_mqc_t *mqc = t1->mqc; /* MQC component */
838 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
839 (T1_COLFLAG_RBS * row))) ==
840 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
841 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
842 T1_SGN_S))) : (*flagsp);
843 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
844 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
845 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
846 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
847 v = (OPJ_UINT32)opj_mqc_decode(mqc);
848 t = v ? poshalf : neghalf;
849 *datap += *datap < 0 ? -t : t;
850 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
851 (T1_COLFLAG_RBS * row)));
853 } /* VSC and BYPASS by Antonin */
856 static void opj_t1_enc_refpass(
863 OPJ_UINT32 i, j, k, vsc;
867 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
868 for (k = 0; k < t1->h; k += 4) {
869 for (i = 0; i < t1->w; ++i) {
870 for (j = k; j < k + 4 && j < t1->h; ++j) {
871 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
872 j == t1->h - 1)) ? 1 : 0;
873 opj_t1_enc_refpass_step(
875 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
876 &t1->data[(j * t1->data_stride) + i],
887 static void opj_t1_dec_refpass_raw(
891 OPJ_INT32 one, poshalf, neghalf;
893 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
896 neghalf = bpno > 0 ? -poshalf : -1;
897 for (k = 0; k < t1->h; k += 4) {
898 for (i = 0; i < t1->w; ++i) {
899 opj_colflag_t *colflags2 = colflags1 + i;
900 for (j = k; j < k + 4 && j < t1->h; ++j) {
901 opj_t1_dec_refpass_step_raw(
904 &t1->data[(j * t1->w) + i],
909 colflags1 += t1->flags_stride;
911 } /* VSC and BYPASS by Antonin */
913 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
915 OPJ_INT32 one, poshalf, neghalf; \
916 OPJ_UINT32 i, j, k; \
917 OPJ_INT32 *data1 = t1->data; \
918 opj_flag_t *flags1 = &t1->flags[1]; \
919 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
921 poshalf = one >> 1; \
922 neghalf = bpno > 0 ? -poshalf : -1; \
923 for (k = 0; k < (h & ~3u); k += 4) { \
924 for (i = 0; i < w; ++i) { \
925 OPJ_INT32 *data2 = data1 + i; \
926 opj_flag_t *flags2 = flags1 + i; \
927 opj_colflag_t *colflags2 = colflags1 + i; \
928 if( *colflags2 == 0 ) continue; \
929 flags2 += flags_stride; \
930 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
932 flags2 += flags_stride; \
933 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
935 flags2 += flags_stride; \
936 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
938 flags2 += flags_stride; \
939 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
943 flags1 += flags_stride << 2; \
944 colflags1 += flags_stride; \
946 for (i = 0; i < w; ++i) { \
947 OPJ_INT32 *data2 = data1 + i; \
948 opj_flag_t *flags2 = flags1 + i; \
949 opj_colflag_t *colflags2 = colflags1 + i; \
950 for (j = k; j < h; ++j) { \
951 flags2 += flags_stride; \
952 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
958 static void opj_t1_dec_refpass_mqc_64x64(
962 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
965 static void opj_t1_dec_refpass_mqc_generic(
969 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
972 /* VSC and BYPASS by Antonin */
973 static void opj_t1_dec_refpass_mqc_vsc(
977 OPJ_INT32 one, poshalf, neghalf;
980 OPJ_INT32 *data1 = t1->data;
981 opj_flag_t *flags1 = &t1->flags[1];
982 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
985 neghalf = bpno > 0 ? -poshalf : -1;
986 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
987 for (i = 0; i < t1->w; ++i) {
988 OPJ_INT32 *data2 = data1 + i;
989 opj_flag_t *flags2 = flags1 + i;
990 opj_colflag_t *colflags2 = colflags1 + i;
991 flags2 += t1->flags_stride;
992 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
995 flags2 += t1->flags_stride;
996 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
999 flags2 += t1->flags_stride;
1000 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1003 flags2 += t1->flags_stride;
1004 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1008 data1 += t1->w << 2;
1009 flags1 += t1->flags_stride << 2;
1010 colflags1 += t1->flags_stride;
1012 for (i = 0; i < t1->w; ++i) {
1013 opj_colflag_t *colflags2 = colflags1 + i;
1014 for (j = k; j < t1->h; ++j) {
1015 vsc = (j == t1->h - 1) ? 1 : 0;
1016 opj_t1_dec_refpass_step_mqc_vsc(
1018 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1020 &t1->data[(j * t1->w) + i],
1026 } /* VSC and BYPASS by Antonin */
1029 static void opj_t1_enc_clnpass_step(
1042 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1044 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1045 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
1049 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1050 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1051 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1052 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1055 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1056 (OPJ_UINT32)(bpno));
1057 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1058 v = *datap < 0 ? 1 : 0;
1059 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1060 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1063 *flagsp &= (opj_flag_t)~T1_VISIT;
1066 static void opj_t1_dec_clnpass_step_partial(
1069 opj_colflag_t *colflagsp,
1071 OPJ_INT32 oneplushalf,
1075 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1078 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1079 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1080 *datap = v ? -oneplushalf : oneplushalf;
1081 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1083 #ifdef CONSISTENCY_CHECK
1084 *flagsp &= ~T1_VISIT;
1086 } /* VSC and BYPASS by Antonin */
1088 static void opj_t1_dec_clnpass_step(
1091 opj_colflag_t *colflagsp,
1093 OPJ_INT32 oneplushalf,
1098 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1099 #ifdef CONSISTENCY_CHECK
1100 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1101 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1103 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1106 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1107 if (opj_mqc_decode(mqc)) {
1108 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1109 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1110 *datap = v ? -oneplushalf : oneplushalf;
1111 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1115 #ifdef CONSISTENCY_CHECK
1116 *flagsp &= ~T1_VISIT;
1118 } /* VSC and BYPASS by Antonin */
1120 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1123 opj_colflag_t *colflagsp,
1125 OPJ_INT32 oneplushalf,
1127 OPJ_UINT32 flags_stride)
1132 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1135 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1137 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1138 if (opj_mqc_decode(mqc)) {
1139 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1140 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1141 *datap = v ? -oneplushalf : oneplushalf;
1142 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1145 /*flagsp &= ~T1_VISIT;*/
1148 static void opj_t1_dec_clnpass_step_vsc(
1151 opj_colflag_t *colflagsp,
1153 OPJ_INT32 oneplushalf,
1160 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1162 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1167 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1168 (T1_COLFLAG_RBS * row)))) {
1169 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1170 if (opj_mqc_decode(mqc)) {
1172 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1173 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1174 *datap = v ? -oneplushalf : oneplushalf;
1175 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1178 #ifdef CONSISTENCY_CHECK
1179 *flagsp &= ~T1_VISIT;
1183 static void opj_t1_enc_clnpass(
1191 OPJ_UINT32 agg, runlen, vsc;
1193 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1196 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1197 for (k = 0; k < t1->h; k += 4) {
1198 for (i = 0; i < t1->w; ++i) {
1199 if (k + 3 < t1->h) {
1200 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1201 agg = !(MACRO_t1_flags(1 + k, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1202 || MACRO_t1_flags(1 + k + 1, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1203 || MACRO_t1_flags(1 + k + 2, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1204 || (MACRO_t1_flags(1 + k + 3, 1 + i)
1205 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT |
1208 agg = !((MACRO_t1_flags(1 + k, 1 + i) |
1209 MACRO_t1_flags(1 + k + 1, 1 + i) |
1210 MACRO_t1_flags(1 + k + 2, 1 + i) |
1211 MACRO_t1_flags(1 + k + 3, 1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1217 for (runlen = 0; runlen < 4; ++runlen) {
1218 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1222 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1223 opj_mqc_encode(mqc, runlen != 4);
1227 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1228 opj_mqc_encode(mqc, runlen >> 1);
1229 opj_mqc_encode(mqc, runlen & 1);
1233 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1234 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
1235 j == t1->h - 1)) ? 1 : 0;
1236 opj_t1_enc_clnpass_step(
1238 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1239 &t1->data[(j * t1->data_stride) + i],
1243 agg && (j == k + runlen),
1250 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1252 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1254 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1255 OPJ_UINT32 i, j, k; \
1256 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1258 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1262 oneplushalf = one | half; \
1263 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1264 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1265 for (k = 0; k < h; k += 4) { \
1266 for (i = 0; i < w; ++i) { \
1267 opj_colflag_t *colflags2 = colflags1 + i; \
1269 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1270 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1271 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1272 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1273 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1274 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1279 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1280 if (!opj_mqc_decode(mqc)) { \
1283 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1284 runlen = opj_mqc_decode(mqc); \
1285 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1289 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1290 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1291 opj_t1_dec_clnpass_step_vsc( \
1293 &t1->flags[((j+1) * flags_stride) + i + 1], \
1295 &t1->data[(j * w) + i], \
1297 agg && (j == k + (OPJ_UINT32)runlen), \
1300 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1302 colflags1 += flags_stride; \
1305 OPJ_INT32 *data1 = t1->data; \
1306 opj_flag_t *flags1 = &t1->flags[1]; \
1307 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1308 for (k = 0; k < (h & ~3u); k += 4) { \
1309 for (i = 0; i < w; ++i) { \
1310 OPJ_INT32 *data2 = data1 + i; \
1311 opj_flag_t *flags2 = flags1 + i; \
1312 opj_colflag_t *colflags2 = colflags1 + i; \
1313 opj_colflag_t colflags = *colflags2; \
1314 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1315 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1316 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1317 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1318 if( consistency_check ) { \
1319 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1320 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1321 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1322 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1325 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1326 if (!opj_mqc_decode(mqc)) { \
1329 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1330 runlen = opj_mqc_decode(mqc); \
1331 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1332 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1333 data2 += (OPJ_UINT32)runlen * w; \
1334 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1335 flags2 += flags_stride; \
1336 if (j == (OPJ_UINT32)runlen) { \
1337 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1339 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1344 flags2 += flags_stride; \
1345 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1346 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1347 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1349 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1351 flags2 += flags_stride; \
1352 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1353 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1354 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1356 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1358 flags2 += flags_stride; \
1359 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1360 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1361 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1363 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1365 flags2 += flags_stride; \
1366 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1367 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1368 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1370 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1373 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1376 flags1 += flags_stride << 2; \
1377 colflags1 += flags_stride; \
1379 for (i = 0; i < w; ++i) { \
1380 OPJ_INT32 *data2 = data1 + i; \
1381 opj_flag_t *flags2 = flags1 + i; \
1382 opj_colflag_t *colflags2 = colflags1 + i; \
1383 for (j = k; j < h; ++j) { \
1384 flags2 += flags_stride; \
1385 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1388 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1394 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1395 v = opj_mqc_decode(mqc); \
1396 v = (v << 1) | opj_mqc_decode(mqc); \
1397 v = (v << 1) | opj_mqc_decode(mqc); \
1398 v = (v << 1) | opj_mqc_decode(mqc); \
1401 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1405 } /* VSC and BYPASS by Antonin */
1407 static void opj_t1_dec_clnpass_64x64(
1412 #ifdef CONSISTENCY_CHECK
1413 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1415 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1419 static void opj_t1_dec_clnpass_generic(
1424 #ifdef CONSISTENCY_CHECK
1425 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1428 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1434 /** mod fixed_quality */
1435 static OPJ_FLOAT64 opj_t1_getwmsedec(
1442 OPJ_FLOAT64 stepsize,
1443 OPJ_UINT32 numcomps,
1444 const OPJ_FLOAT64 * mct_norms,
1445 OPJ_UINT32 mct_numcomps)
1447 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1448 OPJ_ARG_NOT_USED(numcomps);
1450 if (mct_norms && (compno < mct_numcomps)) {
1451 w1 = mct_norms[compno];
1455 w2 = opj_dwt_getnorm(level, orient);
1456 } else { /* if (qmfbid == 0) */
1457 w2 = opj_dwt_getnorm_real(level, orient);
1460 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1461 wmsedec *= wmsedec * nmsedec / 8192.0;
1466 static OPJ_BOOL opj_t1_allocate_buffers(
1471 /* encoder uses tile buffer, so no need to allocate */
1475 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1476 /* Overflow check */
1477 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1478 /* FIXME event manager error callback */
1482 datasize = (size_t)w * h;
1484 /* Overflow check */
1485 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1486 /* FIXME event manager error callback */
1490 if (datasize > (size_t)t1->datasize) {
1491 opj_aligned_free(t1->data);
1492 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1494 /* FIXME event manager error callback */
1497 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1498 /* TODO remove this if t1->datasize type changes to size_t */
1499 /* Overflow check */
1500 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1501 /* FIXME event manager error callback */
1505 t1->datasize = (OPJ_UINT32)datasize;
1507 /* memset first arg is declared to never be null by gcc */
1508 if (t1->data != NULL) {
1509 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1516 /* Overflow check */
1517 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1518 /* FIXME event manager error callback */
1521 t1->flags_stride = w + 2U; /* can't be 0U */
1523 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1524 /* Overflow check */
1525 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1526 /* FIXME event manager error callback */
1530 flagssize = (size_t)h + 3U;
1532 /* Overflow check */
1533 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1534 /* FIXME event manager error callback */
1537 flagssize *= (size_t)t1->flags_stride;
1539 if (flagssize > (size_t)t1->flagssize) {
1540 /* Overflow check */
1541 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1542 /* FIXME event manager error callback */
1545 opj_aligned_free(t1->flags);
1546 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1548 /* FIXME event manager error callback */
1551 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1552 /* TODO remove this if t1->flagssize type changes to size_t */
1553 /* Overflow check */
1554 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1555 /* FIXME event manager error callback */
1559 t1->flagssize = (OPJ_UINT32)flagssize;
1561 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1564 size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1565 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1567 /* Overflow check */
1568 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1569 /* FIXME event manager error callback */
1572 colflags_size *= (size_t)t1->flags_stride;
1574 if (colflags_size > (size_t)t1->colflags_size) {
1575 /* Overflow check */
1576 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1577 /* FIXME event manager error callback */
1580 opj_aligned_free(t1->colflags);
1581 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1583 if (!t1->colflags) {
1584 /* FIXME event manager error callback */
1587 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1588 /* TODO remove this if t1->colflags_size type changes to size_t */
1589 /* Overflow check */
1590 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1591 /* FIXME event manager error callback */
1595 t1->colflags_size = (OPJ_UINT32)colflags_size;
1597 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1606 /* ----------------------------------------------------------------------- */
1608 /* ----------------------------------------------------------------------- */
1610 * Creates a new Tier 1 handle
1611 * and initializes the look-up tables of the Tier-1 coder/decoder
1612 * @return a new T1 handle if successful, returns NULL otherwise
1614 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1616 opj_t1_t *l_t1 = 00;
1618 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1623 /* create MQC and RAW handles */
1624 l_t1->mqc = opj_mqc_create();
1626 opj_t1_destroy(l_t1);
1630 l_t1->raw = opj_raw_create();
1632 opj_t1_destroy(l_t1);
1635 l_t1->encoder = isEncoder;
1642 * Destroys a previously created T1 handle
1644 * @param p_t1 Tier 1 handle to destroy
1646 void opj_t1_destroy(opj_t1_t *p_t1)
1652 /* destroy MQC and RAW handles */
1653 opj_mqc_destroy(p_t1->mqc);
1655 opj_raw_destroy(p_t1->raw);
1658 /* encoder uses tile buffer, so no need to free */
1659 if (!p_t1->encoder && p_t1->data) {
1660 opj_aligned_free(p_t1->data);
1665 opj_aligned_free(p_t1->flags);
1669 if (p_t1->colflags) {
1670 opj_aligned_free(p_t1->colflags);
1671 p_t1->colflags = 00;
1678 opj_tcd_cblk_dec_t* cblk;
1679 opj_tcd_band_t* band;
1680 opj_tcd_tilecomp_t* tilec;
1682 volatile OPJ_BOOL* pret;
1683 } opj_t1_cblk_decode_processing_job_t;
1685 static void opj_t1_destroy_wrapper(void* t1)
1687 opj_t1_destroy((opj_t1_t*) t1);
1690 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1692 opj_tcd_cblk_dec_t* cblk;
1693 opj_tcd_band_t* band;
1694 opj_tcd_tilecomp_t* tilec;
1696 OPJ_INT32* OPJ_RESTRICT datap;
1697 OPJ_UINT32 cblk_w, cblk_h;
1700 opj_t1_cblk_decode_processing_job_t* job;
1705 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1711 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1713 if (!*(job->pret)) {
1718 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1720 t1 = opj_t1_create(OPJ_FALSE);
1721 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1724 if (OPJ_FALSE == opj_t1_decode_cblk(
1728 (OPJ_UINT32)tccp->roishift,
1730 *(job->pret) = OPJ_FALSE;
1735 x = cblk->x0 - band->x0;
1736 y = cblk->y0 - band->y0;
1737 if (band->bandno & 1) {
1738 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1739 x += pres->x1 - pres->x0;
1741 if (band->bandno & 2) {
1742 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1743 y += pres->y1 - pres->y0;
1750 if (tccp->roishift) {
1751 OPJ_INT32 thresh = 1 << tccp->roishift;
1752 for (j = 0; j < cblk_h; ++j) {
1753 for (i = 0; i < cblk_w; ++i) {
1754 OPJ_INT32 val = datap[(j * cblk_w) + i];
1755 OPJ_INT32 mag = abs(val);
1756 if (mag >= thresh) {
1757 mag >>= tccp->roishift;
1758 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1763 if (tccp->qmfbid == 1) {
1764 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1766 for (j = 0; j < cblk_h; ++j) {
1768 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1769 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1770 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1771 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1772 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1773 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1774 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1775 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1776 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1778 for (; i < cblk_w; ++i) {
1779 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1780 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1783 } else { /* if (tccp->qmfbid == 0) */
1784 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1785 tile_w + (OPJ_UINT32)x];
1786 for (j = 0; j < cblk_h; ++j) {
1787 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1788 for (i = 0; i < cblk_w; ++i) {
1789 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1802 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1803 volatile OPJ_BOOL* pret,
1804 opj_tcd_tilecomp_t* tilec,
1808 OPJ_UINT32 resno, bandno, precno, cblkno;
1810 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1811 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1813 for (bandno = 0; bandno < res->numbands; ++bandno) {
1814 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1816 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1817 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1819 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1820 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1821 opj_t1_cblk_decode_processing_job_t* job;
1823 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1824 sizeof(opj_t1_cblk_decode_processing_job_t));
1835 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1848 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1849 opj_tcd_cblk_dec_t* cblk,
1851 OPJ_UINT32 roishift,
1854 opj_raw_t *raw = t1->raw; /* RAW component */
1855 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1857 OPJ_INT32 bpno_plus_one;
1858 OPJ_UINT32 passtype;
1859 OPJ_UINT32 segno, passno;
1860 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1862 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1864 if (!opj_t1_allocate_buffers(
1866 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1867 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1871 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1874 opj_mqc_resetstates(mqc);
1875 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1876 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1877 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1879 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1880 opj_tcd_seg_t *seg = &cblk->segs[segno];
1883 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1884 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1885 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1886 if (seg->data == 00) {
1889 if (type == T1_TYPE_RAW) {
1890 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1892 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
1898 if (t1->w == 64 && t1->h == 64) {
1899 for (passno = 0; (passno < seg->real_num_passes) &&
1900 (bpno_plus_one >= 1); ++passno) {
1903 if (type == T1_TYPE_RAW) {
1904 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1906 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1907 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1909 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1914 if (type == T1_TYPE_RAW) {
1915 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1917 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1918 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1920 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1925 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1929 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1930 opj_mqc_resetstates(mqc);
1931 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1932 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1933 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1935 if (++passtype == 3) {
1941 for (passno = 0; (passno < seg->real_num_passes) &&
1942 (bpno_plus_one >= 1); ++passno) {
1945 if (type == T1_TYPE_RAW) {
1946 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1948 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1949 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1951 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1956 if (type == T1_TYPE_RAW) {
1957 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1959 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1960 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1962 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1967 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1971 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1972 opj_mqc_resetstates(mqc);
1973 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1974 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1975 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1977 if (++passtype == 3) {
1990 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1991 opj_tcd_tile_t *tile,
1993 const OPJ_FLOAT64 * mct_norms,
1994 OPJ_UINT32 mct_numcomps
1997 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1999 tile->distotile = 0; /* fixed_quality */
2001 for (compno = 0; compno < tile->numcomps; ++compno) {
2002 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2003 opj_tccp_t* tccp = &tcp->tccps[compno];
2004 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2006 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2007 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2009 for (bandno = 0; bandno < res->numbands; ++bandno) {
2010 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2011 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2013 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2014 opj_tcd_precinct_t *prc = &band->precincts[precno];
2016 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2017 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2018 OPJ_INT32* OPJ_RESTRICT tiledp;
2021 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2023 OPJ_INT32 x = cblk->x0 - band->x0;
2024 OPJ_INT32 y = cblk->y0 - band->y0;
2025 if (band->bandno & 1) {
2026 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2027 x += pres->x1 - pres->x0;
2029 if (band->bandno & 2) {
2030 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2031 y += pres->y1 - pres->y0;
2034 if (!opj_t1_allocate_buffers(
2036 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2037 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2043 tileLineAdvance = tile_w - cblk_w;
2045 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2047 t1->data_stride = tile_w;
2048 if (tccp->qmfbid == 1) {
2049 for (j = 0; j < cblk_h; ++j) {
2050 for (i = 0; i < cblk_w; ++i) {
2051 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2054 tileIndex += tileLineAdvance;
2056 } else { /* if (tccp->qmfbid == 0) */
2057 for (j = 0; j < cblk_h; ++j) {
2058 for (i = 0; i < cblk_w; ++i) {
2059 OPJ_INT32 tmp = tiledp[tileIndex];
2066 tileIndex += tileLineAdvance;
2075 tilec->numresolutions - 1 - resno,
2092 /* Returns whether the pass (bpno, passtype) is terminated */
2093 static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
2096 OPJ_UINT32 passtype)
2098 /* Is it the last cleanup pass ? */
2099 if (passtype == 2 && bpno == 0) {
2103 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
2107 if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2108 /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
2109 if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
2112 /* and beyond terminate all the magnitude refinement passes (in raw) */
2113 /* and cleanup passes (in MQC) */
2114 if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
2123 /** mod fixed_quality */
2124 static void opj_t1_encode_cblk(opj_t1_t *t1,
2125 opj_tcd_cblk_enc_t* cblk,
2130 OPJ_FLOAT64 stepsize,
2132 OPJ_UINT32 numcomps,
2133 opj_tcd_tile_t * tile,
2134 const OPJ_FLOAT64 * mct_norms,
2135 OPJ_UINT32 mct_numcomps)
2137 OPJ_FLOAT64 cumwmsedec = 0.0;
2139 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2143 OPJ_UINT32 passtype;
2144 OPJ_INT32 nmsedec = 0;
2147 OPJ_BYTE type = T1_TYPE_MQ;
2148 OPJ_FLOAT64 tempwmsedec;
2151 printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
2152 cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
2155 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2158 for (i = 0; i < t1->w; ++i) {
2159 for (j = 0; j < t1->h; ++j) {
2160 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2161 max = opj_int_max(max, tmp);
2165 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2166 T1_NMSEDEC_FRACBITS) : 0;
2168 bpno = (OPJ_INT32)(cblk->numbps - 1);
2171 opj_mqc_resetstates(mqc);
2172 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2173 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2174 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2175 opj_mqc_init_enc(mqc, cblk->data);
2177 for (passno = 0; bpno >= 0; ++passno) {
2178 opj_tcd_pass_t *pass = &cblk->passes[passno];
2179 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2180 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2182 /* If the previous pass was terminating, we need to reset the encoder */
2183 if (passno > 0 && cblk->passes[passno - 1].term) {
2184 if (type == T1_TYPE_RAW) {
2185 opj_mqc_bypass_init_enc(mqc);
2187 opj_mqc_restart_init_enc(mqc);
2193 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2196 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2199 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2200 /* code switch SEGMARK (i.e. SEGSYM) */
2201 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2202 opj_mqc_segmark_enc(mqc);
2208 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2209 stepsize, numcomps, mct_norms, mct_numcomps) ;
2210 cumwmsedec += tempwmsedec;
2211 tile->distotile += tempwmsedec;
2212 pass->distortiondec = cumwmsedec;
2214 if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
2215 /* If it is a terminated pass, terminate it */
2216 if (type == T1_TYPE_RAW) {
2217 opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
2219 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2220 opj_mqc_erterm_enc(mqc);
2226 pass->rate = opj_mqc_numbytes(mqc);
2228 /* Non terminated pass */
2229 OPJ_UINT32 rate_extra_bytes;
2230 if (type == T1_TYPE_RAW) {
2231 rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
2232 mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
2234 rate_extra_bytes = 3;
2237 pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
2240 if (++passtype == 3) {
2245 /* Code-switch "RESET" */
2246 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2247 opj_mqc_reset_enc(mqc);
2251 cblk->totalpasses = passno;
2253 if (cblk->totalpasses) {
2254 /* Make sure that pass rates are increasing */
2255 OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
2256 for (passno = cblk->totalpasses; passno > 0;) {
2257 opj_tcd_pass_t *pass = &cblk->passes[--passno];
2258 if (pass->rate > last_pass_rate) {
2259 pass->rate = last_pass_rate;
2261 last_pass_rate = pass->rate;
2266 for (passno = 0; passno < cblk->totalpasses; passno++) {
2267 opj_tcd_pass_t *pass = &cblk->passes[passno];
2269 /* Prevent generation of FF as last data byte of a pass*/
2270 /* For terminating passes, the flushing procedure ensured this already */
2271 assert(pass->rate > 0);
2272 if ((cblk->data[pass->rate - 1] == 0xFF)) {
2275 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2279 printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
2281 /* Check that there not 0xff >=0x90 sequences */
2282 if (cblk->totalpasses) {
2284 OPJ_UINT32 len = opj_mqc_numbytes(mqc);
2285 for (i = 1; i < len; ++i) {
2286 if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
2287 printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);
2296 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
2307 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2308 opj_raw_t *raw = t1->raw; /* RAW component */
2310 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2312 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2313 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2314 if (type == T1_TYPE_RAW) {
2315 v = opj_raw_decode(raw);
2317 v = opj_mqc_decode(mqc);
2319 t = v ? poshalf : neghalf;
2320 *datap += *datap < 0 ? -t : t;
2321 *flagsp |= T1_REFINE;
2323 } /* VSC and BYPASS by Antonin */
2329 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
2333 OPJ_INT32 oneplushalf,
2339 opj_raw_t *raw = t1->raw; /* RAW component */
2340 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2342 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2344 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2345 if (type == T1_TYPE_RAW) {
2346 if (opj_raw_decode(raw)) {
2347 v = opj_raw_decode(raw); /* ESSAI */
2348 *datap = v ? -oneplushalf : oneplushalf;
2349 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2352 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2353 if (opj_mqc_decode(mqc)) {
2354 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2355 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2356 *datap = v ? -oneplushalf : oneplushalf;
2357 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2360 *flagsp |= T1_VISIT;
2362 } /* VSC and BYPASS by Antonin */