dwt.c 24.9 KB
Newer Older
Julien Michel's avatar
Julien Michel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/*
 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
 * Copyright (c) 2002-2007, Professor Benoit Macq
 * Copyright (c) 2001-2003, David Janssens
 * Copyright (c) 2002-2003, Yannick Verschueren
 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
 * Copyright (c) 2007, Jonathan Ballard <dzonatas@dzonux.net>
 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef __SSE__
#include <xmmintrin.h>
#endif

#include "dwt.h"
#include "j2k.h"
#include "tcd.h"
#include "fix.h"
#include "opj_malloc.h"
#include "int.h"

/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
/*@{*/

#define WS(i) v->mem[(i)*2]
#define WD(i) v->mem[(1+(i)*2)]

/** @name Local data structures */
/*@{*/

typedef struct dwt_local {
	OPJ_INT32* mem;
	OPJ_INT32 dn;
	OPJ_INT32 sn;
	OPJ_INT32 cas;
} dwt_t;

typedef union {
	OPJ_FLOAT32	f[4];
} v4;

typedef struct v4dwt_local {
	v4*	wavelet ;
	OPJ_INT32		dn ;
	OPJ_INT32		sn ;
	OPJ_INT32		cas ;
} v4dwt_t ;

static const OPJ_FLOAT32 alpha =  1.586134342f; //  12994
static const OPJ_FLOAT32 beta  =  0.052980118f; //    434
static const OPJ_FLOAT32 gamma = -0.882911075f; //  -7233
static const OPJ_FLOAT32 delta = -0.443506852f; //  -3633

static const OPJ_FLOAT32 K      = 1.230174105f; //  10078
/* FIXME: What is this constant? */
static const OPJ_FLOAT32 c13318 = 1.625732422f;

/*@}*/

/**
Virtual function type for wavelet transform in 1-D 
*/
typedef void (*DWT1DFN)(dwt_t* v);

/** @name Local static functions */
/*@{*/

/**
Forward lazy transform (horizontal)
*/
static void dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
/**
Forward lazy transform (vertical)
*/
static void dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
/**
Inverse lazy transform (horizontal)
*/
static void dwt_interleave_h(dwt_t* h, OPJ_INT32 *a);
/**
Inverse lazy transform (vertical)
*/
static void dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
/**
Forward 5-3 wavelet transform in 1-D
*/
static void dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
/**
Inverse 5-3 wavelet transform in 1-D
*/
static void dwt_decode_1(dwt_t *v);
/**
Forward 9-7 wavelet transform in 1-D
*/
static void dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
/**
Explicit calculation of the Quantization Stepsizes 
*/
static void dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
/**
Inverse wavelet transform in 2-D.
*/
Julien Michel's avatar
Julien Michel committed
127
static OPJ_BOOL dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
Julien Michel's avatar
Julien Michel committed
128 129 130

static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i);

131 132
// static INLINE OPJ_BOOL dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
static bool dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
Julien Michel's avatar
Julien Michel committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
/*@}*/

/*@}*/

#define S(i) a[(i)*2]
#define D(i) a[(1+(i)*2)]
#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
/* new */
#define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
#define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))

/* <summary>                                                              */
/* This table contains the norms of the 5-3 wavelets for different bands. */
/* </summary>                                                             */
static const OPJ_FLOAT64 dwt_norms[4][10] = {
	{1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
	{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
	{1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
	{.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
};

/* <summary>                                                              */
/* This table contains the norms of the 9-7 wavelets for different bands. */
/* </summary>                                                             */
static const OPJ_FLOAT64 dwt_norms_real[4][10] = {
	{1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
	{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
	{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
	{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};

/* 
==========================================================
   local functions
==========================================================
*/

/* <summary>			                 */
/* Forward lazy transform (horizontal).  */
/* </summary>                            */ 
static void dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
	OPJ_INT32 i;
	
	OPJ_INT32 * l_dest = b;
	OPJ_INT32 * l_src = a+cas;
    for 
		(i=0; i<sn; ++i) 
	{
		*l_dest++ = *l_src;
		l_src += 2;
	}
	l_dest = b + sn;
	l_src = a + 1 - cas;
    for 
		(i=0; i<dn; ++i) 
	{
		*l_dest++=*l_src;
		l_src += 2;
	}
}

/* <summary>                             */  
/* Forward lazy transform (vertical).    */
/* </summary>                            */ 
static void dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
    OPJ_INT32 i = sn;
	OPJ_INT32 * l_dest = b;
	OPJ_INT32 * l_src = a+cas;

    while 
		(i--) 
	{
		*l_dest = *l_src;
		l_dest += x;
		l_src += 2;
		/* b[i*x]=a[2*i+cas]; */
	}
	l_dest = b + sn * x;
	l_src = a + 1 - cas;
	
	i = dn;
    while 
		(i--) 
	{
		*l_dest = *l_src;
		l_dest += x;
		l_src += 2;
		/*b[(sn+i)*x]=a[(2*i+1-cas)];*/
	}
}

/* <summary>                             */
/* Inverse lazy transform (horizontal).  */
/* </summary>                            */
static void dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) {
    OPJ_INT32 *ai = a;
    OPJ_INT32 *bi = h->mem + h->cas;
    OPJ_INT32  i	= h->sn;
    while
		( i-- ) 
	{
		*bi = *(ai++);
		bi += 2;
    }
    ai	= a + h->sn;
    bi	= h->mem + 1 - h->cas;
    i	= h->dn ;
    while
		( i-- ) 
	{
		*bi = *(ai++);
		bi += 2;
    }
}

/* <summary>                             */  
/* Inverse lazy transform (vertical).    */
/* </summary>                            */ 
static void dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
    OPJ_INT32 *ai = a;
    OPJ_INT32 *bi = v->mem + v->cas;
    OPJ_INT32  i = v->sn;
    while( i-- ) {
      *bi = *ai;
	  bi += 2;
	  ai += x;
    }
    ai = a + (v->sn * x);
    bi = v->mem + 1 - v->cas;
    i = v->dn ;
    while( i-- ) {
      *bi = *ai;
	  bi += 2;  
	  ai += x;
    }
}


/* <summary>                            */
/* Forward 5-3 wavelet transform in 1-D. */
/* </summary>                           */
static void dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
	OPJ_INT32 i;
	
	if (!cas) {
		if ((dn > 0) || (sn > 1)) {	/* NEW :  CASE ONE ELEMENT */
			for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
			for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
		}
	} else {
		if (!sn && dn == 1)		    /* NEW :  CASE ONE ELEMENT */
			S(0) *= 2;
		else {
			for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
			for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
		}
	}
}

/* <summary>                            */
/* Inverse 5-3 wavelet transform in 1-D. */
/* </summary>                           */ 
static void dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
	OPJ_INT32 i;
	
	if (!cas) {
		if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
			for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
			for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
		}
	} else {
		if (!sn  && dn == 1)          /* NEW :  CASE ONE ELEMENT */
			S(0) /= 2;
		else {
			for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
			for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
		}
	}
}

/* <summary>                            */
/* Inverse 5-3 wavelet transform in 1-D. */
/* </summary>                           */ 
static void dwt_decode_1(dwt_t *v) {
	dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
}

/* <summary>                             */
/* Forward 9-7 wavelet transform in 1-D. */
/* </summary>                            */
static void dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
	OPJ_INT32 i;
	if (!cas) {
		if ((dn > 0) || (sn > 1)) {	/* NEW :  CASE ONE ELEMENT */
			for (i = 0; i < dn; i++)
				D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
			for (i = 0; i < sn; i++)
				S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
			for (i = 0; i < dn; i++)
				D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
			for (i = 0; i < sn; i++)
				S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
			for (i = 0; i < dn; i++)
				D(i) = fix_mul(D(i), 5038);	/*5038 */
			for (i = 0; i < sn; i++)
				S(i) = fix_mul(S(i), 6659);	/*6660 */
		}
	} else {
		if ((sn > 0) || (dn > 1)) {	/* NEW :  CASE ONE ELEMENT */
			for (i = 0; i < dn; i++)
				S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
			for (i = 0; i < sn; i++)
				D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
			for (i = 0; i < dn; i++)
				S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
			for (i = 0; i < sn; i++)
				D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
			for (i = 0; i < dn; i++)
				S(i) = fix_mul(S(i), 5038);	/*5038 */
			for (i = 0; i < sn; i++)
				D(i) = fix_mul(D(i), 6659);	/*6660 */
		}
	}
}

static void dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
	OPJ_INT32 p, n;
	p = int_floorlog2(stepsize) - 13;
	n = 11 - int_floorlog2(stepsize);
	bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
	bandno_stepsize->expn = numbps - p;
}

/* 
==========================================================
   DWT interface
==========================================================
*/

/* <summary>                            */
/* Forward 5-3 wavelet transform in 2-D. */
/* </summary>                           */
376 377
// INLINE OPJ_BOOL 
INLINE bool dwt_encode_procedure(opj_tcd_tilecomp_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
Julien Michel's avatar
Julien Michel committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
{
	OPJ_INT32 i, j, k;
	OPJ_INT32 *a = 00;
	OPJ_INT32 *aj = 00;
	OPJ_INT32 *bj = 00;
	OPJ_INT32 w, l;
	
	OPJ_INT32 rw;			/* width of the resolution level computed   */
	OPJ_INT32 rh;			/* height of the resolution level computed  */
	OPJ_INT32 l_data_size;
	
	opj_tcd_resolution_t * l_cur_res = 0;
	opj_tcd_resolution_t * l_last_res = 0;

	w = tilec->x1-tilec->x0;
	l = tilec->numresolutions-1;
	a = tilec->data;
	
	l_cur_res = tilec->resolutions + l;
	l_last_res = l_cur_res - 1;

	rw = l_cur_res->x1 - l_cur_res->x0;
	rh = l_cur_res->y1 - l_cur_res->y0;

	l_data_size = dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
	bj = opj_malloc(l_data_size);
	if
		(! bj)
	{
		return false;
	}
	i = l;
	
	while
		(i--)
	{
		OPJ_INT32 rw1;		/* width of the resolution level once lower than computed one                                       */
		OPJ_INT32 rh1;		/* height of the resolution level once lower than computed one                                      */
		OPJ_INT32 cas_col;	/* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
		OPJ_INT32 cas_row;	/* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
		OPJ_INT32 dn, sn;
		
		rw  = l_cur_res->x1 - l_cur_res->x0;
		rh  = l_cur_res->y1 - l_cur_res->y0;
		rw1 = l_last_res->x1 - l_last_res->x0;
		rh1 = l_last_res->y1 - l_last_res->y0;
		
		cas_row = l_cur_res->x0 & 1;
		cas_col = l_cur_res->y0 & 1;
        
		sn = rh1;
		dn = rh - rh1;
		for 
			(j = 0; j < rw; ++j) 
		{
			aj = a + j;
			for 
				(k = 0; k < rh; ++k)  
			{
				bj[k] = aj[k*w];
			}
			(*p_function) (bj, dn, sn, cas_col);
			dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
		}
		sn = rw1;
		dn = rw - rw1;
		for (j = 0; j < rh; j++) 
		{
			aj = a + j * w;
			for (k = 0; k < rw; k++)  bj[k] = aj[k];
			(*p_function) (bj, dn, sn, cas_row);
			dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
		}
		l_cur_res = l_last_res;
		--l_last_res;
	}
	opj_free(bj);
	return true;
}
/* Forward 5-3 wavelet transform in 2-D. */
/* </summary>                           */
Julien Michel's avatar
Julien Michel committed
459
OPJ_BOOL dwt_encode(opj_tcd_tilecomp_t * tilec)
Julien Michel's avatar
Julien Michel committed
460 461 462 463 464 465 466
{
	return dwt_encode_procedure(tilec,dwt_encode_1);
}

/* <summary>                            */
/* Inverse 5-3 wavelet transform in 2-D. */
/* </summary>                           */
Julien Michel's avatar
Julien Michel committed
467
OPJ_BOOL dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
Julien Michel's avatar
Julien Michel committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	return dwt_decode_tile(tilec, numres, &dwt_decode_1);
}


/* <summary>                          */
/* Get gain of 5-3 wavelet transform. */
/* </summary>                         */
OPJ_UINT32 dwt_getgain(OPJ_UINT32 orient) {
	if (orient == 0)
		return 0;
	if (orient == 1 || orient == 2)
		return 1;
	return 2;
}

/* <summary>                */
/* Get norm of 5-3 wavelet. */
/* </summary>               */
OPJ_FLOAT64 dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
	return dwt_norms[orient][level];
}

/* <summary>                             */
/* Forward 9-7 wavelet transform in 2-D. */
/* </summary>                            */
Julien Michel's avatar
Julien Michel committed
493
OPJ_BOOL dwt_encode_real(opj_tcd_tilecomp_t * tilec)
Julien Michel's avatar
Julien Michel committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
{
	return dwt_encode_procedure(tilec,dwt_encode_1_real);
}



/* <summary>                          */
/* Get gain of 9-7 wavelet transform. */
/* </summary>                         */
OPJ_UINT32 dwt_getgain_real(OPJ_UINT32 orient) {
	(void)orient;
	return 0;
}

/* <summary>                */
/* Get norm of 9-7 wavelet. */
/* </summary>               */
OPJ_FLOAT64 dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
	return dwt_norms_real[orient][level];
}

void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
	OPJ_UINT32 numbands, bandno;
	numbands = 3 * tccp->numresolutions - 2;
	for (bandno = 0; bandno < numbands; bandno++) {
		OPJ_FLOAT64 stepsize;
		OPJ_UINT32 resno, level, orient, gain;

		resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
		orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
		level = tccp->numresolutions - 1 - resno;
		gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
		if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
			stepsize = 1.0;
		} else {
			OPJ_FLOAT64 norm = dwt_norms_real[orient][level];
			stepsize = (1 << (gain)) / norm;
		}
		dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
	}
}


/* <summary>                             */
/* Determine maximum computed resolution level for inverse wavelet transform */
/* </summary>                            */
static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
	OPJ_UINT32 mr	= 0;
	OPJ_UINT32 w;
	while( --i ) {
		++r;
		if( mr < ( w = r->x1 - r->x0 ) )
			mr = w ;
		if( mr < ( w = r->y1 - r->y0 ) )
			mr = w ;
	}
	return mr ;
}


/* <summary>                            */
/* Inverse wavelet transform in 2-D.     */
/* </summary>                           */
Julien Michel's avatar
Julien Michel committed
557
static OPJ_BOOL dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
Julien Michel's avatar
Julien Michel committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	dwt_t h;
	dwt_t v;

	opj_tcd_resolution_t* tr = tilec->resolutions;

	OPJ_UINT32 rw = tr->x1 - tr->x0;	/* width of the resolution level computed */
	OPJ_UINT32 rh = tr->y1 - tr->y0;	/* height of the resolution level computed */

	OPJ_UINT32 w = tilec->x1 - tilec->x0;

	h.mem = opj_aligned_malloc(dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
	if
		(! h.mem)
	{
		return false;
	}

	v.mem = h.mem;

	while( --numres) {
		OPJ_INT32 * restrict tiledp = tilec->data;
		OPJ_UINT32 j;

		++tr;
		h.sn = rw;
		v.sn = rh;

		rw = tr->x1 - tr->x0;
		rh = tr->y1 - tr->y0;

		h.dn = rw - h.sn;
		h.cas = tr->x0 % 2;

		for(j = 0; j < rh; ++j) {
			dwt_interleave_h(&h, &tiledp[j*w]);
			(dwt_1D)(&h);
			memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
		}

		v.dn = rh - v.sn;
		v.cas = tr->y0 % 2;

		for(j = 0; j < rw; ++j){
			OPJ_UINT32 k;
			dwt_interleave_v(&v, &tiledp[j], w);
			(dwt_1D)(&v);
			for(k = 0; k < rh; ++k) {
				tiledp[k * w + j] = v.mem[k];
			}
		}
	}
	opj_aligned_free(h.mem);
	return true;
}

static void v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
	OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
	OPJ_INT32 count = w->sn;
	OPJ_INT32 i, k;
	for(k = 0; k < 2; ++k){
		for(i = 0; i < count; ++i){
			OPJ_INT32 j = i;
			bi[i*8    ] = a[j];
			j += x;
			if(j >= size) continue;
			bi[i*8 + 1] = a[j];
			j += x;
			if(j >= size) continue;
			bi[i*8 + 2] = a[j];
			j += x;
			if(j >= size) continue;
			bi[i*8 + 3] = a[j];
		}
		bi = (OPJ_FLOAT32*) (w->wavelet + 1 - w->cas);
		a += w->sn;
		size -= w->sn;
		count = w->dn;
	}
}

static void v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x){
	v4* restrict bi = v->wavelet + v->cas;
	OPJ_INT32 i;
	for(i = 0; i < v->sn; ++i){
		memcpy(&bi[i*2], &a[i*x], 4 * sizeof(OPJ_FLOAT32));
	}
	a += v->sn * x;
	bi = v->wavelet + 1 - v->cas;
	for(i = 0; i < v->dn; ++i){
		memcpy(&bi[i*2], &a[i*x], 4 * sizeof(OPJ_FLOAT32));
	}
}

#ifdef __SSE__

static void v4dwt_decode_step1_sse(v4* w, OPJ_INT32 count, const __m128 c){
	__m128* restrict vw = (__m128*) w;
	OPJ_INT32 i;
	for(i = 0; i < count; ++i){
		__m128 tmp = vw[i*2];
		vw[i*2] = tmp * c;
	}
}

static void v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c){
	__m128* restrict vl = (__m128*) l;
	__m128* restrict vw = (__m128*) w;
	OPJ_INT32 i;
	for(i = 0; i < m; ++i){
		__m128 tmp1 = vl[ 0];
		__m128 tmp2 = vw[-1];
		__m128 tmp3 = vw[ 0];
		vw[-1] = tmp2 + ((tmp1 + tmp3) * c);
		vl = vw;
		vw += 2;
	}
	if(m >= k){
		return;
	}
	c += c;
	c *= vl[0];
	for(; m < k; ++m){
		__m128 tmp = vw[-1];
		vw[-1] = tmp + c;
		vw += 2;
	}
}

#else

static void v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c){
	OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
	OPJ_INT32 i;
	for(i = 0; i < count; ++i){
		OPJ_FLOAT32 tmp1 = fw[i*8    ];
		OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
		OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
		OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
		fw[i*8    ] = tmp1 * c;
		fw[i*8 + 1] = tmp2 * c;
		fw[i*8 + 2] = tmp3 * c;
		fw[i*8 + 3] = tmp4 * c;
	}
}

static void v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c){
	OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
	OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
	OPJ_INT32 i;
	for(i = 0; i < m; ++i){
		OPJ_FLOAT32 tmp1_1 = fl[0];
		OPJ_FLOAT32 tmp1_2 = fl[1];
		OPJ_FLOAT32 tmp1_3 = fl[2];
		OPJ_FLOAT32 tmp1_4 = fl[3];
		OPJ_FLOAT32 tmp2_1 = fw[-4];
		OPJ_FLOAT32 tmp2_2 = fw[-3];
		OPJ_FLOAT32 tmp2_3 = fw[-2];
		OPJ_FLOAT32 tmp2_4 = fw[-1];
		OPJ_FLOAT32 tmp3_1 = fw[0];
		OPJ_FLOAT32 tmp3_2 = fw[1];
		OPJ_FLOAT32 tmp3_3 = fw[2];
		OPJ_FLOAT32 tmp3_4 = fw[3];
		fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
		fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
		fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
		fw[-1] = tmp2_4 + ((tmp1_4 + tmp3_4) * c);
		fl = fw;
		fw += 8;
	}
	if(m < k){
		OPJ_FLOAT32 c1;
		OPJ_FLOAT32 c2;
		OPJ_FLOAT32 c3;
		OPJ_FLOAT32 c4;
		c += c;
		c1 = fl[0] * c;
		c2 = fl[1] * c;
		c3 = fl[2] * c;
		c4 = fl[3] * c;
		for(; m < k; ++m){
			OPJ_FLOAT32 tmp1 = fw[-4];
			OPJ_FLOAT32 tmp2 = fw[-3];
			OPJ_FLOAT32 tmp3 = fw[-2];
			OPJ_FLOAT32 tmp4 = fw[-1];
			fw[-4] = tmp1 + c1;
			fw[-3] = tmp2 + c2;
			fw[-2] = tmp3 + c3;
			fw[-1] = tmp4 + c4;
			fw += 8;
		}
	}
}

#endif

/* <summary>                             */
/* Inverse 9-7 wavelet transform in 1-D. */
/* </summary>                            */
static void v4dwt_decode(v4dwt_t* restrict dwt){
	OPJ_INT32 a, b;
	if(dwt->cas == 0) {
		if(!((dwt->dn > 0) || (dwt->sn > 1))){
			return;
		}
		a = 0;
		b = 1;
	}else{
		if(!((dwt->sn > 0) || (dwt->dn > 1))) {
			return;
		}
		a = 1;
		b = 0;
	}
#ifdef __SSE__
	v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K));
	v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318));
	v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(delta));
	v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(gamma));
	v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(beta));
	v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(alpha));
#else
	v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K);
	v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318);
	v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), delta);
	v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), gamma);
	v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), beta);
	v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), alpha);
#endif
}

/* <summary>                             */
/* Inverse 9-7 wavelet transform in 2-D. */
/* </summary>                            */
Julien Michel's avatar
Julien Michel committed
791
OPJ_BOOL dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, OPJ_UINT32 numres){
Julien Michel's avatar
Julien Michel committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
	v4dwt_t h;
	v4dwt_t v;

	opj_tcd_resolution_t* res = tilec->resolutions;

	OPJ_UINT32 rw = res->x1 - res->x0;	/* width of the resolution level computed */
	OPJ_UINT32 rh = res->y1 - res->y0;	/* height of the resolution level computed */

	OPJ_UINT32 w = tilec->x1 - tilec->x0;

	h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4));
	v.wavelet = h.wavelet;

	while( --numres) {
		OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
		OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
		OPJ_INT32 j;

		h.sn = rw;
		v.sn = rh;

		++res;

		rw = res->x1 - res->x0;	/* width of the resolution level computed */
		rh = res->y1 - res->y0;	/* height of the resolution level computed */

		h.dn = rw - h.sn;
		h.cas = res->x0 & 1;

		for(j = rh; j > 0; j -= 4){
			v4dwt_interleave_h(&h, aj, w, bufsize);
			v4dwt_decode(&h);
			if(j >= 4){
				OPJ_INT32 k = rw;
				while
					(--k >= 0)
				{
					aj[k    ] = h.wavelet[k].f[0];
					aj[k+w  ] = h.wavelet[k].f[1];
					aj[k+w*2] = h.wavelet[k].f[2];
					aj[k+w*3] = h.wavelet[k].f[3];
				}
			}else{
				OPJ_INT32 k = rw;
				while
					(--k >= 0)
				{
					switch(j) {
						case 3: aj[k+w*2] = h.wavelet[k].f[2];
						case 2: aj[k+w  ] = h.wavelet[k].f[1];
						case 1: aj[k    ] = h.wavelet[k].f[0];
					}
				}
			}
			aj += w*4;
			bufsize -= w*4;
		}

		v.dn = rh - v.sn;
		v.cas = res->y0 % 2;

		aj = (OPJ_FLOAT32*) tilec->data;
		for(j = rw; j > 0; j -= 4){
			v4dwt_interleave_v(&v, aj, w);
			v4dwt_decode(&v);
			if(j >= 4){
				OPJ_UINT32 k;
				for(k = 0; k < rh; ++k){
					memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
				}
			}else{
				OPJ_UINT32 k;
				for(k = 0; k < rh; ++k){
					memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
				}
			}
			aj += 4;
		}
	}

	opj_aligned_free(h.wavelet);
	return true;
}