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