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 #include "opj_includes.h"
35 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
36 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
38 fprintf(fd, "image {\n");
39 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
40 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
42 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
43 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
44 fprintf(fd, " tile {\n");
45 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
46 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
47 for (compno = 0; compno < tile->numcomps; compno++) {
48 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
49 fprintf(fd, " tilec {\n");
51 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
52 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
53 for (resno = 0; resno < tilec->numresolutions; resno++) {
54 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
55 fprintf(fd, "\n res {\n");
57 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
58 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
59 for (bandno = 0; bandno < res->numbands; bandno++) {
60 opj_tcd_band_t *band = &res->bands[bandno];
61 fprintf(fd, " band {\n");
63 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
64 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
65 for (precno = 0; precno < res->pw * res->ph; precno++) {
66 opj_tcd_precinct_t *prec = &band->precincts[precno];
67 fprintf(fd, " prec {\n");
69 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
70 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
72 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
73 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
74 fprintf(fd, " cblk {\n");
76 " x0=%d, y0=%d, x1=%d, y1=%d\n",
77 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
94 /* ----------------------------------------------------------------------- */
97 Create a new TCD handle
99 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
100 /* create the tcd structure */
101 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
102 if(!tcd) return NULL;
104 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
105 if(!tcd->tcd_image) {
114 Destroy a previously created TCD handle
116 void tcd_destroy(opj_tcd_t *tcd) {
118 opj_free(tcd->tcd_image);
123 /* ----------------------------------------------------------------------- */
125 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
126 int tileno, compno, resno, bandno, precno, cblkno;
130 tcd->tcd_image->tw = cp->tw;
131 tcd->tcd_image->th = cp->th;
132 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
134 for (tileno = 0; tileno < 1; tileno++) {
135 opj_tcp_t *tcp = &cp->tcps[curtileno];
138 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
139 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
140 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
142 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
143 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
145 /* 4 borders of the tile rescale on the image if necessary */
146 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
147 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
148 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
149 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
150 tile->numcomps = image->numcomps;
151 /* tile->PPT=image->PPT; */
153 /* Modification of the RATE >> */
154 for (j = 0; j < tcp->numlayers; j++) {
155 tcp->rates[j] = tcp->rates[j] ?
157 (((float) (tile->numcomps
158 * (tile->x1 - tile->x0)
159 * (tile->y1 - tile->y0)
160 * image->comps[0].prec))
161 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
163 ((float) (tile->numcomps
164 * (tile->x1 - tile->x0)
165 * (tile->y1 - tile->y0)
166 * image->comps[0].prec))/
167 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
171 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
172 tcp->rates[j] = tcp->rates[j - 1] + 20;
174 if (!j && tcp->rates[j] < 30)
178 if(j == (tcp->numlayers-1)){
179 tcp->rates[j] = tcp->rates[j]- 2;
183 /* << Modification of the RATE */
185 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
186 for (compno = 0; compno < tile->numcomps; compno++) {
187 opj_tccp_t *tccp = &tcp->tccps[compno];
189 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
191 /* border of each tile component (global) */
192 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
193 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
194 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
195 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
197 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
198 tilec->numresolutions = tccp->numresolutions;
200 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
202 for (resno = 0; resno < tilec->numresolutions; resno++) {
204 int levelno = tilec->numresolutions - 1 - resno;
205 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
206 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
207 int cbgwidthexpn, cbgheightexpn;
208 int cblkwidthexpn, cblkheightexpn;
210 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
212 /* border for each resolution level (global) */
213 res->x0 = int_ceildivpow2(tilec->x0, levelno);
214 res->y0 = int_ceildivpow2(tilec->y0, levelno);
215 res->x1 = int_ceildivpow2(tilec->x1, levelno);
216 res->y1 = int_ceildivpow2(tilec->y1, levelno);
218 res->numbands = resno == 0 ? 1 : 3;
219 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
220 if (tccp->csty & J2K_CCP_CSTY_PRT) {
221 pdx = tccp->prcw[resno];
222 pdy = tccp->prch[resno];
227 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
228 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
229 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
231 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
232 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
234 res->pw = (brprcxend - tlprcxstart) >> pdx;
235 res->ph = (brprcyend - tlprcystart) >> pdy;
238 tlcbgxstart = tlprcxstart;
239 tlcbgystart = tlprcystart;
240 brcbgxend = brprcxend;
241 brcbgyend = brprcyend;
245 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
246 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
247 brcbgxend = int_ceildivpow2(brprcxend, 1);
248 brcbgyend = int_ceildivpow2(brprcyend, 1);
249 cbgwidthexpn = pdx - 1;
250 cbgheightexpn = pdy - 1;
253 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
254 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
256 for (bandno = 0; bandno < res->numbands; bandno++) {
259 opj_stepsize_t *ss = NULL;
261 opj_tcd_band_t *band = &res->bands[bandno];
263 band->bandno = resno == 0 ? 0 : bandno + 1;
264 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
265 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
267 if (band->bandno == 0) {
268 /* band border (global) */
269 band->x0 = int_ceildivpow2(tilec->x0, levelno);
270 band->y0 = int_ceildivpow2(tilec->y0, levelno);
271 band->x1 = int_ceildivpow2(tilec->x1, levelno);
272 band->y1 = int_ceildivpow2(tilec->y1, levelno);
274 /* band border (global) */
275 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
276 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
277 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
278 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
281 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
282 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
283 numbps = image->comps[compno].prec + gain;
285 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
286 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
288 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
290 for (i = 0; i < res->pw * res->ph * 3; i++) {
291 band->precincts[i].imsbtree = NULL;
292 band->precincts[i].incltree = NULL;
295 for (precno = 0; precno < res->pw * res->ph; precno++) {
296 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
298 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
299 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
300 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
301 int cbgyend = cbgystart + (1 << cbgheightexpn);
303 opj_tcd_precinct_t *prc = &band->precincts[precno];
305 /* precinct size (global) */
306 prc->x0 = int_max(cbgxstart, band->x0);
307 prc->y0 = int_max(cbgystart, band->y0);
308 prc->x1 = int_min(cbgxend, band->x1);
309 prc->y1 = int_min(cbgyend, band->y1);
311 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
312 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
313 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
314 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
315 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
316 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
318 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
319 prc->incltree = tgt_create(prc->cw, prc->ch);
320 prc->imsbtree = tgt_create(prc->cw, prc->ch);
322 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
323 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
324 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
325 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
326 int cblkyend = cblkystart + (1 << cblkheightexpn);
328 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
330 /* code-block size (global) */
331 cblk->x0 = int_max(cblkxstart, prc->x0);
332 cblk->y0 = int_max(cblkystart, prc->y0);
333 cblk->x1 = int_min(cblkxend, prc->x1);
334 cblk->y1 = int_min(cblkyend, prc->y1);
335 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
336 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
338 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
339 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
347 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
350 void tcd_free_encode(opj_tcd_t *tcd) {
351 int tileno, compno, resno, bandno, precno, cblkno;
353 for (tileno = 0; tileno < 1; tileno++) {
354 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
356 for (compno = 0; compno < tile->numcomps; compno++) {
357 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
359 for (resno = 0; resno < tilec->numresolutions; resno++) {
360 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
362 for (bandno = 0; bandno < res->numbands; bandno++) {
363 opj_tcd_band_t *band = &res->bands[bandno];
365 for (precno = 0; precno < res->pw * res->ph; precno++) {
366 opj_tcd_precinct_t *prc = &band->precincts[precno];
368 if (prc->incltree != NULL) {
369 tgt_destroy(prc->incltree);
370 prc->incltree = NULL;
372 if (prc->imsbtree != NULL) {
373 tgt_destroy(prc->imsbtree);
374 prc->imsbtree = NULL;
376 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
377 opj_free(prc->cblks.enc[cblkno].data - 2);
378 opj_free(prc->cblks.enc[cblkno].layers);
379 opj_free(prc->cblks.enc[cblkno].passes);
381 opj_free(prc->cblks.enc);
383 opj_free(band->precincts);
384 band->precincts = NULL;
387 opj_free(tilec->resolutions);
388 tilec->resolutions = NULL;
390 opj_free(tile->comps);
393 opj_free(tcd->tcd_image->tiles);
394 tcd->tcd_image->tiles = NULL;
397 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
398 int tileno, compno, resno, bandno, precno, cblkno;
400 for (tileno = 0; tileno < 1; tileno++) {
401 opj_tcp_t *tcp = &cp->tcps[curtileno];
403 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
404 int p = curtileno % cp->tw;
405 int q = curtileno / cp->tw;
407 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
409 /* 4 borders of the tile rescale on the image if necessary */
410 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
411 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
412 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
413 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
415 tile->numcomps = image->numcomps;
416 /* tile->PPT=image->PPT; */
418 /* Modification of the RATE >> */
419 for (j = 0; j < tcp->numlayers; j++) {
420 tcp->rates[j] = tcp->rates[j] ?
422 (((float) (tile->numcomps
423 * (tile->x1 - tile->x0)
424 * (tile->y1 - tile->y0)
425 * image->comps[0].prec))
426 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
428 ((float) (tile->numcomps
429 * (tile->x1 - tile->x0)
430 * (tile->y1 - tile->y0)
431 * image->comps[0].prec))/
432 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
436 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
437 tcp->rates[j] = tcp->rates[j - 1] + 20;
439 if (!j && tcp->rates[j] < 30)
444 /* << Modification of the RATE */
446 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
447 for (compno = 0; compno < tile->numcomps; compno++) {
448 opj_tccp_t *tccp = &tcp->tccps[compno];
450 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
452 /* border of each tile component (global) */
453 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
454 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
455 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
456 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
458 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
459 tilec->numresolutions = tccp->numresolutions;
460 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
461 for (resno = 0; resno < tilec->numresolutions; resno++) {
464 int levelno = tilec->numresolutions - 1 - resno;
465 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
466 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
467 int cbgwidthexpn, cbgheightexpn;
468 int cblkwidthexpn, cblkheightexpn;
470 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
472 /* border for each resolution level (global) */
473 res->x0 = int_ceildivpow2(tilec->x0, levelno);
474 res->y0 = int_ceildivpow2(tilec->y0, levelno);
475 res->x1 = int_ceildivpow2(tilec->x1, levelno);
476 res->y1 = int_ceildivpow2(tilec->y1, levelno);
477 res->numbands = resno == 0 ? 1 : 3;
479 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
480 if (tccp->csty & J2K_CCP_CSTY_PRT) {
481 pdx = tccp->prcw[resno];
482 pdy = tccp->prch[resno];
487 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
488 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
489 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
490 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
491 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
493 res->pw = (brprcxend - tlprcxstart) >> pdx;
494 res->ph = (brprcyend - tlprcystart) >> pdy;
497 tlcbgxstart = tlprcxstart;
498 tlcbgystart = tlprcystart;
499 brcbgxend = brprcxend;
500 brcbgyend = brprcyend;
504 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
505 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
506 brcbgxend = int_ceildivpow2(brprcxend, 1);
507 brcbgyend = int_ceildivpow2(brprcyend, 1);
508 cbgwidthexpn = pdx - 1;
509 cbgheightexpn = pdy - 1;
512 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
513 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
515 for (bandno = 0; bandno < res->numbands; bandno++) {
518 opj_stepsize_t *ss = NULL;
520 opj_tcd_band_t *band = &res->bands[bandno];
522 band->bandno = resno == 0 ? 0 : bandno + 1;
523 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
524 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
526 if (band->bandno == 0) {
528 band->x0 = int_ceildivpow2(tilec->x0, levelno);
529 band->y0 = int_ceildivpow2(tilec->y0, levelno);
530 band->x1 = int_ceildivpow2(tilec->x1, levelno);
531 band->y1 = int_ceildivpow2(tilec->y1, levelno);
533 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
534 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
535 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
536 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
539 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
540 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
541 numbps = image->comps[compno].prec + gain;
542 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
543 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
545 for (precno = 0; precno < res->pw * res->ph; precno++) {
546 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
548 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
549 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
550 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
551 int cbgyend = cbgystart + (1 << cbgheightexpn);
553 opj_tcd_precinct_t *prc = &band->precincts[precno];
555 /* precinct size (global) */
556 prc->x0 = int_max(cbgxstart, band->x0);
557 prc->y0 = int_max(cbgystart, band->y0);
558 prc->x1 = int_min(cbgxend, band->x1);
559 prc->y1 = int_min(cbgyend, band->y1);
561 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
562 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
563 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
564 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
565 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
566 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
568 opj_free(prc->cblks.enc);
569 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
571 if (prc->incltree != NULL) {
572 tgt_destroy(prc->incltree);
574 if (prc->imsbtree != NULL) {
575 tgt_destroy(prc->imsbtree);
578 prc->incltree = tgt_create(prc->cw, prc->ch);
579 prc->imsbtree = tgt_create(prc->cw, prc->ch);
581 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
582 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
583 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
584 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
585 int cblkyend = cblkystart + (1 << cblkheightexpn);
587 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
589 /* code-block size (global) */
590 cblk->x0 = int_max(cblkxstart, prc->x0);
591 cblk->y0 = int_max(cblkystart, prc->y0);
592 cblk->x1 = int_min(cblkxend, prc->x1);
593 cblk->y1 = int_min(cblkyend, prc->y1);
594 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
595 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
597 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
598 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
606 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
609 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
610 int i, j, tileno, p, q;
611 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
614 tcd->tcd_image->tw = cp->tw;
615 tcd->tcd_image->th = cp->th;
616 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
619 Allocate place to store the decoded data = final image
620 Place limited by the tile really present in the codestream
623 for (j = 0; j < cp->tileno_size; j++) {
624 opj_tcd_tile_t *tile;
626 tileno = cp->tileno[j];
627 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
628 tile->numcomps = image->numcomps;
629 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
632 for (i = 0; i < image->numcomps; i++) {
633 for (j = 0; j < cp->tileno_size; j++) {
634 opj_tcd_tile_t *tile;
635 opj_tcd_tilecomp_t *tilec;
637 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
639 tileno = cp->tileno[j];
641 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
642 tilec = &tile->comps[i];
644 p = tileno % cp->tw; /* si numerotation matricielle .. */
645 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
647 /* 4 borders of the tile rescale on the image if necessary */
648 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
649 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
650 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
651 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
653 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
654 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
655 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
656 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
658 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
659 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->x0);
660 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
661 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
664 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
665 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
667 image->comps[i].w = w;
668 image->comps[i].h = h;
669 image->comps[i].x0 = x0;
670 image->comps[i].y0 = y0;
674 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) {
675 int compno, resno, bandno, precno, cblkno;
677 opj_tcd_tile_t *tile;
679 OPJ_ARG_NOT_USED(cstr_info);
683 tcp = &(cp->tcps[cp->tileno[tileno]]);
684 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
686 tileno = cp->tileno[tileno];
688 for (compno = 0; compno < tile->numcomps; compno++) {
689 opj_tccp_t *tccp = &tcp->tccps[compno];
690 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
692 /* border of each tile component (global) */
693 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
694 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
695 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
696 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
698 tilec->numresolutions = tccp->numresolutions;
699 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
701 for (resno = 0; resno < tilec->numresolutions; resno++) {
703 int levelno = tilec->numresolutions - 1 - resno;
704 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
705 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
706 int cbgwidthexpn, cbgheightexpn;
707 int cblkwidthexpn, cblkheightexpn;
709 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
711 /* border for each resolution level (global) */
712 res->x0 = int_ceildivpow2(tilec->x0, levelno);
713 res->y0 = int_ceildivpow2(tilec->y0, levelno);
714 res->x1 = int_ceildivpow2(tilec->x1, levelno);
715 res->y1 = int_ceildivpow2(tilec->y1, levelno);
716 res->numbands = resno == 0 ? 1 : 3;
718 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
719 if (tccp->csty & J2K_CCP_CSTY_PRT) {
720 pdx = tccp->prcw[resno];
721 pdy = tccp->prch[resno];
727 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
728 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
729 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
730 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
731 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
733 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
734 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
737 tlcbgxstart = tlprcxstart;
738 tlcbgystart = tlprcystart;
739 brcbgxend = brprcxend;
740 brcbgyend = brprcyend;
744 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
745 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
746 brcbgxend = int_ceildivpow2(brprcxend, 1);
747 brcbgyend = int_ceildivpow2(brprcyend, 1);
748 cbgwidthexpn = pdx - 1;
749 cbgheightexpn = pdy - 1;
752 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
753 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
755 for (bandno = 0; bandno < res->numbands; bandno++) {
758 opj_stepsize_t *ss = NULL;
760 opj_tcd_band_t *band = &res->bands[bandno];
761 band->bandno = resno == 0 ? 0 : bandno + 1;
762 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
763 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
765 if (band->bandno == 0) {
766 /* band border (global) */
767 band->x0 = int_ceildivpow2(tilec->x0, levelno);
768 band->y0 = int_ceildivpow2(tilec->y0, levelno);
769 band->x1 = int_ceildivpow2(tilec->x1, levelno);
770 band->y1 = int_ceildivpow2(tilec->y1, levelno);
772 /* band border (global) */
773 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
774 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
775 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
776 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
779 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
780 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
781 numbps = image->comps[compno].prec + gain;
782 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
783 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
785 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
787 for (precno = 0; precno < res->pw * res->ph; precno++) {
788 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
789 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
790 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
791 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
792 int cbgyend = cbgystart + (1 << cbgheightexpn);
794 opj_tcd_precinct_t *prc = &band->precincts[precno];
795 /* precinct size (global) */
796 prc->x0 = int_max(cbgxstart, band->x0);
797 prc->y0 = int_max(cbgystart, band->y0);
798 prc->x1 = int_min(cbgxend, band->x1);
799 prc->y1 = int_min(cbgyend, band->y1);
801 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
802 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
803 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
804 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
805 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
806 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
808 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
810 prc->incltree = tgt_create(prc->cw, prc->ch);
811 prc->imsbtree = tgt_create(prc->cw, prc->ch);
813 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
814 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
815 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
816 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
817 int cblkyend = cblkystart + (1 << cblkheightexpn);
819 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
822 /* code-block size (global) */
823 cblk->x0 = int_max(cblkxstart, prc->x0);
824 cblk->y0 = int_max(cblkystart, prc->y0);
825 cblk->x1 = int_min(cblkxend, prc->x1);
826 cblk->y1 = int_min(cblkyend, prc->y1);
833 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
836 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
837 int compno, resno, bandno, precno, cblkno;
838 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
839 int matrice[10][10][3];
842 opj_cp_t *cp = tcd->cp;
843 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
844 opj_tcp_t *tcd_tcp = tcd->tcp;
846 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
848 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
849 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
850 for (i = 0; i < tcd_tcp->numlayers; i++) {
851 for (j = 0; j < tilec->numresolutions; j++) {
852 for (k = 0; k < 3; k++) {
854 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
855 * (float) (tcd->image->comps[compno].prec / 16.0));
860 for (resno = 0; resno < tilec->numresolutions; resno++) {
861 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
862 for (bandno = 0; bandno < res->numbands; bandno++) {
863 opj_tcd_band_t *band = &res->bands[bandno];
864 for (precno = 0; precno < res->pw * res->ph; precno++) {
865 opj_tcd_precinct_t *prc = &band->precincts[precno];
866 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
867 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
868 opj_tcd_layer_t *layer = &cblk->layers[layno];
870 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
871 /* Correction of the matrix of coefficient to include the IMSB information */
873 value = matrice[layno][resno][bandno];
880 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
881 if (imsb >= matrice[layno - 1][resno][bandno]) {
882 value -= (imsb - matrice[layno - 1][resno][bandno]);
890 cblk->numpassesinlayers = 0;
893 n = cblk->numpassesinlayers;
894 if (cblk->numpassesinlayers == 0) {
896 n = 3 * value - 2 + cblk->numpassesinlayers;
898 n = cblk->numpassesinlayers;
901 n = 3 * value + cblk->numpassesinlayers;
904 layer->numpasses = n - cblk->numpassesinlayers;
906 if (!layer->numpasses)
909 if (cblk->numpassesinlayers == 0) {
910 layer->len = cblk->passes[n - 1].rate;
911 layer->data = cblk->data;
913 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
914 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
917 cblk->numpassesinlayers = n;
925 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
927 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
928 tcd_makelayer_fixed(tcd, layno, 1);
932 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
933 int compno, resno, bandno, precno, cblkno, passno;
935 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
937 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
939 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
940 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
941 for (resno = 0; resno < tilec->numresolutions; resno++) {
942 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
943 for (bandno = 0; bandno < res->numbands; bandno++) {
944 opj_tcd_band_t *band = &res->bands[bandno];
945 for (precno = 0; precno < res->pw * res->ph; precno++) {
946 opj_tcd_precinct_t *prc = &band->precincts[precno];
947 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
948 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
949 opj_tcd_layer_t *layer = &cblk->layers[layno];
953 cblk->numpassesinlayers = 0;
955 n = cblk->numpassesinlayers;
956 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
959 opj_tcd_pass_t *pass = &cblk->passes[passno];
962 dd = pass->distortiondec;
964 dr = pass->rate - cblk->passes[n - 1].rate;
965 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
972 if (dd / dr >= thresh)
975 layer->numpasses = n - cblk->numpassesinlayers;
977 if (!layer->numpasses) {
981 if (cblk->numpassesinlayers == 0) {
982 layer->len = cblk->passes[n - 1].rate;
983 layer->data = cblk->data;
984 layer->disto = cblk->passes[n - 1].distortiondec;
986 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
987 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
988 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
991 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
994 cblk->numpassesinlayers = n;
1002 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1003 int compno, resno, bandno, precno, cblkno, passno, layno;
1005 double cumdisto[100]; /* fixed_quality */
1006 const double K = 1; /* 1.1; fixed_quality */
1009 opj_cp_t *cp = tcd->cp;
1010 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1011 opj_tcp_t *tcd_tcp = tcd->tcp;
1016 tcd_tile->numpix = 0; /* fixed_quality */
1018 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1019 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1022 for (resno = 0; resno < tilec->numresolutions; resno++) {
1023 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1025 for (bandno = 0; bandno < res->numbands; bandno++) {
1026 opj_tcd_band_t *band = &res->bands[bandno];
1028 for (precno = 0; precno < res->pw * res->ph; precno++) {
1029 opj_tcd_precinct_t *prc = &band->precincts[precno];
1031 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1032 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1034 for (passno = 0; passno < cblk->totalpasses; passno++) {
1035 opj_tcd_pass_t *pass = &cblk->passes[passno];
1040 dd = pass->distortiondec;
1042 dr = pass->rate - cblk->passes[passno - 1].rate;
1043 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1049 if (rdslope < min) {
1052 if (rdslope > max) {
1058 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1059 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1065 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1066 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1067 * ((double)(tilec->numpix));
1072 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1073 tile_info->numpix = tcd_tile->numpix;
1074 tile_info->distotile = tcd_tile->distotile;
1075 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1078 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1082 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1083 double goodthresh = 0;
1084 double stable_thresh = 0;
1086 double distotarget; /* fixed_quality */
1089 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1091 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1092 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1093 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1094 ==> possible to have some lossy layers and the last layer for sure lossless */
1095 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1096 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1099 for (i = 0; i < 128; i++) {
1101 double distoachieved = 0; /* fixed_quality */
1102 thresh = (lo + hi) / 2;
1104 tcd_makelayer(tcd, layno, thresh, 0);
1106 if (cp->fixed_quality) { /* fixed_quality */
1108 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);
1113 distoachieved = layno == 0 ?
1114 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1115 if (distoachieved < distotarget) {
1117 stable_thresh = thresh;
1124 distoachieved = (layno == 0) ?
1125 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1126 if (distoachieved < distotarget) {
1128 stable_thresh = thresh;
1134 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);
1135 /* TODO: what to do with l ??? seek / tell ??? */
1136 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1142 stable_thresh = thresh;
1146 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1157 if(cstr_info) { /* Threshold for Marcela Index */
1158 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1160 tcd_makelayer(tcd, layno, goodthresh, 1);
1163 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1169 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1171 int l, i, numpacks = 0;
1172 opj_tcd_tile_t *tile = NULL;
1173 opj_tcp_t *tcd_tcp = NULL;
1174 opj_cp_t *cp = NULL;
1176 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1177 opj_tccp_t *tccp = &tcp->tccps[0];
1178 opj_image_t *image = tcd->image;
1180 opj_t1_t *t1 = NULL; /* T1 component */
1181 opj_t2_t *t2 = NULL; /* T2 component */
1183 tcd->tcd_tileno = tileno;
1184 tcd->tcd_tile = tcd->tcd_image->tiles;
1185 tcd->tcp = &tcd->cp->tcps[tileno];
1187 tile = tcd->tcd_tile;
1191 if(tcd->cur_tp_num == 0){
1192 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1193 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1195 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1196 for (i = 0; i < tilec_idx->numresolutions; i++) {
1197 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1199 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1200 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1202 numpacks += res_idx->pw * res_idx->ph;
1204 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1205 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1207 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1211 /*---------------TILE-------------------*/
1213 for (compno = 0; compno < tile->numcomps; compno++) {
1216 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1217 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1218 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1220 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1221 int tw = tilec->x1 - tilec->x0;
1222 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1224 /* extract tile data */
1226 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1227 for (y = tilec->y0; y < tilec->y1; y++) {
1228 /* start of the src tile scanline */
1229 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1230 /* start of the dst tile scanline */
1231 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1232 for (x = tilec->x0; x < tilec->x1; x++) {
1233 *tile_data++ = *data++ - adjust;
1236 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1237 for (y = tilec->y0; y < tilec->y1; y++) {
1238 /* start of the src tile scanline */
1239 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1240 /* start of the dst tile scanline */
1241 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1242 for (x = tilec->x0; x < tilec->x1; x++) {
1243 *tile_data++ = (*data++ - adjust) << 11;
1250 /*----------------MCT-------------------*/
1252 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1253 if (tcd_tcp->tccps[0].qmfbid == 0) {
1254 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1256 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1260 /*----------------DWT---------------------*/
1262 for (compno = 0; compno < tile->numcomps; compno++) {
1263 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1264 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1266 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1267 dwt_encode_real(tilec);
1271 /*------------------TIER1-----------------*/
1272 t1 = t1_create(tcd->cinfo);
1273 t1_encode_cblks(t1, tile, tcd_tcp);
1276 /*-----------RATE-ALLOCATE------------------*/
1280 cstr_info->index_write = 0;
1282 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1283 /* Normal Rate/distortion allocation */
1284 tcd_rateallocate(tcd, dest, len, cstr_info);
1286 /* Fixed layer allocation */
1287 tcd_rateallocate_fixed(tcd);
1290 /*--------------TIER2------------------*/
1294 cstr_info->index_write = 1;
1297 t2 = t2_create(tcd->cinfo, image, cp);
1298 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);
1301 /*---------------CLEAN-------------------*/
1304 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1305 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1306 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1308 /* cleaning memory */
1309 for (compno = 0; compno < tile->numcomps; compno++) {
1310 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1311 opj_aligned_free(tilec->data);
1318 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1322 double tile_time, t1_time, dwt_time;
1323 opj_tcd_tile_t *tile = NULL;
1325 opj_t1_t *t1 = NULL; /* T1 component */
1326 opj_t2_t *t2 = NULL; /* T2 component */
1328 tcd->tcd_tileno = tileno;
1329 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1330 tcd->tcp = &(tcd->cp->tcps[tileno]);
1331 tile = tcd->tcd_tile;
1333 tile_time = opj_clock(); /* time needed to decode a tile */
1334 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1338 int resno, compno, numprec = 0;
1339 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1340 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1341 opj_tccp_t *tccp = &tcp->tccps[compno];
1342 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1343 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1344 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1345 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1346 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1347 numprec += res_idx->pw * res_idx->ph;
1348 if (tccp->csty & J2K_CP_CSTY_PRT) {
1349 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1350 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1353 cstr_info->tile[tileno].pdx[resno] = 15;
1354 cstr_info->tile[tileno].pdx[resno] = 15;
1358 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1359 cstr_info->packno = 0;
1363 /*--------------TIER2------------------*/
1365 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1366 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1371 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1374 /*------------------TIER1-----------------*/
1376 t1_time = opj_clock(); /* time needed to decode a tile */
1377 t1 = t1_create(tcd->cinfo);
1378 for (compno = 0; compno < tile->numcomps; ++compno) {
1379 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1380 /* The +3 is headroom required by the vectorized DWT */
1381 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1382 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1385 t1_time = opj_clock() - t1_time;
1386 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1388 /*----------------DWT---------------------*/
1390 dwt_time = opj_clock(); /* time needed to decode a tile */
1391 for (compno = 0; compno < tile->numcomps; compno++) {
1392 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1395 if (tcd->cp->reduce != 0) {
1396 tcd->image->comps[compno].resno_decoded =
1397 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1398 if (tcd->image->comps[compno].resno_decoded < 0) {
1399 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1400 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1405 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1406 if(numres2decode > 0){
1407 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1408 dwt_decode(tilec, numres2decode);
1410 dwt_decode_real(tilec, numres2decode);
1414 dwt_time = opj_clock() - dwt_time;
1415 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1417 /*----------------MCT-------------------*/
1419 if (tcd->tcp->mct) {
1420 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1422 if (tile->numcomps >= 3 ){
1423 if (tcd->tcp->tccps[0].qmfbid == 1) {
1425 tile->comps[0].data,
1426 tile->comps[1].data,
1427 tile->comps[2].data,
1431 (float*)tile->comps[0].data,
1432 (float*)tile->comps[1].data,
1433 (float*)tile->comps[2].data,
1437 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1441 /*---------------TILE-------------------*/
1443 for (compno = 0; compno < tile->numcomps; ++compno) {
1444 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1445 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1446 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1447 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1448 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1449 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1451 int tw = tilec->x1 - tilec->x0;
1454 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1455 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1459 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1461 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1462 for(j = res->y0; j < res->y1; ++j) {
1463 for(i = res->x0; i < res->x1; ++i) {
1464 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1466 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1470 for(j = res->y0; j < res->y1; ++j) {
1471 for(i = res->x0; i < res->x1; ++i) {
1472 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1473 int v = lrintf(tmp);
1475 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1479 opj_aligned_free(tilec->data);
1482 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1483 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1492 void tcd_free_decode(opj_tcd_t *tcd) {
1493 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1494 opj_free(tcd_image->tiles);
1497 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1498 int compno,resno,bandno,precno;
1500 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1502 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1503 for (compno = 0; compno < tile->numcomps; compno++) {
1504 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1505 for (resno = 0; resno < tilec->numresolutions; resno++) {
1506 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1507 for (bandno = 0; bandno < res->numbands; bandno++) {
1508 opj_tcd_band_t *band = &res->bands[bandno];
1509 for (precno = 0; precno < res->ph * res->pw; precno++) {
1510 opj_tcd_precinct_t *prec = &band->precincts[precno];
1511 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1512 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1514 opj_free(band->precincts);
1517 opj_free(tilec->resolutions);
1519 opj_free(tile->comps);