1 /*
2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "t1.h"
35 #include "t1_luts.h"
36 #include "opj_includes.h"
37 #include "j2k.h"
38 #include "tcd.h"
39 #include "mqc.h"
40 #include "raw.h"
41 #include "opj_malloc.h"
42 #include "int.h"
43 #include "dwt.h"
44 #include "fix.h"
45 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
46 /*@{*/
47
48 /** @name Local static functions */
49 /*@{*/
50
51 static INLINE OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
52 static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f);
53 static INLINE OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f);
54 static OPJ_BYTE t1_getspb(OPJ_UINT32 f);
55 static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
57 static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
58 /**
59 Encode significant pass
60 */
61 static void t1_enc_sigpass_step(
62 opj_t1_t *t1,
63 flag_t *flagsp,
64 OPJ_INT32 *datap,
65 OPJ_UINT32 orient,
66 OPJ_INT32 bpno,
67 OPJ_INT32 one,
68 OPJ_INT32 *nmsedec,
69 OPJ_BYTE type,
70 OPJ_UINT32 vsc);
71 /**
72 Decode significant pass
73 */
74 static void t1_dec_sigpass_step(
75 opj_t1_t *t1,
76 flag_t *flagsp,
77 OPJ_INT32 *datap,
78 OPJ_UINT32 orient,
79 OPJ_INT32 oneplushalf,
80 OPJ_BYTE type,
81 OPJ_UINT32 vsc);
82 /**
83 Encode significant pass
84 */
85 static void t1_enc_sigpass(
86 opj_t1_t *t1,
87 OPJ_INT32 bpno,
88 OPJ_UINT32 orient,
89 OPJ_INT32 *nmsedec,
90 OPJ_BYTE type,
91 OPJ_UINT32 cblksty);
92 /**
93 Decode significant pass
94 */
95 static void t1_dec_sigpass(
96 opj_t1_t *t1,
97 OPJ_INT32 bpno,
98 OPJ_UINT32 orient,
99 OPJ_BYTE type,
100 OPJ_UINT32 cblksty);
101 /**
102 Encode refinement pass
103 */
104 static void t1_enc_refpass_step(
105 opj_t1_t *t1,
106 flag_t *flagsp,
107 OPJ_INT32 *datap,
108 OPJ_INT32 bpno,
109 OPJ_INT32 one,
110 OPJ_INT32 *nmsedec,
111 OPJ_BYTE type,
112 OPJ_UINT32 vsc);
113 /**
114 Decode refinement pass
115 */
116 static void t1_dec_refpass_step(
117 opj_t1_t *t1,
118 flag_t *flagsp,
119 OPJ_INT32 *datap,
120 OPJ_INT32 poshalf,
121 OPJ_INT32 neghalf,
122 OPJ_BYTE type,
123 OPJ_UINT32 vsc);
124 /**
125 Encode refinement pass
126 */
127 static void t1_enc_refpass(
128 opj_t1_t *t1,
129 OPJ_INT32 bpno,
130 OPJ_INT32 *nmsedec,
131 OPJ_BYTE type,
132 OPJ_UINT32 cblksty);
133 /**
134 Decode refinement pass
135 */
136 static void t1_dec_refpass(
137 opj_t1_t *t1,
138 OPJ_INT32 bpno,
139 OPJ_BYTE type,
140 OPJ_UINT32 cblksty);
141 /**
142 Encode clean-up pass
143 */
144 static void t1_enc_clnpass_step(
145 opj_t1_t *t1,
146 flag_t *flagsp,
147 OPJ_INT32 *datap,
148 OPJ_UINT32 orient,
149 OPJ_INT32 bpno,
150 OPJ_INT32 one,
151 OPJ_INT32 *nmsedec,
152 OPJ_UINT32 partial,
153 OPJ_UINT32 vsc);
154 /**
155 Decode clean-up pass
156 */
157 static void t1_dec_clnpass_step(
158 opj_t1_t *t1,
159 flag_t *flagsp,
160 OPJ_INT32 *datap,
161 OPJ_UINT32 orient,
162 OPJ_INT32 oneplushalf,
163 OPJ_UINT32 partial,
164 OPJ_UINT32 vsc);
165 /**
166 Encode clean-up pass
167 */
168 static void t1_enc_clnpass(
169 opj_t1_t *t1,
170 OPJ_INT32 bpno,
171 OPJ_UINT32 orient,
172 OPJ_INT32 *nmsedec,
173 OPJ_UINT32 cblksty);
174 /**
175 Decode clean-up pass
176 */
177 static void t1_dec_clnpass(
178 opj_t1_t *t1,
179 OPJ_INT32 bpno,
180 OPJ_UINT32 orient,
181 OPJ_UINT32 cblksty);
182
183 static OPJ_FLOAT64 t1_getwmsedec(
184 OPJ_INT32 nmsedec,
185 OPJ_UINT32 compno,
186 OPJ_UINT32 level,
187 OPJ_UINT32 orient,
188 OPJ_INT32 bpno,
189 OPJ_UINT32 qmfbid,
190 OPJ_FLOAT64 stepsize,
191 OPJ_UINT32 numcomps,
192 const OPJ_FLOAT64 * mct_norms);
193 /**
194 Encode 1 code-block
195 @param t1 T1 handle
196 @param cblk Code-block coding parameters
197 @param orient
198 @param compno Component number
199 @param level
200 @param qmfbid
201 @param stepsize
202 @param cblksty Code-block style
203 @param numcomps
204 @param tile
205 */
206 static void t1_encode_cblk(
207 opj_t1_t *t1,
208 opj_tcd_cblk_enc_t* cblk,
209 OPJ_UINT32 orient,
210 OPJ_UINT32 compno,
211 OPJ_UINT32 level,
212 OPJ_UINT32 qmfbid,
213 OPJ_FLOAT64 stepsize,
214 OPJ_UINT32 cblksty,
215 OPJ_UINT32 numcomps,
216 opj_tcd_tile_t * tile,
217 const OPJ_FLOAT64 * mct_norms);
218 /**
219 Decode 1 code-block
220 @param t1 T1 handle
221 @param cblk Code-block coding parameters
222 @param orient
223 @param roishift Region of interest shifting value
224 @param cblksty Code-block style
225 */
226 static void t1_decode_cblk(
227 opj_t1_t *t1,
228 opj_tcd_cblk_dec_t* cblk,
229 OPJ_UINT32 orient,
230 OPJ_UINT32 roishift,
231 OPJ_UINT32 cblksty);
232
233 /*@}*/
234
235 /*@}*/
236
237 /* ----------------------------------------------------------------------- */
238
t1_getctxno_zc(OPJ_UINT32 f,OPJ_UINT32 orient)239 static OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
240 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
241 }
242
t1_getctxno_sc(OPJ_UINT32 f)243 static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f) {
244 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
245 }
246
t1_getctxno_mag(OPJ_UINT32 f)247 static OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f) {
248 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
249 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
250 return (tmp2);
251 }
252
t1_getspb(OPJ_UINT32 f)253 static OPJ_BYTE t1_getspb(OPJ_UINT32 f) {
254 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
255 }
256
t1_getnmsedec_sig(OPJ_UINT32 x,OPJ_UINT32 bitpos)257 static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
258 {
259 if (bitpos > T1_NMSEDEC_FRACBITS) {
260 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
261 }
262
263 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
264 }
265
t1_getnmsedec_ref(OPJ_UINT32 x,OPJ_UINT32 bitpos)266 static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
267 if (bitpos > T1_NMSEDEC_FRACBITS) {
268 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
269 }
270
271 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
272 }
273
t1_updateflags(flag_t * flagsp,OPJ_UINT32 s,OPJ_UINT32 stride)274 static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
275 flag_t *np = flagsp - stride;
276 flag_t *sp = flagsp + stride;
277
278 static const flag_t mod[] = {
279 T1_SIG_S, T1_SIG_S|T1_SGN_S,
280 T1_SIG_E, T1_SIG_E|T1_SGN_E,
281 T1_SIG_W, T1_SIG_W|T1_SGN_W,
282 T1_SIG_N, T1_SIG_N|T1_SGN_N
283 };
284
285 np[-1] |= T1_SIG_SE;
286 np[0] |= mod[s];
287 np[1] |= T1_SIG_SW;
288
289 flagsp[-1] |= mod[s+2];
290 flagsp[0] |= T1_SIG;
291 flagsp[1] |= mod[s+4];
292
293 sp[-1] |= T1_SIG_NE;
294 sp[0] |= mod[s+6];
295 sp[1] |= T1_SIG_NW;
296 }
297
t1_enc_sigpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 vsc)298 static void t1_enc_sigpass_step(
299 opj_t1_t *t1,
300 flag_t *flagsp,
301 OPJ_INT32 *datap,
302 OPJ_UINT32 orient,
303 OPJ_INT32 bpno,
304 OPJ_INT32 one,
305 OPJ_INT32 *nmsedec,
306 OPJ_BYTE type,
307 OPJ_UINT32 vsc)
308 {
309 OPJ_INT32 v;
310 OPJ_UINT32 flag;
311
312 opj_mqc_t *mqc = t1->mqc; /* MQC component */
313
314 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
315 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
316 v = int_abs(*datap) & one ? 1 : 0;
317 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
318 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
319 mqc_bypass_enc(mqc, v);
320 } else {
321 mqc_encode(mqc, v);
322 }
323 if (v) {
324 v = *datap < 0 ? 1 : 0;
325 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
326 mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
327 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
328 mqc_bypass_enc(mqc, v);
329 } else {
330 mqc_encode(mqc, v ^ t1_getspb(flag));
331 }
332 t1_updateflags(flagsp, v, t1->flags_stride);
333 }
334 *flagsp |= T1_VISIT;
335 }
336 }
337
t1_dec_sigpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 oneplushalf,OPJ_BYTE type,OPJ_UINT32 vsc)338 static void t1_dec_sigpass_step(
339 opj_t1_t *t1,
340 flag_t *flagsp,
341 OPJ_INT32 *datap,
342 OPJ_UINT32 orient,
343 OPJ_INT32 oneplushalf,
344 OPJ_BYTE type,
345 OPJ_UINT32 vsc)
346 {
347 OPJ_UINT32 v, flag;
348
349 opj_raw_t *raw = t1->raw; /* RAW component */
350 opj_mqc_t *mqc = t1->mqc; /* MQC component */
351
352 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
353 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
354 if (type == T1_TYPE_RAW) {
355 if (raw_decode(raw)) {
356 v = raw_decode(raw); /* ESSAI */
357 *datap = v ? -oneplushalf : oneplushalf;
358 t1_updateflags(flagsp, v, t1->flags_stride);
359 }
360 } else {
361 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
362 if (mqc_decode(mqc)) {
363 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
364 v = mqc_decode(mqc) ^ t1_getspb(flag);
365 *datap = v ? -oneplushalf : oneplushalf;
366 t1_updateflags(flagsp, v, t1->flags_stride);
367 }
368 }
369 *flagsp |= T1_VISIT;
370 }
371 } /* VSC and BYPASS by Antonin */
372
t1_enc_sigpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 cblksty)373 static void t1_enc_sigpass(
374 opj_t1_t *t1,
375 OPJ_INT32 bpno,
376 OPJ_UINT32 orient,
377 OPJ_INT32 *nmsedec,
378 OPJ_BYTE type,
379 OPJ_UINT32 cblksty)
380 {
381 OPJ_UINT32 i, j, k, vsc;
382 OPJ_INT32 one;
383
384 *nmsedec = 0;
385 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
386 for (k = 0; k < t1->h; k += 4) {
387 for (i = 0; i < t1->w; ++i) {
388 for (j = k; j < k + 4 && j < t1->h; ++j) {
389 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
390 t1_enc_sigpass_step(
391 t1,
392 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
393 &t1->data[(j * t1->w) + i],
394 orient,
395 bpno,
396 one,
397 nmsedec,
398 type,
399 vsc);
400 }
401 }
402 }
403 }
404
t1_dec_sigpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_BYTE type,OPJ_UINT32 cblksty)405 static void t1_dec_sigpass(
406 opj_t1_t *t1,
407 OPJ_INT32 bpno,
408 OPJ_UINT32 orient,
409 OPJ_BYTE type,
410 OPJ_UINT32 cblksty)
411 {
412 OPJ_UINT32 i, j, k, vsc;
413 OPJ_INT32 one, half, oneplushalf;
414 one = 1 << bpno;
415 half = one >> 1;
416 oneplushalf = one | half;
417 for (k = 0; k < t1->h; k += 4) {
418 for (i = 0; i < t1->w; ++i) {
419 for (j = k; j < k + 4 && j < t1->h; ++j) {
420 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
421 t1_dec_sigpass_step(
422 t1,
423 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
424 &t1->data[(j * t1->w) + i],
425 orient,
426 oneplushalf,
427 type,
428 vsc);
429 }
430 }
431 }
432 } /* VSC and BYPASS by Antonin */
433
t1_enc_refpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 vsc)434 static void t1_enc_refpass_step(
435 opj_t1_t *t1,
436 flag_t *flagsp,
437 OPJ_INT32 *datap,
438 OPJ_INT32 bpno,
439 OPJ_INT32 one,
440 OPJ_INT32 *nmsedec,
441 OPJ_BYTE type,
442 OPJ_UINT32 vsc)
443 {
444 OPJ_INT32 v;
445 OPJ_UINT32 flag;
446
447 opj_mqc_t *mqc = t1->mqc; /* MQC component */
448
449 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
450 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
451 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
452 v = int_abs(*datap) & one ? 1 : 0;
453 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
454 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
455 mqc_bypass_enc(mqc, v);
456 } else {
457 mqc_encode(mqc, v);
458 }
459 *flagsp |= T1_REFINE;
460 }
461 }
462
t1_dec_refpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 poshalf,OPJ_INT32 neghalf,OPJ_BYTE type,OPJ_UINT32 vsc)463 static void t1_dec_refpass_step(
464 opj_t1_t *t1,
465 flag_t *flagsp,
466 OPJ_INT32 *datap,
467 OPJ_INT32 poshalf,
468 OPJ_INT32 neghalf,
469 OPJ_BYTE type,
470 OPJ_UINT32 vsc)
471 {
472 OPJ_INT32 t;
473 OPJ_UINT32 v,flag;
474
475 opj_mqc_t *mqc = t1->mqc; /* MQC component */
476 opj_raw_t *raw = t1->raw; /* RAW component */
477
478 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
479 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
480 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
481 if (type == T1_TYPE_RAW) {
482 v = raw_decode(raw);
483 } else {
484 v = mqc_decode(mqc);
485 }
486 t = v ? poshalf : neghalf;
487 *datap += *datap < 0 ? -t : t;
488 *flagsp |= T1_REFINE;
489 }
490 } /* VSC and BYPASS by Antonin */
491
t1_enc_refpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 cblksty)492 static void t1_enc_refpass(
493 opj_t1_t *t1,
494 OPJ_INT32 bpno,
495 OPJ_INT32 *nmsedec,
496 OPJ_BYTE type,
497 OPJ_UINT32 cblksty)
498 {
499 OPJ_UINT32 i, j, k, vsc;
500 OPJ_INT32 one;
501
502 *nmsedec = 0;
503 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
504 for (k = 0; k < t1->h; k += 4) {
505 for (i = 0; i < t1->w; ++i) {
506 for (j = k; j < k + 4 && j < t1->h; ++j) {
507 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
508 t1_enc_refpass_step(
509 t1,
510 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
511 &t1->data[(j * t1->w) + i],
512 bpno,
513 one,
514 nmsedec,
515 type,
516 vsc);
517 }
518 }
519 }
520 }
521
t1_dec_refpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_BYTE type,OPJ_UINT32 cblksty)522 static void t1_dec_refpass(
523 opj_t1_t *t1,
524 OPJ_INT32 bpno,
525 OPJ_BYTE type,
526 OPJ_UINT32 cblksty)
527 {
528 OPJ_UINT32 i, j, k;
529 OPJ_INT32 one, poshalf, neghalf;
530 OPJ_UINT32 vsc;
531 one = 1 << bpno;
532 poshalf = one >> 1;
533 neghalf = bpno > 0 ? -poshalf : -1;
534 for (k = 0; k < t1->h; k += 4) {
535 for (i = 0; i < t1->w; ++i) {
536 for (j = k; j < k + 4 && j < t1->h; ++j) {
537 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
538 t1_dec_refpass_step(
539 t1,
540 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
541 &t1->data[(j * t1->w) + i],
542 poshalf,
543 neghalf,
544 type,
545 vsc);
546 }
547 }
548 }
549 } /* VSC and BYPASS by Antonin */
550
t1_enc_clnpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_UINT32 partial,OPJ_UINT32 vsc)551 static void t1_enc_clnpass_step(
552 opj_t1_t *t1,
553 flag_t *flagsp,
554 OPJ_INT32 *datap,
555 OPJ_UINT32 orient,
556 OPJ_INT32 bpno,
557 OPJ_INT32 one,
558 OPJ_INT32 *nmsedec,
559 OPJ_UINT32 partial,
560 OPJ_UINT32 vsc)
561 {
562 OPJ_INT32 v;
563 OPJ_UINT32 flag;
564
565 opj_mqc_t *mqc = t1->mqc; /* MQC component */
566
567 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
568 if (partial) {
569 goto LABEL_PARTIAL;
570 }
571 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
572 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
573 v = int_abs(*datap) & one ? 1 : 0;
574 mqc_encode(mqc, v);
575 if (v) {
576 LABEL_PARTIAL:
577 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
578 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
579 v = *datap < 0 ? 1 : 0;
580 mqc_encode(mqc, v ^ t1_getspb(flag));
581 t1_updateflags(flagsp, v, t1->flags_stride);
582 }
583 }
584 *flagsp &= ~T1_VISIT;
585 }
586
t1_dec_clnpass_step(opj_t1_t * t1,flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 oneplushalf,OPJ_UINT32 partial,OPJ_UINT32 vsc)587 static void t1_dec_clnpass_step(
588 opj_t1_t *t1,
589 flag_t *flagsp,
590 OPJ_INT32 *datap,
591 OPJ_UINT32 orient,
592 OPJ_INT32 oneplushalf,
593 OPJ_UINT32 partial,
594 OPJ_UINT32 vsc)
595 {
596 OPJ_INT32 v;
597 OPJ_UINT32 flag;
598
599 opj_mqc_t *mqc = t1->mqc; /* MQC component */
600
601 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
602 if (partial) {
603 goto LABEL_PARTIAL;
604 }
605 if (!(flag & (T1_SIG | T1_VISIT))) {
606 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
607 if (mqc_decode(mqc)) {
608 LABEL_PARTIAL:
609 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
610 v = mqc_decode(mqc) ^ t1_getspb(flag);
611 *datap = v ? -oneplushalf : oneplushalf;
612 t1_updateflags(flagsp, v, t1->flags_stride);
613 }
614 }
615 *flagsp &= ~T1_VISIT;
616 } /* VSC and BYPASS by Antonin */
617
t1_enc_clnpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_INT32 * nmsedec,OPJ_UINT32 cblksty)618 static void t1_enc_clnpass(
619 opj_t1_t *t1,
620 OPJ_INT32 bpno,
621 OPJ_UINT32 orient,
622 OPJ_INT32 *nmsedec,
623 OPJ_UINT32 cblksty)
624 {
625 OPJ_UINT32 i, j, k;
626 OPJ_INT32 one;
627 OPJ_UINT32 agg, runlen, vsc;
628
629 opj_mqc_t *mqc = t1->mqc; /* MQC component */
630
631 *nmsedec = 0;
632 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
633 for (k = 0; k < t1->h; k += 4) {
634 for (i = 0; i < t1->w; ++i) {
635 if (k + 3 < t1->h) {
636 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
637 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
638 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
639 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
640 || (MACRO_t1_flags(1 + k + 3,1 + i)
641 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
642 } else {
643 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
644 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
645 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
646 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
647 }
648 } else {
649 agg = 0;
650 }
651 if (agg) {
652 for (runlen = 0; runlen < 4; ++runlen) {
653 if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
654 break;
655 }
656 mqc_setcurctx(mqc, T1_CTXNO_AGG);
657 mqc_encode(mqc, runlen != 4);
658 if (runlen == 4) {
659 continue;
660 }
661 mqc_setcurctx(mqc, T1_CTXNO_UNI);
662 mqc_encode(mqc, runlen >> 1);
663 mqc_encode(mqc, runlen & 1);
664 } else {
665 runlen = 0;
666 }
667 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
668 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
669 t1_enc_clnpass_step(
670 t1,
671 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
672 &t1->data[(j * t1->w) + i],
673 orient,
674 bpno,
675 one,
676 nmsedec,
677 agg && (j == k + runlen),
678 vsc);
679 }
680 }
681 }
682 }
683
t1_dec_clnpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_UINT32 cblksty)684 static void t1_dec_clnpass(
685 opj_t1_t *t1,
686 OPJ_INT32 bpno,
687 OPJ_UINT32 orient,
688 OPJ_UINT32 cblksty)
689 {
690 OPJ_UINT32 i, j, k, one;
691 OPJ_INT32 half, oneplushalf;
692 OPJ_UINT32 agg, runlen, vsc;
693 OPJ_UINT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
694
695 opj_mqc_t *mqc = t1->mqc; /* MQC component */
696
697 one = 1 << bpno;
698 half = one >> 1;
699 oneplushalf = one | half;
700 for (k = 0; k < t1->h; k += 4) {
701 for (i = 0; i < t1->w; ++i) {
702 if (k + 3 < t1->h) {
703 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
704 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
705 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
706 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
707 || (MACRO_t1_flags(1 + k + 3,1 + i)
708 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
709 } else {
710 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
711 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
712 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
713 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
714 }
715 } else {
716 agg = 0;
717 }
718 if (agg) {
719 mqc_setcurctx(mqc, T1_CTXNO_AGG);
720 if (!mqc_decode(mqc)) {
721 continue;
722 }
723 mqc_setcurctx(mqc, T1_CTXNO_UNI);
724 runlen = mqc_decode(mqc);
725 runlen = (runlen << 1) | mqc_decode(mqc);
726 } else {
727 runlen = 0;
728 }
729 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
730 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
731 t1_dec_clnpass_step(
732 t1,
733 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
734 &t1->data[(j * t1->w) + i],
735 orient,
736 oneplushalf,
737 agg && (j == k + runlen),
738 vsc);
739 }
740 }
741 }
742 if (segsym) {
743 OPJ_UINT32 v = 0;
744 mqc_setcurctx(mqc, T1_CTXNO_UNI);
745 v = mqc_decode(mqc);
746 v = (v << 1) | mqc_decode(mqc);
747 v = (v << 1) | mqc_decode(mqc);
748 v = (v << 1) | mqc_decode(mqc);
749 /*
750 if (v!=0xa) {
751 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
752 }
753 */
754 }
755 } /* VSC and BYPASS by Antonin */
756
757
758 /** mod fixed_quality */
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)759 static OPJ_FLOAT64 t1_getwmsedec(
760 OPJ_INT32 nmsedec,
761 OPJ_UINT32 compno,
762 OPJ_UINT32 level,
763 OPJ_UINT32 orient,
764 OPJ_INT32 bpno,
765 OPJ_UINT32 qmfbid,
766 OPJ_FLOAT64 stepsize,
767 OPJ_UINT32 numcomps,
768 const OPJ_FLOAT64 * mct_norms)
769 {
770 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
771 if
772 (mct_norms)
773 {
774 w1 = mct_norms[compno];
775 }
776 if (qmfbid == 1)
777 {
778 w2 = dwt_getnorm(level, orient);
779 } else { /* if (qmfbid == 0) */
780 w2 = dwt_getnorm_real(level, orient);
781 }
782 wmsedec = w1 * w2 * stepsize * (1 << bpno);
783 wmsedec *= wmsedec * nmsedec / 8192.0;
784 return wmsedec;
785 }
786
allocate_buffers(opj_t1_t * t1,OPJ_UINT32 w,OPJ_UINT32 h)787 static bool allocate_buffers(
788 opj_t1_t *t1,
789 OPJ_UINT32 w,
790 OPJ_UINT32 h)
791 {
792 OPJ_UINT32 datasize=w * h;
793 OPJ_UINT32 flagssize;
794
795 if(datasize > t1->datasize){
796 opj_aligned_free(t1->data);
797 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
798 if(!t1->data){
799 return false;
800 }
801 t1->datasize=datasize;
802 }
803 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
804
805 t1->flags_stride=w+2;
806 flagssize=t1->flags_stride * (h+2);
807
808 if(flagssize > t1->flagssize){
809 opj_aligned_free(t1->flags);
810 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
811 if(!t1->flags){
812 return false;
813 }
814 t1->flagssize=flagssize;
815 }
816 memset(t1->flags,0,flagssize * sizeof(flag_t));
817
818 t1->w=w;
819 t1->h=h;
820
821 return true;
822 }
823
824 /** mod fixed_quality */
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)825 static void t1_encode_cblk(
826 opj_t1_t *t1,
827 opj_tcd_cblk_enc_t* cblk,
828 OPJ_UINT32 orient,
829 OPJ_UINT32 compno,
830 OPJ_UINT32 level,
831 OPJ_UINT32 qmfbid,
832 OPJ_FLOAT64 stepsize,
833 OPJ_UINT32 cblksty,
834 OPJ_UINT32 numcomps,
835 opj_tcd_tile_t * tile,
836 const OPJ_FLOAT64 * mct_norms)
837 {
838 OPJ_FLOAT64 cumwmsedec = 0.0;
839
840 opj_mqc_t *mqc = t1->mqc; /* MQC component */
841
842 OPJ_UINT32 passno;
843 OPJ_INT32 bpno;
844 OPJ_UINT32 passtype;
845 OPJ_INT32 nmsedec = 0;
846 OPJ_INT32 max;
847 OPJ_UINT32 i;
848 OPJ_BYTE type = T1_TYPE_MQ;
849 OPJ_FLOAT64 tempwmsedec;
850
851 max = 0;
852 for (i = 0; i < t1->w * t1->h; ++i) {
853 OPJ_INT32 tmp = abs(t1->data[i]);
854 max = int_max(max, tmp);
855 }
856
857 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
858
859 bpno = cblk->numbps - 1;
860 passtype = 2;
861
862 mqc_resetstates(mqc);
863 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
864 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
865 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
866 mqc_init_enc(mqc, cblk->data);
867
868 for (passno = 0; bpno >= 0; ++passno) {
869 opj_tcd_pass_t *pass = &cblk->passes[passno];
870 OPJ_UINT32 correction = 3;
871 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
872
873 switch (passtype) {
874 case 0:
875 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
876 break;
877 case 1:
878 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
879 break;
880 case 2:
881 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
882 /* code switch SEGMARK (i.e. SEGSYM) */
883 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
884 mqc_segmark_enc(mqc);
885 break;
886 }
887
888 /* fixed_quality */
889 tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
890 cumwmsedec += tempwmsedec;
891 tile->distotile += tempwmsedec;
892
893 /* Code switch "RESTART" (i.e. TERMALL) */
894 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
895 if (type == T1_TYPE_RAW) {
896 mqc_flush(mqc);
897 correction = 1;
898 /* correction = mqc_bypass_flush_enc(); */
899 } else { /* correction = mqc_restart_enc(); */
900 mqc_flush(mqc);
901 correction = 1;
902 }
903 pass->term = 1;
904 } else {
905 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
906 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
907 if (type == T1_TYPE_RAW) {
908 mqc_flush(mqc);
909 correction = 1;
910 /* correction = mqc_bypass_flush_enc(); */
911 } else { /* correction = mqc_restart_enc(); */
912 mqc_flush(mqc);
913 correction = 1;
914 }
915 pass->term = 1;
916 } else {
917 pass->term = 0;
918 }
919 }
920
921 if (++passtype == 3) {
922 passtype = 0;
923 bpno--;
924 }
925
926 if (pass->term && bpno > 0) {
927 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
928 if (type == T1_TYPE_RAW)
929 mqc_bypass_init_enc(mqc);
930 else
931 mqc_restart_init_enc(mqc);
932 }
933
934 pass->distortiondec = cumwmsedec;
935 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
936
937 /* Code-switch "RESET" */
938 if (cblksty & J2K_CCP_CBLKSTY_RESET)
939 mqc_reset_enc(mqc);
940 }
941
942 /* Code switch "ERTERM" (i.e. PTERM) */
943 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
944 mqc_erterm_enc(mqc);
945 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
946 mqc_flush(mqc);
947
948 cblk->totalpasses = passno;
949
950 for (passno = 0; passno<cblk->totalpasses; passno++) {
951 opj_tcd_pass_t *pass = &cblk->passes[passno];
952 if (pass->rate > mqc_numbytes(mqc))
953 pass->rate = mqc_numbytes(mqc);
954 /*Preventing generation of FF as last data byte of a pass*/
955 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
956 pass->rate--;
957 }
958 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
959 }
960 }
961
t1_decode_cblk(opj_t1_t * t1,opj_tcd_cblk_dec_t * cblk,OPJ_UINT32 orient,OPJ_UINT32 roishift,OPJ_UINT32 cblksty)962 static void t1_decode_cblk(
963 opj_t1_t *t1,
964 opj_tcd_cblk_dec_t* cblk,
965 OPJ_UINT32 orient,
966 OPJ_UINT32 roishift,
967 OPJ_UINT32 cblksty)
968 {
969 opj_raw_t *raw = t1->raw; /* RAW component */
970 opj_mqc_t *mqc = t1->mqc; /* MQC component */
971
972 OPJ_INT32 bpno;
973 OPJ_UINT32 passtype;
974 OPJ_UINT32 segno, passno;
975 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
976
977 if(!allocate_buffers(
978 t1,
979 cblk->x1 - cblk->x0,
980 cblk->y1 - cblk->y0))
981 {
982 return;
983 }
984
985 bpno = roishift + cblk->numbps - 1;
986 passtype = 2;
987
988 mqc_resetstates(mqc);
989 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
990 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
991 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
992
993 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
994 opj_tcd_seg_t *seg = &cblk->segs[segno];
995
996 /* BYPASS mode */
997 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
998 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
999 if(seg->data == 00){
1000 continue;
1001 }
1002 if (type == T1_TYPE_RAW) {
1003 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1004 } else {
1005 mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
1006 }
1007
1008 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1009 switch (passtype) {
1010 case 0:
1011 t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1012 break;
1013 case 1:
1014 t1_dec_refpass(t1, bpno+1, type, cblksty);
1015 break;
1016 case 2:
1017 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1018 break;
1019 }
1020
1021 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1022 mqc_resetstates(mqc);
1023 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1024 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1025 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1026 }
1027 if (++passtype == 3) {
1028 passtype = 0;
1029 bpno--;
1030 }
1031 }
1032 }
1033 }
1034
1035 /* ----------------------------------------------------------------------- */
1036 /**
1037 * Creates a new Tier 1 handle
1038 * and initializes the look-up tables of the Tier-1 coder/decoder
1039 * @return a new T1 handle if successful, returns NULL otherwise
1040 */
t1_create()1041 opj_t1_t* t1_create()
1042 {
1043 opj_t1_t *l_t1 = 00;
1044
1045 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1046 if
1047 (!l_t1)
1048 {
1049 return 00;
1050 }
1051 memset(l_t1,0,sizeof(opj_t1_t));
1052
1053 /* create MQC and RAW handles */
1054 l_t1->mqc = mqc_create();
1055 if
1056 (! l_t1->mqc)
1057 {
1058 t1_destroy(l_t1);
1059 return 00;
1060 }
1061 l_t1->raw = raw_create();
1062 if
1063 (! l_t1->raw)
1064 {
1065 t1_destroy(l_t1);
1066 return 00;
1067 }
1068 return l_t1;
1069 }
1070
1071 /**
1072 * Destroys a previously created T1 handle
1073 *
1074 * @param p_t1 Tier 1 handle to destroy
1075 */
t1_destroy(opj_t1_t * p_t1)1076 void t1_destroy(opj_t1_t *p_t1)
1077 {
1078 if
1079 (! p_t1)
1080 {
1081 return;
1082 }
1083
1084 /* destroy MQC and RAW handles */
1085 mqc_destroy(p_t1->mqc);
1086 p_t1->mqc = 00;
1087 raw_destroy(p_t1->raw);
1088 p_t1->raw = 00;
1089 if
1090 (p_t1->data)
1091 {
1092 opj_aligned_free(p_t1->data);
1093 p_t1->data = 00;
1094 }
1095 if
1096 (p_t1->flags)
1097 {
1098 opj_aligned_free(p_t1->flags);
1099 p_t1->flags = 00;
1100 }
1101 opj_free(p_t1);
1102 }
1103
t1_encode_cblks(opj_t1_t * t1,opj_tcd_tile_t * tile,opj_tcp_t * tcp,const OPJ_FLOAT64 * mct_norms)1104 bool t1_encode_cblks(
1105 opj_t1_t *t1,
1106 opj_tcd_tile_t *tile,
1107 opj_tcp_t *tcp,
1108 const OPJ_FLOAT64 * mct_norms)
1109 {
1110 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1111
1112 tile->distotile = 0; /* fixed_quality */
1113
1114 for (compno = 0; compno < tile->numcomps; ++compno) {
1115 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1116 opj_tccp_t* tccp = &tcp->tccps[compno];
1117 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1118
1119 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1120 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1121
1122 for (bandno = 0; bandno < res->numbands; ++bandno) {
1123 opj_tcd_band_t* restrict band = &res->bands[bandno];
1124
1125 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1126 opj_tcd_precinct_t *prc = &band->precincts[precno];
1127
1128 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1129 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1130 OPJ_INT32 * restrict datap;
1131 OPJ_INT32* restrict tiledp;
1132 OPJ_UINT32 cblk_w;
1133 OPJ_UINT32 cblk_h;
1134 OPJ_UINT32 i, j;
1135
1136 OPJ_INT32 x = cblk->x0 - band->x0;
1137 OPJ_INT32 y = cblk->y0 - band->y0;
1138 if (band->bandno & 1) {
1139 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1140 x += pres->x1 - pres->x0;
1141 }
1142 if (band->bandno & 2) {
1143 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1144 y += pres->y1 - pres->y0;
1145 }
1146
1147 if(!allocate_buffers(
1148 t1,
1149 cblk->x1 - cblk->x0,
1150 cblk->y1 - cblk->y0))
1151 {
1152 return false;
1153 }
1154
1155 datap=t1->data;
1156 cblk_w = t1->w;
1157 cblk_h = t1->h;
1158
1159 tiledp=&tilec->data[(y * tile_w) + x];
1160 if (tccp->qmfbid == 1) {
1161 for (j = 0; j < cblk_h; ++j) {
1162 for (i = 0; i < cblk_w; ++i) {
1163 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1164 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1165 }
1166 }
1167 } else { /* if (tccp->qmfbid == 0) */
1168 for (j = 0; j < cblk_h; ++j) {
1169 for (i = 0; i < cblk_w; ++i) {
1170 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1171 datap[(j * cblk_w) + i] =
1172 fix_mul(
1173 tmp,
1174 8192 * 8192 / ((OPJ_INT32) floorf(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
1175 }
1176 }
1177 }
1178
1179 t1_encode_cblk(
1180 t1,
1181 cblk,
1182 band->bandno,
1183 compno,
1184 tilec->numresolutions - 1 - resno,
1185 tccp->qmfbid,
1186 band->stepsize,
1187 tccp->cblksty,
1188 tile->numcomps,
1189 tile,
1190 mct_norms);
1191
1192 } /* cblkno */
1193 } /* precno */
1194 } /* bandno */
1195 } /* resno */
1196 } /* compno */
1197 return true;
1198 }
1199
t1_decode_cblks(opj_t1_t * t1,opj_tcd_tilecomp_t * tilec,opj_tccp_t * tccp)1200 void t1_decode_cblks(
1201 opj_t1_t* t1,
1202 opj_tcd_tilecomp_t* tilec,
1203 opj_tccp_t* tccp)
1204 {
1205 OPJ_UINT32 resno, bandno, precno, cblkno;
1206
1207 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1208
1209 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1210 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1211
1212 for (bandno = 0; bandno < res->numbands; ++bandno) {
1213 opj_tcd_band_t* restrict band = &res->bands[bandno];
1214
1215 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1216 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1217
1218 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1219 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1220 OPJ_INT32* restrict datap;
1221 void* restrict tiledp;
1222 OPJ_UINT32 cblk_w, cblk_h;
1223 OPJ_INT32 x, y;
1224 OPJ_UINT32 i, j;
1225
1226 t1_decode_cblk(
1227 t1,
1228 cblk,
1229 band->bandno,
1230 tccp->roishift,
1231 tccp->cblksty);
1232
1233 x = cblk->x0 - band->x0;
1234 y = cblk->y0 - band->y0;
1235 if (band->bandno & 1) {
1236 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1237 x += pres->x1 - pres->x0;
1238 }
1239 if (band->bandno & 2) {
1240 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1241 y += pres->y1 - pres->y0;
1242 }
1243
1244 datap=t1->data;
1245 cblk_w = t1->w;
1246 cblk_h = t1->h;
1247
1248 if (tccp->roishift) {
1249 OPJ_INT32 thresh = 1 << tccp->roishift;
1250 for (j = 0; j < cblk_h; ++j) {
1251 for (i = 0; i < cblk_w; ++i) {
1252 OPJ_INT32 val = datap[(j * cblk_w) + i];
1253 OPJ_INT32 mag = abs(val);
1254 if (mag >= thresh) {
1255 mag >>= tccp->roishift;
1256 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1257 }
1258 }
1259 }
1260 }
1261
1262 tiledp=(void*)&tilec->data[(y * tile_w) + x];
1263 if (tccp->qmfbid == 1) {
1264 for (j = 0; j < cblk_h; ++j) {
1265 for (i = 0; i < cblk_w; ++i) {
1266 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1267 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1268 }
1269 }
1270 } else { /* if (tccp->qmfbid == 0) */
1271 for (j = 0; j < cblk_h; ++j) {
1272 for (i = 0; i < cblk_w; ++i) {
1273 float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1274 ((float*)tiledp)[(j * tile_w) + i] = tmp;
1275 }
1276 }
1277 }
1278 //opj_free(cblk->segs);
1279 //cblk->segs = 00;
1280 } /* cblkno */
1281 } /* precno */
1282 } /* bandno */
1283 } /* resno */
1284 }
1285
1286
1287