2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 /* ----------------------------------------------------------------------- */
37 static opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd );
39 static opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd );
41 static opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd );
43 static opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd );
45 static opj_bool tcd_t2_encode ( opj_tcd_v2_t *p_tcd,
46 OPJ_BYTE * p_dest_data,
47 OPJ_UINT32 * p_data_written,
48 OPJ_UINT32 p_max_dest_size,
49 opj_codestream_info_t *p_cstr_info );
51 static opj_bool tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
52 OPJ_BYTE * p_dest_data,
53 OPJ_UINT32 p_max_dest_size,
54 opj_codestream_info_t *p_cstr_info );
56 /* ----------------------------------------------------------------------- */
59 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
60 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
62 fprintf(fd, "image {\n");
63 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
64 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
66 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
67 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
68 fprintf(fd, " tile {\n");
69 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
70 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
71 for (compno = 0; compno < tile->numcomps; compno++) {
72 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
73 fprintf(fd, " tilec {\n");
75 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
76 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
77 for (resno = 0; resno < tilec->numresolutions; resno++) {
78 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
79 fprintf(fd, "\n res {\n");
81 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
82 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
83 for (bandno = 0; bandno < res->numbands; bandno++) {
84 opj_tcd_band_t *band = &res->bands[bandno];
85 fprintf(fd, " band {\n");
87 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
88 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
89 for (precno = 0; precno < res->pw * res->ph; precno++) {
90 opj_tcd_precinct_t *prec = &band->precincts[precno];
91 fprintf(fd, " prec {\n");
93 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
94 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
96 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
97 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
98 fprintf(fd, " cblk {\n");
100 " x0=%d, y0=%d, x1=%d, y1=%d\n",
101 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
118 * Allocates memory for a decoding code block.
120 static opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
123 * Deallocates the decoding data of the given precinct.
125 static void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
128 * Allocates memory for an encoding code block.
130 static opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block);
133 * Deallocates the encoding data of the given precinct.
135 static void tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct);
139 Free the memory allocated for encoding
140 @param tcd TCD handle
142 static void tcd_free_tile(opj_tcd_v2_t *tcd);
145 opj_bool tcd_t2_decode (
147 OPJ_BYTE * p_src_data,
148 OPJ_UINT32 * p_data_read,
149 OPJ_UINT32 p_max_src_size,
150 opj_codestream_index_t *p_cstr_index
153 opj_bool tcd_t1_decode (
157 opj_bool tcd_dwt_decode (
161 opj_bool tcd_mct_decode (
165 opj_bool tcd_dc_level_shift_decode (
170 /* ----------------------------------------------------------------------- */
173 Create a new TCD handle
175 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
176 /* create the tcd structure */
177 opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
178 if(!tcd) return NULL;
180 tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
181 if(!tcd->tcd_image) {
190 Create a new TCD handle
192 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
194 opj_tcd_v2_t *l_tcd = 00;
196 /* create the tcd structure */
197 l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
201 memset(l_tcd,0,sizeof(opj_tcd_v2_t));
203 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
205 l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
206 if (!l_tcd->tcd_image) {
210 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
216 Destroy a previously created TCD handle
218 void tcd_destroy(opj_tcd_t *tcd) {
220 opj_free(tcd->tcd_image);
225 /* ----------------------------------------------------------------------- */
227 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
228 int tileno, compno, resno, bandno, precno, cblkno;
232 tcd->tcd_image->tw = cp->tw;
233 tcd->tcd_image->th = cp->th;
234 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
236 for (tileno = 0; tileno < 1; tileno++) {
237 opj_tcp_t *tcp = &cp->tcps[curtileno];
240 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
241 int p = curtileno % cp->tw; /* si numerotation matricielle .. */
242 int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
244 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
245 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
247 /* 4 borders of the tile rescale on the image if necessary */
248 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
249 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
250 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
251 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
252 tile->numcomps = image->numcomps;
253 /* tile->PPT=image->PPT; */
255 /* Modification of the RATE >> */
256 for (j = 0; j < tcp->numlayers; j++) {
257 tcp->rates[j] = tcp->rates[j] ?
259 (((float) (tile->numcomps
260 * (tile->x1 - tile->x0)
261 * (tile->y1 - tile->y0)
262 * image->comps[0].prec))
263 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
265 ((float) (tile->numcomps
266 * (tile->x1 - tile->x0)
267 * (tile->y1 - tile->y0)
268 * image->comps[0].prec))/
269 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
273 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
274 tcp->rates[j] = tcp->rates[j - 1] + 20;
276 if (!j && tcp->rates[j] < 30)
280 if(j == (tcp->numlayers-1)){
281 tcp->rates[j] = tcp->rates[j]- 2;
285 /* << Modification of the RATE */
287 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
288 for (compno = 0; compno < tile->numcomps; compno++) {
289 opj_tccp_t *tccp = &tcp->tccps[compno];
291 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
293 /* border of each tile component (global) */
294 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
295 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
296 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
297 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
299 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
300 tilec->numresolutions = tccp->numresolutions;
302 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
304 for (resno = 0; resno < tilec->numresolutions; resno++) {
306 int levelno = tilec->numresolutions - 1 - resno;
307 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
308 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
309 int cbgwidthexpn, cbgheightexpn;
310 int cblkwidthexpn, cblkheightexpn;
312 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
314 /* border for each resolution level (global) */
315 res->x0 = int_ceildivpow2(tilec->x0, levelno);
316 res->y0 = int_ceildivpow2(tilec->y0, levelno);
317 res->x1 = int_ceildivpow2(tilec->x1, levelno);
318 res->y1 = int_ceildivpow2(tilec->y1, levelno);
320 res->numbands = resno == 0 ? 1 : 3;
321 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
322 if (tccp->csty & J2K_CCP_CSTY_PRT) {
323 pdx = tccp->prcw[resno];
324 pdy = tccp->prch[resno];
329 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
330 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
331 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
333 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
334 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
336 res->pw = (brprcxend - tlprcxstart) >> pdx;
337 res->ph = (brprcyend - tlprcystart) >> pdy;
340 tlcbgxstart = tlprcxstart;
341 tlcbgystart = tlprcystart;
342 brcbgxend = brprcxend;
343 brcbgyend = brprcyend;
347 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
348 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
349 brcbgxend = int_ceildivpow2(brprcxend, 1);
350 brcbgyend = int_ceildivpow2(brprcyend, 1);
351 cbgwidthexpn = pdx - 1;
352 cbgheightexpn = pdy - 1;
355 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
356 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
358 for (bandno = 0; bandno < res->numbands; bandno++) {
361 opj_stepsize_t *ss = NULL;
363 opj_tcd_band_t *band = &res->bands[bandno];
365 band->bandno = resno == 0 ? 0 : bandno + 1;
366 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
367 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
369 if (band->bandno == 0) {
370 /* band border (global) */
371 band->x0 = int_ceildivpow2(tilec->x0, levelno);
372 band->y0 = int_ceildivpow2(tilec->y0, levelno);
373 band->x1 = int_ceildivpow2(tilec->x1, levelno);
374 band->y1 = int_ceildivpow2(tilec->y1, levelno);
376 /* band border (global) */
377 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
378 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
379 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
380 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
383 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
384 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
385 numbps = image->comps[compno].prec + gain;
387 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
388 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
390 band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
392 for (i = 0; i < res->pw * res->ph * 3; i++) {
393 band->precincts[i].imsbtree = NULL;
394 band->precincts[i].incltree = NULL;
395 band->precincts[i].cblks.enc = NULL;
398 for (precno = 0; precno < res->pw * res->ph; precno++) {
399 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
401 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
402 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
403 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
404 int cbgyend = cbgystart + (1 << cbgheightexpn);
406 opj_tcd_precinct_t *prc = &band->precincts[precno];
408 /* precinct size (global) */
409 prc->x0 = int_max(cbgxstart, band->x0);
410 prc->y0 = int_max(cbgystart, band->y0);
411 prc->x1 = int_min(cbgxend, band->x1);
412 prc->y1 = int_min(cbgyend, band->y1);
414 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
415 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
416 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
417 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
418 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
419 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
421 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
422 prc->incltree = tgt_create(prc->cw, prc->ch);
423 prc->imsbtree = tgt_create(prc->cw, prc->ch);
425 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
426 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
427 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
428 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
429 int cblkyend = cblkystart + (1 << cblkheightexpn);
431 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
433 /* code-block size (global) */
434 cblk->x0 = int_max(cblkxstart, prc->x0);
435 cblk->y0 = int_max(cblkystart, prc->y0);
436 cblk->x1 = int_min(cblkxend, prc->x1);
437 cblk->y1 = int_min(cblkyend, prc->y1);
438 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
439 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
443 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
444 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
452 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
455 void tcd_free_encode(opj_tcd_t *tcd) {
456 int tileno, compno, resno, bandno, precno, cblkno;
458 for (tileno = 0; tileno < 1; tileno++) {
459 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
461 for (compno = 0; compno < tile->numcomps; compno++) {
462 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
464 for (resno = 0; resno < tilec->numresolutions; resno++) {
465 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
467 for (bandno = 0; bandno < res->numbands; bandno++) {
468 opj_tcd_band_t *band = &res->bands[bandno];
470 for (precno = 0; precno < res->pw * res->ph; precno++) {
471 opj_tcd_precinct_t *prc = &band->precincts[precno];
473 if (prc->incltree != NULL) {
474 tgt_destroy(prc->incltree);
475 prc->incltree = NULL;
477 if (prc->imsbtree != NULL) {
478 tgt_destroy(prc->imsbtree);
479 prc->imsbtree = NULL;
481 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
482 opj_free(prc->cblks.enc[cblkno].data - 2);
483 opj_free(prc->cblks.enc[cblkno].layers);
484 opj_free(prc->cblks.enc[cblkno].passes);
486 opj_free(prc->cblks.enc);
488 opj_free(band->precincts);
489 band->precincts = NULL;
492 opj_free(tilec->resolutions);
493 tilec->resolutions = NULL;
495 opj_free(tile->comps);
498 opj_free(tcd->tcd_image->tiles);
499 tcd->tcd_image->tiles = NULL;
502 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
503 int tileno, compno, resno, bandno, precno, cblkno;
505 for (tileno = 0; tileno < 1; tileno++) {
506 opj_tcp_t *tcp = &cp->tcps[curtileno];
508 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
509 int p = curtileno % cp->tw;
510 int q = curtileno / cp->tw;
512 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
514 /* 4 borders of the tile rescale on the image if necessary */
515 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
516 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
517 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
518 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
520 tile->numcomps = image->numcomps;
521 /* tile->PPT=image->PPT; */
523 /* Modification of the RATE >> */
524 for (j = 0; j < tcp->numlayers; j++) {
525 tcp->rates[j] = tcp->rates[j] ?
527 (((float) (tile->numcomps
528 * (tile->x1 - tile->x0)
529 * (tile->y1 - tile->y0)
530 * image->comps[0].prec))
531 /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
533 ((float) (tile->numcomps
534 * (tile->x1 - tile->x0)
535 * (tile->y1 - tile->y0)
536 * image->comps[0].prec))/
537 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
541 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
542 tcp->rates[j] = tcp->rates[j - 1] + 20;
544 if (!j && tcp->rates[j] < 30)
549 /* << Modification of the RATE */
551 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
552 for (compno = 0; compno < tile->numcomps; compno++) {
553 opj_tccp_t *tccp = &tcp->tccps[compno];
555 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
557 /* border of each tile component (global) */
558 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
559 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
560 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
561 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
563 tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
564 tilec->numresolutions = tccp->numresolutions;
565 /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
566 for (resno = 0; resno < tilec->numresolutions; resno++) {
569 int levelno = tilec->numresolutions - 1 - resno;
570 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
571 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
572 int cbgwidthexpn, cbgheightexpn;
573 int cblkwidthexpn, cblkheightexpn;
575 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
577 /* border for each resolution level (global) */
578 res->x0 = int_ceildivpow2(tilec->x0, levelno);
579 res->y0 = int_ceildivpow2(tilec->y0, levelno);
580 res->x1 = int_ceildivpow2(tilec->x1, levelno);
581 res->y1 = int_ceildivpow2(tilec->y1, levelno);
582 res->numbands = resno == 0 ? 1 : 3;
584 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
585 if (tccp->csty & J2K_CCP_CSTY_PRT) {
586 pdx = tccp->prcw[resno];
587 pdy = tccp->prch[resno];
592 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
593 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
594 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
595 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
596 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
598 res->pw = (brprcxend - tlprcxstart) >> pdx;
599 res->ph = (brprcyend - tlprcystart) >> pdy;
602 tlcbgxstart = tlprcxstart;
603 tlcbgystart = tlprcystart;
604 brcbgxend = brprcxend;
605 brcbgyend = brprcyend;
609 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
610 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
611 brcbgxend = int_ceildivpow2(brprcxend, 1);
612 brcbgyend = int_ceildivpow2(brprcyend, 1);
613 cbgwidthexpn = pdx - 1;
614 cbgheightexpn = pdy - 1;
617 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
618 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
620 for (bandno = 0; bandno < res->numbands; bandno++) {
623 opj_stepsize_t *ss = NULL;
625 opj_tcd_band_t *band = &res->bands[bandno];
627 band->bandno = resno == 0 ? 0 : bandno + 1;
628 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
629 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
631 if (band->bandno == 0) {
633 band->x0 = int_ceildivpow2(tilec->x0, levelno);
634 band->y0 = int_ceildivpow2(tilec->y0, levelno);
635 band->x1 = int_ceildivpow2(tilec->x1, levelno);
636 band->y1 = int_ceildivpow2(tilec->y1, levelno);
638 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
639 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
640 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
641 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
644 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
645 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
646 numbps = image->comps[compno].prec + gain;
647 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
648 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
650 for (precno = 0; precno < res->pw * res->ph; precno++) {
651 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
653 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
654 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
655 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
656 int cbgyend = cbgystart + (1 << cbgheightexpn);
658 opj_tcd_precinct_t *prc = &band->precincts[precno];
660 /* precinct size (global) */
661 prc->x0 = int_max(cbgxstart, band->x0);
662 prc->y0 = int_max(cbgystart, band->y0);
663 prc->x1 = int_min(cbgxend, band->x1);
664 prc->y1 = int_min(cbgyend, band->y1);
666 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
667 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
668 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
669 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
670 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
671 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
673 opj_free(prc->cblks.enc);
674 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
676 if (prc->incltree != NULL) {
677 tgt_destroy(prc->incltree);
679 if (prc->imsbtree != NULL) {
680 tgt_destroy(prc->imsbtree);
683 prc->incltree = tgt_create(prc->cw, prc->ch);
684 prc->imsbtree = tgt_create(prc->cw, prc->ch);
686 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
687 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
688 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
689 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
690 int cblkyend = cblkystart + (1 << cblkheightexpn);
692 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
694 /* code-block size (global) */
695 cblk->x0 = int_max(cblkxstart, prc->x0);
696 cblk->y0 = int_max(cblkystart, prc->y0);
697 cblk->x1 = int_min(cblkxend, prc->x1);
698 cblk->y1 = int_min(cblkyend, prc->y1);
699 cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
700 /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
704 cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
705 cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
713 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
716 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
717 int i, j, tileno, p, q;
718 unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
721 tcd->tcd_image->tw = cp->tw;
722 tcd->tcd_image->th = cp->th;
723 tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
726 Allocate place to store the decoded data = final image
727 Place limited by the tile really present in the codestream
730 for (j = 0; j < cp->tileno_size; j++) {
731 opj_tcd_tile_t *tile;
733 tileno = cp->tileno[j];
734 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
735 tile->numcomps = image->numcomps;
736 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
739 for (i = 0; i < image->numcomps; i++) {
740 for (j = 0; j < cp->tileno_size; j++) {
741 opj_tcd_tile_t *tile;
742 opj_tcd_tilecomp_t *tilec;
744 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
746 tileno = cp->tileno[j];
748 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
749 tilec = &tile->comps[i];
751 p = tileno % cp->tw; /* si numerotation matricielle .. */
752 q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
754 /* 4 borders of the tile rescale on the image if necessary */
755 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
756 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
757 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
758 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
760 tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
761 tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
762 tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
763 tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
765 x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
766 y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
767 x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
768 y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
771 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
772 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
774 image->comps[i].w = w;
775 image->comps[i].h = h;
776 image->comps[i].x0 = x0;
777 image->comps[i].y0 = y0;
781 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) {
782 int compno, resno, bandno, precno, cblkno;
784 opj_tcd_tile_t *tile;
786 OPJ_ARG_NOT_USED(cstr_info);
790 tcp = &(cp->tcps[cp->tileno[tileno]]);
791 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
793 tileno = cp->tileno[tileno];
795 for (compno = 0; compno < tile->numcomps; compno++) {
796 opj_tccp_t *tccp = &tcp->tccps[compno];
797 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
799 if (tccp->numresolutions <= 0)
801 cp->tileno[tileno] = -1;
805 /* border of each tile component (global) */
806 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
807 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
808 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
809 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
811 tilec->numresolutions = tccp->numresolutions;
812 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
814 for (resno = 0; resno < tilec->numresolutions; resno++) {
816 int levelno = tilec->numresolutions - 1 - resno;
817 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
818 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
819 int cbgwidthexpn, cbgheightexpn;
820 int cblkwidthexpn, cblkheightexpn;
822 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
824 /* border for each resolution level (global) */
825 res->x0 = int_ceildivpow2(tilec->x0, levelno);
826 res->y0 = int_ceildivpow2(tilec->y0, levelno);
827 res->x1 = int_ceildivpow2(tilec->x1, levelno);
828 res->y1 = int_ceildivpow2(tilec->y1, levelno);
829 res->numbands = resno == 0 ? 1 : 3;
831 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
832 if (tccp->csty & J2K_CCP_CSTY_PRT) {
833 pdx = tccp->prcw[resno];
834 pdy = tccp->prch[resno];
840 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
841 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
842 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
843 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
844 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
846 res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
847 res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
850 tlcbgxstart = tlprcxstart;
851 tlcbgystart = tlprcystart;
852 brcbgxend = brprcxend;
853 brcbgyend = brprcyend;
857 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
858 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
859 brcbgxend = int_ceildivpow2(brprcxend, 1);
860 brcbgyend = int_ceildivpow2(brprcyend, 1);
861 cbgwidthexpn = pdx - 1;
862 cbgheightexpn = pdy - 1;
865 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
866 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
868 for (bandno = 0; bandno < res->numbands; bandno++) {
871 opj_stepsize_t *ss = NULL;
873 opj_tcd_band_t *band = &res->bands[bandno];
874 band->bandno = resno == 0 ? 0 : bandno + 1;
875 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
876 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
878 if (band->bandno == 0) {
879 /* band border (global) */
880 band->x0 = int_ceildivpow2(tilec->x0, levelno);
881 band->y0 = int_ceildivpow2(tilec->y0, levelno);
882 band->x1 = int_ceildivpow2(tilec->x1, levelno);
883 band->y1 = int_ceildivpow2(tilec->y1, levelno);
885 /* band border (global) */
886 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
887 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
888 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
889 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
892 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
893 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
894 numbps = image->comps[compno].prec + gain;
895 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
896 band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
898 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
900 for (precno = 0; precno < res->pw * res->ph; precno++) {
901 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
902 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
903 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
904 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
905 int cbgyend = cbgystart + (1 << cbgheightexpn);
907 opj_tcd_precinct_t *prc = &band->precincts[precno];
908 /* precinct size (global) */
909 prc->x0 = int_max(cbgxstart, band->x0);
910 prc->y0 = int_max(cbgystart, band->y0);
911 prc->x1 = int_min(cbgxend, band->x1);
912 prc->y1 = int_min(cbgyend, band->y1);
914 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
915 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
916 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
917 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
918 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
919 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
921 prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
923 prc->incltree = tgt_create(prc->cw, prc->ch);
924 prc->imsbtree = tgt_create(prc->cw, prc->ch);
926 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
927 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
928 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
929 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
930 int cblkyend = cblkystart + (1 << cblkheightexpn);
932 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
935 /* code-block size (global) */
936 cblk->x0 = int_max(cblkxstart, prc->x0);
937 cblk->y0 = int_max(cblkystart, prc->y0);
938 cblk->x1 = int_min(cblkxend, prc->x1);
939 cblk->y1 = int_min(cblkyend, prc->y1);
946 /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
949 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
950 int compno, resno, bandno, precno, cblkno;
951 int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
952 int matrice[10][10][3];
955 opj_cp_t *cp = tcd->cp;
956 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
957 opj_tcp_t *tcd_tcp = tcd->tcp;
959 /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
961 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
962 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
963 for (i = 0; i < tcd_tcp->numlayers; i++) {
964 for (j = 0; j < tilec->numresolutions; j++) {
965 for (k = 0; k < 3; k++) {
967 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
968 * (float) (tcd->image->comps[compno].prec / 16.0));
973 for (resno = 0; resno < tilec->numresolutions; resno++) {
974 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
975 for (bandno = 0; bandno < res->numbands; bandno++) {
976 opj_tcd_band_t *band = &res->bands[bandno];
977 for (precno = 0; precno < res->pw * res->ph; precno++) {
978 opj_tcd_precinct_t *prc = &band->precincts[precno];
979 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
980 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
981 opj_tcd_layer_t *layer = &cblk->layers[layno];
983 int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
984 /* Correction of the matrix of coefficient to include the IMSB information */
986 value = matrice[layno][resno][bandno];
993 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
994 if (imsb >= matrice[layno - 1][resno][bandno]) {
995 value -= (imsb - matrice[layno - 1][resno][bandno]);
1003 cblk->numpassesinlayers = 0;
1006 n = cblk->numpassesinlayers;
1007 if (cblk->numpassesinlayers == 0) {
1009 n = 3 * value - 2 + cblk->numpassesinlayers;
1011 n = cblk->numpassesinlayers;
1014 n = 3 * value + cblk->numpassesinlayers;
1017 layer->numpasses = n - cblk->numpassesinlayers;
1019 if (!layer->numpasses)
1022 if (cblk->numpassesinlayers == 0) {
1023 layer->len = cblk->passes[n - 1].rate;
1024 layer->data = cblk->data;
1026 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1027 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1030 cblk->numpassesinlayers = n;
1038 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
1040 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1041 tcd_makelayer_fixed(tcd, layno, 1);
1045 void tcd_rateallocate_fixed_v2(opj_tcd_v2_t *tcd) {
1048 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1049 tcd_makelayer_fixed_v2(tcd, layno, 1);
1053 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
1054 int compno, resno, bandno, precno, cblkno, passno;
1056 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1058 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1060 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1061 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1062 for (resno = 0; resno < tilec->numresolutions; resno++) {
1063 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1064 for (bandno = 0; bandno < res->numbands; bandno++) {
1065 opj_tcd_band_t *band = &res->bands[bandno];
1066 for (precno = 0; precno < res->pw * res->ph; precno++) {
1067 opj_tcd_precinct_t *prc = &band->precincts[precno];
1068 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1069 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1070 opj_tcd_layer_t *layer = &cblk->layers[layno];
1074 cblk->numpassesinlayers = 0;
1076 n = cblk->numpassesinlayers;
1077 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1080 opj_tcd_pass_t *pass = &cblk->passes[passno];
1083 dd = pass->distortiondec;
1085 dr = pass->rate - cblk->passes[n - 1].rate;
1086 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1093 if (dd / dr >= thresh)
1096 layer->numpasses = n - cblk->numpassesinlayers;
1098 if (!layer->numpasses) {
1102 if (cblk->numpassesinlayers == 0) {
1103 layer->len = cblk->passes[n - 1].rate;
1104 layer->data = cblk->data;
1105 layer->disto = cblk->passes[n - 1].distortiondec;
1107 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1108 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1109 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1112 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1115 cblk->numpassesinlayers = n;
1123 void tcd_makelayer_v2( opj_tcd_v2_t *tcd,
1128 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1131 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1133 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
1135 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1136 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1138 for (resno = 0; resno < tilec->numresolutions; resno++) {
1139 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1141 for (bandno = 0; bandno < res->numbands; bandno++) {
1142 opj_tcd_band_v2_t *band = &res->bands[bandno];
1144 for (precno = 0; precno < res->pw * res->ph; precno++) {
1145 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1147 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1148 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1149 opj_tcd_layer_t *layer = &cblk->layers[layno];
1153 cblk->numpassesinlayers = 0;
1156 n = cblk->numpassesinlayers;
1158 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1161 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1165 dd = pass->distortiondec;
1167 dr = pass->rate - cblk->passes[n - 1].rate;
1168 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1176 if (dd / dr >= thresh)
1180 layer->numpasses = n - cblk->numpassesinlayers;
1182 if (!layer->numpasses) {
1187 if (cblk->numpassesinlayers == 0) {
1188 layer->len = cblk->passes[n - 1].rate;
1189 layer->data = cblk->data;
1190 layer->disto = cblk->passes[n - 1].distortiondec;
1192 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1193 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1194 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1197 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
1200 cblk->numpassesinlayers = n;
1208 void tcd_makelayer_fixed_v2(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
1209 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1210 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
1211 OPJ_INT32 matrice[10][10][3];
1214 opj_cp_v2_t *cp = tcd->cp;
1215 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1216 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1218 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1219 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1221 for (i = 0; i < tcd_tcp->numlayers; i++) {
1222 for (j = 0; j < tilec->numresolutions; j++) {
1223 for (k = 0; k < 3; k++) {
1225 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
1226 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
1231 for (resno = 0; resno < tilec->numresolutions; resno++) {
1232 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1234 for (bandno = 0; bandno < res->numbands; bandno++) {
1235 opj_tcd_band_v2_t *band = &res->bands[bandno];
1237 for (precno = 0; precno < res->pw * res->ph; precno++) {
1238 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1240 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1241 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1242 opj_tcd_layer_t *layer = &cblk->layers[layno];
1244 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
1246 /* Correction of the matrix of coefficient to include the IMSB information */
1248 value = matrice[layno][resno][bandno];
1249 if (imsb >= value) {
1255 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
1256 if (imsb >= matrice[layno - 1][resno][bandno]) {
1257 value -= (imsb - matrice[layno - 1][resno][bandno]);
1265 cblk->numpassesinlayers = 0;
1268 n = cblk->numpassesinlayers;
1269 if (cblk->numpassesinlayers == 0) {
1271 n = 3 * value - 2 + cblk->numpassesinlayers;
1273 n = cblk->numpassesinlayers;
1276 n = 3 * value + cblk->numpassesinlayers;
1279 layer->numpasses = n - cblk->numpassesinlayers;
1281 if (!layer->numpasses)
1284 if (cblk->numpassesinlayers == 0) {
1285 layer->len = cblk->passes[n - 1].rate;
1286 layer->data = cblk->data;
1288 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1289 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1293 cblk->numpassesinlayers = n;
1301 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1302 int compno, resno, bandno, precno, cblkno, passno, layno;
1304 double cumdisto[100]; /* fixed_quality */
1305 const double K = 1; /* 1.1; fixed_quality */
1308 opj_cp_t *cp = tcd->cp;
1309 opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1310 opj_tcp_t *tcd_tcp = tcd->tcp;
1315 tcd_tile->numpix = 0; /* fixed_quality */
1317 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1318 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1321 for (resno = 0; resno < tilec->numresolutions; resno++) {
1322 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1324 for (bandno = 0; bandno < res->numbands; bandno++) {
1325 opj_tcd_band_t *band = &res->bands[bandno];
1327 for (precno = 0; precno < res->pw * res->ph; precno++) {
1328 opj_tcd_precinct_t *prc = &band->precincts[precno];
1330 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1331 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1333 for (passno = 0; passno < cblk->totalpasses; passno++) {
1334 opj_tcd_pass_t *pass = &cblk->passes[passno];
1339 dd = pass->distortiondec;
1341 dr = pass->rate - cblk->passes[passno - 1].rate;
1342 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1348 if (rdslope < min) {
1351 if (rdslope > max) {
1357 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1358 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1364 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
1365 * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
1366 * ((double)(tilec->numpix));
1371 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1372 tile_info->numpix = tcd_tile->numpix;
1373 tile_info->distotile = tcd_tile->distotile;
1374 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1377 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1381 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1382 double goodthresh = 0;
1383 double stable_thresh = 0;
1385 double distotarget; /* fixed_quality */
1388 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1390 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1391 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1392 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1393 ==> possible to have some lossy layers and the last layer for sure lossless */
1394 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1395 opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1398 for (i = 0; i < 128; i++) {
1400 double distoachieved = 0; /* fixed_quality */
1401 thresh = (lo + hi) / 2;
1403 tcd_makelayer(tcd, layno, thresh, 0);
1405 if (cp->fixed_quality) { /* fixed_quality */
1407 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);
1412 distoachieved = layno == 0 ?
1413 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1414 if (distoachieved < distotarget) {
1416 stable_thresh = thresh;
1423 distoachieved = (layno == 0) ?
1424 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1425 if (distoachieved < distotarget) {
1427 stable_thresh = thresh;
1433 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);
1434 /* TODO: what to do with l ??? seek / tell ??? */
1435 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1441 stable_thresh = thresh;
1445 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1456 if(cstr_info) { /* Threshold for Marcela Index */
1457 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1459 tcd_makelayer(tcd, layno, goodthresh, 1);
1462 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1468 opj_bool tcd_rateallocate_v2( opj_tcd_v2_t *tcd,
1470 OPJ_UINT32 * p_data_written,
1472 opj_codestream_info_t *cstr_info)
1474 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
1476 OPJ_FLOAT64 min, max;
1477 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
1478 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
1479 OPJ_FLOAT64 maxSE = 0;
1481 opj_cp_v2_t *cp = tcd->cp;
1482 opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1483 opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1488 tcd_tile->numpix = 0; /* fixed_quality */
1490 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1491 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1494 for (resno = 0; resno < tilec->numresolutions; resno++) {
1495 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1497 for (bandno = 0; bandno < res->numbands; bandno++) {
1498 opj_tcd_band_v2_t *band = &res->bands[bandno];
1500 for (precno = 0; precno < res->pw * res->ph; precno++) {
1501 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1503 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1504 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1506 for (passno = 0; passno < cblk->totalpasses; passno++) {
1507 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1509 OPJ_FLOAT64 dd, rdslope;
1513 dd = pass->distortiondec;
1515 dr = pass->rate - cblk->passes[passno - 1].rate;
1516 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1524 if (rdslope < min) {
1528 if (rdslope > max) {
1534 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1535 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1541 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
1542 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
1543 * ((OPJ_FLOAT64)(tilec->numpix));
1548 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1549 tile_info->numpix = tcd_tile->numpix;
1550 tile_info->distotile = tcd_tile->distotile;
1551 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
1554 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1555 OPJ_FLOAT64 lo = min;
1556 OPJ_FLOAT64 hi = max;
1557 opj_bool success = OPJ_FALSE;
1558 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
1559 OPJ_FLOAT64 goodthresh = 0;
1560 OPJ_FLOAT64 stable_thresh = 0;
1562 OPJ_FLOAT64 distotarget; /* fixed_quality */
1565 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1567 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1568 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
1569 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
1570 ==> possible to have some lossy layers and the last layer for sure lossless */
1571 if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1572 opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp);
1573 OPJ_FLOAT64 thresh = 0;
1579 for (i = 0; i < 128; ++i) {
1580 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
1582 thresh = (lo + hi) / 2;
1584 tcd_makelayer_v2(tcd, layno, thresh, 0);
1586 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
1587 if(cp->m_specific_param.m_enc.m_cinema){
1588 if (! t2_encode_packets_v2(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
1594 distoachieved = layno == 0 ?
1595 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1597 if (distoachieved < distotarget) {
1599 stable_thresh = thresh;
1606 distoachieved = (layno == 0) ?
1607 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1609 if (distoachieved < distotarget) {
1611 stable_thresh = thresh;
1617 if (! t2_encode_packets_v2(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
1619 /* TODO: what to do with l ??? seek / tell ??? */
1620 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1626 stable_thresh = thresh;
1631 goodthresh = stable_thresh == 0? thresh : stable_thresh;
1643 if(cstr_info) { /* Threshold for Marcela Index */
1644 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1647 tcd_makelayer_v2(tcd, layno, goodthresh, 1);
1650 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1657 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1659 int l, i, numpacks = 0;
1660 opj_tcd_tile_t *tile = NULL;
1661 opj_tcp_t *tcd_tcp = NULL;
1662 opj_cp_t *cp = NULL;
1664 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1665 opj_tccp_t *tccp = &tcp->tccps[0];
1666 opj_image_t *image = tcd->image;
1668 opj_t1_t *t1 = NULL; /* T1 component */
1669 opj_t2_t *t2 = NULL; /* T2 component */
1671 tcd->tcd_tileno = tileno;
1672 tcd->tcd_tile = tcd->tcd_image->tiles;
1673 tcd->tcp = &tcd->cp->tcps[tileno];
1675 tile = tcd->tcd_tile;
1679 if(tcd->cur_tp_num == 0){
1680 tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
1681 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1683 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
1684 for (i = 0; i < tilec_idx->numresolutions; i++) {
1685 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1687 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1688 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1690 numpacks += res_idx->pw * res_idx->ph;
1692 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1693 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1695 cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1699 /*---------------TILE-------------------*/
1701 for (compno = 0; compno < tile->numcomps; compno++) {
1704 int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1705 int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1706 int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1708 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1709 int tw = tilec->x1 - tilec->x0;
1710 int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1712 /* extract tile data */
1714 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1715 for (y = tilec->y0; y < tilec->y1; y++) {
1716 /* start of the src tile scanline */
1717 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1718 /* start of the dst tile scanline */
1719 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1720 for (x = tilec->x0; x < tilec->x1; x++) {
1721 *tile_data++ = *data++ - adjust;
1724 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1725 for (y = tilec->y0; y < tilec->y1; y++) {
1726 /* start of the src tile scanline */
1727 int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1728 /* start of the dst tile scanline */
1729 int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1730 for (x = tilec->x0; x < tilec->x1; x++) {
1731 *tile_data++ = (*data++ - adjust) << 11;
1738 /*----------------MCT-------------------*/
1740 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1741 if (tcd_tcp->tccps[0].qmfbid == 0) {
1742 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1744 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1748 /*----------------DWT---------------------*/
1750 for (compno = 0; compno < tile->numcomps; compno++) {
1751 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1752 if (tcd_tcp->tccps[compno].qmfbid == 1) {
1754 } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1755 dwt_encode_real(tilec);
1759 /*------------------TIER1-----------------*/
1760 t1 = t1_create(tcd->cinfo);
1761 t1_encode_cblks(t1, tile, tcd_tcp);
1764 /*-----------RATE-ALLOCATE------------------*/
1768 cstr_info->index_write = 0;
1770 if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
1771 /* Normal Rate/distortion allocation */
1772 tcd_rateallocate(tcd, dest, len, cstr_info);
1774 /* Fixed layer allocation */
1775 tcd_rateallocate_fixed(tcd);
1778 /*--------------TIER2------------------*/
1782 cstr_info->index_write = 1;
1785 t2 = t2_create(tcd->cinfo, image, cp);
1786 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);
1789 /*---------------CLEAN-------------------*/
1792 if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1793 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1794 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1796 /* cleaning memory */
1797 for (compno = 0; compno < tile->numcomps; compno++) {
1798 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1799 opj_aligned_free(tilec->data);
1806 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1810 double tile_time, t1_time, dwt_time;
1811 opj_tcd_tile_t *tile = NULL;
1813 opj_t1_t *t1 = NULL; /* T1 component */
1814 opj_t2_t *t2 = NULL; /* T2 component */
1816 tcd->tcd_tileno = tileno;
1817 tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1818 tcd->tcp = &(tcd->cp->tcps[tileno]);
1819 tile = tcd->tcd_tile;
1821 tile_time = opj_clock(); /* time needed to decode a tile */
1822 opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1826 int resno, compno, numprec = 0;
1827 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1828 opj_tcp_t *tcp = &tcd->cp->tcps[0];
1829 opj_tccp_t *tccp = &tcp->tccps[compno];
1830 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
1831 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1832 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1833 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1834 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1835 numprec += res_idx->pw * res_idx->ph;
1836 if (tccp->csty & J2K_CP_CSTY_PRT) {
1837 cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1838 cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1841 cstr_info->tile[tileno].pdx[resno] = 15;
1842 cstr_info->tile[tileno].pdy[resno] = 15;
1846 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1847 cstr_info->packno = 0;
1851 /*--------------TIER2------------------*/
1853 t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1854 l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1859 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1862 /*------------------TIER1-----------------*/
1864 t1_time = opj_clock(); /* time needed to decode a tile */
1865 t1 = t1_create(tcd->cinfo);
1866 for (compno = 0; compno < tile->numcomps; ++compno) {
1867 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1868 /* The +3 is headroom required by the vectorized DWT */
1869 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1870 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1873 t1_time = opj_clock() - t1_time;
1874 opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1876 /*----------------DWT---------------------*/
1878 dwt_time = opj_clock(); /* time needed to decode a tile */
1879 for (compno = 0; compno < tile->numcomps; compno++) {
1880 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1883 if (tcd->cp->reduce != 0) {
1884 tcd->image->comps[compno].resno_decoded =
1885 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1886 if (tcd->image->comps[compno].resno_decoded < 0) {
1887 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1888 " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1893 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1894 if(numres2decode > 0){
1895 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1896 dwt_decode(tilec, numres2decode);
1898 dwt_decode_real(tilec, numres2decode);
1902 dwt_time = opj_clock() - dwt_time;
1903 opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1905 /*----------------MCT-------------------*/
1907 if (tcd->tcp->mct) {
1908 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1910 if (tile->numcomps >= 3 ){
1911 if (tcd->tcp->tccps[0].qmfbid == 1) {
1913 tile->comps[0].data,
1914 tile->comps[1].data,
1915 tile->comps[2].data,
1919 (float*)tile->comps[0].data,
1920 (float*)tile->comps[1].data,
1921 (float*)tile->comps[2].data,
1925 opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1929 /*---------------TILE-------------------*/
1931 for (compno = 0; compno < tile->numcomps; ++compno) {
1932 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1933 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1934 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1935 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1936 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1937 int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1939 int tw = tilec->x1 - tilec->x0;
1942 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1943 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1947 imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1949 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1950 for(j = res->y0; j < res->y1; ++j) {
1951 for(i = res->x0; i < res->x1; ++i) {
1952 int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1954 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1958 for(j = res->y0; j < res->y1; ++j) {
1959 for(i = res->x0; i < res->x1; ++i) {
1960 float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1961 int v = lrintf(tmp);
1963 imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1967 opj_aligned_free(tilec->data);
1970 tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
1971 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1980 void tcd_free_decode(opj_tcd_t *tcd) {
1981 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1982 opj_free(tcd_image->tiles);
1985 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1986 int compno,resno,bandno,precno;
1988 opj_tcd_image_t *tcd_image = tcd->tcd_image;
1990 opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1991 for (compno = 0; compno < tile->numcomps; compno++) {
1992 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1993 for (resno = 0; resno < tilec->numresolutions; resno++) {
1994 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1995 for (bandno = 0; bandno < res->numbands; bandno++) {
1996 opj_tcd_band_t *band = &res->bands[bandno];
1997 for (precno = 0; precno < res->ph * res->pw; precno++) {
1998 opj_tcd_precinct_t *prec = &band->precincts[precno];
1999 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
2000 if (prec->incltree != NULL) tgt_destroy(prec->incltree);
2002 opj_free(band->precincts);
2005 opj_free(tilec->resolutions);
2007 opj_free(tile->comps);
2011 opj_bool tcd_init_v2( opj_tcd_v2_t *p_tcd,
2012 opj_image_t * p_image,
2013 opj_cp_v2_t * p_cp )
2015 OPJ_UINT32 l_tile_comp_size;
2017 p_tcd->image = p_image;
2020 p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
2021 if (! p_tcd->tcd_image->tiles) {
2024 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
2026 l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
2027 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
2028 if (! p_tcd->tcd_image->tiles->comps ) {
2031 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
2033 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
2034 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
2040 Destroy a previously created TCD handle
2042 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
2046 if (tcd->tcd_image) {
2047 opj_free(tcd->tcd_image);
2048 tcd->tcd_image = 00;
2054 /* ----------------------------------------------------------------------- */
2056 * Initialize the tile coder and may reuse some meory.
2057 * @param p_tcd TCD handle.
2058 * @param p_image raw image.
2059 * @param p_cp coding parameters.
2060 * @param p_tile_no current tile index to encode.
2062 * @return true if the encoding values could be set (false otherwise).
2064 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
2065 opj_bool FUNCTION ( opj_tcd_v2_t *p_tcd, \
2066 OPJ_UINT32 p_tile_no \
2069 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
2070 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
2071 opj_tcp_v2_t * l_tcp = 00; \
2072 opj_cp_v2_t * l_cp = 00; \
2073 opj_tcd_tile_v2_t * l_tile = 00; \
2074 opj_tccp_t *l_tccp = 00; \
2075 opj_tcd_tilecomp_v2_t *l_tilec = 00; \
2076 opj_image_comp_t * l_image_comp = 00; \
2077 opj_tcd_resolution_v2_t *l_res = 00; \
2078 opj_tcd_band_v2_t *l_band = 00; \
2079 opj_stepsize_t * l_step_size = 00; \
2080 opj_tcd_precinct_v2_t *l_current_precinct = 00; \
2081 TYPE* l_code_block = 00; \
2082 opj_image_t *l_image = 00; \
2084 OPJ_UINT32 l_level_no; \
2085 OPJ_UINT32 l_pdx, l_pdy; \
2086 OPJ_UINT32 l_gain; \
2087 OPJ_INT32 l_x0b, l_y0b; \
2088 /* extent of precincts , top left, bottom right**/ \
2089 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
2090 /* number of precinct for a resolution */ \
2091 OPJ_UINT32 l_nb_precincts; \
2092 /* room needed to store l_nb_precinct precinct for a resolution */ \
2093 OPJ_UINT32 l_nb_precinct_size; \
2094 /* number of code blocks for a precinct*/ \
2095 OPJ_UINT32 l_nb_code_blocks; \
2096 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
2097 OPJ_UINT32 l_nb_code_blocks_size; \
2098 /* size of data for a tile */ \
2099 OPJ_UINT32 l_data_size; \
2102 l_tcp = &(l_cp->tcps[p_tile_no]); \
2103 l_tile = p_tcd->tcd_image->tiles; \
2104 l_tccp = l_tcp->tccps; \
2105 l_tilec = l_tile->comps; \
2106 l_image = p_tcd->image; \
2107 l_image_comp = p_tcd->image->comps; \
2109 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
2110 q = p_tile_no / l_cp->tw; \
2111 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
2113 /* 4 borders of the tile rescale on the image if necessary */ \
2114 l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0); \
2115 l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0); \
2116 l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1); \
2117 l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1); \
2118 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
2120 /*tile->numcomps = image->numcomps; */ \
2121 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
2122 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
2124 /* border of each l_tile component (global) */ \
2125 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx); \
2126 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy); \
2127 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx); \
2128 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy); \
2129 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
2131 l_data_size = (l_tilec->x1 - l_tilec->x0) \
2132 * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 ); \
2133 l_tilec->numresolutions = l_tccp->numresolutions; \
2134 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
2135 l_tilec->minimum_num_resolutions = 1; \
2138 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
2139 - l_cp->m_specific_param.m_dec.m_reduce; \
2142 if (l_tilec->data == 00) { \
2143 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
2144 if (! l_tilec->data ) { \
2147 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
2149 l_tilec->data_size = l_data_size; \
2151 else if (l_data_size > l_tilec->data_size) { \
2152 l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
2153 if (! l_tilec->data) { \
2156 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
2157 l_tilec->data_size = l_data_size; \
2160 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t); \
2162 if (l_tilec->resolutions == 00) { \
2163 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
2164 if (! l_tilec->resolutions ) { \
2167 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/ \
2168 l_tilec->resolutions_size = l_data_size; \
2169 memset(l_tilec->resolutions,0,l_data_size); \
2171 else if (l_data_size > l_tilec->resolutions_size) { \
2172 l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
2173 if (! l_tilec->resolutions) { \
2176 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
2177 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
2178 l_tilec->resolutions_size = l_data_size; \
2181 l_level_no = l_tilec->numresolutions - 1; \
2182 l_res = l_tilec->resolutions; \
2183 l_step_size = l_tccp->stepsizes; \
2184 if (l_tccp->qmfbid == 0) { \
2185 l_gain_ptr = &dwt_getgain_real_v2; \
2188 l_gain_ptr = &dwt_getgain_v2; \
2190 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
2192 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
2193 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
2194 OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend; \
2195 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
2196 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
2198 /* border for each resolution level (global) */ \
2199 l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
2200 l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
2201 l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
2202 l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
2203 /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/ \
2204 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
2205 l_pdx = l_tccp->prcw[resno]; \
2206 l_pdy = l_tccp->prch[resno]; \
2207 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
2208 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
2209 l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx; \
2210 l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy; \
2211 l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx; \
2212 l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy; \
2213 /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/ \
2215 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
2216 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
2217 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
2219 l_nb_precincts = l_res->pw * l_res->ph; \
2220 l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t); \
2222 tlcbgxstart = l_tl_prc_x_start; \
2223 tlcbgystart = l_tl_prc_y_start; \
2224 brcbgxend = l_br_prc_x_end; \
2225 brcbgyend = l_br_prc_y_end; \
2226 cbgwidthexpn = l_pdx; \
2227 cbgheightexpn = l_pdy; \
2228 l_res->numbands = 1; \
2231 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1); \
2232 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1); \
2233 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1); \
2234 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1); \
2235 cbgwidthexpn = l_pdx - 1; \
2236 cbgheightexpn = l_pdy - 1; \
2237 l_res->numbands = 3; \
2240 cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn); \
2241 cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn); \
2242 l_band = l_res->bands; \
2244 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
2246 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
2249 l_band->bandno = 0 ; \
2250 l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no); \
2251 l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no); \
2252 l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no); \
2253 l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no); \
2256 l_band->bandno = bandno + 1; \
2257 /* x0b = 1 if bandno = 1 or 3 */ \
2258 l_x0b = l_band->bandno&1; \
2259 /* y0b = 1 if bandno = 2 or 3 */ \
2260 l_y0b = (l_band->bandno)>>1; \
2261 /* l_band border (global) */ \
2262 l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
2263 l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
2264 l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1); \
2265 l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1); \
2268 /** avoid an if with storing function pointer */ \
2269 l_gain = (*l_gain_ptr) (l_band->bandno); \
2270 numbps = l_image_comp->prec + l_gain; \
2271 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
2272 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1; /* WHY -1 ? */\
2274 if (! l_band->precincts) { \
2275 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
2276 if (! l_band->precincts) { \
2279 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size); */ \
2280 memset(l_band->precincts,0,l_nb_precinct_size); \
2281 l_band->precincts_data_size = l_nb_precinct_size; \
2283 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
2285 l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
2286 if (! l_band->precincts) { \
2289 /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_v2_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/\
2290 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
2291 l_band->precincts_data_size = l_nb_precinct_size; \
2294 l_current_precinct = l_band->precincts; \
2295 for (precno = 0; precno < l_nb_precincts; ++precno) { \
2296 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
2297 OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
2298 OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn); \
2299 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
2300 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
2301 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
2302 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
2304 /* precinct size (global) */ \
2305 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
2307 l_current_precinct->x0 = int_max(cbgxstart, l_band->x0); \
2308 l_current_precinct->y0 = int_max(cbgystart, l_band->y0); \
2309 l_current_precinct->x1 = int_min(cbgxend, l_band->x1); \
2310 l_current_precinct->y1 = int_min(cbgyend, l_band->y1); \
2311 /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ \
2313 tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn; \
2314 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
2315 tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn; \
2316 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
2317 brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn; \
2318 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
2319 brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
2320 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
2321 l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn; \
2322 l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
2324 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
2325 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
2326 l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE); \
2328 if (! l_current_precinct->cblks.ELEMENT) { \
2329 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
2330 if (! l_current_precinct->cblks.ELEMENT ) { \
2333 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/ \
2335 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
2337 l_current_precinct->block_size = l_nb_code_blocks_size; \
2339 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
2340 l_current_precinct->cblks.ELEMENT = (TYPE*) \
2341 opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
2342 if (! l_current_precinct->cblks.ELEMENT ) { \
2345 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size); */ \
2347 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
2349 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
2351 l_current_precinct->block_size = l_nb_code_blocks_size; \
2354 if (! l_current_precinct->incltree) { \
2355 l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw, \
2356 l_current_precinct->ch); \
2359 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree, \
2360 l_current_precinct->cw, \
2361 l_current_precinct->ch); \
2364 if (! l_current_precinct->incltree) { \
2365 fprintf(stderr, "WARNING: No incltree created.\n");\
2366 /*return OPJ_FALSE;*/ \
2369 if (! l_current_precinct->imsbtree) { \
2370 l_current_precinct->imsbtree = tgt_create_v2( \
2371 l_current_precinct->cw, \
2372 l_current_precinct->ch);\
2375 l_current_precinct->imsbtree = tgt_init( \
2376 l_current_precinct->imsbtree, \
2377 l_current_precinct->cw, \
2378 l_current_precinct->ch); \
2381 if (! l_current_precinct->imsbtree) { \
2382 fprintf(stderr, "WARNING: No imsbtree created.\n");\
2383 /*return OPJ_FALSE;*/ \
2386 l_code_block = l_current_precinct->cblks.ELEMENT; \
2388 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
2389 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
2390 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
2391 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
2392 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
2394 /* code-block size (global) */ \
2395 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
2396 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
2397 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1); \
2398 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1); \
2400 if (! FUNCTION_ELEMENT(l_code_block)) { \
2405 ++l_current_precinct; \
2421 MACRO_TCD_ALLOCATE(tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, tcd_code_block_enc_allocate)
2422 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
2424 #undef MACRO_TCD_ALLOCATE
2427 * Allocates memory for an encoding code block.
2429 opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
2431 if (! p_code_block->data) {
2433 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
2434 if(! p_code_block->data) {
2438 p_code_block->data[0] = 0;
2439 p_code_block->data+=1;
2441 /* no memset since data */
2442 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
2443 if (! p_code_block->layers) {
2447 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
2448 if (! p_code_block->passes) {
2453 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
2454 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
2460 * Allocates memory for a decoding code block.
2462 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2464 OPJ_UINT32 l_seg_size;
2466 if (! p_code_block->data) {
2468 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2469 if (! p_code_block->data) {
2472 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2474 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2475 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2476 if (! p_code_block->segs) {
2479 memset(p_code_block->segs,0,l_seg_size);
2480 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2482 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2483 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2486 /*p_code_block->numsegs = 0; */
2491 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2494 OPJ_UINT32 l_data_size = 0;
2495 opj_image_comp_t * l_img_comp = 00;
2496 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2497 opj_tcd_resolution_v2_t * l_res = 00;
2498 OPJ_UINT32 l_size_comp, l_remaining;
2500 l_tile_comp = p_tcd->tcd_image->tiles->comps;
2501 l_img_comp = p_tcd->image->comps;
2503 for (i=0;i<p_tcd->image->numcomps;++i) {
2504 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2505 l_remaining = l_img_comp->prec & 7; /* (%8) */
2511 if (l_size_comp == 3) {
2515 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2516 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2524 opj_bool tcd_encode_tile_v2(opj_tcd_v2_t *p_tcd,
2525 OPJ_UINT32 p_tile_no,
2527 OPJ_UINT32 * p_data_written,
2528 OPJ_UINT32 p_max_length,
2529 opj_codestream_info_t *p_cstr_info)
2532 if (p_tcd->cur_tp_num == 0) {
2534 p_tcd->tcd_tileno = p_tile_no;
2535 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
2537 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
2539 OPJ_UINT32 l_num_packs = 0;
2541 opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
2542 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
2544 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
2545 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
2547 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
2548 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
2550 l_num_packs += l_res_idx->pw * l_res_idx->ph;
2551 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
2552 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
2554 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
2558 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2559 /*---------------TILE-------------------*/
2560 if (! tcd_dc_level_shift_encode(p_tcd)) {
2563 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2565 /* FIXME _ProfStart(PGROUP_MCT); */
2566 if (! tcd_mct_encode(p_tcd)) {
2569 /* FIXME _ProfStop(PGROUP_MCT); */
2571 /* FIXME _ProfStart(PGROUP_DWT); */
2572 if (! tcd_dwt_encode(p_tcd)) {
2575 /* FIXME _ProfStop(PGROUP_DWT); */
2577 /* FIXME _ProfStart(PGROUP_T1); */
2578 if (! tcd_t1_encode(p_tcd)) {
2581 /* FIXME _ProfStop(PGROUP_T1); */
2583 /* FIXME _ProfStart(PGROUP_RATE); */
2584 if (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
2587 /* FIXME _ProfStop(PGROUP_RATE); */
2590 /*--------------TIER2------------------*/
2594 p_cstr_info->index_write = 1;
2596 /* FIXME _ProfStart(PGROUP_T2); */
2598 if (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
2601 /* FIXME _ProfStop(PGROUP_T2); */
2603 /*---------------CLEAN-------------------*/
2608 opj_bool tcd_decode_tile_v2(
2609 opj_tcd_v2_t *p_tcd,
2611 OPJ_UINT32 p_max_length,
2612 OPJ_UINT32 p_tile_no,
2613 opj_codestream_index_t *p_cstr_index)
2615 OPJ_UINT32 l_data_read;
2616 p_tcd->tcd_tileno = p_tile_no;
2617 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2619 #ifdef TODO_MSD /* FIXME */
2622 OPJ_UINT32 resno, compno, numprec = 0;
2623 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2624 opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2625 opj_tccp_t *tccp = &tcp->tccps[compno];
2626 opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2627 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2628 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2629 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2630 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2631 numprec += res_idx->pw * res_idx->ph;
2632 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2633 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2636 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2637 p_cstr_info->packno = 0;
2642 /*--------------TIER2------------------*/
2643 /* FIXME _ProfStart(PGROUP_T2); */
2646 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2650 /* FIXME _ProfStop(PGROUP_T2); */
2652 /*------------------TIER1-----------------*/
2654 /* FIXME _ProfStart(PGROUP_T1); */
2656 (! tcd_t1_decode(p_tcd))
2660 /* FIXME _ProfStop(PGROUP_T1); */
2662 /*----------------DWT---------------------*/
2664 /* FIXME _ProfStart(PGROUP_DWT); */
2666 (! tcd_dwt_decode(p_tcd))
2670 /* FIXME _ProfStop(PGROUP_DWT); */
2672 /*----------------MCT-------------------*/
2673 /* FIXME _ProfStart(PGROUP_MCT); */
2675 (! tcd_mct_decode(p_tcd))
2679 /* FIXME _ProfStop(PGROUP_MCT); */
2681 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2683 (! tcd_dc_level_shift_decode(p_tcd))
2687 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2690 /*---------------TILE-------------------*/
2694 opj_bool tcd_update_tile_data (
2695 opj_tcd_v2_t *p_tcd,
2697 OPJ_UINT32 p_dest_length
2700 OPJ_UINT32 i,j,k,l_data_size = 0;
2701 opj_image_comp_t * l_img_comp = 00;
2702 opj_tcd_tilecomp_v2_t * l_tilec = 00;
2703 opj_tcd_resolution_v2_t * l_res;
2704 OPJ_UINT32 l_size_comp, l_remaining;
2705 OPJ_UINT32 l_stride, l_width,l_height;
2707 l_data_size = tcd_get_decoded_tile_size(p_tcd);
2708 if (l_data_size > p_dest_length) {
2712 l_tilec = p_tcd->tcd_image->tiles->comps;
2713 l_img_comp = p_tcd->image->comps;
2715 for (i=0;i<p_tcd->image->numcomps;++i) {
2716 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2717 l_remaining = l_img_comp->prec & 7; /* (%8) */
2718 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2719 l_width = (l_res->x1 - l_res->x0);
2720 l_height = (l_res->y1 - l_res->y0);
2721 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2727 if (l_size_comp == 3) {
2731 switch (l_size_comp)
2735 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2736 const OPJ_INT32 * l_src_ptr = l_tilec->data;
2738 if (l_img_comp->sgnd) {
2739 for (j=0;j<l_height;++j) {
2740 for (k=0;k<l_width;++k) {
2741 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2743 l_src_ptr += l_stride;
2747 for (j=0;j<l_height;++j) {
2748 for (k=0;k<l_width;++k) {
2749 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2751 l_src_ptr += l_stride;
2755 p_dest = (OPJ_BYTE *)l_dest_ptr;
2760 const OPJ_INT32 * l_src_ptr = l_tilec->data;
2761 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2763 if (l_img_comp->sgnd) {
2764 for (j=0;j<l_height;++j) {
2765 for (k=0;k<l_width;++k) {
2766 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2768 l_src_ptr += l_stride;
2772 for (j=0;j<l_height;++j) {
2773 for (k=0;k<l_width;++k) {
2774 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2776 l_src_ptr += l_stride;
2780 p_dest = (OPJ_BYTE*) l_dest_ptr;
2785 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2786 OPJ_INT32 * l_src_ptr = l_tilec->data;
2788 for (j=0;j<l_height;++j) {
2789 for (k=0;k<l_width;++k) {
2790 *(l_dest_ptr++) = (*(l_src_ptr++));
2792 l_src_ptr += l_stride;
2795 p_dest = (OPJ_BYTE*) l_dest_ptr;
2810 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2812 OPJ_UINT32 compno, resno, bandno, precno;
2813 opj_tcd_tile_v2_t *l_tile = 00;
2814 opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2815 opj_tcd_resolution_v2_t *l_res = 00;
2816 opj_tcd_band_v2_t *l_band = 00;
2817 opj_tcd_precinct_v2_t *l_precinct = 00;
2818 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2819 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2825 if (! p_tcd->tcd_image) {
2829 if (p_tcd->m_is_decoder) {
2830 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2833 l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
2836 l_tile = p_tcd->tcd_image->tiles;
2841 l_tile_comp = l_tile->comps;
2843 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2844 l_res = l_tile_comp->resolutions;
2847 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2848 for (resno = 0; resno < l_nb_resolutions; ++resno) {
2849 l_band = l_res->bands;
2850 for (bandno = 0; bandno < 3; ++bandno) {
2851 l_precinct = l_band->precincts;
2854 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2855 for (precno = 0; precno < l_nb_precincts; ++precno) {
2856 tgt_destroy(l_precinct->incltree);
2857 l_precinct->incltree = 00;
2858 tgt_destroy(l_precinct->imsbtree);
2859 l_precinct->imsbtree = 00;
2860 (*l_tcd_code_block_deallocate) (l_precinct);
2864 opj_free(l_band->precincts);
2865 l_band->precincts = 00;
2872 opj_free(l_tile_comp->resolutions);
2873 l_tile_comp->resolutions = 00;
2876 if (l_tile_comp->data) {
2877 opj_free(l_tile_comp->data);
2878 l_tile_comp->data = 00;
2883 opj_free(l_tile->comps);
2885 opj_free(p_tcd->tcd_image->tiles);
2886 p_tcd->tcd_image->tiles = 00;
2890 opj_bool tcd_t2_decode (
2891 opj_tcd_v2_t *p_tcd,
2892 OPJ_BYTE * p_src_data,
2893 OPJ_UINT32 * p_data_read,
2894 OPJ_UINT32 p_max_src_size,
2895 opj_codestream_index_t *p_cstr_index
2900 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2905 if (! t2_decode_packets_v2(
2908 p_tcd->tcd_image->tiles,
2913 t2_destroy_v2(l_t2);
2917 t2_destroy_v2(l_t2);
2919 /*---------------CLEAN-------------------*/
2923 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2927 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2928 opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2929 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2932 l_t1 = t1_create_v2();
2937 for (compno = 0; compno < l_tile->numcomps; ++compno) {
2938 /* The +3 is headroom required by the vectorized DWT */
2939 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2944 t1_destroy_v2(l_t1);
2950 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2953 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2954 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2955 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2956 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2958 for (compno = 0; compno < l_tile->numcomps; compno++) {
2960 if (tcd->cp->reduce != 0) {
2961 tcd->image->comps[compno].resno_decoded =
2962 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2963 if (tcd->image->comps[compno].resno_decoded < 0)
2968 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2969 if(numres2decode > 0){
2972 if (l_tccp->qmfbid == 1) {
2973 if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2978 if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2990 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2992 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2993 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2994 opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2995 OPJ_UINT32 l_samples,i;
3001 l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3003 if (l_tile->numcomps >= 3 ){
3004 if (l_tcp->mct == 2) {
3007 if (! l_tcp->m_mct_decoding_matrix) {
3011 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3016 for (i=0;i<l_tile->numcomps;++i) {
3017 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3021 if (! mct_decode_custom(/* MCT data */
3022 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
3023 /* size of components */
3027 /* nb of components (i.e. size of pData) */
3029 /* tells if the data is signed */
3030 p_tcd->image->comps->sgnd)) {
3038 if (l_tcp->tccps->qmfbid == 1) {
3039 mct_decode( l_tile->comps[0].data,
3040 l_tile->comps[1].data,
3041 l_tile->comps[2].data,
3045 mct_decode_real( (float*)l_tile->comps[0].data,
3046 (float*)l_tile->comps[1].data,
3047 (float*)l_tile->comps[2].data,
3053 /* FIXME need to use opj_event_msg_v2 function */
3054 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
3061 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
3064 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3065 opj_tccp_t * l_tccp = 00;
3066 opj_image_comp_t * l_img_comp = 00;
3067 opj_tcd_resolution_v2_t* l_res = 00;
3068 opj_tcp_v2_t * l_tcp = 00;
3069 opj_tcd_tile_v2_t * l_tile;
3070 OPJ_UINT32 l_width,l_height,i,j;
3071 OPJ_INT32 * l_current_ptr;
3072 OPJ_INT32 l_min, l_max;
3073 OPJ_UINT32 l_stride;
3075 l_tile = p_tcd->tcd_image->tiles;
3076 l_tile_comp = l_tile->comps;
3078 l_tccp = p_tcd->tcp->tccps;
3079 l_img_comp = p_tcd->image->comps;
3081 for (compno = 0; compno < l_tile->numcomps; compno++) {
3082 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
3083 l_width = (l_res->x1 - l_res->x0);
3084 l_height = (l_res->y1 - l_res->y0);
3085 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
3087 if (l_img_comp->sgnd) {
3088 l_min = -(1 << (l_img_comp->prec - 1));
3089 l_max = (1 << (l_img_comp->prec - 1)) - 1;
3093 l_max = (1 << l_img_comp->prec) - 1;
3096 l_current_ptr = l_tile_comp->data;
3098 if (l_tccp->qmfbid == 1) {
3099 for (j=0;j<l_height;++j) {
3100 for (i = 0; i < l_width; ++i) {
3101 *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
3104 l_current_ptr += l_stride;
3108 for (j=0;j<l_height;++j) {
3109 for (i = 0; i < l_width; ++i) {
3110 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
3111 *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
3114 l_current_ptr += l_stride;
3129 * Deallocates the encoding data of the given precinct.
3131 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3133 OPJ_UINT32 cblkno , l_nb_code_blocks;
3135 opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
3137 /*fprintf(stderr,"deallocate codeblock:{\n");*/
3138 /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
3139 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
3140 l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
3143 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
3144 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
3146 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
3148 if (l_code_block->data) {
3149 opj_free(l_code_block->data);
3150 l_code_block->data = 00;
3153 if (l_code_block->segs) {
3154 opj_free(l_code_block->segs );
3155 l_code_block->segs = 00;
3161 opj_free(p_precinct->cblks.dec);
3162 p_precinct->cblks.dec = 00;
3167 * Deallocates the encoding data of the given precinct.
3169 void tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3171 OPJ_UINT32 cblkno , l_nb_code_blocks;
3173 opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc;
3175 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
3177 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
3178 if (l_code_block->data) {
3179 opj_free(l_code_block->data-1);
3180 l_code_block->data = 00;
3183 if (l_code_block->layers) {
3184 opj_free(l_code_block->layers );
3185 l_code_block->layers = 00;
3188 if (l_code_block->passes) {
3189 opj_free(l_code_block->passes );
3190 l_code_block->passes = 00;
3195 opj_free(p_precinct->cblks.enc);
3197 p_precinct->cblks.enc = 00;
3201 OPJ_UINT32 tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
3203 OPJ_UINT32 i,l_data_size = 0;
3204 opj_image_comp_t * l_img_comp = 00;
3205 opj_tcd_tilecomp_v2_t * l_tilec = 00;
3206 OPJ_UINT32 l_size_comp, l_remaining;
3208 l_tilec = p_tcd->tcd_image->tiles->comps;
3209 l_img_comp = p_tcd->image->comps;
3210 for (i=0;i<p_tcd->image->numcomps;++i) {
3211 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3212 l_remaining = l_img_comp->prec & 7; /* (%8) */
3218 if (l_size_comp == 3) {
3222 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3230 opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
3233 opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3234 opj_tccp_t * l_tccp = 00;
3235 opj_image_comp_t * l_img_comp = 00;
3236 opj_tcp_v2_t * l_tcp = 00;
3237 opj_tcd_tile_v2_t * l_tile;
3238 OPJ_UINT32 l_nb_elem,i;
3239 OPJ_INT32 * l_current_ptr;
3241 l_tile = p_tcd->tcd_image->tiles;
3242 l_tile_comp = l_tile->comps;
3244 l_tccp = p_tcd->tcp->tccps;
3245 l_img_comp = p_tcd->image->comps;
3247 for (compno = 0; compno < l_tile->numcomps; compno++) {
3248 l_current_ptr = l_tile_comp->data;
3249 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3251 if (l_tccp->qmfbid == 1) {
3252 for (i = 0; i < l_nb_elem; ++i) {
3253 *l_current_ptr -= l_tccp->m_dc_level_shift ;
3258 for (i = 0; i < l_nb_elem; ++i) {
3259 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
3272 opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
3274 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3275 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3276 OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3278 OPJ_BYTE ** l_data = 00;
3279 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3281 if(!p_tcd->tcp->mct) {
3285 if (p_tcd->tcp->mct == 2) {
3286 if (! p_tcd->tcp->m_mct_coding_matrix) {
3290 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3295 for (i=0;i<l_tile->numcomps;++i) {
3296 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3300 if (! mct_encode_custom(/* MCT data */
3301 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
3302 /* size of components */
3306 /* nb of components (i.e. size of pData) */
3308 /* tells if the data is signed */
3309 p_tcd->image->comps->sgnd) )
3317 else if (l_tcp->tccps->qmfbid == 0) {
3318 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3321 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3327 opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
3329 opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3330 opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3331 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
3334 for (compno = 0; compno < l_tile->numcomps; ++compno) {
3335 if (l_tccp->qmfbid == 1) {
3336 if (! dwt_encode_v2(l_tile_comp)) {
3340 else if (l_tccp->qmfbid == 0) {
3341 if (! dwt_encode_real_v2(l_tile_comp)) {
3353 opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
3356 const OPJ_FLOAT64 * l_mct_norms;
3357 opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3359 l_t1 = t1_create_v2();
3364 if (l_tcp->mct == 1) {
3365 /* irreversible encoding */
3366 if (l_tcp->tccps->qmfbid == 0) {
3367 l_mct_norms = get_mct_norms_real();
3370 l_mct_norms = get_mct_norms();
3374 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
3377 if (! t1_encode_cblks_v2(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
3378 t1_destroy_v2(l_t1);
3382 t1_destroy_v2(l_t1);
3387 opj_bool tcd_t2_encode (opj_tcd_v2_t *p_tcd,
3388 OPJ_BYTE * p_dest_data,
3389 OPJ_UINT32 * p_data_written,
3390 OPJ_UINT32 p_max_dest_size,
3391 opj_codestream_info_t *p_cstr_info )
3395 l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
3400 if (! t2_encode_packets_v2(
3403 p_tcd->tcd_image->tiles,
3404 p_tcd->tcp->numlayers,
3414 t2_destroy_v2(l_t2);
3418 t2_destroy_v2(l_t2);
3420 /*---------------CLEAN-------------------*/
3425 opj_bool tcd_rate_allocate_encode( opj_tcd_v2_t *p_tcd,
3426 OPJ_BYTE * p_dest_data,
3427 OPJ_UINT32 p_max_dest_size,
3428 opj_codestream_info_t *p_cstr_info )
3430 opj_cp_v2_t * l_cp = p_tcd->cp;
3431 OPJ_UINT32 l_nb_written = 0;
3434 p_cstr_info->index_write = 0;
3437 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
3439 /* Normal Rate/distortion allocation */
3440 if (! tcd_rateallocate_v2(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
3445 /* Fixed layer allocation */
3446 tcd_rateallocate_fixed_v2(p_tcd);
3453 opj_bool tcd_copy_tile_data ( opj_tcd_v2_t *p_tcd,
3455 OPJ_UINT32 p_src_length )
3457 OPJ_UINT32 i,j,l_data_size = 0;
3458 opj_image_comp_t * l_img_comp = 00;
3459 opj_tcd_tilecomp_v2_t * l_tilec = 00;
3460 OPJ_UINT32 l_size_comp, l_remaining;
3461 OPJ_UINT32 l_nb_elem;
3463 l_data_size = tcd_get_encoded_tile_size(p_tcd);
3464 if (l_data_size != p_src_length) {
3468 l_tilec = p_tcd->tcd_image->tiles->comps;
3469 l_img_comp = p_tcd->image->comps;
3470 for (i=0;i<p_tcd->image->numcomps;++i) {
3471 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3472 l_remaining = l_img_comp->prec & 7; /* (%8) */
3473 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3479 if (l_size_comp == 3) {
3483 switch (l_size_comp) {
3486 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
3487 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3489 if (l_img_comp->sgnd) {
3490 for (j=0;j<l_nb_elem;++j) {
3491 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3495 for (j=0;j<l_nb_elem;++j) {
3496 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
3500 p_src = (OPJ_BYTE*) l_src_ptr;
3505 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3506 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
3508 if (l_img_comp->sgnd) {
3509 for (j=0;j<l_nb_elem;++j) {
3510 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3514 for (j=0;j<l_nb_elem;++j) {
3515 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
3519 p_src = (OPJ_BYTE*) l_src_ptr;
3524 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
3525 OPJ_INT32 * l_dest_ptr = l_tilec->data;
3527 for (j=0;j<l_nb_elem;++j) {
3528 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3531 p_src = (OPJ_BYTE*) l_src_ptr;