2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
42 /* #define CONSISTENCY_CHECK */
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
47 /** @name Local static functions */
50 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
52 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
53 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
54 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
55 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
59 Encode significant pass
61 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
71 Decode significant pass
74 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
78 OPJ_INT32 oneplushalf,
83 static INLINE void opj_t1_dec_sigpass_step_raw(
86 opj_colflag_t* colflagsp,
88 OPJ_INT32 oneplushalf,
91 static INLINE void opj_t1_dec_sigpass_step_mqc(
94 opj_colflag_t* colflagsp,
96 OPJ_INT32 oneplushalf,
98 OPJ_UINT32 flags_stride);
99 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
102 opj_colflag_t* colflagsp,
104 OPJ_INT32 oneplushalf,
110 Encode significant pass
112 static void opj_t1_enc_sigpass(opj_t1_t *t1,
119 Decode significant pass
121 static void opj_t1_dec_sigpass_raw(
125 static void opj_t1_dec_sigpass_mqc_vsc(
132 Encode refinement pass
134 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
145 Encode refinement pass
147 static void opj_t1_enc_refpass(opj_t1_t *t1,
154 Decode refinement pass
156 static void opj_t1_dec_refpass_raw(
159 static void opj_t1_dec_refpass_mqc_vsc(
165 Decode refinement pass
168 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
177 static INLINE void opj_t1_dec_refpass_step_raw(
179 opj_colflag_t *colflagsp,
184 static INLINE void opj_t1_dec_refpass_step_mqc(
187 opj_colflag_t *colflagsp,
192 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
195 opj_colflag_t *colflagsp,
207 static void opj_t1_enc_clnpass_step(
219 static void opj_t1_dec_clnpass_step_partial(
222 opj_colflag_t *colflagsp,
224 OPJ_INT32 oneplushalf,
226 static void opj_t1_dec_clnpass_step(
229 opj_colflag_t *colflagsp,
231 OPJ_INT32 oneplushalf,
233 static void opj_t1_dec_clnpass_step_vsc(
236 opj_colflag_t *colflagsp,
238 OPJ_INT32 oneplushalf,
245 static void opj_t1_enc_clnpass(
251 static OPJ_FLOAT64 opj_t1_getwmsedec(
258 OPJ_FLOAT64 stepsize,
260 const OPJ_FLOAT64 * mct_norms,
261 OPJ_UINT32 mct_numcomps);
263 static void opj_t1_encode_cblk(opj_t1_t *t1,
264 opj_tcd_cblk_enc_t* cblk,
269 OPJ_FLOAT64 stepsize,
272 opj_tcd_tile_t * tile,
273 const OPJ_FLOAT64 * mct_norms,
274 OPJ_UINT32 mct_numcomps);
279 @param cblk Code-block coding parameters
281 @param roishift Region of interest shifting value
282 @param cblksty Code-block style
284 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
285 opj_tcd_cblk_dec_t* cblk,
290 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
298 /* ----------------------------------------------------------------------- */
300 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
302 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
305 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f)
307 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
310 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
312 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
313 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
317 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f)
319 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
322 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
325 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
328 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
331 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
334 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
340 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
343 opj_flag_t *np = flagsp - stride;
344 opj_flag_t *sp = flagsp + stride;
346 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
347 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
348 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
350 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
353 np[0] |= (opj_flag_t)(flag_N << 2U);
356 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
358 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
361 sp[0] |= (opj_flag_t)flag_N;
365 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp,
366 opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
368 opj_t1_updateflags(flagsp, s, stride);
370 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
371 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
372 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
373 (T1_COLFLAG_RBS * row)) |
374 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
375 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
376 (T1_COLFLAG_RBS * row)) |
377 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
378 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
379 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
380 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
381 } else if (row == 3U) {
382 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
383 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
384 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
385 (T1_COLFLAG_RBS * row)) |
386 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
387 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
388 (T1_COLFLAG_RBS * row)) |
389 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1))));
390 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
391 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
392 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
394 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
395 (T1_COLFLAG_RBS * row)) |
396 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
397 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
398 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
399 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
400 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
401 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
402 (T1_COLFLAG_RBS * row)) |
403 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
404 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
408 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
421 opj_mqc_t *mqc = t1->mqc; /* MQC component */
423 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
424 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
425 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
426 v = (opj_int_abs(*datap) & one) ? 1 : 0;
427 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag)); /* ESSAI */
428 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
429 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
431 opj_mqc_encode(mqc, (OPJ_UINT32)v);
434 v = *datap < 0 ? 1 : 0;
435 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
437 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
438 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
439 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
441 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
443 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
450 static INLINE void opj_t1_dec_sigpass_step_raw(
453 opj_colflag_t* colflagsp,
455 OPJ_INT32 oneplushalf,
460 opj_raw_t *raw = t1->raw; /* RAW component */
462 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
464 if ((flag & T1_SIG_OTH) &&
465 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
466 (T1_COLFLAG_RBS * row)))) {
467 if (opj_raw_decode(raw)) {
468 v = opj_raw_decode(raw); /* ESSAI */
469 *datap = v ? -oneplushalf : oneplushalf;
470 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
472 #ifdef CONSISTENCY_CHECK
475 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
479 static INLINE void opj_t1_dec_sigpass_step_mqc(
482 opj_colflag_t* colflagsp,
484 OPJ_INT32 oneplushalf,
486 OPJ_UINT32 flags_stride)
490 opj_mqc_t *mqc = t1->mqc; /* MQC component */
491 #ifdef CONSISTENCY_CHECK
492 assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
493 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
494 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
495 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
497 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
498 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
499 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
501 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
502 if (opj_mqc_decode(mqc)) {
503 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
504 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
505 *datap = v ? -oneplushalf : oneplushalf;
506 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
508 #ifdef CONSISTENCY_CHECK
511 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
513 } /* VSC and BYPASS by Antonin */
515 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
518 opj_colflag_t* colflagsp,
520 OPJ_INT32 oneplushalf,
526 opj_mqc_t *mqc = t1->mqc; /* MQC component */
528 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
530 if ((flag & T1_SIG_OTH) &&
531 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
532 (T1_COLFLAG_RBS * row)))) {
533 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
534 if (opj_mqc_decode(mqc)) {
535 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
536 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
537 *datap = v ? -oneplushalf : oneplushalf;
538 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
540 #ifdef CONSISTENCY_CHECK
543 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
545 } /* VSC and BYPASS by Antonin */
549 static void opj_t1_enc_sigpass(opj_t1_t *t1,
556 OPJ_UINT32 i, j, k, vsc;
560 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
561 for (k = 0; k < t1->h; k += 4) {
562 for (i = 0; i < t1->w; ++i) {
563 for (j = k; j < k + 4 && j < t1->h; ++j) {
564 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
565 j == t1->h - 1)) ? 1 : 0;
566 opj_t1_enc_sigpass_step(
568 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
569 &t1->data[(j * t1->data_stride) + i],
580 static void opj_t1_dec_sigpass_raw(
585 OPJ_INT32 one, half, oneplushalf, vsc;
587 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
590 oneplushalf = one | half;
591 for (k = 0; k < t1->h; k += 4) {
592 for (i = 0; i < t1->w; ++i) {
593 opj_colflag_t *colflags2 = colflags1 + i;
594 for (j = k; j < k + 4 && j < t1->h; ++j) {
595 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
596 j == t1->h - 1)) ? 1 : 0;
597 opj_t1_dec_sigpass_step_raw(
599 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
601 &t1->data[(j * t1->w) + i],
607 colflags1 += t1->flags_stride;
609 } /* VSC and BYPASS by Antonin */
611 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
613 OPJ_INT32 one, half, oneplushalf; \
614 OPJ_UINT32 i, j, k; \
615 OPJ_INT32 *data1 = t1->data; \
616 opj_flag_t *flags1 = &t1->flags[1]; \
617 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
620 oneplushalf = one | half; \
621 for (k = 0; k < (h & ~3u); k += 4) { \
622 for (i = 0; i < w; ++i) { \
623 OPJ_INT32 *data2 = data1 + i; \
624 opj_flag_t *flags2 = flags1 + i; \
625 opj_colflag_t *colflags2 = colflags1 + i; \
626 if( *colflags2 == 0 ) continue; \
627 flags2 += flags_stride; \
628 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
630 flags2 += flags_stride; \
631 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
633 flags2 += flags_stride; \
634 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
636 flags2 += flags_stride; \
637 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
641 flags1 += flags_stride << 2; \
642 colflags1 += flags_stride; \
644 for (i = 0; i < w; ++i) { \
645 OPJ_INT32 *data2 = data1 + i; \
646 opj_flag_t *flags2 = flags1 + i; \
647 opj_colflag_t *colflags2 = colflags1 + i; \
648 for (j = k; j < h; ++j) { \
649 flags2 += flags_stride; \
650 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
656 static void opj_t1_dec_sigpass_mqc_64x64(
660 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
663 static void opj_t1_dec_sigpass_mqc_generic(
667 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
670 /* VSC and BYPASS by Antonin */
671 static void opj_t1_dec_sigpass_mqc_vsc(
675 OPJ_INT32 one, half, oneplushalf, vsc;
677 OPJ_INT32 *data1 = t1->data;
678 opj_flag_t *flags1 = &t1->flags[1];
679 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
682 oneplushalf = one | half;
683 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
684 for (i = 0; i < t1->w; ++i) {
685 OPJ_INT32 *data2 = data1 + i;
686 opj_flag_t *flags2 = flags1 + i;
687 opj_colflag_t *colflags2 = colflags1 + i;
688 flags2 += t1->flags_stride;
689 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
692 flags2 += t1->flags_stride;
693 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
696 flags2 += t1->flags_stride;
697 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
700 flags2 += t1->flags_stride;
701 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
706 flags1 += t1->flags_stride << 2;
707 colflags1 += t1->flags_stride;
709 for (i = 0; i < t1->w; ++i) {
710 opj_colflag_t *colflags2 = colflags1 + i;
711 for (j = k; j < t1->h; ++j) {
712 vsc = (j == t1->h - 1) ? 1 : 0;
713 opj_t1_dec_sigpass_step_mqc_vsc(
715 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
717 &t1->data[(j * t1->w) + i],
723 } /* VSC and BYPASS by Antonin */
727 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
739 opj_mqc_t *mqc = t1->mqc; /* MQC component */
741 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
742 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
743 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
744 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
746 v = (opj_int_abs(*datap) & one) ? 1 : 0;
747 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
748 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
749 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
751 opj_mqc_encode(mqc, (OPJ_UINT32)v);
753 *flagsp |= T1_REFINE;
757 static INLINE void opj_t1_dec_refpass_step_raw(
759 opj_colflag_t *colflagsp,
767 opj_raw_t *raw = t1->raw; /* RAW component */
769 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
770 (T1_COLFLAG_RBS * row))) ==
771 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
772 v = (OPJ_INT32)opj_raw_decode(raw);
773 t = v ? poshalf : neghalf;
774 *datap += *datap < 0 ? -t : t;
775 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 <<
776 (T1_COLFLAG_RBS * row));
778 } /* VSC and BYPASS by Antonin */
780 static INLINE void opj_t1_dec_refpass_step_mqc(
782 #ifdef CONSISTENCY_CHECK
785 opj_flag_t *flagsp_unused,
787 opj_colflag_t *colflagsp,
795 opj_mqc_t *mqc = t1->mqc; /* MQC component */
796 #ifdef CONSISTENCY_CHECK
797 assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
798 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
799 (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
804 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
805 (T1_COLFLAG_RBS * row))) ==
806 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
807 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
808 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
809 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
810 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
811 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
812 v = opj_mqc_decode(mqc);
813 t = v ? poshalf : neghalf;
814 *datap += *datap < 0 ? -t : t;
815 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 <<
816 (T1_COLFLAG_RBS * row));
818 } /* VSC and BYPASS by Antonin */
820 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
823 opj_colflag_t *colflagsp,
833 opj_mqc_t *mqc = t1->mqc; /* MQC component */
835 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
836 (T1_COLFLAG_RBS * row))) ==
837 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
838 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
839 T1_SGN_S))) : (*flagsp);
840 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
841 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
842 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
843 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
844 v = (OPJ_UINT32)opj_mqc_decode(mqc);
845 t = v ? poshalf : neghalf;
846 *datap += *datap < 0 ? -t : t;
847 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 <<
848 (T1_COLFLAG_RBS * row));
850 } /* VSC and BYPASS by Antonin */
853 static void opj_t1_enc_refpass(
860 OPJ_UINT32 i, j, k, vsc;
864 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
865 for (k = 0; k < t1->h; k += 4) {
866 for (i = 0; i < t1->w; ++i) {
867 for (j = k; j < k + 4 && j < t1->h; ++j) {
868 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
869 j == t1->h - 1)) ? 1 : 0;
870 opj_t1_enc_refpass_step(
872 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
873 &t1->data[(j * t1->data_stride) + i],
884 static void opj_t1_dec_refpass_raw(
888 OPJ_INT32 one, poshalf, neghalf;
890 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
893 neghalf = bpno > 0 ? -poshalf : -1;
894 for (k = 0; k < t1->h; k += 4) {
895 for (i = 0; i < t1->w; ++i) {
896 opj_colflag_t *colflags2 = colflags1 + i;
897 for (j = k; j < k + 4 && j < t1->h; ++j) {
898 opj_t1_dec_refpass_step_raw(
901 &t1->data[(j * t1->w) + i],
906 colflags1 += t1->flags_stride;
908 } /* VSC and BYPASS by Antonin */
910 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
912 OPJ_INT32 one, poshalf, neghalf; \
913 OPJ_UINT32 i, j, k; \
914 OPJ_INT32 *data1 = t1->data; \
915 opj_flag_t *flags1 = &t1->flags[1]; \
916 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
918 poshalf = one >> 1; \
919 neghalf = bpno > 0 ? -poshalf : -1; \
920 for (k = 0; k < (h & ~3u); k += 4) { \
921 for (i = 0; i < w; ++i) { \
922 OPJ_INT32 *data2 = data1 + i; \
923 opj_flag_t *flags2 = flags1 + i; \
924 opj_colflag_t *colflags2 = colflags1 + i; \
925 if( *colflags2 == 0 ) continue; \
926 flags2 += flags_stride; \
927 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
929 flags2 += flags_stride; \
930 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
932 flags2 += flags_stride; \
933 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
935 flags2 += flags_stride; \
936 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
940 flags1 += flags_stride << 2; \
941 colflags1 += flags_stride; \
943 for (i = 0; i < w; ++i) { \
944 OPJ_INT32 *data2 = data1 + i; \
945 opj_flag_t *flags2 = flags1 + i; \
946 opj_colflag_t *colflags2 = colflags1 + i; \
947 for (j = k; j < h; ++j) { \
948 flags2 += flags_stride; \
949 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
955 static void opj_t1_dec_refpass_mqc_64x64(
959 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
962 static void opj_t1_dec_refpass_mqc_generic(
966 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
969 /* VSC and BYPASS by Antonin */
970 static void opj_t1_dec_refpass_mqc_vsc(
974 OPJ_INT32 one, poshalf, neghalf;
977 OPJ_INT32 *data1 = t1->data;
978 opj_flag_t *flags1 = &t1->flags[1];
979 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
982 neghalf = bpno > 0 ? -poshalf : -1;
983 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
984 for (i = 0; i < t1->w; ++i) {
985 OPJ_INT32 *data2 = data1 + i;
986 opj_flag_t *flags2 = flags1 + i;
987 opj_colflag_t *colflags2 = colflags1 + i;
988 flags2 += t1->flags_stride;
989 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
992 flags2 += t1->flags_stride;
993 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
996 flags2 += t1->flags_stride;
997 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1000 flags2 += t1->flags_stride;
1001 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1005 data1 += t1->w << 2;
1006 flags1 += t1->flags_stride << 2;
1007 colflags1 += t1->flags_stride;
1009 for (i = 0; i < t1->w; ++i) {
1010 opj_colflag_t *colflags2 = colflags1 + i;
1011 for (j = k; j < t1->h; ++j) {
1012 vsc = (j == t1->h - 1) ? 1 : 0;
1013 opj_t1_dec_refpass_step_mqc_vsc(
1015 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1017 &t1->data[(j * t1->w) + i],
1023 } /* VSC and BYPASS by Antonin */
1026 static void opj_t1_enc_clnpass_step(
1039 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1041 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1042 T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
1046 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1047 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1048 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1049 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1052 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1053 (OPJ_UINT32)(bpno));
1054 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1055 v = *datap < 0 ? 1 : 0;
1056 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1057 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1060 *flagsp &= (opj_flag_t)~T1_VISIT;
1063 static void opj_t1_dec_clnpass_step_partial(
1066 opj_colflag_t *colflagsp,
1068 OPJ_INT32 oneplushalf,
1072 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1075 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1076 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1077 *datap = v ? -oneplushalf : oneplushalf;
1078 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1080 #ifdef CONSISTENCY_CHECK
1081 *flagsp &= ~T1_VISIT;
1083 } /* VSC and BYPASS by Antonin */
1085 static void opj_t1_dec_clnpass_step(
1088 opj_colflag_t *colflagsp,
1090 OPJ_INT32 oneplushalf,
1095 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1096 #ifdef CONSISTENCY_CHECK
1097 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1098 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1100 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1103 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1104 if (opj_mqc_decode(mqc)) {
1105 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1106 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1107 *datap = v ? -oneplushalf : oneplushalf;
1108 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1112 #ifdef CONSISTENCY_CHECK
1113 *flagsp &= ~T1_VISIT;
1115 } /* VSC and BYPASS by Antonin */
1117 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1120 opj_colflag_t *colflagsp,
1122 OPJ_INT32 oneplushalf,
1124 OPJ_UINT32 flags_stride)
1129 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1132 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1134 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1135 if (opj_mqc_decode(mqc)) {
1136 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1137 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1138 *datap = v ? -oneplushalf : oneplushalf;
1139 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1142 /*flagsp &= ~T1_VISIT;*/
1145 static void opj_t1_dec_clnpass_step_vsc(
1148 opj_colflag_t *colflagsp,
1150 OPJ_INT32 oneplushalf,
1157 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1159 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1164 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1165 (T1_COLFLAG_RBS * row)))) {
1166 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1167 if (opj_mqc_decode(mqc)) {
1169 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1170 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1171 *datap = v ? -oneplushalf : oneplushalf;
1172 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1175 #ifdef CONSISTENCY_CHECK
1176 *flagsp &= ~T1_VISIT;
1180 static void opj_t1_enc_clnpass(
1188 OPJ_UINT32 agg, runlen, vsc;
1190 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1193 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1194 for (k = 0; k < t1->h; k += 4) {
1195 for (i = 0; i < t1->w; ++i) {
1196 if (k + 3 < t1->h) {
1197 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1198 agg = !(MACRO_t1_flags(1 + k, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1199 || MACRO_t1_flags(1 + k + 1, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1200 || MACRO_t1_flags(1 + k + 2, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1201 || (MACRO_t1_flags(1 + k + 3, 1 + i)
1202 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT |
1205 agg = !((MACRO_t1_flags(1 + k, 1 + i) |
1206 MACRO_t1_flags(1 + k + 1, 1 + i) |
1207 MACRO_t1_flags(1 + k + 2, 1 + i) |
1208 MACRO_t1_flags(1 + k + 3, 1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1214 for (runlen = 0; runlen < 4; ++runlen) {
1215 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1219 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1220 opj_mqc_encode(mqc, runlen != 4);
1224 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1225 opj_mqc_encode(mqc, runlen >> 1);
1226 opj_mqc_encode(mqc, runlen & 1);
1230 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1231 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
1232 j == t1->h - 1)) ? 1 : 0;
1233 opj_t1_enc_clnpass_step(
1235 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1236 &t1->data[(j * t1->data_stride) + i],
1240 agg && (j == k + runlen),
1247 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1249 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1251 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1252 OPJ_UINT32 i, j, k; \
1253 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1255 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1259 oneplushalf = one | half; \
1260 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1261 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1262 for (k = 0; k < h; k += 4) { \
1263 for (i = 0; i < w; ++i) { \
1264 opj_colflag_t *colflags2 = colflags1 + i; \
1266 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1267 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1268 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1269 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1270 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1271 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1276 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1277 if (!opj_mqc_decode(mqc)) { \
1280 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1281 runlen = opj_mqc_decode(mqc); \
1282 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1286 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1287 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1288 opj_t1_dec_clnpass_step_vsc( \
1290 &t1->flags[((j+1) * flags_stride) + i + 1], \
1292 &t1->data[(j * w) + i], \
1294 agg && (j == k + (OPJ_UINT32)runlen), \
1297 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1299 colflags1 += flags_stride; \
1302 OPJ_INT32 *data1 = t1->data; \
1303 opj_flag_t *flags1 = &t1->flags[1]; \
1304 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1305 for (k = 0; k < (h & ~3u); k += 4) { \
1306 for (i = 0; i < w; ++i) { \
1307 OPJ_INT32 *data2 = data1 + i; \
1308 opj_flag_t *flags2 = flags1 + i; \
1309 opj_colflag_t *colflags2 = colflags1 + i; \
1310 opj_colflag_t colflags = *colflags2; \
1311 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1312 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1313 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1314 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1315 if( consistency_check ) { \
1316 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1317 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1318 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1319 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1322 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1323 if (!opj_mqc_decode(mqc)) { \
1326 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1327 runlen = opj_mqc_decode(mqc); \
1328 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1329 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1330 data2 += (OPJ_UINT32)runlen * w; \
1331 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1332 flags2 += flags_stride; \
1333 if (j == (OPJ_UINT32)runlen) { \
1334 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1336 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1341 flags2 += flags_stride; \
1342 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1343 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1344 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1346 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1348 flags2 += flags_stride; \
1349 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1350 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1351 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1353 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1355 flags2 += flags_stride; \
1356 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1357 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1358 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1360 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1362 flags2 += flags_stride; \
1363 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1364 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1365 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1367 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1370 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1373 flags1 += flags_stride << 2; \
1374 colflags1 += flags_stride; \
1376 for (i = 0; i < w; ++i) { \
1377 OPJ_INT32 *data2 = data1 + i; \
1378 opj_flag_t *flags2 = flags1 + i; \
1379 opj_colflag_t *colflags2 = colflags1 + i; \
1380 for (j = k; j < h; ++j) { \
1381 flags2 += flags_stride; \
1382 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1385 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1391 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1392 v = opj_mqc_decode(mqc); \
1393 v = (v << 1) | opj_mqc_decode(mqc); \
1394 v = (v << 1) | opj_mqc_decode(mqc); \
1395 v = (v << 1) | opj_mqc_decode(mqc); \
1398 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1402 } /* VSC and BYPASS by Antonin */
1404 static void opj_t1_dec_clnpass_64x64(
1409 #ifdef CONSISTENCY_CHECK
1410 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1412 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1416 static void opj_t1_dec_clnpass_generic(
1421 #ifdef CONSISTENCY_CHECK
1422 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1425 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1431 /** mod fixed_quality */
1432 static OPJ_FLOAT64 opj_t1_getwmsedec(
1439 OPJ_FLOAT64 stepsize,
1440 OPJ_UINT32 numcomps,
1441 const OPJ_FLOAT64 * mct_norms,
1442 OPJ_UINT32 mct_numcomps)
1444 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1445 OPJ_ARG_NOT_USED(numcomps);
1447 if (mct_norms && (compno < mct_numcomps)) {
1448 w1 = mct_norms[compno];
1452 w2 = opj_dwt_getnorm(level, orient);
1453 } else { /* if (qmfbid == 0) */
1454 w2 = opj_dwt_getnorm_real(level, orient);
1457 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1458 wmsedec *= wmsedec * nmsedec / 8192.0;
1463 static OPJ_BOOL opj_t1_allocate_buffers(
1468 /* encoder uses tile buffer, so no need to allocate */
1472 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1473 /* Overflow check */
1474 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1475 /* FIXME event manager error callback */
1479 datasize = (size_t)w * h;
1481 /* Overflow check */
1482 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1483 /* FIXME event manager error callback */
1487 if (datasize > (size_t)t1->datasize) {
1488 opj_aligned_free(t1->data);
1489 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1491 /* FIXME event manager error callback */
1494 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1495 /* TODO remove this if t1->datasize type changes to size_t */
1496 /* Overflow check */
1497 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1498 /* FIXME event manager error callback */
1502 t1->datasize = (OPJ_UINT32)datasize;
1504 /* memset first arg is declared to never be null by gcc */
1505 if (t1->data != NULL) {
1506 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1513 /* Overflow check */
1514 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1515 /* FIXME event manager error callback */
1518 t1->flags_stride = w + 2U; /* can't be 0U */
1520 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1521 /* Overflow check */
1522 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1523 /* FIXME event manager error callback */
1527 flagssize = (size_t)h + 3U;
1529 /* Overflow check */
1530 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1531 /* FIXME event manager error callback */
1534 flagssize *= (size_t)t1->flags_stride;
1536 if (flagssize > (size_t)t1->flagssize) {
1537 /* Overflow check */
1538 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1539 /* FIXME event manager error callback */
1542 opj_aligned_free(t1->flags);
1543 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1545 /* FIXME event manager error callback */
1548 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1549 /* TODO remove this if t1->flagssize type changes to size_t */
1550 /* Overflow check */
1551 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1552 /* FIXME event manager error callback */
1556 t1->flagssize = (OPJ_UINT32)flagssize;
1558 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1561 size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1562 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1564 /* Overflow check */
1565 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1566 /* FIXME event manager error callback */
1569 colflags_size *= (size_t)t1->flags_stride;
1571 if (colflags_size > (size_t)t1->colflags_size) {
1572 /* Overflow check */
1573 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1574 /* FIXME event manager error callback */
1577 opj_aligned_free(t1->colflags);
1578 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1580 if (!t1->colflags) {
1581 /* FIXME event manager error callback */
1584 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1585 /* TODO remove this if t1->colflags_size type changes to size_t */
1586 /* Overflow check */
1587 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1588 /* FIXME event manager error callback */
1592 t1->colflags_size = (OPJ_UINT32)colflags_size;
1594 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1603 /* ----------------------------------------------------------------------- */
1605 /* ----------------------------------------------------------------------- */
1607 * Creates a new Tier 1 handle
1608 * and initializes the look-up tables of the Tier-1 coder/decoder
1609 * @return a new T1 handle if successful, returns NULL otherwise
1611 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1613 opj_t1_t *l_t1 = 00;
1615 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1620 /* create MQC and RAW handles */
1621 l_t1->mqc = opj_mqc_create();
1623 opj_t1_destroy(l_t1);
1627 l_t1->raw = opj_raw_create();
1629 opj_t1_destroy(l_t1);
1632 l_t1->encoder = isEncoder;
1639 * Destroys a previously created T1 handle
1641 * @param p_t1 Tier 1 handle to destroy
1643 void opj_t1_destroy(opj_t1_t *p_t1)
1649 /* destroy MQC and RAW handles */
1650 opj_mqc_destroy(p_t1->mqc);
1652 opj_raw_destroy(p_t1->raw);
1655 /* encoder uses tile buffer, so no need to free */
1656 if (!p_t1->encoder && p_t1->data) {
1657 opj_aligned_free(p_t1->data);
1662 opj_aligned_free(p_t1->flags);
1666 if (p_t1->colflags) {
1667 opj_aligned_free(p_t1->colflags);
1668 p_t1->colflags = 00;
1675 opj_tcd_cblk_dec_t* cblk;
1676 opj_tcd_band_t* band;
1677 opj_tcd_tilecomp_t* tilec;
1679 volatile OPJ_BOOL* pret;
1680 } opj_t1_cblk_decode_processing_job_t;
1682 static void opj_t1_destroy_wrapper(void* t1)
1684 opj_t1_destroy((opj_t1_t*) t1);
1687 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1689 opj_tcd_cblk_dec_t* cblk;
1690 opj_tcd_band_t* band;
1691 opj_tcd_tilecomp_t* tilec;
1693 OPJ_INT32* OPJ_RESTRICT datap;
1694 OPJ_UINT32 cblk_w, cblk_h;
1697 opj_t1_cblk_decode_processing_job_t* job;
1702 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1708 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1710 if (!*(job->pret)) {
1715 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1717 t1 = opj_t1_create(OPJ_FALSE);
1718 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1721 if (OPJ_FALSE == opj_t1_decode_cblk(
1725 (OPJ_UINT32)tccp->roishift,
1727 *(job->pret) = OPJ_FALSE;
1732 x = cblk->x0 - band->x0;
1733 y = cblk->y0 - band->y0;
1734 if (band->bandno & 1) {
1735 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1736 x += pres->x1 - pres->x0;
1738 if (band->bandno & 2) {
1739 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1740 y += pres->y1 - pres->y0;
1747 if (tccp->roishift) {
1748 OPJ_INT32 thresh = 1 << tccp->roishift;
1749 for (j = 0; j < cblk_h; ++j) {
1750 for (i = 0; i < cblk_w; ++i) {
1751 OPJ_INT32 val = datap[(j * cblk_w) + i];
1752 OPJ_INT32 mag = abs(val);
1753 if (mag >= thresh) {
1754 mag >>= tccp->roishift;
1755 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1760 if (tccp->qmfbid == 1) {
1761 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1763 for (j = 0; j < cblk_h; ++j) {
1765 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1766 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1767 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1768 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1769 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1770 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1771 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1772 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1773 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1775 for (; i < cblk_w; ++i) {
1776 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1777 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1780 } else { /* if (tccp->qmfbid == 0) */
1781 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1782 tile_w + (OPJ_UINT32)x];
1783 for (j = 0; j < cblk_h; ++j) {
1784 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1785 for (i = 0; i < cblk_w; ++i) {
1786 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1799 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1800 volatile OPJ_BOOL* pret,
1801 opj_tcd_tilecomp_t* tilec,
1805 OPJ_UINT32 resno, bandno, precno, cblkno;
1807 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1808 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1810 for (bandno = 0; bandno < res->numbands; ++bandno) {
1811 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1813 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1814 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1816 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1817 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1818 opj_t1_cblk_decode_processing_job_t* job;
1820 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1821 sizeof(opj_t1_cblk_decode_processing_job_t));
1832 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1845 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1846 opj_tcd_cblk_dec_t* cblk,
1848 OPJ_UINT32 roishift,
1851 opj_raw_t *raw = t1->raw; /* RAW component */
1852 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1854 OPJ_INT32 bpno_plus_one;
1855 OPJ_UINT32 passtype;
1856 OPJ_UINT32 segno, passno;
1857 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1859 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1861 if (!opj_t1_allocate_buffers(
1863 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1864 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1868 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1871 opj_mqc_resetstates(mqc);
1872 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1873 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1874 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1876 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1877 opj_tcd_seg_t *seg = &cblk->segs[segno];
1880 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1881 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1882 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1883 if (seg->data == 00) {
1886 if (type == T1_TYPE_RAW) {
1887 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1889 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
1895 if (t1->w == 64 && t1->h == 64) {
1896 for (passno = 0; (passno < seg->real_num_passes) &&
1897 (bpno_plus_one >= 1); ++passno) {
1900 if (type == T1_TYPE_RAW) {
1901 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1903 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1904 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1906 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1911 if (type == T1_TYPE_RAW) {
1912 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1914 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1915 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1917 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1922 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1926 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1927 opj_mqc_resetstates(mqc);
1928 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1929 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1930 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1932 if (++passtype == 3) {
1938 for (passno = 0; (passno < seg->real_num_passes) &&
1939 (bpno_plus_one >= 1); ++passno) {
1942 if (type == T1_TYPE_RAW) {
1943 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1945 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1946 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1948 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1953 if (type == T1_TYPE_RAW) {
1954 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1956 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1957 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1959 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1964 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1968 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1969 opj_mqc_resetstates(mqc);
1970 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1971 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1972 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1974 if (++passtype == 3) {
1987 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1988 opj_tcd_tile_t *tile,
1990 const OPJ_FLOAT64 * mct_norms,
1991 OPJ_UINT32 mct_numcomps
1994 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1996 tile->distotile = 0; /* fixed_quality */
1998 for (compno = 0; compno < tile->numcomps; ++compno) {
1999 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2000 opj_tccp_t* tccp = &tcp->tccps[compno];
2001 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2003 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2004 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2006 for (bandno = 0; bandno < res->numbands; ++bandno) {
2007 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2008 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2010 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2011 opj_tcd_precinct_t *prc = &band->precincts[precno];
2013 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2014 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2015 OPJ_INT32* OPJ_RESTRICT tiledp;
2018 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2020 OPJ_INT32 x = cblk->x0 - band->x0;
2021 OPJ_INT32 y = cblk->y0 - band->y0;
2022 if (band->bandno & 1) {
2023 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2024 x += pres->x1 - pres->x0;
2026 if (band->bandno & 2) {
2027 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2028 y += pres->y1 - pres->y0;
2031 if (!opj_t1_allocate_buffers(
2033 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2034 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2040 tileLineAdvance = tile_w - cblk_w;
2042 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2044 t1->data_stride = tile_w;
2045 if (tccp->qmfbid == 1) {
2046 for (j = 0; j < cblk_h; ++j) {
2047 for (i = 0; i < cblk_w; ++i) {
2048 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2051 tileIndex += tileLineAdvance;
2053 } else { /* if (tccp->qmfbid == 0) */
2054 for (j = 0; j < cblk_h; ++j) {
2055 for (i = 0; i < cblk_w; ++i) {
2056 OPJ_INT32 tmp = tiledp[tileIndex];
2063 tileIndex += tileLineAdvance;
2072 tilec->numresolutions - 1 - resno,
2089 /** mod fixed_quality */
2090 static void opj_t1_encode_cblk(opj_t1_t *t1,
2091 opj_tcd_cblk_enc_t* cblk,
2096 OPJ_FLOAT64 stepsize,
2098 OPJ_UINT32 numcomps,
2099 opj_tcd_tile_t * tile,
2100 const OPJ_FLOAT64 * mct_norms,
2101 OPJ_UINT32 mct_numcomps)
2103 OPJ_FLOAT64 cumwmsedec = 0.0;
2105 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2109 OPJ_UINT32 passtype;
2110 OPJ_INT32 nmsedec = 0;
2113 OPJ_BYTE type = T1_TYPE_MQ;
2114 OPJ_FLOAT64 tempwmsedec;
2116 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2119 for (i = 0; i < t1->w; ++i) {
2120 for (j = 0; j < t1->h; ++j) {
2121 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2122 max = opj_int_max(max, tmp);
2126 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2127 T1_NMSEDEC_FRACBITS) : 0;
2129 bpno = (OPJ_INT32)(cblk->numbps - 1);
2132 opj_mqc_resetstates(mqc);
2133 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2134 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2135 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2136 opj_mqc_init_enc(mqc, cblk->data);
2138 for (passno = 0; bpno >= 0; ++passno) {
2139 opj_tcd_pass_t *pass = &cblk->passes[passno];
2140 OPJ_UINT32 correction = 3;
2141 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2142 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2146 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2149 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2152 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2153 /* code switch SEGMARK (i.e. SEGSYM) */
2154 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2155 opj_mqc_segmark_enc(mqc);
2161 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2162 stepsize, numcomps, mct_norms, mct_numcomps) ;
2163 cumwmsedec += tempwmsedec;
2164 tile->distotile += tempwmsedec;
2166 /* Code switch "RESTART" (i.e. TERMALL) */
2167 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2169 if (type == T1_TYPE_RAW) {
2172 /* correction = mqc_bypass_flush_enc(); */
2173 } else { /* correction = mqc_restart_enc(); */
2179 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2180 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2181 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2182 if (type == T1_TYPE_RAW) {
2185 /* correction = mqc_bypass_flush_enc(); */
2186 } else { /* correction = mqc_restart_enc(); */
2196 if (++passtype == 3) {
2201 if (pass->term && bpno > 0) {
2202 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2203 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2204 if (type == T1_TYPE_RAW) {
2205 opj_mqc_bypass_init_enc(mqc);
2207 opj_mqc_restart_init_enc(mqc);
2211 pass->distortiondec = cumwmsedec;
2212 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2214 /* Code-switch "RESET" */
2215 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2216 opj_mqc_reset_enc(mqc);
2220 /* Code switch "ERTERM" (i.e. PTERM) */
2221 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2222 opj_mqc_erterm_enc(mqc);
2223 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2227 cblk->totalpasses = passno;
2229 for (passno = 0; passno < cblk->totalpasses; passno++) {
2230 opj_tcd_pass_t *pass = &cblk->passes[passno];
2231 if (pass->rate > opj_mqc_numbytes(mqc)) {
2232 pass->rate = opj_mqc_numbytes(mqc);
2234 /*Preventing generation of FF as last data byte of a pass*/
2235 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2238 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2243 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
2254 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2255 opj_raw_t *raw = t1->raw; /* RAW component */
2257 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2259 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2260 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2261 if (type == T1_TYPE_RAW) {
2262 v = opj_raw_decode(raw);
2264 v = opj_mqc_decode(mqc);
2266 t = v ? poshalf : neghalf;
2267 *datap += *datap < 0 ? -t : t;
2268 *flagsp |= T1_REFINE;
2270 } /* VSC and BYPASS by Antonin */
2276 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
2280 OPJ_INT32 oneplushalf,
2286 opj_raw_t *raw = t1->raw; /* RAW component */
2287 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2289 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2291 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2292 if (type == T1_TYPE_RAW) {
2293 if (opj_raw_decode(raw)) {
2294 v = opj_raw_decode(raw); /* ESSAI */
2295 *datap = v ? -oneplushalf : oneplushalf;
2296 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2299 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2300 if (opj_mqc_decode(mqc)) {
2301 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2302 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2303 *datap = v ? -oneplushalf : oneplushalf;
2304 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2307 *flagsp |= T1_VISIT;
2309 } /* VSC and BYPASS by Antonin */