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(9728+2, sizeof(unsigned char));
338 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
342 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
343 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
351 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
354 void tcd_free_encode(opj_tcd_t *tcd) {
355 int tileno, compno, resno, bandno, precno, cblkno;
357 for (tileno = 0; tileno < 1; tileno++) {
358 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
360 for (compno = 0; compno < tile->numcomps; compno++) {
361 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
363 for (resno = 0; resno < tilec->numresolutions; resno++) {
364 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
366 for (bandno = 0; bandno < res->numbands; bandno++) {
367 opj_tcd_band_t *band = &res->bands[bandno];
369 for (precno = 0; precno < res->pw * res->ph; precno++) {
370 opj_tcd_precinct_t *prc = &band->precincts[precno];
372 if (prc->incltree != NULL) {
373 tgt_destroy(prc->incltree);
374 prc->incltree = NULL;
376 if (prc->imsbtree != NULL) {
377 tgt_destroy(prc->imsbtree);
378 prc->imsbtree = NULL;
380 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
381 opj_free(prc->cblks.enc[cblkno].data - 2);
382 opj_free(prc->cblks.enc[cblkno].layers);
383 opj_free(prc->cblks.enc[cblkno].passes);
385 opj_free(prc->cblks.enc);
387 opj_free(band->precincts);
388 band->precincts = NULL;
391 opj_free(tilec->resolutions);
392 tilec->resolutions = NULL;
394 opj_free(tile->comps);
397 opj_free(tcd->tcd_image->tiles);
398 tcd->tcd_image->tiles = NULL;
401 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
402 int tileno, compno, resno, bandno, precno, cblkno;
404 for (tileno = 0; tileno < 1; tileno++) {
405 opj_tcp_t *tcp = &cp->tcps[curtileno];
407 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
408 int p = curtileno % cp->tw;
409 int q = curtileno / cp->tw;
411 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
413 /* 4 borders of the tile rescale on the image if necessary */
414 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
415 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
416 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
417 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
419 tile->numcomps = image->numcomps;
420 /* tile->PPT=image->PPT; */
422 /* Modification of the RATE >> */
423 for (j = 0; j < tcp->numlayers; j++) {
424 tcp->rates[j] = tcp->rates[j] ?
426 (((float) (tile->numcomps
427 * (tile->x1 - tile->x0)
428 * (tile->y1 - tile->y0)
429 * image->comps[0].prec))
430 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
432 ((float) (tile->numcomps
433 * (tile->x1 - tile->x0)
434 * (tile->y1 - tile->y0)
435 * image->comps[0].prec))/
436 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
440 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
441 tcp->rates[j] = tcp->rates[j - 1] + 20;
443 if (!j && tcp->rates[j] < 30)
448 /* << Modification of the RATE */
450 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
451 for (compno = 0; compno < tile->numcomps; compno++) {
452 opj_tccp_t *tccp = &tcp->tccps[compno];
454 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
456 /* border of each tile component (global) */
457 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
458 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
459 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
460 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
462 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
463 tilec->numresolutions = tccp->numresolutions;
464 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
465 for (resno = 0; resno < tilec->numresolutions; resno++) {
468 int levelno = tilec->numresolutions - 1 - resno;
469 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
470 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
471 int cbgwidthexpn, cbgheightexpn;
472 int cblkwidthexpn, cblkheightexpn;
474 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
476 /* border for each resolution level (global) */
477 res->x0 = int_ceildivpow2(tilec->x0, levelno);
478 res->y0 = int_ceildivpow2(tilec->y0, levelno);
479 res->x1 = int_ceildivpow2(tilec->x1, levelno);
480 res->y1 = int_ceildivpow2(tilec->y1, levelno);
481 res->numbands = resno == 0 ? 1 : 3;
483 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
484 if (tccp->csty & J2K_CCP_CSTY_PRT) {
485 pdx = tccp->prcw[resno];
486 pdy = tccp->prch[resno];
491 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
492 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
493 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
494 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
495 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
497 res->pw = (brprcxend - tlprcxstart) >> pdx;
498 res->ph = (brprcyend - tlprcystart) >> pdy;
501 tlcbgxstart = tlprcxstart;
502 tlcbgystart = tlprcystart;
503 brcbgxend = brprcxend;
504 brcbgyend = brprcyend;
508 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
509 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
510 brcbgxend = int_ceildivpow2(brprcxend, 1);
511 brcbgyend = int_ceildivpow2(brprcyend, 1);
512 cbgwidthexpn = pdx - 1;
513 cbgheightexpn = pdy - 1;
516 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
517 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
519 for (bandno = 0; bandno < res->numbands; bandno++) {
522 opj_stepsize_t *ss = NULL;
524 opj_tcd_band_t *band = &res->bands[bandno];
526 band->bandno = resno == 0 ? 0 : bandno + 1;
527 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
528 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
530 if (band->bandno == 0) {
532 band->x0 = int_ceildivpow2(tilec->x0, levelno);
533 band->y0 = int_ceildivpow2(tilec->y0, levelno);
534 band->x1 = int_ceildivpow2(tilec->x1, levelno);
535 band->y1 = int_ceildivpow2(tilec->y1, levelno);
537 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
538 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
539 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
540 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
543 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
544 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
545 numbps = image->comps[compno].prec + gain;
546 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
547 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
549 for (precno = 0; precno < res->pw * res->ph; precno++) {
550 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
552 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
553 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
554 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
555 int cbgyend = cbgystart + (1 << cbgheightexpn);
557 opj_tcd_precinct_t *prc = &band->precincts[precno];
559 /* precinct size (global) */
560 prc->x0 = int_max(cbgxstart, band->x0);
561 prc->y0 = int_max(cbgystart, band->y0);
562 prc->x1 = int_min(cbgxend, band->x1);
563 prc->y1 = int_min(cbgyend, band->y1);
565 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
566 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
567 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
568 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
569 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
570 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
572 opj_free(prc->cblks.enc);
573 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
575 if (prc->incltree != NULL) {
576 tgt_destroy(prc->incltree);
578 if (prc->imsbtree != NULL) {
579 tgt_destroy(prc->imsbtree);
582 prc->incltree = tgt_create(prc->cw, prc->ch);
583 prc->imsbtree = tgt_create(prc->cw, prc->ch);
585 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
586 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
587 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
588 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
589 int cblkyend = cblkystart + (1 << cblkheightexpn);
591 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
593 /* code-block size (global) */
594 cblk->x0 = int_max(cblkxstart, prc->x0);
595 cblk->y0 = int_max(cblkystart, prc->y0);
596 cblk->x1 = int_min(cblkxend, prc->x1);
597 cblk->y1 = int_min(cblkyend, prc->y1);
598 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
599 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
603 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
604 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
612 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
615 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
616 int i, j, tileno, p, q;
617 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
620 tcd->tcd_image->tw = cp->tw;
621 tcd->tcd_image->th = cp->th;
622 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(cp->tw * cp->th, sizeof(opj_tcd_tile_t));
625 Allocate place to store the decoded data = final image
626 Place limited by the tile really present in the codestream
629 for (j = 0; j < cp->tileno_size; j++) {
630 opj_tcd_tile_t *tile;
632 tileno = cp->tileno[j];
633 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
634 tile->numcomps = image->numcomps;
635 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
638 for (i = 0; i < image->numcomps; i++) {
639 for (j = 0; j < cp->tileno_size; j++) {
640 opj_tcd_tile_t *tile;
641 opj_tcd_tilecomp_t *tilec;
643 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
645 tileno = cp->tileno[j];
647 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
648 tilec = &tile->comps[i];
650 p = tileno % cp->tw; /* si numerotation matricielle .. */
651 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
653 /* 4 borders of the tile rescale on the image if necessary */
654 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
655 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
656 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
657 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
659 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
660 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
661 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
662 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
664 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
665 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
666 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
667 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
670 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
671 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
673 image->comps[i].w = w;
674 image->comps[i].h = h;
675 image->comps[i].x0 = x0;
676 image->comps[i].y0 = y0;
680 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) {
681 int compno, resno, bandno, precno, cblkno;
683 opj_tcd_tile_t *tile;
685 OPJ_ARG_NOT_USED(cstr_info);
689 tcp = &(cp->tcps[cp->tileno[tileno]]);
690 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
692 tileno = cp->tileno[tileno];
694 for (compno = 0; compno < tile->numcomps; compno++) {
695 opj_tccp_t *tccp = &tcp->tccps[compno];
696 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
698 if (tccp->numresolutions <= 0)
700 cp->tileno[tileno] = -1;
704 /* border of each tile component (global) */
705 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
706 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
707 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
708 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
710 tilec->numresolutions = tccp->numresolutions;
711 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
713 for (resno = 0; resno < tilec->numresolutions; resno++) {
715 int levelno = tilec->numresolutions - 1 - resno;
716 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
717 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
718 int cbgwidthexpn, cbgheightexpn;
719 int cblkwidthexpn, cblkheightexpn;
721 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
723 /* border for each resolution level (global) */
724 res->x0 = int_ceildivpow2(tilec->x0, levelno);
725 res->y0 = int_ceildivpow2(tilec->y0, levelno);
726 res->x1 = int_ceildivpow2(tilec->x1, levelno);
727 res->y1 = int_ceildivpow2(tilec->y1, levelno);
728 res->numbands = resno == 0 ? 1 : 3;
730 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
731 if (tccp->csty & J2K_CCP_CSTY_PRT) {
732 pdx = tccp->prcw[resno];
733 pdy = tccp->prch[resno];
739 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
740 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
741 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
742 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
743 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
745 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
746 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
749 tlcbgxstart = tlprcxstart;
750 tlcbgystart = tlprcystart;
751 brcbgxend = brprcxend;
752 brcbgyend = brprcyend;
756 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
757 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
758 brcbgxend = int_ceildivpow2(brprcxend, 1);
759 brcbgyend = int_ceildivpow2(brprcyend, 1);
760 cbgwidthexpn = pdx - 1;
761 cbgheightexpn = pdy - 1;
764 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
765 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
767 for (bandno = 0; bandno < res->numbands; bandno++) {
770 opj_stepsize_t *ss = NULL;
772 opj_tcd_band_t *band = &res->bands[bandno];
773 band->bandno = resno == 0 ? 0 : bandno + 1;
774 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
775 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
777 if (band->bandno == 0) {
778 /* band border (global) */
779 band->x0 = int_ceildivpow2(tilec->x0, levelno);
780 band->y0 = int_ceildivpow2(tilec->y0, levelno);
781 band->x1 = int_ceildivpow2(tilec->x1, levelno);
782 band->y1 = int_ceildivpow2(tilec->y1, levelno);
784 /* band border (global) */
785 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
786 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
787 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
788 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
791 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
792 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
793 numbps = image->comps[compno].prec + gain;
794 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
795 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
797 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
799 for (precno = 0; precno < res->pw * res->ph; precno++) {
800 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
801 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
802 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
803 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
804 int cbgyend = cbgystart + (1 << cbgheightexpn);
806 opj_tcd_precinct_t *prc = &band->precincts[precno];
807 /* precinct size (global) */
808 prc->x0 = int_max(cbgxstart, band->x0);
809 prc->y0 = int_max(cbgystart, band->y0);
810 prc->x1 = int_min(cbgxend, band->x1);
811 prc->y1 = int_min(cbgyend, band->y1);
813 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
814 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
815 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
816 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
817 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
818 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
820 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
822 prc->incltree = tgt_create(prc->cw, prc->ch);
823 prc->imsbtree = tgt_create(prc->cw, prc->ch);
825 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
826 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
827 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
828 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
829 int cblkyend = cblkystart + (1 << cblkheightexpn);
831 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
834 /* code-block size (global) */
835 cblk->x0 = int_max(cblkxstart, prc->x0);
836 cblk->y0 = int_max(cblkystart, prc->y0);
837 cblk->x1 = int_min(cblkxend, prc->x1);
838 cblk->y1 = int_min(cblkyend, prc->y1);
845 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
848 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
849 int compno, resno, bandno, precno, cblkno;
850 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
851 int matrice[10][10][3];
854 opj_cp_t *cp = tcd->cp;
855 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
856 opj_tcp_t *tcd_tcp = tcd->tcp;
858 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
860 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
861 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
862 for (i = 0; i < tcd_tcp->numlayers; i++) {
863 for (j = 0; j < tilec->numresolutions; j++) {
864 for (k = 0; k < 3; k++) {
866 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
867 * (float) (tcd->image->comps[compno].prec / 16.0));
872 for (resno = 0; resno < tilec->numresolutions; resno++) {
873 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
874 for (bandno = 0; bandno < res->numbands; bandno++) {
875 opj_tcd_band_t *band = &res->bands[bandno];
876 for (precno = 0; precno < res->pw * res->ph; precno++) {
877 opj_tcd_precinct_t *prc = &band->precincts[precno];
878 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
879 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
880 opj_tcd_layer_t *layer = &cblk->layers[layno];
882 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
883 /* Correction of the matrix of coefficient to include the IMSB information */
885 value = matrice[layno][resno][bandno];
892 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
893 if (imsb >= matrice[layno - 1][resno][bandno]) {
894 value -= (imsb - matrice[layno - 1][resno][bandno]);
902 cblk->numpassesinlayers = 0;
905 n = cblk->numpassesinlayers;
906 if (cblk->numpassesinlayers == 0) {
908 n = 3 * value - 2 + cblk->numpassesinlayers;
910 n = cblk->numpassesinlayers;
913 n = 3 * value + cblk->numpassesinlayers;
916 layer->numpasses = n - cblk->numpassesinlayers;
918 if (!layer->numpasses)
921 if (cblk->numpassesinlayers == 0) {
922 layer->len = cblk->passes[n - 1].rate;
923 layer->data = cblk->data;
925 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
926 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
929 cblk->numpassesinlayers = n;
937 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
939 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
940 tcd_makelayer_fixed(tcd, layno, 1);
944 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
945 int compno, resno, bandno, precno, cblkno, passno;
947 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
949 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
951 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
952 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
953 for (resno = 0; resno < tilec->numresolutions; resno++) {
954 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
955 for (bandno = 0; bandno < res->numbands; bandno++) {
956 opj_tcd_band_t *band = &res->bands[bandno];
957 for (precno = 0; precno < res->pw * res->ph; precno++) {
958 opj_tcd_precinct_t *prc = &band->precincts[precno];
959 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
960 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
961 opj_tcd_layer_t *layer = &cblk->layers[layno];
965 cblk->numpassesinlayers = 0;
967 n = cblk->numpassesinlayers;
968 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
971 opj_tcd_pass_t *pass = &cblk->passes[passno];
974 dd = pass->distortiondec;
976 dr = pass->rate - cblk->passes[n - 1].rate;
977 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
984 if (dd / dr >= thresh)
987 layer->numpasses = n - cblk->numpassesinlayers;
989 if (!layer->numpasses) {
993 if (cblk->numpassesinlayers == 0) {
994 layer->len = cblk->passes[n - 1].rate;
995 layer->data = cblk->data;
996 layer->disto = cblk->passes[n - 1].distortiondec;
998 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
999 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1000 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1003 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1006 cblk->numpassesinlayers = n;
1014 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1015 int compno, resno, bandno, precno, cblkno, passno, layno;
1017 double cumdisto[100]; /* fixed_quality */
1018 const double K = 1; /* 1.1; fixed_quality */
1021 opj_cp_t *cp = tcd->cp;
1022 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1023 opj_tcp_t *tcd_tcp = tcd->tcp;
1028 tcd_tile->numpix = 0; /* fixed_quality */
1030 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1031 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1034 for (resno = 0; resno < tilec->numresolutions; resno++) {
1035 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1037 for (bandno = 0; bandno < res->numbands; bandno++) {
1038 opj_tcd_band_t *band = &res->bands[bandno];
1040 for (precno = 0; precno < res->pw * res->ph; precno++) {
1041 opj_tcd_precinct_t *prc = &band->precincts[precno];
1043 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1044 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1046 for (passno = 0; passno < cblk->totalpasses; passno++) {
1047 opj_tcd_pass_t *pass = &cblk->passes[passno];
1052 dd = pass->distortiondec;
1054 dr = pass->rate - cblk->passes[passno - 1].rate;
1055 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1061 if (rdslope < min) {
1064 if (rdslope > max) {
1070 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1071 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1077 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1078 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1079 * ((double)(tilec->numpix));
1084 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1085 tile_info->numpix = tcd_tile->numpix;
1086 tile_info->distotile = tcd_tile->distotile;
1087 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1090 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1094 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1095 double goodthresh = 0;
1096 double stable_thresh = 0;
1098 double distotarget; /* fixed_quality */
1101 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1103 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1104 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1105 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1106 ==> possible to have some lossy layers and the last layer for sure lossless */
1107 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1108 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1111 for (i = 0; i < 128; i++) {
1113 double distoachieved = 0; /* fixed_quality */
1114 thresh = (lo + hi) / 2;
1116 tcd_makelayer(tcd, layno, thresh, 0);
1118 if (cp->fixed_quality) { /* fixed_quality */
1120 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);
1125 distoachieved = layno == 0 ?
1126 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1127 if (distoachieved < distotarget) {
1129 stable_thresh = thresh;
1136 distoachieved = (layno == 0) ?
1137 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1138 if (distoachieved < distotarget) {
1140 stable_thresh = thresh;
1146 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);
1147 /* TODO: what to do with l ??? seek / tell ??? */
1148 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1154 stable_thresh = thresh;
1158 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1169 if(cstr_info) { /* Threshold for Marcela Index */
1170 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1172 tcd_makelayer(tcd, layno, goodthresh, 1);
1175 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1181 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1183 int l, i, numpacks = 0;
1184 opj_tcd_tile_t *tile = NULL;
1185 opj_tcp_t *tcd_tcp = NULL;
1186 opj_cp_t *cp = NULL;
1188 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1189 opj_tccp_t *tccp = &tcp->tccps[0];
1190 opj_image_t *image = tcd->image;
1192 opj_t1_t *t1 = NULL; /* T1 component */
1193 opj_t2_t *t2 = NULL; /* T2 component */
1195 tcd->tcd_tileno = tileno;
1196 tcd->tcd_tile = tcd->tcd_image->tiles;
1197 tcd->tcp = &tcd->cp->tcps[tileno];
1199 tile = tcd->tcd_tile;
1203 if(tcd->cur_tp_num == 0){
1204 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1205 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1207 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1208 for (i = 0; i < tilec_idx->numresolutions; i++) {
1209 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1211 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1212 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1214 numpacks += res_idx->pw * res_idx->ph;
1216 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1217 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1219 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1223 /*---------------TILE-------------------*/
1225 for (compno = 0; compno < tile->numcomps; compno++) {
1228 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1229 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1230 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1232 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1233 int tw = tilec->x1 - tilec->x0;
1234 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1236 /* extract tile data */
1238 if (tcd_tcp->tccps[compno].qmfbid == 1) {
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;
1248 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1249 for (y = tilec->y0; y < tilec->y1; y++) {
1250 /* start of the src tile scanline */
1251 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1252 /* start of the dst tile scanline */
1253 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1254 for (x = tilec->x0; x < tilec->x1; x++) {
1255 *tile_data++ = (*data++ - adjust) << 11;
1262 /*----------------MCT-------------------*/
1264 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1265 if (tcd_tcp->tccps[0].qmfbid == 0) {
1266 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1268 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1272 /*----------------DWT---------------------*/
1274 for (compno = 0; compno < tile->numcomps; compno++) {
1275 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1276 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1278 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1279 dwt_encode_real(tilec);
1283 /*------------------TIER1-----------------*/
1284 t1 = t1_create(tcd->cinfo);
1285 t1_encode_cblks(t1, tile, tcd_tcp);
1288 /*-----------RATE-ALLOCATE------------------*/
1292 cstr_info->index_write = 0;
1294 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1295 /* Normal Rate/distortion allocation */
1296 tcd_rateallocate(tcd, dest, len, cstr_info);
1298 /* Fixed layer allocation */
1299 tcd_rateallocate_fixed(tcd);
1302 /*--------------TIER2------------------*/
1306 cstr_info->index_write = 1;
1309 t2 = t2_create(tcd->cinfo, image, cp);
1310 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);
1313 /*---------------CLEAN-------------------*/
1316 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1317 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1318 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1320 /* cleaning memory */
1321 for (compno = 0; compno < tile->numcomps; compno++) {
1322 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1323 opj_aligned_free(tilec->data);
1330 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1334 double tile_time, t1_time, dwt_time;
1335 opj_tcd_tile_t *tile = NULL;
1337 opj_t1_t *t1 = NULL; /* T1 component */
1338 opj_t2_t *t2 = NULL; /* T2 component */
1340 tcd->tcd_tileno = tileno;
1341 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1342 tcd->tcp = &(tcd->cp->tcps[tileno]);
1343 tile = tcd->tcd_tile;
1345 tile_time = opj_clock(); /* time needed to decode a tile */
1346 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1350 int resno, compno, numprec = 0;
1351 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1352 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1353 opj_tccp_t *tccp = &tcp->tccps[compno];
1354 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1355 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1356 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1357 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1358 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1359 numprec += res_idx->pw * res_idx->ph;
1360 if (tccp->csty & J2K_CP_CSTY_PRT) {
1361 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1362 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1365 cstr_info->tile[tileno].pdx[resno] = 15;
1366 cstr_info->tile[tileno].pdy[resno] = 15;
1370 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1371 cstr_info->packno = 0;
1375 /*--------------TIER2------------------*/
1377 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1378 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1383 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1386 /*------------------TIER1-----------------*/
1388 t1_time = opj_clock(); /* time needed to decode a tile */
1389 t1 = t1_create(tcd->cinfo);
1392 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1397 for (compno = 0; compno < tile->numcomps; ++compno) {
1398 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1399 /* The +3 is headroom required by the vectorized DWT */
1400 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1401 if (tilec->data == NULL)
1403 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1407 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1410 t1_time = opj_clock() - t1_time;
1411 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1413 /*----------------DWT---------------------*/
1415 dwt_time = opj_clock(); /* time needed to decode a tile */
1416 for (compno = 0; compno < tile->numcomps; compno++) {
1417 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1420 if (tcd->cp->reduce != 0) {
1421 if ( tile->comps[compno].numresolutions < ( tcd->cp->reduce - 1 ) ) {
1422 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1423 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1427 tcd->image->comps[compno].resno_decoded =
1428 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1432 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1433 if(numres2decode > 0){
1434 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1435 dwt_decode(tilec, numres2decode);
1437 dwt_decode_real(tilec, numres2decode);
1441 dwt_time = opj_clock() - dwt_time;
1442 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1444 /*----------------MCT-------------------*/
1446 if (tcd->tcp->mct) {
1447 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1449 if (tile->numcomps >= 3 ){
1450 if (tcd->tcp->tccps[0].qmfbid == 1) {
1452 tile->comps[0].data,
1453 tile->comps[1].data,
1454 tile->comps[2].data,
1458 (float*)tile->comps[0].data,
1459 (float*)tile->comps[1].data,
1460 (float*)tile->comps[2].data,
1464 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1468 /*---------------TILE-------------------*/
1470 for (compno = 0; compno < tile->numcomps; ++compno) {
1471 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1472 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1473 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1474 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1475 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1476 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1478 int tw = tilec->x1 - tilec->x0;
1481 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1482 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1486 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1490 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1493 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1494 for(j = res->y0; j < res->y1; ++j) {
1495 for(i = res->x0; i < res->x1; ++i) {
1496 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1498 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1502 for(j = res->y0; j < res->y1; ++j) {
1503 for(i = res->x0; i < res->x1; ++i) {
1504 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1505 int v = lrintf(tmp);
1507 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1511 opj_aligned_free(tilec->data);
1514 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1515 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1524 void tcd_free_decode(opj_tcd_t *tcd) {
1525 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1527 for (i = 0; i < tcd_image->tw * tcd_image->th; i++)
1529 tcd_free_decode_tile(tcd, i);
1532 opj_free(tcd_image->tiles);
1535 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1536 int compno,resno,bandno,precno,cblkno;
1538 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1540 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1541 if (tile->comps != NULL) {
1542 for (compno = 0; compno < tile->numcomps; compno++) {
1543 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1544 for (resno = 0; resno < tilec->numresolutions; resno++) {
1545 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1546 for (bandno = 0; bandno < res->numbands; bandno++) {
1547 opj_tcd_band_t *band = &res->bands[bandno];
1548 for (precno = 0; precno < res->ph * res->pw; precno++) {
1549 opj_tcd_precinct_t *prec = &band->precincts[precno];
1550 if (prec->cblks.dec != NULL) {
1551 for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
1552 opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
1553 opj_free(cblk->data);
1554 opj_free(cblk->segs);
1556 opj_free(prec->cblks.dec);
1558 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1559 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1563 opj_free(band->precincts);
1566 opj_free(tilec->resolutions);
1568 opj_free(tile->comps);