diff options
| author | Francois-Olivier Devaux <fodevaux@users.noreply.github.com> | 2007-11-14 10:52:02 +0000 |
|---|---|---|
| committer | Francois-Olivier Devaux <fodevaux@users.noreply.github.com> | 2007-11-14 10:52:02 +0000 |
| commit | 010ae27471655e3fe05912bd9965245a4f2cd7f3 (patch) | |
| tree | 5799bb0c4d6b0a1c26e842a7f0c6ece5276724b9 /libopenjpeg/t2.c | |
| parent | 24e189e4d8a231e7273d788b2bd77e2484cec761 (diff) | |
Patch by Callum Lerwick. The opj_tcd_cblk array is one of the largest allocations, because it contains a bunch of static buffers. This also makes it a major source of cache thrashing. This patch allocates the buffers from the heap, and dynamically sizes them in the decoder. I have not yet managed to dynamically size them in the encoder, getting the decoder to do it was tricky enough... I also split opj_tcd_cblk_t into separate encode and decode versions. A lot of fields were not used by both, so this cuts its size even further.
Diffstat (limited to 'libopenjpeg/t2.c')
| -rw-r--r-- | libopenjpeg/t2.c | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/libopenjpeg/t2.c b/libopenjpeg/t2.c index b10b822d..81f46cd4 100644 --- a/libopenjpeg/t2.c +++ b/libopenjpeg/t2.c @@ -63,7 +63,7 @@ static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterato @param cblksty @param first */ -static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first); +static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first); /** Decode a packet of a tile from a source buffer @param t2 T2 handle @@ -160,7 +160,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; cblk->numpasses = 0; tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps); } @@ -176,14 +176,14 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; opj_tcd_layer_t *layer = &cblk->layers[layno]; if (!cblk->numpasses && layer->numpasses) { tgt_setvalue(prc->incltree, cblkno, layno); } } for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; opj_tcd_layer_t *layer = &cblk->layers[layno]; int increment = 0; int nump = 0; @@ -267,7 +267,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno]; opj_tcd_layer_t *layer = &cblk->layers[layno]; if (!layer->numpasses) { continue; @@ -294,7 +294,12 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera return (c - dest); } -static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) { +static void t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) { + opj_tcd_seg_t* seg; + cblk->segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t)); + seg = &cblk->segs[index]; + seg->data = NULL; + seg->dataindex = 0; seg->numpasses = 0; seg->len = 0; if (cblksty & J2K_CCP_CBLKSTY_TERMALL) { @@ -340,7 +345,7 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; cblk->numsegs = 0; } } @@ -424,9 +429,8 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - int included, increment, n; - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; - opj_tcd_seg_t *seg = NULL; + int included, increment, n, segno; + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; /* if cblk not yet included before --> inclusion tagtree */ if (!cblk->numsegs) { included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); @@ -454,23 +458,25 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t increment = t2_getcommacode(bio); /* length indicator increment */ cblk->numlenbits += increment; + segno = 0; if (!cblk->numsegs) { - seg = &cblk->segs[0]; - t2_init_seg(seg, tcp->tccps[compno].cblksty, 1); + t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1); } else { - seg = &cblk->segs[cblk->numsegs - 1]; - if (seg->numpasses == seg->maxpasses) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); + segno = cblk->numsegs - 1; + if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) { + ++segno; + t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); } } n = cblk->numnewpasses; do { - seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n); - seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses)); - n -= seg->numnewpasses; + cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n); + cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses)); + n -= cblk->segs[segno].numnewpasses; if (n > 0) { - t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); + ++segno; + t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); } } while (n > 0); } @@ -518,7 +524,7 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { - opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; + opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; opj_tcd_seg_t *seg = NULL; if (!cblk->numnewpasses) continue; @@ -559,9 +565,11 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t #endif /* USE_JPWL */ + cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char*)); memcpy(cblk->data + cblk->len, c, seg->newlen); if (seg->numpasses == 0) { - seg->data = cblk->data + cblk->len; + seg->data = &cblk->data; + seg->dataindex = cblk->len; } c += seg->newlen; cblk->len += seg->newlen; |
