t1.c revision ee451cb395940862dad63c85adfe8f2fd55e864c
1/*
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.
6 *
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.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
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.
25 *
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.
37 */
38
39#include "opj_includes.h"
40#include "t1_luts.h"
41
42/** @defgroup T1 T1 - Implementation of the tier-1 coding */
43/*@{*/
44
45/** @name Local static functions */
46/*@{*/
47
48static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
49static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
50static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
51static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
52static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
53static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
54static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
55/**
56Encode significant pass
57*/
58static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
59                                    opj_flag_t *flagsp,
60                                    OPJ_INT32 *datap,
61                                    OPJ_UINT32 orient,
62                                    OPJ_INT32 bpno,
63                                    OPJ_INT32 one,
64                                    OPJ_INT32 *nmsedec,
65                                    OPJ_BYTE type,
66                                    OPJ_UINT32 vsc);
67
68/**
69Decode significant pass
70*/
71#if 0
72static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
73                                    opj_flag_t *flagsp,
74                                    OPJ_INT32 *datap,
75                                    OPJ_UINT32 orient,
76                                    OPJ_INT32 oneplushalf,
77                                    OPJ_BYTE type,
78                                    OPJ_UINT32 vsc);
79#endif
80
81static INLINE void opj_t1_dec_sigpass_step_raw(
82                opj_t1_t *t1,
83                opj_flag_t *flagsp,
84                OPJ_INT32 *datap,
85                OPJ_INT32 orient,
86                OPJ_INT32 oneplushalf,
87                OPJ_INT32 vsc);
88static INLINE void opj_t1_dec_sigpass_step_mqc(
89                opj_t1_t *t1,
90                opj_flag_t *flagsp,
91                OPJ_INT32 *datap,
92                OPJ_INT32 orient,
93                OPJ_INT32 oneplushalf);
94static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
95                opj_t1_t *t1,
96                opj_flag_t *flagsp,
97                OPJ_INT32 *datap,
98                OPJ_INT32 orient,
99                OPJ_INT32 oneplushalf,
100                OPJ_INT32 vsc);
101
102
103/**
104Encode significant pass
105*/
106static void opj_t1_enc_sigpass( opj_t1_t *t1,
107                                OPJ_INT32 bpno,
108                                OPJ_UINT32 orient,
109                                OPJ_INT32 *nmsedec,
110                                OPJ_BYTE type,
111                                OPJ_UINT32 cblksty);
112
113/**
114Decode significant pass
115*/
116static void opj_t1_dec_sigpass_raw(
117                opj_t1_t *t1,
118                OPJ_INT32 bpno,
119                OPJ_INT32 orient,
120                OPJ_INT32 cblksty);
121static void opj_t1_dec_sigpass_mqc(
122                opj_t1_t *t1,
123                OPJ_INT32 bpno,
124                OPJ_INT32 orient);
125static void opj_t1_dec_sigpass_mqc_vsc(
126                opj_t1_t *t1,
127                OPJ_INT32 bpno,
128                OPJ_INT32 orient);
129
130
131
132/**
133Encode refinement pass
134*/
135static void opj_t1_enc_refpass_step(opj_t1_t *t1,
136                                    opj_flag_t *flagsp,
137                                    OPJ_INT32 *datap,
138                                    OPJ_INT32 bpno,
139                                    OPJ_INT32 one,
140                                    OPJ_INT32 *nmsedec,
141                                    OPJ_BYTE type,
142                                    OPJ_UINT32 vsc);
143
144
145/**
146Encode refinement pass
147*/
148static void opj_t1_enc_refpass( opj_t1_t *t1,
149                                OPJ_INT32 bpno,
150                                OPJ_INT32 *nmsedec,
151                                OPJ_BYTE type,
152                                OPJ_UINT32 cblksty);
153
154/**
155Decode refinement pass
156*/
157static void opj_t1_dec_refpass_raw(
158                opj_t1_t *t1,
159                OPJ_INT32 bpno,
160                OPJ_INT32 cblksty);
161static void opj_t1_dec_refpass_mqc(
162                opj_t1_t *t1,
163                OPJ_INT32 bpno);
164static void opj_t1_dec_refpass_mqc_vsc(
165                opj_t1_t *t1,
166                OPJ_INT32 bpno);
167
168
169/**
170Decode refinement pass
171*/
172#if 0
173static void opj_t1_dec_refpass_step(opj_t1_t *t1,
174                                    opj_flag_t *flagsp,
175                                    OPJ_INT32 *datap,
176                                    OPJ_INT32 poshalf,
177                                    OPJ_INT32 neghalf,
178                                    OPJ_BYTE type,
179                                    OPJ_UINT32 vsc);
180#endif
181
182static INLINE void  opj_t1_dec_refpass_step_raw(
183                opj_t1_t *t1,
184                opj_flag_t *flagsp,
185                OPJ_INT32 *datap,
186                OPJ_INT32 poshalf,
187                OPJ_INT32 neghalf,
188                OPJ_INT32 vsc);
189static INLINE void opj_t1_dec_refpass_step_mqc(
190                opj_t1_t *t1,
191                opj_flag_t *flagsp,
192                OPJ_INT32 *datap,
193                OPJ_INT32 poshalf,
194                OPJ_INT32 neghalf);
195static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
196                opj_t1_t *t1,
197                opj_flag_t *flagsp,
198                OPJ_INT32 *datap,
199                OPJ_INT32 poshalf,
200                OPJ_INT32 neghalf,
201                OPJ_INT32 vsc);
202
203
204
205/**
206Encode clean-up pass
207*/
208static void opj_t1_enc_clnpass_step(
209		opj_t1_t *t1,
210		opj_flag_t *flagsp,
211		OPJ_INT32 *datap,
212		OPJ_UINT32 orient,
213		OPJ_INT32 bpno,
214		OPJ_INT32 one,
215		OPJ_INT32 *nmsedec,
216		OPJ_UINT32 partial,
217		OPJ_UINT32 vsc);
218/**
219Decode clean-up pass
220*/
221static void opj_t1_dec_clnpass_step_partial(
222		opj_t1_t *t1,
223		opj_flag_t *flagsp,
224		OPJ_INT32 *datap,
225		OPJ_INT32 orient,
226		OPJ_INT32 oneplushalf);
227static void opj_t1_dec_clnpass_step(
228		opj_t1_t *t1,
229		opj_flag_t *flagsp,
230		OPJ_INT32 *datap,
231		OPJ_INT32 orient,
232		OPJ_INT32 oneplushalf);
233static void opj_t1_dec_clnpass_step_vsc(
234		opj_t1_t *t1,
235		opj_flag_t *flagsp,
236		OPJ_INT32 *datap,
237		OPJ_INT32 orient,
238		OPJ_INT32 oneplushalf,
239		OPJ_INT32 partial,
240		OPJ_INT32 vsc);
241/**
242Encode clean-up pass
243*/
244static void opj_t1_enc_clnpass(
245		opj_t1_t *t1,
246		OPJ_INT32 bpno,
247		OPJ_UINT32 orient,
248		OPJ_INT32 *nmsedec,
249		OPJ_UINT32 cblksty);
250/**
251Decode clean-up pass
252*/
253static void opj_t1_dec_clnpass(
254		opj_t1_t *t1,
255		OPJ_INT32 bpno,
256		OPJ_INT32 orient,
257		OPJ_INT32 cblksty);
258
259static OPJ_FLOAT64 opj_t1_getwmsedec(
260		OPJ_INT32 nmsedec,
261		OPJ_UINT32 compno,
262		OPJ_UINT32 level,
263		OPJ_UINT32 orient,
264		OPJ_INT32 bpno,
265		OPJ_UINT32 qmfbid,
266		OPJ_FLOAT64 stepsize,
267		OPJ_UINT32 numcomps,
268		const OPJ_FLOAT64 * mct_norms);
269
270static void opj_t1_encode_cblk( opj_t1_t *t1,
271                                opj_tcd_cblk_enc_t* cblk,
272                                OPJ_UINT32 orient,
273                                OPJ_UINT32 compno,
274                                OPJ_UINT32 level,
275                                OPJ_UINT32 qmfbid,
276                                OPJ_FLOAT64 stepsize,
277                                OPJ_UINT32 cblksty,
278                                OPJ_UINT32 numcomps,
279                                opj_tcd_tile_t * tile,
280                                const OPJ_FLOAT64 * mct_norms);
281
282/**
283Decode 1 code-block
284@param t1 T1 handle
285@param cblk Code-block coding parameters
286@param orient
287@param roishift Region of interest shifting value
288@param cblksty Code-block style
289*/
290static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
291                                    opj_tcd_cblk_dec_t* cblk,
292                                    OPJ_UINT32 orient,
293                                    OPJ_UINT32 roishift,
294                                    OPJ_UINT32 cblksty);
295
296OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
297                                    OPJ_UINT32 w,
298                                    OPJ_UINT32 h);
299
300/*@}*/
301
302/*@}*/
303
304/* ----------------------------------------------------------------------- */
305
306OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
307	return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
308}
309
310OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
311	return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
312}
313
314OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
315	OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
316	OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
317	return (tmp2);
318}
319
320OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
321	return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
322}
323
324OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
325	if (bitpos > T1_NMSEDEC_FRACBITS) {
326		return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
327	}
328
329	return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
330}
331
332OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
333	if (bitpos > T1_NMSEDEC_FRACBITS) {
334		return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
335	}
336
337    return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
338}
339
340void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
341	opj_flag_t *np = flagsp - stride;
342	opj_flag_t *sp = flagsp + stride;
343
344	static const opj_flag_t mod[] = {
345		T1_SIG_S, T1_SIG_S|T1_SGN_S,
346		T1_SIG_E, T1_SIG_E|T1_SGN_E,
347		T1_SIG_W, T1_SIG_W|T1_SGN_W,
348		T1_SIG_N, T1_SIG_N|T1_SGN_N
349	};
350
351	np[-1] |= T1_SIG_SE;
352	np[0]  |= mod[s];
353	np[1]  |= T1_SIG_SW;
354
355	flagsp[-1] |= mod[s+2];
356	flagsp[0]  |= T1_SIG;
357	flagsp[1]  |= mod[s+4];
358
359	sp[-1] |= T1_SIG_NE;
360	sp[0]  |= mod[s+6];
361	sp[1]  |= T1_SIG_NW;
362}
363
364void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
365                                opj_flag_t *flagsp,
366                                OPJ_INT32 *datap,
367                                OPJ_UINT32 orient,
368                                OPJ_INT32 bpno,
369                                OPJ_INT32 one,
370                                OPJ_INT32 *nmsedec,
371                                OPJ_BYTE type,
372                                OPJ_UINT32 vsc
373                                )
374{
375	OPJ_INT32 v;
376    OPJ_UINT32 flag;
377
378	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
379
380	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
381	if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
382		v = opj_int_abs(*datap) & one ? 1 : 0;
383		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));	/* ESSAI */
384		if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
385			opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
386		} else {
387			opj_mqc_encode(mqc, (OPJ_UINT32)v);
388		}
389		if (v) {
390			v = *datap < 0 ? 1 : 0;
391			*nmsedec +=	opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
392			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));	/* ESSAI */
393			if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
394				opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
395			} else {
396				opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
397			}
398			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
399		}
400		*flagsp |= T1_VISIT;
401	}
402}
403
404
405static INLINE void opj_t1_dec_sigpass_step_raw(
406                opj_t1_t *t1,
407                opj_flag_t *flagsp,
408                OPJ_INT32 *datap,
409                OPJ_INT32 orient,
410                OPJ_INT32 oneplushalf,
411                OPJ_INT32 vsc)
412{
413        OPJ_INT32 v, flag;
414        opj_raw_t *raw = t1->raw;       /* RAW component */
415        OPJ_ARG_NOT_USED(orient);
416
417        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
418        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
419                        if (opj_raw_decode(raw)) {
420                                v = (OPJ_INT32)opj_raw_decode(raw);    /* ESSAI */
421                                *datap = v ? -oneplushalf : oneplushalf;
422                                opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
423                        }
424                *flagsp |= T1_VISIT;
425        }
426}
427
428INLINE void opj_t1_dec_sigpass_step_mqc(
429                opj_t1_t *t1,
430                opj_flag_t *flagsp,
431                OPJ_INT32 *datap,
432                OPJ_INT32 orient,
433                OPJ_INT32 oneplushalf)
434{
435        OPJ_INT32 v, flag;
436
437        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
438
439        flag = *flagsp;
440        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
441                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
442                        if (opj_mqc_decode(mqc)) {
443                                opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
444                                v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
445                                *datap = v ? -oneplushalf : oneplushalf;
446                                opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
447                        }
448                *flagsp |= T1_VISIT;
449        }
450}                               /* VSC and  BYPASS by Antonin */
451
452INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
453                opj_t1_t *t1,
454                opj_flag_t *flagsp,
455                OPJ_INT32 *datap,
456                OPJ_INT32 orient,
457                OPJ_INT32 oneplushalf,
458                OPJ_INT32 vsc)
459{
460        OPJ_INT32 v, flag;
461
462        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
463
464        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
465        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
466                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
467                if (opj_mqc_decode(mqc)) {
468                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
469                        v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
470                        *datap = v ? -oneplushalf : oneplushalf;
471                        opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
472                }
473                *flagsp |= T1_VISIT;
474        }
475}                               /* VSC and  BYPASS by Antonin */
476
477
478
479void opj_t1_enc_sigpass(opj_t1_t *t1,
480                        OPJ_INT32 bpno,
481                        OPJ_UINT32 orient,
482                        OPJ_INT32 *nmsedec,
483                        OPJ_BYTE type,
484                        OPJ_UINT32 cblksty
485                        )
486{
487	OPJ_UINT32 i, j, k, vsc;
488    OPJ_INT32 one;
489
490	*nmsedec = 0;
491	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
492	for (k = 0; k < t1->h; k += 4) {
493		for (i = 0; i < t1->w; ++i) {
494			for (j = k; j < k + 4 && j < t1->h; ++j) {
495				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
496				opj_t1_enc_sigpass_step(
497						t1,
498						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
499						&t1->data[(j * t1->w) + i],
500						orient,
501						bpno,
502						one,
503						nmsedec,
504						type,
505						vsc);
506			}
507		}
508	}
509}
510
511void opj_t1_dec_sigpass_raw(
512                opj_t1_t *t1,
513                OPJ_INT32 bpno,
514                OPJ_INT32 orient,
515                OPJ_INT32 cblksty)
516{
517        OPJ_INT32 one, half, oneplushalf, vsc;
518        OPJ_UINT32 i, j, k;
519        one = 1 << bpno;
520        half = one >> 1;
521        oneplushalf = one | half;
522        for (k = 0; k < t1->h; k += 4) {
523                for (i = 0; i < t1->w; ++i) {
524                        for (j = k; j < k + 4 && j < t1->h; ++j) {
525                                vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
526                                opj_t1_dec_sigpass_step_raw(
527                                                t1,
528                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
529                                                &t1->data[(j * t1->w) + i],
530                                                orient,
531                                                oneplushalf,
532                                                vsc);
533                        }
534                }
535        }
536}                               /* VSC and  BYPASS by Antonin */
537
538void opj_t1_dec_sigpass_mqc(
539                opj_t1_t *t1,
540                OPJ_INT32 bpno,
541                OPJ_INT32 orient)
542{
543        OPJ_INT32 one, half, oneplushalf;
544        OPJ_UINT32 i, j, k;
545        OPJ_INT32 *data1 = t1->data;
546        opj_flag_t *flags1 = &t1->flags[1];
547        one = 1 << bpno;
548        half = one >> 1;
549        oneplushalf = one | half;
550        for (k = 0; k < (t1->h & ~3u); k += 4) {
551                for (i = 0; i < t1->w; ++i) {
552                        OPJ_INT32 *data2 = data1 + i;
553                        opj_flag_t *flags2 = flags1 + i;
554                        flags2 += t1->flags_stride;
555                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
556                        data2 += t1->w;
557                        flags2 += t1->flags_stride;
558                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
559                        data2 += t1->w;
560                        flags2 += t1->flags_stride;
561                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
562                        data2 += t1->w;
563                        flags2 += t1->flags_stride;
564                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
565                        data2 += t1->w;
566                }
567                data1 += t1->w << 2;
568                flags1 += t1->flags_stride << 2;
569        }
570        for (i = 0; i < t1->w; ++i) {
571                OPJ_INT32 *data2 = data1 + i;
572                opj_flag_t *flags2 = flags1 + i;
573                for (j = k; j < t1->h; ++j) {
574                        flags2 += t1->flags_stride;
575                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
576                        data2 += t1->w;
577                }
578        }
579}                               /* VSC and  BYPASS by Antonin */
580
581void opj_t1_dec_sigpass_mqc_vsc(
582                opj_t1_t *t1,
583                OPJ_INT32 bpno,
584                OPJ_INT32 orient)
585{
586        OPJ_INT32 one, half, oneplushalf, vsc;
587        OPJ_UINT32 i, j, k;
588        one = 1 << bpno;
589        half = one >> 1;
590        oneplushalf = one | half;
591        for (k = 0; k < t1->h; k += 4) {
592                for (i = 0; i < t1->w; ++i) {
593                        for (j = k; j < k + 4 && j < t1->h; ++j) {
594                                vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
595                                opj_t1_dec_sigpass_step_mqc_vsc(
596                                                t1,
597                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
598                                                &t1->data[(j * t1->w) + i],
599                                                orient,
600                                                oneplushalf,
601                                                vsc);
602                        }
603                }
604        }
605}                               /* VSC and  BYPASS by Antonin */
606
607
608
609void opj_t1_enc_refpass_step(   opj_t1_t *t1,
610                                opj_flag_t *flagsp,
611                                OPJ_INT32 *datap,
612                                OPJ_INT32 bpno,
613                                OPJ_INT32 one,
614                                OPJ_INT32 *nmsedec,
615                                OPJ_BYTE type,
616                                OPJ_UINT32 vsc)
617{
618	OPJ_INT32 v;
619	OPJ_UINT32 flag;
620
621	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
622
623	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
624	if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
625		*nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
626		v = opj_int_abs(*datap) & one ? 1 : 0;
627		opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));	/* ESSAI */
628		if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
629			opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
630		} else {
631			opj_mqc_encode(mqc, (OPJ_UINT32)v);
632		}
633		*flagsp |= T1_REFINE;
634	}
635}
636
637INLINE void opj_t1_dec_refpass_step_raw(
638                opj_t1_t *t1,
639                opj_flag_t *flagsp,
640                OPJ_INT32 *datap,
641                OPJ_INT32 poshalf,
642                OPJ_INT32 neghalf,
643                OPJ_INT32 vsc)
644{
645        OPJ_INT32 v, t, flag;
646
647        opj_raw_t *raw = t1->raw;       /* RAW component */
648
649        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
650        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
651                        v = (OPJ_INT32)opj_raw_decode(raw);
652                t = v ? poshalf : neghalf;
653                *datap += *datap < 0 ? -t : t;
654                *flagsp |= T1_REFINE;
655        }
656}                               /* VSC and  BYPASS by Antonin  */
657
658INLINE void opj_t1_dec_refpass_step_mqc(
659                opj_t1_t *t1,
660                opj_flag_t *flagsp,
661                OPJ_INT32 *datap,
662                OPJ_INT32 poshalf,
663                OPJ_INT32 neghalf)
664{
665        OPJ_INT32 v, t, flag;
666
667        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
668
669        flag = *flagsp;
670        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
671                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
672                        v = opj_mqc_decode(mqc);
673                t = v ? poshalf : neghalf;
674                *datap += *datap < 0 ? -t : t;
675                *flagsp |= T1_REFINE;
676                }
677}                               /* VSC and  BYPASS by Antonin  */
678
679INLINE void opj_t1_dec_refpass_step_mqc_vsc(
680                opj_t1_t *t1,
681                opj_flag_t *flagsp,
682                OPJ_INT32 *datap,
683                OPJ_INT32 poshalf,
684                OPJ_INT32 neghalf,
685                OPJ_INT32 vsc)
686{
687        OPJ_INT32 v, t, flag;
688
689        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
690
691        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
692        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
693                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
694                v = opj_mqc_decode(mqc);
695                t = v ? poshalf : neghalf;
696                *datap += *datap < 0 ? -t : t;
697                *flagsp |= T1_REFINE;
698        }
699}                               /* VSC and  BYPASS by Antonin  */
700
701
702void opj_t1_enc_refpass(
703		opj_t1_t *t1,
704		OPJ_INT32 bpno,
705		OPJ_INT32 *nmsedec,
706		OPJ_BYTE type,
707		OPJ_UINT32 cblksty)
708{
709	OPJ_UINT32 i, j, k, vsc;
710    OPJ_INT32 one;
711
712	*nmsedec = 0;
713	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
714	for (k = 0; k < t1->h; k += 4) {
715		for (i = 0; i < t1->w; ++i) {
716			for (j = k; j < k + 4 && j < t1->h; ++j) {
717				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
718				opj_t1_enc_refpass_step(
719						t1,
720						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
721						&t1->data[(j * t1->w) + i],
722						bpno,
723						one,
724						nmsedec,
725						type,
726						vsc);
727			}
728		}
729	}
730}
731
732void opj_t1_dec_refpass_raw(
733                opj_t1_t *t1,
734                OPJ_INT32 bpno,
735                OPJ_INT32 cblksty)
736{
737        OPJ_INT32 one, poshalf, neghalf;
738        OPJ_UINT32 i, j, k;
739        OPJ_INT32 vsc;
740        one = 1 << bpno;
741        poshalf = one >> 1;
742        neghalf = bpno > 0 ? -poshalf : -1;
743        for (k = 0; k < t1->h; k += 4) {
744                for (i = 0; i < t1->w; ++i) {
745                        for (j = k; j < k + 4 && j < t1->h; ++j) {
746                                vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
747                                opj_t1_dec_refpass_step_raw(
748                                                t1,
749                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
750                                                &t1->data[(j * t1->w) + i],
751                                                poshalf,
752                                                neghalf,
753                                                vsc);
754                        }
755                }
756        }
757}                               /* VSC and  BYPASS by Antonin */
758
759void opj_t1_dec_refpass_mqc(
760                opj_t1_t *t1,
761                OPJ_INT32 bpno)
762{
763        OPJ_INT32 one, poshalf, neghalf;
764        OPJ_UINT32 i, j, k;
765        OPJ_INT32 *data1 = t1->data;
766        opj_flag_t *flags1 = &t1->flags[1];
767        one = 1 << bpno;
768        poshalf = one >> 1;
769        neghalf = bpno > 0 ? -poshalf : -1;
770        for (k = 0; k < (t1->h & ~3u); k += 4) {
771                for (i = 0; i < t1->w; ++i) {
772                        OPJ_INT32 *data2 = data1 + i;
773                        opj_flag_t *flags2 = flags1 + i;
774                        flags2 += t1->flags_stride;
775                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
776                        data2 += t1->w;
777                        flags2 += t1->flags_stride;
778                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
779                        data2 += t1->w;
780                        flags2 += t1->flags_stride;
781                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
782                        data2 += t1->w;
783                        flags2 += t1->flags_stride;
784                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
785                        data2 += t1->w;
786                }
787                data1 += t1->w << 2;
788                flags1 += t1->flags_stride << 2;
789        }
790        for (i = 0; i < t1->w; ++i) {
791                OPJ_INT32 *data2 = data1 + i;
792                opj_flag_t *flags2 = flags1 + i;
793                for (j = k; j < t1->h; ++j) {
794                        flags2 += t1->flags_stride;
795                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
796                        data2 += t1->w;
797                }
798        }
799}                               /* VSC and  BYPASS by Antonin */
800
801void opj_t1_dec_refpass_mqc_vsc(
802                opj_t1_t *t1,
803                OPJ_INT32 bpno)
804{
805        OPJ_INT32 one, poshalf, neghalf;
806        OPJ_UINT32 i, j, k;
807        OPJ_INT32 vsc;
808        one = 1 << bpno;
809        poshalf = one >> 1;
810        neghalf = bpno > 0 ? -poshalf : -1;
811        for (k = 0; k < t1->h; k += 4) {
812                for (i = 0; i < t1->w; ++i) {
813                        for (j = k; j < k + 4 && j < t1->h; ++j) {
814                                vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
815                                opj_t1_dec_refpass_step_mqc_vsc(
816                                                t1,
817                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
818                                                &t1->data[(j * t1->w) + i],
819                                                poshalf,
820                                                neghalf,
821                                                vsc);
822                        }
823                }
824        }
825}                               /* VSC and  BYPASS by Antonin */
826
827
828void opj_t1_enc_clnpass_step(
829		opj_t1_t *t1,
830		opj_flag_t *flagsp,
831		OPJ_INT32 *datap,
832		OPJ_UINT32 orient,
833		OPJ_INT32 bpno,
834		OPJ_INT32 one,
835		OPJ_INT32 *nmsedec,
836		OPJ_UINT32 partial,
837		OPJ_UINT32 vsc)
838{
839	OPJ_INT32 v;
840	OPJ_UINT32 flag;
841
842	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
843
844	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
845	if (partial) {
846		goto LABEL_PARTIAL;
847	}
848	if (!(*flagsp & (T1_SIG | T1_VISIT))) {
849		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
850		v = opj_int_abs(*datap) & one ? 1 : 0;
851		opj_mqc_encode(mqc, (OPJ_UINT32)v);
852		if (v) {
853LABEL_PARTIAL:
854			*nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
855			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
856			v = *datap < 0 ? 1 : 0;
857			opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
858			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
859		}
860	}
861	*flagsp &= ~T1_VISIT;
862}
863
864static void opj_t1_dec_clnpass_step_partial(
865		opj_t1_t *t1,
866		opj_flag_t *flagsp,
867		OPJ_INT32 *datap,
868		OPJ_INT32 orient,
869		OPJ_INT32 oneplushalf)
870{
871	OPJ_INT32 v, flag;
872	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
873
874	OPJ_ARG_NOT_USED(orient);
875
876	flag = *flagsp;
877	opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
878	v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
879	*datap = v ? -oneplushalf : oneplushalf;
880	opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
881	*flagsp &= ~T1_VISIT;
882}				/* VSC and  BYPASS by Antonin */
883
884static void opj_t1_dec_clnpass_step(
885		opj_t1_t *t1,
886		opj_flag_t *flagsp,
887		OPJ_INT32 *datap,
888		OPJ_INT32 orient,
889		OPJ_INT32 oneplushalf)
890{
891	OPJ_INT32 v, flag;
892
893	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
894
895	flag = *flagsp;
896	if (!(flag & (T1_SIG | T1_VISIT))) {
897		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
898		if (opj_mqc_decode(mqc)) {
899			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
900			v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
901			*datap = v ? -oneplushalf : oneplushalf;
902			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
903		}
904	}
905	*flagsp &= ~T1_VISIT;
906}				/* VSC and  BYPASS by Antonin */
907
908static void opj_t1_dec_clnpass_step_vsc(
909		opj_t1_t *t1,
910		opj_flag_t *flagsp,
911		OPJ_INT32 *datap,
912		OPJ_INT32 orient,
913		OPJ_INT32 oneplushalf,
914		OPJ_INT32 partial,
915		OPJ_INT32 vsc)
916{
917	OPJ_INT32 v, flag;
918
919	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
920
921	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
922	if (partial) {
923		goto LABEL_PARTIAL;
924	}
925	if (!(flag & (T1_SIG | T1_VISIT))) {
926		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
927		if (opj_mqc_decode(mqc)) {
928LABEL_PARTIAL:
929			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
930			v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
931			*datap = v ? -oneplushalf : oneplushalf;
932			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
933		}
934	}
935	*flagsp &= ~T1_VISIT;
936}
937
938void opj_t1_enc_clnpass(
939		opj_t1_t *t1,
940		OPJ_INT32 bpno,
941		OPJ_UINT32 orient,
942		OPJ_INT32 *nmsedec,
943		OPJ_UINT32 cblksty)
944{
945	OPJ_UINT32 i, j, k;
946	OPJ_INT32 one;
947	OPJ_UINT32 agg, runlen, vsc;
948
949	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
950
951	*nmsedec = 0;
952	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
953	for (k = 0; k < t1->h; k += 4) {
954		for (i = 0; i < t1->w; ++i) {
955			if (k + 3 < t1->h) {
956				if (cblksty & J2K_CCP_CBLKSTY_VSC) {
957					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
958						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
959						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960						|| (MACRO_t1_flags(1 + k + 3,1 + i)
961						& (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |	T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
962				} else {
963					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
964						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
965						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966						|| MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
967				}
968			} else {
969				agg = 0;
970			}
971			if (agg) {
972				for (runlen = 0; runlen < 4; ++runlen) {
973					if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
974						break;
975				}
976				opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
977				opj_mqc_encode(mqc, runlen != 4);
978				if (runlen == 4) {
979					continue;
980				}
981				opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
982				opj_mqc_encode(mqc, runlen >> 1);
983				opj_mqc_encode(mqc, runlen & 1);
984			} else {
985				runlen = 0;
986			}
987			for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
988				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
989				opj_t1_enc_clnpass_step(
990						t1,
991						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
992						&t1->data[(j * t1->w) + i],
993						orient,
994						bpno,
995						one,
996						nmsedec,
997						agg && (j == k + runlen),
998						vsc);
999			}
1000		}
1001	}
1002}
1003
1004static void opj_t1_dec_clnpass(
1005		opj_t1_t *t1,
1006		OPJ_INT32 bpno,
1007		OPJ_INT32 orient,
1008		OPJ_INT32 cblksty)
1009{
1010	OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
1011    OPJ_UINT32 i, j, k;
1012	OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1013
1014	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1015
1016	one = 1 << bpno;
1017	half = one >> 1;
1018	oneplushalf = one | half;
1019	if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1020	for (k = 0; k < t1->h; k += 4) {
1021		for (i = 0; i < t1->w; ++i) {
1022			if (k + 3 < t1->h) {
1023					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1024						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1025						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1026						|| (MACRO_t1_flags(1 + k + 3,1 + i)
1027						& (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |	T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1028				} else {
1029				agg = 0;
1030			}
1031			if (agg) {
1032				opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1033				if (!opj_mqc_decode(mqc)) {
1034					continue;
1035				}
1036				opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1037				runlen = opj_mqc_decode(mqc);
1038				runlen = (runlen << 1) | opj_mqc_decode(mqc);
1039			} else {
1040				runlen = 0;
1041			}
1042			for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1043					vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1044					opj_t1_dec_clnpass_step_vsc(
1045						t1,
1046						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
1047						&t1->data[(j * t1->w) + i],
1048						orient,
1049						oneplushalf,
1050						agg && (j == k + (OPJ_UINT32)runlen),
1051						vsc);
1052			}
1053		}
1054	}
1055	} else {
1056		OPJ_INT32 *data1 = t1->data;
1057		opj_flag_t *flags1 = &t1->flags[1];
1058		for (k = 0; k < (t1->h & ~3u); k += 4) {
1059			for (i = 0; i < t1->w; ++i) {
1060				OPJ_INT32 *data2 = data1 + i;
1061				opj_flag_t *flags2 = flags1 + i;
1062				agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1063					|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1064					|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1065					|| MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1066				if (agg) {
1067					opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1068					if (!opj_mqc_decode(mqc)) {
1069						continue;
1070					}
1071					opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1072					runlen = opj_mqc_decode(mqc);
1073					runlen = (runlen << 1) | opj_mqc_decode(mqc);
1074					flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
1075					data2 += (OPJ_UINT32)runlen * t1->w;
1076					for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1077						flags2 += t1->flags_stride;
1078						if (agg && (j == k + (OPJ_UINT32)runlen)) {
1079							opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1080						} else {
1081							opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1082						}
1083						data2 += t1->w;
1084					}
1085				} else {
1086					flags2 += t1->flags_stride;
1087					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1088					data2 += t1->w;
1089					flags2 += t1->flags_stride;
1090					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1091					data2 += t1->w;
1092					flags2 += t1->flags_stride;
1093					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1094					data2 += t1->w;
1095					flags2 += t1->flags_stride;
1096					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1097					data2 += t1->w;
1098				}
1099			}
1100			data1 += t1->w << 2;
1101			flags1 += t1->flags_stride << 2;
1102		}
1103		for (i = 0; i < t1->w; ++i) {
1104			OPJ_INT32 *data2 = data1 + i;
1105			opj_flag_t *flags2 = flags1 + i;
1106			for (j = k; j < t1->h; ++j) {
1107				flags2 += t1->flags_stride;
1108				opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1109				data2 += t1->w;
1110			}
1111		}
1112	}
1113
1114	if (segsym) {
1115		OPJ_INT32 v = 0;
1116		opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1117		v = opj_mqc_decode(mqc);
1118		v = (v << 1) | opj_mqc_decode(mqc);
1119		v = (v << 1) | opj_mqc_decode(mqc);
1120		v = (v << 1) | opj_mqc_decode(mqc);
1121		/*
1122		if (v!=0xa) {
1123			opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1124		}
1125		*/
1126	}
1127}				/* VSC and  BYPASS by Antonin */
1128
1129
1130/** mod fixed_quality */
1131static OPJ_FLOAT64 opj_t1_getwmsedec(
1132		OPJ_INT32 nmsedec,
1133		OPJ_UINT32 compno,
1134		OPJ_UINT32 level,
1135		OPJ_UINT32 orient,
1136		OPJ_INT32 bpno,
1137		OPJ_UINT32 qmfbid,
1138		OPJ_FLOAT64 stepsize,
1139		OPJ_UINT32 numcomps,
1140		const OPJ_FLOAT64 * mct_norms)
1141{
1142	OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1143    OPJ_ARG_NOT_USED(numcomps);
1144
1145	if (mct_norms) {
1146		w1 = mct_norms[compno];
1147	}
1148
1149	if (qmfbid == 1) {
1150		w2 = opj_dwt_getnorm(level, orient);
1151	} else {	/* if (qmfbid == 0) */
1152		w2 = opj_dwt_getnorm_real(level, orient);
1153	}
1154
1155	wmsedec = w1 * w2 * stepsize * (1 << bpno);
1156	wmsedec *= wmsedec * nmsedec / 8192.0;
1157
1158	return wmsedec;
1159}
1160
1161OPJ_BOOL opj_t1_allocate_buffers(
1162		opj_t1_t *t1,
1163		OPJ_UINT32 w,
1164		OPJ_UINT32 h)
1165{
1166	OPJ_UINT32 datasize=w * h;
1167	OPJ_UINT32 flagssize;
1168
1169	if(datasize > t1->datasize){
1170		opj_aligned_free(t1->data);
1171		t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1172		if(!t1->data){
1173			return OPJ_FALSE;
1174		}
1175		t1->datasize=datasize;
1176	}
1177	memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1178
1179	t1->flags_stride=w+2;
1180	flagssize=t1->flags_stride * (h+2);
1181
1182	if(flagssize > t1->flagssize){
1183		opj_aligned_free(t1->flags);
1184		t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1185		if(!t1->flags){
1186			return OPJ_FALSE;
1187		}
1188		t1->flagssize=flagssize;
1189	}
1190	memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1191
1192	t1->w=w;
1193	t1->h=h;
1194
1195	return OPJ_TRUE;
1196}
1197
1198/* ----------------------------------------------------------------------- */
1199
1200/* ----------------------------------------------------------------------- */
1201/**
1202 * Creates a new Tier 1 handle
1203 * and initializes the look-up tables of the Tier-1 coder/decoder
1204 * @return a new T1 handle if successful, returns NULL otherwise
1205*/
1206opj_t1_t* opj_t1_create()
1207{
1208	opj_t1_t *l_t1 = 00;
1209
1210	l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1211	if (!l_t1) {
1212		return 00;
1213	}
1214	memset(l_t1,0,sizeof(opj_t1_t));
1215
1216	/* create MQC and RAW handles */
1217	l_t1->mqc = opj_mqc_create();
1218	if (! l_t1->mqc) {
1219		opj_t1_destroy(l_t1);
1220		return 00;
1221	}
1222
1223	l_t1->raw = opj_raw_create();
1224	if (! l_t1->raw) {
1225		opj_t1_destroy(l_t1);
1226		return 00;
1227	}
1228
1229	return l_t1;
1230}
1231
1232
1233/**
1234 * Destroys a previously created T1 handle
1235 *
1236 * @param p_t1 Tier 1 handle to destroy
1237*/
1238void opj_t1_destroy(opj_t1_t *p_t1)
1239{
1240	if (! p_t1) {
1241		return;
1242	}
1243
1244	/* destroy MQC and RAW handles */
1245	opj_mqc_destroy(p_t1->mqc);
1246	p_t1->mqc = 00;
1247	opj_raw_destroy(p_t1->raw);
1248	p_t1->raw = 00;
1249
1250    if (p_t1->data) {
1251		opj_aligned_free(p_t1->data);
1252		p_t1->data = 00;
1253	}
1254
1255	if (p_t1->flags) {
1256		opj_aligned_free(p_t1->flags);
1257		p_t1->flags = 00;
1258	}
1259
1260	opj_free(p_t1);
1261}
1262
1263OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
1264                            opj_tcd_tilecomp_t* tilec,
1265                            opj_tccp_t* tccp
1266                            )
1267{
1268	OPJ_UINT32 resno, bandno, precno, cblkno;
1269	OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1270
1271	for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1272		opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1273
1274		for (bandno = 0; bandno < res->numbands; ++bandno) {
1275			opj_tcd_band_t* restrict band = &res->bands[bandno];
1276
1277			for (precno = 0; precno < res->pw * res->ph; ++precno) {
1278				opj_tcd_precinct_t* precinct = &band->precincts[precno];
1279
1280				for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1281					opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1282					OPJ_INT32* restrict datap;
1283					/*void* restrict tiledp;*/
1284					OPJ_UINT32 cblk_w, cblk_h;
1285					OPJ_INT32 x, y;
1286					OPJ_UINT32 i, j;
1287
1288                    if (OPJ_FALSE == opj_t1_decode_cblk(
1289                                            t1,
1290                                            cblk,
1291                                            band->bandno,
1292                                            (OPJ_UINT32)tccp->roishift,
1293                                            tccp->cblksty)) {
1294                            return OPJ_FALSE;
1295                    }
1296
1297					x = cblk->x0 - band->x0;
1298					y = cblk->y0 - band->y0;
1299					if (band->bandno & 1) {
1300						opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1301						x += pres->x1 - pres->x0;
1302					}
1303					if (band->bandno & 2) {
1304						opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1305						y += pres->y1 - pres->y0;
1306					}
1307
1308					datap=t1->data;
1309					cblk_w = t1->w;
1310					cblk_h = t1->h;
1311
1312					if (tccp->roishift) {
1313						OPJ_INT32 thresh = 1 << tccp->roishift;
1314						for (j = 0; j < cblk_h; ++j) {
1315							for (i = 0; i < cblk_w; ++i) {
1316								OPJ_INT32 val = datap[(j * cblk_w) + i];
1317								OPJ_INT32 mag = abs(val);
1318								if (mag >= thresh) {
1319									mag >>= tccp->roishift;
1320									datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1321								}
1322							}
1323						}
1324					}
1325
1326					/*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1327					if (tccp->qmfbid == 1) {
1328                        OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1329						for (j = 0; j < cblk_h; ++j) {
1330							for (i = 0; i < cblk_w; ++i) {
1331								OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1332								((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1333							}
1334						}
1335					} else {		/* if (tccp->qmfbid == 0) */
1336                        OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1337						for (j = 0; j < cblk_h; ++j) {
1338                            OPJ_FLOAT32* restrict tiledp2 = tiledp;
1339							for (i = 0; i < cblk_w; ++i) {
1340                                OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1341                                *tiledp2 = tmp;
1342                                datap++;
1343                                tiledp2++;
1344								/*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1345								((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1346
1347							}
1348                            tiledp += tile_w;
1349						}
1350					}
1351                    /*opj_free(cblk->data);
1352					opj_free(cblk->segs);*/
1353					/*cblk->segs = 00;*/
1354				} /* cblkno */
1355                /*opj_free(precinct->cblks.dec);*/
1356			} /* precno */
1357		} /* bandno */
1358	} /* resno */
1359        return OPJ_TRUE;
1360}
1361
1362
1363OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1364                            opj_tcd_cblk_dec_t* cblk,
1365                            OPJ_UINT32 orient,
1366                            OPJ_UINT32 roishift,
1367                            OPJ_UINT32 cblksty)
1368{
1369	opj_raw_t *raw = t1->raw;	/* RAW component */
1370	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1371
1372	OPJ_INT32 bpno;
1373	OPJ_UINT32 passtype;
1374	OPJ_UINT32 segno, passno;
1375	OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1376
1377	if(!opj_t1_allocate_buffers(
1378				t1,
1379				(OPJ_UINT32)(cblk->x1 - cblk->x0),
1380				(OPJ_UINT32)(cblk->y1 - cblk->y0)))
1381	{
1382		return OPJ_FALSE;
1383	}
1384
1385	bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
1386	passtype = 2;
1387
1388	opj_mqc_resetstates(mqc);
1389	opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1390	opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1391	opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1392
1393	for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1394		opj_tcd_seg_t *seg = &cblk->segs[segno];
1395
1396		/* BYPASS mode */
1397		type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1398		/* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1399		if(seg->data == 00){
1400			continue;
1401		}
1402		if (type == T1_TYPE_RAW) {
1403			opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1404		} else {
1405            if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1406                    return OPJ_FALSE;
1407            }
1408		}
1409
1410		for (passno = 0; passno < seg->real_num_passes; ++passno) {
1411            switch (passtype) {
1412                case 0:
1413                    if (type == T1_TYPE_RAW) {
1414                        opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1415                    } else {
1416                        if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1417                            opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
1418                        } else {
1419                            opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
1420                        }
1421                    }
1422                    break;
1423                case 1:
1424                    if (type == T1_TYPE_RAW) {
1425                            opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
1426                    } else {
1427                        if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1428                            opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1429                        } else {
1430                            opj_t1_dec_refpass_mqc(t1, bpno+1);
1431                        }
1432                    }
1433                    break;
1434                case 2:
1435                    opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1436                    break;
1437            }
1438
1439			if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1440				opj_mqc_resetstates(mqc);
1441				opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1442				opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1443				opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1444			}
1445			if (++passtype == 3) {
1446				passtype = 0;
1447				bpno--;
1448			}
1449		}
1450	}
1451        return OPJ_TRUE;
1452}
1453
1454
1455
1456
1457OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1458                                opj_tcd_tile_t *tile,
1459                                opj_tcp_t *tcp,
1460                                const OPJ_FLOAT64 * mct_norms
1461                                )
1462{
1463	OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1464
1465	tile->distotile = 0;		/* fixed_quality */
1466
1467	for (compno = 0; compno < tile->numcomps; ++compno) {
1468		opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1469		opj_tccp_t* tccp = &tcp->tccps[compno];
1470		OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1471
1472		for (resno = 0; resno < tilec->numresolutions; ++resno) {
1473			opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1474
1475			for (bandno = 0; bandno < res->numbands; ++bandno) {
1476				opj_tcd_band_t* restrict band = &res->bands[bandno];
1477                OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1478
1479				for (precno = 0; precno < res->pw * res->ph; ++precno) {
1480					opj_tcd_precinct_t *prc = &band->precincts[precno];
1481
1482					for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1483						opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1484						OPJ_INT32 * restrict datap;
1485						OPJ_INT32* restrict tiledp;
1486						OPJ_UINT32 cblk_w;
1487						OPJ_UINT32 cblk_h;
1488						OPJ_UINT32 i, j;
1489
1490						OPJ_INT32 x = cblk->x0 - band->x0;
1491						OPJ_INT32 y = cblk->y0 - band->y0;
1492						if (band->bandno & 1) {
1493							opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1494							x += pres->x1 - pres->x0;
1495						}
1496						if (band->bandno & 2) {
1497							opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1498							y += pres->y1 - pres->y0;
1499						}
1500
1501						if(!opj_t1_allocate_buffers(
1502									t1,
1503									(OPJ_UINT32)(cblk->x1 - cblk->x0),
1504									(OPJ_UINT32)(cblk->y1 - cblk->y0)))
1505						{
1506							return OPJ_FALSE;
1507						}
1508
1509						datap=t1->data;
1510						cblk_w = t1->w;
1511						cblk_h = t1->h;
1512
1513						tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1514						if (tccp->qmfbid == 1) {
1515							for (j = 0; j < cblk_h; ++j) {
1516								for (i = 0; i < cblk_w; ++i) {
1517									OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1518									datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1519								}
1520							}
1521						} else {		/* if (tccp->qmfbid == 0) */
1522							for (j = 0; j < cblk_h; ++j) {
1523								for (i = 0; i < cblk_w; ++i) {
1524									OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1525									datap[(j * cblk_w) + i] =
1526										opj_int_fix_mul(
1527										tmp,
1528										bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1529								}
1530							}
1531						}
1532
1533						opj_t1_encode_cblk(
1534								t1,
1535								cblk,
1536								band->bandno,
1537								compno,
1538								tilec->numresolutions - 1 - resno,
1539								tccp->qmfbid,
1540								band->stepsize,
1541								tccp->cblksty,
1542								tile->numcomps,
1543								tile,
1544								mct_norms);
1545
1546					} /* cblkno */
1547				} /* precno */
1548			} /* bandno */
1549		} /* resno  */
1550	} /* compno  */
1551	return OPJ_TRUE;
1552}
1553
1554/** mod fixed_quality */
1555void opj_t1_encode_cblk(opj_t1_t *t1,
1556                        opj_tcd_cblk_enc_t* cblk,
1557                        OPJ_UINT32 orient,
1558                        OPJ_UINT32 compno,
1559                        OPJ_UINT32 level,
1560                        OPJ_UINT32 qmfbid,
1561                        OPJ_FLOAT64 stepsize,
1562                        OPJ_UINT32 cblksty,
1563                        OPJ_UINT32 numcomps,
1564                        opj_tcd_tile_t * tile,
1565                        const OPJ_FLOAT64 * mct_norms)
1566{
1567	OPJ_FLOAT64 cumwmsedec = 0.0;
1568
1569	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1570
1571	OPJ_UINT32 passno;
1572	OPJ_INT32 bpno;
1573	OPJ_UINT32 passtype;
1574	OPJ_INT32 nmsedec = 0;
1575	OPJ_INT32 max;
1576	OPJ_UINT32 i;
1577	OPJ_BYTE type = T1_TYPE_MQ;
1578	OPJ_FLOAT64 tempwmsedec;
1579
1580	max = 0;
1581	for (i = 0; i < t1->w * t1->h; ++i) {
1582		OPJ_INT32 tmp = abs(t1->data[i]);
1583		max = opj_int_max(max, tmp);
1584	}
1585
1586	cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1587
1588	bpno = (OPJ_INT32)(cblk->numbps - 1);
1589	passtype = 2;
1590
1591	opj_mqc_resetstates(mqc);
1592	opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1593	opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1594	opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1595	opj_mqc_init_enc(mqc, cblk->data);
1596
1597	for (passno = 0; bpno >= 0; ++passno) {
1598		opj_tcd_pass_t *pass = &cblk->passes[passno];
1599		OPJ_UINT32 correction = 3;
1600		type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1601
1602		switch (passtype) {
1603			case 0:
1604				opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1605				break;
1606			case 1:
1607				opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1608				break;
1609			case 2:
1610				opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1611				/* code switch SEGMARK (i.e. SEGSYM) */
1612				if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1613					opj_mqc_segmark_enc(mqc);
1614				break;
1615		}
1616
1617		/* fixed_quality */
1618		tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1619		cumwmsedec += tempwmsedec;
1620		tile->distotile += tempwmsedec;
1621
1622		/* Code switch "RESTART" (i.e. TERMALL) */
1623		if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)	&& !((passtype == 2) && (bpno - 1 < 0))) {
1624			if (type == T1_TYPE_RAW) {
1625				opj_mqc_flush(mqc);
1626				correction = 1;
1627				/* correction = mqc_bypass_flush_enc(); */
1628			} else {			/* correction = mqc_restart_enc(); */
1629				opj_mqc_flush(mqc);
1630				correction = 1;
1631			}
1632			pass->term = 1;
1633		} else {
1634			if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1635				|| ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1636				if (type == T1_TYPE_RAW) {
1637					opj_mqc_flush(mqc);
1638					correction = 1;
1639					/* correction = mqc_bypass_flush_enc(); */
1640				} else {		/* correction = mqc_restart_enc(); */
1641					opj_mqc_flush(mqc);
1642					correction = 1;
1643				}
1644				pass->term = 1;
1645			} else {
1646				pass->term = 0;
1647			}
1648		}
1649
1650		if (++passtype == 3) {
1651			passtype = 0;
1652			bpno--;
1653		}
1654
1655		if (pass->term && bpno > 0) {
1656			type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1657			if (type == T1_TYPE_RAW)
1658				opj_mqc_bypass_init_enc(mqc);
1659			else
1660				opj_mqc_restart_init_enc(mqc);
1661		}
1662
1663		pass->distortiondec = cumwmsedec;
1664		pass->rate = opj_mqc_numbytes(mqc) + correction;	/* FIXME */
1665
1666		/* Code-switch "RESET" */
1667		if (cblksty & J2K_CCP_CBLKSTY_RESET)
1668			opj_mqc_reset_enc(mqc);
1669	}
1670
1671	/* Code switch "ERTERM" (i.e. PTERM) */
1672	if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1673		opj_mqc_erterm_enc(mqc);
1674	else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1675		opj_mqc_flush(mqc);
1676
1677	cblk->totalpasses = passno;
1678
1679	for (passno = 0; passno<cblk->totalpasses; passno++) {
1680		opj_tcd_pass_t *pass = &cblk->passes[passno];
1681		if (pass->rate > opj_mqc_numbytes(mqc))
1682			pass->rate = opj_mqc_numbytes(mqc);
1683		/*Preventing generation of FF as last data byte of a pass*/
1684		if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1685			pass->rate--;
1686		}
1687		pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1688	}
1689}
1690
1691#if 0
1692void opj_t1_dec_refpass_step(   opj_t1_t *t1,
1693                                opj_flag_t *flagsp,
1694                                OPJ_INT32 *datap,
1695                                OPJ_INT32 poshalf,
1696                                OPJ_INT32 neghalf,
1697                                OPJ_BYTE type,
1698                                OPJ_UINT32 vsc)
1699{
1700	OPJ_INT32  t;
1701	OPJ_UINT32 v,flag;
1702
1703	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1704	opj_raw_t *raw = t1->raw;	/* RAW component */
1705
1706	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1707	if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1708		opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));	/* ESSAI */
1709		if (type == T1_TYPE_RAW) {
1710			v = opj_raw_decode(raw);
1711		} else {
1712			v = opj_mqc_decode(mqc);
1713		}
1714		t = v ? poshalf : neghalf;
1715		*datap += *datap < 0 ? -t : t;
1716		*flagsp |= T1_REFINE;
1717	}
1718}				/* VSC and  BYPASS by Antonin  */
1719#endif
1720
1721
1722
1723#if 0
1724void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
1725                                opj_flag_t *flagsp,
1726                                OPJ_INT32 *datap,
1727                                OPJ_UINT32 orient,
1728                                OPJ_INT32 oneplushalf,
1729                                OPJ_BYTE type,
1730                                OPJ_UINT32 vsc)
1731{
1732	OPJ_UINT32 v, flag;
1733
1734	opj_raw_t *raw = t1->raw;	/* RAW component */
1735	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1736
1737	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1738	if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1739		if (type == T1_TYPE_RAW) {
1740			if (opj_raw_decode(raw)) {
1741				v = opj_raw_decode(raw);	/* ESSAI */
1742				*datap = v ? -oneplushalf : oneplushalf;
1743				opj_t1_updateflags(flagsp, v, t1->flags_stride);
1744			}
1745		} else {
1746			opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1747			if (opj_mqc_decode(mqc)) {
1748				opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1749				v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1750				*datap = v ? -oneplushalf : oneplushalf;
1751				opj_t1_updateflags(flagsp, v, t1->flags_stride);
1752			}
1753		}
1754		*flagsp |= T1_VISIT;
1755	}
1756}				/* VSC and  BYPASS by Antonin */
1757#endif
1758