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) {
43 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
45 fprintf(fd, "image {\n");
46 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
47 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
49 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
50 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
51 fprintf(fd, " tile {\n");
52 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
53 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
54 for (compno = 0; compno < tile->numcomps; compno++) {
55 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
56 fprintf(fd, " tilec {\n");
58 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
59 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
60 for (resno = 0; resno < tilec->numresolutions; resno++) {
61 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
62 fprintf(fd, "\n res {\n");
64 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
65 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
66 for (bandno = 0; bandno < res->numbands; bandno++) {
67 opj_tcd_band_t *band = &res->bands[bandno];
68 fprintf(fd, " band {\n");
70 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
71 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
72 for (precno = 0; precno < res->pw * res->ph; precno++) {
73 opj_tcd_precinct_t *prec = &band->precincts[precno];
74 fprintf(fd, " prec {\n");
76 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
77 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
79 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
80 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
81 fprintf(fd, " cblk {\n");
83 " x0=%d, y0=%d, x1=%d, y1=%d\n",
84 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
101 /* ----------------------------------------------------------------------- */
104 Create a new TCD handle
106 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
107 /* create the tcd structure */
108 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
109 if(!tcd) return NULL;
111 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
112 if(!tcd->tcd_image) {
121 Destroy a previously created TCD handle
123 void tcd_destroy(opj_tcd_t *tcd) {
125 opj_free(tcd->tcd_image);
130 /* ----------------------------------------------------------------------- */
132 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
133 int tileno, compno, resno, bandno, precno, cblkno;
137 tcd->tcd_image->tw = cp->tw;
138 tcd->tcd_image->th = cp->th;
139 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
141 for (tileno = 0; tileno < 1; tileno++) {
142 opj_tcp_t *tcp = &cp->tcps[curtileno];
145 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
146 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
147 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
149 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
150 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
152 /* 4 borders of the tile rescale on the image if necessary */
153 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
154 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
155 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
156 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
157 tile->numcomps = image->numcomps;
158 /* tile->PPT=image->PPT; */
160 /* Modification of the RATE >> */
161 for (j = 0; j < tcp->numlayers; j++) {
162 tcp->rates[j] = tcp->rates[j] ?
164 (((float) (tile->numcomps
165 * (tile->x1 - tile->x0)
166 * (tile->y1 - tile->y0)
167 * image->comps[0].prec))
168 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
170 ((float) (tile->numcomps
171 * (tile->x1 - tile->x0)
172 * (tile->y1 - tile->y0)
173 * image->comps[0].prec))/
174 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
178 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
179 tcp->rates[j] = tcp->rates[j - 1] + 20;
181 if (!j && tcp->rates[j] < 30)
185 if(j == (tcp->numlayers-1)){
186 tcp->rates[j] = tcp->rates[j]- 2;
190 /* << Modification of the RATE */
192 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
193 for (compno = 0; compno < tile->numcomps; compno++) {
194 opj_tccp_t *tccp = &tcp->tccps[compno];
196 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
198 /* border of each tile component (global) */
199 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
200 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
201 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
202 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
204 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
205 tilec->numresolutions = tccp->numresolutions;
207 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
209 for (resno = 0; resno < tilec->numresolutions; resno++) {
211 int levelno = tilec->numresolutions - 1 - resno;
212 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
213 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
214 int cbgwidthexpn, cbgheightexpn;
215 int cblkwidthexpn, cblkheightexpn;
217 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
219 /* border for each resolution level (global) */
220 res->x0 = int_ceildivpow2(tilec->x0, levelno);
221 res->y0 = int_ceildivpow2(tilec->y0, levelno);
222 res->x1 = int_ceildivpow2(tilec->x1, levelno);
223 res->y1 = int_ceildivpow2(tilec->y1, levelno);
225 res->numbands = resno == 0 ? 1 : 3;
226 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
227 if (tccp->csty & J2K_CCP_CSTY_PRT) {
228 pdx = tccp->prcw[resno];
229 pdy = tccp->prch[resno];
234 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
235 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
236 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
238 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
239 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
241 res->pw = (brprcxend - tlprcxstart) >> pdx;
242 res->ph = (brprcyend - tlprcystart) >> pdy;
245 tlcbgxstart = tlprcxstart;
246 tlcbgystart = tlprcystart;
247 brcbgxend = brprcxend;
248 brcbgyend = brprcyend;
252 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
253 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
254 brcbgxend = int_ceildivpow2(brprcxend, 1);
255 brcbgyend = int_ceildivpow2(brprcyend, 1);
256 cbgwidthexpn = pdx - 1;
257 cbgheightexpn = pdy - 1;
260 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
261 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
263 for (bandno = 0; bandno < res->numbands; bandno++) {
266 opj_stepsize_t *ss = NULL;
268 opj_tcd_band_t *band = &res->bands[bandno];
270 band->bandno = resno == 0 ? 0 : bandno + 1;
271 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
272 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
274 if (band->bandno == 0) {
275 /* band border (global) */
276 band->x0 = int_ceildivpow2(tilec->x0, levelno);
277 band->y0 = int_ceildivpow2(tilec->y0, levelno);
278 band->x1 = int_ceildivpow2(tilec->x1, levelno);
279 band->y1 = int_ceildivpow2(tilec->y1, levelno);
281 /* band border (global) */
282 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
283 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
284 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
285 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
288 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
289 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
290 numbps = image->comps[compno].prec + gain;
292 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
293 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
295 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
297 for (i = 0; i < res->pw * res->ph * 3; i++) {
298 band->precincts[i].imsbtree = NULL;
299 band->precincts[i].incltree = NULL;
300 band->precincts[i].cblks.enc = NULL;
303 for (precno = 0; precno < res->pw * res->ph; precno++) {
304 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
306 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
307 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
308 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
309 int cbgyend = cbgystart + (1 << cbgheightexpn);
311 opj_tcd_precinct_t *prc = &band->precincts[precno];
313 /* precinct size (global) */
314 prc->x0 = int_max(cbgxstart, band->x0);
315 prc->y0 = int_max(cbgystart, band->y0);
316 prc->x1 = int_min(cbgxend, band->x1);
317 prc->y1 = int_min(cbgyend, band->y1);
319 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
320 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
321 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
322 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
323 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
324 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
326 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
327 prc->incltree = tgt_create(prc->cw, prc->ch);
328 prc->imsbtree = tgt_create(prc->cw, prc->ch);
330 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
331 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
332 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
333 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
334 int cblkyend = cblkystart + (1 << cblkheightexpn);
336 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
338 /* code-block size (global) */
339 cblk->x0 = int_max(cblkxstart, prc->x0);
340 cblk->y0 = int_max(cblkystart, prc->y0);
341 cblk->x1 = int_min(cblkxend, prc->x1);
342 cblk->y1 = int_min(cblkyend, prc->y1);
343 cblk->data = (unsigned char*) opj_calloc(9728+2, sizeof(unsigned char));
344 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
348 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
349 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
357 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
360 void tcd_free_encode(opj_tcd_t *tcd) {
361 int tileno, compno, resno, bandno, precno, cblkno;
363 for (tileno = 0; tileno < 1; tileno++) {
364 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
366 for (compno = 0; compno < tile->numcomps; compno++) {
367 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
369 for (resno = 0; resno < tilec->numresolutions; resno++) {
370 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
372 for (bandno = 0; bandno < res->numbands; bandno++) {
373 opj_tcd_band_t *band = &res->bands[bandno];
375 for (precno = 0; precno < res->pw * res->ph; precno++) {
376 opj_tcd_precinct_t *prc = &band->precincts[precno];
378 if (prc->incltree != NULL) {
379 tgt_destroy(prc->incltree);
380 prc->incltree = NULL;
382 if (prc->imsbtree != NULL) {
383 tgt_destroy(prc->imsbtree);
384 prc->imsbtree = NULL;
386 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
387 opj_free(prc->cblks.enc[cblkno].data - 2);
388 opj_free(prc->cblks.enc[cblkno].layers);
389 opj_free(prc->cblks.enc[cblkno].passes);
391 opj_free(prc->cblks.enc);
393 opj_free(band->precincts);
394 band->precincts = NULL;
397 opj_free(tilec->resolutions);
398 tilec->resolutions = NULL;
400 opj_free(tile->comps);
403 opj_free(tcd->tcd_image->tiles);
404 tcd->tcd_image->tiles = NULL;
407 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
408 int tileno, compno, resno, bandno, precno, cblkno;
410 for (tileno = 0; tileno < 1; tileno++) {
411 opj_tcp_t *tcp = &cp->tcps[curtileno];
413 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
414 int p = curtileno % cp->tw;
415 int q = curtileno / cp->tw;
417 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
419 /* 4 borders of the tile rescale on the image if necessary */
420 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
421 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
422 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
423 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
425 tile->numcomps = image->numcomps;
426 /* tile->PPT=image->PPT; */
428 /* Modification of the RATE >> */
429 for (j = 0; j < tcp->numlayers; j++) {
430 tcp->rates[j] = tcp->rates[j] ?
432 (((float) (tile->numcomps
433 * (tile->x1 - tile->x0)
434 * (tile->y1 - tile->y0)
435 * image->comps[0].prec))
436 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
438 ((float) (tile->numcomps
439 * (tile->x1 - tile->x0)
440 * (tile->y1 - tile->y0)
441 * image->comps[0].prec))/
442 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
446 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
447 tcp->rates[j] = tcp->rates[j - 1] + 20;
449 if (!j && tcp->rates[j] < 30)
454 /* << Modification of the RATE */
456 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
457 for (compno = 0; compno < tile->numcomps; compno++) {
458 opj_tccp_t *tccp = &tcp->tccps[compno];
460 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
462 /* border of each tile component (global) */
463 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
464 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
465 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
466 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
468 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
469 tilec->numresolutions = tccp->numresolutions;
470 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
471 for (resno = 0; resno < tilec->numresolutions; resno++) {
474 int levelno = tilec->numresolutions - 1 - resno;
475 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
476 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
477 int cbgwidthexpn, cbgheightexpn;
478 int cblkwidthexpn, cblkheightexpn;
480 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
482 /* border for each resolution level (global) */
483 res->x0 = int_ceildivpow2(tilec->x0, levelno);
484 res->y0 = int_ceildivpow2(tilec->y0, levelno);
485 res->x1 = int_ceildivpow2(tilec->x1, levelno);
486 res->y1 = int_ceildivpow2(tilec->y1, levelno);
487 res->numbands = resno == 0 ? 1 : 3;
489 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
490 if (tccp->csty & J2K_CCP_CSTY_PRT) {
491 pdx = tccp->prcw[resno];
492 pdy = tccp->prch[resno];
497 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
498 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
499 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
500 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
501 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
503 res->pw = (brprcxend - tlprcxstart) >> pdx;
504 res->ph = (brprcyend - tlprcystart) >> pdy;
507 tlcbgxstart = tlprcxstart;
508 tlcbgystart = tlprcystart;
509 brcbgxend = brprcxend;
510 brcbgyend = brprcyend;
514 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
515 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
516 brcbgxend = int_ceildivpow2(brprcxend, 1);
517 brcbgyend = int_ceildivpow2(brprcyend, 1);
518 cbgwidthexpn = pdx - 1;
519 cbgheightexpn = pdy - 1;
522 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
523 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
525 for (bandno = 0; bandno < res->numbands; bandno++) {
528 opj_stepsize_t *ss = NULL;
530 opj_tcd_band_t *band = &res->bands[bandno];
532 band->bandno = resno == 0 ? 0 : bandno + 1;
533 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
534 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
536 if (band->bandno == 0) {
538 band->x0 = int_ceildivpow2(tilec->x0, levelno);
539 band->y0 = int_ceildivpow2(tilec->y0, levelno);
540 band->x1 = int_ceildivpow2(tilec->x1, levelno);
541 band->y1 = int_ceildivpow2(tilec->y1, levelno);
543 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
544 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
545 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
546 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
549 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
550 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
551 numbps = image->comps[compno].prec + gain;
552 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
553 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
555 for (precno = 0; precno < res->pw * res->ph; precno++) {
556 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
558 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
559 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
560 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
561 int cbgyend = cbgystart + (1 << cbgheightexpn);
563 opj_tcd_precinct_t *prc = &band->precincts[precno];
565 /* precinct size (global) */
566 prc->x0 = int_max(cbgxstart, band->x0);
567 prc->y0 = int_max(cbgystart, band->y0);
568 prc->x1 = int_min(cbgxend, band->x1);
569 prc->y1 = int_min(cbgyend, band->y1);
571 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
572 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
573 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
574 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
575 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
576 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
578 opj_free(prc->cblks.enc);
579 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
581 if (prc->incltree != NULL) {
582 tgt_destroy(prc->incltree);
584 if (prc->imsbtree != NULL) {
585 tgt_destroy(prc->imsbtree);
588 prc->incltree = tgt_create(prc->cw, prc->ch);
589 prc->imsbtree = tgt_create(prc->cw, prc->ch);
591 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
592 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
593 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
594 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
595 int cblkyend = cblkystart + (1 << cblkheightexpn);
597 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
599 /* code-block size (global) */
600 cblk->x0 = int_max(cblkxstart, prc->x0);
601 cblk->y0 = int_max(cblkystart, prc->y0);
602 cblk->x1 = int_min(cblkxend, prc->x1);
603 cblk->y1 = int_min(cblkyend, prc->y1);
604 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
605 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
609 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
610 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
618 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
621 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
622 int i, j, tileno, p, q;
623 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
626 tcd->tcd_image->tw = cp->tw;
627 tcd->tcd_image->th = cp->th;
628 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(cp->tw * cp->th, sizeof(opj_tcd_tile_t));
631 Allocate place to store the decoded data = final image
632 Place limited by the tile really present in the codestream
635 for (j = 0; j < cp->tileno_size; j++) {
636 opj_tcd_tile_t *tile;
638 tileno = cp->tileno[j];
639 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
640 tile->numcomps = image->numcomps;
641 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
644 for (i = 0; i < image->numcomps; i++) {
645 for (j = 0; j < cp->tileno_size; j++) {
646 opj_tcd_tile_t *tile;
647 opj_tcd_tilecomp_t *tilec;
649 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
651 tileno = cp->tileno[j];
653 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
654 tilec = &tile->comps[i];
656 p = tileno % cp->tw; /* si numerotation matricielle .. */
657 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
659 /* 4 borders of the tile rescale on the image if necessary */
660 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
661 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
662 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
663 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
665 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
666 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
667 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
668 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
670 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
671 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
672 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
673 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
676 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
677 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
679 image->comps[i].w = w;
680 image->comps[i].h = h;
681 image->comps[i].x0 = x0;
682 image->comps[i].y0 = y0;
686 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) {
687 int compno, resno, bandno, precno, cblkno;
689 opj_tcd_tile_t *tile;
691 OPJ_ARG_NOT_USED(cstr_info);
695 tcp = &(cp->tcps[cp->tileno[tileno]]);
696 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
698 tileno = cp->tileno[tileno];
700 for (compno = 0; compno < tile->numcomps; compno++) {
701 opj_tccp_t *tccp = &tcp->tccps[compno];
702 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
704 if (tccp->numresolutions <= 0)
706 cp->tileno[tileno] = -1;
710 /* border of each tile component (global) */
711 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
712 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
713 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
714 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
716 tilec->numresolutions = tccp->numresolutions;
717 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
719 for (resno = 0; resno < tilec->numresolutions; resno++) {
721 int levelno = tilec->numresolutions - 1 - resno;
722 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
723 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
724 int cbgwidthexpn, cbgheightexpn;
725 int cblkwidthexpn, cblkheightexpn;
727 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
729 /* border for each resolution level (global) */
730 res->x0 = int_ceildivpow2(tilec->x0, levelno);
731 res->y0 = int_ceildivpow2(tilec->y0, levelno);
732 res->x1 = int_ceildivpow2(tilec->x1, levelno);
733 res->y1 = int_ceildivpow2(tilec->y1, levelno);
734 res->numbands = resno == 0 ? 1 : 3;
736 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
737 if (tccp->csty & J2K_CCP_CSTY_PRT) {
738 pdx = tccp->prcw[resno];
739 pdy = tccp->prch[resno];
745 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
746 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
747 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
748 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
749 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
751 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
752 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
755 tlcbgxstart = tlprcxstart;
756 tlcbgystart = tlprcystart;
757 brcbgxend = brprcxend;
758 brcbgyend = brprcyend;
762 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
763 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
764 brcbgxend = int_ceildivpow2(brprcxend, 1);
765 brcbgyend = int_ceildivpow2(brprcyend, 1);
766 cbgwidthexpn = pdx - 1;
767 cbgheightexpn = pdy - 1;
770 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
771 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
773 for (bandno = 0; bandno < res->numbands; bandno++) {
776 opj_stepsize_t *ss = NULL;
778 opj_tcd_band_t *band = &res->bands[bandno];
779 band->bandno = resno == 0 ? 0 : bandno + 1;
780 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
781 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
783 if (band->bandno == 0) {
784 /* band border (global) */
785 band->x0 = int_ceildivpow2(tilec->x0, levelno);
786 band->y0 = int_ceildivpow2(tilec->y0, levelno);
787 band->x1 = int_ceildivpow2(tilec->x1, levelno);
788 band->y1 = int_ceildivpow2(tilec->y1, levelno);
790 /* band border (global) */
791 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
792 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
793 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
794 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
797 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
798 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
799 numbps = image->comps[compno].prec + gain;
800 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
801 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
803 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
805 for (precno = 0; precno < res->pw * res->ph; precno++) {
806 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
807 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
808 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
809 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
810 int cbgyend = cbgystart + (1 << cbgheightexpn);
812 opj_tcd_precinct_t *prc = &band->precincts[precno];
813 /* precinct size (global) */
814 prc->x0 = int_max(cbgxstart, band->x0);
815 prc->y0 = int_max(cbgystart, band->y0);
816 prc->x1 = int_min(cbgxend, band->x1);
817 prc->y1 = int_min(cbgyend, band->y1);
819 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
820 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
821 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
822 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
823 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
824 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
826 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
828 prc->incltree = tgt_create(prc->cw, prc->ch);
829 prc->imsbtree = tgt_create(prc->cw, prc->ch);
831 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
832 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
833 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
834 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
835 int cblkyend = cblkystart + (1 << cblkheightexpn);
837 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
840 /* code-block size (global) */
841 cblk->x0 = int_max(cblkxstart, prc->x0);
842 cblk->y0 = int_max(cblkystart, prc->y0);
843 cblk->x1 = int_min(cblkxend, prc->x1);
844 cblk->y1 = int_min(cblkyend, prc->y1);
851 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
854 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
855 int compno, resno, bandno, precno, cblkno;
856 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
857 int matrice[10][10][3];
860 opj_cp_t *cp = tcd->cp;
861 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
862 opj_tcp_t *tcd_tcp = tcd->tcp;
864 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
866 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
867 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
868 for (i = 0; i < tcd_tcp->numlayers; i++) {
869 for (j = 0; j < tilec->numresolutions; j++) {
870 for (k = 0; k < 3; k++) {
872 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
873 * (float) (tcd->image->comps[compno].prec / 16.0));
878 for (resno = 0; resno < tilec->numresolutions; resno++) {
879 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
880 for (bandno = 0; bandno < res->numbands; bandno++) {
881 opj_tcd_band_t *band = &res->bands[bandno];
882 for (precno = 0; precno < res->pw * res->ph; precno++) {
883 opj_tcd_precinct_t *prc = &band->precincts[precno];
884 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
885 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
886 opj_tcd_layer_t *layer = &cblk->layers[layno];
888 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
889 /* Correction of the matrix of coefficient to include the IMSB information */
891 value = matrice[layno][resno][bandno];
898 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
899 if (imsb >= matrice[layno - 1][resno][bandno]) {
900 value -= (imsb - matrice[layno - 1][resno][bandno]);
908 cblk->numpassesinlayers = 0;
911 n = cblk->numpassesinlayers;
912 if (cblk->numpassesinlayers == 0) {
914 n = 3 * value - 2 + cblk->numpassesinlayers;
916 n = cblk->numpassesinlayers;
919 n = 3 * value + cblk->numpassesinlayers;
922 layer->numpasses = n - cblk->numpassesinlayers;
924 if (!layer->numpasses)
927 if (cblk->numpassesinlayers == 0) {
928 layer->len = cblk->passes[n - 1].rate;
929 layer->data = cblk->data;
931 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
932 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
935 cblk->numpassesinlayers = n;
943 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
945 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
946 tcd_makelayer_fixed(tcd, layno, 1);
950 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
951 int compno, resno, bandno, precno, cblkno, passno;
953 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
955 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
957 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
958 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
959 for (resno = 0; resno < tilec->numresolutions; resno++) {
960 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
961 for (bandno = 0; bandno < res->numbands; bandno++) {
962 opj_tcd_band_t *band = &res->bands[bandno];
963 for (precno = 0; precno < res->pw * res->ph; precno++) {
964 opj_tcd_precinct_t *prc = &band->precincts[precno];
965 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
966 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
967 opj_tcd_layer_t *layer = &cblk->layers[layno];
971 cblk->numpassesinlayers = 0;
973 n = cblk->numpassesinlayers;
974 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
977 opj_tcd_pass_t *pass = &cblk->passes[passno];
980 dd = pass->distortiondec;
982 dr = pass->rate - cblk->passes[n - 1].rate;
983 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
990 if (dd / dr >= thresh)
993 layer->numpasses = n - cblk->numpassesinlayers;
995 if (!layer->numpasses) {
999 if (cblk->numpassesinlayers == 0) {
1000 layer->len = cblk->passes[n - 1].rate;
1001 layer->data = cblk->data;
1002 layer->disto = cblk->passes[n - 1].distortiondec;
1004 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1005 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1006 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1009 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1012 cblk->numpassesinlayers = n;
1020 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1021 int compno, resno, bandno, precno, cblkno, passno, layno;
1023 double cumdisto[100]; /* fixed_quality */
1024 const double K = 1; /* 1.1; fixed_quality */
1027 opj_cp_t *cp = tcd->cp;
1028 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1029 opj_tcp_t *tcd_tcp = tcd->tcp;
1034 tcd_tile->numpix = 0; /* fixed_quality */
1036 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1037 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1040 for (resno = 0; resno < tilec->numresolutions; resno++) {
1041 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1043 for (bandno = 0; bandno < res->numbands; bandno++) {
1044 opj_tcd_band_t *band = &res->bands[bandno];
1046 for (precno = 0; precno < res->pw * res->ph; precno++) {
1047 opj_tcd_precinct_t *prc = &band->precincts[precno];
1049 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1050 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1052 for (passno = 0; passno < cblk->totalpasses; passno++) {
1053 opj_tcd_pass_t *pass = &cblk->passes[passno];
1058 dd = pass->distortiondec;
1060 dr = pass->rate - cblk->passes[passno - 1].rate;
1061 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1067 if (rdslope < min) {
1070 if (rdslope > max) {
1076 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1077 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1083 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1084 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1085 * ((double)(tilec->numpix));
1090 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1091 tile_info->numpix = tcd_tile->numpix;
1092 tile_info->distotile = tcd_tile->distotile;
1093 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1096 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1100 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1101 double goodthresh = 0;
1102 double stable_thresh = 0;
1104 double distotarget; /* fixed_quality */
1107 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1109 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1110 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1111 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1112 ==> possible to have some lossy layers and the last layer for sure lossless */
1113 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1114 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1117 for (i = 0; i < 128; i++) {
1119 double distoachieved = 0; /* fixed_quality */
1120 thresh = (lo + hi) / 2;
1122 tcd_makelayer(tcd, layno, thresh, 0);
1124 if (cp->fixed_quality) { /* fixed_quality */
1126 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);
1131 distoachieved = layno == 0 ?
1132 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1133 if (distoachieved < distotarget) {
1135 stable_thresh = thresh;
1142 distoachieved = (layno == 0) ?
1143 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1144 if (distoachieved < distotarget) {
1146 stable_thresh = thresh;
1152 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);
1153 /* TODO: what to do with l ??? seek / tell ??? */
1154 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1160 stable_thresh = thresh;
1164 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1175 if(cstr_info) { /* Threshold for Marcela Index */
1176 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1178 tcd_makelayer(tcd, layno, goodthresh, 1);
1181 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1187 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1189 int l, i, numpacks = 0;
1190 opj_tcd_tile_t *tile = NULL;
1191 opj_tcp_t *tcd_tcp = NULL;
1192 opj_cp_t *cp = NULL;
1194 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1195 opj_tccp_t *tccp = &tcp->tccps[0];
1196 opj_image_t *image = tcd->image;
1198 opj_t1_t *t1 = NULL; /* T1 component */
1199 opj_t2_t *t2 = NULL; /* T2 component */
1201 tcd->tcd_tileno = tileno;
1202 tcd->tcd_tile = tcd->tcd_image->tiles;
1203 tcd->tcp = &tcd->cp->tcps[tileno];
1205 tile = tcd->tcd_tile;
1209 if(tcd->cur_tp_num == 0){
1210 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1211 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1213 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1214 for (i = 0; i < tilec_idx->numresolutions; i++) {
1215 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1217 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1218 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1220 numpacks += res_idx->pw * res_idx->ph;
1222 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1223 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1225 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1229 /*---------------TILE-------------------*/
1231 for (compno = 0; compno < tile->numcomps; compno++) {
1234 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1235 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1236 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1238 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1239 int tw = tilec->x1 - tilec->x0;
1240 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1242 /* extract tile data */
1244 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1245 for (y = tilec->y0; y < tilec->y1; y++) {
1246 /* start of the src tile scanline */
1247 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1248 /* start of the dst tile scanline */
1249 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1250 for (x = tilec->x0; x < tilec->x1; x++) {
1251 *tile_data++ = *data++ - adjust;
1254 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1255 for (y = tilec->y0; y < tilec->y1; y++) {
1256 /* start of the src tile scanline */
1257 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1258 /* start of the dst tile scanline */
1259 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1260 for (x = tilec->x0; x < tilec->x1; x++) {
1261 *tile_data++ = (*data++ - adjust) << 11;
1268 /*----------------MCT-------------------*/
1270 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1271 if (tcd_tcp->tccps[0].qmfbid == 0) {
1272 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1274 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1278 /*----------------DWT---------------------*/
1280 for (compno = 0; compno < tile->numcomps; compno++) {
1281 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1282 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1284 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1285 dwt_encode_real(tilec);
1289 /*------------------TIER1-----------------*/
1290 t1 = t1_create(tcd->cinfo);
1291 t1_encode_cblks(t1, tile, tcd_tcp);
1294 /*-----------RATE-ALLOCATE------------------*/
1298 cstr_info->index_write = 0;
1300 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1301 /* Normal Rate/distortion allocation */
1302 tcd_rateallocate(tcd, dest, len, cstr_info);
1304 /* Fixed layer allocation */
1305 tcd_rateallocate_fixed(tcd);
1308 /*--------------TIER2------------------*/
1312 cstr_info->index_write = 1;
1315 t2 = t2_create(tcd->cinfo, image, cp);
1316 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);
1319 /*---------------CLEAN-------------------*/
1322 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1323 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1324 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1326 /* cleaning memory */
1327 for (compno = 0; compno < tile->numcomps; compno++) {
1328 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1329 opj_aligned_free(tilec->data);
1336 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1340 double tile_time, t1_time, dwt_time;
1341 opj_tcd_tile_t *tile = NULL;
1343 opj_t1_t *t1 = NULL; /* T1 component */
1344 opj_t2_t *t2 = NULL; /* T2 component */
1346 tcd->tcd_tileno = tileno;
1347 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1348 tcd->tcp = &(tcd->cp->tcps[tileno]);
1349 tile = tcd->tcd_tile;
1351 tile_time = opj_clock(); /* time needed to decode a tile */
1352 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1356 int resno, compno, numprec = 0;
1357 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1358 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1359 opj_tccp_t *tccp = &tcp->tccps[compno];
1360 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1361 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1362 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1363 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1364 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1365 numprec += res_idx->pw * res_idx->ph;
1366 if (tccp->csty & J2K_CP_CSTY_PRT) {
1367 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1368 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1371 cstr_info->tile[tileno].pdx[resno] = 15;
1372 cstr_info->tile[tileno].pdy[resno] = 15;
1376 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1377 cstr_info->packno = 0;
1381 /*--------------TIER2------------------*/
1383 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1384 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1389 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1392 /*------------------TIER1-----------------*/
1394 t1_time = opj_clock(); /* time needed to decode a tile */
1395 t1 = t1_create(tcd->cinfo);
1398 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1403 for (compno = 0; compno < tile->numcomps; ++compno) {
1404 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1405 /* The +3 is headroom required by the vectorized DWT */
1406 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1407 if (tilec->data == NULL)
1409 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1413 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1416 t1_time = opj_clock() - t1_time;
1417 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1419 /*----------------DWT---------------------*/
1421 dwt_time = opj_clock(); /* time needed to decode a tile */
1422 for (compno = 0; compno < tile->numcomps; compno++) {
1423 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1426 if (tcd->cp->reduce != 0) {
1427 if ( tile->comps[compno].numresolutions < ( tcd->cp->reduce - 1 ) ) {
1428 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1429 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1433 tcd->image->comps[compno].resno_decoded =
1434 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1438 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1439 if(numres2decode > 0){
1440 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1441 dwt_decode(tilec, numres2decode);
1443 dwt_decode_real(tilec, numres2decode);
1447 dwt_time = opj_clock() - dwt_time;
1448 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1450 /*----------------MCT-------------------*/
1452 if (tcd->tcp->mct) {
1453 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1455 if (tile->numcomps >= 3 ){
1456 if (tcd->tcp->tccps[0].qmfbid == 1) {
1458 tile->comps[0].data,
1459 tile->comps[1].data,
1460 tile->comps[2].data,
1464 (float*)tile->comps[0].data,
1465 (float*)tile->comps[1].data,
1466 (float*)tile->comps[2].data,
1470 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1474 /*---------------TILE-------------------*/
1476 for (compno = 0; compno < tile->numcomps; ++compno) {
1477 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1478 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1479 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1480 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1481 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1482 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1484 int tw = tilec->x1 - tilec->x0;
1487 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1488 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1492 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1496 opj_event_msg(tcd->cinfo, EVT_ERROR, "Out of memory\n");
1499 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1500 for(j = res->y0; j < res->y1; ++j) {
1501 for(i = res->x0; i < res->x1; ++i) {
1502 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1504 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1508 for(j = res->y0; j < res->y1; ++j) {
1509 for(i = res->x0; i < res->x1; ++i) {
1510 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1511 int v = lrintf(tmp);
1513 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1517 opj_aligned_free(tilec->data);
1520 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1521 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1530 void tcd_free_decode(opj_tcd_t *tcd) {
1531 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1533 for (i = 0; i < tcd_image->tw * tcd_image->th; i++)
1535 tcd_free_decode_tile(tcd, i);
1538 opj_free(tcd_image->tiles);
1541 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1542 int compno,resno,bandno,precno,cblkno;
1544 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1546 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1547 if (tile->comps != NULL) {
1548 for (compno = 0; compno < tile->numcomps; compno++) {
1549 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1550 for (resno = 0; resno < tilec->numresolutions; resno++) {
1551 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1552 for (bandno = 0; bandno < res->numbands; bandno++) {
1553 opj_tcd_band_t *band = &res->bands[bandno];
1554 for (precno = 0; precno < res->ph * res->pw; precno++) {
1555 opj_tcd_precinct_t *prec = &band->precincts[precno];
1556 if (prec->cblks.dec != NULL) {
1557 for (cblkno = 0; cblkno < prec->cw * prec->ch; ++cblkno) {
1558 opj_tcd_cblk_dec_t* cblk = &prec->cblks.dec[cblkno];
1559 opj_free(cblk->data);
1560 opj_free(cblk->segs);
1562 opj_free(prec->cblks.dec);
1564 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1565 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1569 opj_free(band->precincts);
1572 opj_free(tilec->resolutions);
1574 opj_free(tile->comps);