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) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #define _ISOC99_SOURCE /* lrintf is C99 */
34 #include "opj_includes.h"
37 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
38 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
40 fprintf(fd, "image {\n");
41 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
42 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
44 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
45 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
46 fprintf(fd, " tile {\n");
47 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
48 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
49 for (compno = 0; compno < tile->numcomps; compno++) {
50 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
51 fprintf(fd, " tilec {\n");
53 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
54 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
55 for (resno = 0; resno < tilec->numresolutions; resno++) {
56 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
57 fprintf(fd, "\n res {\n");
59 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
60 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
61 for (bandno = 0; bandno < res->numbands; bandno++) {
62 opj_tcd_band_t *band = &res->bands[bandno];
63 fprintf(fd, " band {\n");
65 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
66 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
67 for (precno = 0; precno < res->pw * res->ph; precno++) {
68 opj_tcd_precinct_t *prec = &band->precincts[precno];
69 fprintf(fd, " prec {\n");
71 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
72 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
74 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
75 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
76 fprintf(fd, " cblk {\n");
78 " x0=%d, y0=%d, x1=%d, y1=%d\n",
79 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
96 /* ----------------------------------------------------------------------- */
99 Create a new TCD handle
101 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
102 /* create the tcd structure */
103 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
104 if(!tcd) return NULL;
106 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
107 if(!tcd->tcd_image) {
116 Destroy a previously created TCD handle
118 void tcd_destroy(opj_tcd_t *tcd) {
120 opj_free(tcd->tcd_image);
125 /* ----------------------------------------------------------------------- */
127 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
128 int tileno, compno, resno, bandno, precno, cblkno;
132 tcd->tcd_image->tw = cp->tw;
133 tcd->tcd_image->th = cp->th;
134 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
136 for (tileno = 0; tileno < 1; tileno++) {
137 opj_tcp_t *tcp = &cp->tcps[curtileno];
140 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
141 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
142 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
144 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
145 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
147 /* 4 borders of the tile rescale on the image if necessary */
148 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
149 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
150 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
151 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
152 tile->numcomps = image->numcomps;
153 /* tile->PPT=image->PPT; */
155 /* Modification of the RATE >> */
156 for (j = 0; j < tcp->numlayers; j++) {
157 tcp->rates[j] = tcp->rates[j] ?
159 (((float) (tile->numcomps
160 * (tile->x1 - tile->x0)
161 * (tile->y1 - tile->y0)
162 * image->comps[0].prec))
163 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
165 ((float) (tile->numcomps
166 * (tile->x1 - tile->x0)
167 * (tile->y1 - tile->y0)
168 * image->comps[0].prec))/
169 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
173 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
174 tcp->rates[j] = tcp->rates[j - 1] + 20;
176 if (!j && tcp->rates[j] < 30)
180 if(j == (tcp->numlayers-1)){
181 tcp->rates[j] = tcp->rates[j]- 2;
185 /* << Modification of the RATE */
187 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
188 for (compno = 0; compno < tile->numcomps; compno++) {
189 opj_tccp_t *tccp = &tcp->tccps[compno];
191 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
193 /* border of each tile component (global) */
194 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
195 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
196 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
197 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
199 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
200 tilec->numresolutions = tccp->numresolutions;
202 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
204 for (resno = 0; resno < tilec->numresolutions; resno++) {
206 int levelno = tilec->numresolutions - 1 - resno;
207 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
208 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
209 int cbgwidthexpn, cbgheightexpn;
210 int cblkwidthexpn, cblkheightexpn;
212 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
214 /* border for each resolution level (global) */
215 res->x0 = int_ceildivpow2(tilec->x0, levelno);
216 res->y0 = int_ceildivpow2(tilec->y0, levelno);
217 res->x1 = int_ceildivpow2(tilec->x1, levelno);
218 res->y1 = int_ceildivpow2(tilec->y1, levelno);
220 res->numbands = resno == 0 ? 1 : 3;
221 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
222 if (tccp->csty & J2K_CCP_CSTY_PRT) {
223 pdx = tccp->prcw[resno];
224 pdy = tccp->prch[resno];
229 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
230 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
231 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
233 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
234 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
236 res->pw = (brprcxend - tlprcxstart) >> pdx;
237 res->ph = (brprcyend - tlprcystart) >> pdy;
240 tlcbgxstart = tlprcxstart;
241 tlcbgystart = tlprcystart;
242 brcbgxend = brprcxend;
243 brcbgyend = brprcyend;
247 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
248 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
249 brcbgxend = int_ceildivpow2(brprcxend, 1);
250 brcbgyend = int_ceildivpow2(brprcyend, 1);
251 cbgwidthexpn = pdx - 1;
252 cbgheightexpn = pdy - 1;
257 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
258 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
260 for (bandno = 0; bandno < res->numbands; bandno++) {
263 opj_stepsize_t *ss = NULL;
265 opj_tcd_band_t *band = &res->bands[bandno];
267 band->bandno = resno == 0 ? 0 : bandno + 1;
268 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
269 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
271 if (band->bandno == 0) {
272 /* band border (global) */
273 band->x0 = int_ceildivpow2(tilec->x0, levelno);
274 band->y0 = int_ceildivpow2(tilec->y0, levelno);
275 band->x1 = int_ceildivpow2(tilec->x1, levelno);
276 band->y1 = int_ceildivpow2(tilec->y1, levelno);
278 /* band border (global) */
279 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
280 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
281 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
282 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
285 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
286 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
287 numbps = image->comps[compno].prec + gain;
289 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
290 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
292 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
294 for (i = 0; i < res->pw * res->ph * 3; i++) {
295 band->precincts[i].imsbtree = NULL;
296 band->precincts[i].incltree = NULL;
297 band->precincts[i].cblks.enc = NULL;
300 for (precno = 0; precno < res->pw * res->ph; precno++) {
301 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
303 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
304 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
305 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
306 int cbgyend = cbgystart + (1 << cbgheightexpn);
308 opj_tcd_precinct_t *prc = &band->precincts[precno];
310 /* precinct size (global) */
311 prc->x0 = int_max(cbgxstart, band->x0);
312 prc->y0 = int_max(cbgystart, band->y0);
313 prc->x1 = int_min(cbgxend, band->x1);
314 prc->y1 = int_min(cbgyend, band->y1);
316 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
317 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
318 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
319 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
320 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
321 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
323 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
324 prc->incltree = tgt_create(prc->cw, prc->ch);
325 prc->imsbtree = tgt_create(prc->cw, prc->ch);
327 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
328 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
329 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
330 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
331 int cblkyend = cblkystart + (1 << cblkheightexpn);
333 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
335 /* code-block size (global) */
336 cblk->x0 = int_max(cblkxstart, prc->x0);
337 cblk->y0 = int_max(cblkystart, prc->y0);
338 cblk->x1 = int_min(cblkxend, prc->x1);
339 cblk->y1 = int_min(cblkyend, prc->y1);
340 cblk->data = (unsigned char*) opj_calloc(9728+2, sizeof(unsigned char));
341 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
345 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
346 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
354 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
357 void tcd_free_encode(opj_tcd_t *tcd) {
358 int tileno, compno, resno, bandno, precno, cblkno;
360 for (tileno = 0; tileno < 1; tileno++) {
361 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
363 for (compno = 0; compno < tile->numcomps; compno++) {
364 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
366 for (resno = 0; resno < tilec->numresolutions; resno++) {
367 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
369 for (bandno = 0; bandno < res->numbands; bandno++) {
370 opj_tcd_band_t *band = &res->bands[bandno];
372 for (precno = 0; precno < res->pw * res->ph; precno++) {
373 opj_tcd_precinct_t *prc = &band->precincts[precno];
375 if (prc->incltree != NULL) {
376 tgt_destroy(prc->incltree);
377 prc->incltree = NULL;
379 if (prc->imsbtree != NULL) {
380 tgt_destroy(prc->imsbtree);
381 prc->imsbtree = NULL;
383 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
384 opj_free(prc->cblks.enc[cblkno].data - 2);
385 opj_free(prc->cblks.enc[cblkno].layers);
386 opj_free(prc->cblks.enc[cblkno].passes);
388 opj_free(prc->cblks.enc);
390 opj_free(band->precincts);
391 band->precincts = NULL;
394 opj_free(tilec->resolutions);
395 tilec->resolutions = NULL;
397 opj_free(tile->comps);
400 opj_free(tcd->tcd_image->tiles);
401 tcd->tcd_image->tiles = NULL;
404 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
405 int tileno, compno, resno, bandno, precno, cblkno;
407 for (tileno = 0; tileno < 1; tileno++) {
408 opj_tcp_t *tcp = &cp->tcps[curtileno];
410 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
411 int p = curtileno % cp->tw;
412 int q = curtileno / cp->tw;
414 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
416 /* 4 borders of the tile rescale on the image if necessary */
417 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
418 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
419 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
420 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
422 tile->numcomps = image->numcomps;
423 /* tile->PPT=image->PPT; */
425 /* Modification of the RATE >> */
426 for (j = 0; j < tcp->numlayers; j++) {
427 tcp->rates[j] = tcp->rates[j] ?
429 (((float) (tile->numcomps
430 * (tile->x1 - tile->x0)
431 * (tile->y1 - tile->y0)
432 * image->comps[0].prec))
433 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
435 ((float) (tile->numcomps
436 * (tile->x1 - tile->x0)
437 * (tile->y1 - tile->y0)
438 * image->comps[0].prec))/
439 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
443 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
444 tcp->rates[j] = tcp->rates[j - 1] + 20;
446 if (!j && tcp->rates[j] < 30)
451 /* << Modification of the RATE */
453 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
454 for (compno = 0; compno < tile->numcomps; compno++) {
455 opj_tccp_t *tccp = &tcp->tccps[compno];
457 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
459 /* border of each tile component (global) */
460 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
461 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
462 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
463 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
465 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
466 tilec->numresolutions = tccp->numresolutions;
467 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
468 for (resno = 0; resno < tilec->numresolutions; resno++) {
471 int levelno = tilec->numresolutions - 1 - resno;
472 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
473 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
474 int cbgwidthexpn, cbgheightexpn;
475 int cblkwidthexpn, cblkheightexpn;
477 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
479 /* border for each resolution level (global) */
480 res->x0 = int_ceildivpow2(tilec->x0, levelno);
481 res->y0 = int_ceildivpow2(tilec->y0, levelno);
482 res->x1 = int_ceildivpow2(tilec->x1, levelno);
483 res->y1 = int_ceildivpow2(tilec->y1, levelno);
484 res->numbands = resno == 0 ? 1 : 3;
486 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
487 if (tccp->csty & J2K_CCP_CSTY_PRT) {
488 pdx = tccp->prcw[resno];
489 pdy = tccp->prch[resno];
494 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
495 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
496 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
497 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
498 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
500 res->pw = (brprcxend - tlprcxstart) >> pdx;
501 res->ph = (brprcyend - tlprcystart) >> pdy;
504 tlcbgxstart = tlprcxstart;
505 tlcbgystart = tlprcystart;
506 brcbgxend = brprcxend;
507 brcbgyend = brprcyend;
511 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
512 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
513 brcbgxend = int_ceildivpow2(brprcxend, 1);
514 brcbgyend = int_ceildivpow2(brprcyend, 1);
515 cbgwidthexpn = pdx - 1;
516 cbgheightexpn = pdy - 1;
521 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
522 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
524 for (bandno = 0; bandno < res->numbands; bandno++) {
527 opj_stepsize_t *ss = NULL;
529 opj_tcd_band_t *band = &res->bands[bandno];
531 band->bandno = resno == 0 ? 0 : bandno + 1;
532 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
533 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
535 if (band->bandno == 0) {
537 band->x0 = int_ceildivpow2(tilec->x0, levelno);
538 band->y0 = int_ceildivpow2(tilec->y0, levelno);
539 band->x1 = int_ceildivpow2(tilec->x1, levelno);
540 band->y1 = int_ceildivpow2(tilec->y1, levelno);
542 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
543 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
544 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
545 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
548 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
549 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
550 numbps = image->comps[compno].prec + gain;
551 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
552 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
554 for (precno = 0; precno < res->pw * res->ph; precno++) {
555 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
557 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
558 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
559 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
560 int cbgyend = cbgystart + (1 << cbgheightexpn);
562 opj_tcd_precinct_t *prc = &band->precincts[precno];
564 /* precinct size (global) */
565 prc->x0 = int_max(cbgxstart, band->x0);
566 prc->y0 = int_max(cbgystart, band->y0);
567 prc->x1 = int_min(cbgxend, band->x1);
568 prc->y1 = int_min(cbgyend, band->y1);
570 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
571 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
572 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
573 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
574 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
575 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
577 opj_free(prc->cblks.enc);
578 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
580 if (prc->incltree != NULL) {
581 tgt_destroy(prc->incltree);
583 if (prc->imsbtree != NULL) {
584 tgt_destroy(prc->imsbtree);
587 prc->incltree = tgt_create(prc->cw, prc->ch);
588 prc->imsbtree = tgt_create(prc->cw, prc->ch);
590 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
591 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
592 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
593 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
594 int cblkyend = cblkystart + (1 << cblkheightexpn);
596 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
598 /* code-block size (global) */
599 cblk->x0 = int_max(cblkxstart, prc->x0);
600 cblk->y0 = int_max(cblkystart, prc->y0);
601 cblk->x1 = int_min(cblkxend, prc->x1);
602 cblk->y1 = int_min(cblkyend, prc->y1);
603 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
604 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
608 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
609 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
617 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
620 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
621 int i, j, tileno, p, q;
622 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
625 tcd->tcd_image->tw = cp->tw;
626 tcd->tcd_image->th = cp->th;
627 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(cp->tw * cp->th, sizeof(opj_tcd_tile_t));
630 Allocate place to store the decoded data = final image
631 Place limited by the tile really present in the codestream
634 for (j = 0; j < cp->tileno_size; j++) {
635 opj_tcd_tile_t *tile;
637 tileno = cp->tileno[j];
638 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
639 tile->numcomps = image->numcomps;
640 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
643 for (i = 0; i < image->numcomps; i++) {
644 for (j = 0; j < cp->tileno_size; j++) {
645 opj_tcd_tile_t *tile;
646 opj_tcd_tilecomp_t *tilec;
648 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
650 tileno = cp->tileno[j];
652 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
653 tilec = &tile->comps[i];
655 p = tileno % cp->tw; /* si numerotation matricielle .. */
656 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
658 /* 4 borders of the tile rescale on the image if necessary */
659 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
660 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
661 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
662 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
664 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
665 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
666 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
667 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
669 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
670 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
671 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
672 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
675 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
676 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
678 image->comps[i].w = w;
679 image->comps[i].h = h;
680 image->comps[i].x0 = x0;
681 image->comps[i].y0 = y0;
685 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
686 int compno, resno, bandno, precno, cblkno;
688 opj_tcd_tile_t *tile;
690 OPJ_ARG_NOT_USED(cstr_info);
694 tcp = &(cp->tcps[cp->tileno[tileno]]);
695 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
697 tileno = cp->tileno[tileno];
699 for (compno = 0; compno < tile->numcomps; compno++) {
700 opj_tccp_t *tccp = &tcp->tccps[compno];
701 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
703 if (tccp->numresolutions <= 0)
705 cp->tileno[tileno] = -1;
709 /* border of each tile component (global) */
710 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
711 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
712 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
713 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
715 tilec->numresolutions = tccp->numresolutions;
716 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
718 for (resno = 0; resno < tilec->numresolutions; resno++) {
720 int levelno = tilec->numresolutions - 1 - resno;
721 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
722 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
723 int cbgwidthexpn, cbgheightexpn;
724 int cblkwidthexpn, cblkheightexpn;
726 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
728 /* border for each resolution level (global) */
729 res->x0 = int_ceildivpow2(tilec->x0, levelno);
730 res->y0 = int_ceildivpow2(tilec->y0, levelno);
731 res->x1 = int_ceildivpow2(tilec->x1, levelno);
732 res->y1 = int_ceildivpow2(tilec->y1, levelno);
733 res->numbands = resno == 0 ? 1 : 3;
735 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
736 if (tccp->csty & J2K_CCP_CSTY_PRT) {
737 pdx = tccp->prcw[resno];
738 pdy = tccp->prch[resno];
744 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
745 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
746 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
747 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
748 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
750 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
751 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
754 tlcbgxstart = tlprcxstart;
755 tlcbgystart = tlprcystart;
756 brcbgxend = brprcxend;
757 brcbgyend = brprcyend;
761 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
762 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
763 brcbgxend = int_ceildivpow2(brprcxend, 1);
764 brcbgyend = int_ceildivpow2(brprcyend, 1);
765 cbgwidthexpn = pdx - 1;
766 cbgheightexpn = pdy - 1;
771 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
772 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
774 for (bandno = 0; bandno < res->numbands; bandno++) {
777 opj_stepsize_t *ss = NULL;
779 opj_tcd_band_t *band = &res->bands[bandno];
780 band->bandno = resno == 0 ? 0 : bandno + 1;
781 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
782 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
784 if (band->bandno == 0) {
785 /* band border (global) */
786 band->x0 = int_ceildivpow2(tilec->x0, levelno);
787 band->y0 = int_ceildivpow2(tilec->y0, levelno);
788 band->x1 = int_ceildivpow2(tilec->x1, levelno);
789 band->y1 = int_ceildivpow2(tilec->y1, levelno);
791 /* band border (global) */
792 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
793 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
794 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
795 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
798 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
799 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
800 numbps = image->comps[compno].prec + gain;
801 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
802 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
804 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
806 for (precno = 0; precno < res->pw * res->ph; precno++) {
807 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
808 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
809 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
810 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
811 int cbgyend = cbgystart + (1 << cbgheightexpn);
813 opj_tcd_precinct_t *prc = &band->precincts[precno];
814 /* precinct size (global) */
815 prc->x0 = int_max(cbgxstart, band->x0);
816 prc->y0 = int_max(cbgystart, band->y0);
817 prc->x1 = int_min(cbgxend, band->x1);
818 prc->y1 = int_min(cbgyend, band->y1);
820 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
821 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
822 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
823 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
824 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
825 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
827 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
829 prc->incltree = tgt_create(prc->cw, prc->ch);
830 prc->imsbtree = tgt_create(prc->cw, prc->ch);
832 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
833 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
834 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
835 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
836 int cblkyend = cblkystart + (1 << cblkheightexpn);
838 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
841 /* code-block size (global) */
842 cblk->x0 = int_max(cblkxstart, prc->x0);
843 cblk->y0 = int_max(cblkystart, prc->y0);
844 cblk->x1 = int_min(cblkxend, prc->x1);
845 cblk->y1 = int_min(cblkyend, prc->y1);
852 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
855 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
856 int compno, resno, bandno, precno, cblkno;
857 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
858 int matrice[10][10][3];
861 opj_cp_t *cp = tcd->cp;
862 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
863 opj_tcp_t *tcd_tcp = tcd->tcp;
865 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
867 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
868 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
869 for (i = 0; i < tcd_tcp->numlayers; i++) {
870 for (j = 0; j < tilec->numresolutions; j++) {
871 for (k = 0; k < 3; k++) {
873 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
874 * (float) (tcd->image->comps[compno].prec / 16.0));
879 for (resno = 0; resno < tilec->numresolutions; resno++) {
880 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
881 for (bandno = 0; bandno < res->numbands; bandno++) {
882 opj_tcd_band_t *band = &res->bands[bandno];
883 for (precno = 0; precno < res->pw * res->ph; precno++) {
884 opj_tcd_precinct_t *prc = &band->precincts[precno];
885 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
886 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
887 opj_tcd_layer_t *layer = &cblk->layers[layno];
889 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
890 /* Correction of the matrix of coefficient to include the IMSB information */
892 value = matrice[layno][resno][bandno];
899 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
900 if (imsb >= matrice[layno - 1][resno][bandno]) {
901 value -= (imsb - matrice[layno - 1][resno][bandno]);
909 cblk->numpassesinlayers = 0;
912 n = cblk->numpassesinlayers;
913 if (cblk->numpassesinlayers == 0) {
915 n = 3 * value - 2 + cblk->numpassesinlayers;
917 n = cblk->numpassesinlayers;
920 n = 3 * value + cblk->numpassesinlayers;
923 layer->numpasses = n - cblk->numpassesinlayers;
925 if (!layer->numpasses)
928 if (cblk->numpassesinlayers == 0) {
929 layer->len = cblk->passes[n - 1].rate;
930 layer->data = cblk->data;
932 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
933 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
936 cblk->numpassesinlayers = n;
944 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
946 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
947 tcd_makelayer_fixed(tcd, layno, 1);
951 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
952 int compno, resno, bandno, precno, cblkno, passno;
954 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
956 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
958 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
959 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
960 for (resno = 0; resno < tilec->numresolutions; resno++) {
961 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
962 for (bandno = 0; bandno < res->numbands; bandno++) {
963 opj_tcd_band_t *band = &res->bands[bandno];
964 for (precno = 0; precno < res->pw * res->ph; precno++) {
965 opj_tcd_precinct_t *prc = &band->precincts[precno];
966 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
967 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
968 opj_tcd_layer_t *layer = &cblk->layers[layno];
972 cblk->numpassesinlayers = 0;
974 n = cblk->numpassesinlayers;
975 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
978 opj_tcd_pass_t *pass = &cblk->passes[passno];
981 dd = pass->distortiondec;
983 dr = pass->rate - cblk->passes[n - 1].rate;
984 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
991 if (dd / dr >= thresh)
994 layer->numpasses = n - cblk->numpassesinlayers;
996 if (!layer->numpasses) {
1000 if (cblk->numpassesinlayers == 0) {
1001 layer->len = cblk->passes[n - 1].rate;
1002 layer->data = cblk->data;
1003 layer->disto = cblk->passes[n - 1].distortiondec;
1005 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1006 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1007 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1010 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1013 cblk->numpassesinlayers = n;
1021 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1022 int compno, resno, bandno, precno, cblkno, passno, layno;
1024 double cumdisto[100]; /* fixed_quality */
1025 const double K = 1; /* 1.1; fixed_quality */
1028 opj_cp_t *cp = tcd->cp;
1029 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1030 opj_tcp_t *tcd_tcp = tcd->tcp;
1035 tcd_tile->numpix = 0; /* fixed_quality */
1037 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1038 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1041 for (resno = 0; resno < tilec->numresolutions; resno++) {
1042 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1044 for (bandno = 0; bandno < res->numbands; bandno++) {
1045 opj_tcd_band_t *band = &res->bands[bandno];
1047 for (precno = 0; precno < res->pw * res->ph; precno++) {
1048 opj_tcd_precinct_t *prc = &band->precincts[precno];
1050 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1051 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1053 for (passno = 0; passno < cblk->totalpasses; passno++) {
1054 opj_tcd_pass_t *pass = &cblk->passes[passno];
1059 dd = pass->distortiondec;
1061 dr = pass->rate - cblk->passes[passno - 1].rate;
1062 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1068 if (rdslope < min) {
1071 if (rdslope > max) {
1077 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1078 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1084 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1085 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1086 * ((double)(tilec->numpix));
1091 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1092 tile_info->numpix = tcd_tile->numpix;
1093 tile_info->distotile = tcd_tile->distotile;
1094 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1097 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1101 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1102 double goodthresh = 0;
1103 double stable_thresh = 0;
1105 double distotarget; /* fixed_quality */
1108 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1110 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1111 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1112 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1113 ==> possible to have some lossy layers and the last layer for sure lossless */
1114 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1115 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1118 for (i = 0; i < 128; i++) {
1120 double distoachieved = 0; /* fixed_quality */
1121 thresh = (lo + hi) / 2;
1123 tcd_makelayer(tcd, layno, thresh, 0);
1125 if (cp->fixed_quality) { /* fixed_quality */
1127 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
1132 distoachieved = layno == 0 ?
1133 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1134 if (distoachieved < distotarget) {
1136 stable_thresh = thresh;
1143 distoachieved = (layno == 0) ?
1144 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1145 if (distoachieved < distotarget) {
1147 stable_thresh = thresh;
1153 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
1154 /* TODO: what to do with l ??? seek / tell ??? */
1155 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1161 stable_thresh = thresh;
1165 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1176 if(cstr_info) { /* Threshold for Marcela Index */
1177 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1179 tcd_makelayer(tcd, layno, goodthresh, 1);
1182 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1188 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1190 int l, i, numpacks = 0;
1191 opj_tcd_tile_t *tile = NULL;
1192 opj_tcp_t *tcd_tcp = NULL;
1193 opj_cp_t *cp = NULL;
1195 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1196 opj_tccp_t *tccp = &tcp->tccps[0];
1197 opj_image_t *image = tcd->image;
1199 opj_t1_t *t1 = NULL; /* T1 component */
1200 opj_t2_t *t2 = NULL; /* T2 component */
1202 tcd->tcd_tileno = tileno;
1203 tcd->tcd_tile = tcd->tcd_image->tiles;
1204 tcd->tcp = &tcd->cp->tcps[tileno];
1206 tile = tcd->tcd_tile;
1210 if(tcd->cur_tp_num == 0){
1211 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1212 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1214 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1215 for (i = 0; i < tilec_idx->numresolutions; i++) {
1216 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1218 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1219 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1221 numpacks += res_idx->pw * res_idx->ph;
1223 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1224 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1226 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1230 /*---------------TILE-------------------*/
1232 for (compno = 0; compno < tile->numcomps; compno++) {
1235 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1236 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1237 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1239 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1240 int tw = tilec->x1 - tilec->x0;
1241 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1243 /* extract tile data */
1245 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1246 for (y = tilec->y0; y < tilec->y1; y++) {
1247 /* start of the src tile scanline */
1248 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1249 /* start of the dst tile scanline */
1250 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1251 for (x = tilec->x0; x < tilec->x1; x++) {
1252 *tile_data++ = *data++ - adjust;
1255 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1256 for (y = tilec->y0; y < tilec->y1; y++) {
1257 /* start of the src tile scanline */
1258 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1259 /* start of the dst tile scanline */
1260 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1261 for (x = tilec->x0; x < tilec->x1; x++) {
1262 *tile_data++ = (*data++ - adjust) << 11;
1269 /*----------------MCT-------------------*/
1271 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1272 if (tcd_tcp->tccps[0].qmfbid == 0) {
1273 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1275 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1279 /*----------------DWT---------------------*/
1281 for (compno = 0; compno < tile->numcomps; compno++) {
1282 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1283 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1285 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1286 dwt_encode_real(tilec);
1290 /*------------------TIER1-----------------*/
1291 t1 = t1_create(tcd->cinfo);
1292 t1_encode_cblks(t1, tile, tcd_tcp);
1295 /*-----------RATE-ALLOCATE------------------*/
1299 cstr_info->index_write = 0;
1301 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1302 /* Normal Rate/distortion allocation */
1303 tcd_rateallocate(tcd, dest, len, cstr_info);
1305 /* Fixed layer allocation */
1306 tcd_rateallocate_fixed(tcd);
1309 /*--------------TIER2------------------*/
1313 cstr_info->index_write = 1;
1316 t2 = t2_create(tcd->cinfo, image, cp);
1317 l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
1320 /*---------------CLEAN-------------------*/
1323 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1324 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1325 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1327 /* cleaning memory */
1328 for (compno = 0; compno < tile->numcomps; compno++) {
1329 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1330 opj_aligned_free(tilec->data);
1337 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1341 double tile_time, t1_time, dwt_time;
1342 opj_tcd_tile_t *tile = NULL;
1344 opj_t1_t *t1 = NULL; /* T1 component */
1345 opj_t2_t *t2 = NULL; /* T2 component */
1347 tcd->tcd_tileno = tileno;
1348 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1349 tcd->tcp = &(tcd->cp->tcps[tileno]);
1350 tile = tcd->tcd_tile;
1352 tile_time = opj_clock(); /* time needed to decode a tile */
1353 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1357 int resno, compno, numprec = 0;
1358 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1359 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1360 opj_tccp_t *tccp = &tcp->tccps[compno];
1361 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1362 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1363 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1364 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1365 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1366 numprec += res_idx->pw * res_idx->ph;
1367 if (tccp->csty & J2K_CP_CSTY_PRT) {
1368 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1369 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1372 cstr_info->tile[tileno].pdx[resno] = 15;
1373 cstr_info->tile[tileno].pdy[resno] = 15;
1377 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1378 cstr_info->packno = 0;
1382 /*--------------TIER2------------------*/
1384 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1385 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1390 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1394 /*------------------TIER1-----------------*/
1396 t1_time = opj_clock(); /* time needed to decode a tile */
1397 t1 = t1_create(tcd->cinfo);
1400 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1405 for (compno = 0; compno < tile->numcomps; ++compno) {
1406 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1407 /* The +3 is headroom required by the vectorized DWT */
1408 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1409 if (tilec->data == NULL)
1411 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1416 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1419 t1_time = opj_clock() - t1_time;
1420 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1422 /*----------------DWT---------------------*/
1424 dwt_time = opj_clock(); /* time needed to decode a tile */
1425 for (compno = 0; compno < tile->numcomps; compno++) {
1426 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1429 if (tcd->cp->reduce != 0) {
1430 if ( tile->comps[compno].numresolutions < ( tcd->cp->reduce - 1 ) ) {
1431 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1432 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1436 tcd->image->comps[compno].resno_decoded =
1437 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1441 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1442 if(numres2decode > 0){
1443 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1444 dwt_decode(tilec, numres2decode);
1446 dwt_decode_real(tilec, numres2decode);
1450 dwt_time = opj_clock() - dwt_time;
1451 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1453 /*----------------MCT-------------------*/
1455 if (tcd->tcp->mct) {
1456 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1458 if (tile->numcomps >= 3 ){
1459 /* testcase 1336.pdf.asan.47.376 */
1460 if ((tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) < n ||
1461 ( tile->comps[1].x1 - tile->comps[1].x0) * (tile->comps[1].y1 - tile->comps[1].y0) < n ||
1462 ( tile->comps[2].x1 - tile->comps[2].x0) * (tile->comps[2].y1 - tile->comps[2].y0) < n) {
1463 opj_event_msg(tcd->cinfo, EVT_ERROR, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1466 if (tcd->tcp->tccps[0].qmfbid == 1) {
1468 tile->comps[0].data,
1469 tile->comps[1].data,
1470 tile->comps[2].data,
1474 (float*)tile->comps[0].data,
1475 (float*)tile->comps[1].data,
1476 (float*)tile->comps[2].data,
1480 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1484 /*---------------TILE-------------------*/
1486 for (compno = 0; compno < tile->numcomps; ++compno) {
1487 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1488 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1489 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1490 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1491 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1492 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1494 int tw = tilec->x1 - tilec->x0;
1498 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1499 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1500 /* NR-DEC-2977.pdf.asan.67.2198.jp2-52-decode */
1501 if( res->x0 - offset_x < 0 || res->x1 - offset_x < 0
1502 || res->y0 - offset_y < 0 || res->y1 - offset_y < 0 )
1504 opj_event_msg(tcd->cinfo, EVT_ERROR, "Impossible offsets %d / %d\n", offset_x, offset_y);
1507 assert( 0 <= res->x0 - offset_x && 0 <= res->x1 - offset_x );
1508 assert( 0 <= res->y0 - offset_y && 0 <= res->y1 - offset_y );
1511 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1515 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1518 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1519 for(j = res->y0; j < res->y1; ++j) {
1520 for(i = res->x0; i < res->x1; ++i) {
1521 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1523 /*assert( (i - offset_x) + (j - offset_y) * w >= 0 );*/
1524 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1528 for(j = res->y0; j < res->y1; ++j) {
1529 for(i = res->x0; i < res->x1; ++i) {
1530 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1531 int v = lrintf(tmp);
1533 /*assert( (i - offset_x) + (j - offset_y) * w >= 0 );*/
1534 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1538 opj_aligned_free(tilec->data);
1541 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1542 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1551 void tcd_free_decode(opj_tcd_t *tcd) {
1552 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1554 for (i = 0; i < tcd_image->tw * tcd_image->th; i++)
1556 tcd_free_decode_tile(tcd, i);
1559 opj_free(tcd_image->tiles);
1562 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1563 int compno,resno,bandno,precno,cblkno;
1565 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1567 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1568 if (tile->comps != NULL) {
1569 for (compno = 0; compno < tile->numcomps; compno++) {
1570 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1571 for (resno = 0; resno < tilec->numresolutions; resno++) {
1572 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1573 for (bandno = 0; bandno < res->numbands; bandno++) {
1574 opj_tcd_band_t *band = &res->bands[bandno];
1575 for (precno = 0; precno < res->ph * res->pw; precno++) {
1576 opj_tcd_precinct_t *prec = &band->precincts[precno];
1577 if (prec->cblks.dec != NULL) {
1578 for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
1579 opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
1580 opj_free(cblk->data);
1581 opj_free(cblk->segs);
1583 opj_free(prec->cblks.dec);
1585 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1586 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1590 opj_free(band->precincts);
1593 opj_free(tilec->resolutions);
1595 opj_free(tile->comps);