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,
1312 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1315 if ((cblksty & J2K_CCP_CBLKSTY_VSC) == 0) {
1316 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1317 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1319 if ((*flagsp & check) == check) {
1321 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1322 } else if (runlen == 1) {
1323 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1324 } else if (runlen == 2) {
1325 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1326 } else if (runlen == 3) {
1327 *flagsp &= ~(T1_PI_3);
1333 lim = 4U < (t1->h - y) ? 4U : (t1->h - y);
1334 for (ci = runlen; ci < lim; ++ci) {
1336 OPJ_UINT32 vsc_mask;
1337 opj_flag_enc_t flags;
1339 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1340 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1343 if ((agg != 0) && (ci == runlen)) {
1347 flags = *flagsp & vsc_mask;
1349 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1350 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U), orient);
1351 #ifdef DEBUG_ENC_CLN
1352 printf(" ctxt1=%d\n", ctxt1);
1354 opj_mqc_setcurctx(mqc, ctxt1);
1355 v = opj_int_abs(*datap) & one ? 1 : 0;
1356 opj_mqc_encode(mqc, v);
1358 OPJ_UINT32 ctxt2, spb;
1360 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno);
1361 ctxt2 = opj_t1_enc_getctxno_sc(*flagsp & vsc_mask,
1362 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1364 #ifdef DEBUG_ENC_CLN
1365 printf(" ctxt2=%d\n", ctxt2);
1367 opj_mqc_setcurctx(mqc, ctxt2);
1369 v = *datap < 0 ? 1 : 0;
1370 spb = opj_t1_enc_getspb(*flagsp & vsc_mask,
1371 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask, ci);
1372 #ifdef DEBUG_ENC_CLN
1373 printf(" spb=%d\n", spb);
1375 opj_mqc_encode(mqc, v ^ spb);
1376 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
1379 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1380 datap += t1->data_stride;
1385 static void opj_t1_dec_clnpass_step_partial(
1388 opj_colflag_t *colflagsp,
1390 OPJ_INT32 oneplushalf,
1394 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1397 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1398 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1399 *datap = v ? -oneplushalf : oneplushalf;
1400 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1402 #ifdef CONSISTENCY_CHECK
1403 *flagsp &= ~T1_VISIT;
1405 } /* VSC and BYPASS by Antonin */
1407 static void opj_t1_dec_clnpass_step(
1410 opj_colflag_t *colflagsp,
1412 OPJ_INT32 oneplushalf,
1417 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1418 #ifdef CONSISTENCY_CHECK
1419 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1420 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1422 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1425 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1426 if (opj_mqc_decode(mqc)) {
1427 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1428 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1429 *datap = v ? -oneplushalf : oneplushalf;
1430 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1434 #ifdef CONSISTENCY_CHECK
1435 *flagsp &= ~T1_VISIT;
1437 } /* VSC and BYPASS by Antonin */
1439 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1442 opj_colflag_t *colflagsp,
1444 OPJ_INT32 oneplushalf,
1446 OPJ_UINT32 flags_stride)
1451 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1454 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1456 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1457 if (opj_mqc_decode(mqc)) {
1458 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1459 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1460 *datap = v ? -oneplushalf : oneplushalf;
1461 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1464 /*flagsp &= ~T1_VISIT;*/
1467 static void opj_t1_dec_clnpass_step_vsc(
1470 opj_colflag_t *colflagsp,
1472 OPJ_INT32 oneplushalf,
1479 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1481 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1486 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1487 (T1_COLFLAG_RBS * row)))) {
1488 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1489 if (opj_mqc_decode(mqc)) {
1491 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1492 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1493 *datap = v ? -oneplushalf : oneplushalf;
1494 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1497 #ifdef CONSISTENCY_CHECK
1498 *flagsp &= ~T1_VISIT;
1502 #define MACRO_t1_enc_flags(x,y) t1->enc_flags[((x)*(t1->flags_stride))+(y)]
1504 static void opj_t1_enc_clnpass(
1512 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1513 OPJ_UINT32 agg, runlen;
1515 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1518 #ifdef DEBUG_ENC_CLN
1519 printf("enc_clnpass: bpno=%d\n", bpno);
1521 for (k = 0; k < t1->h; k += 4) {
1522 #ifdef DEBUG_ENC_CLN
1523 printf(" k=%d\n", k);
1525 for (i = 0; i < t1->w; ++i) {
1526 #ifdef DEBUG_ENC_CLN
1527 printf(" i=%d\n", i);
1529 if (k + 3 < t1->h) {
1530 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1531 agg = !(ENC_FLAGS(i, k) & ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 |
1534 agg = !ENC_FLAGS(i, k);
1539 #ifdef DEBUG_ENC_CLN
1540 printf(" agg=%d\n", agg);
1543 for (runlen = 0; runlen < 4; ++runlen) {
1544 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1548 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1549 opj_mqc_encode(mqc, runlen != 4);
1553 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1554 opj_mqc_encode(mqc, runlen >> 1);
1555 opj_mqc_encode(mqc, runlen & 1);
1559 opj_t1_enc_clnpass_step(
1562 &t1->data[((k + runlen) * t1->data_stride) + i],
1577 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1579 #ifdef CONSISTENCY_CHECK
1580 #define REMOVE_VISIT_FLAG(flagptr) *(flagptr) &= (opj_flag_t)~T1_VISIT
1581 #define T1_SIG_OR_VISIT (T1_SIG | T1_VISIT)
1583 #define REMOVE_VISIT_FLAG(flagptr) do {} while(0)
1584 #define T1_SIG_OR_VISIT 0
1587 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1589 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1590 OPJ_UINT32 i, j, k; \
1591 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1593 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1597 oneplushalf = one | half; \
1598 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1599 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1600 for (k = 0; k < h; k += 4) { \
1601 for (i = 0; i < w; ++i) { \
1602 opj_colflag_t *colflags2 = colflags1 + i; \
1604 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1605 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1606 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1607 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1608 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1609 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1614 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1615 if (!opj_mqc_decode(mqc)) { \
1618 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1619 runlen = opj_mqc_decode(mqc); \
1620 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1624 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1625 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1626 opj_t1_dec_clnpass_step_vsc( \
1628 &t1->flags[((j+1) * flags_stride) + i + 1], \
1630 &t1->data[(j * w) + i], \
1632 agg && (j == k + (OPJ_UINT32)runlen), \
1635 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1637 colflags1 += flags_stride; \
1640 OPJ_INT32 *data1 = t1->data; \
1641 opj_flag_t *flags1 = &t1->flags[1]; \
1642 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1643 for (k = 0; k < (h & ~3u); k += 4) { \
1644 for (i = 0; i < w; ++i) { \
1645 OPJ_INT32 *data2 = data1 + i; \
1646 opj_flag_t *flags2 = flags1 + i; \
1647 opj_colflag_t *colflags2 = colflags1 + i; \
1648 opj_colflag_t colflags = *colflags2; \
1649 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1650 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1651 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1652 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1653 if( consistency_check ) { \
1654 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1655 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1656 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1657 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG_OR_VISIT | T1_SIG_OTH)) ); \
1660 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1661 if (!opj_mqc_decode(mqc)) { \
1664 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1665 runlen = opj_mqc_decode(mqc); \
1666 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1667 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1668 data2 += (OPJ_UINT32)runlen * w; \
1669 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1670 flags2 += flags_stride; \
1671 if (j == (OPJ_UINT32)runlen) { \
1672 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1674 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1679 flags2 += flags_stride; \
1680 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1681 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1682 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1684 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1686 flags2 += flags_stride; \
1687 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1688 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1689 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1691 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1693 flags2 += flags_stride; \
1694 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1695 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1696 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1698 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1700 flags2 += flags_stride; \
1701 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1702 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1703 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1705 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1708 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1711 flags1 += flags_stride << 2; \
1712 colflags1 += flags_stride; \
1714 for (i = 0; i < w; ++i) { \
1715 OPJ_INT32 *data2 = data1 + i; \
1716 opj_flag_t *flags2 = flags1 + i; \
1717 opj_colflag_t *colflags2 = colflags1 + i; \
1718 for (j = k; j < h; ++j) { \
1719 flags2 += flags_stride; \
1720 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1723 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1729 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1730 v = opj_mqc_decode(mqc); \
1731 v = (v << 1) | opj_mqc_decode(mqc); \
1732 v = (v << 1) | opj_mqc_decode(mqc); \
1733 v = (v << 1) | opj_mqc_decode(mqc); \
1736 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1740 } /* VSC and BYPASS by Antonin */
1742 static void opj_t1_dec_clnpass_64x64(
1747 #ifdef CONSISTENCY_CHECK
1748 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1750 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1754 static void opj_t1_dec_clnpass_generic(
1759 #ifdef CONSISTENCY_CHECK
1760 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1763 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1769 /** mod fixed_quality */
1770 static OPJ_FLOAT64 opj_t1_getwmsedec(
1777 OPJ_FLOAT64 stepsize,
1778 OPJ_UINT32 numcomps,
1779 const OPJ_FLOAT64 * mct_norms,
1780 OPJ_UINT32 mct_numcomps)
1782 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1783 OPJ_ARG_NOT_USED(numcomps);
1785 if (mct_norms && (compno < mct_numcomps)) {
1786 w1 = mct_norms[compno];
1790 w2 = opj_dwt_getnorm(level, orient);
1791 } else { /* if (qmfbid == 0) */
1792 w2 = opj_dwt_getnorm_real(level, orient);
1795 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1796 wmsedec *= wmsedec * nmsedec / 8192.0;
1801 static OPJ_BOOL opj_t1_allocate_buffers(
1808 /* encoder uses tile buffer, so no need to allocate */
1812 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1813 /* Overflow check */
1814 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1815 /* FIXME event manager error callback */
1819 datasize = (size_t)w * h;
1821 /* Overflow check */
1822 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1823 /* FIXME event manager error callback */
1827 if (datasize > (size_t)t1->datasize) {
1828 opj_aligned_free(t1->data);
1829 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1831 /* FIXME event manager error callback */
1834 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1835 /* TODO remove this if t1->datasize type changes to size_t */
1836 /* Overflow check */
1837 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1838 /* FIXME event manager error callback */
1842 t1->datasize = (OPJ_UINT32)datasize;
1844 /* memset first arg is declared to never be null by gcc */
1845 if (t1->data != NULL) {
1846 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1850 /* Overflow check */
1851 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1852 /* FIXME event manager error callback */
1855 t1->flags_stride = w + 2U; /* can't be 0U */
1857 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1858 /* Overflow check */
1859 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1860 /* FIXME event manager error callback */
1865 flagssize = (size_t)h + 2U;
1867 flagssize = (h + 3U) / 4U + 2U;
1870 /* Overflow check */
1871 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1872 /* FIXME event manager error callback */
1875 flagssize *= (size_t)t1->flags_stride;
1879 if (flagssize > (size_t)t1->flagssize) {
1880 /* Overflow check */
1881 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1882 /* FIXME event manager error callback */
1885 opj_aligned_free(t1->flags);
1886 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1888 /* FIXME event manager error callback */
1891 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1892 /* TODO remove this if t1->flagssize type changes to size_t */
1893 /* Overflow check */
1894 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1895 /* FIXME event manager error callback */
1900 t1->flagssize = (OPJ_UINT32)flagssize;
1902 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1904 size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1905 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1907 /* Overflow check */
1908 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1909 /* FIXME event manager error callback */
1912 colflags_size *= (size_t)t1->flags_stride;
1914 if (colflags_size > (size_t)t1->colflags_size) {
1915 /* Overflow check */
1916 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1917 /* FIXME event manager error callback */
1920 opj_aligned_free(t1->colflags);
1921 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1923 if (!t1->colflags) {
1924 /* FIXME event manager error callback */
1927 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1928 /* TODO remove this if t1->colflags_size type changes to size_t */
1929 /* Overflow check */
1930 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1931 /* FIXME event manager error callback */
1935 t1->colflags_size = (OPJ_UINT32)colflags_size;
1937 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1942 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1944 if (flagssize > (size_t)t1->flagssize) {
1945 /* Overflow check */
1946 if (flagssize > (SIZE_MAX / sizeof(opj_flag_enc_t))) {
1947 /* FIXME event manager error callback */
1950 opj_aligned_free(t1->enc_flags);
1951 t1->enc_flags = (opj_flag_enc_t*) opj_aligned_malloc(flagssize * sizeof(
1953 if (!t1->enc_flags) {
1954 /* FIXME event manager error callback */
1957 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1958 /* TODO remove this if t1->flagssize type changes to size_t */
1959 /* Overflow check */
1960 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1961 /* FIXME event manager error callback */
1966 t1->flagssize = (OPJ_UINT32)flagssize;
1968 memset(t1->enc_flags, 0, flagssize * sizeof(opj_flag_enc_t));
1970 p = &t1->enc_flags[0];
1971 for (x = 0; x < t1->flags_stride; ++x) {
1972 /* magic value to hopefully stop any passes being interested in this entry */
1973 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1976 p = &t1->enc_flags[((flags_height + 1) * t1->flags_stride)];
1977 for (x = 0; x < t1->flags_stride; ++x) {
1978 /* magic value to hopefully stop any passes being interested in this entry */
1979 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1984 p = &t1->enc_flags[((flags_height) * t1->flags_stride)];
1986 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1987 } else if (h % 4 == 2) {
1988 v |= T1_PI_2 | T1_PI_3;
1989 } else if (h % 4 == 3) {
1992 for (x = 0; x < t1->flags_stride; ++x) {
2004 /* ----------------------------------------------------------------------- */
2006 /* ----------------------------------------------------------------------- */
2008 * Creates a new Tier 1 handle
2009 * and initializes the look-up tables of the Tier-1 coder/decoder
2010 * @return a new T1 handle if successful, returns NULL otherwise
2012 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
2014 opj_t1_t *l_t1 = 00;
2016 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
2021 /* create MQC and RAW handles */
2022 l_t1->mqc = opj_mqc_create();
2024 opj_t1_destroy(l_t1);
2028 l_t1->raw = opj_raw_create();
2030 opj_t1_destroy(l_t1);
2033 l_t1->encoder = isEncoder;
2040 * Destroys a previously created T1 handle
2042 * @param p_t1 Tier 1 handle to destroy
2044 void opj_t1_destroy(opj_t1_t *p_t1)
2050 /* destroy MQC and RAW handles */
2051 opj_mqc_destroy(p_t1->mqc);
2053 opj_raw_destroy(p_t1->raw);
2056 /* encoder uses tile buffer, so no need to free */
2057 if (!p_t1->encoder && p_t1->data) {
2058 opj_aligned_free(p_t1->data);
2063 opj_aligned_free(p_t1->flags);
2067 if (p_t1->colflags) {
2068 opj_aligned_free(p_t1->colflags);
2069 p_t1->colflags = 00;
2072 if (p_t1->enc_flags) {
2073 opj_aligned_free(p_t1->enc_flags);
2074 p_t1->enc_flags = 00;
2081 opj_tcd_cblk_dec_t* cblk;
2082 opj_tcd_band_t* band;
2083 opj_tcd_tilecomp_t* tilec;
2085 volatile OPJ_BOOL* pret;
2086 } opj_t1_cblk_decode_processing_job_t;
2088 static void opj_t1_destroy_wrapper(void* t1)
2090 opj_t1_destroy((opj_t1_t*) t1);
2093 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
2095 opj_tcd_cblk_dec_t* cblk;
2096 opj_tcd_band_t* band;
2097 opj_tcd_tilecomp_t* tilec;
2099 OPJ_INT32* OPJ_RESTRICT datap;
2100 OPJ_UINT32 cblk_w, cblk_h;
2103 opj_t1_cblk_decode_processing_job_t* job;
2108 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
2114 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2116 if (!*(job->pret)) {
2121 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
2123 t1 = opj_t1_create(OPJ_FALSE);
2124 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
2127 if (OPJ_FALSE == opj_t1_decode_cblk(
2131 (OPJ_UINT32)tccp->roishift,
2133 *(job->pret) = OPJ_FALSE;
2138 x = cblk->x0 - band->x0;
2139 y = cblk->y0 - band->y0;
2140 if (band->bandno & 1) {
2141 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2142 x += pres->x1 - pres->x0;
2144 if (band->bandno & 2) {
2145 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2146 y += pres->y1 - pres->y0;
2153 if (tccp->roishift) {
2154 OPJ_INT32 thresh = 1 << tccp->roishift;
2155 for (j = 0; j < cblk_h; ++j) {
2156 for (i = 0; i < cblk_w; ++i) {
2157 OPJ_INT32 val = datap[(j * cblk_w) + i];
2158 OPJ_INT32 mag = abs(val);
2159 if (mag >= thresh) {
2160 mag >>= tccp->roishift;
2161 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
2166 if (tccp->qmfbid == 1) {
2167 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
2169 for (j = 0; j < cblk_h; ++j) {
2171 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
2172 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
2173 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
2174 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
2175 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
2176 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
2177 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
2178 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
2179 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
2181 for (; i < cblk_w; ++i) {
2182 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
2183 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
2186 } else { /* if (tccp->qmfbid == 0) */
2187 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
2188 tile_w + (OPJ_UINT32)x];
2189 for (j = 0; j < cblk_h; ++j) {
2190 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
2191 for (i = 0; i < cblk_w; ++i) {
2192 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
2205 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
2206 volatile OPJ_BOOL* pret,
2207 opj_tcd_tilecomp_t* tilec,
2211 OPJ_UINT32 resno, bandno, precno, cblkno;
2213 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
2214 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2216 for (bandno = 0; bandno < res->numbands; ++bandno) {
2217 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2219 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2220 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2222 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2223 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2224 opj_t1_cblk_decode_processing_job_t* job;
2226 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2227 sizeof(opj_t1_cblk_decode_processing_job_t));
2238 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2251 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2252 opj_tcd_cblk_dec_t* cblk,
2254 OPJ_UINT32 roishift,
2257 opj_raw_t *raw = t1->raw; /* RAW component */
2258 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2260 OPJ_INT32 bpno_plus_one;
2261 OPJ_UINT32 passtype;
2262 OPJ_UINT32 segno, passno;
2263 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2265 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2267 if (!opj_t1_allocate_buffers(
2269 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2270 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2274 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2277 opj_mqc_resetstates(mqc);
2278 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2279 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2280 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2282 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2283 opj_tcd_seg_t *seg = &cblk->segs[segno];
2286 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2287 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2288 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2289 if (seg->data == 00) {
2292 if (type == T1_TYPE_RAW) {
2293 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2295 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2301 if (t1->w == 64 && t1->h == 64) {
2302 for (passno = 0; (passno < seg->real_num_passes) &&
2303 (bpno_plus_one >= 1); ++passno) {
2306 if (type == T1_TYPE_RAW) {
2307 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2309 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2310 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2312 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2317 if (type == T1_TYPE_RAW) {
2318 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2320 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2321 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2323 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2328 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2332 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2333 opj_mqc_resetstates(mqc);
2334 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2335 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2336 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2338 if (++passtype == 3) {
2344 for (passno = 0; (passno < seg->real_num_passes) &&
2345 (bpno_plus_one >= 1); ++passno) {
2348 if (type == T1_TYPE_RAW) {
2349 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2351 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2352 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2354 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2359 if (type == T1_TYPE_RAW) {
2360 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2362 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2363 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2365 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2370 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2374 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2375 opj_mqc_resetstates(mqc);
2376 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2377 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2378 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2380 if (++passtype == 3) {
2393 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2394 opj_tcd_tile_t *tile,
2396 const OPJ_FLOAT64 * mct_norms,
2397 OPJ_UINT32 mct_numcomps
2400 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2402 tile->distotile = 0; /* fixed_quality */
2404 for (compno = 0; compno < tile->numcomps; ++compno) {
2405 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2406 opj_tccp_t* tccp = &tcp->tccps[compno];
2407 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2409 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2410 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2412 for (bandno = 0; bandno < res->numbands; ++bandno) {
2413 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2414 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2416 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2417 opj_tcd_precinct_t *prc = &band->precincts[precno];
2419 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2420 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2421 OPJ_INT32* OPJ_RESTRICT tiledp;
2424 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2426 OPJ_INT32 x = cblk->x0 - band->x0;
2427 OPJ_INT32 y = cblk->y0 - band->y0;
2428 if (band->bandno & 1) {
2429 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2430 x += pres->x1 - pres->x0;
2432 if (band->bandno & 2) {
2433 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2434 y += pres->y1 - pres->y0;
2437 if (!opj_t1_allocate_buffers(
2439 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2440 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2446 tileLineAdvance = tile_w - cblk_w;
2448 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2450 t1->data_stride = tile_w;
2451 if (tccp->qmfbid == 1) {
2452 for (j = 0; j < cblk_h; ++j) {
2453 for (i = 0; i < cblk_w; ++i) {
2454 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2457 tileIndex += tileLineAdvance;
2459 } else { /* if (tccp->qmfbid == 0) */
2460 for (j = 0; j < cblk_h; ++j) {
2461 for (i = 0; i < cblk_w; ++i) {
2462 OPJ_INT32 tmp = tiledp[tileIndex];
2469 tileIndex += tileLineAdvance;
2478 tilec->numresolutions - 1 - resno,
2495 /** mod fixed_quality */
2496 static void opj_t1_encode_cblk(opj_t1_t *t1,
2497 opj_tcd_cblk_enc_t* cblk,
2502 OPJ_FLOAT64 stepsize,
2504 OPJ_UINT32 numcomps,
2505 opj_tcd_tile_t * tile,
2506 const OPJ_FLOAT64 * mct_norms,
2507 OPJ_UINT32 mct_numcomps)
2509 OPJ_FLOAT64 cumwmsedec = 0.0;
2511 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2515 OPJ_UINT32 passtype;
2516 OPJ_INT32 nmsedec = 0;
2519 OPJ_BYTE type = T1_TYPE_MQ;
2520 OPJ_FLOAT64 tempwmsedec;
2522 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2525 for (i = 0; i < t1->w; ++i) {
2526 for (j = 0; j < t1->h; ++j) {
2527 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2528 max = opj_int_max(max, tmp);
2532 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2533 T1_NMSEDEC_FRACBITS) : 0;
2535 bpno = (OPJ_INT32)(cblk->numbps - 1);
2538 opj_mqc_resetstates(mqc);
2539 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2540 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2541 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2542 opj_mqc_init_enc(mqc, cblk->data);
2544 for (passno = 0; bpno >= 0; ++passno) {
2545 opj_tcd_pass_t *pass = &cblk->passes[passno];
2546 OPJ_UINT32 correction = 3;
2547 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2548 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2552 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
2555 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2558 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
2559 /* code switch SEGMARK (i.e. SEGSYM) */
2560 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2561 opj_mqc_segmark_enc(mqc);
2567 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2568 stepsize, numcomps, mct_norms, mct_numcomps) ;
2569 cumwmsedec += tempwmsedec;
2570 tile->distotile += tempwmsedec;
2572 /* Code switch "RESTART" (i.e. TERMALL) */
2573 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2575 if (type == T1_TYPE_RAW) {
2578 /* correction = mqc_bypass_flush_enc(); */
2579 } else { /* correction = mqc_restart_enc(); */
2585 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2586 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2587 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2588 if (type == T1_TYPE_RAW) {
2591 /* correction = mqc_bypass_flush_enc(); */
2592 } else { /* correction = mqc_restart_enc(); */
2602 if (++passtype == 3) {
2607 if (pass->term && bpno > 0) {
2608 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2609 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2610 if (type == T1_TYPE_RAW) {
2611 opj_mqc_bypass_init_enc(mqc);
2613 opj_mqc_restart_init_enc(mqc);
2617 pass->distortiondec = cumwmsedec;
2618 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2620 /* Code-switch "RESET" */
2621 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2622 opj_mqc_reset_enc(mqc);
2626 /* Code switch "ERTERM" (i.e. PTERM) */
2627 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2628 opj_mqc_erterm_enc(mqc);
2629 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2633 cblk->totalpasses = passno;
2635 for (passno = 0; passno < cblk->totalpasses; passno++) {
2636 opj_tcd_pass_t *pass = &cblk->passes[passno];
2637 if (pass->rate > opj_mqc_numbytes(mqc)) {
2638 pass->rate = opj_mqc_numbytes(mqc);
2640 /*Preventing generation of FF as last data byte of a pass*/
2641 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2644 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);