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 * Copyright (c) 2012, Carl Hetherington
16 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
44 /* #define CONSISTENCY_CHECK */
46 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
49 #define ENC_FLAGS(x, y) (t1->enc_flags[x + 1 + ((y / 4) + 1) * t1->flags_stride])
51 /** @name Local static functions */
54 static INLINE OPJ_BYTE opj_t1_enc_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
55 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
56 static OPJ_BYTE opj_t1_enc_getctxno_sc(OPJ_UINT32 fX, OPJ_UINT32 pfX,
57 OPJ_UINT32 nfX, OPJ_UINT32 ci);
58 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
59 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f);
60 static OPJ_BYTE opj_t1_enc_getspb(OPJ_UINT32 fX, OPJ_UINT32 pfX, OPJ_UINT32 nfX,
62 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
63 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
64 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
65 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
66 OPJ_UINT32 s, OPJ_UINT32 stride);
67 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
72 Decode significant pass
75 static INLINE void opj_t1_dec_sigpass_step_raw(
78 opj_colflag_t* colflagsp,
80 OPJ_INT32 oneplushalf,
83 static INLINE void opj_t1_dec_sigpass_step_mqc(
86 opj_colflag_t* colflagsp,
88 OPJ_INT32 oneplushalf,
90 OPJ_UINT32 flags_stride);
91 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
94 opj_colflag_t* colflagsp,
96 OPJ_INT32 oneplushalf,
102 Encode significant pass
104 static void opj_t1_enc_sigpass(opj_t1_t *t1,
112 Decode significant pass
114 static void opj_t1_dec_sigpass_raw(
118 static void opj_t1_dec_sigpass_mqc_vsc(
125 Encode refinement pass
127 static void opj_t1_enc_refpass(opj_t1_t *t1,
134 Decode refinement pass
136 static void opj_t1_dec_refpass_raw(
139 static void opj_t1_dec_refpass_mqc_vsc(
145 Decode refinement pass
148 static INLINE void opj_t1_dec_refpass_step_raw(
150 opj_colflag_t *colflagsp,
155 static INLINE void opj_t1_dec_refpass_step_mqc(
158 opj_colflag_t *colflagsp,
163 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
166 opj_colflag_t *colflagsp,
177 static void opj_t1_dec_clnpass_step_partial(
180 opj_colflag_t *colflagsp,
182 OPJ_INT32 oneplushalf,
184 static void opj_t1_dec_clnpass_step(
187 opj_colflag_t *colflagsp,
189 OPJ_INT32 oneplushalf,
191 static void opj_t1_dec_clnpass_step_vsc(
194 opj_colflag_t *colflagsp,
196 OPJ_INT32 oneplushalf,
203 static void opj_t1_enc_clnpass(
210 static OPJ_FLOAT64 opj_t1_getwmsedec(
217 OPJ_FLOAT64 stepsize,
219 const OPJ_FLOAT64 * mct_norms,
220 OPJ_UINT32 mct_numcomps);
222 static void opj_t1_encode_cblk(opj_t1_t *t1,
223 opj_tcd_cblk_enc_t* cblk,
228 OPJ_FLOAT64 stepsize,
231 opj_tcd_tile_t * tile,
232 const OPJ_FLOAT64 * mct_norms,
233 OPJ_UINT32 mct_numcomps);
238 @param cblk Code-block coding parameters
240 @param roishift Region of interest shifting value
241 @param cblksty Code-block style
243 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
244 opj_tcd_cblk_dec_t* cblk,
249 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
257 /* ----------------------------------------------------------------------- */
259 static INLINE OPJ_BYTE opj_t1_enc_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
261 return lut_enc_ctxno_zc[(orient << 9) | (f & T1_SIGMA_NEIGHBOURS)];
264 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
266 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
270 static OPJ_BYTE opj_t1_enc_getctxno_sc(OPJ_UINT32 fX, OPJ_UINT32 pfX,
271 OPJ_UINT32 nfX, OPJ_UINT32 ci)
274 0 pfX T1_CHI_THIS T1_LUT_CTXNO_SGN_W
275 1 tfX T1_SIGMA_1 T1_LUT_CTXNO_SIG_N
276 2 nfX T1_CHI_THIS T1_LUT_CTXNO_SGN_E
277 3 tfX T1_SIGMA_3 T1_LUT_CTXNO_SIG_W
278 4 fX T1_CHI_(THIS - 1) T1_LUT_CTXNO_SGN_N
279 5 tfX T1_SIGMA_5 T1_LUT_CTXNO_SIG_E
280 6 fX T1_CHI_(THIS + 1) T1_LUT_CTXNO_SGN_S
281 7 tfX T1_SIGMA_7 T1_LUT_CTXNO_SIG_S
284 OPJ_UINT32 lu = (fX >> (ci * 3)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
287 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
288 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
290 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
292 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
294 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
296 return lut_enc_ctxno_sc[lu];
299 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f)
301 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
304 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f)
306 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
307 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
311 static OPJ_BYTE opj_t1_enc_getspb(OPJ_UINT32 fX, OPJ_UINT32 pfX, OPJ_UINT32 nfX,
315 0 pfX T1_CHI_THIS T1_LUT_SGN_W
316 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
317 2 nfX T1_CHI_THIS T1_LUT_SGN_E
318 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
319 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
320 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
321 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
322 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
325 int lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
328 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
329 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
331 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
333 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
335 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
337 return lut_enc_spb[lu];
340 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f)
342 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
345 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
348 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
351 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
354 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
357 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
360 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
364 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
365 OPJ_UINT32 s, OPJ_UINT32 stride)
367 /* set up to point to the north and south data points' flags words, if required */
368 opj_flag_enc_t* north;
369 opj_flag_enc_t* south;
371 /* mark target as significant */
372 *flagsp |= T1_SIGMA_4 << (3U * ci);
374 /* north-west, north, north-east */
376 north = flagsp - stride;
377 *north |= T1_SIGMA_16;
378 north[-1] |= T1_SIGMA_17;
379 north[1] |= T1_SIGMA_15;
382 /* south-west, south, south-east */
384 south = flagsp + stride;
385 *south |= T1_SIGMA_1;
386 south[-1] |= T1_SIGMA_2;
387 south[1] |= T1_SIGMA_0;
391 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
394 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
419 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
422 opj_flag_t *np = flagsp - stride;
423 opj_flag_t *sp = flagsp + stride;
425 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
426 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
427 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
429 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
432 np[0] |= (opj_flag_t)(flag_N << 2U);
435 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
436 #ifdef CONSISTENCY_CHECK
439 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
442 sp[0] = (opj_flag_t)(sp[0] | flag_N);
446 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp,
447 opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
449 opj_t1_updateflags(flagsp, s, stride);
451 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
452 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
453 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
454 (T1_COLFLAG_RBS * row)) |
455 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
456 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
457 (T1_COLFLAG_RBS * row)) |
458 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
459 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
460 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
461 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
462 } else if (row == 3U) {
463 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
464 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
465 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
466 (T1_COLFLAG_RBS * row)) |
467 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
468 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
469 (T1_COLFLAG_RBS * row)) |
470 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1))));
471 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
472 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
473 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
475 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
476 (T1_COLFLAG_RBS * row)) |
477 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
478 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
479 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
480 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
481 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
482 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
483 (T1_COLFLAG_RBS * row)) |
484 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
485 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
490 Encode significant pass
492 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
493 opj_flag_enc_t *flagsp,
506 opj_mqc_t *mqc = t1->mqc; /* MQC component */
508 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
509 T1_CHI_S) << (ci * 3U)) : ~0U;
510 OPJ_UINT32 const flags = *flagsp & vsc_mask;
512 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
513 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
514 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U),
516 v = opj_int_abs(*datap) & one ? 1 : 0;
518 fprintf(stderr, " ctxt1=%d\n", ctxt1);
520 opj_mqc_setcurctx(mqc, ctxt1); /* ESSAI */
521 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
522 opj_mqc_bypass_enc(mqc, v);
524 opj_mqc_encode(mqc, v);
527 OPJ_UINT32 ctxt2 = opj_t1_enc_getctxno_sc(*flagsp & vsc_mask,
528 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
530 v = *datap < 0 ? 1 : 0;
531 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno);
533 fprintf(stderr, " ctxt2=%d\n", ctxt2);
535 opj_mqc_setcurctx(mqc, ctxt2); /* ESSAI */
536 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
537 opj_mqc_bypass_enc(mqc, v);
539 OPJ_UINT32 spb = opj_t1_enc_getspb(*flagsp & vsc_mask,
540 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask, ci);
542 fprintf(stderr, " spb=%d\n", spb);
544 opj_mqc_encode(mqc, v ^ spb);
546 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
548 *flagsp |= T1_PI_THIS << (ci * 3U);
552 static INLINE void opj_t1_dec_sigpass_step_raw(
555 opj_colflag_t* colflagsp,
557 OPJ_INT32 oneplushalf,
562 opj_raw_t *raw = t1->raw; /* RAW component */
564 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
566 if ((flag & T1_SIG_OTH) &&
567 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
568 (T1_COLFLAG_RBS * row)))) {
569 if (opj_raw_decode(raw)) {
570 v = opj_raw_decode(raw); /* ESSAI */
571 *datap = v ? -oneplushalf : oneplushalf;
572 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
574 #ifdef CONSISTENCY_CHECK
577 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
578 (T1_COLFLAG_RBS * row)));
582 static INLINE void opj_t1_dec_sigpass_step_mqc(
585 opj_colflag_t* colflagsp,
587 OPJ_INT32 oneplushalf,
589 OPJ_UINT32 flags_stride)
593 opj_mqc_t *mqc = t1->mqc; /* MQC component */
594 #ifdef CONSISTENCY_CHECK
595 assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
596 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
597 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
598 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
600 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
601 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
602 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
604 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
605 if (opj_mqc_decode(mqc)) {
606 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
607 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
608 *datap = v ? -oneplushalf : oneplushalf;
609 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
611 #ifdef CONSISTENCY_CHECK
614 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
615 (T1_COLFLAG_RBS * row)));
617 } /* VSC and BYPASS by Antonin */
619 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
622 opj_colflag_t* colflagsp,
624 OPJ_INT32 oneplushalf,
630 opj_mqc_t *mqc = t1->mqc; /* MQC component */
632 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
634 if ((flag & T1_SIG_OTH) &&
635 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
636 (T1_COLFLAG_RBS * row)))) {
637 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
638 if (opj_mqc_decode(mqc)) {
639 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
640 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
641 *datap = v ? -oneplushalf : oneplushalf;
642 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
644 #ifdef CONSISTENCY_CHECK
647 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
648 (T1_COLFLAG_RBS * row)));
650 } /* VSC and BYPASS by Antonin */
653 static void opj_t1_enc_sigpass(opj_t1_t *t1,
662 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
663 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
664 OPJ_UINT32 const extra = t1->flags_stride - t1->w;
668 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
670 for (k = 0; k < (t1->h & ~3U); k += 4) {
672 fprintf(stderr, " k=%d\n", k);
674 for (i = 0; i < t1->w; ++i) {
676 fprintf(stderr, " i=%d\n", i);
679 /* Nothing to do for any of the 4 data points */
683 opj_t1_enc_sigpass_step(
686 &t1->data[((k + 0) * t1->data_stride) + i],
693 opj_t1_enc_sigpass_step(
696 &t1->data[((k + 1) * t1->data_stride) + i],
703 opj_t1_enc_sigpass_step(
706 &t1->data[((k + 2) * t1->data_stride) + i],
713 opj_t1_enc_sigpass_step(
716 &t1->data[((k + 3) * t1->data_stride) + i],
722 3, cblksty & J2K_CCP_CBLKSTY_VSC);
731 fprintf(stderr, " k=%d\n", k);
733 for (i = 0; i < t1->w; ++i) {
735 fprintf(stderr, " i=%d\n", i);
738 /* Nothing to do for any of the 4 data points */
742 for (j = k; j < t1->h; ++j) {
743 opj_t1_enc_sigpass_step(
746 &t1->data[(j * t1->data_stride) + i],
753 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
760 static void opj_t1_dec_sigpass_raw(
765 OPJ_INT32 one, half, oneplushalf, vsc;
767 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
770 oneplushalf = one | half;
771 for (k = 0; k < t1->h; k += 4) {
772 for (i = 0; i < t1->w; ++i) {
773 opj_colflag_t *colflags2 = colflags1 + i;
774 for (j = k; j < k + 4 && j < t1->h; ++j) {
775 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
776 j == t1->h - 1)) ? 1 : 0;
777 opj_t1_dec_sigpass_step_raw(
779 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
781 &t1->data[(j * t1->w) + i],
787 colflags1 += t1->flags_stride;
789 } /* VSC and BYPASS by Antonin */
791 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
793 OPJ_INT32 one, half, oneplushalf; \
794 OPJ_UINT32 i, j, k; \
795 OPJ_INT32 *data1 = t1->data; \
796 opj_flag_t *flags1 = &t1->flags[1]; \
797 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
800 oneplushalf = one | half; \
801 for (k = 0; k < (h & ~3u); k += 4) { \
802 for (i = 0; i < w; ++i) { \
803 OPJ_INT32 *data2 = data1 + i; \
804 opj_flag_t *flags2 = flags1 + i; \
805 opj_colflag_t *colflags2 = colflags1 + i; \
806 if( *colflags2 == 0 ) continue; \
807 flags2 += flags_stride; \
808 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
810 flags2 += flags_stride; \
811 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
813 flags2 += flags_stride; \
814 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
816 flags2 += flags_stride; \
817 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
821 flags1 += flags_stride << 2; \
822 colflags1 += flags_stride; \
824 for (i = 0; i < w; ++i) { \
825 OPJ_INT32 *data2 = data1 + i; \
826 opj_flag_t *flags2 = flags1 + i; \
827 opj_colflag_t *colflags2 = colflags1 + i; \
828 for (j = k; j < h; ++j) { \
829 flags2 += flags_stride; \
830 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
836 static void opj_t1_dec_sigpass_mqc_64x64(
840 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
843 static void opj_t1_dec_sigpass_mqc_generic(
847 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
850 /* VSC and BYPASS by Antonin */
851 static void opj_t1_dec_sigpass_mqc_vsc(
855 OPJ_INT32 one, half, oneplushalf, vsc;
857 OPJ_INT32 *data1 = t1->data;
858 opj_flag_t *flags1 = &t1->flags[1];
859 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
862 oneplushalf = one | half;
863 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
864 for (i = 0; i < t1->w; ++i) {
865 OPJ_INT32 *data2 = data1 + i;
866 opj_flag_t *flags2 = flags1 + i;
867 opj_colflag_t *colflags2 = colflags1 + i;
868 flags2 += t1->flags_stride;
869 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
872 flags2 += t1->flags_stride;
873 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
876 flags2 += t1->flags_stride;
877 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
880 flags2 += t1->flags_stride;
881 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
886 flags1 += t1->flags_stride << 2;
887 colflags1 += t1->flags_stride;
889 for (i = 0; i < t1->w; ++i) {
890 opj_colflag_t *colflags2 = colflags1 + i;
891 for (j = k; j < t1->h; ++j) {
892 vsc = (j == t1->h - 1) ? 1 : 0;
893 opj_t1_dec_sigpass_step_mqc_vsc(
895 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
897 &t1->data[(j * t1->w) + i],
903 } /* VSC and BYPASS by Antonin */
907 Encode refinement pass step
909 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
910 opj_flag_enc_t *flagsp,
922 opj_mqc_t *mqc = t1->mqc; /* MQC component */
924 OPJ_UINT32 const shift_flags =
926 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
928 (*flagsp >> (ci * 3U));
930 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
931 OPJ_UINT32 ctxt = opj_t1_enc_getctxno_mag(shift_flags);
932 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno);
933 v = opj_int_abs(*datap) & one ? 1 : 0;
935 fprintf(stderr, " ctxt=%d\n", ctxt);
937 opj_mqc_setcurctx(mqc, ctxt); /* ESSAI */
938 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
939 opj_mqc_bypass_enc(mqc, v);
941 opj_mqc_encode(mqc, v);
943 *flagsp |= T1_MU_THIS << (ci * 3U);
948 static INLINE void opj_t1_dec_refpass_step_raw(
950 opj_colflag_t *colflagsp,
958 opj_raw_t *raw = t1->raw; /* RAW component */
960 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
961 (T1_COLFLAG_RBS * row))) ==
962 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
963 v = (OPJ_INT32)opj_raw_decode(raw);
964 t = v ? poshalf : neghalf;
965 *datap += *datap < 0 ? -t : t;
966 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
967 (T1_COLFLAG_RBS * row)));
969 } /* VSC and BYPASS by Antonin */
971 static INLINE void opj_t1_dec_refpass_step_mqc(
973 #ifdef CONSISTENCY_CHECK
976 opj_flag_t *flagsp_unused,
978 opj_colflag_t *colflagsp,
986 opj_mqc_t *mqc = t1->mqc; /* MQC component */
987 #ifdef CONSISTENCY_CHECK
988 assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
989 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
990 (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
995 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
996 (T1_COLFLAG_RBS * row))) ==
997 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
998 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
999 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
1000 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
1001 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
1002 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
1003 v = opj_mqc_decode(mqc);
1004 t = v ? poshalf : neghalf;
1005 *datap += *datap < 0 ? -t : t;
1006 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
1007 (T1_COLFLAG_RBS * row)));
1009 } /* VSC and BYPASS by Antonin */
1011 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
1014 opj_colflag_t *colflagsp,
1024 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1026 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1027 (T1_COLFLAG_RBS * row))) ==
1028 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
1029 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1030 T1_SGN_S))) : (*flagsp);
1031 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
1032 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
1033 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
1034 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
1035 v = (OPJ_UINT32)opj_mqc_decode(mqc);
1036 t = v ? poshalf : neghalf;
1037 *datap += *datap < 0 ? -t : t;
1038 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
1039 (T1_COLFLAG_RBS * row)));
1041 } /* VSC and BYPASS by Antonin */
1043 static void opj_t1_enc_refpass(
1051 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1052 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
1053 const OPJ_UINT32 extra = t1->flags_stride - t1->w;
1056 #ifdef DEBUG_ENC_REF
1057 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
1059 for (k = 0; k < (t1->h & ~3U); k += 4) {
1060 #ifdef DEBUG_ENC_REF
1061 fprintf(stderr, " k=%d\n", k);
1063 for (i = 0; i < t1->w; ++i) {
1064 #ifdef DEBUG_ENC_REF
1065 fprintf(stderr, " i=%d\n", i);
1067 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1068 /* none significant */
1072 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
1073 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
1074 /* all processed by sigpass */
1079 opj_t1_enc_refpass_step(
1082 &t1->data[((k + 0) * t1->data_stride) + i],
1088 opj_t1_enc_refpass_step(
1091 &t1->data[((k + 1) * t1->data_stride) + i],
1097 opj_t1_enc_refpass_step(
1100 &t1->data[((k + 2) * t1->data_stride) + i],
1106 opj_t1_enc_refpass_step(
1109 &t1->data[((k + 3) * t1->data_stride) + i],
1114 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1122 #ifdef DEBUG_ENC_REF
1123 fprintf(stderr, " k=%d\n", k);
1125 for (i = 0; i < t1->w; ++i) {
1126 #ifdef DEBUG_ENC_REF
1127 fprintf(stderr, " i=%d\n", i);
1129 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1130 /* none significant */
1134 for (j = k; j < t1->h; ++j) {
1135 opj_t1_enc_refpass_step(
1138 &t1->data[(j * t1->data_stride) + i],
1144 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1152 static void opj_t1_dec_refpass_raw(
1156 OPJ_INT32 one, poshalf, neghalf;
1158 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1161 neghalf = bpno > 0 ? -poshalf : -1;
1162 for (k = 0; k < t1->h; k += 4) {
1163 for (i = 0; i < t1->w; ++i) {
1164 opj_colflag_t *colflags2 = colflags1 + i;
1165 for (j = k; j < k + 4 && j < t1->h; ++j) {
1166 opj_t1_dec_refpass_step_raw(
1169 &t1->data[(j * t1->w) + i],
1174 colflags1 += t1->flags_stride;
1176 } /* VSC and BYPASS by Antonin */
1178 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1180 OPJ_INT32 one, poshalf, neghalf; \
1181 OPJ_UINT32 i, j, k; \
1182 OPJ_INT32 *data1 = t1->data; \
1183 opj_flag_t *flags1 = &t1->flags[1]; \
1184 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1186 poshalf = one >> 1; \
1187 neghalf = bpno > 0 ? -poshalf : -1; \
1188 for (k = 0; k < (h & ~3u); k += 4) { \
1189 for (i = 0; i < w; ++i) { \
1190 OPJ_INT32 *data2 = data1 + i; \
1191 opj_flag_t *flags2 = flags1 + i; \
1192 opj_colflag_t *colflags2 = colflags1 + i; \
1193 if( *colflags2 == 0 ) continue; \
1194 flags2 += flags_stride; \
1195 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
1197 flags2 += flags_stride; \
1198 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
1200 flags2 += flags_stride; \
1201 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
1203 flags2 += flags_stride; \
1204 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
1208 flags1 += flags_stride << 2; \
1209 colflags1 += flags_stride; \
1211 for (i = 0; i < w; ++i) { \
1212 OPJ_INT32 *data2 = data1 + i; \
1213 opj_flag_t *flags2 = flags1 + i; \
1214 opj_colflag_t *colflags2 = colflags1 + i; \
1215 for (j = k; j < h; ++j) { \
1216 flags2 += flags_stride; \
1217 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
1223 static void opj_t1_dec_refpass_mqc_64x64(
1227 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1230 static void opj_t1_dec_refpass_mqc_generic(
1234 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
1237 /* VSC and BYPASS by Antonin */
1238 static void opj_t1_dec_refpass_mqc_vsc(
1242 OPJ_INT32 one, poshalf, neghalf;
1245 OPJ_INT32 *data1 = t1->data;
1246 opj_flag_t *flags1 = &t1->flags[1];
1247 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1250 neghalf = bpno > 0 ? -poshalf : -1;
1251 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1252 for (i = 0; i < t1->w; ++i) {
1253 OPJ_INT32 *data2 = data1 + i;
1254 opj_flag_t *flags2 = flags1 + i;
1255 opj_colflag_t *colflags2 = colflags1 + i;
1256 flags2 += t1->flags_stride;
1257 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1260 flags2 += t1->flags_stride;
1261 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1264 flags2 += t1->flags_stride;
1265 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1268 flags2 += t1->flags_stride;
1269 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1273 data1 += t1->w << 2;
1274 flags1 += t1->flags_stride << 2;
1275 colflags1 += t1->flags_stride;
1277 for (i = 0; i < t1->w; ++i) {
1278 opj_colflag_t *colflags2 = colflags1 + i;
1279 for (j = k; j < t1->h; ++j) {
1280 vsc = (j == t1->h - 1) ? 1 : 0;
1281 opj_t1_dec_refpass_step_mqc_vsc(
1283 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1285 &t1->data[(j * t1->w) + i],
1291 } /* VSC and BYPASS by Antonin */
1294 Encode clean-up pass step
1296 static void opj_t1_enc_clnpass_step(
1298 opj_flag_enc_t *flagsp,
1311 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1313 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1314 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1316 if ((*flagsp & check) == check) {
1318 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1319 } else if (runlen == 1) {
1320 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1321 } else if (runlen == 2) {
1322 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1323 } else if (runlen == 3) {
1324 *flagsp &= ~(T1_PI_3);
1329 for (ci = runlen; ci < lim; ++ci) {
1331 OPJ_UINT32 vsc_mask;
1332 opj_flag_enc_t flags;
1334 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1335 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1338 if ((agg != 0) && (ci == runlen)) {
1342 flags = *flagsp & vsc_mask;
1344 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1345 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U), orient);
1346 #ifdef DEBUG_ENC_CLN
1347 printf(" ctxt1=%d\n", ctxt1);
1349 opj_mqc_setcurctx(mqc, ctxt1);
1350 v = opj_int_abs(*datap) & one ? 1 : 0;
1351 opj_mqc_encode(mqc, v);
1353 OPJ_UINT32 ctxt2, spb;
1355 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno);
1356 ctxt2 = opj_t1_enc_getctxno_sc(*flagsp & vsc_mask,
1357 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1359 #ifdef DEBUG_ENC_CLN
1360 printf(" ctxt2=%d\n", ctxt2);
1362 opj_mqc_setcurctx(mqc, ctxt2);
1364 v = *datap < 0 ? 1 : 0;
1365 spb = opj_t1_enc_getspb(*flagsp & vsc_mask,
1366 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask, ci);
1367 #ifdef DEBUG_ENC_CLN
1368 printf(" spb=%d\n", spb);
1370 opj_mqc_encode(mqc, v ^ spb);
1371 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
1374 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1375 datap += t1->data_stride;
1380 static void opj_t1_dec_clnpass_step_partial(
1383 opj_colflag_t *colflagsp,
1385 OPJ_INT32 oneplushalf,
1389 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1392 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1393 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1394 *datap = v ? -oneplushalf : oneplushalf;
1395 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1397 #ifdef CONSISTENCY_CHECK
1398 *flagsp &= ~T1_VISIT;
1400 } /* VSC and BYPASS by Antonin */
1402 static void opj_t1_dec_clnpass_step(
1405 opj_colflag_t *colflagsp,
1407 OPJ_INT32 oneplushalf,
1412 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1413 #ifdef CONSISTENCY_CHECK
1414 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1415 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1417 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1420 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1421 if (opj_mqc_decode(mqc)) {
1422 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1423 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1424 *datap = v ? -oneplushalf : oneplushalf;
1425 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1429 #ifdef CONSISTENCY_CHECK
1430 *flagsp &= ~T1_VISIT;
1432 } /* VSC and BYPASS by Antonin */
1434 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1437 opj_colflag_t *colflagsp,
1439 OPJ_INT32 oneplushalf,
1441 OPJ_UINT32 flags_stride)
1446 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1449 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1451 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1452 if (opj_mqc_decode(mqc)) {
1453 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1454 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1455 *datap = v ? -oneplushalf : oneplushalf;
1456 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1459 /*flagsp &= ~T1_VISIT;*/
1462 static void opj_t1_dec_clnpass_step_vsc(
1465 opj_colflag_t *colflagsp,
1467 OPJ_INT32 oneplushalf,
1474 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1476 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1481 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1482 (T1_COLFLAG_RBS * row)))) {
1483 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1484 if (opj_mqc_decode(mqc)) {
1486 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1487 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1488 *datap = v ? -oneplushalf : oneplushalf;
1489 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1492 #ifdef CONSISTENCY_CHECK
1493 *flagsp &= ~T1_VISIT;
1497 #define MACRO_t1_enc_flags(x,y) t1->enc_flags[((x)*(t1->flags_stride))+(y)]
1499 static void opj_t1_enc_clnpass(
1507 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1508 OPJ_UINT32 agg, runlen;
1510 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1512 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1513 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1517 #ifdef DEBUG_ENC_CLN
1518 printf("enc_clnpass: bpno=%d\n", bpno);
1520 for (k = 0; k < (t1->h & ~3U); k += 4) {
1521 #ifdef DEBUG_ENC_CLN
1522 printf(" k=%d\n", k);
1524 for (i = 0; i < t1->w; ++i) {
1525 #ifdef DEBUG_ENC_CLN
1526 printf(" i=%d\n", i);
1528 agg = !(ENC_FLAGS(i, k) & agg_mask);
1529 #ifdef DEBUG_ENC_CLN
1530 printf(" agg=%d\n", agg);
1533 for (runlen = 0; runlen < 4; ++runlen) {
1534 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1538 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1539 opj_mqc_encode(mqc, runlen != 4);
1543 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1544 opj_mqc_encode(mqc, runlen >> 1);
1545 opj_mqc_encode(mqc, runlen & 1);
1549 opj_t1_enc_clnpass_step(
1552 &t1->data[((k + runlen) * t1->data_stride) + i],
1566 #ifdef DEBUG_ENC_CLN
1567 printf(" k=%d\n", k);
1569 for (i = 0; i < t1->w; ++i) {
1570 #ifdef DEBUG_ENC_CLN
1571 printf(" i=%d\n", i);
1572 printf(" agg=%d\n", agg);
1574 opj_t1_enc_clnpass_step(
1577 &t1->data[((k + runlen) * t1->data_stride) + i],
1591 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1593 #ifdef CONSISTENCY_CHECK
1594 #define REMOVE_VISIT_FLAG(flagptr) *(flagptr) &= (opj_flag_t)~T1_VISIT
1595 #define T1_SIG_OR_VISIT (T1_SIG | T1_VISIT)
1597 #define REMOVE_VISIT_FLAG(flagptr) do {} while(0)
1598 #define T1_SIG_OR_VISIT 0
1601 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1603 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1604 OPJ_UINT32 i, j, k; \
1605 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1607 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1611 oneplushalf = one | half; \
1612 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1613 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1614 for (k = 0; k < h; k += 4) { \
1615 for (i = 0; i < w; ++i) { \
1616 opj_colflag_t *colflags2 = colflags1 + i; \
1618 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1619 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1620 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1621 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1622 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1623 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1628 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1629 if (!opj_mqc_decode(mqc)) { \
1632 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1633 runlen = opj_mqc_decode(mqc); \
1634 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1638 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1639 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1640 opj_t1_dec_clnpass_step_vsc( \
1642 &t1->flags[((j+1) * flags_stride) + i + 1], \
1644 &t1->data[(j * w) + i], \
1646 agg && (j == k + (OPJ_UINT32)runlen), \
1649 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1651 colflags1 += flags_stride; \
1654 OPJ_INT32 *data1 = t1->data; \
1655 opj_flag_t *flags1 = &t1->flags[1]; \
1656 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1657 for (k = 0; k < (h & ~3u); k += 4) { \
1658 for (i = 0; i < w; ++i) { \
1659 OPJ_INT32 *data2 = data1 + i; \
1660 opj_flag_t *flags2 = flags1 + i; \
1661 opj_colflag_t *colflags2 = colflags1 + i; \
1662 opj_colflag_t colflags = *colflags2; \
1663 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1664 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1665 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1666 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1667 if( consistency_check ) { \
1668 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1669 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1670 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1671 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG_OR_VISIT | T1_SIG_OTH)) ); \
1674 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1675 if (!opj_mqc_decode(mqc)) { \
1678 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1679 runlen = opj_mqc_decode(mqc); \
1680 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1681 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1682 data2 += (OPJ_UINT32)runlen * w; \
1683 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1684 flags2 += flags_stride; \
1685 if (j == (OPJ_UINT32)runlen) { \
1686 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1688 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1693 flags2 += flags_stride; \
1694 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1695 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1696 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1698 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1700 flags2 += flags_stride; \
1701 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1702 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1703 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1705 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1707 flags2 += flags_stride; \
1708 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1709 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1710 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1712 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1714 flags2 += flags_stride; \
1715 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1716 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1717 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1719 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1722 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1725 flags1 += flags_stride << 2; \
1726 colflags1 += flags_stride; \
1728 for (i = 0; i < w; ++i) { \
1729 OPJ_INT32 *data2 = data1 + i; \
1730 opj_flag_t *flags2 = flags1 + i; \
1731 opj_colflag_t *colflags2 = colflags1 + i; \
1732 for (j = k; j < h; ++j) { \
1733 flags2 += flags_stride; \
1734 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1737 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1743 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1744 v = opj_mqc_decode(mqc); \
1745 v = (v << 1) | opj_mqc_decode(mqc); \
1746 v = (v << 1) | opj_mqc_decode(mqc); \
1747 v = (v << 1) | opj_mqc_decode(mqc); \
1750 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1754 } /* VSC and BYPASS by Antonin */
1756 static void opj_t1_dec_clnpass_64x64(
1761 #ifdef CONSISTENCY_CHECK
1762 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1764 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1768 static void opj_t1_dec_clnpass_generic(
1773 #ifdef CONSISTENCY_CHECK
1774 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1777 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1783 /** mod fixed_quality */
1784 static OPJ_FLOAT64 opj_t1_getwmsedec(
1791 OPJ_FLOAT64 stepsize,
1792 OPJ_UINT32 numcomps,
1793 const OPJ_FLOAT64 * mct_norms,
1794 OPJ_UINT32 mct_numcomps)
1796 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1797 OPJ_ARG_NOT_USED(numcomps);
1799 if (mct_norms && (compno < mct_numcomps)) {
1800 w1 = mct_norms[compno];
1804 w2 = opj_dwt_getnorm(level, orient);
1805 } else { /* if (qmfbid == 0) */
1806 w2 = opj_dwt_getnorm_real(level, orient);
1809 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1810 wmsedec *= wmsedec * nmsedec / 8192.0;
1815 static OPJ_BOOL opj_t1_allocate_buffers(
1822 /* encoder uses tile buffer, so no need to allocate */
1826 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1827 /* Overflow check */
1828 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1829 /* FIXME event manager error callback */
1833 datasize = (size_t)w * h;
1835 /* Overflow check */
1836 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1837 /* FIXME event manager error callback */
1841 if (datasize > (size_t)t1->datasize) {
1842 opj_aligned_free(t1->data);
1843 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1845 /* FIXME event manager error callback */
1848 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1849 /* TODO remove this if t1->datasize type changes to size_t */
1850 /* Overflow check */
1851 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1852 /* FIXME event manager error callback */
1856 t1->datasize = (OPJ_UINT32)datasize;
1858 /* memset first arg is declared to never be null by gcc */
1859 if (t1->data != NULL) {
1860 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1864 /* Overflow check */
1865 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1866 /* FIXME event manager error callback */
1869 t1->flags_stride = w + 2U; /* can't be 0U */
1871 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1872 /* Overflow check */
1873 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1874 /* FIXME event manager error callback */
1879 flagssize = (size_t)h + 2U;
1881 flagssize = (h + 3U) / 4U + 2U;
1884 /* Overflow check */
1885 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1886 /* FIXME event manager error callback */
1889 flagssize *= (size_t)t1->flags_stride;
1893 if (flagssize > (size_t)t1->flagssize) {
1894 /* Overflow check */
1895 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1896 /* FIXME event manager error callback */
1899 opj_aligned_free(t1->flags);
1900 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1902 /* FIXME event manager error callback */
1905 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1906 /* TODO remove this if t1->flagssize type changes to size_t */
1907 /* Overflow check */
1908 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1909 /* FIXME event manager error callback */
1914 t1->flagssize = (OPJ_UINT32)flagssize;
1916 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1918 size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1919 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1921 /* Overflow check */
1922 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1923 /* FIXME event manager error callback */
1926 colflags_size *= (size_t)t1->flags_stride;
1928 if (colflags_size > (size_t)t1->colflags_size) {
1929 /* Overflow check */
1930 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1931 /* FIXME event manager error callback */
1934 opj_aligned_free(t1->colflags);
1935 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1937 if (!t1->colflags) {
1938 /* FIXME event manager error callback */
1941 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1942 /* TODO remove this if t1->colflags_size type changes to size_t */
1943 /* Overflow check */
1944 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1945 /* FIXME event manager error callback */
1949 t1->colflags_size = (OPJ_UINT32)colflags_size;
1951 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1956 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1958 if (flagssize > (size_t)t1->flagssize) {
1959 /* Overflow check */
1960 if (flagssize > (SIZE_MAX / sizeof(opj_flag_enc_t))) {
1961 /* FIXME event manager error callback */
1964 opj_aligned_free(t1->enc_flags);
1965 t1->enc_flags = (opj_flag_enc_t*) opj_aligned_malloc(flagssize * sizeof(
1967 if (!t1->enc_flags) {
1968 /* FIXME event manager error callback */
1971 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1972 /* TODO remove this if t1->flagssize type changes to size_t */
1973 /* Overflow check */
1974 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1975 /* FIXME event manager error callback */
1980 t1->flagssize = (OPJ_UINT32)flagssize;
1982 memset(t1->enc_flags, 0, flagssize * sizeof(opj_flag_enc_t));
1984 p = &t1->enc_flags[0];
1985 for (x = 0; x < t1->flags_stride; ++x) {
1986 /* magic value to hopefully stop any passes being interested in this entry */
1987 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1990 p = &t1->enc_flags[((flags_height + 1) * t1->flags_stride)];
1991 for (x = 0; x < t1->flags_stride; ++x) {
1992 /* magic value to hopefully stop any passes being interested in this entry */
1993 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1998 p = &t1->enc_flags[((flags_height) * t1->flags_stride)];
2000 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
2001 } else if (h % 4 == 2) {
2002 v |= T1_PI_2 | T1_PI_3;
2003 } else if (h % 4 == 3) {
2006 for (x = 0; x < t1->flags_stride; ++x) {
2018 /* ----------------------------------------------------------------------- */
2020 /* ----------------------------------------------------------------------- */
2022 * Creates a new Tier 1 handle
2023 * and initializes the look-up tables of the Tier-1 coder/decoder
2024 * @return a new T1 handle if successful, returns NULL otherwise
2026 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
2028 opj_t1_t *l_t1 = 00;
2030 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
2035 /* create MQC and RAW handles */
2036 l_t1->mqc = opj_mqc_create();
2038 opj_t1_destroy(l_t1);
2042 l_t1->raw = opj_raw_create();
2044 opj_t1_destroy(l_t1);
2047 l_t1->encoder = isEncoder;
2054 * Destroys a previously created T1 handle
2056 * @param p_t1 Tier 1 handle to destroy
2058 void opj_t1_destroy(opj_t1_t *p_t1)
2064 /* destroy MQC and RAW handles */
2065 opj_mqc_destroy(p_t1->mqc);
2067 opj_raw_destroy(p_t1->raw);
2070 /* encoder uses tile buffer, so no need to free */
2071 if (!p_t1->encoder && p_t1->data) {
2072 opj_aligned_free(p_t1->data);
2077 opj_aligned_free(p_t1->flags);
2081 if (p_t1->colflags) {
2082 opj_aligned_free(p_t1->colflags);
2083 p_t1->colflags = 00;
2086 if (p_t1->enc_flags) {
2087 opj_aligned_free(p_t1->enc_flags);
2088 p_t1->enc_flags = 00;
2095 opj_tcd_cblk_dec_t* cblk;
2096 opj_tcd_band_t* band;
2097 opj_tcd_tilecomp_t* tilec;
2099 volatile OPJ_BOOL* pret;
2100 } opj_t1_cblk_decode_processing_job_t;
2102 static void opj_t1_destroy_wrapper(void* t1)
2104 opj_t1_destroy((opj_t1_t*) t1);
2107 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
2109 opj_tcd_cblk_dec_t* cblk;
2110 opj_tcd_band_t* band;
2111 opj_tcd_tilecomp_t* tilec;
2113 OPJ_INT32* OPJ_RESTRICT datap;
2114 OPJ_UINT32 cblk_w, cblk_h;
2117 opj_t1_cblk_decode_processing_job_t* job;
2122 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
2128 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2130 if (!*(job->pret)) {
2135 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
2137 t1 = opj_t1_create(OPJ_FALSE);
2138 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
2141 if (OPJ_FALSE == opj_t1_decode_cblk(
2145 (OPJ_UINT32)tccp->roishift,
2147 *(job->pret) = OPJ_FALSE;
2152 x = cblk->x0 - band->x0;
2153 y = cblk->y0 - band->y0;
2154 if (band->bandno & 1) {
2155 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2156 x += pres->x1 - pres->x0;
2158 if (band->bandno & 2) {
2159 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2160 y += pres->y1 - pres->y0;
2167 if (tccp->roishift) {
2168 OPJ_INT32 thresh = 1 << tccp->roishift;
2169 for (j = 0; j < cblk_h; ++j) {
2170 for (i = 0; i < cblk_w; ++i) {
2171 OPJ_INT32 val = datap[(j * cblk_w) + i];
2172 OPJ_INT32 mag = abs(val);
2173 if (mag >= thresh) {
2174 mag >>= tccp->roishift;
2175 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
2180 if (tccp->qmfbid == 1) {
2181 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
2183 for (j = 0; j < cblk_h; ++j) {
2185 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
2186 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
2187 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
2188 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
2189 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
2190 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
2191 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
2192 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
2193 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
2195 for (; i < cblk_w; ++i) {
2196 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
2197 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
2200 } else { /* if (tccp->qmfbid == 0) */
2201 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
2202 tile_w + (OPJ_UINT32)x];
2203 for (j = 0; j < cblk_h; ++j) {
2204 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
2205 for (i = 0; i < cblk_w; ++i) {
2206 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
2219 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
2220 volatile OPJ_BOOL* pret,
2221 opj_tcd_tilecomp_t* tilec,
2225 OPJ_UINT32 resno, bandno, precno, cblkno;
2227 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
2228 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2230 for (bandno = 0; bandno < res->numbands; ++bandno) {
2231 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2233 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2234 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2236 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2237 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2238 opj_t1_cblk_decode_processing_job_t* job;
2240 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2241 sizeof(opj_t1_cblk_decode_processing_job_t));
2252 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2265 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2266 opj_tcd_cblk_dec_t* cblk,
2268 OPJ_UINT32 roishift,
2271 opj_raw_t *raw = t1->raw; /* RAW component */
2272 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2274 OPJ_INT32 bpno_plus_one;
2275 OPJ_UINT32 passtype;
2276 OPJ_UINT32 segno, passno;
2277 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2279 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2281 if (!opj_t1_allocate_buffers(
2283 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2284 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2288 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2291 opj_mqc_resetstates(mqc);
2292 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2293 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2294 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2296 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2297 opj_tcd_seg_t *seg = &cblk->segs[segno];
2300 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2301 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2302 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2303 if (seg->data == 00) {
2306 if (type == T1_TYPE_RAW) {
2307 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2309 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2315 if (t1->w == 64 && t1->h == 64) {
2316 for (passno = 0; (passno < seg->real_num_passes) &&
2317 (bpno_plus_one >= 1); ++passno) {
2320 if (type == T1_TYPE_RAW) {
2321 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2323 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2324 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2326 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2331 if (type == T1_TYPE_RAW) {
2332 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2334 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2335 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2337 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2342 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2346 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2347 opj_mqc_resetstates(mqc);
2348 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2349 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2350 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2352 if (++passtype == 3) {
2358 for (passno = 0; (passno < seg->real_num_passes) &&
2359 (bpno_plus_one >= 1); ++passno) {
2362 if (type == T1_TYPE_RAW) {
2363 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2365 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2366 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2368 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2373 if (type == T1_TYPE_RAW) {
2374 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2376 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2377 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2379 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2384 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2388 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2389 opj_mqc_resetstates(mqc);
2390 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2391 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2392 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2394 if (++passtype == 3) {
2407 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2408 opj_tcd_tile_t *tile,
2410 const OPJ_FLOAT64 * mct_norms,
2411 OPJ_UINT32 mct_numcomps
2414 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2416 tile->distotile = 0; /* fixed_quality */
2418 for (compno = 0; compno < tile->numcomps; ++compno) {
2419 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2420 opj_tccp_t* tccp = &tcp->tccps[compno];
2421 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2423 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2424 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2426 for (bandno = 0; bandno < res->numbands; ++bandno) {
2427 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2428 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2430 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2431 opj_tcd_precinct_t *prc = &band->precincts[precno];
2433 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2434 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2435 OPJ_INT32* OPJ_RESTRICT tiledp;
2438 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2440 OPJ_INT32 x = cblk->x0 - band->x0;
2441 OPJ_INT32 y = cblk->y0 - band->y0;
2442 if (band->bandno & 1) {
2443 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2444 x += pres->x1 - pres->x0;
2446 if (band->bandno & 2) {
2447 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2448 y += pres->y1 - pres->y0;
2451 if (!opj_t1_allocate_buffers(
2453 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2454 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2460 tileLineAdvance = tile_w - cblk_w;
2462 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2464 t1->data_stride = tile_w;
2465 if (tccp->qmfbid == 1) {
2466 for (j = 0; j < cblk_h; ++j) {
2467 for (i = 0; i < cblk_w; ++i) {
2468 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2471 tileIndex += tileLineAdvance;
2473 } else { /* if (tccp->qmfbid == 0) */
2474 for (j = 0; j < cblk_h; ++j) {
2475 for (i = 0; i < cblk_w; ++i) {
2476 OPJ_INT32 tmp = tiledp[tileIndex];
2483 tileIndex += tileLineAdvance;
2492 tilec->numresolutions - 1 - resno,
2509 /** mod fixed_quality */
2510 static void opj_t1_encode_cblk(opj_t1_t *t1,
2511 opj_tcd_cblk_enc_t* cblk,
2516 OPJ_FLOAT64 stepsize,
2518 OPJ_UINT32 numcomps,
2519 opj_tcd_tile_t * tile,
2520 const OPJ_FLOAT64 * mct_norms,
2521 OPJ_UINT32 mct_numcomps)
2523 OPJ_FLOAT64 cumwmsedec = 0.0;
2525 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2529 OPJ_UINT32 passtype;
2530 OPJ_INT32 nmsedec = 0;
2533 OPJ_BYTE type = T1_TYPE_MQ;
2534 OPJ_FLOAT64 tempwmsedec;
2536 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2539 for (i = 0; i < t1->w; ++i) {
2540 for (j = 0; j < t1->h; ++j) {
2541 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2542 max = opj_int_max(max, tmp);
2546 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2547 T1_NMSEDEC_FRACBITS) : 0;
2549 bpno = (OPJ_INT32)(cblk->numbps - 1);
2552 opj_mqc_resetstates(mqc);
2553 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2554 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2555 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2556 opj_mqc_init_enc(mqc, cblk->data);
2558 for (passno = 0; bpno >= 0; ++passno) {
2559 opj_tcd_pass_t *pass = &cblk->passes[passno];
2560 OPJ_UINT32 correction = 3;
2561 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2562 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2566 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
2569 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2572 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
2573 /* code switch SEGMARK (i.e. SEGSYM) */
2574 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2575 opj_mqc_segmark_enc(mqc);
2581 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2582 stepsize, numcomps, mct_norms, mct_numcomps) ;
2583 cumwmsedec += tempwmsedec;
2584 tile->distotile += tempwmsedec;
2586 /* Code switch "RESTART" (i.e. TERMALL) */
2587 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2589 if (type == T1_TYPE_RAW) {
2592 /* correction = mqc_bypass_flush_enc(); */
2593 } else { /* correction = mqc_restart_enc(); */
2599 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2600 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2601 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2602 if (type == T1_TYPE_RAW) {
2605 /* correction = mqc_bypass_flush_enc(); */
2606 } else { /* correction = mqc_restart_enc(); */
2616 if (++passtype == 3) {
2621 if (pass->term && bpno > 0) {
2622 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2623 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2624 if (type == T1_TYPE_RAW) {
2625 opj_mqc_bypass_init_enc(mqc);
2627 opj_mqc_restart_init_enc(mqc);
2631 pass->distortiondec = cumwmsedec;
2632 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2634 /* Code-switch "RESET" */
2635 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2636 opj_mqc_reset_enc(mqc);
2640 /* Code switch "ERTERM" (i.e. PTERM) */
2641 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2642 opj_mqc_erterm_enc(mqc);
2643 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2647 cblk->totalpasses = passno;
2649 for (passno = 0; passno < cblk->totalpasses; passno++) {
2650 opj_tcd_pass_t *pass = &cblk->passes[passno];
2651 if (pass->rate > opj_mqc_numbytes(mqc)) {
2652 pass->rate = opj_mqc_numbytes(mqc);
2654 /*Preventing generation of FF as last data byte of a pass*/
2655 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2658 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);