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 /* border of each tile component (global) */
699 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
700 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
701 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
702 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
704 tilec->numresolutions = tccp->numresolutions;
705 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
707 for (resno = 0; resno < tilec->numresolutions; resno++) {
709 int levelno = tilec->numresolutions - 1 - resno;
710 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
711 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
712 int cbgwidthexpn, cbgheightexpn;
713 int cblkwidthexpn, cblkheightexpn;
715 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
717 /* border for each resolution level (global) */
718 res->x0 = int_ceildivpow2(tilec->x0, levelno);
719 res->y0 = int_ceildivpow2(tilec->y0, levelno);
720 res->x1 = int_ceildivpow2(tilec->x1, levelno);
721 res->y1 = int_ceildivpow2(tilec->y1, levelno);
722 res->numbands = resno == 0 ? 1 : 3;
724 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
725 if (tccp->csty & J2K_CCP_CSTY_PRT) {
726 pdx = tccp->prcw[resno];
727 pdy = tccp->prch[resno];
733 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
734 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
735 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
736 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
737 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
739 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
740 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
743 tlcbgxstart = tlprcxstart;
744 tlcbgystart = tlprcystart;
745 brcbgxend = brprcxend;
746 brcbgyend = brprcyend;
750 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
751 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
752 brcbgxend = int_ceildivpow2(brprcxend, 1);
753 brcbgyend = int_ceildivpow2(brprcyend, 1);
754 cbgwidthexpn = pdx - 1;
755 cbgheightexpn = pdy - 1;
758 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
759 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
761 for (bandno = 0; bandno < res->numbands; bandno++) {
764 opj_stepsize_t *ss = NULL;
766 opj_tcd_band_t *band = &res->bands[bandno];
767 band->bandno = resno == 0 ? 0 : bandno + 1;
768 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
769 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
771 if (band->bandno == 0) {
772 /* band border (global) */
773 band->x0 = int_ceildivpow2(tilec->x0, levelno);
774 band->y0 = int_ceildivpow2(tilec->y0, levelno);
775 band->x1 = int_ceildivpow2(tilec->x1, levelno);
776 band->y1 = int_ceildivpow2(tilec->y1, levelno);
778 /* band border (global) */
779 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
780 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
781 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
782 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
785 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
786 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
787 numbps = image->comps[compno].prec + gain;
788 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
789 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
791 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
793 for (precno = 0; precno < res->pw * res->ph; precno++) {
794 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
795 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
796 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
797 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
798 int cbgyend = cbgystart + (1 << cbgheightexpn);
800 opj_tcd_precinct_t *prc = &band->precincts[precno];
801 /* precinct size (global) */
802 prc->x0 = int_max(cbgxstart, band->x0);
803 prc->y0 = int_max(cbgystart, band->y0);
804 prc->x1 = int_min(cbgxend, band->x1);
805 prc->y1 = int_min(cbgyend, band->y1);
807 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
808 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
809 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
810 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
811 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
812 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
814 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
816 prc->incltree = tgt_create(prc->cw, prc->ch);
817 prc->imsbtree = tgt_create(prc->cw, prc->ch);
819 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
820 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
821 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
822 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
823 int cblkyend = cblkystart + (1 << cblkheightexpn);
825 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
828 /* code-block size (global) */
829 cblk->x0 = int_max(cblkxstart, prc->x0);
830 cblk->y0 = int_max(cblkystart, prc->y0);
831 cblk->x1 = int_min(cblkxend, prc->x1);
832 cblk->y1 = int_min(cblkyend, prc->y1);
839 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
842 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
843 int compno, resno, bandno, precno, cblkno;
844 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
845 int matrice[10][10][3];
848 opj_cp_t *cp = tcd->cp;
849 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
850 opj_tcp_t *tcd_tcp = tcd->tcp;
852 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
854 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
855 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
856 for (i = 0; i < tcd_tcp->numlayers; i++) {
857 for (j = 0; j < tilec->numresolutions; j++) {
858 for (k = 0; k < 3; k++) {
860 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
861 * (float) (tcd->image->comps[compno].prec / 16.0));
866 for (resno = 0; resno < tilec->numresolutions; resno++) {
867 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
868 for (bandno = 0; bandno < res->numbands; bandno++) {
869 opj_tcd_band_t *band = &res->bands[bandno];
870 for (precno = 0; precno < res->pw * res->ph; precno++) {
871 opj_tcd_precinct_t *prc = &band->precincts[precno];
872 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
873 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
874 opj_tcd_layer_t *layer = &cblk->layers[layno];
876 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
877 /* Correction of the matrix of coefficient to include the IMSB information */
879 value = matrice[layno][resno][bandno];
886 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
887 if (imsb >= matrice[layno - 1][resno][bandno]) {
888 value -= (imsb - matrice[layno - 1][resno][bandno]);
896 cblk->numpassesinlayers = 0;
899 n = cblk->numpassesinlayers;
900 if (cblk->numpassesinlayers == 0) {
902 n = 3 * value - 2 + cblk->numpassesinlayers;
904 n = cblk->numpassesinlayers;
907 n = 3 * value + cblk->numpassesinlayers;
910 layer->numpasses = n - cblk->numpassesinlayers;
912 if (!layer->numpasses)
915 if (cblk->numpassesinlayers == 0) {
916 layer->len = cblk->passes[n - 1].rate;
917 layer->data = cblk->data;
919 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
920 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
923 cblk->numpassesinlayers = n;
931 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
933 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
934 tcd_makelayer_fixed(tcd, layno, 1);
938 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
939 int compno, resno, bandno, precno, cblkno, passno;
941 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
943 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
945 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
946 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
947 for (resno = 0; resno < tilec->numresolutions; resno++) {
948 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
949 for (bandno = 0; bandno < res->numbands; bandno++) {
950 opj_tcd_band_t *band = &res->bands[bandno];
951 for (precno = 0; precno < res->pw * res->ph; precno++) {
952 opj_tcd_precinct_t *prc = &band->precincts[precno];
953 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
954 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
955 opj_tcd_layer_t *layer = &cblk->layers[layno];
959 cblk->numpassesinlayers = 0;
961 n = cblk->numpassesinlayers;
962 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
965 opj_tcd_pass_t *pass = &cblk->passes[passno];
968 dd = pass->distortiondec;
970 dr = pass->rate - cblk->passes[n - 1].rate;
971 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
978 if (dd / dr >= thresh)
981 layer->numpasses = n - cblk->numpassesinlayers;
983 if (!layer->numpasses) {
987 if (cblk->numpassesinlayers == 0) {
988 layer->len = cblk->passes[n - 1].rate;
989 layer->data = cblk->data;
990 layer->disto = cblk->passes[n - 1].distortiondec;
992 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
993 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
994 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
997 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1000 cblk->numpassesinlayers = n;
1008 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1009 int compno, resno, bandno, precno, cblkno, passno, layno;
1011 double cumdisto[100]; /* fixed_quality */
1012 const double K = 1; /* 1.1; fixed_quality */
1015 opj_cp_t *cp = tcd->cp;
1016 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1017 opj_tcp_t *tcd_tcp = tcd->tcp;
1022 tcd_tile->numpix = 0; /* fixed_quality */
1024 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1025 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1028 for (resno = 0; resno < tilec->numresolutions; resno++) {
1029 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1031 for (bandno = 0; bandno < res->numbands; bandno++) {
1032 opj_tcd_band_t *band = &res->bands[bandno];
1034 for (precno = 0; precno < res->pw * res->ph; precno++) {
1035 opj_tcd_precinct_t *prc = &band->precincts[precno];
1037 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1038 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1040 for (passno = 0; passno < cblk->totalpasses; passno++) {
1041 opj_tcd_pass_t *pass = &cblk->passes[passno];
1046 dd = pass->distortiondec;
1048 dr = pass->rate - cblk->passes[passno - 1].rate;
1049 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1055 if (rdslope < min) {
1058 if (rdslope > max) {
1064 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1065 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1071 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1072 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1073 * ((double)(tilec->numpix));
1078 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1079 tile_info->numpix = tcd_tile->numpix;
1080 tile_info->distotile = tcd_tile->distotile;
1081 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1084 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1088 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1089 double goodthresh = 0;
1090 double stable_thresh = 0;
1092 double distotarget; /* fixed_quality */
1095 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1097 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1098 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1099 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1100 ==> possible to have some lossy layers and the last layer for sure lossless */
1101 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1102 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1105 for (i = 0; i < 128; i++) {
1107 double distoachieved = 0; /* fixed_quality */
1108 thresh = (lo + hi) / 2;
1110 tcd_makelayer(tcd, layno, thresh, 0);
1112 if (cp->fixed_quality) { /* fixed_quality */
1114 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);
1119 distoachieved = layno == 0 ?
1120 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1121 if (distoachieved < distotarget) {
1123 stable_thresh = thresh;
1130 distoachieved = (layno == 0) ?
1131 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1132 if (distoachieved < distotarget) {
1134 stable_thresh = thresh;
1140 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);
1141 /* TODO: what to do with l ??? seek / tell ??? */
1142 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1148 stable_thresh = thresh;
1152 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1163 if(cstr_info) { /* Threshold for Marcela Index */
1164 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1166 tcd_makelayer(tcd, layno, goodthresh, 1);
1169 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1175 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1177 int l, i, numpacks = 0;
1178 opj_tcd_tile_t *tile = NULL;
1179 opj_tcp_t *tcd_tcp = NULL;
1180 opj_cp_t *cp = NULL;
1182 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1183 opj_tccp_t *tccp = &tcp->tccps[0];
1184 opj_image_t *image = tcd->image;
1186 opj_t1_t *t1 = NULL; /* T1 component */
1187 opj_t2_t *t2 = NULL; /* T2 component */
1189 tcd->tcd_tileno = tileno;
1190 tcd->tcd_tile = tcd->tcd_image->tiles;
1191 tcd->tcp = &tcd->cp->tcps[tileno];
1193 tile = tcd->tcd_tile;
1197 if(tcd->cur_tp_num == 0){
1198 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1199 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1201 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1202 for (i = 0; i < tilec_idx->numresolutions; i++) {
1203 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1205 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1206 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1208 numpacks += res_idx->pw * res_idx->ph;
1210 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1211 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1213 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1217 /*---------------TILE-------------------*/
1219 for (compno = 0; compno < tile->numcomps; compno++) {
1222 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1223 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1224 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1226 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1227 int tw = tilec->x1 - tilec->x0;
1228 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1230 /* extract tile data */
1232 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1233 for (y = tilec->y0; y < tilec->y1; y++) {
1234 /* start of the src tile scanline */
1235 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1236 /* start of the dst tile scanline */
1237 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1238 for (x = tilec->x0; x < tilec->x1; x++) {
1239 *tile_data++ = *data++ - adjust;
1242 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1243 for (y = tilec->y0; y < tilec->y1; y++) {
1244 /* start of the src tile scanline */
1245 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1246 /* start of the dst tile scanline */
1247 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1248 for (x = tilec->x0; x < tilec->x1; x++) {
1249 *tile_data++ = (*data++ - adjust) << 11;
1256 /*----------------MCT-------------------*/
1258 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1259 if (tcd_tcp->tccps[0].qmfbid == 0) {
1260 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1262 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1266 /*----------------DWT---------------------*/
1268 for (compno = 0; compno < tile->numcomps; compno++) {
1269 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1270 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1272 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1273 dwt_encode_real(tilec);
1277 /*------------------TIER1-----------------*/
1278 t1 = t1_create(tcd->cinfo);
1279 t1_encode_cblks(t1, tile, tcd_tcp);
1282 /*-----------RATE-ALLOCATE------------------*/
1286 cstr_info->index_write = 0;
1288 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1289 /* Normal Rate/distortion allocation */
1290 tcd_rateallocate(tcd, dest, len, cstr_info);
1292 /* Fixed layer allocation */
1293 tcd_rateallocate_fixed(tcd);
1296 /*--------------TIER2------------------*/
1300 cstr_info->index_write = 1;
1303 t2 = t2_create(tcd->cinfo, image, cp);
1304 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);
1307 /*---------------CLEAN-------------------*/
1310 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1311 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1312 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1314 /* cleaning memory */
1315 for (compno = 0; compno < tile->numcomps; compno++) {
1316 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1317 opj_aligned_free(tilec->data);
1324 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1328 double tile_time, t1_time, dwt_time;
1329 opj_tcd_tile_t *tile = NULL;
1331 opj_t1_t *t1 = NULL; /* T1 component */
1332 opj_t2_t *t2 = NULL; /* T2 component */
1334 tcd->tcd_tileno = tileno;
1335 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1336 tcd->tcp = &(tcd->cp->tcps[tileno]);
1337 tile = tcd->tcd_tile;
1339 tile_time = opj_clock(); /* time needed to decode a tile */
1340 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1344 int resno, compno, numprec = 0;
1345 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1346 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1347 opj_tccp_t *tccp = &tcp->tccps[compno];
1348 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1349 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1350 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1351 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1352 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1353 numprec += res_idx->pw * res_idx->ph;
1354 if (tccp->csty & J2K_CP_CSTY_PRT) {
1355 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1356 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1359 cstr_info->tile[tileno].pdx[resno] = 15;
1360 cstr_info->tile[tileno].pdy[resno] = 15;
1364 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1365 cstr_info->packno = 0;
1369 /*--------------TIER2------------------*/
1371 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1372 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1377 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1380 /*------------------TIER1-----------------*/
1382 t1_time = opj_clock(); /* time needed to decode a tile */
1383 t1 = t1_create(tcd->cinfo);
1386 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1391 for (compno = 0; compno < tile->numcomps; ++compno) {
1392 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1393 /* The +3 is headroom required by the vectorized DWT */
1394 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1395 if (tilec->data == NULL)
1397 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1401 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1404 t1_time = opj_clock() - t1_time;
1405 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1407 /*----------------DWT---------------------*/
1409 dwt_time = opj_clock(); /* time needed to decode a tile */
1410 for (compno = 0; compno < tile->numcomps; compno++) {
1411 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1414 if (tcd->cp->reduce != 0) {
1415 tcd->image->comps[compno].resno_decoded =
1416 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1417 if (tcd->image->comps[compno].resno_decoded < 0) {
1418 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1419 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1424 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1425 if(numres2decode > 0){
1426 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1427 dwt_decode(tilec, numres2decode);
1429 dwt_decode_real(tilec, numres2decode);
1433 dwt_time = opj_clock() - dwt_time;
1434 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1436 /*----------------MCT-------------------*/
1438 if (tcd->tcp->mct) {
1439 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1441 if (tile->numcomps >= 3 ){
1442 if (tcd->tcp->tccps[0].qmfbid == 1) {
1444 tile->comps[0].data,
1445 tile->comps[1].data,
1446 tile->comps[2].data,
1450 (float*)tile->comps[0].data,
1451 (float*)tile->comps[1].data,
1452 (float*)tile->comps[2].data,
1456 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1460 /*---------------TILE-------------------*/
1462 for (compno = 0; compno < tile->numcomps; ++compno) {
1463 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1464 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1465 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1466 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1467 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1468 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1470 int tw = tilec->x1 - tilec->x0;
1473 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1474 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1478 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1482 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1485 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1486 for(j = res->y0; j < res->y1; ++j) {
1487 for(i = res->x0; i < res->x1; ++i) {
1488 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1490 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1494 for(j = res->y0; j < res->y1; ++j) {
1495 for(i = res->x0; i < res->x1; ++i) {
1496 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1497 int v = lrintf(tmp);
1499 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1503 opj_aligned_free(tilec->data);
1506 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1507 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1516 void tcd_free_decode(opj_tcd_t *tcd) {
1517 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1519 for (i = 0; i < tcd_image->tw * tcd_image->th; i++)
1521 tcd_free_decode_tile(tcd, i);
1524 opj_free(tcd_image->tiles);
1527 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1528 int compno,resno,bandno,precno,cblkno;
1530 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1532 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1533 if (tile->comps != NULL) {
1534 for (compno = 0; compno < tile->numcomps; compno++) {
1535 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1536 for (resno = 0; resno < tilec->numresolutions; resno++) {
1537 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1538 for (bandno = 0; bandno < res->numbands; bandno++) {
1539 opj_tcd_band_t *band = &res->bands[bandno];
1540 for (precno = 0; precno < res->ph * res->pw; precno++) {
1541 opj_tcd_precinct_t *prec = &band->precincts[precno];
1542 if (prec->cblks.dec != NULL) {
1543 for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
1544 opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
1545 opj_free(cblk->data);
1546 opj_free(cblk->segs);
1548 opj_free(prec->cblks.dec);
1550 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1551 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1555 opj_free(band->precincts);
1558 opj_free(tilec->resolutions);
1560 opj_free(tile->comps);