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, Jonathan Ballard <dzonatas@dzonux.net>
9 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
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 #ifdef __SSE__
35 #include <xmmintrin.h>
36 #endif
37
38 #include "opj_includes.h"
39
40 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
41 /*@{*/
42
43 #define OPJ_WS(i) v->mem[(i)*2]
44 #define OPJ_WD(i) v->mem[(1+(i)*2)]
45
46 /** @name Local data structures */
47 /*@{*/
48
49 typedef struct dwt_local {
50 OPJ_INT32* mem;
51 OPJ_INT32 dn;
52 OPJ_INT32 sn;
53 OPJ_INT32 cas;
54 } opj_dwt_t;
55
56 typedef union {
57 OPJ_FLOAT32 f[4];
58 } opj_v4_t;
59
60 typedef struct v4dwt_local {
61 opj_v4_t* wavelet ;
62 OPJ_INT32 dn ;
63 OPJ_INT32 sn ;
64 OPJ_INT32 cas ;
65 } opj_v4dwt_t ;
66
67 static const OPJ_FLOAT32 opj_dwt_alpha = 1.586134342f; /* 12994 */
68 static const OPJ_FLOAT32 opj_dwt_beta = 0.052980118f; /* 434 */
69 static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /* -7233 */
70 static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /* -3633 */
71
72 static const OPJ_FLOAT32 opj_K = 1.230174105f; /* 10078 */
73 static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
74
75 /*@}*/
76
77 /**
78 Virtual function type for wavelet transform in 1-D
79 */
80 typedef void (*DWT1DFN)(opj_dwt_t* v);
81
82 /** @name Local static functions */
83 /*@{*/
84
85 /**
86 Forward lazy transform (horizontal)
87 */
88 static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
89 /**
90 Forward lazy transform (vertical)
91 */
92 static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
93 /**
94 Inverse lazy transform (horizontal)
95 */
96 static void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a);
97 /**
98 Inverse lazy transform (vertical)
99 */
100 static void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
101 /**
102 Forward 5-3 wavelet transform in 1-D
103 */
104 static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
105 /**
106 Inverse 5-3 wavelet transform in 1-D
107 */
108 static void opj_dwt_decode_1(opj_dwt_t *v);
109 static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
110 /**
111 Forward 9-7 wavelet transform in 1-D
112 */
113 static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
114 /**
115 Explicit calculation of the Quantization Stepsizes
116 */
117 static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
118 /**
119 Inverse wavelet transform in 2-D.
120 */
121 static OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
122
123 static OPJ_BOOL opj_dwt_encode_procedure( opj_tcd_tilecomp_t * tilec,
124 void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
125
126 static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i);
127
128 /* <summary> */
129 /* Inverse 9-7 wavelet transform in 1-D. */
130 /* </summary> */
131 static void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt);
132
133 static void opj_v4dwt_interleave_h(opj_v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
134
135 static void opj_v4dwt_interleave_v(opj_v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
136
137 #ifdef __SSE__
138 static void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c);
139
140 static void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
141
142 #else
143 static void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
144
145 static void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
146
147 #endif
148
149 /*@}*/
150
151 /*@}*/
152
153 #define OPJ_S(i) a[(i)*2]
154 #define OPJ_D(i) a[(1+(i)*2)]
155 #define OPJ_S_(i) ((i)<0?OPJ_S(0):((i)>=sn?OPJ_S(sn-1):OPJ_S(i)))
156 #define OPJ_D_(i) ((i)<0?OPJ_D(0):((i)>=dn?OPJ_D(dn-1):OPJ_D(i)))
157 /* new */
158 #define OPJ_SS_(i) ((i)<0?OPJ_S(0):((i)>=dn?OPJ_S(dn-1):OPJ_S(i)))
159 #define OPJ_DD_(i) ((i)<0?OPJ_D(0):((i)>=sn?OPJ_D(sn-1):OPJ_D(i)))
160
161 /* <summary> */
162 /* This table contains the norms of the 5-3 wavelets for different bands. */
163 /* </summary> */
164 static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
165 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
166 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
167 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
168 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
169 };
170
171 /* <summary> */
172 /* This table contains the norms of the 9-7 wavelets for different bands. */
173 /* </summary> */
174 static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
175 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
176 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
177 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
178 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
179 };
180
181 /*
182 ==========================================================
183 local functions
184 ==========================================================
185 */
186
187 /* <summary> */
188 /* Forward lazy transform (horizontal). */
189 /* </summary> */
opj_dwt_deinterleave_h(OPJ_INT32 * a,OPJ_INT32 * b,OPJ_INT32 dn,OPJ_INT32 sn,OPJ_INT32 cas)190 void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
191 OPJ_INT32 i;
192 OPJ_INT32 * l_dest = b;
193 OPJ_INT32 * l_src = a+cas;
194
195 for (i=0; i<sn; ++i) {
196 *l_dest++ = *l_src;
197 l_src += 2;
198 }
199
200 l_dest = b + sn;
201 l_src = a + 1 - cas;
202
203 for (i=0; i<dn; ++i) {
204 *l_dest++=*l_src;
205 l_src += 2;
206 }
207 }
208
209 /* <summary> */
210 /* Forward lazy transform (vertical). */
211 /* </summary> */
opj_dwt_deinterleave_v(OPJ_INT32 * a,OPJ_INT32 * b,OPJ_INT32 dn,OPJ_INT32 sn,OPJ_INT32 x,OPJ_INT32 cas)212 void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
213 OPJ_INT32 i = sn;
214 OPJ_INT32 * l_dest = b;
215 OPJ_INT32 * l_src = a+cas;
216
217 while (i--) {
218 *l_dest = *l_src;
219 l_dest += x;
220 l_src += 2;
221 } /* b[i*x]=a[2*i+cas]; */
222
223 l_dest = b + sn * x;
224 l_src = a + 1 - cas;
225
226 i = dn;
227 while (i--) {
228 *l_dest = *l_src;
229 l_dest += x;
230 l_src += 2;
231 } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
232 }
233
234 /* <summary> */
235 /* Inverse lazy transform (horizontal). */
236 /* </summary> */
opj_dwt_interleave_h(opj_dwt_t * h,OPJ_INT32 * a)237 void opj_dwt_interleave_h(opj_dwt_t* h, OPJ_INT32 *a) {
238 OPJ_INT32 *ai = a;
239 OPJ_INT32 *bi = h->mem + h->cas;
240 OPJ_INT32 i = h->sn;
241 while( i-- ) {
242 *bi = *(ai++);
243 bi += 2;
244 }
245 ai = a + h->sn;
246 bi = h->mem + 1 - h->cas;
247 i = h->dn ;
248 while( i-- ) {
249 *bi = *(ai++);
250 bi += 2;
251 }
252 }
253
254 /* <summary> */
255 /* Inverse lazy transform (vertical). */
256 /* </summary> */
opj_dwt_interleave_v(opj_dwt_t * v,OPJ_INT32 * a,OPJ_INT32 x)257 void opj_dwt_interleave_v(opj_dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
258 OPJ_INT32 *ai = a;
259 OPJ_INT32 *bi = v->mem + v->cas;
260 OPJ_INT32 i = v->sn;
261 while( i-- ) {
262 *bi = *ai;
263 bi += 2;
264 ai += x;
265 }
266 ai = a + (v->sn * x);
267 bi = v->mem + 1 - v->cas;
268 i = v->dn ;
269 while( i-- ) {
270 *bi = *ai;
271 bi += 2;
272 ai += x;
273 }
274 }
275
276
277 /* <summary> */
278 /* Forward 5-3 wavelet transform in 1-D. */
279 /* </summary> */
opj_dwt_encode_1(OPJ_INT32 * a,OPJ_INT32 dn,OPJ_INT32 sn,OPJ_INT32 cas)280 void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
281 OPJ_INT32 i;
282
283 if (!cas) {
284 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
285 for (i = 0; i < dn; i++) OPJ_D(i) -= (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
286 for (i = 0; i < sn; i++) OPJ_S(i) += (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
287 }
288 } else {
289 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
290 OPJ_S(0) *= 2;
291 else {
292 for (i = 0; i < dn; i++) OPJ_S(i) -= (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
293 for (i = 0; i < sn; i++) OPJ_D(i) += (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
294 }
295 }
296 }
297
298 /* <summary> */
299 /* Inverse 5-3 wavelet transform in 1-D. */
300 /* </summary> */
opj_dwt_decode_1_(OPJ_INT32 * a,OPJ_INT32 dn,OPJ_INT32 sn,OPJ_INT32 cas)301 void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
302 OPJ_INT32 i;
303
304 if (!cas) {
305 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
306 for (i = 0; i < sn; i++) OPJ_S(i) -= (OPJ_D_(i - 1) + OPJ_D_(i) + 2) >> 2;
307 for (i = 0; i < dn; i++) OPJ_D(i) += (OPJ_S_(i) + OPJ_S_(i + 1)) >> 1;
308 }
309 } else {
310 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
311 OPJ_S(0) /= 2;
312 else {
313 for (i = 0; i < sn; i++) OPJ_D(i) -= (OPJ_SS_(i) + OPJ_SS_(i + 1) + 2) >> 2;
314 for (i = 0; i < dn; i++) OPJ_S(i) += (OPJ_DD_(i) + OPJ_DD_(i - 1)) >> 1;
315 }
316 }
317 }
318
319 /* <summary> */
320 /* Inverse 5-3 wavelet transform in 1-D. */
321 /* </summary> */
opj_dwt_decode_1(opj_dwt_t * v)322 void opj_dwt_decode_1(opj_dwt_t *v) {
323 opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
324 }
325
326 /* <summary> */
327 /* Forward 9-7 wavelet transform in 1-D. */
328 /* </summary> */
opj_dwt_encode_1_real(OPJ_INT32 * a,OPJ_INT32 dn,OPJ_INT32 sn,OPJ_INT32 cas)329 void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
330 OPJ_INT32 i;
331 if (!cas) {
332 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
333 for (i = 0; i < dn; i++)
334 OPJ_D(i) -= opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 12993);
335 for (i = 0; i < sn; i++)
336 OPJ_S(i) -= opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 434);
337 for (i = 0; i < dn; i++)
338 OPJ_D(i) += opj_int_fix_mul(OPJ_S_(i) + OPJ_S_(i + 1), 7233);
339 for (i = 0; i < sn; i++)
340 OPJ_S(i) += opj_int_fix_mul(OPJ_D_(i - 1) + OPJ_D_(i), 3633);
341 for (i = 0; i < dn; i++)
342 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 5038); /*5038 */
343 for (i = 0; i < sn; i++)
344 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 6659); /*6660 */
345 }
346 } else {
347 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
348 for (i = 0; i < dn; i++)
349 OPJ_S(i) -= opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 12993);
350 for (i = 0; i < sn; i++)
351 OPJ_D(i) -= opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 434);
352 for (i = 0; i < dn; i++)
353 OPJ_S(i) += opj_int_fix_mul(OPJ_DD_(i) + OPJ_DD_(i - 1), 7233);
354 for (i = 0; i < sn; i++)
355 OPJ_D(i) += opj_int_fix_mul(OPJ_SS_(i) + OPJ_SS_(i + 1), 3633);
356 for (i = 0; i < dn; i++)
357 OPJ_S(i) = opj_int_fix_mul(OPJ_S(i), 5038); /*5038 */
358 for (i = 0; i < sn; i++)
359 OPJ_D(i) = opj_int_fix_mul(OPJ_D(i), 6659); /*6660 */
360 }
361 }
362 }
363
opj_dwt_encode_stepsize(OPJ_INT32 stepsize,OPJ_INT32 numbps,opj_stepsize_t * bandno_stepsize)364 void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
365 OPJ_INT32 p, n;
366 p = opj_int_floorlog2(stepsize) - 13;
367 n = 11 - opj_int_floorlog2(stepsize);
368 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
369 bandno_stepsize->expn = numbps - p;
370 }
371
372 /*
373 ==========================================================
374 DWT interface
375 ==========================================================
376 */
377
378
379 /* <summary> */
380 /* Forward 5-3 wavelet transform in 2-D. */
381 /* </summary> */
opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (* p_function)(OPJ_INT32 *,OPJ_INT32,OPJ_INT32,OPJ_INT32))382 INLINE OPJ_BOOL opj_dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
383 {
384 OPJ_INT32 i, j, k;
385 OPJ_INT32 *a = 00;
386 OPJ_INT32 *aj = 00;
387 OPJ_INT32 *bj = 00;
388 OPJ_INT32 w, l;
389
390 OPJ_INT32 rw; /* width of the resolution level computed */
391 OPJ_INT32 rh; /* height of the resolution level computed */
392 OPJ_UINT32 l_data_size;
393
394 opj_tcd_resolution_t * l_cur_res = 0;
395 opj_tcd_resolution_t * l_last_res = 0;
396
397 w = tilec->x1-tilec->x0;
398 l = (OPJ_INT32)tilec->numresolutions-1;
399 a = tilec->data;
400
401 l_cur_res = tilec->resolutions + l;
402 l_last_res = l_cur_res - 1;
403
404 l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * (OPJ_UINT32)sizeof(OPJ_INT32);
405 bj = (OPJ_INT32*)opj_malloc((size_t)l_data_size);
406 if (! bj) {
407 return OPJ_FALSE;
408 }
409 i = l;
410
411 while (i--) {
412 OPJ_INT32 rw1; /* width of the resolution level once lower than computed one */
413 OPJ_INT32 rh1; /* height of the resolution level once lower than computed one */
414 OPJ_INT32 cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
415 OPJ_INT32 cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
416 OPJ_INT32 dn, sn;
417
418 rw = l_cur_res->x1 - l_cur_res->x0;
419 rh = l_cur_res->y1 - l_cur_res->y0;
420 rw1 = l_last_res->x1 - l_last_res->x0;
421 rh1 = l_last_res->y1 - l_last_res->y0;
422
423 cas_row = l_cur_res->x0 & 1;
424 cas_col = l_cur_res->y0 & 1;
425
426 sn = rh1;
427 dn = rh - rh1;
428 for (j = 0; j < rw; ++j) {
429 aj = a + j;
430 for (k = 0; k < rh; ++k) {
431 bj[k] = aj[k*w];
432 }
433
434 (*p_function) (bj, dn, sn, cas_col);
435
436 opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
437 }
438
439 sn = rw1;
440 dn = rw - rw1;
441
442 for (j = 0; j < rh; j++) {
443 aj = a + j * w;
444 for (k = 0; k < rw; k++) bj[k] = aj[k];
445 (*p_function) (bj, dn, sn, cas_row);
446 opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
447 }
448
449 l_cur_res = l_last_res;
450
451 --l_last_res;
452 }
453
454 opj_free(bj);
455 return OPJ_TRUE;
456 }
457
458 /* Forward 5-3 wavelet transform in 2-D. */
459 /* </summary> */
opj_dwt_encode(opj_tcd_tilecomp_t * tilec)460 OPJ_BOOL opj_dwt_encode(opj_tcd_tilecomp_t * tilec)
461 {
462 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
463 }
464
465 /* <summary> */
466 /* Inverse 5-3 wavelet transform in 2-D. */
467 /* </summary> */
opj_dwt_decode(opj_tcd_tilecomp_t * tilec,OPJ_UINT32 numres)468 OPJ_BOOL opj_dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
469 return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
470 }
471
472
473 /* <summary> */
474 /* Get gain of 5-3 wavelet transform. */
475 /* </summary> */
opj_dwt_getgain(OPJ_UINT32 orient)476 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
477 if (orient == 0)
478 return 0;
479 if (orient == 1 || orient == 2)
480 return 1;
481 return 2;
482 }
483
484 /* <summary> */
485 /* Get norm of 5-3 wavelet. */
486 /* </summary> */
opj_dwt_getnorm(OPJ_UINT32 level,OPJ_UINT32 orient)487 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
488 return opj_dwt_norms[orient][level];
489 }
490
491 /* <summary> */
492 /* Forward 9-7 wavelet transform in 2-D. */
493 /* </summary> */
opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)494 OPJ_BOOL opj_dwt_encode_real(opj_tcd_tilecomp_t * tilec)
495 {
496 return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
497 }
498
499 /* <summary> */
500 /* Get gain of 9-7 wavelet transform. */
501 /* </summary> */
opj_dwt_getgain_real(OPJ_UINT32 orient)502 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
503 (void)orient;
504 return 0;
505 }
506
507 /* <summary> */
508 /* Get norm of 9-7 wavelet. */
509 /* </summary> */
opj_dwt_getnorm_real(OPJ_UINT32 level,OPJ_UINT32 orient)510 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
511 return opj_dwt_norms_real[orient][level];
512 }
513
opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp,OPJ_UINT32 prec)514 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
515 OPJ_UINT32 numbands, bandno;
516 numbands = 3 * tccp->numresolutions - 2;
517 for (bandno = 0; bandno < numbands; bandno++) {
518 OPJ_FLOAT64 stepsize;
519 OPJ_UINT32 resno, level, orient, gain;
520
521 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
522 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
523 level = tccp->numresolutions - 1 - resno;
524 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
525 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
526 stepsize = 1.0;
527 } else {
528 OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
529 stepsize = (1 << (gain)) / norm;
530 }
531 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), (OPJ_INT32)(prec + gain), &tccp->stepsizes[bandno]);
532 }
533 }
534
535 /* <summary> */
536 /* Determine maximum computed resolution level for inverse wavelet transform */
537 /* </summary> */
opj_dwt_max_resolution(opj_tcd_resolution_t * restrict r,OPJ_UINT32 i)538 OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
539 OPJ_UINT32 mr = 0;
540 OPJ_UINT32 w;
541 while( --i ) {
542 ++r;
543 if( mr < ( w = (OPJ_UINT32)(r->x1 - r->x0) ) )
544 mr = w ;
545 if( mr < ( w = (OPJ_UINT32)(r->y1 - r->y0) ) )
546 mr = w ;
547 }
548 return mr ;
549 }
550
551 /* <summary> */
552 /* Inverse wavelet transform in 2-D. */
553 /* </summary> */
opj_dwt_decode_tile(opj_tcd_tilecomp_t * tilec,OPJ_UINT32 numres,DWT1DFN dwt_1D)554 OPJ_BOOL opj_dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
555 opj_dwt_t h;
556 opj_dwt_t v;
557
558 opj_tcd_resolution_t* tr = tilec->resolutions;
559
560 OPJ_UINT32 rw = (OPJ_UINT32)(tr->x1 - tr->x0); /* width of the resolution level computed */
561 OPJ_UINT32 rh = (OPJ_UINT32)(tr->y1 - tr->y0); /* height of the resolution level computed */
562
563 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
564
565 h.mem = (OPJ_INT32*)
566 opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
567 if (! h.mem){
568 return OPJ_FALSE;
569 }
570
571 v.mem = h.mem;
572
573 while( --numres) {
574 OPJ_INT32 * restrict tiledp = tilec->data;
575 OPJ_UINT32 j;
576
577 ++tr;
578 h.sn = (OPJ_INT32)rw;
579 v.sn = (OPJ_INT32)rh;
580
581 rw = (OPJ_UINT32)(tr->x1 - tr->x0);
582 rh = (OPJ_UINT32)(tr->y1 - tr->y0);
583
584 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
585 h.cas = tr->x0 % 2;
586
587 for(j = 0; j < rh; ++j) {
588 opj_dwt_interleave_h(&h, &tiledp[j*w]);
589 (dwt_1D)(&h);
590 memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
591 }
592
593 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
594 v.cas = tr->y0 % 2;
595
596 for(j = 0; j < rw; ++j){
597 OPJ_UINT32 k;
598 opj_dwt_interleave_v(&v, &tiledp[j], (OPJ_INT32)w);
599 (dwt_1D)(&v);
600 for(k = 0; k < rh; ++k) {
601 tiledp[k * w + j] = v.mem[k];
602 }
603 }
604 }
605 opj_aligned_free(h.mem);
606 return OPJ_TRUE;
607 }
608
opj_v4dwt_interleave_h(opj_v4dwt_t * restrict w,OPJ_FLOAT32 * restrict a,OPJ_INT32 x,OPJ_INT32 size)609 void opj_v4dwt_interleave_h(opj_v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
610 OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
611 OPJ_INT32 count = w->sn;
612 OPJ_INT32 i, k;
613
614 for(k = 0; k < 2; ++k){
615 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
616 /* Fast code path */
617 for(i = 0; i < count; ++i){
618 OPJ_INT32 j = i;
619 bi[i*8 ] = a[j];
620 j += x;
621 bi[i*8 + 1] = a[j];
622 j += x;
623 bi[i*8 + 2] = a[j];
624 j += x;
625 bi[i*8 + 3] = a[j];
626 }
627 }
628 else {
629 /* Slow code path */
630 for(i = 0; i < count; ++i){
631 OPJ_INT32 j = i;
632 bi[i*8 ] = a[j];
633 j += x;
634 if(j >= size) continue;
635 bi[i*8 + 1] = a[j];
636 j += x;
637 if(j >= size) continue;
638 bi[i*8 + 2] = a[j];
639 j += x;
640 if(j >= size) continue;
641 bi[i*8 + 3] = a[j]; /* This one*/
642 }
643 }
644
645 bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas);
646 a += w->sn;
647 size -= w->sn;
648 count = w->dn;
649 }
650 }
651
opj_v4dwt_interleave_v(opj_v4dwt_t * restrict v,OPJ_FLOAT32 * restrict a,OPJ_INT32 x,OPJ_INT32 nb_elts_read)652 void opj_v4dwt_interleave_v(opj_v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
653 opj_v4_t* restrict bi = v->wavelet + v->cas;
654 OPJ_INT32 i;
655
656 for(i = 0; i < v->sn; ++i){
657 memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
658 }
659
660 a += v->sn * x;
661 bi = v->wavelet + 1 - v->cas;
662
663 for(i = 0; i < v->dn; ++i){
664 memcpy(&bi[i*2], &a[i*x], (size_t)nb_elts_read * sizeof(OPJ_FLOAT32));
665 }
666 }
667
668 #ifdef __SSE__
669
opj_v4dwt_decode_step1_sse(opj_v4_t * w,OPJ_INT32 count,const __m128 c)670 void opj_v4dwt_decode_step1_sse(opj_v4_t* w, OPJ_INT32 count, const __m128 c){
671 __m128* restrict vw = (__m128*) w;
672 OPJ_INT32 i;
673 /* 4x unrolled loop */
674 for(i = 0; i < count >> 2; ++i){
675 *vw = _mm_mul_ps(*vw, c);
676 vw += 2;
677 *vw = _mm_mul_ps(*vw, c);
678 vw += 2;
679 *vw = _mm_mul_ps(*vw, c);
680 vw += 2;
681 *vw = _mm_mul_ps(*vw, c);
682 vw += 2;
683 }
684 count &= 3;
685 for(i = 0; i < count; ++i){
686 *vw = _mm_mul_ps(*vw, c);
687 vw += 2;
688 }
689 }
690
opj_v4dwt_decode_step2_sse(opj_v4_t * l,opj_v4_t * w,OPJ_INT32 k,OPJ_INT32 m,__m128 c)691 void opj_v4dwt_decode_step2_sse(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){
692 __m128* restrict vl = (__m128*) l;
693 __m128* restrict vw = (__m128*) w;
694 OPJ_INT32 i;
695 __m128 tmp1, tmp2, tmp3;
696 tmp1 = vl[0];
697 for(i = 0; i < m; ++i){
698 tmp2 = vw[-1];
699 tmp3 = vw[ 0];
700 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
701 tmp1 = tmp3;
702 vw += 2;
703 }
704 vl = vw - 2;
705 if(m >= k){
706 return;
707 }
708 c = _mm_add_ps(c, c);
709 c = _mm_mul_ps(c, vl[0]);
710 for(; m < k; ++m){
711 __m128 tmp = vw[-1];
712 vw[-1] = _mm_add_ps(tmp, c);
713 vw += 2;
714 }
715 }
716
717 #else
718
opj_v4dwt_decode_step1(opj_v4_t * w,OPJ_INT32 count,const OPJ_FLOAT32 c)719 void opj_v4dwt_decode_step1(opj_v4_t* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
720 {
721 OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
722 OPJ_INT32 i;
723 for(i = 0; i < count; ++i){
724 OPJ_FLOAT32 tmp1 = fw[i*8 ];
725 OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
726 OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
727 OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
728 fw[i*8 ] = tmp1 * c;
729 fw[i*8 + 1] = tmp2 * c;
730 fw[i*8 + 2] = tmp3 * c;
731 fw[i*8 + 3] = tmp4 * c;
732 }
733 }
734
opj_v4dwt_decode_step2(opj_v4_t * l,opj_v4_t * w,OPJ_INT32 k,OPJ_INT32 m,OPJ_FLOAT32 c)735 void opj_v4dwt_decode_step2(opj_v4_t* l, opj_v4_t* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
736 {
737 OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
738 OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
739 OPJ_INT32 i;
740 for(i = 0; i < m; ++i){
741 OPJ_FLOAT32 tmp1_1 = fl[0];
742 OPJ_FLOAT32 tmp1_2 = fl[1];
743 OPJ_FLOAT32 tmp1_3 = fl[2];
744 OPJ_FLOAT32 tmp1_4 = fl[3];
745 OPJ_FLOAT32 tmp2_1 = fw[-4];
746 OPJ_FLOAT32 tmp2_2 = fw[-3];
747 OPJ_FLOAT32 tmp2_3 = fw[-2];
748 OPJ_FLOAT32 tmp2_4 = fw[-1];
749 OPJ_FLOAT32 tmp3_1 = fw[0];
750 OPJ_FLOAT32 tmp3_2 = fw[1];
751 OPJ_FLOAT32 tmp3_3 = fw[2];
752 OPJ_FLOAT32 tmp3_4 = fw[3];
753 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
754 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
755 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
756 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
757 fl = fw;
758 fw += 8;
759 }
760 if(m < k){
761 OPJ_FLOAT32 c1;
762 OPJ_FLOAT32 c2;
763 OPJ_FLOAT32 c3;
764 OPJ_FLOAT32 c4;
765 c += c;
766 c1 = fl[0] * c;
767 c2 = fl[1] * c;
768 c3 = fl[2] * c;
769 c4 = fl[3] * c;
770 for(; m < k; ++m){
771 OPJ_FLOAT32 tmp1 = fw[-4];
772 OPJ_FLOAT32 tmp2 = fw[-3];
773 OPJ_FLOAT32 tmp3 = fw[-2];
774 OPJ_FLOAT32 tmp4 = fw[-1];
775 fw[-4] = tmp1 + c1;
776 fw[-3] = tmp2 + c2;
777 fw[-2] = tmp3 + c3;
778 fw[-1] = tmp4 + c4;
779 fw += 8;
780 }
781 }
782 }
783
784 #endif
785
786 /* <summary> */
787 /* Inverse 9-7 wavelet transform in 1-D. */
788 /* </summary> */
opj_v4dwt_decode(opj_v4dwt_t * restrict dwt)789 void opj_v4dwt_decode(opj_v4dwt_t* restrict dwt)
790 {
791 OPJ_INT32 a, b;
792 if(dwt->cas == 0) {
793 if(!((dwt->dn > 0) || (dwt->sn > 1))){
794 return;
795 }
796 a = 0;
797 b = 1;
798 }else{
799 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
800 return;
801 }
802 a = 1;
803 b = 0;
804 }
805 #ifdef __SSE__
806 opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
807 opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
808 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_delta));
809 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_gamma));
810 opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_beta));
811 opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_alpha));
812 #else
813 opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
814 opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
815 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
816 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
817 opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
818 opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
819 #endif
820 }
821
822
823 /* <summary> */
824 /* Inverse 9-7 wavelet transform in 2-D. */
825 /* </summary> */
opj_dwt_decode_real(opj_tcd_tilecomp_t * restrict tilec,OPJ_UINT32 numres)826 OPJ_BOOL opj_dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres)
827 {
828 opj_v4dwt_t h;
829 opj_v4dwt_t v;
830
831 opj_tcd_resolution_t* res = tilec->resolutions;
832
833 OPJ_UINT32 rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
834 OPJ_UINT32 rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
835
836 OPJ_UINT32 w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
837
838 h.wavelet = (opj_v4_t*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(opj_v4_t));
839 v.wavelet = h.wavelet;
840
841 while( --numres) {
842 OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
843 OPJ_UINT32 bufsize = (OPJ_UINT32)((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0));
844 OPJ_INT32 j;
845
846 h.sn = (OPJ_INT32)rw;
847 v.sn = (OPJ_INT32)rh;
848
849 ++res;
850
851 rw = (OPJ_UINT32)(res->x1 - res->x0); /* width of the resolution level computed */
852 rh = (OPJ_UINT32)(res->y1 - res->y0); /* height of the resolution level computed */
853
854 h.dn = (OPJ_INT32)(rw - (OPJ_UINT32)h.sn);
855 h.cas = res->x0 % 2;
856
857 for(j = (OPJ_INT32)rh; j > 3; j -= 4) {
858 OPJ_INT32 k;
859 opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
860 opj_v4dwt_decode(&h);
861
862 for(k = (OPJ_INT32)rw; --k >= 0;){
863 aj[k ] = h.wavelet[k].f[0];
864 aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
865 aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
866 aj[k+(OPJ_INT32)w*3] = h.wavelet[k].f[3];
867 }
868
869 aj += w*4;
870 bufsize -= w*4;
871 }
872
873 if (rh & 0x03) {
874 OPJ_INT32 k;
875 j = rh & 0x03;
876 opj_v4dwt_interleave_h(&h, aj, (OPJ_INT32)w, (OPJ_INT32)bufsize);
877 opj_v4dwt_decode(&h);
878 for(k = (OPJ_INT32)rw; --k >= 0;){
879 switch(j) {
880 case 3: aj[k+(OPJ_INT32)w*2] = h.wavelet[k].f[2];
881 case 2: aj[k+(OPJ_INT32)w ] = h.wavelet[k].f[1];
882 case 1: aj[k ] = h.wavelet[k].f[0];
883 }
884 }
885 }
886
887 v.dn = (OPJ_INT32)(rh - (OPJ_UINT32)v.sn);
888 v.cas = res->y0 % 2;
889
890 aj = (OPJ_FLOAT32*) tilec->data;
891 for(j = (OPJ_INT32)rw; j > 3; j -= 4){
892 OPJ_UINT32 k;
893
894 opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, 4);
895 opj_v4dwt_decode(&v);
896
897 for(k = 0; k < rh; ++k){
898 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
899 }
900 aj += 4;
901 }
902
903 if (rw & 0x03){
904 OPJ_UINT32 k;
905
906 j = rw & 0x03;
907
908 opj_v4dwt_interleave_v(&v, aj, (OPJ_INT32)w, j);
909 opj_v4dwt_decode(&v);
910
911 for(k = 0; k < rh; ++k){
912 memcpy(&aj[k*w], &v.wavelet[k], (size_t)j * sizeof(OPJ_FLOAT32));
913 }
914 }
915 }
916
917 opj_aligned_free(h.wavelet);
918 return OPJ_TRUE;
919 }
920