fix.h, dwt.c and t1.c optimized. Thanks a lot to Dzonatas <dzonatas at dzonux.net> and Callum Lerwick <seg at haxxed.com> for these great patches !

This commit is contained in:
Francois-Olivier Devaux 2007-04-05 14:24:19 +00:00
parent dd18811600
commit 0930d9886b
4 changed files with 246 additions and 199 deletions

View File

@ -5,6 +5,11 @@ What's New for OpenJPEG
! : changed ! : changed
+ : added + : added
April 5, 2007
! [FOD] fix.h optimized. Thanks a lot to Dzonatas <dzonatas at dzonux.net> !
! [FOD] dwt.c optimized. Thanks a lot to Dzonatas <dzonatas at dzonux.net> !
! [FOD] t1.c optimized. Thanks a lot to Callum Lerwick <seg at haxxed.com> !
April 4,2007 April 4,2007
+ [Parvatha] Digital cinema compliance for 4K chosen by "-cinema4K" option. Modification in image_to_j2k.c. + [Parvatha] Digital cinema compliance for 4K chosen by "-cinema4K" option. Modification in image_to_j2k.c.
+ [Parvatha] Bit rate limitation for each color component. Modification in image_to_j2k.c, t2.c. + [Parvatha] Bit rate limitation for each color component. Modification in image_to_j2k.c, t2.c.

View File

@ -29,33 +29,32 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
/*
* NOTE:
* This is a modified version of the openjpeg dwt.c file.
* Average speed improvement compared to the original file (measured on
* my own machine, a P4 running at 3.0 GHz):
* 5x3 wavelets about 2 times faster
* 9x7 wavelets about 3 times faster
* for both, encoding and decoding.
*
* The better performance is caused by doing the 1-dimensional DWT
* within a temporary buffer where the data can be accessed sequential
* for both directions, horizontal and vertical. The 2d vertical DWT was
* the major bottleneck in the former version.
*
* I have also removed the "Add Patrick" part because it is not longer
* needed.
*
* 6/6/2005
* -Ive (aka Reiner Wahler)
* mail: ive@lilysoft.com
*/
#include "opj_includes.h" #include "opj_includes.h"
/** @defgroup DWT DWT - Implementation of a discrete wavelet transform */ /** @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{
int * mem ;
int dn ;
int sn ;
int cas ;
} dwt_t ;
/*@}*/
/**
Virtual function type for wavelet transform in 1-D
*/
typedef void (*DWT1DFN)(dwt_t* v);
/** @name Local static functions */ /** @name Local static functions */
/*@{*/ /*@{*/
@ -70,11 +69,11 @@ static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas);
/** /**
Inverse lazy transform (horizontal) Inverse lazy transform (horizontal)
*/ */
static void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas); static void dwt_interleave_h(dwt_t* h, int *a);
/** /**
Inverse lazy transform (vertical) Inverse lazy transform (vertical)
*/ */
static void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas); static void dwt_interleave_v(dwt_t* v, int *a, int x);
/** /**
Forward 5-3 wavelet tranform in 1-D Forward 5-3 wavelet tranform in 1-D
*/ */
@ -82,7 +81,7 @@ static void dwt_encode_1(int *a, int dn, int sn, int cas);
/** /**
Inverse 5-3 wavelet tranform in 1-D Inverse 5-3 wavelet tranform in 1-D
*/ */
static void dwt_decode_1(int *a, int dn, int sn, int cas); static void dwt_decode_1(dwt_t *v);
/** /**
Forward 9-7 wavelet transform in 1-D Forward 9-7 wavelet transform in 1-D
*/ */
@ -90,11 +89,15 @@ static void dwt_encode_1_real(int *a, int dn, int sn, int cas);
/** /**
Inverse 9-7 wavelet transform in 1-D Inverse 9-7 wavelet transform in 1-D
*/ */
static void dwt_decode_1_real(int *a, int dn, int sn, int cas); static void dwt_decode_1_real(dwt_t *v);
/** /**
FIXME : comment ??? FIXME : comment ???
*/ */
static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize); static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize);
/**
Inverse wavelet tranform in 2-D.
*/
static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop , DWT1DFN fn);
/*@}*/ /*@}*/
@ -155,43 +158,39 @@ static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
/* <summary> */ /* <summary> */
/* Inverse lazy transform (horizontal). */ /* Inverse lazy transform (horizontal). */
/* </summary> */ /* </summary> */
static void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) { static void dwt_interleave_h(dwt_t* h, int *a) {
int i; int *ai = a;
int *ai = NULL; int *bi = h->mem + h->cas;
int *bi = NULL; int i = h->sn;
ai = a; while( i-- ) {
bi = b + cas; *bi = *(ai++);
for (i = 0; i < sn; i++) {
*bi = *ai;
bi += 2; bi += 2;
ai++;
} }
ai = a + sn; ai = a + h->sn;
bi = b + 1 - cas; bi = h->mem + 1 - h->cas;
for (i = 0; i < dn; i++) { i = h->dn ;
*bi = *ai; while( i-- ) {
*bi = *(ai++);
bi += 2; bi += 2;
ai++;
} }
} }
/* <summary> */ /* <summary> */
/* Inverse lazy transform (vertical). */ /* Inverse lazy transform (vertical). */
/* </summary> */ /* </summary> */
static void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) { static void dwt_interleave_v(dwt_t* v, int *a, int x) {
int i; int *ai = a;
int *ai = NULL; int *bi = v->mem + v->cas;
int *bi = NULL; int i = v->sn;
ai = a; while( i-- ) {
bi = b + cas;
for (i = 0; i < sn; i++) {
*bi = *ai; *bi = *ai;
bi += 2; bi += 2;
ai += x; ai += x;
} }
ai = a + (sn * x); ai = a + (v->sn * x);
bi = b + 1 - cas; bi = v->mem + 1 - v->cas;
for (i = 0; i < dn; i++) { i = v->dn ;
while( i-- ) {
*bi = *ai; *bi = *ai;
bi += 2; bi += 2;
ai += x; ai += x;
@ -223,7 +222,7 @@ static void dwt_encode_1(int *a, int dn, int sn, int cas) {
/* <summary> */ /* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */ /* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */ /* </summary> */
static void dwt_decode_1(int *a, int dn, int sn, int cas) { static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
int i; int i;
if (!cas) { if (!cas) {
@ -241,6 +240,13 @@ static void dwt_decode_1(int *a, int dn, int sn, int cas) {
} }
} }
/* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */
static void dwt_decode_1(dwt_t *v) {
dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
}
/* <summary> */ /* <summary> */
/* Forward 9-7 wavelet transform in 1-D. */ /* Forward 9-7 wavelet transform in 1-D. */
/* </summary> */ /* </summary> */
@ -279,40 +285,98 @@ static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
} }
} }
static void dwt_decode_sm(dwt_t* v, int k, int n, int x) {
int m = k > n ? n : k;
int l = v->mem[1]; //D(0);
int j;
int i;
for (i = 0; i < m; i++) {
j = l;
WS(i) -= fix_mul( ( l = WD(i) ) + j , x);
}
if( i < k ) {
l = fix_mul( l + l , x );
for (; i < k; i++)
WS(i) -= l;
}
}
static void dwt_decode_sp(dwt_t* v, int k, int n, int x) {
int m = k > n ? n : k;
int l = v->mem[1]; //D(0);
int j;
int i;
for (i = 0; i < m; i++) {
j = l;
WS(i) += fix_mul( ( l = WD(i) ) + j , x);
}
if( i < k ) {
l = fix_mul( l + l , x );
for (; i < k; i++)
WS(i) += l;
}
}
static void dwt_decode_dm(dwt_t* v, int k, int n, int x) {
int m = k >= n ? n-1 : k;
int l = v->mem[0]; //S(0);
int i;
int j;
for (i = 0; i < m; i++) {
j = l;
WD(i) -= fix_mul( ( l = WS(i+1) ) + j , x);
}
if( i < k ) {
l = fix_mul( l + l , x );
for (; i < k; i++)
WD(i) -= l;
}
}
static void dwt_decode_dp(dwt_t* v, int k, int n, int x) {
int m = k >= n ? n-1 : k;
int l = v->mem[0]; //S(0);
int i;
int j;
for (i = 0; i < m; i++) {
j = l;
WD(i) += fix_mul( ( l = WS(i+1) ) + j , x);
}
if( i < k ) {
l = fix_mul( l + l , x );
for (; i < k; i++)
WD(i) += l;
}
}
/* <summary> */ /* <summary> */
/* Inverse 9-7 wavelet transform in 1-D. */ /* Inverse 9-7 wavelet transform in 1-D. */
/* </summary> */ /* </summary> */
static void dwt_decode_1_real(int *a, int dn, int sn, int cas) { static void dwt_decode_1_real(dwt_t* v) {
int i; int i;
if (!cas) { if (!v->cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */ if ((v->dn > 0) || (v->sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++) for (i = 0; i < v->sn; i++)
S(i) = fix_mul(S(i), 10078); /* 10076 */ WS(i) = fix_mul(WS(i), 10078); /* 10076 */
for (i = 0; i < dn; i++) for (i = 0; i < v->dn; i++)
D(i) = fix_mul(D(i), 13318); /* 13320 */ WD(i) = fix_mul(WD(i), 13318); /* 13320 */
for (i = 0; i < sn; i++) dwt_decode_sm(v, v->sn, v->dn, 3633);
S(i) -= fix_mul(D_(i - 1) + D_(i), 3633); dwt_decode_dm(v, v->dn, v->sn, 7233);
for (i = 0; i < dn; i++) dwt_decode_sp(v, v->sn, v->dn, 434);
D(i) -= fix_mul(S_(i) + S_(i + 1), 7233); dwt_decode_dp(v, v->dn, v->sn, 12994);
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), 12994); /* 12993 */
} }
} else { } else {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */ if ((v->sn > 0) || (v->dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++) for (i = 0; i < v->sn; i++)
D(i) = fix_mul(D(i), 10078); /* 10076 */ WD(i) = fix_mul(WD(i), 10078); /* 10076 */
for (i = 0; i < dn; i++) for (i = 0; i < v->dn; i++)
S(i) = fix_mul(S(i), 13318); /* 13320 */ WS(i) = fix_mul(WS(i), 13318); /* 13320 */
for (i = 0; i < sn; i++) dwt_decode_dm(v, v->sn, v->dn, 3633);
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633); dwt_decode_sm(v, v->dn, v->sn, 7233);
for (i = 0; i < dn; i++) dwt_decode_dp(v, v->sn, v->dn, 434);
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233); dwt_decode_sp(v, v->dn, v->sn, 12994);
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), 12994); /* 12993 */
} }
} }
} }
@ -391,55 +455,7 @@ void dwt_encode(opj_tcd_tilecomp_t * tilec) {
/* Inverse 5-3 wavelet tranform in 2-D. */ /* Inverse 5-3 wavelet tranform in 2-D. */
/* </summary> */ /* </summary> */
void dwt_decode(opj_tcd_tilecomp_t * tilec, int stop) { void dwt_decode(opj_tcd_tilecomp_t * tilec, int stop) {
int i, j, k; dwt_decode_tile(tilec, stop, &dwt_decode_1);
int *a = NULL;
int *aj = NULL;
int *bj = NULL;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l - 1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rw1;
dn = rw - rw1;
bj = (int*)opj_malloc(rw * sizeof(int));
for (j = 0; j < rh; j++) {
aj = a + j*w;
dwt_interleave_h(aj, bj, dn, sn, cas_row);
dwt_decode_1(bj, dn, sn, cas_row);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
opj_free(bj);
sn = rh1;
dn = rh - rh1;
bj = (int*)opj_malloc(rh * sizeof(int));
for (j = 0; j < rw; j++) {
aj = a + j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_col);
dwt_decode_1(bj, dn, sn, cas_col);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
opj_free(bj);
}
} }
@ -522,55 +538,7 @@ void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
/* Inverse 9-7 wavelet transform in 2-D. */ /* Inverse 9-7 wavelet transform in 2-D. */
/* </summary> */ /* </summary> */
void dwt_decode_real(opj_tcd_tilecomp_t * tilec, int stop) { void dwt_decode_real(opj_tcd_tilecomp_t * tilec, int stop) {
int i, j, k; dwt_decode_tile(tilec, stop, dwt_decode_1_real);
int *a = NULL;
int *aj = NULL;
int *bj = NULL;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l-1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
sn = rw1;
dn = rw-rw1;
bj = (int*)opj_malloc(rw * sizeof(int));
for (j = 0; j < rh; j++) {
aj = a + j * w;
dwt_interleave_h(aj, bj, dn, sn, cas_col);
dwt_decode_1_real(bj, dn, sn, cas_col);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
opj_free(bj);
sn = rh1;
dn = rh-rh1;
bj = (int*)opj_malloc(rh * sizeof(int));
for (j = 0; j < rw; j++) {
aj = a + j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_row);
dwt_decode_1_real(bj, dn, sn, cas_row);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
opj_free(bj);
}
} }
@ -609,3 +577,85 @@ void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]); dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
} }
} }
/* <summary> */
/* Determine maximum computed resolution level for inverse wavelet transform */
/* </summary> */
static int dwt_decode_max_resolution(opj_tcd_resolution_t* r, int i) {
int mr = 1;
int 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 tranform in 2-D. */
/* </summary> */
static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop, DWT1DFN dwt_1D) {
opj_tcd_resolution_t* tr;
int i, j, k;
int *a = NULL;
int *aj = NULL;
int *m;
int w; //, l;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
dwt_t h;
dwt_t v;
if( 1 > ( i = tilec->numresolutions - stop ) )
return ;
tr = tilec->resolutions;
w = tilec->x1-tilec->x0;
a = tilec->data;
m = (int*)opj_malloc(sizeof(int) * (dwt_decode_max_resolution(tr, i)+5));
h.mem = v.mem = (int*)( (unsigned)m + 16 - ( (unsigned)m % 16 ) ) ;
rw = tr->x1 - tr->x0;
rh = tr->y1 - tr->y0;
while( --i ) {
tr++;
h.sn = rw;
v.sn = rh;
h.dn = ( rw = tr->x1 - tr->x0 ) - h.sn;
v.dn = ( rh = tr->y1 - tr->y0 ) - v.sn;
h.cas = tr->x0 % 2;
v.cas = tr->y0 % 2;
aj = a;
j = rh;
while( j-- ) {
dwt_interleave_h(&h, aj);
(dwt_1D)(&h);
k = rw;
while( k-- )
aj[k] = h.mem[k];
aj += w;
}
aj = a;
j = rw;
while( j-- ) {
dwt_interleave_v(&v, aj, w);
(dwt_1D)(&v);
k = rh;
while( k-- )
aj[k * w] = v.mem[k];
aj++;
}
}
opj_free(m);
}

View File

@ -54,8 +54,9 @@ Multiply two fixed-precision rational numbers.
@return Returns a * b @return Returns a * b
*/ */
static INLINE int fix_mul(int a, int b) { static INLINE int fix_mul(int a, int b) {
int64 temp = (int64) a * (int64) b >> 12; int64 temp = (int64) a * (int64) b ;
return (int) ((temp >> 1) + (temp & 1)) ; temp += temp & 4096;
return (int) (temp >> 13) ;
} }
/*@}*/ /*@}*/

View File

@ -543,11 +543,8 @@ static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int
cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0; cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
/* Changed by Dmitry Kolyadin */ for (i = 0; i <= h; ++i) {
for (i = 0; i <= w; i++) { memset(&t1->flags[i], 0, (w+1) * sizeof(int));
for (j = 0; j <= h; j++) {
t1->flags[j][i] = 0;
}
} }
bpno = cblk->numbps - 1; bpno = cblk->numbps - 1;
@ -653,7 +650,7 @@ static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int
} }
static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) { static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
int i, j, w, h; int i, w, h;
int bpno, passtype; int bpno, passtype;
int segno, passno; int segno, passno;
char type = T1_TYPE_MQ; /* BYPASS mode */ char type = T1_TYPE_MQ; /* BYPASS mode */
@ -664,18 +661,12 @@ static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int
w = cblk->x1 - cblk->x0; w = cblk->x1 - cblk->x0;
h = cblk->y1 - cblk->y0; h = cblk->y1 - cblk->y0;
/* Changed by Dmitry Kolyadin */ for (i = 0; i <= h; ++i) {
for (j = 0; j <= h; j++) { memset(&t1->flags[i], 0, (w + 1) * sizeof(int));
for (i = 0; i <= w; i++) {
t1->flags[j][i] = 0;
}
} }
/* Changed by Dmitry Kolyadin */ for (i = 0; i < h; ++i) {
for (i = 0; i < w; i++) { memset(&t1->data[i], 0, w * sizeof(int));
for (j = 0; j < h; j++){
t1->data[j][i] = 0;
}
} }
bpno = roishift + cblk->numbps - 1; bpno = roishift + cblk->numbps - 1;