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"
36 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
37 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
39 fprintf(fd, "image {\n");
40 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
41 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
43 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
44 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
45 fprintf(fd, " tile {\n");
46 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
47 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
48 for (compno = 0; compno < tile->numcomps; compno++) {
49 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
50 fprintf(fd, " tilec {\n");
52 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
53 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
54 for (resno = 0; resno < tilec->numresolutions; resno++) {
55 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
56 fprintf(fd, "\n res {\n");
58 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
59 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
60 for (bandno = 0; bandno < res->numbands; bandno++) {
61 opj_tcd_band_t *band = &res->bands[bandno];
62 fprintf(fd, " band {\n");
64 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
65 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
66 for (precno = 0; precno < res->pw * res->ph; precno++) {
67 opj_tcd_precinct_t *prec = &band->precincts[precno];
68 fprintf(fd, " prec {\n");
70 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
71 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
73 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
74 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
75 fprintf(fd, " cblk {\n");
77 " x0=%d, y0=%d, x1=%d, y1=%d\n",
78 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
95 /* ----------------------------------------------------------------------- */
98 Create a new TCD handle
100 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
101 /* create the tcd structure */
102 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
103 if(!tcd) return NULL;
105 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
106 if(!tcd->tcd_image) {
115 Destroy a previously created TCD handle
117 void tcd_destroy(opj_tcd_t *tcd) {
119 opj_free(tcd->tcd_image);
124 /* ----------------------------------------------------------------------- */
126 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
127 int tileno, compno, resno, bandno, precno, cblkno;
131 tcd->tcd_image->tw = cp->tw;
132 tcd->tcd_image->th = cp->th;
133 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
135 for (tileno = 0; tileno < 1; tileno++) {
136 opj_tcp_t *tcp = &cp->tcps[curtileno];
139 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
140 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
141 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
143 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
144 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
146 /* 4 borders of the tile rescale on the image if necessary */
147 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
148 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
149 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
150 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
151 tile->numcomps = image->numcomps;
152 /* tile->PPT=image->PPT; */
154 /* Modification of the RATE >> */
155 for (j = 0; j < tcp->numlayers; j++) {
156 tcp->rates[j] = tcp->rates[j] ?
158 (((float) (tile->numcomps
159 * (tile->x1 - tile->x0)
160 * (tile->y1 - tile->y0)
161 * image->comps[0].prec))
162 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
164 ((float) (tile->numcomps
165 * (tile->x1 - tile->x0)
166 * (tile->y1 - tile->y0)
167 * image->comps[0].prec))/
168 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
172 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
173 tcp->rates[j] = tcp->rates[j - 1] + 20;
175 if (!j && tcp->rates[j] < 30)
179 if(j == (tcp->numlayers-1)){
180 tcp->rates[j] = tcp->rates[j]- 2;
184 /* << Modification of the RATE */
186 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
187 for (compno = 0; compno < tile->numcomps; compno++) {
188 opj_tccp_t *tccp = &tcp->tccps[compno];
190 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
192 /* border of each tile component (global) */
193 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
194 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
195 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
196 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
198 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
199 tilec->numresolutions = tccp->numresolutions;
201 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
203 for (resno = 0; resno < tilec->numresolutions; resno++) {
205 int levelno = tilec->numresolutions - 1 - resno;
206 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
207 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
208 int cbgwidthexpn, cbgheightexpn;
209 int cblkwidthexpn, cblkheightexpn;
211 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
213 /* border for each resolution level (global) */
214 res->x0 = int_ceildivpow2(tilec->x0, levelno);
215 res->y0 = int_ceildivpow2(tilec->y0, levelno);
216 res->x1 = int_ceildivpow2(tilec->x1, levelno);
217 res->y1 = int_ceildivpow2(tilec->y1, levelno);
219 res->numbands = resno == 0 ? 1 : 3;
220 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
221 if (tccp->csty & J2K_CCP_CSTY_PRT) {
222 pdx = tccp->prcw[resno];
223 pdy = tccp->prch[resno];
228 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
229 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
230 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
232 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
233 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
235 res->pw = (brprcxend - tlprcxstart) >> pdx;
236 res->ph = (brprcyend - tlprcystart) >> pdy;
239 tlcbgxstart = tlprcxstart;
240 tlcbgystart = tlprcystart;
241 brcbgxend = brprcxend;
242 brcbgyend = brprcyend;
246 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
247 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
248 brcbgxend = int_ceildivpow2(brprcxend, 1);
249 brcbgyend = int_ceildivpow2(brprcyend, 1);
250 cbgwidthexpn = pdx - 1;
251 cbgheightexpn = pdy - 1;
254 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
255 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
257 for (bandno = 0; bandno < res->numbands; bandno++) {
260 opj_stepsize_t *ss = NULL;
262 opj_tcd_band_t *band = &res->bands[bandno];
264 band->bandno = resno == 0 ? 0 : bandno + 1;
265 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
266 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
268 if (band->bandno == 0) {
269 /* band border (global) */
270 band->x0 = int_ceildivpow2(tilec->x0, levelno);
271 band->y0 = int_ceildivpow2(tilec->y0, levelno);
272 band->x1 = int_ceildivpow2(tilec->x1, levelno);
273 band->y1 = int_ceildivpow2(tilec->y1, levelno);
275 /* band border (global) */
276 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
277 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
278 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
279 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
282 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
283 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
284 numbps = image->comps[compno].prec + gain;
286 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
287 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
289 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
291 for (i = 0; i < res->pw * res->ph * 3; i++) {
292 band->precincts[i].imsbtree = NULL;
293 band->precincts[i].incltree = NULL;
294 band->precincts[i].cblks.enc = NULL;
297 for (precno = 0; precno < res->pw * res->ph; precno++) {
298 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
300 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
301 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
302 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
303 int cbgyend = cbgystart + (1 << cbgheightexpn);
305 opj_tcd_precinct_t *prc = &band->precincts[precno];
307 /* precinct size (global) */
308 prc->x0 = int_max(cbgxstart, band->x0);
309 prc->y0 = int_max(cbgystart, band->y0);
310 prc->x1 = int_min(cbgxend, band->x1);
311 prc->y1 = int_min(cbgyend, band->y1);
313 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
314 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
315 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
316 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
317 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
318 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
320 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
321 prc->incltree = tgt_create(prc->cw, prc->ch);
322 prc->imsbtree = tgt_create(prc->cw, prc->ch);
324 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
325 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
326 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
327 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
328 int cblkyend = cblkystart + (1 << cblkheightexpn);
330 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
332 /* code-block size (global) */
333 cblk->x0 = int_max(cblkxstart, prc->x0);
334 cblk->y0 = int_max(cblkystart, prc->y0);
335 cblk->x1 = int_min(cblkxend, prc->x1);
336 cblk->y1 = int_min(cblkyend, prc->y1);
337 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
338 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
340 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
341 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
349 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
352 void tcd_free_encode(opj_tcd_t *tcd) {
353 int tileno, compno, resno, bandno, precno, cblkno;
355 for (tileno = 0; tileno < 1; tileno++) {
356 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
358 for (compno = 0; compno < tile->numcomps; compno++) {
359 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
361 for (resno = 0; resno < tilec->numresolutions; resno++) {
362 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
364 for (bandno = 0; bandno < res->numbands; bandno++) {
365 opj_tcd_band_t *band = &res->bands[bandno];
367 for (precno = 0; precno < res->pw * res->ph; precno++) {
368 opj_tcd_precinct_t *prc = &band->precincts[precno];
370 if (prc->incltree != NULL) {
371 tgt_destroy(prc->incltree);
372 prc->incltree = NULL;
374 if (prc->imsbtree != NULL) {
375 tgt_destroy(prc->imsbtree);
376 prc->imsbtree = NULL;
378 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
379 opj_free(prc->cblks.enc[cblkno].data - 2);
380 opj_free(prc->cblks.enc[cblkno].layers);
381 opj_free(prc->cblks.enc[cblkno].passes);
383 opj_free(prc->cblks.enc);
385 opj_free(band->precincts);
386 band->precincts = NULL;
389 opj_free(tilec->resolutions);
390 tilec->resolutions = NULL;
392 opj_free(tile->comps);
395 opj_free(tcd->tcd_image->tiles);
396 tcd->tcd_image->tiles = NULL;
399 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
400 int tileno, compno, resno, bandno, precno, cblkno;
402 for (tileno = 0; tileno < 1; tileno++) {
403 opj_tcp_t *tcp = &cp->tcps[curtileno];
405 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
406 int p = curtileno % cp->tw;
407 int q = curtileno / cp->tw;
409 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
411 /* 4 borders of the tile rescale on the image if necessary */
412 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
413 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
414 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
415 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
417 tile->numcomps = image->numcomps;
418 /* tile->PPT=image->PPT; */
420 /* Modification of the RATE >> */
421 for (j = 0; j < tcp->numlayers; j++) {
422 tcp->rates[j] = tcp->rates[j] ?
424 (((float) (tile->numcomps
425 * (tile->x1 - tile->x0)
426 * (tile->y1 - tile->y0)
427 * image->comps[0].prec))
428 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
430 ((float) (tile->numcomps
431 * (tile->x1 - tile->x0)
432 * (tile->y1 - tile->y0)
433 * image->comps[0].prec))/
434 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
438 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
439 tcp->rates[j] = tcp->rates[j - 1] + 20;
441 if (!j && tcp->rates[j] < 30)
446 /* << Modification of the RATE */
448 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
449 for (compno = 0; compno < tile->numcomps; compno++) {
450 opj_tccp_t *tccp = &tcp->tccps[compno];
452 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
454 /* border of each tile component (global) */
455 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
456 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
457 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
458 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
460 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
461 tilec->numresolutions = tccp->numresolutions;
462 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
463 for (resno = 0; resno < tilec->numresolutions; resno++) {
466 int levelno = tilec->numresolutions - 1 - resno;
467 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
468 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
469 int cbgwidthexpn, cbgheightexpn;
470 int cblkwidthexpn, cblkheightexpn;
472 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
474 /* border for each resolution level (global) */
475 res->x0 = int_ceildivpow2(tilec->x0, levelno);
476 res->y0 = int_ceildivpow2(tilec->y0, levelno);
477 res->x1 = int_ceildivpow2(tilec->x1, levelno);
478 res->y1 = int_ceildivpow2(tilec->y1, levelno);
479 res->numbands = resno == 0 ? 1 : 3;
481 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
482 if (tccp->csty & J2K_CCP_CSTY_PRT) {
483 pdx = tccp->prcw[resno];
484 pdy = tccp->prch[resno];
489 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
490 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
491 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
492 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
493 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
495 res->pw = (brprcxend - tlprcxstart) >> pdx;
496 res->ph = (brprcyend - tlprcystart) >> pdy;
499 tlcbgxstart = tlprcxstart;
500 tlcbgystart = tlprcystart;
501 brcbgxend = brprcxend;
502 brcbgyend = brprcyend;
506 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
507 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
508 brcbgxend = int_ceildivpow2(brprcxend, 1);
509 brcbgyend = int_ceildivpow2(brprcyend, 1);
510 cbgwidthexpn = pdx - 1;
511 cbgheightexpn = pdy - 1;
514 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
515 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
517 for (bandno = 0; bandno < res->numbands; bandno++) {
520 opj_stepsize_t *ss = NULL;
522 opj_tcd_band_t *band = &res->bands[bandno];
524 band->bandno = resno == 0 ? 0 : bandno + 1;
525 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
526 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
528 if (band->bandno == 0) {
530 band->x0 = int_ceildivpow2(tilec->x0, levelno);
531 band->y0 = int_ceildivpow2(tilec->y0, levelno);
532 band->x1 = int_ceildivpow2(tilec->x1, levelno);
533 band->y1 = int_ceildivpow2(tilec->y1, levelno);
535 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
536 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
537 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
538 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
541 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
542 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
543 numbps = image->comps[compno].prec + gain;
544 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
545 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
547 for (precno = 0; precno < res->pw * res->ph; precno++) {
548 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
550 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
551 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
552 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
553 int cbgyend = cbgystart + (1 << cbgheightexpn);
555 opj_tcd_precinct_t *prc = &band->precincts[precno];
557 /* precinct size (global) */
558 prc->x0 = int_max(cbgxstart, band->x0);
559 prc->y0 = int_max(cbgystart, band->y0);
560 prc->x1 = int_min(cbgxend, band->x1);
561 prc->y1 = int_min(cbgyend, band->y1);
563 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
564 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
565 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
566 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
567 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
568 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
570 opj_free(prc->cblks.enc);
571 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
573 if (prc->incltree != NULL) {
574 tgt_destroy(prc->incltree);
576 if (prc->imsbtree != NULL) {
577 tgt_destroy(prc->imsbtree);
580 prc->incltree = tgt_create(prc->cw, prc->ch);
581 prc->imsbtree = tgt_create(prc->cw, prc->ch);
583 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
584 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
585 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
586 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
587 int cblkyend = cblkystart + (1 << cblkheightexpn);
589 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
591 /* code-block size (global) */
592 cblk->x0 = int_max(cblkxstart, prc->x0);
593 cblk->y0 = int_max(cblkystart, prc->y0);
594 cblk->x1 = int_min(cblkxend, prc->x1);
595 cblk->y1 = int_min(cblkyend, prc->y1);
596 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
597 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
599 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
600 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
608 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
611 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
612 int i, j, tileno, p, q;
613 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
616 tcd->tcd_image->tw = cp->tw;
617 tcd->tcd_image->th = cp->th;
618 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
621 Allocate place to store the decoded data = final image
622 Place limited by the tile really present in the codestream
625 for (j = 0; j < cp->tileno_size; j++) {
626 opj_tcd_tile_t *tile;
628 tileno = cp->tileno[j];
629 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
630 tile->numcomps = image->numcomps;
631 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
634 for (i = 0; i < image->numcomps; i++) {
635 for (j = 0; j < cp->tileno_size; j++) {
636 opj_tcd_tile_t *tile;
637 opj_tcd_tilecomp_t *tilec;
639 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
641 tileno = cp->tileno[j];
643 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
644 tilec = &tile->comps[i];
646 p = tileno % cp->tw; /* si numerotation matricielle .. */
647 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
649 /* 4 borders of the tile rescale on the image if necessary */
650 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
651 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
652 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
653 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
655 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
656 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
657 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
658 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
660 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
661 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
662 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
663 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
666 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
667 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
669 image->comps[i].w = w;
670 image->comps[i].h = h;
671 image->comps[i].x0 = x0;
672 image->comps[i].y0 = y0;
676 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) {
677 int compno, resno, bandno, precno, cblkno;
679 opj_tcd_tile_t *tile;
681 OPJ_ARG_NOT_USED(cstr_info);
685 tcp = &(cp->tcps[cp->tileno[tileno]]);
686 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
688 tileno = cp->tileno[tileno];
690 for (compno = 0; compno < tile->numcomps; compno++) {
691 opj_tccp_t *tccp = &tcp->tccps[compno];
692 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
694 /* border of each tile component (global) */
695 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
696 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
697 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
698 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
700 tilec->numresolutions = tccp->numresolutions;
701 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
703 for (resno = 0; resno < tilec->numresolutions; resno++) {
705 int levelno = tilec->numresolutions - 1 - resno;
706 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
707 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
708 int cbgwidthexpn, cbgheightexpn;
709 int cblkwidthexpn, cblkheightexpn;
711 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
713 /* border for each resolution level (global) */
714 res->x0 = int_ceildivpow2(tilec->x0, levelno);
715 res->y0 = int_ceildivpow2(tilec->y0, levelno);
716 res->x1 = int_ceildivpow2(tilec->x1, levelno);
717 res->y1 = int_ceildivpow2(tilec->y1, levelno);
718 res->numbands = resno == 0 ? 1 : 3;
720 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
721 if (tccp->csty & J2K_CCP_CSTY_PRT) {
722 pdx = tccp->prcw[resno];
723 pdy = tccp->prch[resno];
729 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
730 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
731 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
732 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
733 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
735 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
736 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
739 tlcbgxstart = tlprcxstart;
740 tlcbgystart = tlprcystart;
741 brcbgxend = brprcxend;
742 brcbgyend = brprcyend;
746 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
747 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
748 brcbgxend = int_ceildivpow2(brprcxend, 1);
749 brcbgyend = int_ceildivpow2(brprcyend, 1);
750 cbgwidthexpn = pdx - 1;
751 cbgheightexpn = pdy - 1;
754 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
755 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
757 for (bandno = 0; bandno < res->numbands; bandno++) {
760 opj_stepsize_t *ss = NULL;
762 opj_tcd_band_t *band = &res->bands[bandno];
763 band->bandno = resno == 0 ? 0 : bandno + 1;
764 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
765 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
767 if (band->bandno == 0) {
768 /* band border (global) */
769 band->x0 = int_ceildivpow2(tilec->x0, levelno);
770 band->y0 = int_ceildivpow2(tilec->y0, levelno);
771 band->x1 = int_ceildivpow2(tilec->x1, levelno);
772 band->y1 = int_ceildivpow2(tilec->y1, levelno);
774 /* band border (global) */
775 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
776 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
777 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
778 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
781 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
782 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
783 numbps = image->comps[compno].prec + gain;
784 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
785 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
787 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
789 for (precno = 0; precno < res->pw * res->ph; precno++) {
790 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
791 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
792 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
793 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
794 int cbgyend = cbgystart + (1 << cbgheightexpn);
796 opj_tcd_precinct_t *prc = &band->precincts[precno];
797 /* precinct size (global) */
798 prc->x0 = int_max(cbgxstart, band->x0);
799 prc->y0 = int_max(cbgystart, band->y0);
800 prc->x1 = int_min(cbgxend, band->x1);
801 prc->y1 = int_min(cbgyend, band->y1);
803 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
804 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
805 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
806 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
807 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
808 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
810 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
812 prc->incltree = tgt_create(prc->cw, prc->ch);
813 prc->imsbtree = tgt_create(prc->cw, prc->ch);
815 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
816 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
817 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
818 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
819 int cblkyend = cblkystart + (1 << cblkheightexpn);
821 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
824 /* code-block size (global) */
825 cblk->x0 = int_max(cblkxstart, prc->x0);
826 cblk->y0 = int_max(cblkystart, prc->y0);
827 cblk->x1 = int_min(cblkxend, prc->x1);
828 cblk->y1 = int_min(cblkyend, prc->y1);
835 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
838 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
839 int compno, resno, bandno, precno, cblkno;
840 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
841 int matrice[10][10][3];
844 opj_cp_t *cp = tcd->cp;
845 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
846 opj_tcp_t *tcd_tcp = tcd->tcp;
848 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
850 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
851 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
852 for (i = 0; i < tcd_tcp->numlayers; i++) {
853 for (j = 0; j < tilec->numresolutions; j++) {
854 for (k = 0; k < 3; k++) {
856 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
857 * (float) (tcd->image->comps[compno].prec / 16.0));
862 for (resno = 0; resno < tilec->numresolutions; resno++) {
863 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
864 for (bandno = 0; bandno < res->numbands; bandno++) {
865 opj_tcd_band_t *band = &res->bands[bandno];
866 for (precno = 0; precno < res->pw * res->ph; precno++) {
867 opj_tcd_precinct_t *prc = &band->precincts[precno];
868 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
869 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
870 opj_tcd_layer_t *layer = &cblk->layers[layno];
872 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
873 /* Correction of the matrix of coefficient to include the IMSB information */
875 value = matrice[layno][resno][bandno];
882 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
883 if (imsb >= matrice[layno - 1][resno][bandno]) {
884 value -= (imsb - matrice[layno - 1][resno][bandno]);
892 cblk->numpassesinlayers = 0;
895 n = cblk->numpassesinlayers;
896 if (cblk->numpassesinlayers == 0) {
898 n = 3 * value - 2 + cblk->numpassesinlayers;
900 n = cblk->numpassesinlayers;
903 n = 3 * value + cblk->numpassesinlayers;
906 layer->numpasses = n - cblk->numpassesinlayers;
908 if (!layer->numpasses)
911 if (cblk->numpassesinlayers == 0) {
912 layer->len = cblk->passes[n - 1].rate;
913 layer->data = cblk->data;
915 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
916 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
919 cblk->numpassesinlayers = n;
927 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
929 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
930 tcd_makelayer_fixed(tcd, layno, 1);
934 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
935 int compno, resno, bandno, precno, cblkno, passno;
937 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
939 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
941 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
942 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
943 for (resno = 0; resno < tilec->numresolutions; resno++) {
944 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
945 for (bandno = 0; bandno < res->numbands; bandno++) {
946 opj_tcd_band_t *band = &res->bands[bandno];
947 for (precno = 0; precno < res->pw * res->ph; precno++) {
948 opj_tcd_precinct_t *prc = &band->precincts[precno];
949 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
950 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
951 opj_tcd_layer_t *layer = &cblk->layers[layno];
955 cblk->numpassesinlayers = 0;
957 n = cblk->numpassesinlayers;
958 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
961 opj_tcd_pass_t *pass = &cblk->passes[passno];
964 dd = pass->distortiondec;
966 dr = pass->rate - cblk->passes[n - 1].rate;
967 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
974 if (dd / dr >= thresh)
977 layer->numpasses = n - cblk->numpassesinlayers;
979 if (!layer->numpasses) {
983 if (cblk->numpassesinlayers == 0) {
984 layer->len = cblk->passes[n - 1].rate;
985 layer->data = cblk->data;
986 layer->disto = cblk->passes[n - 1].distortiondec;
988 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
989 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
990 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
993 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
996 cblk->numpassesinlayers = n;
1004 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1005 int compno, resno, bandno, precno, cblkno, passno, layno;
1007 double cumdisto[100]; /* fixed_quality */
1008 const double K = 1; /* 1.1; fixed_quality */
1011 opj_cp_t *cp = tcd->cp;
1012 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1013 opj_tcp_t *tcd_tcp = tcd->tcp;
1018 tcd_tile->numpix = 0; /* fixed_quality */
1020 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1021 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1024 for (resno = 0; resno < tilec->numresolutions; resno++) {
1025 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1027 for (bandno = 0; bandno < res->numbands; bandno++) {
1028 opj_tcd_band_t *band = &res->bands[bandno];
1030 for (precno = 0; precno < res->pw * res->ph; precno++) {
1031 opj_tcd_precinct_t *prc = &band->precincts[precno];
1033 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1034 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1036 for (passno = 0; passno < cblk->totalpasses; passno++) {
1037 opj_tcd_pass_t *pass = &cblk->passes[passno];
1042 dd = pass->distortiondec;
1044 dr = pass->rate - cblk->passes[passno - 1].rate;
1045 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1051 if (rdslope < min) {
1054 if (rdslope > max) {
1060 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1061 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1067 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1068 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1069 * ((double)(tilec->numpix));
1074 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1075 tile_info->numpix = tcd_tile->numpix;
1076 tile_info->distotile = tcd_tile->distotile;
1077 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1080 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1084 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1085 double goodthresh = 0;
1086 double stable_thresh = 0;
1088 double distotarget; /* fixed_quality */
1091 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1093 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1094 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1095 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1096 ==> possible to have some lossy layers and the last layer for sure lossless */
1097 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1098 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1101 for (i = 0; i < 128; i++) {
1103 double distoachieved = 0; /* fixed_quality */
1104 thresh = (lo + hi) / 2;
1106 tcd_makelayer(tcd, layno, thresh, 0);
1108 if (cp->fixed_quality) { /* fixed_quality */
1110 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);
1115 distoachieved = layno == 0 ?
1116 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1117 if (distoachieved < distotarget) {
1119 stable_thresh = thresh;
1126 distoachieved = (layno == 0) ?
1127 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1128 if (distoachieved < distotarget) {
1130 stable_thresh = thresh;
1136 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);
1137 /* TODO: what to do with l ??? seek / tell ??? */
1138 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1144 stable_thresh = thresh;
1148 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1159 if(cstr_info) { /* Threshold for Marcela Index */
1160 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1162 tcd_makelayer(tcd, layno, goodthresh, 1);
1165 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1171 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1173 int l, i, numpacks = 0;
1174 opj_tcd_tile_t *tile = NULL;
1175 opj_tcp_t *tcd_tcp = NULL;
1176 opj_cp_t *cp = NULL;
1178 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1179 opj_tccp_t *tccp = &tcp->tccps[0];
1180 opj_image_t *image = tcd->image;
1182 opj_t1_t *t1 = NULL; /* T1 component */
1183 opj_t2_t *t2 = NULL; /* T2 component */
1185 tcd->tcd_tileno = tileno;
1186 tcd->tcd_tile = tcd->tcd_image->tiles;
1187 tcd->tcp = &tcd->cp->tcps[tileno];
1189 tile = tcd->tcd_tile;
1193 if(tcd->cur_tp_num == 0){
1194 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1195 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1197 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1198 for (i = 0; i < tilec_idx->numresolutions; i++) {
1199 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1201 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1202 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1204 numpacks += res_idx->pw * res_idx->ph;
1206 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1207 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1209 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1213 /*---------------TILE-------------------*/
1215 for (compno = 0; compno < tile->numcomps; compno++) {
1218 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1219 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1220 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1222 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1223 int tw = tilec->x1 - tilec->x0;
1224 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1226 /* extract tile data */
1228 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1229 for (y = tilec->y0; y < tilec->y1; y++) {
1230 /* start of the src tile scanline */
1231 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1232 /* start of the dst tile scanline */
1233 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1234 for (x = tilec->x0; x < tilec->x1; x++) {
1235 *tile_data++ = *data++ - adjust;
1238 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1239 for (y = tilec->y0; y < tilec->y1; y++) {
1240 /* start of the src tile scanline */
1241 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1242 /* start of the dst tile scanline */
1243 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1244 for (x = tilec->x0; x < tilec->x1; x++) {
1245 *tile_data++ = (*data++ - adjust) << 11;
1252 /*----------------MCT-------------------*/
1254 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1255 if (tcd_tcp->tccps[0].qmfbid == 0) {
1256 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1258 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1262 /*----------------DWT---------------------*/
1264 for (compno = 0; compno < tile->numcomps; compno++) {
1265 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1266 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1268 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1269 dwt_encode_real(tilec);
1273 /*------------------TIER1-----------------*/
1274 t1 = t1_create(tcd->cinfo);
1275 t1_encode_cblks(t1, tile, tcd_tcp);
1278 /*-----------RATE-ALLOCATE------------------*/
1282 cstr_info->index_write = 0;
1284 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1285 /* Normal Rate/distortion allocation */
1286 tcd_rateallocate(tcd, dest, len, cstr_info);
1288 /* Fixed layer allocation */
1289 tcd_rateallocate_fixed(tcd);
1292 /*--------------TIER2------------------*/
1296 cstr_info->index_write = 1;
1299 t2 = t2_create(tcd->cinfo, image, cp);
1300 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);
1303 /*---------------CLEAN-------------------*/
1306 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1307 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1308 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1310 /* cleaning memory */
1311 for (compno = 0; compno < tile->numcomps; compno++) {
1312 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1313 opj_aligned_free(tilec->data);
1320 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1324 double tile_time, t1_time, dwt_time;
1325 opj_tcd_tile_t *tile = NULL;
1327 opj_t1_t *t1 = NULL; /* T1 component */
1328 opj_t2_t *t2 = NULL; /* T2 component */
1330 tcd->tcd_tileno = tileno;
1331 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1332 tcd->tcp = &(tcd->cp->tcps[tileno]);
1333 tile = tcd->tcd_tile;
1335 tile_time = opj_clock(); /* time needed to decode a tile */
1336 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1340 int resno, compno, numprec = 0;
1341 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1342 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1343 opj_tccp_t *tccp = &tcp->tccps[compno];
1344 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1345 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1346 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1347 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1348 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1349 numprec += res_idx->pw * res_idx->ph;
1350 if (tccp->csty & J2K_CP_CSTY_PRT) {
1351 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1352 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1355 cstr_info->tile[tileno].pdx[resno] = 15;
1356 cstr_info->tile[tileno].pdy[resno] = 15;
1360 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1361 cstr_info->packno = 0;
1365 /*--------------TIER2------------------*/
1367 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1368 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1373 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1376 /*------------------TIER1-----------------*/
1378 t1_time = opj_clock(); /* time needed to decode a tile */
1379 t1 = t1_create(tcd->cinfo);
1380 for (compno = 0; compno < tile->numcomps; ++compno) {
1381 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1382 /* The +3 is headroom required by the vectorized DWT */
1383 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1384 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1387 t1_time = opj_clock() - t1_time;
1388 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1390 /*----------------DWT---------------------*/
1392 dwt_time = opj_clock(); /* time needed to decode a tile */
1393 for (compno = 0; compno < tile->numcomps; compno++) {
1394 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1397 if (tcd->cp->reduce != 0) {
1398 tcd->image->comps[compno].resno_decoded =
1399 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1400 if (tcd->image->comps[compno].resno_decoded < 0) {
1401 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1402 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1407 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1408 if(numres2decode > 0){
1409 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1410 dwt_decode(tilec, numres2decode);
1412 dwt_decode_real(tilec, numres2decode);
1416 dwt_time = opj_clock() - dwt_time;
1417 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1419 /*----------------MCT-------------------*/
1421 if (tcd->tcp->mct) {
1422 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1424 if (tile->numcomps >= 3 ){
1425 if (tcd->tcp->tccps[0].qmfbid == 1) {
1427 tile->comps[0].data,
1428 tile->comps[1].data,
1429 tile->comps[2].data,
1433 (float*)tile->comps[0].data,
1434 (float*)tile->comps[1].data,
1435 (float*)tile->comps[2].data,
1439 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1443 /*---------------TILE-------------------*/
1445 for (compno = 0; compno < tile->numcomps; ++compno) {
1446 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1447 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1448 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1449 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1450 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1451 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1453 int tw = tilec->x1 - tilec->x0;
1456 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1457 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1461 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1463 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1464 for(j = res->y0; j < res->y1; ++j) {
1465 for(i = res->x0; i < res->x1; ++i) {
1466 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1468 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1472 for(j = res->y0; j < res->y1; ++j) {
1473 for(i = res->x0; i < res->x1; ++i) {
1474 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1475 int v = lrintf(tmp);
1477 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1481 opj_aligned_free(tilec->data);
1484 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1485 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1494 void tcd_free_decode(opj_tcd_t *tcd) {
1495 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1496 opj_free(tcd_image->tiles);
1499 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1500 int compno,resno,bandno,precno;
1502 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1504 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1505 for (compno = 0; compno < tile->numcomps; compno++) {
1506 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1507 for (resno = 0; resno < tilec->numresolutions; resno++) {
1508 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1509 for (bandno = 0; bandno < res->numbands; bandno++) {
1510 opj_tcd_band_t *band = &res->bands[bandno];
1511 for (precno = 0; precno < res->ph * res->pw; precno++) {
1512 opj_tcd_precinct_t *prec = &band->precincts[precno];
1513 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1514 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1516 opj_free(band->precincts);
1519 opj_free(tilec->resolutions);
1521 opj_free(tile->comps);