2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #define _ISOC99_SOURCE /* lrintf is C99 */
40 #include "opj_includes.h"
42 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
44 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
46 fprintf(fd, "image {\n");
47 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
48 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
51 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
52 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
53 fprintf(fd, " tile {\n");
54 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
55 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
56 for (compno = 0; compno < tile->numcomps; compno++) {
57 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
58 fprintf(fd, " tilec {\n");
60 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
61 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
62 for (resno = 0; resno < tilec->numresolutions; resno++) {
63 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
64 fprintf(fd, "\n res {\n");
66 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
67 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
68 for (bandno = 0; bandno < res->numbands; bandno++) {
69 opj_tcd_band_t *band = &res->bands[bandno];
70 fprintf(fd, " band {\n");
72 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
73 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
74 for (precno = 0; precno < res->pw * res->ph; precno++) {
75 opj_tcd_precinct_t *prec = &band->precincts[precno];
76 fprintf(fd, " prec {\n");
78 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
79 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
81 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
82 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
83 fprintf(fd, " cblk {\n");
85 " x0=%d, y0=%d, x1=%d, y1=%d\n",
86 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
103 /* ----------------------------------------------------------------------- */
106 Create a new TCD handle
108 opj_tcd_t* tcd_create(opj_common_ptr cinfo)
110 /* create the tcd structure */
111 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
116 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
117 if (!tcd->tcd_image) {
126 Destroy a previously created TCD handle
128 void tcd_destroy(opj_tcd_t *tcd)
131 opj_free(tcd->tcd_image);
136 /* ----------------------------------------------------------------------- */
138 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
141 int tileno, compno, resno, bandno, precno, cblkno;
145 tcd->tcd_image->tw = cp->tw;
146 tcd->tcd_image->th = cp->th;
147 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
149 for (tileno = 0; tileno < 1; tileno++) {
150 opj_tcp_t *tcp = &cp->tcps[curtileno];
153 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
154 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
156 cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
158 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
159 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
161 /* 4 borders of the tile rescale on the image if necessary */
162 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
163 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
164 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
165 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
166 tile->numcomps = image->numcomps;
167 /* tile->PPT=image->PPT; */
169 /* Modification of the RATE >> */
170 for (j = 0; j < tcp->numlayers; j++) {
171 tcp->rates[j] = tcp->rates[j] ?
173 (((float)(tile->numcomps
174 * (tile->x1 - tile->x0)
175 * (tile->y1 - tile->y0)
176 * image->comps[0].prec))
177 / (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((
178 tcd->cur_totnum_tp - 1) * 14) / tcp->numlayers)
180 ((float)(tile->numcomps
181 * (tile->x1 - tile->x0)
182 * (tile->y1 - tile->y0)
183 * image->comps[0].prec)) /
184 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
188 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
189 tcp->rates[j] = tcp->rates[j - 1] + 20;
191 if (!j && tcp->rates[j] < 30) {
196 if (j == (tcp->numlayers - 1)) {
197 tcp->rates[j] = tcp->rates[j] - 2;
201 /* << Modification of the RATE */
203 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(
204 opj_tcd_tilecomp_t));
205 for (compno = 0; compno < tile->numcomps; compno++) {
206 opj_tccp_t *tccp = &tcp->tccps[compno];
208 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
210 /* border of each tile component (global) */
211 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
212 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
213 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
214 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
216 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) *
217 (tilec->y1 - tilec->y0) * sizeof(int));
218 tilec->numresolutions = tccp->numresolutions;
220 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(
221 tilec->numresolutions * sizeof(opj_tcd_resolution_t));
223 for (resno = 0; resno < tilec->numresolutions; resno++) {
225 int levelno = tilec->numresolutions - 1 - resno;
226 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
227 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
228 int cbgwidthexpn, cbgheightexpn;
229 int cblkwidthexpn, cblkheightexpn;
231 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
233 /* border for each resolution level (global) */
234 res->x0 = int_ceildivpow2(tilec->x0, levelno);
235 res->y0 = int_ceildivpow2(tilec->y0, levelno);
236 res->x1 = int_ceildivpow2(tilec->x1, levelno);
237 res->y1 = int_ceildivpow2(tilec->y1, levelno);
239 res->numbands = resno == 0 ? 1 : 3;
240 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
241 if (tccp->csty & J2K_CCP_CSTY_PRT) {
242 pdx = tccp->prcw[resno];
243 pdy = tccp->prch[resno];
248 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
249 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
250 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
252 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
253 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
255 res->pw = (brprcxend - tlprcxstart) >> pdx;
256 res->ph = (brprcyend - tlprcystart) >> pdy;
259 tlcbgxstart = tlprcxstart;
260 tlcbgystart = tlprcystart;
261 brcbgxend = brprcxend;
262 brcbgyend = brprcyend;
266 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
267 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
268 brcbgxend = int_ceildivpow2(brprcxend, 1);
269 brcbgyend = int_ceildivpow2(brprcyend, 1);
270 cbgwidthexpn = pdx - 1;
271 cbgheightexpn = pdy - 1;
274 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
275 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
277 for (bandno = 0; bandno < res->numbands; bandno++) {
280 opj_stepsize_t *ss = NULL;
282 opj_tcd_band_t *band = &res->bands[bandno];
284 band->bandno = resno == 0 ? 0 : bandno + 1;
285 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
286 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
288 if (band->bandno == 0) {
289 /* band border (global) */
290 band->x0 = int_ceildivpow2(tilec->x0, levelno);
291 band->y0 = int_ceildivpow2(tilec->y0, levelno);
292 band->x1 = int_ceildivpow2(tilec->x1, levelno);
293 band->y1 = int_ceildivpow2(tilec->y1, levelno);
295 /* band border (global) */
296 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
297 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
298 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
299 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
302 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
303 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(
305 numbps = image->comps[compno].prec + gain;
307 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0,
309 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
311 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph *
312 sizeof(opj_tcd_precinct_t));
314 for (i = 0; i < res->pw * res->ph * 3; i++) {
315 band->precincts[i].imsbtree = NULL;
316 band->precincts[i].incltree = NULL;
317 band->precincts[i].cblks.enc = NULL;
320 for (precno = 0; precno < res->pw * res->ph; precno++) {
321 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
323 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
324 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
325 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
326 int cbgyend = cbgystart + (1 << cbgheightexpn);
328 opj_tcd_precinct_t *prc = &band->precincts[precno];
330 /* precinct size (global) */
331 prc->x0 = int_max(cbgxstart, band->x0);
332 prc->y0 = int_max(cbgystart, band->y0);
333 prc->x1 = int_min(cbgxend, band->x1);
334 prc->y1 = int_min(cbgyend, band->y1);
336 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
337 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
338 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
339 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
340 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
341 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
343 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch),
344 sizeof(opj_tcd_cblk_enc_t));
345 prc->incltree = tgt_create(prc->cw, prc->ch);
346 prc->imsbtree = tgt_create(prc->cw, prc->ch);
348 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
349 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
350 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
351 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
352 int cblkyend = cblkystart + (1 << cblkheightexpn);
354 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
356 /* code-block size (global) */
357 cblk->x0 = int_max(cblkxstart, prc->x0);
358 cblk->y0 = int_max(cblkystart, prc->y0);
359 cblk->x1 = int_min(cblkxend, prc->x1);
360 cblk->y1 = int_min(cblkyend, prc->y1);
361 cblk->data = (unsigned char*) opj_calloc(9728 + 2, sizeof(unsigned char));
362 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
366 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
367 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
375 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
378 void tcd_free_encode(opj_tcd_t *tcd)
380 int tileno, compno, resno, bandno, precno, cblkno;
382 for (tileno = 0; tileno < 1; tileno++) {
383 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
385 for (compno = 0; compno < tile->numcomps; compno++) {
386 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
388 for (resno = 0; resno < tilec->numresolutions; resno++) {
389 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
391 for (bandno = 0; bandno < res->numbands; bandno++) {
392 opj_tcd_band_t *band = &res->bands[bandno];
394 for (precno = 0; precno < res->pw * res->ph; precno++) {
395 opj_tcd_precinct_t *prc = &band->precincts[precno];
397 if (prc->incltree != NULL) {
398 tgt_destroy(prc->incltree);
399 prc->incltree = NULL;
401 if (prc->imsbtree != NULL) {
402 tgt_destroy(prc->imsbtree);
403 prc->imsbtree = NULL;
405 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
406 opj_free(prc->cblks.enc[cblkno].data - 2);
407 opj_free(prc->cblks.enc[cblkno].layers);
408 opj_free(prc->cblks.enc[cblkno].passes);
410 opj_free(prc->cblks.enc);
412 opj_free(band->precincts);
413 band->precincts = NULL;
416 opj_free(tilec->resolutions);
417 tilec->resolutions = NULL;
419 opj_free(tile->comps);
422 opj_free(tcd->tcd_image->tiles);
423 tcd->tcd_image->tiles = NULL;
426 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
429 int tileno, compno, resno, bandno, precno, cblkno;
431 for (tileno = 0; tileno < 1; tileno++) {
432 opj_tcp_t *tcp = &cp->tcps[curtileno];
434 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
435 int p = curtileno % cp->tw;
436 int q = curtileno / cp->tw;
438 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
440 /* 4 borders of the tile rescale on the image if necessary */
441 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
442 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
443 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
444 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
446 tile->numcomps = image->numcomps;
447 /* tile->PPT=image->PPT; */
449 /* Modification of the RATE >> */
450 for (j = 0; j < tcp->numlayers; j++) {
451 tcp->rates[j] = tcp->rates[j] ?
453 (((float)(tile->numcomps
454 * (tile->x1 - tile->x0)
455 * (tile->y1 - tile->y0)
456 * image->comps[0].prec))
457 / (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((
458 tcd->cur_totnum_tp - 1) * 14) / tcp->numlayers)
460 ((float)(tile->numcomps
461 * (tile->x1 - tile->x0)
462 * (tile->y1 - tile->y0)
463 * image->comps[0].prec)) /
464 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
468 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
469 tcp->rates[j] = tcp->rates[j - 1] + 20;
471 if (!j && tcp->rates[j] < 30) {
477 /* << Modification of the RATE */
479 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
480 for (compno = 0; compno < tile->numcomps; compno++) {
481 opj_tccp_t *tccp = &tcp->tccps[compno];
483 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
485 /* border of each tile component (global) */
486 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
487 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
488 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
489 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
491 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) *
492 (tilec->y1 - tilec->y0) * sizeof(int));
493 tilec->numresolutions = tccp->numresolutions;
494 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
495 for (resno = 0; resno < tilec->numresolutions; resno++) {
498 int levelno = tilec->numresolutions - 1 - resno;
499 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
500 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
501 int cbgwidthexpn, cbgheightexpn;
502 int cblkwidthexpn, cblkheightexpn;
504 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
506 /* border for each resolution level (global) */
507 res->x0 = int_ceildivpow2(tilec->x0, levelno);
508 res->y0 = int_ceildivpow2(tilec->y0, levelno);
509 res->x1 = int_ceildivpow2(tilec->x1, levelno);
510 res->y1 = int_ceildivpow2(tilec->y1, levelno);
511 res->numbands = resno == 0 ? 1 : 3;
513 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
514 if (tccp->csty & J2K_CCP_CSTY_PRT) {
515 pdx = tccp->prcw[resno];
516 pdy = tccp->prch[resno];
521 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
522 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
523 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
524 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
525 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
527 res->pw = (brprcxend - tlprcxstart) >> pdx;
528 res->ph = (brprcyend - tlprcystart) >> pdy;
531 tlcbgxstart = tlprcxstart;
532 tlcbgystart = tlprcystart;
533 brcbgxend = brprcxend;
534 brcbgyend = brprcyend;
538 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
539 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
540 brcbgxend = int_ceildivpow2(brprcxend, 1);
541 brcbgyend = int_ceildivpow2(brprcyend, 1);
542 cbgwidthexpn = pdx - 1;
543 cbgheightexpn = pdy - 1;
546 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
547 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
549 for (bandno = 0; bandno < res->numbands; bandno++) {
552 opj_stepsize_t *ss = NULL;
554 opj_tcd_band_t *band = &res->bands[bandno];
556 band->bandno = resno == 0 ? 0 : bandno + 1;
557 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
558 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
560 if (band->bandno == 0) {
562 band->x0 = int_ceildivpow2(tilec->x0, levelno);
563 band->y0 = int_ceildivpow2(tilec->y0, levelno);
564 band->x1 = int_ceildivpow2(tilec->x1, levelno);
565 band->y1 = int_ceildivpow2(tilec->y1, levelno);
567 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
568 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
569 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
570 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
573 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
574 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(
576 numbps = image->comps[compno].prec + gain;
577 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0,
579 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
581 for (precno = 0; precno < res->pw * res->ph; precno++) {
582 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
584 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
585 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
586 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
587 int cbgyend = cbgystart + (1 << cbgheightexpn);
589 opj_tcd_precinct_t *prc = &band->precincts[precno];
591 /* precinct size (global) */
592 prc->x0 = int_max(cbgxstart, band->x0);
593 prc->y0 = int_max(cbgystart, band->y0);
594 prc->x1 = int_min(cbgxend, band->x1);
595 prc->y1 = int_min(cbgyend, band->y1);
597 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
598 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
599 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
600 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
601 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
602 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
604 opj_free(prc->cblks.enc);
605 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch,
606 sizeof(opj_tcd_cblk_enc_t));
608 if (prc->incltree != NULL) {
609 tgt_destroy(prc->incltree);
611 if (prc->imsbtree != NULL) {
612 tgt_destroy(prc->imsbtree);
615 prc->incltree = tgt_create(prc->cw, prc->ch);
616 prc->imsbtree = tgt_create(prc->cw, prc->ch);
618 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
619 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
620 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
621 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
622 int cblkyend = cblkystart + (1 << cblkheightexpn);
624 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
626 /* code-block size (global) */
627 cblk->x0 = int_max(cblkxstart, prc->x0);
628 cblk->y0 = int_max(cblkystart, prc->y0);
629 cblk->x1 = int_min(cblkxend, prc->x1);
630 cblk->y1 = int_min(cblkyend, prc->y1);
631 cblk->data = (unsigned char*) opj_calloc(8192 + 2, sizeof(unsigned char));
632 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
636 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
637 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
645 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
648 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp)
650 int i, j, tileno, p, q;
651 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
654 tcd->tcd_image->tw = cp->tw;
655 tcd->tcd_image->th = cp->th;
656 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(cp->tw * cp->th,
657 sizeof(opj_tcd_tile_t));
660 Allocate place to store the decoded data = final image
661 Place limited by the tile really present in the codestream
664 for (j = 0; j < cp->tileno_size; j++) {
665 opj_tcd_tile_t *tile;
667 tileno = cp->tileno[j];
668 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
669 tile->numcomps = image->numcomps;
670 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps,
671 sizeof(opj_tcd_tilecomp_t));
674 for (i = 0; i < image->numcomps; i++) {
675 for (j = 0; j < cp->tileno_size; j++) {
676 opj_tcd_tile_t *tile;
677 opj_tcd_tilecomp_t *tilec;
679 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
681 tileno = cp->tileno[j];
683 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
684 tilec = &tile->comps[i];
686 p = tileno % cp->tw; /* si numerotation matricielle .. */
688 cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
690 /* 4 borders of the tile rescale on the image if necessary */
691 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
692 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
693 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
694 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
696 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
697 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
698 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
699 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
701 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
702 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
703 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
704 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
707 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
708 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
710 image->comps[i].w = w;
711 image->comps[i].h = h;
712 image->comps[i].x0 = x0;
713 image->comps[i].y0 = y0;
717 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp,
718 int tileno, opj_codestream_info_t *cstr_info)
720 int compno, resno, bandno, precno, cblkno;
722 opj_tcd_tile_t *tile;
724 OPJ_ARG_NOT_USED(cstr_info);
728 tcp = &(cp->tcps[cp->tileno[tileno]]);
729 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
731 tileno = cp->tileno[tileno];
733 for (compno = 0; compno < tile->numcomps; compno++) {
734 opj_tccp_t *tccp = &tcp->tccps[compno];
735 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
737 if (tccp->numresolutions <= 0) {
738 cp->tileno[tileno] = -1;
742 /* border of each tile component (global) */
743 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
744 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
745 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
746 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
748 tilec->numresolutions = tccp->numresolutions;
749 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(
750 tilec->numresolutions * sizeof(opj_tcd_resolution_t));
752 for (resno = 0; resno < tilec->numresolutions; resno++) {
754 int levelno = tilec->numresolutions - 1 - resno;
755 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
756 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
757 int cbgwidthexpn, cbgheightexpn;
758 int cblkwidthexpn, cblkheightexpn;
760 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
762 /* border for each resolution level (global) */
763 res->x0 = int_ceildivpow2(tilec->x0, levelno);
764 res->y0 = int_ceildivpow2(tilec->y0, levelno);
765 res->x1 = int_ceildivpow2(tilec->x1, levelno);
766 res->y1 = int_ceildivpow2(tilec->y1, levelno);
767 res->numbands = resno == 0 ? 1 : 3;
769 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
770 if (tccp->csty & J2K_CCP_CSTY_PRT) {
771 pdx = tccp->prcw[resno];
772 pdy = tccp->prch[resno];
778 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
779 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
780 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
781 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
782 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
784 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
785 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
788 tlcbgxstart = tlprcxstart;
789 tlcbgystart = tlprcystart;
790 brcbgxend = brprcxend;
791 brcbgyend = brprcyend;
795 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
796 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
797 brcbgxend = int_ceildivpow2(brprcxend, 1);
798 brcbgyend = int_ceildivpow2(brprcyend, 1);
799 cbgwidthexpn = pdx - 1;
800 cbgheightexpn = pdy - 1;
803 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
804 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
806 for (bandno = 0; bandno < res->numbands; bandno++) {
809 opj_stepsize_t *ss = NULL;
811 opj_tcd_band_t *band = &res->bands[bandno];
812 band->bandno = resno == 0 ? 0 : bandno + 1;
813 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
814 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
816 if (band->bandno == 0) {
817 /* band border (global) */
818 band->x0 = int_ceildivpow2(tilec->x0, levelno);
819 band->y0 = int_ceildivpow2(tilec->y0, levelno);
820 band->x1 = int_ceildivpow2(tilec->x1, levelno);
821 band->y1 = int_ceildivpow2(tilec->y1, levelno);
823 /* band border (global) */
824 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
825 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
826 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
827 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
830 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
831 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(
833 numbps = image->comps[compno].prec + gain;
834 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0,
835 numbps - ss->expn)) * 0.5);
836 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
838 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(
839 opj_tcd_precinct_t));
841 for (precno = 0; precno < res->pw * res->ph; precno++) {
842 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
843 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
844 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
845 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
846 int cbgyend = cbgystart + (1 << cbgheightexpn);
848 opj_tcd_precinct_t *prc = &band->precincts[precno];
849 /* precinct size (global) */
850 prc->x0 = int_max(cbgxstart, band->x0);
851 prc->y0 = int_max(cbgystart, band->y0);
852 prc->x1 = int_min(cbgxend, band->x1);
853 prc->y1 = int_min(cbgyend, band->y1);
855 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
856 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
857 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
858 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
859 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
860 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
862 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(
863 opj_tcd_cblk_dec_t));
865 prc->incltree = tgt_create(prc->cw, prc->ch);
866 prc->imsbtree = tgt_create(prc->cw, prc->ch);
868 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
869 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
870 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
871 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
872 int cblkyend = cblkystart + (1 << cblkheightexpn);
874 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
877 /* code-block size (global) */
878 cblk->x0 = int_max(cblkxstart, prc->x0);
879 cblk->y0 = int_max(cblkystart, prc->y0);
880 cblk->x1 = int_min(cblkxend, prc->x1);
881 cblk->y1 = int_min(cblkyend, prc->y1);
888 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
891 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final)
893 int compno, resno, bandno, precno, cblkno;
894 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
895 int matrice[10][10][3];
898 opj_cp_t *cp = tcd->cp;
899 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
900 opj_tcp_t *tcd_tcp = tcd->tcp;
902 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
904 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
905 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
906 for (i = 0; i < tcd_tcp->numlayers; i++) {
907 for (j = 0; j < tilec->numresolutions; j++) {
908 for (k = 0; k < 3; k++) {
910 (int)(cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
911 * (float)(tcd->image->comps[compno].prec / 16.0));
916 for (resno = 0; resno < tilec->numresolutions; resno++) {
917 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
918 for (bandno = 0; bandno < res->numbands; bandno++) {
919 opj_tcd_band_t *band = &res->bands[bandno];
920 for (precno = 0; precno < res->pw * res->ph; precno++) {
921 opj_tcd_precinct_t *prc = &band->precincts[precno];
922 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
923 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
924 opj_tcd_layer_t *layer = &cblk->layers[layno];
926 int imsb = tcd->image->comps[compno].prec -
927 cblk->numbps; /* number of bit-plan equal to zero */
928 /* Correction of the matrix of coefficient to include the IMSB information */
930 value = matrice[layno][resno][bandno];
937 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
938 if (imsb >= matrice[layno - 1][resno][bandno]) {
939 value -= (imsb - matrice[layno - 1][resno][bandno]);
947 cblk->numpassesinlayers = 0;
950 n = cblk->numpassesinlayers;
951 if (cblk->numpassesinlayers == 0) {
953 n = 3 * value - 2 + cblk->numpassesinlayers;
955 n = cblk->numpassesinlayers;
958 n = 3 * value + cblk->numpassesinlayers;
961 layer->numpasses = n - cblk->numpassesinlayers;
963 if (!layer->numpasses) {
967 if (cblk->numpassesinlayers == 0) {
968 layer->len = cblk->passes[n - 1].rate;
969 layer->data = cblk->data;
971 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
973 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
976 cblk->numpassesinlayers = n;
985 void tcd_rateallocate_fixed(opj_tcd_t *tcd)
988 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
989 tcd_makelayer_fixed(tcd, layno, 1);
993 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final)
995 int compno, resno, bandno, precno, cblkno, passno;
997 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
999 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1001 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1002 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1003 for (resno = 0; resno < tilec->numresolutions; resno++) {
1004 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1005 for (bandno = 0; bandno < res->numbands; bandno++) {
1006 opj_tcd_band_t *band = &res->bands[bandno];
1007 for (precno = 0; precno < res->pw * res->ph; precno++) {
1008 opj_tcd_precinct_t *prc = &band->precincts[precno];
1009 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1010 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1011 opj_tcd_layer_t *layer = &cblk->layers[layno];
1015 cblk->numpassesinlayers = 0;
1017 n = cblk->numpassesinlayers;
1018 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1021 opj_tcd_pass_t *pass = &cblk->passes[passno];
1024 dd = pass->distortiondec;
1026 dr = pass->rate - cblk->passes[n - 1].rate;
1027 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1035 if (dd / dr >= thresh) {
1039 layer->numpasses = n - cblk->numpassesinlayers;
1041 if (!layer->numpasses) {
1045 if (cblk->numpassesinlayers == 0) {
1046 layer->len = cblk->passes[n - 1].rate;
1047 layer->data = cblk->data;
1048 layer->disto = cblk->passes[n - 1].distortiondec;
1050 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
1052 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1053 layer->disto = cblk->passes[n - 1].distortiondec -
1054 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1057 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1060 cblk->numpassesinlayers = n;
1069 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len,
1070 opj_codestream_info_t *cstr_info)
1072 int compno, resno, bandno, precno, cblkno, passno, layno;
1074 double cumdisto[100]; /* fixed_quality */
1075 const double K = 1; /* 1.1; fixed_quality */
1078 opj_cp_t *cp = tcd->cp;
1079 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1080 opj_tcp_t *tcd_tcp = tcd->tcp;
1085 tcd_tile->numpix = 0; /* fixed_quality */
1087 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1088 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1091 for (resno = 0; resno < tilec->numresolutions; resno++) {
1092 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1094 for (bandno = 0; bandno < res->numbands; bandno++) {
1095 opj_tcd_band_t *band = &res->bands[bandno];
1097 for (precno = 0; precno < res->pw * res->ph; precno++) {
1098 opj_tcd_precinct_t *prc = &band->precincts[precno];
1100 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1101 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1103 for (passno = 0; passno < cblk->totalpasses; passno++) {
1104 opj_tcd_pass_t *pass = &cblk->passes[passno];
1109 dd = pass->distortiondec;
1111 dr = pass->rate - cblk->passes[passno - 1].rate;
1112 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1118 if (rdslope < min) {
1121 if (rdslope > max) {
1127 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1128 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1134 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1135 * ((double)(1 << tcd->image->comps[compno].prec) - 1.0))
1136 * ((double)(tilec->numpix));
1141 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1142 tile_info->numpix = tcd_tile->numpix;
1143 tile_info->distotile = tcd_tile->distotile;
1144 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1147 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1151 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(
1152 tcd_tcp->rates[layno])), len) : len;
1153 double goodthresh = 0;
1154 double stable_thresh = 0;
1156 double distotarget; /* fixed_quality */
1159 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10,
1160 tcd_tcp->distoratio[layno] / 10));
1162 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1163 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1164 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1165 ==> possible to have some lossy layers and the last layer for sure lossless */
1166 if (((cp->disto_alloc == 1) && (tcd_tcp->rates[layno] > 0)) ||
1167 ((cp->fixed_quality == 1) && (tcd_tcp->distoratio[layno] > 0))) {
1168 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1171 for (i = 0; i < 128; i++) {
1173 double distoachieved = 0; /* fixed_quality */
1174 thresh = (lo + hi) / 2;
1176 tcd_makelayer(tcd, layno, thresh, 0);
1178 if (cp->fixed_quality) { /* fixed_quality */
1180 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen,
1181 cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino, THRESH_CALC,
1182 tcd->cur_totnum_tp);
1187 distoachieved = layno == 0 ?
1188 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1189 if (distoachieved < distotarget) {
1191 stable_thresh = thresh;
1198 distoachieved = (layno == 0) ?
1199 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1200 if (distoachieved < distotarget) {
1202 stable_thresh = thresh;
1208 l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen,
1209 cstr_info, tcd->cur_tp_num, tcd->tp_pos, tcd->cur_pino, THRESH_CALC,
1210 tcd->cur_totnum_tp);
1211 /* TODO: what to do with l ??? seek / tell ??? */
1212 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1218 stable_thresh = thresh;
1222 goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
1233 if (cstr_info) { /* Threshold for Marcela Index */
1234 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1236 tcd_makelayer(tcd, layno, goodthresh, 1);
1239 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
1240 (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1246 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len,
1247 opj_codestream_info_t *cstr_info)
1250 int l, i, numpacks = 0;
1251 opj_tcd_tile_t *tile = NULL;
1252 opj_tcp_t *tcd_tcp = NULL;
1253 opj_cp_t *cp = NULL;
1255 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1256 opj_tccp_t *tccp = &tcp->tccps[0];
1257 opj_image_t *image = tcd->image;
1259 opj_t1_t *t1 = NULL; /* T1 component */
1260 opj_t2_t *t2 = NULL; /* T2 component */
1262 tcd->tcd_tileno = tileno;
1263 tcd->tcd_tile = tcd->tcd_image->tiles;
1264 tcd->tcp = &tcd->cp->tcps[tileno];
1266 tile = tcd->tcd_tile;
1270 if (tcd->cur_tp_num == 0) {
1271 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1272 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1274 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1275 for (i = 0; i < tilec_idx->numresolutions; i++) {
1276 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1278 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1279 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1281 numpacks += res_idx->pw * res_idx->ph;
1283 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1284 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1286 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(
1287 cstr_info->numcomps * cstr_info->numlayers * numpacks,
1288 sizeof(opj_packet_info_t));
1292 /*---------------TILE-------------------*/
1294 for (compno = 0; compno < tile->numcomps; compno++) {
1297 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec -
1299 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1300 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1302 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1303 int tw = tilec->x1 - tilec->x0;
1304 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1306 /* extract tile data */
1308 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1309 for (y = tilec->y0; y < tilec->y1; y++) {
1310 /* start of the src tile scanline */
1311 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) +
1312 (y - offset_y) * w];
1313 /* start of the dst tile scanline */
1314 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1315 for (x = tilec->x0; x < tilec->x1; x++) {
1316 *tile_data++ = *data++ - adjust;
1319 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1320 for (y = tilec->y0; y < tilec->y1; y++) {
1321 /* start of the src tile scanline */
1322 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) +
1323 (y - offset_y) * w];
1324 /* start of the dst tile scanline */
1325 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1326 for (x = tilec->x0; x < tilec->x1; x++) {
1327 *tile_data++ = (*data++ - adjust) << 11;
1334 /*----------------MCT-------------------*/
1336 int samples = (tile->comps[0].x1 - tile->comps[0].x0) *
1337 (tile->comps[0].y1 - tile->comps[0].y0);
1338 if (tcd_tcp->tccps[0].qmfbid == 0) {
1339 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1342 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
1347 /*----------------DWT---------------------*/
1349 for (compno = 0; compno < tile->numcomps; compno++) {
1350 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1351 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1353 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1354 dwt_encode_real(tilec);
1358 /*------------------TIER1-----------------*/
1359 t1 = t1_create(tcd->cinfo);
1360 t1_encode_cblks(t1, tile, tcd_tcp);
1363 /*-----------RATE-ALLOCATE------------------*/
1367 cstr_info->index_write = 0;
1369 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1370 /* Normal Rate/distortion allocation */
1371 tcd_rateallocate(tcd, dest, len, cstr_info);
1373 /* Fixed layer allocation */
1374 tcd_rateallocate_fixed(tcd);
1377 /*--------------TIER2------------------*/
1381 cstr_info->index_write = 1;
1384 t2 = t2_create(tcd->cinfo, image, cp);
1385 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len,
1386 cstr_info, tcd->tp_num, tcd->tp_pos, tcd->cur_pino, FINAL_PASS,
1387 tcd->cur_totnum_tp);
1390 /*---------------CLEAN-------------------*/
1393 if (tcd->cur_tp_num == tcd->cur_totnum_tp - 1) {
1394 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1395 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n",
1396 tcd->encoding_time);
1398 /* cleaning memory */
1399 for (compno = 0; compno < tile->numcomps; compno++) {
1400 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1401 opj_aligned_free(tilec->data);
1408 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len,
1409 int tileno, opj_codestream_info_t *cstr_info)
1414 double tile_time, t1_time, dwt_time;
1415 opj_tcd_tile_t *tile = NULL;
1417 opj_t1_t *t1 = NULL; /* T1 component */
1418 opj_t2_t *t2 = NULL; /* T2 component */
1420 tcd->tcd_tileno = tileno;
1421 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1422 tcd->tcp = &(tcd->cp->tcps[tileno]);
1423 tile = tcd->tcd_tile;
1425 tile_time = opj_clock(); /* time needed to decode a tile */
1426 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1,
1427 tcd->cp->tw * tcd->cp->th);
1431 int resno, compno, numprec = 0;
1432 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1433 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1434 opj_tccp_t *tccp = &tcp->tccps[compno];
1435 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1436 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1437 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1438 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1439 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1440 numprec += res_idx->pw * res_idx->ph;
1441 if (tccp->csty & J2K_CP_CSTY_PRT) {
1442 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1443 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1445 cstr_info->tile[tileno].pdx[resno] = 15;
1446 cstr_info->tile[tileno].pdy[resno] = 15;
1450 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(
1451 cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1452 cstr_info->packno = 0;
1456 /*--------------TIER2------------------*/
1458 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1459 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1464 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bitstream\n");
1467 /*------------------TIER1-----------------*/
1469 t1_time = opj_clock(); /* time needed to decode a tile */
1470 t1 = t1_create(tcd->cinfo);
1472 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1477 for (compno = 0; compno < tile->numcomps; ++compno) {
1478 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1479 /* The +3 is headroom required by the vectorized DWT */
1480 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) *
1481 (tilec->y1 - tilec->y0)) + 3) * sizeof(int));
1482 if (tilec->data == NULL) {
1483 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1487 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1490 t1_time = opj_clock() - t1_time;
1491 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1493 /*----------------DWT---------------------*/
1495 dwt_time = opj_clock(); /* time needed to decode a tile */
1496 for (compno = 0; compno < tile->numcomps; compno++) {
1497 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1500 if (tcd->cp->reduce != 0) {
1501 if (tile->comps[compno].numresolutions < (tcd->cp->reduce - 1)) {
1502 opj_event_msg(tcd->cinfo, EVT_ERROR,
1503 "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1504 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n",
1505 tcd->cp->reduce, tile->comps[compno].numresolutions);
1508 tcd->image->comps[compno].resno_decoded =
1509 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1513 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1514 if (numres2decode > 0) {
1515 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1516 dwt_decode(tilec, numres2decode);
1518 dwt_decode_real(tilec, numres2decode);
1522 dwt_time = opj_clock() - dwt_time;
1523 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1525 /*----------------MCT-------------------*/
1527 if (tcd->tcp->mct) {
1528 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 -
1531 if (tile->numcomps >= 3) {
1532 if (tcd->tcp->tccps[0].qmfbid == 1) {
1534 tile->comps[0].data,
1535 tile->comps[1].data,
1536 tile->comps[2].data,
1540 (float*)tile->comps[0].data,
1541 (float*)tile->comps[1].data,
1542 (float*)tile->comps[2].data,
1546 opj_event_msg(tcd->cinfo, EVT_WARNING,
1547 "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
1552 /*---------------TILE-------------------*/
1554 for (compno = 0; compno < tile->numcomps; ++compno) {
1555 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1556 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1557 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1558 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1559 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1560 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 :
1561 (1 << imagec->prec) - 1;
1563 int tw = tilec->x1 - tilec->x0;
1566 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1567 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1570 if (!imagec->data) {
1571 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1573 if (!imagec->data) {
1574 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1577 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1578 for (j = res->y0; j < res->y1; ++j) {
1579 for (i = res->x0; i < res->x1; ++i) {
1580 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1582 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1586 for (j = res->y0; j < res->y1; ++j) {
1587 for (i = res->x0; i < res->x1; ++i) {
1588 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1589 int v = lrintf(tmp);
1591 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1595 opj_aligned_free(tilec->data);
1598 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1599 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1608 void tcd_free_decode(opj_tcd_t *tcd)
1610 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1612 for (i = 0; i < tcd_image->tw * tcd_image->th; i++) {
1613 tcd_free_decode_tile(tcd, i);
1616 opj_free(tcd_image->tiles);
1619 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno)
1621 int compno, resno, bandno, precno, cblkno;
1623 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1625 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1626 if (tile->comps != NULL) {
1627 for (compno = 0; compno < tile->numcomps; compno++) {
1628 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1629 for (resno = 0; resno < tilec->numresolutions; resno++) {
1630 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1631 for (bandno = 0; bandno < res->numbands; bandno++) {
1632 opj_tcd_band_t *band = &res->bands[bandno];
1633 for (precno = 0; precno < res->ph * res->pw; precno++) {
1634 opj_tcd_precinct_t *prec = &band->precincts[precno];
1635 if (prec->cblks.dec != NULL) {
1636 for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
1637 opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
1638 opj_free(cblk->data);
1639 opj_free(cblk->segs);
1641 opj_free(prec->cblks.dec);
1643 if (prec->imsbtree != NULL) {
1644 tgt_destroy(prec->imsbtree);
1646 if (prec->incltree != NULL) {
1647 tgt_destroy(prec->incltree);
1652 opj_free(band->precincts);
1655 opj_free(tilec->resolutions);
1657 opj_free(tile->comps);