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 INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
57 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f);
58 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
59 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
60 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
61 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
62 OPJ_UINT32 s, OPJ_UINT32 stride);
63 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
68 Decode significant pass
71 static INLINE void opj_t1_dec_sigpass_step_raw(
74 opj_colflag_t* colflagsp,
76 OPJ_INT32 oneplushalf,
79 static INLINE void opj_t1_dec_sigpass_step_mqc(
82 opj_colflag_t* colflagsp,
84 OPJ_INT32 oneplushalf,
86 OPJ_UINT32 flags_stride);
87 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
90 opj_colflag_t* colflagsp,
92 OPJ_INT32 oneplushalf,
98 Encode significant pass
100 static void opj_t1_enc_sigpass(opj_t1_t *t1,
108 Decode significant pass
110 static void opj_t1_dec_sigpass_raw(
114 static void opj_t1_dec_sigpass_mqc_vsc(
121 Encode refinement pass
123 static void opj_t1_enc_refpass(opj_t1_t *t1,
130 Decode refinement pass
132 static void opj_t1_dec_refpass_raw(
135 static void opj_t1_dec_refpass_mqc_vsc(
141 Decode refinement pass
144 static INLINE void opj_t1_dec_refpass_step_raw(
146 opj_colflag_t *colflagsp,
151 static INLINE void opj_t1_dec_refpass_step_mqc(
154 opj_colflag_t *colflagsp,
159 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
162 opj_colflag_t *colflagsp,
173 static void opj_t1_dec_clnpass_step_partial(
176 opj_colflag_t *colflagsp,
178 OPJ_INT32 oneplushalf,
180 static void opj_t1_dec_clnpass_step(
183 opj_colflag_t *colflagsp,
185 OPJ_INT32 oneplushalf,
187 static void opj_t1_dec_clnpass_step_vsc(
190 opj_colflag_t *colflagsp,
192 OPJ_INT32 oneplushalf,
199 static void opj_t1_enc_clnpass(
206 static OPJ_FLOAT64 opj_t1_getwmsedec(
213 OPJ_FLOAT64 stepsize,
215 const OPJ_FLOAT64 * mct_norms,
216 OPJ_UINT32 mct_numcomps);
218 static void opj_t1_encode_cblk(opj_t1_t *t1,
219 opj_tcd_cblk_enc_t* cblk,
224 OPJ_FLOAT64 stepsize,
227 opj_tcd_tile_t * tile,
228 const OPJ_FLOAT64 * mct_norms,
229 OPJ_UINT32 mct_numcomps);
234 @param cblk Code-block coding parameters
236 @param roishift Region of interest shifting value
237 @param cblksty Code-block style
239 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
240 opj_tcd_cblk_dec_t* cblk,
245 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
253 /* ----------------------------------------------------------------------- */
255 static INLINE OPJ_BYTE opj_t1_enc_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
257 return lut_enc_ctxno_zc[(orient << 9) | (f & T1_SIGMA_NEIGHBOURS)];
260 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
262 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
265 static INLINE OPJ_UINT32 opj_t1_enc_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
271 0 pfX T1_CHI_THIS T1_LUT_SGN_W
272 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
273 2 nfX T1_CHI_THIS T1_LUT_SGN_E
274 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
275 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
276 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
277 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
278 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
281 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
284 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
285 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
287 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
289 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
291 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
295 static INLINE OPJ_BYTE opj_t1_enc_getctxno_sc(OPJ_UINT32 lu)
297 return lut_enc_ctxno_sc[lu];
300 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f)
302 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
305 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f)
307 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
308 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
312 static INLINE OPJ_BYTE opj_t1_enc_getspb(OPJ_UINT32 lu)
314 return lut_enc_spb[lu];
317 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f)
319 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
322 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
325 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
328 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
331 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
334 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
341 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
342 OPJ_UINT32 s, OPJ_UINT32 stride)
344 /* set up to point to the north and south data points' flags words, if required */
345 opj_flag_enc_t* north;
346 opj_flag_enc_t* south;
348 /* mark target as significant */
349 *flagsp |= T1_SIGMA_4 << (3U * ci);
351 /* north-west, north, north-east */
353 north = flagsp - stride;
354 *north |= T1_SIGMA_16;
355 north[-1] |= T1_SIGMA_17;
356 north[1] |= T1_SIGMA_15;
359 /* south-west, south, south-east */
361 south = flagsp + stride;
362 *south |= T1_SIGMA_1;
363 south[-1] |= T1_SIGMA_2;
364 south[1] |= T1_SIGMA_0;
368 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
371 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
377 north = flagsp - stride;
389 south = flagsp + stride;
398 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
401 opj_flag_t *np = flagsp - stride;
402 opj_flag_t *sp = flagsp + stride;
404 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
405 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
406 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
408 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
411 np[0] |= (opj_flag_t)(flag_N << 2U);
414 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
415 #ifdef CONSISTENCY_CHECK
418 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
421 sp[0] = (opj_flag_t)(sp[0] | flag_N);
425 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp,
426 opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
428 opj_t1_updateflags(flagsp, s, stride);
430 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
431 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
432 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
433 (T1_COLFLAG_RBS * row)) |
434 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
435 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
436 (T1_COLFLAG_RBS * row)) |
437 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
438 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
439 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
440 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
441 } else if (row == 3U) {
442 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
443 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
444 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
445 (T1_COLFLAG_RBS * row)) |
446 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
447 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
448 (T1_COLFLAG_RBS * row)) |
449 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1))));
450 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
451 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
452 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
454 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
455 (T1_COLFLAG_RBS * row)) |
456 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
457 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
458 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
459 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
460 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
461 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
462 (T1_COLFLAG_RBS * row)) |
463 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
464 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
469 Encode significant pass
471 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
472 opj_flag_enc_t *flagsp,
485 opj_mqc_t *mqc = t1->mqc; /* MQC component */
487 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
488 T1_CHI_S) << (ci * 3U)) : ~0U;
489 OPJ_UINT32 const flags = *flagsp & vsc_mask;
491 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
492 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
493 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U),
495 v = opj_int_abs(*datap) & one ? 1 : 0;
497 fprintf(stderr, " ctxt1=%d\n", ctxt1);
499 opj_mqc_setcurctx(mqc, ctxt1); /* ESSAI */
500 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
501 opj_mqc_bypass_enc(mqc, v);
503 opj_mqc_encode(mqc, v);
506 /* Note: using flags instead of *flagsp & vsc_mask result */
507 /* in slow down. Probably because of register pressure */
508 OPJ_UINT32 lu = opj_t1_enc_getctxtno_sc_or_spb_index(
510 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
512 OPJ_UINT32 ctxt2 = opj_t1_enc_getctxno_sc(lu);
513 v = *datap < 0 ? 1 : 0;
514 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
517 fprintf(stderr, " ctxt2=%d\n", ctxt2);
519 opj_mqc_setcurctx(mqc, ctxt2); /* ESSAI */
520 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
521 opj_mqc_bypass_enc(mqc, v);
523 OPJ_UINT32 spb = opj_t1_enc_getspb(lu);
525 fprintf(stderr, " spb=%d\n", spb);
527 opj_mqc_encode(mqc, v ^ spb);
529 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
531 *flagsp |= T1_PI_THIS << (ci * 3U);
535 static INLINE void opj_t1_dec_sigpass_step_raw(
538 opj_colflag_t* colflagsp,
540 OPJ_INT32 oneplushalf,
545 opj_raw_t *raw = t1->raw; /* RAW component */
547 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
549 if ((flag & T1_SIG_OTH) &&
550 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
551 (T1_COLFLAG_RBS * row)))) {
552 if (opj_raw_decode(raw)) {
553 v = opj_raw_decode(raw); /* ESSAI */
554 *datap = v ? -oneplushalf : oneplushalf;
555 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
557 #ifdef CONSISTENCY_CHECK
560 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
561 (T1_COLFLAG_RBS * row)));
565 static INLINE void opj_t1_dec_sigpass_step_mqc(
568 opj_colflag_t* colflagsp,
570 OPJ_INT32 oneplushalf,
572 OPJ_UINT32 flags_stride)
576 opj_mqc_t *mqc = t1->mqc; /* MQC component */
577 #ifdef CONSISTENCY_CHECK
578 assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
579 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
580 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
581 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
583 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
584 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
585 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
587 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
588 if (opj_mqc_decode(mqc)) {
589 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
590 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
591 *datap = v ? -oneplushalf : oneplushalf;
592 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
594 #ifdef CONSISTENCY_CHECK
597 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
598 (T1_COLFLAG_RBS * row)));
600 } /* VSC and BYPASS by Antonin */
602 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
605 opj_colflag_t* colflagsp,
607 OPJ_INT32 oneplushalf,
613 opj_mqc_t *mqc = t1->mqc; /* MQC component */
615 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
617 if ((flag & T1_SIG_OTH) &&
618 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
619 (T1_COLFLAG_RBS * row)))) {
620 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
621 if (opj_mqc_decode(mqc)) {
622 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
623 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
624 *datap = v ? -oneplushalf : oneplushalf;
625 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
627 #ifdef CONSISTENCY_CHECK
630 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
631 (T1_COLFLAG_RBS * row)));
633 } /* VSC and BYPASS by Antonin */
636 static void opj_t1_enc_sigpass(opj_t1_t *t1,
645 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
646 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
647 OPJ_UINT32 const extra = t1->flags_stride - t1->w;
651 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
653 for (k = 0; k < (t1->h & ~3U); k += 4) {
655 fprintf(stderr, " k=%d\n", k);
657 for (i = 0; i < t1->w; ++i) {
659 fprintf(stderr, " i=%d\n", i);
662 /* Nothing to do for any of the 4 data points */
666 opj_t1_enc_sigpass_step(
669 &t1->data[((k + 0) * t1->data_stride) + i],
676 opj_t1_enc_sigpass_step(
679 &t1->data[((k + 1) * t1->data_stride) + i],
686 opj_t1_enc_sigpass_step(
689 &t1->data[((k + 2) * t1->data_stride) + i],
696 opj_t1_enc_sigpass_step(
699 &t1->data[((k + 3) * t1->data_stride) + i],
705 3, cblksty & J2K_CCP_CBLKSTY_VSC);
714 fprintf(stderr, " k=%d\n", k);
716 for (i = 0; i < t1->w; ++i) {
718 fprintf(stderr, " i=%d\n", i);
721 /* Nothing to do for any of the 4 data points */
725 for (j = k; j < t1->h; ++j) {
726 opj_t1_enc_sigpass_step(
729 &t1->data[(j * t1->data_stride) + i],
736 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
743 static void opj_t1_dec_sigpass_raw(
748 OPJ_INT32 one, half, oneplushalf, vsc;
750 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
753 oneplushalf = one | half;
754 for (k = 0; k < t1->h; k += 4) {
755 for (i = 0; i < t1->w; ++i) {
756 opj_colflag_t *colflags2 = colflags1 + i;
757 for (j = k; j < k + 4 && j < t1->h; ++j) {
758 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
759 j == t1->h - 1)) ? 1 : 0;
760 opj_t1_dec_sigpass_step_raw(
762 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
764 &t1->data[(j * t1->w) + i],
770 colflags1 += t1->flags_stride;
772 } /* VSC and BYPASS by Antonin */
774 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
776 OPJ_INT32 one, half, oneplushalf; \
777 OPJ_UINT32 i, j, k; \
778 OPJ_INT32 *data1 = t1->data; \
779 opj_flag_t *flags1 = &t1->flags[1]; \
780 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
783 oneplushalf = one | half; \
784 for (k = 0; k < (h & ~3u); k += 4) { \
785 for (i = 0; i < w; ++i) { \
786 OPJ_INT32 *data2 = data1 + i; \
787 opj_flag_t *flags2 = flags1 + i; \
788 opj_colflag_t *colflags2 = colflags1 + i; \
789 if( *colflags2 == 0 ) continue; \
790 flags2 += flags_stride; \
791 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
793 flags2 += flags_stride; \
794 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
796 flags2 += flags_stride; \
797 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
799 flags2 += flags_stride; \
800 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
804 flags1 += flags_stride << 2; \
805 colflags1 += flags_stride; \
807 for (i = 0; i < w; ++i) { \
808 OPJ_INT32 *data2 = data1 + i; \
809 opj_flag_t *flags2 = flags1 + i; \
810 opj_colflag_t *colflags2 = colflags1 + i; \
811 for (j = k; j < h; ++j) { \
812 flags2 += flags_stride; \
813 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
819 static void opj_t1_dec_sigpass_mqc_64x64(
823 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
826 static void opj_t1_dec_sigpass_mqc_generic(
830 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
833 /* VSC and BYPASS by Antonin */
834 static void opj_t1_dec_sigpass_mqc_vsc(
838 OPJ_INT32 one, half, oneplushalf, vsc;
840 OPJ_INT32 *data1 = t1->data;
841 opj_flag_t *flags1 = &t1->flags[1];
842 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
845 oneplushalf = one | half;
846 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
847 for (i = 0; i < t1->w; ++i) {
848 OPJ_INT32 *data2 = data1 + i;
849 opj_flag_t *flags2 = flags1 + i;
850 opj_colflag_t *colflags2 = colflags1 + i;
851 flags2 += t1->flags_stride;
852 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
855 flags2 += t1->flags_stride;
856 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
859 flags2 += t1->flags_stride;
860 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
863 flags2 += t1->flags_stride;
864 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
869 flags1 += t1->flags_stride << 2;
870 colflags1 += t1->flags_stride;
872 for (i = 0; i < t1->w; ++i) {
873 opj_colflag_t *colflags2 = colflags1 + i;
874 for (j = k; j < t1->h; ++j) {
875 vsc = (j == t1->h - 1) ? 1 : 0;
876 opj_t1_dec_sigpass_step_mqc_vsc(
878 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
880 &t1->data[(j * t1->w) + i],
886 } /* VSC and BYPASS by Antonin */
890 Encode refinement pass step
892 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
893 opj_flag_enc_t *flagsp,
905 opj_mqc_t *mqc = t1->mqc; /* MQC component */
907 OPJ_UINT32 const shift_flags =
909 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
911 (*flagsp >> (ci * 3U));
913 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
914 OPJ_UINT32 ctxt = opj_t1_enc_getctxno_mag(shift_flags);
915 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
917 v = opj_int_abs(*datap) & one ? 1 : 0;
919 fprintf(stderr, " ctxt=%d\n", ctxt);
921 opj_mqc_setcurctx(mqc, ctxt); /* ESSAI */
922 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
923 opj_mqc_bypass_enc(mqc, v);
925 opj_mqc_encode(mqc, v);
927 *flagsp |= T1_MU_THIS << (ci * 3U);
932 static INLINE void opj_t1_dec_refpass_step_raw(
934 opj_colflag_t *colflagsp,
942 opj_raw_t *raw = t1->raw; /* RAW component */
944 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
945 (T1_COLFLAG_RBS * row))) ==
946 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
947 v = (OPJ_INT32)opj_raw_decode(raw);
948 t = v ? poshalf : neghalf;
949 *datap += *datap < 0 ? -t : t;
950 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
951 (T1_COLFLAG_RBS * row)));
953 } /* VSC and BYPASS by Antonin */
955 static INLINE void opj_t1_dec_refpass_step_mqc(
957 #ifdef CONSISTENCY_CHECK
960 opj_flag_t *flagsp_unused,
962 opj_colflag_t *colflagsp,
970 opj_mqc_t *mqc = t1->mqc; /* MQC component */
971 #ifdef CONSISTENCY_CHECK
972 assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
973 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
974 (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
979 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
980 (T1_COLFLAG_RBS * row))) ==
981 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
982 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
983 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
984 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
985 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
986 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
987 v = opj_mqc_decode(mqc);
988 t = v ? poshalf : neghalf;
989 *datap += *datap < 0 ? -t : t;
990 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
991 (T1_COLFLAG_RBS * row)));
993 } /* VSC and BYPASS by Antonin */
995 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
998 opj_colflag_t *colflagsp,
1008 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1010 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1011 (T1_COLFLAG_RBS * row))) ==
1012 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
1013 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1014 T1_SGN_S))) : (*flagsp);
1015 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
1016 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
1017 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
1018 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
1019 v = (OPJ_UINT32)opj_mqc_decode(mqc);
1020 t = v ? poshalf : neghalf;
1021 *datap += *datap < 0 ? -t : t;
1022 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
1023 (T1_COLFLAG_RBS * row)));
1025 } /* VSC and BYPASS by Antonin */
1027 static void opj_t1_enc_refpass(
1035 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1036 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
1037 const OPJ_UINT32 extra = t1->flags_stride - t1->w;
1040 #ifdef DEBUG_ENC_REF
1041 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
1043 for (k = 0; k < (t1->h & ~3U); k += 4) {
1044 #ifdef DEBUG_ENC_REF
1045 fprintf(stderr, " k=%d\n", k);
1047 for (i = 0; i < t1->w; ++i) {
1048 #ifdef DEBUG_ENC_REF
1049 fprintf(stderr, " i=%d\n", i);
1051 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1052 /* none significant */
1056 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
1057 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
1058 /* all processed by sigpass */
1063 opj_t1_enc_refpass_step(
1066 &t1->data[((k + 0) * t1->data_stride) + i],
1072 opj_t1_enc_refpass_step(
1075 &t1->data[((k + 1) * t1->data_stride) + i],
1081 opj_t1_enc_refpass_step(
1084 &t1->data[((k + 2) * t1->data_stride) + i],
1090 opj_t1_enc_refpass_step(
1093 &t1->data[((k + 3) * t1->data_stride) + i],
1098 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1106 #ifdef DEBUG_ENC_REF
1107 fprintf(stderr, " k=%d\n", k);
1109 for (i = 0; i < t1->w; ++i) {
1110 #ifdef DEBUG_ENC_REF
1111 fprintf(stderr, " i=%d\n", i);
1113 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1114 /* none significant */
1118 for (j = k; j < t1->h; ++j) {
1119 opj_t1_enc_refpass_step(
1122 &t1->data[(j * t1->data_stride) + i],
1128 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1136 static void opj_t1_dec_refpass_raw(
1140 OPJ_INT32 one, poshalf, neghalf;
1142 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1145 neghalf = bpno > 0 ? -poshalf : -1;
1146 for (k = 0; k < t1->h; k += 4) {
1147 for (i = 0; i < t1->w; ++i) {
1148 opj_colflag_t *colflags2 = colflags1 + i;
1149 for (j = k; j < k + 4 && j < t1->h; ++j) {
1150 opj_t1_dec_refpass_step_raw(
1153 &t1->data[(j * t1->w) + i],
1158 colflags1 += t1->flags_stride;
1160 } /* VSC and BYPASS by Antonin */
1162 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1164 OPJ_INT32 one, poshalf, neghalf; \
1165 OPJ_UINT32 i, j, k; \
1166 OPJ_INT32 *data1 = t1->data; \
1167 opj_flag_t *flags1 = &t1->flags[1]; \
1168 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1170 poshalf = one >> 1; \
1171 neghalf = bpno > 0 ? -poshalf : -1; \
1172 for (k = 0; k < (h & ~3u); k += 4) { \
1173 for (i = 0; i < w; ++i) { \
1174 OPJ_INT32 *data2 = data1 + i; \
1175 opj_flag_t *flags2 = flags1 + i; \
1176 opj_colflag_t *colflags2 = colflags1 + i; \
1177 if( *colflags2 == 0 ) continue; \
1178 flags2 += flags_stride; \
1179 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
1181 flags2 += flags_stride; \
1182 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
1184 flags2 += flags_stride; \
1185 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
1187 flags2 += flags_stride; \
1188 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
1192 flags1 += flags_stride << 2; \
1193 colflags1 += flags_stride; \
1195 for (i = 0; i < w; ++i) { \
1196 OPJ_INT32 *data2 = data1 + i; \
1197 opj_flag_t *flags2 = flags1 + i; \
1198 opj_colflag_t *colflags2 = colflags1 + i; \
1199 for (j = k; j < h; ++j) { \
1200 flags2 += flags_stride; \
1201 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
1207 static void opj_t1_dec_refpass_mqc_64x64(
1211 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1214 static void opj_t1_dec_refpass_mqc_generic(
1218 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
1221 /* VSC and BYPASS by Antonin */
1222 static void opj_t1_dec_refpass_mqc_vsc(
1226 OPJ_INT32 one, poshalf, neghalf;
1229 OPJ_INT32 *data1 = t1->data;
1230 opj_flag_t *flags1 = &t1->flags[1];
1231 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1234 neghalf = bpno > 0 ? -poshalf : -1;
1235 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1236 for (i = 0; i < t1->w; ++i) {
1237 OPJ_INT32 *data2 = data1 + i;
1238 opj_flag_t *flags2 = flags1 + i;
1239 opj_colflag_t *colflags2 = colflags1 + i;
1240 flags2 += t1->flags_stride;
1241 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1244 flags2 += t1->flags_stride;
1245 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1248 flags2 += t1->flags_stride;
1249 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1252 flags2 += t1->flags_stride;
1253 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1257 data1 += t1->w << 2;
1258 flags1 += t1->flags_stride << 2;
1259 colflags1 += t1->flags_stride;
1261 for (i = 0; i < t1->w; ++i) {
1262 opj_colflag_t *colflags2 = colflags1 + i;
1263 for (j = k; j < t1->h; ++j) {
1264 vsc = (j == t1->h - 1) ? 1 : 0;
1265 opj_t1_dec_refpass_step_mqc_vsc(
1267 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1269 &t1->data[(j * t1->w) + i],
1275 } /* VSC and BYPASS by Antonin */
1278 Encode clean-up pass step
1280 static void opj_t1_enc_clnpass_step(
1282 opj_flag_enc_t *flagsp,
1295 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1297 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1298 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1300 if ((*flagsp & check) == check) {
1302 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1303 } else if (runlen == 1) {
1304 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1305 } else if (runlen == 2) {
1306 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1307 } else if (runlen == 3) {
1308 *flagsp &= ~(T1_PI_3);
1313 for (ci = runlen; ci < lim; ++ci) {
1315 OPJ_UINT32 vsc_mask;
1316 opj_flag_enc_t flags;
1318 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1319 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1321 flags = *flagsp & vsc_mask;
1323 if ((agg != 0) && (ci == runlen)) {
1327 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1328 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U), orient);
1329 #ifdef DEBUG_ENC_CLN
1330 printf(" ctxt1=%d\n", ctxt1);
1332 opj_mqc_setcurctx(mqc, ctxt1);
1333 v = opj_int_abs(*datap) & one ? 1 : 0;
1334 opj_mqc_encode(mqc, v);
1336 OPJ_UINT32 ctxt2, spb;
1339 /* Note: using flags instead of *flagsp & vsc_mask result */
1340 /* in slow down. Probably because of register pressure */
1341 lu = opj_t1_enc_getctxtno_sc_or_spb_index(
1343 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1345 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1347 ctxt2 = opj_t1_enc_getctxno_sc(lu);
1348 #ifdef DEBUG_ENC_CLN
1349 printf(" ctxt2=%d\n", ctxt2);
1351 opj_mqc_setcurctx(mqc, ctxt2);
1353 v = *datap < 0 ? 1 : 0;
1354 spb = opj_t1_enc_getspb(lu);
1355 #ifdef DEBUG_ENC_CLN
1356 printf(" spb=%d\n", spb);
1358 opj_mqc_encode(mqc, v ^ spb);
1359 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
1362 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1363 datap += t1->data_stride;
1368 static void opj_t1_dec_clnpass_step_partial(
1371 opj_colflag_t *colflagsp,
1373 OPJ_INT32 oneplushalf,
1377 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1380 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1381 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1382 *datap = v ? -oneplushalf : oneplushalf;
1383 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1385 #ifdef CONSISTENCY_CHECK
1386 *flagsp &= ~T1_VISIT;
1388 } /* VSC and BYPASS by Antonin */
1390 static void opj_t1_dec_clnpass_step(
1393 opj_colflag_t *colflagsp,
1395 OPJ_INT32 oneplushalf,
1400 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1401 #ifdef CONSISTENCY_CHECK
1402 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1403 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1405 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1408 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1409 if (opj_mqc_decode(mqc)) {
1410 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1411 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1412 *datap = v ? -oneplushalf : oneplushalf;
1413 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1417 #ifdef CONSISTENCY_CHECK
1418 *flagsp &= ~T1_VISIT;
1420 } /* VSC and BYPASS by Antonin */
1422 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1425 opj_colflag_t *colflagsp,
1427 OPJ_INT32 oneplushalf,
1429 OPJ_UINT32 flags_stride)
1434 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1437 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1439 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1440 if (opj_mqc_decode(mqc)) {
1441 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1442 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1443 *datap = v ? -oneplushalf : oneplushalf;
1444 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1447 /*flagsp &= ~T1_VISIT;*/
1450 static void opj_t1_dec_clnpass_step_vsc(
1453 opj_colflag_t *colflagsp,
1455 OPJ_INT32 oneplushalf,
1462 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1464 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1469 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1470 (T1_COLFLAG_RBS * row)))) {
1471 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1472 if (opj_mqc_decode(mqc)) {
1474 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1475 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1476 *datap = v ? -oneplushalf : oneplushalf;
1477 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1480 #ifdef CONSISTENCY_CHECK
1481 *flagsp &= ~T1_VISIT;
1485 #define MACRO_t1_enc_flags(x,y) t1->enc_flags[((x)*(t1->flags_stride))+(y)]
1487 static void opj_t1_enc_clnpass(
1495 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1496 OPJ_UINT32 agg, runlen;
1498 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1500 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1501 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1505 #ifdef DEBUG_ENC_CLN
1506 printf("enc_clnpass: bpno=%d\n", bpno);
1508 for (k = 0; k < (t1->h & ~3U); k += 4) {
1509 #ifdef DEBUG_ENC_CLN
1510 printf(" k=%d\n", k);
1512 for (i = 0; i < t1->w; ++i) {
1513 #ifdef DEBUG_ENC_CLN
1514 printf(" i=%d\n", i);
1516 agg = !(ENC_FLAGS(i, k) & agg_mask);
1517 #ifdef DEBUG_ENC_CLN
1518 printf(" agg=%d\n", agg);
1521 for (runlen = 0; runlen < 4; ++runlen) {
1522 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1526 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1527 opj_mqc_encode(mqc, runlen != 4);
1531 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1532 opj_mqc_encode(mqc, runlen >> 1);
1533 opj_mqc_encode(mqc, runlen & 1);
1537 opj_t1_enc_clnpass_step(
1540 &t1->data[((k + runlen) * t1->data_stride) + i],
1554 #ifdef DEBUG_ENC_CLN
1555 printf(" k=%d\n", k);
1557 for (i = 0; i < t1->w; ++i) {
1558 #ifdef DEBUG_ENC_CLN
1559 printf(" i=%d\n", i);
1560 printf(" agg=%d\n", agg);
1562 opj_t1_enc_clnpass_step(
1565 &t1->data[((k + runlen) * t1->data_stride) + i],
1579 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1581 #ifdef CONSISTENCY_CHECK
1582 #define REMOVE_VISIT_FLAG(flagptr) *(flagptr) &= (opj_flag_t)~T1_VISIT
1583 #define T1_SIG_OR_VISIT (T1_SIG | T1_VISIT)
1585 #define REMOVE_VISIT_FLAG(flagptr) do {} while(0)
1586 #define T1_SIG_OR_VISIT 0
1589 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1591 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1592 OPJ_UINT32 i, j, k; \
1593 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1595 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1599 oneplushalf = one | half; \
1600 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1601 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1602 for (k = 0; k < h; k += 4) { \
1603 for (i = 0; i < w; ++i) { \
1604 opj_colflag_t *colflags2 = colflags1 + i; \
1606 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1607 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1608 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1609 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1610 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1611 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1616 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1617 if (!opj_mqc_decode(mqc)) { \
1620 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1621 runlen = opj_mqc_decode(mqc); \
1622 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1626 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1627 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1628 opj_t1_dec_clnpass_step_vsc( \
1630 &t1->flags[((j+1) * flags_stride) + i + 1], \
1632 &t1->data[(j * w) + i], \
1634 agg && (j == k + (OPJ_UINT32)runlen), \
1637 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1639 colflags1 += flags_stride; \
1642 OPJ_INT32 *data1 = t1->data; \
1643 opj_flag_t *flags1 = &t1->flags[1]; \
1644 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1645 for (k = 0; k < (h & ~3u); k += 4) { \
1646 for (i = 0; i < w; ++i) { \
1647 OPJ_INT32 *data2 = data1 + i; \
1648 opj_flag_t *flags2 = flags1 + i; \
1649 opj_colflag_t *colflags2 = colflags1 + i; \
1650 opj_colflag_t colflags = *colflags2; \
1651 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1652 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1653 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1654 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1655 if( consistency_check ) { \
1656 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1657 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1658 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1659 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG_OR_VISIT | T1_SIG_OTH)) ); \
1662 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1663 if (!opj_mqc_decode(mqc)) { \
1666 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1667 runlen = opj_mqc_decode(mqc); \
1668 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1669 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1670 data2 += (OPJ_UINT32)runlen * w; \
1671 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1672 flags2 += flags_stride; \
1673 if (j == (OPJ_UINT32)runlen) { \
1674 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1676 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1681 flags2 += flags_stride; \
1682 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1683 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1684 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1686 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1688 flags2 += flags_stride; \
1689 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1690 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1691 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1693 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1695 flags2 += flags_stride; \
1696 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1697 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1698 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1700 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1702 flags2 += flags_stride; \
1703 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG_OR_VISIT))) ); } \
1704 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1705 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1707 if( consistency_check ) REMOVE_VISIT_FLAG(*flags2); \
1710 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1713 flags1 += flags_stride << 2; \
1714 colflags1 += flags_stride; \
1716 for (i = 0; i < w; ++i) { \
1717 OPJ_INT32 *data2 = data1 + i; \
1718 opj_flag_t *flags2 = flags1 + i; \
1719 opj_colflag_t *colflags2 = colflags1 + i; \
1720 for (j = k; j < h; ++j) { \
1721 flags2 += flags_stride; \
1722 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1725 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1731 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1732 v = opj_mqc_decode(mqc); \
1733 v = (v << 1) | opj_mqc_decode(mqc); \
1734 v = (v << 1) | opj_mqc_decode(mqc); \
1735 v = (v << 1) | opj_mqc_decode(mqc); \
1738 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1742 } /* VSC and BYPASS by Antonin */
1744 static void opj_t1_dec_clnpass_64x64(
1749 #ifdef CONSISTENCY_CHECK
1750 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1752 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1756 static void opj_t1_dec_clnpass_generic(
1761 #ifdef CONSISTENCY_CHECK
1762 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1765 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1771 /** mod fixed_quality */
1772 static OPJ_FLOAT64 opj_t1_getwmsedec(
1779 OPJ_FLOAT64 stepsize,
1780 OPJ_UINT32 numcomps,
1781 const OPJ_FLOAT64 * mct_norms,
1782 OPJ_UINT32 mct_numcomps)
1784 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1785 OPJ_ARG_NOT_USED(numcomps);
1787 if (mct_norms && (compno < mct_numcomps)) {
1788 w1 = mct_norms[compno];
1792 w2 = opj_dwt_getnorm(level, orient);
1793 } else { /* if (qmfbid == 0) */
1794 w2 = opj_dwt_getnorm_real(level, orient);
1797 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1798 wmsedec *= wmsedec * nmsedec / 8192.0;
1803 static OPJ_BOOL opj_t1_allocate_buffers(
1810 /* encoder uses tile buffer, so no need to allocate */
1814 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1815 /* Overflow check */
1816 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1817 /* FIXME event manager error callback */
1821 datasize = (size_t)w * h;
1823 /* Overflow check */
1824 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1825 /* FIXME event manager error callback */
1829 if (datasize > (size_t)t1->datasize) {
1830 opj_aligned_free(t1->data);
1831 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1833 /* FIXME event manager error callback */
1836 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1837 /* TODO remove this if t1->datasize type changes to size_t */
1838 /* Overflow check */
1839 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1840 /* FIXME event manager error callback */
1844 t1->datasize = (OPJ_UINT32)datasize;
1846 /* memset first arg is declared to never be null by gcc */
1847 if (t1->data != NULL) {
1848 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1852 /* Overflow check */
1853 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1854 /* FIXME event manager error callback */
1857 t1->flags_stride = w + 2U; /* can't be 0U */
1859 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1860 /* Overflow check */
1861 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1862 /* FIXME event manager error callback */
1867 flagssize = (size_t)h + 2U;
1869 flagssize = (h + 3U) / 4U + 2U;
1872 /* Overflow check */
1873 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1874 /* FIXME event manager error callback */
1877 flagssize *= (size_t)t1->flags_stride;
1881 size_t colflags_size;
1883 if (flagssize > (size_t)t1->flagssize) {
1884 /* Overflow check */
1885 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1886 /* FIXME event manager error callback */
1889 opj_aligned_free(t1->flags);
1890 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1892 /* FIXME event manager error callback */
1895 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1896 /* TODO remove this if t1->flagssize type changes to size_t */
1897 /* Overflow check */
1898 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1899 /* FIXME event manager error callback */
1904 t1->flagssize = (OPJ_UINT32)flagssize;
1906 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1908 colflags_size = ((((size_t)h + 3U) / 4U) +
1909 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1911 /* Overflow check */
1912 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1913 /* FIXME event manager error callback */
1916 colflags_size *= (size_t)t1->flags_stride;
1918 if (colflags_size > (size_t)t1->colflags_size) {
1919 /* Overflow check */
1920 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1921 /* FIXME event manager error callback */
1924 opj_aligned_free(t1->colflags);
1925 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1927 if (!t1->colflags) {
1928 /* FIXME event manager error callback */
1931 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1932 /* TODO remove this if t1->colflags_size type changes to size_t */
1933 /* Overflow check */
1934 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1935 /* FIXME event manager error callback */
1939 t1->colflags_size = (OPJ_UINT32)colflags_size;
1941 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1946 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1948 if (flagssize > (size_t)t1->flagssize) {
1949 /* Overflow check */
1950 if (flagssize > (SIZE_MAX / sizeof(opj_flag_enc_t))) {
1951 /* FIXME event manager error callback */
1954 opj_aligned_free(t1->enc_flags);
1955 t1->enc_flags = (opj_flag_enc_t*) opj_aligned_malloc(flagssize * sizeof(
1957 if (!t1->enc_flags) {
1958 /* FIXME event manager error callback */
1961 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1962 /* TODO remove this if t1->flagssize type changes to size_t */
1963 /* Overflow check */
1964 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1965 /* FIXME event manager error callback */
1970 t1->flagssize = (OPJ_UINT32)flagssize;
1972 memset(t1->enc_flags, 0, flagssize * sizeof(opj_flag_enc_t));
1974 p = &t1->enc_flags[0];
1975 for (x = 0; x < t1->flags_stride; ++x) {
1976 /* magic value to hopefully stop any passes being interested in this entry */
1977 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1980 p = &t1->enc_flags[((flags_height + 1) * t1->flags_stride)];
1981 for (x = 0; x < t1->flags_stride; ++x) {
1982 /* magic value to hopefully stop any passes being interested in this entry */
1983 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1988 p = &t1->enc_flags[((flags_height) * t1->flags_stride)];
1990 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1991 } else if (h % 4 == 2) {
1992 v |= T1_PI_2 | T1_PI_3;
1993 } else if (h % 4 == 3) {
1996 for (x = 0; x < t1->flags_stride; ++x) {
2008 /* ----------------------------------------------------------------------- */
2010 /* ----------------------------------------------------------------------- */
2012 * Creates a new Tier 1 handle
2013 * and initializes the look-up tables of the Tier-1 coder/decoder
2014 * @return a new T1 handle if successful, returns NULL otherwise
2016 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
2018 opj_t1_t *l_t1 = 00;
2020 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
2025 /* create MQC and RAW handles */
2026 l_t1->mqc = opj_mqc_create();
2028 opj_t1_destroy(l_t1);
2032 l_t1->raw = opj_raw_create();
2034 opj_t1_destroy(l_t1);
2037 l_t1->encoder = isEncoder;
2044 * Destroys a previously created T1 handle
2046 * @param p_t1 Tier 1 handle to destroy
2048 void opj_t1_destroy(opj_t1_t *p_t1)
2054 /* destroy MQC and RAW handles */
2055 opj_mqc_destroy(p_t1->mqc);
2057 opj_raw_destroy(p_t1->raw);
2060 /* encoder uses tile buffer, so no need to free */
2061 if (!p_t1->encoder && p_t1->data) {
2062 opj_aligned_free(p_t1->data);
2067 opj_aligned_free(p_t1->flags);
2071 if (p_t1->colflags) {
2072 opj_aligned_free(p_t1->colflags);
2073 p_t1->colflags = 00;
2076 if (p_t1->enc_flags) {
2077 opj_aligned_free(p_t1->enc_flags);
2078 p_t1->enc_flags = 00;
2085 opj_tcd_cblk_dec_t* cblk;
2086 opj_tcd_band_t* band;
2087 opj_tcd_tilecomp_t* tilec;
2089 volatile OPJ_BOOL* pret;
2090 } opj_t1_cblk_decode_processing_job_t;
2092 static void opj_t1_destroy_wrapper(void* t1)
2094 opj_t1_destroy((opj_t1_t*) t1);
2097 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
2099 opj_tcd_cblk_dec_t* cblk;
2100 opj_tcd_band_t* band;
2101 opj_tcd_tilecomp_t* tilec;
2103 OPJ_INT32* OPJ_RESTRICT datap;
2104 OPJ_UINT32 cblk_w, cblk_h;
2107 opj_t1_cblk_decode_processing_job_t* job;
2112 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
2118 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2120 if (!*(job->pret)) {
2125 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
2127 t1 = opj_t1_create(OPJ_FALSE);
2128 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
2131 if (OPJ_FALSE == opj_t1_decode_cblk(
2135 (OPJ_UINT32)tccp->roishift,
2137 *(job->pret) = OPJ_FALSE;
2142 x = cblk->x0 - band->x0;
2143 y = cblk->y0 - band->y0;
2144 if (band->bandno & 1) {
2145 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2146 x += pres->x1 - pres->x0;
2148 if (band->bandno & 2) {
2149 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2150 y += pres->y1 - pres->y0;
2157 if (tccp->roishift) {
2158 OPJ_INT32 thresh = 1 << tccp->roishift;
2159 for (j = 0; j < cblk_h; ++j) {
2160 for (i = 0; i < cblk_w; ++i) {
2161 OPJ_INT32 val = datap[(j * cblk_w) + i];
2162 OPJ_INT32 mag = abs(val);
2163 if (mag >= thresh) {
2164 mag >>= tccp->roishift;
2165 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
2170 if (tccp->qmfbid == 1) {
2171 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
2173 for (j = 0; j < cblk_h; ++j) {
2175 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
2176 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
2177 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
2178 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
2179 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
2180 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
2181 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
2182 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
2183 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
2185 for (; i < cblk_w; ++i) {
2186 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
2187 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
2190 } else { /* if (tccp->qmfbid == 0) */
2191 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
2192 tile_w + (OPJ_UINT32)x];
2193 for (j = 0; j < cblk_h; ++j) {
2194 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
2195 for (i = 0; i < cblk_w; ++i) {
2196 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
2209 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
2210 volatile OPJ_BOOL* pret,
2211 opj_tcd_tilecomp_t* tilec,
2215 OPJ_UINT32 resno, bandno, precno, cblkno;
2217 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
2218 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2220 for (bandno = 0; bandno < res->numbands; ++bandno) {
2221 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2223 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2224 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2226 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2227 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2228 opj_t1_cblk_decode_processing_job_t* job;
2230 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2231 sizeof(opj_t1_cblk_decode_processing_job_t));
2242 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2255 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2256 opj_tcd_cblk_dec_t* cblk,
2258 OPJ_UINT32 roishift,
2261 opj_raw_t *raw = t1->raw; /* RAW component */
2262 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2264 OPJ_INT32 bpno_plus_one;
2265 OPJ_UINT32 passtype;
2266 OPJ_UINT32 segno, passno;
2267 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2269 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2271 if (!opj_t1_allocate_buffers(
2273 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2274 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2278 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2281 opj_mqc_resetstates(mqc);
2282 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2283 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2284 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2286 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2287 opj_tcd_seg_t *seg = &cblk->segs[segno];
2290 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2291 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2292 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2293 if (seg->data == 00) {
2296 if (type == T1_TYPE_RAW) {
2297 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2299 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2305 if (t1->w == 64 && t1->h == 64) {
2306 for (passno = 0; (passno < seg->real_num_passes) &&
2307 (bpno_plus_one >= 1); ++passno) {
2310 if (type == T1_TYPE_RAW) {
2311 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2313 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2314 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2316 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2321 if (type == T1_TYPE_RAW) {
2322 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2324 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2325 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2327 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2332 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2336 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2337 opj_mqc_resetstates(mqc);
2338 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2339 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2340 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2342 if (++passtype == 3) {
2348 for (passno = 0; (passno < seg->real_num_passes) &&
2349 (bpno_plus_one >= 1); ++passno) {
2352 if (type == T1_TYPE_RAW) {
2353 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2355 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2356 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2358 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2363 if (type == T1_TYPE_RAW) {
2364 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2366 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2367 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2369 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2374 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2378 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2379 opj_mqc_resetstates(mqc);
2380 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2381 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2382 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2384 if (++passtype == 3) {
2397 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2398 opj_tcd_tile_t *tile,
2400 const OPJ_FLOAT64 * mct_norms,
2401 OPJ_UINT32 mct_numcomps
2404 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2406 tile->distotile = 0; /* fixed_quality */
2408 for (compno = 0; compno < tile->numcomps; ++compno) {
2409 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2410 opj_tccp_t* tccp = &tcp->tccps[compno];
2411 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2413 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2414 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2416 for (bandno = 0; bandno < res->numbands; ++bandno) {
2417 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2418 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2420 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2421 opj_tcd_precinct_t *prc = &band->precincts[precno];
2423 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2424 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2425 OPJ_INT32* OPJ_RESTRICT tiledp;
2428 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2430 OPJ_INT32 x = cblk->x0 - band->x0;
2431 OPJ_INT32 y = cblk->y0 - band->y0;
2432 if (band->bandno & 1) {
2433 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2434 x += pres->x1 - pres->x0;
2436 if (band->bandno & 2) {
2437 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2438 y += pres->y1 - pres->y0;
2441 if (!opj_t1_allocate_buffers(
2443 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2444 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2450 tileLineAdvance = tile_w - cblk_w;
2452 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2454 t1->data_stride = tile_w;
2455 if (tccp->qmfbid == 1) {
2456 for (j = 0; j < cblk_h; ++j) {
2457 for (i = 0; i < cblk_w; ++i) {
2458 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2461 tileIndex += tileLineAdvance;
2463 } else { /* if (tccp->qmfbid == 0) */
2464 for (j = 0; j < cblk_h; ++j) {
2465 for (i = 0; i < cblk_w; ++i) {
2466 OPJ_INT32 tmp = tiledp[tileIndex];
2473 tileIndex += tileLineAdvance;
2482 tilec->numresolutions - 1 - resno,
2499 /** mod fixed_quality */
2500 static void opj_t1_encode_cblk(opj_t1_t *t1,
2501 opj_tcd_cblk_enc_t* cblk,
2506 OPJ_FLOAT64 stepsize,
2508 OPJ_UINT32 numcomps,
2509 opj_tcd_tile_t * tile,
2510 const OPJ_FLOAT64 * mct_norms,
2511 OPJ_UINT32 mct_numcomps)
2513 OPJ_FLOAT64 cumwmsedec = 0.0;
2515 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2519 OPJ_UINT32 passtype;
2520 OPJ_INT32 nmsedec = 0;
2523 OPJ_BYTE type = T1_TYPE_MQ;
2524 OPJ_FLOAT64 tempwmsedec;
2526 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2529 for (i = 0; i < t1->w; ++i) {
2530 for (j = 0; j < t1->h; ++j) {
2531 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2532 max = opj_int_max(max, tmp);
2536 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2537 T1_NMSEDEC_FRACBITS) : 0;
2539 bpno = (OPJ_INT32)(cblk->numbps - 1);
2542 opj_mqc_resetstates(mqc);
2543 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2544 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2545 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2546 opj_mqc_init_enc(mqc, cblk->data);
2548 for (passno = 0; bpno >= 0; ++passno) {
2549 opj_tcd_pass_t *pass = &cblk->passes[passno];
2550 OPJ_UINT32 correction = 3;
2551 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2552 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2556 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
2559 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2562 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
2563 /* code switch SEGMARK (i.e. SEGSYM) */
2564 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2565 opj_mqc_segmark_enc(mqc);
2571 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2572 stepsize, numcomps, mct_norms, mct_numcomps) ;
2573 cumwmsedec += tempwmsedec;
2574 tile->distotile += tempwmsedec;
2576 /* Code switch "RESTART" (i.e. TERMALL) */
2577 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2579 if (type == T1_TYPE_RAW) {
2582 /* correction = mqc_bypass_flush_enc(); */
2583 } else { /* correction = mqc_restart_enc(); */
2589 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2590 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2591 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2592 if (type == T1_TYPE_RAW) {
2595 /* correction = mqc_bypass_flush_enc(); */
2596 } else { /* correction = mqc_restart_enc(); */
2606 if (++passtype == 3) {
2611 if (pass->term && bpno > 0) {
2612 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2613 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2614 if (type == T1_TYPE_RAW) {
2615 opj_mqc_bypass_init_enc(mqc);
2617 opj_mqc_restart_init_enc(mqc);
2621 pass->distortiondec = cumwmsedec;
2622 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2624 /* Code-switch "RESET" */
2625 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2626 opj_mqc_reset_enc(mqc);
2630 /* Code switch "ERTERM" (i.e. PTERM) */
2631 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2632 opj_mqc_erterm_enc(mqc);
2633 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2637 cblk->totalpasses = passno;
2639 for (passno = 0; passno < cblk->totalpasses; passno++) {
2640 opj_tcd_pass_t *pass = &cblk->passes[passno];
2641 if (pass->rate > opj_mqc_numbytes(mqc)) {
2642 pass->rate = opj_mqc_numbytes(mqc);
2644 /*Preventing generation of FF as last data byte of a pass*/
2645 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2648 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);