[trunk] Finalize dwt.v/.h with the v2 style : remove last functions already optimized...
[openjpeg.git] / src / lib / openjp2 / dwt.c
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 WS(i) v->mem[(i)*2]
44 #define 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 } dwt_t;
55
56 typedef union {
57         OPJ_FLOAT32     f[4];
58 } v4;
59
60 typedef struct v4dwt_local {
61         v4*     wavelet ;
62         OPJ_INT32               dn ;
63         OPJ_INT32               sn ;
64         OPJ_INT32               cas ;
65 } 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)(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(dwt_t* h, OPJ_INT32 *a);
97 /**
98 Inverse lazy transform (vertical)
99 */
100 static void opj_dwt_interleave_v(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(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_v2_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
122
123 static opj_bool opj_dwt_encode_procedure(       opj_tcd_tilecomp_v2_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_v2_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(v4dwt_t* restrict dwt);
132
133 static void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
134
135 static void opj_v4dwt_interleave_v(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(v4* w, OPJ_INT32 count, const __m128 c);
139
140 static void opj_v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
141
142 #else
143 static void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
144
145 static void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
146
147 #endif
148
149 /*@}*/
150
151 /*@}*/
152
153 #define S(i) a[(i)*2]
154 #define D(i) a[(1+(i)*2)]
155 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
156 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
157 /* new */
158 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
159 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):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>                            */ 
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>                            */ 
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>                            */
237 void opj_dwt_interleave_h(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>                            */ 
257 void opj_dwt_interleave_v(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>                           */
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++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
286                         for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
287                 }
288         } else {
289                 if (!sn && dn == 1)                 /* NEW :  CASE ONE ELEMENT */
290                         S(0) *= 2;
291                 else {
292                         for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
293                         for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
294                 }
295         }
296 }
297
298 /* <summary>                            */
299 /* Inverse 5-3 wavelet transform in 1-D. */
300 /* </summary>                           */ 
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++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
307                         for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
308                 }
309         } else {
310                 if (!sn  && dn == 1)          /* NEW :  CASE ONE ELEMENT */
311                         S(0) /= 2;
312                 else {
313                         for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
314                         for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
315                 }
316         }
317 }
318
319 /* <summary>                            */
320 /* Inverse 5-3 wavelet transform in 1-D. */
321 /* </summary>                           */ 
322 void opj_dwt_decode_1(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>                            */
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                                 D(i) -= opj_int_fix_mul(S_(i) + S_(i + 1), 12993);
335                         for (i = 0; i < sn; i++)
336                                 S(i) -= opj_int_fix_mul(D_(i - 1) + D_(i), 434);
337                         for (i = 0; i < dn; i++)
338                                 D(i) += opj_int_fix_mul(S_(i) + S_(i + 1), 7233);
339                         for (i = 0; i < sn; i++)
340                                 S(i) += opj_int_fix_mul(D_(i - 1) + D_(i), 3633);
341                         for (i = 0; i < dn; i++)
342                                 D(i) = opj_int_fix_mul(D(i), 5038);     /*5038 */
343                         for (i = 0; i < sn; i++)
344                                 S(i) = opj_int_fix_mul(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                                 S(i) -= opj_int_fix_mul(DD_(i) + DD_(i - 1), 12993);
350                         for (i = 0; i < sn; i++)
351                                 D(i) -= opj_int_fix_mul(SS_(i) + SS_(i + 1), 434);
352                         for (i = 0; i < dn; i++)
353                                 S(i) += opj_int_fix_mul(DD_(i) + DD_(i - 1), 7233);
354                         for (i = 0; i < sn; i++)
355                                 D(i) += opj_int_fix_mul(SS_(i) + SS_(i + 1), 3633);
356                         for (i = 0; i < dn; i++)
357                                 S(i) = opj_int_fix_mul(S(i), 5038);     /*5038 */
358                         for (i = 0; i < sn; i++)
359                                 D(i) = opj_int_fix_mul(D(i), 6659);     /*6660 */
360                 }
361         }
362 }
363
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>                           */
382 INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_v2_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_INT32 l_data_size;
393
394         opj_tcd_resolution_v2_t * l_cur_res = 0;
395         opj_tcd_resolution_v2_t * l_last_res = 0;
396
397         w = tilec->x1-tilec->x0;
398         l = 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         rw = l_cur_res->x1 - l_cur_res->x0;
405         rh = l_cur_res->y1 - l_cur_res->y0;
406
407         l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
408         bj = (OPJ_INT32*)opj_malloc(l_data_size);
409         if (! bj) {
410                 return OPJ_FALSE;
411         }
412         i = l;
413
414         while (i--) {
415                 OPJ_INT32 rw1;          /* width of the resolution level once lower than computed one                                       */
416                 OPJ_INT32 rh1;          /* height of the resolution level once lower than computed one                                      */
417                 OPJ_INT32 cas_col;      /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
418                 OPJ_INT32 cas_row;      /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
419                 OPJ_INT32 dn, sn;
420
421                 rw  = l_cur_res->x1 - l_cur_res->x0;
422                 rh  = l_cur_res->y1 - l_cur_res->y0;
423                 rw1 = l_last_res->x1 - l_last_res->x0;
424                 rh1 = l_last_res->y1 - l_last_res->y0;
425
426                 cas_row = l_cur_res->x0 & 1;
427                 cas_col = l_cur_res->y0 & 1;
428
429                 sn = rh1;
430                 dn = rh - rh1;
431                 for (j = 0; j < rw; ++j) {
432                         aj = a + j;
433                         for (k = 0; k < rh; ++k) {
434                                 bj[k] = aj[k*w];
435                         }
436
437                         (*p_function) (bj, dn, sn, cas_col);
438
439                         opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
440                 }
441
442                 sn = rw1;
443                 dn = rw - rw1;
444
445                 for (j = 0; j < rh; j++) {
446                         aj = a + j * w;
447                         for (k = 0; k < rw; k++)  bj[k] = aj[k];
448                         (*p_function) (bj, dn, sn, cas_row);
449                         opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
450                 }
451
452                 l_cur_res = l_last_res;
453
454                 --l_last_res;
455         }
456
457         opj_free(bj);
458         return OPJ_TRUE;
459 }
460
461 /* Forward 5-3 wavelet transform in 2-D. */
462 /* </summary>                           */
463 opj_bool opj_dwt_encode(opj_tcd_tilecomp_v2_t * tilec)
464 {
465         return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
466 }
467
468 /* <summary>                            */
469 /* Inverse 5-3 wavelet transform in 2-D. */
470 /* </summary>                           */
471 opj_bool opj_dwt_decode(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
472         return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
473 }
474
475
476 /* <summary>                          */
477 /* Get gain of 5-3 wavelet transform. */
478 /* </summary>                         */
479 OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
480         if (orient == 0)
481                 return 0;
482         if (orient == 1 || orient == 2)
483                 return 1;
484         return 2;
485 }
486
487 /* <summary>                */
488 /* Get norm of 5-3 wavelet. */
489 /* </summary>               */
490 OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
491         return opj_dwt_norms[orient][level];
492 }
493
494 /* <summary>                             */
495 /* Forward 9-7 wavelet transform in 2-D. */
496 /* </summary>                            */
497 opj_bool opj_dwt_encode_real(opj_tcd_tilecomp_v2_t * tilec)
498 {
499         return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
500 }
501
502 /* <summary>                          */
503 /* Get gain of 9-7 wavelet transform. */
504 /* </summary>                         */
505 OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
506         (void)orient;
507         return 0;
508 }
509
510 /* <summary>                */
511 /* Get norm of 9-7 wavelet. */
512 /* </summary>               */
513 OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
514         return opj_dwt_norms_real[orient][level];
515 }
516
517 void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
518         OPJ_UINT32 numbands, bandno;
519         numbands = 3 * tccp->numresolutions - 2;
520         for (bandno = 0; bandno < numbands; bandno++) {
521                 OPJ_FLOAT64 stepsize;
522                 OPJ_UINT32 resno, level, orient, gain;
523
524                 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
525                 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
526                 level = tccp->numresolutions - 1 - resno;
527                 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
528                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
529                         stepsize = 1.0;
530                 } else {
531                         OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
532                         stepsize = (1 << (gain)) / norm;
533                 }
534                 opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
535         }
536 }
537
538 /* <summary>                             */
539 /* Determine maximum computed resolution level for inverse wavelet transform */
540 /* </summary>                            */
541 OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i) {
542         OPJ_UINT32 mr   = 0;
543         OPJ_UINT32 w;
544         while( --i ) {
545                 ++r;
546                 if( mr < ( w = r->x1 - r->x0 ) )
547                         mr = w ;
548                 if( mr < ( w = r->y1 - r->y0 ) )
549                         mr = w ;
550         }
551         return mr ;
552 }
553
554 /* <summary>                            */
555 /* Inverse wavelet transform in 2-D.     */
556 /* </summary>                           */
557 opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
558         dwt_t h;
559         dwt_t v;
560
561         opj_tcd_resolution_v2_t* tr = tilec->resolutions;
562
563         OPJ_UINT32 rw = tr->x1 - tr->x0;        /* width of the resolution level computed */
564         OPJ_UINT32 rh = tr->y1 - tr->y0;        /* height of the resolution level computed */
565
566         OPJ_UINT32 w = tilec->x1 - tilec->x0;
567
568         h.mem = (OPJ_INT32*)
569         opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
570         if (! h.mem){
571                 return OPJ_FALSE;
572         }
573
574         v.mem = h.mem;
575
576         while( --numres) {
577                 OPJ_INT32 * restrict tiledp = tilec->data;
578                 OPJ_UINT32 j;
579
580                 ++tr;
581                 h.sn = rw;
582                 v.sn = rh;
583
584                 rw = tr->x1 - tr->x0;
585                 rh = tr->y1 - tr->y0;
586
587                 h.dn = rw - h.sn;
588                 h.cas = tr->x0 % 2;
589
590                 for(j = 0; j < rh; ++j) {
591                         opj_dwt_interleave_h(&h, &tiledp[j*w]);
592                         (dwt_1D)(&h);
593                         memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
594                 }
595
596                 v.dn = rh - v.sn;
597                 v.cas = tr->y0 % 2;
598
599                 for(j = 0; j < rw; ++j){
600                         OPJ_UINT32 k;
601                         opj_dwt_interleave_v(&v, &tiledp[j], w);
602                         (dwt_1D)(&v);
603                         for(k = 0; k < rh; ++k) {
604                                 tiledp[k * w + j] = v.mem[k];
605                         }
606                 }
607         }
608         opj_aligned_free(h.mem);
609         return OPJ_TRUE;
610 }
611
612 void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
613         OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
614         OPJ_INT32 count = w->sn;
615         OPJ_INT32 i, k;
616
617         for(k = 0; k < 2; ++k){
618                 if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
619                         /* Fast code path */
620                         for(i = 0; i < count; ++i){
621                                 OPJ_INT32 j = i;
622                                 bi[i*8    ] = a[j];
623                                 j += x;
624                                 bi[i*8 + 1] = a[j];
625                                 j += x;
626                                 bi[i*8 + 2] = a[j];
627                                 j += x;
628                                 bi[i*8 + 3] = a[j];
629                         }
630                 }
631                 else {
632                         /* Slow code path */
633                         for(i = 0; i < count; ++i){
634                                 OPJ_INT32 j = i;
635                                 bi[i*8    ] = a[j];
636                                 j += x;
637                                 if(j >= size) continue;
638                                 bi[i*8 + 1] = a[j];
639                                 j += x;
640                                 if(j >= size) continue;
641                                 bi[i*8 + 2] = a[j];
642                                 j += x;
643                                 if(j >= size) continue;
644                                 bi[i*8 + 3] = a[j]; /* This one*/
645                         }
646                 }
647
648                 bi = (float*) (w->wavelet + 1 - w->cas);
649                 a += w->sn;
650                 size -= w->sn;
651                 count = w->dn;
652         }
653 }
654
655 void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
656         v4* restrict bi = v->wavelet + v->cas;
657         OPJ_INT32 i;
658
659         for(i = 0; i < v->sn; ++i){
660                 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
661         }
662
663         a += v->sn * x;
664         bi = v->wavelet + 1 - v->cas;
665
666         for(i = 0; i < v->dn; ++i){
667                 memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
668         }
669 }
670
671 #ifdef __SSE__
672
673 void opj_v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
674         __m128* restrict vw = (__m128*) w;
675         OPJ_INT32 i;
676         /* 4x unrolled loop */
677         for(i = 0; i < count >> 2; ++i){
678                 *vw = _mm_mul_ps(*vw, c);
679                 vw += 2;
680                 *vw = _mm_mul_ps(*vw, c);
681                 vw += 2;
682                 *vw = _mm_mul_ps(*vw, c);
683                 vw += 2;
684                 *vw = _mm_mul_ps(*vw, c);
685                 vw += 2;
686         }
687         count &= 3;
688         for(i = 0; i < count; ++i){
689                 *vw = _mm_mul_ps(*vw, c);
690                 vw += 2;
691         }
692 }
693
694 void opj_v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
695         __m128* restrict vl = (__m128*) l;
696         __m128* restrict vw = (__m128*) w;
697         int i;
698         __m128 tmp1, tmp2, tmp3;
699         tmp1 = vl[0];
700         for(i = 0; i < m; ++i){
701                 tmp2 = vw[-1];
702                 tmp3 = vw[ 0];
703                 vw[-1] = _mm_add_ps(tmp2, _mm_mul_ps(_mm_add_ps(tmp1, tmp3), c));
704                 tmp1 = tmp3;
705                 vw += 2;
706         }
707         vl = vw - 2;
708         if(m >= k){
709                 return;
710         }
711         c = _mm_add_ps(c, c);
712         c = _mm_mul_ps(c, vl[0]);
713         for(; m < k; ++m){
714                 __m128 tmp = vw[-1];
715                 vw[-1] = _mm_add_ps(tmp, c);
716                 vw += 2;
717         }
718 }
719
720 #else
721
722 void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
723 {
724         OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
725         OPJ_INT32 i;
726         for(i = 0; i < count; ++i){
727                 OPJ_FLOAT32 tmp1 = fw[i*8    ];
728                 OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
729                 OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
730                 OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
731                 fw[i*8    ] = tmp1 * c;
732                 fw[i*8 + 1] = tmp2 * c;
733                 fw[i*8 + 2] = tmp3 * c;
734                 fw[i*8 + 3] = tmp4 * c;
735         }
736 }
737
738 void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
739 {
740         OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
741         OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
742         int i;
743         for(i = 0; i < m; ++i){
744                 OPJ_FLOAT32 tmp1_1 = fl[0];
745                 OPJ_FLOAT32 tmp1_2 = fl[1];
746                 OPJ_FLOAT32 tmp1_3 = fl[2];
747                 OPJ_FLOAT32 tmp1_4 = fl[3];
748                 OPJ_FLOAT32 tmp2_1 = fw[-4];
749                 OPJ_FLOAT32 tmp2_2 = fw[-3];
750                 OPJ_FLOAT32 tmp2_3 = fw[-2];
751                 OPJ_FLOAT32 tmp2_4 = fw[-1];
752                 OPJ_FLOAT32 tmp3_1 = fw[0];
753                 OPJ_FLOAT32 tmp3_2 = fw[1];
754                 OPJ_FLOAT32 tmp3_3 = fw[2];
755                 OPJ_FLOAT32 tmp3_4 = fw[3];
756                 fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
757                 fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
758                 fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
759                 fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
760                 fl = fw;
761                 fw += 8;
762         }
763         if(m < k){
764                 OPJ_FLOAT32 c1;
765                 OPJ_FLOAT32 c2;
766                 OPJ_FLOAT32 c3;
767                 OPJ_FLOAT32 c4;
768                 c += c;
769                 c1 = fl[0] * c;
770                 c2 = fl[1] * c;
771                 c3 = fl[2] * c;
772                 c4 = fl[3] * c;
773                 for(; m < k; ++m){
774                         OPJ_FLOAT32 tmp1 = fw[-4];
775                         OPJ_FLOAT32 tmp2 = fw[-3];
776                         OPJ_FLOAT32 tmp3 = fw[-2];
777                         OPJ_FLOAT32 tmp4 = fw[-1];
778                         fw[-4] = tmp1 + c1;
779                         fw[-3] = tmp2 + c2;
780                         fw[-2] = tmp3 + c3;
781                         fw[-1] = tmp4 + c4;
782                         fw += 8;
783                 }
784         }
785 }
786
787 #endif
788
789 /* <summary>                             */
790 /* Inverse 9-7 wavelet transform in 1-D. */
791 /* </summary>                            */
792 void opj_v4dwt_decode(v4dwt_t* restrict dwt)
793 {
794         int a, b;
795         if(dwt->cas == 0) {
796                 if(!((dwt->dn > 0) || (dwt->sn > 1))){
797                         return;
798                 }
799                 a = 0;
800                 b = 1;
801         }else{
802                 if(!((dwt->sn > 0) || (dwt->dn > 1))) {
803                         return;
804                 }
805                 a = 1;
806                 b = 0;
807         }
808 #ifdef __SSE__
809         opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
810         opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
811         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));
812         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));
813         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));
814         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));
815 #else
816         opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
817         opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
818         opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
819         opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
820         opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, opj_int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
821         opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, opj_int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
822 #endif
823 }
824
825
826 /* <summary>                             */
827 /* Inverse 9-7 wavelet transform in 2-D. */
828 /* </summary>                            */
829 opj_bool opj_dwt_decode_real(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 numres)
830 {
831         v4dwt_t h;
832         v4dwt_t v;
833
834         opj_tcd_resolution_v2_t* res = tilec->resolutions;
835
836         OPJ_UINT32 rw = res->x1 - res->x0;      /* width of the resolution level computed */
837         OPJ_UINT32 rh = res->y1 - res->y0;      /* height of the resolution level computed */
838
839         OPJ_UINT32 w = tilec->x1 - tilec->x0;
840
841         h.wavelet = (v4*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(v4));
842         v.wavelet = h.wavelet;
843
844         while( --numres) {
845                 OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
846                 OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
847                 OPJ_INT32 j;
848
849                 h.sn = rw;
850                 v.sn = rh;
851
852                 ++res;
853
854                 rw = res->x1 - res->x0; /* width of the resolution level computed */
855                 rh = res->y1 - res->y0; /* height of the resolution level computed */
856
857                 h.dn = rw - h.sn;
858                 h.cas = res->x0 % 2;
859
860                 for(j = rh; j > 3; j -= 4) {
861                         OPJ_INT32 k;
862                         opj_v4dwt_interleave_h(&h, aj, w, bufsize);
863                         opj_v4dwt_decode(&h);
864
865                         for(k = rw; --k >= 0;){
866                                 aj[k    ] = h.wavelet[k].f[0];
867                                 aj[k+w  ] = h.wavelet[k].f[1];
868                                 aj[k+w*2] = h.wavelet[k].f[2];
869                                 aj[k+w*3] = h.wavelet[k].f[3];
870                         }
871
872                         aj += w*4;
873                         bufsize -= w*4;
874                 }
875
876                 if (rh & 0x03) {
877                         OPJ_INT32 k;
878                         j = rh & 0x03;
879                         opj_v4dwt_interleave_h(&h, aj, w, bufsize);
880                         opj_v4dwt_decode(&h);
881                         for(k = rw; --k >= 0;){
882                                 switch(j) {
883                                         case 3: aj[k+w*2] = h.wavelet[k].f[2];
884                                         case 2: aj[k+w  ] = h.wavelet[k].f[1];
885                                         case 1: aj[k    ] = h.wavelet[k].f[0];
886                                 }
887                         }
888                 }
889
890                 v.dn = rh - v.sn;
891                 v.cas = res->y0 % 2;
892
893                 aj = (OPJ_FLOAT32*) tilec->data;
894                 for(j = rw; j > 3; j -= 4){
895                         OPJ_UINT32 k;
896
897                         opj_v4dwt_interleave_v(&v, aj, w, 4);
898                         opj_v4dwt_decode(&v);
899
900                         for(k = 0; k < rh; ++k){
901                                 memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
902                         }
903                         aj += 4;
904                 }
905
906                 if (rw & 0x03){
907                         OPJ_UINT32 k;
908
909                         j = rw & 0x03;
910
911                         opj_v4dwt_interleave_v(&v, aj, w, j);
912                         opj_v4dwt_decode(&v);
913
914                         for(k = 0; k < rh; ++k){
915                                 memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
916                         }
917                 }
918         }
919
920         opj_aligned_free(h.wavelet);
921         return OPJ_TRUE;
922 }